From 36f9d70182c4c2f2e617192ae34abd6c44549a78 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 30 Apr 2014 02:45:01 +0200 Subject: shell: close all fds > 2 after forking socket/fcntl to set FD_CLOEXEC isn't thread-safe with async verify handlers, and SOCK_CLOEXEC isn't portable. --- src/fastd.c | 6 +++--- src/fastd.h | 2 ++ src/shell.c | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/fastd.c b/src/fastd.c index 523889a..191c82d 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -378,7 +378,7 @@ static inline void maintenance(void) { } -static void close_fds(void) { +void fastd_close_all_fds(void) { struct rlimit rl; int fd, maxfd; @@ -465,7 +465,7 @@ static void drop_caps(void) { fastd_cap_drop(); } -/* will double fork and forward potential exit codes from the child to the parent */ +/* will double fork and wait for a status notification from the child */ static int daemonize(void) { uint8_t status = 1; int pipefd[2]; @@ -562,7 +562,7 @@ int main(int argc, char *argv[]) { } #endif - close_fds(); + fastd_close_all_fds(); fastd_random_bytes(&ctx.randseed, sizeof(ctx.randseed), false); diff --git a/src/fastd.h b/src/fastd.h index 73c7229..2654bfd 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -306,6 +306,8 @@ void fastd_receive(fastd_socket_t *sock); void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer); +void fastd_close_all_fds(void); + bool fastd_socket_handle_binds(void); fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af); void fastd_socket_close(fastd_socket_t *sock); diff --git a/src/shell.c b/src/shell.c index 3b2644b..9d03fbc 100644 --- a/src/shell.c +++ b/src/shell.c @@ -120,6 +120,8 @@ static bool shell_command_do_exec(const fastd_shell_command_t *command, const fa /* child process */ + fastd_close_all_fds(); + if (chdir(command->dir)) _exit(126); -- cgit v1.2.3