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().
This commit is contained in:
Todd C. Miller
2012-04-06 15:20:16 -04:00
parent bb898cd5df
commit c8ce3a0a85
6 changed files with 31 additions and 10 deletions

View File

@@ -114,6 +114,8 @@ const char *const sudo_debug_subsystems[] = {
static int sudo_debug_settings[NUM_SUBSYSTEMS]; static int sudo_debug_settings[NUM_SUBSYSTEMS];
static int sudo_debug_fd = -1; static int sudo_debug_fd = -1;
static int sudo_debug_mode; 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; extern sudo_conv_t sudo_conv;
@@ -174,9 +176,27 @@ int sudo_debug_init(const char *debugfile, const char *settings)
} }
efree(buf); efree(buf);
(void)snprintf(sudo_debug_pidstr, sizeof(sudo_debug_pidstr), "[%d] ",
(int)getpid());
sudo_debug_pidlen = strlen(sudo_debug_pidstr);
return 1; 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 void
sudo_debug_enter(const char *func, const char *file, int line, sudo_debug_enter(const char *func, const char *file, int line,
int subsys) int subsys)
@@ -298,11 +318,11 @@ sudo_debug_write_file(const char *func, const char *file, int lineno,
int iovcnt = 4; int iovcnt = 4;
bool need_newline = false; 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_base = (char *)getprogname();
iov[1].iov_len = strlen(iov[1].iov_base); iov[1].iov_len = strlen(iov[1].iov_base);
iov[2].iov_base = " "; iov[2].iov_base = sudo_debug_pidstr;
iov[2].iov_len = 1; iov[2].iov_len = sudo_debug_pidlen;
/* Add string along with newline if it doesn't have one. */ /* Add string along with newline if it doesn't have one. */
iov[3].iov_base = (char *)str; iov[3].iov_base = (char *)str;

View File

@@ -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_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_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); 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 */ #endif /* _SUDO_DEBUG_H */

View File

@@ -441,7 +441,7 @@ send_mail(const char *fmt, ...)
debug_return; debug_return;
/* Fork and return, child will daemonize. */ /* Fork and return, child will daemonize. */
switch (pid = fork()) { switch (pid = sudo_debug_fork()) {
case -1: case -1:
/* Error. */ /* Error. */
error(1, _("unable to fork")); error(1, _("unable to fork"));
@@ -510,7 +510,7 @@ send_mail(const char *fmt, ...)
_exit(1); _exit(1);
} }
switch (pid = fork()) { switch (pid = sudo_debug_fork()) {
case -1: case -1:
/* Error. */ /* Error. */
mysyslog(LOG_ERR, _("unable to fork: %m")); mysyslog(LOG_ERR, _("unable to fork: %m"));

View File

@@ -751,7 +751,7 @@ run_command(char *path, char **argv)
pid_t pid, rv; pid_t pid, rv;
debug_decl(run_command, SUDO_DEBUG_UTIL) debug_decl(run_command, SUDO_DEBUG_UTIL)
switch (pid = fork()) { switch (pid = sudo_debug_fork()) {
case -1: case -1:
error(1, _("unable to execute %s"), path); error(1, _("unable to execute %s"), path);
break; /* NOTREACHED */ break; /* NOTREACHED */

View File

@@ -99,7 +99,7 @@ static int fork_cmnd(struct command_details *details, int sv[2])
sa.sa_handler = handler; sa.sa_handler = handler;
sigaction(SIGCONT, &sa, NULL); sigaction(SIGCONT, &sa, NULL);
child = fork(); child = sudo_debug_fork();
switch (child) { switch (child) {
case -1: case -1:
error(1, _("unable to fork")); 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 running in background mode, fork and exit. */
if (ISSET(details->flags, CD_BACKGROUND)) { if (ISSET(details->flags, CD_BACKGROUND)) {
switch (fork()) { switch (sudo_debug_fork()) {
case -1: case -1:
cstat->type = CMD_ERRNO; cstat->type = CMD_ERRNO;
cstat->val = errno; cstat->val = errno;

View File

@@ -618,7 +618,7 @@ fork_pty(struct command_details *details, int sv[], int *maxfd)
} }
} }
child = fork(); child = sudo_debug_fork();
switch (child) { switch (child) {
case -1: case -1:
error(1, _("unable to fork")); 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 */ /* Start command and wait for it to stop or exit */
if (pipe(errpipe) == -1) if (pipe(errpipe) == -1)
error(1, _("unable to create pipe")); error(1, _("unable to create pipe"));
child = fork(); child = sudo_debug_fork();
if (child == -1) { if (child == -1) {
warning(_("unable to fork")); warning(_("unable to fork"));
goto bad; goto bad;