From 3b69f8e30c5e351907cb2fc67158943ee9c6f125 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 20 Jul 2016 10:07:12 +0100 Subject: [PATCH] sysdeps: move _dbus_system_log() into the shared library This is in preparation for optionally making _dbus_warn() use it. dbus-daemon closes its stderr under some circumstances, including when launched by dbus-launch, which makes failures in that situation rather hard to debug. _dbus_system_log() is the same on Unix and Windows, so move it to dbus-sysdeps.c. _dbus_system_logv() remains platform-specific. Signed-off-by: Simon McVittie --- dbus/dbus-sysdeps-unix.c | 71 ++++++++++++++++++++++++++++++++++ dbus/dbus-sysdeps-util-unix.c | 89 ------------------------------------------- dbus/dbus-sysdeps-util-win.c | 57 --------------------------- dbus/dbus-sysdeps-win.c | 39 +++++++++++++++++++ dbus/dbus-sysdeps.c | 18 +++++++++ dbus/dbus-sysdeps.h | 3 ++ 6 files changed, 131 insertions(+), 146 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index ce8cbf7..40ae6ff 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -4431,4 +4431,75 @@ _dbus_restore_socket_errno (int saved_errno) errno = saved_errno; } +void +_dbus_init_system_log (dbus_bool_t is_daemon) +{ +#ifdef HAVE_SYSLOG_H + int logopts = LOG_PID; + +#if HAVE_DECL_LOG_PERROR +#ifdef HAVE_SYSTEMD + if (!is_daemon || sd_booted () <= 0) +#endif + logopts |= LOG_PERROR; +#endif + + openlog ("dbus", logopts, LOG_DAEMON); +#endif +} + +/** + * Log a message to the system log file (e.g. syslog on Unix). + * + * @param severity a severity value + * @param msg a printf-style format string + * @param args arguments for the format string + * + * If the FATAL severity is given, this function will terminate the program + * with an error code. + */ +void +_dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) +{ + va_list tmp; +#ifdef HAVE_SYSLOG_H + int flags; + switch (severity) + { + case DBUS_SYSTEM_LOG_INFO: + flags = LOG_DAEMON | LOG_NOTICE; + break; + case DBUS_SYSTEM_LOG_WARNING: + flags = LOG_DAEMON | LOG_WARNING; + break; + case DBUS_SYSTEM_LOG_SECURITY: + flags = LOG_AUTH | LOG_NOTICE; + break; + case DBUS_SYSTEM_LOG_FATAL: + flags = LOG_DAEMON|LOG_CRIT; + break; + default: + return; + } + + DBUS_VA_COPY (tmp, args); + vsyslog (flags, msg, tmp); + va_end (tmp); +#endif + +#if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR + { + /* vsyslog() won't write to stderr, so we'd better do it */ + DBUS_VA_COPY (tmp, args); + fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ()); + vfprintf (stderr, msg, tmp); + fputc ('\n', stderr); + va_end (tmp); + } +#endif + + if (severity == DBUS_SYSTEM_LOG_FATAL) + exit (1); +} + /* tests in dbus-sysdeps-util.c */ diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 1f9ddb9..5822a4e 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -516,95 +516,6 @@ _dbus_rlimit_free (DBusRLimit *lim) dbus_free (lim); } -void -_dbus_init_system_log (dbus_bool_t is_daemon) -{ -#ifdef HAVE_SYSLOG_H - int logopts = LOG_PID; - -#if HAVE_DECL_LOG_PERROR -#ifdef HAVE_SYSTEMD - if (!is_daemon || sd_booted () <= 0) -#endif - logopts |= LOG_PERROR; -#endif - - openlog ("dbus", logopts, LOG_DAEMON); -#endif -} - -/** - * Log a message to the system log file (e.g. syslog on Unix). - * - * @param severity a severity value - * @param msg a printf-style format string - */ -void -_dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) -{ - va_list args; - - va_start (args, msg); - - _dbus_system_logv (severity, msg, args); - - va_end (args); -} - -/** - * Log a message to the system log file (e.g. syslog on Unix). - * - * @param severity a severity value - * @param msg a printf-style format string - * @param args arguments for the format string - * - * If the FATAL severity is given, this function will terminate the program - * with an error code. - */ -void -_dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) -{ - va_list tmp; -#ifdef HAVE_SYSLOG_H - int flags; - switch (severity) - { - case DBUS_SYSTEM_LOG_INFO: - flags = LOG_DAEMON | LOG_NOTICE; - break; - case DBUS_SYSTEM_LOG_WARNING: - flags = LOG_DAEMON | LOG_WARNING; - break; - case DBUS_SYSTEM_LOG_SECURITY: - flags = LOG_AUTH | LOG_NOTICE; - break; - case DBUS_SYSTEM_LOG_FATAL: - flags = LOG_DAEMON|LOG_CRIT; - break; - default: - return; - } - - DBUS_VA_COPY (tmp, args); - vsyslog (flags, msg, tmp); - va_end (tmp); -#endif - -#if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR - { - /* vsyslog() won't write to stderr, so we'd better do it */ - DBUS_VA_COPY (tmp, args); - fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ()); - vfprintf (stderr, msg, tmp); - fputc ('\n', stderr); - va_end (tmp); - } -#endif - - if (severity == DBUS_SYSTEM_LOG_FATAL) - exit (1); -} - /** Installs a UNIX signal handler * * @param sig the signal to handle diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c index 789bd18..dacb8d2 100644 --- a/dbus/dbus-sysdeps-util-win.c +++ b/dbus/dbus-sysdeps-util-win.c @@ -296,63 +296,6 @@ _dbus_rlimit_free (DBusRLimit *lim) _dbus_assert (lim == NULL); } -void -_dbus_init_system_log (dbus_bool_t is_daemon) -{ - /* OutputDebugStringA doesn't need any special initialization, do nothing */ -} - -/** - * Log a message to the system log file (e.g. syslog on Unix). - * - * @param severity a severity value - * @param msg a printf-style format string - */ -void -_dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) -{ - va_list args; - - va_start (args, msg); - - _dbus_system_logv (severity, msg, args); - - va_end (args); -} - -/** - * Log a message to the system log file (e.g. syslog on Unix). - * - * @param severity a severity value - * @param msg a printf-style format string - * @param args arguments for the format string - * - * If the FATAL severity is given, this function will terminate the program - * with an error code. - */ -void -_dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) -{ - char *s = ""; - char buf[1024]; - char format[1024]; - - switch(severity) - { - case DBUS_SYSTEM_LOG_INFO: s = "info"; break; - case DBUS_SYSTEM_LOG_WARNING: s = "warning"; break; - case DBUS_SYSTEM_LOG_SECURITY: s = "security"; break; - case DBUS_SYSTEM_LOG_FATAL: s = "fatal"; break; - } - - snprintf(format, sizeof(format), "%s%s", s ,msg); - vsnprintf(buf, sizeof(buf), format, args); - OutputDebugStringA(buf); - - if (severity == DBUS_SYSTEM_LOG_FATAL) - exit (1); -} - /** Installs a signal handler * * @param sig the signal to handle diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 698e7be..7dac66d 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3634,5 +3634,44 @@ _dbus_restore_socket_errno (int saved_errno) _dbus_win_set_errno (saved_errno); } +void +_dbus_init_system_log (dbus_bool_t is_daemon) +{ + /* OutputDebugStringA doesn't need any special initialization, do nothing */ +} + +/** + * Log a message to the system log file (e.g. syslog on Unix). + * + * @param severity a severity value + * @param msg a printf-style format string + * @param args arguments for the format string + * + * If the FATAL severity is given, this function will terminate the program + * with an error code. + */ +void +_dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) +{ + char *s = ""; + char buf[1024]; + char format[1024]; + + switch(severity) + { + case DBUS_SYSTEM_LOG_INFO: s = "info"; break; + case DBUS_SYSTEM_LOG_WARNING: s = "warning"; break; + case DBUS_SYSTEM_LOG_SECURITY: s = "security"; break; + case DBUS_SYSTEM_LOG_FATAL: s = "fatal"; break; + } + + snprintf(format, sizeof(format), "%s%s", s ,msg); + vsnprintf(buf, sizeof(buf), format, args); + OutputDebugStringA(buf); + + if (severity == DBUS_SYSTEM_LOG_FATAL) + exit (1); +} + /** @} end of sysdeps-win */ /* tests in dbus-sysdeps-util.c */ diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 8b986d5..23ffc86 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -751,6 +751,24 @@ _dbus_strerror_from_errno (void) return _dbus_strerror (errno); } +/** + * Log a message to the system log file (e.g. syslog on Unix). + * + * @param severity a severity value + * @param msg a printf-style format string + */ +void +_dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) +{ + va_list args; + + va_start (args, msg); + + _dbus_system_logv (severity, msg, args); + + va_end (args); +} + /** @} end of sysdeps */ /* tests in dbus-sysdeps-util.c */ diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 3659dd4..fb4be88 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -555,6 +555,7 @@ void _dbus_set_signal_handler (int sig, dbus_bool_t _dbus_user_at_console (const char *username, DBusError *error); +DBUS_PRIVATE_EXPORT void _dbus_init_system_log (dbus_bool_t is_daemon); typedef enum { @@ -564,7 +565,9 @@ typedef enum { DBUS_SYSTEM_LOG_FATAL } DBusSystemLogSeverity; +DBUS_PRIVATE_EXPORT void _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (2, 3); +DBUS_PRIVATE_EXPORT void _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args); /* Define DBUS_VA_COPY() to do the right thing for copying va_list variables. -- 2.8.1