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