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
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>
150 #define f_flag f_fglob->fg_flag
151 #define f_type f_fglob->fg_type
152 #define f_msgcount f_fglob->fg_msgcount
153 #define f_cred f_fglob->fg_cred
154 #define f_ops f_fglob->fg_ops
155 #define f_offset f_fglob->fg_offset
156 #define f_data f_fglob->fg_data
159 * interfaces to the outside world exported through file operations
161 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
162 int flags
, vfs_context_t ctx
);
163 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
164 int flags
, vfs_context_t ctx
);
165 static int pipe_close(struct fileglob
*fg
, vfs_context_t ctx
);
166 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
,
168 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
170 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
172 static int pipe_drain(struct fileproc
*fp
,vfs_context_t ctx
);
174 struct fileops pipeops
=
183 static void filt_pipedetach(struct knote
*kn
);
184 static int filt_piperead(struct knote
*kn
, long hint
);
185 static int filt_pipewrite(struct knote
*kn
, long hint
);
187 static struct filterops pipe_rfiltops
= {
189 .f_detach
= filt_pipedetach
,
190 .f_event
= filt_piperead
,
193 static struct filterops pipe_wfiltops
= {
195 .f_detach
= filt_pipedetach
,
196 .f_event
= filt_pipewrite
,
199 static int nbigpipe
; /* for compatibility sake. no longer used */
200 static int amountpipes
; /* total number of pipes in system */
201 static int amountpipekva
; /* total memory used by pipes */
203 int maxpipekva
= PIPE_KVAMAX
; /* allowing 16MB max. */
206 SYSCTL_DECL(_kern_ipc
);
208 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
209 &maxpipekva
, 0, "Pipe KVA limit");
210 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
211 &maxpipekvawired
, 0, "Pipe KVA wired limit");
212 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
213 &amountpipes
, 0, "Current # of pipes");
214 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
215 &nbigpipe
, 0, "Current # of big pipes");
216 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
217 &amountpipekva
, 0, "Pipe KVA usage");
218 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
|CTLFLAG_LOCKED
,
219 &amountpipekvawired
, 0, "Pipe wired KVA usage");
222 static void pipeclose(struct pipe
*cpipe
);
223 static void pipe_free_kmem(struct pipe
*cpipe
);
224 static int pipe_create(struct pipe
**cpipep
);
225 static int pipespace(struct pipe
*cpipe
, int size
);
226 static int choose_pipespace(unsigned long current
, unsigned long expected
);
227 static int expand_pipespace(struct pipe
*p
, int target_size
);
228 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
229 static __inline
int pipeio_lock(struct pipe
*cpipe
, int catch);
230 static __inline
void pipeio_unlock(struct pipe
*cpipe
);
232 extern int postpipeevent(struct pipe
*, int);
233 extern void evpipefree(struct pipe
*cpipe
);
235 static lck_grp_t
*pipe_mtx_grp
;
236 static lck_attr_t
*pipe_mtx_attr
;
237 static lck_grp_attr_t
*pipe_mtx_grp_attr
;
239 static zone_t pipe_zone
;
241 #define MAX_PIPESIZE(pipe) ( MAX(PIPE_SIZE, (pipe)->pipe_buffer.size) )
243 #define PIPE_GARBAGE_AGE_LIMIT 5000 /* In milliseconds */
244 #define PIPE_GARBAGE_QUEUE_LIMIT 32000
246 struct pipe_garbage
{
247 struct pipe
*pg_pipe
;
248 struct pipe_garbage
*pg_next
;
249 uint64_t pg_timestamp
;
252 static zone_t pipe_garbage_zone
;
253 static struct pipe_garbage
*pipe_garbage_head
= NULL
;
254 static struct pipe_garbage
*pipe_garbage_tail
= NULL
;
255 static uint64_t pipe_garbage_age_limit
= PIPE_GARBAGE_AGE_LIMIT
;
256 static int pipe_garbage_count
= 0;
257 static lck_mtx_t
*pipe_garbage_lock
;
258 static void pipe_garbage_collect(struct pipe
*cpipe
);
260 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
262 /* initial setup done at time of sysinit */
269 zone_size
= 8192 * sizeof(struct pipe
);
270 pipe_zone
= zinit(sizeof(struct pipe
), zone_size
, 4096, "pipe zone");
273 /* allocate lock group attribute and group for pipe mutexes */
274 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
275 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
277 /* allocate the lock attribute for pipe mutexes */
278 pipe_mtx_attr
= lck_attr_alloc_init();
281 * Set up garbage collection for dead pipes
283 zone_size
= (PIPE_GARBAGE_QUEUE_LIMIT
+ 20) *
284 sizeof(struct pipe_garbage
);
285 pipe_garbage_zone
= (zone_t
)zinit(sizeof(struct pipe_garbage
),
286 zone_size
, 4096, "pipe garbage zone");
287 pipe_garbage_lock
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
);
291 /* Bitmap for things to touch in pipe_touch() */
292 #define PIPE_ATIME 0x00000001 /* time of last access */
293 #define PIPE_MTIME 0x00000002 /* time of last modification */
294 #define PIPE_CTIME 0x00000004 /* time of last status change */
297 pipe_touch(struct pipe
*tpipe
, int touch
)
303 if (touch
& PIPE_ATIME
) {
304 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
305 tpipe
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
308 if (touch
& PIPE_MTIME
) {
309 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
310 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
313 if (touch
& PIPE_CTIME
) {
314 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
315 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
319 static const unsigned int pipesize_blocks
[] = {128,256,1024,2048,PAGE_SIZE
, PAGE_SIZE
* 2, PIPE_SIZE
, PIPE_SIZE
* 4 };
322 * finds the right size from possible sizes in pipesize_blocks
323 * returns the size which matches max(current,expected)
326 choose_pipespace(unsigned long current
, unsigned long expected
)
328 int i
= sizeof(pipesize_blocks
)/sizeof(unsigned int) -1;
329 unsigned long target
;
331 if (expected
> current
)
336 while ( i
>0 && pipesize_blocks
[i
-1] > target
) {
341 return pipesize_blocks
[i
];
346 * expand the size of pipe while there is data to be read,
347 * and then free the old buffer once the current buffered
348 * data has been transferred to new storage.
349 * Required: PIPE_LOCK and io lock to be held by caller.
350 * returns 0 on success or no expansion possible
353 expand_pipespace(struct pipe
*p
, int target_size
)
355 struct pipe tmp
, oldpipe
;
357 tmp
.pipe_buffer
.buffer
= 0;
359 if (p
->pipe_buffer
.size
>= (unsigned) target_size
) {
360 return 0; /* the existing buffer is max size possible */
363 /* create enough space in the target */
364 error
= pipespace(&tmp
, target_size
);
368 oldpipe
.pipe_buffer
.buffer
= p
->pipe_buffer
.buffer
;
369 oldpipe
.pipe_buffer
.size
= p
->pipe_buffer
.size
;
371 memcpy(tmp
.pipe_buffer
.buffer
, p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
372 if (p
->pipe_buffer
.cnt
> 0 && p
->pipe_buffer
.in
<= p
->pipe_buffer
.out
){
373 /* we are in State 3 and need extra copying for read to be consistent */
374 memcpy(&tmp
.pipe_buffer
.buffer
[p
->pipe_buffer
.size
], p
->pipe_buffer
.buffer
, p
->pipe_buffer
.size
);
375 p
->pipe_buffer
.in
+= p
->pipe_buffer
.size
;
378 p
->pipe_buffer
.buffer
= tmp
.pipe_buffer
.buffer
;
379 p
->pipe_buffer
.size
= tmp
.pipe_buffer
.size
;
382 pipe_free_kmem(&oldpipe
);
387 * The pipe system call for the DTYPE_PIPE type of pipes
390 * FREAD | fd0 | -->[struct rpipe] --> |~~buffer~~| \
392 * FWRITE | fd1 | -->[struct wpipe] --X /
397 pipe(proc_t p
, __unused
struct pipe_args
*uap
, int32_t *retval
)
399 struct fileproc
*rf
, *wf
;
400 struct pipe
*rpipe
, *wpipe
;
404 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
407 rpipe
= wpipe
= NULL
;
408 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
413 * allocate the space for the normal I/O direction up
414 * front... we'll delay the allocation for the other
415 * direction until a write actually occurs (most likely it won't)...
417 error
= pipespace(rpipe
, choose_pipespace(rpipe
->pipe_buffer
.size
, 0));
421 TAILQ_INIT(&rpipe
->pipe_evlist
);
422 TAILQ_INIT(&wpipe
->pipe_evlist
);
424 error
= falloc(p
, &rf
, &fd
, vfs_context_current());
431 * for now we'll create half-duplex pipes(refer returns section above).
432 * this is what we've always supported..
435 rf
->f_type
= DTYPE_PIPE
;
436 rf
->f_data
= (caddr_t
)rpipe
;
437 rf
->f_ops
= &pipeops
;
439 error
= falloc(p
, &wf
, &fd
, vfs_context_current());
441 fp_free(p
, retval
[0], rf
);
445 wf
->f_type
= DTYPE_PIPE
;
446 wf
->f_data
= (caddr_t
)wpipe
;
447 wf
->f_ops
= &pipeops
;
449 rpipe
->pipe_peer
= wpipe
;
450 wpipe
->pipe_peer
= rpipe
;
451 /* both structures share the same mutex */
452 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
457 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
459 * struct pipe represents a pipe endpoint. The MAC label is shared
460 * between the connected endpoints. As a result mac_pipe_label_init() and
461 * mac_pipe_label_associate() should only be called on one of the endpoints
462 * after they have been connected.
464 mac_pipe_label_init(rpipe
);
465 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
466 wpipe
->pipe_label
= rpipe
->pipe_label
;
469 procfdtbl_releasefd(p
, retval
[0], NULL
);
470 procfdtbl_releasefd(p
, retval
[1], NULL
);
471 fp_drop(p
, retval
[0], rf
, 1);
472 fp_drop(p
, retval
[1], wf
, 1);
481 lck_mtx_free(pmtx
, pipe_mtx_grp
);
487 pipe_stat(struct pipe
*cpipe
, void *ub
, int isstat64
)
494 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
495 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
502 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
508 if (cpipe
->pipe_buffer
.buffer
== 0) {
509 /* must be stat'ing the write fd */
510 if (cpipe
->pipe_peer
) {
511 /* the peer still exists, use it's info */
512 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
513 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
518 pipe_size
= MAX_PIPESIZE(cpipe
);
519 pipe_count
= cpipe
->pipe_buffer
.cnt
;
522 * since peer's buffer is setup ouside of lock
523 * we might catch it in transient state
526 pipe_size
= MAX(PIPE_SIZE
, pipesize_blocks
[0]);
529 sb64
= (struct stat64
*)ub
;
531 bzero(sb64
, sizeof(*sb64
));
532 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
533 sb64
->st_blksize
= pipe_size
;
534 sb64
->st_size
= pipe_count
;
535 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
537 sb64
->st_uid
= kauth_getuid();
538 sb64
->st_gid
= kauth_getgid();
540 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
541 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
543 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
544 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
546 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
547 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
550 * Return a relatively unique inode number based on the current
551 * address of this pipe's struct pipe. This number may be recycled
552 * relatively quickly.
554 sb64
->st_ino
= (ino64_t
)VM_KERNEL_ADDRPERM((uintptr_t)cpipe
);
556 sb
= (struct stat
*)ub
;
558 bzero(sb
, sizeof(*sb
));
559 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
560 sb
->st_blksize
= pipe_size
;
561 sb
->st_size
= pipe_count
;
562 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
564 sb
->st_uid
= kauth_getuid();
565 sb
->st_gid
= kauth_getgid();
567 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
568 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
570 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
571 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
573 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
574 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
577 * Return a relatively unique inode number based on the current
578 * address of this pipe's struct pipe. This number may be recycled
579 * relatively quickly.
581 sb
->st_ino
= (ino_t
)VM_KERNEL_ADDRPERM((uintptr_t)cpipe
);
586 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
589 * XXX (st_dev) should be unique, but there is no device driver that
590 * XXX is associated with pipes, since they are implemented via a
591 * XXX struct fileops indirection rather than as FS objects.
598 * Allocate kva for pipe circular buffer, the space is pageable
599 * This routine will 'realloc' the size of a pipe safely, if it fails
600 * it will retain the old buffer.
601 * If it fails it will return ENOMEM.
604 pipespace(struct pipe
*cpipe
, int size
)
611 if ((buffer
= (vm_offset_t
)kalloc(size
)) == 0 )
614 /* free old resources if we're resizing */
615 pipe_free_kmem(cpipe
);
616 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
617 cpipe
->pipe_buffer
.size
= size
;
618 cpipe
->pipe_buffer
.in
= 0;
619 cpipe
->pipe_buffer
.out
= 0;
620 cpipe
->pipe_buffer
.cnt
= 0;
622 OSAddAtomic(1, &amountpipes
);
623 OSAddAtomic(cpipe
->pipe_buffer
.size
, &amountpipekva
);
629 * initialize and allocate VM and memory for pipe
632 pipe_create(struct pipe
**cpipep
)
635 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
637 if ((*cpipep
= cpipe
) == NULL
)
641 * protect so pipespace or pipeclose don't follow a junk pointer
642 * if pipespace() fails.
644 bzero(cpipe
, sizeof *cpipe
);
646 /* Initial times are all the time of creation of the pipe */
647 pipe_touch(cpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
653 * lock a pipe for I/O, blocking other access
656 pipeio_lock(struct pipe
*cpipe
, int catch)
659 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
660 cpipe
->pipe_state
|= PIPE_LWANT
;
661 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
666 cpipe
->pipe_state
|= PIPE_LOCKFL
;
671 * unlock a pipe I/O lock
674 pipeio_unlock(struct pipe
*cpipe
)
676 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
677 if (cpipe
->pipe_state
& PIPE_LWANT
) {
678 cpipe
->pipe_state
&= ~PIPE_LWANT
;
684 * wakeup anyone whos blocked in select
687 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
689 if (cpipe
->pipe_state
& PIPE_SEL
) {
690 cpipe
->pipe_state
&= ~PIPE_SEL
;
691 selwakeup(&cpipe
->pipe_sel
);
693 if (cpipe
->pipe_state
& PIPE_KNOTE
)
694 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
696 postpipeevent(cpipe
, EV_RWBYTES
);
698 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
699 if (spipe
->pipe_pgid
< 0)
700 gsignal(-spipe
->pipe_pgid
, SIGIO
);
702 proc_signal(spipe
->pipe_pgid
, SIGIO
);
707 * Read n bytes from the buffer. Semantics are similar to file read.
708 * returns: number of bytes read from the buffer
712 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
713 __unused vfs_context_t ctx
)
715 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
723 error
= pipeio_lock(rpipe
, 1);
728 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
734 while (uio_resid(uio
)) {
736 * normal pipe buffer receive
738 if (rpipe
->pipe_buffer
.cnt
> 0) {
740 * # bytes to read is min( bytes from read pointer until end of buffer,
741 * total unread bytes,
742 * user requested byte count)
744 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
745 if (size
> rpipe
->pipe_buffer
.cnt
)
746 size
= rpipe
->pipe_buffer
.cnt
;
747 // LP64todo - fix this!
748 if (size
> (u_int
) uio_resid(uio
))
749 size
= (u_int
) uio_resid(uio
);
751 PIPE_UNLOCK(rpipe
); /* we still hold io lock.*/
753 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
759 rpipe
->pipe_buffer
.out
+= size
;
760 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
761 rpipe
->pipe_buffer
.out
= 0;
763 rpipe
->pipe_buffer
.cnt
-= size
;
766 * If there is no more to read in the pipe, reset
767 * its pointers to the beginning. This improves
770 if (rpipe
->pipe_buffer
.cnt
== 0) {
771 rpipe
->pipe_buffer
.in
= 0;
772 rpipe
->pipe_buffer
.out
= 0;
777 * detect EOF condition
778 * read returns 0 on EOF, no need to set error
780 if (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
785 * If the "write-side" has been blocked, wake it up now.
787 if (rpipe
->pipe_state
& PIPE_WANTW
) {
788 rpipe
->pipe_state
&= ~PIPE_WANTW
;
793 * Break if some data was read in previous iteration.
799 * Unlock the pipe buffer for our remaining processing.
800 * We will either break out with an error or we will
801 * sleep and relock to loop.
803 pipeio_unlock(rpipe
);
806 * Handle non-blocking mode operation or
807 * wait for more data.
809 if (fp
->f_flag
& FNONBLOCK
) {
812 rpipe
->pipe_state
|= PIPE_WANTR
;
813 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
815 error
= pipeio_lock(rpipe
, 1);
824 pipeio_unlock(rpipe
);
830 * PIPE_WANT processing only makes sense if pipe_busy is 0.
832 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
833 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
835 } else if (rpipe
->pipe_buffer
.cnt
< rpipe
->pipe_buffer
.size
) {
837 * Handle write blocking hysteresis.
839 if (rpipe
->pipe_state
& PIPE_WANTW
) {
840 rpipe
->pipe_state
&= ~PIPE_WANTW
;
845 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) > 0)
846 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
848 /* update last read time */
849 pipe_touch(rpipe
, PIPE_ATIME
);
857 * perform a write of n bytes into the read side of buffer. Since
858 * pipes are unidirectional a write is meant to be read by the otherside only.
861 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
862 __unused vfs_context_t ctx
)
867 struct pipe
*wpipe
, *rpipe
;
868 // LP64todo - fix this!
869 orig_resid
= uio_resid(uio
);
872 rpipe
= (struct pipe
*)fp
->f_data
;
875 wpipe
= rpipe
->pipe_peer
;
878 * detect loss of pipe read side, issue SIGPIPE if lost.
880 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
885 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
896 * need to allocate some storage... we delay the allocation
897 * until the first write on fd[0] to avoid allocating storage for both
898 * 'pipe ends'... most pipes are half-duplex with the writes targeting
899 * fd[1], so allocating space for both ends is a waste...
902 if ( wpipe
->pipe_buffer
.buffer
== 0 || (
903 (unsigned)orig_resid
> wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
&&
904 amountpipekva
< maxpipekva
) ) {
906 pipe_size
= choose_pipespace(wpipe
->pipe_buffer
.size
, wpipe
->pipe_buffer
.cnt
+ orig_resid
);
910 * need to do initial allocation or resizing of pipe
911 * holding both structure and io locks.
913 if ((error
= pipeio_lock(wpipe
, 1)) == 0) {
914 if (wpipe
->pipe_buffer
.cnt
== 0)
915 error
= pipespace(wpipe
, pipe_size
);
917 error
= expand_pipespace(wpipe
, pipe_size
);
919 pipeio_unlock(wpipe
);
921 /* allocation failed */
922 if (wpipe
->pipe_buffer
.buffer
== 0)
927 * If an error occurred unbusy and return, waking up any pending
931 if ((wpipe
->pipe_busy
== 0) &&
932 (wpipe
->pipe_state
& PIPE_WANT
)) {
933 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
941 while (uio_resid(uio
)) {
944 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
946 /* Writes of size <= PIPE_BUF must be atomic. */
947 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
952 if ((error
= pipeio_lock(wpipe
,1)) == 0) {
953 int size
; /* Transfer size */
954 int segsize
; /* first segment to transfer */
956 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
957 pipeio_unlock(wpipe
);
962 * If a process blocked in pipeio_lock, our
963 * value for space might be bad... the mutex
964 * is dropped while we're blocked
966 if (space
> (int)(wpipe
->pipe_buffer
.size
-
967 wpipe
->pipe_buffer
.cnt
)) {
968 pipeio_unlock(wpipe
);
973 * Transfer size is minimum of uio transfer
974 * and free space in pipe buffer.
976 // LP64todo - fix this!
977 if (space
> uio_resid(uio
))
978 size
= uio_resid(uio
);
982 * First segment to transfer is minimum of
983 * transfer size and contiguous space in
984 * pipe buffer. If first segment to transfer
985 * is less than the transfer size, we've got
986 * a wraparound in the buffer.
988 segsize
= wpipe
->pipe_buffer
.size
-
989 wpipe
->pipe_buffer
.in
;
993 /* Transfer first segment */
996 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1000 if (error
== 0 && segsize
< size
) {
1002 * Transfer remaining part now, to
1003 * support atomic writes. Wraparound
1004 * happened. (State 3)
1006 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1007 wpipe
->pipe_buffer
.size
)
1008 panic("Expected pipe buffer "
1009 "wraparound disappeared");
1013 &wpipe
->pipe_buffer
.buffer
[0],
1014 size
- segsize
, uio
);
1018 * readers never know to read until count is updated.
1021 wpipe
->pipe_buffer
.in
+= size
;
1022 if (wpipe
->pipe_buffer
.in
>
1023 wpipe
->pipe_buffer
.size
) {
1024 if (wpipe
->pipe_buffer
.in
!=
1026 wpipe
->pipe_buffer
.size
)
1029 wpipe
->pipe_buffer
.in
= size
-
1033 wpipe
->pipe_buffer
.cnt
+= size
;
1034 if (wpipe
->pipe_buffer
.cnt
>
1035 wpipe
->pipe_buffer
.size
)
1036 panic("Pipe buffer overflow");
1039 pipeio_unlock(wpipe
);
1046 * If the "read-side" has been blocked, wake it up now.
1048 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1049 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1053 * don't block on non-blocking I/O
1054 * we'll do the pipeselwakeup on the way out
1056 if (fp
->f_flag
& FNONBLOCK
) {
1062 * If read side wants to go away, we just issue a signal
1065 if (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) {
1071 * We have no more space and have something to offer,
1072 * wake up select/poll.
1074 pipeselwakeup(wpipe
, wpipe
);
1076 wpipe
->pipe_state
|= PIPE_WANTW
;
1078 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1086 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1087 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1090 if (wpipe
->pipe_buffer
.cnt
> 0) {
1092 * If there are any characters in the buffer, we wake up
1093 * the reader if it was blocked waiting for data.
1095 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1096 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1100 * wake up thread blocked in select/poll or post the notification
1102 pipeselwakeup(wpipe
, wpipe
);
1105 /* Update modification, status change (# of bytes in pipe) times */
1106 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1107 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1114 * we implement a very minimal set of ioctls for compatibility with sockets.
1118 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1119 __unused vfs_context_t ctx
)
1121 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1129 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1145 mpipe
->pipe_state
|= PIPE_ASYNC
;
1147 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1153 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1158 mpipe
->pipe_pgid
= *(int *)data
;
1164 *(int *)data
= mpipe
->pipe_pgid
;
1176 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1178 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1182 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1187 wpipe
= rpipe
->pipe_peer
;
1192 * XXX We should use a per thread credential here; minimally, the
1193 * XXX process credential should have a persistent reference on it
1194 * XXX before being passed in here.
1196 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1204 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1205 (rpipe
->pipe_buffer
.cnt
> 0) ||
1206 (rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1210 rpipe
->pipe_state
|= PIPE_SEL
;
1211 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1217 wpipe
->pipe_state
|= PIPE_WSELECT
;
1218 if (wpipe
== NULL
|| (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1219 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1220 (MAX_PIPESIZE(wpipe
) - wpipe
->pipe_buffer
.cnt
) > 0)) {
1224 wpipe
->pipe_state
|= PIPE_SEL
;
1225 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1229 rpipe
->pipe_state
|= PIPE_SEL
;
1230 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1241 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1245 proc_fdlock_spin(vfs_context_proc(ctx
));
1246 cpipe
= (struct pipe
*)fg
->fg_data
;
1248 proc_fdunlock(vfs_context_proc(ctx
));
1256 pipe_free_kmem(struct pipe
*cpipe
)
1258 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1259 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), &amountpipekva
);
1260 OSAddAtomic(-1, &amountpipes
);
1261 kfree((void *)cpipe
->pipe_buffer
.buffer
,
1262 cpipe
->pipe_buffer
.size
);
1263 cpipe
->pipe_buffer
.buffer
= NULL
;
1264 cpipe
->pipe_buffer
.size
= 0;
1272 pipeclose(struct pipe
*cpipe
)
1278 /* partially created pipes won't have a valid mutex. */
1279 if (PIPE_MTX(cpipe
) != NULL
)
1284 * If the other side is blocked, wake it up saying that
1285 * we want to close it down.
1287 cpipe
->pipe_state
&= ~PIPE_DRAIN
;
1288 cpipe
->pipe_state
|= PIPE_EOF
;
1289 pipeselwakeup(cpipe
, cpipe
);
1291 while (cpipe
->pipe_busy
) {
1292 cpipe
->pipe_state
|= PIPE_WANT
;
1295 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1300 * Free the shared pipe label only after the two ends are disconnected.
1302 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1303 mac_pipe_label_destroy(cpipe
);
1307 * Disconnect from peer
1309 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1311 ppipe
->pipe_state
&= ~(PIPE_DRAIN
);
1312 ppipe
->pipe_state
|= PIPE_EOF
;
1314 pipeselwakeup(ppipe
, ppipe
);
1317 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1318 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1320 postpipeevent(ppipe
, EV_RCLOSED
);
1322 ppipe
->pipe_peer
= NULL
;
1329 if (PIPE_MTX(cpipe
) != NULL
) {
1330 if (ppipe
!= NULL
) {
1332 * since the mutex is shared and the peer is still
1333 * alive, we need to release the mutex, not free it
1338 * peer is gone, so we're the sole party left with
1339 * interest in this mutex... we can just free it
1341 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1344 pipe_free_kmem(cpipe
);
1345 if (cpipe
->pipe_state
& PIPE_WSELECT
) {
1346 pipe_garbage_collect(cpipe
);
1348 zfree(pipe_zone
, cpipe
);
1349 pipe_garbage_collect(NULL
);
1356 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
1360 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1365 * XXX We should use a per thread credential here; minimally, the
1366 * XXX process credential should have a persistent reference on it
1367 * XXX before being passed in here.
1369 if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx
), kn
, cpipe
) != 0) {
1375 switch (kn
->kn_filter
) {
1377 kn
->kn_fop
= &pipe_rfiltops
;
1381 kn
->kn_fop
= &pipe_wfiltops
;
1383 if (cpipe
->pipe_peer
== NULL
) {
1385 * other end of pipe has been closed
1390 if (cpipe
->pipe_peer
)
1391 cpipe
= cpipe
->pipe_peer
;
1398 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1399 cpipe
->pipe_state
|= PIPE_KNOTE
;
1406 filt_pipedetach(struct knote
*kn
)
1408 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1412 if (kn
->kn_filter
== EVFILT_WRITE
) {
1413 if (cpipe
->pipe_peer
== NULL
) {
1417 cpipe
= cpipe
->pipe_peer
;
1419 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1420 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1421 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1428 filt_piperead(struct knote
*kn
, long hint
)
1430 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1435 * if hint == 0, then we've been called from the kevent
1436 * world directly and do not currently hold the pipe mutex...
1437 * if hint == 1, we're being called back via the KNOTE post
1438 * we made in pipeselwakeup, and we already hold the mutex...
1443 wpipe
= rpipe
->pipe_peer
;
1444 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1445 if ((rpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
)) ||
1446 (wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1447 kn
->kn_flags
|= EV_EOF
;
1451 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1452 if (rpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> MAX_PIPESIZE(rpipe
))
1453 lowwat
= MAX_PIPESIZE(rpipe
);
1454 else if (kn
->kn_sdata
> lowwat
)
1455 lowwat
= kn
->kn_sdata
;
1457 retval
= kn
->kn_data
>= lowwat
;
1468 filt_pipewrite(struct knote
*kn
, long hint
)
1470 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1474 * if hint == 0, then we've been called from the kevent
1475 * world directly and do not currently hold the pipe mutex...
1476 * if hint == 1, we're being called back via the KNOTE post
1477 * we made in pipeselwakeup, and we already hold the mutex...
1482 wpipe
= rpipe
->pipe_peer
;
1484 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& (PIPE_DRAIN
| PIPE_EOF
))) {
1486 kn
->kn_flags
|= EV_EOF
;
1492 kn
->kn_data
= MAX_PIPESIZE(wpipe
) - wpipe
->pipe_buffer
.cnt
;
1494 int64_t lowwat
= PIPE_BUF
;
1495 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
1496 if (wpipe
->pipe_buffer
.size
&& kn
->kn_sdata
> MAX_PIPESIZE(wpipe
))
1497 lowwat
= MAX_PIPESIZE(wpipe
);
1498 else if (kn
->kn_sdata
> lowwat
)
1499 lowwat
= kn
->kn_sdata
;
1505 return (kn
->kn_data
>= lowwat
);
1509 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1515 struct vinfo_stat
* ub
;
1524 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1530 if (cpipe
->pipe_buffer
.buffer
== 0) {
1532 * must be stat'ing the write fd
1534 if (cpipe
->pipe_peer
) {
1536 * the peer still exists, use it's info
1538 pipe_size
= MAX_PIPESIZE(cpipe
->pipe_peer
);
1539 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1544 pipe_size
= MAX_PIPESIZE(cpipe
);
1545 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1548 * since peer's buffer is setup ouside of lock
1549 * we might catch it in transient state
1552 pipe_size
= PIPE_SIZE
;
1554 ub
= &pinfo
->pipe_stat
;
1556 bzero(ub
, sizeof(*ub
));
1557 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1558 ub
->vst_blksize
= pipe_size
;
1559 ub
->vst_size
= pipe_count
;
1560 if (ub
->vst_blksize
!= 0)
1561 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1564 ub
->vst_uid
= kauth_getuid();
1565 ub
->vst_gid
= kauth_getgid();
1568 ub
->vst_atime
= now
.tv_sec
;
1569 ub
->vst_atimensec
= now
.tv_usec
* 1000;
1571 ub
->vst_mtime
= now
.tv_sec
;
1572 ub
->vst_mtimensec
= now
.tv_usec
* 1000;
1574 ub
->vst_ctime
= now
.tv_sec
;
1575 ub
->vst_ctimensec
= now
.tv_usec
* 1000;
1578 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1579 * XXX (st_dev, st_ino) should be unique.
1582 pinfo
->pipe_handle
= (uint64_t)((uintptr_t)cpipe
);
1583 pinfo
->pipe_peerhandle
= (uint64_t)((uintptr_t)(cpipe
->pipe_peer
));
1584 pinfo
->pipe_status
= cpipe
->pipe_state
;
1593 pipe_drain(struct fileproc
*fp
, __unused vfs_context_t ctx
)
1596 /* Note: fdlock already held */
1597 struct pipe
*ppipe
, *cpipe
= (struct pipe
*)(fp
->f_fglob
->fg_data
);
1601 cpipe
->pipe_state
|= PIPE_DRAIN
;
1602 cpipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1605 /* Must wake up peer: a writer sleeps on the read side */
1606 if ((ppipe
= cpipe
->pipe_peer
)) {
1607 ppipe
->pipe_state
|= PIPE_DRAIN
;
1608 ppipe
->pipe_state
&= ~(PIPE_WANTR
| PIPE_WANTW
);
1621 * When a thread sets a write-select on a pipe, it creates an implicit,
1622 * untracked dependency between that thread and the peer of the pipe
1623 * on which the select is set. If the peer pipe is closed and freed
1624 * before the select()ing thread wakes up, the system will panic as
1625 * it attempts to unwind the dangling select(). To avoid that panic,
1626 * we notice whenever a dangerous select() is set on a pipe, and
1627 * defer the final deletion of the pipe until that select()s are all
1628 * resolved. Since we can't currently detect exactly when that
1629 * resolution happens, we use a simple garbage collection queue to
1630 * reap the at-risk pipes 'later'.
1633 pipe_garbage_collect(struct pipe
*cpipe
)
1636 struct pipe_garbage
*pgp
;
1638 /* Convert msecs to nsecs and then to abstime */
1639 old
= pipe_garbage_age_limit
* 1000000;
1640 nanoseconds_to_absolutetime(old
, &old
);
1642 lck_mtx_lock(pipe_garbage_lock
);
1644 /* Free anything that's been on the queue for <mumble> seconds */
1645 now
= mach_absolute_time();
1647 while ((pgp
= pipe_garbage_head
) && pgp
->pg_timestamp
< old
) {
1648 pipe_garbage_head
= pgp
->pg_next
;
1649 if (pipe_garbage_head
== NULL
)
1650 pipe_garbage_tail
= NULL
;
1651 pipe_garbage_count
--;
1652 zfree(pipe_zone
, pgp
->pg_pipe
);
1653 zfree(pipe_garbage_zone
, pgp
);
1656 /* Add the new pipe (if any) to the tail of the garbage queue */
1658 cpipe
->pipe_state
= PIPE_DEAD
;
1659 pgp
= (struct pipe_garbage
*)zalloc(pipe_garbage_zone
);
1662 * We're too low on memory to garbage collect the
1663 * pipe. Freeing it runs the risk of panicing the
1664 * system. All we can do is leak it and leave
1665 * a breadcrumb behind. The good news, such as it
1666 * is, is that this will probably never happen.
1667 * We will probably hit the panic below first.
1669 printf("Leaking pipe %p - no room left in the queue",
1671 lck_mtx_unlock(pipe_garbage_lock
);
1675 pgp
->pg_pipe
= cpipe
;
1676 pgp
->pg_timestamp
= now
;
1677 pgp
->pg_next
= NULL
;
1679 if (pipe_garbage_tail
)
1680 pipe_garbage_tail
->pg_next
= pgp
;
1681 pipe_garbage_tail
= pgp
;
1682 if (pipe_garbage_head
== NULL
)
1683 pipe_garbage_head
= pipe_garbage_tail
;
1685 if (pipe_garbage_count
++ >= PIPE_GARBAGE_QUEUE_LIMIT
)
1686 panic("Length of pipe garbage queue exceeded %d",
1687 PIPE_GARBAGE_QUEUE_LIMIT
);
1689 lck_mtx_unlock(pipe_garbage_lock
);