From e0ad29d007d02901a71da06e66975c06e06fe412 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Jul 2016 10:29:10 +0100 Subject: [PATCH] _dbus_init_system_log: record a syslog tag (executable name) Instead of hard-coding "dbus", report what the executable really is. Signed-off-by: Simon McVittie --- bus/bus.c | 2 +- dbus/dbus-sysdeps-unix.c | 20 +++++++++++++++++--- dbus/dbus-sysdeps-win.c | 13 ++++++++++++- dbus/dbus-sysdeps.h | 3 ++- test/internals/syslog.c | 6 +++--- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/bus/bus.c b/bus/bus.c index 9f1daa2..b8db6d8 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -288,7 +288,7 @@ process_config_first_time_only (BusContext *context, auth_mechanisms = NULL; pidfile = NULL; - _dbus_init_system_log (TRUE); + _dbus_init_system_log ("dbus-daemon", TRUE); if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION) context->systemd_activation = TRUE; diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 40ae6ff..8da129c 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -4431,8 +4431,21 @@ _dbus_restore_socket_errno (int saved_errno) errno = saved_errno; } +static const char *syslog_tag = "dbus"; + +/** + * Initialize the system log. + * + * The "tag" is not copied, and must remain valid for the entire lifetime of + * the process or until _dbus_init_system_log() is called again. In practice + * 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 + */ void -_dbus_init_system_log (dbus_bool_t is_daemon) +_dbus_init_system_log (const char *tag, + dbus_bool_t is_daemon) { #ifdef HAVE_SYSLOG_H int logopts = LOG_PID; @@ -4444,7 +4457,8 @@ _dbus_init_system_log (dbus_bool_t is_daemon) logopts |= LOG_PERROR; #endif - openlog ("dbus", logopts, LOG_DAEMON); + syslog_tag = tag; + openlog (tag, logopts, LOG_DAEMON); #endif } @@ -4491,7 +4505,7 @@ _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args { /* 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 ()); + fprintf (stderr, "%s[" DBUS_PID_FORMAT "]: ", syslog_tag, _dbus_getpid ()); vfprintf (stderr, msg, tmp); fputc ('\n', stderr); va_end (tmp); diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 7dac66d..efd3d54 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -3634,8 +3634,19 @@ _dbus_restore_socket_errno (int saved_errno) _dbus_win_set_errno (saved_errno); } +/** + * Initialize the system log. + * + * The "tag" is not copied, and must remain valid for the entire lifetime of + * the process or until _dbus_init_system_log() is called again. In practice + * 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 + */ void -_dbus_init_system_log (dbus_bool_t is_daemon) +_dbus_init_system_log (const char *tag, + dbus_bool_t is_daemon) { /* OutputDebugStringA doesn't need any special initialization, do nothing */ } diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index fb4be88..d5594c7 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -556,7 +556,8 @@ 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); +void _dbus_init_system_log (const char *tag, + dbus_bool_t is_daemon); typedef enum { DBUS_SYSTEM_LOG_INFO, diff --git a/test/internals/syslog.c b/test/internals/syslog.c index 805c578..c0c1f91 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 (FALSE); + _dbus_init_system_log ("test-syslog", FALSE); _dbus_system_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 (FALSE); + _dbus_init_system_log ("test-syslog", FALSE); _dbus_system_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_system_log (DBUS_SYSTEM_LOG_WARNING, MESSAGE "%d", 45); _dbus_system_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 (FALSE); + _dbus_init_system_log ("test-syslog", FALSE); _dbus_system_log (DBUS_SYSTEM_LOG_INFO, MESSAGE "%d", 42); _dbus_system_log (DBUS_SYSTEM_LOG_WARNING, MESSAGE "%d", 45); _dbus_system_log (DBUS_SYSTEM_LOG_SECURITY, MESSAGE "%d", 666); -- 2.8.1