From: visa Date: Mon, 29 Nov 2021 16:11:46 +0000 (+0000) Subject: Register-time event should make poll/select non-blocking. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=2b613d4d4599a3e3935554e08adc8e9e18dc9ca0;p=openbsd Register-time event should make poll/select non-blocking. --- diff --git a/regress/sys/kern/poll/Makefile b/regress/sys/kern/poll/Makefile index d0e39c0fc87..2db888bb7b9 100644 --- a/regress/sys/kern/poll/Makefile +++ b/regress/sys/kern/poll/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.5 2021/11/27 15:07:26 visa Exp $ +# $OpenBSD: Makefile,v 1.6 2021/11/29 16:11:46 visa Exp $ -PROGS= poll_close pollnval pollretval +PROGS= poll_close poll_regevent pollnval pollretval LDADD= -lpthread WARNINGS= yes diff --git a/regress/sys/kern/poll/poll_regevent.c b/regress/sys/kern/poll/poll_regevent.c new file mode 100644 index 00000000000..36fc35d5aa3 --- /dev/null +++ b/regress/sys/kern/poll/poll_regevent.c @@ -0,0 +1,61 @@ +/* $OpenBSD: poll_regevent.c,v 1.1 2021/11/29 16:11:46 visa Exp $ */ + +/* + * Copyright (c) 2021 Visa Hankala + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Test that poll/select does not block if a pending event is found + * during registering. + */ + +#include +#include +#include +#include + +int +main(void) +{ + struct pollfd pfd[2]; + int p1[2]; + int p2[2]; + int ret; + + /* Enforce test timeout. */ + alarm(10); + + if (pipe(p1) == -1) + err(1, "pipe"); + if (pipe(p2) == -1) + err(1, "pipe"); + + close(p2[0]); + + /* fd without event */ + pfd[0].fd = p1[0]; + pfd[0].events = POLLIN; + + /* fd with event */ + pfd[1].fd = p2[1]; + pfd[1].events = POLLOUT; + + ret = poll(pfd, 2, INFTIM); + assert(ret == 1); + assert(pfd[0].revents == 0); + assert(pfd[1].revents != 0); + + return 0; +} diff --git a/regress/sys/kern/select/Makefile b/regress/sys/kern/select/Makefile index 9de4216906e..b66af2368bf 100644 --- a/regress/sys/kern/select/Makefile +++ b/regress/sys/kern/select/Makefile @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile,v 1.1 2021/11/21 06:21:01 visa Exp $ +# $OpenBSD: Makefile,v 1.2 2021/11/29 16:11:46 visa Exp $ -PROGS= select_close +PROGS= select_close select_regevent LDADD= -lpthread WARNINGS= yes diff --git a/regress/sys/kern/select/select_regevent.c b/regress/sys/kern/select/select_regevent.c new file mode 100644 index 00000000000..64ef1f441b6 --- /dev/null +++ b/regress/sys/kern/select/select_regevent.c @@ -0,0 +1,64 @@ +/* $OpenBSD: select_regevent.c,v 1.1 2021/11/29 16:11:46 visa Exp $ */ + +/* + * Copyright (c) 2021 Visa Hankala + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Test that poll/select does not block if a pending event is found + * during registering. + */ + +#include +#include +#include +#include + +int +main(void) +{ + fd_set rfd; + fd_set wfd; + int p1[2]; + int p2[2]; + int ret; + + /* Enforce test timeout. */ + alarm(10); + + if (pipe(p1) == -1) + err(1, "pipe"); + if (pipe(p2) == -1) + err(1, "pipe"); + + close(p2[0]); + + /* fd without event */ + FD_ZERO(&rfd); + FD_SET(p1[0], &rfd); + + /* fd with event */ + FD_ZERO(&wfd); + FD_SET(p2[1], &wfd); + + assert(p1[0] < p2[1]); + + ret = select(p2[1] + 1, &rfd, &wfd, NULL, NULL); + assert(ret == 1); + assert(FD_ISSET(p1[0], &rfd) == 0); + assert(FD_ISSET(p2[1], &wfd) != 0); + + return 0; +}