misc: add syscall fallback for close_range for musl libc

Add fallback for the close_range syscall wrapper. This is needed for
musl libc, which currently does not have a close_range wrapper.

Also set errno on errors.
This commit is contained in:
Natanael Copa 2023-08-30 13:34:23 +02:00 committed by William Hubbs
parent e447562aaa
commit c4785f1b99
2 changed files with 11 additions and 0 deletions

View File

@ -197,6 +197,8 @@ endif
if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include <unistd.h>') if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include <unistd.h>')
add_project_arguments('-DHAVE_CLOSE_RANGE', language: 'c') 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 endif
if cc.has_function('strlcpy', prefix: '#define _GNU_SOURCE\n#include <string.h>') if cc.has_function('strlcpy', prefix: '#define _GNU_SOURCE\n#include <string.h>')

View File

@ -24,6 +24,9 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <limits.h> #include <limits.h>
#ifdef HAVE_LINUX_CLOSE_RANGE_H
# include <linux/close_range.h>
#endif
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -31,6 +34,7 @@
#include <sys/file.h> #include <sys/file.h>
#include <sys/time.h> #include <sys/time.h>
#ifdef __linux__ #ifdef __linux__
# include <sys/syscall.h> /* for close_range */
# include <sys/sysinfo.h> # include <sys/sysinfo.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
@ -511,7 +515,12 @@ static inline int close_range(int first RC_UNUSED,
int last RC_UNUSED, int last RC_UNUSED,
unsigned int flags RC_UNUSED) unsigned int flags RC_UNUSED)
{ {
#ifdef SYS_close_range
return syscall(SYS_close_range, first, last, flags);
#else
errno = ENOSYS;
return -1; return -1;
#endif
} }
#endif #endif
#ifndef CLOSE_RANGE_CLOEXEC #ifndef CLOSE_RANGE_CLOEXEC