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>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
+#ifdef HAVE_SYSLOG_H
+#include <syslog.h>
+#endif
#ifdef HAVE_WRITEV
#include <sys/uio.h>
#endif
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 */
#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
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
_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
_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 */
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 */
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 {
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.