1 /* Smoke test for the closefrom.
2 Copyright (C) 2021 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>
28 #include <array_length.h>
33 open_multiple_temp_files (void)
35 /* Check if the temporary file descriptor has no no gaps. */
36 int lowfd
= xopen ("/dev/null", O_RDONLY
, 0600);
37 for (int i
= 1; i
<= NFDS
; i
++)
38 TEST_COMPARE (xopen ("/dev/null", O_RDONLY
, 0600), lowfd
+ i
);
45 struct support_descriptors
*descrs
= support_descriptors_list ();
47 int lowfd
= open_multiple_temp_files ();
49 const int maximum_fd
= lowfd
+ NFDS
;
50 const int half_fd
= lowfd
+ NFDS
/ 2;
51 const int gap
= maximum_fd
/ 4;
53 /* Close half of the descriptors and check result. */
56 for (int i
= half_fd
; i
<= maximum_fd
; i
++)
58 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
59 TEST_COMPARE (errno
, EBADF
);
61 for (int i
= 0; i
< half_fd
; i
++)
62 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
64 /* Create some gaps, close up to a threshold, and check result. */
70 /* Close half of the descriptors and check result. */
72 for (int i
= gap
+ 1; i
< maximum_fd
; i
++)
74 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
75 TEST_COMPARE (errno
, EBADF
);
77 for (int i
= 0; i
< gap
; i
++)
78 TEST_VERIFY (fcntl (i
, F_GETFL
) > -1);
80 /* Close the remmaining but the last one. */
81 closefrom (lowfd
+ 1);
82 for (int i
= lowfd
+ 1; i
<= maximum_fd
; i
++)
84 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
85 TEST_COMPARE (errno
, EBADF
);
87 TEST_VERIFY (fcntl (lowfd
, F_GETFL
) > -1);
89 /* Close the last one. */
91 TEST_COMPARE (fcntl (lowfd
, F_GETFL
), -1);
92 TEST_COMPARE (errno
, EBADF
);
94 /* Double check by check the /proc. */
95 support_descriptors_check (descrs
);
96 support_descriptors_free (descrs
);
101 /* Check if closefrom works even when no new file descriptors can be
104 closefrom_test_file_desc_limit (void)
109 if (getrlimit (RLIMIT_NOFILE
, &rl
) == -1)
110 FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
112 max_fd
= (rl
.rlim_cur
< max_fd
? rl
.rlim_cur
: max_fd
);
113 rl
.rlim_cur
= max_fd
;
115 if (setrlimit (RLIMIT_NOFILE
, &rl
) == 1)
116 FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
119 /* Exhauste the file descriptor limit. */
120 int lowfd
= xopen ("/dev/null", O_RDONLY
, 0600);
123 int fd
= open ("/dev/null", O_RDONLY
, 0600);
127 FAIL_EXIT1 ("open: %m");
130 TEST_VERIFY_EXIT (fd
< max_fd
);
134 for (int i
= lowfd
; i
< NFDS
; i
++)
136 TEST_COMPARE (fcntl (i
, F_GETFL
), -1);
137 TEST_COMPARE (errno
, EBADF
);
147 closefrom_test_file_desc_limit ();
152 #include <support/test-driver.c>