1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/* Tests for support_open_dev_null_range.
Copyright (C) 2021-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xunistd.h>
#include <sys/resource.h>
#include <stdlib.h>
#ifndef PATH_MAX
# define PATH_MAX 1024
#endif
#include <stdio.h>
static void
check_path (int fd)
{
char *proc_fd_path = xasprintf ("/proc/self/fd/%d", fd);
char file_path[PATH_MAX];
ssize_t file_path_length
= readlink (proc_fd_path, file_path, sizeof (file_path));
if (file_path_length < 0)
FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path,
sizeof (file_path));
free (proc_fd_path);
file_path[file_path_length] = '\0';
TEST_COMPARE_STRING (file_path, "/dev/null");
}
static int
number_of_opened_files (void)
{
DIR *fds = opendir ("/proc/self/fd");
if (fds == NULL)
FAIL_EXIT1 ("opendir (\"/proc/self/fd\"): %m");
int r = 0;
while (true)
{
errno = 0;
struct dirent64 *e = readdir64 (fds);
if (e == NULL)
{
if (errno != 0)
FAIL_EXIT1 ("readdir: %m");
break;
}
if (e->d_name[0] == '.')
continue;
char *endptr;
long int fd = strtol (e->d_name, &endptr, 10);
if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
FAIL_EXIT1 ("readdir: invalid file descriptor name: /proc/self/fd/%s",
e->d_name);
/* Skip the descriptor which is used to enumerate the
descriptors. */
if (fd == dirfd (fds))
continue;
r = r + 1;
}
closedir (fds);
return r;
}
static int
do_test (void)
{
const int nfds1 = 8;
int lowfd = support_open_dev_null_range (nfds1, O_RDONLY, 0600);
for (int i = 0; i < nfds1; i++)
{
TEST_VERIFY (fcntl (lowfd + i, F_GETFL) > -1);
check_path (lowfd + i);
}
/* create some gaps. */
xclose (lowfd + 1);
xclose (lowfd + 5);
xclose (lowfd + 6);
const int nfds2 = 16;
int lowfd2 = support_open_dev_null_range (nfds2, O_RDONLY, 0600);
for (int i = 0; i < nfds2; i++)
{
TEST_VERIFY (fcntl (lowfd2 + i, F_GETFL) > -1);
check_path (lowfd2 + i);
}
/* Decrease the maximum number of files. */
{
struct rlimit rl;
if (getrlimit (RLIMIT_NOFILE, &rl) == -1)
FAIL_EXIT1 ("getrlimit (RLIMIT_NOFILE): %m");
rl.rlim_cur = number_of_opened_files ();
if (setrlimit (RLIMIT_NOFILE, &rl) == 1)
FAIL_EXIT1 ("setrlimit (RLIMIT_NOFILE): %m");
}
const int nfds3 = 16;
int lowfd3 = support_open_dev_null_range (nfds3, O_RDONLY, 0600);
for (int i = 0; i < nfds3; i++)
{
TEST_VERIFY (fcntl (lowfd3 + i, F_GETFL) > -1);
check_path (lowfd3 + i);
}
/* create a lot of gaps to trigger the range extension. */
xclose (lowfd3 + 1);
xclose (lowfd3 + 3);
xclose (lowfd3 + 5);
xclose (lowfd3 + 7);
xclose (lowfd3 + 9);
xclose (lowfd3 + 11);
xclose (lowfd3 + 13);
const int nfds4 = 16;
int lowfd4 = support_open_dev_null_range (nfds4, O_RDONLY, 0600);
for (int i = 0; i < nfds4; i++)
{
TEST_VERIFY (fcntl (lowfd4 + i, F_GETFL) > -1);
check_path (lowfd4 + i);
}
return 0;
}
#include <support/test-driver.c>
|