1 /*- 2 * Copyright (c) 1990, 1993, 1995 3 * The Regents of the University of California. 4 * Copyright (c) 2005 Robert N. M. Watson 5 * 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 * 4. 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 * $FreeBSD$ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/event.h> 37 #include <sys/file.h> 38 #include <sys/filedesc.h> 39 #include <sys/filio.h> 40 #include <sys/fcntl.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/malloc.h> 45 #include <sys/poll.h> 46 #include <sys/proc.h> /* XXXKSE */ 47 #include <sys/signalvar.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sx.h> 51 #include <sys/systm.h> 52 #include <sys/un.h> 53 #include <sys/unistd.h> 54 #include <sys/vnode.h> 55 #include <fs/fifofs/fifo.h> 56 57 static fo_rdwr_t fifo_read_f; 58 static fo_rdwr_t fifo_write_f; 59 static fo_ioctl_t fifo_ioctl_f; 60 static fo_poll_t fifo_poll_f; 61 static fo_kqfilter_t fifo_kqfilter_f; 62 static fo_stat_t fifo_stat_f; 63 static fo_close_t fifo_close_f; 64 static fo_truncate_t fifo_truncate_f; 65 66 struct fileops fifo_ops_f = { 67 .fo_read = fifo_read_f, 68 .fo_write = fifo_write_f, 69 .fo_truncate = fifo_truncate_f, 70 .fo_ioctl = fifo_ioctl_f, 71 .fo_poll = fifo_poll_f, 72 .fo_kqfilter = fifo_kqfilter_f, 73 .fo_stat = fifo_stat_f, 74 .fo_close = fifo_close_f, 75 .fo_flags = DFLAG_PASSABLE 76 }; 77 78 /* 79 * This structure is associated with the FIFO vnode and stores 80 * the state associated with the FIFO. 81 */ 82 struct fifoinfo { 83 struct socket *fi_readsock; 84 struct socket *fi_writesock; 85 long fi_readers; 86 long fi_writers; 87 }; 88 89 static vop_print_t fifo_print; 90 static vop_open_t fifo_open; 91 static vop_close_t fifo_close; 92 static vop_ioctl_t fifo_ioctl; 93 static vop_kqfilter_t fifo_kqfilter; 94 static vop_pathconf_t fifo_pathconf; 95 static vop_advlock_t fifo_advlock; 96 97 static void filt_fifordetach(struct knote *kn); 98 static int filt_fiforead(struct knote *kn, long hint); 99 static void filt_fifowdetach(struct knote *kn); 100 static int filt_fifowrite(struct knote *kn, long hint); 101 static void filt_fifodetach_notsup(struct knote *kn); 102 static int filt_fifo_notsup(struct knote *kn, long hint); 103 104 static struct filterops fiforead_filtops = 105 { 1, NULL, filt_fifordetach, filt_fiforead }; 106 static struct filterops fifowrite_filtops = 107 { 1, NULL, filt_fifowdetach, filt_fifowrite }; 108 static struct filterops fifo_notsup_filtops = 109 { 1, NULL, filt_fifodetach_notsup, filt_fifo_notsup }; 110 111 struct vop_vector fifo_specops = { 112 .vop_default = &default_vnodeops, 113 114 .vop_access = VOP_EBADF, 115 .vop_advlock = fifo_advlock, 116 .vop_close = fifo_close, 117 .vop_create = VOP_PANIC, 118 .vop_getattr = VOP_EBADF, 119 .vop_ioctl = fifo_ioctl, 120 .vop_kqfilter = fifo_kqfilter, 121 .vop_lease = VOP_NULL, 122 .vop_link = VOP_PANIC, 123 .vop_mkdir = VOP_PANIC, 124 .vop_mknod = VOP_PANIC, 125 .vop_open = fifo_open, 126 .vop_pathconf = fifo_pathconf, 127 .vop_print = fifo_print, 128 .vop_read = VOP_PANIC, 129 .vop_readdir = VOP_PANIC, 130 .vop_readlink = VOP_PANIC, 131 .vop_reallocblks = VOP_PANIC, 132 .vop_reclaim = VOP_NULL, 133 .vop_remove = VOP_PANIC, 134 .vop_rename = VOP_PANIC, 135 .vop_rmdir = VOP_PANIC, 136 .vop_setattr = VOP_EBADF, 137 .vop_symlink = VOP_PANIC, 138 .vop_write = VOP_PANIC, 139 }; 140 141 struct mtx fifo_mtx; 142 MTX_SYSINIT(fifo, &fifo_mtx, "fifo mutex", MTX_DEF); 143 144 /* 145 * Dispose of fifo resources. 146 */ 147 static void 148 fifo_cleanup(struct vnode *vp) 149 { 150 struct fifoinfo *fip = vp->v_fifoinfo; 151 152 ASSERT_VOP_LOCKED(vp, "fifo_cleanup"); 153 if (fip->fi_readers == 0 && fip->fi_writers == 0) { 154 vp->v_fifoinfo = NULL; 155 (void)soclose(fip->fi_readsock); 156 (void)soclose(fip->fi_writesock); 157 FREE(fip, M_VNODE); 158 } 159 } 160 161 /* 162 * Open called to set up a new instance of a fifo or 163 * to find an active instance of a fifo. 164 */ 165 /* ARGSUSED */ 166 static int 167 fifo_open(ap) 168 struct vop_open_args /* { 169 struct vnode *a_vp; 170 int a_mode; 171 struct ucred *a_cred; 172 struct thread *a_td; 173 int a_fdidx; 174 } */ *ap; 175 { 176 struct vnode *vp = ap->a_vp; 177 struct fifoinfo *fip; 178 struct thread *td = ap->a_td; 179 struct ucred *cred = ap->a_cred; 180 struct file *fp = ap->a_fp; 181 struct socket *rso, *wso; 182 int error; 183 184 ASSERT_VOP_ELOCKED(vp, "fifo_open"); 185 if (fp == NULL) 186 return (EINVAL); 187 if ((fip = vp->v_fifoinfo) == NULL) { 188 MALLOC(fip, struct fifoinfo *, sizeof(*fip), M_VNODE, M_WAITOK); 189 error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, cred, td); 190 if (error) 191 goto fail1; 192 fip->fi_readsock = rso; 193 error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, cred, td); 194 if (error) 195 goto fail2; 196 fip->fi_writesock = wso; 197 error = soconnect2(wso, rso); 198 if (error) { 199 (void)soclose(wso); 200 fail2: 201 (void)soclose(rso); 202 fail1: 203 free(fip, M_VNODE); 204 return (error); 205 } 206 fip->fi_readers = fip->fi_writers = 0; 207 wso->so_snd.sb_lowat = PIPE_BUF; 208 SOCKBUF_LOCK(&rso->so_rcv); 209 rso->so_rcv.sb_state |= SBS_CANTRCVMORE; 210 SOCKBUF_UNLOCK(&rso->so_rcv); 211 KASSERT(vp->v_fifoinfo == NULL, 212 ("fifo_open: v_fifoinfo race")); 213 vp->v_fifoinfo = fip; 214 } 215 216 /* 217 * General access to fi_readers and fi_writers is protected using 218 * the vnode lock. 219 * 220 * Protect the increment of fi_readers and fi_writers and the 221 * associated calls to wakeup() with the fifo mutex in addition 222 * to the vnode lock. This allows the vnode lock to be dropped 223 * for the msleep() calls below, and using the fifo mutex with 224 * msleep() prevents the wakeup from being missed. 225 */ 226 mtx_lock(&fifo_mtx); 227 if (ap->a_mode & FREAD) { 228 fip->fi_readers++; 229 if (fip->fi_readers == 1) { 230 SOCKBUF_LOCK(&fip->fi_writesock->so_snd); 231 fip->fi_writesock->so_snd.sb_state &= ~SBS_CANTSENDMORE; 232 SOCKBUF_UNLOCK(&fip->fi_writesock->so_snd); 233 if (fip->fi_writers > 0) { 234 wakeup(&fip->fi_writers); 235 sowwakeup(fip->fi_writesock); 236 } 237 } 238 } 239 if (ap->a_mode & FWRITE) { 240 if ((ap->a_mode & O_NONBLOCK) && fip->fi_readers == 0) { 241 mtx_unlock(&fifo_mtx); 242 return (ENXIO); 243 } 244 fip->fi_writers++; 245 if (fip->fi_writers == 1) { 246 SOCKBUF_LOCK(&fip->fi_readsock->so_rcv); 247 fip->fi_readsock->so_rcv.sb_state &= ~SBS_CANTRCVMORE; 248 SOCKBUF_UNLOCK(&fip->fi_readsock->so_rcv); 249 if (fip->fi_readers > 0) { 250 wakeup(&fip->fi_readers); 251 sorwakeup(fip->fi_readsock); 252 } 253 } 254 } 255 if ((ap->a_mode & O_NONBLOCK) == 0) { 256 if ((ap->a_mode & FREAD) && fip->fi_writers == 0) { 257 VOP_UNLOCK(vp, 0); 258 error = msleep(&fip->fi_readers, &fifo_mtx, 259 PDROP | PCATCH | PSOCK, "fifoor", 0); 260 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 261 if (error) { 262 fip->fi_readers--; 263 if (fip->fi_readers == 0) { 264 socantsendmore(fip->fi_writesock); 265 fifo_cleanup(vp); 266 } 267 return (error); 268 } 269 mtx_lock(&fifo_mtx); 270 /* 271 * We must have got woken up because we had a writer. 272 * That (and not still having one) is the condition 273 * that we must wait for. 274 */ 275 } 276 if ((ap->a_mode & FWRITE) && fip->fi_readers == 0) { 277 VOP_UNLOCK(vp, 0); 278 error = msleep(&fip->fi_writers, &fifo_mtx, 279 PDROP | PCATCH | PSOCK, "fifoow", 0); 280 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 281 if (error) { 282 fip->fi_writers--; 283 if (fip->fi_writers == 0) { 284 socantrcvmore(fip->fi_readsock); 285 fifo_cleanup(vp); 286 } 287 return (error); 288 } 289 /* 290 * We must have got woken up because we had 291 * a reader. That (and not still having one) 292 * is the condition that we must wait for. 293 */ 294 mtx_lock(&fifo_mtx); 295 } 296 } 297 mtx_unlock(&fifo_mtx); 298 KASSERT(fp != NULL, ("can't fifo/vnode bypass")); 299 KASSERT(fp->f_ops == &badfileops, ("not badfileops in fifo_open")); 300 finit(fp, fp->f_flag, DTYPE_FIFO, fip, &fifo_ops_f); 301 return (0); 302 } 303 304 /* 305 * Now unused vnode ioctl routine. 306 */ 307 /* ARGSUSED */ 308 static int 309 fifo_ioctl(ap) 310 struct vop_ioctl_args /* { 311 struct vnode *a_vp; 312 u_long a_command; 313 caddr_t a_data; 314 int a_fflag; 315 struct ucred *a_cred; 316 struct thread *a_td; 317 } */ *ap; 318 { 319 320 printf("WARNING: fifo_ioctl called unexpectedly\n"); 321 return (ENOTTY); 322 } 323 324 /* 325 * Now unused vnode kqfilter routine. 326 */ 327 /* ARGSUSED */ 328 static int 329 fifo_kqfilter(ap) 330 struct vop_kqfilter_args /* { 331 struct vnode *a_vp; 332 struct knote *a_kn; 333 } */ *ap; 334 { 335 336 printf("WARNING: fifo_kqfilter called unexpectedly\n"); 337 return (EINVAL); 338 } 339 340 static void 341 filt_fifordetach(struct knote *kn) 342 { 343 struct socket *so = (struct socket *)kn->kn_hook; 344 345 SOCKBUF_LOCK(&so->so_rcv); 346 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); 347 if (knlist_empty(&so->so_rcv.sb_sel.si_note)) 348 so->so_rcv.sb_flags &= ~SB_KNOTE; 349 SOCKBUF_UNLOCK(&so->so_rcv); 350 } 351 352 static int 353 filt_fiforead(struct knote *kn, long hint) 354 { 355 struct socket *so = (struct socket *)kn->kn_hook; 356 357 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 358 kn->kn_data = so->so_rcv.sb_cc; 359 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 360 kn->kn_flags |= EV_EOF; 361 return (1); 362 } else { 363 kn->kn_flags &= ~EV_EOF; 364 return (kn->kn_data > 0); 365 } 366 } 367 368 static void 369 filt_fifowdetach(struct knote *kn) 370 { 371 struct socket *so = (struct socket *)kn->kn_hook; 372 373 SOCKBUF_LOCK(&so->so_snd); 374 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1); 375 if (knlist_empty(&so->so_snd.sb_sel.si_note)) 376 so->so_snd.sb_flags &= ~SB_KNOTE; 377 SOCKBUF_UNLOCK(&so->so_snd); 378 } 379 380 static int 381 filt_fifowrite(struct knote *kn, long hint) 382 { 383 struct socket *so = (struct socket *)kn->kn_hook; 384 385 SOCKBUF_LOCK_ASSERT(&so->so_snd); 386 kn->kn_data = sbspace(&so->so_snd); 387 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 388 kn->kn_flags |= EV_EOF; 389 return (1); 390 } else { 391 kn->kn_flags &= ~EV_EOF; 392 return (kn->kn_data >= so->so_snd.sb_lowat); 393 } 394 } 395 396 static void 397 filt_fifodetach_notsup(struct knote *kn) 398 { 399 400 } 401 402 static int 403 filt_fifo_notsup(struct knote *kn, long hint) 404 { 405 406 return (0); 407 } 408 409 /* 410 * Device close routine 411 */ 412 /* ARGSUSED */ 413 static int 414 fifo_close(ap) 415 struct vop_close_args /* { 416 struct vnode *a_vp; 417 int a_fflag; 418 struct ucred *a_cred; 419 struct thread *a_td; 420 } */ *ap; 421 { 422 struct vnode *vp = ap->a_vp; 423 struct fifoinfo *fip = vp->v_fifoinfo; 424 425 ASSERT_VOP_LOCKED(vp, "fifo_close"); 426 KASSERT(fip != NULL, ("fifo_close: no v_fifoinfo")); 427 if (ap->a_fflag & FREAD) { 428 fip->fi_readers--; 429 if (fip->fi_readers == 0) 430 socantsendmore(fip->fi_writesock); 431 } 432 if (ap->a_fflag & FWRITE) { 433 fip->fi_writers--; 434 if (fip->fi_writers == 0) 435 socantrcvmore(fip->fi_readsock); 436 } 437 fifo_cleanup(vp); 438 return (0); 439 } 440 441 /* 442 * Print out internal contents of a fifo vnode. 443 */ 444 int 445 fifo_printinfo(vp) 446 struct vnode *vp; 447 { 448 register struct fifoinfo *fip = vp->v_fifoinfo; 449 450 if (fip == NULL){ 451 printf(", NULL v_fifoinfo"); 452 return (0); 453 } 454 printf(", fifo with %ld readers and %ld writers", 455 fip->fi_readers, fip->fi_writers); 456 return (0); 457 } 458 459 /* 460 * Print out the contents of a fifo vnode. 461 */ 462 static int 463 fifo_print(ap) 464 struct vop_print_args /* { 465 struct vnode *a_vp; 466 } */ *ap; 467 { 468 fifo_printinfo(ap->a_vp); 469 printf("\n"); 470 return (0); 471 } 472 473 /* 474 * Return POSIX pathconf information applicable to fifo's. 475 */ 476 static int 477 fifo_pathconf(ap) 478 struct vop_pathconf_args /* { 479 struct vnode *a_vp; 480 int a_name; 481 int *a_retval; 482 } */ *ap; 483 { 484 485 switch (ap->a_name) { 486 case _PC_LINK_MAX: 487 *ap->a_retval = LINK_MAX; 488 return (0); 489 case _PC_PIPE_BUF: 490 *ap->a_retval = PIPE_BUF; 491 return (0); 492 case _PC_CHOWN_RESTRICTED: 493 *ap->a_retval = 1; 494 return (0); 495 default: 496 return (EINVAL); 497 } 498 /* NOTREACHED */ 499 } 500 501 /* 502 * Fifo advisory byte-level locks. 503 */ 504 /* ARGSUSED */ 505 static int 506 fifo_advlock(ap) 507 struct vop_advlock_args /* { 508 struct vnode *a_vp; 509 caddr_t a_id; 510 int a_op; 511 struct flock *a_fl; 512 int a_flags; 513 } */ *ap; 514 { 515 516 return (ap->a_flags & F_FLOCK ? EOPNOTSUPP : EINVAL); 517 } 518 519 static int 520 fifo_close_f(struct file *fp, struct thread *td) 521 { 522 523 return (vnops.fo_close(fp, td)); 524 } 525 526 /* 527 * The implementation of ioctl() for named fifos is complicated by the fact 528 * that we permit O_RDWR fifo file descriptors, meaning that the actions of 529 * ioctls may have to be applied to both the underlying sockets rather than 530 * just one. The original implementation simply forward the ioctl to one 531 * or both sockets based on fp->f_flag. We now consider each ioctl 532 * separately, as the composition effect requires careful ordering. 533 * 534 * We do not blindly pass all ioctls through to the socket in order to avoid 535 * providing unnecessary ioctls that might be improperly depended on by 536 * applications (such as socket-specific, routing, and interface ioctls). 537 * 538 * Unlike sys_pipe.c, fifos do not implement the deprecated TIOCSPGRP and 539 * TIOCGPGRP ioctls. Earlier implementations of fifos did forward SIOCSPGRP 540 * and SIOCGPGRP ioctls, so we might need to re-add those here. 541 */ 542 static int 543 fifo_ioctl_f(struct file *fp, u_long com, void *data, struct ucred *cred, 544 struct thread *td) 545 { 546 struct fifoinfo *fi; 547 struct file filetmp; /* Local, so need not be locked. */ 548 int error; 549 550 error = ENOTTY; 551 fi = fp->f_data; 552 553 switch (com) { 554 case FIONBIO: 555 /* 556 * Non-blocking I/O is implemented at the fifo layer using 557 * MSG_NBIO, so does not need to be forwarded down the stack. 558 */ 559 return (0); 560 561 case FIOASYNC: 562 case FIOSETOWN: 563 case FIOGETOWN: 564 /* 565 * These socket ioctls don't have any ordering requirements, 566 * so are called in an arbitrary order, and only on the 567 * sockets indicated by the file descriptor rights. 568 * 569 * XXXRW: If O_RDWR and the read socket accepts an ioctl but 570 * the write socket doesn't, the socketpair is left in an 571 * inconsistent state. 572 */ 573 if (fp->f_flag & FREAD) { 574 filetmp.f_data = fi->fi_readsock; 575 filetmp.f_cred = cred; 576 error = soo_ioctl(&filetmp, com, data, cred, td); 577 if (error) 578 return (error); 579 } 580 if (fp->f_flag & FWRITE) { 581 filetmp.f_data = fi->fi_writesock; 582 filetmp.f_cred = cred; 583 error = soo_ioctl(&filetmp, com, data, cred, td); 584 } 585 return (error); 586 587 case FIONREAD: 588 /* 589 * FIONREAD will return 0 for non-readable descriptors, and 590 * the results of FIONREAD on the read socket for readable 591 * descriptors. 592 */ 593 if (!(fp->f_flag & FREAD)) { 594 *(int *)data = 0; 595 return (0); 596 } 597 filetmp.f_data = fi->fi_readsock; 598 filetmp.f_cred = cred; 599 return (soo_ioctl(&filetmp, com, data, cred, td)); 600 601 default: 602 return (ENOTTY); 603 } 604 } 605 606 /* 607 * Because fifos are now a file descriptor layer object, EVFILT_VNODE is not 608 * implemented. Likely, fifo_kqfilter() should be removed, and 609 * fifo_kqfilter_f() should know how to forward the request to the underling 610 * vnode using f_vnode in the file descriptor here. 611 */ 612 static int 613 fifo_kqfilter_f(struct file *fp, struct knote *kn) 614 { 615 struct fifoinfo *fi; 616 struct socket *so; 617 struct sockbuf *sb; 618 619 fi = fp->f_data; 620 621 /* 622 * If a filter is requested that is not supported by this file 623 * descriptor, don't return an error, but also don't ever generate an 624 * event. 625 */ 626 if ((kn->kn_filter == EVFILT_READ) && !(fp->f_flag & FREAD)) { 627 kn->kn_fop = &fifo_notsup_filtops; 628 return (0); 629 } 630 631 if ((kn->kn_filter == EVFILT_WRITE) && !(fp->f_flag & FWRITE)) { 632 kn->kn_fop = &fifo_notsup_filtops; 633 return (0); 634 } 635 636 switch (kn->kn_filter) { 637 case EVFILT_READ: 638 kn->kn_fop = &fiforead_filtops; 639 so = fi->fi_readsock; 640 sb = &so->so_rcv; 641 break; 642 case EVFILT_WRITE: 643 kn->kn_fop = &fifowrite_filtops; 644 so = fi->fi_writesock; 645 sb = &so->so_snd; 646 break; 647 default: 648 return (EINVAL); 649 } 650 651 kn->kn_hook = (caddr_t)so; 652 653 SOCKBUF_LOCK(sb); 654 knlist_add(&sb->sb_sel.si_note, kn, 1); 655 sb->sb_flags |= SB_KNOTE; 656 SOCKBUF_UNLOCK(sb); 657 658 return (0); 659 } 660 661 static int 662 fifo_poll_f(struct file *fp, int events, struct ucred *cred, struct thread *td) 663 { 664 struct fifoinfo *fip; 665 struct file filetmp; 666 int levents, revents = 0; 667 668 fip = fp->f_data; 669 levents = events & 670 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | POLLRDBAND); 671 if ((fp->f_flag & FREAD) && levents) { 672 /* 673 * If POLLIN or POLLRDNORM is requested and POLLINIGNEOF is 674 * not, then convert the first two to the last one. This 675 * tells the socket poll function to ignore EOF so that we 676 * block if there is no writer (and no data). Callers can 677 * set POLLINIGNEOF to get non-blocking behavior. 678 */ 679 if (levents & (POLLIN | POLLRDNORM) && 680 !(levents & POLLINIGNEOF)) { 681 levents &= ~(POLLIN | POLLRDNORM); 682 levents |= POLLINIGNEOF; 683 } 684 685 filetmp.f_data = fip->fi_readsock; 686 filetmp.f_cred = cred; 687 revents |= soo_poll(&filetmp, levents, cred, td); 688 689 /* Reverse the above conversion. */ 690 if ((revents & POLLINIGNEOF) && !(events & POLLINIGNEOF)) { 691 revents |= (events & (POLLIN | POLLRDNORM)); 692 revents &= ~POLLINIGNEOF; 693 } 694 } 695 levents = events & (POLLOUT | POLLWRNORM | POLLWRBAND); 696 if ((fp->f_flag & FWRITE) && levents) { 697 filetmp.f_data = fip->fi_writesock; 698 filetmp.f_cred = cred; 699 revents |= soo_poll(&filetmp, levents, cred, td); 700 } 701 return (revents); 702 } 703 704 static int 705 fifo_read_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 706 { 707 struct fifoinfo *fip; 708 int error, sflags; 709 710 fip = fp->f_data; 711 KASSERT(uio->uio_rw == UIO_READ,("fifo_read mode")); 712 if (uio->uio_resid == 0) 713 return (0); 714 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0; 715 mtx_lock(&Giant); 716 error = soreceive(fip->fi_readsock, NULL, uio, NULL, NULL, &sflags); 717 mtx_unlock(&Giant); 718 return (error); 719 } 720 721 static int 722 fifo_stat_f(struct file *fp, struct stat *sb, struct ucred *cred, struct thread *td) 723 { 724 725 return (vnops.fo_stat(fp, sb, cred, td)); 726 } 727 728 static int 729 fifo_truncate_f(struct file *fp, off_t length, struct ucred *cred, struct thread *td) 730 { 731 732 return (vnops.fo_truncate(fp, length, cred, td)); 733 } 734 735 static int 736 fifo_write_f(struct file *fp, struct uio *uio, struct ucred *cred, int flags, struct thread *td) 737 { 738 struct fifoinfo *fip; 739 int error, sflags; 740 741 fip = fp->f_data; 742 KASSERT(uio->uio_rw == UIO_WRITE,("fifo_write mode")); 743 sflags = (fp->f_flag & FNONBLOCK) ? MSG_NBIO : 0; 744 mtx_lock(&Giant); 745 error = sosend(fip->fi_writesock, NULL, uio, 0, NULL, sflags, td); 746 mtx_unlock(&Giant); 747 return (error); 748 } 749