Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
mx_util: Add close_range syscall wrapper for glibc < 2.34
  • Loading branch information
donald committed May 9, 2022
1 parent 79ce67c commit 16aee83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mx_util.h
Expand Up @@ -183,4 +183,9 @@ time_t mx_clock_boottime(void);
int mx_call_external(char *helper, char **argv);
char *mx_pipe_external(char *args, char **argv);

#if __GLIBC__ <2 || __GLIBC__ == 2 && __GLIBC_MINOR__ < 34
#include <sys/syscall.h>
#define close_range(first, last, flags) syscall(SYS_close_range, first, last, flags)
#endif

#endif
20 changes: 20 additions & 0 deletions test_mx_util.c
Expand Up @@ -5,6 +5,8 @@
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "mx_util.h"
#include "mx_proc.h"
Expand Down Expand Up @@ -589,6 +591,23 @@ static void test_mx_call_external(void) {
assert(errno == EPROTO);
}

static void test_mx_closerange(void) {
int res;

int fd1 = open("/dev/null", O_RDONLY);
assert (fd1 != -1);
int fd2 = dup2(fd1, 100);
assert (fd2 == 100 );

res=close_range(3, ~0u, 0);
assert(res == 0);

res = close(fd1);
assert (res == -1 && errno == EBADF);
res = close(fd2);
assert (res == -1 && errno == EBADF);
}

int main(void)
{
test_mx_strskipwhitespaces();
Expand All @@ -611,5 +630,6 @@ int main(void)
test_mx_df();
test_mx_pipe_external();
test_mx_call_external();
test_mx_closerange();
return 0;
}

0 comments on commit 16aee83

Please sign in to comment.