diff --git a/ChangeLog b/ChangeLog index f66571c1f7..855042cb8b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,8 @@ * dispatcher-daemon/NetworkManagerDispatcher.c, src/NetworkManager.c: Open the pid file O_TRUNC, so if it already exists we truncate it to zero length. Also, be more verbose about warnings generated during - writing out the pid file. + writing out the pid file. Finally, always write out the pid file if + in daemon mode. Use "--pid-file" to override the default. 2006-02-27 Robert Love diff --git a/dispatcher-daemon/Makefile.am b/dispatcher-daemon/Makefile.am index 470e7584d6..8c4c3051e7 100644 --- a/dispatcher-daemon/Makefile.am +++ b/dispatcher-daemon/Makefile.am @@ -4,14 +4,15 @@ NULL= sbin_PROGRAMS = NetworkManagerDispatcher -NetworkManagerDispatcher_CPPFLAGS = \ - $(DBUS_CFLAGS) \ - $(GTHREAD_CFLAGS) \ - -Wall \ - -DDBUS_API_SUBJECT_TO_CHANGE \ - -DBINDIR=\"$(bindir)\" \ - -DDATADIR=\"$(datadir)\" \ - -DSYSCONFDIR=\"$(sysconfdir)\" \ +NetworkManagerDispatcher_CPPFLAGS = \ + $(DBUS_CFLAGS) \ + $(GTHREAD_CFLAGS) \ + -Wall \ + -DDBUS_API_SUBJECT_TO_CHANGE \ + -DBINDIR=\"$(bindir)\" \ + -DDATADIR=\"$(datadir)\" \ + -DSYSCONFDIR=\"$(sysconfdir)\" \ + -DLOCALSTATEDIR=\"$(localstatedir)\" \ $(NULL) NetworkManagerDispatcher_SOURCES = NetworkManagerDispatcher.c diff --git a/dispatcher-daemon/NetworkManagerDispatcher.c b/dispatcher-daemon/NetworkManagerDispatcher.c index 37491b806c..9f6841285b 100644 --- a/dispatcher-daemon/NetworkManagerDispatcher.c +++ b/dispatcher-daemon/NetworkManagerDispatcher.c @@ -48,7 +48,9 @@ enum NMDAction typedef enum NMDAction NMDAction; -#define NM_SCRIPT_DIR SYSCONFDIR"/NetworkManager/dispatcher.d" +#define NM_SCRIPT_DIR SYSCONFDIR"/NetworkManager/dispatcher.d" + +#define NMD_DEFAULT_PID_FILE LOCALSTATEDIR"/run/NetworkManagerDispatcher.pid" /* @@ -309,6 +311,7 @@ int main (int argc, char *argv[]) GMainLoop * loop = NULL; DBusConnection *connection = NULL; char * pidfile = NULL; + char * user_pidfile = NULL; /* Parse options */ while (1) @@ -340,7 +343,7 @@ int main (int argc, char *argv[]) else if (strcmp (opt, "no-daemon") == 0) become_daemon = FALSE; else if (strcmp (opt, "pid-file") == 0) - pidfile = g_strdup (optarg); + user_pidfile = g_strdup (optarg); else { nmd_print_usage (); @@ -357,19 +360,22 @@ int main (int argc, char *argv[]) openlog("NetworkManagerDispatcher", (become_daemon) ? LOG_CONS : LOG_CONS | LOG_PERROR, (become_daemon) ? LOG_DAEMON : LOG_USER); - if (become_daemon && daemon (FALSE, FALSE) < 0) + if (become_daemon) { - nm_warning ("NetworkManagerDispatcher could not daemonize: %s", strerror (errno)); - exit (1); + if (daemon (FALSE, FALSE) < 0) + { + nm_warning ("NetworkManagerDispatcher could not daemonize: %s", strerror (errno)); + exit (1); + } + + pidfile = user_pidfile ? user_pidfile : NMD_DEFAULT_PID_FILE; + write_pidfile (pidfile); } g_type_init (); if (!g_thread_supported ()) g_thread_init (NULL); - if (pidfile) - write_pidfile (pidfile); - /* Connect to the NetworkManager dbus service and run the main loop */ if ((connection = nmd_dbus_init ())) { @@ -380,7 +386,7 @@ int main (int argc, char *argv[]) /* Clean up pidfile */ if (pidfile) unlink (pidfile); - g_free (pidfile); + g_free (user_pidfile); return 0; } diff --git a/src/NetworkManager.c b/src/NetworkManager.c index c9a31139c5..4e0e63ac33 100644 --- a/src/NetworkManager.c +++ b/src/NetworkManager.c @@ -53,6 +53,8 @@ #define NM_WIRELESS_LINK_STATE_POLL_INTERVAL (5 * 1000) +#define NM_DEFAULT_PID_FILE LOCALSTATEDIR"/run/NetworkManager.pid" + /* * Globals */ @@ -717,6 +719,7 @@ int main( int argc, char *argv[] ) gboolean enable_test_devices = FALSE; char * owner; char * pidfile = NULL; + char * user_pidfile = NULL; if (getuid () != 0) { @@ -757,7 +760,7 @@ int main( int argc, char *argv[] ) else if (strcmp (opt, "enable-test-devices") == 0) enable_test_devices = TRUE; else if (strcmp (opt, "pid-file") == 0) - pidfile = g_strdup (optarg); + user_pidfile = g_strdup (optarg); break; default: @@ -767,14 +770,20 @@ int main( int argc, char *argv[] ) } } - if (become_daemon && daemon (0, 0) < 0) + if (become_daemon) { - int saved_errno; + if (daemon (0, 0) < 0) + { + int saved_errno; - saved_errno = errno; - nm_error ("NetworkManager could not daemonize: %s [error %u]", - g_strerror (saved_errno), saved_errno); - exit (EXIT_FAILURE); + saved_errno = errno; + nm_error ("NetworkManager could not daemonize: %s [error %u]", + g_strerror (saved_errno), saved_errno); + exit (EXIT_FAILURE); + } + + pidfile = user_pidfile ? user_pidfile : NM_DEFAULT_PID_FILE; + write_pidfile (pidfile); } g_type_init (); @@ -782,9 +791,6 @@ int main( int argc, char *argv[] ) g_thread_init (NULL); dbus_g_thread_init (); - if (pidfile) - write_pidfile (pidfile); - nm_logging_setup (become_daemon); nm_info ("starting..."); @@ -858,7 +864,7 @@ int main( int argc, char *argv[] ) /* Clean up pidfile */ if (pidfile) unlink (pidfile); - g_free (pidfile); + g_free (user_pidfile); exit (0); }