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