From: Roy Marples Date: Wed, 5 Nov 2025 10:00:55 +0000 (+0000) Subject: privsep: Drain the log when the root process is exiting X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=HEAD;p=thirdparty%2Fdhcpcd.git privsep: Drain the log when the root process is exiting So we stand a better chance of printing the exit messages. --- diff --git a/src/logerr.c b/src/logerr.c index 7e8038bd..e04493d3 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -395,11 +395,11 @@ logsetfd(int fd) #endif } -int +ssize_t logreadfd(int fd) { struct logctx *ctx = &_logctx; - int len, pri; + int pri; pid_t pid; char buf[LOGERR_SYSLOGBUF] = { '\0' }; struct iovec iov[] = { @@ -411,10 +411,11 @@ logreadfd(int fd) .msg_iov = iov, .msg_iovlen = sizeof(iov) / sizeof(iov[0]) }; + ssize_t len; - len = (int)recvmsg(fd, &msg, MSG_WAITALL); + len = recvmsg(fd, &msg, MSG_WAITALL); if (len == -1 || len == 0) - return -1; + return len; /* Ensure we received the minimum and at least one character to log */ if ((size_t)len < sizeof(pri) + sizeof(pid) + 1 || msg.msg_flags & MSG_TRUNC) { diff --git a/src/logerr.h b/src/logerr.h index a6507c26..b6d708ca 100644 --- a/src/logerr.h +++ b/src/logerr.h @@ -79,7 +79,7 @@ __printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...); /* For logging in a chroot using SOCK_SEQPACKET */ int loggetfd(void); void logsetfd(int); -int logreadfd(int); +ssize_t logreadfd(int); unsigned int loggetopts(void); void logsetopts(unsigned int); diff --git a/src/privsep-root.c b/src/privsep-root.c index 890daffc..3f4930cd 100644 --- a/src/privsep-root.c +++ b/src/privsep-root.c @@ -89,10 +89,8 @@ ps_root_readerrorcb(void *arg, unsigned short events) ssize_t len; int exit_code = EXIT_FAILURE; - if (events & ELE_HANGUP) { - logerrx("%s: hangup", __func__); + if (events & ELE_HANGUP) goto out; - } if (events != ELE_READ) logerrx("%s: unexpected event 0x%04x", __func__, events); @@ -982,12 +980,20 @@ ps_root_stop(struct dhcpcd_ctx *ctx) ctx->eloop == NULL) return 0; + /* If we are the root process then remove the pidfile */ - if (ctx->options & DHCPCD_PRIVSEPROOT && - !(ctx->options & DHCPCD_TEST)) - { - if (unlink(ctx->pidfile) == -1) + if (ctx->options & DHCPCD_PRIVSEPROOT) { + if (!(ctx->options & DHCPCD_TEST) && unlink(ctx->pidfile) == -1) logerr("%s: unlink: %s", __func__, ctx->pidfile); + + /* drain the log */ + if (ctx->ps_log_root_fd != -1) { + ssize_t loglen; + + do { + loglen = logreadfd(ctx->ps_log_root_fd); + } while (loglen != 0 && loglen != -1); + } } /* Only the manager process gets past this point. */