Skip to content

Commit

Permalink
misc: add syscall fallback for close_range for musl libc
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ncopa committed Sep 1, 2023
1 parent 1c22d40 commit a438f14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ endif

if cc.has_function('close_range', prefix: '#define _GNU_SOURCE\n#include <unistd.h>')
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 <string.h>')
Expand Down
15 changes: 15 additions & 0 deletions src/shared/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#ifdef HAVE_LINUX_CLOSE_RANGE_H
# include <linux/close_range.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/time.h>
#ifdef __linux__
# include <sys/syscall.h> /* for close_range */
# include <sys/sysinfo.h>
#endif
#include <sys/types.h>
Expand Down Expand Up @@ -511,7 +515,18 @@ static inline int close_range(int first RC_UNUSED,
int last RC_UNUSED,
unsigned int flags RC_UNUSED)
{
#ifdef SYS_close_range
int r = syscall(SYS_close_range, first, last, flags);
if (r < 0) {
errno = -r;
r = -1;
}
return r;
#else
errno = ENOSYS;
return -1;
#endif
#endif
}
#endif
#ifndef CLOSE_RANGE_CLOEXEC
Expand Down

0 comments on commit a438f14

Please sign in to comment.