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
|CTLFLAG_LOCKED
,
235 &maxpipekva
, 0, "Pipe KVA limit");
236 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
237 &maxpipekvawired
, 0, "Pipe KVA wired limit");
238 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
239 &amountpipes
, 0, "Current # of pipes");
240 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
241 &nbigpipe
, 0, "Current # of big pipes");
242 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
243 &amountpipekva
, 0, "Pipe KVA usage");
244 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
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 #define PIPE_GARBAGE_AGE_LIMIT 5000 /* In milliseconds */
275 #define PIPE_GARBAGE_QUEUE_LIMIT 32000
277 struct pipe_garbage
{
278 struct pipe
*pg_pipe
;
279 struct pipe_garbage
*pg_next
;
280 uint64_t pg_timestamp
;
283 static zone_t pipe_garbage_zone
;
284 static struct pipe_garbage
*pipe_garbage_head
= NULL
;
285 static struct pipe_garbage
*pipe_garbage_tail
= NULL
;
286 static uint64_t pipe_garbage_age_limit
= PIPE_GARBAGE_AGE_LIMIT
;
287 static int pipe_garbage_count
= 0;
288 static lck_mtx_t
*pipe_garbage_lock
;
290 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
297 zone_size
= 8192 * sizeof(struct pipe
);
298 pipe_zone
= zinit(sizeof(struct pipe
), zone_size
, 4096, "pipe zone");
301 * allocate lock group attribute and group for pipe mutexes
303 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
304 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
307 * allocate the lock attribute for pipe mutexes
309 pipe_mtx_attr
= lck_attr_alloc_init();
312 * Set up garbage collection for dead pipes
314 zone_size
= (PIPE_GARBAGE_QUEUE_LIMIT
+ 20) *
315 sizeof(struct pipe_garbage
);
316 pipe_garbage_zone
= (zone_t
)zinit(sizeof(struct pipe_garbage
),
317 zone_size
, 4096, "pipe garbage zone");
318 pipe_garbage_lock
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
);
321 /* Bitmap for things to touch in pipe_touch() */
322 #define PIPE_ATIME 0x00000001 /* time of last access */
323 #define PIPE_MTIME 0x00000002 /* time of last modification */
324 #define PIPE_CTIME 0x00000004 /* time of last status change */
327 pipe_touch(struct pipe
*tpipe
, int touch
)
333 if (touch
& PIPE_ATIME
) {
334 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
335 tpipe
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
338 if (touch
& PIPE_MTIME
) {
339 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
340 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
343 if (touch
& PIPE_CTIME
) {
344 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
345 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
352 * The pipe system call for the DTYPE_PIPE type of pipes
357 pipe(proc_t p
, __unused
struct pipe_args
*uap
, int32_t *retval
)
359 struct fileproc
*rf
, *wf
;
360 struct pipe
*rpipe
, *wpipe
;
364 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
367 rpipe
= wpipe
= NULL
;
368 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
373 * allocate the space for the normal I/O direction up
374 * front... we'll delay the allocation for the other
375 * direction until a write actually occurs (most
376 * likely it won't)...
378 * Reduce to 1/4th pipe size if we're over our global max.
380 if (amountpipekva
> maxpipekva
/ 2)
381 error
= pipespace(rpipe
, SMALL_PIPE_SIZE
);
383 error
= pipespace(rpipe
, PIPE_SIZE
);
387 #ifndef PIPE_NODIRECT
388 rpipe
->pipe_state
|= PIPE_DIRECTOK
;
389 wpipe
->pipe_state
|= PIPE_DIRECTOK
;
391 TAILQ_INIT(&rpipe
->pipe_evlist
);
392 TAILQ_INIT(&wpipe
->pipe_evlist
);
394 error
= falloc(p
, &rf
, &fd
, vfs_context_current());
401 * for now we'll create half-duplex
402 * pipes... this is what we've always
406 rf
->f_type
= DTYPE_PIPE
;
407 rf
->f_data
= (caddr_t
)rpipe
;
408 rf
->f_ops
= &pipeops
;
410 error
= falloc(p
, &wf
, &fd
, vfs_context_current());
412 fp_free(p
, retval
[0], rf
);
416 wf
->f_type
= DTYPE_PIPE
;
417 wf
->f_data
= (caddr_t
)wpipe
;
418 wf
->f_ops
= &pipeops
;
420 rpipe
->pipe_peer
= wpipe
;
421 wpipe
->pipe_peer
= rpipe
;
422 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
427 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
429 * struct pipe represents a pipe endpoint. The MAC label is shared
430 * between the connected endpoints. As a result mac_pipe_label_init() and
431 * mac_pipe_label_associate() should only be called on one of the endpoints
432 * after they have been connected.
434 mac_pipe_label_init(rpipe
);
435 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
436 wpipe
->pipe_label
= rpipe
->pipe_label
;
439 procfdtbl_releasefd(p
, retval
[0], NULL
);
440 procfdtbl_releasefd(p
, retval
[1], NULL
);
441 fp_drop(p
, retval
[0], rf
, 1);
442 fp_drop(p
, retval
[1], wf
, 1);
451 lck_mtx_free(pmtx
, pipe_mtx_grp
);
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 */
472 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
478 if (cpipe
->pipe_buffer
.buffer
== 0) {
480 * must be stat'ing the write fd
482 if (cpipe
->pipe_peer
) {
484 * the peer still exists, use it's info
486 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
487 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
492 pipe_size
= cpipe
->pipe_buffer
.size
;
493 pipe_count
= cpipe
->pipe_buffer
.cnt
;
496 * since peer's buffer is setup ouside of lock
497 * we might catch it in transient state
500 pipe_size
= PIPE_SIZE
;
503 sb64
= (struct stat64
*)ub
;
505 bzero(sb64
, sizeof(*sb64
));
506 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
507 sb64
->st_blksize
= pipe_size
;
508 sb64
->st_size
= pipe_count
;
509 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
511 sb64
->st_uid
= kauth_getuid();
512 sb64
->st_gid
= kauth_getgid();
514 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
515 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
517 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
518 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
520 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
521 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
524 * Return a relatively unique inode number based on the current
525 * address of this pipe's struct pipe. This number may be recycled
526 * relatively quickly.
528 sb64
->st_ino
= (ino64_t
)((uintptr_t)cpipe
);
530 sb
= (struct stat
*)ub
;
532 bzero(sb
, sizeof(*sb
));
533 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
534 sb
->st_blksize
= pipe_size
;
535 sb
->st_size
= pipe_count
;
536 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
538 sb
->st_uid
= kauth_getuid();
539 sb
->st_gid
= kauth_getgid();
541 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
542 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
544 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
545 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
547 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
548 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
551 * Return a relatively unique inode number based on the current
552 * address of this pipe's struct pipe. This number may be recycled
553 * relatively quickly.
555 sb
->st_ino
= (ino_t
)(uintptr_t)cpipe
;
560 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
563 * XXX (st_dev) should be unique, but there is no device driver that
564 * XXX is associated with pipes, since they are implemented via a
565 * XXX struct fileops indirection rather than as FS objects.
572 * Allocate kva for pipe circular buffer, the space is pageable
573 * This routine will 'realloc' the size of a pipe safely, if it fails
574 * it will retain the old buffer.
575 * If it fails it will return ENOMEM.
578 pipespace(struct pipe
*cpipe
, int size
)
582 size
= round_page(size
);
584 if (kmem_alloc(kernel_map
, &buffer
, size
) != KERN_SUCCESS
)
587 /* free old resources if we're resizing */
588 pipe_free_kmem(cpipe
);
589 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
590 cpipe
->pipe_buffer
.size
= size
;
591 cpipe
->pipe_buffer
.in
= 0;
592 cpipe
->pipe_buffer
.out
= 0;
593 cpipe
->pipe_buffer
.cnt
= 0;
595 OSAddAtomic(1, &amountpipes
);
596 OSAddAtomic(cpipe
->pipe_buffer
.size
, &amountpipekva
);
602 * initialize and allocate VM and memory for pipe
605 pipe_create(struct pipe
**cpipep
)
609 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
611 if ((*cpipep
= cpipe
) == NULL
)
615 * protect so pipespace or pipeclose don't follow a junk pointer
616 * if pipespace() fails.
618 bzero(cpipe
, sizeof *cpipe
);
620 /* Initial times are all the time of creation of the pipe */
621 pipe_touch(cpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
628 * lock a pipe for I/O, blocking other access
631 pipelock(struct pipe
*cpipe
, int catch)
635 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
636 cpipe
->pipe_state
|= PIPE_LWANT
;
638 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
643 cpipe
->pipe_state
|= PIPE_LOCKFL
;
649 * unlock a pipe I/O lock
652 pipeunlock(struct pipe
*cpipe
)
654 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
656 if (cpipe
->pipe_state
& PIPE_LWANT
) {
657 cpipe
->pipe_state
&= ~PIPE_LWANT
;
663 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
665 if (cpipe
->pipe_state
& PIPE_SEL
) {
666 cpipe
->pipe_state
&= ~PIPE_SEL
;
667 selwakeup(&cpipe
->pipe_sel
);
669 if (cpipe
->pipe_state
& PIPE_KNOTE
)
670 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
672 postpipeevent(cpipe
, EV_RWBYTES
);
674 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
675 if (spipe
->pipe_pgid
< 0)
676 gsignal(-spipe
->pipe_pgid
, SIGIO
);
678 proc_signal(spipe
->pipe_pgid
, SIGIO
);
684 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
685 __unused vfs_context_t ctx
)
687 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
695 error
= pipelock(rpipe
, 1);
700 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
705 while (uio_resid(uio
)) {
707 * normal pipe buffer receive
709 if (rpipe
->pipe_buffer
.cnt
> 0) {
710 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
711 if (size
> rpipe
->pipe_buffer
.cnt
)
712 size
= rpipe
->pipe_buffer
.cnt
;
713 // LP64todo - fix this!
714 if (size
> (u_int
) uio_resid(uio
))
715 size
= (u_int
) uio_resid(uio
);
719 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
725 rpipe
->pipe_buffer
.out
+= size
;
726 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
727 rpipe
->pipe_buffer
.out
= 0;
729 rpipe
->pipe_buffer
.cnt
-= size
;
732 * If there is no more to read in the pipe, reset
733 * its pointers to the beginning. This improves
736 if (rpipe
->pipe_buffer
.cnt
== 0) {
737 rpipe
->pipe_buffer
.in
= 0;
738 rpipe
->pipe_buffer
.out
= 0;
741 #ifndef PIPE_NODIRECT
743 * Direct copy, bypassing a kernel buffer.
745 } else if ((size
= rpipe
->pipe_map
.cnt
) &&
746 (rpipe
->pipe_state
& PIPE_DIRECTW
)) {
748 // LP64todo - fix this!
749 if (size
> (u_int
) uio_resid(uio
))
750 size
= (u_int
) uio_resid(uio
);
752 va
= (caddr_t
) rpipe
->pipe_map
.kva
+
755 error
= uiomove(va
, size
, uio
);
760 rpipe
->pipe_map
.pos
+= size
;
761 rpipe
->pipe_map
.cnt
-= size
;
762 if (rpipe
->pipe_map
.cnt
== 0) {
763 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
769 * detect EOF condition
770 * read returns 0 on EOF, no need to set error
772 if (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
777 * If the "write-side" has been blocked, wake it up now.
779 if (rpipe
->pipe_state
& PIPE_WANTW
) {
780 rpipe
->pipe_state
&= ~PIPE_WANTW
;
785 * Break if some data was read.
791 * Unlock the pipe buffer for our remaining processing.
792 * We will either break out with an error or we will
793 * sleep and relock to loop.
798 * Handle non-blocking mode operation or
799 * wait for more data.
801 if (fp
->f_flag
& FNONBLOCK
) {
804 rpipe
->pipe_state
|= PIPE_WANTR
;
806 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
809 error
= pipelock(rpipe
, 1);
824 * PIPE_WANT processing only makes sense if pipe_busy is 0.
826 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
827 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
829 } else if (rpipe
->pipe_buffer
.cnt
< MINPIPESIZE
) {
831 * Handle write blocking hysteresis.
833 if (rpipe
->pipe_state
& PIPE_WANTW
) {
834 rpipe
->pipe_state
&= ~PIPE_WANTW
;
839 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)
840 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
842 /* update last read time */
843 pipe_touch(rpipe
, PIPE_ATIME
);
852 #ifndef PIPE_NODIRECT
854 * Map the sending processes' buffer into kernel space and wire it.
855 * This is similar to a physical write operation.
858 pipe_build_write_buffer(wpipe
, uio
)
865 vm_offset_t addr
, endaddr
;
868 size
= (u_int
) uio
->uio_iov
->iov_len
;
869 if (size
> wpipe
->pipe_buffer
.size
)
870 size
= wpipe
->pipe_buffer
.size
;
872 pmap
= vmspace_pmap(curproc
->p_vmspace
);
873 endaddr
= round_page((vm_offset_t
)uio
->uio_iov
->iov_base
+ size
);
874 addr
= trunc_page((vm_offset_t
)uio
->uio_iov
->iov_base
);
875 for (i
= 0; addr
< endaddr
; addr
+= PAGE_SIZE
, i
++) {
877 * vm_fault_quick() can sleep. Consequently,
878 * vm_page_lock_queue() and vm_page_unlock_queue()
879 * should not be performed outside of this loop.
882 if (vm_fault_quick((caddr_t
)addr
, VM_PROT_READ
) < 0) {
883 vm_page_lock_queues();
884 for (j
= 0; j
< i
; j
++)
885 vm_page_unhold(wpipe
->pipe_map
.ms
[j
]);
886 vm_page_unlock_queues();
889 wpipe
->pipe_map
.ms
[i
] = pmap_extract_and_hold(pmap
, addr
,
891 if (wpipe
->pipe_map
.ms
[i
] == NULL
)
896 * set up the control block
898 wpipe
->pipe_map
.npages
= i
;
899 wpipe
->pipe_map
.pos
=
900 ((vm_offset_t
) uio
->uio_iov
->iov_base
) & PAGE_MASK
;
901 wpipe
->pipe_map
.cnt
= size
;
906 if (wpipe
->pipe_map
.kva
== 0) {
908 * We need to allocate space for an extra page because the
909 * address range might (will) span pages at times.
911 wpipe
->pipe_map
.kva
= kmem_alloc_nofault(kernel_map
,
912 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
913 atomic_add_int(&amountpipekvawired
,
914 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
916 pmap_qenter(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.ms
,
917 wpipe
->pipe_map
.npages
);
920 * and update the uio data
923 uio
->uio_iov
->iov_len
-= size
;
924 uio
->uio_iov
->iov_base
= (char *)uio
->uio_iov
->iov_base
+ size
;
925 if (uio
->uio_iov
->iov_len
== 0)
927 uio_setresid(uio
, (uio_resid(uio
) - size
));
928 uio
->uio_offset
+= size
;
933 * unmap and unwire the process buffer
936 pipe_destroy_write_buffer(wpipe
)
941 if (wpipe
->pipe_map
.kva
) {
942 pmap_qremove(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.npages
);
944 if (amountpipekvawired
> maxpipekvawired
/ 2) {
945 /* Conserve address space */
946 vm_offset_t kva
= wpipe
->pipe_map
.kva
;
947 wpipe
->pipe_map
.kva
= 0;
948 kmem_free(kernel_map
, kva
,
949 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
950 atomic_subtract_int(&amountpipekvawired
,
951 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
954 vm_page_lock_queues();
955 for (i
= 0; i
< wpipe
->pipe_map
.npages
; i
++) {
956 vm_page_unhold(wpipe
->pipe_map
.ms
[i
]);
958 vm_page_unlock_queues();
959 wpipe
->pipe_map
.npages
= 0;
963 * In the case of a signal, the writing process might go away. This
964 * code copies the data into the circular buffer so that the source
965 * pages can be freed without loss of data.
968 pipe_clone_write_buffer(wpipe
)
974 size
= wpipe
->pipe_map
.cnt
;
975 pos
= wpipe
->pipe_map
.pos
;
977 wpipe
->pipe_buffer
.in
= size
;
978 wpipe
->pipe_buffer
.out
= 0;
979 wpipe
->pipe_buffer
.cnt
= size
;
980 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
983 bcopy((caddr_t
) wpipe
->pipe_map
.kva
+ pos
,
984 wpipe
->pipe_buffer
.buffer
, size
);
985 pipe_destroy_write_buffer(wpipe
);
990 * This implements the pipe buffer write mechanism. Note that only
991 * a direct write OR a normal pipe write can be pending at any given time.
992 * If there are any characters in the pipe buffer, the direct write will
993 * be deferred until the receiving process grabs all of the bytes from
994 * the pipe buffer. Then the direct mapping write is set-up.
997 pipe_direct_write(wpipe
, uio
)
1004 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1005 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1006 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1009 wpipe
->pipe_state
|= PIPE_WANTW
;
1010 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
1011 PRIBIO
| PCATCH
, "pipdww", 0);
1014 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1019 wpipe
->pipe_map
.cnt
= 0; /* transfer not ready yet */
1020 if (wpipe
->pipe_buffer
.cnt
> 0) {
1021 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1022 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1026 wpipe
->pipe_state
|= PIPE_WANTW
;
1027 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
1028 PRIBIO
| PCATCH
, "pipdwc", 0);
1031 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1038 wpipe
->pipe_state
|= PIPE_DIRECTW
;
1042 error
= pipe_build_write_buffer(wpipe
, uio
);
1046 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
1051 while (!error
&& (wpipe
->pipe_state
& PIPE_DIRECTW
)) {
1052 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1055 pipe_destroy_write_buffer(wpipe
);
1057 pipeselwakeup(wpipe
, wpipe
);
1062 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1063 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1066 pipeselwakeup(wpipe
, wpipe
);
1067 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
,
1072 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1074 * this bit of trickery substitutes a kernel buffer for
1075 * the process that might be going away.
1077 pipe_clone_write_buffer(wpipe
);
1080 pipe_destroy_write_buffer(wpipe
);
1095 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
1096 __unused vfs_context_t ctx
)
1101 struct pipe
*wpipe
, *rpipe
;
1103 rpipe
= (struct pipe
*)fp
->f_data
;
1106 wpipe
= rpipe
->pipe_peer
;
1109 * detect loss of pipe read side, issue SIGPIPE if lost.
1111 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1116 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
1126 if (wpipe
->pipe_buffer
.buffer
== 0) {
1128 * need to allocate some storage... we delay the allocation
1129 * until the first write on fd[0] to avoid allocating storage for both
1130 * 'pipe ends'... most pipes are half-duplex with the writes targeting
1131 * fd[1], so allocating space for both ends is a waste...
1133 * Reduce to 1/4th pipe size if we're over our global max.
1135 if (amountpipekva
> maxpipekva
/ 2)
1136 pipe_size
= SMALL_PIPE_SIZE
;
1138 pipe_size
= PIPE_SIZE
;
1142 * If it is advantageous to resize the pipe buffer, do
1145 if ((uio_resid(uio
) > PIPE_SIZE
) &&
1146 (wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
1147 (amountpipekva
< maxpipekva
/ 2) &&
1148 (nbigpipe
< LIMITBIGPIPES
) &&
1149 #ifndef PIPE_NODIRECT
1150 (wpipe
->pipe_state
& PIPE_DIRECTW
) == 0 &&
1152 (wpipe
->pipe_buffer
.cnt
== 0)) {
1154 pipe_size
= BIG_PIPE_SIZE
;
1159 * need to do initial allocation or resizing of pipe
1161 if ((error
= pipelock(wpipe
, 1)) == 0) {
1163 if (pipespace(wpipe
, pipe_size
) == 0)
1164 OSAddAtomic(1, &nbigpipe
);
1168 if (wpipe
->pipe_buffer
.buffer
== 0) {
1170 * initial allocation failed
1177 * If an error occurred unbusy and return, waking up any pending
1181 if ((wpipe
->pipe_busy
== 0) &&
1182 (wpipe
->pipe_state
& PIPE_WANT
)) {
1183 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1190 // LP64todo - fix this!
1191 orig_resid
= uio_resid(uio
);
1193 while (uio_resid(uio
)) {
1196 #ifndef PIPE_NODIRECT
1198 * If the transfer is large, we can gain performance if
1199 * we do process-to-process copies directly.
1200 * If the write is non-blocking, we don't use the
1201 * direct write mechanism.
1203 * The direct write mechanism will detect the reader going
1206 if ((uio
->uio_iov
->iov_len
>= PIPE_MINDIRECT
) &&
1207 (fp
->f_flag
& FNONBLOCK
) == 0 &&
1208 amountpipekvawired
+ uio_resid(uio
) < maxpipekvawired
) {
1209 error
= pipe_direct_write(wpipe
, uio
);
1216 * Pipe buffered writes cannot be coincidental with
1217 * direct writes. We wait until the currently executing
1218 * direct write is completed before we start filling the
1219 * pipe buffer. We break out if a signal occurs or the
1223 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1224 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1225 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1228 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipbww", 0);
1230 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))
1238 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1241 * Writes of size <= PIPE_BUF must be atomic.
1243 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
1248 if ((error
= pipelock(wpipe
,1)) == 0) {
1249 int size
; /* Transfer size */
1250 int segsize
; /* first segment to transfer */
1252 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1257 #ifndef PIPE_NODIRECT
1259 * It is possible for a direct write to
1260 * slip in on us... handle it here...
1262 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1268 * If a process blocked in pipelock, our
1269 * value for space might be bad... the mutex
1270 * is dropped while we're blocked
1272 if (space
> (int)(wpipe
->pipe_buffer
.size
-
1273 wpipe
->pipe_buffer
.cnt
)) {
1279 * Transfer size is minimum of uio transfer
1280 * and free space in pipe buffer.
1282 // LP64todo - fix this!
1283 if (space
> uio_resid(uio
))
1284 size
= uio_resid(uio
);
1288 * First segment to transfer is minimum of
1289 * transfer size and contiguous space in
1290 * pipe buffer. If first segment to transfer
1291 * is less than the transfer size, we've got
1292 * a wraparound in the buffer.
1294 segsize
= wpipe
->pipe_buffer
.size
-
1295 wpipe
->pipe_buffer
.in
;
1299 /* Transfer first segment */
1302 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1306 if (error
== 0 && segsize
< size
) {
1308 * Transfer remaining part now, to
1309 * support atomic writes. Wraparound
1312 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1313 wpipe
->pipe_buffer
.size
)
1314 panic("Expected pipe buffer "
1315 "wraparound disappeared");
1319 &wpipe
->pipe_buffer
.buffer
[0],
1320 size
- segsize
, uio
);
1324 wpipe
->pipe_buffer
.in
+= size
;
1325 if (wpipe
->pipe_buffer
.in
>=
1326 wpipe
->pipe_buffer
.size
) {
1327 if (wpipe
->pipe_buffer
.in
!=
1329 wpipe
->pipe_buffer
.size
)
1332 wpipe
->pipe_buffer
.in
= size
-
1336 wpipe
->pipe_buffer
.cnt
+= size
;
1337 if (wpipe
->pipe_buffer
.cnt
>
1338 wpipe
->pipe_buffer
.size
)
1339 panic("Pipe buffer overflow");
1349 * If the "read-side" has been blocked, wake it up now.
1351 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1352 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1356 * don't block on non-blocking I/O
1357 * we'll do the pipeselwakeup on the way out
1359 if (fp
->f_flag
& FNONBLOCK
) {
1365 * If read side wants to go away, we just issue a signal
1368 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1374 * We have no more space and have something to offer,
1375 * wake up select/poll.
1377 pipeselwakeup(wpipe
, wpipe
);
1379 wpipe
->pipe_state
|= PIPE_WANTW
;
1381 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1389 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1390 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1393 if (wpipe
->pipe_buffer
.cnt
> 0) {
1395 * If there are any characters in the buffer, we wake up
1396 * the reader if it was blocked waiting for data.
1398 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1399 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1403 * wake up thread blocked in select/poll or post the notification
1405 pipeselwakeup(wpipe
, wpipe
);
1408 /* Update modification, status change (# of bytes in pipe) times */
1409 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1410 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1417 * we implement a very minimal set of ioctls for compatibility with sockets.
1421 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1422 __unused vfs_context_t ctx
)
1424 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1432 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1448 mpipe
->pipe_state
|= PIPE_ASYNC
;
1450 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1456 #ifndef PIPE_NODIRECT
1457 if (mpipe
->pipe_state
& PIPE_DIRECTW
)
1458 *(int *)data
= mpipe
->pipe_map
.cnt
;
1461 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1466 mpipe
->pipe_pgid
= *(int *)data
;
1472 *(int *)data
= mpipe
->pipe_pgid
;
1484 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1486 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1490 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1495 wpipe
= rpipe
->pipe_peer
;
1499 * XXX We should use a per thread credential here; minimally, the
1500 * XXX process credential should have a persistent reference on it
1501 * XXX before being passed in here.
1503 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1511 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1512 (rpipe
->pipe_buffer
.cnt
> 0) ||
1513 (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1517 rpipe
->pipe_state
|= PIPE_SEL
;
1518 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1524 wpipe
->pipe_state
|= PIPE_WSELECT
;
1525 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1526 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1527 (wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1531 wpipe
->pipe_state
|= PIPE_SEL
;
1532 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1536 rpipe
->pipe_state
|= PIPE_SEL
;
1537 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1548 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1552 proc_fdlock_spin(vfs_context_proc(ctx
));
1553 cpipe
= (struct pipe
*)fg
->fg_data
;
1555 proc_fdunlock(vfs_context_proc(ctx
));
1564 pipe_free_kmem(struct pipe
*cpipe
)
1567 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1568 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1569 OSAddAtomic(-1, &nbigpipe
);
1570 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), &amountpipekva
);
1571 OSAddAtomic(-1, &amountpipes
);
1573 kmem_free(kernel_map
, (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1574 cpipe
->pipe_buffer
.size
);
1575 cpipe
->pipe_buffer
.buffer
= NULL
;
1577 #ifndef PIPE_NODIRECT
1578 if (cpipe
->pipe_map
.kva
!= 0) {
1579 atomic_subtract_int(&amountpipekvawired
,
1580 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1581 kmem_free(kernel_map
,
1582 cpipe
->pipe_map
.kva
,
1583 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1584 cpipe
->pipe_map
.cnt
= 0;
1585 cpipe
->pipe_map
.kva
= 0;
1586 cpipe
->pipe_map
.pos
= 0;
1587 cpipe
->pipe_map
.npages
= 0;
1593 * When a thread sets a write-select on a pipe, it creates an implicit,
1594 * untracked dependency between that thread and the peer of the pipe
1595 * on which the select is set. If the peer pipe is closed and freed
1596 * before the select()ing thread wakes up, the system will panic as
1597 * it attempts to unwind the dangling select(). To avoid that panic,
1598 * we notice whenever a dangerous select() is set on a pipe, and
1599 * defer the final deletion of the pipe until that select()s are all
1600 * resolved. Since we can't currently detect exactly when that
1601 * resolution happens, we use a simple garbage collection queue to
1602 * reap the at-risk pipes 'later'.
1605 pipe_garbage_collect(struct pipe
*cpipe
)
1608 struct pipe_garbage
*pgp
;
1610 /* Convert msecs to nsecs and then to abstime */
1611 old
= pipe_garbage_age_limit
* 1000000;
1612 nanoseconds_to_absolutetime(old
, &old
);
1614 lck_mtx_lock(pipe_garbage_lock
);
1616 /* Free anything that's been on the queue for <mumble> seconds */
1617 now
= mach_absolute_time();
1619 while ((pgp
= pipe_garbage_head
) && pgp
->pg_timestamp
< old
) {
1620 pipe_garbage_head
= pgp
->pg_next
;
1621 if (pipe_garbage_head
== NULL
)
1622 pipe_garbage_tail
= NULL
;
1623 pipe_garbage_count
--;
1624 zfree(pipe_zone
, pgp
->pg_pipe
);
1625 zfree(pipe_garbage_zone
, pgp
);
1628 /* Add the new pipe (if any) to the tail of the garbage queue */
1630 cpipe
->pipe_state
= PIPE_DEAD
;
1631 pgp
= (struct pipe_garbage
*)zalloc(pipe_garbage_zone
);
1634 * We're too low on memory to garbage collect the
1635 * pipe. Freeing it runs the risk of panicing the
1636 * system. All we can do is leak it and leave
1637 * a breadcrumb behind. The good news, such as it
1638 * is, is that this will probably never happen.
1639 * We will probably hit the panic below first.
1641 printf("Leaking pipe %p - no room left in the queue",
1643 lck_mtx_unlock(pipe_garbage_lock
);
1647 pgp
->pg_pipe
= cpipe
;
1648 pgp
->pg_timestamp
= now
;
1649 pgp
->pg_next
= NULL
;
1651 if (pipe_garbage_tail
)
1652 pipe_garbage_tail
->pg_next
= pgp
;
1653 pipe_garbage_tail
= pgp
;
1654 if (pipe_garbage_head
== NULL
)
1655 pipe_garbage_head
= pipe_garbage_tail
;
1657 if (pipe_garbage_count
++ >= PIPE_GARBAGE_QUEUE_LIMIT
)
1658 panic("Length of pipe garbage queue exceeded %d",
1659 PIPE_GARBAGE_QUEUE_LIMIT
);
1661 lck_mtx_unlock(pipe_garbage_lock
);
1668 pipeclose(struct pipe
*cpipe
)
1675 /* partially created pipes won't have a valid mutex. */
1676 if (PIPE_MTX(cpipe
) != NULL
)
1681 * If the other side is blocked, wake it up saying that
1682 * we want to close it down.
1684 cpipe
->pipe_state
&= ~PIPE_DRAIN
;
1685 cpipe
->pipe_state
|= PIPE_EOF
;
1686 pipeselwakeup(cpipe
, cpipe
);
1688 while (cpipe
->pipe_busy
) {
1689 cpipe
->pipe_state
|= PIPE_WANT
;
1692 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1697 * Free the shared pipe label only after the two ends are disconnected.
1699 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1700 mac_pipe_label_destroy(cpipe
);
1704 * Disconnect from peer
1706 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1708 ppipe
->pipe_state
&= ~(PIPE_DRAIN
);
1709 ppipe
->pipe_state
|= PIPE_EOF
;
1711 pipeselwakeup(ppipe
, ppipe
);
1714 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1715 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1717 postpipeevent(ppipe
, EV_RCLOSED
);
1719 ppipe
->pipe_peer
= NULL
;
1726 if (PIPE_MTX(cpipe
) != NULL
) {
1727 if (ppipe
!= NULL
) {
1729 * since the mutex is shared and the peer is still
1730 * alive, we need to release the mutex, not free it
1735 * peer is gone, so we're the sole party left with
1736 * interest in this mutex... we can just free it
1738 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1741 pipe_free_kmem(cpipe
);
1742 if (cpipe
->pipe_state
& PIPE_WSELECT
) {
1743 pipe_garbage_collect(cpipe
);
1745 zfree(pipe_zone
, cpipe
);
1746 pipe_garbage_collect(NULL
);
1752 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
1756 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1761 * XXX We should use a per thread credential here; minimally, the
1762 * XXX process credential should have a persistent reference on it
1763 * XXX before being passed in here.
1765 if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx
), kn
, cpipe
) != 0) {
1771 switch (kn
->kn_filter
) {
1773 kn
->kn_fop
= &pipe_rfiltops
;
1777 kn
->kn_fop
= &pipe_wfiltops
;
1779 if (cpipe
->pipe_peer
== NULL
) {
1781 * other end of pipe has been closed
1786 if (cpipe
->pipe_peer
)
1787 cpipe
= cpipe
->pipe_peer
;
1794 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1795 cpipe
->pipe_state
|= PIPE_KNOTE
;
1802 filt_pipedetach(struct knote
*kn
)
1804 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1808 if (kn
->kn_filter
== EVFILT_WRITE
) {
1809 if (cpipe
->pipe_peer
== NULL
) {
1813 cpipe
= cpipe
->pipe_peer
;
1815 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1816 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1817 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1824 filt_piperead(struct knote
*kn
, long hint
)
1826 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1831 * if hint == 0, then we've been called from the kevent
1832 * world directly and do not currently hold the pipe mutex...
1833 * if hint == 1, we're being called back via the KNOTE post
1834 * we made in pipeselwakeup, and we already hold the mutex...
1839 wpipe
= rpipe
->pipe_peer
;
1840 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1842 #ifndef PIPE_NODIRECT
1843 if ((kn
->kn_data
== 0) && (rpipe
->pipe_state
& PIPE_DIRECTW
))
1844 kn
->kn_data
= rpipe
->pipe_map
.cnt
;
1846 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1847 (wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1848 kn
->kn_flags
|= EV_EOF
;
1852 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1853 if (rpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> rpipe
->pipe_buffer
.size
)
1854 lowwat
= rpipe
->pipe_buffer
.size
;
1855 else if (kn
->kn_sdata
> lowwat
)
1856 lowwat
= kn
->kn_sdata
;
1858 retval
= kn
->kn_data
>= lowwat
;
1869 filt_pipewrite(struct knote
*kn
, long hint
)
1871 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1875 * if hint == 0, then we've been called from the kevent
1876 * world directly and do not currently hold the pipe mutex...
1877 * if hint == 1, we're being called back via the KNOTE post
1878 * we made in pipeselwakeup, and we already hold the mutex...
1883 wpipe
= rpipe
->pipe_peer
;
1885 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1887 kn
->kn_flags
|= EV_EOF
;
1893 kn
->kn_data
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1894 if (!kn
->kn_data
&& wpipe
->pipe_buffer
.size
== 0)
1895 kn
->kn_data
= PIPE_BUF
; /* unwritten pipe is ready for write */
1897 #ifndef PIPE_NODIRECT
1898 if (wpipe
->pipe_state
& PIPE_DIRECTW
)
1901 int64_t lowwat
= PIPE_BUF
;
1902 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1903 if (wpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> wpipe
->pipe_buffer
.size
)
1904 lowwat
= wpipe
->pipe_buffer
.size
;
1905 else if (kn
->kn_sdata
> lowwat
)
1906 lowwat
= kn
->kn_sdata
;
1912 return (kn
->kn_data
>= lowwat
);
1916 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1922 struct vinfo_stat
* ub
;
1931 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1937 if (cpipe
->pipe_buffer
.buffer
== 0) {
1939 * must be stat'ing the write fd
1941 if (cpipe
->pipe_peer
) {
1943 * the peer still exists, use it's info
1945 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
1946 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1951 pipe_size
= cpipe
->pipe_buffer
.size
;
1952 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1955 * since peer's buffer is setup ouside of lock
1956 * we might catch it in transient state
1959 pipe_size
= PIPE_SIZE
;
1961 ub
= &pinfo
->pipe_stat
;
1963 bzero(ub
, sizeof(*ub
));
1964 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1965 ub
->vst_blksize
= pipe_size
;
1966 ub
->vst_size
= pipe_count
;
1967 if (ub
->vst_blksize
!= 0)
1968 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1971 ub
->vst_uid
= kauth_getuid();
1972 ub
->vst_gid
= kauth_getgid();
1975 ub
->vst_atime
= now
.tv_sec
;
1976 ub
->vst_atimensec
= now
.tv_usec
* 1000;
1978 ub
->vst_mtime
= now
.tv_sec
;
1979 ub
->vst_mtimensec
= now
.tv_usec
* 1000;
1981 ub
->vst_ctime
= now
.tv_sec
;
1982 ub
->vst_ctimensec
= now
.tv_usec
* 1000;
1985 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1986 * XXX (st_dev, st_ino) should be unique.
1989 pinfo
->pipe_handle
= (uint64_t)((uintptr_t)cpipe
);
1990 pinfo
->pipe_peerhandle
= (uint64_t)((uintptr_t)(cpipe
->pipe_peer
));
1991 pinfo
->pipe_status
= cpipe
->pipe_state
;
2000 pipe_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
2003 /* Note: fdlock already held */
2004 struct pipe
*ppipe
, *cpipe
= (struct pipe
*)(fp
->f_fglob
->fg_data
);
2008 cpipe
->pipe_state
|= PIPE_DRAIN
;
2009 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
2012 /* Must wake up peer: a writer sleeps on the read side */
2013 if ((ppipe
= cpipe
->pipe_peer
)) {
2014 ppipe
->pipe_state
|= PIPE_DRAIN
;
2015 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);