From 4c396b6e2c12dbf3901e23551d16ef8f73dceba6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:06:29 +0100 Subject: [PATCH] main: refactor nm_main_utils_check_pidfile() to exit directly on failure And rename the function to nm_main_utils_ensure_not_running_pidfile() to match the other _ensure_ functions that exit(1). Also no longer pass @name to nm_main_utils_ensure_not_running_pidfile() and use g_get_prgname() instead. nm_main_utils_ensure_not_running_pidfile() checks that the running process has the same program name, so this changes behavior if the user renamed the binary. Before, we would check whether the running process is named 'NetworkManager' ('nm-iface-helper'). Now we check whether the process has the same name as the current process. This means, that if you rename the binary to 'NetworkManager2' we would now only detect a conflicting 'NetworkManager2'. Before we would only detect conflicting 'NetworkManager' binaries. (cherry picked from commit 12ad2c7fe7ebd94c40edd9a28b81ce27563bb2e3) --- src/main-utils.c | 44 +++++++++++++++++++++---------------------- src/main-utils.h | 2 +- src/main.c | 4 +--- src/nm-iface-helper.c | 6 +----- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index bf8eb4b329..f6a2a60e9e 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -33,6 +33,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "main-utils.h" #include "nm-posix-signals.h" #include "nm-logging.h" @@ -163,59 +164,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 18e7e534bd..3d64535768 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 28d8defa64..6c1fd9960d 100644 --- a/src/main.c +++ b/src/main.c @@ -312,9 +312,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_new (&error); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 0c39d8fa5b..74db5b0ec0 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -381,11 +381,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) {