Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-spawn.c
index 5ced84f..a4652a3 100644 (file)
@@ -1,10 +1,10 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-spawn.c Wrapper around fork/exec
  * 
- * Copyright (C) 2002, 2003  Red Hat, Inc.
+ * Copyright (C) 2002, 2003, 2004  Red Hat, Inc.
  * Copyright (C) 2003 CodeFactory AB
  *
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+
+#include <config.h>
+
 #include "dbus-spawn.h"
-#include "dbus-sysdeps.h"
+#include "dbus-sysdeps-unix.h"
 #include "dbus-internals.h"
 #include "dbus-test.h"
+#include "dbus-protocol.h"
 
 #include <unistd.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <sys/wait.h>
+#include <stdlib.h>
+#ifdef HAVE_ERRNO_H
 #include <errno.h>
+#endif
+
+extern char **environ;
 
 /**
  * @addtogroup DBusInternalsUtils
  * if you thought about it a bit.
  */
 
+/**
+ * Enumeration for status of a read()
+ */
 typedef enum
 {
-  READ_STATUS_OK,
-  READ_STATUS_ERROR,
-  READ_STATUS_EOF
+  READ_STATUS_OK,    /**< Read succeeded */
+  READ_STATUS_ERROR, /**< Some kind of error */
+  READ_STATUS_EOF    /**< EOF returned */
 } ReadStatus;
 
 static ReadStatus
@@ -65,15 +77,15 @@ read_ints (int        fd,
   
   while (TRUE)
     {
-      size_t chunk;
+      ssize_t chunk;
       size_t to_read;
-      
-    again:
 
       to_read = sizeof (int) * n_ints_in_buf - bytes;
 
       if (to_read == 0)
         break;
+
+    again:
       
       chunk = read (fd,
                     ((char*)buf) + bytes,
@@ -120,14 +132,15 @@ read_pid (int        fd,
   
   while (TRUE)
     {
-      size_t chunk;    
+      ssize_t chunk;
       size_t to_read;
-      
-    again:
+
       to_read = sizeof (pid_t) - bytes;
 
       if (to_read == 0)
         break;
+
+    again:
       
       chunk = read (fd,
                     ((char*)buf) + bytes,
@@ -172,28 +185,34 @@ enum
   CHILD_PID                /* Followed by pid_t */
 };
 
+/**
+ * Babysitter implementation details
+ */
 struct DBusBabysitter
 {
-  int refcount;
+  int refcount; /**< Reference count */
 
   char *executable; /**< executable name to use in error messages */
   
-  int socket_to_babysitter;
-  int error_pipe_from_child;
+  int socket_to_babysitter; /**< Connection to the babysitter process */
+  int error_pipe_from_child; /**< Connection to the process that does the exec() */
   
-  pid_t sitter_pid;
-  pid_t grandchild_pid;
+  pid_t sitter_pid;  /**< PID Of the babysitter */
+  pid_t grandchild_pid; /**< PID of the grandchild */
 
-  DBusWatchList *watches;
+  DBusWatchList *watches; /**< Watches */
 
-  DBusWatch *error_watch;
-  DBusWatch *sitter_watch;
+  DBusWatch *error_watch; /**< Error pipe watch */
+  DBusWatch *sitter_watch; /**< Sitter pipe watch */
 
-  int errnum;
-  int status;
-  unsigned int have_child_status : 1;
-  unsigned int have_fork_errnum : 1;
-  unsigned int have_exec_errnum : 1;
+  DBusBabysitterFinishedFunc finished_cb;
+  void *finished_data;
+
+  int errnum; /**< Error number */
+  int status; /**< Exit status code */
+  unsigned int have_child_status : 1; /**< True if child status has been reaped */
+  unsigned int have_fork_errnum : 1; /**< True if we have an error code from fork() */
+  unsigned int have_exec_errnum : 1; /**< True if we have an error code from exec() */
 };
 
 static DBusBabysitter*
@@ -228,18 +247,27 @@ _dbus_babysitter_new (void)
  * Increment the reference count on the babysitter object.
  *
  * @param sitter the babysitter
+ * @returns the babysitter
  */
-void
+DBusBabysitter *
 _dbus_babysitter_ref (DBusBabysitter *sitter)
 {
   _dbus_assert (sitter != NULL);
   _dbus_assert (sitter->refcount > 0);
   
   sitter->refcount += 1;
+
+  return sitter;
 }
 
+static void close_socket_to_babysitter  (DBusBabysitter *sitter);
+static void close_error_pipe_from_child (DBusBabysitter *sitter);
+
 /**
  * Decrement the reference count on the babysitter object.
+ * When the reference count of the babysitter object reaches
+ * zero, the babysitter is killed and the child that was being
+ * babysat gets emancipated.
  *
  * @param sitter the babysitter
  */
@@ -251,27 +279,39 @@ _dbus_babysitter_unref (DBusBabysitter *sitter)
   
   sitter->refcount -= 1;
   if (sitter->refcount == 0)
-    {      
-      if (sitter->socket_to_babysitter >= 0)
-        {
-          close (sitter->socket_to_babysitter);
-          sitter->socket_to_babysitter = -1;
-        }
+    {
+      /* If we haven't forked other babysitters
+       * since this babysitter and socket were
+       * created then this close will cause the
+       * babysitter to wake up from poll with
+       * a hangup and then the babysitter will
+       * quit itself.
+       */
+      close_socket_to_babysitter (sitter);
 
-      if (sitter->error_pipe_from_child >= 0)
-        {
-          close (sitter->error_pipe_from_child);
-          sitter->error_pipe_from_child = -1;
-        }
+      close_error_pipe_from_child (sitter);
 
-      if (sitter->sitter_pid != -1)
+      if (sitter->sitter_pid > 0)
         {
           int status;
           int ret;
 
-          /* Reap the babysitter */
+          /* It's possible the babysitter died on its own above 
+           * from the close, or was killed randomly
+           * by some other process, so first try to reap it
+           */
+          ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
+
+          /* If we couldn't reap the child then kill it, and
+           * try again
+           */
+          if (ret == 0)
+            kill (sitter->sitter_pid, SIGKILL);
+
         again:
-          ret = waitpid (sitter->sitter_pid, &status, 0);
+          if (ret == 0)
+            ret = waitpid (sitter->sitter_pid, &status, 0);
+
           if (ret < 0)
             {
               if (errno == EINTR)
@@ -284,6 +324,9 @@ _dbus_babysitter_unref (DBusBabysitter *sitter)
             }
           else
             {
+              _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
+                             (long) ret, (long) sitter->sitter_pid);
+              
               if (WIFEXITED (sitter->status))
                 _dbus_verbose ("Babysitter exited with status %d\n",
                                WEXITSTATUS (sitter->status));
@@ -293,22 +336,10 @@ _dbus_babysitter_unref (DBusBabysitter *sitter)
               else
                 _dbus_verbose ("Babysitter exited abnormally\n");
             }
-        }
-      
-      if (sitter->error_watch)
-        {
-          _dbus_watch_invalidate (sitter->error_watch);
-          _dbus_watch_unref (sitter->error_watch);
-          sitter->error_watch = NULL;
-        }
 
-      if (sitter->sitter_watch)
-        {
-          _dbus_watch_invalidate (sitter->sitter_watch);
-          _dbus_watch_unref (sitter->sitter_watch);
-          sitter->sitter_watch = NULL;
+          sitter->sitter_pid = -1;
         }
-      
+
       if (sitter->watches)
         _dbus_watch_list_free (sitter->watches);
 
@@ -324,11 +355,9 @@ read_data (DBusBabysitter *sitter,
 {
   int what;
   int got;
-  DBusError error;
+  DBusError error = DBUS_ERROR_INIT;
   ReadStatus r;
-  
-  dbus_error_init (&error);
-  
+
   r = read_ints (fd, &what, 1, &got, &error);
 
   switch (r)
@@ -375,6 +404,7 @@ read_data (DBusBabysitter *sitter,
                   {
                     sitter->have_child_status = TRUE;
                     sitter->status = arg;
+                    sitter->errnum = 0;
                     _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
                                    WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
                                    WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
@@ -430,16 +460,42 @@ static void
 close_socket_to_babysitter (DBusBabysitter *sitter)
 {
   _dbus_verbose ("Closing babysitter\n");
-  close (sitter->socket_to_babysitter);
-  sitter->socket_to_babysitter = -1;
+
+  if (sitter->sitter_watch != NULL)
+    {
+      _dbus_assert (sitter->watches != NULL);
+      _dbus_watch_list_remove_watch (sitter->watches,  sitter->sitter_watch);
+      _dbus_watch_invalidate (sitter->sitter_watch);
+      _dbus_watch_unref (sitter->sitter_watch);
+      sitter->sitter_watch = NULL;
+    }
+
+  if (sitter->socket_to_babysitter >= 0)
+    {
+      _dbus_close_socket (sitter->socket_to_babysitter, NULL);
+      sitter->socket_to_babysitter = -1;
+    }
 }
 
 static void
 close_error_pipe_from_child (DBusBabysitter *sitter)
 {
   _dbus_verbose ("Closing child error\n");
-  close (sitter->error_pipe_from_child);
-  sitter->error_pipe_from_child = -1;
+
+  if (sitter->error_watch != NULL)
+    {
+      _dbus_assert (sitter->watches != NULL);
+      _dbus_watch_list_remove_watch (sitter->watches,  sitter->error_watch);
+      _dbus_watch_invalidate (sitter->error_watch);
+      _dbus_watch_unref (sitter->error_watch);
+      sitter->error_watch = NULL;
+    }
+
+  if (sitter->error_pipe_from_child >= 0)
+    {
+      _dbus_close_socket (sitter->error_pipe_from_child, NULL);
+      sitter->error_pipe_from_child = -1;
+    }
 }
 
 static void
@@ -511,10 +567,21 @@ babysitter_iteration (DBusBabysitter *sitter,
     {
       int ret;
 
-      ret = _dbus_poll (fds, i, 0);
+      do
+        {
+          ret = _dbus_poll (fds, i, 0);
+        }
+      while (ret < 0 && errno == EINTR);
+
       if (ret == 0 && block)
-        ret = _dbus_poll (fds, i, -1);
-      
+        {
+          do
+            {
+              ret = _dbus_poll (fds, i, -1);
+            }
+          while (ret < 0 && errno == EINTR);
+        }
+
       if (ret > 0)
         {
           descriptors_ready = TRUE;
@@ -539,7 +606,6 @@ babysitter_iteration (DBusBabysitter *sitter,
  */
 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
 
-
 /**
  * Blocks until the babysitter process gives us the PID of the spawned grandchild,
  * then kills the spawned grandchild.
@@ -554,6 +620,9 @@ _dbus_babysitter_kill_child (DBusBabysitter *sitter)
          sitter->grandchild_pid == -1)
     babysitter_iteration (sitter, TRUE);
 
+  _dbus_verbose ("Got child PID %ld for killing\n",
+                 (long) sitter->grandchild_pid);
+  
   if (sitter->grandchild_pid == -1)
     return; /* child is already dead, or we're so hosed we'll never recover */
 
@@ -578,11 +647,31 @@ _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
   return sitter->socket_to_babysitter < 0;
 }
 
-static void
-_dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
+/**
+ * Gets the exit status of the child. We do this so implementation specific
+ * detail is not cluttering up dbus, for example the system launcher code.
+ * This can only be called if the child has exited, i.e. call
+ * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
+ * did not return a status code, e.g. because the child was signaled
+ * or we failed to ever launch the child in the first place.
+ *
+ * @param sitter the babysitter
+ * @param status the returned status code
+ * @returns #FALSE on failure
+ */
+dbus_bool_t
+_dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
+                                        int            *status)
 {
-  while (LIVE_CHILDREN (sitter))
-    babysitter_iteration (sitter, TRUE);
+  if (!_dbus_babysitter_get_child_exited (sitter))
+    _dbus_assert_not_reached ("Child has not exited");
+  
+  if (!sitter->have_child_status ||
+      !(WIFEXITED (sitter->status)))
+    return FALSE;
+
+  *status = WEXITSTATUS (sitter->status);
+  return TRUE;
 }
 
 /**
@@ -668,20 +757,12 @@ _dbus_babysitter_set_watch_functions (DBusBabysitter            *sitter,
                                          free_data_function);
 }
 
-/**
- * Handles watch when descriptors are ready.
- *
- * @param sitter the babysitter.
- * @param watch the watch object
- * @param condition the descriptor conditions
- * @returns #FALSE if there wasn't enough memory.
- * 
- */
-dbus_bool_t
-_dbus_babysitter_handle_watch (DBusBabysitter  *sitter,
-                               DBusWatch       *watch,
-                               unsigned int     condition)
+static dbus_bool_t
+handle_watch (DBusWatch       *watch,
+              unsigned int     condition,
+              void            *data)
 {
+  DBusBabysitter *sitter = _dbus_babysitter_ref (data);
   int revents;
   int fd;
   
@@ -693,17 +774,36 @@ _dbus_babysitter_handle_watch (DBusBabysitter  *sitter,
   if (condition & DBUS_WATCH_HANGUP)
     revents |= _DBUS_POLLHUP;
 
-  fd = dbus_watch_get_fd (watch);
+  fd = dbus_watch_get_socket (watch);
 
   if (fd == sitter->error_pipe_from_child)
     handle_error_pipe (sitter, revents);
   else if (fd == sitter->socket_to_babysitter)
     handle_babysitter_socket (sitter, revents);
-  
+
+  while (LIVE_CHILDREN (sitter) &&
+         babysitter_iteration (sitter, FALSE))
+    ;
+
+  /* fd.o #32992: if the handle_* methods closed their sockets, they previously
+   * didn't always remove the watches. Check that we don't regress. */
+  _dbus_assert (sitter->socket_to_babysitter != -1 || sitter->sitter_watch == NULL);
+  _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
+
+  if (_dbus_babysitter_get_child_exited (sitter) &&
+      sitter->finished_cb != NULL)
+    {
+      sitter->finished_cb (sitter, sitter->finished_data);
+      sitter->finished_cb = NULL;
+    }
+
+  _dbus_babysitter_unref (sitter);
   return TRUE;
 }
 
+/** Helps remember which end of the pipe is which */
 #define READ_END 0
+/** Helps remember which end of the pipe is which */
 #define WRITE_END 1
 
 
@@ -720,7 +820,7 @@ close_and_invalidate (int *fd)
     return -1;
   else
     {
-      ret = close (*fd);
+      ret = _dbus_close_socket (*fd, NULL);
       *fd = -1;
     }
 
@@ -731,9 +831,25 @@ static dbus_bool_t
 make_pipe (int         p[2],
            DBusError  *error)
 {
+  int retval;
+
+#ifdef HAVE_PIPE2
+  dbus_bool_t cloexec_done;
+
+  retval = pipe2 (p, O_CLOEXEC);
+  cloexec_done = retval >= 0;
+
+  /* Check if kernel seems to be too old to know pipe2(). We assume
+     that if pipe2 is available, O_CLOEXEC is too.  */
+  if (retval < 0 && errno == ENOSYS)
+#endif
+    {
+      retval = pipe(p);
+    }
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-  
-  if (pipe (p) < 0)
+
+  if (retval < 0)
     {
       dbus_set_error (error,
                      DBUS_ERROR_SPAWN_FAILED,
@@ -742,6 +858,14 @@ make_pipe (int         p[2],
       return FALSE;
     }
 
+#ifdef HAVE_PIPE2
+  if (!cloexec_done)
+#endif
+    {
+      _dbus_fd_set_close_on_exec (p[0]);
+      _dbus_fd_set_close_on_exec (p[1]);
+    }
+
   return TRUE;
 }
 
@@ -764,7 +888,7 @@ do_write (int fd, const void *buf, size_t count)
       else
         {
           _dbus_warn ("Failed to write data to pipe!\n");
-          _exit (1); /* give up, we suck */
+          exit (1); /* give up, we suck */
         }
     }
   else
@@ -782,7 +906,7 @@ write_err_and_exit (int fd, int msg)
   do_write (fd, &msg, sizeof (msg));
   do_write (fd, &en, sizeof (en));
   
-  _exit (1);
+  exit (1);
 }
 
 static void
@@ -802,12 +926,13 @@ write_status_and_exit (int fd, int status)
   do_write (fd, &msg, sizeof (msg));
   do_write (fd, &status, sizeof (status));
   
-  _exit (0);
+  exit (0);
 }
 
 static void
 do_exec (int                       child_err_report_fd,
         char                    **argv,
+        char                    **envp,
         DBusSpawnChildSetupFunc   child_setup,
         void                     *user_data)
 {
@@ -815,6 +940,10 @@ do_exec (int                       child_err_report_fd,
   int i, max_open;
 #endif
 
+  _dbus_verbose_reset ();
+  _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
+                 _dbus_getpid ());
+  
   if (child_setup)
     (* child_setup) (user_data);
 
@@ -834,8 +963,15 @@ do_exec (int                       child_err_report_fd,
        _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
     }
 #endif
+
+  if (envp == NULL)
+    {
+      _dbus_assert (environ != NULL);
+
+      envp = environ;
+    }
   
-  execv (argv[0], argv);
+  execve (argv[0], argv, envp);
   
   /* Exec failed */
   write_err_and_exit (child_err_report_fd,
@@ -850,7 +986,14 @@ check_babysit_events (pid_t grandchild_pid,
   pid_t ret;
   int status;
   
-  ret = waitpid (grandchild_pid, &status, WNOHANG);
+  do
+    {
+      ret = waitpid (grandchild_pid, &status, WNOHANG);
+      /* The man page says EINTR can't happen with WNOHANG,
+       * but there are reports of it (maybe only with valgrind?)
+       */
+    }
+  while (ret < 0 && errno == EINTR);
 
   if (ret == 0)
     {
@@ -863,30 +1006,32 @@ check_babysit_events (pid_t grandchild_pid,
       /* This isn't supposed to happen. */
       _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
                   _dbus_strerror (errno));
-      _exit (1);
+      exit (1);
     }
   else if (ret == grandchild_pid)
     {
       /* Child exited */
+      _dbus_verbose ("reaped child pid %ld\n", (long) ret);
+      
       write_status_and_exit (parent_pipe, status);
     }
   else
     {
       _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
                   (int) ret);
-      _exit (1);
+      exit (1);
     }
 
   if (revents & _DBUS_POLLIN)
     {
-      /* Data to read from parent */
-
+      _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
     }
 
   if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
     {
       /* Parent is gone, so we just exit */
-      _exit (0);
+      _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
+      exit (0);
     }
 }
 
@@ -897,19 +1042,22 @@ babysit_signal_handler (int signo)
 {
   char b = '\0';
  again:
-  write (babysit_sigchld_pipe, &b, 1);
-  if (errno == EINTR)
-    goto again;
+  if (write (babysit_sigchld_pipe, &b, 1) <= 0) 
+    if (errno == EINTR)
+      goto again;
 }
 
 static void
 babysit (pid_t grandchild_pid,
          int   parent_pipe)
-{  
-  struct sigaction act;
-  sigset_t empty_mask;
+{
   int sigchld_pipe[2];
 
+  /* We don't exec, so we keep parent state, such as the pid that
+   * _dbus_verbose() uses. Reset the pid here.
+   */
+  _dbus_verbose_reset ();
+  
   /* I thought SIGCHLD would just wake up the poll, but
    * that didn't seem to work, so added this pipe.
    * Probably the pipe is more likely to work on busted
@@ -918,16 +1066,12 @@ babysit (pid_t grandchild_pid,
   if (pipe (sigchld_pipe) < 0)
     {
       _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
-      _exit (1);
+      exit (1);
     }
 
   babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
-  
-  sigemptyset (&empty_mask);
-  act.sa_handler = babysit_signal_handler;
-  act.sa_mask    = empty_mask;
-  act.sa_flags   = 0;
-  sigaction (SIGCHLD,  &act, 0);
+
+  _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
   
   write_pid (parent_pipe, grandchild_pid);
 
@@ -945,7 +1089,11 @@ babysit (pid_t grandchild_pid,
       pfds[1].events = _DBUS_POLLIN;
       pfds[1].revents = 0;
       
-      _dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1);
+      if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
+        {
+          _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
+          exit (1);
+        }
 
       if (pfds[0].revents != 0)
         {
@@ -954,13 +1102,14 @@ babysit (pid_t grandchild_pid,
       else if (pfds[1].revents & _DBUS_POLLIN)
         {
           char b;
-          read (sigchld_pipe[READ_END], &b, 1);
+          if (read (sigchld_pipe[READ_END], &b, 1) == -1)
+            /* ignore */;
           /* do waitpid check */
           check_babysit_events (grandchild_pid, parent_pipe, 0);
         }
     }
   
-  _exit (1);
+  exit (1);
 }
 
 /**
@@ -976,6 +1125,7 @@ babysit (pid_t grandchild_pid,
  *
  * @param sitter_p return location for babysitter or #NULL
  * @param argv the executable and arguments
+ * @param env the environment (not used on unix yet)
  * @param child_setup function to call in child pre-exec()
  * @param user_data user data for setup function
  * @param error error object to be filled in if function fails
@@ -984,6 +1134,7 @@ babysit (pid_t grandchild_pid,
 dbus_bool_t
 _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
                                    char                    **argv,
+                                   char                    **env,
                                    DBusSpawnChildSetupFunc   child_setup,
                                    void                     *user_data,
                                    DBusError                *error)
@@ -995,7 +1146,9 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
   
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  *sitter_p = NULL;
+  if (sitter_p != NULL)
+    *sitter_p = NULL;
+
   sitter = NULL;
 
   sitter = _dbus_babysitter_new ();
@@ -1015,13 +1168,57 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
   if (!make_pipe (child_err_report_pipe, error))
     goto cleanup_and_fail;
 
-  _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
-  
   if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
     goto cleanup_and_fail;
 
-  _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
-  _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
+  /* Setting up the babysitter is only useful in the parent,
+   * but we don't want to run out of memory and fail
+   * after we've already forked, since then we'd leak
+   * child processes everywhere.
+   */
+  sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
+                                         DBUS_WATCH_READABLE,
+                                         TRUE, handle_watch, sitter, NULL);
+  if (sitter->error_watch == NULL)
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto cleanup_and_fail;
+    }
+        
+  if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->error_watch))
+    {
+      /* we need to free it early so the destructor won't try to remove it
+       * without it having been added, which DBusLoop doesn't allow */
+      _dbus_watch_invalidate (sitter->error_watch);
+      _dbus_watch_unref (sitter->error_watch);
+      sitter->error_watch = NULL;
+
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto cleanup_and_fail;
+    }
+      
+  sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
+                                          DBUS_WATCH_READABLE,
+                                          TRUE, handle_watch, sitter, NULL);
+  if (sitter->sitter_watch == NULL)
+    {
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto cleanup_and_fail;
+    }
+      
+  if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->sitter_watch))
+    {
+      /* we need to free it early so the destructor won't try to remove it
+       * without it having been added, which DBusLoop doesn't allow */
+      _dbus_watch_invalidate (sitter->sitter_watch);
+      _dbus_watch_unref (sitter->sitter_watch);
+      sitter->sitter_watch = NULL;
+
+      dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
+      goto cleanup_and_fail;
+    }
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
   pid = fork ();
   
@@ -1060,6 +1257,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
        {
          do_exec (child_err_report_pipe[WRITE_END],
                   argv,
+                  env,
                   child_setup, user_data);
           _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
        }
@@ -1070,38 +1268,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
        }
     }
   else
-    {
-      /* Parent */
-      sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
-                                             DBUS_WATCH_READABLE,
-                                             TRUE);
-      if (sitter->error_watch == NULL)
-        {
-          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-          goto cleanup_and_fail;
-        }
-        
-      if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->error_watch))
-        {
-          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-          goto cleanup_and_fail;
-        }
-      
-      sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
-                                              DBUS_WATCH_READABLE,
-                                              TRUE);
-      if (sitter->sitter_watch == NULL)
-        {
-          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-          goto cleanup_and_fail;
-        }
-      
-      if (!_dbus_watch_list_add_watch (sitter->watches,  sitter->sitter_watch))
-        {
-          dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
-          goto cleanup_and_fail;
-        }
-      
+    {      
       /* Close the uncared-about ends of the pipes */
       close_and_invalidate (&child_err_report_pipe[WRITE_END]);
       close_and_invalidate (&babysitter_pipe[1]);
@@ -1119,6 +1286,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
       else
         _dbus_babysitter_unref (sitter);
 
+      dbus_free_string_array (env);
+
       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
       
       return TRUE;
@@ -1139,26 +1308,38 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
   return FALSE;
 }
 
+void
+_dbus_babysitter_set_result_function  (DBusBabysitter             *sitter,
+                                       DBusBabysitterFinishedFunc  finished,
+                                       void                       *user_data)
+{
+  sitter->finished_cb = finished;
+  sitter->finished_data = user_data;
+}
+
 /** @} */
 
 #ifdef DBUS_BUILD_TESTS
 
+static void
+_dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
+{
+  while (LIVE_CHILDREN (sitter))
+    babysitter_iteration (sitter, TRUE);
+}
+
 static dbus_bool_t
 check_spawn_nonexistent (void *data)
 {
   char *argv[4] = { NULL, NULL, NULL, NULL };
-  DBusBabysitter *sitter;
-  DBusError error;
-  
-  sitter = NULL;
-  
-  dbus_error_init (&error);
+  DBusBabysitter *sitter = NULL;
+  DBusError error = DBUS_ERROR_INIT;
 
   /*** Test launching nonexistent binary */
   
   argv[0] = "/this/does/not/exist/32542sdgafgafdg";
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
-                                         NULL, NULL,
+                                         NULL, NULL, NULL,
                                          &error))
     {
       _dbus_babysitter_block_for_child_exit (sitter);
@@ -1192,18 +1373,14 @@ static dbus_bool_t
 check_spawn_segfault (void *data)
 {
   char *argv[4] = { NULL, NULL, NULL, NULL };
-  DBusBabysitter *sitter;
-  DBusError error;
-  
-  sitter = NULL;
-  
-  dbus_error_init (&error);
+  DBusBabysitter *sitter = NULL;
+  DBusError error = DBUS_ERROR_INIT;
 
   /*** Test launching segfault binary */
   
   argv[0] = TEST_SEGFAULT_BINARY;
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
-                                         NULL, NULL,
+                                         NULL, NULL, NULL,
                                          &error))
     {
       _dbus_babysitter_block_for_child_exit (sitter);
@@ -1237,18 +1414,14 @@ static dbus_bool_t
 check_spawn_exit (void *data)
 {
   char *argv[4] = { NULL, NULL, NULL, NULL };
-  DBusBabysitter *sitter;
-  DBusError error;
-  
-  sitter = NULL;
-  
-  dbus_error_init (&error);
+  DBusBabysitter *sitter = NULL;
+  DBusError error = DBUS_ERROR_INIT;
 
   /*** Test launching exit failure binary */
   
   argv[0] = TEST_EXIT_BINARY;
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
-                                         NULL, NULL,
+                                         NULL, NULL, NULL,
                                          &error))
     {
       _dbus_babysitter_block_for_child_exit (sitter);
@@ -1282,18 +1455,14 @@ static dbus_bool_t
 check_spawn_and_kill (void *data)
 {
   char *argv[4] = { NULL, NULL, NULL, NULL };
-  DBusBabysitter *sitter;
-  DBusError error;
-  
-  sitter = NULL;
-  
-  dbus_error_init (&error);
+  DBusBabysitter *sitter = NULL;
+  DBusError error = DBUS_ERROR_INIT;
 
   /*** Test launching sleeping binary then killing it */
 
   argv[0] = TEST_SLEEP_FOREVER_BINARY;
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
-                                         NULL, NULL,
+                                         NULL, NULL, NULL,
                                          &error))
     {
       _dbus_babysitter_kill_child (sitter);