1 /* Smoke test for the closefrom.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <sys/resource.h>
24 #include <support/check.h>
25 #include <support/descriptors.h>
26 #include <support/xunistd.h>
27 #include <support/support.h>
29 #include <array_length.h>
36 struct support_descriptors
*descrs
= support_descriptors_list ();
38 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
40 const int maximum_fd
= lowfd
+ NFDS
- 1;
41 const int half_fd
= lowfd
+ NFDS
/ 2;
42 const int gap
= lowfd
+ NFDS
/ 4;
44 /* Close half of the descriptors and check result. */
47 for (int i
= half_fd
; i
<= maximum_fd
; i
++)
49 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
50 TEST_COMPARE (errno
, EBADF
);
52 for (int i
= lowfd
; i
< half_fd
; i
++)
53 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
55 /* Create some gaps, close up to a threshold, and check result. */
61 /* Close half of the descriptors and check result. */
63 for (int i
= gap
+ 1; i
< maximum_fd
; i
++)
65 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
66 TEST_COMPARE (errno
, EBADF
);
68 for (int i
= lowfd
; i
< gap
; i
++)
69 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
71 /* Close the remmaining but the last one. */
72 closefrom (lowfd
+ 1);
73 for (int i
= lowfd
+ 1; i
<= maximum_fd
; i
++)
75 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
76 TEST_COMPARE (errno
, EBADF
);
78 TEST_VERIFY (fcntl (lowfd
, F_GETFL
) > -1);
80 /* Close the last one. */
82 TEST_COMPARE (fcntl (lowfd
, F_GETFL
), -1);
83 TEST_COMPARE (errno
, EBADF
);
85 /* Double check by check the /proc. */
86 support_descriptors_check (descrs
);
87 support_descriptors_free (descrs
);
92 /* Check if closefrom works even when no new file descriptors can be
95 closefrom_test_file_desc_limit (void)
100 if (getrlimit (RLIMIT_NOFILE
, &rl
) == -1)
101 FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
103 max_fd
= (rl
.rlim_cur
< max_fd
? rl
.rlim_cur
: max_fd
);
104 rl
.rlim_cur
= max_fd
;
106 if (setrlimit (RLIMIT_NOFILE
, &rl
) == 1)
107 FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
110 /* Exhauste the file descriptor limit. */
111 int lowfd
= xopen ("/dev/null", O_RDONLY
, 0600);
114 int fd
= open ("/dev/null", O_RDONLY
, 0600);
118 FAIL_EXIT1 ("open: %m");
121 TEST_VERIFY_EXIT (fd
< max_fd
);
125 for (int i
= lowfd
; i
< NFDS
; i
++)
127 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
128 TEST_COMPARE (errno
, EBADF
);
138 closefrom_test_file_desc_limit ();
143 #include <support/test-driver.c>