xref: /openbsd-src/regress/sys/kern/poll/poll_regevent.c (revision 2b613d4d4599a3e3935554e08adc8e9e18dc9ca0)
1*2b613d4dSvisa /*	$OpenBSD: poll_regevent.c,v 1.1 2021/11/29 16:11:46 visa Exp $	*/
2*2b613d4dSvisa 
3*2b613d4dSvisa /*
4*2b613d4dSvisa  * Copyright (c) 2021 Visa Hankala
5*2b613d4dSvisa  *
6*2b613d4dSvisa  * Permission to use, copy, modify, and distribute this software for any
7*2b613d4dSvisa  * purpose with or without fee is hereby granted, provided that the above
8*2b613d4dSvisa  * copyright notice and this permission notice appear in all copies.
9*2b613d4dSvisa  *
10*2b613d4dSvisa  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*2b613d4dSvisa  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*2b613d4dSvisa  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*2b613d4dSvisa  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*2b613d4dSvisa  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*2b613d4dSvisa  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*2b613d4dSvisa  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*2b613d4dSvisa  */
18*2b613d4dSvisa 
19*2b613d4dSvisa /*
20*2b613d4dSvisa  * Test that poll/select does not block if a pending event is found
21*2b613d4dSvisa  * during registering.
22*2b613d4dSvisa  */
23*2b613d4dSvisa 
24*2b613d4dSvisa #include <assert.h>
25*2b613d4dSvisa #include <err.h>
26*2b613d4dSvisa #include <poll.h>
27*2b613d4dSvisa #include <unistd.h>
28*2b613d4dSvisa 
29*2b613d4dSvisa int
main(void)30*2b613d4dSvisa main(void)
31*2b613d4dSvisa {
32*2b613d4dSvisa 	struct pollfd pfd[2];
33*2b613d4dSvisa 	int p1[2];
34*2b613d4dSvisa 	int p2[2];
35*2b613d4dSvisa 	int ret;
36*2b613d4dSvisa 
37*2b613d4dSvisa 	/* Enforce test timeout. */
38*2b613d4dSvisa 	alarm(10);
39*2b613d4dSvisa 
40*2b613d4dSvisa 	if (pipe(p1) == -1)
41*2b613d4dSvisa 		err(1, "pipe");
42*2b613d4dSvisa 	if (pipe(p2) == -1)
43*2b613d4dSvisa 		err(1, "pipe");
44*2b613d4dSvisa 
45*2b613d4dSvisa 	close(p2[0]);
46*2b613d4dSvisa 
47*2b613d4dSvisa 	/* fd without event */
48*2b613d4dSvisa 	pfd[0].fd = p1[0];
49*2b613d4dSvisa 	pfd[0].events = POLLIN;
50*2b613d4dSvisa 
51*2b613d4dSvisa 	/* fd with event */
52*2b613d4dSvisa 	pfd[1].fd = p2[1];
53*2b613d4dSvisa 	pfd[1].events = POLLOUT;
54*2b613d4dSvisa 
55*2b613d4dSvisa 	ret = poll(pfd, 2, INFTIM);
56*2b613d4dSvisa 	assert(ret == 1);
57*2b613d4dSvisa 	assert(pfd[0].revents == 0);
58*2b613d4dSvisa 	assert(pfd[1].revents != 0);
59*2b613d4dSvisa 
60*2b613d4dSvisa 	return 0;
61*2b613d4dSvisa }
62