[rename] renamed kdbus related macros
[platform/upstream/dbus.git] / dbus / dbus-spawn.c
index 5654380..f8d5fd1 100644 (file)
@@ -205,6 +205,9 @@ struct DBusBabysitter
   DBusWatch *error_watch; /**< Error pipe watch */
   DBusWatch *sitter_watch; /**< Sitter pipe watch */
 
+  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 */
@@ -787,6 +790,13 @@ handle_watch (DBusWatch       *watch,
   _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;
 }
@@ -926,7 +936,7 @@ do_exec (int                       child_err_report_fd,
         DBusSpawnChildSetupFunc   child_setup,
         void                     *user_data)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   int i, max_open;
 #endif
 
@@ -937,7 +947,7 @@ do_exec (int                       child_err_report_fd,
   if (child_setup)
     (* child_setup) (user_data);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   max_open = sysconf (_SC_OPEN_MAX);
   
   for (i = 3; i < max_open; i++)
@@ -1093,7 +1103,9 @@ babysit (pid_t grandchild_pid,
         {
           char b;
           if (read (sigchld_pipe[READ_END], &b, 1) == -1)
-            /* ignore */;
+            {
+              /* ignore */
+            }
           /* do waitpid check */
           check_babysit_events (grandchild_pid, parent_pipe, 0);
         }
@@ -1244,7 +1256,11 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter          **sitter_p,
           _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
        }
       else if (grandchild_pid == 0)
-       {
+      {
+          /* Go back to ignoring SIGPIPE, since it's evil
+           */
+          signal (SIGPIPE, SIG_IGN);
+
          do_exec (child_err_report_pipe[WRITE_END],
                   argv,
                   env,
@@ -1298,9 +1314,42 @@ _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
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+
+static char *
+get_test_exec (const char *exe,
+               DBusString *scratch_space)
+{
+  const char *dbus_test_exec;
+
+  dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
+
+  if (dbus_test_exec == NULL)
+    dbus_test_exec = DBUS_TEST_EXEC;
+
+  if (!_dbus_string_init (scratch_space))
+    return NULL;
+
+  if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
+                                   dbus_test_exec, exe, DBUS_EXEEXT))
+    {
+      _dbus_string_free (scratch_space);
+      return NULL;
+    }
+
+  return _dbus_string_get_data (scratch_space);
+}
 
 static void
 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
@@ -1356,10 +1405,18 @@ check_spawn_segfault (void *data)
   char *argv[4] = { NULL, NULL, NULL, NULL };
   DBusBabysitter *sitter = NULL;
   DBusError error = DBUS_ERROR_INIT;
+  DBusString argv0;
 
   /*** Test launching segfault binary */
-  
-  argv[0] = TEST_SEGFAULT_BINARY;
+
+  argv[0] = get_test_exec ("test-segfault", &argv0);
+
+  if (argv[0] == NULL)
+    {
+      /* OOM was simulated, never mind */
+      return TRUE;
+    }
+
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
                                          NULL, NULL, NULL,
                                          &error))
@@ -1368,6 +1425,8 @@ check_spawn_segfault (void *data)
       _dbus_babysitter_set_child_exit_error (sitter, &error);
     }
 
+  _dbus_string_free (&argv0);
+
   if (sitter)
     _dbus_babysitter_unref (sitter);
 
@@ -1397,10 +1456,18 @@ check_spawn_exit (void *data)
   char *argv[4] = { NULL, NULL, NULL, NULL };
   DBusBabysitter *sitter = NULL;
   DBusError error = DBUS_ERROR_INIT;
+  DBusString argv0;
 
   /*** Test launching exit failure binary */
-  
-  argv[0] = TEST_EXIT_BINARY;
+
+  argv[0] = get_test_exec ("test-exit", &argv0);
+
+  if (argv[0] == NULL)
+    {
+      /* OOM was simulated, never mind */
+      return TRUE;
+    }
+
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
                                          NULL, NULL, NULL,
                                          &error))
@@ -1409,6 +1476,8 @@ check_spawn_exit (void *data)
       _dbus_babysitter_set_child_exit_error (sitter, &error);
     }
 
+  _dbus_string_free (&argv0);
+
   if (sitter)
     _dbus_babysitter_unref (sitter);
 
@@ -1438,10 +1507,18 @@ check_spawn_and_kill (void *data)
   char *argv[4] = { NULL, NULL, NULL, NULL };
   DBusBabysitter *sitter = NULL;
   DBusError error = DBUS_ERROR_INIT;
+  DBusString argv0;
 
   /*** Test launching sleeping binary then killing it */
 
-  argv[0] = TEST_SLEEP_FOREVER_BINARY;
+  argv[0] = get_test_exec ("test-sleep-forever", &argv0);
+
+  if (argv[0] == NULL)
+    {
+      /* OOM was simulated, never mind */
+      return TRUE;
+    }
+
   if (_dbus_spawn_async_with_babysitter (&sitter, argv,
                                          NULL, NULL, NULL,
                                          &error))
@@ -1453,6 +1530,8 @@ check_spawn_and_kill (void *data)
       _dbus_babysitter_set_child_exit_error (sitter, &error);
     }
 
+  _dbus_string_free (&argv0);
+
   if (sitter)
     _dbus_babysitter_unref (sitter);