1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-spawn.c Wrapper around fork/exec
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-spawn.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-internals.h"
27 #include "dbus-test.h"
36 * @addtogroup DBusInternalsUtils
41 * I'm pretty sure this whole spawn file could be made simpler,
42 * if you thought about it a bit.
46 * Enumeration for status of a read()
50 READ_STATUS_OK, /**< Read succeeded */
51 READ_STATUS_ERROR, /**< Some kind of error */
52 READ_STATUS_EOF /**< EOF returned */
65 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
67 retval = READ_STATUS_OK;
76 to_read = sizeof (int) * n_ints_in_buf - bytes;
85 if (chunk < 0 && errno == EINTR)
90 dbus_set_error (error,
91 DBUS_ERROR_SPAWN_FAILED,
92 "Failed to read from child pipe (%s)",
93 _dbus_strerror (errno));
95 retval = READ_STATUS_ERROR;
100 retval = READ_STATUS_EOF;
107 *n_ints_read = (int)(bytes / sizeof(int));
120 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
122 retval = READ_STATUS_OK;
130 to_read = sizeof (pid_t) - bytes;
136 ((char*)buf) + bytes,
138 if (chunk < 0 && errno == EINTR)
143 dbus_set_error (error,
144 DBUS_ERROR_SPAWN_FAILED,
145 "Failed to read from child pipe (%s)",
146 _dbus_strerror (errno));
148 retval = READ_STATUS_ERROR;
153 retval = READ_STATUS_EOF;
163 /* The implementation uses an intermediate child between the main process
164 * and the grandchild. The grandchild is our spawned process. The intermediate
165 * child is a babysitter process; it keeps track of when the grandchild
166 * exits/crashes, and reaps the grandchild.
169 /* Messages from children to parents */
172 CHILD_EXITED, /* This message is followed by the exit status int */
173 CHILD_FORK_FAILED, /* Followed by errno */
174 CHILD_EXEC_FAILED, /* Followed by errno */
175 CHILD_PID /* Followed by pid_t */
178 struct DBusBabysitter
182 char *executable; /**< executable name to use in error messages */
184 int socket_to_babysitter;
185 int error_pipe_from_child;
188 pid_t grandchild_pid;
190 DBusWatchList *watches;
192 DBusWatch *error_watch;
193 DBusWatch *sitter_watch;
197 unsigned int have_child_status : 1;
198 unsigned int have_fork_errnum : 1;
199 unsigned int have_exec_errnum : 1;
202 static DBusBabysitter*
203 _dbus_babysitter_new (void)
205 DBusBabysitter *sitter;
207 sitter = dbus_new0 (DBusBabysitter, 1);
211 sitter->refcount = 1;
213 sitter->socket_to_babysitter = -1;
214 sitter->error_pipe_from_child = -1;
216 sitter->sitter_pid = -1;
217 sitter->grandchild_pid = -1;
219 sitter->watches = _dbus_watch_list_new ();
220 if (sitter->watches == NULL)
226 _dbus_babysitter_unref (sitter);
231 * Increment the reference count on the babysitter object.
233 * @param sitter the babysitter
236 _dbus_babysitter_ref (DBusBabysitter *sitter)
238 _dbus_assert (sitter != NULL);
239 _dbus_assert (sitter->refcount > 0);
241 sitter->refcount += 1;
245 * Decrement the reference count on the babysitter object.
247 * @param sitter the babysitter
250 _dbus_babysitter_unref (DBusBabysitter *sitter)
252 _dbus_assert (sitter != NULL);
253 _dbus_assert (sitter->refcount > 0);
255 sitter->refcount -= 1;
256 if (sitter->refcount == 0)
258 if (sitter->socket_to_babysitter >= 0)
260 close (sitter->socket_to_babysitter);
261 sitter->socket_to_babysitter = -1;
264 if (sitter->error_pipe_from_child >= 0)
266 close (sitter->error_pipe_from_child);
267 sitter->error_pipe_from_child = -1;
270 if (sitter->sitter_pid != -1)
275 /* Reap the babysitter */
277 ret = waitpid (sitter->sitter_pid, &status, 0);
282 else if (errno == ECHILD)
283 _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
285 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
286 errno, _dbus_strerror (errno));
290 if (WIFEXITED (sitter->status))
291 _dbus_verbose ("Babysitter exited with status %d\n",
292 WEXITSTATUS (sitter->status));
293 else if (WIFSIGNALED (sitter->status))
294 _dbus_verbose ("Babysitter received signal %d\n",
295 WTERMSIG (sitter->status));
297 _dbus_verbose ("Babysitter exited abnormally\n");
300 sitter->sitter_pid = -1;
303 if (sitter->error_watch)
305 _dbus_watch_invalidate (sitter->error_watch);
306 _dbus_watch_unref (sitter->error_watch);
307 sitter->error_watch = NULL;
310 if (sitter->sitter_watch)
312 _dbus_watch_invalidate (sitter->sitter_watch);
313 _dbus_watch_unref (sitter->sitter_watch);
314 sitter->sitter_watch = NULL;
318 _dbus_watch_list_free (sitter->watches);
320 dbus_free (sitter->executable);
327 read_data (DBusBabysitter *sitter,
335 dbus_error_init (&error);
337 r = read_ints (fd, &what, 1, &got, &error);
341 case READ_STATUS_ERROR:
342 _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
343 dbus_error_free (&error);
346 case READ_STATUS_EOF:
358 case CHILD_FORK_FAILED:
359 case CHILD_EXEC_FAILED:
363 r = read_ints (fd, &arg, 1, &got, &error);
367 case READ_STATUS_ERROR:
368 _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
369 dbus_error_free (&error);
371 case READ_STATUS_EOF:
379 if (what == CHILD_EXITED)
381 sitter->have_child_status = TRUE;
382 sitter->status = arg;
383 _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
384 WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
385 WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
387 else if (what == CHILD_FORK_FAILED)
389 sitter->have_fork_errnum = TRUE;
390 sitter->errnum = arg;
391 _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
393 else if (what == CHILD_EXEC_FAILED)
395 sitter->have_exec_errnum = TRUE;
396 sitter->errnum = arg;
397 _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
406 r = read_pid (fd, &pid, &error);
410 case READ_STATUS_ERROR:
411 _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
412 dbus_error_free (&error);
414 case READ_STATUS_EOF:
420 sitter->grandchild_pid = pid;
422 _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
426 _dbus_warn ("Unknown message received from babysitter process\n");
435 close_socket_to_babysitter (DBusBabysitter *sitter)
437 _dbus_verbose ("Closing babysitter\n");
438 close (sitter->socket_to_babysitter);
439 sitter->socket_to_babysitter = -1;
443 close_error_pipe_from_child (DBusBabysitter *sitter)
445 _dbus_verbose ("Closing child error\n");
446 close (sitter->error_pipe_from_child);
447 sitter->error_pipe_from_child = -1;
451 handle_babysitter_socket (DBusBabysitter *sitter,
454 /* Even if we have POLLHUP, we want to keep reading
455 * data until POLLIN goes away; so this function only
456 * looks at HUP/ERR if no IN is set.
458 if (revents & _DBUS_POLLIN)
460 _dbus_verbose ("Reading data from babysitter\n");
461 if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
462 close_socket_to_babysitter (sitter);
464 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
466 close_socket_to_babysitter (sitter);
471 handle_error_pipe (DBusBabysitter *sitter,
474 if (revents & _DBUS_POLLIN)
476 _dbus_verbose ("Reading data from child error\n");
477 if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
478 close_error_pipe_from_child (sitter);
480 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
482 close_error_pipe_from_child (sitter);
486 /* returns whether there were any poll events handled */
488 babysitter_iteration (DBusBabysitter *sitter,
493 dbus_bool_t descriptors_ready;
495 descriptors_ready = FALSE;
499 if (sitter->error_pipe_from_child >= 0)
501 fds[i].fd = sitter->error_pipe_from_child;
502 fds[i].events = _DBUS_POLLIN;
507 if (sitter->socket_to_babysitter >= 0)
509 fds[i].fd = sitter->socket_to_babysitter;
510 fds[i].events = _DBUS_POLLIN;
519 ret = _dbus_poll (fds, i, 0);
520 if (ret == 0 && block)
521 ret = _dbus_poll (fds, i, -1);
525 descriptors_ready = TRUE;
530 if (fds[i].fd == sitter->error_pipe_from_child)
531 handle_error_pipe (sitter, fds[i].revents);
532 else if (fds[i].fd == sitter->socket_to_babysitter)
533 handle_babysitter_socket (sitter, fds[i].revents);
538 return descriptors_ready;
542 * Macro returns #TRUE if the babysitter still has live sockets open to the
543 * babysitter child or the grandchild.
545 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
548 * Blocks until the babysitter process gives us the PID of the spawned grandchild,
549 * then kills the spawned grandchild.
551 * @param sitter the babysitter object
554 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
556 /* be sure we have the PID of the child */
557 while (LIVE_CHILDREN (sitter) &&
558 sitter->grandchild_pid == -1)
559 babysitter_iteration (sitter, TRUE);
561 _dbus_verbose ("Got child PID %ld for killing\n",
562 (long) sitter->grandchild_pid);
564 if (sitter->grandchild_pid == -1)
565 return; /* child is already dead, or we're so hosed we'll never recover */
567 kill (sitter->grandchild_pid, SIGKILL);
571 * Checks whether the child has exited, without blocking.
573 * @param sitter the babysitter
576 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
579 /* Be sure we're up-to-date */
580 while (LIVE_CHILDREN (sitter) &&
581 babysitter_iteration (sitter, FALSE))
584 /* We will have exited the babysitter when the child has exited */
585 return sitter->socket_to_babysitter < 0;
589 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
591 while (LIVE_CHILDREN (sitter))
592 babysitter_iteration (sitter, TRUE);
596 * Sets the #DBusError with an explanation of why the spawned
597 * child process exited (on a signal, or whatever). If
598 * the child process has not exited, does nothing (error
599 * will remain unset).
601 * @param sitter the babysitter
602 * @param error an error to fill in
605 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
608 if (!_dbus_babysitter_get_child_exited (sitter))
611 /* Note that if exec fails, we will also get a child status
612 * from the babysitter saying the child exited,
613 * so we need to give priority to the exec error
615 if (sitter->have_exec_errnum)
617 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
618 "Failed to execute program %s: %s",
619 sitter->executable, _dbus_strerror (sitter->errnum));
621 else if (sitter->have_fork_errnum)
623 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
624 "Failed to fork a new process %s: %s",
625 sitter->executable, _dbus_strerror (sitter->errnum));
627 else if (sitter->have_child_status)
629 if (WIFEXITED (sitter->status))
630 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
631 "Process %s exited with status %d",
632 sitter->executable, WEXITSTATUS (sitter->status));
633 else if (WIFSIGNALED (sitter->status))
634 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
635 "Process %s received signal %d",
636 sitter->executable, WTERMSIG (sitter->status));
638 dbus_set_error (error, DBUS_ERROR_FAILED,
639 "Process %s exited abnormally",
644 dbus_set_error (error, DBUS_ERROR_FAILED,
645 "Process %s exited, reason unknown",
651 * Sets watch functions to notify us when the
652 * babysitter object needs to read/write file descriptors.
654 * @param sitter the babysitter
655 * @param add_function function to begin monitoring a new descriptor.
656 * @param remove_function function to stop monitoring a descriptor.
657 * @param toggled_function function to notify when the watch is enabled/disabled
658 * @param data data to pass to add_function and remove_function.
659 * @param free_data_function function to be called to free the data.
660 * @returns #FALSE on failure (no memory)
663 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
664 DBusAddWatchFunction add_function,
665 DBusRemoveWatchFunction remove_function,
666 DBusWatchToggledFunction toggled_function,
668 DBusFreeFunction free_data_function)
670 return _dbus_watch_list_set_functions (sitter->watches,
679 * Handles watch when descriptors are ready.
681 * @param sitter the babysitter.
682 * @param watch the watch object
683 * @param condition the descriptor conditions
684 * @returns #FALSE if there wasn't enough memory.
688 _dbus_babysitter_handle_watch (DBusBabysitter *sitter,
690 unsigned int condition)
696 if (condition & DBUS_WATCH_READABLE)
697 revents |= _DBUS_POLLIN;
698 if (condition & DBUS_WATCH_ERROR)
699 revents |= _DBUS_POLLERR;
700 if (condition & DBUS_WATCH_HANGUP)
701 revents |= _DBUS_POLLHUP;
703 fd = dbus_watch_get_fd (watch);
705 if (fd == sitter->error_pipe_from_child)
706 handle_error_pipe (sitter, revents);
707 else if (fd == sitter->socket_to_babysitter)
708 handle_babysitter_socket (sitter, revents);
710 while (LIVE_CHILDREN (sitter) &&
711 babysitter_iteration (sitter, FALSE))
717 /** Helps remember which end of the pipe is which */
719 /** Helps remember which end of the pipe is which */
723 /* Avoids a danger in threaded situations (calling close()
724 * on a file descriptor twice, and another thread has
725 * re-opened it since the first close)
728 close_and_invalidate (int *fd)
747 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
751 dbus_set_error (error,
752 DBUS_ERROR_SPAWN_FAILED,
753 "Failed to create pipe for communicating with child process (%s)",
754 _dbus_strerror (errno));
762 do_write (int fd, const void *buf, size_t count)
764 size_t bytes_written;
771 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
779 _dbus_warn ("Failed to write data to pipe!\n");
780 _exit (1); /* give up, we suck */
784 bytes_written += ret;
786 if (bytes_written < count)
791 write_err_and_exit (int fd, int msg)
795 do_write (fd, &msg, sizeof (msg));
796 do_write (fd, &en, sizeof (en));
802 write_pid (int fd, pid_t pid)
806 do_write (fd, &msg, sizeof (msg));
807 do_write (fd, &pid, sizeof (pid));
811 write_status_and_exit (int fd, int status)
813 int msg = CHILD_EXITED;
815 do_write (fd, &msg, sizeof (msg));
816 do_write (fd, &status, sizeof (status));
822 do_exec (int child_err_report_fd,
824 DBusSpawnChildSetupFunc child_setup,
827 #ifdef DBUS_BUILD_TESTS
831 _dbus_verbose_reset ();
832 _dbus_verbose ("Child process has PID %lu\n",
836 (* child_setup) (user_data);
838 #ifdef DBUS_BUILD_TESTS
839 max_open = sysconf (_SC_OPEN_MAX);
841 for (i = 3; i < max_open; i++)
845 if (i == child_err_report_fd)
848 retval = fcntl (i, F_GETFD);
850 if (retval != -1 && !(retval & FD_CLOEXEC))
851 _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
855 execv (argv[0], argv);
858 write_err_and_exit (child_err_report_fd,
863 check_babysit_events (pid_t grandchild_pid,
870 ret = waitpid (grandchild_pid, &status, WNOHANG);
874 _dbus_verbose ("no child exited\n");
876 ; /* no child exited */
880 /* This isn't supposed to happen. */
881 _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
882 _dbus_strerror (errno));
885 else if (ret == grandchild_pid)
888 _dbus_verbose ("reaped child pid %ld\n", (long) ret);
890 write_status_and_exit (parent_pipe, status);
894 _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
899 if (revents & _DBUS_POLLIN)
901 _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
904 if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
906 /* Parent is gone, so we just exit */
907 _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
912 static int babysit_sigchld_pipe = -1;
915 babysit_signal_handler (int signo)
919 write (babysit_sigchld_pipe, &b, 1);
925 babysit (pid_t grandchild_pid,
930 /* We don't exec, so we keep parent state, such as the pid that
931 * _dbus_verbose() uses. Reset the pid here.
933 _dbus_verbose_reset ();
935 /* I thought SIGCHLD would just wake up the poll, but
936 * that didn't seem to work, so added this pipe.
937 * Probably the pipe is more likely to work on busted
938 * operating systems anyhow.
940 if (pipe (sigchld_pipe) < 0)
942 _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
946 babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
948 _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
950 write_pid (parent_pipe, grandchild_pid);
952 check_babysit_events (grandchild_pid, parent_pipe, 0);
958 pfds[0].fd = parent_pipe;
959 pfds[0].events = _DBUS_POLLIN;
962 pfds[1].fd = sigchld_pipe[READ_END];
963 pfds[1].events = _DBUS_POLLIN;
966 _dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1);
968 if (pfds[0].revents != 0)
970 check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
972 else if (pfds[1].revents & _DBUS_POLLIN)
975 read (sigchld_pipe[READ_END], &b, 1);
976 /* do waitpid check */
977 check_babysit_events (grandchild_pid, parent_pipe, 0);
985 * Spawns a new process. The executable name and argv[0]
986 * are the same, both are provided in argv[0]. The child_setup
987 * function is passed the given user_data and is run in the child
988 * just before calling exec().
990 * Also creates a "babysitter" which tracks the status of the
991 * child process, advising the parent if the child exits.
992 * If the spawn fails, no babysitter is created.
993 * If sitter_p is #NULL, no babysitter is kept.
995 * @param sitter_p return location for babysitter or #NULL
996 * @param argv the executable and arguments
997 * @param child_setup function to call in child pre-exec()
998 * @param user_data user data for setup function
999 * @param error error object to be filled in if function fails
1000 * @returns #TRUE on success, #FALSE if error is filled in
1003 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
1005 DBusSpawnChildSetupFunc child_setup,
1009 DBusBabysitter *sitter;
1010 int child_err_report_pipe[2] = { -1, -1 };
1011 int babysitter_pipe[2] = { -1, -1 };
1014 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1019 sitter = _dbus_babysitter_new ();
1022 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1026 sitter->executable = _dbus_strdup (argv[0]);
1027 if (sitter->executable == NULL)
1029 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1030 goto cleanup_and_fail;
1033 if (!make_pipe (child_err_report_pipe, error))
1034 goto cleanup_and_fail;
1036 _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
1038 if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1039 goto cleanup_and_fail;
1041 _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
1042 _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
1044 /* Setting up the babysitter is only useful in the parent,
1045 * but we don't want to run out of memory and fail
1046 * after we've already forked, since then we'd leak
1047 * child processes everywhere.
1049 sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1050 DBUS_WATCH_READABLE,
1052 if (sitter->error_watch == NULL)
1054 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1055 goto cleanup_and_fail;
1058 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1060 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1061 goto cleanup_and_fail;
1064 sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1065 DBUS_WATCH_READABLE,
1067 if (sitter->sitter_watch == NULL)
1069 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1070 goto cleanup_and_fail;
1073 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1075 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1076 goto cleanup_and_fail;
1079 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1085 dbus_set_error (error,
1086 DBUS_ERROR_SPAWN_FORK_FAILED,
1087 "Failed to fork (%s)",
1088 _dbus_strerror (errno));
1089 goto cleanup_and_fail;
1093 /* Immediate child, this is the babysitter process. */
1096 /* Be sure we crash if the parent exits
1097 * and we write to the err_report_pipe
1099 signal (SIGPIPE, SIG_DFL);
1101 /* Close the parent's end of the pipes. */
1102 close_and_invalidate (&child_err_report_pipe[READ_END]);
1103 close_and_invalidate (&babysitter_pipe[0]);
1105 /* Create the child that will exec () */
1106 grandchild_pid = fork ();
1108 if (grandchild_pid < 0)
1110 write_err_and_exit (babysitter_pipe[1],
1112 _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1114 else if (grandchild_pid == 0)
1116 do_exec (child_err_report_pipe[WRITE_END],
1118 child_setup, user_data);
1119 _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1123 babysit (grandchild_pid, babysitter_pipe[1]);
1124 _dbus_assert_not_reached ("Got to code after babysit()");
1129 /* Close the uncared-about ends of the pipes */
1130 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1131 close_and_invalidate (&babysitter_pipe[1]);
1133 sitter->socket_to_babysitter = babysitter_pipe[0];
1134 babysitter_pipe[0] = -1;
1136 sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1137 child_err_report_pipe[READ_END] = -1;
1139 sitter->sitter_pid = pid;
1141 if (sitter_p != NULL)
1144 _dbus_babysitter_unref (sitter);
1146 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1153 _DBUS_ASSERT_ERROR_IS_SET (error);
1155 close_and_invalidate (&child_err_report_pipe[READ_END]);
1156 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1157 close_and_invalidate (&babysitter_pipe[0]);
1158 close_and_invalidate (&babysitter_pipe[1]);
1161 _dbus_babysitter_unref (sitter);
1168 #ifdef DBUS_BUILD_TESTS
1171 check_spawn_nonexistent (void *data)
1173 char *argv[4] = { NULL, NULL, NULL, NULL };
1174 DBusBabysitter *sitter;
1179 dbus_error_init (&error);
1181 /*** Test launching nonexistent binary */
1183 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1184 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1188 _dbus_babysitter_block_for_child_exit (sitter);
1189 _dbus_babysitter_set_child_exit_error (sitter, &error);
1193 _dbus_babysitter_unref (sitter);
1195 if (!dbus_error_is_set (&error))
1197 _dbus_warn ("Did not get an error launching nonexistent executable\n");
1201 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1202 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
1204 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1205 error.name, error.message);
1206 dbus_error_free (&error);
1210 dbus_error_free (&error);
1216 check_spawn_segfault (void *data)
1218 char *argv[4] = { NULL, NULL, NULL, NULL };
1219 DBusBabysitter *sitter;
1224 dbus_error_init (&error);
1226 /*** Test launching segfault binary */
1228 argv[0] = TEST_SEGFAULT_BINARY;
1229 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1233 _dbus_babysitter_block_for_child_exit (sitter);
1234 _dbus_babysitter_set_child_exit_error (sitter, &error);
1238 _dbus_babysitter_unref (sitter);
1240 if (!dbus_error_is_set (&error))
1242 _dbus_warn ("Did not get an error launching segfaulting binary\n");
1246 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1247 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1249 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1250 error.name, error.message);
1251 dbus_error_free (&error);
1255 dbus_error_free (&error);
1261 check_spawn_exit (void *data)
1263 char *argv[4] = { NULL, NULL, NULL, NULL };
1264 DBusBabysitter *sitter;
1269 dbus_error_init (&error);
1271 /*** Test launching exit failure binary */
1273 argv[0] = TEST_EXIT_BINARY;
1274 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1278 _dbus_babysitter_block_for_child_exit (sitter);
1279 _dbus_babysitter_set_child_exit_error (sitter, &error);
1283 _dbus_babysitter_unref (sitter);
1285 if (!dbus_error_is_set (&error))
1287 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1291 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1292 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
1294 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1295 error.name, error.message);
1296 dbus_error_free (&error);
1300 dbus_error_free (&error);
1306 check_spawn_and_kill (void *data)
1308 char *argv[4] = { NULL, NULL, NULL, NULL };
1309 DBusBabysitter *sitter;
1314 dbus_error_init (&error);
1316 /*** Test launching sleeping binary then killing it */
1318 argv[0] = TEST_SLEEP_FOREVER_BINARY;
1319 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1323 _dbus_babysitter_kill_child (sitter);
1325 _dbus_babysitter_block_for_child_exit (sitter);
1327 _dbus_babysitter_set_child_exit_error (sitter, &error);
1331 _dbus_babysitter_unref (sitter);
1333 if (!dbus_error_is_set (&error))
1335 _dbus_warn ("Did not get an error after killing spawned binary\n");
1339 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1340 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1342 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1343 error.name, error.message);
1344 dbus_error_free (&error);
1348 dbus_error_free (&error);
1354 _dbus_spawn_test (const char *test_data_dir)
1356 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1357 check_spawn_nonexistent,
1361 if (!_dbus_test_oom_handling ("spawn_segfault",
1362 check_spawn_segfault,
1366 if (!_dbus_test_oom_handling ("spawn_exit",
1371 if (!_dbus_test_oom_handling ("spawn_and_kill",
1372 check_spawn_and_kill,