From f116b1282f8482dff6dc797f64270d95f79b53a3 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Sun, 16 Feb 2003 10:00:37 +0000 Subject: 2003-02-16 Anders Carlsson * bus/activation.c: (load_directory), (bus_activation_init), (bus_activation_activate_service): * bus/activation.h: * bus/driver.c: (bus_driver_handle_activate_service), (bus_driver_handle_message): More work on the activation handling. * dbus/dbus-errors.h: Add some error messages * dbus/dbus-message.c: (dbus_message_new_error_reply): * dbus/dbus-message.h: New function that creates an error message. * dbus/dbus-protocol.h: Add ACTIVATE_SERVER message. * dbus/dbus-server-unix.c: (unix_handle_watch), (_dbus_server_new_for_domain_socket): Call _dbus_fd_set_close_on_exec. * dbus/dbus-sysdeps.c: (make_pipe), (do_exec), (_dbus_spawn_async), (_dbus_disable_sigpipe), (_dbus_fd_set_close_on_exec): * dbus/dbus-sysdeps.h: Add _dbus_fd_set_close_on exec function. Also add function that checks that all open fds are set to close-on-exec and warns otherwise. * dbus/dbus-transport-unix.c: (_dbus_transport_new_for_domain_socket): Call _dbus_fd_set_close_on_exec. --- dbus/dbus-sysdeps.c | 61 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'dbus/dbus-sysdeps.c') diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 67677707..83b1f044 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -281,7 +281,7 @@ _dbus_connect_unix_socket (const char *path, struct sockaddr_un addr; fd = socket (AF_LOCAL, SOCK_STREAM, 0); - + if (fd < 0) { dbus_set_result (result, @@ -340,7 +340,7 @@ _dbus_listen_unix_socket (const char *path, struct sockaddr_un addr; listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0); - + if (listen_fd < 0) { dbus_set_result (result, _dbus_result_from_errno (errno)); @@ -1490,7 +1490,11 @@ make_pipe (int p[2], return FALSE; } else - return TRUE; + { + _dbus_fd_set_close_on_exec (p[0]); + _dbus_fd_set_close_on_exec (p[1]); + return TRUE; + } } enum @@ -1563,6 +1567,23 @@ static void do_exec (int child_err_report_fd, char **argv) { +#ifdef DBUS_BUILD_TESTS + int i, max_open; + + max_open = sysconf (_SC_OPEN_MAX); + + + for (i = 3; i < max_open; i++) + { + int retval; + + retval = fcntl (i, F_GETFD); + + if (retval != -1 && !(retval & FD_CLOEXEC)) + _dbus_warn ("Fd %d did not have the close-on-exec flag set!\n", i); + } +#endif + execvp (argv[0], argv); /* Exec failed */ @@ -1577,19 +1598,13 @@ _dbus_spawn_async (char **argv, { int pid = -1, grandchild_pid; int child_err_report_pipe[2] = { -1, -1 }; - int child_pid_report_pipe[2] = { -1, -1 }; int status; - printf ("spawning application: %s\n", argv[0]); - if (!make_pipe (child_err_report_pipe, error)) return FALSE; - if (!make_pipe (child_pid_report_pipe, error)) - goto cleanup_and_fail; - pid = fork (); - + if (pid < 0) { dbus_set_error (error, @@ -1612,7 +1627,6 @@ _dbus_spawn_async (char **argv, * though */ close_and_invalidate (&child_err_report_pipe[0]); - close_and_invalidate (&child_pid_report_pipe[0]); /* We need to fork an intermediate child that launches the * final child. The purpose of the intermediate child @@ -1623,10 +1637,6 @@ _dbus_spawn_async (char **argv, if (grandchild_pid < 0) { - /* report -1 as child PID */ - write (child_pid_report_pipe[1], &grandchild_pid, - sizeof(grandchild_pid)); - write_err_and_exit (child_err_report_pipe[1], CHILD_FORK_FAILED); } @@ -1637,9 +1647,6 @@ _dbus_spawn_async (char **argv, } else { - write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid)); - close_and_invalidate (&child_pid_report_pipe[1]); - _exit (0); } } @@ -1652,7 +1659,6 @@ _dbus_spawn_async (char **argv, /* Close the uncared-about ends of the pipes */ close_and_invalidate (&child_err_report_pipe[1]); - close_and_invalidate (&child_pid_report_pipe[1]); wait_again: if (waitpid (pid, &status, 0) < 0) @@ -1716,8 +1722,6 @@ _dbus_spawn_async (char **argv, close_and_invalidate (&child_err_report_pipe[0]); close_and_invalidate (&child_err_report_pipe[1]); - close_and_invalidate (&child_pid_report_pipe[0]); - close_and_invalidate (&child_pid_report_pipe[1]); return FALSE; } @@ -1731,4 +1735,19 @@ _dbus_disable_sigpipe (void) signal (SIGPIPE, SIG_IGN); } +void +_dbus_fd_set_close_on_exec (int fd) +{ + int val; + + val = fcntl (fd, F_GETFD, 0); + + if (val < 0) + return; + + val |= FD_CLOEXEC; + + fcntl (fd, F_SETFD, val); +} + /** @} end of sysdeps */ -- cgit