1 /* 2 * Copyright (c) 1996 John S. Dyson 3 * 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 immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Absolutely no warranty of function or purpose is made by the author 15 * John S. Dyson. 16 * 4. Modifications may be freely made to this file if the above conditions 17 * are met. 18 * 19 * $FreeBSD$ 20 */ 21 22 /* 23 * This file contains a high-performance replacement for the socket-based 24 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support 25 * all features of sockets, but does do everything that pipes normally 26 * do. 27 */ 28 29 /* 30 * This code has two modes of operation, a small write mode and a large 31 * write mode. The small write mode acts like conventional pipes with 32 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the 33 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT 34 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and 35 * the receiving process can copy it directly from the pages in the sending 36 * process. 37 * 38 * If the sending process receives a signal, it is possible that it will 39 * go away, and certainly its address space can change, because control 40 * is returned back to the user-mode side. In that case, the pipe code 41 * arranges to copy the buffer supplied by the user process, to a pageable 42 * kernel buffer, and the receiving process will grab the data from the 43 * pageable kernel buffer. Since signals don't happen all that often, 44 * the copy operation is normally eliminated. 45 * 46 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will 47 * happen for small transfers so that the system will not spend all of 48 * its time context switching. PIPE_SIZE is constrained by the 49 * amount of kernel virtual memory. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/fcntl.h> 55 #include <sys/file.h> 56 #include <sys/filedesc.h> 57 #include <sys/filio.h> 58 #include <sys/kernel.h> 59 #include <sys/lock.h> 60 #include <sys/mutex.h> 61 #include <sys/ttycom.h> 62 #include <sys/stat.h> 63 #include <sys/malloc.h> 64 #include <sys/poll.h> 65 #include <sys/selinfo.h> 66 #include <sys/signalvar.h> 67 #include <sys/sysproto.h> 68 #include <sys/pipe.h> 69 #include <sys/proc.h> 70 #include <sys/vnode.h> 71 #include <sys/uio.h> 72 #include <sys/event.h> 73 74 #include <vm/vm.h> 75 #include <vm/vm_param.h> 76 #include <vm/vm_object.h> 77 #include <vm/vm_kern.h> 78 #include <vm/vm_extern.h> 79 #include <vm/pmap.h> 80 #include <vm/vm_map.h> 81 #include <vm/vm_page.h> 82 #include <vm/uma.h> 83 84 /* 85 * Use this define if you want to disable *fancy* VM things. Expect an 86 * approx 30% decrease in transfer rate. This could be useful for 87 * NetBSD or OpenBSD. 88 */ 89 /* #define PIPE_NODIRECT */ 90 91 /* 92 * interfaces to the outside world 93 */ 94 static int pipe_read(struct file *fp, struct uio *uio, 95 struct ucred *cred, int flags, struct thread *td); 96 static int pipe_write(struct file *fp, struct uio *uio, 97 struct ucred *cred, int flags, struct thread *td); 98 static int pipe_close(struct file *fp, struct thread *td); 99 static int pipe_poll(struct file *fp, int events, struct ucred *cred, 100 struct thread *td); 101 static int pipe_kqfilter(struct file *fp, struct knote *kn); 102 static int pipe_stat(struct file *fp, struct stat *sb, struct thread *td); 103 static int pipe_ioctl(struct file *fp, u_long cmd, void *data, 104 struct thread *td); 105 106 static struct fileops pipeops = { 107 pipe_read, pipe_write, pipe_ioctl, pipe_poll, pipe_kqfilter, 108 pipe_stat, pipe_close 109 }; 110 111 static void filt_pipedetach(struct knote *kn); 112 static int filt_piperead(struct knote *kn, long hint); 113 static int filt_pipewrite(struct knote *kn, long hint); 114 115 static struct filterops pipe_rfiltops = 116 { 1, NULL, filt_pipedetach, filt_piperead }; 117 static struct filterops pipe_wfiltops = 118 { 1, NULL, filt_pipedetach, filt_pipewrite }; 119 120 #define PIPE_GET_GIANT(pipe) \ 121 do { \ 122 KASSERT(((pipe)->pipe_state & PIPE_LOCKFL) != 0, \ 123 ("%s:%d PIPE_GET_GIANT: line pipe not locked", \ 124 __FILE__, __LINE__)); \ 125 PIPE_UNLOCK(pipe); \ 126 mtx_lock(&Giant); \ 127 } while (0) 128 129 #define PIPE_DROP_GIANT(pipe) \ 130 do { \ 131 mtx_unlock(&Giant); \ 132 PIPE_LOCK(pipe); \ 133 } while (0) 134 135 /* 136 * Default pipe buffer size(s), this can be kind-of large now because pipe 137 * space is pageable. The pipe code will try to maintain locality of 138 * reference for performance reasons, so small amounts of outstanding I/O 139 * will not wipe the cache. 140 */ 141 #define MINPIPESIZE (PIPE_SIZE/3) 142 #define MAXPIPESIZE (2*PIPE_SIZE/3) 143 144 /* 145 * Maximum amount of kva for pipes -- this is kind-of a soft limit, but 146 * is there so that on large systems, we don't exhaust it. 147 */ 148 #define MAXPIPEKVA (8*1024*1024) 149 150 /* 151 * Limit for direct transfers, we cannot, of course limit 152 * the amount of kva for pipes in general though. 153 */ 154 #define LIMITPIPEKVA (16*1024*1024) 155 156 /* 157 * Limit the number of "big" pipes 158 */ 159 #define LIMITBIGPIPES 32 160 static int nbigpipe; 161 162 static int amountpipekva; 163 164 static void pipeinit(void *dummy __unused); 165 static void pipeclose(struct pipe *cpipe); 166 static void pipe_free_kmem(struct pipe *cpipe); 167 static int pipe_create(struct pipe **cpipep); 168 static __inline int pipelock(struct pipe *cpipe, int catch); 169 static __inline void pipeunlock(struct pipe *cpipe); 170 static __inline void pipeselwakeup(struct pipe *cpipe); 171 #ifndef PIPE_NODIRECT 172 static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio); 173 static void pipe_destroy_write_buffer(struct pipe *wpipe); 174 static int pipe_direct_write(struct pipe *wpipe, struct uio *uio); 175 static void pipe_clone_write_buffer(struct pipe *wpipe); 176 #endif 177 static int pipespace(struct pipe *cpipe, int size); 178 179 static uma_zone_t pipe_zone; 180 181 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL); 182 183 static void 184 pipeinit(void *dummy __unused) 185 { 186 pipe_zone = uma_zcreate("PIPE", sizeof(struct pipe), NULL, 187 NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 188 } 189 190 /* 191 * The pipe system call for the DTYPE_PIPE type of pipes 192 */ 193 194 /* ARGSUSED */ 195 int 196 pipe(td, uap) 197 struct thread *td; 198 struct pipe_args /* { 199 int dummy; 200 } */ *uap; 201 { 202 struct filedesc *fdp = td->td_proc->p_fd; 203 struct file *rf, *wf; 204 struct pipe *rpipe, *wpipe; 205 struct mtx *pmtx; 206 int fd, error; 207 208 KASSERT(pipe_zone != NULL, ("pipe_zone not initialized")); 209 210 pmtx = malloc(sizeof(*pmtx), M_TEMP, M_WAITOK | M_ZERO); 211 212 rpipe = wpipe = NULL; 213 if (pipe_create(&rpipe) || pipe_create(&wpipe)) { 214 pipeclose(rpipe); 215 pipeclose(wpipe); 216 free(pmtx, M_TEMP); 217 return (ENFILE); 218 } 219 220 rpipe->pipe_state |= PIPE_DIRECTOK; 221 wpipe->pipe_state |= PIPE_DIRECTOK; 222 223 error = falloc(td, &rf, &fd); 224 if (error) { 225 pipeclose(rpipe); 226 pipeclose(wpipe); 227 free(pmtx, M_TEMP); 228 return (error); 229 } 230 fhold(rf); 231 td->td_retval[0] = fd; 232 233 /* 234 * Warning: once we've gotten past allocation of the fd for the 235 * read-side, we can only drop the read side via fdrop() in order 236 * to avoid races against processes which manage to dup() the read 237 * side while we are blocked trying to allocate the write side. 238 */ 239 FILE_LOCK(rf); 240 rf->f_flag = FREAD | FWRITE; 241 rf->f_type = DTYPE_PIPE; 242 rf->f_data = rpipe; 243 rf->f_ops = &pipeops; 244 FILE_UNLOCK(rf); 245 error = falloc(td, &wf, &fd); 246 if (error) { 247 FILEDESC_LOCK(fdp); 248 if (fdp->fd_ofiles[td->td_retval[0]] == rf) { 249 fdp->fd_ofiles[td->td_retval[0]] = NULL; 250 FILEDESC_UNLOCK(fdp); 251 fdrop(rf, td); 252 } else 253 FILEDESC_UNLOCK(fdp); 254 fdrop(rf, td); 255 /* rpipe has been closed by fdrop(). */ 256 pipeclose(wpipe); 257 free(pmtx, M_TEMP); 258 return (error); 259 } 260 FILE_LOCK(wf); 261 wf->f_flag = FREAD | FWRITE; 262 wf->f_type = DTYPE_PIPE; 263 wf->f_data = wpipe; 264 wf->f_ops = &pipeops; 265 FILE_UNLOCK(wf); 266 td->td_retval[1] = fd; 267 rpipe->pipe_peer = wpipe; 268 wpipe->pipe_peer = rpipe; 269 mtx_init(pmtx, "pipe mutex", NULL, MTX_DEF | MTX_RECURSE); 270 rpipe->pipe_mtxp = wpipe->pipe_mtxp = pmtx; 271 fdrop(rf, td); 272 273 return (0); 274 } 275 276 /* 277 * Allocate kva for pipe circular buffer, the space is pageable 278 * This routine will 'realloc' the size of a pipe safely, if it fails 279 * it will retain the old buffer. 280 * If it fails it will return ENOMEM. 281 */ 282 static int 283 pipespace(cpipe, size) 284 struct pipe *cpipe; 285 int size; 286 { 287 struct vm_object *object; 288 caddr_t buffer; 289 int npages, error; 290 291 GIANT_REQUIRED; 292 KASSERT(cpipe->pipe_mtxp == NULL || !mtx_owned(PIPE_MTX(cpipe)), 293 ("pipespace: pipe mutex locked")); 294 295 npages = round_page(size)/PAGE_SIZE; 296 /* 297 * Create an object, I don't like the idea of paging to/from 298 * kernel_object. 299 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 300 */ 301 object = vm_object_allocate(OBJT_DEFAULT, npages); 302 buffer = (caddr_t) vm_map_min(kernel_map); 303 304 /* 305 * Insert the object into the kernel map, and allocate kva for it. 306 * The map entry is, by default, pageable. 307 * XXX -- minor change needed here for NetBSD/OpenBSD VM systems. 308 */ 309 error = vm_map_find(kernel_map, object, 0, 310 (vm_offset_t *) &buffer, size, 1, 311 VM_PROT_ALL, VM_PROT_ALL, 0); 312 313 if (error != KERN_SUCCESS) { 314 vm_object_deallocate(object); 315 return (ENOMEM); 316 } 317 318 /* free old resources if we're resizing */ 319 pipe_free_kmem(cpipe); 320 cpipe->pipe_buffer.object = object; 321 cpipe->pipe_buffer.buffer = buffer; 322 cpipe->pipe_buffer.size = size; 323 cpipe->pipe_buffer.in = 0; 324 cpipe->pipe_buffer.out = 0; 325 cpipe->pipe_buffer.cnt = 0; 326 amountpipekva += cpipe->pipe_buffer.size; 327 return (0); 328 } 329 330 /* 331 * initialize and allocate VM and memory for pipe 332 */ 333 static int 334 pipe_create(cpipep) 335 struct pipe **cpipep; 336 { 337 struct pipe *cpipe; 338 int error; 339 340 *cpipep = uma_zalloc(pipe_zone, M_WAITOK); 341 if (*cpipep == NULL) 342 return (ENOMEM); 343 344 cpipe = *cpipep; 345 346 /* so pipespace()->pipe_free_kmem() doesn't follow junk pointer */ 347 cpipe->pipe_buffer.object = NULL; 348 #ifndef PIPE_NODIRECT 349 cpipe->pipe_map.kva = NULL; 350 #endif 351 /* 352 * protect so pipeclose() doesn't follow a junk pointer 353 * if pipespace() fails. 354 */ 355 bzero(&cpipe->pipe_sel, sizeof(cpipe->pipe_sel)); 356 cpipe->pipe_state = 0; 357 cpipe->pipe_peer = NULL; 358 cpipe->pipe_busy = 0; 359 360 #ifndef PIPE_NODIRECT 361 /* 362 * pipe data structure initializations to support direct pipe I/O 363 */ 364 cpipe->pipe_map.cnt = 0; 365 cpipe->pipe_map.kva = 0; 366 cpipe->pipe_map.pos = 0; 367 cpipe->pipe_map.npages = 0; 368 /* cpipe->pipe_map.ms[] = invalid */ 369 #endif 370 371 cpipe->pipe_mtxp = NULL; /* avoid pipespace assertion */ 372 error = pipespace(cpipe, PIPE_SIZE); 373 if (error) 374 return (error); 375 376 vfs_timestamp(&cpipe->pipe_ctime); 377 cpipe->pipe_atime = cpipe->pipe_ctime; 378 cpipe->pipe_mtime = cpipe->pipe_ctime; 379 380 return (0); 381 } 382 383 384 /* 385 * lock a pipe for I/O, blocking other access 386 */ 387 static __inline int 388 pipelock(cpipe, catch) 389 struct pipe *cpipe; 390 int catch; 391 { 392 int error; 393 394 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 395 while (cpipe->pipe_state & PIPE_LOCKFL) { 396 cpipe->pipe_state |= PIPE_LWANT; 397 error = msleep(cpipe, PIPE_MTX(cpipe), 398 catch ? (PRIBIO | PCATCH) : PRIBIO, 399 "pipelk", 0); 400 if (error != 0) 401 return (error); 402 } 403 cpipe->pipe_state |= PIPE_LOCKFL; 404 return (0); 405 } 406 407 /* 408 * unlock a pipe I/O lock 409 */ 410 static __inline void 411 pipeunlock(cpipe) 412 struct pipe *cpipe; 413 { 414 415 PIPE_LOCK_ASSERT(cpipe, MA_OWNED); 416 cpipe->pipe_state &= ~PIPE_LOCKFL; 417 if (cpipe->pipe_state & PIPE_LWANT) { 418 cpipe->pipe_state &= ~PIPE_LWANT; 419 wakeup(cpipe); 420 } 421 } 422 423 static __inline void 424 pipeselwakeup(cpipe) 425 struct pipe *cpipe; 426 { 427 428 if (cpipe->pipe_state & PIPE_SEL) { 429 cpipe->pipe_state &= ~PIPE_SEL; 430 selwakeup(&cpipe->pipe_sel); 431 } 432 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_sigio) 433 pgsigio(&cpipe->pipe_sigio, SIGIO, 0); 434 KNOTE(&cpipe->pipe_sel.si_note, 0); 435 } 436 437 /* ARGSUSED */ 438 static int 439 pipe_read(fp, uio, cred, flags, td) 440 struct file *fp; 441 struct uio *uio; 442 struct ucred *cred; 443 struct thread *td; 444 int flags; 445 { 446 struct pipe *rpipe = (struct pipe *) fp->f_data; 447 int error; 448 int nread = 0; 449 u_int size; 450 451 PIPE_LOCK(rpipe); 452 ++rpipe->pipe_busy; 453 error = pipelock(rpipe, 1); 454 if (error) 455 goto unlocked_error; 456 457 while (uio->uio_resid) { 458 /* 459 * normal pipe buffer receive 460 */ 461 if (rpipe->pipe_buffer.cnt > 0) { 462 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 463 if (size > rpipe->pipe_buffer.cnt) 464 size = rpipe->pipe_buffer.cnt; 465 if (size > (u_int) uio->uio_resid) 466 size = (u_int) uio->uio_resid; 467 468 PIPE_UNLOCK(rpipe); 469 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 470 size, uio); 471 PIPE_LOCK(rpipe); 472 if (error) 473 break; 474 475 rpipe->pipe_buffer.out += size; 476 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 477 rpipe->pipe_buffer.out = 0; 478 479 rpipe->pipe_buffer.cnt -= size; 480 481 /* 482 * If there is no more to read in the pipe, reset 483 * its pointers to the beginning. This improves 484 * cache hit stats. 485 */ 486 if (rpipe->pipe_buffer.cnt == 0) { 487 rpipe->pipe_buffer.in = 0; 488 rpipe->pipe_buffer.out = 0; 489 } 490 nread += size; 491 #ifndef PIPE_NODIRECT 492 /* 493 * Direct copy, bypassing a kernel buffer. 494 */ 495 } else if ((size = rpipe->pipe_map.cnt) && 496 (rpipe->pipe_state & PIPE_DIRECTW)) { 497 caddr_t va; 498 if (size > (u_int) uio->uio_resid) 499 size = (u_int) uio->uio_resid; 500 501 va = (caddr_t) rpipe->pipe_map.kva + 502 rpipe->pipe_map.pos; 503 PIPE_UNLOCK(rpipe); 504 error = uiomove(va, size, uio); 505 PIPE_LOCK(rpipe); 506 if (error) 507 break; 508 nread += size; 509 rpipe->pipe_map.pos += size; 510 rpipe->pipe_map.cnt -= size; 511 if (rpipe->pipe_map.cnt == 0) { 512 rpipe->pipe_state &= ~PIPE_DIRECTW; 513 wakeup(rpipe); 514 } 515 #endif 516 } else { 517 /* 518 * detect EOF condition 519 * read returns 0 on EOF, no need to set error 520 */ 521 if (rpipe->pipe_state & PIPE_EOF) 522 break; 523 524 /* 525 * If the "write-side" has been blocked, wake it up now. 526 */ 527 if (rpipe->pipe_state & PIPE_WANTW) { 528 rpipe->pipe_state &= ~PIPE_WANTW; 529 wakeup(rpipe); 530 } 531 532 /* 533 * Break if some data was read. 534 */ 535 if (nread > 0) 536 break; 537 538 /* 539 * Unlock the pipe buffer for our remaining processing. We 540 * will either break out with an error or we will sleep and 541 * relock to loop. 542 */ 543 pipeunlock(rpipe); 544 545 /* 546 * Handle non-blocking mode operation or 547 * wait for more data. 548 */ 549 if (fp->f_flag & FNONBLOCK) { 550 error = EAGAIN; 551 } else { 552 rpipe->pipe_state |= PIPE_WANTR; 553 if ((error = msleep(rpipe, PIPE_MTX(rpipe), 554 PRIBIO | PCATCH, 555 "piperd", 0)) == 0) 556 error = pipelock(rpipe, 1); 557 } 558 if (error) 559 goto unlocked_error; 560 } 561 } 562 pipeunlock(rpipe); 563 564 /* XXX: should probably do this before getting any locks. */ 565 if (error == 0) 566 vfs_timestamp(&rpipe->pipe_atime); 567 unlocked_error: 568 --rpipe->pipe_busy; 569 570 /* 571 * PIPE_WANT processing only makes sense if pipe_busy is 0. 572 */ 573 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 574 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 575 wakeup(rpipe); 576 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 577 /* 578 * Handle write blocking hysteresis. 579 */ 580 if (rpipe->pipe_state & PIPE_WANTW) { 581 rpipe->pipe_state &= ~PIPE_WANTW; 582 wakeup(rpipe); 583 } 584 } 585 586 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 587 pipeselwakeup(rpipe); 588 589 PIPE_UNLOCK(rpipe); 590 return (error); 591 } 592 593 #ifndef PIPE_NODIRECT 594 /* 595 * Map the sending processes' buffer into kernel space and wire it. 596 * This is similar to a physical write operation. 597 */ 598 static int 599 pipe_build_write_buffer(wpipe, uio) 600 struct pipe *wpipe; 601 struct uio *uio; 602 { 603 u_int size; 604 int i; 605 vm_offset_t addr, endaddr, paddr; 606 607 GIANT_REQUIRED; 608 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED); 609 610 size = (u_int) uio->uio_iov->iov_len; 611 if (size > wpipe->pipe_buffer.size) 612 size = wpipe->pipe_buffer.size; 613 614 endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); 615 addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); 616 for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) { 617 vm_page_t m; 618 619 /* 620 * vm_fault_quick() can sleep. Consequently, 621 * vm_page_lock_queue() and vm_page_unlock_queue() 622 * should not be performed outside of this loop. 623 */ 624 if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0 || 625 (paddr = pmap_extract(vmspace_pmap(curproc->p_vmspace), 626 addr)) == 0) { 627 int j; 628 629 vm_page_lock_queues(); 630 for (j = 0; j < i; j++) 631 vm_page_unwire(wpipe->pipe_map.ms[j], 1); 632 vm_page_unlock_queues(); 633 return (EFAULT); 634 } 635 636 m = PHYS_TO_VM_PAGE(paddr); 637 vm_page_lock_queues(); 638 vm_page_wire(m); 639 vm_page_unlock_queues(); 640 wpipe->pipe_map.ms[i] = m; 641 } 642 643 /* 644 * set up the control block 645 */ 646 wpipe->pipe_map.npages = i; 647 wpipe->pipe_map.pos = 648 ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; 649 wpipe->pipe_map.cnt = size; 650 651 /* 652 * and map the buffer 653 */ 654 if (wpipe->pipe_map.kva == 0) { 655 /* 656 * We need to allocate space for an extra page because the 657 * address range might (will) span pages at times. 658 */ 659 wpipe->pipe_map.kva = kmem_alloc_pageable(kernel_map, 660 wpipe->pipe_buffer.size + PAGE_SIZE); 661 amountpipekva += wpipe->pipe_buffer.size + PAGE_SIZE; 662 } 663 pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, 664 wpipe->pipe_map.npages); 665 666 /* 667 * and update the uio data 668 */ 669 670 uio->uio_iov->iov_len -= size; 671 uio->uio_iov->iov_base += size; 672 if (uio->uio_iov->iov_len == 0) 673 uio->uio_iov++; 674 uio->uio_resid -= size; 675 uio->uio_offset += size; 676 return (0); 677 } 678 679 /* 680 * unmap and unwire the process buffer 681 */ 682 static void 683 pipe_destroy_write_buffer(wpipe) 684 struct pipe *wpipe; 685 { 686 int i; 687 688 GIANT_REQUIRED; 689 PIPE_LOCK_ASSERT(wpipe, MA_NOTOWNED); 690 691 if (wpipe->pipe_map.kva) { 692 pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); 693 694 if (amountpipekva > MAXPIPEKVA) { 695 vm_offset_t kva = wpipe->pipe_map.kva; 696 wpipe->pipe_map.kva = 0; 697 kmem_free(kernel_map, kva, 698 wpipe->pipe_buffer.size + PAGE_SIZE); 699 amountpipekva -= wpipe->pipe_buffer.size + PAGE_SIZE; 700 } 701 } 702 vm_page_lock_queues(); 703 for (i = 0; i < wpipe->pipe_map.npages; i++) 704 vm_page_unwire(wpipe->pipe_map.ms[i], 1); 705 vm_page_unlock_queues(); 706 wpipe->pipe_map.npages = 0; 707 } 708 709 /* 710 * In the case of a signal, the writing process might go away. This 711 * code copies the data into the circular buffer so that the source 712 * pages can be freed without loss of data. 713 */ 714 static void 715 pipe_clone_write_buffer(wpipe) 716 struct pipe *wpipe; 717 { 718 int size; 719 int pos; 720 721 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 722 size = wpipe->pipe_map.cnt; 723 pos = wpipe->pipe_map.pos; 724 725 wpipe->pipe_buffer.in = size; 726 wpipe->pipe_buffer.out = 0; 727 wpipe->pipe_buffer.cnt = size; 728 wpipe->pipe_state &= ~PIPE_DIRECTW; 729 730 PIPE_GET_GIANT(wpipe); 731 bcopy((caddr_t) wpipe->pipe_map.kva + pos, 732 wpipe->pipe_buffer.buffer, size); 733 pipe_destroy_write_buffer(wpipe); 734 PIPE_DROP_GIANT(wpipe); 735 } 736 737 /* 738 * This implements the pipe buffer write mechanism. Note that only 739 * a direct write OR a normal pipe write can be pending at any given time. 740 * If there are any characters in the pipe buffer, the direct write will 741 * be deferred until the receiving process grabs all of the bytes from 742 * the pipe buffer. Then the direct mapping write is set-up. 743 */ 744 static int 745 pipe_direct_write(wpipe, uio) 746 struct pipe *wpipe; 747 struct uio *uio; 748 { 749 int error; 750 751 retry: 752 PIPE_LOCK_ASSERT(wpipe, MA_OWNED); 753 while (wpipe->pipe_state & PIPE_DIRECTW) { 754 if (wpipe->pipe_state & PIPE_WANTR) { 755 wpipe->pipe_state &= ~PIPE_WANTR; 756 wakeup(wpipe); 757 } 758 wpipe->pipe_state |= PIPE_WANTW; 759 error = msleep(wpipe, PIPE_MTX(wpipe), 760 PRIBIO | PCATCH, "pipdww", 0); 761 if (error) 762 goto error1; 763 if (wpipe->pipe_state & PIPE_EOF) { 764 error = EPIPE; 765 goto error1; 766 } 767 } 768 wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ 769 if (wpipe->pipe_buffer.cnt > 0) { 770 if (wpipe->pipe_state & PIPE_WANTR) { 771 wpipe->pipe_state &= ~PIPE_WANTR; 772 wakeup(wpipe); 773 } 774 775 wpipe->pipe_state |= PIPE_WANTW; 776 error = msleep(wpipe, PIPE_MTX(wpipe), 777 PRIBIO | PCATCH, "pipdwc", 0); 778 if (error) 779 goto error1; 780 if (wpipe->pipe_state & PIPE_EOF) { 781 error = EPIPE; 782 goto error1; 783 } 784 goto retry; 785 } 786 787 wpipe->pipe_state |= PIPE_DIRECTW; 788 789 pipelock(wpipe, 0); 790 PIPE_GET_GIANT(wpipe); 791 error = pipe_build_write_buffer(wpipe, uio); 792 PIPE_DROP_GIANT(wpipe); 793 pipeunlock(wpipe); 794 if (error) { 795 wpipe->pipe_state &= ~PIPE_DIRECTW; 796 goto error1; 797 } 798 799 error = 0; 800 while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { 801 if (wpipe->pipe_state & PIPE_EOF) { 802 pipelock(wpipe, 0); 803 PIPE_GET_GIANT(wpipe); 804 pipe_destroy_write_buffer(wpipe); 805 PIPE_DROP_GIANT(wpipe); 806 pipeunlock(wpipe); 807 pipeselwakeup(wpipe); 808 error = EPIPE; 809 goto error1; 810 } 811 if (wpipe->pipe_state & PIPE_WANTR) { 812 wpipe->pipe_state &= ~PIPE_WANTR; 813 wakeup(wpipe); 814 } 815 pipeselwakeup(wpipe); 816 error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, 817 "pipdwt", 0); 818 } 819 820 pipelock(wpipe,0); 821 if (wpipe->pipe_state & PIPE_DIRECTW) { 822 /* 823 * this bit of trickery substitutes a kernel buffer for 824 * the process that might be going away. 825 */ 826 pipe_clone_write_buffer(wpipe); 827 } else { 828 PIPE_GET_GIANT(wpipe); 829 pipe_destroy_write_buffer(wpipe); 830 PIPE_DROP_GIANT(wpipe); 831 } 832 pipeunlock(wpipe); 833 return (error); 834 835 error1: 836 wakeup(wpipe); 837 return (error); 838 } 839 #endif 840 841 static int 842 pipe_write(fp, uio, cred, flags, td) 843 struct file *fp; 844 struct uio *uio; 845 struct ucred *cred; 846 struct thread *td; 847 int flags; 848 { 849 int error = 0; 850 int orig_resid; 851 struct pipe *wpipe, *rpipe; 852 853 rpipe = (struct pipe *) fp->f_data; 854 wpipe = rpipe->pipe_peer; 855 856 PIPE_LOCK(rpipe); 857 /* 858 * detect loss of pipe read side, issue SIGPIPE if lost. 859 */ 860 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 861 PIPE_UNLOCK(rpipe); 862 return (EPIPE); 863 } 864 ++wpipe->pipe_busy; 865 866 /* 867 * If it is advantageous to resize the pipe buffer, do 868 * so. 869 */ 870 if ((uio->uio_resid > PIPE_SIZE) && 871 (nbigpipe < LIMITBIGPIPES) && 872 (wpipe->pipe_state & PIPE_DIRECTW) == 0 && 873 (wpipe->pipe_buffer.size <= PIPE_SIZE) && 874 (wpipe->pipe_buffer.cnt == 0)) { 875 876 if ((error = pipelock(wpipe,1)) == 0) { 877 PIPE_GET_GIANT(wpipe); 878 if (pipespace(wpipe, BIG_PIPE_SIZE) == 0) 879 nbigpipe++; 880 PIPE_DROP_GIANT(wpipe); 881 pipeunlock(wpipe); 882 } 883 } 884 885 /* 886 * If an early error occured unbusy and return, waking up any pending 887 * readers. 888 */ 889 if (error) { 890 --wpipe->pipe_busy; 891 if ((wpipe->pipe_busy == 0) && 892 (wpipe->pipe_state & PIPE_WANT)) { 893 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 894 wakeup(wpipe); 895 } 896 PIPE_UNLOCK(rpipe); 897 return(error); 898 } 899 900 KASSERT(wpipe->pipe_buffer.buffer != NULL, ("pipe buffer gone")); 901 902 orig_resid = uio->uio_resid; 903 904 while (uio->uio_resid) { 905 int space; 906 907 #ifndef PIPE_NODIRECT 908 /* 909 * If the transfer is large, we can gain performance if 910 * we do process-to-process copies directly. 911 * If the write is non-blocking, we don't use the 912 * direct write mechanism. 913 * 914 * The direct write mechanism will detect the reader going 915 * away on us. 916 */ 917 if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && 918 (fp->f_flag & FNONBLOCK) == 0 && 919 (wpipe->pipe_map.kva || (amountpipekva < LIMITPIPEKVA)) && 920 (uio->uio_iov->iov_len >= PIPE_MINDIRECT)) { 921 error = pipe_direct_write( wpipe, uio); 922 if (error) 923 break; 924 continue; 925 } 926 #endif 927 928 /* 929 * Pipe buffered writes cannot be coincidental with 930 * direct writes. We wait until the currently executing 931 * direct write is completed before we start filling the 932 * pipe buffer. We break out if a signal occurs or the 933 * reader goes away. 934 */ 935 retrywrite: 936 while (wpipe->pipe_state & PIPE_DIRECTW) { 937 if (wpipe->pipe_state & PIPE_WANTR) { 938 wpipe->pipe_state &= ~PIPE_WANTR; 939 wakeup(wpipe); 940 } 941 error = msleep(wpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH, 942 "pipbww", 0); 943 if (wpipe->pipe_state & PIPE_EOF) 944 break; 945 if (error) 946 break; 947 } 948 if (wpipe->pipe_state & PIPE_EOF) { 949 error = EPIPE; 950 break; 951 } 952 953 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 954 955 /* Writes of size <= PIPE_BUF must be atomic. */ 956 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 957 space = 0; 958 959 if (space > 0 && (wpipe->pipe_buffer.cnt < PIPE_SIZE)) { 960 if ((error = pipelock(wpipe,1)) == 0) { 961 int size; /* Transfer size */ 962 int segsize; /* first segment to transfer */ 963 964 /* 965 * It is possible for a direct write to 966 * slip in on us... handle it here... 967 */ 968 if (wpipe->pipe_state & PIPE_DIRECTW) { 969 pipeunlock(wpipe); 970 goto retrywrite; 971 } 972 /* 973 * If a process blocked in uiomove, our 974 * value for space might be bad. 975 * 976 * XXX will we be ok if the reader has gone 977 * away here? 978 */ 979 if (space > wpipe->pipe_buffer.size - 980 wpipe->pipe_buffer.cnt) { 981 pipeunlock(wpipe); 982 goto retrywrite; 983 } 984 985 /* 986 * Transfer size is minimum of uio transfer 987 * and free space in pipe buffer. 988 */ 989 if (space > uio->uio_resid) 990 size = uio->uio_resid; 991 else 992 size = space; 993 /* 994 * First segment to transfer is minimum of 995 * transfer size and contiguous space in 996 * pipe buffer. If first segment to transfer 997 * is less than the transfer size, we've got 998 * a wraparound in the buffer. 999 */ 1000 segsize = wpipe->pipe_buffer.size - 1001 wpipe->pipe_buffer.in; 1002 if (segsize > size) 1003 segsize = size; 1004 1005 /* Transfer first segment */ 1006 1007 PIPE_UNLOCK(rpipe); 1008 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 1009 segsize, uio); 1010 PIPE_LOCK(rpipe); 1011 1012 if (error == 0 && segsize < size) { 1013 /* 1014 * Transfer remaining part now, to 1015 * support atomic writes. Wraparound 1016 * happened. 1017 */ 1018 if (wpipe->pipe_buffer.in + segsize != 1019 wpipe->pipe_buffer.size) 1020 panic("Expected pipe buffer wraparound disappeared"); 1021 1022 PIPE_UNLOCK(rpipe); 1023 error = uiomove(&wpipe->pipe_buffer.buffer[0], 1024 size - segsize, uio); 1025 PIPE_LOCK(rpipe); 1026 } 1027 if (error == 0) { 1028 wpipe->pipe_buffer.in += size; 1029 if (wpipe->pipe_buffer.in >= 1030 wpipe->pipe_buffer.size) { 1031 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size) 1032 panic("Expected wraparound bad"); 1033 wpipe->pipe_buffer.in = size - segsize; 1034 } 1035 1036 wpipe->pipe_buffer.cnt += size; 1037 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size) 1038 panic("Pipe buffer overflow"); 1039 1040 } 1041 pipeunlock(wpipe); 1042 } 1043 if (error) 1044 break; 1045 1046 } else { 1047 /* 1048 * If the "read-side" has been blocked, wake it up now. 1049 */ 1050 if (wpipe->pipe_state & PIPE_WANTR) { 1051 wpipe->pipe_state &= ~PIPE_WANTR; 1052 wakeup(wpipe); 1053 } 1054 1055 /* 1056 * don't block on non-blocking I/O 1057 */ 1058 if (fp->f_flag & FNONBLOCK) { 1059 error = EAGAIN; 1060 break; 1061 } 1062 1063 /* 1064 * We have no more space and have something to offer, 1065 * wake up select/poll. 1066 */ 1067 pipeselwakeup(wpipe); 1068 1069 wpipe->pipe_state |= PIPE_WANTW; 1070 error = msleep(wpipe, PIPE_MTX(rpipe), 1071 PRIBIO | PCATCH, "pipewr", 0); 1072 if (error != 0) 1073 break; 1074 /* 1075 * If read side wants to go away, we just issue a signal 1076 * to ourselves. 1077 */ 1078 if (wpipe->pipe_state & PIPE_EOF) { 1079 error = EPIPE; 1080 break; 1081 } 1082 } 1083 } 1084 1085 --wpipe->pipe_busy; 1086 1087 if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { 1088 wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); 1089 wakeup(wpipe); 1090 } else if (wpipe->pipe_buffer.cnt > 0) { 1091 /* 1092 * If we have put any characters in the buffer, we wake up 1093 * the reader. 1094 */ 1095 if (wpipe->pipe_state & PIPE_WANTR) { 1096 wpipe->pipe_state &= ~PIPE_WANTR; 1097 wakeup(wpipe); 1098 } 1099 } 1100 1101 /* 1102 * Don't return EPIPE if I/O was successful 1103 */ 1104 if ((wpipe->pipe_buffer.cnt == 0) && 1105 (uio->uio_resid == 0) && 1106 (error == EPIPE)) { 1107 error = 0; 1108 } 1109 1110 if (error == 0) 1111 vfs_timestamp(&wpipe->pipe_mtime); 1112 1113 /* 1114 * We have something to offer, 1115 * wake up select/poll. 1116 */ 1117 if (wpipe->pipe_buffer.cnt) 1118 pipeselwakeup(wpipe); 1119 1120 PIPE_UNLOCK(rpipe); 1121 return (error); 1122 } 1123 1124 /* 1125 * we implement a very minimal set of ioctls for compatibility with sockets. 1126 */ 1127 int 1128 pipe_ioctl(fp, cmd, data, td) 1129 struct file *fp; 1130 u_long cmd; 1131 void *data; 1132 struct thread *td; 1133 { 1134 struct pipe *mpipe = (struct pipe *)fp->f_data; 1135 1136 switch (cmd) { 1137 1138 case FIONBIO: 1139 return (0); 1140 1141 case FIOASYNC: 1142 PIPE_LOCK(mpipe); 1143 if (*(int *)data) { 1144 mpipe->pipe_state |= PIPE_ASYNC; 1145 } else { 1146 mpipe->pipe_state &= ~PIPE_ASYNC; 1147 } 1148 PIPE_UNLOCK(mpipe); 1149 return (0); 1150 1151 case FIONREAD: 1152 PIPE_LOCK(mpipe); 1153 if (mpipe->pipe_state & PIPE_DIRECTW) 1154 *(int *)data = mpipe->pipe_map.cnt; 1155 else 1156 *(int *)data = mpipe->pipe_buffer.cnt; 1157 PIPE_UNLOCK(mpipe); 1158 return (0); 1159 1160 case FIOSETOWN: 1161 return (fsetown(*(int *)data, &mpipe->pipe_sigio)); 1162 1163 case FIOGETOWN: 1164 *(int *)data = fgetown(mpipe->pipe_sigio); 1165 return (0); 1166 1167 /* This is deprecated, FIOSETOWN should be used instead. */ 1168 case TIOCSPGRP: 1169 return (fsetown(-(*(int *)data), &mpipe->pipe_sigio)); 1170 1171 /* This is deprecated, FIOGETOWN should be used instead. */ 1172 case TIOCGPGRP: 1173 *(int *)data = -fgetown(mpipe->pipe_sigio); 1174 return (0); 1175 1176 } 1177 return (ENOTTY); 1178 } 1179 1180 int 1181 pipe_poll(fp, events, cred, td) 1182 struct file *fp; 1183 int events; 1184 struct ucred *cred; 1185 struct thread *td; 1186 { 1187 struct pipe *rpipe = (struct pipe *)fp->f_data; 1188 struct pipe *wpipe; 1189 int revents = 0; 1190 1191 wpipe = rpipe->pipe_peer; 1192 PIPE_LOCK(rpipe); 1193 if (events & (POLLIN | POLLRDNORM)) 1194 if ((rpipe->pipe_state & PIPE_DIRECTW) || 1195 (rpipe->pipe_buffer.cnt > 0) || 1196 (rpipe->pipe_state & PIPE_EOF)) 1197 revents |= events & (POLLIN | POLLRDNORM); 1198 1199 if (events & (POLLOUT | POLLWRNORM)) 1200 if (wpipe == NULL || (wpipe->pipe_state & PIPE_EOF) || 1201 (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && 1202 (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) 1203 revents |= events & (POLLOUT | POLLWRNORM); 1204 1205 if ((rpipe->pipe_state & PIPE_EOF) || 1206 (wpipe == NULL) || 1207 (wpipe->pipe_state & PIPE_EOF)) 1208 revents |= POLLHUP; 1209 1210 if (revents == 0) { 1211 if (events & (POLLIN | POLLRDNORM)) { 1212 selrecord(td, &rpipe->pipe_sel); 1213 rpipe->pipe_state |= PIPE_SEL; 1214 } 1215 1216 if (events & (POLLOUT | POLLWRNORM)) { 1217 selrecord(td, &wpipe->pipe_sel); 1218 wpipe->pipe_state |= PIPE_SEL; 1219 } 1220 } 1221 PIPE_UNLOCK(rpipe); 1222 1223 return (revents); 1224 } 1225 1226 /* 1227 * We shouldn't need locks here as we're doing a read and this should 1228 * be a natural race. 1229 */ 1230 static int 1231 pipe_stat(fp, ub, td) 1232 struct file *fp; 1233 struct stat *ub; 1234 struct thread *td; 1235 { 1236 struct pipe *pipe = (struct pipe *)fp->f_data; 1237 1238 bzero(ub, sizeof(*ub)); 1239 ub->st_mode = S_IFIFO; 1240 ub->st_blksize = pipe->pipe_buffer.size; 1241 ub->st_size = pipe->pipe_buffer.cnt; 1242 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 1243 ub->st_atimespec = pipe->pipe_atime; 1244 ub->st_mtimespec = pipe->pipe_mtime; 1245 ub->st_ctimespec = pipe->pipe_ctime; 1246 ub->st_uid = fp->f_cred->cr_uid; 1247 ub->st_gid = fp->f_cred->cr_gid; 1248 /* 1249 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen. 1250 * XXX (st_dev, st_ino) should be unique. 1251 */ 1252 return (0); 1253 } 1254 1255 /* ARGSUSED */ 1256 static int 1257 pipe_close(fp, td) 1258 struct file *fp; 1259 struct thread *td; 1260 { 1261 struct pipe *cpipe = (struct pipe *)fp->f_data; 1262 1263 fp->f_ops = &badfileops; 1264 fp->f_data = NULL; 1265 funsetown(&cpipe->pipe_sigio); 1266 pipeclose(cpipe); 1267 return (0); 1268 } 1269 1270 static void 1271 pipe_free_kmem(cpipe) 1272 struct pipe *cpipe; 1273 { 1274 1275 GIANT_REQUIRED; 1276 KASSERT(cpipe->pipe_mtxp == NULL || !mtx_owned(PIPE_MTX(cpipe)), 1277 ("pipespace: pipe mutex locked")); 1278 1279 if (cpipe->pipe_buffer.buffer != NULL) { 1280 if (cpipe->pipe_buffer.size > PIPE_SIZE) 1281 --nbigpipe; 1282 amountpipekva -= cpipe->pipe_buffer.size; 1283 kmem_free(kernel_map, 1284 (vm_offset_t)cpipe->pipe_buffer.buffer, 1285 cpipe->pipe_buffer.size); 1286 cpipe->pipe_buffer.buffer = NULL; 1287 } 1288 #ifndef PIPE_NODIRECT 1289 if (cpipe->pipe_map.kva != NULL) { 1290 amountpipekva -= cpipe->pipe_buffer.size + PAGE_SIZE; 1291 kmem_free(kernel_map, 1292 cpipe->pipe_map.kva, 1293 cpipe->pipe_buffer.size + PAGE_SIZE); 1294 cpipe->pipe_map.cnt = 0; 1295 cpipe->pipe_map.kva = 0; 1296 cpipe->pipe_map.pos = 0; 1297 cpipe->pipe_map.npages = 0; 1298 } 1299 #endif 1300 } 1301 1302 /* 1303 * shutdown the pipe 1304 */ 1305 static void 1306 pipeclose(cpipe) 1307 struct pipe *cpipe; 1308 { 1309 struct pipe *ppipe; 1310 int hadpeer; 1311 1312 if (cpipe == NULL) 1313 return; 1314 1315 hadpeer = 0; 1316 1317 /* partially created pipes won't have a valid mutex. */ 1318 if (PIPE_MTX(cpipe) != NULL) 1319 PIPE_LOCK(cpipe); 1320 1321 pipeselwakeup(cpipe); 1322 1323 /* 1324 * If the other side is blocked, wake it up saying that 1325 * we want to close it down. 1326 */ 1327 while (cpipe->pipe_busy) { 1328 wakeup(cpipe); 1329 cpipe->pipe_state |= PIPE_WANT | PIPE_EOF; 1330 msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0); 1331 } 1332 1333 /* 1334 * Disconnect from peer 1335 */ 1336 if ((ppipe = cpipe->pipe_peer) != NULL) { 1337 hadpeer++; 1338 pipeselwakeup(ppipe); 1339 1340 ppipe->pipe_state |= PIPE_EOF; 1341 wakeup(ppipe); 1342 KNOTE(&ppipe->pipe_sel.si_note, 0); 1343 ppipe->pipe_peer = NULL; 1344 } 1345 /* 1346 * free resources 1347 */ 1348 if (PIPE_MTX(cpipe) != NULL) { 1349 PIPE_UNLOCK(cpipe); 1350 if (!hadpeer) { 1351 mtx_destroy(PIPE_MTX(cpipe)); 1352 free(PIPE_MTX(cpipe), M_TEMP); 1353 } 1354 } 1355 mtx_lock(&Giant); 1356 pipe_free_kmem(cpipe); 1357 uma_zfree(pipe_zone, cpipe); 1358 mtx_unlock(&Giant); 1359 } 1360 1361 /*ARGSUSED*/ 1362 static int 1363 pipe_kqfilter(struct file *fp, struct knote *kn) 1364 { 1365 struct pipe *cpipe; 1366 1367 cpipe = (struct pipe *)kn->kn_fp->f_data; 1368 switch (kn->kn_filter) { 1369 case EVFILT_READ: 1370 kn->kn_fop = &pipe_rfiltops; 1371 break; 1372 case EVFILT_WRITE: 1373 kn->kn_fop = &pipe_wfiltops; 1374 cpipe = cpipe->pipe_peer; 1375 break; 1376 default: 1377 return (1); 1378 } 1379 kn->kn_hook = cpipe; 1380 1381 PIPE_LOCK(cpipe); 1382 SLIST_INSERT_HEAD(&cpipe->pipe_sel.si_note, kn, kn_selnext); 1383 PIPE_UNLOCK(cpipe); 1384 return (0); 1385 } 1386 1387 static void 1388 filt_pipedetach(struct knote *kn) 1389 { 1390 struct pipe *cpipe = (struct pipe *)kn->kn_hook; 1391 1392 PIPE_LOCK(cpipe); 1393 SLIST_REMOVE(&cpipe->pipe_sel.si_note, kn, knote, kn_selnext); 1394 PIPE_UNLOCK(cpipe); 1395 } 1396 1397 /*ARGSUSED*/ 1398 static int 1399 filt_piperead(struct knote *kn, long hint) 1400 { 1401 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1402 struct pipe *wpipe = rpipe->pipe_peer; 1403 1404 PIPE_LOCK(rpipe); 1405 kn->kn_data = rpipe->pipe_buffer.cnt; 1406 if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) 1407 kn->kn_data = rpipe->pipe_map.cnt; 1408 1409 if ((rpipe->pipe_state & PIPE_EOF) || 1410 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1411 kn->kn_flags |= EV_EOF; 1412 PIPE_UNLOCK(rpipe); 1413 return (1); 1414 } 1415 PIPE_UNLOCK(rpipe); 1416 return (kn->kn_data > 0); 1417 } 1418 1419 /*ARGSUSED*/ 1420 static int 1421 filt_pipewrite(struct knote *kn, long hint) 1422 { 1423 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 1424 struct pipe *wpipe = rpipe->pipe_peer; 1425 1426 PIPE_LOCK(rpipe); 1427 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 1428 kn->kn_data = 0; 1429 kn->kn_flags |= EV_EOF; 1430 PIPE_UNLOCK(rpipe); 1431 return (1); 1432 } 1433 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 1434 if (wpipe->pipe_state & PIPE_DIRECTW) 1435 kn->kn_data = 0; 1436 1437 PIPE_UNLOCK(rpipe); 1438 return (kn->kn_data >= PIPE_BUF); 1439 } 1440