1 /* $NetBSD: fifo_vnops.c,v 1.57 2006/11/16 01:33:38 christos Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: fifo_vnops.c,v 1.57 2006/11/16 01:33:38 christos Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/time.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/socket.h>
44 #include <sys/protosw.h>
45 #include <sys/socketvar.h>
46 #include <sys/stat.h>
47 #include <sys/ioctl.h>
48 #include <sys/file.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 #include <sys/un.h>
52 #include <sys/poll.h>
53 #include <sys/event.h>
54
55 #include <miscfs/fifofs/fifo.h>
56 #include <miscfs/genfs/genfs.h>
57
58 /*
59 * This structure is associated with the FIFO vnode and stores
60 * the state associated with the FIFO.
61 */
62 struct fifoinfo {
63 struct socket *fi_readsock;
64 struct socket *fi_writesock;
65 long fi_readers;
66 long fi_writers;
67 };
68
69 int (**fifo_vnodeop_p)(void *);
70 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
71 { &vop_default_desc, vn_default_error },
72 { &vop_lookup_desc, fifo_lookup }, /* lookup */
73 { &vop_create_desc, fifo_create }, /* create */
74 { &vop_mknod_desc, fifo_mknod }, /* mknod */
75 { &vop_open_desc, fifo_open }, /* open */
76 { &vop_close_desc, fifo_close }, /* close */
77 { &vop_access_desc, fifo_access }, /* access */
78 { &vop_getattr_desc, fifo_getattr }, /* getattr */
79 { &vop_setattr_desc, fifo_setattr }, /* setattr */
80 { &vop_read_desc, fifo_read }, /* read */
81 { &vop_write_desc, fifo_write }, /* write */
82 { &vop_lease_desc, fifo_lease_check }, /* lease */
83 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
84 { &vop_poll_desc, fifo_poll }, /* poll */
85 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
86 { &vop_revoke_desc, fifo_revoke }, /* revoke */
87 { &vop_mmap_desc, fifo_mmap }, /* mmap */
88 { &vop_fsync_desc, fifo_fsync }, /* fsync */
89 { &vop_seek_desc, fifo_seek }, /* seek */
90 { &vop_remove_desc, fifo_remove }, /* remove */
91 { &vop_link_desc, fifo_link }, /* link */
92 { &vop_rename_desc, fifo_rename }, /* rename */
93 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
94 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
95 { &vop_symlink_desc, fifo_symlink }, /* symlink */
96 { &vop_readdir_desc, fifo_readdir }, /* readdir */
97 { &vop_readlink_desc, fifo_readlink }, /* readlink */
98 { &vop_abortop_desc, fifo_abortop }, /* abortop */
99 { &vop_inactive_desc, fifo_inactive }, /* inactive */
100 { &vop_reclaim_desc, fifo_reclaim }, /* reclaim */
101 { &vop_lock_desc, fifo_lock }, /* lock */
102 { &vop_unlock_desc, fifo_unlock }, /* unlock */
103 { &vop_bmap_desc, fifo_bmap }, /* bmap */
104 { &vop_strategy_desc, fifo_strategy }, /* strategy */
105 { &vop_print_desc, fifo_print }, /* print */
106 { &vop_islocked_desc, fifo_islocked }, /* islocked */
107 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
108 { &vop_advlock_desc, fifo_advlock }, /* advlock */
109 { &vop_bwrite_desc, fifo_bwrite }, /* bwrite */
110 { &vop_putpages_desc, fifo_putpages }, /* putpages */
111 { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL }
112 };
113 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
114 { &fifo_vnodeop_p, fifo_vnodeop_entries };
115
116 /*
117 * Trivial lookup routine that always fails.
118 */
119 /* ARGSUSED */
120 int
121 fifo_lookup(void *v)
122 {
123 struct vop_lookup_args /* {
124 struct vnode *a_dvp;
125 struct vnode **a_vpp;
126 struct componentname *a_cnp;
127 } */ *ap = v;
128
129 *ap->a_vpp = NULL;
130 return (ENOTDIR);
131 }
132
133 /*
134 * Open called to set up a new instance of a fifo or
135 * to find an active instance of a fifo.
136 */
137 /* ARGSUSED */
138 int
139 fifo_open(void *v)
140 {
141 struct vop_open_args /* {
142 struct vnode *a_vp;
143 int a_mode;
144 kauth_cred_t a_cred;
145 struct lwp *a_l;
146 } */ *ap = v;
147 struct vnode *vp;
148 struct fifoinfo *fip;
149 struct lwp *l = ap->a_l;
150 struct proc *p;
151 struct socket *rso, *wso;
152 int error;
153
154 vp = ap->a_vp;
155 p = ap->a_l->l_proc;
156
157 if ((fip = vp->v_fifoinfo) == NULL) {
158 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK);
159 vp->v_fifoinfo = fip;
160 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l);
161 if (error != 0) {
162 free(fip, M_VNODE);
163 vp->v_fifoinfo = NULL;
164 return (error);
165 }
166 fip->fi_readsock = rso;
167 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l);
168 if (error != 0) {
169 (void)soclose(rso);
170 free(fip, M_VNODE);
171 vp->v_fifoinfo = NULL;
172 return (error);
173 }
174 fip->fi_writesock = wso;
175 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0) {
176 (void)soclose(wso);
177 (void)soclose(rso);
178 free(fip, M_VNODE);
179 vp->v_fifoinfo = NULL;
180 return (error);
181 }
182 fip->fi_readers = fip->fi_writers = 0;
183 wso->so_state |= SS_CANTRCVMORE;
184 rso->so_state |= SS_CANTSENDMORE;
185 }
186 if (ap->a_mode & FREAD) {
187 if (fip->fi_readers++ == 0) {
188 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE;
189 if (fip->fi_writers > 0)
190 wakeup(&fip->fi_writers);
191 }
192 }
193 if (ap->a_mode & FWRITE) {
194 if (fip->fi_writers++ == 0) {
195 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE;
196 if (fip->fi_readers > 0)
197 wakeup(&fip->fi_readers);
198 }
199 }
200 if (ap->a_mode & FREAD) {
201 if (ap->a_mode & O_NONBLOCK) {
202 } else {
203 while (!soreadable(fip->fi_readsock) && fip->fi_writers == 0) {
204 VOP_UNLOCK(vp, 0);
205 error = tsleep(&fip->fi_readers,
206 PCATCH | PSOCK, "fifor", 0);
207 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
208 if (error)
209 goto bad;
210 }
211 }
212 }
213 if (ap->a_mode & FWRITE) {
214 if (ap->a_mode & O_NONBLOCK) {
215 if (fip->fi_readers == 0) {
216 error = ENXIO;
217 goto bad;
218 }
219 } else {
220 while (fip->fi_readers == 0) {
221 VOP_UNLOCK(vp, 0);
222 error = tsleep(&fip->fi_writers,
223 PCATCH | PSOCK, "fifow", 0);
224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
225 if (error)
226 goto bad;
227 }
228 }
229 }
230 return (0);
231 bad:
232 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_l);
233 return (error);
234 }
235
236 /*
237 * Vnode op for read
238 */
239 /* ARGSUSED */
240 int
241 fifo_read(void *v)
242 {
243 struct vop_read_args /* {
244 struct vnode *a_vp;
245 struct uio *a_uio;
246 int a_ioflag;
247 kauth_cred_t a_cred;
248 } */ *ap = v;
249 struct uio *uio;
250 struct socket *rso;
251 int error;
252 size_t startresid;
253
254 uio = ap->a_uio;
255 rso = ap->a_vp->v_fifoinfo->fi_readsock;
256 #ifdef DIAGNOSTIC
257 if (uio->uio_rw != UIO_READ)
258 panic("fifo_read mode");
259 #endif
260 if (uio->uio_resid == 0)
261 return (0);
262 if (ap->a_ioflag & IO_NDELAY)
263 rso->so_state |= SS_NBIO;
264 startresid = uio->uio_resid;
265 VOP_UNLOCK(ap->a_vp, 0);
266 error = (*rso->so_receive)(rso, (struct mbuf **)0, uio,
267 (struct mbuf **)0, (struct mbuf **)0, (int *)0);
268 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
269 /*
270 * Clear EOF indication after first such return.
271 */
272 if (uio->uio_resid == startresid)
273 rso->so_state &= ~SS_CANTRCVMORE;
274 if (ap->a_ioflag & IO_NDELAY) {
275 rso->so_state &= ~SS_NBIO;
276 if (error == EWOULDBLOCK &&
277 ap->a_vp->v_fifoinfo->fi_writers == 0)
278 error = 0;
279 }
280 return (error);
281 }
282
283 /*
284 * Vnode op for write
285 */
286 /* ARGSUSED */
287 int
288 fifo_write(void *v)
289 {
290 struct vop_write_args /* {
291 struct vnode *a_vp;
292 struct uio *a_uio;
293 int a_ioflag;
294 kauth_cred_t a_cred;
295 } */ *ap = v;
296 struct socket *wso;
297 int error;
298
299 wso = ap->a_vp->v_fifoinfo->fi_writesock;
300 #ifdef DIAGNOSTIC
301 if (ap->a_uio->uio_rw != UIO_WRITE)
302 panic("fifo_write mode");
303 #endif
304 if (ap->a_ioflag & IO_NDELAY)
305 wso->so_state |= SS_NBIO;
306 VOP_UNLOCK(ap->a_vp, 0);
307 error = (*wso->so_send)(wso, (struct mbuf *)0, ap->a_uio, 0,
308 (struct mbuf *)0, 0, curlwp /*XXX*/);
309 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
310 if (ap->a_ioflag & IO_NDELAY)
311 wso->so_state &= ~SS_NBIO;
312 return (error);
313 }
314
315 /*
316 * Device ioctl operation.
317 */
318 /* ARGSUSED */
319 int
320 fifo_ioctl(void *v)
321 {
322 struct vop_ioctl_args /* {
323 struct vnode *a_vp;
324 u_long a_command;
325 void *a_data;
326 int a_fflag;
327 kauth_cred_t a_cred;
328 struct lwp *a_l;
329 } */ *ap = v;
330 struct file filetmp;
331 int error;
332
333 if (ap->a_command == FIONBIO)
334 return (0);
335 if (ap->a_fflag & FREAD) {
336 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
337 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
338 if (error)
339 return (error);
340 }
341 if (ap->a_fflag & FWRITE) {
342 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
343 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_l);
344 if (error)
345 return (error);
346 }
347 return (0);
348 }
349
350 /* ARGSUSED */
351 int
352 fifo_poll(void *v)
353 {
354 struct vop_poll_args /* {
355 struct vnode *a_vp;
356 int a_events;
357 struct lwp *a_l;
358 } */ *ap = v;
359 struct file filetmp;
360 int revents;
361
362 revents = 0;
363 if (ap->a_events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
364 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
365 if (filetmp.f_data)
366 revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
367 }
368 if (ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
369 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
370 if (filetmp.f_data)
371 revents |= soo_poll(&filetmp, ap->a_events, ap->a_l);
372 }
373
374 return (revents);
375 }
376
377 int
378 fifo_inactive(void *v)
379 {
380 struct vop_inactive_args /* {
381 struct vnode *a_vp;
382 struct lwp *a_l;
383 } */ *ap = v;
384
385 VOP_UNLOCK(ap->a_vp, 0);
386 return (0);
387 }
388
389 /*
390 * This is a noop, simply returning what one has been given.
391 */
392 int
393 fifo_bmap(void *v)
394 {
395 struct vop_bmap_args /* {
396 struct vnode *a_vp;
397 daddr_t a_bn;
398 struct vnode **a_vpp;
399 daddr_t *a_bnp;
400 int *a_runp;
401 } */ *ap = v;
402
403 if (ap->a_vpp != NULL)
404 *ap->a_vpp = ap->a_vp;
405 if (ap->a_bnp != NULL)
406 *ap->a_bnp = ap->a_bn;
407 if (ap->a_runp != NULL)
408 *ap->a_runp = 0;
409 return (0);
410 }
411
412 /*
413 * Device close routine
414 */
415 /* ARGSUSED */
416 int
417 fifo_close(void *v)
418 {
419 struct vop_close_args /* {
420 struct vnode *a_vp;
421 int a_fflag;
422 kauth_cred_t a_cred;
423 struct lwp *a_l;
424 } */ *ap = v;
425 struct vnode *vp;
426 struct fifoinfo *fip;
427 int isrevoke;
428
429 vp = ap->a_vp;
430 fip = vp->v_fifoinfo;
431 isrevoke = (ap->a_fflag & (FREAD | FWRITE | FNONBLOCK)) == FNONBLOCK;
432 if (isrevoke) {
433 if (fip->fi_readers != 0) {
434 fip->fi_readers = 0;
435 socantsendmore(fip->fi_writesock);
436 }
437 if (fip->fi_writers != 0) {
438 fip->fi_writers = 0;
439 socantrcvmore(fip->fi_readsock);
440 }
441 } else {
442 if ((ap->a_fflag & FREAD) && --fip->fi_readers == 0)
443 socantsendmore(fip->fi_writesock);
444 if ((ap->a_fflag & FWRITE) && --fip->fi_writers == 0)
445 socantrcvmore(fip->fi_readsock);
446 }
447 /* Shut down if all readers and writers are gone. */
448 if ((fip->fi_readers + fip->fi_writers) == 0) {
449 (void) soclose(fip->fi_readsock);
450 (void) soclose(fip->fi_writesock);
451 FREE(fip, M_VNODE);
452 vp->v_fifoinfo = NULL;
453 }
454 return (0);
455 }
456
457 /*
458 * Print out the contents of a fifo vnode.
459 */
460 int
461 fifo_print(void *v)
462 {
463 struct vop_print_args /* {
464 struct vnode *a_vp;
465 } */ *ap = v;
466
467 printf("tag VT_NON");
468 fifo_printinfo(ap->a_vp);
469 printf("\n");
470 return 0;
471 }
472
473 /*
474 * Print out internal contents of a fifo vnode.
475 */
476 void
477 fifo_printinfo(struct vnode *vp)
478 {
479 struct fifoinfo *fip;
480
481 fip = vp->v_fifoinfo;
482 printf(", fifo with %ld readers and %ld writers",
483 fip->fi_readers, fip->fi_writers);
484 }
485
486 /*
487 * Return POSIX pathconf information applicable to fifo's.
488 */
489 int
490 fifo_pathconf(void *v)
491 {
492 struct vop_pathconf_args /* {
493 struct vnode *a_vp;
494 int a_name;
495 register_t *a_retval;
496 } */ *ap = v;
497
498 switch (ap->a_name) {
499 case _PC_LINK_MAX:
500 *ap->a_retval = LINK_MAX;
501 return (0);
502 case _PC_PIPE_BUF:
503 *ap->a_retval = PIPE_BUF;
504 return (0);
505 case _PC_CHOWN_RESTRICTED:
506 *ap->a_retval = 1;
507 return (0);
508 case _PC_SYNC_IO:
509 *ap->a_retval = 1;
510 return (0);
511 default:
512 return (EINVAL);
513 }
514 /* NOTREACHED */
515 }
516
517 static void
518 filt_fifordetach(struct knote *kn)
519 {
520 struct socket *so;
521
522 so = (struct socket *)kn->kn_hook;
523 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
524 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
525 so->so_rcv.sb_flags &= ~SB_KNOTE;
526 }
527
528 static int
529 filt_fiforead(struct knote *kn, long hint)
530 {
531 struct socket *so;
532
533 so = (struct socket *)kn->kn_hook;
534 kn->kn_data = so->so_rcv.sb_cc;
535 if (so->so_state & SS_CANTRCVMORE) {
536 kn->kn_flags |= EV_EOF;
537 return (1);
538 }
539 kn->kn_flags &= ~EV_EOF;
540 return (kn->kn_data > 0);
541 }
542
543 static void
544 filt_fifowdetach(struct knote *kn)
545 {
546 struct socket *so;
547
548 so = (struct socket *)kn->kn_hook;
549 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
550 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
551 so->so_snd.sb_flags &= ~SB_KNOTE;
552 }
553
554 static int
555 filt_fifowrite(struct knote *kn, long hint)
556 {
557 struct socket *so;
558
559 so = (struct socket *)kn->kn_hook;
560 kn->kn_data = sbspace(&so->so_snd);
561 if (so->so_state & SS_CANTSENDMORE) {
562 kn->kn_flags |= EV_EOF;
563 return (1);
564 }
565 kn->kn_flags &= ~EV_EOF;
566 return (kn->kn_data >= so->so_snd.sb_lowat);
567 }
568
569 static const struct filterops fiforead_filtops =
570 { 1, NULL, filt_fifordetach, filt_fiforead };
571 static const struct filterops fifowrite_filtops =
572 { 1, NULL, filt_fifowdetach, filt_fifowrite };
573
574 /* ARGSUSED */
575 int
576 fifo_kqfilter(void *v)
577 {
578 struct vop_kqfilter_args /* {
579 struct vnode *a_vp;
580 struct knote *a_kn;
581 } */ *ap = v;
582 struct socket *so;
583 struct sockbuf *sb;
584
585 so = (struct socket *)ap->a_vp->v_fifoinfo->fi_readsock;
586 switch (ap->a_kn->kn_filter) {
587 case EVFILT_READ:
588 ap->a_kn->kn_fop = &fiforead_filtops;
589 sb = &so->so_rcv;
590 break;
591 case EVFILT_WRITE:
592 ap->a_kn->kn_fop = &fifowrite_filtops;
593 sb = &so->so_snd;
594 break;
595 default:
596 return (1);
597 }
598
599 ap->a_kn->kn_hook = so;
600
601 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, ap->a_kn, kn_selnext);
602 sb->sb_flags |= SB_KNOTE;
603 return (0);
604 }
Cache object: 8fcf56e095ddca9375d8fdb793ec8590
|