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