]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
privsep: Drain the log when the root process is exiting master
authorRoy Marples <roy@marples.name>
Wed, 5 Nov 2025 10:00:55 +0000 (10:00 +0000)
committerRoy Marples <roy@marples.name>
Wed, 5 Nov 2025 10:00:55 +0000 (10:00 +0000)
So we stand a better chance of printing the exit messages.

src/logerr.c
src/logerr.h
src/privsep-root.c

index 7e8038bda003283c65af0e324076934841c63852..e04493d33a3f6c3b30970cc831736c4b4ef337c7 100644 (file)
@@ -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) {
index a6507c26e9178ff386bcccb714e604ee33ba576d..b6d708cac7dae7fd46522d4cfb38b7414b3f2dba 100644 (file)
@@ -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);
index 890daffc4cc3ff16f7a7b6e1fc93b6422bb76e57..3f4930cdb588ad66ca5f28db07150036430f78db 100644 (file)
@@ -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. */