0b26f7
commit 772e33411bc730f832f415f93eb3e7c67e4d5488
0b26f7
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
0b26f7
Date:   Tue Aug 24 16:15:50 2021 -0300
0b26f7
0b26f7
    Use support_open_dev_null_range io/tst-closefrom, misc/tst-close_range, and posix/tst-spawn5 (BZ #28260)
0b26f7
    
0b26f7
    It ensures a continuous range of file descriptor and avoid hitting
0b26f7
    the RLIMIT_NOFILE.
0b26f7
    
0b26f7
    Checked on x86_64-linux-gnu.
0b26f7
    
0b26f7
    (cherry picked from commit 6b20880b22d1d0fce7e9f506baa6fe2d5c7fcfdc)
0b26f7
0b26f7
diff --git a/io/tst-closefrom.c b/io/tst-closefrom.c
0b26f7
index d4c187073c7280e9..395ec0d894101a47 100644
0b26f7
--- a/io/tst-closefrom.c
0b26f7
+++ b/io/tst-closefrom.c
0b26f7
@@ -24,31 +24,22 @@
0b26f7
 #include <support/check.h>
0b26f7
 #include <support/descriptors.h>
0b26f7
 #include <support/xunistd.h>
0b26f7
+#include <support/support.h>
0b26f7
 
0b26f7
 #include <array_length.h>
0b26f7
 
0b26f7
 #define NFDS 100
0b26f7
 
0b26f7
-static int
0b26f7
-open_multiple_temp_files (void)
0b26f7
-{
0b26f7
-  /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = xopen ("/dev/null", O_RDONLY, 0600);
0b26f7
-  for (int i = 1; i <= NFDS; i++)
0b26f7
-    TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600), lowfd + i);
0b26f7
-  return lowfd;
0b26f7
-}
0b26f7
-
0b26f7
 static int
0b26f7
 closefrom_test (void)
0b26f7
 {
0b26f7
   struct support_descriptors *descrs = support_descriptors_list ();
0b26f7
 
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
-  const int maximum_fd = lowfd + NFDS;
0b26f7
+  const int maximum_fd = lowfd + NFDS - 1;
0b26f7
   const int half_fd = lowfd + NFDS / 2;
0b26f7
-  const int gap = maximum_fd / 4;
0b26f7
+  const int gap = lowfd + NFDS / 4;
0b26f7
 
0b26f7
   /* Close half of the descriptors and check result.  */
0b26f7
   closefrom (half_fd);
0b26f7
@@ -58,7 +49,7 @@ closefrom_test (void)
0b26f7
       TEST_COMPARE (fcntl (i, F_GETFL), -1);
0b26f7
       TEST_COMPARE (errno, EBADF);
0b26f7
     }
0b26f7
-  for (int i = 0; i < half_fd; i++)
0b26f7
+  for (int i = lowfd; i < half_fd; i++)
0b26f7
     TEST_VERIFY (fcntl (i, F_GETFL) > -1);
0b26f7
 
0b26f7
   /* Create some gaps, close up to a threshold, and check result.  */
0b26f7
@@ -74,7 +65,7 @@ closefrom_test (void)
0b26f7
       TEST_COMPARE (fcntl (i, F_GETFL), -1);
0b26f7
       TEST_COMPARE (errno, EBADF);
0b26f7
     }
0b26f7
-  for (int i = 0; i < gap; i++)
0b26f7
+  for (int i = lowfd; i < gap; i++)
0b26f7
     TEST_VERIFY (fcntl (i, F_GETFL) > -1);
0b26f7
 
0b26f7
   /* Close the remmaining but the last one.  */
0b26f7
diff --git a/posix/tst-spawn5.c b/posix/tst-spawn5.c
0b26f7
index ac6673800464ce72..a95199af6b3b7c9a 100644
0b26f7
--- a/posix/tst-spawn5.c
0b26f7
+++ b/posix/tst-spawn5.c
0b26f7
@@ -47,17 +47,6 @@ static int initial_argv_count;
0b26f7
 
0b26f7
 #define NFDS 100
0b26f7
 
0b26f7
-static int
0b26f7
-open_multiple_temp_files (void)
0b26f7
-{
0b26f7
-  /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = xopen ("/dev/null", O_RDONLY, 0600);
0b26f7
-  for (int i = 1; i <= NFDS; i++)
0b26f7
-    TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600),
0b26f7
-		  lowfd + i);
0b26f7
-  return lowfd;
0b26f7
-}
0b26f7
-
0b26f7
 static int
0b26f7
 parse_fd (const char *str)
0b26f7
 {
0b26f7
@@ -185,7 +174,7 @@ spawn_closefrom_test (posix_spawn_file_actions_t *fa, int lowfd, int highfd,
0b26f7
 static void
0b26f7
 do_test_closefrom (void)
0b26f7
 {
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
   const int half_fd = lowfd + NFDS / 2;
0b26f7
 
0b26f7
   /* Close half of the descriptors and check result.  */
0b26f7
diff --git a/sysdeps/unix/sysv/linux/tst-close_range.c b/sysdeps/unix/sysv/linux/tst-close_range.c
0b26f7
index dccb6189c53fcb90..f5069d1b8a067241 100644
0b26f7
--- a/sysdeps/unix/sysv/linux/tst-close_range.c
0b26f7
+++ b/sysdeps/unix/sysv/linux/tst-close_range.c
0b26f7
@@ -36,23 +36,12 @@
0b26f7
 
0b26f7
 #define NFDS 100
0b26f7
 
0b26f7
-static int
0b26f7
-open_multiple_temp_files (void)
0b26f7
-{
0b26f7
-  /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = xopen ("/dev/null", O_RDONLY, 0600);
0b26f7
-  for (int i = 1; i <= NFDS; i++)
0b26f7
-    TEST_COMPARE (xopen ("/dev/null", O_RDONLY, 0600),
0b26f7
-		  lowfd + i);
0b26f7
-  return lowfd;
0b26f7
-}
0b26f7
-
0b26f7
 static void
0b26f7
 close_range_test_max_upper_limit (void)
0b26f7
 {
0b26f7
   struct support_descriptors *descrs = support_descriptors_list ();
0b26f7
 
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
   {
0b26f7
     int r = close_range (lowfd, ~0U, 0);
0b26f7
@@ -68,7 +57,7 @@ close_range_test_max_upper_limit (void)
0b26f7
 static void
0b26f7
 close_range_test_common (int lowfd, unsigned int flags)
0b26f7
 {
0b26f7
-  const int maximum_fd = lowfd + NFDS;
0b26f7
+  const int maximum_fd = lowfd + NFDS - 1;
0b26f7
   const int half_fd = lowfd + NFDS / 2;
0b26f7
   const int gap_1 = maximum_fd - 8;
0b26f7
 
0b26f7
@@ -121,7 +110,7 @@ close_range_test (void)
0b26f7
   struct support_descriptors *descrs = support_descriptors_list ();
0b26f7
 
0b26f7
   /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
   close_range_test_common (lowfd, 0);
0b26f7
 
0b26f7
@@ -146,7 +135,7 @@ close_range_test_subprocess (void)
0b26f7
   struct support_descriptors *descrs = support_descriptors_list ();
0b26f7
 
0b26f7
   /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
   struct support_stack stack = support_stack_alloc (4096);
0b26f7
 
0b26f7
@@ -184,7 +173,7 @@ close_range_unshare_test (void)
0b26f7
   struct support_descriptors *descrs1 = support_descriptors_list ();
0b26f7
 
0b26f7
   /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
   struct support_descriptors *descrs2 = support_descriptors_list ();
0b26f7
 
0b26f7
@@ -200,7 +189,7 @@ close_range_unshare_test (void)
0b26f7
 
0b26f7
   support_stack_free (&stack);
0b26f7
 
0b26f7
-  for (int i = 0; i < NFDS; i++)
0b26f7
+  for (int i = lowfd; i < lowfd + NFDS; i++)
0b26f7
     TEST_VERIFY (fcntl (i, F_GETFL) > -1);
0b26f7
 
0b26f7
   support_descriptors_check (descrs2);
0b26f7
@@ -226,9 +215,9 @@ static void
0b26f7
 close_range_cloexec_test (void)
0b26f7
 {
0b26f7
   /* Check if the temporary file descriptor has no no gaps.  */
0b26f7
-  const int lowfd = open_multiple_temp_files ();
0b26f7
+  int lowfd = support_open_dev_null_range (NFDS, O_RDONLY, 0600);
0b26f7
 
0b26f7
-  const int maximum_fd = lowfd + NFDS;
0b26f7
+  const int maximum_fd = lowfd + NFDS - 1;
0b26f7
   const int half_fd = lowfd + NFDS / 2;
0b26f7
   const int gap_1 = maximum_fd - 8;
0b26f7
 
0b26f7
@@ -251,13 +240,13 @@ close_range_cloexec_test (void)
0b26f7
   /* Create some gaps, close up to a threshold, and check result.  */
0b26f7
   static int gap_close[] = { 57, 78, 81, 82, 84, 90 };
0b26f7
   for (int i = 0; i < array_length (gap_close); i++)
0b26f7
-    xclose (gap_close[i]);
0b26f7
+    xclose (lowfd + gap_close[i]);
0b26f7
 
0b26f7
   TEST_COMPARE (close_range (half_fd + 1, gap_1, CLOSE_RANGE_CLOEXEC), 0);
0b26f7
   for (int i = half_fd + 1; i < gap_1; i++)
0b26f7
     {
0b26f7
       int flags = fcntl (i, F_GETFD);
0b26f7
-      if (is_in_array (gap_close, array_length (gap_close), i))
0b26f7
+      if (is_in_array (gap_close, array_length (gap_close), i - lowfd))
0b26f7
         TEST_COMPARE (flags, -1);
0b26f7
       else
0b26f7
         {