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