h_client.c revision 1.6 1 /* $NetBSD: h_client.c,v 1.6 2011/03/14 15:56:40 pooka 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(1, "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(1, "select");
67 if (rv != 0)
68 errx(1, "select succesful");
69
70 if (FD_ISSET(pipefd[0], &rfds))
71 errx(1, "stdin fileno is still set");
72 exit(0);
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(1, "select");
86 if (rv != 0)
87 errx(1, "select succesful");
88
89 rv = select(0, NULL, NULL, NULL, &tv);
90 if (rv == -1)
91 err(1, "select2");
92 if (rv != 0)
93 errx(1, "select2 succesful");
94
95 exit(0);
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(1, "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(1, "poll unexpected rv %d (%d)", rv, errno);
112 if (pfd[1].revents != POLLNVAL || pfd[0].revents != 0)
113 errx(1, "poll unexpected revents");
114
115 exit(0);
116 } else if (strcmp(argv[1], "fdoff8") == 0) {
117 int fd;
118
119 do
120 if ((fd = open("/dev/null", O_RDWR)) == -1)
121 err(1, "open1");
122 while (fd < 7);
123 fd = open("/dev/null", O_RDWR);
124 if (fd != -1 || errno != ENFILE)
125 errx(1, "unexpected fd8 %d %d", fd, errno);
126 if (fcntl(0, F_MAXFD) != 7)
127 errx(1, "fd leak?");
128 if ((fd = open("/rump/dev/null", O_RDWR)) != 8)
129 errx(1, "rump open %d %d", fd, errno);
130 exit(0);
131 } else {
132 return ENOTSUP;
133 }
134 }
135