diff -ur a b --- a/src/dhcdbd.c 2007-07-08 17:31:50.000000000 +0100 +++ b/src/dhcdbd.c 2007-07-08 17:38:39.000000000 +0100 @@ -2778,9 +2778,6 @@ char path[1024]; int fd, l; - if (dhcdbd_daemonize && (daemon (0, 0) == -1)) - return errno; - openlog ("dhcdbd", LOG_NDELAY | LOG_CONS, LOG_USER); dbus = dbus_svc_init (bus, dhcdbd_destination, dhcdbd_log, 0L); @@ -2821,6 +2818,17 @@ (dbus, dhcdbd_object_path, path, dhcdbd_if_subscribe, dhco)) return (1); + /* Daemonize and write the pidfile write away, to avoid races */ + if (dhcdbd_daemonize && (daemon (0, 0) == -1)) + return errno; + unlink (DHCDBD_PID_FILE); + if ((fd = open (DHCDBD_PID_FILE, O_WRONLY | O_CREAT)) == -1) + exit (errno); + l = sprintf (path, "%u", getpid ()); + l = write (fd, path, l); + fsync (fd); + close (fd); + memset (&sa, '\0', sizeof (struct sigaction)); sa.sa_sigaction = dhc_reaper; sa.sa_flags = SA_SIGINFO; /* NO RESTART, NO DEFER, CLDSTOP */ @@ -2828,13 +2836,7 @@ dhcdbd_log ("sigaction failed: %s\n", strerror (errno)); return (1); } - unlink (DHCDBD_PID_FILE); - if ((fd = open (DHCDBD_PID_FILE, O_WRONLY | O_CREAT)) == -1) - exit (errno); - l = sprintf (path, "%u", getpid ()); - l = write (fd, path, l); - fsync (fd); - close (fd); + dhcdbd_log ("Started up."); dbus_svc_main_loop (dbus, dhcdbd_work); dhcdbd_debug ("Main Loop Exited.");