1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/* $NetBSD: pollpal.c,v 1.4 2022/03/28 12:33:22 riastradh Exp $ */
/*-
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pollpal.c,v 1.4 2022/03/28 12:33:22 riastradh Exp $");
#include <sys/module.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/lwp.h>
#include <sys/condvar.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/mutex.h>
#include <sys/kmem.h>
#include <sys/poll.h>
#include <sys/select.h>
/*
* Create a device /dev/pollpal
*
* To use this device you need to do:
* mknod /dev/pollpal c 351 0
*
*/
dev_type_open(pollpal_open);
static struct cdevsw pollpal_cdevsw = {
.d_open = pollpal_open,
.d_close = noclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER
};
static int pollpal_nopen = 0;
static int pollpal_close(file_t *);
static int pollpal_write(file_t *, off_t *, struct uio *, kauth_cred_t,
int);
static int pollpal_read(file_t *, off_t *, struct uio *, kauth_cred_t,
int);
static int pollpal_poll(file_t *, int);
const struct fileops pollpal_fileops = {
.fo_read = pollpal_read,
.fo_write = pollpal_write,
.fo_ioctl = fbadop_ioctl,
.fo_fcntl = fnullop_fcntl,
.fo_poll = pollpal_poll,
.fo_stat = fbadop_stat,
.fo_close = pollpal_close,
.fo_kqfilter = fnullop_kqfilter,
.fo_restart = fnullop_restart,
};
typedef struct pollpal_softc {
kmutex_t lock;
kcondvar_t sc_cv;
struct selinfo psel;
char *buf;
size_t buf_len;
/* Device can have two states 1.READ_WAITING, 2.WRITE_WAITING. */
enum states {
READ_WAITING = 0,
WRITE_WAITING = 1
} sc_state;
} pal_t;
static int
check_pal(const char *str, size_t len)
{
size_t n;
n = 0;
while (n <= len / 2) {
if (str[n] != str[len - n - 1])
return 0;
n++;
}
return 1;
}
int
pollpal_open(dev_t dev, int flag, int mode, struct lwp *l __unused)
{
struct file *fp;
int error, fd;
pal_t *pl;
error = fd_allocfile(&fp, &fd);
if (error)
return error;
++pollpal_nopen;
pl = kmem_zalloc(sizeof(*pl), KM_SLEEP);
pl->sc_state = READ_WAITING;
mutex_init(&pl->lock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&pl->sc_cv, "sc_cv");
selinit(&pl->psel);
return fd_clone(fp, fd, flag, &pollpal_fileops, pl);
}
int
pollpal_close(file_t * fp)
{
pal_t * pl = fp->f_data;
KASSERT(pl != NULL);
if (pl->buf != NULL)
kmem_free(pl->buf, pl->buf_len);
seldestroy(&pl->psel);
cv_destroy(&pl->sc_cv);
mutex_destroy(&pl->lock);
kmem_free(pl, sizeof(*pl));
--pollpal_nopen;
return 0;
}
/*
* Device would write only in READ_WAITING state and then update the state to
* WRITE_WAITING.
*/
int
pollpal_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
int flag)
{
pal_t *pl = fp->f_data;
int error;
error = 0;
/* To ensure that only single process can write in device at a time. */
mutex_enter(&pl->lock);
cv_broadcast(&pl->sc_cv);
switch (pl->sc_state) {
case READ_WAITING:
pl->sc_state = WRITE_WAITING;
selnotify(&pl->psel, POLLOUT | POLLWRNORM, 0);
while (pl->sc_state == WRITE_WAITING) {
if (pl->buf) {
error = cv_wait_sig(&pl->sc_cv, &pl->lock);
if (error)
goto ret;
}
pl->buf_len = uio->uio_iov->iov_len;
pl->buf = kmem_alloc(pl->buf_len, KM_SLEEP);
uiomove(pl->buf, pl->buf_len, uio);
printf("Use cat to know the result.\n");
break;
}
break;
case WRITE_WAITING:
printf("State: WRITE_WAITING\n");
break;
}
ret:
mutex_exit(&pl->lock);
return error;
}
/*
* Device would read only in WRITE_WAITING state and then update the state to
* READ_WAITING.
*/
int
pollpal_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
int flags)
{
pal_t *pl = fp->f_data;
int error;
error = 0;
/* To ensure that only single process can read device at a time. */
mutex_enter(&pl->lock);
cv_broadcast(&pl->sc_cv);
switch (pl->sc_state) {
case READ_WAITING:
printf("State: READ_WAITING\n");
printf("You need to write something to the module first!\n");
goto ret;
case WRITE_WAITING:
pl->sc_state = READ_WAITING;
selnotify(&pl->psel, POLLIN | POLLRDNORM, 0);
while (pl->sc_state == READ_WAITING) {
if (!pl->buf) {
error = cv_wait_sig(&pl->sc_cv, &pl->lock);
if (error)
goto ret;
}
printf("The string you entered was: ");
uiomove(pl->buf, pl->buf_len, uio);
printf("\n");
if (check_pal(pl->buf, pl->buf_len))
printf("String '%s' was a palindrome.\n",
pl->buf);
else
printf("String '%s' was not a palindrome.\n",
pl->buf);
break;
}
break;
}
kmem_free(pl->buf, pl->buf_len);
pl->buf = NULL;
ret:
mutex_exit(&pl->lock);
return error;
}
int
pollpal_poll(struct file *fp, int events)
{
pal_t *pl = fp->f_data;
int revents;
revents = 0;
mutex_enter(&pl->lock);
switch (pl->sc_state) {
case READ_WAITING:
if (events & (POLLOUT | POLLWRNORM)) {
/* When device is in READ_WAITING state it can write */
revents |= POLLOUT | POLLWRNORM;
} else {
/* Record the request if it wasn't satisfied. */
selrecord(curlwp, &pl->psel);
}
break;
case WRITE_WAITING:
if (events & (POLLIN | POLLRDNORM)) {
/* When device is in WRITE_WAITING state it can read. */
revents |= POLLIN | POLLRDNORM;
} else {
/* Record the request if it wasn't satisfied. */
selrecord(curlwp, &pl->psel);
}
break;
}
mutex_exit(&pl->lock);
return revents;
}
MODULE(MODULE_CLASS_MISC, pollpal, NULL);
static int
pollpal_modcmd(modcmd_t cmd, void *arg __unused)
{
int cmajor = 351, bmajor = -1;
switch (cmd) {
case MODULE_CMD_INIT:
if (devsw_attach("pollpal", NULL, &bmajor, &pollpal_cdevsw,
&cmajor))
return ENXIO;
return 0;
case MODULE_CMD_FINI:
if (pollpal_nopen != 0)
return EBUSY;
devsw_detach(NULL, &pollpal_cdevsw);
return 0;
default:
return ENOTTY;
}
}
|