]> sourceware.org Git - glibc.git/commitdiff
linux: Add close_range
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 10 Mar 2021 15:26:31 +0000 (12:26 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 8 Jul 2021 17:08:13 +0000 (14:08 -0300)
It was added on Linux 5.9 (278a5fbaed89) with CLOSE_RANGE_CLOEXEC
added on 5.11 (582f1fb6b721f).  Although FreeBSD has added the same
syscall, this only adds the symbol on Linux ports.  This syscall is
required to provided a fail-safe way to implement the closefrom
symbol (BZ #10353).

Checked on x86_64-linux-gnu and i686-linux-gnu on kernel 5.11 and 4.15.

40 files changed:
NEWS
include/bits/unistd_ext.h [new file with mode: 0644]
manual/llio.texi
sysdeps/unix/sysv/linux/Makefile
sysdeps/unix/sysv/linux/Versions
sysdeps/unix/sysv/linux/aarch64/libc.abilist
sysdeps/unix/sysv/linux/alpha/libc.abilist
sysdeps/unix/sysv/linux/arc/libc.abilist
sysdeps/unix/sysv/linux/arm/be/libc.abilist
sysdeps/unix/sysv/linux/arm/le/libc.abilist
sysdeps/unix/sysv/linux/bits/unistd_ext.h
sysdeps/unix/sysv/linux/csky/libc.abilist
sysdeps/unix/sysv/linux/hppa/libc.abilist
sysdeps/unix/sysv/linux/i386/libc.abilist
sysdeps/unix/sysv/linux/ia64/libc.abilist
sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
sysdeps/unix/sysv/linux/microblaze/be/libc.abilist
sysdeps/unix/sysv/linux/microblaze/le/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
sysdeps/unix/sysv/linux/nios2/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist
sysdeps/unix/sysv/linux/riscv/rv32/libc.abilist
sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
sysdeps/unix/sysv/linux/sh/be/libc.abilist
sysdeps/unix/sysv/linux/sh/le/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
sysdeps/unix/sysv/linux/syscalls.list
sysdeps/unix/sysv/linux/tst-close_range.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist

diff --git a/NEWS b/NEWS
index 8e72946c3f225e756fb5ab4cf69c33cff4509a63..be04b217fed22910c4048bdf28f10dd36d6d8644 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -60,6 +60,9 @@ Major new features:
   to call async-signal-safe functions (such as raise or execve).  This function
   is currently a GNU extension.
 
+* On Linux, the close_range function has been added.  It allows efficiently
+  closing a range of file descriptors on recent kernels (version 5.9).
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * The function pthread_mutex_consistent_np has been deprecated; programs
diff --git a/include/bits/unistd_ext.h b/include/bits/unistd_ext.h
new file mode 100644 (file)
index 0000000..277be05
--- /dev/null
@@ -0,0 +1,6 @@
+#include_next <bits/unistd_ext.h>
+
+#ifndef _ISOMAC
+extern int __close_range (unsigned int lowfd, unsigned int highfd, int flags);
+libc_hidden_proto (__close_range);
+#endif
index eafc27120df19cd8ce6afedebb5c40b91320188f..e21a71fdd035757a9bba9712d545b9a7d4e753e9 100644 (file)
@@ -284,6 +284,57 @@ of trying to close its underlying file descriptor with @code{close}.
 This flushes any buffered output and updates the stream object to
 indicate that it is closed.
 
+@deftypefun int close_range (unsigned int @var{lowfd}, unsigned int @var{maxfd}, int @var{flags})
+@standards{Linux, unistd.h}
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
+@c This is a syscall for Linux v5.9.  There is no fallback emulation for
+@c older kernels.
+
+The function @code{close_range} closes the file descriptor from @var{lowfd}
+to @var{maxfd} (inclusive).  This function is similar to call @code{close} in
+specified file descriptor range depending on the @var{flags}.
+
+This is function is only supported on recent Linux versions and @theglibc{}
+does not provide any fallback (the application will need to handle possible
+@code{ENOSYS}).
+
+The @var{flags} add options on how the files are closes.  Linux currently
+supports:
+
+@vtable @code
+@item CLOSE_RANGE_UNSHARE
+Unshare the file descriptor table before closing file descriptors.
+
+@item CLOSE_RANGE_CLOEXEC
+Set the @code{FD_CLOEXEC} bit instead of closing the file descriptor.
+@end vtable
+
+The normal return value from @code{close_range} is @math{0}; a value
+of @math{-1} is returned in case of failure.  The following @code{errno} error
+conditions are defined for this function:
+
+@table @code
+@item EINVAL
+The @var{lowfd} value is larger than @var{maxfd} or an unsupported @var{flags}
+is used.
+
+@item ENOMEM
+Either there is not enough memory for the operation, or the process is
+out of address space.  It can only happnes when @code{CLOSE_RANGE_UNSHARED}
+flag is used.
+
+@item EMFILE
+The process has too many files open and it can only happens when
+@code{CLOSE_RANGE_UNSHARED} flag is used.
+The maximum number of file descriptors is controlled by the
+@code{RLIMIT_NOFILE} resource limit; @pxref{Limits on Resources}.
+
+@item ENOSYS
+The kernel does not implement the required functionality.
+@end table
+@end deftypefun
+
+
 @node I/O Primitives
 @section Input and Output Primitives
 
index faea02bd1be940b6766434b837ac6f99513e499e..e30871116875689dfaf6ee9fcddf0258b41cb857 100644 (file)
@@ -116,7 +116,8 @@ tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
         tst-tgkill tst-sysvsem-linux tst-sysvmsg-linux tst-sysvshm-linux \
         tst-timerfd tst-ppoll \
         tst-clock_adjtime tst-adjtimex tst-ntp_adjtime tst-ntp_gettime \
-        tst-ntp_gettimex tst-sigtimedwait tst-misalign-clone
+        tst-ntp_gettimex tst-sigtimedwait tst-misalign-clone \
+        tst-close_range
 
 # Test for the symbol version of fcntl that was replaced in glibc 2.28.
 ifeq ($(have-GLIBC_2.27)$(build-shared),yesyes)
index 53cb7b23e2a15d70a73db31a3da3bb7127366240..48842168251baaa7b9b1939624ac21ecc7dce2cf 100644 (file)
@@ -283,6 +283,7 @@ libc {
     __wait3_time64;
     __wait4_time64;
 %endif
+    close_range;
   }
   GLIBC_PRIVATE {
     # functions used in other libraries
index 6f19b07b47a5d4f6d22d8d1435b7f44c9c641143..428b23fc6577ecd72387f868d00222f01e51b4b0 100644 (file)
@@ -2407,6 +2407,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 6ece449d4b90186ed0bd57392c802d30af584a19..d5c24d09eee2667304ea1d3ea5b692927f32d2dc 100644 (file)
@@ -2506,6 +2506,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 3bf846349fb9c5d6d943899051c6d0f6f5f6358a..6213e42bba92ebfed278031c706adc383ccc33b6 100644 (file)
@@ -2166,6 +2166,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 08be998d35d8c685ae97c7b05ee4f53fcdeba8b1..37c395e4cb5db80cb52db67665e1e9dcdba3eff9 100644 (file)
@@ -300,6 +300,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index ebd767ed96d26e4ce410cc7ef2cde78425257488..dd850ce03557cfaaa76ccc610d0f9f0468191d66 100644 (file)
@@ -297,6 +297,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 2e529be5773e613b72fb0031d249921b602e2031..ae9994403cbed74018987c0357090833a3389c85 100644 (file)
    not detached and has not been joined.  */
 extern __pid_t gettid (void) __THROW;
 
+#ifdef __has_include
+# if __has_include ("linux/close_range.h")
+#  include "linux/close_range.h"
+# endif
 #endif
+/* Unshare the file descriptor table before closing file descriptors.  */
+#ifndef CLOSE_RANGE_UNSHARE
+# define CLOSE_RANGE_UNSHARE (1U << 1)
+#endif
+/* Set the FD_CLOEXEC bit instead of closing the file descriptor.  */
+#ifndef CLOSE_RANGE_CLOEXEC
+# define CLOSE_RANGE_CLOEXEC (1U << 2)
+#endif
+
+/* Close all file descriptors in the range FD up to MAX_FD.  The flag FLAGS
+   are define by the CLOSE_RANGE prefix.  This function behaves like close
+   on the range, but in a fail-safe where it will either fail and not close
+   any file descriptor or close all of them.  Gaps where the file descriptor
+   is invalid are ignored.   Returns 0 on successor or -1 for failure (and
+   sets errno accordingly).  */
+extern int close_range (unsigned int __fd, unsigned int __max_fd,
+                       int __flags) __THROW;
+
+#endif /* __USE_GNU  */
index 4a467e706e34c71354ac4de111168bb0fcf00710..48dc96e2d58a0024bfdf82de5c2961d4775467ae 100644 (file)
@@ -2432,6 +2432,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index bdef8d2a344d4fe78dc8c316351aa7543450ab72..13772d42d36eb3a7d918bb59d5afc0f5421a1978 100644 (file)
@@ -2385,6 +2385,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index eba95a2cf0e4a9d2f2c6648c196171a90a3dfdea..6613f4c5f056f60a02d295e2b7306ed306f27536 100644 (file)
@@ -2569,6 +2569,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 3fbc5ae978ce0815d04c15bb19d9492ff9d0b21e..06d096d9451855aed2d1d2a0cfe2854cb5088ba2 100644 (file)
@@ -2344,6 +2344,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 0aef2ec43ae8105ab28fb7c9092ed19f9c5d683c..dc2466f3cd182048fdf839e5d7a95de9a2c2615d 100644 (file)
@@ -301,6 +301,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index a4704f7e96219e0444fa3206a40fd3a7f00a9654..d425dbad0bc03a0386f6a132f1c1b83e55d34297 100644 (file)
@@ -2512,6 +2512,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 8f135f30c08134e676f836d593de7273532eea86..f1fc79525ce94d761440f7eafb4e1260c7743837 100644 (file)
@@ -2483,6 +2483,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 719609ac100fa6f48ae225adbe36cb569396457e..7cf1c864b7ab99499153e5d80cace2cbae465edd 100644 (file)
@@ -2480,6 +2480,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index f9a6bccdc50d2c3fcc395b84b2bbe5e5123755e5..41b7927b86198f5f3a2549dfcc6dca389890fa24 100644 (file)
@@ -2477,6 +2477,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index cee02495c67d9a40e796cbbd777c452ae9852ccc..91f06d23ca412f826d1c7e717991b17d6b7b5436 100644 (file)
@@ -2475,6 +2475,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index c9fdfe3a55ff0828dfbd4743934b9fedffc66744..a4d3e069411dcf29d36c8e988bb050ead5b94a28 100644 (file)
@@ -2483,6 +2483,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index d27c97094e797aa3f4b03cc401b2fa25e23b9be0..3e96b69ba4c0c2f1460c91873baf0b3601dd0934 100644 (file)
@@ -2395,6 +2395,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 25f270403f7fb01e7d5e854df57cab418fd1a6bb..3618eaa4e0bed07833478348bed32b8c6058afd2 100644 (file)
@@ -2522,6 +2522,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 0256a272ddd22043a36b89044f620154af341190..3951054b616e2522708b33ba6f8e0fbb44693144 100644 (file)
@@ -2539,6 +2539,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 946f855c642a68b7b5e47fdf11ebe12fe6098a56..4c2adbd8ab07c77292524da59f9ed2410815c9e0 100644 (file)
@@ -2572,6 +2572,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 602da17327143975ea1856d35b4360567faf0099..e9d30b1ed79af22e506584f897f24f60cdb18869 100644 (file)
@@ -2308,6 +2308,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 287285a21adf5f97f6729d7697488c4f378ce388..81c1eac79cc6a8229dc87ff985f0b79b5ee560f7 100644 (file)
@@ -2603,6 +2603,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index cdeee28d0f6bf1c6e1e92cc0e9c8f5b3818587d5..6fa30b247b5691d80339e8d302f4fdab307bf0f4 100644 (file)
@@ -2168,6 +2168,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index a74b54d6bbe459578c302d362611a87d54eac44f..7c20b71f7ada175c92e66a36de0805be8109c382 100644 (file)
@@ -2368,6 +2368,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index e96371317c63b947a129ebab1e23c2cc8177db27..fe0c9a2d06a9c1723a35aaf6a878ccd52f641c75 100644 (file)
@@ -2537,6 +2537,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index aadc7c1d66e1c3ad42be7ef0476482cff7744b58..da52d75a7d1dd4a0e850a2f85fd1e114998c03a8 100644 (file)
@@ -2345,6 +2345,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index cc4a625cb4e26a14a97f7b278e5479da59c282d8..6f022d19e8bf7b830534f50e6532f5eee6652f32 100644 (file)
@@ -2392,6 +2392,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 163d2cd6133033ddcdc9cf9d939c17102e5b4cb3..a4934d88e9286216927ddc6e788f8ece1ce54c78 100644 (file)
@@ -2389,6 +2389,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 6eceee6b88a526e4339ecb2073e7d0ed1442a528..684e9d7b06dea1c0e88ad4b3d38b3174b9f01bf0 100644 (file)
@@ -2532,6 +2532,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index e6bac469babecf5b64787afd28fa99878e2e1f65..af10195edba26d2bb96a054a576a082b767338de 100644 (file)
@@ -2367,6 +2367,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 62329abb49de6adc281bf689e78fb263355a9454..29899eb2646d5d6c04290e333c4e16e0a0c7e23d 100644 (file)
@@ -99,3 +99,4 @@ pkey_alloc    EXTRA   pkey_alloc      i:ii    pkey_alloc
 pkey_free      EXTRA   pkey_free       i:i     pkey_free
 gettid          EXTRA   gettid          Ei:     __gettid       gettid
 tgkill          EXTRA   tgkill          i:iii   __tgkill       tgkill
+close_range     EXTRA   close_range     i:iii   __close_range   close_range
diff --git a/sysdeps/unix/sysv/linux/tst-close_range.c b/sysdeps/unix/sysv/linux/tst-close_range.c
new file mode 100644 (file)
index 0000000..dccb618
--- /dev/null
@@ -0,0 +1,303 @@
+/* Test for the close_range system call.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <getopt.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include <array_length.h>
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+#include <support/descriptors.h>
+#include <support/support.h>
+#include <support/xsched.h>
+#include <support/xunistd.h>
+
+#define NFDS 100
+
+static int
+open_multiple_temp_files (void)
+{
+  /* Check if the temporary file descriptor has no no gaps.  */
+  int lowfd = xopen ("/dev/null", O_RDONLY, 0600);
+  for (int i = 1; i <= NFDS; i++)
+    TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600),
+                 lowfd + i);
+  return lowfd;
+}
+
+static void
+close_range_test_max_upper_limit (void)
+{
+  struct support_descriptors *descrs = support_descriptors_list ();
+
+  int lowfd = open_multiple_temp_files ();
+
+  {
+    int r = close_range (lowfd, ~0U, 0);
+    if (r == -1 && errno == ENOSYS)
+      FAIL_UNSUPPORTED ("close_range not supported");
+    TEST_COMPARE (r, 0);
+  }
+
+  support_descriptors_check (descrs);
+  support_descriptors_free (descrs);
+}
+
+static void
+close_range_test_common (int lowfd, unsigned int flags)
+{
+  const int maximum_fd = lowfd + NFDS;
+  const int half_fd = lowfd + NFDS / 2;
+  const int gap_1 = maximum_fd - 8;
+
+  /* Close half of the descriptors and check result.  */
+  TEST_COMPARE (close_range (lowfd, half_fd, flags), 0);
+  for (int i = lowfd; i <= half_fd; i++)
+    {
+      TEST_COMPARE (fcntl (i, F_GETFL), -1);
+      TEST_COMPARE (errno, EBADF);
+    }
+  for (int i = half_fd + 1; i < maximum_fd; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) > -1);
+
+  /* Create some gaps, close up to a threshold, and check result.  */
+  xclose (lowfd + 57);
+  xclose (lowfd + 78);
+  xclose (lowfd + 81);
+  xclose (lowfd + 82);
+  xclose (lowfd + 84);
+  xclose (lowfd + 90);
+
+  TEST_COMPARE (close_range (half_fd + 1, gap_1, flags), 0);
+  for (int i = half_fd + 1; i < gap_1; i++)
+    {
+      TEST_COMPARE (fcntl (i, F_GETFL), -1);
+      TEST_COMPARE (errno, EBADF);
+    }
+  for (int i = gap_1 + 1; i < maximum_fd; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) > -1);
+
+  /* Close the remaining but the last one.  */
+  TEST_COMPARE (close_range (gap_1 + 1, maximum_fd - 1, flags), 0);
+  for (int i = gap_1 + 1; i < maximum_fd - 1; i++)
+    {
+      TEST_COMPARE (fcntl (i, F_GETFL), -1);
+      TEST_COMPARE (errno, EBADF);
+    }
+  TEST_VERIFY (fcntl (maximum_fd, F_GETFL) > -1);
+
+  /* Close the last one.  */
+  TEST_COMPARE (close_range (maximum_fd, maximum_fd, flags), 0);
+  TEST_COMPARE (fcntl (maximum_fd, F_GETFL), -1);
+  TEST_COMPARE (errno, EBADF);
+}
+
+/* Basic tests: check if the syscall close ranges with and without gaps.  */
+static void
+close_range_test (void)
+{
+  struct support_descriptors *descrs = support_descriptors_list ();
+
+  /* Check if the temporary file descriptor has no no gaps.  */
+  int lowfd = open_multiple_temp_files ();
+
+  close_range_test_common (lowfd, 0);
+
+  /* Double check by check the /proc.  */
+  support_descriptors_check (descrs);
+  support_descriptors_free (descrs);
+}
+
+_Noreturn static int
+close_range_test_fn (void *arg)
+{
+  int lowfd = (int) ((uintptr_t) arg);
+  close_range_test_common (lowfd, 0);
+  exit (EXIT_SUCCESS);
+}
+
+/* Check if a clone_range on a subprocess created with CLONE_FILES close
+   the shared file descriptor table entries in the parent.  */
+static void
+close_range_test_subprocess (void)
+{
+  struct support_descriptors *descrs = support_descriptors_list ();
+
+  /* Check if the temporary file descriptor has no no gaps.  */
+  int lowfd = open_multiple_temp_files ();
+
+  struct support_stack stack = support_stack_alloc (4096);
+
+  pid_t pid = xclone (close_range_test_fn, (void*) (uintptr_t) lowfd,
+                     stack.stack, stack.size, CLONE_FILES | SIGCHLD);
+  TEST_VERIFY_EXIT (pid > 0);
+  int status;
+  xwaitpid (pid, &status, 0);
+  TEST_VERIFY (WIFEXITED (status));
+  TEST_COMPARE (WEXITSTATUS (status), 0);
+
+  support_stack_free (&stack);
+
+  for (int i = lowfd; i < NFDS; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) < 0);
+
+  support_descriptors_check (descrs);
+  support_descriptors_free (descrs);
+}
+
+
+_Noreturn static int
+close_range_unshare_test_fn (void *arg)
+{
+  int lowfd = (int) ((uintptr_t) arg);
+  close_range_test_common (lowfd, CLOSE_RANGE_UNSHARE);
+  exit (EXIT_SUCCESS);
+}
+
+/* Check if a close_range with CLOSE_RANGE_UNSHARE issued from a subprocess
+   created with CLONE_FILES does not close the parent file descriptor list.  */
+static void
+close_range_unshare_test (void)
+{
+  struct support_descriptors *descrs1 = support_descriptors_list ();
+
+  /* Check if the temporary file descriptor has no no gaps.  */
+  int lowfd = open_multiple_temp_files ();
+
+  struct support_descriptors *descrs2 = support_descriptors_list ();
+
+  struct support_stack stack = support_stack_alloc (4096);
+
+  pid_t pid = xclone (close_range_unshare_test_fn, (void*) (uintptr_t) lowfd,
+                     stack.stack, stack.size, CLONE_FILES | SIGCHLD);
+  TEST_VERIFY_EXIT (pid > 0);
+  int status;
+  xwaitpid (pid, &status, 0);
+  TEST_VERIFY (WIFEXITED (status));
+  TEST_COMPARE (WEXITSTATUS (status), 0);
+
+  support_stack_free (&stack);
+
+  for (int i = 0; i < NFDS; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) > -1);
+
+  support_descriptors_check (descrs2);
+  support_descriptors_free (descrs2);
+
+  TEST_COMPARE (close_range (lowfd, lowfd + NFDS, 0), 0);
+
+  support_descriptors_check (descrs1);
+  support_descriptors_free (descrs1);
+}
+
+static bool
+is_in_array (int *arr, size_t len, int fd)
+{
+  bool r = false;
+  for (int i = 0; i < len; i++)
+    if (arr[i] == fd)
+      return true;
+  return r;
+}
+
+static void
+close_range_cloexec_test (void)
+{
+  /* Check if the temporary file descriptor has no no gaps.  */
+  const int lowfd = open_multiple_temp_files ();
+
+  const int maximum_fd = lowfd + NFDS;
+  const int half_fd = lowfd + NFDS / 2;
+  const int gap_1 = maximum_fd - 8;
+
+  /* Close half of the descriptors and check result.  */
+  int r = close_range (lowfd, half_fd, CLOSE_RANGE_CLOEXEC);
+  if (r == -1 && errno == EINVAL)
+    {
+      printf ("%s: CLOSE_RANGE_CLOEXEC not supported\n", __func__);
+      return;
+    }
+  for (int i = lowfd; i <= half_fd; i++)
+    {
+      int flags = fcntl (i, F_GETFD);
+      TEST_VERIFY (flags > -1);
+      TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
+    }
+  for (int i = half_fd + 1; i < maximum_fd; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) > -1);
+
+  /* Create some gaps, close up to a threshold, and check result.  */
+  static int gap_close[] = { 57, 78, 81, 82, 84, 90 };
+  for (int i = 0; i < array_length (gap_close); i++)
+    xclose (gap_close[i]);
+
+  TEST_COMPARE (close_range (half_fd + 1, gap_1, CLOSE_RANGE_CLOEXEC), 0);
+  for (int i = half_fd + 1; i < gap_1; i++)
+    {
+      int flags = fcntl (i, F_GETFD);
+      if (is_in_array (gap_close, array_length (gap_close), i))
+        TEST_COMPARE (flags, -1);
+      else
+        {
+          TEST_VERIFY (flags > -1);
+          TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
+        }
+    }
+  for (int i = gap_1 + 1; i < maximum_fd; i++)
+    TEST_VERIFY (fcntl (i, F_GETFL) > -1);
+
+  /* Close the remaining but the last one.  */
+  TEST_COMPARE (close_range (gap_1 + 1, maximum_fd - 1, CLOSE_RANGE_CLOEXEC),
+                0);
+  for (int i = gap_1 + 1; i < maximum_fd - 1; i++)
+    {
+      int flags = fcntl (i, F_GETFD);
+      TEST_VERIFY (flags > -1);
+      TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
+    }
+  TEST_VERIFY (fcntl (maximum_fd, F_GETFL) > -1);
+
+  /* Close the last one.  */
+  TEST_COMPARE (close_range (maximum_fd, maximum_fd, CLOSE_RANGE_CLOEXEC), 0);
+  {
+    int flags = fcntl (maximum_fd, F_GETFD);
+    TEST_VERIFY (flags > -1);
+    TEST_COMPARE (flags & FD_CLOEXEC, FD_CLOEXEC);
+  }
+}
+
+static int
+do_test (void)
+{
+  close_range_test_max_upper_limit ();
+  close_range_test ();
+  close_range_test_subprocess ();
+  close_range_unshare_test ();
+  close_range_cloexec_test ();
+
+  return 0;
+}
+
+#include <support/test-driver.c>
index e90f3e54c0ab14356f290ab78d208a307f49acbf..b5e2710a97a2ba75d0e615594adb4bdc3ea08d29 100644 (file)
@@ -2323,6 +2323,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
index 792f681bbafaf06f31ac1fd35ae74818c91a6772..ecd0b4efb835853be29ddf064d762567708f0794 100644 (file)
@@ -2422,6 +2422,7 @@ GLIBC_2.34 aio_suspend64 F
 GLIBC_2.34 aio_write F
 GLIBC_2.34 aio_write64 F
 GLIBC_2.34 call_once F
+GLIBC_2.34 close_range F
 GLIBC_2.34 cnd_broadcast F
 GLIBC_2.34 cnd_destroy F
 GLIBC_2.34 cnd_init F
This page took 0.114228 seconds and 5 git commands to generate.