From: Todd C. Miller Date: Fri, 6 Apr 2012 19:20:16 +0000 (-0400) Subject: Log the process id in the debug file output. Since we don't want X-Git-Tag: SUDO_1_8_5~1^2~77 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c8ce3a0a854054576383e45c1f65c707315b628a;p=sudo Log the process id in the debug file output. Since we don't want to keep calling getpid(), stash the value at init time and when we fork(). --- diff --git a/common/sudo_debug.c b/common/sudo_debug.c index c2b12cdef..f41fa8b4c 100644 --- a/common/sudo_debug.c +++ b/common/sudo_debug.c @@ -114,6 +114,8 @@ const char *const sudo_debug_subsystems[] = { static int sudo_debug_settings[NUM_SUBSYSTEMS]; static int sudo_debug_fd = -1; static int sudo_debug_mode; +static char sudo_debug_pidstr[(((sizeof(int) * 8) + 2) / 3) + 3]; +static size_t sudo_debug_pidlen; extern sudo_conv_t sudo_conv; @@ -174,9 +176,27 @@ int sudo_debug_init(const char *debugfile, const char *settings) } efree(buf); + (void)snprintf(sudo_debug_pidstr, sizeof(sudo_debug_pidstr), "[%d] ", + (int)getpid()); + sudo_debug_pidlen = strlen(sudo_debug_pidstr); + return 1; } +pid_t +sudo_debug_fork(void) +{ + pid_t pid; + + if ((pid = fork()) == 0) { + (void)snprintf(sudo_debug_pidstr, sizeof(sudo_debug_pidstr), "[%d] ", + (int)getpid()); + sudo_debug_pidlen = strlen(sudo_debug_pidstr); + } + + return pid; +} + void sudo_debug_enter(const char *func, const char *file, int line, int subsys) @@ -298,11 +318,11 @@ sudo_debug_write_file(const char *func, const char *file, int lineno, int iovcnt = 4; bool need_newline = false; - /* Prepend program name with trailing space. */ + /* Prepend program name and pid with a trailing space. */ iov[1].iov_base = (char *)getprogname(); iov[1].iov_len = strlen(iov[1].iov_base); - iov[2].iov_base = " "; - iov[2].iov_len = 1; + iov[2].iov_base = sudo_debug_pidstr; + iov[2].iov_len = sudo_debug_pidlen; /* Add string along with newline if it doesn't have one. */ iov[3].iov_base = (char *)str; diff --git a/include/sudo_debug.h b/include/sudo_debug.h index 5a8a62d32..b7b458d12 100644 --- a/include/sudo_debug.h +++ b/include/sudo_debug.h @@ -203,5 +203,6 @@ int sudo_debug_init(const char *debugfile, const char *settings); void sudo_debug_printf2(const char *func, const char *file, int line, int level, const char *format, ...) __printflike(5, 6); void sudo_debug_write(const char *str, int len, int errno_val); void sudo_debug_write2(const char *func, const char *file, int line, const char *str, int len, int errno_val); +pid_t sudo_debug_fork(void); #endif /* _SUDO_DEBUG_H */ diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index c6cff0be8..4d3e13e47 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -441,7 +441,7 @@ send_mail(const char *fmt, ...) debug_return; /* Fork and return, child will daemonize. */ - switch (pid = fork()) { + switch (pid = sudo_debug_fork()) { case -1: /* Error. */ error(1, _("unable to fork")); @@ -510,7 +510,7 @@ send_mail(const char *fmt, ...) _exit(1); } - switch (pid = fork()) { + switch (pid = sudo_debug_fork()) { case -1: /* Error. */ mysyslog(LOG_ERR, _("unable to fork: %m")); diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index 7e5312580..8175b6efc 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -751,7 +751,7 @@ run_command(char *path, char **argv) pid_t pid, rv; debug_decl(run_command, SUDO_DEBUG_UTIL) - switch (pid = fork()) { + switch (pid = sudo_debug_fork()) { case -1: error(1, _("unable to execute %s"), path); break; /* NOTREACHED */ diff --git a/src/exec.c b/src/exec.c index 36175e2a6..285176009 100644 --- a/src/exec.c +++ b/src/exec.c @@ -99,7 +99,7 @@ static int fork_cmnd(struct command_details *details, int sv[2]) sa.sa_handler = handler; sigaction(SIGCONT, &sa, NULL); - child = fork(); + child = sudo_debug_fork(); switch (child) { case -1: error(1, _("unable to fork")); @@ -214,7 +214,7 @@ sudo_execute(struct command_details *details, struct command_status *cstat) /* If running in background mode, fork and exit. */ if (ISSET(details->flags, CD_BACKGROUND)) { - switch (fork()) { + switch (sudo_debug_fork()) { case -1: cstat->type = CMD_ERRNO; cstat->val = errno; diff --git a/src/exec_pty.c b/src/exec_pty.c index 5ce320824..dd14f8ad5 100644 --- a/src/exec_pty.c +++ b/src/exec_pty.c @@ -618,7 +618,7 @@ fork_pty(struct command_details *details, int sv[], int *maxfd) } } - child = fork(); + child = sudo_debug_fork(); switch (child) { case -1: error(1, _("unable to fork")); @@ -953,7 +953,7 @@ exec_monitor(struct command_details *details, int backchannel) /* Start command and wait for it to stop or exit */ if (pipe(errpipe) == -1) error(1, _("unable to create pipe")); - child = fork(); + child = sudo_debug_fork(); if (child == -1) { warning(_("unable to fork")); goto bad;