xref: /netbsd-src/tests/lib/libc/sys/t_select.c (revision 2c2346570d2ce999f9439f8cef1e71b985b433f7)
1 /*	$NetBSD: t_select.c,v 1.5 2024/10/18 16:39:41 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundatiom
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 
34 #include <sys/select.h>
35 #include <sys/wait.h>
36 
37 #include <assert.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include <atf-c.h>
48 
49 static sig_atomic_t keep_going = 1;
50 
51 static void
52 sig_handler(int signum __unused)
53 {
54 	keep_going = 0;
55 }
56 
57 static void
58 sigchld(int signum __unused)
59 {
60 }
61 
62 static char
63 xtoa(uint8_t n)
64 {
65 	static const char xarray[] = "0123456789abcdef";
66 	assert(n < sizeof(xarray));
67 	return xarray[n];
68 }
69 
70 static const char *
71 prmask(const sigset_t *m, char *buf, size_t len)
72 {
73 	size_t j = 2;
74 	assert(len >= 3 + sizeof(*m));
75 	buf[0] = '0';
76 	buf[1] = 'x';
77 #define N(p, a)	(((p) >> ((a) * 4)) & 0xf)
78 	for (size_t i = __arraycount(m->__bits); i > 0; i--) {
79 		uint32_t p = m->__bits[i - 1];
80 		for (size_t k = sizeof(p); k > 0; k--)
81 			buf[j++] = xtoa(N(p, k - 1));
82 	}
83 	buf[j] = '\0';
84 	return buf;
85 }
86 
87 static __dead void
88 child(const struct timespec *ts)
89 {
90 	struct sigaction sa;
91 	sigset_t set, oset, nset;
92 	char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3];
93 	int fd;
94 
95 	memset(&sa, 0, sizeof(sa));
96 	sa.sa_handler = sig_handler;
97 	if ((fd = open("/dev/null", O_RDONLY)) == -1)
98 		err(1, "open");
99 
100 	if (sigaction(SIGTERM, &sa, NULL) == -1)
101 		err(1, "sigaction");
102 
103 	sigfillset(&set);
104 	if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
105 		err(1, "sigprocmask");
106 
107 	if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
108 		err(1, "sigprocmask");
109 
110 	sigemptyset(&set);
111 
112 	for (;;) {
113 		fd_set rset;
114 		FD_ZERO(&rset);
115 		FD_SET(fd, &rset);
116 		if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) {
117 			if(errno == EINTR) {
118 				if (!keep_going)
119 					break;
120 			}
121 		}
122 		if (ts)
123 			break;
124 	}
125 	if (sigprocmask(SIG_BLOCK, NULL, &nset) == -1)
126 		err(1, "sigprocmask");
127 	if (memcmp(&oset, &nset, sizeof(oset)) != 0)
128 		atf_tc_fail("pselect() masks don't match "
129 		    "after timeout %s != %s",
130 		    prmask(&nset, nbuf, sizeof(nbuf)),
131 		    prmask(&oset, obuf, sizeof(obuf)));
132 	_exit(0);
133 }
134 
135 ATF_TC(pselect_sigmask);
136 ATF_TC_HEAD(pselect_sigmask, tc)
137 {
138 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
139 	    "setting when a signal is received (PR lib/43625)");
140 }
141 
142 ATF_TC_BODY(pselect_sigmask, tc)
143 {
144 	pid_t pid;
145 	int status;
146 
147 	signal(SIGCHLD, sigchld);
148 
149 	switch (pid = fork()) {
150 	case 0:
151 		child(NULL);
152 		/*NOTREACHED*/
153 	case -1:
154 		err(1, "fork");
155 	default:
156 		sleep(1);
157 		if (kill(pid, SIGTERM) == -1)
158 			err(1, "kill");
159 		sleep(1);
160 		switch (waitpid(pid, &status, WNOHANG)) {
161 		case -1:
162 			err(1, "wait");
163 		case 0:
164 			if (kill(pid, SIGKILL) == -1)
165 				err(1, "kill");
166 			atf_tc_fail("pselect() did not receive signal");
167 			break;
168 		default:
169 			break;
170 		}
171 	}
172 }
173 
174 ATF_TC(pselect_timeout);
175 ATF_TC_HEAD(pselect_timeout, tc)
176 {
177 
178 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
179 	    "setting when a timeout occurs");
180 }
181 
182 ATF_TC_BODY(pselect_timeout, tc)
183 {
184 	pid_t pid;
185 	int status;
186 	static const struct timespec zero = { 0, 0 };
187 
188 	signal(SIGCHLD, sigchld);
189 
190 	switch (pid = fork()) {
191 	case 0:
192 		child(&zero);
193 		break;
194 	case -1:
195 		err(1, "fork");
196 	default:
197 		sleep(1);
198 		switch (waitpid(pid, &status, WNOHANG)) {
199 		case -1:
200 			err(1, "wait");
201 		case 0:
202 			if (kill(pid, SIGKILL) == -1)
203 				err(1, "kill");
204 			atf_tc_fail("pselect() did not receive signal");
205 			break;
206 		default:
207 			break;
208 		}
209 	}
210 }
211 
212 ATF_TC(select_badfd);
213 ATF_TC_HEAD(select_badfd, tc)
214 {
215 
216 	atf_tc_set_md_var(tc, "descr", "Checks select rejects bad fds");
217 }
218 
219 ATF_TC_BODY(select_badfd, tc)
220 {
221 	int fd;
222 
223 	for (fd = 0; fd < FD_SETSIZE; fd++) {
224 		fd_set readfds;
225 		int ret, error;
226 
227 		if (fcntl(fd, F_GETFL) != -1 || errno != EBADF)
228 			continue;
229 
230 		FD_ZERO(&readfds);
231 		FD_SET(fd, &readfds);
232 		alarm(5);
233 		errno = 0;
234 		ret = select(fd + 1, &readfds, NULL, NULL, NULL);
235 		error = errno;
236 		alarm(0);
237 		errno = error;
238 		ATF_CHECK_ERRNO(EBADF, ret == -1);
239 	}
240 }
241 
242 ATF_TP_ADD_TCS(tp)
243 {
244 
245 	ATF_TP_ADD_TC(tp, pselect_sigmask);
246 	ATF_TP_ADD_TC(tp, pselect_timeout);
247 	ATF_TP_ADD_TC(tp, select_badfd);
248 
249 	return atf_no_error();
250 }
251