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-2020 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
59 * Pipes are implemented as circular buffers. Following are the valid states in pipes operations
61 * _________________________________
62 * 1. |_________________________________| r=w, c=0
64 * _________________________________
65 * 2. |__r:::::wc_______________________| r <= w , c > 0
67 * _________________________________
68 * 3. |::::wc_____r:::::::::::::::::::::| r>w , c > 0
70 * _________________________________
71 * 4. |:::::::wrc:::::::::::::::::::::::| w=r, c = Max size
75 * a-z define the steps in a program flow
76 * 1-4 are the states as defined aboe
77 * Action: is what file operation is done on the pipe
79 * Current:None Action: initialize with size M=200
80 * a. State 1 ( r=0, w=0, c=0)
82 * Current: a Action: write(100) (w < M)
83 * b. State 2 (r=0, w=100, c=100)
85 * Current: b Action: write(100) (w = M-w)
86 * c. State 4 (r=0,w=0,c=200)
88 * Current: b Action: read(70) ( r < c )
89 * d. State 2(r=70,w=100,c=30)
91 * Current: d Action: write(75) ( w < (m-w))
92 * e. State 2 (r=70,w=175,c=105)
94 * Current: d Action: write(110) ( w > (m-w))
95 * f. State 3 (r=70,w=10,c=140)
97 * Current: d Action: read(30) (r >= c )
98 * g. State 1 (r=100,w=100,c=0)
103 * This code create half duplex pipe buffers for facilitating file like
104 * operations on pipes. The initial buffer is very small, but this can
105 * dynamically change to larger sizes based on usage. The buffer size is never
106 * reduced. The total amount of kernel memory used is governed by maxpipekva.
107 * In case of dynamic expansion limit is reached, the output thread is blocked
108 * until the pipe buffer empties enough to continue.
110 * In order to limit the resource use of pipes, two sysctls exist:
112 * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable
113 * address space available to us in pipe_map.
115 * Memory usage may be monitored through the sysctls
116 * kern.ipc.pipes, kern.ipc.pipekva.
120 #include <sys/param.h>
121 #include <sys/systm.h>
122 #include <sys/filedesc.h>
123 #include <sys/kernel.h>
124 #include <sys/vnode.h>
125 #include <sys/proc_internal.h>
126 #include <sys/kauth.h>
127 #include <sys/file_internal.h>
128 #include <sys/stat.h>
129 #include <sys/ioctl.h>
130 #include <sys/fcntl.h>
131 #include <sys/malloc.h>
132 #include <sys/syslog.h>
133 #include <sys/unistd.h>
134 #include <sys/resourcevar.h>
135 #include <sys/aio_kern.h>
136 #include <sys/signalvar.h>
137 #include <sys/pipe.h>
138 #include <sys/sysproto.h>
139 #include <sys/proc_info.h>
141 #include <security/audit/audit.h>
143 #include <sys/kdebug.h>
145 #include <kern/zalloc.h>
146 #include <kern/kalloc.h>
147 #include <vm/vm_kern.h>
148 #include <libkern/OSAtomic.h>
149 #include <libkern/section_keywords.h>
152 #include <security/mac_framework.h>
155 #define f_flag fp_glob->fg_flag
156 #define f_ops fp_glob->fg_ops
157 #define f_data fp_glob->fg_data
161 struct pipe pp_rpipe
;
162 struct pipe pp_wpipe
;
163 uint64_t pp_pipe_id
; /* unique ID shared by both pipe ends */
166 #define PIPE_PAIR(pipe) \
167 __container_of(PIPE_MTX(pipe), struct pipepair, pp_mtx)
170 * interfaces to the outside world exported through file operations
172 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
173 int flags
, vfs_context_t ctx
);
174 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
175 int flags
, vfs_context_t ctx
);
176 static int pipe_close(struct fileglob
*fg
, vfs_context_t ctx
);
177 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
,
179 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
180 struct kevent_qos_s
*kev
);
181 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
183 static int pipe_drain(struct fileproc
*fp
, vfs_context_t ctx
);
185 static const struct fileops pipeops
= {
186 .fo_type
= DTYPE_PIPE
,
187 .fo_read
= pipe_read
,
188 .fo_write
= pipe_write
,
189 .fo_ioctl
= pipe_ioctl
,
190 .fo_select
= pipe_select
,
191 .fo_close
= pipe_close
,
192 .fo_drain
= pipe_drain
,
193 .fo_kqfilter
= pipe_kqfilter
,
196 static void filt_pipedetach(struct knote
*kn
);
198 static int filt_pipenotsup(struct knote
*kn
, long hint
);
199 static int filt_pipenotsuptouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
200 static int filt_pipenotsupprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
202 static int filt_piperead(struct knote
*kn
, long hint
);
203 static int filt_pipereadtouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
204 static int filt_pipereadprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
206 static int filt_pipewrite(struct knote
*kn
, long hint
);
207 static int filt_pipewritetouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
208 static int filt_pipewriteprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
210 SECURITY_READ_ONLY_EARLY(struct filterops
) pipe_nfiltops
= {
212 .f_detach
= filt_pipedetach
,
213 .f_event
= filt_pipenotsup
,
214 .f_touch
= filt_pipenotsuptouch
,
215 .f_process
= filt_pipenotsupprocess
,
218 SECURITY_READ_ONLY_EARLY(struct filterops
) pipe_rfiltops
= {
220 .f_detach
= filt_pipedetach
,
221 .f_event
= filt_piperead
,
222 .f_touch
= filt_pipereadtouch
,
223 .f_process
= filt_pipereadprocess
,
226 SECURITY_READ_ONLY_EARLY(struct filterops
) pipe_wfiltops
= {
228 .f_detach
= filt_pipedetach
,
229 .f_event
= filt_pipewrite
,
230 .f_touch
= filt_pipewritetouch
,
231 .f_process
= filt_pipewriteprocess
,
235 static int nbigpipe
; /* for compatibility sake. no longer used */
237 static int amountpipes
; /* total number of pipes in system */
238 static int amountpipekva
; /* total memory used by pipes */
240 static _Atomic
uint64_t pipe_unique_id
= 1;
242 int maxpipekva
__attribute__((used
)) = PIPE_KVAMAX
; /* allowing 16MB max. */
245 SYSCTL_DECL(_kern_ipc
);
247 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
248 &maxpipekva
, 0, "Pipe KVA limit");
249 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
250 &maxpipekvawired
, 0, "Pipe KVA wired limit");
251 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
252 &amountpipes
, 0, "Current # of pipes");
253 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
254 &nbigpipe
, 0, "Current # of big pipes");
255 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
256 &amountpipekva
, 0, "Pipe KVA usage");
257 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
258 &amountpipekvawired
, 0, "Pipe wired KVA usage");
261 static int pipepair_alloc(struct pipe
**rpipe
, struct pipe
**wpipe
);
262 static void pipeclose(struct pipe
*cpipe
);
263 static void pipe_free_kmem(struct pipe
*cpipe
);
264 static int pipespace(struct pipe
*cpipe
, int size
);
265 static int choose_pipespace(unsigned long current
, unsigned long expected
);
266 static int expand_pipespace(struct pipe
*p
, int target_size
);
267 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
268 static __inline
int pipeio_lock(struct pipe
*cpipe
, int catch);
269 static __inline
void pipeio_unlock(struct pipe
*cpipe
);
271 static LCK_GRP_DECLARE(pipe_mtx_grp
, "pipe");
272 static ZONE_DECLARE(pipe_zone
, "pipe zone", sizeof(struct pipepair
), ZC_NONE
);
274 #define MAX_PIPESIZE(pipe) ( MAX(PIPE_SIZE, (pipe)->pipe_buffer.size) )
276 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
278 #if defined(XNU_TARGET_OS_OSX)
279 /* Bitmap for things to touch in pipe_touch() */
280 #define PIPE_ATIME 0x00000001 /* time of last access */
281 #define PIPE_MTIME 0x00000002 /* time of last modification */
282 #define PIPE_CTIME 0x00000004 /* time of last status change */
285 pipe_touch(struct pipe
*tpipe
, int touch
)
291 if (touch
& PIPE_ATIME
) {
292 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
293 tpipe
->st_atimespec
.tv_nsec
= now
.tv_nsec
;
296 if (touch
& PIPE_MTIME
) {
297 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
298 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_nsec
;
301 if (touch
& PIPE_CTIME
) {
302 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
303 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_nsec
;
308 static const unsigned int pipesize_blocks
[] = {512, 1024, 2048, 4096, 4096 * 2, PIPE_SIZE
, PIPE_SIZE
* 4 };
311 * finds the right size from possible sizes in pipesize_blocks
312 * returns the size which matches max(current,expected)
315 choose_pipespace(unsigned long current
, unsigned long expected
)
317 int i
= sizeof(pipesize_blocks
) / sizeof(unsigned int) - 1;
318 unsigned long target
;
321 * assert that we always get an atomic transaction sized pipe buffer,
322 * even if the system pipe buffer high-water mark has been crossed.
324 assert(PIPE_BUF
== pipesize_blocks
[0]);
326 if (expected
> current
) {
332 while (i
> 0 && pipesize_blocks
[i
- 1] > target
) {
336 return pipesize_blocks
[i
];
341 * expand the size of pipe while there is data to be read,
342 * and then free the old buffer once the current buffered
343 * data has been transferred to new storage.
344 * Required: PIPE_LOCK and io lock to be held by caller.
345 * returns 0 on success or no expansion possible
348 expand_pipespace(struct pipe
*p
, int target_size
)
350 struct pipe tmp
, oldpipe
;
352 tmp
.pipe_buffer
.buffer
= 0;
354 if (p
->pipe_buffer
.size
>= (unsigned) target_size
) {
355 return 0; /* the existing buffer is max size possible */
358 /* create enough space in the target */
359 error
= pipespace(&tmp
, target_size
);
364 oldpipe
.pipe_buffer
.buffer
= p
->pipe_buffer
.buffer
;
365 oldpipe
.pipe_buffer
.size
= p
->pipe_buffer
.size
;
367 memcpy(tmp
.pipe_buffer
.buffer
, p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
368 if (p
->pipe_buffer
.cnt
> 0 && p
->pipe_buffer
.in
<= p
->pipe_buffer
.out
) {
369 /* we are in State 3 and need extra copying for read to be consistent */
370 memcpy(&tmp
.pipe_buffer
.buffer
[p
->pipe_buffer
.size
], p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
371 p
->pipe_buffer
.in
+= p
->pipe_buffer
.size
;
374 p
->pipe_buffer
.buffer
= tmp
.pipe_buffer
.buffer
;
375 p
->pipe_buffer
.size
= tmp
.pipe_buffer
.size
;
378 pipe_free_kmem(&oldpipe
);
383 * The pipe system call for the DTYPE_PIPE type of pipes
386 * FREAD | fd0 | -->[struct rpipe] --> |~~buffer~~| \
388 * FWRITE | fd1 | -->[struct wpipe] --X /
393 pipe(proc_t p
, __unused
struct pipe_args
*uap
, int32_t *retval
)
395 struct fileproc
*rf
, *wf
;
396 struct pipe
*rpipe
, *wpipe
;
399 error
= pipepair_alloc(&rpipe
, &wpipe
);
405 * for now we'll create half-duplex pipes(refer returns section above).
406 * this is what we've always supported..
409 error
= falloc(p
, &rf
, &retval
[0], vfs_context_current());
414 rf
->f_data
= (caddr_t
)rpipe
;
415 rf
->f_ops
= &pipeops
;
417 error
= falloc(p
, &wf
, &retval
[1], vfs_context_current());
419 fp_free(p
, retval
[0], rf
);
423 wf
->f_data
= (caddr_t
)wpipe
;
424 wf
->f_ops
= &pipeops
;
426 rpipe
->pipe_peer
= wpipe
;
427 wpipe
->pipe_peer
= rpipe
;
431 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
433 * struct pipe represents a pipe endpoint. The MAC label is shared
434 * between the connected endpoints. As a result mac_pipe_label_init() and
435 * mac_pipe_label_associate() should only be called on one of the endpoints
436 * after they have been connected.
438 mac_pipe_label_init(rpipe
);
439 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
440 wpipe
->pipe_label
= rpipe
->pipe_label
;
443 procfdtbl_releasefd(p
, retval
[0], NULL
);
444 procfdtbl_releasefd(p
, retval
[1], NULL
);
445 fp_drop(p
, retval
[0], rf
, 1);
446 fp_drop(p
, retval
[1], wf
, 1);
457 pipe_stat(struct pipe
*cpipe
, void *ub
, int isstat64
)
464 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
465 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
473 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
479 if (cpipe
->pipe_buffer
.buffer
== 0) {
480 /* must be stat'ing the write fd */
481 if (cpipe
->pipe_peer
) {
482 /* the peer still exists, use it's info */
483 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
484 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
489 pipe_size
= MAX_PIPESIZE(cpipe
);
490 pipe_count
= cpipe
->pipe_buffer
.cnt
;
493 * since peer's buffer is setup ouside of lock
494 * we might catch it in transient state
496 if (pipe_size
== 0) {
497 pipe_size
= MAX(PIPE_SIZE
, pipesize_blocks
[0]);
501 sb64
= (struct stat64
*)ub
;
503 bzero(sb64
, sizeof(*sb64
));
504 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
505 sb64
->st_blksize
= pipe_size
;
506 sb64
->st_size
= pipe_count
;
507 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
509 sb64
->st_uid
= kauth_getuid();
510 sb64
->st_gid
= kauth_getgid();
512 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
513 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
515 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
516 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
518 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
519 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
522 * Return a relatively unique inode number based on the current
523 * address of this pipe's struct pipe. This number may be recycled
524 * relatively quickly.
526 sb64
->st_ino
= (ino64_t
)VM_KERNEL_ADDRHASH((uintptr_t)cpipe
);
528 sb
= (struct stat
*)ub
;
530 bzero(sb
, sizeof(*sb
));
531 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
532 sb
->st_blksize
= pipe_size
;
533 sb
->st_size
= pipe_count
;
534 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
536 sb
->st_uid
= kauth_getuid();
537 sb
->st_gid
= kauth_getgid();
539 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
540 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
542 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
543 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
545 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
546 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
549 * Return a relatively unique inode number based on the current
550 * address of this pipe's struct pipe. This number may be recycled
551 * relatively quickly.
553 sb
->st_ino
= (ino_t
)VM_KERNEL_ADDRHASH((uintptr_t)cpipe
);
558 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
561 * XXX (st_dev) should be unique, but there is no device driver that
562 * XXX is associated with pipes, since they are implemented via a
563 * XXX struct fileops indirection rather than as FS objects.
569 pipe_id(struct pipe
*p
)
571 return PIPE_PAIR(p
)->pp_pipe_id
;
575 * Allocate kva for pipe circular buffer, the space is pageable
576 * This routine will 'realloc' the size of a pipe safely, if it fails
577 * it will retain the old buffer.
578 * If it fails it will return ENOMEM.
581 pipespace(struct pipe
*cpipe
, int size
)
589 buffer
= (vm_offset_t
)kheap_alloc(KHEAP_DATA_BUFFERS
, size
, Z_WAITOK
);
594 /* free old resources if we're resizing */
595 pipe_free_kmem(cpipe
);
596 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
597 cpipe
->pipe_buffer
.size
= size
;
598 cpipe
->pipe_buffer
.in
= 0;
599 cpipe
->pipe_buffer
.out
= 0;
600 cpipe
->pipe_buffer
.cnt
= 0;
602 OSAddAtomic(1, &amountpipes
);
603 OSAddAtomic(cpipe
->pipe_buffer
.size
, &amountpipekva
);
609 * initialize and allocate VM and memory for pipe
612 pipepair_alloc(struct pipe
**rp_out
, struct pipe
**wp_out
)
614 struct pipepair
*pp
= zalloc(pipe_zone
);
615 struct pipe
*rpipe
= &pp
->pp_rpipe
;
616 struct pipe
*wpipe
= &pp
->pp_wpipe
;
623 * protect so pipespace or pipeclose don't follow a junk pointer
624 * if pipespace() fails.
626 bzero(pp
, sizeof(struct pipepair
));
627 pp
->pp_pipe_id
= os_atomic_inc_orig(&pipe_unique_id
, relaxed
);
628 lck_mtx_init(&pp
->pp_mtx
, &pipe_mtx_grp
, LCK_ATTR_NULL
);
630 rpipe
->pipe_mtxp
= &pp
->pp_mtx
;
631 wpipe
->pipe_mtxp
= &pp
->pp_mtx
;
633 #if defined(XNU_TARGET_OS_OSX)
634 /* Initial times are all the time of creation of the pipe */
635 pipe_touch(rpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
636 pipe_touch(wpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
640 * allocate the space for the normal I/O direction up
641 * front... we'll delay the allocation for the other
642 * direction until a write actually occurs (most likely it won't)...
644 int error
= pipespace(rpipe
, choose_pipespace(rpipe
->pipe_buffer
.size
, 0));
645 if (__improbable(error
)) {
646 lck_mtx_destroy(&pp
->pp_mtx
, &pipe_mtx_grp
);
647 zfree(pipe_zone
, pp
);
657 pipepair_destroy_pipe(struct pipepair
*pp
, struct pipe
*cpipe
)
661 pipe_free_kmem(cpipe
);
663 lck_mtx_lock(&pp
->pp_mtx
);
664 if (__improbable(cpipe
->pipe_state
& PIPE_DEAD
)) {
665 panic("double free of pipe %p in pair %p", cpipe
, pp
);
668 cpipe
->pipe_state
|= PIPE_DEAD
;
670 can_free
= (pp
->pp_rpipe
.pipe_state
& PIPE_DEAD
) &&
671 (pp
->pp_wpipe
.pipe_state
& PIPE_DEAD
);
672 lck_mtx_unlock(&pp
->pp_mtx
);
675 lck_mtx_destroy(&pp
->pp_mtx
, &pipe_mtx_grp
);
676 zfree(pipe_zone
, pp
);
681 * lock a pipe for I/O, blocking other access
684 pipeio_lock(struct pipe
*cpipe
, int catch)
687 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
688 cpipe
->pipe_state
|= PIPE_LWANT
;
689 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
695 cpipe
->pipe_state
|= PIPE_LOCKFL
;
700 * unlock a pipe I/O lock
703 pipeio_unlock(struct pipe
*cpipe
)
705 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
706 if (cpipe
->pipe_state
& PIPE_LWANT
) {
707 cpipe
->pipe_state
&= ~PIPE_LWANT
;
713 * wakeup anyone whos blocked in select
716 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
718 if (cpipe
->pipe_state
& PIPE_SEL
) {
719 cpipe
->pipe_state
&= ~PIPE_SEL
;
720 selwakeup(&cpipe
->pipe_sel
);
723 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
725 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
726 if (spipe
->pipe_pgid
< 0) {
727 gsignal(-spipe
->pipe_pgid
, SIGIO
);
729 proc_signal(spipe
->pipe_pgid
, SIGIO
);
735 * Read n bytes from the buffer. Semantics are similar to file read.
736 * returns: number of bytes read from the buffer
740 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
741 __unused vfs_context_t ctx
)
743 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
751 error
= pipeio_lock(rpipe
, 1);
757 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
764 while (uio_resid(uio
)) {
766 * normal pipe buffer receive
768 if (rpipe
->pipe_buffer
.cnt
> 0) {
770 * # bytes to read is min( bytes from read pointer until end of buffer,
771 * total unread bytes,
772 * user requested byte count)
774 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
775 if (size
> rpipe
->pipe_buffer
.cnt
) {
776 size
= rpipe
->pipe_buffer
.cnt
;
779 size
= (u_int
) MIN(INT_MAX
, MIN((user_size_t
)size
,
780 (user_size_t
)uio_resid(uio
)));
782 PIPE_UNLOCK(rpipe
); /* we still hold io lock.*/
784 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
791 rpipe
->pipe_buffer
.out
+= size
;
792 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
) {
793 rpipe
->pipe_buffer
.out
= 0;
796 rpipe
->pipe_buffer
.cnt
-= size
;
799 * If there is no more to read in the pipe, reset
800 * its pointers to the beginning. This improves
803 if (rpipe
->pipe_buffer
.cnt
== 0) {
804 rpipe
->pipe_buffer
.in
= 0;
805 rpipe
->pipe_buffer
.out
= 0;
810 * detect EOF condition
811 * read returns 0 on EOF, no need to set error
813 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
814 (fileproc_get_vflags(fp
) & FPV_DRAIN
)) {
819 * If the "write-side" has been blocked, wake it up now.
821 if (rpipe
->pipe_state
& PIPE_WANTW
) {
822 rpipe
->pipe_state
&= ~PIPE_WANTW
;
827 * Break if some data was read in previous iteration.
834 * Unlock the pipe buffer for our remaining processing.
835 * We will either break out with an error or we will
836 * sleep and relock to loop.
838 pipeio_unlock(rpipe
);
841 * Handle non-blocking mode operation or
842 * wait for more data.
844 if (fp
->f_flag
& FNONBLOCK
) {
847 rpipe
->pipe_state
|= PIPE_WANTR
;
848 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
850 error
= pipeio_lock(rpipe
, 1);
861 pipeio_unlock(rpipe
);
867 * PIPE_WANT processing only makes sense if pipe_busy is 0.
869 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
870 rpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTW
);
872 } else if (rpipe
->pipe_buffer
.cnt
< rpipe
->pipe_buffer
.size
) {
874 * Handle write blocking hysteresis.
876 if (rpipe
->pipe_state
& PIPE_WANTW
) {
877 rpipe
->pipe_state
&= ~PIPE_WANTW
;
882 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) > 0) {
883 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
886 #if defined(XNU_TARGET_OS_OSX)
887 /* update last read time */
888 pipe_touch(rpipe
, PIPE_ATIME
);
897 * perform a write of n bytes into the read side of buffer. Since
898 * pipes are unidirectional a write is meant to be read by the otherside only.
901 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
902 __unused vfs_context_t ctx
)
907 struct pipe
*wpipe
, *rpipe
;
908 // LP64todo - fix this!
909 orig_resid
= (size_t)uio_resid(uio
);
910 if (orig_resid
> LONG_MAX
) {
915 rpipe
= (struct pipe
*)fp
->f_data
;
918 wpipe
= rpipe
->pipe_peer
;
921 * detect loss of pipe read side, issue SIGPIPE if lost.
923 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
924 (fileproc_get_vflags(fp
) & FPV_DRAIN
)) {
929 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
940 * need to allocate some storage... we delay the allocation
941 * until the first write on fd[0] to avoid allocating storage for both
942 * 'pipe ends'... most pipes are half-duplex with the writes targeting
943 * fd[1], so allocating space for both ends is a waste...
946 if (wpipe
->pipe_buffer
.buffer
== 0 || (
947 (unsigned)orig_resid
> wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
&&
948 amountpipekva
< maxpipekva
)) {
949 pipe_size
= choose_pipespace(wpipe
->pipe_buffer
.size
, wpipe
->pipe_buffer
.cnt
+ orig_resid
);
953 * need to do initial allocation or resizing of pipe
954 * holding both structure and io locks.
956 if ((error
= pipeio_lock(wpipe
, 1)) == 0) {
957 if (wpipe
->pipe_buffer
.cnt
== 0) {
958 error
= pipespace(wpipe
, pipe_size
);
960 error
= expand_pipespace(wpipe
, pipe_size
);
963 pipeio_unlock(wpipe
);
965 /* allocation failed */
966 if (wpipe
->pipe_buffer
.buffer
== 0) {
972 * If an error occurred unbusy and return, waking up any pending
976 if ((wpipe
->pipe_busy
== 0) &&
977 (wpipe
->pipe_state
& PIPE_WANT
)) {
978 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
986 while (uio_resid(uio
)) {
988 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
990 /* Writes of size <= PIPE_BUF must be atomic. */
991 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
)) {
996 if ((error
= pipeio_lock(wpipe
, 1)) == 0) {
997 size_t size
; /* Transfer size */
998 size_t segsize
; /* first segment to transfer */
1000 if ((wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1001 (fileproc_get_vflags(fp
) & FPV_DRAIN
)) {
1002 pipeio_unlock(wpipe
);
1007 * If a process blocked in pipeio_lock, our
1008 * value for space might be bad... the mutex
1009 * is dropped while we're blocked
1011 if (space
> (int)(wpipe
->pipe_buffer
.size
-
1012 wpipe
->pipe_buffer
.cnt
)) {
1013 pipeio_unlock(wpipe
);
1018 * Transfer size is minimum of uio transfer
1019 * and free space in pipe buffer.
1021 // LP64todo - fix this!
1022 if (space
> uio_resid(uio
)) {
1023 size
= (size_t)uio_resid(uio
);
1024 if (size
> LONG_MAX
) {
1025 panic("size greater than LONG_MAX");
1031 * First segment to transfer is minimum of
1032 * transfer size and contiguous space in
1033 * pipe buffer. If first segment to transfer
1034 * is less than the transfer size, we've got
1035 * a wraparound in the buffer.
1037 segsize
= wpipe
->pipe_buffer
.size
-
1038 wpipe
->pipe_buffer
.in
;
1039 if (segsize
> size
) {
1043 /* Transfer first segment */
1046 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1050 if (error
== 0 && segsize
< size
) {
1052 * Transfer remaining part now, to
1053 * support atomic writes. Wraparound
1054 * happened. (State 3)
1056 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1057 wpipe
->pipe_buffer
.size
) {
1058 panic("Expected pipe buffer "
1059 "wraparound disappeared");
1064 &wpipe
->pipe_buffer
.buffer
[0],
1065 (int)(size
- segsize
), uio
);
1069 * readers never know to read until count is updated.
1072 wpipe
->pipe_buffer
.in
+= size
;
1073 if (wpipe
->pipe_buffer
.in
>
1074 wpipe
->pipe_buffer
.size
) {
1075 if (wpipe
->pipe_buffer
.in
!=
1077 wpipe
->pipe_buffer
.size
) {
1081 wpipe
->pipe_buffer
.in
= (unsigned int)(size
-
1085 wpipe
->pipe_buffer
.cnt
+= size
;
1086 if (wpipe
->pipe_buffer
.cnt
>
1087 wpipe
->pipe_buffer
.size
) {
1088 panic("Pipe buffer overflow");
1091 pipeio_unlock(wpipe
);
1098 * If the "read-side" has been blocked, wake it up now.
1100 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1101 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1106 * If read side wants to go away, we just issue a signal
1109 if ((wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1110 (fileproc_get_vflags(fp
) & FPV_DRAIN
)) {
1116 * don't block on non-blocking I/O
1117 * we'll do the pipeselwakeup on the way out
1119 if (fp
->f_flag
& FNONBLOCK
) {
1125 * We have no more space and have something to offer,
1126 * wake up select/poll.
1128 pipeselwakeup(wpipe
, wpipe
);
1130 wpipe
->pipe_state
|= PIPE_WANTW
;
1132 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1141 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1142 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1145 if (wpipe
->pipe_buffer
.cnt
> 0) {
1147 * If there are any characters in the buffer, we wake up
1148 * the reader if it was blocked waiting for data.
1150 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1151 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1155 * wake up thread blocked in select/poll or post the notification
1157 pipeselwakeup(wpipe
, wpipe
);
1160 #if defined(XNU_TARGET_OS_OSX)
1161 /* Update modification, status change (# of bytes in pipe) times */
1162 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1163 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1171 * we implement a very minimal set of ioctls for compatibility with sockets.
1175 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1176 __unused vfs_context_t ctx
)
1178 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1186 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1201 mpipe
->pipe_state
|= PIPE_ASYNC
;
1203 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1209 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1214 mpipe
->pipe_pgid
= *(int *)data
;
1220 *(int *)data
= mpipe
->pipe_pgid
;
1231 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1233 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1237 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1) {
1243 wpipe
= rpipe
->pipe_peer
;
1248 * XXX We should use a per thread credential here; minimally, the
1249 * XXX process credential should have a persistent reference on it
1250 * XXX before being passed in here.
1252 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1259 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1260 (rpipe
->pipe_buffer
.cnt
> 0) ||
1261 (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1262 (fileproc_get_vflags(fp
) & FPV_DRAIN
)) {
1265 rpipe
->pipe_state
|= PIPE_SEL
;
1266 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1272 wpipe
->pipe_state
|= PIPE_WSELECT
;
1274 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1275 (fileproc_get_vflags(fp
) & FPV_DRAIN
) ||
1276 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1277 (MAX_PIPESIZE(wpipe
) - wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1280 wpipe
->pipe_state
|= PIPE_SEL
;
1281 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1285 rpipe
->pipe_state
|= PIPE_SEL
;
1286 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1297 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1301 proc_fdlock_spin(vfs_context_proc(ctx
));
1302 cpipe
= (struct pipe
*)fg
->fg_data
;
1304 proc_fdunlock(vfs_context_proc(ctx
));
1313 pipe_free_kmem(struct pipe
*cpipe
)
1315 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1316 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), &amountpipekva
);
1317 OSAddAtomic(-1, &amountpipes
);
1318 kheap_free(KHEAP_DATA_BUFFERS
, cpipe
->pipe_buffer
.buffer
,
1319 cpipe
->pipe_buffer
.size
);
1320 cpipe
->pipe_buffer
.buffer
= NULL
;
1321 cpipe
->pipe_buffer
.size
= 0;
1329 pipeclose(struct pipe
*cpipe
)
1336 * If the other side is blocked, wake it up saying that
1337 * we want to close it down.
1339 cpipe
->pipe_state
&= ~PIPE_DRAIN
;
1340 cpipe
->pipe_state
|= PIPE_EOF
;
1341 pipeselwakeup(cpipe
, cpipe
);
1343 while (cpipe
->pipe_busy
) {
1344 cpipe
->pipe_state
|= PIPE_WANT
;
1347 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1352 * Free the shared pipe label only after the two ends are disconnected.
1354 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
) {
1355 mac_pipe_label_destroy(cpipe
);
1360 * Disconnect from peer
1362 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1363 ppipe
->pipe_state
&= ~(PIPE_DRAIN
);
1364 ppipe
->pipe_state
|= PIPE_EOF
;
1366 pipeselwakeup(ppipe
, ppipe
);
1369 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1371 ppipe
->pipe_peer
= NULL
;
1380 pipepair_destroy_pipe(PIPE_PAIR(cpipe
), cpipe
);
1384 filt_pipelowwat(struct knote
*kn
, struct pipe
*rpipe
, int64_t def_lowwat
)
1386 if ((kn
->kn_sfflags
& NOTE_LOWAT
) == 0) {
1389 if (rpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> MAX_PIPESIZE(rpipe
)) {
1390 return MAX_PIPESIZE(rpipe
);
1392 return MAX(kn
->kn_sdata
, def_lowwat
);
1396 filt_pipe_draincommon(struct knote
*kn
, struct pipe
*rpipe
)
1398 struct pipe
*wpipe
= rpipe
->pipe_peer
;
1400 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1401 (wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1402 kn
->kn_flags
|= EV_EOF
;
1410 filt_pipenotsup(struct knote
*kn
, long hint
)
1412 #pragma unused(hint)
1413 struct pipe
*rpipe
= kn
->kn_hook
;
1415 return filt_pipe_draincommon(kn
, rpipe
);
1419 filt_pipenotsuptouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
1421 struct pipe
*rpipe
= kn
->kn_hook
;
1426 /* accept new kevent data (and save off lowat threshold and flag) */
1427 kn
->kn_sfflags
= kev
->fflags
;
1428 kn
->kn_sdata
= kev
->data
;
1430 /* determine if any event is now deemed fired */
1431 res
= filt_pipe_draincommon(kn
, rpipe
);
1439 filt_pipenotsupprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
1441 struct pipe
*rpipe
= kn
->kn_hook
;
1445 res
= filt_pipe_draincommon(kn
, rpipe
);
1447 knote_fill_kevent(kn
, kev
, 0);
1456 filt_piperead_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct pipe
*rpipe
)
1458 int64_t data
= rpipe
->pipe_buffer
.cnt
;
1461 if (filt_pipe_draincommon(kn
, rpipe
)) {
1464 res
= data
>= filt_pipelowwat(kn
, rpipe
, 1);
1467 knote_fill_kevent(kn
, kev
, data
);
1473 filt_piperead(struct knote
*kn
, long hint
)
1475 #pragma unused(hint)
1476 struct pipe
*rpipe
= kn
->kn_hook
;
1478 return filt_piperead_common(kn
, NULL
, rpipe
);
1482 filt_pipereadtouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
1484 struct pipe
*rpipe
= kn
->kn_hook
;
1489 /* accept new inputs (and save the low water threshold and flag) */
1490 kn
->kn_sdata
= kev
->data
;
1491 kn
->kn_sfflags
= kev
->fflags
;
1493 /* identify if any events are now fired */
1494 retval
= filt_piperead_common(kn
, NULL
, rpipe
);
1502 filt_pipereadprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
1504 struct pipe
*rpipe
= kn
->kn_hook
;
1508 retval
= filt_piperead_common(kn
, kev
, rpipe
);
1516 filt_pipewrite_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct pipe
*rpipe
)
1521 if (filt_pipe_draincommon(kn
, rpipe
)) {
1524 data
= MAX_PIPESIZE(rpipe
) - rpipe
->pipe_buffer
.cnt
;
1525 res
= data
>= filt_pipelowwat(kn
, rpipe
, PIPE_BUF
);
1528 knote_fill_kevent(kn
, kev
, data
);
1535 filt_pipewrite(struct knote
*kn
, long hint
)
1537 #pragma unused(hint)
1538 struct pipe
*rpipe
= kn
->kn_hook
;
1540 return filt_pipewrite_common(kn
, NULL
, rpipe
);
1545 filt_pipewritetouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
1547 struct pipe
*rpipe
= kn
->kn_hook
;
1552 /* accept new kevent data (and save off lowat threshold and flag) */
1553 kn
->kn_sfflags
= kev
->fflags
;
1554 kn
->kn_sdata
= kev
->data
;
1556 /* determine if any event is now deemed fired */
1557 res
= filt_pipewrite_common(kn
, NULL
, rpipe
);
1565 filt_pipewriteprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
1567 struct pipe
*rpipe
= kn
->kn_hook
;
1571 res
= filt_pipewrite_common(kn
, kev
, rpipe
);
1579 pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
1580 __unused
struct kevent_qos_s
*kev
)
1582 struct pipe
*cpipe
= (struct pipe
*)fp
->f_data
;
1583 struct pipe
*rpipe
= &PIPE_PAIR(cpipe
)->pp_rpipe
;
1589 * XXX We should use a per thread credential here; minimally, the
1590 * XXX process credential should have a persistent reference on it
1591 * XXX before being passed in here.
1593 kauth_cred_t cred
= vfs_context_ucred(vfs_context_current());
1594 if (mac_pipe_check_kqfilter(cred
, kn
, cpipe
) != 0) {
1596 knote_set_error(kn
, EPERM
);
1602 * FreeBSD will fail the attach with EPIPE if the peer pipe is detached,
1603 * however, this isn't a programming error as the other side closing
1604 * could race with the kevent registration.
1606 * Attach should only fail for programming mistakes else it will break
1609 * Like FreeBSD, have a "Neutered" filter that will not fire until
1610 * the pipe dies if the wrong filter is attached to the wrong end.
1612 * Knotes are always attached to the "rpipe".
1614 switch (kn
->kn_filter
) {
1616 if (fp
->f_flag
& FREAD
) {
1617 kn
->kn_filtid
= EVFILTID_PIPE_R
;
1618 res
= filt_piperead_common(kn
, NULL
, rpipe
);
1620 kn
->kn_filtid
= EVFILTID_PIPE_N
;
1621 res
= filt_pipe_draincommon(kn
, rpipe
);
1626 if (fp
->f_flag
& FWRITE
) {
1627 kn
->kn_filtid
= EVFILTID_PIPE_W
;
1628 res
= filt_pipewrite_common(kn
, NULL
, rpipe
);
1630 kn
->kn_filtid
= EVFILTID_PIPE_N
;
1631 res
= filt_pipe_draincommon(kn
, rpipe
);
1637 knote_set_error(kn
, EINVAL
);
1641 kn
->kn_hook
= rpipe
;
1642 KNOTE_ATTACH(&rpipe
->pipe_sel
.si_note
, kn
);
1649 filt_pipedetach(struct knote
*kn
)
1651 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1652 struct pipe
*rpipe
= &PIPE_PAIR(cpipe
)->pp_rpipe
;
1655 KNOTE_DETACH(&rpipe
->pipe_sel
.si_note
, kn
);
1660 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1665 struct timespec now
;
1666 struct vinfo_stat
* ub
;
1670 if (cpipe
== NULL
) {
1676 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1682 if (cpipe
->pipe_buffer
.buffer
== 0) {
1684 * must be stat'ing the write fd
1686 if (cpipe
->pipe_peer
) {
1688 * the peer still exists, use it's info
1690 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
1691 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1696 pipe_size
= MAX_PIPESIZE(cpipe
);
1697 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1700 * since peer's buffer is setup ouside of lock
1701 * we might catch it in transient state
1703 if (pipe_size
== 0) {
1704 pipe_size
= PIPE_SIZE
;
1707 ub
= &pinfo
->pipe_stat
;
1709 bzero(ub
, sizeof(*ub
));
1710 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1711 ub
->vst_blksize
= pipe_size
;
1712 ub
->vst_size
= pipe_count
;
1713 if (ub
->vst_blksize
!= 0) {
1714 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1718 ub
->vst_uid
= kauth_getuid();
1719 ub
->vst_gid
= kauth_getgid();
1722 ub
->vst_atime
= now
.tv_sec
;
1723 ub
->vst_atimensec
= now
.tv_nsec
;
1725 ub
->vst_mtime
= now
.tv_sec
;
1726 ub
->vst_mtimensec
= now
.tv_nsec
;
1728 ub
->vst_ctime
= now
.tv_sec
;
1729 ub
->vst_ctimensec
= now
.tv_nsec
;
1732 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1733 * XXX (st_dev, st_ino) should be unique.
1736 pinfo
->pipe_handle
= (uint64_t)VM_KERNEL_ADDRHASH((uintptr_t)cpipe
);
1737 pinfo
->pipe_peerhandle
= (uint64_t)VM_KERNEL_ADDRHASH((uintptr_t)(cpipe
->pipe_peer
));
1738 pinfo
->pipe_status
= cpipe
->pipe_state
;
1747 pipe_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
1749 /* Note: fdlock already held */
1750 struct pipe
*ppipe
, *cpipe
= (struct pipe
*)(fp
->fp_glob
->fg_data
);
1751 boolean_t drain_pipe
= FALSE
;
1753 /* Check if the pipe is going away */
1754 lck_mtx_lock_spin(&fp
->fp_glob
->fg_lock
);
1755 if (os_ref_get_count_raw(&fp
->fp_glob
->fg_count
) == 1) {
1758 lck_mtx_unlock(&fp
->fp_glob
->fg_lock
);
1764 cpipe
->pipe_state
|= PIPE_DRAIN
;
1765 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1769 /* Must wake up peer: a writer sleeps on the read side */
1770 if ((ppipe
= cpipe
->pipe_peer
)) {
1772 ppipe
->pipe_state
|= PIPE_DRAIN
;
1773 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);