xref: /openbsd-src/regress/sys/kern/pipe/test-select.c (revision 4b171f7d69c92a0a296bba1639eef5570e49697a)
1*4b171f7dSanton /*	$OpenBSD: test-select.c,v 1.1 2021/10/22 05:03:04 anton Exp $	*/
2*4b171f7dSanton 
3*4b171f7dSanton /*
4*4b171f7dSanton  * Copyright (c) 2021 Anton Lindqvist <anton@openbsd.org>
5*4b171f7dSanton  *
6*4b171f7dSanton  * Permission to use, copy, modify, and distribute this software for any
7*4b171f7dSanton  * purpose with or without fee is hereby granted, provided that the above
8*4b171f7dSanton  * copyright notice and this permission notice appear in all copies.
9*4b171f7dSanton  *
10*4b171f7dSanton  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*4b171f7dSanton  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*4b171f7dSanton  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*4b171f7dSanton  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*4b171f7dSanton  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*4b171f7dSanton  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*4b171f7dSanton  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*4b171f7dSanton  */
18*4b171f7dSanton 
19*4b171f7dSanton #include <sys/select.h>
20*4b171f7dSanton 
21*4b171f7dSanton #include <err.h>
22*4b171f7dSanton #include <errno.h>
23*4b171f7dSanton #include <unistd.h>
24*4b171f7dSanton 
25*4b171f7dSanton #include "pipe.h"
26*4b171f7dSanton 
27*4b171f7dSanton /*
28*4b171f7dSanton  * Verify select(2) hangup semantics.
29*4b171f7dSanton  */
30*4b171f7dSanton int
test_select_hup(void)31*4b171f7dSanton test_select_hup(void)
32*4b171f7dSanton {
33*4b171f7dSanton 	fd_set writefds;
34*4b171f7dSanton 	ssize_t n;
35*4b171f7dSanton 	int pip[2];
36*4b171f7dSanton 	int nready;
37*4b171f7dSanton 	char c = 'c';
38*4b171f7dSanton 
39*4b171f7dSanton 	if (pipe(pip) == -1)
40*4b171f7dSanton 		err(1, "pipe");
41*4b171f7dSanton 	close(pip[0]);
42*4b171f7dSanton 
43*4b171f7dSanton 	FD_ZERO(&writefds);
44*4b171f7dSanton 	FD_SET(pip[1], &writefds);
45*4b171f7dSanton 	nready = select(pip[1] + 1, NULL, &writefds, NULL, NULL);
46*4b171f7dSanton 	if (nready == -1)
47*4b171f7dSanton 		err(1, "select");
48*4b171f7dSanton 	if (nready != 1)
49*4b171f7dSanton 		errx(1, "select: want 1, got %d", nready);
50*4b171f7dSanton 	n = write(pip[1], &c, sizeof(c));
51*4b171f7dSanton 	if (n != -1)
52*4b171f7dSanton 		errx(1, "read: want -1, got %zd", n);
53*4b171f7dSanton 	if (errno != EPIPE)
54*4b171f7dSanton 		errx(1, "errno: want %d, got %d", EPIPE, errno);
55*4b171f7dSanton 
56*4b171f7dSanton 	return 0;
57*4b171f7dSanton }
58