From 61dc9affb72f12a309e5ebb851da5724ac4c561a Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Mon, 8 May 2023 01:01:26 -0400 Subject: [core] posix_spawn_file_actions_addclosefrom_np() use posix_spawn_file_actions_addclosefrom_np() where available --- src/CMakeLists.txt | 1 + src/config.h.cmake | 1 + src/fdevent.c | 12 ++++++++++++ src/meson.build | 1 + 4 files changed, 15 insertions(+) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea7ba2f9..92642d08 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -212,6 +212,7 @@ endif() check_include_files(spawn.h HAVE_SPAWN_H) if(HAVE_SPAWN_H) check_function_exists(posix_spawn HAVE_POSIX_SPAWN) +check_function_exists(posix_spawn_file_actions_addclosefrom_np HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP) check_function_exists(posix_spawn_file_actions_addfchdir_np HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDFCHDIR_NP) endif() diff --git a/src/config.h.cmake b/src/config.h.cmake index 99dfd954..1482fdc0 100644 --- a/src/config.h.cmake +++ b/src/config.h.cmake @@ -179,6 +179,7 @@ #cmakedefine HAVE_PIPE2 #cmakedefine HAVE_POLL #cmakedefine HAVE_POSIX_SPAWN +#cmakedefine HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP #cmakedefine HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDFCHDIR_NP #cmakedefine HAVE_PORT_CREATE #cmakedefine HAVE_PREAD diff --git a/src/fdevent.c b/src/fdevent.c index 1c704fd9..115dca5a 100644 --- a/src/fdevent.c +++ b/src/fdevent.c @@ -512,6 +512,18 @@ pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin && 0 == (rc = sigaddset(&sigs, SIGUSR1)) && 0 == (rc = posix_spawnattr_setsigdefault(&attr, &sigs))) { + /* optional: potentially improve performance when many fds open + * (might create new file descriptor table containing only 0,1,2 + * instead of close() on all other fds with O_CLOEXEC flag set) + * optional: disable manually and externally via gdb or other + * debugger by setting trace_children to non-zero value */ + static volatile sig_atomic_t trace_children; + if (!trace_children) { + #ifdef HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP + posix_spawn_file_actions_addclosefrom_np(&file_actions, 3); + #endif + } + #if !defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDFCHDIR_NP) \ && !defined(HAVE_POSIX_SPAWNATTR_SETCWD_NP) /* not thread-safe, but ok since lighttpd not (currently) threaded diff --git a/src/meson.build b/src/meson.build index 2d7e7169..ed7a3953 100644 --- a/src/meson.build +++ b/src/meson.build @@ -137,6 +137,7 @@ functions = { 'pipe2': 'unistd.h', 'poll': 'poll.h', 'posix_spawn': 'spawn.h', + 'posix_spawn_file_actions_addclosefrom_np': 'spawn.h', 'posix_spawn_file_actions_addfchdir_np': 'spawn.h', 'pread': 'unistd.h', 'preadv': 'sys/uio.h', -- cgit v1.2.1