The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sys_pipe.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 96066b36d8fdb095c1db76859e5d3333


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.