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-2014 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 f_fglob->fg_flag
156 #define f_msgcount f_fglob->fg_msgcount
157 #define f_cred f_fglob->fg_cred
158 #define f_ops f_fglob->fg_ops
159 #define f_offset f_fglob->fg_offset
160 #define f_data f_fglob->fg_data
163 * interfaces to the outside world exported through file operations
165 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
166 int flags
, vfs_context_t ctx
);
167 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
168 int flags
, vfs_context_t ctx
);
169 static int pipe_close(struct fileglob
*fg
, vfs_context_t ctx
);
170 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
,
172 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
173 struct kevent_internal_s
*kev
, vfs_context_t ctx
);
174 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
176 static int pipe_drain(struct fileproc
*fp
,vfs_context_t ctx
);
178 static const struct fileops pipeops
= {
179 .fo_type
= DTYPE_PIPE
,
180 .fo_read
= pipe_read
,
181 .fo_write
= pipe_write
,
182 .fo_ioctl
= pipe_ioctl
,
183 .fo_select
= pipe_select
,
184 .fo_close
= pipe_close
,
185 .fo_kqfilter
= pipe_kqfilter
,
186 .fo_drain
= pipe_drain
,
189 static void filt_pipedetach(struct knote
*kn
);
191 static int filt_piperead(struct knote
*kn
, long hint
);
192 static int filt_pipereadtouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
193 static int filt_pipereadprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
195 static int filt_pipewrite(struct knote
*kn
, long hint
);
196 static int filt_pipewritetouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
197 static int filt_pipewriteprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
199 SECURITY_READ_ONLY_EARLY(struct filterops
) pipe_rfiltops
= {
201 .f_detach
= filt_pipedetach
,
202 .f_event
= filt_piperead
,
203 .f_touch
= filt_pipereadtouch
,
204 .f_process
= filt_pipereadprocess
,
207 SECURITY_READ_ONLY_EARLY(struct filterops
) pipe_wfiltops
= {
209 .f_detach
= filt_pipedetach
,
210 .f_event
= filt_pipewrite
,
211 .f_touch
= filt_pipewritetouch
,
212 .f_process
= filt_pipewriteprocess
,
215 static int nbigpipe
; /* for compatibility sake. no longer used */
216 static int amountpipes
; /* total number of pipes in system */
217 static int amountpipekva
; /* total memory used by pipes */
219 int maxpipekva
__attribute__((used
)) = PIPE_KVAMAX
; /* allowing 16MB max. */
222 SYSCTL_DECL(_kern_ipc
);
224 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
225 &maxpipekva
, 0, "Pipe KVA limit");
226 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
227 &maxpipekvawired
, 0, "Pipe KVA wired limit");
228 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
229 &amountpipes
, 0, "Current # of pipes");
230 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
231 &nbigpipe
, 0, "Current # of big pipes");
232 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
233 &amountpipekva
, 0, "Pipe KVA usage");
234 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
235 &amountpipekvawired
, 0, "Pipe wired KVA usage");
238 static void pipeclose(struct pipe
*cpipe
);
239 static void pipe_free_kmem(struct pipe
*cpipe
);
240 static int pipe_create(struct pipe
**cpipep
);
241 static int pipespace(struct pipe
*cpipe
, int size
);
242 static int choose_pipespace(unsigned long current
, unsigned long expected
);
243 static int expand_pipespace(struct pipe
*p
, int target_size
);
244 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
245 static __inline
int pipeio_lock(struct pipe
*cpipe
, int catch);
246 static __inline
void pipeio_unlock(struct pipe
*cpipe
);
248 extern int postpipeevent(struct pipe
*, int);
249 extern void evpipefree(struct pipe
*cpipe
);
251 static lck_grp_t
*pipe_mtx_grp
;
252 static lck_attr_t
*pipe_mtx_attr
;
253 static lck_grp_attr_t
*pipe_mtx_grp_attr
;
255 static zone_t pipe_zone
;
257 #define MAX_PIPESIZE(pipe) ( MAX(PIPE_SIZE, (pipe)->pipe_buffer.size) )
259 #define PIPE_GARBAGE_AGE_LIMIT 5000 /* In milliseconds */
260 #define PIPE_GARBAGE_QUEUE_LIMIT 32000
262 struct pipe_garbage
{
263 struct pipe
*pg_pipe
;
264 struct pipe_garbage
*pg_next
;
265 uint64_t pg_timestamp
;
268 static zone_t pipe_garbage_zone
;
269 static struct pipe_garbage
*pipe_garbage_head
= NULL
;
270 static struct pipe_garbage
*pipe_garbage_tail
= NULL
;
271 static uint64_t pipe_garbage_age_limit
= PIPE_GARBAGE_AGE_LIMIT
;
272 static int pipe_garbage_count
= 0;
273 static lck_mtx_t
*pipe_garbage_lock
;
274 static void pipe_garbage_collect(struct pipe
*cpipe
);
276 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
278 /* initial setup done at time of sysinit */
285 zone_size
= 8192 * sizeof(struct pipe
);
286 pipe_zone
= zinit(sizeof(struct pipe
), zone_size
, 4096, "pipe zone");
289 /* allocate lock group attribute and group for pipe mutexes */
290 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
291 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
293 /* allocate the lock attribute for pipe mutexes */
294 pipe_mtx_attr
= lck_attr_alloc_init();
297 * Set up garbage collection for dead pipes
299 zone_size
= (PIPE_GARBAGE_QUEUE_LIMIT
+ 20) *
300 sizeof(struct pipe_garbage
);
301 pipe_garbage_zone
= (zone_t
)zinit(sizeof(struct pipe_garbage
),
302 zone_size
, 4096, "pipe garbage zone");
303 pipe_garbage_lock
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
);
307 #ifndef CONFIG_EMBEDDED
308 /* Bitmap for things to touch in pipe_touch() */
309 #define PIPE_ATIME 0x00000001 /* time of last access */
310 #define PIPE_MTIME 0x00000002 /* time of last modification */
311 #define PIPE_CTIME 0x00000004 /* time of last status change */
314 pipe_touch(struct pipe
*tpipe
, int touch
)
320 if (touch
& PIPE_ATIME
) {
321 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
322 tpipe
->st_atimespec
.tv_nsec
= now
.tv_nsec
;
325 if (touch
& PIPE_MTIME
) {
326 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
327 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_nsec
;
330 if (touch
& PIPE_CTIME
) {
331 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
332 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_nsec
;
337 static const unsigned int pipesize_blocks
[] = {512,1024,2048,4096, 4096 * 2, PIPE_SIZE
, PIPE_SIZE
* 4 };
340 * finds the right size from possible sizes in pipesize_blocks
341 * returns the size which matches max(current,expected)
344 choose_pipespace(unsigned long current
, unsigned long expected
)
346 int i
= sizeof(pipesize_blocks
)/sizeof(unsigned int) -1;
347 unsigned long target
;
350 * assert that we always get an atomic transaction sized pipe buffer,
351 * even if the system pipe buffer high-water mark has been crossed.
353 assert(PIPE_BUF
== pipesize_blocks
[0]);
355 if (expected
> current
)
360 while ( i
>0 && pipesize_blocks
[i
-1] > target
) {
365 return pipesize_blocks
[i
];
370 * expand the size of pipe while there is data to be read,
371 * and then free the old buffer once the current buffered
372 * data has been transferred to new storage.
373 * Required: PIPE_LOCK and io lock to be held by caller.
374 * returns 0 on success or no expansion possible
377 expand_pipespace(struct pipe
*p
, int target_size
)
379 struct pipe tmp
, oldpipe
;
381 tmp
.pipe_buffer
.buffer
= 0;
383 if (p
->pipe_buffer
.size
>= (unsigned) target_size
) {
384 return 0; /* the existing buffer is max size possible */
387 /* create enough space in the target */
388 error
= pipespace(&tmp
, target_size
);
392 oldpipe
.pipe_buffer
.buffer
= p
->pipe_buffer
.buffer
;
393 oldpipe
.pipe_buffer
.size
= p
->pipe_buffer
.size
;
395 memcpy(tmp
.pipe_buffer
.buffer
, p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
396 if (p
->pipe_buffer
.cnt
> 0 && p
->pipe_buffer
.in
<= p
->pipe_buffer
.out
){
397 /* we are in State 3 and need extra copying for read to be consistent */
398 memcpy(&tmp
.pipe_buffer
.buffer
[p
->pipe_buffer
.size
], p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
399 p
->pipe_buffer
.in
+= p
->pipe_buffer
.size
;
402 p
->pipe_buffer
.buffer
= tmp
.pipe_buffer
.buffer
;
403 p
->pipe_buffer
.size
= tmp
.pipe_buffer
.size
;
406 pipe_free_kmem(&oldpipe
);
411 * The pipe system call for the DTYPE_PIPE type of pipes
414 * FREAD | fd0 | -->[struct rpipe] --> |~~buffer~~| \
416 * FWRITE | fd1 | -->[struct wpipe] --X /
421 pipe(proc_t p
, __unused
struct pipe_args
*uap
, int32_t *retval
)
423 struct fileproc
*rf
, *wf
;
424 struct pipe
*rpipe
, *wpipe
;
428 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
431 rpipe
= wpipe
= NULL
;
432 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
437 * allocate the space for the normal I/O direction up
438 * front... we'll delay the allocation for the other
439 * direction until a write actually occurs (most likely it won't)...
441 error
= pipespace(rpipe
, choose_pipespace(rpipe
->pipe_buffer
.size
, 0));
445 TAILQ_INIT(&rpipe
->pipe_evlist
);
446 TAILQ_INIT(&wpipe
->pipe_evlist
);
448 error
= falloc(p
, &rf
, &fd
, vfs_context_current());
455 * for now we'll create half-duplex pipes(refer returns section above).
456 * this is what we've always supported..
459 rf
->f_data
= (caddr_t
)rpipe
;
460 rf
->f_ops
= &pipeops
;
462 error
= falloc(p
, &wf
, &fd
, vfs_context_current());
464 fp_free(p
, retval
[0], rf
);
468 wf
->f_data
= (caddr_t
)wpipe
;
469 wf
->f_ops
= &pipeops
;
471 rpipe
->pipe_peer
= wpipe
;
472 wpipe
->pipe_peer
= rpipe
;
473 /* both structures share the same mutex */
474 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
479 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
481 * struct pipe represents a pipe endpoint. The MAC label is shared
482 * between the connected endpoints. As a result mac_pipe_label_init() and
483 * mac_pipe_label_associate() should only be called on one of the endpoints
484 * after they have been connected.
486 mac_pipe_label_init(rpipe
);
487 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
488 wpipe
->pipe_label
= rpipe
->pipe_label
;
491 procfdtbl_releasefd(p
, retval
[0], NULL
);
492 procfdtbl_releasefd(p
, retval
[1], NULL
);
493 fp_drop(p
, retval
[0], rf
, 1);
494 fp_drop(p
, retval
[1], wf
, 1);
503 lck_mtx_free(pmtx
, pipe_mtx_grp
);
509 pipe_stat(struct pipe
*cpipe
, void *ub
, int isstat64
)
516 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
517 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
524 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
530 if (cpipe
->pipe_buffer
.buffer
== 0) {
531 /* must be stat'ing the write fd */
532 if (cpipe
->pipe_peer
) {
533 /* the peer still exists, use it's info */
534 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
535 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
540 pipe_size
= MAX_PIPESIZE(cpipe
);
541 pipe_count
= cpipe
->pipe_buffer
.cnt
;
544 * since peer's buffer is setup ouside of lock
545 * we might catch it in transient state
548 pipe_size
= MAX(PIPE_SIZE
, pipesize_blocks
[0]);
551 sb64
= (struct stat64
*)ub
;
553 bzero(sb64
, sizeof(*sb64
));
554 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
555 sb64
->st_blksize
= pipe_size
;
556 sb64
->st_size
= pipe_count
;
557 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
559 sb64
->st_uid
= kauth_getuid();
560 sb64
->st_gid
= kauth_getgid();
562 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
563 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
565 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
566 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
568 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
569 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
572 * Return a relatively unique inode number based on the current
573 * address of this pipe's struct pipe. This number may be recycled
574 * relatively quickly.
576 sb64
->st_ino
= (ino64_t
)VM_KERNEL_ADDRPERM((uintptr_t)cpipe
);
578 sb
= (struct stat
*)ub
;
580 bzero(sb
, sizeof(*sb
));
581 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
582 sb
->st_blksize
= pipe_size
;
583 sb
->st_size
= pipe_count
;
584 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
586 sb
->st_uid
= kauth_getuid();
587 sb
->st_gid
= kauth_getgid();
589 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
590 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
592 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
593 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
595 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
596 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
599 * Return a relatively unique inode number based on the current
600 * address of this pipe's struct pipe. This number may be recycled
601 * relatively quickly.
603 sb
->st_ino
= (ino_t
)VM_KERNEL_ADDRPERM((uintptr_t)cpipe
);
608 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
611 * XXX (st_dev) should be unique, but there is no device driver that
612 * XXX is associated with pipes, since they are implemented via a
613 * XXX struct fileops indirection rather than as FS objects.
620 * Allocate kva for pipe circular buffer, the space is pageable
621 * This routine will 'realloc' the size of a pipe safely, if it fails
622 * it will retain the old buffer.
623 * If it fails it will return ENOMEM.
626 pipespace(struct pipe
*cpipe
, int size
)
633 if ((buffer
= (vm_offset_t
)kalloc(size
)) == 0 )
636 /* free old resources if we're resizing */
637 pipe_free_kmem(cpipe
);
638 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
639 cpipe
->pipe_buffer
.size
= size
;
640 cpipe
->pipe_buffer
.in
= 0;
641 cpipe
->pipe_buffer
.out
= 0;
642 cpipe
->pipe_buffer
.cnt
= 0;
644 OSAddAtomic(1, &amountpipes
);
645 OSAddAtomic(cpipe
->pipe_buffer
.size
, &amountpipekva
);
651 * initialize and allocate VM and memory for pipe
654 pipe_create(struct pipe
**cpipep
)
657 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
659 if ((*cpipep
= cpipe
) == NULL
)
663 * protect so pipespace or pipeclose don't follow a junk pointer
664 * if pipespace() fails.
666 bzero(cpipe
, sizeof *cpipe
);
668 #ifndef CONFIG_EMBEDDED
669 /* Initial times are all the time of creation of the pipe */
670 pipe_touch(cpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
677 * lock a pipe for I/O, blocking other access
680 pipeio_lock(struct pipe
*cpipe
, int catch)
683 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
684 cpipe
->pipe_state
|= PIPE_LWANT
;
685 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
690 cpipe
->pipe_state
|= PIPE_LOCKFL
;
695 * unlock a pipe I/O lock
698 pipeio_unlock(struct pipe
*cpipe
)
700 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
701 if (cpipe
->pipe_state
& PIPE_LWANT
) {
702 cpipe
->pipe_state
&= ~PIPE_LWANT
;
708 * wakeup anyone whos blocked in select
711 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
713 if (cpipe
->pipe_state
& PIPE_SEL
) {
714 cpipe
->pipe_state
&= ~PIPE_SEL
;
715 selwakeup(&cpipe
->pipe_sel
);
717 if (cpipe
->pipe_state
& PIPE_KNOTE
)
718 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
720 postpipeevent(cpipe
, EV_RWBYTES
);
722 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
723 if (spipe
->pipe_pgid
< 0)
724 gsignal(-spipe
->pipe_pgid
, SIGIO
);
726 proc_signal(spipe
->pipe_pgid
, SIGIO
);
731 * Read n bytes from the buffer. Semantics are similar to file read.
732 * returns: number of bytes read from the buffer
736 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
737 __unused vfs_context_t ctx
)
739 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
747 error
= pipeio_lock(rpipe
, 1);
752 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
758 while (uio_resid(uio
)) {
760 * normal pipe buffer receive
762 if (rpipe
->pipe_buffer
.cnt
> 0) {
764 * # bytes to read is min( bytes from read pointer until end of buffer,
765 * total unread bytes,
766 * user requested byte count)
768 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
769 if (size
> rpipe
->pipe_buffer
.cnt
)
770 size
= rpipe
->pipe_buffer
.cnt
;
771 // LP64todo - fix this!
772 if (size
> (u_int
) uio_resid(uio
))
773 size
= (u_int
) uio_resid(uio
);
775 PIPE_UNLOCK(rpipe
); /* we still hold io lock.*/
777 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
783 rpipe
->pipe_buffer
.out
+= size
;
784 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
785 rpipe
->pipe_buffer
.out
= 0;
787 rpipe
->pipe_buffer
.cnt
-= size
;
790 * If there is no more to read in the pipe, reset
791 * its pointers to the beginning. This improves
794 if (rpipe
->pipe_buffer
.cnt
== 0) {
795 rpipe
->pipe_buffer
.in
= 0;
796 rpipe
->pipe_buffer
.out
= 0;
801 * detect EOF condition
802 * read returns 0 on EOF, no need to set error
804 if (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
809 * If the "write-side" has been blocked, wake it up now.
811 if (rpipe
->pipe_state
& PIPE_WANTW
) {
812 rpipe
->pipe_state
&= ~PIPE_WANTW
;
817 * Break if some data was read in previous iteration.
823 * Unlock the pipe buffer for our remaining processing.
824 * We will either break out with an error or we will
825 * sleep and relock to loop.
827 pipeio_unlock(rpipe
);
830 * Handle non-blocking mode operation or
831 * wait for more data.
833 if (fp
->f_flag
& FNONBLOCK
) {
836 rpipe
->pipe_state
|= PIPE_WANTR
;
837 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
839 error
= pipeio_lock(rpipe
, 1);
848 pipeio_unlock(rpipe
);
854 * PIPE_WANT processing only makes sense if pipe_busy is 0.
856 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
857 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
859 } else if (rpipe
->pipe_buffer
.cnt
< rpipe
->pipe_buffer
.size
) {
861 * Handle write blocking hysteresis.
863 if (rpipe
->pipe_state
& PIPE_WANTW
) {
864 rpipe
->pipe_state
&= ~PIPE_WANTW
;
869 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) > 0)
870 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
872 #ifndef CONFIG_EMBEDDED
873 /* update last read time */
874 pipe_touch(rpipe
, PIPE_ATIME
);
883 * perform a write of n bytes into the read side of buffer. Since
884 * pipes are unidirectional a write is meant to be read by the otherside only.
887 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
888 __unused vfs_context_t ctx
)
893 struct pipe
*wpipe
, *rpipe
;
894 // LP64todo - fix this!
895 orig_resid
= uio_resid(uio
);
898 rpipe
= (struct pipe
*)fp
->f_data
;
901 wpipe
= rpipe
->pipe_peer
;
904 * detect loss of pipe read side, issue SIGPIPE if lost.
906 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
911 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
922 * need to allocate some storage... we delay the allocation
923 * until the first write on fd[0] to avoid allocating storage for both
924 * 'pipe ends'... most pipes are half-duplex with the writes targeting
925 * fd[1], so allocating space for both ends is a waste...
928 if ( wpipe
->pipe_buffer
.buffer
== 0 || (
929 (unsigned)orig_resid
> wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
&&
930 amountpipekva
< maxpipekva
) ) {
932 pipe_size
= choose_pipespace(wpipe
->pipe_buffer
.size
, wpipe
->pipe_buffer
.cnt
+ orig_resid
);
936 * need to do initial allocation or resizing of pipe
937 * holding both structure and io locks.
939 if ((error
= pipeio_lock(wpipe
, 1)) == 0) {
940 if (wpipe
->pipe_buffer
.cnt
== 0)
941 error
= pipespace(wpipe
, pipe_size
);
943 error
= expand_pipespace(wpipe
, pipe_size
);
945 pipeio_unlock(wpipe
);
947 /* allocation failed */
948 if (wpipe
->pipe_buffer
.buffer
== 0)
953 * If an error occurred unbusy and return, waking up any pending
957 if ((wpipe
->pipe_busy
== 0) &&
958 (wpipe
->pipe_state
& PIPE_WANT
)) {
959 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
967 while (uio_resid(uio
)) {
970 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
972 /* Writes of size <= PIPE_BUF must be atomic. */
973 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
978 if ((error
= pipeio_lock(wpipe
,1)) == 0) {
979 int size
; /* Transfer size */
980 int segsize
; /* first segment to transfer */
982 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
983 pipeio_unlock(wpipe
);
988 * If a process blocked in pipeio_lock, our
989 * value for space might be bad... the mutex
990 * is dropped while we're blocked
992 if (space
> (int)(wpipe
->pipe_buffer
.size
-
993 wpipe
->pipe_buffer
.cnt
)) {
994 pipeio_unlock(wpipe
);
999 * Transfer size is minimum of uio transfer
1000 * and free space in pipe buffer.
1002 // LP64todo - fix this!
1003 if (space
> uio_resid(uio
))
1004 size
= uio_resid(uio
);
1008 * First segment to transfer is minimum of
1009 * transfer size and contiguous space in
1010 * pipe buffer. If first segment to transfer
1011 * is less than the transfer size, we've got
1012 * a wraparound in the buffer.
1014 segsize
= wpipe
->pipe_buffer
.size
-
1015 wpipe
->pipe_buffer
.in
;
1019 /* Transfer first segment */
1022 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1026 if (error
== 0 && segsize
< size
) {
1028 * Transfer remaining part now, to
1029 * support atomic writes. Wraparound
1030 * happened. (State 3)
1032 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1033 wpipe
->pipe_buffer
.size
)
1034 panic("Expected pipe buffer "
1035 "wraparound disappeared");
1039 &wpipe
->pipe_buffer
.buffer
[0],
1040 size
- segsize
, uio
);
1044 * readers never know to read until count is updated.
1047 wpipe
->pipe_buffer
.in
+= size
;
1048 if (wpipe
->pipe_buffer
.in
>
1049 wpipe
->pipe_buffer
.size
) {
1050 if (wpipe
->pipe_buffer
.in
!=
1052 wpipe
->pipe_buffer
.size
)
1055 wpipe
->pipe_buffer
.in
= size
-
1059 wpipe
->pipe_buffer
.cnt
+= size
;
1060 if (wpipe
->pipe_buffer
.cnt
>
1061 wpipe
->pipe_buffer
.size
)
1062 panic("Pipe buffer overflow");
1065 pipeio_unlock(wpipe
);
1072 * If the "read-side" has been blocked, wake it up now.
1074 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1075 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1079 * don't block on non-blocking I/O
1080 * we'll do the pipeselwakeup on the way out
1082 if (fp
->f_flag
& FNONBLOCK
) {
1088 * If read side wants to go away, we just issue a signal
1091 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1097 * We have no more space and have something to offer,
1098 * wake up select/poll.
1100 pipeselwakeup(wpipe
, wpipe
);
1102 wpipe
->pipe_state
|= PIPE_WANTW
;
1104 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1112 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1113 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1116 if (wpipe
->pipe_buffer
.cnt
> 0) {
1118 * If there are any characters in the buffer, we wake up
1119 * the reader if it was blocked waiting for data.
1121 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1122 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1126 * wake up thread blocked in select/poll or post the notification
1128 pipeselwakeup(wpipe
, wpipe
);
1131 #ifndef CONFIG_EMBEDDED
1132 /* Update modification, status change (# of bytes in pipe) times */
1133 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1134 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1142 * we implement a very minimal set of ioctls for compatibility with sockets.
1146 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1147 __unused vfs_context_t ctx
)
1149 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1157 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1173 mpipe
->pipe_state
|= PIPE_ASYNC
;
1175 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1181 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1186 mpipe
->pipe_pgid
= *(int *)data
;
1192 *(int *)data
= mpipe
->pipe_pgid
;
1204 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1206 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1210 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1215 wpipe
= rpipe
->pipe_peer
;
1220 * XXX We should use a per thread credential here; minimally, the
1221 * XXX process credential should have a persistent reference on it
1222 * XXX before being passed in here.
1224 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1232 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1233 (rpipe
->pipe_buffer
.cnt
> 0) ||
1234 (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1238 rpipe
->pipe_state
|= PIPE_SEL
;
1239 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1245 wpipe
->pipe_state
|= PIPE_WSELECT
;
1246 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1247 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1248 (MAX_PIPESIZE(wpipe
) - wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1252 wpipe
->pipe_state
|= PIPE_SEL
;
1253 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1257 rpipe
->pipe_state
|= PIPE_SEL
;
1258 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1269 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1273 proc_fdlock_spin(vfs_context_proc(ctx
));
1274 cpipe
= (struct pipe
*)fg
->fg_data
;
1276 proc_fdunlock(vfs_context_proc(ctx
));
1284 pipe_free_kmem(struct pipe
*cpipe
)
1286 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1287 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), &amountpipekva
);
1288 OSAddAtomic(-1, &amountpipes
);
1289 kfree((void *)cpipe
->pipe_buffer
.buffer
,
1290 cpipe
->pipe_buffer
.size
);
1291 cpipe
->pipe_buffer
.buffer
= NULL
;
1292 cpipe
->pipe_buffer
.size
= 0;
1300 pipeclose(struct pipe
*cpipe
)
1306 /* partially created pipes won't have a valid mutex. */
1307 if (PIPE_MTX(cpipe
) != NULL
)
1312 * If the other side is blocked, wake it up saying that
1313 * we want to close it down.
1315 cpipe
->pipe_state
&= ~PIPE_DRAIN
;
1316 cpipe
->pipe_state
|= PIPE_EOF
;
1317 pipeselwakeup(cpipe
, cpipe
);
1319 while (cpipe
->pipe_busy
) {
1320 cpipe
->pipe_state
|= PIPE_WANT
;
1323 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1328 * Free the shared pipe label only after the two ends are disconnected.
1330 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1331 mac_pipe_label_destroy(cpipe
);
1335 * Disconnect from peer
1337 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1339 ppipe
->pipe_state
&= ~(PIPE_DRAIN
);
1340 ppipe
->pipe_state
|= PIPE_EOF
;
1342 pipeselwakeup(ppipe
, ppipe
);
1345 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1346 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1348 postpipeevent(ppipe
, EV_RCLOSED
);
1350 ppipe
->pipe_peer
= NULL
;
1357 if (PIPE_MTX(cpipe
) != NULL
) {
1358 if (ppipe
!= NULL
) {
1360 * since the mutex is shared and the peer is still
1361 * alive, we need to release the mutex, not free it
1366 * peer is gone, so we're the sole party left with
1367 * interest in this mutex... unlock and free it
1370 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1373 pipe_free_kmem(cpipe
);
1374 if (cpipe
->pipe_state
& PIPE_WSELECT
) {
1375 pipe_garbage_collect(cpipe
);
1377 zfree(pipe_zone
, cpipe
);
1378 pipe_garbage_collect(NULL
);
1385 filt_piperead_common(struct knote
*kn
, struct pipe
*rpipe
)
1391 * we're being called back via the KNOTE post
1392 * we made in pipeselwakeup, and we already hold the mutex...
1395 wpipe
= rpipe
->pipe_peer
;
1396 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1397 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1398 (wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1399 kn
->kn_flags
|= EV_EOF
;
1403 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1404 if (rpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> MAX_PIPESIZE(rpipe
))
1405 lowwat
= MAX_PIPESIZE(rpipe
);
1406 else if (kn
->kn_sdata
> lowwat
)
1407 lowwat
= kn
->kn_sdata
;
1409 retval
= kn
->kn_data
>= lowwat
;
1415 filt_piperead(struct knote
*kn
, long hint
)
1417 #pragma unused(hint)
1418 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1420 return filt_piperead_common(kn
, rpipe
);
1424 filt_pipereadtouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
1426 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1431 /* accept new inputs (and save the low water threshold and flag) */
1432 kn
->kn_sdata
= kev
->data
;
1433 kn
->kn_sfflags
= kev
->fflags
;
1434 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
1435 kn
->kn_udata
= kev
->udata
;
1437 /* identify if any events are now fired */
1438 retval
= filt_piperead_common(kn
, rpipe
);
1446 filt_pipereadprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
1448 #pragma unused(data)
1449 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1453 retval
= filt_piperead_common(kn
, rpipe
);
1455 *kev
= kn
->kn_kevent
;
1456 if (kn
->kn_flags
& EV_CLEAR
) {
1468 filt_pipewrite_common(struct knote
*kn
, struct pipe
*rpipe
)
1473 * we're being called back via the KNOTE post
1474 * we made in pipeselwakeup, and we already hold the mutex...
1476 wpipe
= rpipe
->pipe_peer
;
1478 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1480 kn
->kn_flags
|= EV_EOF
;
1483 kn
->kn_data
= MAX_PIPESIZE(wpipe
) - wpipe
->pipe_buffer
.cnt
;
1485 int64_t lowwat
= PIPE_BUF
;
1486 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1487 if (wpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> MAX_PIPESIZE(wpipe
))
1488 lowwat
= MAX_PIPESIZE(wpipe
);
1489 else if (kn
->kn_sdata
> lowwat
)
1490 lowwat
= kn
->kn_sdata
;
1493 return (kn
->kn_data
>= lowwat
);
1498 filt_pipewrite(struct knote
*kn
, long hint
)
1500 #pragma unused(hint)
1501 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1503 return filt_pipewrite_common(kn
, rpipe
);
1508 filt_pipewritetouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
1510 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1515 /* accept new kevent data (and save off lowat threshold and flag) */
1516 kn
->kn_sfflags
= kev
->fflags
;
1517 kn
->kn_sdata
= kev
->data
;
1518 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
1519 kn
->kn_udata
= kev
->udata
;
1521 /* determine if any event is now deemed fired */
1522 res
= filt_pipewrite_common(kn
, rpipe
);
1530 filt_pipewriteprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
1532 #pragma unused(data)
1533 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1537 res
= filt_pipewrite_common(kn
, rpipe
);
1539 *kev
= kn
->kn_kevent
;
1540 if (kn
->kn_flags
& EV_CLEAR
) {
1552 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
,
1553 __unused
struct kevent_internal_s
*kev
, __unused vfs_context_t ctx
)
1555 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1561 * XXX We should use a per thread credential here; minimally, the
1562 * XXX process credential should have a persistent reference on it
1563 * XXX before being passed in here.
1565 if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx
), kn
, cpipe
) != 0) {
1567 kn
->kn_flags
= EV_ERROR
;
1568 kn
->kn_data
= EPERM
;
1573 switch (kn
->kn_filter
) {
1575 kn
->kn_filtid
= EVFILTID_PIPE_R
;
1577 /* determine initial state */
1578 res
= filt_piperead_common(kn
, cpipe
);
1582 kn
->kn_filtid
= EVFILTID_PIPE_W
;
1584 if (cpipe
->pipe_peer
== NULL
) {
1586 * other end of pipe has been closed
1589 kn
->kn_flags
= EV_ERROR
;
1590 kn
->kn_data
= EPIPE
;
1593 if (cpipe
->pipe_peer
)
1594 cpipe
= cpipe
->pipe_peer
;
1596 /* determine inital state */
1597 res
= filt_pipewrite_common(kn
, cpipe
);
1601 kn
->kn_flags
= EV_ERROR
;
1602 kn
->kn_data
= EINVAL
;
1606 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1607 cpipe
->pipe_state
|= PIPE_KNOTE
;
1614 filt_pipedetach(struct knote
*kn
)
1616 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1620 if (kn
->kn_filter
== EVFILT_WRITE
) {
1621 if (cpipe
->pipe_peer
== NULL
) {
1625 cpipe
= cpipe
->pipe_peer
;
1627 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1628 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1629 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1635 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1640 struct timespec now
;
1641 struct vinfo_stat
* ub
;
1650 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1656 if (cpipe
->pipe_buffer
.buffer
== 0) {
1658 * must be stat'ing the write fd
1660 if (cpipe
->pipe_peer
) {
1662 * the peer still exists, use it's info
1664 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
1665 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1670 pipe_size
= MAX_PIPESIZE(cpipe
);
1671 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1674 * since peer's buffer is setup ouside of lock
1675 * we might catch it in transient state
1678 pipe_size
= PIPE_SIZE
;
1680 ub
= &pinfo
->pipe_stat
;
1682 bzero(ub
, sizeof(*ub
));
1683 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1684 ub
->vst_blksize
= pipe_size
;
1685 ub
->vst_size
= pipe_count
;
1686 if (ub
->vst_blksize
!= 0)
1687 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1690 ub
->vst_uid
= kauth_getuid();
1691 ub
->vst_gid
= kauth_getgid();
1694 ub
->vst_atime
= now
.tv_sec
;
1695 ub
->vst_atimensec
= now
.tv_nsec
;
1697 ub
->vst_mtime
= now
.tv_sec
;
1698 ub
->vst_mtimensec
= now
.tv_nsec
;
1700 ub
->vst_ctime
= now
.tv_sec
;
1701 ub
->vst_ctimensec
= now
.tv_nsec
;
1704 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1705 * XXX (st_dev, st_ino) should be unique.
1708 pinfo
->pipe_handle
= (uint64_t)VM_KERNEL_ADDRPERM((uintptr_t)cpipe
);
1709 pinfo
->pipe_peerhandle
= (uint64_t)VM_KERNEL_ADDRPERM((uintptr_t)(cpipe
->pipe_peer
));
1710 pinfo
->pipe_status
= cpipe
->pipe_state
;
1719 pipe_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
1722 /* Note: fdlock already held */
1723 struct pipe
*ppipe
, *cpipe
= (struct pipe
*)(fp
->f_fglob
->fg_data
);
1727 cpipe
->pipe_state
|= PIPE_DRAIN
;
1728 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1731 /* Must wake up peer: a writer sleeps on the read side */
1732 if ((ppipe
= cpipe
->pipe_peer
)) {
1733 ppipe
->pipe_state
|= PIPE_DRAIN
;
1734 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1747 * When a thread sets a write-select on a pipe, it creates an implicit,
1748 * untracked dependency between that thread and the peer of the pipe
1749 * on which the select is set. If the peer pipe is closed and freed
1750 * before the select()ing thread wakes up, the system will panic as
1751 * it attempts to unwind the dangling select(). To avoid that panic,
1752 * we notice whenever a dangerous select() is set on a pipe, and
1753 * defer the final deletion of the pipe until that select()s are all
1754 * resolved. Since we can't currently detect exactly when that
1755 * resolution happens, we use a simple garbage collection queue to
1756 * reap the at-risk pipes 'later'.
1759 pipe_garbage_collect(struct pipe
*cpipe
)
1762 struct pipe_garbage
*pgp
;
1764 /* Convert msecs to nsecs and then to abstime */
1765 old
= pipe_garbage_age_limit
* 1000000;
1766 nanoseconds_to_absolutetime(old
, &old
);
1768 lck_mtx_lock(pipe_garbage_lock
);
1770 /* Free anything that's been on the queue for <mumble> seconds */
1771 now
= mach_absolute_time();
1773 while ((pgp
= pipe_garbage_head
) && pgp
->pg_timestamp
< old
) {
1774 pipe_garbage_head
= pgp
->pg_next
;
1775 if (pipe_garbage_head
== NULL
)
1776 pipe_garbage_tail
= NULL
;
1777 pipe_garbage_count
--;
1778 zfree(pipe_zone
, pgp
->pg_pipe
);
1779 zfree(pipe_garbage_zone
, pgp
);
1782 /* Add the new pipe (if any) to the tail of the garbage queue */
1784 cpipe
->pipe_state
= PIPE_DEAD
;
1785 pgp
= (struct pipe_garbage
*)zalloc(pipe_garbage_zone
);
1788 * We're too low on memory to garbage collect the
1789 * pipe. Freeing it runs the risk of panicing the
1790 * system. All we can do is leak it and leave
1791 * a breadcrumb behind. The good news, such as it
1792 * is, is that this will probably never happen.
1793 * We will probably hit the panic below first.
1795 printf("Leaking pipe %p - no room left in the queue",
1797 lck_mtx_unlock(pipe_garbage_lock
);
1801 pgp
->pg_pipe
= cpipe
;
1802 pgp
->pg_timestamp
= now
;
1803 pgp
->pg_next
= NULL
;
1805 if (pipe_garbage_tail
)
1806 pipe_garbage_tail
->pg_next
= pgp
;
1807 pipe_garbage_tail
= pgp
;
1808 if (pipe_garbage_head
== NULL
)
1809 pipe_garbage_head
= pipe_garbage_tail
;
1811 if (pipe_garbage_count
++ >= PIPE_GARBAGE_QUEUE_LIMIT
)
1812 panic("Length of pipe garbage queue exceeded %d",
1813 PIPE_GARBAGE_QUEUE_LIMIT
);
1815 lck_mtx_unlock(pipe_garbage_lock
);