1 /* Tests for support_open_dev_null_range.
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/>. */
23 #include <support/check.h>
24 #include <support/support.h>
25 #include <support/xunistd.h>
26 #include <sys/resource.h>
30 # define PATH_MAX 1024
38 char *proc_fd_path = xasprintf ("/proc/self/fd/%d", fd);
39 char file_path[PATH_MAX];
40 ssize_t file_path_length
41 = readlink (proc_fd_path, file_path, sizeof (file_path));
42 if (file_path_length < 0)
43 FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path,
47 file_path[file_path_length] = '\0';
48 TEST_COMPARE_STRING (file_path, "/dev/null");
52 number_of_opened_files (void)
54 DIR *fds = opendir ("/proc/self/fd");
56 FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
62 struct dirent64 *e = readdir64 (fds);
66 FAIL_EXIT1 ("readdir: %m");
70 if (e->d_name[0] == '.')
74 long int fd = strtol (e->d_name, &endptr, 10);
75 if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
76 FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
79 /* Skip the descriptor which is used to enumerate the
81 if (fd == dirfd (fds))
96 int lowfd = support_open_dev_null_range (nfds1, O_RDONLY, 0600);
97 for (int i = 0; i < nfds1; i++)
99 TEST_VERIFY (fcntl (lowfd + i, F_GETFL) > -1);
100 check_path (lowfd + i);
103 /* create some gaps. */
108 const int nfds2 = 16;
109 int lowfd2 = support_open_dev_null_range (nfds2, O_RDONLY, 0600);
110 for (int i = 0; i < nfds2; i++)
112 TEST_VERIFY (fcntl (lowfd2 + i, F_GETFL) > -1);
113 check_path (lowfd2 + i);
116 /* Decrease the maximum number of files. */
119 if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
120 FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
122 rl.rlim_cur = number_of_opened_files ();
124 if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
125 FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
128 const int nfds3 = 16;
129 int lowfd3 = support_open_dev_null_range (nfds3, O_RDONLY, 0600);
130 for (int i = 0; i < nfds3; i++)
132 TEST_VERIFY (fcntl (lowfd3 + i, F_GETFL) > -1);
133 check_path (lowfd3 + i);
136 /* create a lot of gaps to trigger the range extension. */
142 xclose (lowfd3 + 11);
143 xclose (lowfd3 + 13);
145 const int nfds4 = 16;
146 int lowfd4 = support_open_dev_null_range (nfds4, O_RDONLY, 0600);
147 for (int i = 0; i < nfds4; i++)
149 TEST_VERIFY (fcntl (lowfd4 + i, F_GETFL) > -1);
150 check_path (lowfd4 + i);
156 #include <support/test-driver.c>