-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-spawn.c Wrapper around fork/exec
*
* Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+
+#include <config.h>
+
#include "dbus-spawn.h"
-#include "dbus-sysdeps.h"
+#include "dbus-sysdeps-unix.h"
#include "dbus-internals.h"
#include "dbus-test.h"
#include "dbus-protocol.h"
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
-#include <errno.h>
#include <stdlib.h>
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+extern char **environ;
/**
* @addtogroup DBusInternalsUtils
while (TRUE)
{
- size_t chunk;
+ ssize_t chunk;
size_t to_read;
to_read = sizeof (int) * n_ints_in_buf - bytes;
while (TRUE)
{
- size_t chunk;
+ ssize_t chunk;
size_t to_read;
-
+
to_read = sizeof (pid_t) - bytes;
if (to_read == 0)
DBusWatch *error_watch; /**< Error pipe watch */
DBusWatch *sitter_watch; /**< Sitter pipe watch */
+ DBusBabysitterFinishedFunc finished_cb;
+ void *finished_data;
+
int errnum; /**< Error number */
int status; /**< Exit status code */
unsigned int have_child_status : 1; /**< True if child status has been reaped */
return sitter;
}
+static void close_socket_to_babysitter (DBusBabysitter *sitter);
+static void close_error_pipe_from_child (DBusBabysitter *sitter);
+
/**
* Decrement the reference count on the babysitter object.
* When the reference count of the babysitter object reaches
sitter->refcount -= 1;
if (sitter->refcount == 0)
- {
- if (sitter->socket_to_babysitter >= 0)
- {
- /* If we haven't forked other babysitters
- * since this babysitter and socket were
- * created then this close will cause the
- * babysitter to wake up from poll with
- * a hangup and then the babysitter will
- * quit itself.
- */
- _dbus_close_socket (sitter->socket_to_babysitter, NULL);
- sitter->socket_to_babysitter = -1;
- }
+ {
+ /* If we haven't forked other babysitters
+ * since this babysitter and socket were
+ * created then this close will cause the
+ * babysitter to wake up from poll with
+ * a hangup and then the babysitter will
+ * quit itself.
+ */
+ close_socket_to_babysitter (sitter);
- if (sitter->error_pipe_from_child >= 0)
- {
- _dbus_close_socket (sitter->error_pipe_from_child, NULL);
- sitter->error_pipe_from_child = -1;
- }
+ close_error_pipe_from_child (sitter);
if (sitter->sitter_pid > 0)
{
sitter->sitter_pid = -1;
}
-
- if (sitter->error_watch)
- {
- _dbus_watch_invalidate (sitter->error_watch);
- _dbus_watch_unref (sitter->error_watch);
- sitter->error_watch = NULL;
- }
- if (sitter->sitter_watch)
- {
- _dbus_watch_invalidate (sitter->sitter_watch);
- _dbus_watch_unref (sitter->sitter_watch);
- sitter->sitter_watch = NULL;
- }
-
if (sitter->watches)
_dbus_watch_list_free (sitter->watches);
{
int what;
int got;
- DBusError error;
+ DBusError error = DBUS_ERROR_INIT;
ReadStatus r;
-
- dbus_error_init (&error);
-
+
r = read_ints (fd, &what, 1, &got, &error);
switch (r)
{
sitter->have_child_status = TRUE;
sitter->status = arg;
+ sitter->errnum = 0;
_dbus_verbose ("recorded child status exited = %d signaled = %d exitstatus = %d termsig = %d\n",
WIFEXITED (sitter->status), WIFSIGNALED (sitter->status),
WEXITSTATUS (sitter->status), WTERMSIG (sitter->status));
close_socket_to_babysitter (DBusBabysitter *sitter)
{
_dbus_verbose ("Closing babysitter\n");
- _dbus_close_socket (sitter->socket_to_babysitter, NULL);
- sitter->socket_to_babysitter = -1;
+
+ if (sitter->sitter_watch != NULL)
+ {
+ _dbus_assert (sitter->watches != NULL);
+ _dbus_watch_list_remove_watch (sitter->watches, sitter->sitter_watch);
+ _dbus_watch_invalidate (sitter->sitter_watch);
+ _dbus_watch_unref (sitter->sitter_watch);
+ sitter->sitter_watch = NULL;
+ }
+
+ if (sitter->socket_to_babysitter >= 0)
+ {
+ _dbus_close_socket (sitter->socket_to_babysitter, NULL);
+ sitter->socket_to_babysitter = -1;
+ }
}
static void
close_error_pipe_from_child (DBusBabysitter *sitter)
{
_dbus_verbose ("Closing child error\n");
- _dbus_close_socket (sitter->error_pipe_from_child, NULL);
- sitter->error_pipe_from_child = -1;
+
+ if (sitter->error_watch != NULL)
+ {
+ _dbus_assert (sitter->watches != NULL);
+ _dbus_watch_list_remove_watch (sitter->watches, sitter->error_watch);
+ _dbus_watch_invalidate (sitter->error_watch);
+ _dbus_watch_unref (sitter->error_watch);
+ sitter->error_watch = NULL;
+ }
+
+ if (sitter->error_pipe_from_child >= 0)
+ {
+ _dbus_close_socket (sitter->error_pipe_from_child, NULL);
+ sitter->error_pipe_from_child = -1;
+ }
}
static void
{
int ret;
- ret = _dbus_poll (fds, i, 0);
+ do
+ {
+ ret = _dbus_poll (fds, i, 0);
+ }
+ while (ret < 0 && errno == EINTR);
+
if (ret == 0 && block)
- ret = _dbus_poll (fds, i, -1);
-
+ {
+ do
+ {
+ ret = _dbus_poll (fds, i, -1);
+ }
+ while (ret < 0 && errno == EINTR);
+ }
+
if (ret > 0)
{
descriptors_ready = TRUE;
}
/**
+ * Gets the exit status of the child. We do this so implementation specific
+ * detail is not cluttering up dbus, for example the system launcher code.
+ * This can only be called if the child has exited, i.e. call
+ * _dbus_babysitter_get_child_exited(). It returns FALSE if the child
+ * did not return a status code, e.g. because the child was signaled
+ * or we failed to ever launch the child in the first place.
+ *
+ * @param sitter the babysitter
+ * @param status the returned status code
+ * @returns #FALSE on failure
+ */
+dbus_bool_t
+_dbus_babysitter_get_child_exit_status (DBusBabysitter *sitter,
+ int *status)
+{
+ if (!_dbus_babysitter_get_child_exited (sitter))
+ _dbus_assert_not_reached ("Child has not exited");
+
+ if (!sitter->have_child_status ||
+ !(WIFEXITED (sitter->status)))
+ return FALSE;
+
+ *status = WEXITSTATUS (sitter->status);
+ return TRUE;
+}
+
+/**
* Sets the #DBusError with an explanation of why the spawned
* child process exited (on a signal, or whatever). If
* the child process has not exited, does nothing (error
unsigned int condition,
void *data)
{
- DBusBabysitter *sitter = data;
+ DBusBabysitter *sitter = _dbus_babysitter_ref (data);
int revents;
int fd;
if (condition & DBUS_WATCH_HANGUP)
revents |= _DBUS_POLLHUP;
- fd = dbus_watch_get_fd (watch);
+ fd = dbus_watch_get_socket (watch);
if (fd == sitter->error_pipe_from_child)
handle_error_pipe (sitter, revents);
while (LIVE_CHILDREN (sitter) &&
babysitter_iteration (sitter, FALSE))
;
-
+
+ /* fd.o #32992: if the handle_* methods closed their sockets, they previously
+ * didn't always remove the watches. Check that we don't regress. */
+ _dbus_assert (sitter->socket_to_babysitter != -1 || sitter->sitter_watch == NULL);
+ _dbus_assert (sitter->error_pipe_from_child != -1 || sitter->error_watch == NULL);
+
+ if (_dbus_babysitter_get_child_exited (sitter) &&
+ sitter->finished_cb != NULL)
+ {
+ sitter->finished_cb (sitter, sitter->finished_data);
+ sitter->finished_cb = NULL;
+ }
+
+ _dbus_babysitter_unref (sitter);
return TRUE;
}
make_pipe (int p[2],
DBusError *error)
{
+ int retval;
+
+#ifdef HAVE_PIPE2
+ dbus_bool_t cloexec_done;
+
+ retval = pipe2 (p, O_CLOEXEC);
+ cloexec_done = retval >= 0;
+
+ /* Check if kernel seems to be too old to know pipe2(). We assume
+ that if pipe2 is available, O_CLOEXEC is too. */
+ if (retval < 0 && errno == ENOSYS)
+#endif
+ {
+ retval = pipe(p);
+ }
+
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
- if (pipe (p) < 0)
+
+ if (retval < 0)
{
dbus_set_error (error,
DBUS_ERROR_SPAWN_FAILED,
return FALSE;
}
+#ifdef HAVE_PIPE2
+ if (!cloexec_done)
+#endif
+ {
+ _dbus_fd_set_close_on_exec (p[0]);
+ _dbus_fd_set_close_on_exec (p[1]);
+ }
+
return TRUE;
}
static void
do_exec (int child_err_report_fd,
char **argv,
+ char **envp,
DBusSpawnChildSetupFunc child_setup,
void *user_data)
{
#endif
_dbus_verbose_reset ();
- _dbus_verbose ("Child process has PID %lu\n",
+ _dbus_verbose ("Child process has PID " DBUS_PID_FORMAT "\n",
_dbus_getpid ());
if (child_setup)
_dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i);
}
#endif
+
+ if (envp == NULL)
+ {
+ _dbus_assert (environ != NULL);
+
+ envp = environ;
+ }
- execv (argv[0], argv);
+ execve (argv[0], argv, envp);
/* Exec failed */
write_err_and_exit (child_err_report_fd,
{
char b = '\0';
again:
- write (babysit_sigchld_pipe, &b, 1);
- if (errno == EINTR)
- goto again;
+ if (write (babysit_sigchld_pipe, &b, 1) <= 0)
+ if (errno == EINTR)
+ goto again;
}
static void
pfds[1].events = _DBUS_POLLIN;
pfds[1].revents = 0;
- _dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1);
+ if (_dbus_poll (pfds, _DBUS_N_ELEMENTS (pfds), -1) < 0 && errno != EINTR)
+ {
+ _dbus_warn ("_dbus_poll() error: %s\n", strerror (errno));
+ exit (1);
+ }
if (pfds[0].revents != 0)
{
else if (pfds[1].revents & _DBUS_POLLIN)
{
char b;
- read (sigchld_pipe[READ_END], &b, 1);
+ if (read (sigchld_pipe[READ_END], &b, 1) == -1)
+ {
+ /* ignore */
+ }
/* do waitpid check */
check_babysit_events (grandchild_pid, parent_pipe, 0);
}
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
- *sitter_p = NULL;
+ if (sitter_p != NULL)
+ *sitter_p = NULL;
+
sitter = NULL;
sitter = _dbus_babysitter_new ();
if (!make_pipe (child_err_report_pipe, error))
goto cleanup_and_fail;
- _dbus_fd_set_close_on_exec (child_err_report_pipe[READ_END]);
- _dbus_fd_set_close_on_exec (child_err_report_pipe[WRITE_END]);
-
if (!_dbus_full_duplex_pipe (&babysitter_pipe[0], &babysitter_pipe[1], TRUE, error))
goto cleanup_and_fail;
- _dbus_fd_set_close_on_exec (babysitter_pipe[0]);
- _dbus_fd_set_close_on_exec (babysitter_pipe[1]);
-
/* Setting up the babysitter is only useful in the parent,
* but we don't want to run out of memory and fail
* after we've already forked, since then we'd leak
if (!_dbus_watch_list_add_watch (sitter->watches, sitter->error_watch))
{
+ /* we need to free it early so the destructor won't try to remove it
+ * without it having been added, which DBusLoop doesn't allow */
+ _dbus_watch_invalidate (sitter->error_watch);
+ _dbus_watch_unref (sitter->error_watch);
+ sitter->error_watch = NULL;
+
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto cleanup_and_fail;
}
if (!_dbus_watch_list_add_watch (sitter->watches, sitter->sitter_watch))
{
+ /* we need to free it early so the destructor won't try to remove it
+ * without it having been added, which DBusLoop doesn't allow */
+ _dbus_watch_invalidate (sitter->sitter_watch);
+ _dbus_watch_unref (sitter->sitter_watch);
+ sitter->sitter_watch = NULL;
+
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
goto cleanup_and_fail;
}
_dbus_assert_not_reached ("Got to code after write_err_and_exit()");
}
else if (grandchild_pid == 0)
- {
+ {
+ /* Go back to ignoring SIGPIPE, since it's evil
+ */
+ signal (SIGPIPE, SIG_IGN);
+
do_exec (child_err_report_pipe[WRITE_END],
argv,
+ env,
child_setup, user_data);
_dbus_assert_not_reached ("Got to code after exec() - should have exited on error");
}
else
_dbus_babysitter_unref (sitter);
+ dbus_free_string_array (env);
+
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return TRUE;
return FALSE;
}
+void
+_dbus_babysitter_set_result_function (DBusBabysitter *sitter,
+ DBusBabysitterFinishedFunc finished,
+ void *user_data)
+{
+ sitter->finished_cb = finished;
+ sitter->finished_data = user_data;
+}
+
/** @} */
#ifdef DBUS_BUILD_TESTS
+static char *
+get_test_exec (const char *exe,
+ DBusString *scratch_space)
+{
+ const char *dbus_test_exec;
+
+ dbus_test_exec = _dbus_getenv ("DBUS_TEST_EXEC");
+
+ if (dbus_test_exec == NULL)
+ dbus_test_exec = DBUS_TEST_EXEC;
+
+ if (!_dbus_string_init (scratch_space))
+ return NULL;
+
+ if (!_dbus_string_append_printf (scratch_space, "%s/%s%s",
+ dbus_test_exec, exe, DBUS_EXEEXT))
+ {
+ _dbus_string_free (scratch_space);
+ return NULL;
+ }
+
+ return _dbus_string_get_data (scratch_space);
+}
+
static void
_dbus_babysitter_block_for_child_exit (DBusBabysitter *sitter)
{
check_spawn_nonexistent (void *data)
{
char *argv[4] = { NULL, NULL, NULL, NULL };
- DBusBabysitter *sitter;
- DBusError error;
-
- sitter = NULL;
-
- dbus_error_init (&error);
+ DBusBabysitter *sitter = NULL;
+ DBusError error = DBUS_ERROR_INIT;
/*** Test launching nonexistent binary */
check_spawn_segfault (void *data)
{
char *argv[4] = { NULL, NULL, NULL, NULL };
- DBusBabysitter *sitter;
- DBusError error;
-
- sitter = NULL;
-
- dbus_error_init (&error);
+ DBusBabysitter *sitter = NULL;
+ DBusError error = DBUS_ERROR_INIT;
+ DBusString argv0;
/*** Test launching segfault binary */
-
- argv[0] = TEST_SEGFAULT_BINARY;
+
+ argv[0] = get_test_exec ("test-segfault", &argv0);
+
+ if (argv[0] == NULL)
+ {
+ /* OOM was simulated, never mind */
+ return TRUE;
+ }
+
if (_dbus_spawn_async_with_babysitter (&sitter, argv,
NULL, NULL, NULL,
&error))
_dbus_babysitter_set_child_exit_error (sitter, &error);
}
+ _dbus_string_free (&argv0);
+
if (sitter)
_dbus_babysitter_unref (sitter);
check_spawn_exit (void *data)
{
char *argv[4] = { NULL, NULL, NULL, NULL };
- DBusBabysitter *sitter;
- DBusError error;
-
- sitter = NULL;
-
- dbus_error_init (&error);
+ DBusBabysitter *sitter = NULL;
+ DBusError error = DBUS_ERROR_INIT;
+ DBusString argv0;
/*** Test launching exit failure binary */
-
- argv[0] = TEST_EXIT_BINARY;
+
+ argv[0] = get_test_exec ("test-exit", &argv0);
+
+ if (argv[0] == NULL)
+ {
+ /* OOM was simulated, never mind */
+ return TRUE;
+ }
+
if (_dbus_spawn_async_with_babysitter (&sitter, argv,
NULL, NULL, NULL,
&error))
_dbus_babysitter_set_child_exit_error (sitter, &error);
}
+ _dbus_string_free (&argv0);
+
if (sitter)
_dbus_babysitter_unref (sitter);
check_spawn_and_kill (void *data)
{
char *argv[4] = { NULL, NULL, NULL, NULL };
- DBusBabysitter *sitter;
- DBusError error;
-
- sitter = NULL;
-
- dbus_error_init (&error);
+ DBusBabysitter *sitter = NULL;
+ DBusError error = DBUS_ERROR_INIT;
+ DBusString argv0;
/*** Test launching sleeping binary then killing it */
- argv[0] = TEST_SLEEP_FOREVER_BINARY;
+ argv[0] = get_test_exec ("test-sleep-forever", &argv0);
+
+ if (argv[0] == NULL)
+ {
+ /* OOM was simulated, never mind */
+ return TRUE;
+ }
+
if (_dbus_spawn_async_with_babysitter (&sitter, argv,
NULL, NULL, NULL,
&error))
_dbus_babysitter_set_child_exit_error (sitter, &error);
}
+ _dbus_string_free (&argv0);
+
if (sitter)
_dbus_babysitter_unref (sitter);