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