t_poll.c revision 1.10 1 1.10 riastrad /* $NetBSD: t_poll.c,v 1.10 2025/02/09 17:10:23 riastradh Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*-
4 1.1 jruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jruoho * by Matthias Scheler.
9 1.1 jruoho *
10 1.1 jruoho * Redistribution and use in source and binary forms, with or without
11 1.1 jruoho * modification, are permitted provided that the following conditions
12 1.1 jruoho * are met:
13 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
14 1.1 jruoho * notice, this list of conditions and the following disclaimer.
15 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
17 1.1 jruoho * documentation and/or other materials provided with the distribution.
18 1.1 jruoho *
19 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jruoho */
31 1.1 jruoho
32 1.5 thorpej #include <sys/stat.h>
33 1.1 jruoho #include <sys/time.h>
34 1.2 jruoho #include <sys/wait.h>
35 1.1 jruoho
36 1.1 jruoho #include <atf-c.h>
37 1.1 jruoho #include <errno.h>
38 1.1 jruoho #include <fcntl.h>
39 1.1 jruoho #include <paths.h>
40 1.1 jruoho #include <poll.h>
41 1.2 jruoho #include <stdio.h>
42 1.7 thorpej #include <stdlib.h>
43 1.1 jruoho #include <signal.h>
44 1.1 jruoho #include <unistd.h>
45 1.1 jruoho
46 1.2 jruoho static int desc;
47 1.2 jruoho
48 1.2 jruoho static void
49 1.2 jruoho child1(void)
50 1.2 jruoho {
51 1.2 jruoho struct pollfd pfd;
52 1.2 jruoho
53 1.2 jruoho pfd.fd = desc;
54 1.2 jruoho pfd.events = POLLIN | POLLHUP | POLLOUT;
55 1.2 jruoho
56 1.2 jruoho (void)poll(&pfd, 1, 2000);
57 1.2 jruoho (void)printf("child1 exit\n");
58 1.2 jruoho }
59 1.2 jruoho
60 1.2 jruoho static void
61 1.2 jruoho child2(void)
62 1.2 jruoho {
63 1.2 jruoho struct pollfd pfd;
64 1.2 jruoho
65 1.2 jruoho pfd.fd = desc;
66 1.2 jruoho pfd.events = POLLIN | POLLHUP | POLLOUT;
67 1.2 jruoho
68 1.2 jruoho (void)sleep(1);
69 1.2 jruoho (void)poll(&pfd, 1, INFTIM);
70 1.2 jruoho (void)printf("child2 exit\n");
71 1.2 jruoho }
72 1.2 jruoho
73 1.2 jruoho static void
74 1.2 jruoho child3(void)
75 1.2 jruoho {
76 1.2 jruoho struct pollfd pfd;
77 1.2 jruoho
78 1.2 jruoho (void)sleep(5);
79 1.2 jruoho
80 1.2 jruoho pfd.fd = desc;
81 1.2 jruoho pfd.events = POLLIN | POLLHUP | POLLOUT;
82 1.2 jruoho
83 1.2 jruoho (void)poll(&pfd, 1, INFTIM);
84 1.2 jruoho (void)printf("child3 exit\n");
85 1.2 jruoho }
86 1.2 jruoho
87 1.4 kamil ATF_TC(3way);
88 1.4 kamil ATF_TC_HEAD(3way, tc)
89 1.2 jruoho {
90 1.2 jruoho atf_tc_set_md_var(tc, "timeout", "15");
91 1.2 jruoho atf_tc_set_md_var(tc, "descr",
92 1.2 jruoho "Check for 3-way collision for descriptor. First child comes "
93 1.2 jruoho "and polls on descriptor, second child comes and polls, first "
94 1.2 jruoho "child times out and exits, third child comes and polls. When "
95 1.2 jruoho "the wakeup event happens, the two remaining children should "
96 1.2 jruoho "both be awaken. (kern/17517)");
97 1.2 jruoho }
98 1.2 jruoho
99 1.4 kamil ATF_TC_BODY(3way, tc)
100 1.2 jruoho {
101 1.2 jruoho int pf[2];
102 1.2 jruoho int status, i;
103 1.2 jruoho pid_t pid;
104 1.10 riastrad ssize_t nwrit;
105 1.2 jruoho
106 1.10 riastrad RL(pipe(pf));
107 1.2 jruoho desc = pf[0];
108 1.2 jruoho
109 1.10 riastrad RL(pid = fork());
110 1.2 jruoho if (pid == 0) {
111 1.10 riastrad if (close(pf[1]) == -1)
112 1.10 riastrad _exit(1);
113 1.2 jruoho child1();
114 1.2 jruoho _exit(0);
115 1.2 jruoho /* NOTREACHED */
116 1.2 jruoho }
117 1.2 jruoho
118 1.10 riastrad RL(pid = fork());
119 1.2 jruoho if (pid == 0) {
120 1.10 riastrad if (close(pf[1]) == -1)
121 1.10 riastrad _exit(1);
122 1.2 jruoho child2();
123 1.2 jruoho _exit(0);
124 1.2 jruoho /* NOTREACHED */
125 1.2 jruoho }
126 1.2 jruoho
127 1.10 riastrad RL(pid = fork());
128 1.2 jruoho if (pid == 0) {
129 1.10 riastrad if (close(pf[1]) == -1)
130 1.10 riastrad _exit(1);
131 1.2 jruoho child3();
132 1.2 jruoho _exit(0);
133 1.2 jruoho /* NOTREACHED */
134 1.2 jruoho }
135 1.2 jruoho
136 1.2 jruoho (void)sleep(10);
137 1.2 jruoho
138 1.2 jruoho (void)printf("parent write\n");
139 1.2 jruoho
140 1.10 riastrad RL(nwrit = write(pf[1], "konec\n", 6));
141 1.10 riastrad ATF_REQUIRE_EQ_MSG(nwrit, 6, "nwrit=%zd", nwrit);
142 1.2 jruoho
143 1.10 riastrad for (i = 0; i < 3; i++)
144 1.10 riastrad RL(wait(&status));
145 1.2 jruoho
146 1.2 jruoho (void)printf("parent terminated\n");
147 1.2 jruoho }
148 1.2 jruoho
149 1.4 kamil ATF_TC(basic);
150 1.4 kamil ATF_TC_HEAD(basic, tc)
151 1.1 jruoho {
152 1.1 jruoho atf_tc_set_md_var(tc, "timeout", "10");
153 1.1 jruoho atf_tc_set_md_var(tc, "descr",
154 1.1 jruoho "Basis functionality test for poll(2)");
155 1.1 jruoho }
156 1.1 jruoho
157 1.4 kamil ATF_TC_BODY(basic, tc)
158 1.1 jruoho {
159 1.1 jruoho int fds[2];
160 1.1 jruoho struct pollfd pfds[2];
161 1.1 jruoho int ret;
162 1.10 riastrad ssize_t nwrit;
163 1.1 jruoho
164 1.10 riastrad RL(pipe(fds));
165 1.1 jruoho
166 1.1 jruoho pfds[0].fd = fds[0];
167 1.1 jruoho pfds[0].events = POLLIN;
168 1.1 jruoho pfds[1].fd = fds[1];
169 1.1 jruoho pfds[1].events = POLLOUT;
170 1.1 jruoho
171 1.1 jruoho /*
172 1.1 jruoho * Check that we get a timeout waiting for data on the read end
173 1.1 jruoho * of our pipe.
174 1.1 jruoho */
175 1.1 jruoho pfds[0].revents = -1;
176 1.1 jruoho pfds[1].revents = -1;
177 1.10 riastrad RL(ret = poll(&pfds[0], 1, 1));
178 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 0, "got: %d", ret);
179 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
180 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[1].revents, -1, "got: %d", pfds[1].revents);
181 1.1 jruoho
182 1.1 jruoho /* Check that the write end of the pipe as reported as ready. */
183 1.1 jruoho pfds[0].revents = -1;
184 1.1 jruoho pfds[1].revents = -1;
185 1.10 riastrad RL(ret = poll(&pfds[1], 1, 1));
186 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
187 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[0].revents, -1, "got: %d", pfds[0].revents);
188 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",\
189 1.1 jruoho pfds[1].revents);
190 1.1 jruoho
191 1.1 jruoho /* Check that only the write end of the pipe as reported as ready. */
192 1.1 jruoho pfds[0].revents = -1;
193 1.1 jruoho pfds[1].revents = -1;
194 1.10 riastrad RL(ret = poll(pfds, 2, 1));
195 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
196 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[0].revents, 0, "got: %d", pfds[0].revents);
197 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
198 1.1 jruoho pfds[1].revents);
199 1.1 jruoho
200 1.1 jruoho /* Write data to our pipe. */
201 1.10 riastrad RL(nwrit = write(fds[1], "", 1));
202 1.10 riastrad ATF_REQUIRE_EQ_MSG(nwrit, 1, "nwrit=%zd", nwrit);
203 1.1 jruoho
204 1.1 jruoho /* Check that both ends of our pipe are reported as ready. */
205 1.1 jruoho pfds[0].revents = -1;
206 1.1 jruoho pfds[1].revents = -1;
207 1.10 riastrad RL(ret = poll(pfds, 2, 1));
208 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 2, "got: %d", ret);
209 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[0].revents, POLLIN, "got: %d",
210 1.1 jruoho pfds[0].revents);
211 1.1 jruoho ATF_REQUIRE_EQ_MSG(pfds[1].revents, POLLOUT, "got: %d",
212 1.1 jruoho pfds[1].revents);
213 1.1 jruoho
214 1.10 riastrad RL(close(fds[0]));
215 1.10 riastrad RL(close(fds[1]));
216 1.1 jruoho }
217 1.1 jruoho
218 1.4 kamil ATF_TC(err);
219 1.4 kamil ATF_TC_HEAD(err, tc)
220 1.1 jruoho {
221 1.1 jruoho atf_tc_set_md_var(tc, "descr", "Check errors from poll(2)");
222 1.1 jruoho }
223 1.1 jruoho
224 1.4 kamil ATF_TC_BODY(err, tc)
225 1.1 jruoho {
226 1.1 jruoho struct pollfd pfd;
227 1.1 jruoho int fd = 0;
228 1.1 jruoho
229 1.1 jruoho pfd.fd = fd;
230 1.1 jruoho pfd.events = POLLIN;
231 1.1 jruoho
232 1.1 jruoho errno = 0;
233 1.1 jruoho ATF_REQUIRE_ERRNO(EFAULT, poll((struct pollfd *)-1, 1, -1) == -1);
234 1.1 jruoho
235 1.1 jruoho errno = 0;
236 1.1 jruoho ATF_REQUIRE_ERRNO(EINVAL, poll(&pfd, 1, -2) == -1);
237 1.1 jruoho }
238 1.1 jruoho
239 1.5 thorpej static const char fifo_path[] = "pollhup_fifo";
240 1.5 thorpej
241 1.5 thorpej static void
242 1.5 thorpej fifo_support(void)
243 1.5 thorpej {
244 1.10 riastrad
245 1.5 thorpej errno = 0;
246 1.5 thorpej if (mkfifo(fifo_path, 0600) == 0) {
247 1.10 riastrad RL(unlink(fifo_path));
248 1.5 thorpej return;
249 1.5 thorpej }
250 1.5 thorpej
251 1.5 thorpej if (errno == EOPNOTSUPP) {
252 1.5 thorpej atf_tc_skip("the kernel does not support FIFOs");
253 1.5 thorpej } else {
254 1.5 thorpej atf_tc_fail("mkfifo(2) failed");
255 1.5 thorpej }
256 1.5 thorpej }
257 1.5 thorpej
258 1.7 thorpej ATF_TC_WITH_CLEANUP(fifo_inout);
259 1.7 thorpej ATF_TC_HEAD(fifo_inout, tc)
260 1.7 thorpej {
261 1.7 thorpej atf_tc_set_md_var(tc, "descr",
262 1.7 thorpej "Check POLLIN/POLLOUT behavior with fifos");
263 1.7 thorpej }
264 1.7 thorpej
265 1.7 thorpej ATF_TC_BODY(fifo_inout, tc)
266 1.7 thorpej {
267 1.7 thorpej struct pollfd pfd[2];
268 1.7 thorpej char *buf;
269 1.7 thorpej int rfd, wfd;
270 1.7 thorpej long pipe_buf;
271 1.10 riastrad int ret;
272 1.10 riastrad ssize_t nwrit, nread;
273 1.7 thorpej
274 1.7 thorpej fifo_support();
275 1.7 thorpej
276 1.10 riastrad RL(mkfifo(fifo_path, 0600));
277 1.10 riastrad RL(rfd = open(fifo_path, O_RDONLY | O_NONBLOCK));
278 1.10 riastrad RL(wfd = open(fifo_path, O_WRONLY | O_NONBLOCK));
279 1.7 thorpej
280 1.7 thorpej /* Get the maximum atomic pipe write size. */
281 1.7 thorpej pipe_buf = fpathconf(wfd, _PC_PIPE_BUF);
282 1.10 riastrad ATF_REQUIRE_MSG(pipe_buf > 1, "pipe_buf=%ld", pipe_buf);
283 1.7 thorpej
284 1.10 riastrad REQUIRE_LIBC(buf = malloc(pipe_buf), NULL);
285 1.7 thorpej
286 1.7 thorpej memset(&pfd, 0, sizeof(pfd));
287 1.7 thorpej pfd[0].fd = rfd;
288 1.7 thorpej pfd[0].events = POLLIN | POLLRDNORM;
289 1.7 thorpej pfd[1].fd = wfd;
290 1.7 thorpej pfd[1].events = POLLOUT | POLLWRNORM;
291 1.7 thorpej
292 1.7 thorpej /* We expect the FIFO to be writable but not readable. */
293 1.10 riastrad RL(ret = poll(pfd, 2, 0));
294 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
295 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[0].revents, 0,
296 1.10 riastrad "pfd[0].revents=0x%x", pfd[0].revents);
297 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, POLLOUT|POLLWRNORM,
298 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
299 1.7 thorpej
300 1.7 thorpej /* Write a single byte of data into the FIFO. */
301 1.10 riastrad RL(nwrit = write(wfd, buf, 1));
302 1.10 riastrad ATF_REQUIRE_EQ_MSG(nwrit, 1, "nwrit=%zd", nwrit);
303 1.7 thorpej
304 1.7 thorpej /* We expect the FIFO to be readable and writable. */
305 1.10 riastrad RL(ret = poll(pfd, 2, 0));
306 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 2, "got: %d", ret);
307 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[0].revents, POLLIN|POLLRDNORM,
308 1.10 riastrad "pfd[0].revents=0x%x", pfd[0].revents);
309 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, POLLOUT|POLLWRNORM,
310 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
311 1.7 thorpej
312 1.7 thorpej /* Read that single byte back out. */
313 1.10 riastrad RL(nread = read(rfd, buf, 1));
314 1.10 riastrad ATF_REQUIRE_EQ_MSG(nread, 1, "nread=%zd", nread);
315 1.7 thorpej
316 1.7 thorpej /*
317 1.7 thorpej * Write data into the FIFO until it is full, which is
318 1.7 thorpej * defined as insufficient buffer space to hold a the
319 1.7 thorpej * maximum atomic pipe write size.
320 1.7 thorpej */
321 1.7 thorpej while (write(wfd, buf, pipe_buf) != -1) {
322 1.7 thorpej continue;
323 1.7 thorpej }
324 1.10 riastrad ATF_REQUIRE_EQ_MSG(errno, EAGAIN, "errno=%d", errno);
325 1.7 thorpej
326 1.7 thorpej /* We expect the FIFO to be readble but not writable. */
327 1.10 riastrad RL(ret = poll(pfd, 2, 0));
328 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
329 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[0].revents, POLLIN|POLLRDNORM,
330 1.10 riastrad "pfd[0].revents=0x%x", pfd[0].revents);
331 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, 0,
332 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
333 1.7 thorpej
334 1.7 thorpej /* Read a single byte of data from the FIFO. */
335 1.10 riastrad RL(nread = read(rfd, buf, 1));
336 1.10 riastrad ATF_REQUIRE_EQ_MSG(nread, 1, "nread=%zd", nread);
337 1.7 thorpej
338 1.7 thorpej /*
339 1.7 thorpej * Because we have read only a single byte out, there will
340 1.7 thorpej * be insufficient space for a pipe_buf-sized message, so
341 1.7 thorpej * the FIFO should still not be writable.
342 1.7 thorpej */
343 1.10 riastrad RL(ret = poll(pfd, 2, 0));
344 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
345 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[0].revents, POLLIN|POLLRDNORM,
346 1.10 riastrad "pfd[0].revents=0x%x", pfd[0].revents);
347 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, 0,
348 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
349 1.7 thorpej
350 1.7 thorpej /*
351 1.8 thorpej * Now read enough so that exactly pipe_buf space should
352 1.8 thorpej * be available. The FIFO should be writable after that.
353 1.8 thorpej * N.B. we don't care if it's readable at this point.
354 1.8 thorpej */
355 1.10 riastrad RL(nread = read(rfd, buf, pipe_buf - 1));
356 1.10 riastrad ATF_REQUIRE_EQ_MSG(nread, pipe_buf - 1, "nread=%zd pipe_buf-1=%ld",
357 1.10 riastrad nread, pipe_buf - 1);
358 1.10 riastrad RL(ret = poll(pfd, 2, 0));
359 1.10 riastrad ATF_REQUIRE_MSG(ret >= 1, "got: %d", ret);
360 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, POLLOUT|POLLWRNORM,
361 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
362 1.8 thorpej
363 1.8 thorpej /*
364 1.7 thorpej * Now read all of the data out of the FIFO and ensure that
365 1.7 thorpej * we get back to the initial state.
366 1.7 thorpej */
367 1.7 thorpej while (read(rfd, buf, pipe_buf) != -1) {
368 1.7 thorpej continue;
369 1.7 thorpej }
370 1.10 riastrad ATF_REQUIRE_EQ_MSG(errno, EAGAIN, "errno=%d", errno);
371 1.7 thorpej
372 1.10 riastrad RL(ret = poll(pfd, 2, 0));
373 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
374 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[0].revents, 0,
375 1.10 riastrad "pfd[0].revents=0x%x", pfd[0].revents);
376 1.10 riastrad ATF_REQUIRE_EQ_MSG(pfd[1].revents, POLLOUT|POLLWRNORM,
377 1.10 riastrad "pfd[1].revents=0x%x", pfd[1].revents);
378 1.7 thorpej
379 1.10 riastrad RL(close(wfd));
380 1.10 riastrad RL(close(rfd));
381 1.7 thorpej }
382 1.7 thorpej
383 1.7 thorpej ATF_TC_CLEANUP(fifo_inout, tc)
384 1.7 thorpej {
385 1.7 thorpej (void)unlink(fifo_path);
386 1.7 thorpej }
387 1.7 thorpej
388 1.5 thorpej ATF_TC_WITH_CLEANUP(fifo_hup1);
389 1.5 thorpej ATF_TC_HEAD(fifo_hup1, tc)
390 1.5 thorpej {
391 1.5 thorpej atf_tc_set_md_var(tc, "descr",
392 1.5 thorpej "Check POLLHUP behavior with fifos [1]");
393 1.5 thorpej }
394 1.5 thorpej
395 1.5 thorpej ATF_TC_BODY(fifo_hup1, tc)
396 1.5 thorpej {
397 1.5 thorpej struct pollfd pfd;
398 1.5 thorpej int rfd, wfd;
399 1.10 riastrad int ret;
400 1.5 thorpej
401 1.5 thorpej fifo_support();
402 1.5 thorpej
403 1.10 riastrad RL(mkfifo(fifo_path, 0600));
404 1.10 riastrad RL(rfd = open(fifo_path, O_RDONLY | O_NONBLOCK));
405 1.10 riastrad RL(wfd = open(fifo_path, O_WRONLY));
406 1.5 thorpej
407 1.5 thorpej memset(&pfd, 0, sizeof(pfd));
408 1.5 thorpej pfd.fd = rfd;
409 1.5 thorpej pfd.events = POLLIN;
410 1.5 thorpej
411 1.10 riastrad RL(close(wfd));
412 1.5 thorpej
413 1.10 riastrad RL(ret = poll(&pfd, 1, 0));
414 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
415 1.9 riastrad ATF_REQUIRE_EQ_MSG((pfd.revents & (POLLHUP|POLLOUT)), POLLHUP,
416 1.9 riastrad "revents=0x%x expected POLLHUP=0x%x and not POLLOUT=0x%x",
417 1.9 riastrad pfd.revents, POLLHUP, POLLOUT);
418 1.6 thorpej
419 1.6 thorpej /*
420 1.6 thorpej * Check that POLLHUP is cleared when a writer re-connects.
421 1.6 thorpej * Since the writer will not put any data into the FIFO, we
422 1.6 thorpej * expect no events.
423 1.6 thorpej */
424 1.6 thorpej memset(&pfd, 0, sizeof(pfd));
425 1.6 thorpej pfd.fd = rfd;
426 1.6 thorpej pfd.events = POLLIN;
427 1.6 thorpej
428 1.10 riastrad RL(wfd = open(fifo_path, O_WRONLY));
429 1.10 riastrad RL(ret = poll(&pfd, 1, 0));
430 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 0, "got: %d", ret);
431 1.5 thorpej }
432 1.5 thorpej
433 1.5 thorpej ATF_TC_CLEANUP(fifo_hup1, tc)
434 1.5 thorpej {
435 1.5 thorpej (void)unlink(fifo_path);
436 1.5 thorpej }
437 1.5 thorpej
438 1.5 thorpej ATF_TC_WITH_CLEANUP(fifo_hup2);
439 1.5 thorpej ATF_TC_HEAD(fifo_hup2, tc)
440 1.5 thorpej {
441 1.5 thorpej atf_tc_set_md_var(tc, "descr",
442 1.5 thorpej "Check POLLHUP behavior with fifos [2]");
443 1.5 thorpej }
444 1.5 thorpej
445 1.5 thorpej ATF_TC_BODY(fifo_hup2, tc)
446 1.5 thorpej {
447 1.5 thorpej struct pollfd pfd;
448 1.5 thorpej int rfd, wfd;
449 1.5 thorpej pid_t pid;
450 1.5 thorpej struct timespec ts1, ts2;
451 1.10 riastrad int ret;
452 1.5 thorpej
453 1.5 thorpej fifo_support();
454 1.5 thorpej
455 1.10 riastrad RL(mkfifo(fifo_path, 0600));
456 1.10 riastrad RL(rfd = open(fifo_path, O_RDONLY | O_NONBLOCK));
457 1.10 riastrad RL(wfd = open(fifo_path, O_WRONLY));
458 1.5 thorpej
459 1.5 thorpej memset(&pfd, 0, sizeof(pfd));
460 1.5 thorpej pfd.fd = rfd;
461 1.5 thorpej pfd.events = POLLIN;
462 1.5 thorpej
463 1.10 riastrad RL(pid = fork());
464 1.5 thorpej if (pid == 0) {
465 1.10 riastrad if (close(rfd))
466 1.10 riastrad _exit(1);
467 1.5 thorpej sleep(5);
468 1.10 riastrad if (close(wfd))
469 1.10 riastrad _exit(1);
470 1.5 thorpej _exit(0);
471 1.5 thorpej }
472 1.10 riastrad RL(close(wfd));
473 1.5 thorpej
474 1.10 riastrad RL(clock_gettime(CLOCK_MONOTONIC, &ts1));
475 1.10 riastrad RL(ret = poll(&pfd, 1, INFTIM));
476 1.10 riastrad ATF_REQUIRE_EQ_MSG(ret, 1, "got: %d", ret);
477 1.10 riastrad RL(clock_gettime(CLOCK_MONOTONIC, &ts2));
478 1.5 thorpej
479 1.5 thorpej /* Make sure at least a couple of seconds have elapsed. */
480 1.10 riastrad ATF_REQUIRE_MSG(ts2.tv_sec - ts1.tv_sec >= 2,
481 1.10 riastrad "ts1=%lld.%09ld ts2=%lld.%09ld",
482 1.10 riastrad (long long)ts1.tv_sec, ts1.tv_nsec,
483 1.10 riastrad (long long)ts2.tv_sec, ts2.tv_nsec);
484 1.5 thorpej
485 1.9 riastrad ATF_REQUIRE_EQ_MSG((pfd.revents & (POLLHUP|POLLOUT)), POLLHUP,
486 1.9 riastrad "revents=0x%x expected POLLHUP=0x%x and not POLLOUT=0x%x",
487 1.9 riastrad pfd.revents, POLLHUP, POLLOUT);
488 1.5 thorpej }
489 1.5 thorpej
490 1.5 thorpej ATF_TC_CLEANUP(fifo_hup2, tc)
491 1.5 thorpej {
492 1.5 thorpej (void)unlink(fifo_path);
493 1.5 thorpej }
494 1.5 thorpej
495 1.1 jruoho ATF_TP_ADD_TCS(tp)
496 1.1 jruoho {
497 1.1 jruoho
498 1.4 kamil ATF_TP_ADD_TC(tp, 3way);
499 1.4 kamil ATF_TP_ADD_TC(tp, basic);
500 1.4 kamil ATF_TP_ADD_TC(tp, err);
501 1.1 jruoho
502 1.7 thorpej ATF_TP_ADD_TC(tp, fifo_inout);
503 1.5 thorpej ATF_TP_ADD_TC(tp, fifo_hup1);
504 1.5 thorpej ATF_TP_ADD_TC(tp, fifo_hup2);
505 1.5 thorpej
506 1.1 jruoho return atf_no_error();
507 1.1 jruoho }
508