dbus/test/internals/syslog.c
Simon McVittie 92bd5ef290 _dbus_logv: configurably log to syslog and/or stderr
This changes the behaviour of _dbus_logv() if _dbus_init_system_log() was
not called. Previously, _dbus_logv() would always log to syslog;
additionally, it would log to stderr, unless the process is dbus-daemon
and it was started by systemd. Now, it will log to stderr only,
unless _dbus_init_system_log() was called first.

This is the desired behaviour because when we hook up
_dbus_warn_check_failed() to _dbus_logv() in the next commit, we don't
want typical users of libdbus to start logging their check failures to
syslog - we only want the dbus-daemon to do that.

In practice this is not usually a behaviour change, because there was
only one situation in which we called _dbus_logv() without first calling
_dbus_init_system_log(), namely an error while parsing configuration
files. Initialize the system log "just in time" in that situation
to preserve existing behaviour.

Signed-off-by: Simon McVittie <smcv@debian.org>
2016-09-30 19:36:50 +01:00

106 lines
3.1 KiB
C

/* Manual regression test for syslog support
*
* Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
* Copyright © 2011 Nokia Corporation
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <config.h>
#include <stdlib.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-sysdeps.h>
#include "test-utils-glib.h"
typedef struct {
int dummy;
} Fixture;
static void
setup (Fixture *f,
gconstpointer data)
{
}
/* hopefully clear enough that people don't think these messages in syslog
* are a bug */
#define MESSAGE "regression test for _dbus_log(): "
static void
test_syslog (Fixture *f,
gconstpointer data)
{
#ifndef G_OS_WIN32
if (g_test_trap_fork (0, 0))
{
_dbus_init_system_log ("test-syslog",
DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
_dbus_log (DBUS_SYSTEM_LOG_FATAL, MESSAGE "%d", 23);
/* should not be reached: exit 0 so the assertion in the main process
* will fail */
exit (0);
}
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*" MESSAGE "23\n*");
if (g_test_trap_fork (0, 0))
{
_dbus_init_system_log ("test-syslog",
DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
_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);
exit (0);
}
g_test_trap_assert_passed ();
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",
DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
_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);
}
static void
teardown (Fixture *f,
gconstpointer data)
{
}
int
main (int argc,
char **argv)
{
test_init (&argc, &argv);
g_test_add ("/syslog", Fixture, NULL, setup, test_syslog, teardown);
return g_test_run ();
}