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