Only redirect child processes to systemd Journal if using syslog
authorSimon McVittie <smcv@debian.org>
Thu, 21 Jul 2016 09:21:35 +0000 (10:21 +0100)
committerSimon McVittie <smcv@debian.org>
Fri, 30 Sep 2016 18:36:50 +0000 (19:36 +0100)
In particular this means the test suite won't spam the Journal
any more.

Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97009

bus/activation.c
bus/bus.c
bus/bus.h
dbus/dbus-spawn-test.c
dbus/dbus-spawn-win.c
dbus/dbus-spawn.c
dbus/dbus-spawn.h
test/spawn-test.c

index 3bddb35..c614d52 100644 (file)
@@ -1661,6 +1661,7 @@ bus_activation_activate_service (BusActivation  *activation,
   dbus_bool_t was_pending_activation;
   DBusString command;
   int limit;
+  DBusSpawnFlags flags = DBUS_SPAWN_NONE;
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -2097,10 +2098,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))
index 8856dab..df8239f 100644 (file)
--- 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, ...)
 {
index b7869b2..ca42533 100644 (file)
--- 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,
index 8c90fcc..01a2625 100644 (file)
@@ -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);
index 5cb3044..564f61d 100644 (file)
@@ -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)
index e810ad5..70664cd 100644 (file)
@@ -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 ();
index 12ea586..dc04dc7 100644 (file)
@@ -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);
index 12d37c8..a12c869 100644 (file)
@@ -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);