]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/io/: SYSLOG(): Preserve errno
authorAlejandro Colomar <alx@kernel.org>
Mon, 3 Nov 2025 17:08:11 +0000 (18:08 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 17 Jul 2026 13:48:04 +0000 (08:48 -0500)
In some cases, we print strerrno() with this macro.  Because this is
a macro, and internal calls such as strdup(3) may set errno, that value
could be corrupted when we arrive at syslog(3).  While we could solve
this here, it's not robust.  Instead, we'll use a dedicated wrapper for
that, which will be added in the following commits: SYSLOGE().

What we'll do here is preserve errno when we exit from this macro, as we
often follow SYSLOG() calls with fprintf(stderr,) calls, which also use
the errno value, and we don't want to pollute that.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/io/syslog.h

index 84c65c74ff65e5b29ab576a0d0c5340802a870ee..323941c70e5170d88a7b5a016359223b507805cb 100644 (file)
 #endif
 
 #if !USE_SYSLOG
-# define SYSLOG(...)
+# define SYSLOG_(...)
 #elif defined(ENABLE_NLS)
-# define SYSLOG(...)  SYSLOG_C(__VA_ARGS__)
+# define SYSLOG_(...)  SYSLOG_C(__VA_ARGS__)
 #else
-# define SYSLOG(...)  syslog(__VA_ARGS__)
+# define SYSLOG_(...)  syslog(__VA_ARGS__)
 #endif
 
+#define SYSLOG(...)  do                                               \
+{                                                                     \
+       int  e_;                                                      \
+                                                                      \
+       e_ = errno;                                                   \
+       SYSLOG_(__VA_ARGS__);                                         \
+       errno = e_;                                                   \
+} while (0)
+
 /* The default syslog settings can now be changed here,
    in just one place.  */