xref: /netbsd/tests/lib/libc/sys/t_pollts.c (revision b8c2ae9c)
1 /*	$NetBSD: t_pollts.c,v 1.1 2020/07/17 15:34:17 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011, 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matthias Scheler.
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 <sys/time.h>
33 #include <sys/wait.h>
34 
35 #include <atf-c.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <poll.h>
40 #include <stdio.h>
41 #include <signal.h>
42 #include <unistd.h>
43 
44 #ifndef POLLTS
45 #define POLLTS pollts
46 #endif
47 
48 ATF_TC(basic);
ATF_TC_HEAD(basic,tc)49 ATF_TC_HEAD(basic, tc)
50 {
51 	atf_tc_set_md_var(tc, "timeout", "10");
52 	atf_tc_set_md_var(tc, "descr",
53 	    "Basis functionality test for ppoll(2)/pollts(2)");
54 }
55 
ATF_TC_BODY(basic,tc)56 ATF_TC_BODY(basic, tc)
57 {
58 	int fds[2];
59 	struct pollfd pfds[2];
60 	struct timespec timeout;
61 	int ret;
62 
63 	ATF_REQUIRE_EQ(pipe(fds), 0);
64 
65 	pfds[0].fd = fds[0];
66 	pfds[0].events = POLLIN;
67 	pfds[1].fd = fds[1];
68 	pfds[1].events = POLLOUT;
69 
70 	/* Use a timeout of 1 second. */
71 	timeout.tv_sec = 1;
72 	timeout.tv_nsec = 0;
73 
74 	/*
75 	 * Check that we get a timeout waiting for data on the read end
76 	 * of our pipe.
77 	 */
78 	pfds[0].revents = -1;
79 	pfds[1].revents = -1;
80 	ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfds[0], 1, &timeout, NULL), 0,
81 	    "got: %d", ret);
82 	ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
83 	ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
84 
85 	/* Check that the write end of the pipe as reported as ready. */
86 	pfds[0].revents = -1;
87 	pfds[1].revents = -1;
88 	ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfds[1], 1, &timeout, NULL), 1,
89 	    "got: %d", ret);
90 	ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
91 	ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
92 	    pfds[1].revents);
93 
94 	/* Check that only the write end of the pipe as reported as ready. */
95 	pfds[0].revents = -1;
96 	pfds[1].revents = -1;
97 	ATF_REQUIRE_EQ_MSG(ret = POLLTS(pfds, 2, &timeout, NULL), 1,
98 	    "got: %d", ret);
99 	ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
100 	ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
101 	    pfds[1].revents);
102 
103 	/* Write data to our pipe. */
104 	ATF_REQUIRE_EQ(write(fds[1], "", 1), 1);
105 
106 	/* Check that both ends of our pipe are reported as ready. */
107 	pfds[0].revents = -1;
108 	pfds[1].revents = -1;
109 	ATF_REQUIRE_EQ_MSG(ret = POLLTS(pfds, 2, &timeout, NULL), 2,
110 	    "got: %d", ret);
111 	ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
112 	    pfds[0].revents);
113 	ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
114 	    pfds[1].revents);
115 
116 	ATF_REQUIRE_EQ(close(fds[0]), 0);
117 	ATF_REQUIRE_EQ(close(fds[1]), 0);
118 }
119 
120 ATF_TC(err);
ATF_TC_HEAD(err,tc)121 ATF_TC_HEAD(err, tc)
122 {
123 	atf_tc_set_md_var(tc, "descr", "Check errors from ppoll(2)/pollts(2)");
124 }
125 
ATF_TC_BODY(err,tc)126 ATF_TC_BODY(err, tc)
127 {
128 	struct timespec timeout;
129 	struct pollfd pfd;
130 	int fd = 0;
131 
132 	pfd.fd = fd;
133 	pfd.events = POLLIN;
134 
135 	timeout.tv_sec = 1;
136 	timeout.tv_nsec = 0;
137 
138 	errno = 0;
139 	ATF_REQUIRE_ERRNO(EFAULT, POLLTS((void *)-1, 1, &timeout, NULL) == -1);
140 
141 	timeout.tv_sec = -1;
142 	timeout.tv_nsec = -1;
143 
144 	errno = 0;
145 	ATF_REQUIRE_ERRNO(EINVAL, POLLTS(&pfd, 1, &timeout, NULL) == -1);
146 }
147 
148 ATF_TC(sigmask);
ATF_TC_HEAD(sigmask,tc)149 ATF_TC_HEAD(sigmask, tc)
150 {
151 	atf_tc_set_md_var(tc, "timeout", "10");
152 	atf_tc_set_md_var(tc, "descr",
153 	    "Check that ppoll(2)/pollts(2) restores the signal mask (PR kern/44986)");
154 }
155 
ATF_TC_BODY(sigmask,tc)156 ATF_TC_BODY(sigmask, tc)
157 {
158 	int fd;
159 	struct pollfd pfd;
160 	struct timespec timeout;
161 	sigset_t mask;
162 	int ret;
163 
164 	fd = open(_PATH_DEVNULL, O_RDONLY);
165 	ATF_REQUIRE(fd >= 0);
166 
167 	pfd.fd = fd;
168 	pfd.events = POLLIN;
169 
170 	/* Use a timeout of 1 second. */
171 	timeout.tv_sec = 1;
172 	timeout.tv_nsec = 0;
173 
174 	/* Unblock all signals. */
175 	ATF_REQUIRE_EQ(sigfillset(&mask), 0);
176 	ATF_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &mask, NULL), 0);
177 
178 	/*
179 	 * Check that ppoll(2)/pollts(2) immediately returns. We block *all*
180 	 * signals during ppoll(2)/pollts(2).
181 	 */
182 	ATF_REQUIRE_EQ_MSG(ret = POLLTS(&pfd, 1, &timeout, &mask), 1,
183 	    "got: %d", ret);
184 
185 	/* Check that signals are now longer blocked. */
186 	ATF_REQUIRE_EQ(sigprocmask(SIG_SETMASK, NULL, &mask), 0);
187 	ATF_REQUIRE_EQ_MSG(sigismember(&mask, SIGUSR1), 0,
188 	    "signal mask was changed.");
189 
190 	ATF_REQUIRE_EQ(close(fd), 0);
191 }
192 
ATF_TP_ADD_TCS(tp)193 ATF_TP_ADD_TCS(tp)
194 {
195 
196 	ATF_TP_ADD_TC(tp, basic);
197 	ATF_TP_ADD_TC(tp, err);
198 	ATF_TP_ADD_TC(tp, sigmask);
199 
200 	return atf_no_error();
201 }
202