1 1.10 andvar /* $NetBSD: h_client.c,v 1.10 2021/09/16 22:19:12 andvar 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.10 andvar errx(EXIT_FAILURE, "select successful"); 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.9 mrg fd_set rfds, wfds, efds; 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.9 mrg FD_ZERO(&rfds); 82 1.9 mrg FD_ZERO(&wfds); 83 1.9 mrg FD_ZERO(&efds); 84 1.2 pooka 85 1.9 mrg rv = select(100, &rfds, &wfds, &efds, &tv); 86 1.2 pooka if (rv == -1) 87 1.7 jruoho err(EXIT_FAILURE, "select"); 88 1.2 pooka if (rv != 0) 89 1.10 andvar errx(EXIT_FAILURE, "select successful"); 90 1.2 pooka 91 1.2 pooka rv = select(0, NULL, NULL, NULL, &tv); 92 1.2 pooka if (rv == -1) 93 1.7 jruoho err(EXIT_FAILURE, "select2"); 94 1.2 pooka if (rv != 0) 95 1.10 andvar errx(EXIT_FAILURE, "select2 successful"); 96 1.2 pooka 97 1.7 jruoho return EXIT_SUCCESS; 98 1.3 pooka } else if (strcmp(argv[1], "invafd") == 0) { 99 1.3 pooka struct pollfd pfd[2]; 100 1.5 pooka int fd, rv; 101 1.3 pooka 102 1.3 pooka fd = open("/rump/dev/null", O_RDWR); 103 1.3 pooka if (fd == -1) 104 1.7 jruoho err(EXIT_FAILURE, "open"); 105 1.3 pooka close(fd); 106 1.3 pooka 107 1.3 pooka pfd[0].fd = STDIN_FILENO; 108 1.3 pooka pfd[0].events = POLLIN; 109 1.3 pooka pfd[1].fd = fd; 110 1.3 pooka pfd[1].events = POLLIN; 111 1.3 pooka 112 1.5 pooka if ((rv = poll(pfd, 2, INFTIM)) != 1) 113 1.7 jruoho errx(EXIT_FAILURE, "poll unexpected rv %d (%d)", 114 1.7 jruoho rv, errno); 115 1.3 pooka if (pfd[1].revents != POLLNVAL || pfd[0].revents != 0) 116 1.7 jruoho errx(EXIT_FAILURE, "poll unexpected revents"); 117 1.3 pooka 118 1.7 jruoho return EXIT_SUCCESS; 119 1.6 pooka } else if (strcmp(argv[1], "fdoff8") == 0) { 120 1.8 jruoho 121 1.8 jruoho (void)closefrom(0); 122 1.8 jruoho 123 1.6 pooka int fd; 124 1.6 pooka 125 1.8 jruoho do { 126 1.6 pooka if ((fd = open("/dev/null", O_RDWR)) == -1) 127 1.7 jruoho err(EXIT_FAILURE, "open1"); 128 1.8 jruoho } while (fd < 7); 129 1.6 pooka fd = open("/dev/null", O_RDWR); 130 1.6 pooka if (fd != -1 || errno != ENFILE) 131 1.7 jruoho errx(EXIT_FAILURE, "unexpected fd8 %d %d", fd, errno); 132 1.6 pooka if (fcntl(0, F_MAXFD) != 7) 133 1.7 jruoho errx(EXIT_FAILURE, "fd leak?"); 134 1.6 pooka if ((fd = open("/rump/dev/null", O_RDWR)) != 8) 135 1.7 jruoho errx(EXIT_FAILURE, "rump open %d %d", fd, errno); 136 1.7 jruoho return EXIT_SUCCESS; 137 1.1 pooka } else { 138 1.1 pooka return ENOTSUP; 139 1.1 pooka } 140 1.1 pooka } 141