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