File tree Expand file tree Collapse file tree 1 file changed +15
-14
lines changed
Expand file tree Collapse file tree 1 file changed +15
-14
lines changed Original file line number Diff line number Diff line change 1111
1212#include <unistd.h>
1313#include <errno.h>
14+ #include <sys/syscall.h>
1415#include <sys/wait.h>
1516
1617#ifdef HAVE_FCNTL_H
2122#include <signal.h>
2223#endif
2324
24- #if defined(HAVE_LINUX_CLOSE_RANGE_H )
25- #define _GNU_SOURCE
26- #include <linux/close_range.h>
27- #endif
28-
2925void
3026closefrom_excluding (int lowfd , int excludingFd ) {
31- #if defined(HAVE_CLOSE_RANGE )
32- close_range (lowfd , excludingFd - 1 );
33- closefrom (excludingFd + 1 );
34- #else
35- for (int i = lowfd ; i < excludingFd ; i ++ ) {
36- close (i );
37- }
27+ // Try using the close_range syscall, provided in Linux kernel >= 5.9.
28+ // We do this directly because not all C libs provide a wrapper (like musl)
29+ long ret = syscall (SYS_close_range , lowfd , excludingFd - 1 );
3830
39- closefrom (excludingFd + 1 );
40- #endif
31+ if (ret != -1 ) {
32+ // If that worked, closefrom the remaining range
33+ closefrom (excludingFd + 1 );
34+ } else {
35+ // Otherwise, fall back to a loop + closefrom
36+ for (int i = lowfd ; i < excludingFd ; i ++ ) {
37+ close (i );
38+ }
39+
40+ closefrom (excludingFd + 1 );
41+ }
4142}
4243
4344// If a process was terminated by a signal, the exit status we return
You can’t perform that action at this time.
0 commit comments