2 * Copyright (c) 2014 EMC Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/limits.h>
32 #include <sys/resource.h>
34 #include <sys/sysctl.h>
49 static volatile sig_atomic_t done;
54 #define RENDEZVOUS "rendezvous"
57 ATF_TC_WITHOUT_HEAD(dup2__simple);
58 ATF_TC_BODY(dup2__simple, tc)
63 ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
65 ATF_REQUIRE(dup2(fd1, fd2) != -1);
66 ATF_REQUIRE(fstat(fd1, &sb1) != -1);
67 ATF_REQUIRE(fstat(fd2, &sb2) != -1);
68 ATF_REQUIRE(bcmp(&sb1, &sb2, sizeof(sb1)) == 0);
71 ATF_TC(dup2__ebadf_when_2nd_arg_out_of_range);
72 ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc)
74 atf_tc_set_md_var(tc, "descr", "Regression test for r234131");
77 ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc)
81 ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
84 ATF_CHECK_EQ(-1, ret);
85 ATF_CHECK_EQ(EBADF, errno);
89 handler(int s __unused)
101 for (i = 0; i < n; i++) {
102 r = open(AFILE, O_RDONLY);
104 fprintf(stderr, "open: %s\n", strerror(errno));
108 kill(getppid(), SIGUSR1);
111 if (access(RENDEZVOUS, R_OK) != 0)
123 signal(SIGUSR1, handler);
124 ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1);
126 ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1);
129 for (i = 0; i < PARALLEL; i++)
131 openfiles2(n / PARALLEL);
132 while (done != PARALLEL) {
134 ATF_REQUIRE_EQ_MSG(0, waitpid(-1, NULL, WNOHANG),
135 "a child exited unexpectedly");
138 for (i = 0; i < PARALLEL; i++)
139 ATF_CHECK_MSG(wait(NULL) > 0, "wait: %s", strerror(errno));
142 ATF_TC_WITH_CLEANUP(kern_maxfiles__increase);
143 ATF_TC_HEAD(kern_maxfiles__increase, tc)
145 atf_tc_set_md_var(tc, "require.user", "root");
146 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
147 atf_tc_set_md_var(tc, "descr",
148 "Check kern.maxfiles expansion");
151 ATF_TC_BODY(kern_maxfiles__increase, tc)
154 int maxfiles, oldmaxfiles, current;
158 oldlen = sizeof(maxfiles);
159 if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1)
160 atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
162 if (sysctlbyname("kern.openfiles", ¤t, &oldlen, NULL, 0) == -1)
163 atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles",
166 oldmaxfiles = maxfiles;
168 /* Store old kern.maxfiles in a symlink for cleanup */
169 snprintf(buf, sizeof(buf), "%d", oldmaxfiles);
170 if (symlink(buf, VALUE) == 1)
171 atf_tc_fail("symlink(%s, %s): %s", buf, VALUE,
174 maxfiles += EXPANDBY;
175 if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1)
176 atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
179 rl.rlim_cur = rl.rlim_max = maxfiles;
180 ATF_REQUIRE_EQ_MSG(0, setrlimit(RLIMIT_NOFILE, &rl),
181 "setrlimit(RLIMIT_NOFILE, %d): %s", maxfiles, strerror(errno));
183 openfiles(oldmaxfiles - current + EXPANDBY / 2);
186 ATF_TC_CLEANUP(kern_maxfiles__increase, tc)
192 if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) {
193 buf[MIN((size_t)n, sizeof(buf) - 1)] = '\0';
194 if (sscanf(buf, "%d", &oldmaxfiles) == 1) {
195 oldlen = sizeof(oldmaxfiles);
196 (void) sysctlbyname("kern.maxfiles", NULL, 0,
197 &oldmaxfiles, oldlen);
207 ATF_TP_ADD_TC(tp, dup2__simple);
208 ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range);
209 ATF_TP_ADD_TC(tp, kern_maxfiles__increase);
211 return (atf_no_error());