1*11be35a1SLionel Sambuc /* $NetBSD: t_poll.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 Foundation
8*11be35a1SLionel Sambuc * by Matthias Scheler.
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 <sys/time.h>
33*11be35a1SLionel Sambuc #include <sys/wait.h>
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <atf-c.h>
36*11be35a1SLionel Sambuc #include <errno.h>
37*11be35a1SLionel Sambuc #include <fcntl.h>
38*11be35a1SLionel Sambuc #include <paths.h>
39*11be35a1SLionel Sambuc #include <poll.h>
40*11be35a1SLionel Sambuc #include <stdio.h>
41*11be35a1SLionel Sambuc #include <signal.h>
42*11be35a1SLionel Sambuc #include <unistd.h>
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc static int desc;
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc static void
child1(void)47*11be35a1SLionel Sambuc child1(void)
48*11be35a1SLionel Sambuc {
49*11be35a1SLionel Sambuc struct pollfd pfd;
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc pfd.fd = desc;
52*11be35a1SLionel Sambuc pfd.events = POLLIN | POLLHUP | POLLOUT;
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc (void)poll(&pfd, 1, 2000);
55*11be35a1SLionel Sambuc (void)printf("child1 exit\n");
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc static void
child2(void)59*11be35a1SLionel Sambuc child2(void)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc struct pollfd pfd;
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc pfd.fd = desc;
64*11be35a1SLionel Sambuc pfd.events = POLLIN | POLLHUP | POLLOUT;
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc (void)sleep(1);
67*11be35a1SLionel Sambuc (void)poll(&pfd, 1, INFTIM);
68*11be35a1SLionel Sambuc (void)printf("child2 exit\n");
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc static void
child3(void)72*11be35a1SLionel Sambuc child3(void)
73*11be35a1SLionel Sambuc {
74*11be35a1SLionel Sambuc struct pollfd pfd;
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc (void)sleep(5);
77*11be35a1SLionel Sambuc
78*11be35a1SLionel Sambuc pfd.fd = desc;
79*11be35a1SLionel Sambuc pfd.events = POLLIN | POLLHUP | POLLOUT;
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc (void)poll(&pfd, 1, INFTIM);
82*11be35a1SLionel Sambuc (void)printf("child3 exit\n");
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc ATF_TC(poll_3way);
ATF_TC_HEAD(poll_3way,tc)86*11be35a1SLionel Sambuc ATF_TC_HEAD(poll_3way, tc)
87*11be35a1SLionel Sambuc {
88*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "15");
89*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
90*11be35a1SLionel Sambuc "Check for 3-way collision for descriptor. First child comes "
91*11be35a1SLionel Sambuc "and polls on descriptor, second child comes and polls, first "
92*11be35a1SLionel Sambuc "child times out and exits, third child comes and polls. When "
93*11be35a1SLionel Sambuc "the wakeup event happens, the two remaining children should "
94*11be35a1SLionel Sambuc "both be awaken. (kern/17517)");
95*11be35a1SLionel Sambuc }
96*11be35a1SLionel Sambuc
ATF_TC_BODY(poll_3way,tc)97*11be35a1SLionel Sambuc ATF_TC_BODY(poll_3way, tc)
98*11be35a1SLionel Sambuc {
99*11be35a1SLionel Sambuc int pf[2];
100*11be35a1SLionel Sambuc int status, i;
101*11be35a1SLionel Sambuc pid_t pid;
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc pipe(pf);
104*11be35a1SLionel Sambuc desc = pf[0];
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc pid = fork();
107*11be35a1SLionel Sambuc ATF_REQUIRE(pid >= 0);
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc if (pid == 0) {
110*11be35a1SLionel Sambuc (void)close(pf[1]);
111*11be35a1SLionel Sambuc child1();
112*11be35a1SLionel Sambuc _exit(0);
113*11be35a1SLionel Sambuc /* NOTREACHED */
114*11be35a1SLionel Sambuc }
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc pid = fork();
117*11be35a1SLionel Sambuc ATF_REQUIRE(pid >= 0);
118*11be35a1SLionel Sambuc
119*11be35a1SLionel Sambuc if (pid == 0) {
120*11be35a1SLionel Sambuc (void)close(pf[1]);
121*11be35a1SLionel Sambuc child2();
122*11be35a1SLionel Sambuc _exit(0);
123*11be35a1SLionel Sambuc /* NOTREACHED */
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc pid = fork();
127*11be35a1SLionel Sambuc ATF_REQUIRE( pid >= 0);
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc if (pid == 0) {
130*11be35a1SLionel Sambuc (void)close(pf[1]);
131*11be35a1SLionel Sambuc child3();
132*11be35a1SLionel Sambuc _exit(0);
133*11be35a1SLionel Sambuc /* NOTREACHED */
134*11be35a1SLionel Sambuc }
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc (void)sleep(10);
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc (void)printf("parent write\n");
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc ATF_REQUIRE(write(pf[1], "konec\n", 6) == 6);
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc for(i = 0; i < 3; ++i)
143*11be35a1SLionel Sambuc (void)wait(&status);
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc (void)printf("parent terminated\n");
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc ATF_TC(poll_basic);
ATF_TC_HEAD(poll_basic,tc)149*11be35a1SLionel Sambuc ATF_TC_HEAD(poll_basic, tc)
150*11be35a1SLionel Sambuc {
151*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "10");
152*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
153*11be35a1SLionel Sambuc "Basis functionality test for poll(2)");
154*11be35a1SLionel Sambuc }
155*11be35a1SLionel Sambuc
ATF_TC_BODY(poll_basic,tc)156*11be35a1SLionel Sambuc ATF_TC_BODY(poll_basic, tc)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc int fds[2];
159*11be35a1SLionel Sambuc struct pollfd pfds[2];
160*11be35a1SLionel Sambuc int ret;
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(pipe(fds), 0);
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc pfds[0].fd = fds[0];
165*11be35a1SLionel Sambuc pfds[0].events = POLLIN;
166*11be35a1SLionel Sambuc pfds[1].fd = fds[1];
167*11be35a1SLionel Sambuc pfds[1].events = POLLOUT;
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc /*
170*11be35a1SLionel Sambuc * Check that we get a timeout waiting for data on the read end
171*11be35a1SLionel Sambuc * of our pipe.
172*11be35a1SLionel Sambuc */
173*11be35a1SLionel Sambuc pfds[0].revents = -1;
174*11be35a1SLionel Sambuc pfds[1].revents = -1;
175*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = poll(&pfds[0], 1, 1), 0,
176*11be35a1SLionel Sambuc "got: %d", ret);
177*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
178*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc /* Check that the write end of the pipe as reported as ready. */
181*11be35a1SLionel Sambuc pfds[0].revents = -1;
182*11be35a1SLionel Sambuc pfds[1].revents = -1;
183*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = poll(&pfds[1], 1, 1), 1,
184*11be35a1SLionel Sambuc "got: %d", ret);
185*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
186*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
187*11be35a1SLionel Sambuc pfds[1].revents);
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambuc /* Check that only the write end of the pipe as reported as ready. */
190*11be35a1SLionel Sambuc pfds[0].revents = -1;
191*11be35a1SLionel Sambuc pfds[1].revents = -1;
192*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = poll(pfds, 2, 1), 1,
193*11be35a1SLionel Sambuc "got: %d", ret);
194*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
195*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
196*11be35a1SLionel Sambuc pfds[1].revents);
197*11be35a1SLionel Sambuc
198*11be35a1SLionel Sambuc /* Write data to our pipe. */
199*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(write(fds[1], "", 1), 1);
200*11be35a1SLionel Sambuc
201*11be35a1SLionel Sambuc /* Check that both ends of our pipe are reported as ready. */
202*11be35a1SLionel Sambuc pfds[0].revents = -1;
203*11be35a1SLionel Sambuc pfds[1].revents = -1;
204*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = poll(pfds, 2, 1), 2,
205*11be35a1SLionel Sambuc "got: %d", ret);
206*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
207*11be35a1SLionel Sambuc pfds[0].revents);
208*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
209*11be35a1SLionel Sambuc pfds[1].revents);
210*11be35a1SLionel Sambuc
211*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(close(fds[0]), 0);
212*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(close(fds[1]), 0);
213*11be35a1SLionel Sambuc }
214*11be35a1SLionel Sambuc
215*11be35a1SLionel Sambuc ATF_TC(poll_err);
ATF_TC_HEAD(poll_err,tc)216*11be35a1SLionel Sambuc ATF_TC_HEAD(poll_err, tc)
217*11be35a1SLionel Sambuc {
218*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Check errors from poll(2)");
219*11be35a1SLionel Sambuc }
220*11be35a1SLionel Sambuc
ATF_TC_BODY(poll_err,tc)221*11be35a1SLionel Sambuc ATF_TC_BODY(poll_err, tc)
222*11be35a1SLionel Sambuc {
223*11be35a1SLionel Sambuc struct pollfd pfd;
224*11be35a1SLionel Sambuc int fd = 0;
225*11be35a1SLionel Sambuc
226*11be35a1SLionel Sambuc pfd.fd = fd;
227*11be35a1SLionel Sambuc pfd.events = POLLIN;
228*11be35a1SLionel Sambuc
229*11be35a1SLionel Sambuc errno = 0;
230*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFAULT, poll((struct pollfd *)-1, 1, -1) == -1);
231*11be35a1SLionel Sambuc
232*11be35a1SLionel Sambuc errno = 0;
233*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EINVAL, poll(&pfd, 1, -2) == -1);
234*11be35a1SLionel Sambuc }
235*11be35a1SLionel Sambuc
236*11be35a1SLionel Sambuc ATF_TC(pollts_basic);
ATF_TC_HEAD(pollts_basic,tc)237*11be35a1SLionel Sambuc ATF_TC_HEAD(pollts_basic, tc)
238*11be35a1SLionel Sambuc {
239*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "10");
240*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
241*11be35a1SLionel Sambuc "Basis functionality test for pollts(2)");
242*11be35a1SLionel Sambuc }
243*11be35a1SLionel Sambuc
ATF_TC_BODY(pollts_basic,tc)244*11be35a1SLionel Sambuc ATF_TC_BODY(pollts_basic, tc)
245*11be35a1SLionel Sambuc {
246*11be35a1SLionel Sambuc int fds[2];
247*11be35a1SLionel Sambuc struct pollfd pfds[2];
248*11be35a1SLionel Sambuc struct timespec timeout;
249*11be35a1SLionel Sambuc int ret;
250*11be35a1SLionel Sambuc
251*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(pipe(fds), 0);
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc pfds[0].fd = fds[0];
254*11be35a1SLionel Sambuc pfds[0].events = POLLIN;
255*11be35a1SLionel Sambuc pfds[1].fd = fds[1];
256*11be35a1SLionel Sambuc pfds[1].events = POLLOUT;
257*11be35a1SLionel Sambuc
258*11be35a1SLionel Sambuc /* Use a timeout of 1 second. */
259*11be35a1SLionel Sambuc timeout.tv_sec = 1;
260*11be35a1SLionel Sambuc timeout.tv_nsec = 0;
261*11be35a1SLionel Sambuc
262*11be35a1SLionel Sambuc /*
263*11be35a1SLionel Sambuc * Check that we get a timeout waiting for data on the read end
264*11be35a1SLionel Sambuc * of our pipe.
265*11be35a1SLionel Sambuc */
266*11be35a1SLionel Sambuc pfds[0].revents = -1;
267*11be35a1SLionel Sambuc pfds[1].revents = -1;
268*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = pollts(&pfds[0], 1, &timeout, NULL), 0,
269*11be35a1SLionel Sambuc "got: %d", ret);
270*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
271*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
272*11be35a1SLionel Sambuc
273*11be35a1SLionel Sambuc /* Check that the write end of the pipe as reported as ready. */
274*11be35a1SLionel Sambuc pfds[0].revents = -1;
275*11be35a1SLionel Sambuc pfds[1].revents = -1;
276*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = pollts(&pfds[1], 1, &timeout, NULL), 1,
277*11be35a1SLionel Sambuc "got: %d", ret);
278*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
279*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
280*11be35a1SLionel Sambuc pfds[1].revents);
281*11be35a1SLionel Sambuc
282*11be35a1SLionel Sambuc /* Check that only the write end of the pipe as reported as ready. */
283*11be35a1SLionel Sambuc pfds[0].revents = -1;
284*11be35a1SLionel Sambuc pfds[1].revents = -1;
285*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = pollts(pfds, 2, &timeout, NULL), 1,
286*11be35a1SLionel Sambuc "got: %d", ret);
287*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
288*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
289*11be35a1SLionel Sambuc pfds[1].revents);
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc /* Write data to our pipe. */
292*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(write(fds[1], "", 1), 1);
293*11be35a1SLionel Sambuc
294*11be35a1SLionel Sambuc /* Check that both ends of our pipe are reported as ready. */
295*11be35a1SLionel Sambuc pfds[0].revents = -1;
296*11be35a1SLionel Sambuc pfds[1].revents = -1;
297*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = pollts(pfds, 2, &timeout, NULL), 2,
298*11be35a1SLionel Sambuc "got: %d", ret);
299*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
300*11be35a1SLionel Sambuc pfds[0].revents);
301*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
302*11be35a1SLionel Sambuc pfds[1].revents);
303*11be35a1SLionel Sambuc
304*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(close(fds[0]), 0);
305*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(close(fds[1]), 0);
306*11be35a1SLionel Sambuc }
307*11be35a1SLionel Sambuc
308*11be35a1SLionel Sambuc ATF_TC(pollts_err);
ATF_TC_HEAD(pollts_err,tc)309*11be35a1SLionel Sambuc ATF_TC_HEAD(pollts_err, tc)
310*11be35a1SLionel Sambuc {
311*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Check errors from pollts(2)");
312*11be35a1SLionel Sambuc }
313*11be35a1SLionel Sambuc
ATF_TC_BODY(pollts_err,tc)314*11be35a1SLionel Sambuc ATF_TC_BODY(pollts_err, tc)
315*11be35a1SLionel Sambuc {
316*11be35a1SLionel Sambuc struct timespec timeout;
317*11be35a1SLionel Sambuc struct pollfd pfd;
318*11be35a1SLionel Sambuc int fd = 0;
319*11be35a1SLionel Sambuc
320*11be35a1SLionel Sambuc pfd.fd = fd;
321*11be35a1SLionel Sambuc pfd.events = POLLIN;
322*11be35a1SLionel Sambuc
323*11be35a1SLionel Sambuc timeout.tv_sec = 1;
324*11be35a1SLionel Sambuc timeout.tv_nsec = 0;
325*11be35a1SLionel Sambuc
326*11be35a1SLionel Sambuc errno = 0;
327*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EFAULT, pollts((void *)-1, 1, &timeout, NULL) == -1);
328*11be35a1SLionel Sambuc
329*11be35a1SLionel Sambuc timeout.tv_sec = -1;
330*11be35a1SLionel Sambuc timeout.tv_nsec = -1;
331*11be35a1SLionel Sambuc
332*11be35a1SLionel Sambuc errno = 0;
333*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EINVAL, pollts(&pfd, 1, &timeout, NULL) == -1);
334*11be35a1SLionel Sambuc }
335*11be35a1SLionel Sambuc
336*11be35a1SLionel Sambuc ATF_TC(pollts_sigmask);
ATF_TC_HEAD(pollts_sigmask,tc)337*11be35a1SLionel Sambuc ATF_TC_HEAD(pollts_sigmask, tc)
338*11be35a1SLionel Sambuc {
339*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "10");
340*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
341*11be35a1SLionel Sambuc "Check that pollts(2) restores the signal mask (PR kern/44986)");
342*11be35a1SLionel Sambuc }
343*11be35a1SLionel Sambuc
ATF_TC_BODY(pollts_sigmask,tc)344*11be35a1SLionel Sambuc ATF_TC_BODY(pollts_sigmask, tc)
345*11be35a1SLionel Sambuc {
346*11be35a1SLionel Sambuc int fd;
347*11be35a1SLionel Sambuc struct pollfd pfd;
348*11be35a1SLionel Sambuc struct timespec timeout;
349*11be35a1SLionel Sambuc sigset_t mask;
350*11be35a1SLionel Sambuc int ret;
351*11be35a1SLionel Sambuc
352*11be35a1SLionel Sambuc fd = open(_PATH_DEVNULL, O_RDONLY);
353*11be35a1SLionel Sambuc ATF_REQUIRE(fd >= 0);
354*11be35a1SLionel Sambuc
355*11be35a1SLionel Sambuc pfd.fd = fd;
356*11be35a1SLionel Sambuc pfd.events = POLLIN;
357*11be35a1SLionel Sambuc
358*11be35a1SLionel Sambuc /* Use a timeout of 1 second. */
359*11be35a1SLionel Sambuc timeout.tv_sec = 1;
360*11be35a1SLionel Sambuc timeout.tv_nsec = 0;
361*11be35a1SLionel Sambuc
362*11be35a1SLionel Sambuc /* Unblock all signals. */
363*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(sigfillset(&mask), 0);
364*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &mask, NULL), 0);
365*11be35a1SLionel Sambuc
366*11be35a1SLionel Sambuc /*
367*11be35a1SLionel Sambuc * Check that pollts(2) immediately returns. We block *all*
368*11be35a1SLionel Sambuc * signals during pollts(2).
369*11be35a1SLionel Sambuc */
370*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(ret = pollts(&pfd, 1, &timeout, &mask), 1,
371*11be35a1SLionel Sambuc "got: %d", ret);
372*11be35a1SLionel Sambuc
373*11be35a1SLionel Sambuc /* Check that signals are now longer blocked. */
374*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(sigprocmask(SIG_SETMASK, NULL, &mask), 0);
375*11be35a1SLionel Sambuc ATF_REQUIRE_EQ_MSG(sigismember(&mask, SIGUSR1), 0,
376*11be35a1SLionel Sambuc "signal mask was changed.");
377*11be35a1SLionel Sambuc
378*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(close(fd), 0);
379*11be35a1SLionel Sambuc }
380*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)381*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
382*11be35a1SLionel Sambuc {
383*11be35a1SLionel Sambuc
384*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, poll_3way);
385*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, poll_basic);
386*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, poll_err);
387*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, pollts_basic);
388*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, pollts_err);
389*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, pollts_sigmask);
390*11be35a1SLionel Sambuc
391*11be35a1SLionel Sambuc return atf_no_error();
392*11be35a1SLionel Sambuc }
393