1 /* $NetBSD: t_select.c,v 1.3 2012/03/18 07:00:52 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundatiom
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/types.h>
34 #include <sys/select.h>
47 static sig_atomic_t keep_going = 1;
51 sig_handler(int signum __unused)
53 sig_handler(int signum)
61 sigchld(int signum __unused)
71 static const char xarray[] = "0123456789abcdef";
72 assert(n < sizeof(xarray));
77 prmask(const sigset_t *m, char *buf, size_t len)
80 assert(len >= 3 + sizeof(*m));
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));
94 child(const struct timespec *ts)
97 sigset_t set, oset, nset;
98 char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3];
101 memset(&sa, 0, sizeof(sa));
102 sa.sa_handler = sig_handler;
103 if ((fd = open("/dev/null", O_RDONLY)) == -1)
106 if (sigaction(SIGTERM, &sa, NULL) == -1)
110 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1)
111 err(1, "sigprocmask");
113 if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1)
114 err(1, "sigprocmask");
122 if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) {
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)));
140 ATF_TC(pselect_sigmask);
141 ATF_TC_HEAD(pselect_sigmask, tc)
143 atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
144 "setting when a signal is received (PR lib/43625)");
147 ATF_TC_BODY(pselect_sigmask, tc)
152 signal(SIGCHLD, sigchld);
154 switch (pid = fork()) {
161 if (kill(pid, SIGTERM) == -1)
164 switch (waitpid(pid, &status, WNOHANG)) {
168 if (kill(pid, SIGKILL) == -1)
170 atf_tc_fail("pselect() did not receive signal");
178 ATF_TC(pselect_timeout);
179 ATF_TC_HEAD(pselect_timeout, tc)
182 atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask "
183 "setting when a timeout occurs");
186 ATF_TC_BODY(pselect_timeout, tc)
190 static const struct timespec zero = { 0, 0 };
192 signal(SIGCHLD, sigchld);
194 switch (pid = fork()) {
202 switch (waitpid(pid, &status, WNOHANG)) {
206 if (kill(pid, SIGKILL) == -1)
208 atf_tc_fail("pselect() did not receive signal");
219 ATF_TP_ADD_TC(tp, pselect_sigmask);
220 ATF_TP_ADD_TC(tp, pselect_timeout);
222 return atf_no_error();