]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Unix: Flush stdout and stderr streams before forking
authorSimon McVittie <smcv@collabora.com>
Tue, 14 Nov 2017 15:30:03 +0000 (15:30 +0000)
committerSimon McVittie <smcv@collabora.com>
Wed, 15 Nov 2017 12:12:18 +0000 (12:12 +0000)
stdout and stderr are close-on-exec and buffered, so we can't rely on
their buffers being empty. If we continue to execute application code
after forking (as opposed to immediately exec()ing), then the child
process might later flush the libc stdio buffers, resulting in
output that is printed by the parent also being printed by the child.

In particular, test-bus.log sometimes grows extremely large for
this reason, because this test repeatedly attempts to carry out
legacy activation.

Reviewed-by: Philip Withnall <withnall@endlessm.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=103601

dbus/dbus-spawn.c
dbus/dbus-sysdeps-unix.c
dbus/dbus-sysdeps-util-unix.c
test/test-service.c
tools/dbus-launch.c
tools/dbus-run-session.c

index 8ab529a4997430cfe5daf15bcf7d6f868b0993ef..b2bcc2260382e02bf00b9f4c012f539834550775 100644 (file)
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <sys/wait.h>
+#include <stdio.h>
 #include <stdlib.h>
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
@@ -1362,6 +1363,11 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
     }
 #endif
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   pid = fork ();
   
   if (pid < 0)
@@ -1385,7 +1391,10 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
       /* Close the parent's end of the pipes. */
       close_and_invalidate (&child_err_report_pipe[READ_END]);
       close_and_invalidate (&babysitter_pipe[0].fd);
-      
+
+      fflush (stdout);
+      fflush (stderr);
+
       /* Create the child that will exec () */
       grandchild_pid = fork ();
       
index bebf2073b680549991e7eb8a9071471ec6cd42a9..f103323c53db00bef35d774e467c0cbf886420df 100644 (file)
@@ -1040,6 +1040,11 @@ _dbus_connect_exec (const char     *path,
       _dbus_fd_set_close_on_exec (fds[1]);
     }
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   pid = fork ();
   if (pid < 0)
     {
@@ -3623,6 +3628,11 @@ _read_subprocess_line_argv (const char *progpath,
       goto out;
     }
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   pid = fork ();
   if (pid < 0)
     {
index 2be5b7792eba1766f167da9d314179d412981712..b841bf63fb83c189920ee4dd4c6066ed504ad982 100644 (file)
@@ -35,6 +35,7 @@
 #include "dbus-test.h"
 
 #include <sys/types.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
@@ -99,6 +100,12 @@ _dbus_become_daemon (const DBusString *pidfile,
     }
 
   _dbus_verbose ("forking...\n");
+
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   switch ((child_pid = fork ()))
     {
     case -1:
index b500af31441188aea136e7f25f3c9893d8c2bfef..f71e3e039bece83513b53edcd1bb043461ca44e0 100644 (file)
@@ -1,6 +1,7 @@
 #include <config.h>
 
 #include "test-utils.h"
+#include <stdio.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -424,7 +425,14 @@ main (int    argc,
 #ifndef DBUS_WIN
    if (do_fork)
     {
-      pid_t pid = fork ();
+      pid_t pid;
+
+      /* Make sure our output buffers aren't redundantly printed by both the
+       * parent and the child */
+      fflush (stdout);
+      fflush (stderr);
+
+      pid = fork ();
       if (pid != 0)
         exit (0);
       sleep (1);
index f52875482afa3a19a1b8101da9f9eef829cb2d20..425914a19201d6c5ff38672a9ccbb010a389764b 100644 (file)
@@ -656,6 +656,11 @@ babysit (int   exit_with_session,
       exit (1);
     }
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   ret = fork ();
 
   if (ret < 0)
@@ -1125,6 +1130,11 @@ main (int argc, char **argv)
       exit (1);
     }
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   ret = fork ();
   if (ret < 0)
     {
@@ -1150,7 +1160,9 @@ main (int argc, char **argv)
       verbose ("=== Babysitter's intermediate parent created\n");
 
       /* Fork once more to create babysitter */
-      
+
+      fflush (stdout);
+      fflush (stderr);
       ret = fork ();
       if (ret < 0)
         {
index 0adb5ad1d1125dfb9c49b081e89007ee889368d9..6b93997ef46d8045a5cfaefd8ffb3a64987e7bb2 100644 (file)
@@ -354,6 +354,11 @@ main (int argc, char **argv)
       return 127;
     }
 
+  /* Make sure our output buffers aren't redundantly printed by both the
+   * parent and the child */
+  fflush (stdout);
+  fflush (stderr);
+
   bus_pid = fork ();
 
   if (bus_pid < 0)
@@ -401,6 +406,9 @@ main (int argc, char **argv)
       !dbus_setenv ("DBUS_STARTER_BUS_TYPE", NULL))
     oom ();
 
+  fflush (stdout);
+  fflush (stderr);
+
   app_pid = fork ();
 
   if (app_pid < 0)