Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: t_sigio.c,v 1.2 2026/09/25 01:09:57 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: t_sigio.c,v 1.2 2026/09/25 01:09:57 riastradh Exp $");
     31 
     32 #include <sys/ioctl.h>
     33 #include <sys/socket.h>
     34 #include <sys/stat.h>
     35 
     36 #include <atf-c.h>
     37 #include <errno.h>
     38 #include <fcntl.h>
     39 #include <limits.h>
     40 #include <netdb.h>
     41 #include <poll.h>
     42 #include <pthread.h>
     43 #include <signal.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <termios.h>
     47 #include <unistd.h>
     48 
     49 #include "h_macros.h"
     50 
     51 static int expected_si_fd = -1;
     52 static int expected_si_code = -1;
     53 static long expected_si_band = 0;
     54 static long si_band_mask = 0;
     55 static int siginfo_received;
     56 
     57 static void
     58 formatbit(char **bufp, size_t *lenp, bool *firstp, const char *name)
     59 {
     60 	int n;
     61 	size_t k;
     62 
     63 	n = snprintf(*bufp, *lenp, "%s%s", *firstp ? "" : ",", name);
     64 	*firstp = false;
     65 	k = ((unsigned)n >= *lenp ? *lenp : (unsigned)n);
     66 	*bufp += k;
     67 	*lenp -= k;
     68 }
     69 
     70 static char *
     71 formatpollevents(char *buf, size_t len, long events)
     72 {
     73 	char *start = buf;
     74 	bool first = true;
     75 	int n;
     76 
     77 	n = snprintf(buf, len, "0x%lx<", events);
     78 	if ((unsigned)n >= len)
     79 		goto out;
     80 	buf += (unsigned)n;
     81 	len -= (unsigned)n;
     82 	if (events & POLLIN)
     83 		formatbit(&buf, &len, &first, "POLLIN");
     84 	if (events & POLLOUT)
     85 		formatbit(&buf, &len, &first, "POLLOUT");
     86 	if (events & POLLHUP)
     87 		formatbit(&buf, &len, &first, "POLLHUP");
     88 	if (events & POLLERR)
     89 		formatbit(&buf, &len, &first, "POLLERR");
     90 	if (events & POLLRDNORM)
     91 		formatbit(&buf, &len, &first, "POLLRDNORM");
     92 	if (events & POLLWRNORM)
     93 		formatbit(&buf, &len, &first, "POLLWRNORM");
     94 	if (events & POLLRDBAND)
     95 		formatbit(&buf, &len, &first, "POLLRDBAND");
     96 	if (events & POLLWRBAND)
     97 		formatbit(&buf, &len, &first, "POLLWRBAND");
     98 	if (events & POLLNVAL)
     99 		formatbit(&buf, &len, &first, "POLLNVAL");
    100 	snprintf(buf, len, ">");
    101 out:	return start;
    102 }
    103 
    104 struct context {
    105 	pthread_barrier_t *bar;
    106 	int fd;
    107 	int lowat;
    108 };
    109 
    110 static void *
    111 start_closer(void *cookie)
    112 {
    113 	struct context *C = cookie;
    114 	sigset_t mask;
    115 
    116 	RL(sigemptyset(&mask));
    117 	RL(sigaddset(&mask, SIGIO));
    118 	RL(pthread_sigmask(SIG_BLOCK, &mask, NULL));
    119 
    120 	fprintf(stderr, "[thread] waiting for barrier\n");
    121 	(void)pthread_barrier_wait(C->bar);
    122 	fprintf(stderr, "[thread] giving other thread a head start\n");
    123 	RL(usleep(1));
    124 	fprintf(stderr, "[thread] closing\n");
    125 	RL(close(C->fd));
    126 	fprintf(stderr, "[thread] closed\n");
    127 
    128 	return NULL;
    129 }
    130 
    131 static void *
    132 start_shutdownrd(void *cookie)
    133 {
    134 	struct context *C = cookie;
    135 	sigset_t mask;
    136 
    137 	RL(sigemptyset(&mask));
    138 	RL(sigaddset(&mask, SIGIO));
    139 	RL(pthread_sigmask(SIG_BLOCK, &mask, NULL));
    140 
    141 	fprintf(stderr, "[thread] waiting for barrier\n");
    142 	(void)pthread_barrier_wait(C->bar);
    143 	fprintf(stderr, "[thread] giving other thread a head start\n");
    144 	RL(usleep(1));
    145 	fprintf(stderr, "[thread] shutdown(SHUT_RD)\n");
    146 	RL(shutdown(C->fd, SHUT_RD));
    147 	fprintf(stderr, "[thread] shutdown\n");
    148 
    149 	return NULL;
    150 }
    151 
    152 static void *
    153 start_shutdownwr(void *cookie)
    154 {
    155 	struct context *C = cookie;
    156 	sigset_t mask;
    157 
    158 	RL(sigemptyset(&mask));
    159 	RL(sigaddset(&mask, SIGIO));
    160 	RL(pthread_sigmask(SIG_BLOCK, &mask, NULL));
    161 
    162 	fprintf(stderr, "[thread] waiting for barrier\n");
    163 	(void)pthread_barrier_wait(C->bar);
    164 	fprintf(stderr, "[thread] giving other thread a head start\n");
    165 	RL(usleep(1));
    166 	fprintf(stderr, "[thread] shutdown(SHUT_WR)\n");
    167 	RL(shutdown(C->fd, SHUT_WR));
    168 	fprintf(stderr, "[thread] shutdown\n");
    169 
    170 	return NULL;
    171 }
    172 
    173 static void *
    174 start_reader(void *cookie)
    175 {
    176 	struct context *C = cookie;
    177 	sigset_t mask;
    178 	char *buf;
    179 	ssize_t nread;
    180 	size_t resid = C->lowat;
    181 
    182 	REQUIRE_LIBC(buf = malloc(C->lowat), NULL);
    183 
    184 	RL(sigemptyset(&mask));
    185 	RL(sigaddset(&mask, SIGIO));
    186 	RL(pthread_sigmask(SIG_BLOCK, &mask, NULL));
    187 
    188 	fprintf(stderr, "[thread] waiting for barrier\n");
    189 	(void)pthread_barrier_wait(C->bar);
    190 	fprintf(stderr, "[thread] giving other thread a head start\n");
    191 	RL(usleep(1));
    192 	fprintf(stderr, "[thread] reading first byte\n");
    193 	RL(nread = read(C->fd, buf, 1));
    194 	fprintf(stderr, "[thread] read first byte\n");
    195 	ATF_CHECK_EQ_MSG(nread, 1, "nread=%zd", nread);
    196 	resid -= (size_t)nread;
    197 	RL(usleep(1));
    198 	fprintf(stderr, "[thread] slept\n");
    199 
    200 	if (C->lowat <= 1) {
    201 		fprintf(stderr, "[thread] lowat=1, our job is done\n");
    202 		goto out;
    203 	}
    204 
    205 	/*
    206 	 * Reading a single byte out of the buffer shouldn't be enough
    207 	 * to deliver SIGIO -- only clearing space for C->lowat bytes
    208 	 * should be.
    209 	 */
    210 	ATF_CHECK_EQ_MSG(siginfo_received, 0, "siginfo_received=%d",
    211 	    siginfo_received);
    212 
    213 	while (resid > 0) {
    214 		fprintf(stderr, "[thread] reading more bytes, %zu remain\n",
    215 		    resid);
    216 		RL(nread = read(C->fd, buf + C->lowat - resid, resid));
    217 		fprintf(stderr, "[thread] read %zd bytes\n", nread);
    218 		ATF_REQUIRE_MSG((size_t)nread <= resid, "nread=%zd resid=%zu",
    219 		    nread, resid);
    220 		resid -= (size_t)nread;
    221 	}
    222 
    223 out:	fprintf(stderr, "[thread] done reading\n");
    224 	free(buf);
    225 	return NULL;
    226 }
    227 
    228 static void *
    229 start_writer(void *cookie)
    230 {
    231 	struct context *C = cookie;
    232 	sigset_t mask;
    233 	char ch = 1;
    234 	ssize_t nwrit;
    235 
    236 	RL(sigemptyset(&mask));
    237 	RL(sigaddset(&mask, SIGIO));
    238 	RL(pthread_sigmask(SIG_BLOCK, &mask, NULL));
    239 
    240 	fprintf(stderr, "[thread] waiting for barrier\n");
    241 	(void)pthread_barrier_wait(C->bar);
    242 	fprintf(stderr, "[thread] giving other thread a head start\n");
    243 	RL(usleep(1));
    244 	fprintf(stderr, "[thread] writing\n");
    245 	RL(nwrit = write(C->fd, &ch, 1));
    246 	fprintf(stderr, "[thread] wrote %zd bytes\n", nwrit);
    247 	ATF_CHECK_EQ_MSG(nwrit, 1, "nwrit=%zd", nwrit);
    248 
    249 	return NULL;
    250 }
    251 
    252 static void
    253 ptysetup(int *hostfd, int *appfd)
    254 {
    255 	struct termios t;
    256 	char *pts;
    257 
    258 	RL(*hostfd = posix_openpt(O_RDWR|O_NOCTTY));
    259 	RL(grantpt(*hostfd));
    260 	RL(unlockpt(*hostfd));
    261 	REQUIRE_LIBC(pts = ptsname(*hostfd), NULL);
    262 	RL(*appfd = open(pts, O_RDWR|O_NOCTTY));
    263 
    264 	RL(tcgetattr(*appfd, &t));
    265 	t.c_lflag &= ~ICANON;	/* block rather than drop input */
    266 	RL(tcsetattr(*appfd, TCSANOW, &t));
    267 }
    268 
    269 static void
    270 socksetup(int s[static 2], int af, const char *host)
    271 {
    272 	struct addrinfo *ai, hints = {
    273 		.ai_family = af,
    274 		.ai_socktype = SOCK_STREAM,
    275 		.ai_flags = AI_NUMERICHOST|AI_PASSIVE,
    276 	};
    277 	union {
    278 		struct sockaddr sa;
    279 		struct sockaddr_storage ss;
    280 	} addr;
    281 	socklen_t addrlen = sizeof(addr);
    282 	int server = -1;
    283 	int error;
    284 
    285 	error = getaddrinfo(host, NULL, &hints, &ai);
    286 	ATF_REQUIRE_MSG(error == 0, "getaddrinfo: %d (%s)", error,
    287 	    gai_strerror(errno));
    288 	RL(server = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol));
    289 	RL(bind(server, ai->ai_addr, ai->ai_addrlen));
    290 	RL(getsockname(server, &addr.sa, &addrlen));
    291 	RL(listen(server, 1));
    292 	RL(s[0] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol));
    293 	RL(connect(s[0], &addr.sa, addrlen));
    294 	RL(s[1] = accept(server, NULL, NULL));
    295 	RL(close(server));
    296 	freeaddrinfo(ai);
    297 }
    298 
    299 static void
    300 fillpipebuf(int writefd)
    301 {
    302 	char buf[BUFSIZ] = {0};
    303 	size_t n = 0;
    304 	ssize_t nwrit;
    305 	int flags;
    306 
    307 	RL(flags = fcntl(writefd, F_GETFL));
    308 	RL(fcntl(writefd, F_SETFL, flags|O_NONBLOCK));
    309 	while ((nwrit = write(writefd, buf, sizeof(buf))) != -1)
    310 		n += (size_t)nwrit;
    311 	ATF_CHECK_EQ_MSG(errno, EAGAIN, "errno=%d", errno);
    312 	RL(fcntl(writefd, F_SETFL, flags));
    313 	fprintf(stderr, "filled %d with %zu bytes\n", writefd, n);
    314 }
    315 
    316 static void
    317 check_write(int writefd)
    318 {
    319 	int flags;
    320 	char c = 0;
    321 	ssize_t nwrit;
    322 
    323 	fprintf(stderr, "checking that we can write a byte...\n");
    324 	RL(flags = fcntl(writefd, F_GETFL));
    325 	RL(fcntl(writefd, F_SETFL, flags|O_NONBLOCK));
    326 	RL(nwrit = write(writefd, &c, 1));
    327 	ATF_CHECK_EQ_MSG(nwrit, 1, "nwrit=%zd", nwrit);
    328 	RL(fcntl(writefd, F_SETFL, flags));
    329 }
    330 
    331 static void
    332 check_sendnonblock(int sendfd)
    333 {
    334 	char c = 0;
    335 	ssize_t nsent;
    336 
    337 	fprintf(stderr, "checking that we can send a single byte...\n");
    338 	RL(nsent = send(sendfd, &c, 1, MSG_DONTWAIT));
    339 	ATF_CHECK_EQ_MSG(nsent, 1, "nsent=%zd", nsent);
    340 }
    341 
    342 static void
    343 check_read(int readfd)
    344 {
    345 	char c;
    346 	ssize_t nread;
    347 
    348 	fprintf(stderr, "checking that we can read a byte...\n");
    349 	RL(nread = read(readfd, &c, 1));
    350 	ATF_CHECK_EQ_MSG(nread, 1, "nread=%zd", nread);
    351 }
    352 
    353 static void
    354 check_write_fail(int writefd, int error)
    355 {
    356 	int flags;
    357 	void (*sighandler)(int);
    358 	char c = 0;
    359 	ssize_t nwrit;
    360 
    361 	fprintf(stderr, "checking that write fails with %d (%s)...\n",
    362 	    error, strerror(error));
    363 
    364 	RL(flags = fcntl(writefd, F_GETFL));
    365 	RL(fcntl(writefd, F_SETFL, flags|O_NONBLOCK));
    366 
    367 	REQUIRE_LIBC(sighandler = signal(SIGPIPE, SIG_IGN), SIG_ERR);
    368 	ATF_CHECK_ERRNO(error, (nwrit = write(writefd, &c, 1)) == -1);
    369 	ATF_CHECK_EQ_MSG(nwrit, -1, "nwrit=%zd", nwrit);
    370 	REQUIRE_LIBC(signal(SIGPIPE, sighandler), SIG_ERR);
    371 
    372 	RL(fcntl(writefd, F_SETFL, flags));
    373 }
    374 
    375 static void
    376 check_read_eof(int readfd)
    377 {
    378 	int flags;
    379 	char c;
    380 	ssize_t nread;
    381 
    382 	fprintf(stderr, "checking that read returns EOF...\n");
    383 
    384 	RL(flags = fcntl(readfd, F_GETFL));
    385 	RL(fcntl(readfd, F_SETFL, flags|O_NONBLOCK));
    386 
    387 	RL(nread = read(readfd, &c, 1));
    388 	ATF_CHECK_EQ_MSG(nread, 0, "nread=%zu", nread);
    389 
    390 	RL(fcntl(readfd, F_SETFL, flags));
    391 }
    392 
    393 static void
    394 check_sigio(int signo, siginfo_t *si)
    395 {
    396 	char actbuf[128], expbuf[128];
    397 
    398 	fprintf(stderr, "[signal] signo=%d (%s)"
    399 	    " si_code=%d si_fd=%d si_band=%s\n",
    400 	    signo, strsignal(signo),
    401 	    si->si_code, si->si_fd,
    402 	    formatpollevents(actbuf, sizeof(actbuf), si->si_band));
    403 	ATF_CHECK_EQ_MSG(signo, SIGIO, "signo=%d (%s)",
    404 	    signo, strsignal(signo));
    405 	ATF_CHECK_EQ_MSG(si->si_signo, SIGIO, "signo=%d (%s)",
    406 	    si->si_signo, strsignal(si->si_signo));
    407 	if (expected_si_fd != -1) {
    408 		ATF_CHECK_EQ_MSG(si->si_fd, expected_si_fd,
    409 		    "si_fd=%d, expected %d",
    410 		    si->si_fd, expected_si_fd);
    411 	}
    412 	if (expected_si_code != -1) {
    413 		ATF_CHECK_EQ_MSG(si->si_code, expected_si_code,
    414 		    "si_code=%d, expected %d",
    415 		    si->si_code, expected_si_code);
    416 	}
    417 	ATF_CHECK_EQ_MSG((si->si_band & si_band_mask), expected_si_band,
    418 	    "si_band=%s, expected %s",
    419 	    formatpollevents(actbuf, sizeof(actbuf), si->si_band),
    420 	    formatpollevents(expbuf, sizeof(expbuf), expected_si_band));
    421 }
    422 
    423 static void
    424 on_sigio(int signo, siginfo_t *si, void *ctx)
    425 {
    426 
    427 	check_sigio(signo, si);
    428 	siginfo_received++;
    429 }
    430 
    431 static void
    432 setup(sigset_t *omask)
    433 {
    434 	struct sigaction sa;
    435 
    436 	memset(&sa, 0, sizeof(sa));
    437 	sa.sa_sigaction = &on_sigio;
    438 	sa.sa_flags = SA_SIGINFO|SA_RESTART;
    439 	RL(sigemptyset(&sa.sa_mask));
    440 	RL(sigaddset(&sa.sa_mask, SIGIO));
    441 	RL(pthread_sigmask(SIG_BLOCK, &sa.sa_mask, omask));
    442 	RL(sigaction(SIGIO, &sa, NULL));
    443 }
    444 
    445 static void
    446 wait_for_io(sigset_t *omask)
    447 {
    448 
    449 	ATF_CHECK_ERRNO(EINTR, sigsuspend(omask) == -1);
    450 	ATF_CHECK_EQ_MSG(siginfo_received, 1, "siginfo_received=%d",
    451 	    siginfo_received);
    452 }
    453 
    454 ATF_TC(fifo_read);
    455 ATF_TC_HEAD(fifo_read, tc)
    456 {
    457 	atf_tc_set_md_var(tc, "descr", "Test SIGIO when fifo is readable");
    458 }
    459 ATF_TC_BODY(fifo_read, tc)
    460 {
    461 	sigset_t omask;
    462 	int fiford, fifowr, flags;
    463 	pthread_barrier_t bar;
    464 	struct context ctx, *C = &ctx;
    465 	pthread_t t;
    466 
    467 	setup(&omask);
    468 
    469 	RL(mkfifo("fifo", 0600));
    470 	RL(fiford = open("fifo", O_RDONLY|O_NONBLOCK));
    471 	RL(flags = fcntl(fiford, F_GETFL));
    472 	RL(fcntl(fiford, F_SETFL, flags & ~O_ASYNC));
    473 	RL(fifowr = open("fifo", O_WRONLY));
    474 
    475 	RL(fcntl(fiford, F_SETOWN, getpid()));
    476 	RL(flags = fcntl(fiford, F_GETFL));
    477 	RL(fcntl(fiford, F_SETFL, flags | O_ASYNC));
    478 
    479 	RZ(pthread_barrier_init(&bar, NULL, 2));
    480 
    481 	memset(C, 0, sizeof(*C));
    482 	C->bar = &bar;
    483 	C->fd = fifowr;
    484 	RZ(pthread_create(&t, NULL, &start_writer, C));
    485 
    486 	expected_si_fd = fiford;
    487 	expected_si_code = POLL_IN;
    488 	expected_si_band = POLLIN|POLLRDNORM;
    489 	si_band_mask = ~0;
    490 
    491 	/*
    492 	 * SIGIO is delivered with wrong si_fd = -1, but correct
    493 	 * si_code and si_band.
    494 	 */
    495 	atf_tc_expect_fail("PR kern/60785:"
    496 	    " fifo delivers SIGIO with wrong si_fd");
    497 
    498 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    499 	(void)pthread_barrier_wait(&bar);
    500 	wait_for_io(&omask);
    501 	check_read(fiford);
    502 
    503 	RZ(pthread_join(t, NULL));
    504 }
    505 
    506 ATF_TC(fifo_read_closed);
    507 ATF_TC_HEAD(fifo_read_closed, tc)
    508 {
    509 	atf_tc_set_md_var(tc, "descr",
    510 	    "Test SIGIO when fifo is readable because closed");
    511 }
    512 ATF_TC_BODY(fifo_read_closed, tc)
    513 {
    514 	sigset_t omask;
    515 	int fiford, fifowr, flags;
    516 	pthread_barrier_t bar;
    517 	struct context ctx, *C = &ctx;
    518 	pthread_t t;
    519 
    520 	setup(&omask);
    521 
    522 	RL(mkfifo("fifo", 0600));
    523 	RL(fiford = open("fifo", O_RDONLY|O_NONBLOCK));
    524 	RL(flags = fcntl(fiford, F_GETFL));
    525 	RL(fcntl(fiford, F_SETFL, flags & ~O_ASYNC));
    526 	RL(fifowr = open("fifo", O_WRONLY));
    527 
    528 	RL(fcntl(fifowr, F_SETOWN, getpid()));
    529 	RL(flags = fcntl(fifowr, F_GETFL));
    530 	RL(fcntl(fifowr, F_SETFL, flags | O_ASYNC));
    531 
    532 	RZ(pthread_barrier_init(&bar, NULL, 2));
    533 
    534 	memset(C, 0, sizeof(*C));
    535 	C->bar = &bar;
    536 	C->fd = fifowr;
    537 	RZ(pthread_create(&t, NULL, &start_closer, C));
    538 
    539 	expected_si_fd = fiford;
    540 	expected_si_code = POLL_HUP;
    541 	expected_si_band = POLLHUP;
    542 	si_band_mask = ~0;
    543 
    544 	/*
    545 	 * Missing wakeups similar to some poll bugs.  Not really the
    546 	 * same issue but we're tracking a lot of related stuff
    547 	 * there...
    548 	 */
    549 	atf_tc_expect_signal(SIGALRM, "PR kern/59056: poll POLLHUP bugs");
    550 
    551 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    552 	(void)pthread_barrier_wait(&bar);
    553 	wait_for_io(&omask);
    554 	check_read_eof(fiford);
    555 
    556 	RZ(pthread_join(t, NULL));
    557 }
    558 
    559 ATF_TC(fifo_write);
    560 ATF_TC_HEAD(fifo_write, tc)
    561 {
    562 	atf_tc_set_md_var(tc, "descr", "Test SIGIO when fifo is writable");
    563 }
    564 ATF_TC_BODY(fifo_write, tc)
    565 {
    566 	sigset_t omask;
    567 	int fiford, fifowr, flags;
    568 	pthread_barrier_t bar;
    569 	struct context ctx, *C = &ctx;
    570 	pthread_t t;
    571 
    572 	setup(&omask);
    573 
    574 	RL(mkfifo("fifo", 0600));
    575 	RL(fiford = open("fifo", O_RDONLY|O_NONBLOCK));
    576 	RL(flags = fcntl(fiford, F_GETFL));
    577 	RL(fcntl(fiford, F_SETFL, flags & ~O_ASYNC));
    578 	RL(fifowr = open("fifo", O_WRONLY));
    579 
    580 	fillpipebuf(fifowr);
    581 	RL(fcntl(fifowr, F_SETOWN, getpid()));
    582 	RL(flags = fcntl(fifowr, F_GETFL));
    583 	RL(fcntl(fifowr, F_SETFL, flags | O_ASYNC));
    584 
    585 	RZ(pthread_barrier_init(&bar, NULL, 2));
    586 
    587 	memset(C, 0, sizeof(*C));
    588 	C->bar = &bar;
    589 	C->fd = fiford;
    590 	C->lowat = PIPE_BUF;
    591 	RZ(pthread_create(&t, NULL, &start_reader, C));
    592 
    593 	expected_si_fd = fifowr;
    594 	expected_si_code = POLL_OUT;
    595 	expected_si_band = POLLOUT|POLLWRNORM;
    596 	si_band_mask = ~0;
    597 
    598 	/*
    599 	 * SIGIO is delivered with wrong si_fd = -1, but correct
    600 	 * si_code and si_band.  Also delivered too early.
    601 	 */
    602 	atf_tc_expect_fail("PR kern/60785:"
    603 	    " fifo delivers SIGIO with wrong si_fd"
    604 	    "; "
    605 	    "PR kern/60782:"
    606 	    " SIGIO says socket ready for send before send does");
    607 
    608 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    609 	(void)pthread_barrier_wait(&bar);
    610 	wait_for_io(&omask);
    611 	check_write(fifowr);
    612 
    613 	RZ(pthread_join(t, NULL));
    614 }
    615 
    616 ATF_TC(fifo_write_closed);
    617 ATF_TC_HEAD(fifo_write_closed, tc)
    618 {
    619 	atf_tc_set_md_var(tc, "descr",
    620 	    "Test SIGIO when fifo is writable because closed");
    621 }
    622 ATF_TC_BODY(fifo_write_closed, tc)
    623 {
    624 	sigset_t omask;
    625 	int fiford, fifowr, flags;
    626 	pthread_barrier_t bar;
    627 	struct context ctx, *C = &ctx;
    628 	pthread_t t;
    629 
    630 	setup(&omask);
    631 
    632 	RL(mkfifo("fifo", 0600));
    633 	RL(fiford = open("fifo", O_RDONLY|O_NONBLOCK));
    634 	RL(flags = fcntl(fiford, F_GETFL));
    635 	RL(fcntl(fiford, F_SETFL, flags & ~O_ASYNC));
    636 	RL(fifowr = open("fifo", O_WRONLY));
    637 
    638 	fillpipebuf(fifowr);
    639 	RL(fcntl(fifowr, F_SETOWN, getpid()));
    640 	RL(flags = fcntl(fifowr, F_GETFL));
    641 	RL(fcntl(fifowr, F_SETFL, flags | O_ASYNC));
    642 
    643 	RZ(pthread_barrier_init(&bar, NULL, 2));
    644 
    645 	memset(C, 0, sizeof(*C));
    646 	C->bar = &bar;
    647 	C->fd = fiford;
    648 	RZ(pthread_create(&t, NULL, &start_closer, C));
    649 
    650 	expected_si_fd = fifowr;
    651 	expected_si_code = POLL_ERR;
    652 	expected_si_band = POLLERR;
    653 	si_band_mask = ~0;
    654 
    655 	/*
    656 	 * SIGIO is delivered with:
    657 	 *
    658 	 * - si_fd = -1
    659 	 * - si_code = POLL_OUT (2)
    660 	 * - si_band = POLLOUT|POLLWRNORM (0x40, POLLWRNORM is just an alias)
    661 	 *
    662 	 * which (except for the fd part) is consistent with sockets
    663 	 * but not consistent with the POSIX.1-2024 definition of
    664 	 * POLLERR, and is the same issue as various poll-related
    665 	 * POLLHUP things.
    666 	 */
    667 	atf_tc_expect_fail("PR kern/59056: poll POLLHUP bugs");
    668 
    669 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    670 	(void)pthread_barrier_wait(&bar);
    671 	wait_for_io(&omask);
    672 	check_write_fail(fifowr, EPIPE);
    673 
    674 	RZ(pthread_join(t, NULL));
    675 }
    676 
    677 ATF_TC(pipe_read);
    678 ATF_TC_HEAD(pipe_read, tc)
    679 {
    680 	atf_tc_set_md_var(tc, "descr", "Test SIGIO when pipe is readable");
    681 }
    682 ATF_TC_BODY(pipe_read, tc)
    683 {
    684 	sigset_t omask;
    685 	int pipefd[2], flags;
    686 	pthread_barrier_t bar;
    687 	struct context ctx, *C = &ctx;
    688 	pthread_t t;
    689 
    690 	setup(&omask);
    691 
    692 	RL(pipe(pipefd));
    693 	RL(fcntl(pipefd[0], F_SETOWN, getpid()));
    694 	RL(flags = fcntl(pipefd[0], F_GETFL));
    695 	RL(fcntl(pipefd[0], F_SETFL, flags | O_ASYNC));
    696 
    697 	RZ(pthread_barrier_init(&bar, NULL, 2));
    698 
    699 	memset(C, 0, sizeof(*C));
    700 	C->bar = &bar;
    701 	C->fd = pipefd[1];
    702 	RZ(pthread_create(&t, NULL, &start_writer, C));
    703 
    704 	expected_si_fd = pipefd[0];
    705 	expected_si_code = POLL_IN;
    706 	expected_si_band = POLLIN|POLLRDNORM;
    707 	si_band_mask = ~0;
    708 
    709 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    710 	(void)pthread_barrier_wait(&bar);
    711 	wait_for_io(&omask);
    712 	check_read(pipefd[0]);
    713 
    714 	RZ(pthread_join(t, NULL));
    715 }
    716 
    717 ATF_TC(pipe_read_closed);
    718 ATF_TC_HEAD(pipe_read_closed, tc)
    719 {
    720 	atf_tc_set_md_var(tc, "descr",
    721 	    "Test SIGIO when pipe is readable because closed");
    722 }
    723 ATF_TC_BODY(pipe_read_closed, tc)
    724 {
    725 	sigset_t omask;
    726 	int pipefd[2], flags;
    727 	pthread_barrier_t bar;
    728 	struct context ctx, *C = &ctx;
    729 	pthread_t t;
    730 
    731 	setup(&omask);
    732 
    733 	RL(pipe(pipefd));
    734 	RL(fcntl(pipefd[0], F_SETOWN, getpid()));
    735 	RL(flags = fcntl(pipefd[0], F_GETFL));
    736 	RL(fcntl(pipefd[0], F_SETFL, flags | O_ASYNC));
    737 
    738 	RZ(pthread_barrier_init(&bar, NULL, 2));
    739 
    740 	memset(C, 0, sizeof(*C));
    741 	C->bar = &bar;
    742 	C->fd = pipefd[1];
    743 	RZ(pthread_create(&t, NULL, &start_closer, C));
    744 
    745 	expected_si_fd = pipefd[0];
    746 	expected_si_code = POLL_HUP;
    747 	expected_si_band = POLLHUP;
    748 	si_band_mask = ~0;
    749 
    750 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    751 	(void)pthread_barrier_wait(&bar);
    752 	wait_for_io(&omask);
    753 	check_read_eof(pipefd[0]);
    754 
    755 	RZ(pthread_join(t, NULL));
    756 }
    757 
    758 ATF_TC(pipe_write);
    759 ATF_TC_HEAD(pipe_write, tc)
    760 {
    761 	atf_tc_set_md_var(tc, "descr", "Test SIGIO when pipe is writable");
    762 }
    763 ATF_TC_BODY(pipe_write, tc)
    764 {
    765 	sigset_t omask;
    766 	int pipefd[2], flags;
    767 	pthread_barrier_t bar;
    768 	struct context ctx, *C = &ctx;
    769 	pthread_t t;
    770 
    771 	setup(&omask);
    772 
    773 	RL(pipe(pipefd));
    774 	fillpipebuf(pipefd[1]);
    775 	RL(fcntl(pipefd[1], F_SETOWN, getpid()));
    776 	RL(flags = fcntl(pipefd[1], F_GETFL));
    777 	RL(fcntl(pipefd[1], F_SETFL, flags | O_ASYNC));
    778 
    779 	RZ(pthread_barrier_init(&bar, NULL, 2));
    780 
    781 	memset(C, 0, sizeof(*C));
    782 	C->bar = &bar;
    783 	C->fd = pipefd[0];
    784 	C->lowat = PIPE_BUF;
    785 	RZ(pthread_create(&t, NULL, &start_reader, C));
    786 
    787 	expected_si_fd = pipefd[1];
    788 	expected_si_code = POLL_OUT;
    789 	expected_si_band = POLLOUT|POLLWRNORM;
    790 	si_band_mask = ~0;
    791 
    792 	/*
    793 	 * OK, not quite the same bug but we're tracking a bunch of
    794 	 * related issues there...  The wrong fd is reported in SIGIO,
    795 	 * because the sys_pipe.c logic has a bunch of variables
    796 	 * backwards and confused about which side of the pipe is
    797 	 * which.
    798 	 */
    799 	atf_tc_expect_fail("PR kern/59056: poll POLLHUP bugs");
    800 
    801 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    802 	(void)pthread_barrier_wait(&bar);
    803 	wait_for_io(&omask);
    804 	check_write(pipefd[1]);
    805 
    806 	RZ(pthread_join(t, NULL));
    807 }
    808 
    809 ATF_TC(pipe_write_closed);
    810 ATF_TC_HEAD(pipe_write_closed, tc)
    811 {
    812 	atf_tc_set_md_var(tc, "descr",
    813 	    "Test SIGIO when pipe is writable because closed");
    814 }
    815 ATF_TC_BODY(pipe_write_closed, tc)
    816 {
    817 	sigset_t omask;
    818 	int pipefd[2], flags;
    819 	pthread_barrier_t bar;
    820 	struct context ctx, *C = &ctx;
    821 	pthread_t t;
    822 
    823 	setup(&omask);
    824 
    825 	RL(pipe(pipefd));
    826 	fillpipebuf(pipefd[1]);
    827 	RL(fcntl(pipefd[1], F_SETOWN, getpid()));
    828 	RL(flags = fcntl(pipefd[1], F_GETFL));
    829 	RL(fcntl(pipefd[1], F_SETFL, flags | O_ASYNC));
    830 
    831 	RZ(pthread_barrier_init(&bar, NULL, 2));
    832 
    833 	memset(C, 0, sizeof(*C));
    834 	C->bar = &bar;
    835 	C->fd = pipefd[0];
    836 	RZ(pthread_create(&t, NULL, &start_closer, C));
    837 
    838 	expected_si_fd = pipefd[1];
    839 	expected_si_code = POLL_ERR;
    840 	expected_si_band = POLLERR;
    841 	si_band_mask = ~0;
    842 
    843 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    844 	(void)pthread_barrier_wait(&bar);
    845 	atf_tc_expect_fail("PR kern/59056: poll POLLHUP bugs");
    846 	wait_for_io(&omask);
    847 	check_write_fail(pipefd[1], EPIPE);
    848 
    849 	RZ(pthread_join(t, NULL));
    850 }
    851 
    852 ATF_TC(ptyapp_read);
    853 ATF_TC_HEAD(ptyapp_read, tc)
    854 {
    855 	atf_tc_set_md_var(tc, "descr",
    856 	    "Test SIGIO when app side of pty is readable");
    857 }
    858 ATF_TC_BODY(ptyapp_read, tc)
    859 {
    860 	sigset_t omask;
    861 	int ptyhost, ptyapp, flags;
    862 	pthread_barrier_t bar;
    863 	struct context ctx, *C = &ctx;
    864 	pthread_t t;
    865 
    866 	setup(&omask);
    867 
    868 	ptysetup(&ptyhost, &ptyapp);
    869 	RL(fcntl(ptyapp, F_SETOWN, getpid()));
    870 	RL(flags = fcntl(ptyapp, F_GETFL));
    871 	RL(fcntl(ptyapp, F_SETFL, flags | O_ASYNC));
    872 
    873 	RZ(pthread_barrier_init(&bar, NULL, 2));
    874 
    875 	memset(C, 0, sizeof(*C));
    876 	C->bar = &bar;
    877 	C->fd = ptyhost;
    878 	RZ(pthread_create(&t, NULL, &start_writer, C));
    879 
    880 	expected_si_fd = ptyapp;
    881 	expected_si_code = POLL_IN;
    882 	expected_si_band = POLLIN|POLLRDNORM;
    883 	si_band_mask = ~0;
    884 
    885 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    886 	(void)pthread_barrier_wait(&bar);
    887 	atf_tc_expect_fail("PR kern/60783: tty: missing siginfo_t in SIGIO");
    888 	wait_for_io(&omask);
    889 	check_read(ptyapp);
    890 
    891 	RZ(pthread_join(t, NULL));
    892 }
    893 
    894 ATF_TC(ptyapp_read_closed);
    895 ATF_TC_HEAD(ptyapp_read_closed, tc)
    896 {
    897 	atf_tc_set_md_var(tc, "descr",
    898 	    "Test SIGIO when app side of pty is readable because host closed");
    899 }
    900 ATF_TC_BODY(ptyapp_read_closed, tc)
    901 {
    902 	sigset_t omask;
    903 	int ptyhost, ptyapp, flags;
    904 	pthread_barrier_t bar;
    905 	struct context ctx, *C = &ctx;
    906 	pthread_t t;
    907 
    908 	setup(&omask);
    909 
    910 	ptysetup(&ptyhost, &ptyapp);
    911 	RL(fcntl(ptyapp, F_SETOWN, getpid()));
    912 	RL(flags = fcntl(ptyapp, F_GETFL));
    913 	RL(fcntl(ptyapp, F_SETFL, flags | O_ASYNC));
    914 
    915 	RZ(pthread_barrier_init(&bar, NULL, 2));
    916 
    917 	memset(C, 0, sizeof(*C));
    918 	C->bar = &bar;
    919 	C->fd = ptyhost;
    920 	RZ(pthread_create(&t, NULL, &start_closer, C));
    921 
    922 	expected_si_fd = ptyapp;
    923 	expected_si_code = POLL_HUP;
    924 	expected_si_band = POLLHUP;
    925 	si_band_mask = ~0;
    926 
    927 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    928 	(void)pthread_barrier_wait(&bar);
    929 	atf_tc_expect_fail("PR kern/60783: tty: missing siginfo_t in SIGIO");
    930 	wait_for_io(&omask);
    931 	check_read_eof(ptyapp);
    932 
    933 	RZ(pthread_join(t, NULL));
    934 }
    935 
    936 ATF_TC(ptyapp_write);
    937 ATF_TC_HEAD(ptyapp_write, tc)
    938 {
    939 	atf_tc_set_md_var(tc, "descr",
    940 	    "Test SIGIO when app side of pty is writable");
    941 }
    942 ATF_TC_BODY(ptyapp_write, tc)
    943 {
    944 	sigset_t omask;
    945 	int ptyhost, ptyapp, flags;
    946 	pthread_barrier_t bar;
    947 	struct context ctx, *C = &ctx;
    948 	pthread_t t;
    949 
    950 	setup(&omask);
    951 
    952 	ptysetup(&ptyhost, &ptyapp);
    953 	fillpipebuf(ptyapp);
    954 	RL(fcntl(ptyapp, F_SETOWN, getpid()));
    955 	RL(flags = fcntl(ptyapp, F_GETFL));
    956 	RL(fcntl(ptyapp, F_SETFL, flags | O_ASYNC));
    957 
    958 	RZ(pthread_barrier_init(&bar, NULL, 2));
    959 
    960 	memset(C, 0, sizeof(*C));
    961 	C->bar = &bar;
    962 	C->fd = ptyhost;
    963 	RL(ioctl(ptyapp, TIOCGQSIZE, &C->lowat));
    964 	RZ(pthread_create(&t, NULL, &start_reader, C));
    965 
    966 	expected_si_fd = ptyapp;
    967 	expected_si_code = POLL_OUT;
    968 	expected_si_band = POLLOUT|POLLWRNORM;
    969 	si_band_mask = ~0;
    970 
    971 	atf_tc_expect_signal(SIGALRM, "PR kern/60784:"
    972 	    " pty doesn't deliver SIGIO when writable");
    973 
    974 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
    975 	(void)pthread_barrier_wait(&bar);
    976 	wait_for_io(&omask);
    977 	check_write(ptyapp);
    978 
    979 	RZ(pthread_join(t, NULL));
    980 }
    981 
    982 ATF_TC(ptyapp_write_closed);
    983 ATF_TC_HEAD(ptyapp_write_closed, tc)
    984 {
    985 	atf_tc_set_md_var(tc, "descr",
    986 	    "Test SIGIO when app side of pty is writable because host closed");
    987 }
    988 ATF_TC_BODY(ptyapp_write_closed, tc)
    989 {
    990 	sigset_t omask;
    991 	int ptyhost, ptyapp, flags;
    992 	pthread_barrier_t bar;
    993 	struct context ctx, *C = &ctx;
    994 	pthread_t t;
    995 
    996 	setup(&omask);
    997 
    998 	ptysetup(&ptyhost, &ptyapp);
    999 	fillpipebuf(ptyapp);
   1000 	RL(fcntl(ptyapp, F_SETOWN, getpid()));
   1001 	RL(flags = fcntl(ptyapp, F_GETFL));
   1002 	RL(fcntl(ptyapp, F_SETFL, flags | O_ASYNC));
   1003 
   1004 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1005 
   1006 	memset(C, 0, sizeof(*C));
   1007 	C->bar = &bar;
   1008 	C->fd = ptyhost;
   1009 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1010 
   1011 	expected_si_fd = ptyapp;
   1012 	expected_si_code = POLL_ERR;
   1013 	expected_si_band = POLLERR;
   1014 	si_band_mask = ~0;
   1015 
   1016 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1017 	(void)pthread_barrier_wait(&bar);
   1018 	atf_tc_expect_fail("PR kern/60783: tty: missing siginfo_t in SIGIO");
   1019 	wait_for_io(&omask);
   1020 	check_write_fail(ptyapp, EPIPE);
   1021 
   1022 	RZ(pthread_join(t, NULL));
   1023 }
   1024 
   1025 ATF_TC(ptyhost_read);
   1026 ATF_TC_HEAD(ptyhost_read, tc)
   1027 {
   1028 	atf_tc_set_md_var(tc, "descr",
   1029 	    "Test SIGIO when host side of pty is readable");
   1030 }
   1031 ATF_TC_BODY(ptyhost_read, tc)
   1032 {
   1033 	sigset_t omask;
   1034 	int ptyhost, ptyapp, flags;
   1035 	pthread_barrier_t bar;
   1036 	struct context ctx, *C = &ctx;
   1037 	pthread_t t;
   1038 
   1039 	setup(&omask);
   1040 
   1041 	ptysetup(&ptyhost, &ptyapp);
   1042 	RL(fcntl(ptyhost, F_SETOWN, getpid()));
   1043 	RL(flags = fcntl(ptyhost, F_GETFL));
   1044 	RL(fcntl(ptyhost, F_SETFL, flags | O_ASYNC));
   1045 
   1046 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1047 
   1048 	memset(C, 0, sizeof(*C));
   1049 	C->bar = &bar;
   1050 	C->fd = ptyapp;
   1051 	RZ(pthread_create(&t, NULL, &start_writer, C));
   1052 
   1053 	expected_si_fd = ptyhost;
   1054 	expected_si_code = POLL_IN;
   1055 	expected_si_band = POLLIN|POLLRDNORM;
   1056 	si_band_mask = ~0;
   1057 
   1058 	atf_tc_expect_signal(SIGALRM, "PR kern/60784:"
   1059 	    " pty doesn't deliver SIGIO when writable");
   1060 
   1061 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1062 	(void)pthread_barrier_wait(&bar);
   1063 	wait_for_io(&omask);
   1064 	check_read(ptyhost);
   1065 
   1066 	RZ(pthread_join(t, NULL));
   1067 }
   1068 
   1069 ATF_TC(ptyhost_read_closed);
   1070 ATF_TC_HEAD(ptyhost_read_closed, tc)
   1071 {
   1072 	atf_tc_set_md_var(tc, "descr",
   1073 	    "Test SIGIO when host side of pty is readable because app closed");
   1074 }
   1075 ATF_TC_BODY(ptyhost_read_closed, tc)
   1076 {
   1077 	sigset_t omask;
   1078 	int ptyhost, ptyapp, flags;
   1079 	pthread_barrier_t bar;
   1080 	struct context ctx, *C = &ctx;
   1081 	pthread_t t;
   1082 
   1083 	setup(&omask);
   1084 
   1085 	ptysetup(&ptyhost, &ptyapp);
   1086 	RL(fcntl(ptyhost, F_SETOWN, getpid()));
   1087 	RL(flags = fcntl(ptyhost, F_GETFL));
   1088 	RL(fcntl(ptyhost, F_SETFL, flags | O_ASYNC));
   1089 
   1090 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1091 
   1092 	memset(C, 0, sizeof(*C));
   1093 	C->bar = &bar;
   1094 	C->fd = ptyapp;
   1095 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1096 
   1097 	expected_si_fd = ptyhost;
   1098 	expected_si_code = POLL_HUP;
   1099 	expected_si_band = POLLHUP;
   1100 	si_band_mask = ~0;
   1101 
   1102 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1103 	(void)pthread_barrier_wait(&bar);
   1104 	atf_tc_expect_fail("PR kern/60783: tty: missing siginfo_t in SIGIO");
   1105 	wait_for_io(&omask);
   1106 	check_read_eof(ptyhost);
   1107 
   1108 	RZ(pthread_join(t, NULL));
   1109 }
   1110 
   1111 ATF_TC(ptyhost_write);
   1112 ATF_TC_HEAD(ptyhost_write, tc)
   1113 {
   1114 	atf_tc_set_md_var(tc, "descr",
   1115 	    "Test SIGIO when host side of pty is writable");
   1116 }
   1117 ATF_TC_BODY(ptyhost_write, tc)
   1118 {
   1119 	sigset_t omask;
   1120 	int ptyhost, ptyapp, flags;
   1121 	pthread_barrier_t bar;
   1122 	struct context ctx, *C = &ctx;
   1123 	pthread_t t;
   1124 
   1125 	setup(&omask);
   1126 
   1127 	ptysetup(&ptyhost, &ptyapp);
   1128 	fillpipebuf(ptyhost);
   1129 	RL(fcntl(ptyhost, F_SETOWN, getpid()));
   1130 	RL(flags = fcntl(ptyhost, F_GETFL));
   1131 	RL(fcntl(ptyhost, F_SETFL, flags | O_ASYNC));
   1132 
   1133 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1134 
   1135 	memset(C, 0, sizeof(*C));
   1136 	C->bar = &bar;
   1137 	C->fd = ptyapp;
   1138 	RL(ioctl(ptyhost, TIOCGQSIZE, &C->lowat));
   1139 	RZ(pthread_create(&t, NULL, &start_reader, C));
   1140 
   1141 	expected_si_fd = ptyhost;
   1142 	expected_si_code = POLL_OUT;
   1143 	expected_si_band = POLLOUT|POLLWRNORM;
   1144 	si_band_mask = ~0;
   1145 
   1146 	atf_tc_expect_signal(SIGALRM, "PR kern/60784:"
   1147 	    " pty doesn't deliver SIGIO when writable");
   1148 
   1149 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1150 	(void)pthread_barrier_wait(&bar);
   1151 	wait_for_io(&omask);
   1152 	check_write(ptyhost);
   1153 
   1154 	RZ(pthread_join(t, NULL));
   1155 }
   1156 
   1157 ATF_TC(ptyhost_write_closed);
   1158 ATF_TC_HEAD(ptyhost_write_closed, tc)
   1159 {
   1160 	atf_tc_set_md_var(tc, "descr",
   1161 	    "Test SIGIO when host side of pty is writable because app closed");
   1162 }
   1163 ATF_TC_BODY(ptyhost_write_closed, tc)
   1164 {
   1165 	sigset_t omask;
   1166 	int ptyhost, ptyapp, flags;
   1167 	pthread_barrier_t bar;
   1168 	struct context ctx, *C = &ctx;
   1169 	pthread_t t;
   1170 
   1171 	setup(&omask);
   1172 
   1173 	ptysetup(&ptyhost, &ptyapp);
   1174 	fillpipebuf(ptyhost);
   1175 	RL(fcntl(ptyhost, F_SETOWN, getpid()));
   1176 	RL(flags = fcntl(ptyhost, F_GETFL));
   1177 	RL(fcntl(ptyhost, F_SETFL, flags | O_ASYNC));
   1178 
   1179 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1180 
   1181 	memset(C, 0, sizeof(*C));
   1182 	C->bar = &bar;
   1183 	C->fd = ptyapp;
   1184 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1185 
   1186 	expected_si_fd = ptyhost;
   1187 	expected_si_code = POLL_ERR;
   1188 	expected_si_band = POLLERR;
   1189 	si_band_mask = ~0;
   1190 
   1191 	atf_tc_expect_signal(SIGALRM, "PR kern/60784:"
   1192 	    " pty doesn't deliver SIGIO when writable");
   1193 
   1194 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1195 	(void)pthread_barrier_wait(&bar);
   1196 	wait_for_io(&omask);
   1197 	check_write_fail(ptyhost, EPIPE);
   1198 
   1199 	RZ(pthread_join(t, NULL));
   1200 }
   1201 
   1202 ATF_TC(socket_inet6_read);
   1203 ATF_TC_HEAD(socket_inet6_read, tc)
   1204 {
   1205 	atf_tc_set_md_var(tc, "descr",
   1206 	    "Test SIGIO when socket is readable");
   1207 }
   1208 ATF_TC_BODY(socket_inet6_read, tc)
   1209 {
   1210 	sigset_t omask;
   1211 	int sockfd[2], flags;
   1212 	pthread_barrier_t bar;
   1213 	struct context ctx, *C = &ctx;
   1214 	pthread_t t;
   1215 
   1216 	setup(&omask);
   1217 
   1218 	socksetup(sockfd, AF_INET6, "::1");
   1219 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1220 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1221 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1222 
   1223 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1224 
   1225 	memset(C, 0, sizeof(*C));
   1226 	C->bar = &bar;
   1227 	C->fd = sockfd[1];
   1228 	RZ(pthread_create(&t, NULL, &start_writer, C));
   1229 
   1230 	expected_si_fd = sockfd[0];
   1231 	expected_si_code = POLL_IN;
   1232 	expected_si_band = POLLIN|POLLRDNORM;
   1233 	si_band_mask = ~0;
   1234 
   1235 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1236 	(void)pthread_barrier_wait(&bar);
   1237 	wait_for_io(&omask);
   1238 	check_read(sockfd[0]);
   1239 
   1240 	RZ(pthread_join(t, NULL));
   1241 }
   1242 
   1243 ATF_TC(socket_inet6_read_closed);
   1244 ATF_TC_HEAD(socket_inet6_read_closed, tc)
   1245 {
   1246 	atf_tc_set_md_var(tc, "descr",
   1247 	    "Test SIGIO when socket is readable because closed");
   1248 }
   1249 ATF_TC_BODY(socket_inet6_read_closed, tc)
   1250 {
   1251 	sigset_t omask;
   1252 	int sockfd[2], flags;
   1253 	pthread_barrier_t bar;
   1254 	struct context ctx, *C = &ctx;
   1255 	pthread_t t;
   1256 
   1257 	setup(&omask);
   1258 
   1259 	socksetup(sockfd, AF_INET6, "::1");
   1260 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1261 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1262 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1263 
   1264 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1265 
   1266 	memset(C, 0, sizeof(*C));
   1267 	C->bar = &bar;
   1268 	C->fd = sockfd[1];
   1269 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1270 
   1271 	/*
   1272 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1273 	 * pipes?  POLLIN is right in one sense that we can read
   1274 	 * without blocking, and it will report EOF -- but that is also
   1275 	 * the scenario in which POLLHUP is right.
   1276 	 */
   1277 	expected_si_fd = sockfd[0];
   1278 	expected_si_code = POLL_IN;
   1279 	expected_si_band = POLLIN|POLLRDNORM;
   1280 	si_band_mask = ~0;
   1281 
   1282 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1283 	(void)pthread_barrier_wait(&bar);
   1284 	wait_for_io(&omask);
   1285 	check_read_eof(sockfd[0]);
   1286 
   1287 	RZ(pthread_join(t, NULL));
   1288 }
   1289 
   1290 ATF_TC(socket_inet6_read_shutdown);
   1291 ATF_TC_HEAD(socket_inet6_read_shutdown, tc)
   1292 {
   1293 	atf_tc_set_md_var(tc, "descr",
   1294 	    "Test SIGIO when socket is readable because shutdown");
   1295 }
   1296 ATF_TC_BODY(socket_inet6_read_shutdown, tc)
   1297 {
   1298 	sigset_t omask;
   1299 	int sockfd[2], flags;
   1300 	pthread_barrier_t bar;
   1301 	struct context ctx, *C = &ctx;
   1302 	pthread_t t;
   1303 
   1304 	setup(&omask);
   1305 
   1306 	socksetup(sockfd, AF_INET6, "::1");
   1307 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1308 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1309 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1310 
   1311 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1312 
   1313 	memset(C, 0, sizeof(*C));
   1314 	C->bar = &bar;
   1315 	C->fd = sockfd[1];
   1316 	RZ(pthread_create(&t, NULL, &start_shutdownwr, C));
   1317 
   1318 	/*
   1319 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1320 	 * pipes?  POLLIN is right in one sense that we can read
   1321 	 * without blocking, and it will report EOF -- but that is also
   1322 	 * the scenario in which POLLHUP is right.
   1323 	 */
   1324 	expected_si_fd = sockfd[0];
   1325 	expected_si_code = POLL_IN;
   1326 	expected_si_band = POLLIN|POLLRDNORM;
   1327 	si_band_mask = ~0;
   1328 
   1329 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1330 	(void)pthread_barrier_wait(&bar);
   1331 	wait_for_io(&omask);
   1332 	check_read_eof(sockfd[0]);
   1333 
   1334 	RZ(pthread_join(t, NULL));
   1335 }
   1336 
   1337 ATF_TC(socket_inet6_write);
   1338 ATF_TC_HEAD(socket_inet6_write, tc)
   1339 {
   1340 	atf_tc_set_md_var(tc, "descr",
   1341 	    "Test SIGIO when socket is writable");
   1342 }
   1343 ATF_TC_BODY(socket_inet6_write, tc)
   1344 {
   1345 	sigset_t omask;
   1346 	int sockfd[2], flags;
   1347 	pthread_barrier_t bar;
   1348 	struct context ctx, *C = &ctx;
   1349 	socklen_t optlen;
   1350 	pthread_t t;
   1351 
   1352 	setup(&omask);
   1353 
   1354 	socksetup(sockfd, AF_INET6, "::1");
   1355 	fillpipebuf(sockfd[1]);
   1356 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1357 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1358 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1359 
   1360 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1361 
   1362 	memset(C, 0, sizeof(*C));
   1363 	C->bar = &bar;
   1364 	C->fd = sockfd[0];
   1365 	optlen = sizeof(C->lowat);
   1366 	RL(getsockopt(sockfd[1], SOL_SOCKET, SO_SNDLOWAT, &C->lowat, &optlen));
   1367 	ATF_REQUIRE_EQ_MSG(optlen, sizeof(C->lowat),
   1368 	    "optlen=%zu sizeof(C->lowat)=%zu",
   1369 	    (size_t)optlen, sizeof(C->lowat));
   1370 
   1371 	RZ(pthread_create(&t, NULL, &start_reader, C));
   1372 
   1373 	expected_si_fd = sockfd[1];
   1374 	expected_si_code = POLL_OUT;
   1375 	expected_si_band = POLLOUT|POLLWRNORM;
   1376 	si_band_mask = ~0;
   1377 
   1378 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1379 	(void)pthread_barrier_wait(&bar);
   1380 	wait_for_io(&omask);
   1381 	check_sendnonblock(sockfd[1]);
   1382 	check_write(sockfd[1]);
   1383 
   1384 	RZ(pthread_join(t, NULL));
   1385 }
   1386 
   1387 ATF_TC(socket_inet6_write_closed);
   1388 ATF_TC_HEAD(socket_inet6_write_closed, tc)
   1389 {
   1390 	atf_tc_set_md_var(tc, "descr",
   1391 	    "Test SIGIO when socket is writable because closed");
   1392 }
   1393 ATF_TC_BODY(socket_inet6_write_closed, tc)
   1394 {
   1395 	sigset_t omask;
   1396 	int sockfd[2], flags;
   1397 	pthread_barrier_t bar;
   1398 	struct context ctx, *C = &ctx;
   1399 	pthread_t t;
   1400 
   1401 	setup(&omask);
   1402 
   1403 	socksetup(sockfd, AF_INET6, "::1");
   1404 	fillpipebuf(sockfd[1]);
   1405 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1406 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1407 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1408 
   1409 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1410 
   1411 	memset(C, 0, sizeof(*C));
   1412 	C->bar = &bar;
   1413 	C->fd = sockfd[0];
   1414 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1415 
   1416 	/*
   1417 	 * We get POLLIN|POLLRDNORM because the TCP peer has sent FIN
   1418 	 * to indicate they won't write any more, which will manifest
   1419 	 * on our end by read/recv reporting EOF; TCP has no mechanism
   1420 	 * to indicate that they won't _read_ any more as in POLLERR.
   1421 	 *
   1422 	 * XXX However, the peer may ack some data, leading us to think
   1423 	 * we can write more.  So, to avoid a flaky test, let's shut
   1424 	 * down in the write direction.  Except, oops, that doesn't
   1425 	 * work because of PR kern/60786: poll reports socket writable
   1426 	 * after shutdown(SHUT_WR).  And while we could si_band for
   1427 	 * POLLIN, we don't get all bits at once like poll() does; we
   1428 	 * get _either_ code=POLL_IN band=POLLIN|POLLRDNORM _or_
   1429 	 * code=POLL_OUT band=POLLOUT|POLLWRNORM.
   1430 	 *
   1431 	 * I give up; I'll just not test the code and band for now...
   1432 	 */
   1433 	RL(shutdown(sockfd[1], SHUT_WR));
   1434 	expected_si_fd = sockfd[1];
   1435 	expected_si_code = -1;	/* POLL_IN */
   1436 	expected_si_band = 0;	/* POLLIN|POLLRDNORM */
   1437 	si_band_mask = 0;
   1438 
   1439 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1440 	(void)pthread_barrier_wait(&bar);
   1441 	wait_for_io(&omask);
   1442 	check_write_fail(sockfd[1], EPIPE);
   1443 
   1444 	RZ(pthread_join(t, NULL));
   1445 }
   1446 
   1447 ATF_TC(socket_inet6_write_shutdown);
   1448 ATF_TC_HEAD(socket_inet6_write_shutdown, tc)
   1449 {
   1450 	atf_tc_set_md_var(tc, "descr",
   1451 	    "Test SIGIO when socket is writable because shutdown");
   1452 }
   1453 ATF_TC_BODY(socket_inet6_write_shutdown, tc)
   1454 {
   1455 	sigset_t omask;
   1456 	int sockfd[2], flags;
   1457 	pthread_barrier_t bar;
   1458 	struct context ctx, *C = &ctx;
   1459 	pthread_t t;
   1460 
   1461 	setup(&omask);
   1462 
   1463 	socksetup(sockfd, AF_INET6, "::1");
   1464 	fillpipebuf(sockfd[1]);
   1465 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1466 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1467 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1468 
   1469 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1470 
   1471 	memset(C, 0, sizeof(*C));
   1472 	C->bar = &bar;
   1473 	C->fd = sockfd[0];
   1474 	RZ(pthread_create(&t, NULL, &start_shutdownrd, C));
   1475 
   1476 	/*
   1477 	 * TCP doesn't have a way to say `I will discard any further
   1478 	 * bytes you throw at me, so please stop throwing them'.  But
   1479 	 * it can ack some data making us think we can continue sending
   1480 	 * more.
   1481 	 */
   1482 	expected_si_fd = sockfd[1];
   1483 	expected_si_code = POLL_OUT;
   1484 	expected_si_band = POLLOUT|POLLWRNORM;
   1485 	si_band_mask = ~0;
   1486 
   1487 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1488 	(void)pthread_barrier_wait(&bar);
   1489 	wait_for_io(&omask);
   1490 	check_write(sockfd[1]);
   1491 
   1492 	RZ(pthread_join(t, NULL));
   1493 }
   1494 
   1495 ATF_TC(socket_inet_read);
   1496 ATF_TC_HEAD(socket_inet_read, tc)
   1497 {
   1498 	atf_tc_set_md_var(tc, "descr",
   1499 	    "Test SIGIO when socket is readable");
   1500 }
   1501 ATF_TC_BODY(socket_inet_read, tc)
   1502 {
   1503 	sigset_t omask;
   1504 	int sockfd[2], flags;
   1505 	pthread_barrier_t bar;
   1506 	struct context ctx, *C = &ctx;
   1507 	pthread_t t;
   1508 
   1509 	setup(&omask);
   1510 
   1511 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1512 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1513 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1514 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1515 
   1516 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1517 
   1518 	memset(C, 0, sizeof(*C));
   1519 	C->bar = &bar;
   1520 	C->fd = sockfd[1];
   1521 	RZ(pthread_create(&t, NULL, &start_writer, C));
   1522 
   1523 	expected_si_fd = sockfd[0];
   1524 	expected_si_code = POLL_IN;
   1525 	expected_si_band = POLLIN|POLLRDNORM;
   1526 	si_band_mask = ~0;
   1527 
   1528 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1529 	(void)pthread_barrier_wait(&bar);
   1530 	wait_for_io(&omask);
   1531 	check_read(sockfd[0]);
   1532 
   1533 	RZ(pthread_join(t, NULL));
   1534 }
   1535 
   1536 ATF_TC(socket_inet_read_closed);
   1537 ATF_TC_HEAD(socket_inet_read_closed, tc)
   1538 {
   1539 	atf_tc_set_md_var(tc, "descr",
   1540 	    "Test SIGIO when socket is readable because closed");
   1541 }
   1542 ATF_TC_BODY(socket_inet_read_closed, tc)
   1543 {
   1544 	sigset_t omask;
   1545 	int sockfd[2], flags;
   1546 	pthread_barrier_t bar;
   1547 	struct context ctx, *C = &ctx;
   1548 	pthread_t t;
   1549 
   1550 	setup(&omask);
   1551 
   1552 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1553 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1554 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1555 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1556 
   1557 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1558 
   1559 	memset(C, 0, sizeof(*C));
   1560 	C->bar = &bar;
   1561 	C->fd = sockfd[1];
   1562 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1563 
   1564 	/*
   1565 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1566 	 * pipes?  POLLIN is right in one sense that we can read
   1567 	 * without blocking, and it will report EOF -- but that is also
   1568 	 * the scenario in which POLLHUP is right.
   1569 	 */
   1570 	expected_si_fd = sockfd[0];
   1571 	expected_si_code = POLL_IN;
   1572 	expected_si_band = POLLIN|POLLRDNORM;
   1573 	si_band_mask = ~0;
   1574 
   1575 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1576 	(void)pthread_barrier_wait(&bar);
   1577 	wait_for_io(&omask);
   1578 	check_read_eof(sockfd[0]);
   1579 
   1580 	RZ(pthread_join(t, NULL));
   1581 }
   1582 
   1583 ATF_TC(socket_inet_read_shutdown);
   1584 ATF_TC_HEAD(socket_inet_read_shutdown, tc)
   1585 {
   1586 	atf_tc_set_md_var(tc, "descr",
   1587 	    "Test SIGIO when socket is readable because shutdown");
   1588 }
   1589 ATF_TC_BODY(socket_inet_read_shutdown, tc)
   1590 {
   1591 	sigset_t omask;
   1592 	int sockfd[2], flags;
   1593 	pthread_barrier_t bar;
   1594 	struct context ctx, *C = &ctx;
   1595 	pthread_t t;
   1596 
   1597 	setup(&omask);
   1598 
   1599 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1600 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1601 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1602 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1603 
   1604 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1605 
   1606 	memset(C, 0, sizeof(*C));
   1607 	C->bar = &bar;
   1608 	C->fd = sockfd[1];
   1609 	RZ(pthread_create(&t, NULL, &start_shutdownwr, C));
   1610 
   1611 	/*
   1612 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1613 	 * pipes?  POLLIN is right in one sense that we can read
   1614 	 * without blocking, and it will report EOF -- but that is also
   1615 	 * the scenario in which POLLHUP is right.
   1616 	 */
   1617 	expected_si_fd = sockfd[0];
   1618 	expected_si_code = POLL_IN;
   1619 	expected_si_band = POLLIN|POLLRDNORM;
   1620 	si_band_mask = ~0;
   1621 
   1622 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1623 	(void)pthread_barrier_wait(&bar);
   1624 	wait_for_io(&omask);
   1625 	check_read_eof(sockfd[0]);
   1626 
   1627 	RZ(pthread_join(t, NULL));
   1628 }
   1629 
   1630 ATF_TC(socket_inet_write);
   1631 ATF_TC_HEAD(socket_inet_write, tc)
   1632 {
   1633 	atf_tc_set_md_var(tc, "descr",
   1634 	    "Test SIGIO when socket is writable");
   1635 }
   1636 ATF_TC_BODY(socket_inet_write, tc)
   1637 {
   1638 	sigset_t omask;
   1639 	int sockfd[2], flags;
   1640 	pthread_barrier_t bar;
   1641 	struct context ctx, *C = &ctx;
   1642 	socklen_t optlen;
   1643 	pthread_t t;
   1644 
   1645 	setup(&omask);
   1646 
   1647 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1648 	fillpipebuf(sockfd[1]);
   1649 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1650 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1651 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1652 
   1653 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1654 
   1655 	memset(C, 0, sizeof(*C));
   1656 	C->bar = &bar;
   1657 	C->fd = sockfd[0];
   1658 	optlen = sizeof(C->lowat);
   1659 	RL(getsockopt(sockfd[1], SOL_SOCKET, SO_SNDLOWAT, &C->lowat, &optlen));
   1660 	ATF_REQUIRE_EQ_MSG(optlen, sizeof(C->lowat),
   1661 	    "optlen=%zu sizeof(C->lowat)=%zu",
   1662 	    (size_t)optlen, sizeof(C->lowat));
   1663 
   1664 	RZ(pthread_create(&t, NULL, &start_reader, C));
   1665 
   1666 	expected_si_fd = sockfd[1];
   1667 	expected_si_code = POLL_OUT;
   1668 	expected_si_band = POLLOUT|POLLWRNORM;
   1669 	si_band_mask = ~0;
   1670 
   1671 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1672 	(void)pthread_barrier_wait(&bar);
   1673 	wait_for_io(&omask);
   1674 	check_sendnonblock(sockfd[1]);
   1675 	check_write(sockfd[1]);
   1676 
   1677 	RZ(pthread_join(t, NULL));
   1678 }
   1679 
   1680 ATF_TC(socket_inet_write_closed);
   1681 ATF_TC_HEAD(socket_inet_write_closed, tc)
   1682 {
   1683 	atf_tc_set_md_var(tc, "descr",
   1684 	    "Test SIGIO when socket is writable because closed");
   1685 }
   1686 ATF_TC_BODY(socket_inet_write_closed, tc)
   1687 {
   1688 	sigset_t omask;
   1689 	int sockfd[2], flags;
   1690 	pthread_barrier_t bar;
   1691 	struct context ctx, *C = &ctx;
   1692 	pthread_t t;
   1693 
   1694 	setup(&omask);
   1695 
   1696 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1697 	fillpipebuf(sockfd[1]);
   1698 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1699 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1700 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1701 
   1702 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1703 
   1704 	memset(C, 0, sizeof(*C));
   1705 	C->bar = &bar;
   1706 	C->fd = sockfd[0];
   1707 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1708 
   1709 	/*
   1710 	 * We get POLLIN|POLLRDNORM because the TCP peer has sent FIN
   1711 	 * to indicate they won't write any more, which will manifest
   1712 	 * on our end by read/recv reporting EOF; TCP has no mechanism
   1713 	 * to indicate that they won't _read_ any more as in POLLERR.
   1714 	 *
   1715 	 * XXX However, the peer may ack some data, leading us to think
   1716 	 * we can write more.  So, to avoid a flaky test, let's shut
   1717 	 * down in the write direction.  Except, oops, that doesn't
   1718 	 * work because of PR kern/60786: poll reports socket writable
   1719 	 * after shutdown(SHUT_WR).  And while we could si_band for
   1720 	 * POLLIN, we don't get all bits at once like poll() does; we
   1721 	 * get _either_ code=POLL_IN band=POLLIN|POLLRDNORM _or_
   1722 	 * code=POLL_OUT band=POLLOUT|POLLWRNORM.
   1723 	 *
   1724 	 * I give up; I'll just not test the code and band for now...
   1725 	 */
   1726 	RL(shutdown(sockfd[1], SHUT_WR));
   1727 	expected_si_fd = sockfd[1];
   1728 	expected_si_code = -1;	/* POLL_IN */
   1729 	expected_si_band = 0;	/* POLLIN|POLLRDNORM */
   1730 	si_band_mask = 0;
   1731 
   1732 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1733 	(void)pthread_barrier_wait(&bar);
   1734 	wait_for_io(&omask);
   1735 	check_write_fail(sockfd[1], EPIPE);
   1736 
   1737 	RZ(pthread_join(t, NULL));
   1738 }
   1739 
   1740 ATF_TC(socket_inet_write_shutdown);
   1741 ATF_TC_HEAD(socket_inet_write_shutdown, tc)
   1742 {
   1743 	atf_tc_set_md_var(tc, "descr",
   1744 	    "Test SIGIO when socket is writable because shutdown");
   1745 }
   1746 ATF_TC_BODY(socket_inet_write_shutdown, tc)
   1747 {
   1748 	sigset_t omask;
   1749 	int sockfd[2], flags;
   1750 	pthread_barrier_t bar;
   1751 	struct context ctx, *C = &ctx;
   1752 	pthread_t t;
   1753 
   1754 	setup(&omask);
   1755 
   1756 	socksetup(sockfd, AF_INET, "127.0.0.1");
   1757 	fillpipebuf(sockfd[1]);
   1758 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1759 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1760 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1761 
   1762 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1763 
   1764 	memset(C, 0, sizeof(*C));
   1765 	C->bar = &bar;
   1766 	C->fd = sockfd[0];
   1767 	RZ(pthread_create(&t, NULL, &start_shutdownrd, C));
   1768 
   1769 	/*
   1770 	 * TCP doesn't have a way to say `I will discard any further
   1771 	 * bytes you throw at me, so please stop throwing them', so it
   1772 	 * can't report POLLERR.  But it can ack some data making us
   1773 	 * think we can continue sending more, so it does report
   1774 	 * POLLIN.
   1775 	 */
   1776 	expected_si_fd = sockfd[1];
   1777 	expected_si_code = POLL_OUT;
   1778 	expected_si_band = POLLOUT|POLLWRNORM;
   1779 	si_band_mask = ~0;
   1780 
   1781 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1782 	(void)pthread_barrier_wait(&bar);
   1783 	wait_for_io(&omask);
   1784 	check_write(sockfd[1]);
   1785 
   1786 	RZ(pthread_join(t, NULL));
   1787 }
   1788 
   1789 ATF_TC(socket_local_read);
   1790 ATF_TC_HEAD(socket_local_read, tc)
   1791 {
   1792 	atf_tc_set_md_var(tc, "descr",
   1793 	    "Test SIGIO when socket is readable");
   1794 }
   1795 ATF_TC_BODY(socket_local_read, tc)
   1796 {
   1797 	sigset_t omask;
   1798 	int sockfd[2], flags;
   1799 	pthread_barrier_t bar;
   1800 	struct context ctx, *C = &ctx;
   1801 	pthread_t t;
   1802 
   1803 	setup(&omask);
   1804 
   1805 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   1806 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1807 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1808 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1809 
   1810 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1811 
   1812 	memset(C, 0, sizeof(*C));
   1813 	C->bar = &bar;
   1814 	C->fd = sockfd[1];
   1815 	RZ(pthread_create(&t, NULL, &start_writer, C));
   1816 
   1817 	expected_si_fd = sockfd[0];
   1818 	expected_si_code = POLL_IN;
   1819 	expected_si_band = POLLIN|POLLRDNORM;
   1820 	si_band_mask = ~0;
   1821 
   1822 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1823 	(void)pthread_barrier_wait(&bar);
   1824 	wait_for_io(&omask);
   1825 	check_read(sockfd[0]);
   1826 
   1827 	RZ(pthread_join(t, NULL));
   1828 }
   1829 
   1830 ATF_TC(socket_local_read_closed);
   1831 ATF_TC_HEAD(socket_local_read_closed, tc)
   1832 {
   1833 	atf_tc_set_md_var(tc, "descr",
   1834 	    "Test SIGIO when socket is readable because closed");
   1835 }
   1836 ATF_TC_BODY(socket_local_read_closed, tc)
   1837 {
   1838 	sigset_t omask;
   1839 	int sockfd[2], flags;
   1840 	pthread_barrier_t bar;
   1841 	struct context ctx, *C = &ctx;
   1842 	pthread_t t;
   1843 
   1844 	setup(&omask);
   1845 
   1846 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   1847 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1848 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1849 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1850 
   1851 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1852 
   1853 	memset(C, 0, sizeof(*C));
   1854 	C->bar = &bar;
   1855 	C->fd = sockfd[1];
   1856 	RZ(pthread_create(&t, NULL, &start_closer, C));
   1857 
   1858 	/*
   1859 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1860 	 * pipes?  POLLIN is right in one sense that we can read
   1861 	 * without blocking, and it will report EOF -- but that is also
   1862 	 * the scenario in which POLLHUP is right.
   1863 	 */
   1864 	expected_si_fd = sockfd[0];
   1865 	expected_si_code = POLL_IN;
   1866 	expected_si_band = POLLIN|POLLRDNORM;
   1867 	si_band_mask = ~0;
   1868 
   1869 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1870 	(void)pthread_barrier_wait(&bar);
   1871 	wait_for_io(&omask);
   1872 	check_read_eof(sockfd[0]);
   1873 
   1874 	RZ(pthread_join(t, NULL));
   1875 }
   1876 
   1877 ATF_TC(socket_local_read_shutdown);
   1878 ATF_TC_HEAD(socket_local_read_shutdown, tc)
   1879 {
   1880 	atf_tc_set_md_var(tc, "descr",
   1881 	    "Test SIGIO when socket is readable because shutdown");
   1882 }
   1883 ATF_TC_BODY(socket_local_read_shutdown, tc)
   1884 {
   1885 	sigset_t omask;
   1886 	int sockfd[2], flags;
   1887 	pthread_barrier_t bar;
   1888 	struct context ctx, *C = &ctx;
   1889 	pthread_t t;
   1890 
   1891 	setup(&omask);
   1892 
   1893 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   1894 	RL(fcntl(sockfd[0], F_SETOWN, getpid()));
   1895 	RL(flags = fcntl(sockfd[0], F_GETFL));
   1896 	RL(fcntl(sockfd[0], F_SETFL, flags | O_ASYNC));
   1897 
   1898 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1899 
   1900 	memset(C, 0, sizeof(*C));
   1901 	C->bar = &bar;
   1902 	C->fd = sockfd[1];
   1903 	RZ(pthread_create(&t, NULL, &start_shutdownwr, C));
   1904 
   1905 	/*
   1906 	 * XXX Is this really sensible?  No POLLHUP?  Different from
   1907 	 * pipes?  POLLIN is right in one sense that we can read
   1908 	 * without blocking, and it will report EOF -- but that is also
   1909 	 * the scenario in which POLLHUP is right.
   1910 	 */
   1911 	expected_si_fd = sockfd[0];
   1912 	expected_si_code = POLL_IN;
   1913 	expected_si_band = POLLIN|POLLRDNORM;
   1914 	si_band_mask = ~0;
   1915 
   1916 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1917 	(void)pthread_barrier_wait(&bar);
   1918 	wait_for_io(&omask);
   1919 	check_read_eof(sockfd[0]);
   1920 
   1921 	RZ(pthread_join(t, NULL));
   1922 }
   1923 
   1924 ATF_TC(socket_local_write);
   1925 ATF_TC_HEAD(socket_local_write, tc)
   1926 {
   1927 	atf_tc_set_md_var(tc, "descr",
   1928 	    "Test SIGIO when socket is writable");
   1929 }
   1930 ATF_TC_BODY(socket_local_write, tc)
   1931 {
   1932 	sigset_t omask;
   1933 	int sockfd[2], flags;
   1934 	pthread_barrier_t bar;
   1935 	struct context ctx, *C = &ctx;
   1936 	socklen_t optlen;
   1937 	pthread_t t;
   1938 
   1939 	setup(&omask);
   1940 
   1941 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   1942 	fillpipebuf(sockfd[1]);
   1943 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1944 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1945 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1946 
   1947 	RZ(pthread_barrier_init(&bar, NULL, 2));
   1948 
   1949 	memset(C, 0, sizeof(*C));
   1950 	C->bar = &bar;
   1951 	C->fd = sockfd[0];
   1952 	optlen = sizeof(C->lowat);
   1953 	RL(getsockopt(sockfd[1], SOL_SOCKET, SO_SNDLOWAT, &C->lowat, &optlen));
   1954 	ATF_REQUIRE_EQ_MSG(optlen, sizeof(C->lowat),
   1955 	    "optlen=%zu sizeof(C->lowat)=%zu",
   1956 	    (size_t)optlen, sizeof(C->lowat));
   1957 
   1958 	atf_tc_expect_fail("PR kern/60782:"
   1959 	    " SIGIO says socket ready for send before send does");
   1960 
   1961 	RZ(pthread_create(&t, NULL, &start_reader, C));
   1962 
   1963 	expected_si_fd = sockfd[0];
   1964 	expected_si_code = POLL_OUT;
   1965 	expected_si_band = POLLOUT|POLLWRNORM;
   1966 	si_band_mask = ~0;
   1967 
   1968 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   1969 	(void)pthread_barrier_wait(&bar);
   1970 	wait_for_io(&omask);
   1971 	check_sendnonblock(sockfd[1]);
   1972 	check_write(sockfd[1]);
   1973 
   1974 	RZ(pthread_join(t, NULL));
   1975 }
   1976 
   1977 ATF_TC(socket_local_write_closed);
   1978 ATF_TC_HEAD(socket_local_write_closed, tc)
   1979 {
   1980 	atf_tc_set_md_var(tc, "descr",
   1981 	    "Test SIGIO when socket is writable because closed");
   1982 }
   1983 ATF_TC_BODY(socket_local_write_closed, tc)
   1984 {
   1985 	sigset_t omask;
   1986 	int sockfd[2], flags;
   1987 	pthread_barrier_t bar;
   1988 	struct context ctx, *C = &ctx;
   1989 	pthread_t t;
   1990 
   1991 	setup(&omask);
   1992 
   1993 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   1994 	fillpipebuf(sockfd[1]);
   1995 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   1996 	RL(flags = fcntl(sockfd[1], F_GETFL));
   1997 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   1998 
   1999 	RZ(pthread_barrier_init(&bar, NULL, 2));
   2000 
   2001 	memset(C, 0, sizeof(*C));
   2002 	C->bar = &bar;
   2003 	C->fd = sockfd[0];
   2004 	RZ(pthread_create(&t, NULL, &start_closer, C));
   2005 
   2006 	/*
   2007 	 * Note: This matches the TCP semantics but not the pipe
   2008 	 * semantics.
   2009 	 */
   2010 	expected_si_fd = sockfd[1];
   2011 	expected_si_code = POLL_IN;
   2012 	expected_si_band = POLLIN|POLLRDNORM;
   2013 	si_band_mask = ~0;
   2014 
   2015 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   2016 	(void)pthread_barrier_wait(&bar);
   2017 	wait_for_io(&omask);
   2018 	check_write_fail(sockfd[1], EPIPE);
   2019 
   2020 	RZ(pthread_join(t, NULL));
   2021 }
   2022 
   2023 ATF_TC(socket_local_write_shutdown);
   2024 ATF_TC_HEAD(socket_local_write_shutdown, tc)
   2025 {
   2026 	atf_tc_set_md_var(tc, "descr",
   2027 	    "Test SIGIO when socket is writable because shutdown");
   2028 }
   2029 ATF_TC_BODY(socket_local_write_shutdown, tc)
   2030 {
   2031 	sigset_t omask;
   2032 	int sockfd[2], flags;
   2033 	pthread_barrier_t bar;
   2034 	struct context ctx, *C = &ctx;
   2035 	pthread_t t;
   2036 
   2037 	setup(&omask);
   2038 
   2039 	RL(socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd));
   2040 	fillpipebuf(sockfd[1]);
   2041 	RL(fcntl(sockfd[1], F_SETOWN, getpid()));
   2042 	RL(flags = fcntl(sockfd[1], F_GETFL));
   2043 	RL(fcntl(sockfd[1], F_SETFL, flags | O_ASYNC));
   2044 
   2045 	RZ(pthread_barrier_init(&bar, NULL, 2));
   2046 
   2047 	memset(C, 0, sizeof(*C));
   2048 	C->bar = &bar;
   2049 	C->fd = sockfd[0];
   2050 	RZ(pthread_create(&t, NULL, &start_shutdownrd, C));
   2051 
   2052 	/*
   2053 	 * XXX Should AF_LOCAL sockets report anything to the peer on
   2054 	 * shutdown(SHUT_RD)?
   2055 	 */
   2056 	REQUIRE_LIBC(alarm(1), (unsigned)-1);
   2057 	(void)pthread_barrier_wait(&bar);
   2058 
   2059 	/*
   2060 	 * XXX Why does this return EAGAIN and not EPIPE?  Shouldn't
   2061 	 * this refuse to send any more data now that the reader has
   2062 	 * shutdown that direction?
   2063 	 */
   2064 	check_write_fail(sockfd[1], EAGAIN);
   2065 
   2066 	RZ(pthread_join(t, NULL));
   2067 }
   2068 
   2069 ATF_TP_ADD_TCS(tp)
   2070 {
   2071 
   2072 	ATF_TP_ADD_TC(tp, fifo_read);
   2073 	ATF_TP_ADD_TC(tp, fifo_read_closed);
   2074 	ATF_TP_ADD_TC(tp, fifo_write);
   2075 	ATF_TP_ADD_TC(tp, fifo_write_closed);
   2076 	ATF_TP_ADD_TC(tp, pipe_read);
   2077 	ATF_TP_ADD_TC(tp, pipe_read_closed);
   2078 	ATF_TP_ADD_TC(tp, pipe_write);
   2079 	ATF_TP_ADD_TC(tp, pipe_write_closed);
   2080 	ATF_TP_ADD_TC(tp, ptyapp_read);
   2081 	ATF_TP_ADD_TC(tp, ptyapp_read_closed);
   2082 	ATF_TP_ADD_TC(tp, ptyapp_write);
   2083 	ATF_TP_ADD_TC(tp, ptyapp_write_closed);
   2084 	ATF_TP_ADD_TC(tp, ptyhost_read);
   2085 	ATF_TP_ADD_TC(tp, ptyhost_read_closed);
   2086 	ATF_TP_ADD_TC(tp, ptyhost_write);
   2087 	ATF_TP_ADD_TC(tp, ptyhost_write_closed);
   2088 	ATF_TP_ADD_TC(tp, socket_inet6_read);
   2089 	ATF_TP_ADD_TC(tp, socket_inet6_read_closed);
   2090 	ATF_TP_ADD_TC(tp, socket_inet6_read_shutdown);
   2091 	ATF_TP_ADD_TC(tp, socket_inet6_write);
   2092 	ATF_TP_ADD_TC(tp, socket_inet6_write_closed);
   2093 	ATF_TP_ADD_TC(tp, socket_inet6_write_shutdown);
   2094 	ATF_TP_ADD_TC(tp, socket_inet_read);
   2095 	ATF_TP_ADD_TC(tp, socket_inet_read_closed);
   2096 	ATF_TP_ADD_TC(tp, socket_inet_read_shutdown);
   2097 	ATF_TP_ADD_TC(tp, socket_inet_write);
   2098 	ATF_TP_ADD_TC(tp, socket_inet_write_closed);
   2099 	ATF_TP_ADD_TC(tp, socket_inet_write_shutdown);
   2100 	ATF_TP_ADD_TC(tp, socket_local_read);
   2101 	ATF_TP_ADD_TC(tp, socket_local_read_closed);
   2102 	ATF_TP_ADD_TC(tp, socket_local_read_shutdown);
   2103 	ATF_TP_ADD_TC(tp, socket_local_write);
   2104 	ATF_TP_ADD_TC(tp, socket_local_write_closed);
   2105 	ATF_TP_ADD_TC(tp, socket_local_write_shutdown);
   2106 
   2107 	return atf_no_error();
   2108 }
   2109