putter.c revision 1.21 1 1.21 ad /* $NetBSD: putter.c,v 1.21 2009/04/04 10:12:51 ad Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by the
7 1.1 pooka * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
8 1.1 pooka * Research Foundation of Helsinki University of Technology
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 pooka * SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka /*
33 1.1 pooka * Pass-to-Userspace TransporTER: generic kernel-user request-response
34 1.1 pooka * transport interface.
35 1.1 pooka */
36 1.1 pooka
37 1.1 pooka #include <sys/cdefs.h>
38 1.21 ad __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.21 2009/04/04 10:12:51 ad Exp $");
39 1.1 pooka
40 1.1 pooka #include <sys/param.h>
41 1.13 ad #include <sys/systm.h>
42 1.1 pooka #include <sys/conf.h>
43 1.1 pooka #include <sys/file.h>
44 1.1 pooka #include <sys/filedesc.h>
45 1.1 pooka #include <sys/kmem.h>
46 1.1 pooka #include <sys/poll.h>
47 1.1 pooka #include <sys/socketvar.h>
48 1.10 jmcneill #include <sys/module.h>
49 1.1 pooka
50 1.2 pooka #include <dev/putter/putter_sys.h>
51 1.1 pooka
52 1.1 pooka /*
53 1.5 pooka * Configuration data.
54 1.5 pooka *
55 1.5 pooka * This is static-size for now. Will be redone for devfs.
56 1.5 pooka */
57 1.5 pooka
58 1.5 pooka #define PUTTER_CONFSIZE 16
59 1.5 pooka
60 1.5 pooka static struct putter_config {
61 1.5 pooka int pc_minor;
62 1.5 pooka int (*pc_config)(int, int, int);
63 1.5 pooka } putterconf[PUTTER_CONFSIZE];
64 1.5 pooka
65 1.5 pooka static int
66 1.5 pooka putter_configure(dev_t dev, int flags, int fmt, int fd)
67 1.5 pooka {
68 1.5 pooka struct putter_config *pc;
69 1.5 pooka
70 1.5 pooka /* are we the catch-all node? */
71 1.5 pooka if (minor(dev) == PUTTER_MINOR_WILDCARD
72 1.5 pooka || minor(dev) == PUTTER_MINOR_COMPAT)
73 1.5 pooka return 0;
74 1.5 pooka
75 1.5 pooka /* nopes? try to configure us */
76 1.5 pooka for (pc = putterconf; pc->pc_config; pc++)
77 1.5 pooka if (minor(dev) == pc->pc_minor)
78 1.5 pooka return pc->pc_config(fd, flags, fmt);
79 1.5 pooka return ENXIO;
80 1.5 pooka }
81 1.5 pooka
82 1.5 pooka int
83 1.5 pooka putter_register(putter_config_fn pcfn, int minor)
84 1.5 pooka {
85 1.5 pooka int i;
86 1.5 pooka
87 1.5 pooka for (i = 0; i < PUTTER_CONFSIZE; i++)
88 1.5 pooka if (putterconf[i].pc_config == NULL)
89 1.5 pooka break;
90 1.5 pooka if (i == PUTTER_CONFSIZE)
91 1.5 pooka return EBUSY;
92 1.5 pooka
93 1.5 pooka putterconf[i].pc_minor = minor;
94 1.5 pooka putterconf[i].pc_config = pcfn;
95 1.5 pooka return 0;
96 1.5 pooka }
97 1.5 pooka
98 1.5 pooka /*
99 1.1 pooka * putter instance structures. these are always allocated and freed
100 1.1 pooka * from the context of the transport user.
101 1.1 pooka */
102 1.1 pooka struct putter_instance {
103 1.1 pooka pid_t pi_pid;
104 1.1 pooka int pi_idx;
105 1.1 pooka int pi_fd;
106 1.1 pooka struct selinfo pi_sel;
107 1.1 pooka
108 1.1 pooka void *pi_private;
109 1.1 pooka struct putter_ops *pi_pop;
110 1.1 pooka
111 1.1 pooka uint8_t *pi_curput;
112 1.1 pooka size_t pi_curres;
113 1.1 pooka void *pi_curopaq;
114 1.1 pooka
115 1.1 pooka TAILQ_ENTRY(putter_instance) pi_entries;
116 1.1 pooka };
117 1.1 pooka #define PUTTER_EMBRYO ((void *)-1) /* before attach */
118 1.1 pooka #define PUTTER_DEAD ((void *)-2) /* after detach */
119 1.1 pooka
120 1.1 pooka static TAILQ_HEAD(, putter_instance) putter_ilist
121 1.1 pooka = TAILQ_HEAD_INITIALIZER(putter_ilist);
122 1.1 pooka
123 1.1 pooka static int get_pi_idx(struct putter_instance *);
124 1.1 pooka
125 1.1 pooka #ifdef DEBUG
126 1.1 pooka #ifndef PUTTERDEBUG
127 1.1 pooka #define PUTTERDEBUG
128 1.1 pooka #endif
129 1.1 pooka #endif
130 1.1 pooka
131 1.1 pooka #ifdef PUTTERDEBUG
132 1.7 pooka int putterdebug = 0;
133 1.1 pooka #define DPRINTF(x) if (putterdebug > 0) printf x
134 1.1 pooka #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
135 1.1 pooka #else
136 1.1 pooka #define DPRINTF(x)
137 1.1 pooka #define DPRINTF_VERBOSE(x)
138 1.1 pooka #endif
139 1.1 pooka
140 1.1 pooka /*
141 1.1 pooka * public init / deinit
142 1.1 pooka */
143 1.1 pooka
144 1.1 pooka /* protects both the list and the contents of the list elements */
145 1.1 pooka static kmutex_t pi_mtx;
146 1.1 pooka
147 1.1 pooka void putterattach(void);
148 1.1 pooka
149 1.1 pooka void
150 1.20 cegger putterattach(void)
151 1.1 pooka {
152 1.1 pooka
153 1.1 pooka mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
154 1.1 pooka }
155 1.1 pooka
156 1.1 pooka #if 0
157 1.1 pooka void
158 1.20 cegger putter_destroy(void)
159 1.1 pooka {
160 1.1 pooka
161 1.1 pooka mutex_destroy(&pi_mtx);
162 1.1 pooka }
163 1.1 pooka #endif
164 1.1 pooka
165 1.1 pooka /*
166 1.1 pooka * fd routines, for cloner
167 1.1 pooka */
168 1.9 ad static int putter_fop_read(file_t *, off_t *, struct uio *,
169 1.1 pooka kauth_cred_t, int);
170 1.9 ad static int putter_fop_write(file_t *, off_t *, struct uio *,
171 1.1 pooka kauth_cred_t, int);
172 1.9 ad static int putter_fop_ioctl(file_t*, u_long, void *);
173 1.9 ad static int putter_fop_poll(file_t *, int);
174 1.9 ad static int putter_fop_close(file_t *);
175 1.9 ad static int putter_fop_kqfilter(file_t *, struct knote *);
176 1.1 pooka
177 1.1 pooka
178 1.1 pooka static const struct fileops putter_fileops = {
179 1.21 ad .fo_read = putter_fop_read,
180 1.21 ad .fo_write = putter_fop_write,
181 1.21 ad .fo_ioctl = putter_fop_ioctl,
182 1.21 ad .fo_fcntl = fnullop_fcntl,
183 1.21 ad .fo_poll = putter_fop_poll,
184 1.21 ad .fo_stat = fbadop_stat,
185 1.21 ad .fo_close = putter_fop_close,
186 1.21 ad .fo_kqfilter = putter_fop_kqfilter,
187 1.21 ad .fo_drain = fnullop_drain,
188 1.1 pooka };
189 1.1 pooka
190 1.1 pooka static int
191 1.9 ad putter_fop_read(file_t *fp, off_t *off, struct uio *uio,
192 1.1 pooka kauth_cred_t cred, int flags)
193 1.1 pooka {
194 1.1 pooka struct putter_instance *pi = fp->f_data;
195 1.1 pooka size_t origres, moved;
196 1.1 pooka int error;
197 1.1 pooka
198 1.13 ad KERNEL_LOCK(1, NULL);
199 1.13 ad
200 1.1 pooka if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
201 1.1 pooka printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
202 1.13 ad KERNEL_UNLOCK_ONE(NULL);
203 1.1 pooka return ENOENT;
204 1.1 pooka }
205 1.1 pooka
206 1.1 pooka if (pi->pi_curput == NULL) {
207 1.1 pooka error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
208 1.1 pooka fp->f_flag & O_NONBLOCK, &pi->pi_curput,
209 1.1 pooka &pi->pi_curres, &pi->pi_curopaq);
210 1.13 ad if (error) {
211 1.13 ad KERNEL_UNLOCK_ONE(NULL);
212 1.1 pooka return error;
213 1.13 ad }
214 1.1 pooka }
215 1.1 pooka
216 1.1 pooka origres = uio->uio_resid;
217 1.1 pooka error = uiomove(pi->pi_curput, pi->pi_curres, uio);
218 1.1 pooka moved = origres - uio->uio_resid;
219 1.1 pooka DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
220 1.1 pooka pi, moved, pi->pi_curput, error));
221 1.1 pooka
222 1.1 pooka KASSERT(pi->pi_curres >= moved);
223 1.1 pooka pi->pi_curres -= moved;
224 1.1 pooka pi->pi_curput += moved;
225 1.1 pooka
226 1.1 pooka if (pi->pi_curres == 0) {
227 1.1 pooka pi->pi_pop->pop_releaseout(pi->pi_private,
228 1.1 pooka pi->pi_curopaq, error);
229 1.1 pooka pi->pi_curput = NULL;
230 1.1 pooka }
231 1.1 pooka
232 1.13 ad KERNEL_UNLOCK_ONE(NULL);
233 1.1 pooka return error;
234 1.1 pooka }
235 1.1 pooka
236 1.1 pooka static int
237 1.9 ad putter_fop_write(file_t *fp, off_t *off, struct uio *uio,
238 1.1 pooka kauth_cred_t cred, int flags)
239 1.1 pooka {
240 1.1 pooka struct putter_instance *pi = fp->f_data;
241 1.2 pooka struct putter_hdr pth;
242 1.1 pooka uint8_t *buf;
243 1.1 pooka size_t frsize;
244 1.1 pooka int error;
245 1.1 pooka
246 1.13 ad KERNEL_LOCK(1, NULL);
247 1.13 ad
248 1.2 pooka DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
249 1.1 pooka pi->pi_private, uio->uio_resid));
250 1.1 pooka
251 1.1 pooka if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
252 1.1 pooka printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
253 1.13 ad KERNEL_UNLOCK_ONE(NULL);
254 1.1 pooka return ENOENT;
255 1.1 pooka }
256 1.1 pooka
257 1.2 pooka error = uiomove(&pth, sizeof(struct putter_hdr), uio);
258 1.13 ad if (error) {
259 1.13 ad KERNEL_UNLOCK_ONE(NULL);
260 1.1 pooka return error;
261 1.13 ad }
262 1.1 pooka
263 1.1 pooka /* Sorry mate, the kernel doesn't buffer. */
264 1.2 pooka frsize = pth.pth_framelen - sizeof(struct putter_hdr);
265 1.13 ad if (uio->uio_resid < frsize) {
266 1.13 ad KERNEL_UNLOCK_ONE(NULL);
267 1.1 pooka return EINVAL;
268 1.13 ad }
269 1.1 pooka
270 1.2 pooka buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
271 1.2 pooka memcpy(buf, &pth, sizeof(pth));
272 1.2 pooka error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
273 1.1 pooka if (error == 0) {
274 1.3 pooka pi->pi_pop->pop_dispatch(pi->pi_private,
275 1.3 pooka (struct putter_hdr *)buf);
276 1.1 pooka }
277 1.2 pooka kmem_free(buf, frsize + sizeof(struct putter_hdr));
278 1.1 pooka
279 1.13 ad KERNEL_UNLOCK_ONE(NULL);
280 1.1 pooka return error;
281 1.1 pooka }
282 1.1 pooka
283 1.1 pooka /*
284 1.1 pooka * Poll query interface. The question is only if an event
285 1.2 pooka * can be read from us.
286 1.1 pooka */
287 1.1 pooka #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
288 1.1 pooka static int
289 1.9 ad putter_fop_poll(file_t *fp, int events)
290 1.1 pooka {
291 1.1 pooka struct putter_instance *pi = fp->f_data;
292 1.1 pooka int revents;
293 1.1 pooka
294 1.13 ad KERNEL_LOCK(1, NULL);
295 1.13 ad
296 1.1 pooka if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
297 1.1 pooka printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
298 1.13 ad KERNEL_UNLOCK_ONE(NULL);
299 1.1 pooka return ENOENT;
300 1.1 pooka }
301 1.1 pooka
302 1.1 pooka revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
303 1.13 ad if ((events & PUTTERPOLL_EVSET) == 0) {
304 1.13 ad KERNEL_UNLOCK_ONE(NULL);
305 1.1 pooka return revents;
306 1.13 ad }
307 1.1 pooka
308 1.1 pooka /* check queue */
309 1.1 pooka if (pi->pi_pop->pop_waitcount(pi->pi_private))
310 1.1 pooka revents |= PUTTERPOLL_EVSET;
311 1.1 pooka else
312 1.9 ad selrecord(curlwp, &pi->pi_sel);
313 1.1 pooka
314 1.13 ad KERNEL_UNLOCK_ONE(NULL);
315 1.1 pooka return revents;
316 1.1 pooka }
317 1.1 pooka
318 1.1 pooka /*
319 1.1 pooka * device close = forced unmount.
320 1.1 pooka *
321 1.1 pooka * unmounting is a frightfully complex operation to avoid races
322 1.1 pooka */
323 1.1 pooka static int
324 1.9 ad putter_fop_close(file_t *fp)
325 1.1 pooka {
326 1.1 pooka struct putter_instance *pi = fp->f_data;
327 1.1 pooka int rv;
328 1.1 pooka
329 1.1 pooka DPRINTF(("putter_fop_close: device closed\n"));
330 1.1 pooka
331 1.13 ad KERNEL_LOCK(1, NULL);
332 1.13 ad
333 1.1 pooka restart:
334 1.1 pooka mutex_enter(&pi_mtx);
335 1.1 pooka /*
336 1.1 pooka * First check if the fs was never mounted. In that case
337 1.1 pooka * remove the instance from the list. If mount is attempted later,
338 1.1 pooka * it will simply fail.
339 1.1 pooka */
340 1.1 pooka if (pi->pi_private == PUTTER_EMBRYO) {
341 1.1 pooka TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
342 1.1 pooka mutex_exit(&pi_mtx);
343 1.1 pooka
344 1.1 pooka DPRINTF(("putter_fop_close: data associated with fp %p was "
345 1.1 pooka "embryonic\n", fp));
346 1.1 pooka
347 1.1 pooka goto out;
348 1.1 pooka }
349 1.1 pooka
350 1.1 pooka /*
351 1.1 pooka * Next, analyze if unmount was called and the instance is dead.
352 1.1 pooka * In this case we can just free the structure and go home, it
353 1.1 pooka * was removed from the list by putter_rmprivate().
354 1.1 pooka */
355 1.1 pooka if (pi->pi_private == PUTTER_DEAD) {
356 1.1 pooka mutex_exit(&pi_mtx);
357 1.1 pooka
358 1.1 pooka DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
359 1.1 pooka "dead, freeing\n", fp, pi->pi_idx));
360 1.1 pooka
361 1.1 pooka goto out;
362 1.1 pooka }
363 1.1 pooka
364 1.1 pooka /*
365 1.1 pooka * So we have a reference. Proceed to unwrap the file system.
366 1.1 pooka */
367 1.1 pooka mutex_exit(&pi_mtx);
368 1.1 pooka
369 1.1 pooka /* hmm? suspicious locking? */
370 1.1 pooka while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
371 1.1 pooka goto restart;
372 1.1 pooka
373 1.1 pooka out:
374 1.13 ad KERNEL_UNLOCK_ONE(NULL);
375 1.1 pooka /*
376 1.1 pooka * Finally, release the instance information. It was already
377 1.1 pooka * removed from the list by putter_rmprivate() and we know it's
378 1.1 pooka * dead, so no need to lock.
379 1.1 pooka */
380 1.1 pooka kmem_free(pi, sizeof(struct putter_instance));
381 1.1 pooka
382 1.1 pooka return 0;
383 1.1 pooka }
384 1.1 pooka
385 1.1 pooka static int
386 1.9 ad putter_fop_ioctl(file_t *fp, u_long cmd, void *data)
387 1.1 pooka {
388 1.1 pooka
389 1.1 pooka /*
390 1.1 pooka * work already done in sys_ioctl(). skip sanity checks to enable
391 1.1 pooka * setting non-blocking fd without yet having mounted the fs
392 1.1 pooka */
393 1.1 pooka if (cmd == FIONBIO)
394 1.1 pooka return 0;
395 1.1 pooka
396 1.1 pooka return EINVAL;
397 1.1 pooka }
398 1.1 pooka
399 1.1 pooka /* kqueue stuff */
400 1.1 pooka
401 1.1 pooka static void
402 1.1 pooka filt_putterdetach(struct knote *kn)
403 1.1 pooka {
404 1.1 pooka struct putter_instance *pi = kn->kn_hook;
405 1.1 pooka
406 1.13 ad KERNEL_LOCK(1, NULL);
407 1.1 pooka mutex_enter(&pi_mtx);
408 1.1 pooka SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
409 1.1 pooka mutex_exit(&pi_mtx);
410 1.13 ad KERNEL_UNLOCK_ONE(NULL);
411 1.1 pooka }
412 1.1 pooka
413 1.1 pooka static int
414 1.6 pooka filt_putter(struct knote *kn, long hint)
415 1.1 pooka {
416 1.1 pooka struct putter_instance *pi = kn->kn_hook;
417 1.13 ad int error, rv;
418 1.1 pooka
419 1.13 ad KERNEL_LOCK(1, NULL);
420 1.1 pooka error = 0;
421 1.1 pooka mutex_enter(&pi_mtx);
422 1.1 pooka if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
423 1.1 pooka error = 1;
424 1.1 pooka mutex_exit(&pi_mtx);
425 1.13 ad if (error) {
426 1.13 ad KERNEL_UNLOCK_ONE(NULL);
427 1.1 pooka return 0;
428 1.13 ad }
429 1.1 pooka
430 1.1 pooka kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
431 1.13 ad rv = kn->kn_data != 0;
432 1.13 ad KERNEL_UNLOCK_ONE(NULL);
433 1.13 ad return rv;
434 1.1 pooka }
435 1.1 pooka
436 1.6 pooka static const struct filterops putter_filtops =
437 1.6 pooka { 1, NULL, filt_putterdetach, filt_putter };
438 1.1 pooka
439 1.1 pooka static int
440 1.9 ad putter_fop_kqfilter(file_t *fp, struct knote *kn)
441 1.1 pooka {
442 1.1 pooka struct putter_instance *pi = fp->f_data;
443 1.1 pooka struct klist *klist;
444 1.1 pooka
445 1.13 ad KERNEL_LOCK(1, NULL);
446 1.13 ad
447 1.6 pooka switch (kn->kn_filter) {
448 1.6 pooka case EVFILT_READ:
449 1.6 pooka klist = &pi->pi_sel.sel_klist;
450 1.6 pooka kn->kn_fop = &putter_filtops;
451 1.6 pooka kn->kn_hook = pi;
452 1.1 pooka
453 1.6 pooka mutex_enter(&pi_mtx);
454 1.6 pooka SLIST_INSERT_HEAD(klist, kn, kn_selnext);
455 1.6 pooka mutex_exit(&pi_mtx);
456 1.1 pooka
457 1.6 pooka break;
458 1.6 pooka case EVFILT_WRITE:
459 1.6 pooka kn->kn_fop = &seltrue_filtops;
460 1.6 pooka break;
461 1.6 pooka default:
462 1.13 ad KERNEL_UNLOCK_ONE(NULL);
463 1.6 pooka return EINVAL;
464 1.6 pooka }
465 1.1 pooka
466 1.13 ad KERNEL_UNLOCK_ONE(NULL);
467 1.1 pooka return 0;
468 1.1 pooka }
469 1.1 pooka
470 1.1 pooka /*
471 1.1 pooka * Device routines. These are for when /dev/puffs is initially
472 1.1 pooka * opened before it has been cloned.
473 1.1 pooka */
474 1.1 pooka
475 1.1 pooka dev_type_open(puttercdopen);
476 1.1 pooka dev_type_close(puttercdclose);
477 1.1 pooka dev_type_ioctl(puttercdioctl);
478 1.1 pooka
479 1.1 pooka /* dev */
480 1.1 pooka const struct cdevsw putter_cdevsw = {
481 1.1 pooka puttercdopen, puttercdclose, noread, nowrite,
482 1.1 pooka noioctl, nostop, notty, nopoll,
483 1.1 pooka nommap, nokqfilter, D_OTHER
484 1.1 pooka };
485 1.1 pooka int
486 1.1 pooka puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
487 1.1 pooka {
488 1.1 pooka struct putter_instance *pi;
489 1.9 ad file_t *fp;
490 1.1 pooka int error, fd, idx;
491 1.9 ad proc_t *p;
492 1.1 pooka
493 1.9 ad p = curproc;
494 1.1 pooka pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
495 1.1 pooka mutex_enter(&pi_mtx);
496 1.1 pooka idx = get_pi_idx(pi);
497 1.1 pooka
498 1.9 ad pi->pi_pid = p->p_pid;
499 1.1 pooka pi->pi_idx = idx;
500 1.1 pooka pi->pi_curput = NULL;
501 1.1 pooka pi->pi_curres = 0;
502 1.1 pooka pi->pi_curopaq = NULL;
503 1.4 pooka selinit(&pi->pi_sel);
504 1.1 pooka mutex_exit(&pi_mtx);
505 1.1 pooka
506 1.9 ad if ((error = fd_allocfile(&fp, &fd)) != 0)
507 1.5 pooka goto bad1;
508 1.5 pooka
509 1.5 pooka if ((error = putter_configure(dev, flags, fmt, fd)) != 0)
510 1.5 pooka goto bad2;
511 1.5 pooka
512 1.1 pooka DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
513 1.1 pooka pi->pi_pid));
514 1.1 pooka
515 1.9 ad error = fd_clone(fp, fd, FREAD|FWRITE, &putter_fileops, pi);
516 1.16 pooka KASSERT(error == EMOVEFD);
517 1.5 pooka return error;
518 1.5 pooka
519 1.5 pooka bad2:
520 1.9 ad fd_abort(p, fp, fd);
521 1.5 pooka bad1:
522 1.5 pooka putter_detach(pi);
523 1.5 pooka kmem_free(pi, sizeof(struct putter_instance));
524 1.5 pooka return error;
525 1.1 pooka }
526 1.1 pooka
527 1.1 pooka int
528 1.1 pooka puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
529 1.1 pooka {
530 1.1 pooka
531 1.1 pooka panic("puttercdclose impossible\n");
532 1.1 pooka
533 1.1 pooka return 0;
534 1.1 pooka }
535 1.1 pooka
536 1.1 pooka
537 1.1 pooka /*
538 1.1 pooka * Set the private structure for the file descriptor. This is
539 1.1 pooka * typically done immediately when the counterpart has knowledge
540 1.1 pooka * about the private structure's address and the file descriptor
541 1.1 pooka * (e.g. vfs mount routine).
542 1.1 pooka *
543 1.1 pooka * We only want to make sure that the caller had the right to open the
544 1.1 pooka * device, we don't so much care about which context it gets in case
545 1.1 pooka * the same process opened multiple (since they are equal at this point).
546 1.1 pooka */
547 1.1 pooka struct putter_instance *
548 1.1 pooka putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
549 1.1 pooka {
550 1.1 pooka struct putter_instance *pi = NULL;
551 1.1 pooka
552 1.1 pooka mutex_enter(&pi_mtx);
553 1.1 pooka TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
554 1.1 pooka if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
555 1.1 pooka pi->pi_private = ppriv;
556 1.1 pooka pi->pi_fd = fd;
557 1.1 pooka pi->pi_pop = pop;
558 1.1 pooka break;
559 1.1 pooka }
560 1.1 pooka }
561 1.1 pooka mutex_exit(&pi_mtx);
562 1.1 pooka
563 1.1 pooka DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
564 1.1 pooka pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
565 1.1 pooka
566 1.1 pooka return pi;
567 1.1 pooka }
568 1.1 pooka
569 1.1 pooka /*
570 1.1 pooka * Remove fp <-> private mapping.
571 1.1 pooka */
572 1.1 pooka void
573 1.1 pooka putter_detach(struct putter_instance *pi)
574 1.1 pooka {
575 1.1 pooka
576 1.1 pooka mutex_enter(&pi_mtx);
577 1.1 pooka TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
578 1.1 pooka pi->pi_private = PUTTER_DEAD;
579 1.1 pooka mutex_exit(&pi_mtx);
580 1.1 pooka
581 1.1 pooka DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
582 1.1 pooka }
583 1.1 pooka
584 1.1 pooka void
585 1.1 pooka putter_notify(struct putter_instance *pi)
586 1.1 pooka {
587 1.1 pooka
588 1.8 rmind selnotify(&pi->pi_sel, 0, 0);
589 1.1 pooka }
590 1.1 pooka
591 1.1 pooka /* search sorted list of instances for free minor, sorted insert arg */
592 1.1 pooka static int
593 1.1 pooka get_pi_idx(struct putter_instance *pi_i)
594 1.1 pooka {
595 1.1 pooka struct putter_instance *pi;
596 1.1 pooka int i;
597 1.1 pooka
598 1.4 pooka KASSERT(mutex_owned(&pi_mtx));
599 1.4 pooka
600 1.1 pooka i = 0;
601 1.1 pooka TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
602 1.1 pooka if (i != pi->pi_idx)
603 1.1 pooka break;
604 1.1 pooka i++;
605 1.1 pooka }
606 1.1 pooka
607 1.1 pooka pi_i->pi_private = PUTTER_EMBRYO;
608 1.1 pooka
609 1.1 pooka if (pi == NULL)
610 1.1 pooka TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
611 1.1 pooka else
612 1.1 pooka TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
613 1.1 pooka
614 1.1 pooka return i;
615 1.1 pooka }
616 1.10 jmcneill
617 1.10 jmcneill MODULE(MODULE_CLASS_MISC, putter, NULL);
618 1.10 jmcneill
619 1.10 jmcneill static int
620 1.10 jmcneill putter_modcmd(modcmd_t cmd, void *arg)
621 1.10 jmcneill {
622 1.18 pooka #ifdef _MODULE
623 1.19 drochner devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
624 1.10 jmcneill
625 1.10 jmcneill switch (cmd) {
626 1.10 jmcneill case MODULE_CMD_INIT:
627 1.10 jmcneill return devsw_attach("putter", NULL, &bmajor,
628 1.10 jmcneill &putter_cdevsw, &cmajor);
629 1.10 jmcneill case MODULE_CMD_FINI:
630 1.11 ad return devsw_detach(NULL, &putter_cdevsw);
631 1.10 jmcneill default:
632 1.10 jmcneill return ENOTTY;
633 1.10 jmcneill }
634 1.18 pooka #else
635 1.18 pooka if (cmd == MODULE_CMD_INIT)
636 1.18 pooka return 0;
637 1.18 pooka return ENOTTY;
638 1.18 pooka #endif
639 1.10 jmcneill }
640