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.
This commit is contained in:
Thomas Haller
2015-03-13 23:06:29 +01:00
parent 29718fcaa4
commit 12ad2c7fe7
4 changed files with 24 additions and 32 deletions

View File

@@ -34,6 +34,7 @@
#include <glib-unix.h> #include <glib-unix.h>
#include <gmodule.h> #include <gmodule.h>
#include "gsystem-local-alloc.h"
#include "main-utils.h" #include "main-utils.h"
#include "nm-logging.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 * @pidfile: the pid file
* @name: the process name
* *
* Checks whether the pidfile already exists and contains PID of a running * Checks whether the pidfile already exists and contains PID of a running
* process. * process.
* *
* Returns: %TRUE if the specified pidfile already exists and contains the PID * Exits with code 1 if a conflicting process is running.
* of a running process named @name, or %FALSE if not
*/ */
gboolean void
nm_main_utils_check_pidfile (const char *pidfile, const char *name) 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; gsize len = 0;
glong pid; glong pid;
char *proc_cmdline = NULL;
gboolean nm_running = FALSE;
const char *process_name; 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)) if (!g_file_get_contents (pidfile, &contents, &len, NULL))
return FALSE; return;
if (len <= 0) if (len <= 0)
goto done; return;
errno = 0; errno = 0;
pid = strtol (contents, NULL, 10); pid = strtol (contents, NULL, 10);
if (pid <= 0 || pid > 65536 || errno) 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); proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid);
if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL)) if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL))
goto done; return;
process_name = strrchr (contents, '/'); process_name = strrchr (contents, '/');
if (process_name) if (process_name)
process_name++; process_name++;
else else
process_name = contents; process_name = contents;
if (strcmp (process_name, name) == 0) { if (strcmp (process_name, prgname) == 0) {
/* Check that the process exists */ /* Check that the process exists */
if (kill (pid, 0) == 0) { if (kill (pid, 0) == 0) {
fprintf (stderr, _("%s is already running (pid %ld)\n"), name, pid); fprintf (stderr, _("%s is already running (pid %ld)\n"), prgname, pid);
nm_running = TRUE; exit (1);
} }
} }
done:
g_free (proc_cmdline);
g_free (contents);
return nm_running;
} }
gboolean gboolean

View File

@@ -29,7 +29,7 @@ void nm_main_utils_ensure_rundir (void);
gboolean nm_main_utils_write_pidfile (const char *pidfile); 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, gboolean nm_main_utils_early_setup (const char *progname,
int *argc, int *argc,

View File

@@ -318,9 +318,7 @@ main (int argc, char *argv[])
nm_main_utils_ensure_rundir (); nm_main_utils_ensure_rundir ();
/* check pid file */ nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile);
if (nm_main_utils_check_pidfile (global_opt.pidfile, "NetworkManager"))
exit (1);
/* Read the config file and CLI overrides */ /* Read the config file and CLI overrides */
config = nm_config_setup (config_cli, &error); config = nm_config_setup (config_cli, &error);

View File

@@ -378,11 +378,7 @@ main (int argc, char *argv[])
nm_main_utils_ensure_rundir (); nm_main_utils_ensure_rundir ();
pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex);
g_assert (pidfile); nm_main_utils_ensure_not_running_pidfile (pidfile);
/* check pid file */
if (nm_main_utils_check_pidfile (pidfile, "nm-iface-helper"))
exit (1);
if (global_opt.become_daemon && !global_opt.debug) { if (global_opt.become_daemon && !global_opt.debug) {
if (daemon (0, 0) < 0) { if (daemon (0, 0) < 0) {