From b1093e5fa7d006fb463b2754119c6d2796bb7d82 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 2 Apr 2019 13:41:17 -0400 Subject: [PATCH] Fix: logging: log_add_time() save/restore errno MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The debugging logging macros (e.g. DBG()) are used as printf in the lttng-tools source files. The printf() implementation does not alter the errno value, so the fact that log_add_time() (through clock_gettime()) can alter errno is unexpected. For instance, adding a logging statement for debugging purposes within a function for which errno is expected to stay unchanged on return will change the behavior between execution with -vvv and non-verbose. Signed-off-by: Mathieu Desnoyers Signed-off-by: Jérémie Galarneau --- src/common/error.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common/error.c b/src/common/error.c index 236f21bae..a7d6e1220 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -45,6 +46,7 @@ const char *log_add_time(void) struct tm tm, *res; struct timespec tp; time_t now; + const int errsv = errno; ret = lttng_clock_gettime(CLOCK_REALTIME, &tp); if (ret < 0) { @@ -65,10 +67,12 @@ const char *log_add_time(void) goto error; } + errno = errsv; return URCU_TLS(error_log_time).str; error: /* Return an empty string on error so logging is not affected. */ + errno = errsv; return ""; } -- 2.34.1