pollpal.c revision 1.1 1 1.1 christos /* $NetBSD: 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 __KERNEL_RCSID(0, "$NetBSD: pollpal.c,v 1.1 2020/04/30 00:48:10 christos Exp $");
31 1.1 christos
32 1.1 christos #include <sys/module.h>
33 1.1 christos #include <sys/param.h>
34 1.1 christos #include <sys/kernel.h>
35 1.1 christos #include <sys/systm.h>
36 1.1 christos
37 1.1 christos #include <sys/condvar.h>
38 1.1 christos #include <sys/conf.h>
39 1.1 christos #include <sys/device.h>
40 1.1 christos #include <sys/file.h>
41 1.1 christos #include <sys/filedesc.h>
42 1.1 christos #include <sys/mutex.h>
43 1.1 christos #include <sys/kmem.h>
44 1.1 christos #include <sys/poll.h>
45 1.1 christos #include <sys/select.h>
46 1.1 christos
47 1.1 christos /*
48 1.1 christos * Create a device /dev/pollpal
49 1.1 christos *
50 1.1 christos * To use this device you need to do:
51 1.1 christos * mknod /dev/pollpal c 351 0
52 1.1 christos *
53 1.1 christos */
54 1.1 christos
55 1.1 christos dev_type_open(pollpal_open);
56 1.1 christos
57 1.1 christos static struct cdevsw pollpal_cdevsw = {
58 1.1 christos .d_open = pollpal_open,
59 1.1 christos .d_close = noclose,
60 1.1 christos .d_read = noread,
61 1.1 christos .d_write = nowrite,
62 1.1 christos .d_ioctl = noioctl,
63 1.1 christos .d_stop = nostop,
64 1.1 christos .d_tty = notty,
65 1.1 christos .d_poll = nopoll,
66 1.1 christos .d_mmap = nommap,
67 1.1 christos .d_kqfilter = nokqfilter,
68 1.1 christos .d_discard = nodiscard,
69 1.1 christos .d_flag = D_OTHER
70 1.1 christos };
71 1.1 christos
72 1.1 christos static int pollpal_nopen = 0;
73 1.1 christos
74 1.1 christos static int pollpal_close(file_t *);
75 1.1 christos static int pollpal_write(file_t *, off_t *, struct uio *, kauth_cred_t,
76 1.1 christos int);
77 1.1 christos static int pollpal_read(file_t *, off_t *, struct uio *, kauth_cred_t,
78 1.1 christos int);
79 1.1 christos static int pollpal_poll(file_t *, int);
80 1.1 christos
81 1.1 christos const struct fileops pollpal_fileops = {
82 1.1 christos .fo_read = pollpal_read,
83 1.1 christos .fo_write = pollpal_write,
84 1.1 christos .fo_ioctl = fbadop_ioctl,
85 1.1 christos .fo_fcntl = fnullop_fcntl,
86 1.1 christos .fo_poll = pollpal_poll,
87 1.1 christos .fo_stat = fbadop_stat,
88 1.1 christos .fo_close = pollpal_close,
89 1.1 christos .fo_kqfilter = fnullop_kqfilter,
90 1.1 christos .fo_restart = fnullop_restart,
91 1.1 christos };
92 1.1 christos
93 1.1 christos typedef struct pollpal_softc {
94 1.1 christos kmutex_t lock;
95 1.1 christos kcondvar_t sc_cv;
96 1.1 christos struct selinfo psel;
97 1.1 christos char *buf;
98 1.1 christos size_t buf_len;
99 1.1 christos /* Device can have two states 1.READ_WAITING, 2.WRITE_WAITING. */
100 1.1 christos enum states {
101 1.1 christos READ_WAITING = 0,
102 1.1 christos WRITE_WAITING = 1
103 1.1 christos } sc_state;
104 1.1 christos } pal_t;
105 1.1 christos
106 1.1 christos static int
107 1.1 christos check_pal(const char *str, size_t len)
108 1.1 christos {
109 1.1 christos size_t n;
110 1.1 christos
111 1.1 christos n = 0;
112 1.1 christos
113 1.1 christos while (n <= len / 2) {
114 1.1 christos if (str[n] != str[len - n - 1])
115 1.1 christos return 0;
116 1.1 christos
117 1.1 christos n++;
118 1.1 christos }
119 1.1 christos
120 1.1 christos return 1;
121 1.1 christos }
122 1.1 christos
123 1.1 christos int
124 1.1 christos pollpal_open(dev_t dev, int flag, int mode, struct lwp *l __unused)
125 1.1 christos {
126 1.1 christos struct file *fp;
127 1.1 christos int error, fd;
128 1.1 christos pal_t *pl;
129 1.1 christos
130 1.1 christos error = fd_allocfile(&fp, &fd);
131 1.1 christos if (error)
132 1.1 christos return error;
133 1.1 christos
134 1.1 christos ++pollpal_nopen;
135 1.1 christos
136 1.1 christos pl = kmem_zalloc(sizeof(*pl), KM_SLEEP);
137 1.1 christos
138 1.1 christos pl->sc_state = READ_WAITING;
139 1.1 christos mutex_init(&pl->lock, MUTEX_DEFAULT, IPL_NONE);
140 1.1 christos cv_init(&pl->sc_cv, "sc_cv");
141 1.1 christos selinit(&pl->psel);
142 1.1 christos
143 1.1 christos return fd_clone(fp, fd, flag, &pollpal_fileops, pl);
144 1.1 christos }
145 1.1 christos
146 1.1 christos int
147 1.1 christos pollpal_close(file_t * fp)
148 1.1 christos {
149 1.1 christos pal_t * pl = fp->f_data;
150 1.1 christos KASSERT(pl != NULL);
151 1.1 christos
152 1.1 christos if (pl->buf != NULL)
153 1.1 christos kmem_free(pl->buf, pl->buf_len);
154 1.1 christos
155 1.1 christos seldestroy(&pl->psel);
156 1.1 christos cv_destroy(&pl->sc_cv);
157 1.1 christos mutex_destroy(&pl->lock);
158 1.1 christos kmem_free(pl, sizeof(*pl));
159 1.1 christos
160 1.1 christos --pollpal_nopen;
161 1.1 christos
162 1.1 christos return 0;
163 1.1 christos }
164 1.1 christos
165 1.1 christos /*
166 1.1 christos * Device would write only in READ_WAITING state and then update the state to
167 1.1 christos * WRITE_WAITING.
168 1.1 christos */
169 1.1 christos int
170 1.1 christos pollpal_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
171 1.1 christos int flag)
172 1.1 christos {
173 1.1 christos pal_t *pl = fp->f_data;
174 1.1 christos int error;
175 1.1 christos
176 1.1 christos error = 0;
177 1.1 christos
178 1.1 christos /* To ensure that only single process can write in device at a time. */
179 1.1 christos mutex_enter(&pl->lock);
180 1.1 christos cv_broadcast(&pl->sc_cv);
181 1.1 christos switch (pl->sc_state) {
182 1.1 christos case READ_WAITING:
183 1.1 christos pl->sc_state = WRITE_WAITING;
184 1.1 christos selnotify(&pl->psel, POLLOUT | POLLWRNORM, 0);
185 1.1 christos while (pl->sc_state == WRITE_WAITING) {
186 1.1 christos if (pl->buf) {
187 1.1 christos error = cv_wait_sig(&pl->sc_cv, &pl->lock);
188 1.1 christos if (error)
189 1.1 christos goto ret;
190 1.1 christos
191 1.1 christos }
192 1.1 christos
193 1.1 christos pl->buf_len = uio->uio_iov->iov_len;
194 1.1 christos pl->buf = kmem_alloc(pl->buf_len, KM_SLEEP);
195 1.1 christos uiomove(pl->buf, pl->buf_len, uio);
196 1.1 christos printf("Use cat to know the result.\n");
197 1.1 christos break;
198 1.1 christos }
199 1.1 christos break;
200 1.1 christos case WRITE_WAITING:
201 1.1 christos printf("State: WRITE_WAITING\n");
202 1.1 christos break;
203 1.1 christos }
204 1.1 christos
205 1.1 christos ret:
206 1.1 christos mutex_exit(&pl->lock);
207 1.1 christos return error;
208 1.1 christos }
209 1.1 christos
210 1.1 christos /*
211 1.1 christos * Device would read only in WRITE_WAITING state and then update the state to
212 1.1 christos * READ_WAITING.
213 1.1 christos */
214 1.1 christos int
215 1.1 christos pollpal_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
216 1.1 christos int flags)
217 1.1 christos {
218 1.1 christos pal_t *pl = fp->f_data;
219 1.1 christos int error;
220 1.1 christos
221 1.1 christos error = 0;
222 1.1 christos
223 1.1 christos /* To ensure that only single process can read device at a time. */
224 1.1 christos mutex_enter(&pl->lock);
225 1.1 christos cv_broadcast(&pl->sc_cv);
226 1.1 christos switch (pl->sc_state) {
227 1.1 christos case READ_WAITING:
228 1.1 christos printf("State: READ_WAITING\n");
229 1.1 christos printf("You need to write something to the module first!\n");
230 1.1 christos goto ret;
231 1.1 christos case WRITE_WAITING:
232 1.1 christos pl->sc_state = READ_WAITING;
233 1.1 christos selnotify(&pl->psel, POLLIN | POLLRDNORM, 0);
234 1.1 christos while (pl->sc_state == READ_WAITING) {
235 1.1 christos if (!pl->buf) {
236 1.1 christos error = cv_wait_sig(&pl->sc_cv, &pl->lock);
237 1.1 christos if (error)
238 1.1 christos goto ret;
239 1.1 christos
240 1.1 christos }
241 1.1 christos printf("The string you entered was: ");
242 1.1 christos uiomove(pl->buf, pl->buf_len, uio);
243 1.1 christos printf("\n");
244 1.1 christos if (check_pal(pl->buf, pl->buf_len))
245 1.1 christos printf("String '%s' was a palindrome.\n",
246 1.1 christos pl->buf);
247 1.1 christos else
248 1.1 christos printf("String '%s' was not a palindrome.\n",
249 1.1 christos pl->buf);
250 1.1 christos
251 1.1 christos break;
252 1.1 christos }
253 1.1 christos break;
254 1.1 christos }
255 1.1 christos kmem_free(pl->buf, pl->buf_len);
256 1.1 christos pl->buf = NULL;
257 1.1 christos
258 1.1 christos ret:
259 1.1 christos mutex_exit(&pl->lock);
260 1.1 christos return error;
261 1.1 christos }
262 1.1 christos
263 1.1 christos int
264 1.1 christos pollpal_poll(struct file *fp, int events)
265 1.1 christos {
266 1.1 christos pal_t *pl = fp->f_data;
267 1.1 christos int revents;
268 1.1 christos
269 1.1 christos revents = 0;
270 1.1 christos
271 1.1 christos mutex_enter(&pl->lock);
272 1.1 christos switch (pl->sc_state) {
273 1.1 christos case READ_WAITING:
274 1.1 christos if (events & (POLLOUT | POLLWRNORM)) {
275 1.1 christos /* When device is in READ_WAITING state it can write */
276 1.1 christos revents |= POLLOUT | POLLWRNORM;
277 1.1 christos } else {
278 1.1 christos /* Record the request if it wasn't satisfied. */
279 1.1 christos selrecord(curlwp, &pl->psel);
280 1.1 christos }
281 1.1 christos break;
282 1.1 christos case WRITE_WAITING:
283 1.1 christos if (events && (POLLIN | POLLRDNORM)) {
284 1.1 christos /* When device is in WRITE_WAITING state it can read. */
285 1.1 christos revents |= POLLIN;
286 1.1 christos } else {
287 1.1 christos /* Record the request if it wasn't satisfied. */
288 1.1 christos selrecord(curlwp, &pl->psel);
289 1.1 christos }
290 1.1 christos break;
291 1.1 christos }
292 1.1 christos
293 1.1 christos mutex_exit(&pl->lock);
294 1.1 christos return revents;
295 1.1 christos }
296 1.1 christos
297 1.1 christos MODULE(MODULE_CLASS_MISC, pollpal, NULL);
298 1.1 christos
299 1.1 christos static int
300 1.1 christos pollpal_modcmd(modcmd_t cmd, void *arg __unused)
301 1.1 christos {
302 1.1 christos int cmajor = 351, bmajor = -1;
303 1.1 christos
304 1.1 christos switch (cmd) {
305 1.1 christos case MODULE_CMD_INIT:
306 1.1 christos if (devsw_attach("pollpal", NULL, &bmajor, &pollpal_cdevsw,
307 1.1 christos &cmajor))
308 1.1 christos return ENXIO;
309 1.1 christos return 0;
310 1.1 christos case MODULE_CMD_FINI:
311 1.1 christos if (pollpal_nopen != 0)
312 1.1 christos return EBUSY;
313 1.1 christos return devsw_detach(NULL, &pollpal_cdevsw);
314 1.1 christos default:
315 1.1 christos return ENOTTY;
316 1.1 christos }
317 1.1 christos }
318