mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-01-09 19:27:42 +01:00
closefrom: Use close_range() on Linux when available
Closes: !11 Based-on-patch-by: cptpcrd <cptpcrd.git@gmail.com> Signed-off-by: Guillem Jover <guillem@hadrons.org>
This commit is contained in:
parent
c4fca5bb4f
commit
e832b7687e
@ -19,6 +19,12 @@
|
|||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
# include <sys/syscall.h>
|
||||||
|
# if defined(__NR_close_range) && !defined(SYS_close_range)
|
||||||
|
# define SYS_close_range __NR_close_range
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -61,6 +67,14 @@ closefrom_close(int fd)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__linux__) && defined(SYS_close_range)
|
||||||
|
static inline int
|
||||||
|
sys_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags)
|
||||||
|
{
|
||||||
|
return syscall(SYS_close_range, fd, max_fd, flags);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Close all file descriptors greater than or equal to lowfd.
|
* Close all file descriptors greater than or equal to lowfd.
|
||||||
* This is the expensive (fallback) method.
|
* This is the expensive (fallback) method.
|
||||||
@ -182,11 +196,16 @@ closefrom(int lowfd)
|
|||||||
if (lowfd < 0)
|
if (lowfd < 0)
|
||||||
lowfd = 0;
|
lowfd = 0;
|
||||||
|
|
||||||
/* Try the fast method first, if possible. */
|
/* Try the fast methods first, if possible. */
|
||||||
#if defined(HAVE_FCNTL_CLOSEM)
|
#if defined(HAVE_FCNTL_CLOSEM)
|
||||||
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
|
if (fcntl(lowfd, F_CLOSEM, 0) != -1)
|
||||||
return;
|
return;
|
||||||
#endif /* HAVE_FCNTL_CLOSEM */
|
#endif /* HAVE_FCNTL_CLOSEM */
|
||||||
|
#if defined(__linux__) && defined(SYS_close_range)
|
||||||
|
if (sys_close_range(lowfd, UINT_MAX, 0) == 0)
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(HAVE_PSTAT_GETPROC)
|
#if defined(HAVE_PSTAT_GETPROC)
|
||||||
if (closefrom_pstat(lowfd) != -1)
|
if (closefrom_pstat(lowfd) != -1)
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user