1272343Sngie/*	$NetBSD: h_client.c,v 1.8 2012/04/20 05:15:11 jruoho Exp $	*/
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie#include <sys/poll.h>
32272343Sngie#include <sys/select.h>
33272343Sngie
34272343Sngie#include <err.h>
35272343Sngie#include <errno.h>
36272343Sngie#include <fcntl.h>
37272343Sngie#include <stdio.h>
38272343Sngie#include <stdlib.h>
39272343Sngie#include <string.h>
40272343Sngie#include <unistd.h>
41272343Sngie
42272343Sngieint
43272343Sngiemain(int argc, char *argv[])
44272343Sngie{
45272343Sngie
46272343Sngie	if (argc != 2) {
47272343Sngie		errx(1, "need testname as param");
48272343Sngie	}
49272343Sngie
50272343Sngie	if (strcmp(argv[1], "select_timeout") == 0) {
51272343Sngie		fd_set rfds;
52272343Sngie		struct timeval tv;
53272343Sngie		int pipefd[2];
54272343Sngie		int rv;
55272343Sngie
56272343Sngie		tv.tv_sec = 0;
57272343Sngie		tv.tv_usec = 1;
58272343Sngie
59272343Sngie		if (pipe(pipefd) == -1)
60272343Sngie			err(EXIT_FAILURE, "pipe");
61272343Sngie		FD_ZERO(&rfds);
62272343Sngie		FD_SET(pipefd[0], &rfds);
63272343Sngie
64272343Sngie		rv = select(pipefd[0]+1, &rfds, NULL, NULL, &tv);
65272343Sngie		if (rv == -1)
66272343Sngie			err(EXIT_FAILURE, "select");
67272343Sngie		if (rv != 0)
68272343Sngie			errx(EXIT_FAILURE, "select succesful");
69272343Sngie
70272343Sngie		if (FD_ISSET(pipefd[0], &rfds))
71272343Sngie			errx(EXIT_FAILURE, "stdin fileno is still set");
72272343Sngie		return EXIT_SUCCESS;
73272343Sngie	} else if (strcmp(argv[1], "select_allunset") == 0) {
74272343Sngie		fd_set fds;
75272343Sngie		struct timeval tv;
76272343Sngie		int rv;
77272343Sngie
78272343Sngie		tv.tv_sec = 0;
79272343Sngie		tv.tv_usec = 1;
80272343Sngie
81272343Sngie		FD_ZERO(&fds);
82272343Sngie
83272343Sngie		rv = select(100, &fds, &fds, &fds, &tv);
84272343Sngie		if (rv == -1)
85272343Sngie			err(EXIT_FAILURE, "select");
86272343Sngie		if (rv != 0)
87272343Sngie			errx(EXIT_FAILURE, "select succesful");
88272343Sngie
89272343Sngie		rv = select(0, NULL, NULL, NULL, &tv);
90272343Sngie		if (rv == -1)
91272343Sngie			err(EXIT_FAILURE, "select2");
92272343Sngie		if (rv != 0)
93272343Sngie			errx(EXIT_FAILURE, "select2 succesful");
94272343Sngie
95272343Sngie		return EXIT_SUCCESS;
96272343Sngie	} else if (strcmp(argv[1], "invafd") == 0) {
97272343Sngie		struct pollfd pfd[2];
98272343Sngie		int fd, rv;
99272343Sngie
100272343Sngie		fd = open("/rump/dev/null", O_RDWR);
101272343Sngie		if (fd == -1)
102272343Sngie			err(EXIT_FAILURE, "open");
103272343Sngie		close(fd);
104272343Sngie
105272343Sngie		pfd[0].fd = STDIN_FILENO;
106272343Sngie		pfd[0].events = POLLIN;
107272343Sngie		pfd[1].fd = fd;
108272343Sngie		pfd[1].events = POLLIN;
109272343Sngie
110272343Sngie		if ((rv = poll(pfd, 2, INFTIM)) != 1)
111272343Sngie			errx(EXIT_FAILURE, "poll unexpected rv %d (%d)",
112272343Sngie			    rv, errno);
113272343Sngie		if (pfd[1].revents != POLLNVAL || pfd[0].revents != 0)
114272343Sngie			errx(EXIT_FAILURE, "poll unexpected revents");
115272343Sngie
116272343Sngie		return EXIT_SUCCESS;
117272343Sngie	} else if (strcmp(argv[1], "fdoff8") == 0) {
118272343Sngie
119272343Sngie		(void)closefrom(0);
120272343Sngie
121272343Sngie		int fd;
122272343Sngie
123272343Sngie		do {
124272343Sngie			if ((fd = open("/dev/null", O_RDWR)) == -1)
125272343Sngie				err(EXIT_FAILURE, "open1");
126272343Sngie		} while (fd < 7);
127272343Sngie		fd = open("/dev/null", O_RDWR);
128272343Sngie		if (fd != -1 || errno != ENFILE)
129272343Sngie			errx(EXIT_FAILURE, "unexpected fd8 %d %d", fd, errno);
130272343Sngie		if (fcntl(0, F_MAXFD) != 7)
131272343Sngie			errx(EXIT_FAILURE, "fd leak?");
132272343Sngie		if ((fd = open("/rump/dev/null", O_RDWR)) != 8)
133272343Sngie			errx(EXIT_FAILURE, "rump open %d %d", fd, errno);
134272343Sngie		return EXIT_SUCCESS;
135272343Sngie	} else {
136272343Sngie		return ENOTSUP;
137272343Sngie	}
138272343Sngie}
139