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