xref: /netbsd-src/tests/lib/librumphijack/h_client.c (revision b58602814f44cdb10d1ce70b9ef75bf29672fcc0)
1 /*	$NetBSD: h_client.c,v 1.10 2021/09/16 22:19:12 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 2011 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
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/poll.h>
32 #include <sys/select.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 int
main(int argc,char * argv[])43 main(int argc, char *argv[])
44 {
45 
46 	if (argc != 2) {
47 		errx(1, "need testname as param");
48 	}
49 
50 	if (strcmp(argv[1], "select_timeout") == 0) {
51 		fd_set rfds;
52 		struct timeval tv;
53 		int pipefd[2];
54 		int rv;
55 
56 		tv.tv_sec = 0;
57 		tv.tv_usec = 1;
58 
59 		if (pipe(pipefd) == -1)
60 			err(EXIT_FAILURE, "pipe");
61 		FD_ZERO(&rfds);
62 		FD_SET(pipefd[0], &rfds);
63 
64 		rv = select(pipefd[0]+1, &rfds, NULL, NULL, &tv);
65 		if (rv == -1)
66 			err(EXIT_FAILURE, "select");
67 		if (rv != 0)
68 			errx(EXIT_FAILURE, "select successful");
69 
70 		if (FD_ISSET(pipefd[0], &rfds))
71 			errx(EXIT_FAILURE, "stdin fileno is still set");
72 		return EXIT_SUCCESS;
73 	} else if (strcmp(argv[1], "select_allunset") == 0) {
74 		fd_set rfds, wfds, efds;
75 		struct timeval tv;
76 		int rv;
77 
78 		tv.tv_sec = 0;
79 		tv.tv_usec = 1;
80 
81 		FD_ZERO(&rfds);
82 		FD_ZERO(&wfds);
83 		FD_ZERO(&efds);
84 
85 		rv = select(100, &rfds, &wfds, &efds, &tv);
86 		if (rv == -1)
87 			err(EXIT_FAILURE, "select");
88 		if (rv != 0)
89 			errx(EXIT_FAILURE, "select successful");
90 
91 		rv = select(0, NULL, NULL, NULL, &tv);
92 		if (rv == -1)
93 			err(EXIT_FAILURE, "select2");
94 		if (rv != 0)
95 			errx(EXIT_FAILURE, "select2 successful");
96 
97 		return EXIT_SUCCESS;
98 	} else if (strcmp(argv[1], "invafd") == 0) {
99 		struct pollfd pfd[2];
100 		int fd, rv;
101 
102 		fd = open("/rump/dev/null", O_RDWR);
103 		if (fd == -1)
104 			err(EXIT_FAILURE, "open");
105 		close(fd);
106 
107 		pfd[0].fd = STDIN_FILENO;
108 		pfd[0].events = POLLIN;
109 		pfd[1].fd = fd;
110 		pfd[1].events = POLLIN;
111 
112 		if ((rv = poll(pfd, 2, INFTIM)) != 1)
113 			errx(EXIT_FAILURE, "poll unexpected rv %d (%d)",
114 			    rv, errno);
115 		if (pfd[1].revents != POLLNVAL || pfd[0].revents != 0)
116 			errx(EXIT_FAILURE, "poll unexpected revents");
117 
118 		return EXIT_SUCCESS;
119 	} else if (strcmp(argv[1], "fdoff8") == 0) {
120 
121 		(void)closefrom(0);
122 
123 		int fd;
124 
125 		do {
126 			if ((fd = open("/dev/null", O_RDWR)) == -1)
127 				err(EXIT_FAILURE, "open1");
128 		} while (fd < 7);
129 		fd = open("/dev/null", O_RDWR);
130 		if (fd != -1 || errno != ENFILE)
131 			errx(EXIT_FAILURE, "unexpected fd8 %d %d", fd, errno);
132 		if (fcntl(0, F_MAXFD) != 7)
133 			errx(EXIT_FAILURE, "fd leak?");
134 		if ((fd = open("/rump/dev/null", O_RDWR)) != 8)
135 			errx(EXIT_FAILURE, "rump open %d %d", fd, errno);
136 		return EXIT_SUCCESS;
137 	} else {
138 		return ENOTSUP;
139 	}
140 }
141