2 * Copyright (c) 1996 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
16 * 4. Modifications may be freely made to this file if the above conditions
20 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
22 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
24 * This file contains Original Code and/or Modifications of Original Code
25 * as defined in and that are subject to the Apple Public Source License
26 * Version 2.0 (the 'License'). You may not use this file except in
27 * compliance with the License. The rights granted to you under the License
28 * may not be used to create, or enable the creation or redistribution of,
29 * unlawful or unlicensed copies of an Apple operating system, or to
30 * circumvent, violate, or enable the circumvention or violation of, any
31 * terms of an Apple operating system software license agreement.
33 * Please obtain a copy of the License at
34 * http://www.opensource.apple.com/apsl/ and read it before using this file.
36 * The Original Code and all software distributed under the License are
37 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
38 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
39 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
40 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
41 * Please see the License for the specific language governing rights and
42 * limitations under the License.
44 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
47 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
48 * support for mandatory and extensible security protections. This notice
49 * is included in support of clause 2.2 (b) of the Apple Public License,
54 * This file contains a high-performance replacement for the socket-based
55 * pipes scheme originally used in FreeBSD/4.4Lite. It does not support
56 * all features of sockets, but does do everything that pipes normally
61 * This code has two modes of operation, a small write mode and a large
62 * write mode. The small write mode acts like conventional pipes with
63 * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the
64 * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT
65 * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and
66 * the receiving process can copy it directly from the pages in the sending
69 * If the sending process receives a signal, it is possible that it will
70 * go away, and certainly its address space can change, because control
71 * is returned back to the user-mode side. In that case, the pipe code
72 * arranges to copy the buffer supplied by the user process, to a pageable
73 * kernel buffer, and the receiving process will grab the data from the
74 * pageable kernel buffer. Since signals don't happen all that often,
75 * the copy operation is normally eliminated.
77 * The constant PIPE_MINDIRECT is chosen to make sure that buffering will
78 * happen for small transfers so that the system will not spend all of
79 * its time context switching.
81 * In order to limit the resource use of pipes, two sysctls exist:
83 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
84 * address space available to us in pipe_map. Whenever the amount in use
85 * exceeds half of this value, all new pipes will be created with size
86 * SMALL_PIPE_SIZE, rather than PIPE_SIZE. Big pipe creation will be limited
87 * as well. This value is loader tunable only.
89 * kern.ipc.maxpipekvawired - This value limits the amount of memory that may
90 * be wired in order to facilitate direct copies using page flipping.
91 * Whenever this value is exceeded, pipes will fall back to using regular
92 * copies. This value is sysctl controllable at all times.
94 * These values are autotuned in subr_param.c.
96 * Memory usage may be monitored through the sysctls
97 * kern.ipc.pipes, kern.ipc.pipekva and kern.ipc.pipekvawired.
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/filedesc.h>
104 #include <sys/kernel.h>
105 #include <sys/vnode.h>
106 #include <sys/proc_internal.h>
107 #include <sys/kauth.h>
108 #include <sys/file_internal.h>
109 #include <sys/stat.h>
110 #include <sys/ioctl.h>
111 #include <sys/fcntl.h>
112 #include <sys/malloc.h>
113 #include <sys/syslog.h>
114 #include <sys/unistd.h>
115 #include <sys/resourcevar.h>
116 #include <sys/aio_kern.h>
117 #include <sys/signalvar.h>
118 #include <sys/pipe.h>
119 #include <sys/sysproto.h>
120 #include <sys/proc_info.h>
122 #include <security/audit/audit.h>
124 #include <sys/kdebug.h>
126 #include <kern/zalloc.h>
127 #include <vm/vm_kern.h>
128 #include <libkern/OSAtomic.h>
130 #define f_flag f_fglob->fg_flag
131 #define f_type f_fglob->fg_type
132 #define f_msgcount f_fglob->fg_msgcount
133 #define f_cred f_fglob->fg_cred
134 #define f_ops f_fglob->fg_ops
135 #define f_offset f_fglob->fg_offset
136 #define f_data f_fglob->fg_data
138 * Use this define if you want to disable *fancy* VM things. Expect an
139 * approx 30% decrease in transfer rate. This could be useful for
142 * this needs to be ported to X and the performance measured
143 * before committing to supporting it
145 #define PIPE_NODIRECT 1
147 #ifndef PIPE_NODIRECT
150 #include <vm/vm_param.h>
151 #include <vm/vm_object.h>
152 #include <vm/vm_kern.h>
153 #include <vm/vm_extern.h>
155 #include <vm/vm_map.h>
156 #include <vm/vm_page.h>
162 * interfaces to the outside world
164 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
165 int flags
, vfs_context_t ctx
);
167 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
168 int flags
, vfs_context_t ctx
);
170 static int pipe_close(struct fileglob
*fg
, vfs_context_t ctx
);
172 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
,
175 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
178 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
181 static int pipe_drain(struct fileproc
*fp
,vfs_context_t ctx
);
184 struct fileops pipeops
=
194 static void filt_pipedetach(struct knote
*kn
);
195 static int filt_piperead(struct knote
*kn
, long hint
);
196 static int filt_pipewrite(struct knote
*kn
, long hint
);
198 static struct filterops pipe_rfiltops
= {
200 .f_detach
= filt_pipedetach
,
201 .f_event
= filt_piperead
,
203 static struct filterops pipe_wfiltops
= {
205 .f_detach
= filt_pipedetach
,
206 .f_event
= filt_pipewrite
,
210 * Default pipe buffer size(s), this can be kind-of large now because pipe
211 * space is pageable. The pipe code will try to maintain locality of
212 * reference for performance reasons, so small amounts of outstanding I/O
213 * will not wipe the cache.
215 #define MINPIPESIZE (PIPE_SIZE/3)
218 * Limit the number of "big" pipes
220 #define LIMITBIGPIPES 32
223 static int amountpipes
;
224 static int amountpipekva
;
226 #ifndef PIPE_NODIRECT
227 static int amountpipekvawired
;
229 int maxpipekva
= 1024 * 1024 * 16;
232 SYSCTL_DECL(_kern_ipc
);
234 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
,
235 &maxpipekva
, 0, "Pipe KVA limit");
236 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
,
237 &maxpipekvawired
, 0, "Pipe KVA wired limit");
238 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
,
239 &amountpipes
, 0, "Current # of pipes");
240 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
,
241 &nbigpipe
, 0, "Current # of big pipes");
242 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
,
243 &amountpipekva
, 0, "Pipe KVA usage");
244 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
,
245 &amountpipekvawired
, 0, "Pipe wired KVA usage");
248 static void pipeclose(struct pipe
*cpipe
);
249 static void pipe_free_kmem(struct pipe
*cpipe
);
250 static int pipe_create(struct pipe
**cpipep
);
251 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
252 static __inline
int pipelock(struct pipe
*cpipe
, int catch);
253 static __inline
void pipeunlock(struct pipe
*cpipe
);
255 #ifndef PIPE_NODIRECT
256 static int pipe_build_write_buffer(struct pipe
*wpipe
, struct uio
*uio
);
257 static void pipe_destroy_write_buffer(struct pipe
*wpipe
);
258 static int pipe_direct_write(struct pipe
*wpipe
, struct uio
*uio
);
259 static void pipe_clone_write_buffer(struct pipe
*wpipe
);
262 extern int postpipeevent(struct pipe
*, int);
263 extern void evpipefree(struct pipe
*cpipe
);
266 static int pipespace(struct pipe
*cpipe
, int size
);
268 static lck_grp_t
*pipe_mtx_grp
;
269 static lck_attr_t
*pipe_mtx_attr
;
270 static lck_grp_attr_t
*pipe_mtx_grp_attr
;
272 static zone_t pipe_zone
;
274 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
279 pipe_zone
= (zone_t
)zinit(sizeof(struct pipe
), 8192 * sizeof(struct pipe
), 4096, "pipe zone");
282 * allocate lock group attribute and group for pipe mutexes
284 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
285 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
288 * allocate the lock attribute for pipe mutexes
290 pipe_mtx_attr
= lck_attr_alloc_init();
293 /* Bitmap for things to touch in pipe_touch() */
294 #define PIPE_ATIME 0x00000001 /* time of last access */
295 #define PIPE_MTIME 0x00000002 /* time of last modification */
296 #define PIPE_CTIME 0x00000004 /* time of last status change */
299 pipe_touch(struct pipe
*tpipe
, int touch
)
305 if (touch
& PIPE_ATIME
) {
306 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
307 tpipe
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
310 if (touch
& PIPE_MTIME
) {
311 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
312 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
315 if (touch
& PIPE_CTIME
) {
316 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
317 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
324 * The pipe system call for the DTYPE_PIPE type of pipes
329 pipe(proc_t p
, __unused
struct pipe_args
*uap
, int32_t *retval
)
331 struct fileproc
*rf
, *wf
;
332 struct pipe
*rpipe
, *wpipe
;
336 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
339 rpipe
= wpipe
= NULL
;
340 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
345 * allocate the space for the normal I/O direction up
346 * front... we'll delay the allocation for the other
347 * direction until a write actually occurs (most
348 * likely it won't)...
350 * Reduce to 1/4th pipe size if we're over our global max.
352 if (amountpipekva
> maxpipekva
/ 2)
353 error
= pipespace(rpipe
, SMALL_PIPE_SIZE
);
355 error
= pipespace(rpipe
, PIPE_SIZE
);
359 #ifndef PIPE_NODIRECT
360 rpipe
->pipe_state
|= PIPE_DIRECTOK
;
361 wpipe
->pipe_state
|= PIPE_DIRECTOK
;
363 TAILQ_INIT(&rpipe
->pipe_evlist
);
364 TAILQ_INIT(&wpipe
->pipe_evlist
);
366 error
= falloc(p
, &rf
, &fd
, vfs_context_current());
373 * for now we'll create half-duplex
374 * pipes... this is what we've always
378 rf
->f_type
= DTYPE_PIPE
;
379 rf
->f_data
= (caddr_t
)rpipe
;
380 rf
->f_ops
= &pipeops
;
382 error
= falloc(p
, &wf
, &fd
, vfs_context_current());
384 fp_free(p
, retval
[0], rf
);
388 wf
->f_type
= DTYPE_PIPE
;
389 wf
->f_data
= (caddr_t
)wpipe
;
390 wf
->f_ops
= &pipeops
;
392 rpipe
->pipe_peer
= wpipe
;
393 wpipe
->pipe_peer
= rpipe
;
394 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
399 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
401 * struct pipe represents a pipe endpoint. The MAC label is shared
402 * between the connected endpoints. As a result mac_pipe_label_init() and
403 * mac_pipe_label_associate() should only be called on one of the endpoints
404 * after they have been connected.
406 mac_pipe_label_init(rpipe
);
407 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
408 wpipe
->pipe_label
= rpipe
->pipe_label
;
411 procfdtbl_releasefd(p
, retval
[0], NULL
);
412 procfdtbl_releasefd(p
, retval
[1], NULL
);
413 fp_drop(p
, retval
[0], rf
, 1);
414 fp_drop(p
, retval
[1], wf
, 1);
423 lck_mtx_free(pmtx
, pipe_mtx_grp
);
429 pipe_stat(struct pipe
*cpipe
, void *ub
, int isstat64
)
436 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
437 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
444 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
450 if (cpipe
->pipe_buffer
.buffer
== 0) {
452 * must be stat'ing the write fd
454 if (cpipe
->pipe_peer
) {
456 * the peer still exists, use it's info
458 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
459 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
464 pipe_size
= cpipe
->pipe_buffer
.size
;
465 pipe_count
= cpipe
->pipe_buffer
.cnt
;
468 * since peer's buffer is setup ouside of lock
469 * we might catch it in transient state
472 pipe_size
= PIPE_SIZE
;
475 sb64
= (struct stat64
*)ub
;
477 bzero(sb64
, sizeof(*sb64
));
478 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
479 sb64
->st_blksize
= pipe_size
;
480 sb64
->st_size
= pipe_count
;
481 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
483 sb64
->st_uid
= kauth_getuid();
484 sb64
->st_gid
= kauth_getgid();
486 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
487 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
489 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
490 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
492 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
493 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
496 * Return a relatively unique inode number based on the current
497 * address of this pipe's struct pipe. This number may be recycled
498 * relatively quickly.
500 sb64
->st_ino
= (ino64_t
)((uintptr_t)cpipe
);
502 sb
= (struct stat
*)ub
;
504 bzero(sb
, sizeof(*sb
));
505 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
506 sb
->st_blksize
= pipe_size
;
507 sb
->st_size
= pipe_count
;
508 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
510 sb
->st_uid
= kauth_getuid();
511 sb
->st_gid
= kauth_getgid();
513 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
514 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
516 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
517 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
519 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
520 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
523 * Return a relatively unique inode number based on the current
524 * address of this pipe's struct pipe. This number may be recycled
525 * relatively quickly.
527 sb
->st_ino
= (ino_t
)(uintptr_t)cpipe
;
532 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
535 * XXX (st_dev) should be unique, but there is no device driver that
536 * XXX is associated with pipes, since they are implemented via a
537 * XXX struct fileops indirection rather than as FS objects.
544 * Allocate kva for pipe circular buffer, the space is pageable
545 * This routine will 'realloc' the size of a pipe safely, if it fails
546 * it will retain the old buffer.
547 * If it fails it will return ENOMEM.
550 pipespace(struct pipe
*cpipe
, int size
)
554 size
= round_page(size
);
556 if (kmem_alloc(kernel_map
, &buffer
, size
) != KERN_SUCCESS
)
559 /* free old resources if we're resizing */
560 pipe_free_kmem(cpipe
);
561 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
562 cpipe
->pipe_buffer
.size
= size
;
563 cpipe
->pipe_buffer
.in
= 0;
564 cpipe
->pipe_buffer
.out
= 0;
565 cpipe
->pipe_buffer
.cnt
= 0;
567 OSAddAtomic(1, &amountpipes
);
568 OSAddAtomic(cpipe
->pipe_buffer
.size
, &amountpipekva
);
574 * initialize and allocate VM and memory for pipe
577 pipe_create(struct pipe
**cpipep
)
581 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
583 if ((*cpipep
= cpipe
) == NULL
)
587 * protect so pipespace or pipeclose don't follow a junk pointer
588 * if pipespace() fails.
590 bzero(cpipe
, sizeof *cpipe
);
592 /* Initial times are all the time of creation of the pipe */
593 pipe_touch(cpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
600 * lock a pipe for I/O, blocking other access
603 pipelock(struct pipe
*cpipe
, int catch)
607 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
608 cpipe
->pipe_state
|= PIPE_LWANT
;
610 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
615 cpipe
->pipe_state
|= PIPE_LOCKFL
;
621 * unlock a pipe I/O lock
624 pipeunlock(struct pipe
*cpipe
)
626 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
628 if (cpipe
->pipe_state
& PIPE_LWANT
) {
629 cpipe
->pipe_state
&= ~PIPE_LWANT
;
635 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
637 if (cpipe
->pipe_state
& PIPE_SEL
) {
638 cpipe
->pipe_state
&= ~PIPE_SEL
;
639 selwakeup(&cpipe
->pipe_sel
);
641 if (cpipe
->pipe_state
& PIPE_KNOTE
)
642 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
644 postpipeevent(cpipe
, EV_RWBYTES
);
646 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
647 if (spipe
->pipe_pgid
< 0)
648 gsignal(-spipe
->pipe_pgid
, SIGIO
);
650 proc_signal(spipe
->pipe_pgid
, SIGIO
);
656 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
657 __unused vfs_context_t ctx
)
659 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
667 error
= pipelock(rpipe
, 1);
672 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
677 while (uio_resid(uio
)) {
679 * normal pipe buffer receive
681 if (rpipe
->pipe_buffer
.cnt
> 0) {
682 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
683 if (size
> rpipe
->pipe_buffer
.cnt
)
684 size
= rpipe
->pipe_buffer
.cnt
;
685 // LP64todo - fix this!
686 if (size
> (u_int
) uio_resid(uio
))
687 size
= (u_int
) uio_resid(uio
);
691 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
697 rpipe
->pipe_buffer
.out
+= size
;
698 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
699 rpipe
->pipe_buffer
.out
= 0;
701 rpipe
->pipe_buffer
.cnt
-= size
;
704 * If there is no more to read in the pipe, reset
705 * its pointers to the beginning. This improves
708 if (rpipe
->pipe_buffer
.cnt
== 0) {
709 rpipe
->pipe_buffer
.in
= 0;
710 rpipe
->pipe_buffer
.out
= 0;
713 #ifndef PIPE_NODIRECT
715 * Direct copy, bypassing a kernel buffer.
717 } else if ((size
= rpipe
->pipe_map
.cnt
) &&
718 (rpipe
->pipe_state
& PIPE_DIRECTW
)) {
720 // LP64todo - fix this!
721 if (size
> (u_int
) uio_resid(uio
))
722 size
= (u_int
) uio_resid(uio
);
724 va
= (caddr_t
) rpipe
->pipe_map
.kva
+
727 error
= uiomove(va
, size
, uio
);
732 rpipe
->pipe_map
.pos
+= size
;
733 rpipe
->pipe_map
.cnt
-= size
;
734 if (rpipe
->pipe_map
.cnt
== 0) {
735 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
741 * detect EOF condition
742 * read returns 0 on EOF, no need to set error
744 if (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
749 * If the "write-side" has been blocked, wake it up now.
751 if (rpipe
->pipe_state
& PIPE_WANTW
) {
752 rpipe
->pipe_state
&= ~PIPE_WANTW
;
757 * Break if some data was read.
763 * Unlock the pipe buffer for our remaining processing.
764 * We will either break out with an error or we will
765 * sleep and relock to loop.
770 * Handle non-blocking mode operation or
771 * wait for more data.
773 if (fp
->f_flag
& FNONBLOCK
) {
776 rpipe
->pipe_state
|= PIPE_WANTR
;
778 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
781 error
= pipelock(rpipe
, 1);
796 * PIPE_WANT processing only makes sense if pipe_busy is 0.
798 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
799 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
801 } else if (rpipe
->pipe_buffer
.cnt
< MINPIPESIZE
) {
803 * Handle write blocking hysteresis.
805 if (rpipe
->pipe_state
& PIPE_WANTW
) {
806 rpipe
->pipe_state
&= ~PIPE_WANTW
;
811 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)
812 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
814 /* update last read time */
815 pipe_touch(rpipe
, PIPE_ATIME
);
824 #ifndef PIPE_NODIRECT
826 * Map the sending processes' buffer into kernel space and wire it.
827 * This is similar to a physical write operation.
830 pipe_build_write_buffer(wpipe
, uio
)
837 vm_offset_t addr
, endaddr
;
840 size
= (u_int
) uio
->uio_iov
->iov_len
;
841 if (size
> wpipe
->pipe_buffer
.size
)
842 size
= wpipe
->pipe_buffer
.size
;
844 pmap
= vmspace_pmap(curproc
->p_vmspace
);
845 endaddr
= round_page((vm_offset_t
)uio
->uio_iov
->iov_base
+ size
);
846 addr
= trunc_page((vm_offset_t
)uio
->uio_iov
->iov_base
);
847 for (i
= 0; addr
< endaddr
; addr
+= PAGE_SIZE
, i
++) {
849 * vm_fault_quick() can sleep. Consequently,
850 * vm_page_lock_queue() and vm_page_unlock_queue()
851 * should not be performed outside of this loop.
854 if (vm_fault_quick((caddr_t
)addr
, VM_PROT_READ
) < 0) {
855 vm_page_lock_queues();
856 for (j
= 0; j
< i
; j
++)
857 vm_page_unhold(wpipe
->pipe_map
.ms
[j
]);
858 vm_page_unlock_queues();
861 wpipe
->pipe_map
.ms
[i
] = pmap_extract_and_hold(pmap
, addr
,
863 if (wpipe
->pipe_map
.ms
[i
] == NULL
)
868 * set up the control block
870 wpipe
->pipe_map
.npages
= i
;
871 wpipe
->pipe_map
.pos
=
872 ((vm_offset_t
) uio
->uio_iov
->iov_base
) & PAGE_MASK
;
873 wpipe
->pipe_map
.cnt
= size
;
878 if (wpipe
->pipe_map
.kva
== 0) {
880 * We need to allocate space for an extra page because the
881 * address range might (will) span pages at times.
883 wpipe
->pipe_map
.kva
= kmem_alloc_nofault(kernel_map
,
884 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
885 atomic_add_int(&amountpipekvawired
,
886 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
888 pmap_qenter(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.ms
,
889 wpipe
->pipe_map
.npages
);
892 * and update the uio data
895 uio
->uio_iov
->iov_len
-= size
;
896 uio
->uio_iov
->iov_base
= (char *)uio
->uio_iov
->iov_base
+ size
;
897 if (uio
->uio_iov
->iov_len
== 0)
899 uio_setresid(uio
, (uio_resid(uio
) - size
));
900 uio
->uio_offset
+= size
;
905 * unmap and unwire the process buffer
908 pipe_destroy_write_buffer(wpipe
)
913 if (wpipe
->pipe_map
.kva
) {
914 pmap_qremove(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.npages
);
916 if (amountpipekvawired
> maxpipekvawired
/ 2) {
917 /* Conserve address space */
918 vm_offset_t kva
= wpipe
->pipe_map
.kva
;
919 wpipe
->pipe_map
.kva
= 0;
920 kmem_free(kernel_map
, kva
,
921 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
922 atomic_subtract_int(&amountpipekvawired
,
923 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
926 vm_page_lock_queues();
927 for (i
= 0; i
< wpipe
->pipe_map
.npages
; i
++) {
928 vm_page_unhold(wpipe
->pipe_map
.ms
[i
]);
930 vm_page_unlock_queues();
931 wpipe
->pipe_map
.npages
= 0;
935 * In the case of a signal, the writing process might go away. This
936 * code copies the data into the circular buffer so that the source
937 * pages can be freed without loss of data.
940 pipe_clone_write_buffer(wpipe
)
946 size
= wpipe
->pipe_map
.cnt
;
947 pos
= wpipe
->pipe_map
.pos
;
949 wpipe
->pipe_buffer
.in
= size
;
950 wpipe
->pipe_buffer
.out
= 0;
951 wpipe
->pipe_buffer
.cnt
= size
;
952 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
955 bcopy((caddr_t
) wpipe
->pipe_map
.kva
+ pos
,
956 wpipe
->pipe_buffer
.buffer
, size
);
957 pipe_destroy_write_buffer(wpipe
);
962 * This implements the pipe buffer write mechanism. Note that only
963 * a direct write OR a normal pipe write can be pending at any given time.
964 * If there are any characters in the pipe buffer, the direct write will
965 * be deferred until the receiving process grabs all of the bytes from
966 * the pipe buffer. Then the direct mapping write is set-up.
969 pipe_direct_write(wpipe
, uio
)
976 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
977 if (wpipe
->pipe_state
& PIPE_WANTR
) {
978 wpipe
->pipe_state
&= ~PIPE_WANTR
;
981 wpipe
->pipe_state
|= PIPE_WANTW
;
982 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
983 PRIBIO
| PCATCH
, "pipdww", 0);
986 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
991 wpipe
->pipe_map
.cnt
= 0; /* transfer not ready yet */
992 if (wpipe
->pipe_buffer
.cnt
> 0) {
993 if (wpipe
->pipe_state
& PIPE_WANTR
) {
994 wpipe
->pipe_state
&= ~PIPE_WANTR
;
998 wpipe
->pipe_state
|= PIPE_WANTW
;
999 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
1000 PRIBIO
| PCATCH
, "pipdwc", 0);
1003 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1010 wpipe
->pipe_state
|= PIPE_DIRECTW
;
1014 error
= pipe_build_write_buffer(wpipe
, uio
);
1018 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
1023 while (!error
&& (wpipe
->pipe_state
& PIPE_DIRECTW
)) {
1024 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1027 pipe_destroy_write_buffer(wpipe
);
1029 pipeselwakeup(wpipe
, wpipe
);
1034 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1035 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1038 pipeselwakeup(wpipe
, wpipe
);
1039 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
,
1044 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1046 * this bit of trickery substitutes a kernel buffer for
1047 * the process that might be going away.
1049 pipe_clone_write_buffer(wpipe
);
1052 pipe_destroy_write_buffer(wpipe
);
1067 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
1068 __unused vfs_context_t ctx
)
1073 struct pipe
*wpipe
, *rpipe
;
1075 rpipe
= (struct pipe
*)fp
->f_data
;
1078 wpipe
= rpipe
->pipe_peer
;
1081 * detect loss of pipe read side, issue SIGPIPE if lost.
1083 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1088 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
1098 if (wpipe
->pipe_buffer
.buffer
== 0) {
1100 * need to allocate some storage... we delay the allocation
1101 * until the first write on fd[0] to avoid allocating storage for both
1102 * 'pipe ends'... most pipes are half-duplex with the writes targeting
1103 * fd[1], so allocating space for both ends is a waste...
1105 * Reduce to 1/4th pipe size if we're over our global max.
1107 if (amountpipekva
> maxpipekva
/ 2)
1108 pipe_size
= SMALL_PIPE_SIZE
;
1110 pipe_size
= PIPE_SIZE
;
1114 * If it is advantageous to resize the pipe buffer, do
1117 if ((uio_resid(uio
) > PIPE_SIZE
) &&
1118 (wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
1119 (amountpipekva
< maxpipekva
/ 2) &&
1120 (nbigpipe
< LIMITBIGPIPES
) &&
1121 #ifndef PIPE_NODIRECT
1122 (wpipe
->pipe_state
& PIPE_DIRECTW
) == 0 &&
1124 (wpipe
->pipe_buffer
.cnt
== 0)) {
1126 pipe_size
= BIG_PIPE_SIZE
;
1131 * need to do initial allocation or resizing of pipe
1133 if ((error
= pipelock(wpipe
, 1)) == 0) {
1135 if (pipespace(wpipe
, pipe_size
) == 0)
1136 OSAddAtomic(1, &nbigpipe
);
1140 if (wpipe
->pipe_buffer
.buffer
== 0) {
1142 * initial allocation failed
1149 * If an error occurred unbusy and return, waking up any pending
1153 if ((wpipe
->pipe_busy
== 0) &&
1154 (wpipe
->pipe_state
& PIPE_WANT
)) {
1155 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1162 // LP64todo - fix this!
1163 orig_resid
= uio_resid(uio
);
1165 while (uio_resid(uio
)) {
1168 #ifndef PIPE_NODIRECT
1170 * If the transfer is large, we can gain performance if
1171 * we do process-to-process copies directly.
1172 * If the write is non-blocking, we don't use the
1173 * direct write mechanism.
1175 * The direct write mechanism will detect the reader going
1178 if ((uio
->uio_iov
->iov_len
>= PIPE_MINDIRECT
) &&
1179 (fp
->f_flag
& FNONBLOCK
) == 0 &&
1180 amountpipekvawired
+ uio_resid(uio
) < maxpipekvawired
) {
1181 error
= pipe_direct_write(wpipe
, uio
);
1188 * Pipe buffered writes cannot be coincidental with
1189 * direct writes. We wait until the currently executing
1190 * direct write is completed before we start filling the
1191 * pipe buffer. We break out if a signal occurs or the
1195 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1196 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1197 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1200 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipbww", 0);
1202 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))
1210 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1213 * Writes of size <= PIPE_BUF must be atomic.
1215 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
1220 if ((error
= pipelock(wpipe
,1)) == 0) {
1221 int size
; /* Transfer size */
1222 int segsize
; /* first segment to transfer */
1224 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1229 #ifndef PIPE_NODIRECT
1231 * It is possible for a direct write to
1232 * slip in on us... handle it here...
1234 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1240 * If a process blocked in pipelock, our
1241 * value for space might be bad... the mutex
1242 * is dropped while we're blocked
1244 if (space
> (int)(wpipe
->pipe_buffer
.size
-
1245 wpipe
->pipe_buffer
.cnt
)) {
1251 * Transfer size is minimum of uio transfer
1252 * and free space in pipe buffer.
1254 // LP64todo - fix this!
1255 if (space
> uio_resid(uio
))
1256 size
= uio_resid(uio
);
1260 * First segment to transfer is minimum of
1261 * transfer size and contiguous space in
1262 * pipe buffer. If first segment to transfer
1263 * is less than the transfer size, we've got
1264 * a wraparound in the buffer.
1266 segsize
= wpipe
->pipe_buffer
.size
-
1267 wpipe
->pipe_buffer
.in
;
1271 /* Transfer first segment */
1274 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1278 if (error
== 0 && segsize
< size
) {
1280 * Transfer remaining part now, to
1281 * support atomic writes. Wraparound
1284 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1285 wpipe
->pipe_buffer
.size
)
1286 panic("Expected pipe buffer "
1287 "wraparound disappeared");
1291 &wpipe
->pipe_buffer
.buffer
[0],
1292 size
- segsize
, uio
);
1296 wpipe
->pipe_buffer
.in
+= size
;
1297 if (wpipe
->pipe_buffer
.in
>=
1298 wpipe
->pipe_buffer
.size
) {
1299 if (wpipe
->pipe_buffer
.in
!=
1301 wpipe
->pipe_buffer
.size
)
1304 wpipe
->pipe_buffer
.in
= size
-
1308 wpipe
->pipe_buffer
.cnt
+= size
;
1309 if (wpipe
->pipe_buffer
.cnt
>
1310 wpipe
->pipe_buffer
.size
)
1311 panic("Pipe buffer overflow");
1321 * If the "read-side" has been blocked, wake it up now.
1323 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1324 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1328 * don't block on non-blocking I/O
1329 * we'll do the pipeselwakeup on the way out
1331 if (fp
->f_flag
& FNONBLOCK
) {
1336 * We have no more space and have something to offer,
1337 * wake up select/poll.
1339 pipeselwakeup(wpipe
, wpipe
);
1341 wpipe
->pipe_state
|= PIPE_WANTW
;
1343 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1348 * If read side wants to go away, we just issue a signal
1351 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1359 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1360 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1363 if (wpipe
->pipe_buffer
.cnt
> 0) {
1365 * If there are any characters in the buffer, we wake up
1366 * the reader if it was blocked waiting for data.
1368 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1369 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1373 * wake up thread blocked in select/poll or post the notification
1375 pipeselwakeup(wpipe
, wpipe
);
1378 /* Update modification, status change (# of bytes in pipe) times */
1379 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1380 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1387 * we implement a very minimal set of ioctls for compatibility with sockets.
1391 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1392 __unused vfs_context_t ctx
)
1394 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1402 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1418 mpipe
->pipe_state
|= PIPE_ASYNC
;
1420 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1426 #ifndef PIPE_NODIRECT
1427 if (mpipe
->pipe_state
& PIPE_DIRECTW
)
1428 *(int *)data
= mpipe
->pipe_map
.cnt
;
1431 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1436 mpipe
->pipe_pgid
= *(int *)data
;
1442 *(int *)data
= mpipe
->pipe_pgid
;
1454 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1456 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1460 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1465 wpipe
= rpipe
->pipe_peer
;
1469 * XXX We should use a per thread credential here; minimally, the
1470 * XXX process credential should have a persistent reference on it
1471 * XXX before being passed in here.
1473 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1481 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1482 (rpipe
->pipe_buffer
.cnt
> 0) ||
1483 (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1487 rpipe
->pipe_state
|= PIPE_SEL
;
1488 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1493 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1494 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1495 (wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1499 wpipe
->pipe_state
|= PIPE_SEL
;
1500 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1504 rpipe
->pipe_state
|= PIPE_SEL
;
1505 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1516 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1520 proc_fdlock_spin(vfs_context_proc(ctx
));
1521 cpipe
= (struct pipe
*)fg
->fg_data
;
1523 proc_fdunlock(vfs_context_proc(ctx
));
1532 pipe_free_kmem(struct pipe
*cpipe
)
1535 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1536 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1537 OSAddAtomic(-1, &nbigpipe
);
1538 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), &amountpipekva
);
1539 OSAddAtomic(-1, &amountpipes
);
1541 kmem_free(kernel_map
, (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1542 cpipe
->pipe_buffer
.size
);
1543 cpipe
->pipe_buffer
.buffer
= NULL
;
1545 #ifndef PIPE_NODIRECT
1546 if (cpipe
->pipe_map
.kva
!= 0) {
1547 atomic_subtract_int(&amountpipekvawired
,
1548 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1549 kmem_free(kernel_map
,
1550 cpipe
->pipe_map
.kva
,
1551 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1552 cpipe
->pipe_map
.cnt
= 0;
1553 cpipe
->pipe_map
.kva
= 0;
1554 cpipe
->pipe_map
.pos
= 0;
1555 cpipe
->pipe_map
.npages
= 0;
1564 pipeclose(struct pipe
*cpipe
)
1571 /* partially created pipes won't have a valid mutex. */
1572 if (PIPE_MTX(cpipe
) != NULL
)
1577 * If the other side is blocked, wake it up saying that
1578 * we want to close it down.
1580 cpipe
->pipe_state
&= ~PIPE_DRAIN
;
1581 cpipe
->pipe_state
|= PIPE_EOF
;
1582 pipeselwakeup(cpipe
, cpipe
);
1584 while (cpipe
->pipe_busy
) {
1585 cpipe
->pipe_state
|= PIPE_WANT
;
1588 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1593 * Free the shared pipe label only after the two ends are disconnected.
1595 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1596 mac_pipe_label_destroy(cpipe
);
1600 * Disconnect from peer
1602 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1604 ppipe
->pipe_state
&= ~(PIPE_DRAIN
);
1605 ppipe
->pipe_state
|= PIPE_EOF
;
1607 pipeselwakeup(ppipe
, ppipe
);
1610 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1611 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1613 postpipeevent(ppipe
, EV_RCLOSED
);
1615 ppipe
->pipe_peer
= NULL
;
1622 if (PIPE_MTX(cpipe
) != NULL
) {
1623 if (ppipe
!= NULL
) {
1625 * since the mutex is shared and the peer is still
1626 * alive, we need to release the mutex, not free it
1631 * peer is gone, so we're the sole party left with
1632 * interest in this mutex... we can just free it
1634 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1637 pipe_free_kmem(cpipe
);
1639 zfree(pipe_zone
, cpipe
);
1645 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
1649 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1654 * XXX We should use a per thread credential here; minimally, the
1655 * XXX process credential should have a persistent reference on it
1656 * XXX before being passed in here.
1658 if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx
), kn
, cpipe
) != 0) {
1664 switch (kn
->kn_filter
) {
1666 kn
->kn_fop
= &pipe_rfiltops
;
1670 kn
->kn_fop
= &pipe_wfiltops
;
1672 if (cpipe
->pipe_peer
== NULL
) {
1674 * other end of pipe has been closed
1679 if (cpipe
->pipe_peer
)
1680 cpipe
= cpipe
->pipe_peer
;
1687 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1688 cpipe
->pipe_state
|= PIPE_KNOTE
;
1695 filt_pipedetach(struct knote
*kn
)
1697 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1701 if (kn
->kn_filter
== EVFILT_WRITE
) {
1702 if (cpipe
->pipe_peer
== NULL
) {
1706 cpipe
= cpipe
->pipe_peer
;
1708 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1709 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1710 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1717 filt_piperead(struct knote
*kn
, long hint
)
1719 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1724 * if hint == 0, then we've been called from the kevent
1725 * world directly and do not currently hold the pipe mutex...
1726 * if hint == 1, we're being called back via the KNOTE post
1727 * we made in pipeselwakeup, and we already hold the mutex...
1732 wpipe
= rpipe
->pipe_peer
;
1733 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1735 #ifndef PIPE_NODIRECT
1736 if ((kn
->kn_data
== 0) && (rpipe
->pipe_state
& PIPE_DIRECTW
))
1737 kn
->kn_data
= rpipe
->pipe_map
.cnt
;
1739 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1740 (wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1741 kn
->kn_flags
|= EV_EOF
;
1744 retval
= (kn
->kn_sfflags
& NOTE_LOWAT
) ?
1745 (kn
->kn_data
>= kn
->kn_sdata
) : (kn
->kn_data
> 0);
1756 filt_pipewrite(struct knote
*kn
, long hint
)
1758 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1762 * if hint == 0, then we've been called from the kevent
1763 * world directly and do not currently hold the pipe mutex...
1764 * if hint == 1, we're being called back via the KNOTE post
1765 * we made in pipeselwakeup, and we already hold the mutex...
1770 wpipe
= rpipe
->pipe_peer
;
1772 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1774 kn
->kn_flags
|= EV_EOF
;
1780 kn
->kn_data
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1781 if (!kn
->kn_data
&& wpipe
->pipe_buffer
.size
== 0)
1782 kn
->kn_data
= 1; /* unwritten pipe is ready for write */
1784 #ifndef PIPE_NODIRECT
1785 if (wpipe
->pipe_state
& PIPE_DIRECTW
)
1791 return (kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
1792 kn
->kn_sdata
: PIPE_BUF
));
1796 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1802 struct vinfo_stat
* ub
;
1811 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1817 if (cpipe
->pipe_buffer
.buffer
== 0) {
1819 * must be stat'ing the write fd
1821 if (cpipe
->pipe_peer
) {
1823 * the peer still exists, use it's info
1825 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
1826 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1831 pipe_size
= cpipe
->pipe_buffer
.size
;
1832 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1835 * since peer's buffer is setup ouside of lock
1836 * we might catch it in transient state
1839 pipe_size
= PIPE_SIZE
;
1841 ub
= &pinfo
->pipe_stat
;
1843 bzero(ub
, sizeof(*ub
));
1844 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1845 ub
->vst_blksize
= pipe_size
;
1846 ub
->vst_size
= pipe_count
;
1847 if (ub
->vst_blksize
!= 0)
1848 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1851 ub
->vst_uid
= kauth_getuid();
1852 ub
->vst_gid
= kauth_getgid();
1855 ub
->vst_atime
= now
.tv_sec
;
1856 ub
->vst_atimensec
= now
.tv_usec
* 1000;
1858 ub
->vst_mtime
= now
.tv_sec
;
1859 ub
->vst_mtimensec
= now
.tv_usec
* 1000;
1861 ub
->vst_ctime
= now
.tv_sec
;
1862 ub
->vst_ctimensec
= now
.tv_usec
* 1000;
1865 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1866 * XXX (st_dev, st_ino) should be unique.
1869 pinfo
->pipe_handle
= (uint64_t)((uintptr_t)cpipe
);
1870 pinfo
->pipe_peerhandle
= (uint64_t)((uintptr_t)(cpipe
->pipe_peer
));
1871 pinfo
->pipe_status
= cpipe
->pipe_state
;
1880 pipe_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
1883 /* Note: fdlock already held */
1884 struct pipe
*ppipe
, *cpipe
= (struct pipe
*)(fp
->f_fglob
->fg_data
);
1888 cpipe
->pipe_state
|= PIPE_DRAIN
;
1889 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1892 /* Must wake up peer: a writer sleeps on the read side */
1893 if ((ppipe
= cpipe
->pipe_peer
)) {
1894 ppipe
->pipe_state
|= PIPE_DRAIN
;
1895 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);