sysdeps: On Linux, wrap close_range syscall directly if necessary

This was added to the Linux kernel in version 5.9, but the wrapper
wasn't added to glibc until 2.34. Adding our own wrapper for the
system call means we can use close_range() on Debian 11 and
contemporary distributions.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2022-03-02 12:25:54 +00:00
parent d307616177
commit ccea70515e
3 changed files with 20 additions and 0 deletions

View file

@ -32,6 +32,7 @@ check_include_file(sys/inotify.h HAVE_SYS_INOTIFY_H)
check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/syscall.h HAVE_SYS_SYSCALL_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(sys/uio.h HAVE_SYS_UIO_H)
check_include_file(sys/prctl.h HAVE_SYS_PRCTL_H)

View file

@ -427,6 +427,7 @@ stdint.h
sys/prctl.h
sys/random.h
sys/resource.h
sys/syscall.h
sys/syslimits.h
sys/time.h
unistd.h

View file

@ -84,6 +84,9 @@
#ifdef HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
#ifdef HAVE_ADT
#include <bsm/adt.h>
@ -141,6 +144,21 @@
#endif /* Solaris */
#if defined(__linux__) && defined(__NR_close_range) && !defined(HAVE_CLOSE_RANGE)
/* The kernel headers are new enough to have the close_range syscall,
* but glibc isn't new enough to have the syscall wrapper, so call the
* syscall directly. */
static inline int
close_range (unsigned int first,
unsigned int last,
unsigned int flags)
{
return syscall (__NR_close_range, first, last, flags);
}
/* Now we can call that inline wrapper as though it was provided by glibc. */
#define HAVE_CLOSE_RANGE
#endif
/**
* Ensure that the standard file descriptors stdin, stdout and stderr
* are open, by opening /dev/null if necessary.