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.

Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97009
[smcv: move the #include for syslog.h, too]
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
This commit is contained in:
Simon McVittie 2016-07-20 10:07:12 +01:00 committed by Simon McVittie
parent e3d76634c3
commit a227177918
6 changed files with 134 additions and 150 deletions

View file

@ -62,6 +62,9 @@
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_WRITEV
#include <sys/uio.h>
#endif
@ -4520,4 +4523,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 */

View file

@ -50,10 +50,6 @@
#include <dirent.h>
#include <sys/un.h>
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_SYS_SYSLIMITS_H
#include <sys/syslimits.h>
#endif
@ -511,95 +507,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

View file

@ -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

View file

@ -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 */

View file

@ -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 */

View file

@ -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.