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:
@@ -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 ();
|
||||
|
||||
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
|
||||
|
@@ -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,
|
||||
|
@@ -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);
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user