From 46d71c4360257d4fa97fb2163303d69f383b4ddb Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 17 Sep 2021 10:55:06 -0600 Subject: [PATCH] Store milliseconds in the debug file timestamp. Sometime second granularity is not enough. --- lib/util/sudo_debug.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/util/sudo_debug.c b/lib/util/sudo_debug.c index 07a5e4e76..8625e9d18 100644 --- a/lib/util/sudo_debug.c +++ b/lib/util/sudo_debug.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -606,11 +607,31 @@ void sudo_debug_write2_v1(int fd, const char *func, const char *file, int lineno, const char *str, int len, int errnum) { - char *timestr, numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; - time_t now; + char numbuf[(((sizeof(int) * 8) + 2) / 3) + 2]; + char timebuf[64]; + struct timeval tv; struct iovec iov[12]; int iovcnt = 3; + /* Cannot use sudo_gettime_real() here since it calls sudo_debug. */ + timebuf[0] = '\0'; + if (gettimeofday(&tv, NULL) != -1) { + time_t now = tv.tv_sec; + struct tm *tm = localtime(&now); + if (tm != NULL) { + size_t tlen = strftime(timebuf, sizeof(timebuf), "%b %e %H:%M:%S", tm); + if (tlen == 0) { + /* contents are undefined on error */ + timebuf[0] = '\0'; + } else { + (void)snprintf(timebuf + tlen, sizeof(timebuf) - tlen, + ".%03d ", (int)tv.tv_usec / 1000); + } + } + } + iov[0].iov_base = timebuf; + iov[0].iov_len = strlen(timebuf); + /* Prepend program name and pid with a trailing space. */ iov[1].iov_base = (char *)getprogname(); iov[1].iov_len = strlen(iov[1].iov_base); @@ -667,14 +688,6 @@ sudo_debug_write2_v1(int fd, const char *func, const char *file, int lineno, iov[iovcnt].iov_len = 1; iovcnt++; - /* Do timestamp last due to ctime's static buffer. */ - time(&now); - timestr = ctime(&now) + 4; - timestr[15] = ' '; /* replace year with a space */ - timestr[16] = '\0'; - iov[0].iov_base = timestr; - iov[0].iov_len = 16; - /* Write message in a single syscall */ ignore_result(writev(fd, iov, iovcnt)); }