1 /* $OpenBSD: fifo_vnops.c,v 1.105 2024/05/03 17:43:09 mvs Exp $ */
2 /* $NetBSD: fifo_vnops.c,v 1.18 1996/03/16 23:52:42 christos Exp $ */
3
4 /*
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)fifo_vnops.c 8.4 (Berkeley) 8/10/94
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/time.h>
38 #include <sys/namei.h>
39 #include <sys/vnode.h>
40 #include <sys/lock.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/stat.h>
45 #include <sys/ioctl.h>
46 #include <sys/fcntl.h>
47 #include <sys/file.h>
48 #include <sys/event.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 #include <sys/unistd.h>
52
53 #include <miscfs/fifofs/fifo.h>
54
55 /*
56 * This structure is associated with the FIFO vnode and stores
57 * the state associated with the FIFO.
58 */
59 struct fifoinfo {
60 struct socket *fi_readsock;
61 struct socket *fi_writesock;
62 long fi_readers;
63 long fi_writers;
64 };
65
66 const struct vops fifo_vops = {
67 .vop_lookup = vop_generic_lookup,
68 .vop_create = vop_generic_badop,
69 .vop_mknod = vop_generic_badop,
70 .vop_open = fifo_open,
71 .vop_close = fifo_close,
72 .vop_access = fifo_ebadf,
73 .vop_getattr = fifo_ebadf,
74 .vop_setattr = fifo_ebadf,
75 .vop_read = fifo_read,
76 .vop_write = fifo_write,
77 .vop_ioctl = fifo_ioctl,
78 .vop_kqfilter = fifo_kqfilter,
79 .vop_revoke = vop_generic_revoke,
80 .vop_fsync = nullop,
81 .vop_remove = vop_generic_badop,
82 .vop_link = vop_generic_badop,
83 .vop_rename = vop_generic_badop,
84 .vop_mkdir = vop_generic_badop,
85 .vop_rmdir = vop_generic_badop,
86 .vop_symlink = vop_generic_badop,
87 .vop_readdir = vop_generic_badop,
88 .vop_readlink = vop_generic_badop,
89 .vop_abortop = vop_generic_badop,
90 .vop_inactive = fifo_inactive,
91 .vop_reclaim = fifo_reclaim,
92 .vop_lock = nullop,
93 .vop_unlock = nullop,
94 .vop_islocked = nullop,
95 .vop_bmap = vop_generic_bmap,
96 .vop_strategy = vop_generic_badop,
97 .vop_print = fifo_print,
98 .vop_pathconf = fifo_pathconf,
99 .vop_advlock = fifo_advlock,
100 .vop_bwrite = nullop
101 };
102
103 void filt_fifordetach(struct knote *kn);
104 int filt_fiforead(struct knote *kn, long hint);
105 void filt_fifowdetach(struct knote *kn);
106 int filt_fifowrite(struct knote *kn, long hint);
107 int filt_fifoexcept(struct knote *kn, long hint);
108 int filt_fiformodify(struct kevent *kev, struct knote *kn);
109 int filt_fiforprocess(struct knote *kn, struct kevent *kev);
110 int filt_fifowmodify(struct kevent *kev, struct knote *kn);
111 int filt_fifowprocess(struct knote *kn, struct kevent *kev);
112
113 const struct filterops fiforead_filtops = {
114 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
115 .f_attach = NULL,
116 .f_detach = filt_fifordetach,
117 .f_event = filt_fiforead,
118 .f_modify = filt_fiformodify,
119 .f_process = filt_fiforprocess,
120 };
121
122 const struct filterops fifowrite_filtops = {
123 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
124 .f_attach = NULL,
125 .f_detach = filt_fifowdetach,
126 .f_event = filt_fifowrite,
127 .f_modify = filt_fifowmodify,
128 .f_process = filt_fifowprocess,
129 };
130
131 const struct filterops fifoexcept_filtops = {
132 .f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
133 .f_attach = NULL,
134 .f_detach = filt_fifordetach,
135 .f_event = filt_fifoexcept,
136 .f_modify = filt_fiformodify,
137 .f_process = filt_fiforprocess,
138 };
139
140 /*
141 * Open called to set up a new instance of a fifo or
142 * to find an active instance of a fifo.
143 */
144 int
fifo_open(void * v)145 fifo_open(void *v)
146 {
147 struct vop_open_args *ap = v;
148 struct vnode *vp = ap->a_vp;
149 struct fifoinfo *fip;
150 struct socket *rso, *wso;
151 int error;
152
153 if ((fip = vp->v_fifoinfo) == NULL) {
154 fip = malloc(sizeof(*fip), M_VNODE, M_WAITOK);
155 vp->v_fifoinfo = fip;
156 if ((error = socreate(AF_UNIX, &rso, SOCK_STREAM, 0)) != 0) {
157 free(fip, M_VNODE, sizeof *fip);
158 vp->v_fifoinfo = NULL;
159 return (error);
160 }
161 fip->fi_readsock = rso;
162 if ((error = socreate(AF_UNIX, &wso, SOCK_STREAM, 0)) != 0) {
163 (void)soclose(rso, 0);
164 free(fip, M_VNODE, sizeof *fip);
165 vp->v_fifoinfo = NULL;
166 return (error);
167 }
168 fip->fi_writesock = wso;
169 if ((error = soconnect2(wso, rso)) != 0) {
170 (void)soclose(wso, 0);
171 (void)soclose(rso, 0);
172 free(fip, M_VNODE, sizeof *fip);
173 vp->v_fifoinfo = NULL;
174 return (error);
175 }
176 fip->fi_readers = fip->fi_writers = 0;
177 mtx_enter(&wso->so_snd.sb_mtx);
178 wso->so_snd.sb_state |= SS_CANTSENDMORE;
179 wso->so_snd.sb_lowat = PIPE_BUF;
180 mtx_leave(&wso->so_snd.sb_mtx);
181 } else {
182 rso = fip->fi_readsock;
183 wso = fip->fi_writesock;
184 }
185 if (ap->a_mode & FREAD) {
186 fip->fi_readers++;
187 if (fip->fi_readers == 1) {
188 mtx_enter(&wso->so_snd.sb_mtx);
189 wso->so_snd.sb_state &= ~SS_CANTSENDMORE;
190 mtx_leave(&wso->so_snd.sb_mtx);
191 if (fip->fi_writers > 0)
192 wakeup(&fip->fi_writers);
193 }
194 }
195 if (ap->a_mode & FWRITE) {
196 fip->fi_writers++;
197 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) {
198 error = ENXIO;
199 goto bad;
200 }
201 if (fip->fi_writers == 1) {
202 solock(rso);
203 rso->so_state &= ~SS_ISDISCONNECTED;
204 mtx_enter(&rso->so_rcv.sb_mtx);
205 rso->so_rcv.sb_state &= ~SS_CANTRCVMORE;
206 mtx_leave(&rso->so_rcv.sb_mtx);
207 sounlock(rso);
208 if (fip->fi_readers > 0)
209 wakeup(&fip->fi_readers);
210 }
211 }
212 if ((ap->a_mode & O_NONBLOCK) == 0) {
213 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) {
214 VOP_UNLOCK(vp);
215 error = tsleep_nsec(&fip->fi_readers,
216 PCATCH | PSOCK, "fifor", INFSLP);
217 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
218 if (error)
219 goto bad;
220 }
221 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) {
222 VOP_UNLOCK(vp);
223 error = tsleep_nsec(&fip->fi_writers,
224 PCATCH | PSOCK, "fifow", INFSLP);
225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
226 if (error)
227 goto bad;
228 }
229 }
230 return (0);
231 bad:
232 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, ap->a_p);
233 return (error);
234 }
235
236 /*
237 * Vnode op for read
238 */
239 int
fifo_read(void * v)240 fifo_read(void *v)
241 {
242 struct vop_read_args *ap = v;
243 struct uio *uio = ap->a_uio;
244 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock;
245 int error, flags = 0;
246
247 #ifdef DIAGNOSTIC
248 if (uio->uio_rw != UIO_READ)
249 panic("fifo_read mode");
250 #endif
251 if (uio->uio_resid == 0)
252 return (0);
253 if (ap->a_ioflag & IO_NDELAY)
254 flags |= MSG_DONTWAIT;
255 VOP_UNLOCK(ap->a_vp);
256 error = soreceive(rso, NULL, uio, NULL, NULL, &flags, 0);
257 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
258 if (ap->a_ioflag & IO_NDELAY) {
259 if (error == EWOULDBLOCK &&
260 ap->a_vp->v_fifoinfo->fi_writers == 0)
261 error = 0;
262 }
263 return (error);
264 }
265
266 /*
267 * Vnode op for write
268 */
269 int
fifo_write(void * v)270 fifo_write(void *v)
271 {
272 struct vop_write_args *ap = v;
273 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock;
274 int error, flags = 0;
275
276 #ifdef DIAGNOSTIC
277 if (ap->a_uio->uio_rw != UIO_WRITE)
278 panic("fifo_write mode");
279 #endif
280 if (ap->a_ioflag & IO_NDELAY)
281 flags |= MSG_DONTWAIT;
282 VOP_UNLOCK(ap->a_vp);
283 error = sosend(wso, NULL, ap->a_uio, NULL, NULL, flags);
284 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
285 return (error);
286 }
287
288 /*
289 * Device ioctl operation.
290 */
291 int
fifo_ioctl(void * v)292 fifo_ioctl(void *v)
293 {
294 struct vop_ioctl_args *ap = v;
295 struct file filetmp;
296 int error;
297
298 if (ap->a_command == FIONBIO)
299 return (0);
300 if (ap->a_fflag & FREAD) {
301 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock;
302 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
303 if (error)
304 return (error);
305 }
306 if (ap->a_fflag & FWRITE) {
307 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock;
308 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, ap->a_p);
309 if (error)
310 return (error);
311 }
312 return (0);
313 }
314
315 int
fifo_inactive(void * v)316 fifo_inactive(void *v)
317 {
318 struct vop_inactive_args *ap = v;
319
320 VOP_UNLOCK(ap->a_vp);
321 return (0);
322 }
323
324
325 /*
326 * Device close routine
327 */
328 int
fifo_close(void * v)329 fifo_close(void *v)
330 {
331 struct vop_close_args *ap = v;
332 struct vnode *vp = ap->a_vp;
333 struct fifoinfo *fip = vp->v_fifoinfo;
334 int error1 = 0, error2 = 0;
335
336 if (fip == NULL)
337 return (0);
338
339 if (ap->a_fflag & FREAD) {
340 if (--fip->fi_readers == 0) {
341 struct socket *wso = fip->fi_writesock;
342
343 solock(wso);
344 socantsendmore(wso);
345 sounlock(wso);
346 }
347 }
348 if (ap->a_fflag & FWRITE) {
349 if (--fip->fi_writers == 0) {
350 struct socket *rso = fip->fi_readsock;
351
352 solock(rso);
353 /* SS_ISDISCONNECTED will result in POLLHUP */
354 rso->so_state |= SS_ISDISCONNECTED;
355 socantrcvmore(rso);
356 sounlock(rso);
357 }
358 }
359 if (fip->fi_readers == 0 && fip->fi_writers == 0) {
360 error1 = soclose(fip->fi_readsock, 0);
361 error2 = soclose(fip->fi_writesock, 0);
362 free(fip, M_VNODE, sizeof *fip);
363 vp->v_fifoinfo = NULL;
364 }
365 return (error1 ? error1 : error2);
366 }
367
368 int
fifo_reclaim(void * v)369 fifo_reclaim(void *v)
370 {
371 struct vop_reclaim_args *ap = v;
372 struct vnode *vp = ap->a_vp;
373 struct fifoinfo *fip = vp->v_fifoinfo;
374
375 if (fip == NULL)
376 return (0);
377
378 soclose(fip->fi_readsock, 0);
379 soclose(fip->fi_writesock, 0);
380 free(fip, M_VNODE, sizeof *fip);
381 vp->v_fifoinfo = NULL;
382
383 return (0);
384 }
385
386 /*
387 * Print out the contents of a fifo vnode.
388 */
389 int
fifo_print(void * v)390 fifo_print(void *v)
391 {
392 struct vop_print_args *ap = v;
393
394 printf("tag VT_NON");
395 fifo_printinfo(ap->a_vp);
396 printf("\n");
397 return 0;
398 }
399
400 /*
401 * Print out internal contents of a fifo vnode.
402 */
403 void
fifo_printinfo(struct vnode * vp)404 fifo_printinfo(struct vnode *vp)
405 {
406 struct fifoinfo *fip = vp->v_fifoinfo;
407
408 printf(", fifo with %ld readers and %ld writers",
409 fip->fi_readers, fip->fi_writers);
410 }
411
412 /*
413 * Return POSIX pathconf information applicable to fifo's.
414 */
415 int
fifo_pathconf(void * v)416 fifo_pathconf(void *v)
417 {
418 struct vop_pathconf_args *ap = v;
419 int error = 0;
420
421 switch (ap->a_name) {
422 case _PC_LINK_MAX:
423 *ap->a_retval = LINK_MAX;
424 break;
425 case _PC_CHOWN_RESTRICTED:
426 *ap->a_retval = 1;
427 break;
428 case _PC_TIMESTAMP_RESOLUTION:
429 *ap->a_retval = 1;
430 break;
431 default:
432 error = EINVAL;
433 break;
434 }
435
436 return (error);
437 }
438
439 /*
440 * Fifo failed operation
441 */
442 int
fifo_ebadf(void * v)443 fifo_ebadf(void *v)
444 {
445
446 return (EBADF);
447 }
448
449 /*
450 * Fifo advisory byte-level locks.
451 */
452 int
fifo_advlock(void * v)453 fifo_advlock(void *v)
454 {
455 return (EOPNOTSUPP);
456 }
457
458 int
fifo_kqfilter(void * v)459 fifo_kqfilter(void *v)
460 {
461 struct vop_kqfilter_args *ap = v;
462 struct fifoinfo *fip = ap->a_vp->v_fifoinfo;
463 struct sockbuf *sb;
464 struct socket *so;
465
466 switch (ap->a_kn->kn_filter) {
467 case EVFILT_READ:
468 if (!(ap->a_fflag & FREAD))
469 return (EINVAL);
470 ap->a_kn->kn_fop = &fiforead_filtops;
471 so = fip->fi_readsock;
472 sb = &so->so_rcv;
473 break;
474 case EVFILT_WRITE:
475 if (!(ap->a_fflag & FWRITE)) {
476 /* Tell upper layer to ask for POLLUP only */
477 if (ap->a_kn->kn_flags & (__EV_POLL | __EV_SELECT))
478 return (EPERM);
479 return (EINVAL);
480 }
481 ap->a_kn->kn_fop = &fifowrite_filtops;
482 so = fip->fi_writesock;
483 sb = &so->so_snd;
484 break;
485 case EVFILT_EXCEPT:
486 if (ap->a_kn->kn_flags & __EV_SELECT) {
487 /* Prevent triggering exceptfds. */
488 return (EPERM);
489 }
490 if ((ap->a_kn->kn_flags & __EV_POLL) == 0) {
491 /* Disallow usage through kevent(2). */
492 return (EINVAL);
493 }
494 ap->a_kn->kn_fop = &fifoexcept_filtops;
495 so = fip->fi_readsock;
496 sb = &so->so_rcv;
497 break;
498 default:
499 return (EINVAL);
500 }
501
502 ap->a_kn->kn_hook = so;
503
504 klist_insert(&sb->sb_klist, ap->a_kn);
505
506 return (0);
507 }
508
509 void
filt_fifordetach(struct knote * kn)510 filt_fifordetach(struct knote *kn)
511 {
512 struct socket *so = (struct socket *)kn->kn_hook;
513
514 klist_remove(&so->so_rcv.sb_klist, kn);
515 }
516
517 int
filt_fiforead(struct knote * kn,long hint)518 filt_fiforead(struct knote *kn, long hint)
519 {
520 struct socket *so = kn->kn_hook;
521 int rv;
522
523 MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
524
525 kn->kn_data = so->so_rcv.sb_cc;
526 if (so->so_rcv.sb_state & SS_CANTRCVMORE) {
527 kn->kn_flags |= EV_EOF;
528 if (kn->kn_flags & __EV_POLL) {
529 if (so->so_state & SS_ISDISCONNECTED)
530 kn->kn_flags |= __EV_HUP;
531 else
532 kn->kn_flags &= ~__EV_HUP;
533 }
534 rv = 1;
535 } else {
536 kn->kn_flags &= ~(EV_EOF | __EV_HUP);
537 rv = (kn->kn_data > 0);
538 }
539
540 return (rv);
541 }
542
543 void
filt_fifowdetach(struct knote * kn)544 filt_fifowdetach(struct knote *kn)
545 {
546 struct socket *so = (struct socket *)kn->kn_hook;
547
548 klist_remove(&so->so_snd.sb_klist, kn);
549 }
550
551 int
filt_fifowrite(struct knote * kn,long hint)552 filt_fifowrite(struct knote *kn, long hint)
553 {
554 struct socket *so = kn->kn_hook;
555 int rv;
556
557 MUTEX_ASSERT_LOCKED(&so->so_snd.sb_mtx);
558
559 kn->kn_data = sbspace(so, &so->so_snd);
560 if (so->so_snd.sb_state & SS_CANTSENDMORE) {
561 kn->kn_flags |= EV_EOF;
562 rv = 1;
563 } else {
564 kn->kn_flags &= ~EV_EOF;
565 rv = (kn->kn_data >= so->so_snd.sb_lowat);
566 }
567
568 return (rv);
569 }
570
571 int
filt_fifoexcept(struct knote * kn,long hint)572 filt_fifoexcept(struct knote *kn, long hint)
573 {
574 struct socket *so = kn->kn_hook;
575 int rv = 0;
576
577 MUTEX_ASSERT_LOCKED(&so->so_rcv.sb_mtx);
578
579 if (kn->kn_flags & __EV_POLL) {
580 if (so->so_state & SS_ISDISCONNECTED) {
581 kn->kn_flags |= __EV_HUP;
582 rv = 1;
583 } else {
584 kn->kn_flags &= ~__EV_HUP;
585 }
586 }
587
588 return (rv);
589 }
590
591 int
filt_fiformodify(struct kevent * kev,struct knote * kn)592 filt_fiformodify(struct kevent *kev, struct knote *kn)
593 {
594 struct socket *so = kn->kn_hook;
595 int rv;
596
597 solock(so);
598 mtx_enter(&so->so_rcv.sb_mtx);
599 rv = knote_modify(kev, kn);
600 mtx_leave(&so->so_rcv.sb_mtx);
601 sounlock(so);
602
603 return (rv);
604 }
605
606 int
filt_fiforprocess(struct knote * kn,struct kevent * kev)607 filt_fiforprocess(struct knote *kn, struct kevent *kev)
608 {
609 struct socket *so = kn->kn_hook;
610 int rv;
611
612 solock(so);
613 mtx_enter(&so->so_rcv.sb_mtx);
614 rv = knote_process(kn, kev);
615 mtx_leave(&so->so_rcv.sb_mtx);
616 sounlock(so);
617
618 return (rv);
619 }
620
621 int
filt_fifowmodify(struct kevent * kev,struct knote * kn)622 filt_fifowmodify(struct kevent *kev, struct knote *kn)
623 {
624 struct socket *so = kn->kn_hook;
625 int rv;
626
627 mtx_enter(&so->so_snd.sb_mtx);
628 rv = knote_modify(kev, kn);
629 mtx_leave(&so->so_snd.sb_mtx);
630
631 return (rv);
632 }
633
634 int
filt_fifowprocess(struct knote * kn,struct kevent * kev)635 filt_fifowprocess(struct knote *kn, struct kevent *kev)
636 {
637 struct socket *so = kn->kn_hook;
638 int rv;
639
640 mtx_enter(&so->so_snd.sb_mtx);
641 rv = knote_process(kn, kev);
642 mtx_leave(&so->so_snd.sb_mtx);
643
644 return (rv);
645 }
646