]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/io/syslog.h: SYSLOG_C(): Split code to helper macro
authorAlejandro Colomar <alx@kernel.org>
Wed, 23 Jul 2025 17:50:04 +0000 (19:50 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 6 Mar 2026 01:44:32 +0000 (19:44 -0600)
The name of this macro makes it more evident what it does.  It's a
C-locale version of syslog(3).  We need to implement it as a macro
so that arguments such as strerror(3) are evaluated after setting
the C locale.  This would be impossible with a function.

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

index a2089dad833c5e4a32ec04db7ddf6e6c16e79406..5209d784760f4eabfeb4811dd5b26b8b3822a93c 100644 (file)
 #define LOG_AUTHPRIV LOG_AUTH
 #endif
 
-/* cleaner than lots of #ifdefs everywhere - use this as follows:
-   SYSLOG(LOG_CRIT, "user %s cracked root", user); */
 #ifdef ENABLE_NLS
-/* Temporarily set LC_TIME to "C" to avoid strange dates in syslog.
-   This is a workaround for a more general syslog(d) design problem -
-   syslogd should log the current system time for each event, and not
-   trust the formatted time received from the unix domain (or worse,
-   UDP) socket.  -MM */
-/* Avoid translated PAM error messages: set LC_ALL to "C".
- * --Nekral */
-#define SYSLOG(...)                                                    \
-       do {                                                            \
-               char *old_locale = setlocale (LC_ALL, NULL);            \
-               char *saved_locale = NULL;                              \
-               if (NULL != old_locale) {                               \
-                       saved_locale = strdup (old_locale);             \
-               }                                                       \
-               if (NULL != saved_locale) {                             \
-                       (void) setlocale (LC_ALL, "C");                 \
-               }                                                       \
-               syslog(__VA_ARGS__);                                    \
-               if (NULL != saved_locale) {                             \
-                       (void) setlocale (LC_ALL, saved_locale);        \
-                       free (saved_locale);                            \
-               }                                                       \
-       } while (false)
-#else                          /* !ENABLE_NLS */
+#define SYSLOG(...)  SYSLOG_C(__VA_ARGS__)
+#else
 #define SYSLOG(...)  syslog(__VA_ARGS__)
-#endif                         /* !ENABLE_NLS */
+#endif
 
 /* The default syslog settings can now be changed here,
    in just one place.  */
 #define OPENLOG(progname) openlog(progname, SYSLOG_OPTIONS, SYSLOG_FACILITY)
 
 
+// system log C-locale
+#define SYSLOG_C(...)  do                                             \
+{                                                                     \
+       char  *old_locale;                                            \
+       char  *saved_locale;                                          \
+                                                                     \
+       old_locale = setlocale(LC_ALL, NULL);                         \
+       saved_locale = NULL;                                          \
+                                                                     \
+       if (NULL != old_locale)                                       \
+               saved_locale = strdup(old_locale);                    \
+                                                                     \
+       if (NULL != saved_locale)                                     \
+               setlocale(LC_ALL, "C");                               \
+                                                                     \
+       syslog(__VA_ARGS__);                                          \
+       if (NULL != saved_locale) {                                   \
+               setlocale(LC_ALL, saved_locale);                      \
+               free(saved_locale);                                   \
+       }                                                             \
+} while (0)
+
+
 #endif  // include guard