Commit 13b64054 authored by Simon McVittie's avatar Simon McVittie

Unix: Flush stdout and stderr streams before forking

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's avatarPhilip Withnall <withnall@endlessm.com>
Signed-off-by: Simon McVittie's avatarSimon McVittie <smcv@collabora.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=103601
parent 3d2300ef
......@@ -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 ();
......
......@@ -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)
{
......
......@@ -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:
......
#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);
......
......@@ -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)
{
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment