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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-spawn.h"
25 #include "dbus-sysdeps-unix.h"
26 #include "dbus-internals.h"
27 #include "dbus-test.h"
28 #include "dbus-protocol.h"
40 * @addtogroup DBusInternalsUtils
45 * I'm pretty sure this whole spawn file could be made simpler,
46 * if you thought about it a bit.
50 * Enumeration for status of a read()
54 READ_STATUS_OK, /**< Read succeeded */
55 READ_STATUS_ERROR, /**< Some kind of error */
56 READ_STATUS_EOF /**< EOF returned */
69 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
71 retval = READ_STATUS_OK;
78 to_read = sizeof (int) * n_ints_in_buf - bytes;
89 if (chunk < 0 && errno == EINTR)
94 dbus_set_error (error,
95 DBUS_ERROR_SPAWN_FAILED,
96 "Failed to read from child pipe (%s)",
97 _dbus_strerror (errno));
99 retval = READ_STATUS_ERROR;
104 retval = READ_STATUS_EOF;
111 *n_ints_read = (int)(bytes / sizeof(int));
124 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
126 retval = READ_STATUS_OK;
133 to_read = sizeof (pid_t) - bytes;
141 ((char*)buf) + bytes,
143 if (chunk < 0 && errno == EINTR)
148 dbus_set_error (error,
149 DBUS_ERROR_SPAWN_FAILED,
150 "Failed to read from child pipe (%s)",
151 _dbus_strerror (errno));
153 retval = READ_STATUS_ERROR;
158 retval = READ_STATUS_EOF;
168 /* The implementation uses an intermediate child between the main process
169 * and the grandchild. The grandchild is our spawned process. The intermediate
170 * child is a babysitter process; it keeps track of when the grandchild
171 * exits/crashes, and reaps the grandchild.
174 /* Messages from children to parents */
177 CHILD_EXITED, /* This message is followed by the exit status int */
178 CHILD_FORK_FAILED, /* Followed by errno */
179 CHILD_EXEC_FAILED, /* Followed by errno */
180 CHILD_PID /* Followed by pid_t */
184 * Babysitter implementation details
186 struct DBusBabysitter
188 int refcount; /**< Reference count */
190 char *executable; /**< executable name to use in error messages */
192 int socket_to_babysitter; /**< Connection to the babysitter process */
193 int error_pipe_from_child; /**< Connection to the process that does the exec() */
195 pid_t sitter_pid; /**< PID Of the babysitter */
196 pid_t grandchild_pid; /**< PID of the grandchild */
198 DBusWatchList *watches; /**< Watches */
200 DBusWatch *error_watch; /**< Error pipe watch */
201 DBusWatch *sitter_watch; /**< Sitter pipe watch */
203 int errnum; /**< Error number */
204 int status; /**< Exit status code */
205 unsigned int have_child_status : 1; /**< True if child status has been reaped */
206 unsigned int have_fork_errnum : 1; /**< True if we have an error code from fork() */
207 unsigned int have_exec_errnum : 1; /**< True if we have an error code from exec() */
210 static DBusBabysitter*
211 _dbus_babysitter_new (void)
213 DBusBabysitter *sitter;
215 sitter = dbus_new0 (DBusBabysitter, 1);
219 sitter->refcount = 1;
221 sitter->socket_to_babysitter = -1;
222 sitter->error_pipe_from_child = -1;
224 sitter->sitter_pid = -1;
225 sitter->grandchild_pid = -1;
227 sitter->watches = _dbus_watch_list_new ();
228 if (sitter->watches == NULL)
234 _dbus_babysitter_unref (sitter);
239 * Increment the reference count on the babysitter object.
241 * @param sitter the babysitter
242 * @returns the babysitter
245 _dbus_babysitter_ref (DBusBabysitter *sitter)
247 _dbus_assert (sitter != NULL);
248 _dbus_assert (sitter->refcount > 0);
250 sitter->refcount += 1;
256 * Decrement the reference count on the babysitter object.
257 * When the reference count of the babysitter object reaches
258 * zero, the babysitter is killed and the child that was being
259 * babysat gets emancipated.
261 * @param sitter the babysitter
264 _dbus_babysitter_unref (DBusBabysitter *sitter)
266 _dbus_assert (sitter != NULL);
267 _dbus_assert (sitter->refcount > 0);
269 sitter->refcount -= 1;
270 if (sitter->refcount == 0)
272 if (sitter->socket_to_babysitter >= 0)
274 /* If we haven't forked other babysitters
275 * since this babysitter and socket were
276 * created then this close will cause the
277 * babysitter to wake up from poll with
278 * a hangup and then the babysitter will
281 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
282 sitter->socket_to_babysitter = -1;
285 if (sitter->error_pipe_from_child >= 0)
287 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
288 sitter->error_pipe_from_child = -1;
291 if (sitter->sitter_pid > 0)
296 /* It's possible the babysitter died on its own above
297 * from the close, or was killed randomly
298 * by some other process, so first try to reap it
300 ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
302 /* If we couldn't reap the child then kill it, and
306 kill (sitter->sitter_pid, SIGKILL);
310 ret = waitpid (sitter->sitter_pid, &status, 0);
316 else if (errno == ECHILD)
317 _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
319 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
320 errno, _dbus_strerror (errno));
324 _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
325 (long) ret, (long) sitter->sitter_pid);
327 if (WIFEXITED (sitter->status))
328 _dbus_verbose ("Babysitter exited with status %d\n",
329 WEXITSTATUS (sitter->status));
330 else if (WIFSIGNALED (sitter->status))
331 _dbus_verbose ("Babysitter received signal %d\n",
332 WTERMSIG (sitter->status));
334 _dbus_verbose ("Babysitter exited abnormally\n");
337 sitter->sitter_pid = -1;
340 if (sitter->error_watch)
342 _dbus_watch_invalidate (sitter->error_watch);
343 _dbus_watch_unref (sitter->error_watch);
344 sitter->error_watch = NULL;
347 if (sitter->sitter_watch)
349 _dbus_watch_invalidate (sitter->sitter_watch);
350 _dbus_watch_unref (sitter->sitter_watch);
351 sitter->sitter_watch = NULL;
355 _dbus_watch_list_free (sitter->watches);
357 dbus_free (sitter->executable);
364 read_data (DBusBabysitter *sitter,
369 DBusError error = DBUS_ERROR_INIT;
372 r = read_ints (fd, &what, 1, &got, &error);
376 case READ_STATUS_ERROR:
377 _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
378 dbus_error_free (&error);
381 case READ_STATUS_EOF:
393 case CHILD_FORK_FAILED:
394 case CHILD_EXEC_FAILED:
398 r = read_ints (fd, &arg, 1, &got, &error);
402 case READ_STATUS_ERROR:
403 _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
404 dbus_error_free (&error);
406 case READ_STATUS_EOF:
414 if (what == CHILD_EXITED)
416 sitter->have_child_status = TRUE;
417 sitter->status = arg;
419 _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
420 WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
421 WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
423 else if (what == CHILD_FORK_FAILED)
425 sitter->have_fork_errnum = TRUE;
426 sitter->errnum = arg;
427 _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
429 else if (what == CHILD_EXEC_FAILED)
431 sitter->have_exec_errnum = TRUE;
432 sitter->errnum = arg;
433 _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
442 r = read_pid (fd, &pid, &error);
446 case READ_STATUS_ERROR:
447 _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
448 dbus_error_free (&error);
450 case READ_STATUS_EOF:
456 sitter->grandchild_pid = pid;
458 _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
462 _dbus_warn ("Unknown message received from babysitter process\n");
471 close_socket_to_babysitter (DBusBabysitter *sitter)
473 _dbus_verbose ("Closing babysitter\n");
474 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
475 sitter->socket_to_babysitter = -1;
479 close_error_pipe_from_child (DBusBabysitter *sitter)
481 _dbus_verbose ("Closing child error\n");
482 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
483 sitter->error_pipe_from_child = -1;
487 handle_babysitter_socket (DBusBabysitter *sitter,
490 /* Even if we have POLLHUP, we want to keep reading
491 * data until POLLIN goes away; so this function only
492 * looks at HUP/ERR if no IN is set.
494 if (revents & _DBUS_POLLIN)
496 _dbus_verbose ("Reading data from babysitter\n");
497 if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
498 close_socket_to_babysitter (sitter);
500 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
502 close_socket_to_babysitter (sitter);
507 handle_error_pipe (DBusBabysitter *sitter,
510 if (revents & _DBUS_POLLIN)
512 _dbus_verbose ("Reading data from child error\n");
513 if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
514 close_error_pipe_from_child (sitter);
516 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
518 close_error_pipe_from_child (sitter);
522 /* returns whether there were any poll events handled */
524 babysitter_iteration (DBusBabysitter *sitter,
529 dbus_bool_t descriptors_ready;
531 descriptors_ready = FALSE;
535 if (sitter->error_pipe_from_child >= 0)
537 fds[i].fd = sitter->error_pipe_from_child;
538 fds[i].events = _DBUS_POLLIN;
543 if (sitter->socket_to_babysitter >= 0)
545 fds[i].fd = sitter->socket_to_babysitter;
546 fds[i].events = _DBUS_POLLIN;
557 ret = _dbus_poll (fds, i, 0);
559 while (ret < 0 && errno == EINTR);
561 if (ret == 0 && block)
565 ret = _dbus_poll (fds, i, -1);
567 while (ret < 0 && errno == EINTR);
572 descriptors_ready = TRUE;
577 if (fds[i].fd == sitter->error_pipe_from_child)
578 handle_error_pipe (sitter, fds[i].revents);
579 else if (fds[i].fd == sitter->socket_to_babysitter)
580 handle_babysitter_socket (sitter, fds[i].revents);
585 return descriptors_ready;
589 * Macro returns #TRUE if the babysitter still has live sockets open to the
590 * babysitter child or the grandchild.
592 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
595 * Blocks until the babysitter process gives us the PID of the spawned grandchild,
596 * then kills the spawned grandchild.
598 * @param sitter the babysitter object
601 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
603 /* be sure we have the PID of the child */
604 while (LIVE_CHILDREN (sitter) &&
605 sitter->grandchild_pid == -1)
606 babysitter_iteration (sitter, TRUE);
608 _dbus_verbose ("Got child PID %ld for killing\n",
609 (long) sitter->grandchild_pid);
611 if (sitter->grandchild_pid == -1)
612 return; /* child is already dead, or we're so hosed we'll never recover */
614 kill (sitter->grandchild_pid, SIGKILL);
618 * Checks whether the child has exited, without blocking.
620 * @param sitter the babysitter
623 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
626 /* Be sure we're up-to-date */
627 while (LIVE_CHILDREN (sitter) &&
628 babysitter_iteration (sitter, FALSE))
631 /* We will have exited the babysitter when the child has exited */
632 return sitter->socket_to_babysitter < 0;
636 * Gets the exit status of the child. We do this so implementation specific
637 * detail is not cluttering up dbus, for example the system launcher code.
638 * This can only be called if the child has exited, i.e. call
639 * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
640 * did not return a status code, e.g. because the child was signaled
641 * or we failed to ever launch the child in the first place.
643 * @param sitter the babysitter
644 * @param status the returned status code
645 * @returns #FALSE on failure
648 _dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
651 if (!_dbus_babysitter_get_child_exited (sitter))
652 _dbus_assert_not_reached ("Child has not exited");
654 if (!sitter->have_child_status ||
655 !(WIFEXITED (sitter->status)))
658 *status = WEXITSTATUS (sitter->status);
663 * Sets the #DBusError with an explanation of why the spawned
664 * child process exited (on a signal, or whatever). If
665 * the child process has not exited, does nothing (error
666 * will remain unset).
668 * @param sitter the babysitter
669 * @param error an error to fill in
672 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
675 if (!_dbus_babysitter_get_child_exited (sitter))
678 /* Note that if exec fails, we will also get a child status
679 * from the babysitter saying the child exited,
680 * so we need to give priority to the exec error
682 if (sitter->have_exec_errnum)
684 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
685 "Failed to execute program %s: %s",
686 sitter->executable, _dbus_strerror (sitter->errnum));
688 else if (sitter->have_fork_errnum)
690 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
691 "Failed to fork a new process %s: %s",
692 sitter->executable, _dbus_strerror (sitter->errnum));
694 else if (sitter->have_child_status)
696 if (WIFEXITED (sitter->status))
697 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
698 "Process %s exited with status %d",
699 sitter->executable, WEXITSTATUS (sitter->status));
700 else if (WIFSIGNALED (sitter->status))
701 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
702 "Process %s received signal %d",
703 sitter->executable, WTERMSIG (sitter->status));
705 dbus_set_error (error, DBUS_ERROR_FAILED,
706 "Process %s exited abnormally",
711 dbus_set_error (error, DBUS_ERROR_FAILED,
712 "Process %s exited, reason unknown",
718 * Sets watch functions to notify us when the
719 * babysitter object needs to read/write file descriptors.
721 * @param sitter the babysitter
722 * @param add_function function to begin monitoring a new descriptor.
723 * @param remove_function function to stop monitoring a descriptor.
724 * @param toggled_function function to notify when the watch is enabled/disabled
725 * @param data data to pass to add_function and remove_function.
726 * @param free_data_function function to be called to free the data.
727 * @returns #FALSE on failure (no memory)
730 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
731 DBusAddWatchFunction add_function,
732 DBusRemoveWatchFunction remove_function,
733 DBusWatchToggledFunction toggled_function,
735 DBusFreeFunction free_data_function)
737 return _dbus_watch_list_set_functions (sitter->watches,
746 handle_watch (DBusWatch *watch,
747 unsigned int condition,
750 DBusBabysitter *sitter = data;
755 if (condition & DBUS_WATCH_READABLE)
756 revents |= _DBUS_POLLIN;
757 if (condition & DBUS_WATCH_ERROR)
758 revents |= _DBUS_POLLERR;
759 if (condition & DBUS_WATCH_HANGUP)
760 revents |= _DBUS_POLLHUP;
762 fd = dbus_watch_get_socket (watch);
764 if (fd == sitter->error_pipe_from_child)
765 handle_error_pipe (sitter, revents);
766 else if (fd == sitter->socket_to_babysitter)
767 handle_babysitter_socket (sitter, revents);
769 while (LIVE_CHILDREN (sitter) &&
770 babysitter_iteration (sitter, FALSE))
776 /** Helps remember which end of the pipe is which */
778 /** Helps remember which end of the pipe is which */
782 /* Avoids a danger in threaded situations (calling close()
783 * on a file descriptor twice, and another thread has
784 * re-opened it since the first close)
787 close_and_invalidate (int *fd)
795 ret = _dbus_close_socket (*fd, NULL);
806 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
810 dbus_set_error (error,
811 DBUS_ERROR_SPAWN_FAILED,
812 "Failed to create pipe for communicating with child process (%s)",
813 _dbus_strerror (errno));
821 do_write (int fd, const void *buf, size_t count)
823 size_t bytes_written;
830 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
838 _dbus_warn ("Failed to write data to pipe!\n");
839 exit (1); /* give up, we suck */
843 bytes_written += ret;
845 if (bytes_written < count)
850 write_err_and_exit (int fd, int msg)
854 do_write (fd, &msg, sizeof (msg));
855 do_write (fd, &en, sizeof (en));
861 write_pid (int fd, pid_t pid)
865 do_write (fd, &msg, sizeof (msg));
866 do_write (fd, &pid, sizeof (pid));
870 write_status_and_exit (int fd, int status)
872 int msg = CHILD_EXITED;
874 do_write (fd, &msg, sizeof (msg));
875 do_write (fd, &status, sizeof (status));
881 do_exec (int child_err_report_fd,
883 DBusSpawnChildSetupFunc child_setup,
886 #ifdef DBUS_BUILD_TESTS
890 _dbus_verbose_reset ();
891 _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
895 (* child_setup) (user_data);
897 #ifdef DBUS_BUILD_TESTS
898 max_open = sysconf (_SC_OPEN_MAX);
900 for (i = 3; i < max_open; i++)
904 if (i == child_err_report_fd)
907 retval = fcntl (i, F_GETFD);
909 if (retval != -1 && !(retval & FD_CLOEXEC))
910 _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
914 execv (argv[0], argv);
917 write_err_and_exit (child_err_report_fd,
922 check_babysit_events (pid_t grandchild_pid,
931 ret = waitpid (grandchild_pid, &status, WNOHANG);
932 /* The man page says EINTR can't happen with WNOHANG,
933 * but there are reports of it (maybe only with valgrind?)
936 while (ret < 0 && errno == EINTR);
940 _dbus_verbose ("no child exited\n");
942 ; /* no child exited */
946 /* This isn't supposed to happen. */
947 _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
948 _dbus_strerror (errno));
951 else if (ret == grandchild_pid)
954 _dbus_verbose ("reaped child pid %ld\n", (long) ret);
956 write_status_and_exit (parent_pipe, status);
960 _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
965 if (revents & _DBUS_POLLIN)
967 _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
970 if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
972 /* Parent is gone, so we just exit */
973 _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
978 static int babysit_sigchld_pipe = -1;
981 babysit_signal_handler (int signo)
985 if (write (babysit_sigchld_pipe, &b, 1) <= 0)
991 babysit (pid_t grandchild_pid,
996 /* We don't exec, so we keep parent state, such as the pid that
997 * _dbus_verbose() uses. Reset the pid here.
999 _dbus_verbose_reset ();
1001 /* I thought SIGCHLD would just wake up the poll, but
1002 * that didn't seem to work, so added this pipe.
1003 * Probably the pipe is more likely to work on busted
1004 * operating systems anyhow.
1006 if (pipe (sigchld_pipe) < 0)
1008 _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
1012 babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1014 _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1016 write_pid (parent_pipe, grandchild_pid);
1018 check_babysit_events (grandchild_pid, parent_pipe, 0);
1024 pfds[0].fd = parent_pipe;
1025 pfds[0].events = _DBUS_POLLIN;
1026 pfds[0].revents = 0;
1028 pfds[1].fd = sigchld_pipe[READ_END];
1029 pfds[1].events = _DBUS_POLLIN;
1030 pfds[1].revents = 0;
1032 if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1034 _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
1038 if (pfds[0].revents != 0)
1040 check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1042 else if (pfds[1].revents & _DBUS_POLLIN)
1045 read (sigchld_pipe[READ_END], &b, 1);
1046 /* do waitpid check */
1047 check_babysit_events (grandchild_pid, parent_pipe, 0);
1055 * Spawns a new process. The executable name and argv[0]
1056 * are the same, both are provided in argv[0]. The child_setup
1057 * function is passed the given user_data and is run in the child
1058 * just before calling exec().
1060 * Also creates a "babysitter" which tracks the status of the
1061 * child process, advising the parent if the child exits.
1062 * If the spawn fails, no babysitter is created.
1063 * If sitter_p is #NULL, no babysitter is kept.
1065 * @param sitter_p return location for babysitter or #NULL
1066 * @param argv the executable and arguments
1067 * @param env the environment (not used on unix yet)
1068 * @param child_setup function to call in child pre-exec()
1069 * @param user_data user data for setup function
1070 * @param error error object to be filled in if function fails
1071 * @returns #TRUE on success, #FALSE if error is filled in
1074 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
1077 DBusSpawnChildSetupFunc child_setup,
1081 DBusBabysitter *sitter;
1082 int child_err_report_pipe[2] = { -1, -1 };
1083 int babysitter_pipe[2] = { -1, -1 };
1086 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1088 if (sitter_p != NULL)
1093 sitter = _dbus_babysitter_new ();
1096 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1100 sitter->executable = _dbus_strdup (argv[0]);
1101 if (sitter->executable == NULL)
1103 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1104 goto cleanup_and_fail;
1107 if (!make_pipe (child_err_report_pipe, error))
1108 goto cleanup_and_fail;
1110 _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
1111 _dbus_fd_set_close_on_exec (child_err_report_pipe[WRITE_END]);
1113 if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1114 goto cleanup_and_fail;
1116 _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
1117 _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
1119 /* Setting up the babysitter is only useful in the parent,
1120 * but we don't want to run out of memory and fail
1121 * after we've already forked, since then we'd leak
1122 * child processes everywhere.
1124 sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1125 DBUS_WATCH_READABLE,
1126 TRUE, handle_watch, sitter, NULL);
1127 if (sitter->error_watch == NULL)
1129 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1130 goto cleanup_and_fail;
1133 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1135 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1136 goto cleanup_and_fail;
1139 sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1140 DBUS_WATCH_READABLE,
1141 TRUE, handle_watch, sitter, NULL);
1142 if (sitter->sitter_watch == NULL)
1144 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1145 goto cleanup_and_fail;
1148 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1150 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1151 goto cleanup_and_fail;
1154 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1160 dbus_set_error (error,
1161 DBUS_ERROR_SPAWN_FORK_FAILED,
1162 "Failed to fork (%s)",
1163 _dbus_strerror (errno));
1164 goto cleanup_and_fail;
1168 /* Immediate child, this is the babysitter process. */
1171 /* Be sure we crash if the parent exits
1172 * and we write to the err_report_pipe
1174 signal (SIGPIPE, SIG_DFL);
1176 /* Close the parent's end of the pipes. */
1177 close_and_invalidate (&child_err_report_pipe[READ_END]);
1178 close_and_invalidate (&babysitter_pipe[0]);
1180 /* Create the child that will exec () */
1181 grandchild_pid = fork ();
1183 if (grandchild_pid < 0)
1185 write_err_and_exit (babysitter_pipe[1],
1187 _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1189 else if (grandchild_pid == 0)
1191 do_exec (child_err_report_pipe[WRITE_END],
1193 child_setup, user_data);
1194 _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1198 babysit (grandchild_pid, babysitter_pipe[1]);
1199 _dbus_assert_not_reached ("Got to code after babysit()");
1204 /* Close the uncared-about ends of the pipes */
1205 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1206 close_and_invalidate (&babysitter_pipe[1]);
1208 sitter->socket_to_babysitter = babysitter_pipe[0];
1209 babysitter_pipe[0] = -1;
1211 sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1212 child_err_report_pipe[READ_END] = -1;
1214 sitter->sitter_pid = pid;
1216 if (sitter_p != NULL)
1219 _dbus_babysitter_unref (sitter);
1221 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1228 _DBUS_ASSERT_ERROR_IS_SET (error);
1230 close_and_invalidate (&child_err_report_pipe[READ_END]);
1231 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1232 close_and_invalidate (&babysitter_pipe[0]);
1233 close_and_invalidate (&babysitter_pipe[1]);
1236 _dbus_babysitter_unref (sitter);
1243 #ifdef DBUS_BUILD_TESTS
1246 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1248 while (LIVE_CHILDREN (sitter))
1249 babysitter_iteration (sitter, TRUE);
1253 check_spawn_nonexistent (void *data)
1255 char *argv[4] = { NULL, NULL, NULL, NULL };
1256 DBusBabysitter *sitter = NULL;
1257 DBusError error = DBUS_ERROR_INIT;
1259 /*** Test launching nonexistent binary */
1261 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1262 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1266 _dbus_babysitter_block_for_child_exit (sitter);
1267 _dbus_babysitter_set_child_exit_error (sitter, &error);
1271 _dbus_babysitter_unref (sitter);
1273 if (!dbus_error_is_set (&error))
1275 _dbus_warn ("Did not get an error launching nonexistent executable\n");
1279 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1280 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
1282 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1283 error.name, error.message);
1284 dbus_error_free (&error);
1288 dbus_error_free (&error);
1294 check_spawn_segfault (void *data)
1296 char *argv[4] = { NULL, NULL, NULL, NULL };
1297 DBusBabysitter *sitter = NULL;
1298 DBusError error = DBUS_ERROR_INIT;
1300 /*** Test launching segfault binary */
1302 argv[0] = TEST_SEGFAULT_BINARY;
1303 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1307 _dbus_babysitter_block_for_child_exit (sitter);
1308 _dbus_babysitter_set_child_exit_error (sitter, &error);
1312 _dbus_babysitter_unref (sitter);
1314 if (!dbus_error_is_set (&error))
1316 _dbus_warn ("Did not get an error launching segfaulting binary\n");
1320 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1321 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1323 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1324 error.name, error.message);
1325 dbus_error_free (&error);
1329 dbus_error_free (&error);
1335 check_spawn_exit (void *data)
1337 char *argv[4] = { NULL, NULL, NULL, NULL };
1338 DBusBabysitter *sitter = NULL;
1339 DBusError error = DBUS_ERROR_INIT;
1341 /*** Test launching exit failure binary */
1343 argv[0] = TEST_EXIT_BINARY;
1344 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1348 _dbus_babysitter_block_for_child_exit (sitter);
1349 _dbus_babysitter_set_child_exit_error (sitter, &error);
1353 _dbus_babysitter_unref (sitter);
1355 if (!dbus_error_is_set (&error))
1357 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1361 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1362 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
1364 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1365 error.name, error.message);
1366 dbus_error_free (&error);
1370 dbus_error_free (&error);
1376 check_spawn_and_kill (void *data)
1378 char *argv[4] = { NULL, NULL, NULL, NULL };
1379 DBusBabysitter *sitter = NULL;
1380 DBusError error = DBUS_ERROR_INIT;
1382 /*** Test launching sleeping binary then killing it */
1384 argv[0] = TEST_SLEEP_FOREVER_BINARY;
1385 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1389 _dbus_babysitter_kill_child (sitter);
1391 _dbus_babysitter_block_for_child_exit (sitter);
1393 _dbus_babysitter_set_child_exit_error (sitter, &error);
1397 _dbus_babysitter_unref (sitter);
1399 if (!dbus_error_is_set (&error))
1401 _dbus_warn ("Did not get an error after killing spawned binary\n");
1405 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1406 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1408 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1409 error.name, error.message);
1410 dbus_error_free (&error);
1414 dbus_error_free (&error);
1420 _dbus_spawn_test (const char *test_data_dir)
1422 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1423 check_spawn_nonexistent,
1427 if (!_dbus_test_oom_handling ("spawn_segfault",
1428 check_spawn_segfault,
1432 if (!_dbus_test_oom_handling ("spawn_exit",
1437 if (!_dbus_test_oom_handling ("spawn_and_kill",
1438 check_spawn_and_kill,