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