diff --git a/meson.build b/meson.build index 2cdd2b90e..d1a8bd5bc 100644 --- a/meson.build +++ b/meson.build @@ -195,12 +195,10 @@ if cc.compiles(malloc_attribute_test, name : 'malloc attribute with arguments') add_project_arguments('-DHAVE_MALLOC_EXTENDED_ATTRIBUTE', language: 'c') 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 if cc.has_function('strlcpy', prefix: '#define _GNU_SOURCE\n#include ') diff --git a/src/shared/misc.c b/src/shared/misc.c index c987ce8ce..28f4e2352 100644 --- a/src/shared/misc.c +++ b/src/shared/misc.c @@ -15,10 +15,18 @@ * except according to the terms contained in the LICENSE file. */ +#ifdef HAVE_CLOSE_RANGE +/* For close_range() */ +# define _GNU_SOURCE +#endif + #include #include #include #include +#ifdef HAVE_LINUX_CLOSE_RANGE_H +# include +#endif #include #include #include @@ -26,6 +34,7 @@ #include #include #ifdef __linux__ +# include /* for close_range */ # include #endif #include @@ -500,3 +509,30 @@ pid_t get_pid(const char *applet,const char *pidfile) return pid; } + +#ifndef HAVE_CLOSE_RANGE +static inline int close_range(int first RC_UNUSED, + int last RC_UNUSED, + unsigned int flags RC_UNUSED) +{ +#ifdef SYS_close_range + return syscall(SYS_close_range, first, last, flags); +#else + errno = ENOSYS; + return -1; +#endif +} +#endif +#ifndef CLOSE_RANGE_CLOEXEC +# define CLOSE_RANGE_CLOEXEC (1U << 2) +#endif + +void +cloexec_fds_from(int first) +{ + int i; + if (close_range(first, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0) { + for (i = getdtablesize() - 1; i >= first; --i) + fcntl(i, F_SETFD, FD_CLOEXEC); + } +} diff --git a/src/shared/misc.h b/src/shared/misc.h index f4ab25adb..b158a7860 100644 --- a/src/shared/misc.h +++ b/src/shared/misc.h @@ -73,4 +73,6 @@ void from_time_t(char *time_string, time_t tv); time_t to_time_t(char *timestring); pid_t get_pid(const char *applet, const char *pidfile); +void cloexec_fds_from(int); + #endif diff --git a/src/start-stop-daemon/start-stop-daemon.c b/src/start-stop-daemon/start-stop-daemon.c index ef6f454eb..3e4a19a19 100644 --- a/src/start-stop-daemon/start-stop-daemon.c +++ b/src/start-stop-daemon/start-stop-daemon.c @@ -1098,12 +1098,7 @@ int main(int argc, char **argv) || rc_yesno(getenv("EINFO_QUIET"))) dup2(stderr_fd, STDERR_FILENO); -#ifdef HAVE_CLOSEFROM - closefrom(3); -#else - for (i = getdtablesize() - 1; i >= 3; --i) - close(i); -#endif + cloexec_fds_from(3); if (scheduler != NULL) { int scheduler_index; diff --git a/src/supervise-daemon/supervise-daemon.c b/src/supervise-daemon/supervise-daemon.c index 836a1ec77..f0f298d2e 100644 --- a/src/supervise-daemon/supervise-daemon.c +++ b/src/supervise-daemon/supervise-daemon.c @@ -22,11 +22,6 @@ #define ONE_SECOND 1000000000 #define ONE_MS 1000000 -#ifdef HAVE_CLOSE_RANGE_CLOEXEC -/* For close_range() */ -# define _GNU_SOURCE -#endif - #include #include #include @@ -570,11 +565,8 @@ 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 - if (close_range(3, UINT_MAX, CLOSE_RANGE_CLOEXEC) < 0) -#endif - for (i = getdtablesize() - 1; i >= 3; --i) - fcntl(i, F_SETFD, FD_CLOEXEC); + cloexec_fds_from(3); + cmdline = make_cmdline(argv); syslog(LOG_INFO, "Child command line: %s", cmdline); free(cmdline);