1 /* Test for the close_range system call.
2 Copyright (C) 2021-2023 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/>. */
29 #include <array_length.h>
30 #include <support/capture_subprocess.h>
31 #include <support/check.h>
32 #include <support/descriptors.h>
33 #include <support/support.h>
34 #include <support/xsched.h>
35 #include <support/xunistd.h>
40 close_range_test_max_upper_limit (void)
42 struct support_descriptors
*descrs
= support_descriptors_list ();
44 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
47 int r
= close_range (lowfd
, ~0U, 0);
48 if (r
== -1 && errno
== ENOSYS
)
49 FAIL_UNSUPPORTED ("close_range not supported");
53 support_descriptors_check (descrs
);
54 support_descriptors_free (descrs
);
58 close_range_test_common (int lowfd
, unsigned int flags
)
60 const int maximum_fd
= lowfd
+ NFDS
- 1;
61 const int half_fd
= lowfd
+ NFDS
/ 2;
62 const int gap_1
= maximum_fd
- 8;
64 /* Close half of the descriptors and check result. */
65 TEST_COMPARE (close_range (lowfd
, half_fd
, flags
), 0);
66 for (int i
= lowfd
; i
<= half_fd
; i
++)
68 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
69 TEST_COMPARE (errno
, EBADF
);
71 for (int i
= half_fd
+ 1; i
< maximum_fd
; i
++)
72 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
74 /* Create some gaps, close up to a threshold, and check result. */
82 TEST_COMPARE (close_range (half_fd
+ 1, gap_1
, flags
), 0);
83 for (int i
= half_fd
+ 1; i
< gap_1
; i
++)
85 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
86 TEST_COMPARE (errno
, EBADF
);
88 for (int i
= gap_1
+ 1; i
< maximum_fd
; i
++)
89 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
91 /* Close the remaining but the last one. */
92 TEST_COMPARE (close_range (gap_1
+ 1, maximum_fd
- 1, flags
), 0);
93 for (int i
= gap_1
+ 1; i
< maximum_fd
- 1; i
++)
95 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
96 TEST_COMPARE (errno
, EBADF
);
98 TEST_VERIFY (fcntl (maximum_fd
, F_GETFL
) > -1);
100 /* Close the last one. */
101 TEST_COMPARE (close_range (maximum_fd
, maximum_fd
, flags
), 0);
102 TEST_COMPARE (fcntl (maximum_fd
, F_GETFL
), -1);
103 TEST_COMPARE (errno
, EBADF
);
106 /* Basic tests: check if the syscall close ranges with and without gaps. */
108 close_range_test (void)
110 struct support_descriptors
*descrs
= support_descriptors_list ();
112 /* Check if the temporary file descriptor has no no gaps. */
113 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
115 close_range_test_common (lowfd
, 0);
117 /* Double check by check the /proc. */
118 support_descriptors_check (descrs
);
119 support_descriptors_free (descrs
);
124 close_range_test_fn (void *arg
)
126 int lowfd
= (int) ((uintptr_t) arg
);
127 close_range_test_common (lowfd
, 0);
131 /* Check if a clone_range on a subprocess created with CLONE_FILES close
132 the shared file descriptor table entries in the parent. */
134 close_range_test_subprocess (void)
136 struct support_descriptors
*descrs
= support_descriptors_list ();
138 /* Check if the temporary file descriptor has no no gaps. */
139 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
141 struct support_stack stack
= support_stack_alloc (4096);
143 pid_t pid
= xclone (close_range_test_fn
, (void*) (uintptr_t) lowfd
,
144 stack
.stack
, stack
.size
, CLONE_FILES
| SIGCHLD
);
145 TEST_VERIFY_EXIT (pid
> 0);
147 xwaitpid (pid
, &status
, 0);
148 TEST_VERIFY (WIFEXITED (status
));
149 TEST_COMPARE (WEXITSTATUS (status
), 0);
151 support_stack_free (&stack
);
153 for (int i
= lowfd
; i
< NFDS
; i
++)
154 TEST_VERIFY (fcntl (i
, F_GETFL
) < 0);
156 support_descriptors_check (descrs
);
157 support_descriptors_free (descrs
);
162 #ifdef CLOSE_RANGE_UNSHARE
164 close_range_unshare_test_fn (void *arg
)
166 int lowfd
= (int) ((uintptr_t) arg
);
167 close_range_test_common (lowfd
, CLOSE_RANGE_UNSHARE
);
171 /* Check if a close_range with CLOSE_RANGE_UNSHARE issued from a subprocess
172 created with CLONE_FILES does not close the parent file descriptor list. */
174 close_range_unshare_test (void)
176 struct support_descriptors
*descrs1
= support_descriptors_list ();
178 /* Check if the temporary file descriptor has no no gaps. */
179 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
181 struct support_descriptors
*descrs2
= support_descriptors_list ();
183 struct support_stack stack
= support_stack_alloc (4096);
185 pid_t pid
= xclone (close_range_unshare_test_fn
, (void*) (uintptr_t) lowfd
,
186 stack
.stack
, stack
.size
, CLONE_FILES
| SIGCHLD
);
187 TEST_VERIFY_EXIT (pid
> 0);
189 xwaitpid (pid
, &status
, 0);
190 TEST_VERIFY (WIFEXITED (status
));
191 TEST_COMPARE (WEXITSTATUS (status
), 0);
193 support_stack_free (&stack
);
195 for (int i
= lowfd
; i
< lowfd
+ NFDS
; i
++)
196 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
198 support_descriptors_check (descrs2
);
199 support_descriptors_free (descrs2
);
201 TEST_COMPARE (close_range (lowfd
, lowfd
+ NFDS
, 0), 0);
203 support_descriptors_check (descrs1
);
204 support_descriptors_free (descrs1
);
209 is_in_array (int *arr
, size_t len
, int fd
)
212 for (int i
= 0; i
< len
; i
++)
219 close_range_cloexec_test (void)
221 /* Check if the temporary file descriptor has no no gaps. */
222 int lowfd
= support_open_dev_null_range (NFDS
, O_RDONLY
, 0600);
224 const int maximum_fd
= lowfd
+ NFDS
- 1;
225 const int half_fd
= lowfd
+ NFDS
/ 2;
226 const int gap_1
= maximum_fd
- 8;
228 /* Close half of the descriptors and check result. */
229 int r
= close_range (lowfd
, half_fd
, CLOSE_RANGE_CLOEXEC
);
230 if (r
== -1 && errno
== EINVAL
)
232 printf ("%s: CLOSE_RANGE_CLOEXEC not supported\n", __func__
);
235 for (int i
= lowfd
; i
<= half_fd
; i
++)
237 int flags
= fcntl (i
, F_GETFD
);
238 TEST_VERIFY (flags
> -1);
239 TEST_COMPARE (flags
& FD_CLOEXEC
, FD_CLOEXEC
);
241 for (int i
= half_fd
+ 1; i
< maximum_fd
; i
++)
242 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
244 /* Create some gaps, close up to a threshold, and check result. */
245 static int gap_close
[] = { 57, 78, 81, 82, 84, 90 };
246 for (int i
= 0; i
< array_length (gap_close
); i
++)
247 xclose (lowfd
+ gap_close
[i
]);
249 TEST_COMPARE (close_range (half_fd
+ 1, gap_1
, CLOSE_RANGE_CLOEXEC
), 0);
250 for (int i
= half_fd
+ 1; i
< gap_1
; i
++)
252 int flags
= fcntl (i
, F_GETFD
);
253 if (is_in_array (gap_close
, array_length (gap_close
), i
- lowfd
))
254 TEST_COMPARE (flags
, -1);
257 TEST_VERIFY (flags
> -1);
258 TEST_COMPARE (flags
& FD_CLOEXEC
, FD_CLOEXEC
);
261 for (int i
= gap_1
+ 1; i
< maximum_fd
; i
++)
262 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
264 /* Close the remaining but the last one. */
265 TEST_COMPARE (close_range (gap_1
+ 1, maximum_fd
- 1, CLOSE_RANGE_CLOEXEC
),
267 for (int i
= gap_1
+ 1; i
< maximum_fd
- 1; i
++)
269 int flags
= fcntl (i
, F_GETFD
);
270 TEST_VERIFY (flags
> -1);
271 TEST_COMPARE (flags
& FD_CLOEXEC
, FD_CLOEXEC
);
273 TEST_VERIFY (fcntl (maximum_fd
, F_GETFL
) > -1);
275 /* Close the last one. */
276 TEST_COMPARE (close_range (maximum_fd
, maximum_fd
, CLOSE_RANGE_CLOEXEC
), 0);
278 int flags
= fcntl (maximum_fd
, F_GETFD
);
279 TEST_VERIFY (flags
> -1);
280 TEST_COMPARE (flags
& FD_CLOEXEC
, FD_CLOEXEC
);
287 close_range_test_max_upper_limit ();
290 close_range_test_subprocess ();
292 #ifdef CLOSE_RANGE_UNSHARE
293 close_range_unshare_test ();
295 close_range_cloexec_test ();
300 #include <support/test-driver.c>