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
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"
39 extern char **environ;
42 * @addtogroup DBusInternalsUtils
47 * I'm pretty sure this whole spawn file could be made simpler,
48 * if you thought about it a bit.
52 * Enumeration for status of a read()
56 READ_STATUS_OK, /**< Read succeeded */
57 READ_STATUS_ERROR, /**< Some kind of error */
58 READ_STATUS_EOF /**< EOF returned */
71 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
73 retval = READ_STATUS_OK;
80 to_read = sizeof (int) * n_ints_in_buf - bytes;
91 if (chunk < 0 && errno == EINTR)
96 dbus_set_error (error,
97 DBUS_ERROR_SPAWN_FAILED,
98 "Failed to read from child pipe (%s)",
99 _dbus_strerror (errno));
101 retval = READ_STATUS_ERROR;
106 retval = READ_STATUS_EOF;
113 *n_ints_read = (int)(bytes / sizeof(int));
126 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
128 retval = READ_STATUS_OK;
135 to_read = sizeof (pid_t) - bytes;
143 ((char*)buf) + bytes,
145 if (chunk < 0 && errno == EINTR)
150 dbus_set_error (error,
151 DBUS_ERROR_SPAWN_FAILED,
152 "Failed to read from child pipe (%s)",
153 _dbus_strerror (errno));
155 retval = READ_STATUS_ERROR;
160 retval = READ_STATUS_EOF;
170 /* The implementation uses an intermediate child between the main process
171 * and the grandchild. The grandchild is our spawned process. The intermediate
172 * child is a babysitter process; it keeps track of when the grandchild
173 * exits/crashes, and reaps the grandchild.
176 /* Messages from children to parents */
179 CHILD_EXITED, /* This message is followed by the exit status int */
180 CHILD_FORK_FAILED, /* Followed by errno */
181 CHILD_EXEC_FAILED, /* Followed by errno */
182 CHILD_PID /* Followed by pid_t */
186 * Babysitter implementation details
188 struct DBusBabysitter
190 int refcount; /**< Reference count */
192 char *executable; /**< executable name to use in error messages */
194 int socket_to_babysitter; /**< Connection to the babysitter process */
195 int error_pipe_from_child; /**< Connection to the process that does the exec() */
197 pid_t sitter_pid; /**< PID Of the babysitter */
198 pid_t grandchild_pid; /**< PID of the grandchild */
200 DBusWatchList *watches; /**< Watches */
202 DBusWatch *error_watch; /**< Error pipe watch */
203 DBusWatch *sitter_watch; /**< Sitter pipe watch */
205 int errnum; /**< Error number */
206 int status; /**< Exit status code */
207 unsigned int have_child_status : 1; /**< True if child status has been reaped */
208 unsigned int have_fork_errnum : 1; /**< True if we have an error code from fork() */
209 unsigned int have_exec_errnum : 1; /**< True if we have an error code from exec() */
212 static DBusBabysitter*
213 _dbus_babysitter_new (void)
215 DBusBabysitter *sitter;
217 sitter = dbus_new0 (DBusBabysitter, 1);
221 sitter->refcount = 1;
223 sitter->socket_to_babysitter = -1;
224 sitter->error_pipe_from_child = -1;
226 sitter->sitter_pid = -1;
227 sitter->grandchild_pid = -1;
229 sitter->watches = _dbus_watch_list_new ();
230 if (sitter->watches == NULL)
236 _dbus_babysitter_unref (sitter);
241 * Increment the reference count on the babysitter object.
243 * @param sitter the babysitter
244 * @returns the babysitter
247 _dbus_babysitter_ref (DBusBabysitter *sitter)
249 _dbus_assert (sitter != NULL);
250 _dbus_assert (sitter->refcount > 0);
252 sitter->refcount += 1;
258 * Decrement the reference count on the babysitter object.
259 * When the reference count of the babysitter object reaches
260 * zero, the babysitter is killed and the child that was being
261 * babysat gets emancipated.
263 * @param sitter the babysitter
266 _dbus_babysitter_unref (DBusBabysitter *sitter)
268 _dbus_assert (sitter != NULL);
269 _dbus_assert (sitter->refcount > 0);
271 sitter->refcount -= 1;
272 if (sitter->refcount == 0)
274 if (sitter->socket_to_babysitter >= 0)
276 /* If we haven't forked other babysitters
277 * since this babysitter and socket were
278 * created then this close will cause the
279 * babysitter to wake up from poll with
280 * a hangup and then the babysitter will
283 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
284 sitter->socket_to_babysitter = -1;
287 if (sitter->error_pipe_from_child >= 0)
289 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
290 sitter->error_pipe_from_child = -1;
293 if (sitter->sitter_pid > 0)
298 /* It's possible the babysitter died on its own above
299 * from the close, or was killed randomly
300 * by some other process, so first try to reap it
302 ret = waitpid (sitter->sitter_pid, &status, WNOHANG);
304 /* If we couldn't reap the child then kill it, and
308 kill (sitter->sitter_pid, SIGKILL);
312 ret = waitpid (sitter->sitter_pid, &status, 0);
318 else if (errno == ECHILD)
319 _dbus_warn ("Babysitter process not available to be reaped; should not happen\n");
321 _dbus_warn ("Unexpected error %d in waitpid() for babysitter: %s\n",
322 errno, _dbus_strerror (errno));
326 _dbus_verbose ("Reaped %ld, waiting for babysitter %ld\n",
327 (long) ret, (long) sitter->sitter_pid);
329 if (WIFEXITED (sitter->status))
330 _dbus_verbose ("Babysitter exited with status %d\n",
331 WEXITSTATUS (sitter->status));
332 else if (WIFSIGNALED (sitter->status))
333 _dbus_verbose ("Babysitter received signal %d\n",
334 WTERMSIG (sitter->status));
336 _dbus_verbose ("Babysitter exited abnormally\n");
339 sitter->sitter_pid = -1;
342 if (sitter->error_watch)
344 _dbus_watch_invalidate (sitter->error_watch);
345 _dbus_watch_unref (sitter->error_watch);
346 sitter->error_watch = NULL;
349 if (sitter->sitter_watch)
351 _dbus_watch_invalidate (sitter->sitter_watch);
352 _dbus_watch_unref (sitter->sitter_watch);
353 sitter->sitter_watch = NULL;
357 _dbus_watch_list_free (sitter->watches);
359 dbus_free (sitter->executable);
366 read_data (DBusBabysitter *sitter,
371 DBusError error = DBUS_ERROR_INIT;
374 r = read_ints (fd, &what, 1, &got, &error);
378 case READ_STATUS_ERROR:
379 _dbus_warn ("Failed to read data from fd %d: %s\n", fd, error.message);
380 dbus_error_free (&error);
383 case READ_STATUS_EOF:
395 case CHILD_FORK_FAILED:
396 case CHILD_EXEC_FAILED:
400 r = read_ints (fd, &arg, 1, &got, &error);
404 case READ_STATUS_ERROR:
405 _dbus_warn ("Failed to read arg from fd %d: %s\n", fd, error.message);
406 dbus_error_free (&error);
408 case READ_STATUS_EOF:
416 if (what == CHILD_EXITED)
418 sitter->have_child_status = TRUE;
419 sitter->status = arg;
421 _dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
422 WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
423 WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
425 else if (what == CHILD_FORK_FAILED)
427 sitter->have_fork_errnum = TRUE;
428 sitter->errnum = arg;
429 _dbus_verbose ("recorded fork errnum %d\n", sitter->errnum);
431 else if (what == CHILD_EXEC_FAILED)
433 sitter->have_exec_errnum = TRUE;
434 sitter->errnum = arg;
435 _dbus_verbose ("recorded exec errnum %d\n", sitter->errnum);
444 r = read_pid (fd, &pid, &error);
448 case READ_STATUS_ERROR:
449 _dbus_warn ("Failed to read PID from fd %d: %s\n", fd, error.message);
450 dbus_error_free (&error);
452 case READ_STATUS_EOF:
458 sitter->grandchild_pid = pid;
460 _dbus_verbose ("recorded grandchild pid %d\n", sitter->grandchild_pid);
464 _dbus_warn ("Unknown message received from babysitter process\n");
473 close_socket_to_babysitter (DBusBabysitter *sitter)
475 _dbus_verbose ("Closing babysitter\n");
476 _dbus_close_socket (sitter->socket_to_babysitter, NULL);
477 sitter->socket_to_babysitter = -1;
481 close_error_pipe_from_child (DBusBabysitter *sitter)
483 _dbus_verbose ("Closing child error\n");
484 _dbus_close_socket (sitter->error_pipe_from_child, NULL);
485 sitter->error_pipe_from_child = -1;
489 handle_babysitter_socket (DBusBabysitter *sitter,
492 /* Even if we have POLLHUP, we want to keep reading
493 * data until POLLIN goes away; so this function only
494 * looks at HUP/ERR if no IN is set.
496 if (revents & _DBUS_POLLIN)
498 _dbus_verbose ("Reading data from babysitter\n");
499 if (read_data (sitter, sitter->socket_to_babysitter) != READ_STATUS_OK)
500 close_socket_to_babysitter (sitter);
502 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
504 close_socket_to_babysitter (sitter);
509 handle_error_pipe (DBusBabysitter *sitter,
512 if (revents & _DBUS_POLLIN)
514 _dbus_verbose ("Reading data from child error\n");
515 if (read_data (sitter, sitter->error_pipe_from_child) != READ_STATUS_OK)
516 close_error_pipe_from_child (sitter);
518 else if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
520 close_error_pipe_from_child (sitter);
524 /* returns whether there were any poll events handled */
526 babysitter_iteration (DBusBabysitter *sitter,
531 dbus_bool_t descriptors_ready;
533 descriptors_ready = FALSE;
537 if (sitter->error_pipe_from_child >= 0)
539 fds[i].fd = sitter->error_pipe_from_child;
540 fds[i].events = _DBUS_POLLIN;
545 if (sitter->socket_to_babysitter >= 0)
547 fds[i].fd = sitter->socket_to_babysitter;
548 fds[i].events = _DBUS_POLLIN;
559 ret = _dbus_poll (fds, i, 0);
561 while (ret < 0 && errno == EINTR);
563 if (ret == 0 && block)
567 ret = _dbus_poll (fds, i, -1);
569 while (ret < 0 && errno == EINTR);
574 descriptors_ready = TRUE;
579 if (fds[i].fd == sitter->error_pipe_from_child)
580 handle_error_pipe (sitter, fds[i].revents);
581 else if (fds[i].fd == sitter->socket_to_babysitter)
582 handle_babysitter_socket (sitter, fds[i].revents);
587 return descriptors_ready;
591 * Macro returns #TRUE if the babysitter still has live sockets open to the
592 * babysitter child or the grandchild.
594 #define LIVE_CHILDREN(sitter) ((sitter)->socket_to_babysitter >= 0 || (sitter)->error_pipe_from_child >= 0)
597 * Blocks until the babysitter process gives us the PID of the spawned grandchild,
598 * then kills the spawned grandchild.
600 * @param sitter the babysitter object
603 _dbus_babysitter_kill_child (DBusBabysitter *sitter)
605 /* be sure we have the PID of the child */
606 while (LIVE_CHILDREN (sitter) &&
607 sitter->grandchild_pid == -1)
608 babysitter_iteration (sitter, TRUE);
610 _dbus_verbose ("Got child PID %ld for killing\n",
611 (long) sitter->grandchild_pid);
613 if (sitter->grandchild_pid == -1)
614 return; /* child is already dead, or we're so hosed we'll never recover */
616 kill (sitter->grandchild_pid, SIGKILL);
620 * Checks whether the child has exited, without blocking.
622 * @param sitter the babysitter
625 _dbus_babysitter_get_child_exited (DBusBabysitter *sitter)
628 /* Be sure we're up-to-date */
629 while (LIVE_CHILDREN (sitter) &&
630 babysitter_iteration (sitter, FALSE))
633 /* We will have exited the babysitter when the child has exited */
634 return sitter->socket_to_babysitter < 0;
638 * Gets the exit status of the child. We do this so implementation specific
639 * detail is not cluttering up dbus, for example the system launcher code.
640 * This can only be called if the child has exited, i.e. call
641 * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
642 * did not return a status code, e.g. because the child was signaled
643 * or we failed to ever launch the child in the first place.
645 * @param sitter the babysitter
646 * @param status the returned status code
647 * @returns #FALSE on failure
650 _dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
653 if (!_dbus_babysitter_get_child_exited (sitter))
654 _dbus_assert_not_reached ("Child has not exited");
656 if (!sitter->have_child_status ||
657 !(WIFEXITED (sitter->status)))
660 *status = WEXITSTATUS (sitter->status);
665 * Sets the #DBusError with an explanation of why the spawned
666 * child process exited (on a signal, or whatever). If
667 * the child process has not exited, does nothing (error
668 * will remain unset).
670 * @param sitter the babysitter
671 * @param error an error to fill in
674 _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter,
677 if (!_dbus_babysitter_get_child_exited (sitter))
680 /* Note that if exec fails, we will also get a child status
681 * from the babysitter saying the child exited,
682 * so we need to give priority to the exec error
684 if (sitter->have_exec_errnum)
686 dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
687 "Failed to execute program %s: %s",
688 sitter->executable, _dbus_strerror (sitter->errnum));
690 else if (sitter->have_fork_errnum)
692 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
693 "Failed to fork a new process %s: %s",
694 sitter->executable, _dbus_strerror (sitter->errnum));
696 else if (sitter->have_child_status)
698 if (WIFEXITED (sitter->status))
699 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
700 "Process %s exited with status %d",
701 sitter->executable, WEXITSTATUS (sitter->status));
702 else if (WIFSIGNALED (sitter->status))
703 dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
704 "Process %s received signal %d",
705 sitter->executable, WTERMSIG (sitter->status));
707 dbus_set_error (error, DBUS_ERROR_FAILED,
708 "Process %s exited abnormally",
713 dbus_set_error (error, DBUS_ERROR_FAILED,
714 "Process %s exited, reason unknown",
720 * Sets watch functions to notify us when the
721 * babysitter object needs to read/write file descriptors.
723 * @param sitter the babysitter
724 * @param add_function function to begin monitoring a new descriptor.
725 * @param remove_function function to stop monitoring a descriptor.
726 * @param toggled_function function to notify when the watch is enabled/disabled
727 * @param data data to pass to add_function and remove_function.
728 * @param free_data_function function to be called to free the data.
729 * @returns #FALSE on failure (no memory)
732 _dbus_babysitter_set_watch_functions (DBusBabysitter *sitter,
733 DBusAddWatchFunction add_function,
734 DBusRemoveWatchFunction remove_function,
735 DBusWatchToggledFunction toggled_function,
737 DBusFreeFunction free_data_function)
739 return _dbus_watch_list_set_functions (sitter->watches,
748 handle_watch (DBusWatch *watch,
749 unsigned int condition,
752 DBusBabysitter *sitter = data;
757 if (condition & DBUS_WATCH_READABLE)
758 revents |= _DBUS_POLLIN;
759 if (condition & DBUS_WATCH_ERROR)
760 revents |= _DBUS_POLLERR;
761 if (condition & DBUS_WATCH_HANGUP)
762 revents |= _DBUS_POLLHUP;
764 fd = dbus_watch_get_socket (watch);
766 if (fd == sitter->error_pipe_from_child)
767 handle_error_pipe (sitter, revents);
768 else if (fd == sitter->socket_to_babysitter)
769 handle_babysitter_socket (sitter, revents);
771 while (LIVE_CHILDREN (sitter) &&
772 babysitter_iteration (sitter, FALSE))
778 /** Helps remember which end of the pipe is which */
780 /** Helps remember which end of the pipe is which */
784 /* Avoids a danger in threaded situations (calling close()
785 * on a file descriptor twice, and another thread has
786 * re-opened it since the first close)
789 close_and_invalidate (int *fd)
797 ret = _dbus_close_socket (*fd, NULL);
808 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
812 dbus_set_error (error,
813 DBUS_ERROR_SPAWN_FAILED,
814 "Failed to create pipe for communicating with child process (%s)",
815 _dbus_strerror (errno));
823 do_write (int fd, const void *buf, size_t count)
825 size_t bytes_written;
832 ret = write (fd, ((const char*)buf) + bytes_written, count - bytes_written);
840 _dbus_warn ("Failed to write data to pipe!\n");
841 exit (1); /* give up, we suck */
845 bytes_written += ret;
847 if (bytes_written < count)
852 write_err_and_exit (int fd, int msg)
856 do_write (fd, &msg, sizeof (msg));
857 do_write (fd, &en, sizeof (en));
863 write_pid (int fd, pid_t pid)
867 do_write (fd, &msg, sizeof (msg));
868 do_write (fd, &pid, sizeof (pid));
872 write_status_and_exit (int fd, int status)
874 int msg = CHILD_EXITED;
876 do_write (fd, &msg, sizeof (msg));
877 do_write (fd, &status, sizeof (status));
883 do_exec (int child_err_report_fd,
886 DBusSpawnChildSetupFunc child_setup,
889 #ifdef DBUS_BUILD_TESTS
893 _dbus_verbose_reset ();
894 _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
898 (* child_setup) (user_data);
900 #ifdef DBUS_BUILD_TESTS
901 max_open = sysconf (_SC_OPEN_MAX);
903 for (i = 3; i < max_open; i++)
907 if (i == child_err_report_fd)
910 retval = fcntl (i, F_GETFD);
912 if (retval != -1 && !(retval & FD_CLOEXEC))
913 _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
919 _dbus_assert (environ != NULL);
924 execve (argv[0], argv, envp);
927 write_err_and_exit (child_err_report_fd,
932 check_babysit_events (pid_t grandchild_pid,
941 ret = waitpid (grandchild_pid, &status, WNOHANG);
942 /* The man page says EINTR can't happen with WNOHANG,
943 * but there are reports of it (maybe only with valgrind?)
946 while (ret < 0 && errno == EINTR);
950 _dbus_verbose ("no child exited\n");
952 ; /* no child exited */
956 /* This isn't supposed to happen. */
957 _dbus_warn ("unexpected waitpid() failure in check_babysit_events(): %s\n",
958 _dbus_strerror (errno));
961 else if (ret == grandchild_pid)
964 _dbus_verbose ("reaped child pid %ld\n", (long) ret);
966 write_status_and_exit (parent_pipe, status);
970 _dbus_warn ("waitpid() reaped pid %d that we've never heard of\n",
975 if (revents & _DBUS_POLLIN)
977 _dbus_verbose ("babysitter got POLLIN from parent pipe\n");
980 if (revents & (_DBUS_POLLERR | _DBUS_POLLHUP))
982 /* Parent is gone, so we just exit */
983 _dbus_verbose ("babysitter got POLLERR or POLLHUP from parent\n");
988 static int babysit_sigchld_pipe = -1;
991 babysit_signal_handler (int signo)
995 if (write (babysit_sigchld_pipe, &b, 1) <= 0)
1001 babysit (pid_t grandchild_pid,
1004 int sigchld_pipe[2];
1006 /* We don't exec, so we keep parent state, such as the pid that
1007 * _dbus_verbose() uses. Reset the pid here.
1009 _dbus_verbose_reset ();
1011 /* I thought SIGCHLD would just wake up the poll, but
1012 * that didn't seem to work, so added this pipe.
1013 * Probably the pipe is more likely to work on busted
1014 * operating systems anyhow.
1016 if (pipe (sigchld_pipe) < 0)
1018 _dbus_warn ("Not enough file descriptors to create pipe in babysitter process\n");
1022 babysit_sigchld_pipe = sigchld_pipe[WRITE_END];
1024 _dbus_set_signal_handler (SIGCHLD, babysit_signal_handler);
1026 write_pid (parent_pipe, grandchild_pid);
1028 check_babysit_events (grandchild_pid, parent_pipe, 0);
1034 pfds[0].fd = parent_pipe;
1035 pfds[0].events = _DBUS_POLLIN;
1036 pfds[0].revents = 0;
1038 pfds[1].fd = sigchld_pipe[READ_END];
1039 pfds[1].events = _DBUS_POLLIN;
1040 pfds[1].revents = 0;
1042 if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
1044 _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
1048 if (pfds[0].revents != 0)
1050 check_babysit_events (grandchild_pid, parent_pipe, pfds[0].revents);
1052 else if (pfds[1].revents & _DBUS_POLLIN)
1055 read (sigchld_pipe[READ_END], &b, 1);
1056 /* do waitpid check */
1057 check_babysit_events (grandchild_pid, parent_pipe, 0);
1065 * Spawns a new process. The executable name and argv[0]
1066 * are the same, both are provided in argv[0]. The child_setup
1067 * function is passed the given user_data and is run in the child
1068 * just before calling exec().
1070 * Also creates a "babysitter" which tracks the status of the
1071 * child process, advising the parent if the child exits.
1072 * If the spawn fails, no babysitter is created.
1073 * If sitter_p is #NULL, no babysitter is kept.
1075 * @param sitter_p return location for babysitter or #NULL
1076 * @param argv the executable and arguments
1077 * @param env the environment (not used on unix yet)
1078 * @param child_setup function to call in child pre-exec()
1079 * @param user_data user data for setup function
1080 * @param error error object to be filled in if function fails
1081 * @returns #TRUE on success, #FALSE if error is filled in
1084 _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p,
1087 DBusSpawnChildSetupFunc child_setup,
1091 DBusBabysitter *sitter;
1092 int child_err_report_pipe[2] = { -1, -1 };
1093 int babysitter_pipe[2] = { -1, -1 };
1096 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1098 if (sitter_p != NULL)
1103 sitter = _dbus_babysitter_new ();
1106 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1110 sitter->executable = _dbus_strdup (argv[0]);
1111 if (sitter->executable == NULL)
1113 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1114 goto cleanup_and_fail;
1117 if (!make_pipe (child_err_report_pipe, error))
1118 goto cleanup_and_fail;
1120 _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
1121 _dbus_fd_set_close_on_exec (child_err_report_pipe[WRITE_END]);
1123 if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
1124 goto cleanup_and_fail;
1126 _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
1127 _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
1129 /* Setting up the babysitter is only useful in the parent,
1130 * but we don't want to run out of memory and fail
1131 * after we've already forked, since then we'd leak
1132 * child processes everywhere.
1134 sitter->error_watch = _dbus_watch_new (child_err_report_pipe[READ_END],
1135 DBUS_WATCH_READABLE,
1136 TRUE, handle_watch, sitter, NULL);
1137 if (sitter->error_watch == NULL)
1139 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1140 goto cleanup_and_fail;
1143 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
1145 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1146 goto cleanup_and_fail;
1149 sitter->sitter_watch = _dbus_watch_new (babysitter_pipe[0],
1150 DBUS_WATCH_READABLE,
1151 TRUE, handle_watch, sitter, NULL);
1152 if (sitter->sitter_watch == NULL)
1154 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1155 goto cleanup_and_fail;
1158 if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
1160 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1161 goto cleanup_and_fail;
1164 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1170 dbus_set_error (error,
1171 DBUS_ERROR_SPAWN_FORK_FAILED,
1172 "Failed to fork (%s)",
1173 _dbus_strerror (errno));
1174 goto cleanup_and_fail;
1178 /* Immediate child, this is the babysitter process. */
1181 /* Be sure we crash if the parent exits
1182 * and we write to the err_report_pipe
1184 signal (SIGPIPE, SIG_DFL);
1186 /* Close the parent's end of the pipes. */
1187 close_and_invalidate (&child_err_report_pipe[READ_END]);
1188 close_and_invalidate (&babysitter_pipe[0]);
1190 /* Create the child that will exec () */
1191 grandchild_pid = fork ();
1193 if (grandchild_pid < 0)
1195 write_err_and_exit (babysitter_pipe[1],
1197 _dbus_assert_not_reached ("Got to code after write_err_and_exit()");
1199 else if (grandchild_pid == 0)
1201 do_exec (child_err_report_pipe[WRITE_END],
1204 child_setup, user_data);
1205 _dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
1209 babysit (grandchild_pid, babysitter_pipe[1]);
1210 _dbus_assert_not_reached ("Got to code after babysit()");
1215 /* Close the uncared-about ends of the pipes */
1216 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1217 close_and_invalidate (&babysitter_pipe[1]);
1219 sitter->socket_to_babysitter = babysitter_pipe[0];
1220 babysitter_pipe[0] = -1;
1222 sitter->error_pipe_from_child = child_err_report_pipe[READ_END];
1223 child_err_report_pipe[READ_END] = -1;
1225 sitter->sitter_pid = pid;
1227 if (sitter_p != NULL)
1230 _dbus_babysitter_unref (sitter);
1232 dbus_free_string_array (env);
1234 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1241 _DBUS_ASSERT_ERROR_IS_SET (error);
1243 close_and_invalidate (&child_err_report_pipe[READ_END]);
1244 close_and_invalidate (&child_err_report_pipe[WRITE_END]);
1245 close_and_invalidate (&babysitter_pipe[0]);
1246 close_and_invalidate (&babysitter_pipe[1]);
1249 _dbus_babysitter_unref (sitter);
1256 #ifdef DBUS_BUILD_TESTS
1259 _dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
1261 while (LIVE_CHILDREN (sitter))
1262 babysitter_iteration (sitter, TRUE);
1266 check_spawn_nonexistent (void *data)
1268 char *argv[4] = { NULL, NULL, NULL, NULL };
1269 DBusBabysitter *sitter = NULL;
1270 DBusError error = DBUS_ERROR_INIT;
1272 /*** Test launching nonexistent binary */
1274 argv[0] = "/this/does/not/exist/32542sdgafgafdg";
1275 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1279 _dbus_babysitter_block_for_child_exit (sitter);
1280 _dbus_babysitter_set_child_exit_error (sitter, &error);
1284 _dbus_babysitter_unref (sitter);
1286 if (!dbus_error_is_set (&error))
1288 _dbus_warn ("Did not get an error launching nonexistent executable\n");
1292 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1293 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_EXEC_FAILED)))
1295 _dbus_warn ("Not expecting error when launching nonexistent executable: %s: %s\n",
1296 error.name, error.message);
1297 dbus_error_free (&error);
1301 dbus_error_free (&error);
1307 check_spawn_segfault (void *data)
1309 char *argv[4] = { NULL, NULL, NULL, NULL };
1310 DBusBabysitter *sitter = NULL;
1311 DBusError error = DBUS_ERROR_INIT;
1313 /*** Test launching segfault binary */
1315 argv[0] = TEST_SEGFAULT_BINARY;
1316 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1320 _dbus_babysitter_block_for_child_exit (sitter);
1321 _dbus_babysitter_set_child_exit_error (sitter, &error);
1325 _dbus_babysitter_unref (sitter);
1327 if (!dbus_error_is_set (&error))
1329 _dbus_warn ("Did not get an error launching segfaulting binary\n");
1333 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1334 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1336 _dbus_warn ("Not expecting error when launching segfaulting executable: %s: %s\n",
1337 error.name, error.message);
1338 dbus_error_free (&error);
1342 dbus_error_free (&error);
1348 check_spawn_exit (void *data)
1350 char *argv[4] = { NULL, NULL, NULL, NULL };
1351 DBusBabysitter *sitter = NULL;
1352 DBusError error = DBUS_ERROR_INIT;
1354 /*** Test launching exit failure binary */
1356 argv[0] = TEST_EXIT_BINARY;
1357 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1361 _dbus_babysitter_block_for_child_exit (sitter);
1362 _dbus_babysitter_set_child_exit_error (sitter, &error);
1366 _dbus_babysitter_unref (sitter);
1368 if (!dbus_error_is_set (&error))
1370 _dbus_warn ("Did not get an error launching binary that exited with failure code\n");
1374 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1375 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)))
1377 _dbus_warn ("Not expecting error when launching exiting executable: %s: %s\n",
1378 error.name, error.message);
1379 dbus_error_free (&error);
1383 dbus_error_free (&error);
1389 check_spawn_and_kill (void *data)
1391 char *argv[4] = { NULL, NULL, NULL, NULL };
1392 DBusBabysitter *sitter = NULL;
1393 DBusError error = DBUS_ERROR_INIT;
1395 /*** Test launching sleeping binary then killing it */
1397 argv[0] = TEST_SLEEP_FOREVER_BINARY;
1398 if (_dbus_spawn_async_with_babysitter (&sitter, argv,
1402 _dbus_babysitter_kill_child (sitter);
1404 _dbus_babysitter_block_for_child_exit (sitter);
1406 _dbus_babysitter_set_child_exit_error (sitter, &error);
1410 _dbus_babysitter_unref (sitter);
1412 if (!dbus_error_is_set (&error))
1414 _dbus_warn ("Did not get an error after killing spawned binary\n");
1418 if (!(dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY) ||
1419 dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_SIGNALED)))
1421 _dbus_warn ("Not expecting error when killing executable: %s: %s\n",
1422 error.name, error.message);
1423 dbus_error_free (&error);
1427 dbus_error_free (&error);
1433 _dbus_spawn_test (const char *test_data_dir)
1435 if (!_dbus_test_oom_handling ("spawn_nonexistent",
1436 check_spawn_nonexistent,
1440 if (!_dbus_test_oom_handling ("spawn_segfault",
1441 check_spawn_segfault,
1445 if (!_dbus_test_oom_handling ("spawn_exit",
1450 if (!_dbus_test_oom_handling ("spawn_and_kill",
1451 check_spawn_and_kill,