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