From 883cc31d5e7c6f153cedebbbd050f66fb2fbf16d Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 14 Nov 2017 16:41:23 +0000 Subject: [PATCH 10/13] bus: Silence the output of the test services when doing OOM testing The echo service frequently fails to connect to the bus when we are testing OOM code paths, again causing a lot of noise in the log. Signed-off-by: Simon McVittie --- bus/activation.c | 5 +++++ bus/bus.c | 6 ++++++ bus/bus.h | 1 + dbus/dbus-spawn.c | 38 +++++++++++++++++++++++++------------- dbus/dbus-spawn.h | 1 + 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/bus/activation.c b/bus/activation.c index 8f3aab86..edf96a2c 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -2220,6 +2220,11 @@ bus_activation_activate_service (BusActivation *activation, dbus_error_init (&tmp_error); +#ifdef DBUS_ENABLE_EMBEDDED_TESTS + if (bus_context_get_quiet_log (activation->context)) + flags |= DBUS_SPAWN_SILENCE_OUTPUT; +#endif + if (bus_context_get_using_syslog (activation->context)) flags |= DBUS_SPAWN_REDIRECT_OUTPUT; diff --git a/bus/bus.c b/bus/bus.c index bd2ab8bd..295dc7b5 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -1848,4 +1848,10 @@ bus_context_quiet_log_end (BusContext *context) { context->quiet_log = FALSE; } + +dbus_bool_t +bus_context_get_quiet_log (BusContext *context) +{ + return context->quiet_log; +} #endif diff --git a/bus/bus.h b/bus/bus.h index 31af363b..647f9988 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -149,6 +149,7 @@ void bus_context_check_all_watches (BusContext #ifdef DBUS_ENABLE_EMBEDDED_TESTS void bus_context_quiet_log_begin (BusContext *context); void bus_context_quiet_log_end (BusContext *context); +dbus_bool_t bus_context_get_quiet_log (BusContext *context); #endif #endif /* BUS_BUS_H */ diff --git a/dbus/dbus-spawn.c b/dbus/dbus-spawn.c index b2bcc226..4fc67b6d 100644 --- a/dbus/dbus-spawn.c +++ b/dbus/dbus-spawn.c @@ -1260,11 +1260,9 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, int child_err_report_pipe[2] = { -1, -1 }; DBusSocket babysitter_pipe[2] = { DBUS_SOCKET_INIT, DBUS_SOCKET_INIT }; pid_t pid; -#ifdef HAVE_SYSTEMD int fd_out = -1; int fd_err = -1; -#endif - + _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_assert (argv[0] != NULL); @@ -1351,8 +1349,27 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (flags & DBUS_SPAWN_SILENCE_OUTPUT) + { + fd_out = open ("/dev/null", O_RDONLY); + + if (fd_out < 0) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to open /dev/null: %s", + _dbus_strerror (errno)); + goto cleanup_and_fail; + } + + _dbus_fd_set_close_on_exec (fd_out); + + fd_err = _dbus_dup (fd_out, error); + + if (fd_err < 0) + goto cleanup_and_fail; + } #ifdef HAVE_SYSTEMD - if (flags & DBUS_SPAWN_REDIRECT_OUTPUT) + else 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 @@ -1431,15 +1448,16 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, signal (SIGPIPE, SIG_IGN); close_and_invalidate (&babysitter_pipe[1].fd); -#ifdef HAVE_SYSTEMD - /* log to systemd journal if possible */ + + /* Redirect stdout, stderr to systemd Journal or /dev/null + * as requested, if possible */ if (fd_out >= 0) dup2 (fd_out, STDOUT_FILENO); if (fd_err >= 0) dup2 (fd_err, STDERR_FILENO); close_and_invalidate (&fd_out); close_and_invalidate (&fd_err); -#endif + do_exec (child_err_report_pipe[WRITE_END], argv, env, @@ -1449,10 +1467,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, else { close_and_invalidate (&child_err_report_pipe[WRITE_END]); -#ifdef HAVE_SYSTEMD close_and_invalidate (&fd_out); close_and_invalidate (&fd_err); -#endif babysit (grandchild_pid, babysitter_pipe[1].fd); _dbus_assert_not_reached ("Got to code after babysit()"); } @@ -1462,10 +1478,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, /* Close the uncared-about ends of the pipes */ close_and_invalidate (&child_err_report_pipe[WRITE_END]); close_and_invalidate (&babysitter_pipe[1].fd); -#ifdef HAVE_SYSTEMD close_and_invalidate (&fd_out); close_and_invalidate (&fd_err); -#endif sitter->socket_to_babysitter = babysitter_pipe[0]; babysitter_pipe[0].fd = -1; @@ -1495,10 +1509,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, close_and_invalidate (&child_err_report_pipe[WRITE_END]); close_and_invalidate (&babysitter_pipe[0].fd); close_and_invalidate (&babysitter_pipe[1].fd); -#ifdef HAVE_SYSTEMD close_and_invalidate (&fd_out); close_and_invalidate (&fd_err); -#endif if (sitter != NULL) _dbus_babysitter_unref (sitter); diff --git a/dbus/dbus-spawn.h b/dbus/dbus-spawn.h index b34324de..a5450d6e 100644 --- a/dbus/dbus-spawn.h +++ b/dbus/dbus-spawn.h @@ -40,6 +40,7 @@ typedef void (* DBusBabysitterFinishedFunc) (DBusBabysitter *sitter, typedef enum { DBUS_SPAWN_REDIRECT_OUTPUT = (1 << 0), + DBUS_SPAWN_SILENCE_OUTPUT = (1 << 1), DBUS_SPAWN_NONE = 0 } DBusSpawnFlags; -- 2.15.0