dhcp: refactor nm_dhcp_client_stop_existing()
This commit is contained in:
@@ -597,51 +597,49 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self,
|
|||||||
void
|
void
|
||||||
nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name)
|
nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name)
|
||||||
{
|
{
|
||||||
char *pid_contents = NULL, *proc_contents = NULL, *proc_path = NULL;
|
guint64 start_time;
|
||||||
long int tmp;
|
pid_t pid, ppid;
|
||||||
|
const char *exe;
|
||||||
|
char proc_path[NM_STRLEN ("/proc/%lu/cmdline") + 100];
|
||||||
|
gs_free char *pid_contents = NULL, *proc_contents = NULL;
|
||||||
|
|
||||||
/* Check for an existing instance and stop it */
|
/* Check for an existing instance and stop it */
|
||||||
if (!g_file_get_contents (pid_file, &pid_contents, NULL, NULL))
|
if (!g_file_get_contents (pid_file, &pid_contents, NULL, NULL))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
errno = 0;
|
pid = _nm_utils_ascii_str_to_int64 (pid_contents, 10, 1, G_MAXINT64, 0);
|
||||||
tmp = strtol (pid_contents, NULL, 10);
|
if (pid <= 0)
|
||||||
if ((errno == 0) && (tmp > 1)) {
|
goto out;
|
||||||
guint64 start_time;
|
|
||||||
const char *exe;
|
|
||||||
pid_t ppid;
|
|
||||||
|
|
||||||
/* Ensure the process is a DHCP client */
|
start_time = nm_utils_get_start_time_for_pid (pid, NULL, &ppid);
|
||||||
start_time = nm_utils_get_start_time_for_pid (tmp, NULL, &ppid);
|
if (start_time == 0)
|
||||||
proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp);
|
goto out;
|
||||||
if ( start_time
|
|
||||||
&& g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) {
|
|
||||||
exe = strrchr (proc_contents, '/');
|
|
||||||
if (exe)
|
|
||||||
exe++;
|
|
||||||
else
|
|
||||||
exe = proc_contents;
|
|
||||||
|
|
||||||
if (!strcmp (exe, binary_name)) {
|
nm_sprintf_buf (proc_path, "/proc/%lu/cmdline", (long unsigned) pid);
|
||||||
if (ppid == getpid ()) {
|
if (!g_file_get_contents (proc_path, &proc_contents, NULL, NULL))
|
||||||
/* the process is our own child. */
|
goto out;
|
||||||
nm_utils_kill_child_sync (tmp, SIGTERM, LOGD_DHCP, "dhcp-client", NULL, 1000 / 2, 1000 / 20);
|
|
||||||
} else {
|
exe = strrchr (proc_contents, '/');
|
||||||
nm_utils_kill_process_sync (tmp, start_time, SIGTERM, LOGD_DHCP,
|
if (exe)
|
||||||
"dhcp-client", 1000 / 2, 1000 / 20, 2000);
|
exe++;
|
||||||
}
|
else
|
||||||
}
|
exe = proc_contents;
|
||||||
}
|
if (!nm_streq0 (exe, binary_name))
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
if (ppid == getpid ()) {
|
||||||
|
/* the process is our own child. */
|
||||||
|
nm_utils_kill_child_sync (pid, SIGTERM, LOGD_DHCP, "dhcp-client", NULL, 1000 / 2, 1000 / 20);
|
||||||
|
} else {
|
||||||
|
nm_utils_kill_process_sync (pid, start_time, SIGTERM, LOGD_DHCP,
|
||||||
|
"dhcp-client", 1000 / 2, 1000 / 20, 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
if (remove (pid_file) == -1) {
|
if (remove (pid_file) == -1) {
|
||||||
nm_log_dbg (LOGD_DHCP, "dhcp: could not remove pid file \"%s\": %d (%s)",
|
nm_log_dbg (LOGD_DHCP, "dhcp: could not remove pid file \"%s\": %d (%s)",
|
||||||
pid_file, errno, g_strerror (errno));
|
pid_file, errno, g_strerror (errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (proc_path);
|
|
||||||
g_free (pid_contents);
|
|
||||||
g_free (proc_contents);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Reference in New Issue
Block a user