Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/sys/kern/kern_descrip_test.c
39483 views
1
/*-
2
* Copyright (c) 2014 EMC Corp.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
* SUCH DAMAGE.
25
*/
26
27
#include <sys/param.h>
28
#include <sys/limits.h>
29
#include <sys/resource.h>
30
#include <sys/stat.h>
31
#include <sys/sysctl.h>
32
#include <sys/time.h>
33
#include <sys/wait.h>
34
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <signal.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <strings.h>
42
#include <unistd.h>
43
44
#include <atf-c.h>
45
46
static volatile sig_atomic_t done;
47
48
#define AFILE "afile"
49
#define EXPANDBY 1000
50
#define PARALLEL 4
51
#define RENDEZVOUS "rendezvous"
52
#define VALUE "value"
53
54
ATF_TC_WITHOUT_HEAD(dup2__simple);
55
ATF_TC_BODY(dup2__simple, tc)
56
{
57
int fd1, fd2;
58
struct stat sb1, sb2;
59
60
ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
61
fd2 = 27;
62
ATF_REQUIRE(dup2(fd1, fd2) != -1);
63
ATF_REQUIRE(fstat(fd1, &sb1) != -1);
64
ATF_REQUIRE(fstat(fd2, &sb2) != -1);
65
ATF_REQUIRE(bcmp(&sb1, &sb2, sizeof(sb1)) == 0);
66
}
67
68
ATF_TC(dup2__ebadf_when_2nd_arg_out_of_range);
69
ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc)
70
{
71
atf_tc_set_md_var(tc, "descr", "Regression test for r234131");
72
}
73
74
ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc)
75
{
76
int fd1, fd2, ret;
77
78
ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
79
fd2 = INT_MAX;
80
ret = dup2(fd1, fd2);
81
ATF_CHECK_EQ(-1, ret);
82
ATF_CHECK_EQ(EBADF, errno);
83
}
84
85
static void
86
handler(int s __unused)
87
{
88
done++;
89
}
90
91
static void
92
openfiles2(size_t n)
93
{
94
size_t i;
95
int r;
96
97
errno = 0;
98
for (i = 0; i < n; i++) {
99
r = open(AFILE, O_RDONLY);
100
if (r < 0) {
101
fprintf(stderr, "open: %s\n", strerror(errno));
102
_exit(1);
103
}
104
}
105
kill(getppid(), SIGUSR1);
106
107
for (;;) {
108
if (access(RENDEZVOUS, R_OK) != 0)
109
break;
110
usleep(1000);
111
}
112
_exit(0);
113
}
114
115
static void
116
openfiles(size_t n)
117
{
118
int i, fd;
119
120
signal(SIGUSR1, handler);
121
ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1);
122
close(fd);
123
ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1);
124
close(fd);
125
done = 0;
126
for (i = 0; i < PARALLEL; i++)
127
if (fork() == 0)
128
openfiles2(n / PARALLEL);
129
while (done != PARALLEL) {
130
usleep(1000);
131
ATF_REQUIRE_EQ_MSG(0, waitpid(-1, NULL, WNOHANG),
132
"a child exited unexpectedly");
133
}
134
unlink(RENDEZVOUS);
135
for (i = 0; i < PARALLEL; i++)
136
ATF_CHECK_MSG(wait(NULL) > 0, "wait: %s", strerror(errno));
137
}
138
139
ATF_TC_WITH_CLEANUP(kern_maxfiles__increase);
140
ATF_TC_HEAD(kern_maxfiles__increase, tc)
141
{
142
atf_tc_set_md_var(tc, "require.user", "root");
143
atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
144
atf_tc_set_md_var(tc, "descr",
145
"Check kern.maxfiles expansion");
146
}
147
148
ATF_TC_BODY(kern_maxfiles__increase, tc)
149
{
150
size_t oldlen;
151
int maxfiles, oldmaxfiles, current;
152
char buf[80];
153
struct rlimit rl;
154
155
oldlen = sizeof(maxfiles);
156
if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1)
157
atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
158
strerror(errno));
159
if (sysctlbyname("kern.openfiles", &current, &oldlen, NULL, 0) == -1)
160
atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles",
161
strerror(errno));
162
163
oldmaxfiles = maxfiles;
164
165
/* Store old kern.maxfiles in a symlink for cleanup */
166
snprintf(buf, sizeof(buf), "%d", oldmaxfiles);
167
if (symlink(buf, VALUE) == 1)
168
atf_tc_fail("symlink(%s, %s): %s", buf, VALUE,
169
strerror(errno));
170
171
maxfiles += EXPANDBY;
172
if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1)
173
atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
174
strerror(errno));
175
176
rl.rlim_cur = rl.rlim_max = maxfiles;
177
ATF_REQUIRE_EQ_MSG(0, setrlimit(RLIMIT_NOFILE, &rl),
178
"setrlimit(RLIMIT_NOFILE, %d): %s", maxfiles, strerror(errno));
179
180
openfiles(oldmaxfiles - current + EXPANDBY / 2);
181
}
182
183
ATF_TC_CLEANUP(kern_maxfiles__increase, tc)
184
{
185
size_t oldlen;
186
int n, oldmaxfiles;
187
char buf[80];
188
189
if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) {
190
buf[MIN((size_t)n, sizeof(buf) - 1)] = '\0';
191
if (sscanf(buf, "%d", &oldmaxfiles) == 1) {
192
oldlen = sizeof(oldmaxfiles);
193
(void) sysctlbyname("kern.maxfiles", NULL, 0,
194
&oldmaxfiles, oldlen);
195
}
196
}
197
(void)unlink(VALUE);
198
(void)unlink(AFILE);
199
}
200
201
ATF_TP_ADD_TCS(tp)
202
{
203
204
ATF_TP_ADD_TC(tp, dup2__simple);
205
ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range);
206
ATF_TP_ADD_TC(tp, kern_maxfiles__increase);
207
208
return (atf_no_error());
209
}
210
211