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