From 3d2a62653d12a7488fee5ee3402f4b491f6d2e3a Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Tue, 29 Aug 2023 14:53:36 +0200 Subject: [PATCH] start-stop-daemon, supervise-daemon: use close_range with musl Make sure that we use close_range also with musl libc, using syscall directly. --- meson.build | 7 +++--- src/start-stop-daemon/start-stop-daemon.c | 26 +++++++++++++++++++++-- src/supervise-daemon/supervise-daemon.c | 23 ++++++++++++++++++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/meson.build b/meson.build index 3e94ce5d6..60e403f5f 100644 --- a/meson.build +++ b/meson.build @@ -198,9 +198,10 @@ endif if cc.has_function('closefrom', prefix: '#define _GNU_SOURCE\n#include ') add_project_arguments('-DHAVE_CLOSEFROM', language: 'c') endif -if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include ') and \ - cc.has_header_symbol('unistd.h', 'CLOSE_RANGE_CLOEXEC', prefix: '#define _GNU_SOURCE') - add_project_arguments('-DHAVE_CLOSE_RANGE_CLOEXEC', language: 'c') +if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include ') + add_project_arguments('-DHAVE_CLOSE_RANGE', language: 'c') +elif cc.has_header('linux/close_range.h') + add_project_arguments('-DHAVE_LINUX_CLOSE_RANGE_H', language: 'c') endif incdir = include_directories('src/shared') diff --git a/src/start-stop-daemon/start-stop-daemon.c b/src/start-stop-daemon/start-stop-daemon.c index ef6f454eb..6dae67c59 100644 --- a/src/start-stop-daemon/start-stop-daemon.c +++ b/src/start-stop-daemon/start-stop-daemon.c @@ -48,6 +48,9 @@ #ifdef __linux__ # include /* For io priority */ # include /* For prctl */ +# if !defined(HAVE_CLOSE_RANGE) && defined(HAVE_LINUX_CLOSE_RANGE_H) +# include +# endif #endif #include #include @@ -191,6 +194,22 @@ static inline int ioprio_set(int which RC_UNUSED, } #endif +#if !defined(SYS_close_range) && defined(__NR_close_range) +# define SYS_close_range __NR_close_range +#endif +#ifndef HAVE_CLOSE_RANGE +static inline int close_range(unsigned int first RC_UNUSED, + unsigned int last RC_UNUSED, + unsigned int flags RC_UNUSED) +{ +#if defined(SYS_close_range) + return syscall(SYS_close_range, first, last, flags); +#else + return -1; +#endif +} +#endif + static void cleanup(void) { @@ -1101,8 +1120,11 @@ int main(int argc, char **argv) #ifdef HAVE_CLOSEFROM closefrom(3); #else - for (i = getdtablesize() - 1; i >= 3; --i) - close(i); +# ifdef CLOSE_RANGE_UNSHARE + if (close_range(3, 0, CLOSE_RANGE_UNSHARE) < 0) +# endif + for (i = getdtablesize() - 1; i >= 3; --i) + close(i); #endif if (scheduler != NULL) { diff --git a/src/supervise-daemon/supervise-daemon.c b/src/supervise-daemon/supervise-daemon.c index 836a1ec77..b36937931 100644 --- a/src/supervise-daemon/supervise-daemon.c +++ b/src/supervise-daemon/supervise-daemon.c @@ -22,7 +22,7 @@ #define ONE_SECOND 1000000000 #define ONE_MS 1000000 -#ifdef HAVE_CLOSE_RANGE_CLOEXEC +#ifdef HAVE_CLOSE_RANGE /* For close_range() */ # define _GNU_SOURCE #endif @@ -43,6 +43,9 @@ #ifdef __linux__ # include /* For io priority */ # include /* For prctl */ +# if !defined(HAVE_CLOSE_FROM) && defined(HAVE_LINUX_CLOSE_RANGE_H) +# include +# endif #endif #include #include @@ -203,6 +206,22 @@ static inline int ioprio_set(int which RC_UNUSED, int who RC_UNUSED, } #endif +#if !defined(SYS_close_range) && defined(__NR_close_range) +# define SYS_close_range __NR_close_range +#endif +#ifndef HAVE_CLOSE_RANGE +static inline int close_range(unsigned int first RC_UNUSED, + unsigned int last RC_UNUSED, + unsigned int flags RC_UNUSED) +{ +#ifdef SYS_close_range + return syscall(SYS_close_range, first, last, flags); +#else + return -1; +#endif +} +#endif + static void cleanup(void) { free(changeuser); @@ -570,7 +589,7 @@ RC_NORETURN static void child_process(char *exec, char **argv) if (redirect_stderr || rc_yesno(getenv("EINFO_QUIET"))) dup2(stderr_fd, STDERR_FILENO); -#ifdef HAVE_CLOSE_RANGE_CLOEXEC +#ifdef CLOSE_RANGE_CLOEXEC if (close_range(3, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0) #endif for (i = getdtablesize() - 1; i >= 3; --i)