From dde6fa7ec68576f663a9fa72d99631fd723b3fd2 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Jul 2016 10:21:35 +0100 Subject: [PATCH 06/11] Only redirect child processes to systemd Journal if using syslog In particular this means the test suite won't spam the Journal any more. Signed-off-by: Simon McVittie Reviewed-by: Ralf Habacker Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97009 --- bus/activation.c | 5 +++++ bus/bus.c | 6 ++++++ bus/bus.h | 1 + dbus/dbus-spawn-test.c | 8 ++++---- dbus/dbus-spawn-win.c | 1 + dbus/dbus-spawn.c | 16 ++++++++++------ dbus/dbus-spawn.h | 6 ++++++ test/spawn-test.c | 4 +++- 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bus/activation.c b/bus/activation.c index 614ce65..1b74969 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -1654,6 +1654,7 @@ bus_activation_activate_service (BusActivation *activation, dbus_bool_t retval; dbus_bool_t was_pending_activation; DBusString command; + DBusSpawnFlags flags = DBUS_SPAWN_NONE; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -2087,10 +2088,14 @@ bus_activation_activate_service (BusActivation *activation, dbus_error_init (&tmp_error); + if (bus_context_get_using_syslog (activation->context)) + flags |= DBUS_SPAWN_REDIRECT_OUTPUT; + if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, service_name, argv, envp, + flags, child_setup, activation, &tmp_error)) diff --git a/bus/bus.c b/bus/bus.c index 8856dab..df8239f 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -1366,6 +1366,12 @@ bus_context_get_initial_fd_limit (BusContext *context) return context->initial_fd_limit; } +dbus_bool_t +bus_context_get_using_syslog (BusContext *context) +{ + return context->syslog; +} + void bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) { diff --git a/bus/bus.h b/bus/bus.h index b7869b2..ca42533 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -121,6 +121,7 @@ int bus_context_get_max_match_rules_per_connection (BusContext int bus_context_get_max_replies_per_connection (BusContext *context); int bus_context_get_reply_timeout (BusContext *context); DBusRLimit * bus_context_get_initial_fd_limit (BusContext *context); +dbus_bool_t bus_context_get_using_syslog (BusContext *context); void bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, diff --git a/dbus/dbus-spawn-test.c b/dbus/dbus-spawn-test.c index 8c90fcc..01a2625 100644 --- a/dbus/dbus-spawn-test.c +++ b/dbus/dbus-spawn-test.c @@ -64,7 +64,7 @@ check_spawn_nonexistent (void *data) argv[0] = "/this/does/not/exist/32542sdgafgafdg"; if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_nonexistent", argv, - NULL, NULL, NULL, + NULL, DBUS_SPAWN_NONE, NULL, NULL, &error)) { _dbus_babysitter_block_for_child_exit (sitter); @@ -113,7 +113,7 @@ check_spawn_segfault (void *data) } if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_segfault", argv, - NULL, NULL, NULL, + NULL, DBUS_SPAWN_NONE, NULL, NULL, &error)) { _dbus_babysitter_block_for_child_exit (sitter); @@ -168,7 +168,7 @@ check_spawn_exit (void *data) } if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_exit", argv, - NULL, NULL, NULL, + NULL, DBUS_SPAWN_NONE, NULL, NULL, &error)) { _dbus_babysitter_block_for_child_exit (sitter); @@ -219,7 +219,7 @@ check_spawn_and_kill (void *data) } if (_dbus_spawn_async_with_babysitter (&sitter, "spawn_and_kill", argv, - NULL, NULL, NULL, + NULL, DBUS_SPAWN_NONE, NULL, NULL, &error)) { _dbus_babysitter_kill_child (sitter); diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index 5cb3044..564f61d 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -645,6 +645,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, char **argv, char **envp, + DBusSpawnFlags flags _DBUS_GNUC_UNUSED, DBusSpawnChildSetupFunc child_setup _DBUS_GNUC_UNUSED, void *user_data _DBUS_GNUC_UNUSED, DBusError *error) diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index e810ad5..70664cd 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -1216,6 +1216,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, char **argv, char **env, + DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error) @@ -1316,12 +1317,15 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, _DBUS_ASSERT_ERROR_IS_CLEAR (error); #ifdef HAVE_SYSTEMD - /* This may fail, but it's not critical. - * In particular, if we were compiled with journald support but are now - * running on a non-systemd system, this is going to fail, so we - * have to cope gracefully. */ - fd_out = sd_journal_stream_fd (sitter->log_name, LOG_INFO, FALSE); - fd_err = sd_journal_stream_fd (sitter->log_name, LOG_WARNING, FALSE); + if (flags & DBUS_SPAWN_REDIRECT_OUTPUT) + { + /* This may fail, but it's not critical. + * In particular, if we were compiled with journald support but are now + * running on a non-systemd system, this is going to fail, so we + * have to cope gracefully. */ + fd_out = sd_journal_stream_fd (sitter->log_name, LOG_INFO, FALSE); + fd_err = sd_journal_stream_fd (sitter->log_name, LOG_WARNING, FALSE); + } #endif pid = fork (); diff --git a/dbus/dbus-spawn.h b/dbus/dbus-spawn.h index 12ea586..dc04dc7 100644 --- a/dbus/dbus-spawn.h +++ b/dbus/dbus-spawn.h @@ -38,10 +38,16 @@ typedef struct DBusBabysitter DBusBabysitter; typedef void (* DBusBabysitterFinishedFunc) (DBusBabysitter *sitter, void *user_data); +typedef enum { + DBUS_SPAWN_REDIRECT_OUTPUT = (1 << 0), + DBUS_SPAWN_NONE = 0 +} DBusSpawnFlags; + dbus_bool_t _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, const char *log_name, char **argv, char **env, + DBusSpawnFlags flags, DBusSpawnChildSetupFunc child_setup, void *user_data, DBusError *error); diff --git a/test/spawn-test.c b/test/spawn-test.c index 12d37c8..a12c869 100644 --- a/test/spawn-test.c +++ b/test/spawn-test.c @@ -30,7 +30,9 @@ main (int argc, char **argv) argv_copy [i] = argv[i + 1]; argv_copy[argc - 1] = NULL; - if (!_dbus_spawn_async_with_babysitter (NULL, argv_copy[0], argv_copy, NULL, setup_func, NULL, &error)) + if (!_dbus_spawn_async_with_babysitter (NULL, argv_copy[0], argv_copy, NULL, + DBUS_SPAWN_NONE, setup_func, NULL, + &error)) { fprintf (stderr, "Could not launch application: \"%s\"\n", error.message); -- 2.8.1