Skip to content

Commit 4c91b77

Browse files
committed
privsep: Drain the log when the root process is exiting
So we stand a better chance of printing the exit messages.
1 parent 8abf6b4 commit 4c91b77

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/logerr.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,11 @@ logsetfd(int fd)
395395
#endif
396396
}
397397

398-
int
398+
ssize_t
399399
logreadfd(int fd)
400400
{
401401
struct logctx *ctx = &_logctx;
402-
int len, pri;
402+
int pri;
403403
pid_t pid;
404404
char buf[LOGERR_SYSLOGBUF] = { '\0' };
405405
struct iovec iov[] = {
@@ -411,10 +411,11 @@ logreadfd(int fd)
411411
.msg_iov = iov,
412412
.msg_iovlen = sizeof(iov) / sizeof(iov[0])
413413
};
414+
ssize_t len;
414415

415-
len = (int)recvmsg(fd, &msg, MSG_WAITALL);
416+
len = recvmsg(fd, &msg, MSG_WAITALL);
416417
if (len == -1 || len == 0)
417-
return -1;
418+
return len;
418419
/* Ensure we received the minimum and at least one character to log */
419420
if ((size_t)len < sizeof(pri) + sizeof(pid) + 1 ||
420421
msg.msg_flags & MSG_TRUNC) {

src/logerr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ __printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...);
7979
/* For logging in a chroot using SOCK_SEQPACKET */
8080
int loggetfd(void);
8181
void logsetfd(int);
82-
int logreadfd(int);
82+
ssize_t logreadfd(int);
8383

8484
unsigned int loggetopts(void);
8585
void logsetopts(unsigned int);

src/privsep-root.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ ps_root_readerrorcb(void *arg, unsigned short events)
8989
ssize_t len;
9090
int exit_code = EXIT_FAILURE;
9191

92-
if (events & ELE_HANGUP) {
93-
logerrx("%s: hangup", __func__);
92+
if (events & ELE_HANGUP)
9493
goto out;
95-
}
9694

9795
if (events != ELE_READ)
9896
logerrx("%s: unexpected event 0x%04x", __func__, events);
@@ -982,12 +980,20 @@ ps_root_stop(struct dhcpcd_ctx *ctx)
982980
ctx->eloop == NULL)
983981
return 0;
984982

983+
985984
/* If we are the root process then remove the pidfile */
986-
if (ctx->options & DHCPCD_PRIVSEPROOT &&
987-
!(ctx->options & DHCPCD_TEST))
988-
{
989-
if (unlink(ctx->pidfile) == -1)
985+
if (ctx->options & DHCPCD_PRIVSEPROOT) {
986+
if (!(ctx->options & DHCPCD_TEST) && unlink(ctx->pidfile) == -1)
990987
logerr("%s: unlink: %s", __func__, ctx->pidfile);
988+
989+
/* drain the log */
990+
if (ctx->ps_log_root_fd != -1) {
991+
ssize_t loglen;
992+
993+
do {
994+
loglen = logreadfd(ctx->ps_log_root_fd);
995+
} while (loglen != 0 && loglen != -1);
996+
}
991997
}
992998

993999
/* Only the manager process gets past this point. */

0 commit comments

Comments
 (0)