diff options
author | Thomas Haller <thaller@redhat.com> | 2016-04-22 11:04:57 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-04-22 12:58:20 +0200 |
commit | cf0c300058722a01898770f1e061ab2dfebf3b71 (patch) | |
tree | feeb21cc2ee9516ffbd1cb8c4417986454bc4f03 | |
parent | d061025f1eb04191270f9e1f2c7ee005f912fc59 (diff) | |
download | NetworkManager-cf0c300058722a01898770f1e061ab2dfebf3b71.tar.gz |
dhcp: refactor nm_dhcp_client_stop_existing()
-rw-r--r-- | src/dhcp-manager/nm-dhcp-client.c | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c index b8161ab5d3..7e72233670 100644 --- a/src/dhcp-manager/nm-dhcp-client.c +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -597,51 +597,49 @@ nm_dhcp_client_start_ip6 (NMDhcpClient *self, void nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name) { - char *pid_contents = NULL, *proc_contents = NULL, *proc_path = NULL; - long int tmp; + guint64 start_time; + 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 */ if (!g_file_get_contents (pid_file, &pid_contents, NULL, NULL)) return; - errno = 0; - tmp = strtol (pid_contents, NULL, 10); - if ((errno == 0) && (tmp > 1)) { - 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 (tmp, NULL, &ppid); - proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp); - 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)) { - if (ppid == getpid ()) { - /* the process is our own child. */ - nm_utils_kill_child_sync (tmp, SIGTERM, LOGD_DHCP, "dhcp-client", NULL, 1000 / 2, 1000 / 20); - } else { - nm_utils_kill_process_sync (tmp, start_time, SIGTERM, LOGD_DHCP, - "dhcp-client", 1000 / 2, 1000 / 20, 2000); - } - } - } + pid = _nm_utils_ascii_str_to_int64 (pid_contents, 10, 1, G_MAXINT64, 0); + if (pid <= 0) + goto out; + + start_time = nm_utils_get_start_time_for_pid (pid, NULL, &ppid); + if (start_time == 0) + goto out; + + nm_sprintf_buf (proc_path, "/proc/%lu/cmdline", (long unsigned) pid); + if (!g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) + goto out; + + exe = strrchr (proc_contents, '/'); + if (exe) + 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) { nm_log_dbg (LOGD_DHCP, "dhcp: could not remove pid file \"%s\": %d (%s)", pid_file, errno, g_strerror (errno)); } - - g_free (proc_path); - g_free (pid_contents); - g_free (proc_contents); } void |