xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_select.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 }
139 
140 ATF_TC(pselect_sigmask);
141 ATF_TC_HEAD(pselect_sigmask, tc)
142 {
143 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
144 	    "setting when a signal is received (PR lib/43625)");
145 }
146 
147 ATF_TC_BODY(pselect_sigmask, tc)
148 {
149 	pid_t pid;
150 	int status;
151 
152 	signal(SIGCHLD, sigchld);
153 
154 	switch (pid = fork()) {
155 	case 0:
156 		child(NULL);
157 	case -1:
158 		err(1, "fork");
159 	default:
160 		sleep(1);
161 		if (kill(pid, SIGTERM) == -1)
162 			err(1, "kill");
163 		sleep(1);
164 		switch (waitpid(pid, &status, WNOHANG)) {
165 		case -1:
166 			err(1, "wait");
167 		case 0:
168 			if (kill(pid, SIGKILL) == -1)
169 				err(1, "kill");
170 			atf_tc_fail("pselect() did not receive signal");
171 			break;
172 		default:
173 			break;
174 		}
175 	}
176 }
177 
178 ATF_TC(pselect_timeout);
179 ATF_TC_HEAD(pselect_timeout, tc)
180 {
181 
182 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
183 	    "setting when a timeout occurs");
184 }
185 
186 ATF_TC_BODY(pselect_timeout, tc)
187 {
188 	pid_t pid;
189 	int status;
190 	static const struct timespec zero = { 0, 0 };
191 
192 	signal(SIGCHLD, sigchld);
193 
194 	switch (pid = fork()) {
195 	case 0:
196 		child(&zero);
197 		break;
198 	case -1:
199 		err(1, "fork");
200 	default:
201 		sleep(1);
202 		switch (waitpid(pid, &status, WNOHANG)) {
203 		case -1:
204 			err(1, "wait");
205 		case 0:
206 			if (kill(pid, SIGKILL) == -1)
207 				err(1, "kill");
208 			atf_tc_fail("pselect() did not receive signal");
209 			break;
210 		default:
211 			break;
212 		}
213 	}
214 }
215 
216 ATF_TP_ADD_TCS(tp)
217 {
218 
219 	ATF_TP_ADD_TC(tp, pselect_sigmask);
220 	ATF_TP_ADD_TC(tp, pselect_timeout);
221 
222 	return atf_no_error();
223 }
224