kern_descrip_test.c revision 282860
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/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/tests/sys/kern/kern_descrip_test.c 282860 2015-05-13 12:17:01Z ngie $");
29
30#include <errno.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <strings.h>
36#include <sys/limits.h>
37#include <sys/stat.h>
38#include <sys/sysctl.h>
39#include <unistd.h>
40#include <atf-c.h>
41
42static volatile sig_atomic_t done;
43
44#define AFILE "afile"
45#define EXPANDBY 1000
46#define PARALLEL 4
47#define RENDEZVOUS "rendezvous"
48#define VALUE "value"
49
50ATF_TC_WITHOUT_HEAD(dup2__simple);
51ATF_TC_BODY(dup2__simple, tc)
52{
53	int fd1, fd2;
54	struct stat sb1, sb2;
55
56	ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
57	fd2 = 27;
58	ATF_REQUIRE(dup2(fd1, fd2) != -1);
59	ATF_REQUIRE(fstat(fd1, &sb1) != -1);
60	ATF_REQUIRE(fstat(fd2, &sb2) != -1);
61	ATF_REQUIRE(bcmp(&sb1, &sb2, sizeof(sb1)) == 0);
62}
63
64ATF_TC(dup2__ebadf_when_2nd_arg_out_of_range);
65ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc)
66{
67	atf_tc_set_md_var(tc, "descr", "Regression test for r234131");
68}
69
70ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc)
71{
72	int fd1, fd2, ret;
73
74	ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1);
75	fd2 = INT_MAX;
76	ret = dup2(fd1, fd2);
77	ATF_CHECK_EQ(-1, ret);
78	ATF_CHECK_EQ(EBADF, errno);
79}
80
81static void
82handler(int s __unused)
83{
84	done++;
85}
86
87static void
88openfiles2(size_t n)
89{
90	size_t i;
91	int r;
92
93	errno = 0;
94	for (i = 0; i < n; i++)
95		ATF_REQUIRE((r = open(AFILE, O_RDONLY)) != -1);
96	kill(getppid(), SIGUSR1);
97
98	for (;;) {
99		if (access(RENDEZVOUS, R_OK) != 0)
100			break;
101		usleep(1000);
102	}
103	_exit(0);
104}
105
106static void
107openfiles(size_t n)
108{
109	int i, fd;
110
111	signal(SIGUSR1, handler);
112	ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1);
113	close(fd);
114	ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1);
115	close(fd);
116	done = 0;
117	for (i = 0; i < PARALLEL; i++)
118		if (fork() == 0)
119			openfiles2(n / PARALLEL);
120	while (done != PARALLEL)
121		usleep(1000);
122	unlink(RENDEZVOUS);
123	usleep(40000);
124}
125
126ATF_TC_WITH_CLEANUP(kern_maxfiles__increase);
127ATF_TC_HEAD(kern_maxfiles__increase, tc)
128{
129	atf_tc_set_md_var(tc, "require.user", "root");
130	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
131	atf_tc_set_md_var(tc, "descr",
132	    "Check kern.maxfiles expansion");
133}
134
135ATF_TC_BODY(kern_maxfiles__increase, tc)
136{
137	size_t oldlen;
138	int maxfiles, oldmaxfiles, current;
139	char buf[80];
140
141	oldlen = sizeof(maxfiles);
142	if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1)
143		atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
144		    strerror(errno));
145	if (sysctlbyname("kern.openfiles", &current, &oldlen, NULL, 0) == -1)
146		atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles",
147		    strerror(errno));
148
149	oldmaxfiles = maxfiles;
150
151	/* Store old kern.maxfiles in a symlink for cleanup */
152	snprintf(buf, sizeof(buf), "%d", oldmaxfiles);
153	if (symlink(buf, VALUE) == 1)
154		atf_tc_fail("symlink(%s, %s): %s", buf, VALUE,
155		    strerror(errno));
156
157	maxfiles += EXPANDBY;
158	if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1)
159		atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles",
160		    strerror(errno));
161
162	openfiles(oldmaxfiles - current + 1);
163	(void)unlink(VALUE);
164}
165
166ATF_TC_CLEANUP(kern_maxfiles__increase, tc)
167{
168	size_t oldlen;
169	int n, oldmaxfiles;
170	char buf[80];
171
172	if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) {
173		buf[n] = '\0';
174		if (sscanf(buf, "%d", &oldmaxfiles) == 1) {
175			oldlen = sizeof(oldmaxfiles);
176			(void) sysctlbyname("kern.maxfiles", NULL, 0,
177			    &oldmaxfiles, oldlen);
178		}
179	}
180}
181
182ATF_TP_ADD_TCS(tp)
183{
184
185	ATF_TP_ADD_TC(tp, dup2__simple);
186	ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range);
187	ATF_TP_ADD_TC(tp, kern_maxfiles__increase);
188
189	return (atf_no_error());
190}
191