diff --git a/src/main-utils.c b/src/main-utils.c index 4daa0eef5..564254dc9 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -34,6 +34,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "main-utils.h" #include "nm-logging.h" @@ -119,59 +120,56 @@ nm_main_utils_ensure_rundir () } /** - * nm_main_utils_check_pidfile: + * nm_main_utils_ensure_not_running_pidfile: * @pidfile: the pid file - * @name: the process name * * Checks whether the pidfile already exists and contains PID of a running * process. * - * Returns: %TRUE if the specified pidfile already exists and contains the PID - * of a running process named @name, or %FALSE if not + * Exits with code 1 if a conflicting process is running. */ -gboolean -nm_main_utils_check_pidfile (const char *pidfile, const char *name) +void +nm_main_utils_ensure_not_running_pidfile (const char *pidfile) { - char *contents = NULL; + gs_free char *contents = NULL; + gs_free char *proc_cmdline = NULL; gsize len = 0; glong pid; - char *proc_cmdline = NULL; - gboolean nm_running = FALSE; const char *process_name; + const char *prgname = g_get_prgname (); + + g_return_if_fail (prgname); + + if (!pidfile || !*pidfile) + return; if (!g_file_get_contents (pidfile, &contents, &len, NULL)) - return FALSE; - + return; if (len <= 0) - goto done; + return; errno = 0; pid = strtol (contents, NULL, 10); if (pid <= 0 || pid > 65536 || errno) - goto done; + return; - g_free (contents); + g_clear_pointer (&contents, g_free); proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid); if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL)) - goto done; + return; process_name = strrchr (contents, '/'); if (process_name) process_name++; else process_name = contents; - if (strcmp (process_name, name) == 0) { + if (strcmp (process_name, prgname) == 0) { /* Check that the process exists */ if (kill (pid, 0) == 0) { - fprintf (stderr, _("%s is already running (pid %ld)\n"), name, pid); - nm_running = TRUE; + fprintf (stderr, _("%s is already running (pid %ld)\n"), prgname, pid); + exit (1); } } - -done: - g_free (proc_cmdline); - g_free (contents); - return nm_running; } gboolean diff --git a/src/main-utils.h b/src/main-utils.h index be3d08626..e928e20b6 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -29,7 +29,7 @@ void nm_main_utils_ensure_rundir (void); gboolean nm_main_utils_write_pidfile (const char *pidfile); -gboolean nm_main_utils_check_pidfile (const char *pidfile, const char *name); +void nm_main_utils_ensure_not_running_pidfile (const char *pidfile); gboolean nm_main_utils_early_setup (const char *progname, int *argc, diff --git a/src/main.c b/src/main.c index 35a47b950..77866a473 100644 --- a/src/main.c +++ b/src/main.c @@ -318,9 +318,7 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); - /* check pid file */ - if (nm_main_utils_check_pidfile (global_opt.pidfile, "NetworkManager")) - exit (1); + nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); /* Read the config file and CLI overrides */ config = nm_config_setup (config_cli, &error); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 3a3a71157..8fc420df3 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -378,11 +378,7 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); - g_assert (pidfile); - - /* check pid file */ - if (nm_main_utils_check_pidfile (pidfile, "nm-iface-helper")) - exit (1); + nm_main_utils_ensure_not_running_pidfile (pidfile); if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) {