diff --git a/mx_util.h b/mx_util.h index 349e5717..173e7192 100644 --- a/mx_util.h +++ b/mx_util.h @@ -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 +#define close_range(first, last, flags) syscall(SYS_close_range, first, last, flags) +#endif + #endif diff --git a/test_mx_util.c b/test_mx_util.c index 9ecf5343..236918fd 100644 --- a/test_mx_util.c +++ b/test_mx_util.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "mx_util.h" #include "mx_proc.h" @@ -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(); @@ -611,5 +630,6 @@ int main(void) test_mx_df(); test_mx_pipe_external(); test_mx_call_external(); + test_mx_closerange(); return 0; }