xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_select.c (revision 8b25e8410533a6e69cceff910546b2dc485a5059)
1 /*	$NetBSD: t_select.c,v 1.3 2012/03/18 07:00:52 jruoho 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 <assert.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
35 #include <sys/wait.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 
45 #include <atf-c.h>
46 
47 static sig_atomic_t keep_going = 1;
48 
49 static void
50 #ifdef __FreeBSD__
51 sig_handler(int signum __unused)
52 #else
53 sig_handler(int signum)
54 #endif
55 {
56 	keep_going = 0;
57 }
58 
59 static void
60 #ifdef __FreeBSD__
61 sigchld(int signum __unused)
62 #else
63 sigchld(int signum)
64 #endif
65 {
66 }
67 
68 static char
69 xtoa(uint8_t n)
70 {
71 	static const char xarray[] = "0123456789abcdef";
72 	assert(n < sizeof(xarray));
73 	return xarray[n];
74 }
75 
76 static const char *
77 prmask(const sigset_t *m, char *buf, size_t len)
78 {
79 	size_t j = 2;
80 	assert(len >= 3 + sizeof(*m));
81 	buf[0] = '0';
82 	buf[1] = 'x';
83 #define N(p, a)	(((p) >> ((a) * 4)) & 0xf)
84 	for (size_t i = __arraycount(m->__bits); i > 0; i--) {
85 		uint32_t p = m->__bits[i - 1];
86 		for (size_t k = sizeof(p); k > 0; k--)
87 			buf[j++] = xtoa(N(p, k - 1));
88 	}
89 	buf[j] = '\0';
90 	return buf;
91 }
92 
93 static void
94 child(const struct timespec *ts)
95 {
96 	struct sigaction sa;
97 	sigset_t set, oset, nset;
98 	char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3];
99 	int fd;
100 
101 	memset(&sa, 0, sizeof(sa));
102 	sa.sa_handler = sig_handler;
103 	if ((fd = open("/dev/null", O_RDONLY)) == -1)
104 		err(1, "open");
105 
106 	if (sigaction(SIGTERM, &sa, NULL) == -1)
107 		err(1, "sigaction");
108 
109 	sigfillset(&set);
110 	if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
111 		err(1, "sigprocmask");
112 
113 	if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
114 		err(1, "sigprocmask");
115 
116 	sigemptyset(&set);
117 
118 	for (;;) {
119 		fd_set rset;
120 		FD_ZERO(&rset);
121 		FD_SET(fd, &rset);
122 		if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) {
123 			if(errno == EINTR) {
124 				if (!keep_going)
125 					break;
126 			}
127 		}
128 		if (ts)
129 			break;
130 	}
131 	if (sigprocmask(SIG_BLOCK, NULL, &nset) == -1)
132 		err(1, "sigprocmask");
133 	if (memcmp(&oset, &nset, sizeof(oset)) != 0)
134 		atf_tc_fail("pselect() masks don't match "
135 		    "after timeout %s != %s",
136 		    prmask(&nset, nbuf, sizeof(nbuf)),
137 		    prmask(&oset, obuf, sizeof(obuf)));
138 #ifdef	__FreeBSD__
139 	_exit(0);
140 #endif
141 }
142 
143 ATF_TC(pselect_sigmask);
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 
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 #ifdef	__FreeBSD__
161 		break;
162 #endif
163 	case -1:
164 		err(1, "fork");
165 	default:
166 		sleep(1);
167 		if (kill(pid, SIGTERM) == -1)
168 			err(1, "kill");
169 		sleep(1);
170 		switch (waitpid(pid, &status, WNOHANG)) {
171 		case -1:
172 			err(1, "wait");
173 		case 0:
174 			if (kill(pid, SIGKILL) == -1)
175 				err(1, "kill");
176 			atf_tc_fail("pselect() did not receive signal");
177 			break;
178 		default:
179 			break;
180 		}
181 	}
182 }
183 
184 ATF_TC(pselect_timeout);
185 ATF_TC_HEAD(pselect_timeout, tc)
186 {
187 
188 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
189 	    "setting when a timeout occurs");
190 }
191 
192 ATF_TC_BODY(pselect_timeout, tc)
193 {
194 	pid_t pid;
195 	int status;
196 	static const struct timespec zero = { 0, 0 };
197 
198 	signal(SIGCHLD, sigchld);
199 
200 	switch (pid = fork()) {
201 	case 0:
202 		child(&zero);
203 		break;
204 	case -1:
205 		err(1, "fork");
206 	default:
207 		sleep(1);
208 		switch (waitpid(pid, &status, WNOHANG)) {
209 		case -1:
210 			err(1, "wait");
211 		case 0:
212 			if (kill(pid, SIGKILL) == -1)
213 				err(1, "kill");
214 			atf_tc_fail("pselect() did not receive signal");
215 			break;
216 		default:
217 			break;
218 		}
219 	}
220 }
221 
222 ATF_TP_ADD_TCS(tp)
223 {
224 
225 	ATF_TP_ADD_TC(tp, pselect_sigmask);
226 	ATF_TP_ADD_TC(tp, pselect_timeout);
227 
228 	return atf_no_error();
229 }
230