From 52767c4d8ebce22be5bc1cb3c0fe3061d67d8d14 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 21 Jan 2017 13:48:09 +0100 Subject: [PATCH] daemon: when daemonizing, close stdout/stderr Otherwise, when used with systemd/upstart, we write logs in two different places. --- NEWS | 4 ++++ src/daemon/lldpd.c | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 9ca6342a..d3e773b3 100644 --- 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 diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index d0b47b5f..0db44693 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -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) -- 2.39.5