xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_select.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*	$NetBSD: t_select.c,v 1.4 2017/01/13 21:18:33 christos 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 sig_handler(int signum __unused)
51 {
52 	keep_going = 0;
53 }
54 
55 static void
56 sigchld(int signum __unused)
57 {
58 }
59 
60 static char
61 xtoa(uint8_t n)
62 {
63 	static const char xarray[] = "0123456789abcdef";
64 	assert(n < sizeof(xarray));
65 	return xarray[n];
66 }
67 
68 static const char *
69 prmask(const sigset_t *m, char *buf, size_t len)
70 {
71 	size_t j = 2;
72 	assert(len >= 3 + sizeof(*m));
73 	buf[0] = '0';
74 	buf[1] = 'x';
75 #define N(p, a)	(((p) >> ((a) * 4)) & 0xf)
76 	for (size_t i = __arraycount(m->__bits); i > 0; i--) {
77 		uint32_t p = m->__bits[i - 1];
78 		for (size_t k = sizeof(p); k > 0; k--)
79 			buf[j++] = xtoa(N(p, k - 1));
80 	}
81 	buf[j] = '\0';
82 	return buf;
83 }
84 
85 static __dead void
86 child(const struct timespec *ts)
87 {
88 	struct sigaction sa;
89 	sigset_t set, oset, nset;
90 	char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3];
91 	int fd;
92 
93 	memset(&sa, 0, sizeof(sa));
94 	sa.sa_handler = sig_handler;
95 	if ((fd = open("/dev/null", O_RDONLY)) == -1)
96 		err(1, "open");
97 
98 	if (sigaction(SIGTERM, &sa, NULL) == -1)
99 		err(1, "sigaction");
100 
101 	sigfillset(&set);
102 	if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
103 		err(1, "sigprocmask");
104 
105 	if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
106 		err(1, "sigprocmask");
107 
108 	sigemptyset(&set);
109 
110 	for (;;) {
111 		fd_set rset;
112 		FD_ZERO(&rset);
113 		FD_SET(fd, &rset);
114 		if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) {
115 			if(errno == EINTR) {
116 				if (!keep_going)
117 					break;
118 			}
119 		}
120 		if (ts)
121 			break;
122 	}
123 	if (sigprocmask(SIG_BLOCK, NULL, &nset) == -1)
124 		err(1, "sigprocmask");
125 	if (memcmp(&oset, &nset, sizeof(oset)) != 0)
126 		atf_tc_fail("pselect() masks don't match "
127 		    "after timeout %s != %s",
128 		    prmask(&nset, nbuf, sizeof(nbuf)),
129 		    prmask(&oset, obuf, sizeof(obuf)));
130 	_exit(0);
131 }
132 
133 ATF_TC(pselect_sigmask);
134 ATF_TC_HEAD(pselect_sigmask, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
137 	    "setting when a signal is received (PR lib/43625)");
138 }
139 
140 ATF_TC_BODY(pselect_sigmask, tc)
141 {
142 	pid_t pid;
143 	int status;
144 
145 	signal(SIGCHLD, sigchld);
146 
147 	switch (pid = fork()) {
148 	case 0:
149 		child(NULL);
150 		/*NOTREACHED*/
151 	case -1:
152 		err(1, "fork");
153 	default:
154 		sleep(1);
155 		if (kill(pid, SIGTERM) == -1)
156 			err(1, "kill");
157 		sleep(1);
158 		switch (waitpid(pid, &status, WNOHANG)) {
159 		case -1:
160 			err(1, "wait");
161 		case 0:
162 			if (kill(pid, SIGKILL) == -1)
163 				err(1, "kill");
164 			atf_tc_fail("pselect() did not receive signal");
165 			break;
166 		default:
167 			break;
168 		}
169 	}
170 }
171 
172 ATF_TC(pselect_timeout);
173 ATF_TC_HEAD(pselect_timeout, tc)
174 {
175 
176 	atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
177 	    "setting when a timeout occurs");
178 }
179 
180 ATF_TC_BODY(pselect_timeout, tc)
181 {
182 	pid_t pid;
183 	int status;
184 	static const struct timespec zero = { 0, 0 };
185 
186 	signal(SIGCHLD, sigchld);
187 
188 	switch (pid = fork()) {
189 	case 0:
190 		child(&zero);
191 		break;
192 	case -1:
193 		err(1, "fork");
194 	default:
195 		sleep(1);
196 		switch (waitpid(pid, &status, WNOHANG)) {
197 		case -1:
198 			err(1, "wait");
199 		case 0:
200 			if (kill(pid, SIGKILL) == -1)
201 				err(1, "kill");
202 			atf_tc_fail("pselect() did not receive signal");
203 			break;
204 		default:
205 			break;
206 		}
207 	}
208 }
209 
210 ATF_TP_ADD_TCS(tp)
211 {
212 
213 	ATF_TP_ADD_TC(tp, pselect_sigmask);
214 	ATF_TP_ADD_TC(tp, pselect_timeout);
215 
216 	return atf_no_error();
217 }
218