From: Roy Marples Date: Tue, 30 Jun 2020 20:33:24 +0000 (+0100) Subject: dhcpcd: Turn off stderr logging rather than closing stderr X-Git-Tag: v9.1.3~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56485df7765c0909a86cc7b2a0d56f797abf7f28;p=thirdparty%2Fdhcpcd.git dhcpcd: Turn off stderr logging rather than closing stderr And only turn it off when stderr is a tty. A better approach might to open an fd just to log to and redirect stdout/stderr to /dev/null alongside stdin at program start, but that's more code. --- diff --git a/src/dhcpcd.c b/src/dhcpcd.c index e917b6c4..67f5a9ed 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -339,6 +339,7 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx) return 0; #else int i; + unsigned int logopts = loggetopts(); if (ctx->options & DHCPCD_DAEMONISE && !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP))) @@ -359,7 +360,7 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx) return; /* Don't use loginfo because this makes no sense in a log. */ - if (!(loggetopts() & LOGERR_QUIET)) + if (!(logopts & LOGERR_QUIET)) (void)fprintf(stderr, "forked to background, child pid %d\n", getpid()); i = EXIT_SUCCESS; @@ -369,16 +370,10 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx) eloop_event_delete(ctx->eloop, ctx->fork_fd); close(ctx->fork_fd); ctx->fork_fd = -1; -#ifdef PRIVSEP - if (IN_PRIVSEP(ctx)) { - fclose(stdout); - fclose(stderr); - } else -#endif - { - if (freopen(_PATH_DEVNULL, "w", stdout) == NULL || - freopen(_PATH_DEVNULL, "w", stderr) == NULL) - logerr("%s: freopen", __func__); + + if (isatty(STDERR_FILENO)) { + logopts &= ~LOGERR_ERR; + logsetopts(logopts); } #endif }