From 012a923596dbab97ae3bf06662d5b624372e4e2a Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 20 Jul 2016 10:54:34 +0100 Subject: [PATCH] _dbus_logv: configurably log to syslog and/or stderr Signed-off-by: Simon McVittie --- bus/bus.c | 24 +++++++++++- dbus/dbus-sysdeps-unix.c | 85 +++++++++++++++++++++++-------------------- dbus/dbus-sysdeps-util-unix.c | 4 -- dbus/dbus-sysdeps-win.c | 36 ++++++++++++++---- dbus/dbus-sysdeps.c | 2 +- dbus/dbus-sysdeps.h | 8 +++- test/internals/syslog.c | 6 +-- 7 files changed, 106 insertions(+), 59 deletions(-) diff --git a/bus/bus.c b/bus/bus.c index 9996be2..16a5fd9 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -47,6 +47,10 @@ #include #endif +#ifdef HAVE_SYSTEMD +#include +#endif + struct BusContext { int refcount; @@ -288,7 +292,24 @@ process_config_first_time_only (BusContext *context, auth_mechanisms = NULL; pidfile = NULL; - _dbus_init_system_log ("dbus-daemon", TRUE); + context->syslog = bus_config_parser_get_syslog (parser); + + if (context->syslog) + { +#ifdef HAVE_SYSTEMD + /* If running under systemd, we don't want to log to both stderr and + * syslog, because our stderr is probably connected to journald too, + * so we'd duplicate all our messages. */ + if (sd_booted () > 0) + _dbus_init_system_log ("dbus-daemon", DBUS_LOG_MODE_SYSTEM_LOG_ONLY); + else +#endif + _dbus_init_system_log ("dbus-daemon", DBUS_LOG_MODE_SYSTEM_LOG); + } + else + { + _dbus_init_system_log ("dbus-daemon", DBUS_LOG_MODE_STDERR); + } if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION) context->systemd_activation = TRUE; @@ -470,7 +491,6 @@ process_config_first_time_only (BusContext *context, } context->fork = bus_config_parser_get_fork (parser); - context->syslog = bus_config_parser_get_syslog (parser); context->keep_umask = bus_config_parser_get_keep_umask (parser); context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser); diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 79a22c6..54c4751 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -62,6 +62,9 @@ #ifdef HAVE_ERRNO_H #include #endif +#ifdef HAVE_SYSLOG_H +#include +#endif #ifdef HAVE_WRITEV #include #endif @@ -4432,6 +4435,9 @@ _dbus_restore_socket_errno (int saved_errno) } static const char *syslog_tag = "dbus"; +#ifdef HAVE_SYSLOG_H +static DBusLogMode log_mode = DBUS_LOG_MODE_STDERR; +#endif /** * Initialize the system log. @@ -4440,30 +4446,28 @@ static const char *syslog_tag = "dbus"; * the process or until _dbus_init_system_log() is called again. In practice * it will normally be a constant. * + * On platforms that do not support a system log, the mode is ignored and + * the library behaves as though #DBUS_LOG_MODE_STDERR had been used. + * * @param tag the name of the executable (syslog tag) - * @param is_daemon #TRUE if this is the dbus-daemon + * @param mode whether to log to stderr, the system log or both */ void -_dbus_init_system_log (const char *tag, - dbus_bool_t is_daemon) +_dbus_init_system_log (const char *tag, + DBusLogMode mode) { -#ifdef HAVE_SYSLOG_H - int logopts = LOG_PID; + syslog_tag = tag; -#if HAVE_DECL_LOG_PERROR -#ifdef HAVE_SYSTEMD - if (!is_daemon || sd_booted () <= 0) -#endif - logopts |= LOG_PERROR; -#endif +#ifdef HAVE_SYSLOG_H + log_mode = mode; - syslog_tag = tag; - openlog (tag, logopts, LOG_DAEMON); + if (mode != DBUS_LOG_MODE_STDERR) + openlog (tag, LOG_PID, LOG_DAEMON); #endif } /** - * Log a message to the system log file (e.g. syslog on Unix). + * Log a message to the system log file (e.g. syslog on Unix) and/or stderr. * * @param severity a severity value * @param msg a printf-style format string @@ -4479,40 +4483,41 @@ _dbus_logv (DBusSystemLogSeverity severity, { 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 (log_mode != DBUS_LOG_MODE_STDERR) + { + 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; + } -#if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR + DBUS_VA_COPY (tmp, args); + vsyslog (flags, msg, tmp); + va_end (tmp); + } + + if (log_mode != DBUS_LOG_MODE_SYSTEM_LOG_ONLY) +#endif { - /* vsyslog() won't write to stderr, so we'd better do it */ DBUS_VA_COPY (tmp, args); fprintf (stderr, "%s[" DBUS_PID_FORMAT "]: ", syslog_tag, _dbus_getpid ()); vfprintf (stderr, msg, tmp); fputc ('\n', stderr); va_end (tmp); } -#endif if (severity == DBUS_SYSTEM_LOG_FATAL) exit (1); diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 5822a4e..040bb7b 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -50,10 +50,6 @@ #include #include -#ifdef HAVE_SYSLOG_H -#include -#endif - #ifdef HAVE_SYS_SYSLIMITS_H #include #endif diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 32b49b8..e632dce 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3634,6 +3634,9 @@ _dbus_restore_socket_errno (int saved_errno) _dbus_win_set_errno (saved_errno); } +static const char *log_tag = "dbus"; +static DBusLogMode log_mode = DBUS_LOG_MODE_STDERR; + /** * Initialize the system log. * @@ -3642,13 +3645,14 @@ _dbus_restore_socket_errno (int saved_errno) * it will normally be a constant. * * @param tag the name of the executable (syslog tag) - * @param is_daemon #TRUE if this is the dbus-daemon + * @param mode whether to log to stderr, the system log or both */ void _dbus_init_system_log (const char *tag, - dbus_bool_t is_daemon) + DBusLogMode mode) { - /* OutputDebugStringA doesn't need any special initialization, do nothing */ + log_tag = tag; + log_mode = mode; } /** @@ -3667,8 +3671,7 @@ _dbus_logv (DBusSystemLogSeverity severity, va_list args) { char *s = ""; - char buf[1024]; - char format[1024]; + va_list tmp; switch(severity) { @@ -3678,9 +3681,26 @@ _dbus_logv (DBusSystemLogSeverity severity, 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 (log_mode != DBUS_LOG_MODE_STDERR) + { + char buf[1024]; + char format[1024]; + + DBUS_VA_COPY (tmp, args); + snprintf (format, sizeof (format), "%s: %s", s, msg); + vsnprintf(buf, sizeof(buf), format, tmp); + OutputDebugStringA(buf); + va_end (tmp); + } + + if (log_mode != DBUS_LOG_MODE_SYSTEM_LOG_ONLY) + { + DBUS_VA_COPY (tmp, args); + fprintf (stderr, "%s[%d]: %s: ", log_tag, _dbus_pid_for_log (), s); + vfprintf (stderr, msg, tmp); + fprintf (stderr, "\n"); + va_end (tmp); + } if (severity == DBUS_SYSTEM_LOG_FATAL) exit (1); diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index d004ff0..c58377b 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -752,7 +752,7 @@ _dbus_strerror_from_errno (void) } /** - * Log a message to the system log file (e.g. syslog on Unix). + * Log a message to the system log file (e.g. syslog on Unix) and/or stderr. * * @param severity a severity value * @param msg a printf-style format string diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index beb2f1e..f942c94 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -555,9 +555,15 @@ void _dbus_set_signal_handler (int sig, dbus_bool_t _dbus_user_at_console (const char *username, DBusError *error); +typedef enum { + DBUS_LOG_MODE_STDERR, + DBUS_LOG_MODE_SYSTEM_LOG, + DBUS_LOG_MODE_SYSTEM_LOG_ONLY +} DBusLogMode; + DBUS_PRIVATE_EXPORT void _dbus_init_system_log (const char *tag, - dbus_bool_t is_daemon); + DBusLogMode mode); typedef enum { DBUS_SYSTEM_LOG_INFO, diff --git a/test/internals/syslog.c b/test/internals/syslog.c index c2467be..36442fe 100644 --- a/test/internals/syslog.c +++ b/test/internals/syslog.c @@ -56,7 +56,7 @@ test_syslog (Fixture *f, #ifndef G_OS_WIN32 if (g_test_trap_fork (0, 0)) { - _dbus_init_system_log ("test-syslog", FALSE); + _dbus_init_system_log ("test-syslog", DBUS_LOG_MODE_SYSTEM_LOG); _dbus_log (DBUS_SYSTEM_LOG_FATAL, MESSAGE "%d", 23); /* should not be reached: exit 0 so the assertion in the main process * will fail */ @@ -68,7 +68,7 @@ test_syslog (Fixture *f, if (g_test_trap_fork (0, 0)) { - _dbus_init_system_log ("test-syslog", FALSE); + _dbus_init_system_log ("test-syslog", DBUS_LOG_MODE_SYSTEM_LOG); _dbus_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_log (DBUS_SYSTEM_LOG_WARNING, MESSAGE "%d", 45); _dbus_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666); @@ -79,7 +79,7 @@ test_syslog (Fixture *f, g_test_trap_assert_stderr ("*" MESSAGE "42\n*" MESSAGE "45\n*" MESSAGE "666\n*"); #endif /* manual test (this is the best we can do on Windows) */ - _dbus_init_system_log ("test-syslog", FALSE); + _dbus_init_system_log ("test-syslog", DBUS_LOG_MODE_SYSTEM_LOG); _dbus_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_log (DBUS_SYSTEM_LOG_WARNING, MESSAGE "%d", 45); _dbus_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666); -- 2.8.1