xref: /freebsd/contrib/netbsd-tests/lib/librumphijack/h_client.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*	$NetBSD: h_client.c,v 1.8 2012/04/20 05:15:11 jruoho 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
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 succesful");
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 fds;
75 		struct timeval tv;
76 		int rv;
77 
78 		tv.tv_sec = 0;
79 		tv.tv_usec = 1;
80 
81 		FD_ZERO(&fds);
82 
83 		rv = select(100, &fds, &fds, &fds, &tv);
84 		if (rv == -1)
85 			err(EXIT_FAILURE, "select");
86 		if (rv != 0)
87 			errx(EXIT_FAILURE, "select succesful");
88 
89 		rv = select(0, NULL, NULL, NULL, &tv);
90 		if (rv == -1)
91 			err(EXIT_FAILURE, "select2");
92 		if (rv != 0)
93 			errx(EXIT_FAILURE, "select2 succesful");
94 
95 		return EXIT_SUCCESS;
96 	} else if (strcmp(argv[1], "invafd") == 0) {
97 		struct pollfd pfd[2];
98 		int fd, rv;
99 
100 		fd = open("/rump/dev/null", O_RDWR);
101 		if (fd == -1)
102 			err(EXIT_FAILURE, "open");
103 		close(fd);
104 
105 		pfd[0].fd = STDIN_FILENO;
106 		pfd[0].events = POLLIN;
107 		pfd[1].fd = fd;
108 		pfd[1].events = POLLIN;
109 
110 		if ((rv = poll(pfd, 2, INFTIM)) != 1)
111 			errx(EXIT_FAILURE, "poll unexpected rv %d (%d)",
112 			    rv, errno);
113 		if (pfd[1].revents != POLLNVAL || pfd[0].revents != 0)
114 			errx(EXIT_FAILURE, "poll unexpected revents");
115 
116 		return EXIT_SUCCESS;
117 	} else if (strcmp(argv[1], "fdoff8") == 0) {
118 
119 		(void)closefrom(0);
120 
121 		int fd;
122 
123 		do {
124 			if ((fd = open("/dev/null", O_RDWR)) == -1)
125 				err(EXIT_FAILURE, "open1");
126 		} while (fd < 7);
127 		fd = open("/dev/null", O_RDWR);
128 		if (fd != -1 || errno != ENFILE)
129 			errx(EXIT_FAILURE, "unexpected fd8 %d %d", fd, errno);
130 		if (fcntl(0, F_MAXFD) != 7)
131 			errx(EXIT_FAILURE, "fd leak?");
132 		if ((fd = open("/rump/dev/null", O_RDWR)) != 8)
133 			errx(EXIT_FAILURE, "rump open %d %d", fd, errno);
134 		return EXIT_SUCCESS;
135 	} else {
136 		return ENOTSUP;
137 	}
138 }
139