1 /* 2 * Copyright (c) 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)fifo_vnops.c 8.10 (Berkeley) 5/27/95 34 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/event.h> 39 #include <sys/filio.h> 40 #include <sys/fcntl.h> 41 #include <sys/file.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/malloc.h> 46 #include <sys/poll.h> 47 #include <sys/proc.h> /* XXXKSE */ 48 #include <sys/signalvar.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/sx.h> 52 #include <sys/systm.h> 53 #include <sys/un.h> 54 #include <sys/unistd.h> 55 #include <sys/vnode.h> 56 #include <fs/fifofs/fifo.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 static int fifo_print(struct vop_print_args *); 70 static int fifo_lookup(struct vop_lookup_args *); 71 static int fifo_open(struct vop_open_args *); 72 static int fifo_close(struct vop_close_args *); 73 static int fifo_read(struct vop_read_args *); 74 static int fifo_write(struct vop_write_args *); 75 static int fifo_ioctl(struct vop_ioctl_args *); 76 static int fifo_poll(struct vop_poll_args *); 77 static int fifo_kqfilter(struct vop_kqfilter_args *); 78 static int fifo_pathconf(struct vop_pathconf_args *); 79 static int fifo_advlock(struct vop_advlock_args *); 80 81 static void filt_fifordetach(struct knote *kn); 82 static int filt_fiforead(struct knote *kn, long hint); 83 static void filt_fifowdetach(struct knote *kn); 84 static int filt_fifowrite(struct knote *kn, long hint); 85 86 static struct filterops fiforead_filtops = 87 { 1, NULL, filt_fifordetach, filt_fiforead }; 88 static struct filterops fifowrite_filtops = 89 { 1, NULL, filt_fifowdetach, filt_fifowrite }; 90 91 vop_t **fifo_vnodeop_p; 92 static struct vnodeopv_entry_desc fifo_vnodeop_entries[] = { 93 { &vop_default_desc, (vop_t *) vop_defaultop }, 94 { &vop_access_desc, (vop_t *) vop_ebadf }, 95 { &vop_advlock_desc, (vop_t *) fifo_advlock }, 96 { &vop_close_desc, (vop_t *) fifo_close }, 97 { &vop_create_desc, (vop_t *) vop_panic }, 98 { &vop_getattr_desc, (vop_t *) vop_ebadf }, 99 { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount }, 100 { &vop_ioctl_desc, (vop_t *) fifo_ioctl }, 101 { &vop_kqfilter_desc, (vop_t *) fifo_kqfilter }, 102 { &vop_lease_desc, (vop_t *) vop_null }, 103 { &vop_link_desc, (vop_t *) vop_panic }, 104 { &vop_lookup_desc, (vop_t *) fifo_lookup }, 105 { &vop_mkdir_desc, (vop_t *) vop_panic }, 106 { &vop_mknod_desc, (vop_t *) vop_panic }, 107 { &vop_open_desc, (vop_t *) fifo_open }, 108 { &vop_pathconf_desc, (vop_t *) fifo_pathconf }, 109 { &vop_poll_desc, (vop_t *) fifo_poll }, 110 { &vop_print_desc, (vop_t *) fifo_print }, 111 { &vop_read_desc, (vop_t *) fifo_read }, 112 { &vop_readdir_desc, (vop_t *) vop_panic }, 113 { &vop_readlink_desc, (vop_t *) vop_panic }, 114 { &vop_reallocblks_desc, (vop_t *) vop_panic }, 115 { &vop_reclaim_desc, (vop_t *) vop_null }, 116 { &vop_remove_desc, (vop_t *) vop_panic }, 117 { &vop_rename_desc, (vop_t *) vop_panic }, 118 { &vop_rmdir_desc, (vop_t *) vop_panic }, 119 { &vop_setattr_desc, (vop_t *) vop_ebadf }, 120 { &vop_symlink_desc, (vop_t *) vop_panic }, 121 { &vop_write_desc, (vop_t *) fifo_write }, 122 { NULL, NULL } 123 }; 124 static struct vnodeopv_desc fifo_vnodeop_opv_desc = 125 { &fifo_vnodeop_p, fifo_vnodeop_entries }; 126 127 VNODEOP_SET(fifo_vnodeop_opv_desc); 128 129 int 130 fifo_vnoperate(ap) 131 struct vop_generic_args /* { 132 struct vnodeop_desc *a_desc; 133 <other random data follows, presumably> 134 } */ *ap; 135 { 136 return (VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, ap)); 137 } 138 139 /* 140 * Trivial lookup routine that always fails. 141 */ 142 /* ARGSUSED */ 143 static int 144 fifo_lookup(ap) 145 struct vop_lookup_args /* { 146 struct vnode * a_dvp; 147 struct vnode ** a_vpp; 148 struct componentname * a_cnp; 149 } */ *ap; 150 { 151 152 *ap->a_vpp = NULL; 153 return (ENOTDIR); 154 } 155 156 /* 157 * Open called to set up a new instance of a fifo or 158 * to find an active instance of a fifo. 159 */ 160 /* ARGSUSED */ 161 static int 162 fifo_open(ap) 163 struct vop_open_args /* { 164 struct vnode *a_vp; 165 int a_mode; 166 struct ucred *a_cred; 167 struct thread *a_td; 168 } */ *ap; 169 { 170 struct vnode *vp = ap->a_vp; 171 struct fifoinfo *fip; 172 struct thread *td = ap->a_td; 173 struct socket *rso, *wso; 174 int error; 175 176 if ((fip = vp->v_fifoinfo) == NULL) { 177 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 178 vp->v_fifoinfo = fip; 179 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, 180 ap->a_td->td_ucred, ap->a_td); 181 if (error) { 182 free(fip, M_VNODE); 183 vp->v_fifoinfo = NULL; 184 return (error); 185 } 186 fip->fi_readsock = rso; 187 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, 188 ap->a_td->td_ucred, ap->a_td); 189 if (error) { 190 (void)soclose(rso); 191 free(fip, M_VNODE); 192 vp->v_fifoinfo = NULL; 193 return (error); 194 } 195 fip->fi_writesock = wso; 196 error = unp_connect2(wso, rso); 197 if (error) { 198 (void)soclose(wso); 199 (void)soclose(rso); 200 free(fip, M_VNODE); 201 vp->v_fifoinfo = NULL; 202 return (error); 203 } 204 fip->fi_readers = fip->fi_writers = 0; 205 wso->so_snd.sb_lowat = PIPE_BUF; 206 rso->so_state |= SS_CANTRCVMORE; 207 } 208 if (ap->a_mode & FREAD) { 209 fip->fi_readers++; 210 if (fip->fi_readers == 1) { 211 fip->fi_writesock->so_state &= ~SS_CANTSENDMORE; 212 if (fip->fi_writers > 0) { 213 wakeup(&fip->fi_writers); 214 sowwakeup(fip->fi_writesock); 215 } 216 } 217 } 218 if (ap->a_mode & FWRITE) { 219 fip->fi_writers++; 220 if (fip->fi_writers == 1) { 221 fip->fi_readsock->so_state &= ~SS_CANTRCVMORE; 222 if (fip->fi_readers > 0) { 223 wakeup(&fip->fi_readers); 224 sorwakeup(fip->fi_writesock); 225 } 226 } 227 } 228 if ((ap->a_mode & FREAD) && (ap->a_mode & O_NONBLOCK) == 0) { 229 if (fip->fi_writers == 0) { 230 VOP_UNLOCK(vp, 0, td); 231 error = tsleep(&fip->fi_readers, 232 PCATCH | PSOCK, "fifoor", 0); 233 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 234 if (error) 235 goto bad; 236 /* 237 * We must have got woken up because we had a writer. 238 * That (and not still having one) is the condition 239 * that we must wait for. 240 */ 241 } 242 } 243 if (ap->a_mode & FWRITE) { 244 if (ap->a_mode & O_NONBLOCK) { 245 if (fip->fi_readers == 0) { 246 error = ENXIO; 247 goto bad; 248 } 249 } else { 250 if (fip->fi_readers == 0) { 251 VOP_UNLOCK(vp, 0, td); 252 error = tsleep(&fip->fi_writers, 253 PCATCH | PSOCK, "fifoow", 0); 254 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 255 if (error) 256 goto bad; 257 /* 258 * We must have got woken up because we had 259 * a reader. That (and not still having one) 260 * is the condition that we must wait for. 261 */ 262 } 263 } 264 } 265 return (0); 266 bad: 267 VOP_CLOSE(vp, ap->a_mode, ap->a_cred, td); 268 return (error); 269 } 270 271 /* 272 * Vnode op for read 273 */ 274 /* ARGSUSED */ 275 static int 276 fifo_read(ap) 277 struct vop_read_args /* { 278 struct vnode *a_vp; 279 struct uio *a_uio; 280 int a_ioflag; 281 struct ucred *a_cred; 282 } */ *ap; 283 { 284 struct uio *uio = ap->a_uio; 285 struct socket *rso = ap->a_vp->v_fifoinfo->fi_readsock; 286 struct thread *td = uio->uio_td; 287 int error, startresid; 288 289 #ifdef DIAGNOSTIC 290 if (uio->uio_rw != UIO_READ) 291 panic("fifo_read mode"); 292 #endif 293 if (uio->uio_resid == 0) 294 return (0); 295 if (ap->a_ioflag & IO_NDELAY) 296 rso->so_state |= SS_NBIO; 297 startresid = uio->uio_resid; 298 VOP_UNLOCK(ap->a_vp, 0, td); 299 error = soreceive(rso, (struct sockaddr **)0, uio, (struct mbuf **)0, 300 (struct mbuf **)0, (int *)0); 301 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td); 302 if (ap->a_ioflag & IO_NDELAY) 303 rso->so_state &= ~SS_NBIO; 304 return (error); 305 } 306 307 /* 308 * Vnode op for write 309 */ 310 /* ARGSUSED */ 311 static int 312 fifo_write(ap) 313 struct vop_write_args /* { 314 struct vnode *a_vp; 315 struct uio *a_uio; 316 int a_ioflag; 317 struct ucred *a_cred; 318 } */ *ap; 319 { 320 struct socket *wso = ap->a_vp->v_fifoinfo->fi_writesock; 321 struct thread *td = ap->a_uio->uio_td; 322 int error; 323 324 #ifdef DIAGNOSTIC 325 if (ap->a_uio->uio_rw != UIO_WRITE) 326 panic("fifo_write mode"); 327 #endif 328 if (ap->a_ioflag & IO_NDELAY) 329 wso->so_state |= SS_NBIO; 330 VOP_UNLOCK(ap->a_vp, 0, td); 331 error = sosend(wso, (struct sockaddr *)0, ap->a_uio, 0, 332 (struct mbuf *)0, 0, td); 333 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td); 334 if (ap->a_ioflag & IO_NDELAY) 335 wso->so_state &= ~SS_NBIO; 336 return (error); 337 } 338 339 /* 340 * Device ioctl operation. 341 */ 342 /* ARGSUSED */ 343 static int 344 fifo_ioctl(ap) 345 struct vop_ioctl_args /* { 346 struct vnode *a_vp; 347 u_long a_command; 348 caddr_t a_data; 349 int a_fflag; 350 struct ucred *a_cred; 351 struct thread *a_td; 352 } */ *ap; 353 { 354 struct file filetmp; /* Local, so need not be locked. */ 355 int error; 356 357 if (ap->a_command == FIONBIO) 358 return (0); 359 if (ap->a_fflag & FREAD) { 360 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock; 361 filetmp.f_cred = ap->a_cred; 362 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, 363 ap->a_td->td_ucred, ap->a_td); 364 if (error) 365 return (error); 366 } 367 if (ap->a_fflag & FWRITE) { 368 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock; 369 filetmp.f_cred = ap->a_cred; 370 error = soo_ioctl(&filetmp, ap->a_command, ap->a_data, 371 ap->a_td->td_ucred, ap->a_td); 372 if (error) 373 return (error); 374 } 375 return (0); 376 } 377 378 /* ARGSUSED */ 379 static int 380 fifo_kqfilter(ap) 381 struct vop_kqfilter_args /* { 382 struct vnode *a_vp; 383 struct knote *a_kn; 384 } */ *ap; 385 { 386 struct fifoinfo *fi = ap->a_vp->v_fifoinfo; 387 struct socket *so; 388 struct sockbuf *sb; 389 390 switch (ap->a_kn->kn_filter) { 391 case EVFILT_READ: 392 ap->a_kn->kn_fop = &fiforead_filtops; 393 so = fi->fi_readsock; 394 sb = &so->so_rcv; 395 break; 396 case EVFILT_WRITE: 397 ap->a_kn->kn_fop = &fifowrite_filtops; 398 so = fi->fi_writesock; 399 sb = &so->so_snd; 400 break; 401 default: 402 return (1); 403 } 404 405 ap->a_kn->kn_hook = (caddr_t)so; 406 407 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, ap->a_kn, kn_selnext); 408 sb->sb_flags |= SB_KNOTE; 409 410 return (0); 411 } 412 413 static void 414 filt_fifordetach(struct knote *kn) 415 { 416 struct socket *so = (struct socket *)kn->kn_hook; 417 418 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext); 419 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note)) 420 so->so_rcv.sb_flags &= ~SB_KNOTE; 421 } 422 423 static int 424 filt_fiforead(struct knote *kn, long hint) 425 { 426 struct socket *so = (struct socket *)kn->kn_hook; 427 428 kn->kn_data = so->so_rcv.sb_cc; 429 if (so->so_state & SS_CANTRCVMORE) { 430 kn->kn_flags |= EV_EOF; 431 return (1); 432 } 433 kn->kn_flags &= ~EV_EOF; 434 return (kn->kn_data > 0); 435 } 436 437 static void 438 filt_fifowdetach(struct knote *kn) 439 { 440 struct socket *so = (struct socket *)kn->kn_hook; 441 442 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext); 443 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note)) 444 so->so_snd.sb_flags &= ~SB_KNOTE; 445 } 446 447 static int 448 filt_fifowrite(struct knote *kn, long hint) 449 { 450 struct socket *so = (struct socket *)kn->kn_hook; 451 452 kn->kn_data = sbspace(&so->so_snd); 453 if (so->so_state & SS_CANTSENDMORE) { 454 kn->kn_flags |= EV_EOF; 455 return (1); 456 } 457 kn->kn_flags &= ~EV_EOF; 458 return (kn->kn_data >= so->so_snd.sb_lowat); 459 } 460 461 /* ARGSUSED */ 462 static int 463 fifo_poll(ap) 464 struct vop_poll_args /* { 465 struct vnode *a_vp; 466 int a_events; 467 struct ucred *a_cred; 468 struct thread *a_td; 469 } */ *ap; 470 { 471 struct file filetmp; 472 int events, revents = 0; 473 474 events = ap->a_events & 475 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); 476 if (events) { 477 /* 478 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is 479 * not, then convert the first two to the last one. This 480 * tells the socket poll function to ignore EOF so that we 481 * block if there is no writer (and no data). Callers can 482 * set POLLINIGNEOF to get non-blocking behavior. 483 */ 484 if (events & (POLLIN | POLLRDNORM) && 485 !(events & POLLINIGNEOF)) { 486 events &= ~(POLLIN | POLLRDNORM); 487 events |= POLLINIGNEOF; 488 } 489 490 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_readsock; 491 filetmp.f_cred = ap->a_cred; 492 if (filetmp.f_data) 493 revents |= soo_poll(&filetmp, events, 494 ap->a_td->td_ucred, ap->a_td); 495 496 /* Reverse the above conversion. */ 497 if ((revents & POLLINIGNEOF) && 498 !(ap->a_events & POLLINIGNEOF)) { 499 revents |= (ap->a_events & (POLLIN | POLLRDNORM)); 500 revents &= ~POLLINIGNEOF; 501 } 502 } 503 events = ap->a_events & (POLLOUT | POLLWRNORM | POLLWRBAND); 504 if (events) { 505 filetmp.f_data = ap->a_vp->v_fifoinfo->fi_writesock; 506 filetmp.f_cred = ap->a_cred; 507 if (filetmp.f_data) { 508 revents |= soo_poll(&filetmp, events, 509 ap->a_td->td_ucred, ap->a_td); 510 } 511 } 512 return (revents); 513 } 514 515 /* 516 * Device close routine 517 */ 518 /* ARGSUSED */ 519 static int 520 fifo_close(ap) 521 struct vop_close_args /* { 522 struct vnode *a_vp; 523 int a_fflag; 524 struct ucred *a_cred; 525 struct thread *a_td; 526 } */ *ap; 527 { 528 register struct vnode *vp = ap->a_vp; 529 register struct fifoinfo *fip = vp->v_fifoinfo; 530 int error1, error2; 531 532 if (ap->a_fflag & FREAD) { 533 fip->fi_readers--; 534 if (fip->fi_readers == 0) 535 socantsendmore(fip->fi_writesock); 536 } 537 if (ap->a_fflag & FWRITE) { 538 fip->fi_writers--; 539 if (fip->fi_writers == 0) 540 socantrcvmore(fip->fi_readsock); 541 } 542 if (vrefcnt(vp) > 1) 543 return (0); 544 error1 = soclose(fip->fi_readsock); 545 error2 = soclose(fip->fi_writesock); 546 FREE(fip, M_VNODE); 547 vp->v_fifoinfo = NULL; 548 if (error1) 549 return (error1); 550 return (error2); 551 } 552 553 554 /* 555 * Print out internal contents of a fifo vnode. 556 */ 557 int 558 fifo_printinfo(vp) 559 struct vnode *vp; 560 { 561 register struct fifoinfo *fip = vp->v_fifoinfo; 562 563 printf(", fifo with %ld readers and %ld writers", 564 fip->fi_readers, fip->fi_writers); 565 return (0); 566 } 567 568 /* 569 * Print out the contents of a fifo vnode. 570 */ 571 static int 572 fifo_print(ap) 573 struct vop_print_args /* { 574 struct vnode *a_vp; 575 } */ *ap; 576 { 577 fifo_printinfo(ap->a_vp); 578 printf("\n"); 579 return (0); 580 } 581 582 /* 583 * Return POSIX pathconf information applicable to fifo's. 584 */ 585 static int 586 fifo_pathconf(ap) 587 struct vop_pathconf_args /* { 588 struct vnode *a_vp; 589 int a_name; 590 int *a_retval; 591 } */ *ap; 592 { 593 594 switch (ap->a_name) { 595 case _PC_LINK_MAX: 596 *ap->a_retval = LINK_MAX; 597 return (0); 598 case _PC_PIPE_BUF: 599 *ap->a_retval = PIPE_BUF; 600 return (0); 601 case _PC_CHOWN_RESTRICTED: 602 *ap->a_retval = 1; 603 return (0); 604 default: 605 return (EINVAL); 606 } 607 /* NOTREACHED */ 608 } 609 610 /* 611 * Fifo advisory byte-level locks. 612 */ 613 /* ARGSUSED */ 614 static int 615 fifo_advlock(ap) 616 struct vop_advlock_args /* { 617 struct vnode *a_vp; 618 caddr_t a_id; 619 int a_op; 620 struct flock *a_fl; 621 int a_flags; 622 } */ *ap; 623 { 624 625 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 626 } 627