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