1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-spawn.c Wrapper around fork/exec
4 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "dbus-spawn.h"
28 #include "dbus-sysdeps-unix.h"
29 #include "dbus-internals.h"
30 #include "dbus-test.h"
31 #include "dbus-protocol.h"
42 extern char **environ;
45 * @addtogroup DBusInternalsUtils
50 * I'm pretty sure this whole spawn file could be made simpler,
51 * if you thought about it a bit.
55 * Enumeration for status of a read()
59 READ_STATUS_OK, /**< Read succeeded */
60 READ_STATUS_ERROR, /**< Some kind of error */
61 READ_STATUS_EOF /**< EOF returned */
74 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
76 retval = READ_STATUS_OK;
83 to_read = sizeof (int) * n_ints_in_buf - bytes;
94 if (chunk < 0 && errno == EINTR)
99 dbus_set_error (error,
100 DBUS_ERROR_SPAWN_FAILED,
101 "Failed to read from child pipe (%s)",
102 _dbus_strerror (errno));
104 retval = READ_STATUS_ERROR;
109 retval = READ_STATUS_EOF;
116 *n_ints_read = (int)(bytes / sizeof(int));
129 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
131 retval = READ_STATUS_OK;
138 to_read = sizeof (pid_t) - bytes;
146 ((char*)buf) + bytes,
148 if (chunk < 0 && errno == EINTR)
153 dbus_set_error (error,
154 DBUS_ERROR_SPAWN_FAILED,
155 "Failed to read from child pipe (%s)",
156 _dbus_strerror (errno));
158 retval = READ_STATUS_ERROR;
163 retval = READ_STATUS_EOF;
173 /* The implementation uses an intermediate child between the main process
174 * and the grandchild. The grandchild is our spawned process. The intermediate
175 * child is a babysitter process; it keeps track of when the grandchild
176 * exits/crashes, and reaps the grandchild.
179 /* Messages from children to parents */
182 CHILD_EXITED, /* This message is followed by the exit status int */
183 CHILD_FORK_FAILED, /* Followed by errno */
184 CHILD_EXEC_FAILED, /* Followed by errno */
185 CHILD_PID /* Followed by pid_t */
189 * Babysitter implementation details
191 struct DBusBabysitter
193 int refcount; /**< Reference count */
195 char *executable; /**< executable name to use in error messages */
197 int socket_to_babysitter; /**< Connection to the babysitter process */
198 int error_pipe_from_child; /**< Connection to the process that does the exec() */
200 pid_t sitter_pid; /**< PID Of the babysitter */
201 pid_t grandchild_pid; /**< PID of the grandchild */
203 DBusWatchList *watches; /**< Watches */
205 DBusWatch *error_watch; /**< Error pipe watch */
206 DBusWatch *sitter_watch; /**< Sitter pipe watch */
208 int errnum; /**< Error number */
209 int status; /**< Exit status code */
210 unsigned int have_child_status : 1; /**< True if child status has been reaped */
211 unsigned int have_fork_errnum : 1; /**< True if we have an error code from fork() */
212 unsigned int have_exec_errnum : 1; /**< True if we have an error code from exec() */
215 static DBusBabysitter*
216 _dbus_babysitter_new (void)
218 DBusBabysitter *sitter;
220 sitter = dbus_new0 (DBusBabysitter, 1);
224 sitter->refcount = 1;
226 sitter->socket_to_babysitter = -1;
227 sitter->error_pipe_from_child = -1;
229 sitter->sitter_pid = -1;
230 sitter->grandchild_pid = -1;
232 sitter->watches = _dbus_watch_list_new ();
233 if (sitter->watches == NULL)
239 _dbus_babysitter_unref (sitter);
244 * Increment the reference count on the babysitter object.
246 * @param sitter the babysitter
247 * @returns the babysitter
250 _dbus_babysitter_ref (DBusBabysitter *sitter)
252 _dbus_assert (sitter != NULL);
253 _dbus_assert (sitter->refcount > 0);
255 sitter->refcount += 1;
261 * Decrement the reference count on the babysitter object.
262 * When the reference count of the babysitter object reaches
263 * zero, the babysitter is killed and the child that was being
264 * babysat gets emancipated.
266 * @param sitter the babysitter
269 _dbus_babysitter_unref (DBusBabysitter *sitter)
271 _dbus_assert (sitter != NULL);
272 _dbus_assert (sitter->refcount > 0);
274 sitter->refcount -= 1;
275 if (sitter->refcount == 0)
277 if (sitter->socket_to_babysitter >= 0)
279 /* If we haven't forked other babysitters
280 * since this babysitter and socket were
281 * created then this close will cause the
282 * babysitter to wake up from poll with
283 * a hangup and then the babysitter will
286 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
287 sitter->socket_to_babysitter = -1;
290 if (sitter->error_pipe_from_child >= 0)
292 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
293 sitter->error_pipe_from_child = -1;
296 if (sitter->sitter_pid > 0)
301 /* It's possible the babysitter died on its own above
302 * from the close, or was killed randomly
303 * by some other process, so first try to reap it
305 ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
307 /* If we couldn't reap the child then kill it, and
311 kill (sitter->sitter_pid, SIGKILL);
315 ret = waitpid (sitter->sitter_pid, &status, 0);
321 else if (errno == ECHILD)
322 _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
324 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
325 errno, _dbus_strerror (errno));
329 _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
330 (long) ret, (long) sitter->sitter_pid);
332 if (WIFEXITED (sitter->status))
333 _dbus_verbose ("Babysitter exited with status %d\n",
334 WEXITSTATUS (sitter->status));
335 else if (WIFSIGNALED (sitter->status))
336 _dbus_verbose ("Babysitter received signal %d\n",
337 WTERMSIG (sitter->status));
339 _dbus_verbose ("Babysitter exited abnormally\n");
342 sitter->sitter_pid = -1;
345 if (sitter->error_watch)
347 _dbus_watch_invalidate (sitter->error_watch);
348 _dbus_watch_unref (sitter->error_watch);
349 sitter->error_watch = NULL;
352 if (sitter->sitter_watch)
354 _dbus_watch_invalidate (sitter->sitter_watch);
355 _dbus_watch_unref (sitter->sitter_watch);
356 sitter->sitter_watch = NULL;
360 _dbus_watch_list_free (sitter->watches);
362 dbus_free (sitter->executable);
369 read_data (DBusBabysitter *sitter,
374 DBusError error = DBUS_ERROR_INIT;
377 r = read_ints (fd, &what, 1, &got, &error);
381 case READ_STATUS_ERROR:
382 _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
383 dbus_error_free (&error);
386 case READ_STATUS_EOF:
398 case CHILD_FORK_FAILED:
399 case CHILD_EXEC_FAILED:
403 r = read_ints (fd, &arg, 1, &got, &error);
407 case READ_STATUS_ERROR:
408 _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
409 dbus_error_free (&error);
411 case READ_STATUS_EOF:
419 if (what == CHILD_EXITED)
421 sitter->have_child_status = TRUE;
422 sitter->status = arg;
424 _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
425 WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
426 WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
428 else if (what == CHILD_FORK_FAILED)
430 sitter->have_fork_errnum = TRUE;
431 sitter->errnum = arg;
432 _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
434 else if (what == CHILD_EXEC_FAILED)
436 sitter->have_exec_errnum = TRUE;
437 sitter->errnum = arg;
438 _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
447 r = read_pid (fd, &pid, &error);
451 case READ_STATUS_ERROR:
452 _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
453 dbus_error_free (&error);
455 case READ_STATUS_EOF:
461 sitter->grandchild_pid = pid;
463 _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
467 _dbus_warn ("Unknown message received from babysitter process\n");
476 close_socket_to_babysitter (DBusBabysitter *sitter)
478 _dbus_verbose ("Closing babysitter\n");
479 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
480 sitter->socket_to_babysitter = -1;
484 close_error_pipe_from_child (DBusBabysitter *sitter)
486 _dbus_verbose ("Closing child error\n");
487 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
488 sitter->error_pipe_from_child = -1;
492 handle_babysitter_socket (DBusBabysitter *sitter,
495 /* Even if we have POLLHUP, we want to keep reading
496 * data until POLLIN goes away; so this function only
497 * looks at HUP/ERR if no IN is set.
499 if (revents & _DBUS_POLLIN)
501 _dbus_verbose ("Reading data from babysitter\n");
502 if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
503 close_socket_to_babysitter (sitter);
505 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
507 close_socket_to_babysitter (sitter);
512 handle_error_pipe (DBusBabysitter *sitter,
515 if (revents & _DBUS_POLLIN)
517 _dbus_verbose ("Reading data from child error\n");
518 if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
519 close_error_pipe_from_child (sitter);
521 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
523 close_error_pipe_from_child (sitter);
527 /* returns whether there were any poll events handled */
529 babysitter_iteration (DBusBabysitter *sitter,
534 dbus_bool_t descriptors_ready;
536 descriptors_ready = FALSE;
540 if (sitter->error_pipe_from_child >= 0)
542 fds[i].fd = sitter->error_pipe_from_child;
543 fds[i].events = _DBUS_POLLIN;
548 if (sitter->socket_to_babysitter >= 0)
550 fds[i].fd = sitter->socket_to_babysitter;
551 fds[i].events = _DBUS_POLLIN;
562 ret = _dbus_poll (fds, i, 0);
564 while (ret < 0 && errno == EINTR);
566 if (ret == 0 && block)
570 ret = _dbus_poll (fds, i, -1);
572 while (ret < 0 && errno == EINTR);
577 descriptors_ready = TRUE;
582 if (fds[i].fd == sitter->error_pipe_from_child)
583 handle_error_pipe (sitter, fds[i].revents);
584 else if (fds[i].fd == sitter->socket_to_babysitter)
585 handle_babysitter_socket (sitter, fds[i].revents);
590 return descriptors_ready;
594 * Macro returns #TRUE if the babysitter still has live sockets open to the
595 * babysitter child or the grandchild.
597 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
600 * Blocks until the babysitter process gives us the PID of the spawned grandchild,
601 * then kills the spawned grandchild.
603 * @param sitter the babysitter object
606 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
608 /* be sure we have the PID of the child */
609 while (LIVE_CHILDREN (sitter) &&
610 sitter->grandchild_pid == -1)
611 babysitter_iteration (sitter, TRUE);
613 _dbus_verbose ("Got child PID %ld for killing\n",
614 (long) sitter->grandchild_pid);
616 if (sitter->grandchild_pid == -1)
617 return; /* child is already dead, or we're so hosed we'll never recover */
619 kill (sitter->grandchild_pid, SIGKILL);
623 * Checks whether the child has exited, without blocking.
625 * @param sitter the babysitter
628 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
631 /* Be sure we're up-to-date */
632 while (LIVE_CHILDREN (sitter) &&
633 babysitter_iteration (sitter, FALSE))
636 /* We will have exited the babysitter when the child has exited */
637 return sitter->socket_to_babysitter < 0;
641 * Gets the exit status of the child. We do this so implementation specific
642 * detail is not cluttering up dbus, for example the system launcher code.
643 * This can only be called if the child has exited, i.e. call
644 * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
645 * did not return a status code, e.g. because the child was signaled
646 * or we failed to ever launch the child in the first place.
648 * @param sitter the babysitter
649 * @param status the returned status code
650 * @returns #FALSE on failure
653 _dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
656 if (!_dbus_babysitter_get_child_exited (sitter))
657 _dbus_assert_not_reached ("Child has not exited");
659 if (!sitter->have_child_status ||
660 !(WIFEXITED (sitter->status)))
663 *status = WEXITSTATUS (sitter->status);
668 * Sets the #DBusError with an explanation of why the spawned
669 * child process exited (on a signal, or whatever). If
670 * the child process has not exited, does nothing (error
671 * will remain unset).
673 * @param sitter the babysitter
674 * @param error an error to fill in
677 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
680 if (!_dbus_babysitter_get_child_exited (sitter))
683 /* Note that if exec fails, we will also get a child status
684 * from the babysitter saying the child exited,
685 * so we need to give priority to the exec error
687 if (sitter->have_exec_errnum)
689 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
690 "Failed to execute program %s: %s",
691 sitter->executable, _dbus_strerror (sitter->errnum));
693 else if (sitter->have_fork_errnum)
695 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
696 "Failed to fork a new process %s: %s",
697 sitter->executable, _dbus_strerror (sitter->errnum));
699 else if (sitter->have_child_status)
701 if (WIFEXITED (sitter->status))
702 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
703 "Process %s exited with status %d",
704 sitter->executable, WEXITSTATUS (sitter->status));
705 else if (WIFSIGNALED (sitter->status))
706 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
707 "Process %s received signal %d",
708 sitter->executable, WTERMSIG (sitter->status));
710 dbus_set_error (error, DBUS_ERROR_FAILED,
711 "Process %s exited abnormally",
716 dbus_set_error (error, DBUS_ERROR_FAILED,
717 "Process %s exited, reason unknown",
723 * Sets watch functions to notify us when the
724 * babysitter object needs to read/write file descriptors.
726 * @param sitter the babysitter
727 * @param add_function function to begin monitoring a new descriptor.
728 * @param remove_function function to stop monitoring a descriptor.
729 * @param toggled_function function to notify when the watch is enabled/disabled
730 * @param data data to pass to add_function and remove_function.
731 * @param free_data_function function to be called to free the data.
732 * @returns #FALSE on failure (no memory)
735 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
736 DBusAddWatchFunction add_function,
737 DBusRemoveWatchFunction remove_function,
738 DBusWatchToggledFunction toggled_function,
740 DBusFreeFunction free_data_function)
742 return _dbus_watch_list_set_functions (sitter->watches,
751 handle_watch (DBusWatch *watch,
752 unsigned int condition,
755 DBusBabysitter *sitter = data;
760 if (condition & DBUS_WATCH_READABLE)
761 revents |= _DBUS_POLLIN;
762 if (condition & DBUS_WATCH_ERROR)
763 revents |= _DBUS_POLLERR;
764 if (condition & DBUS_WATCH_HANGUP)
765 revents |= _DBUS_POLLHUP;
767 fd = dbus_watch_get_socket (watch);
769 if (fd == sitter->error_pipe_from_child)
770 handle_error_pipe (sitter, revents);
771 else if (fd == sitter->socket_to_babysitter)
772 handle_babysitter_socket (sitter, revents);
774 while (LIVE_CHILDREN (sitter) &&
775 babysitter_iteration (sitter, FALSE))
781 /** Helps remember which end of the pipe is which */
783 /** Helps remember which end of the pipe is which */
787 /* Avoids a danger in threaded situations (calling close()
788 * on a file descriptor twice, and another thread has
789 * re-opened it since the first close)
792 close_and_invalidate (int *fd)
800 ret = _dbus_close_socket (*fd, NULL);
814 dbus_bool_t cloexec_done;
816 retval = pipe2 (p, O_CLOEXEC);
817 cloexec_done = retval >= 0;
819 /* Check if kernel seems to be too old to know pipe2(). We assume
820 that if pipe2 is available, O_CLOEXEC is too. */
821 if (retval < 0 && errno == ENOSYS)
827 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
831 dbus_set_error (error,
832 DBUS_ERROR_SPAWN_FAILED,
833 "Failed to create pipe for communicating with child process (%s)",
834 _dbus_strerror (errno));
842 _dbus_fd_set_close_on_exec (p[0]);
843 _dbus_fd_set_close_on_exec (p[1]);
850 do_write (int fd, const void *buf, size_t count)
852 size_t bytes_written;
859 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
867 _dbus_warn ("Failed to write data to pipe!\n");
868 exit (1); /* give up, we suck */
872 bytes_written += ret;
874 if (bytes_written < count)
879 write_err_and_exit (int fd, int msg)
883 do_write (fd, &msg, sizeof (msg));
884 do_write (fd, &en, sizeof (en));
890 write_pid (int fd, pid_t pid)
894 do_write (fd, &msg, sizeof (msg));
895 do_write (fd, &pid, sizeof (pid));
899 write_status_and_exit (int fd, int status)
901 int msg = CHILD_EXITED;
903 do_write (fd, &msg, sizeof (msg));
904 do_write (fd, &status, sizeof (status));
910 do_exec (int child_err_report_fd,
913 DBusSpawnChildSetupFunc child_setup,
916 #ifdef DBUS_BUILD_TESTS
920 _dbus_verbose_reset ();
921 _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
925 (* child_setup) (user_data);
927 #ifdef DBUS_BUILD_TESTS
928 max_open = sysconf (_SC_OPEN_MAX);
930 for (i = 3; i < max_open; i++)
934 if (i == child_err_report_fd)
937 retval = fcntl (i, F_GETFD);
939 if (retval != -1 && !(retval & FD_CLOEXEC))
940 _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
946 _dbus_assert (environ != NULL);
951 execve (argv[0], argv, envp);
954 write_err_and_exit (child_err_report_fd,
959 check_babysit_events (pid_t grandchild_pid,
968 ret = waitpid (grandchild_pid, &status, WNOHANG);
969 /* The man page says EINTR can't happen with WNOHANG,
970 * but there are reports of it (maybe only with valgrind?)
973 while (ret < 0 && errno == EINTR);
977 _dbus_verbose ("no child exited\n");
979 ; /* no child exited */
983 /* This isn't supposed to happen. */
984 _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
985 _dbus_strerror (errno));
988 else if (ret == grandchild_pid)
991 _dbus_verbose ("reaped child pid %ld\n", (long) ret);
993 write_status_and_exit (parent_pipe, status);
997 _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
1002 if (revents & _DBUS_POLLIN)
1004 _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
1007 if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
1009 /* Parent is gone, so we just exit */
1010 _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
1015 static int babysit_sigchld_pipe = -1;
1018 babysit_signal_handler (int signo)
1022 if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1028 babysit (pid_t grandchild_pid,
1031 int sigchld_pipe[2];
1033 /* We don't exec, so we keep parent state, such as the pid that
1034 * _dbus_verbose() uses. Reset the pid here.
1036 _dbus_verbose_reset ();
1038 /* I thought SIGCHLD would just wake up the poll, but
1039 * that didn't seem to work, so added this pipe.
1040 * Probably the pipe is more likely to work on busted
1041 * operating systems anyhow.
1043 if (pipe (sigchld_pipe) < 0)
1045 _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
1049 babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1051 _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1053 write_pid (parent_pipe, grandchild_pid);
1055 check_babysit_events (grandchild_pid, parent_pipe, 0);
1061 pfds[0].fd = parent_pipe;
1062 pfds[0].events = _DBUS_POLLIN;
1063 pfds[0].revents = 0;
1065 pfds[1].fd = sigchld_pipe[READ_END];
1066 pfds[1].events = _DBUS_POLLIN;
1067 pfds[1].revents = 0;
1069 if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1071 _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
1075 if (pfds[0].revents != 0)
1077 check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1079 else if (pfds[1].revents & _DBUS_POLLIN)
1082 read (sigchld_pipe[READ_END], &b, 1);
1083 /* do waitpid check */
1084 check_babysit_events (grandchild_pid, parent_pipe, 0);
1092 * Spawns a new process. The executable name and argv[0]
1093 * are the same, both are provided in argv[0]. The child_setup
1094 * function is passed the given user_data and is run in the child
1095 * just before calling exec().
1097 * Also creates a "babysitter" which tracks the status of the
1098 * child process, advising the parent if the child exits.
1099 * If the spawn fails, no babysitter is created.
1100 * If sitter_p is #NULL, no babysitter is kept.
1102 * @param sitter_p return location for babysitter or #NULL
1103 * @param argv the executable and arguments
1104 * @param env the environment (not used on unix yet)
1105 * @param child_setup function to call in child pre-exec()
1106 * @param user_data user data for setup function
1107 * @param error error object to be filled in if function fails
1108 * @returns #TRUE on success, #FALSE if error is filled in
1111 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
1114 DBusSpawnChildSetupFunc child_setup,
1118 DBusBabysitter *sitter;
1119 int child_err_report_pipe[2] = { -1, -1 };
1120 int babysitter_pipe[2] = { -1, -1 };
1123 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1125 if (sitter_p != NULL)
1130 sitter = _dbus_babysitter_new ();
1133 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1137 sitter->executable = _dbus_strdup (argv[0]);
1138 if (sitter->executable == NULL)
1140 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1141 goto cleanup_and_fail;
1144 if (!make_pipe (child_err_report_pipe, error))
1145 goto cleanup_and_fail;
1147 if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1148 goto cleanup_and_fail;
1150 /* Setting up the babysitter is only useful in the parent,
1151 * but we don't want to run out of memory and fail
1152 * after we've already forked, since then we'd leak
1153 * child processes everywhere.
1155 sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1156 DBUS_WATCH_READABLE,
1157 TRUE, handle_watch, sitter, NULL);
1158 if (sitter->error_watch == NULL)
1160 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1161 goto cleanup_and_fail;
1164 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1166 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1167 goto cleanup_and_fail;
1170 sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1171 DBUS_WATCH_READABLE,
1172 TRUE, handle_watch, sitter, NULL);
1173 if (sitter->sitter_watch == NULL)
1175 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1176 goto cleanup_and_fail;
1179 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1182 goto cleanup_and_fail;
1185 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1191 dbus_set_error (error,
1192 DBUS_ERROR_SPAWN_FORK_FAILED,
1193 "Failed to fork (%s)",
1194 _dbus_strerror (errno));
1195 goto cleanup_and_fail;
1199 /* Immediate child, this is the babysitter process. */
1202 /* Be sure we crash if the parent exits
1203 * and we write to the err_report_pipe
1205 signal (SIGPIPE, SIG_DFL);
1207 /* Close the parent's end of the pipes. */
1208 close_and_invalidate (&child_err_report_pipe[READ_END]);
1209 close_and_invalidate (&babysitter_pipe[0]);
1211 /* Create the child that will exec () */
1212 grandchild_pid = fork ();
1214 if (grandchild_pid < 0)
1216 write_err_and_exit (babysitter_pipe[1],
1218 _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1220 else if (grandchild_pid == 0)
1222 do_exec (child_err_report_pipe[WRITE_END],
1225 child_setup, user_data);
1226 _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1230 babysit (grandchild_pid, babysitter_pipe[1]);
1231 _dbus_assert_not_reached ("Got to code after babysit()");
1236 /* Close the uncared-about ends of the pipes */
1237 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1238 close_and_invalidate (&babysitter_pipe[1]);
1240 sitter->socket_to_babysitter = babysitter_pipe[0];
1241 babysitter_pipe[0] = -1;
1243 sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1244 child_err_report_pipe[READ_END] = -1;
1246 sitter->sitter_pid = pid;
1248 if (sitter_p != NULL)
1251 _dbus_babysitter_unref (sitter);
1253 dbus_free_string_array (env);
1255 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1262 _DBUS_ASSERT_ERROR_IS_SET (error);
1264 close_and_invalidate (&child_err_report_pipe[READ_END]);
1265 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1266 close_and_invalidate (&babysitter_pipe[0]);
1267 close_and_invalidate (&babysitter_pipe[1]);
1270 _dbus_babysitter_unref (sitter);
1277 #ifdef DBUS_BUILD_TESTS
1280 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1282 while (LIVE_CHILDREN (sitter))
1283 babysitter_iteration (sitter, TRUE);
1287 check_spawn_nonexistent (void *data)
1289 char *argv[4] = { NULL, NULL, NULL, NULL };
1290 DBusBabysitter *sitter = NULL;
1291 DBusError error = DBUS_ERROR_INIT;
1293 /*** Test launching nonexistent binary */
1295 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1296 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1300 _dbus_babysitter_block_for_child_exit (sitter);
1301 _dbus_babysitter_set_child_exit_error (sitter, &error);
1305 _dbus_babysitter_unref (sitter);
1307 if (!dbus_error_is_set (&error))
1309 _dbus_warn ("Did not get an error launching nonexistent executable\n");
1313 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1314 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
1316 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1317 error.name, error.message);
1318 dbus_error_free (&error);
1322 dbus_error_free (&error);
1328 check_spawn_segfault (void *data)
1330 char *argv[4] = { NULL, NULL, NULL, NULL };
1331 DBusBabysitter *sitter = NULL;
1332 DBusError error = DBUS_ERROR_INIT;
1334 /*** Test launching segfault binary */
1336 argv[0] = TEST_SEGFAULT_BINARY;
1337 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1341 _dbus_babysitter_block_for_child_exit (sitter);
1342 _dbus_babysitter_set_child_exit_error (sitter, &error);
1346 _dbus_babysitter_unref (sitter);
1348 if (!dbus_error_is_set (&error))
1350 _dbus_warn ("Did not get an error launching segfaulting binary\n");
1354 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1355 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1357 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1358 error.name, error.message);
1359 dbus_error_free (&error);
1363 dbus_error_free (&error);
1369 check_spawn_exit (void *data)
1371 char *argv[4] = { NULL, NULL, NULL, NULL };
1372 DBusBabysitter *sitter = NULL;
1373 DBusError error = DBUS_ERROR_INIT;
1375 /*** Test launching exit failure binary */
1377 argv[0] = TEST_EXIT_BINARY;
1378 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1382 _dbus_babysitter_block_for_child_exit (sitter);
1383 _dbus_babysitter_set_child_exit_error (sitter, &error);
1387 _dbus_babysitter_unref (sitter);
1389 if (!dbus_error_is_set (&error))
1391 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1395 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1396 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
1398 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1399 error.name, error.message);
1400 dbus_error_free (&error);
1404 dbus_error_free (&error);
1410 check_spawn_and_kill (void *data)
1412 char *argv[4] = { NULL, NULL, NULL, NULL };
1413 DBusBabysitter *sitter = NULL;
1414 DBusError error = DBUS_ERROR_INIT;
1416 /*** Test launching sleeping binary then killing it */
1418 argv[0] = TEST_SLEEP_FOREVER_BINARY;
1419 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1423 _dbus_babysitter_kill_child (sitter);
1425 _dbus_babysitter_block_for_child_exit (sitter);
1427 _dbus_babysitter_set_child_exit_error (sitter, &error);
1431 _dbus_babysitter_unref (sitter);
1433 if (!dbus_error_is_set (&error))
1435 _dbus_warn ("Did not get an error after killing spawned binary\n");
1439 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1440 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1442 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1443 error.name, error.message);
1444 dbus_error_free (&error);
1448 dbus_error_free (&error);
1454 _dbus_spawn_test (const char *test_data_dir)
1456 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1457 check_spawn_nonexistent,
1461 if (!_dbus_test_oom_handling ("spawn_segfault",
1462 check_spawn_segfault,
1466 if (!_dbus_test_oom_handling ("spawn_exit",
1471 if (!_dbus_test_oom_handling ("spawn_and_kill",
1472 check_spawn_and_kill,