1 /* $NetBSD: cmd_pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2020 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 CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __RCSID("$NetBSD: cmd_pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $"); 31 32 #include <sys/mman.h> 33 34 #include <errno.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <poll.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <stdarg.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #define BUFFLEN 100 46 #define TIMEOUT 10000 47 48 static int fd; 49 50 static __dead __printflike(2, 3) void 51 done(int e, const char *msg, ...) 52 { 53 if (msg) { 54 va_list ap; 55 56 va_start(ap, msg); 57 verr(e, msg, ap); 58 /*NOTREACHED*/ 59 va_end(ap); 60 } 61 exit(e); 62 } 63 64 static __dead void 65 handler(int sig) 66 { 67 done(EXIT_SUCCESS, NULL); 68 } 69 70 static const char dev[] = "/dev/pollpal"; 71 static const char wbuf[] = "abcdefghijklmnopqrstuvwxyz"; 72 73 int 74 main(int argc, char **argv) 75 { 76 struct pollfd fds; 77 ssize_t ret; 78 size_t i; 79 char rbuf[BUFFLEN]; 80 81 setprogname(argv[0]); 82 83 fd = open(dev, O_RDWR, 0); 84 if (fd == -1) 85 err(EXIT_FAILURE, "open `%s'", dev); 86 87 fds.fd = fd; 88 fds.events = POLLOUT|POLLIN; 89 90 signal(SIGINT, handler); 91 for (i = 1; i <= 100; i++) { 92 ret = poll(&fds, 1, TIMEOUT); 93 if (ret == -1) { 94 if (errno == EINTR) 95 continue; 96 done(EXIT_FAILURE, "poll"); 97 } 98 printf("Poll_executing %zu times\n", i); 99 if (ret == 0) { 100 printf("%d seconds elapsed \n.TIMEOUT.\n", 101 TIMEOUT / 1000); 102 done(EXIT_SUCCESS, NULL); 103 } 104 105 if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) 106 done(EXIT_FAILURE, "ERR|HUP|NVAL"); 107 108 if (fds.revents & POLLOUT) { 109 do { 110 ret = write(fd, wbuf, sizeof(wbuf) - 1); 111 } while (ret == -1 && errno == EINTR); 112 113 if (ret == -1) { 114 done(EXIT_FAILURE, "write"); 115 } 116 printf("Wrote %zd byte\n", ret); 117 } 118 if (fds.revents & POLLIN) { 119 memset(rbuf, 0, BUFFLEN); 120 do { 121 ret = read(fd, rbuf, BUFFLEN); 122 } while (ret == -1 && errno == EINTR); 123 if (ret == -1) { 124 done(EXIT_FAILURE, "read"); 125 } 126 printf("Read %zd bytes\n", ret); 127 } 128 } 129 done(EXIT_SUCCESS, NULL); 130 } 131