summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2015-03-13 23:06:29 +0100
committerThomas Haller <thaller@redhat.com>2015-03-20 11:43:30 +0100
commit12ad2c7fe7ebd94c40edd9a28b81ce27563bb2e3 (patch)
tree2a0b873b2a3682e618c8d932ee8d29a427e8d962
parent29718fcaa43f52606e4f0b3addbca3f7afb2a224 (diff)
downloadNetworkManager-12ad2c7fe7ebd94c40edd9a28b81ce27563bb2e3.tar.gz
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.
-rw-r--r--src/main-utils.c44
-rw-r--r--src/main-utils.h2
-rw-r--r--src/main.c4
-rw-r--r--src/nm-iface-helper.c6
4 files changed, 24 insertions, 32 deletions
diff --git a/src/main-utils.c b/src/main-utils.c
index 4daa0eef52..564254dc91 100644
--- a/src/main-utils.c
+++ b/src/main-utils.c
@@ -34,6 +34,7 @@
#include <glib-unix.h>
#include <gmodule.h>
+#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 ();
- if (!g_file_get_contents (pidfile, &contents, &len, NULL))
- return FALSE;
+ g_return_if_fail (prgname);
+ if (!pidfile || !*pidfile)
+ return;
+
+ if (!g_file_get_contents (pidfile, &contents, &len, NULL))
+ 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 be3d08626f..e928e20b69 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 35a47b9502..77866a473d 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 3a3a71157e..8fc420df3d 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) {