]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
daemon: when daemonizing, close stdout/stderr
authorVincent Bernat <vincent@bernat.im>
Sat, 21 Jan 2017 12:48:09 +0000 (13:48 +0100)
committerVincent Bernat <vincent@bernat.im>
Sat, 21 Jan 2017 12:50:49 +0000 (13:50 +0100)
Otherwise, when used with systemd/upstart, we write logs in two
different places.

NEWS
src/daemon/lldpd.c

diff --git a/NEWS b/NEWS
index 9ca6342a0fd287ee99e64592a019c445d711e2b7..d3e773b35f173c017b676381e741fd72f105e5aa 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+lldpd (0.9.7)
+  * Changes:
+    + When logging to syslog and daemonizing, don't log to stderr.
+
 lldpd (0.9.6)
   * Change:
     + Add a compile-time option to restore pre-0.9.2 JSON format (when
index d0b47b5fb4df8a2cdd7295ac92843765ee0ae782..0db446935c6ff5204ae51e87b9223147a027c979 100644 (file)
@@ -1640,7 +1640,17 @@ lldpd_main(int argc, char *argv[], char *envp[])
 
        log_init(use_syslog, debug, __progname);
        tzset();                /* Get timezone info before chroot */
-
+       if (use_syslog && daemonize) {
+               /* So, we use syslog and we daemonize (or we are started by
+                * upstart/systemd). No need to continue writing to stdout. */
+               int fd;
+               if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
+                       dup2(fd, STDIN_FILENO);
+                       dup2(fd, STDOUT_FILENO);
+                       dup2(fd, STDERR_FILENO);
+                       if (fd > 2) close(fd);
+               }
+       }
        log_debug("main", "lldpd " PACKAGE_VERSION " starting...");
        version_check();
 
@@ -1702,7 +1712,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
                int pid;
                char *spid;
                log_info("main", "going into background");
-               if (daemon(0, 0) != 0)
+               if (daemon(0, 1) != 0)
                        fatal("main", "failed to detach daemon");
                if ((pid = open(pidfile,
                            O_TRUNC | O_CREAT | O_WRONLY, 0666)) == -1)