io: Add closefrom [BZ #10353]
[glibc.git] / io / tst-closefrom.c
blobd4c187073c7280e9264e877950ff49d38cd2c94e
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/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/resource.h>
22 #include <unistd.h>
24 #include <support/check.h>
25 #include <support/descriptors.h>
26 #include <support/xunistd.h>
28 #include <array_length.h>
30 #define NFDS 100
32 static int
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);
39 return lowfd;
42 static int
43 closefrom_test (void)
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. */
54 closefrom (half_fd);
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. */
65 xclose (lowfd + 35);
66 xclose (lowfd + 38);
67 xclose (lowfd + 42);
68 xclose (lowfd + 46);
70 /* Close half of the descriptors and check result. */
71 closefrom (gap);
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. */
90 closefrom (lowfd);
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);
98 return 0;
101 /* Check if closefrom works even when no new file descriptors can be
102 created. */
103 static int
104 closefrom_test_file_desc_limit (void)
106 int max_fd = NFDS;
108 struct rlimit rl;
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);
121 for (;;)
123 int fd = open ("/dev/null", O_RDONLY, 0600);
124 if (fd == -1)
126 if (errno != EMFILE)
127 FAIL_EXIT1 ("open: %m");
128 break;
130 TEST_VERIFY_EXIT (fd < max_fd);
133 closefrom (lowfd);
134 for (int i = lowfd; i < NFDS; i++)
136 TEST_COMPARE (fcntl (i, F_GETFL), -1);
137 TEST_COMPARE (errno, EBADF);
140 return 0;
143 static int
144 do_test (void)
146 closefrom_test ();
147 closefrom_test_file_desc_limit ();
149 return 0;
152 #include <support/test-driver.c>