]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
daemon: wait a bit later to daemonize
authorVincent Bernat <vincent@bernat.im>
Wed, 28 Sep 2016 20:29:49 +0000 (22:29 +0200)
committerVincent Bernat <vincent@bernat.im>
Wed, 28 Sep 2016 20:29:49 +0000 (22:29 +0200)
This way, some additional errors may be advertised in a better way.

src/daemon/lldpd.c

index 72968bfd9a0fdb830967c94bca4d4ddbad21661d..1ab76663a02059e35f46c54bd2dfec4e619137a2 100644 (file)
@@ -1701,30 +1701,6 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        fatal("main", "unable to spawn lldpcli");
        }
 
-       /* Daemonization, unless started by upstart, systemd or launchd or debug */
-#ifndef HOST_OS_OSX
-       if (daemonize &&
-           !lldpd_started_by_upstart() && !lldpd_started_by_systemd()) {
-               int pid;
-               char *spid;
-               log_debug("main", "daemonize");
-               if (daemon(0, 0) != 0)
-                       fatal("main", "failed to detach daemon");
-               if ((pid = open(pidfile,
-                           O_TRUNC | O_CREAT | O_WRONLY, 0666)) == -1)
-                       fatal("main", "unable to open pid file " LLDPD_PID_FILE
-                           " (or the specified one)");
-               if (asprintf(&spid, "%d\n", getpid()) == -1)
-                       fatal("main", "unable to create pid file " LLDPD_PID_FILE
-                           " (or the specified one)");
-               if (write(pid, spid, strlen(spid)) == -1)
-                       fatal("main", "unable to write pid file " LLDPD_PID_FILE
-                           " (or the specified one)");
-               free(spid);
-               close(pid);
-       }
-#endif
-
        /* Try to read system information from /etc/os-release if possible.
           Fall back to lsb_release for compatibility. */
        log_debug("main", "get OS/LSB release information");
@@ -1733,13 +1709,6 @@ lldpd_main(int argc, char *argv[], char *envp[])
                lsb_release = lldpd_get_lsb_release();
        }
 
-       log_debug("main", "initialize privilege separation");
-#ifdef ENABLE_PRIVSEP
-       priv_init(PRIVSEP_CHROOT, ctl, uid, gid);
-#else
-       priv_init(PRIVSEP_CHROOT, ctl, 0, 0);
-#endif
-
        /* Initialization of global configuration */
        if ((cfg = (struct lldpd *)
            calloc(1, sizeof(struct lldpd))) == NULL)
@@ -1851,6 +1820,37 @@ lldpd_main(int argc, char *argv[], char *envp[])
        TAILQ_INSERT_TAIL(&cfg->g_chassis, lchassis, c_entries);
        lchassis->c_refcount++; /* We should always keep a reference to local chassis */
 
+       /* Daemonization, unless started by upstart, systemd or launchd or debug */
+#ifndef HOST_OS_OSX
+       if (daemonize &&
+           !lldpd_started_by_upstart() && !lldpd_started_by_systemd()) {
+               int pid;
+               char *spid;
+               log_debug("main", "daemonize");
+               if (daemon(0, 0) != 0)
+                       fatal("main", "failed to detach daemon");
+               if ((pid = open(pidfile,
+                           O_TRUNC | O_CREAT | O_WRONLY, 0666)) == -1)
+                       fatal("main", "unable to open pid file " LLDPD_PID_FILE
+                           " (or the specified one)");
+               if (asprintf(&spid, "%d\n", getpid()) == -1)
+                       fatal("main", "unable to create pid file " LLDPD_PID_FILE
+                           " (or the specified one)");
+               if (write(pid, spid, strlen(spid)) == -1)
+                       fatal("main", "unable to write pid file " LLDPD_PID_FILE
+                           " (or the specified one)");
+               free(spid);
+               close(pid);
+       }
+#endif
+
+       log_debug("main", "initialize privilege separation");
+#ifdef ENABLE_PRIVSEP
+       priv_init(PRIVSEP_CHROOT, ctl, uid, gid);
+#else
+       priv_init(PRIVSEP_CHROOT, ctl, 0, 0);
+#endif
+
        /* Main loop */
        log_debug("main", "start main loop");
        levent_loop(cfg);