1 /* $OpenBSD: sys_pipe.c,v 1.43 2002/03/14 01:27:04 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1996 John S. Dyson 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 immediately at the beginning of the file, without modification, 12 * 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. Absolutely no warranty of function or purpose is made by the author 17 * John S. Dyson. 18 * 4. Modifications may be freely made to this file if the above conditions 19 * are met. 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 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/proc.h> 32 #include <sys/file.h> 33 #include <sys/protosw.h> 34 #include <sys/stat.h> 35 #include <sys/filedesc.h> 36 #include <sys/malloc.h> 37 #include <sys/pool.h> 38 #include <sys/ioctl.h> 39 #include <sys/stat.h> 40 #include <sys/select.h> 41 #include <sys/signalvar.h> 42 #include <sys/errno.h> 43 #include <sys/queue.h> 44 #include <sys/kernel.h> 45 #include <sys/mount.h> 46 #include <sys/syscallargs.h> 47 #include <sys/event.h> 48 #include <sys/lock.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #include <sys/pipe.h> 53 54 /* 55 * interfaces to the outside world 56 */ 57 int pipe_read(struct file *, off_t *, struct uio *, struct ucred *); 58 int pipe_write(struct file *, off_t *, struct uio *, struct ucred *); 59 int pipe_close(struct file *, struct proc *); 60 int pipe_select(struct file *, int which, struct proc *); 61 int pipe_kqfilter(struct file *fp, struct knote *kn); 62 int pipe_ioctl(struct file *, u_long, caddr_t, struct proc *); 63 int pipe_stat(struct file *fp, struct stat *ub, struct proc *p); 64 65 static struct fileops pipeops = { 66 pipe_read, pipe_write, pipe_ioctl, pipe_select, pipe_kqfilter, 67 pipe_stat, pipe_close 68 }; 69 70 void filt_pipedetach(struct knote *kn); 71 int filt_piperead(struct knote *kn, long hint); 72 int filt_pipewrite(struct knote *kn, long hint); 73 74 struct filterops pipe_rfiltops = 75 { 1, NULL, filt_pipedetach, filt_piperead }; 76 struct filterops pipe_wfiltops = 77 { 1, NULL, filt_pipedetach, filt_pipewrite }; 78 79 /* 80 * Default pipe buffer size(s), this can be kind-of large now because pipe 81 * space is pageable. The pipe code will try to maintain locality of 82 * reference for performance reasons, so small amounts of outstanding I/O 83 * will not wipe the cache. 84 */ 85 #define MINPIPESIZE (PIPE_SIZE/3) 86 87 /* 88 * Limit the number of "big" pipes 89 */ 90 #define LIMITBIGPIPES 32 91 int nbigpipe; 92 static int amountpipekva; 93 94 struct pool pipe_pool; 95 96 void pipeclose(struct pipe *); 97 void pipeinit(struct pipe *); 98 static __inline int pipelock(struct pipe *); 99 static __inline void pipeunlock(struct pipe *); 100 static __inline void pipeselwakeup(struct pipe *); 101 void pipespace(struct pipe *); 102 103 /* 104 * The pipe system call for the DTYPE_PIPE type of pipes 105 */ 106 107 /* ARGSUSED */ 108 int 109 sys_opipe(p, v, retval) 110 struct proc *p; 111 void *v; 112 register_t *retval; 113 { 114 struct filedesc *fdp = p->p_fd; 115 struct file *rf, *wf; 116 struct pipe *rpipe, *wpipe; 117 int fd, error; 118 119 rpipe = pool_get(&pipe_pool, PR_WAITOK); 120 pipeinit(rpipe); 121 wpipe = pool_get(&pipe_pool, PR_WAITOK); 122 pipeinit(wpipe); 123 124 error = falloc(p, &rf, &fd); 125 if (error) 126 goto free2; 127 rf->f_flag = FREAD | FWRITE; 128 rf->f_type = DTYPE_PIPE; 129 rf->f_ops = &pipeops; 130 rf->f_data = (caddr_t)rpipe; 131 retval[0] = fd; 132 133 error = falloc(p, &wf, &fd); 134 if (error) 135 goto free3; 136 wf->f_flag = FREAD | FWRITE; 137 wf->f_type = DTYPE_PIPE; 138 wf->f_ops = &pipeops; 139 wf->f_data = (caddr_t)wpipe; 140 retval[1] = fd; 141 142 rpipe->pipe_peer = wpipe; 143 wpipe->pipe_peer = rpipe; 144 145 FILE_SET_MATURE(rf); 146 FILE_SET_MATURE(wf); 147 return (0); 148 free3: 149 fdremove(fdp, retval[0]); 150 closef(rf, p); 151 rpipe = NULL; 152 free2: 153 (void)pipeclose(wpipe); 154 if (rpipe != NULL) 155 (void)pipeclose(rpipe); 156 return (error); 157 } 158 159 /* 160 * Allocate kva for pipe circular buffer, the space is pageable 161 */ 162 void 163 pipespace(cpipe) 164 struct pipe *cpipe; 165 { 166 cpipe->pipe_buffer.buffer = (caddr_t) uvm_km_valloc(kernel_map, 167 cpipe->pipe_buffer.size); 168 if (cpipe->pipe_buffer.buffer == NULL) 169 panic("pipespace: out of kvm"); 170 171 amountpipekva += cpipe->pipe_buffer.size; 172 } 173 174 /* 175 * initialize and allocate VM and memory for pipe 176 */ 177 void 178 pipeinit(cpipe) 179 struct pipe *cpipe; 180 { 181 182 cpipe->pipe_buffer.in = 0; 183 cpipe->pipe_buffer.out = 0; 184 cpipe->pipe_buffer.cnt = 0; 185 cpipe->pipe_buffer.size = PIPE_SIZE; 186 187 /* Buffer kva gets dynamically allocated */ 188 cpipe->pipe_buffer.buffer = NULL; 189 /* cpipe->pipe_buffer.object = invalid */ 190 191 cpipe->pipe_state = 0; 192 cpipe->pipe_peer = NULL; 193 cpipe->pipe_busy = 0; 194 microtime(&cpipe->pipe_ctime); 195 cpipe->pipe_atime = cpipe->pipe_ctime; 196 cpipe->pipe_mtime = cpipe->pipe_ctime; 197 bzero(&cpipe->pipe_sel, sizeof cpipe->pipe_sel); 198 cpipe->pipe_pgid = NO_PID; 199 } 200 201 202 /* 203 * lock a pipe for I/O, blocking other access 204 */ 205 static __inline int 206 pipelock(cpipe) 207 struct pipe *cpipe; 208 { 209 int error; 210 while (cpipe->pipe_state & PIPE_LOCK) { 211 cpipe->pipe_state |= PIPE_LWANT; 212 if ((error = tsleep(cpipe, PRIBIO|PCATCH, "pipelk", 0))) 213 return error; 214 } 215 cpipe->pipe_state |= PIPE_LOCK; 216 return 0; 217 } 218 219 /* 220 * unlock a pipe I/O lock 221 */ 222 static __inline void 223 pipeunlock(cpipe) 224 struct pipe *cpipe; 225 { 226 cpipe->pipe_state &= ~PIPE_LOCK; 227 if (cpipe->pipe_state & PIPE_LWANT) { 228 cpipe->pipe_state &= ~PIPE_LWANT; 229 wakeup(cpipe); 230 } 231 } 232 233 static __inline void 234 pipeselwakeup(cpipe) 235 struct pipe *cpipe; 236 { 237 if (cpipe->pipe_state & PIPE_SEL) { 238 cpipe->pipe_state &= ~PIPE_SEL; 239 selwakeup(&cpipe->pipe_sel); 240 } 241 if ((cpipe->pipe_state & PIPE_ASYNC) && cpipe->pipe_pgid != NO_PID) 242 gsignal(cpipe->pipe_pgid, SIGIO); 243 KNOTE(&cpipe->pipe_sel.si_note, 0); 244 } 245 246 /* ARGSUSED */ 247 int 248 pipe_read(fp, poff, uio, cred) 249 struct file *fp; 250 off_t *poff; 251 struct uio *uio; 252 struct ucred *cred; 253 { 254 struct pipe *rpipe = (struct pipe *) fp->f_data; 255 int error; 256 int nread = 0; 257 int size; 258 259 error = pipelock(rpipe); 260 if (error) 261 goto unlocked_error; 262 263 ++rpipe->pipe_busy; 264 265 while (uio->uio_resid) { 266 /* 267 * normal pipe buffer receive 268 */ 269 if (rpipe->pipe_buffer.cnt > 0) { 270 size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; 271 if (size > rpipe->pipe_buffer.cnt) 272 size = rpipe->pipe_buffer.cnt; 273 if (size > uio->uio_resid) 274 size = uio->uio_resid; 275 error = uiomove(&rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], 276 size, uio); 277 if (error) { 278 break; 279 } 280 rpipe->pipe_buffer.out += size; 281 if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) 282 rpipe->pipe_buffer.out = 0; 283 284 rpipe->pipe_buffer.cnt -= size; 285 286 /* 287 * If there is no more to read in the pipe, reset 288 * its pointers to the beginning. This improves 289 * cache hit stats. 290 */ 291 if (rpipe->pipe_buffer.cnt == 0) { 292 rpipe->pipe_buffer.in = 0; 293 rpipe->pipe_buffer.out = 0; 294 } 295 nread += size; 296 } else { 297 /* 298 * detect EOF condition 299 */ 300 if (rpipe->pipe_state & PIPE_EOF) { 301 /* XXX error = ? */ 302 break; 303 } 304 305 /* 306 * If the "write-side" has been blocked, wake it up now. 307 */ 308 if (rpipe->pipe_state & PIPE_WANTW) { 309 rpipe->pipe_state &= ~PIPE_WANTW; 310 wakeup(rpipe); 311 } 312 313 /* 314 * Break if some data was read. 315 */ 316 if (nread > 0) 317 break; 318 319 /* 320 * Unlock the pipe buffer for our remaining processing. 321 * We will either break out with an error or we will 322 * sleep and relock to loop. 323 */ 324 pipeunlock(rpipe); 325 326 /* 327 * Handle non-blocking mode operation or 328 * wait for more data. 329 */ 330 if (fp->f_flag & FNONBLOCK) 331 error = EAGAIN; 332 else { 333 rpipe->pipe_state |= PIPE_WANTR; 334 if ((error = tsleep(rpipe, PRIBIO|PCATCH, "piperd", 0)) == 0) 335 error = pipelock(rpipe); 336 } 337 if (error) 338 goto unlocked_error; 339 } 340 } 341 pipeunlock(rpipe); 342 343 if (error == 0) 344 microtime(&rpipe->pipe_atime); 345 unlocked_error: 346 --rpipe->pipe_busy; 347 348 /* 349 * PIPE_WANT processing only makes sense if pipe_busy is 0. 350 */ 351 if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { 352 rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); 353 wakeup(rpipe); 354 } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { 355 /* 356 * Handle write blocking hysteresis. 357 */ 358 if (rpipe->pipe_state & PIPE_WANTW) { 359 rpipe->pipe_state &= ~PIPE_WANTW; 360 wakeup(rpipe); 361 } 362 } 363 364 if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) 365 pipeselwakeup(rpipe); 366 367 return error; 368 } 369 370 int 371 pipe_write(fp, poff, uio, cred) 372 struct file *fp; 373 off_t *poff; 374 struct uio *uio; 375 struct ucred *cred; 376 { 377 int error = 0; 378 int orig_resid; 379 380 struct pipe *wpipe, *rpipe; 381 382 rpipe = (struct pipe *) fp->f_data; 383 wpipe = rpipe->pipe_peer; 384 385 /* 386 * detect loss of pipe read side, issue SIGPIPE if lost. 387 */ 388 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 389 return EPIPE; 390 } 391 392 /* 393 * If it is advantageous to resize the pipe buffer, do 394 * so. 395 */ 396 if ((uio->uio_resid > PIPE_SIZE) && 397 (nbigpipe < LIMITBIGPIPES) && 398 (wpipe->pipe_buffer.size <= PIPE_SIZE) && 399 (wpipe->pipe_buffer.cnt == 0)) { 400 401 if (wpipe->pipe_buffer.buffer) { 402 amountpipekva -= wpipe->pipe_buffer.size; 403 uvm_km_free(kernel_map, 404 (vaddr_t)wpipe->pipe_buffer.buffer, 405 wpipe->pipe_buffer.size); 406 } 407 408 wpipe->pipe_buffer.in = 0; 409 wpipe->pipe_buffer.out = 0; 410 wpipe->pipe_buffer.cnt = 0; 411 wpipe->pipe_buffer.size = BIG_PIPE_SIZE; 412 wpipe->pipe_buffer.buffer = NULL; 413 ++nbigpipe; 414 } 415 416 417 if (wpipe->pipe_buffer.buffer == NULL) { 418 if ((error = pipelock(wpipe)) == 0) { 419 pipespace(wpipe); 420 pipeunlock(wpipe); 421 } else { 422 return error; 423 } 424 } 425 426 ++wpipe->pipe_busy; 427 orig_resid = uio->uio_resid; 428 429 retrywrite: 430 while (uio->uio_resid) { 431 int space; 432 433 if (wpipe->pipe_state & PIPE_EOF) { 434 error = EPIPE; 435 break; 436 } 437 438 space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 439 440 /* Writes of size <= PIPE_BUF must be atomic. */ 441 if ((space < uio->uio_resid) && (orig_resid <= PIPE_BUF)) 442 space = 0; 443 444 if (space > 0 && 445 (wpipe->pipe_buffer.cnt < wpipe->pipe_buffer.size)) { 446 if ((error = pipelock(wpipe)) == 0) { 447 int size; /* Transfer size */ 448 int segsize; /* first segment to transfer */ 449 450 /* 451 * If a process blocked in uiomove, our 452 * value for space might be bad. 453 * 454 * XXX will we be ok if the reader has gone 455 * away here? 456 */ 457 if (space > wpipe->pipe_buffer.size - 458 wpipe->pipe_buffer.cnt) { 459 pipeunlock(wpipe); 460 goto retrywrite; 461 } 462 463 /* 464 * Transfer size is minimum of uio transfer 465 * and free space in pipe buffer. 466 */ 467 if (space > uio->uio_resid) 468 size = uio->uio_resid; 469 else 470 size = space; 471 /* 472 * First segment to transfer is minimum of 473 * transfer size and contiguous space in 474 * pipe buffer. If first segment to transfer 475 * is less than the transfer size, we've got 476 * a wraparound in the buffer. 477 */ 478 segsize = wpipe->pipe_buffer.size - 479 wpipe->pipe_buffer.in; 480 if (segsize > size) 481 segsize = size; 482 483 /* Transfer first segment */ 484 485 error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], 486 segsize, uio); 487 488 if (error == 0 && segsize < size) { 489 /* 490 * Transfer remaining part now, to 491 * support atomic writes. Wraparound 492 * happened. 493 */ 494 #ifdef DIAGNOSTIC 495 if (wpipe->pipe_buffer.in + segsize != 496 wpipe->pipe_buffer.size) 497 panic("Expected pipe buffer wraparound disappeared"); 498 #endif 499 500 error = uiomove(&wpipe->pipe_buffer.buffer[0], 501 size - segsize, uio); 502 } 503 if (error == 0) { 504 wpipe->pipe_buffer.in += size; 505 if (wpipe->pipe_buffer.in >= 506 wpipe->pipe_buffer.size) { 507 #ifdef DIAGNOSTIC 508 if (wpipe->pipe_buffer.in != size - segsize + wpipe->pipe_buffer.size) 509 panic("Expected wraparound bad"); 510 #endif 511 wpipe->pipe_buffer.in = size - segsize; 512 } 513 514 wpipe->pipe_buffer.cnt += size; 515 #ifdef DIAGNOSTIC 516 if (wpipe->pipe_buffer.cnt > wpipe->pipe_buffer.size) 517 panic("Pipe buffer overflow"); 518 #endif 519 } 520 pipeunlock(wpipe); 521 } 522 if (error) 523 break; 524 } else { 525 /* 526 * If the "read-side" has been blocked, wake it up now. 527 */ 528 if (wpipe->pipe_state & PIPE_WANTR) { 529 wpipe->pipe_state &= ~PIPE_WANTR; 530 wakeup(wpipe); 531 } 532 533 /* 534 * don't block on non-blocking I/O 535 */ 536 if (fp->f_flag & FNONBLOCK) { 537 error = EAGAIN; 538 break; 539 } 540 541 /* 542 * We have no more space and have something to offer, 543 * wake up selects. 544 */ 545 pipeselwakeup(wpipe); 546 547 wpipe->pipe_state |= PIPE_WANTW; 548 error = tsleep(wpipe, (PRIBIO + 1)|PCATCH, 549 "pipewr", 0); 550 if (error) 551 break; 552 /* 553 * If read side wants to go away, we just issue a 554 * signal to ourselves. 555 */ 556 if (wpipe->pipe_state & PIPE_EOF) { 557 error = EPIPE; 558 break; 559 } 560 } 561 } 562 563 --wpipe->pipe_busy; 564 if ((wpipe->pipe_busy == 0) && 565 (wpipe->pipe_state & PIPE_WANT)) { 566 wpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTR); 567 wakeup(wpipe); 568 } else if (wpipe->pipe_buffer.cnt > 0) { 569 /* 570 * If we have put any characters in the buffer, we wake up 571 * the reader. 572 */ 573 if (wpipe->pipe_state & PIPE_WANTR) { 574 wpipe->pipe_state &= ~PIPE_WANTR; 575 wakeup(wpipe); 576 } 577 } 578 579 /* 580 * Don't return EPIPE if I/O was successful 581 */ 582 if ((wpipe->pipe_buffer.cnt == 0) && 583 (uio->uio_resid == 0) && 584 (error == EPIPE)) 585 error = 0; 586 587 if (error == 0) 588 microtime(&wpipe->pipe_mtime); 589 /* 590 * We have something to offer, 591 * wake up select. 592 */ 593 if (wpipe->pipe_buffer.cnt) 594 pipeselwakeup(wpipe); 595 596 return error; 597 } 598 599 /* 600 * we implement a very minimal set of ioctls for compatibility with sockets. 601 */ 602 int 603 pipe_ioctl(fp, cmd, data, p) 604 struct file *fp; 605 u_long cmd; 606 caddr_t data; 607 struct proc *p; 608 { 609 struct pipe *mpipe = (struct pipe *)fp->f_data; 610 611 switch (cmd) { 612 613 case FIONBIO: 614 return (0); 615 616 case FIOASYNC: 617 if (*(int *)data) { 618 mpipe->pipe_state |= PIPE_ASYNC; 619 } else { 620 mpipe->pipe_state &= ~PIPE_ASYNC; 621 } 622 return (0); 623 624 case FIONREAD: 625 *(int *)data = mpipe->pipe_buffer.cnt; 626 return (0); 627 628 case SIOCSPGRP: 629 mpipe->pipe_pgid = *(int *)data; 630 return (0); 631 632 case SIOCGPGRP: 633 *(int *)data = mpipe->pipe_pgid; 634 return (0); 635 636 } 637 return (ENOTTY); 638 } 639 640 int 641 pipe_select(fp, which, p) 642 struct file *fp; 643 int which; 644 struct proc *p; 645 { 646 struct pipe *rpipe = (struct pipe *)fp->f_data; 647 struct pipe *wpipe; 648 649 wpipe = rpipe->pipe_peer; 650 switch (which) { 651 652 case FREAD: 653 if ((rpipe->pipe_buffer.cnt > 0) || 654 (rpipe->pipe_state & PIPE_EOF)) { 655 return (1); 656 } 657 selrecord(p, &rpipe->pipe_sel); 658 rpipe->pipe_state |= PIPE_SEL; 659 break; 660 661 case FWRITE: 662 if ((wpipe == NULL) || 663 (wpipe->pipe_state & PIPE_EOF) || 664 ((wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) { 665 return (1); 666 } 667 selrecord(p, &wpipe->pipe_sel); 668 wpipe->pipe_state |= PIPE_SEL; 669 break; 670 671 case 0: 672 if ((rpipe->pipe_state & PIPE_EOF) || 673 (wpipe == NULL) || 674 (wpipe->pipe_state & PIPE_EOF)) { 675 return (1); 676 } 677 678 selrecord(p, &rpipe->pipe_sel); 679 rpipe->pipe_state |= PIPE_SEL; 680 break; 681 } 682 return (0); 683 } 684 685 int 686 pipe_stat(fp, ub, p) 687 struct file *fp; 688 struct stat *ub; 689 struct proc *p; 690 { 691 struct pipe *pipe = (struct pipe *)fp->f_data; 692 693 bzero((caddr_t)ub, sizeof (*ub)); 694 ub->st_mode = S_IFIFO; 695 ub->st_blksize = pipe->pipe_buffer.size; 696 ub->st_size = pipe->pipe_buffer.cnt; 697 ub->st_blocks = (ub->st_size + ub->st_blksize - 1) / ub->st_blksize; 698 TIMEVAL_TO_TIMESPEC(&pipe->pipe_atime, &ub->st_atimespec); 699 TIMEVAL_TO_TIMESPEC(&pipe->pipe_mtime, &ub->st_mtimespec); 700 TIMEVAL_TO_TIMESPEC(&pipe->pipe_ctime, &ub->st_ctimespec); 701 ub->st_uid = fp->f_cred->cr_uid; 702 ub->st_gid = fp->f_cred->cr_gid; 703 /* 704 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen. 705 * XXX (st_dev, st_ino) should be unique. 706 */ 707 return 0; 708 } 709 710 /* ARGSUSED */ 711 int 712 pipe_close(fp, p) 713 struct file *fp; 714 struct proc *p; 715 { 716 struct pipe *cpipe = (struct pipe *)fp->f_data; 717 718 pipeclose(cpipe); 719 fp->f_data = NULL; 720 return 0; 721 } 722 723 /* 724 * shutdown the pipe 725 */ 726 void 727 pipeclose(cpipe) 728 struct pipe *cpipe; 729 { 730 struct pipe *ppipe; 731 if (cpipe) { 732 733 pipeselwakeup(cpipe); 734 735 /* 736 * If the other side is blocked, wake it up saying that 737 * we want to close it down. 738 */ 739 while (cpipe->pipe_busy) { 740 wakeup(cpipe); 741 cpipe->pipe_state |= PIPE_WANT|PIPE_EOF; 742 tsleep(cpipe, PRIBIO, "pipecl", 0); 743 } 744 745 /* 746 * Disconnect from peer 747 */ 748 if ((ppipe = cpipe->pipe_peer) != NULL) { 749 pipeselwakeup(ppipe); 750 751 ppipe->pipe_state |= PIPE_EOF; 752 wakeup(ppipe); 753 ppipe->pipe_peer = NULL; 754 } 755 756 /* 757 * free resources 758 */ 759 if (cpipe->pipe_buffer.buffer) { 760 if (cpipe->pipe_buffer.size > PIPE_SIZE) 761 --nbigpipe; 762 amountpipekva -= cpipe->pipe_buffer.size; 763 uvm_km_free(kernel_map, 764 (vaddr_t)cpipe->pipe_buffer.buffer, 765 cpipe->pipe_buffer.size); 766 } 767 pool_put(&pipe_pool, cpipe); 768 } 769 } 770 771 int 772 pipe_kqfilter(struct file *fp, struct knote *kn) 773 { 774 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 775 struct pipe *wpipe = rpipe->pipe_peer; 776 777 switch (kn->kn_filter) { 778 case EVFILT_READ: 779 kn->kn_fop = &pipe_rfiltops; 780 SLIST_INSERT_HEAD(&rpipe->pipe_sel.si_note, kn, kn_selnext); 781 break; 782 case EVFILT_WRITE: 783 if (wpipe == NULL) 784 return (1); 785 kn->kn_fop = &pipe_wfiltops; 786 SLIST_INSERT_HEAD(&wpipe->pipe_sel.si_note, kn, kn_selnext); 787 break; 788 default: 789 return (1); 790 } 791 792 return (0); 793 } 794 795 void 796 filt_pipedetach(struct knote *kn) 797 { 798 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 799 struct pipe *wpipe = rpipe->pipe_peer; 800 801 switch (kn->kn_filter) { 802 case EVFILT_READ: 803 SLIST_REMOVE(&rpipe->pipe_sel.si_note, kn, knote, kn_selnext); 804 break; 805 case EVFILT_WRITE: 806 if (wpipe == NULL) 807 return; 808 SLIST_REMOVE(&wpipe->pipe_sel.si_note, kn, knote, kn_selnext); 809 break; 810 } 811 } 812 813 /*ARGSUSED*/ 814 int 815 filt_piperead(struct knote *kn, long hint) 816 { 817 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 818 struct pipe *wpipe = rpipe->pipe_peer; 819 820 kn->kn_data = rpipe->pipe_buffer.cnt; 821 822 if ((rpipe->pipe_state & PIPE_EOF) || 823 (wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 824 kn->kn_flags |= EV_EOF; 825 return (1); 826 } 827 return (kn->kn_data > 0); 828 } 829 830 /*ARGSUSED*/ 831 int 832 filt_pipewrite(struct knote *kn, long hint) 833 { 834 struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; 835 struct pipe *wpipe = rpipe->pipe_peer; 836 837 if ((wpipe == NULL) || (wpipe->pipe_state & PIPE_EOF)) { 838 kn->kn_data = 0; 839 kn->kn_flags |= EV_EOF; 840 return (1); 841 } 842 kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; 843 844 return (kn->kn_data >= PIPE_BUF); 845 } 846 847 void 848 pipe_init() 849 { 850 pool_init(&pipe_pool, sizeof(struct pipe), 0, 0, 0, "pipepl", 851 &pool_allocator_nointr); 852 } 853 854