gtestdbus: Don’t close stdout for dbus-daemon
authorPhilip Withnall <philip@tecnocode.co.uk>
Sat, 21 Jun 2014 22:01:08 +0000 (23:01 +0100)
committerPhilip Withnall <philip@tecnocode.co.uk>
Sun, 22 Jun 2014 12:02:18 +0000 (13:02 +0100)
The stdout FD passed to dbus-daemon is propagated to all its child
processes, such as service activated processes. If we close the FD after
reading the bus address from the daemon, any child process which
subsequently writes to stdout (e.g. for logging) will get a SIGPIPE and
explode.

Instead of closing the stdout FD immediately after dbus-daemon has
spawned, keep it open until the daemon is killed.

https://bugzilla.gnome.org/show_bug.cgi?id=732019

gio/gtestdbus.c

index 4b51d29..4af270b 100644 (file)
@@ -418,6 +418,7 @@ struct _GTestDBusPrivate
   GTestDBusFlags flags;
   GPtrArray *service_dirs;
   GPid bus_pid;
+  gint bus_stdout_fd;
   gchar *bus_address;
   gboolean up;
 };
@@ -581,8 +582,8 @@ start_daemon (GTestDBus *self)
   const gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
   gchar *config_path;
   gchar *config_arg;
-  gint stdout_fd;
   GIOChannel *channel;
+  gint stdout_fd2;
   gsize termpos;
   GError *error = NULL;
 
@@ -607,15 +608,21 @@ start_daemon (GTestDBus *self)
                             NULL,
                             &self->priv->bus_pid,
                             NULL,
-                            &stdout_fd,
+                            &self->priv->bus_stdout_fd,
                             NULL,
                             &error);
   g_assert_no_error (error);
 
   _g_test_watcher_add_pid (self->priv->bus_pid);
 
-  /* Read bus address from daemon' stdout */
-  channel = g_io_channel_unix_new (stdout_fd);
+  /* Read bus address from daemon' stdout. We have to be careful to avoid
+   * closing the FD, as it is passed to any D-Bus service activated processes,
+   * and if we close it, they will get a SIGPIPE and die when they try to write
+   * to their stdout. */
+  stdout_fd2 = dup (self->priv->bus_stdout_fd);
+  g_assert_cmpint (stdout_fd2, >=, 0);
+  channel = g_io_channel_unix_new (stdout_fd2);
+
   g_io_channel_read_line (channel, &self->priv->bus_address, NULL,
       &termpos, &error);
   g_assert_no_error (error);
@@ -659,6 +666,8 @@ stop_daemon (GTestDBus *self)
   _g_test_watcher_remove_pid (self->priv->bus_pid);
   g_spawn_close_pid (self->priv->bus_pid);
   self->priv->bus_pid = 0;
+  close (self->priv->bus_stdout_fd);
+  self->priv->bus_stdout_fd = -1;
 
   g_free (self->priv->bus_address);
   self->priv->bus_address = NULL;