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 <bsm/audit_kernel.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>
163 * interfaces to the outside world
165 static int pipe_read(struct fileproc
*fp
, struct uio
*uio
,
166 int flags
, vfs_context_t ctx
);
168 static int pipe_write(struct fileproc
*fp
, struct uio
*uio
,
169 int flags
, vfs_context_t ctx
);
171 static int pipe_close(struct fileglob
*fg
, vfs_context_t ctx
);
173 static int pipe_select(struct fileproc
*fp
, int which
, void * wql
,
176 static int pipe_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
179 static int pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
183 struct fileops pipeops
=
193 static void filt_pipedetach(struct knote
*kn
);
194 static int filt_piperead(struct knote
*kn
, long hint
);
195 static int filt_pipewrite(struct knote
*kn
, long hint
);
197 static struct filterops pipe_rfiltops
=
198 { 1, NULL
, filt_pipedetach
, filt_piperead
};
199 static struct filterops pipe_wfiltops
=
200 { 1, NULL
, filt_pipedetach
, filt_pipewrite
};
203 * Default pipe buffer size(s), this can be kind-of large now because pipe
204 * space is pageable. The pipe code will try to maintain locality of
205 * reference for performance reasons, so small amounts of outstanding I/O
206 * will not wipe the cache.
208 #define MINPIPESIZE (PIPE_SIZE/3)
211 * Limit the number of "big" pipes
213 #define LIMITBIGPIPES 32
216 static int amountpipes
;
217 static int amountpipekva
;
219 #ifndef PIPE_NODIRECT
220 static int amountpipekvawired
;
222 int maxpipekva
= 1024 * 1024 * 16;
225 SYSCTL_DECL(_kern_ipc
);
227 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekva
, CTLFLAG_RD
,
228 &maxpipekva
, 0, "Pipe KVA limit");
229 SYSCTL_INT(_kern_ipc
, OID_AUTO
, maxpipekvawired
, CTLFLAG_RW
,
230 &maxpipekvawired
, 0, "Pipe KVA wired limit");
231 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipes
, CTLFLAG_RD
,
232 &amountpipes
, 0, "Current # of pipes");
233 SYSCTL_INT(_kern_ipc
, OID_AUTO
, bigpipes
, CTLFLAG_RD
,
234 &nbigpipe
, 0, "Current # of big pipes");
235 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekva
, CTLFLAG_RD
,
236 &amountpipekva
, 0, "Pipe KVA usage");
237 SYSCTL_INT(_kern_ipc
, OID_AUTO
, pipekvawired
, CTLFLAG_RD
,
238 &amountpipekvawired
, 0, "Pipe wired KVA usage");
241 static void pipeclose(struct pipe
*cpipe
);
242 static void pipe_free_kmem(struct pipe
*cpipe
);
243 static int pipe_create(struct pipe
**cpipep
);
244 static void pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
);
245 static __inline
int pipelock(struct pipe
*cpipe
, int catch);
246 static __inline
void pipeunlock(struct pipe
*cpipe
);
248 #ifndef PIPE_NODIRECT
249 static int pipe_build_write_buffer(struct pipe
*wpipe
, struct uio
*uio
);
250 static void pipe_destroy_write_buffer(struct pipe
*wpipe
);
251 static int pipe_direct_write(struct pipe
*wpipe
, struct uio
*uio
);
252 static void pipe_clone_write_buffer(struct pipe
*wpipe
);
255 extern int postpipeevent(struct pipe
*, int);
256 extern void evpipefree(struct pipe
*cpipe
);
259 static int pipespace(struct pipe
*cpipe
, int size
);
261 static lck_grp_t
*pipe_mtx_grp
;
262 static lck_attr_t
*pipe_mtx_attr
;
263 static lck_grp_attr_t
*pipe_mtx_grp_attr
;
265 static zone_t pipe_zone
;
267 SYSINIT(vfs
, SI_SUB_VFS
, SI_ORDER_ANY
, pipeinit
, NULL
);
272 pipe_zone
= (zone_t
)zinit(sizeof(struct pipe
), 8192 * sizeof(struct pipe
), 4096, "pipe zone");
275 * allocate lock group attribute and group for pipe mutexes
277 pipe_mtx_grp_attr
= lck_grp_attr_alloc_init();
278 pipe_mtx_grp
= lck_grp_alloc_init("pipe", pipe_mtx_grp_attr
);
281 * allocate the lock attribute for pipe mutexes
283 pipe_mtx_attr
= lck_attr_alloc_init();
286 /* Bitmap for things to touch in pipe_touch() */
287 #define PIPE_ATIME 0x00000001 /* time of last access */
288 #define PIPE_MTIME 0x00000002 /* time of last modification */
289 #define PIPE_CTIME 0x00000004 /* time of last status change */
292 pipe_touch(struct pipe
*tpipe
, int touch
)
298 if (touch
& PIPE_ATIME
) {
299 tpipe
->st_atimespec
.tv_sec
= now
.tv_sec
;
300 tpipe
->st_atimespec
.tv_nsec
= now
.tv_usec
* 1000;
303 if (touch
& PIPE_MTIME
) {
304 tpipe
->st_mtimespec
.tv_sec
= now
.tv_sec
;
305 tpipe
->st_mtimespec
.tv_nsec
= now
.tv_usec
* 1000;
308 if (touch
& PIPE_CTIME
) {
309 tpipe
->st_ctimespec
.tv_sec
= now
.tv_sec
;
310 tpipe
->st_ctimespec
.tv_nsec
= now
.tv_usec
* 1000;
317 * The pipe system call for the DTYPE_PIPE type of pipes
322 pipe(proc_t p
, __unused
struct pipe_args
*uap
, register_t
*retval
)
324 struct fileproc
*rf
, *wf
;
325 struct pipe
*rpipe
, *wpipe
;
329 if ((pmtx
= lck_mtx_alloc_init(pipe_mtx_grp
, pipe_mtx_attr
)) == NULL
)
332 rpipe
= wpipe
= NULL
;
333 if (pipe_create(&rpipe
) || pipe_create(&wpipe
)) {
338 * allocate the space for the normal I/O direction up
339 * front... we'll delay the allocation for the other
340 * direction until a write actually occurs (most
341 * likely it won't)...
343 * Reduce to 1/4th pipe size if we're over our global max.
345 if (amountpipekva
> maxpipekva
/ 2)
346 error
= pipespace(rpipe
, SMALL_PIPE_SIZE
);
348 error
= pipespace(rpipe
, PIPE_SIZE
);
352 #ifndef PIPE_NODIRECT
353 rpipe
->pipe_state
|= PIPE_DIRECTOK
;
354 wpipe
->pipe_state
|= PIPE_DIRECTOK
;
356 TAILQ_INIT(&rpipe
->pipe_evlist
);
357 TAILQ_INIT(&wpipe
->pipe_evlist
);
359 error
= falloc(p
, &rf
, &fd
, vfs_context_current());
366 * for now we'll create half-duplex
367 * pipes... this is what we've always
371 rf
->f_type
= DTYPE_PIPE
;
372 rf
->f_data
= (caddr_t
)rpipe
;
373 rf
->f_ops
= &pipeops
;
375 error
= falloc(p
, &wf
, &fd
, vfs_context_current());
377 fp_free(p
, retval
[0], rf
);
381 wf
->f_type
= DTYPE_PIPE
;
382 wf
->f_data
= (caddr_t
)wpipe
;
383 wf
->f_ops
= &pipeops
;
385 rpipe
->pipe_peer
= wpipe
;
386 wpipe
->pipe_peer
= rpipe
;
387 rpipe
->pipe_mtxp
= wpipe
->pipe_mtxp
= pmtx
;
392 * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX
394 * struct pipe represents a pipe endpoint. The MAC label is shared
395 * between the connected endpoints. As a result mac_pipe_label_init() and
396 * mac_pipe_label_associate() should only be called on one of the endpoints
397 * after they have been connected.
399 mac_pipe_label_init(rpipe
);
400 mac_pipe_label_associate(kauth_cred_get(), rpipe
);
401 wpipe
->pipe_label
= rpipe
->pipe_label
;
404 procfdtbl_releasefd(p
, retval
[0], NULL
);
405 procfdtbl_releasefd(p
, retval
[1], NULL
);
406 fp_drop(p
, retval
[0], rf
, 1);
407 fp_drop(p
, retval
[1], wf
, 1);
416 lck_mtx_free(pmtx
, pipe_mtx_grp
);
422 pipe_stat(struct pipe
*cpipe
, void *ub
, int isstat64
)
429 struct stat
*sb
= (struct stat
*)0; /* warning avoidance ; protected by isstat64 */
430 struct stat64
* sb64
= (struct stat64
*)0; /* warning avoidance ; protected by isstat64 */
437 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
443 if (cpipe
->pipe_buffer
.buffer
== 0) {
445 * must be stat'ing the write fd
447 if (cpipe
->pipe_peer
) {
449 * the peer still exists, use it's info
451 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
452 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
457 pipe_size
= cpipe
->pipe_buffer
.size
;
458 pipe_count
= cpipe
->pipe_buffer
.cnt
;
461 * since peer's buffer is setup ouside of lock
462 * we might catch it in transient state
465 pipe_size
= PIPE_SIZE
;
468 sb64
= (struct stat64
*)ub
;
470 bzero(sb64
, sizeof(*sb64
));
471 sb64
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
472 sb64
->st_blksize
= pipe_size
;
473 sb64
->st_size
= pipe_count
;
474 sb64
->st_blocks
= (sb64
->st_size
+ sb64
->st_blksize
- 1) / sb64
->st_blksize
;
476 sb64
->st_uid
= kauth_getuid();
477 sb64
->st_gid
= kauth_getgid();
479 sb64
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
480 sb64
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
482 sb64
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
483 sb64
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
485 sb64
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
486 sb64
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
489 * Return a relatively unique inode number based on the current
490 * address of this pipe's struct pipe. This number may be recycled
491 * relatively quickly.
493 sb64
->st_ino
= (ino64_t
)((uint32_t)cpipe
);
495 sb
= (struct stat
*)ub
;
497 bzero(sb
, sizeof(*sb
));
498 sb
->st_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
499 sb
->st_blksize
= pipe_size
;
500 sb
->st_size
= pipe_count
;
501 sb
->st_blocks
= (sb
->st_size
+ sb
->st_blksize
- 1) / sb
->st_blksize
;
503 sb
->st_uid
= kauth_getuid();
504 sb
->st_gid
= kauth_getgid();
506 sb
->st_atimespec
.tv_sec
= cpipe
->st_atimespec
.tv_sec
;
507 sb
->st_atimespec
.tv_nsec
= cpipe
->st_atimespec
.tv_nsec
;
509 sb
->st_mtimespec
.tv_sec
= cpipe
->st_mtimespec
.tv_sec
;
510 sb
->st_mtimespec
.tv_nsec
= cpipe
->st_mtimespec
.tv_nsec
;
512 sb
->st_ctimespec
.tv_sec
= cpipe
->st_ctimespec
.tv_sec
;
513 sb
->st_ctimespec
.tv_nsec
= cpipe
->st_ctimespec
.tv_nsec
;
516 * Return a relatively unique inode number based on the current
517 * address of this pipe's struct pipe. This number may be recycled
518 * relatively quickly.
520 sb
->st_ino
= (ino_t
)cpipe
;
525 * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen,
528 * XXX (st_dev) should be unique, but there is no device driver that
529 * XXX is associated with pipes, since they are implemented via a
530 * XXX struct fileops indirection rather than as FS objects.
537 * Allocate kva for pipe circular buffer, the space is pageable
538 * This routine will 'realloc' the size of a pipe safely, if it fails
539 * it will retain the old buffer.
540 * If it fails it will return ENOMEM.
543 pipespace(struct pipe
*cpipe
, int size
)
547 size
= round_page(size
);
549 if (kmem_alloc(kernel_map
, &buffer
, size
) != KERN_SUCCESS
)
552 /* free old resources if we're resizing */
553 pipe_free_kmem(cpipe
);
554 cpipe
->pipe_buffer
.buffer
= (caddr_t
)buffer
;
555 cpipe
->pipe_buffer
.size
= size
;
556 cpipe
->pipe_buffer
.in
= 0;
557 cpipe
->pipe_buffer
.out
= 0;
558 cpipe
->pipe_buffer
.cnt
= 0;
560 OSAddAtomic(1, (SInt32
*)&amountpipes
);
561 OSAddAtomic(cpipe
->pipe_buffer
.size
, (SInt32
*)&amountpipekva
);
567 * initialize and allocate VM and memory for pipe
570 pipe_create(struct pipe
**cpipep
)
574 cpipe
= (struct pipe
*)zalloc(pipe_zone
);
576 if ((*cpipep
= cpipe
) == NULL
)
580 * protect so pipespace or pipeclose don't follow a junk pointer
581 * if pipespace() fails.
583 bzero(cpipe
, sizeof *cpipe
);
585 /* Initial times are all the time of creation of the pipe */
586 pipe_touch(cpipe
, PIPE_ATIME
| PIPE_MTIME
| PIPE_CTIME
);
593 * lock a pipe for I/O, blocking other access
596 pipelock(struct pipe
*cpipe
, int catch)
600 while (cpipe
->pipe_state
& PIPE_LOCKFL
) {
601 cpipe
->pipe_state
|= PIPE_LWANT
;
603 error
= msleep(cpipe
, PIPE_MTX(cpipe
), catch ? (PRIBIO
| PCATCH
) : PRIBIO
,
608 cpipe
->pipe_state
|= PIPE_LOCKFL
;
614 * unlock a pipe I/O lock
617 pipeunlock(struct pipe
*cpipe
)
619 cpipe
->pipe_state
&= ~PIPE_LOCKFL
;
621 if (cpipe
->pipe_state
& PIPE_LWANT
) {
622 cpipe
->pipe_state
&= ~PIPE_LWANT
;
628 pipeselwakeup(struct pipe
*cpipe
, struct pipe
*spipe
)
630 if (cpipe
->pipe_state
& PIPE_SEL
) {
631 cpipe
->pipe_state
&= ~PIPE_SEL
;
632 selwakeup(&cpipe
->pipe_sel
);
634 if (cpipe
->pipe_state
& PIPE_KNOTE
)
635 KNOTE(&cpipe
->pipe_sel
.si_note
, 1);
637 postpipeevent(cpipe
, EV_RWBYTES
);
639 if (spipe
&& (spipe
->pipe_state
& PIPE_ASYNC
) && spipe
->pipe_pgid
) {
640 if (spipe
->pipe_pgid
< 0)
641 gsignal(-spipe
->pipe_pgid
, SIGIO
);
643 proc_signal(spipe
->pipe_pgid
, SIGIO
);
649 pipe_read(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
650 __unused vfs_context_t ctx
)
652 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
660 error
= pipelock(rpipe
, 1);
665 error
= mac_pipe_check_read(kauth_cred_get(), rpipe
);
670 while (uio_resid(uio
)) {
672 * normal pipe buffer receive
674 if (rpipe
->pipe_buffer
.cnt
> 0) {
675 size
= rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.out
;
676 if (size
> rpipe
->pipe_buffer
.cnt
)
677 size
= rpipe
->pipe_buffer
.cnt
;
678 // LP64todo - fix this!
679 if (size
> (u_int
) uio_resid(uio
))
680 size
= (u_int
) uio_resid(uio
);
684 &rpipe
->pipe_buffer
.buffer
[rpipe
->pipe_buffer
.out
],
690 rpipe
->pipe_buffer
.out
+= size
;
691 if (rpipe
->pipe_buffer
.out
>= rpipe
->pipe_buffer
.size
)
692 rpipe
->pipe_buffer
.out
= 0;
694 rpipe
->pipe_buffer
.cnt
-= size
;
697 * If there is no more to read in the pipe, reset
698 * its pointers to the beginning. This improves
701 if (rpipe
->pipe_buffer
.cnt
== 0) {
702 rpipe
->pipe_buffer
.in
= 0;
703 rpipe
->pipe_buffer
.out
= 0;
706 #ifndef PIPE_NODIRECT
708 * Direct copy, bypassing a kernel buffer.
710 } else if ((size
= rpipe
->pipe_map
.cnt
) &&
711 (rpipe
->pipe_state
& PIPE_DIRECTW
)) {
713 // LP64todo - fix this!
714 if (size
> (u_int
) uio_resid(uio
))
715 size
= (u_int
) uio_resid(uio
);
717 va
= (caddr_t
) rpipe
->pipe_map
.kva
+
720 error
= uiomove(va
, size
, uio
);
725 rpipe
->pipe_map
.pos
+= size
;
726 rpipe
->pipe_map
.cnt
-= size
;
727 if (rpipe
->pipe_map
.cnt
== 0) {
728 rpipe
->pipe_state
&= ~PIPE_DIRECTW
;
734 * detect EOF condition
735 * read returns 0 on EOF, no need to set error
737 if (rpipe
->pipe_state
& PIPE_EOF
)
741 * If the "write-side" has been blocked, wake it up now.
743 if (rpipe
->pipe_state
& PIPE_WANTW
) {
744 rpipe
->pipe_state
&= ~PIPE_WANTW
;
749 * Break if some data was read.
755 * Unlock the pipe buffer for our remaining processing.
756 * We will either break out with an error or we will
757 * sleep and relock to loop.
762 * Handle non-blocking mode operation or
763 * wait for more data.
765 if (fp
->f_flag
& FNONBLOCK
) {
768 rpipe
->pipe_state
|= PIPE_WANTR
;
770 error
= msleep(rpipe
, PIPE_MTX(rpipe
), PRIBIO
| PCATCH
, "piperd", 0);
773 error
= pipelock(rpipe
, 1);
788 * PIPE_WANT processing only makes sense if pipe_busy is 0.
790 if ((rpipe
->pipe_busy
== 0) && (rpipe
->pipe_state
& PIPE_WANT
)) {
791 rpipe
->pipe_state
&= ~(PIPE_WANT
|PIPE_WANTW
);
793 } else if (rpipe
->pipe_buffer
.cnt
< MINPIPESIZE
) {
795 * Handle write blocking hysteresis.
797 if (rpipe
->pipe_state
& PIPE_WANTW
) {
798 rpipe
->pipe_state
&= ~PIPE_WANTW
;
803 if ((rpipe
->pipe_buffer
.size
- rpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)
804 pipeselwakeup(rpipe
, rpipe
->pipe_peer
);
806 /* update last read time */
807 pipe_touch(rpipe
, PIPE_ATIME
);
816 #ifndef PIPE_NODIRECT
818 * Map the sending processes' buffer into kernel space and wire it.
819 * This is similar to a physical write operation.
822 pipe_build_write_buffer(wpipe
, uio
)
829 vm_offset_t addr
, endaddr
;
832 size
= (u_int
) uio
->uio_iov
->iov_len
;
833 if (size
> wpipe
->pipe_buffer
.size
)
834 size
= wpipe
->pipe_buffer
.size
;
836 pmap
= vmspace_pmap(curproc
->p_vmspace
);
837 endaddr
= round_page((vm_offset_t
)uio
->uio_iov
->iov_base
+ size
);
838 addr
= trunc_page((vm_offset_t
)uio
->uio_iov
->iov_base
);
839 for (i
= 0; addr
< endaddr
; addr
+= PAGE_SIZE
, i
++) {
841 * vm_fault_quick() can sleep. Consequently,
842 * vm_page_lock_queue() and vm_page_unlock_queue()
843 * should not be performed outside of this loop.
846 if (vm_fault_quick((caddr_t
)addr
, VM_PROT_READ
) < 0) {
847 vm_page_lock_queues();
848 for (j
= 0; j
< i
; j
++)
849 vm_page_unhold(wpipe
->pipe_map
.ms
[j
]);
850 vm_page_unlock_queues();
853 wpipe
->pipe_map
.ms
[i
] = pmap_extract_and_hold(pmap
, addr
,
855 if (wpipe
->pipe_map
.ms
[i
] == NULL
)
860 * set up the control block
862 wpipe
->pipe_map
.npages
= i
;
863 wpipe
->pipe_map
.pos
=
864 ((vm_offset_t
) uio
->uio_iov
->iov_base
) & PAGE_MASK
;
865 wpipe
->pipe_map
.cnt
= size
;
870 if (wpipe
->pipe_map
.kva
== 0) {
872 * We need to allocate space for an extra page because the
873 * address range might (will) span pages at times.
875 wpipe
->pipe_map
.kva
= kmem_alloc_nofault(kernel_map
,
876 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
877 atomic_add_int(&amountpipekvawired
,
878 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
880 pmap_qenter(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.ms
,
881 wpipe
->pipe_map
.npages
);
884 * and update the uio data
887 uio
->uio_iov
->iov_len
-= size
;
888 uio
->uio_iov
->iov_base
= (char *)uio
->uio_iov
->iov_base
+ size
;
889 if (uio
->uio_iov
->iov_len
== 0)
891 uio_setresid(uio
, (uio_resid(uio
) - size
));
892 uio
->uio_offset
+= size
;
897 * unmap and unwire the process buffer
900 pipe_destroy_write_buffer(wpipe
)
905 if (wpipe
->pipe_map
.kva
) {
906 pmap_qremove(wpipe
->pipe_map
.kva
, wpipe
->pipe_map
.npages
);
908 if (amountpipekvawired
> maxpipekvawired
/ 2) {
909 /* Conserve address space */
910 vm_offset_t kva
= wpipe
->pipe_map
.kva
;
911 wpipe
->pipe_map
.kva
= 0;
912 kmem_free(kernel_map
, kva
,
913 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
914 atomic_subtract_int(&amountpipekvawired
,
915 wpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
918 vm_page_lock_queues();
919 for (i
= 0; i
< wpipe
->pipe_map
.npages
; i
++) {
920 vm_page_unhold(wpipe
->pipe_map
.ms
[i
]);
922 vm_page_unlock_queues();
923 wpipe
->pipe_map
.npages
= 0;
927 * In the case of a signal, the writing process might go away. This
928 * code copies the data into the circular buffer so that the source
929 * pages can be freed without loss of data.
932 pipe_clone_write_buffer(wpipe
)
938 size
= wpipe
->pipe_map
.cnt
;
939 pos
= wpipe
->pipe_map
.pos
;
941 wpipe
->pipe_buffer
.in
= size
;
942 wpipe
->pipe_buffer
.out
= 0;
943 wpipe
->pipe_buffer
.cnt
= size
;
944 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
947 bcopy((caddr_t
) wpipe
->pipe_map
.kva
+ pos
,
948 wpipe
->pipe_buffer
.buffer
, size
);
949 pipe_destroy_write_buffer(wpipe
);
954 * This implements the pipe buffer write mechanism. Note that only
955 * a direct write OR a normal pipe write can be pending at any given time.
956 * If there are any characters in the pipe buffer, the direct write will
957 * be deferred until the receiving process grabs all of the bytes from
958 * the pipe buffer. Then the direct mapping write is set-up.
961 pipe_direct_write(wpipe
, uio
)
968 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
969 if (wpipe
->pipe_state
& PIPE_WANTR
) {
970 wpipe
->pipe_state
&= ~PIPE_WANTR
;
973 wpipe
->pipe_state
|= PIPE_WANTW
;
974 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
975 PRIBIO
| PCATCH
, "pipdww", 0);
978 if (wpipe
->pipe_state
& PIPE_EOF
) {
983 wpipe
->pipe_map
.cnt
= 0; /* transfer not ready yet */
984 if (wpipe
->pipe_buffer
.cnt
> 0) {
985 if (wpipe
->pipe_state
& PIPE_WANTR
) {
986 wpipe
->pipe_state
&= ~PIPE_WANTR
;
990 wpipe
->pipe_state
|= PIPE_WANTW
;
991 error
= msleep(wpipe
, PIPE_MTX(wpipe
),
992 PRIBIO
| PCATCH
, "pipdwc", 0);
995 if (wpipe
->pipe_state
& PIPE_EOF
) {
1002 wpipe
->pipe_state
|= PIPE_DIRECTW
;
1006 error
= pipe_build_write_buffer(wpipe
, uio
);
1010 wpipe
->pipe_state
&= ~PIPE_DIRECTW
;
1015 while (!error
&& (wpipe
->pipe_state
& PIPE_DIRECTW
)) {
1016 if (wpipe
->pipe_state
& PIPE_EOF
) {
1019 pipe_destroy_write_buffer(wpipe
);
1021 pipeselwakeup(wpipe
, wpipe
);
1026 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1027 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1030 pipeselwakeup(wpipe
, wpipe
);
1031 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
,
1036 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1038 * this bit of trickery substitutes a kernel buffer for
1039 * the process that might be going away.
1041 pipe_clone_write_buffer(wpipe
);
1044 pipe_destroy_write_buffer(wpipe
);
1059 pipe_write(struct fileproc
*fp
, struct uio
*uio
, __unused
int flags
,
1060 __unused vfs_context_t ctx
)
1065 struct pipe
*wpipe
, *rpipe
;
1067 rpipe
= (struct pipe
*)fp
->f_data
;
1070 wpipe
= rpipe
->pipe_peer
;
1073 * detect loss of pipe read side, issue SIGPIPE if lost.
1075 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_EOF
)) {
1080 error
= mac_pipe_check_write(kauth_cred_get(), wpipe
);
1090 if (wpipe
->pipe_buffer
.buffer
== 0) {
1092 * need to allocate some storage... we delay the allocation
1093 * until the first write on fd[0] to avoid allocating storage for both
1094 * 'pipe ends'... most pipes are half-duplex with the writes targeting
1095 * fd[1], so allocating space for both ends is a waste...
1097 * Reduce to 1/4th pipe size if we're over our global max.
1099 if (amountpipekva
> maxpipekva
/ 2)
1100 pipe_size
= SMALL_PIPE_SIZE
;
1102 pipe_size
= PIPE_SIZE
;
1106 * If it is advantageous to resize the pipe buffer, do
1109 if ((uio_resid(uio
) > PIPE_SIZE
) &&
1110 (wpipe
->pipe_buffer
.size
<= PIPE_SIZE
) &&
1111 (amountpipekva
< maxpipekva
/ 2) &&
1112 (nbigpipe
< LIMITBIGPIPES
) &&
1113 #ifndef PIPE_NODIRECT
1114 (wpipe
->pipe_state
& PIPE_DIRECTW
) == 0 &&
1116 (wpipe
->pipe_buffer
.cnt
== 0)) {
1118 pipe_size
= BIG_PIPE_SIZE
;
1123 * need to do initial allocation or resizing of pipe
1125 if ((error
= pipelock(wpipe
, 1)) == 0) {
1127 if (pipespace(wpipe
, pipe_size
) == 0)
1128 OSAddAtomic(1, (SInt32
*)&nbigpipe
);
1132 if (wpipe
->pipe_buffer
.buffer
== 0) {
1134 * initial allocation failed
1141 * If an error occurred unbusy and return, waking up any pending
1145 if ((wpipe
->pipe_busy
== 0) &&
1146 (wpipe
->pipe_state
& PIPE_WANT
)) {
1147 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1154 // LP64todo - fix this!
1155 orig_resid
= uio_resid(uio
);
1157 while (uio_resid(uio
)) {
1160 #ifndef PIPE_NODIRECT
1162 * If the transfer is large, we can gain performance if
1163 * we do process-to-process copies directly.
1164 * If the write is non-blocking, we don't use the
1165 * direct write mechanism.
1167 * The direct write mechanism will detect the reader going
1170 if ((uio
->uio_iov
->iov_len
>= PIPE_MINDIRECT
) &&
1171 (fp
->f_flag
& FNONBLOCK
) == 0 &&
1172 amountpipekvawired
+ uio
->uio_resid
< maxpipekvawired
) {
1173 error
= pipe_direct_write(wpipe
, uio
);
1180 * Pipe buffered writes cannot be coincidental with
1181 * direct writes. We wait until the currently executing
1182 * direct write is completed before we start filling the
1183 * pipe buffer. We break out if a signal occurs or the
1187 while (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1188 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1189 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1192 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipbww", 0);
1194 if (wpipe
->pipe_state
& PIPE_EOF
)
1202 space
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1205 * Writes of size <= PIPE_BUF must be atomic.
1207 if ((space
< uio_resid(uio
)) && (orig_resid
<= PIPE_BUF
))
1212 if ((error
= pipelock(wpipe
,1)) == 0) {
1213 int size
; /* Transfer size */
1214 int segsize
; /* first segment to transfer */
1216 if (wpipe
->pipe_state
& PIPE_EOF
) {
1221 #ifndef PIPE_NODIRECT
1223 * It is possible for a direct write to
1224 * slip in on us... handle it here...
1226 if (wpipe
->pipe_state
& PIPE_DIRECTW
) {
1232 * If a process blocked in pipelock, our
1233 * value for space might be bad... the mutex
1234 * is dropped while we're blocked
1236 if (space
> (int)(wpipe
->pipe_buffer
.size
-
1237 wpipe
->pipe_buffer
.cnt
)) {
1243 * Transfer size is minimum of uio transfer
1244 * and free space in pipe buffer.
1246 // LP64todo - fix this!
1247 if (space
> uio_resid(uio
))
1248 size
= uio_resid(uio
);
1252 * First segment to transfer is minimum of
1253 * transfer size and contiguous space in
1254 * pipe buffer. If first segment to transfer
1255 * is less than the transfer size, we've got
1256 * a wraparound in the buffer.
1258 segsize
= wpipe
->pipe_buffer
.size
-
1259 wpipe
->pipe_buffer
.in
;
1263 /* Transfer first segment */
1266 error
= uiomove(&wpipe
->pipe_buffer
.buffer
[wpipe
->pipe_buffer
.in
],
1270 if (error
== 0 && segsize
< size
) {
1272 * Transfer remaining part now, to
1273 * support atomic writes. Wraparound
1276 if (wpipe
->pipe_buffer
.in
+ segsize
!=
1277 wpipe
->pipe_buffer
.size
)
1278 panic("Expected pipe buffer "
1279 "wraparound disappeared");
1283 &wpipe
->pipe_buffer
.buffer
[0],
1284 size
- segsize
, uio
);
1288 wpipe
->pipe_buffer
.in
+= size
;
1289 if (wpipe
->pipe_buffer
.in
>=
1290 wpipe
->pipe_buffer
.size
) {
1291 if (wpipe
->pipe_buffer
.in
!=
1293 wpipe
->pipe_buffer
.size
)
1296 wpipe
->pipe_buffer
.in
= size
-
1300 wpipe
->pipe_buffer
.cnt
+= size
;
1301 if (wpipe
->pipe_buffer
.cnt
>
1302 wpipe
->pipe_buffer
.size
)
1303 panic("Pipe buffer overflow");
1313 * If the "read-side" has been blocked, wake it up now.
1315 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1316 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1320 * don't block on non-blocking I/O
1321 * we'll do the pipeselwakeup on the way out
1323 if (fp
->f_flag
& FNONBLOCK
) {
1328 * We have no more space and have something to offer,
1329 * wake up select/poll.
1331 pipeselwakeup(wpipe
, wpipe
);
1333 wpipe
->pipe_state
|= PIPE_WANTW
;
1335 error
= msleep(wpipe
, PIPE_MTX(wpipe
), PRIBIO
| PCATCH
, "pipewr", 0);
1340 * If read side wants to go away, we just issue a signal
1343 if (wpipe
->pipe_state
& PIPE_EOF
) {
1351 if ((wpipe
->pipe_busy
== 0) && (wpipe
->pipe_state
& PIPE_WANT
)) {
1352 wpipe
->pipe_state
&= ~(PIPE_WANT
| PIPE_WANTR
);
1355 if (wpipe
->pipe_buffer
.cnt
> 0) {
1357 * If there are any characters in the buffer, we wake up
1358 * the reader if it was blocked waiting for data.
1360 if (wpipe
->pipe_state
& PIPE_WANTR
) {
1361 wpipe
->pipe_state
&= ~PIPE_WANTR
;
1365 * wake up thread blocked in select/poll or post the notification
1367 pipeselwakeup(wpipe
, wpipe
);
1370 /* Update modification, status change (# of bytes in pipe) times */
1371 pipe_touch(rpipe
, PIPE_MTIME
| PIPE_CTIME
);
1372 pipe_touch(wpipe
, PIPE_MTIME
| PIPE_CTIME
);
1379 * we implement a very minimal set of ioctls for compatibility with sockets.
1383 pipe_ioctl(struct fileproc
*fp
, u_long cmd
, caddr_t data
,
1384 __unused vfs_context_t ctx
)
1386 struct pipe
*mpipe
= (struct pipe
*)fp
->f_data
;
1394 error
= mac_pipe_check_ioctl(kauth_cred_get(), mpipe
, cmd
);
1410 mpipe
->pipe_state
|= PIPE_ASYNC
;
1412 mpipe
->pipe_state
&= ~PIPE_ASYNC
;
1418 #ifndef PIPE_NODIRECT
1419 if (mpipe
->pipe_state
& PIPE_DIRECTW
)
1420 *(int *)data
= mpipe
->pipe_map
.cnt
;
1423 *(int *)data
= mpipe
->pipe_buffer
.cnt
;
1428 mpipe
->pipe_pgid
= *(int *)data
;
1434 *(int *)data
= mpipe
->pipe_pgid
;
1446 pipe_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
1448 struct pipe
*rpipe
= (struct pipe
*)fp
->f_data
;
1452 if (rpipe
== NULL
|| rpipe
== (struct pipe
*)-1)
1457 wpipe
= rpipe
->pipe_peer
;
1461 * XXX We should use a per thread credential here; minimally, the
1462 * XXX process credential should have a persistent reference on it
1463 * XXX before being passed in here.
1465 if (mac_pipe_check_select(vfs_context_ucred(ctx
), rpipe
, which
)) {
1473 if ((rpipe
->pipe_state
& PIPE_DIRECTW
) ||
1474 (rpipe
->pipe_buffer
.cnt
> 0) ||
1475 (rpipe
->pipe_state
& PIPE_EOF
)) {
1479 rpipe
->pipe_state
|= PIPE_SEL
;
1480 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1485 if (wpipe
== NULL
|| (wpipe
->pipe_state
& PIPE_EOF
) ||
1486 (((wpipe
->pipe_state
& PIPE_DIRECTW
) == 0) &&
1487 (wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
) >= PIPE_BUF
)) {
1491 wpipe
->pipe_state
|= PIPE_SEL
;
1492 selrecord(vfs_context_proc(ctx
), &wpipe
->pipe_sel
, wql
);
1496 rpipe
->pipe_state
|= PIPE_SEL
;
1497 selrecord(vfs_context_proc(ctx
), &rpipe
->pipe_sel
, wql
);
1508 pipe_close(struct fileglob
*fg
, __unused vfs_context_t ctx
)
1512 proc_fdlock_spin(vfs_context_proc(ctx
));
1513 cpipe
= (struct pipe
*)fg
->fg_data
;
1515 proc_fdunlock(vfs_context_proc(ctx
));
1524 pipe_free_kmem(struct pipe
*cpipe
)
1527 if (cpipe
->pipe_buffer
.buffer
!= NULL
) {
1528 if (cpipe
->pipe_buffer
.size
> PIPE_SIZE
)
1529 OSAddAtomic(-1, (SInt32
*)&nbigpipe
);
1530 OSAddAtomic(-(cpipe
->pipe_buffer
.size
), (SInt32
*)&amountpipekva
);
1531 OSAddAtomic(-1, (SInt32
*)&amountpipes
);
1533 kmem_free(kernel_map
, (vm_offset_t
)cpipe
->pipe_buffer
.buffer
,
1534 cpipe
->pipe_buffer
.size
);
1535 cpipe
->pipe_buffer
.buffer
= NULL
;
1537 #ifndef PIPE_NODIRECT
1538 if (cpipe
->pipe_map
.kva
!= 0) {
1539 atomic_subtract_int(&amountpipekvawired
,
1540 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1541 kmem_free(kernel_map
,
1542 cpipe
->pipe_map
.kva
,
1543 cpipe
->pipe_buffer
.size
+ PAGE_SIZE
);
1544 cpipe
->pipe_map
.cnt
= 0;
1545 cpipe
->pipe_map
.kva
= 0;
1546 cpipe
->pipe_map
.pos
= 0;
1547 cpipe
->pipe_map
.npages
= 0;
1556 pipeclose(struct pipe
*cpipe
)
1563 /* partially created pipes won't have a valid mutex. */
1564 if (PIPE_MTX(cpipe
) != NULL
)
1569 * If the other side is blocked, wake it up saying that
1570 * we want to close it down.
1572 cpipe
->pipe_state
|= PIPE_EOF
;
1573 pipeselwakeup(cpipe
, cpipe
);
1575 while (cpipe
->pipe_busy
) {
1576 cpipe
->pipe_state
|= PIPE_WANT
;
1579 msleep(cpipe
, PIPE_MTX(cpipe
), PRIBIO
, "pipecl", 0);
1584 * Free the shared pipe label only after the two ends are disconnected.
1586 if (cpipe
->pipe_label
!= NULL
&& cpipe
->pipe_peer
== NULL
)
1587 mac_pipe_label_destroy(cpipe
);
1591 * Disconnect from peer
1593 if ((ppipe
= cpipe
->pipe_peer
) != NULL
) {
1595 ppipe
->pipe_state
|= PIPE_EOF
;
1597 pipeselwakeup(ppipe
, ppipe
);
1600 if (cpipe
->pipe_state
& PIPE_KNOTE
)
1601 KNOTE(&ppipe
->pipe_sel
.si_note
, 1);
1603 postpipeevent(ppipe
, EV_RCLOSED
);
1605 ppipe
->pipe_peer
= NULL
;
1612 if (PIPE_MTX(cpipe
) != NULL
) {
1613 if (ppipe
!= NULL
) {
1615 * since the mutex is shared and the peer is still
1616 * alive, we need to release the mutex, not free it
1621 * peer is gone, so we're the sole party left with
1622 * interest in this mutex... we can just free it
1624 lck_mtx_free(PIPE_MTX(cpipe
), pipe_mtx_grp
);
1627 pipe_free_kmem(cpipe
);
1629 zfree(pipe_zone
, cpipe
);
1634 pipe_kqfilter(__unused
struct fileproc
*fp
, struct knote
*kn
, __unused vfs_context_t ctx
)
1638 cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1643 * XXX We should use a per thread credential here; minimally, the
1644 * XXX process credential should have a persistent reference on it
1645 * XXX before being passed in here.
1647 if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx
), kn
, cpipe
) != 0) {
1653 switch (kn
->kn_filter
) {
1655 kn
->kn_fop
= &pipe_rfiltops
;
1659 kn
->kn_fop
= &pipe_wfiltops
;
1661 if (cpipe
->pipe_peer
== NULL
) {
1663 * other end of pipe has been closed
1668 if (cpipe
->pipe_peer
)
1669 cpipe
= cpipe
->pipe_peer
;
1676 if (KNOTE_ATTACH(&cpipe
->pipe_sel
.si_note
, kn
))
1677 cpipe
->pipe_state
|= PIPE_KNOTE
;
1684 filt_pipedetach(struct knote
*kn
)
1686 struct pipe
*cpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1690 if (kn
->kn_filter
== EVFILT_WRITE
) {
1691 if (cpipe
->pipe_peer
== NULL
) {
1695 cpipe
= cpipe
->pipe_peer
;
1697 if (cpipe
->pipe_state
& PIPE_KNOTE
) {
1698 if (KNOTE_DETACH(&cpipe
->pipe_sel
.si_note
, kn
))
1699 cpipe
->pipe_state
&= ~PIPE_KNOTE
;
1706 filt_piperead(struct knote
*kn
, long hint
)
1708 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1713 * if hint == 0, then we've been called from the kevent
1714 * world directly and do not currently hold the pipe mutex...
1715 * if hint == 1, we're being called back via the KNOTE post
1716 * we made in pipeselwakeup, and we already hold the mutex...
1721 wpipe
= rpipe
->pipe_peer
;
1722 kn
->kn_data
= rpipe
->pipe_buffer
.cnt
;
1724 #ifndef PIPE_NODIRECT
1725 if ((kn
->kn_data
== 0) && (rpipe
->pipe_state
& PIPE_DIRECTW
))
1726 kn
->kn_data
= rpipe
->pipe_map
.cnt
;
1728 if ((rpipe
->pipe_state
& PIPE_EOF
) ||
1729 (wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1730 kn
->kn_flags
|= EV_EOF
;
1733 retval
= (kn
->kn_sfflags
& NOTE_LOWAT
) ?
1734 (kn
->kn_data
>= kn
->kn_sdata
) : (kn
->kn_data
> 0);
1745 filt_pipewrite(struct knote
*kn
, long hint
)
1747 struct pipe
*rpipe
= (struct pipe
*)kn
->kn_fp
->f_data
;
1751 * if hint == 0, then we've been called from the kevent
1752 * world directly and do not currently hold the pipe mutex...
1753 * if hint == 1, we're being called back via the KNOTE post
1754 * we made in pipeselwakeup, and we already hold the mutex...
1759 wpipe
= rpipe
->pipe_peer
;
1761 if ((wpipe
== NULL
) || (wpipe
->pipe_state
& PIPE_EOF
)) {
1763 kn
->kn_flags
|= EV_EOF
;
1769 kn
->kn_data
= wpipe
->pipe_buffer
.size
- wpipe
->pipe_buffer
.cnt
;
1770 if (!kn
->kn_data
&& wpipe
->pipe_buffer
.size
== 0)
1771 kn
->kn_data
= 1; /* unwritten pipe is ready for write */
1773 #ifndef PIPE_NODIRECT
1774 if (wpipe
->pipe_state
& PIPE_DIRECTW
)
1780 return (kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
1781 kn
->kn_sdata
: PIPE_BUF
));
1785 fill_pipeinfo(struct pipe
* cpipe
, struct pipe_info
* pinfo
)
1791 struct vinfo_stat
* ub
;
1800 error
= mac_pipe_check_stat(kauth_cred_get(), cpipe
);
1806 if (cpipe
->pipe_buffer
.buffer
== 0) {
1808 * must be stat'ing the write fd
1810 if (cpipe
->pipe_peer
) {
1812 * the peer still exists, use it's info
1814 pipe_size
= cpipe
->pipe_peer
->pipe_buffer
.size
;
1815 pipe_count
= cpipe
->pipe_peer
->pipe_buffer
.cnt
;
1820 pipe_size
= cpipe
->pipe_buffer
.size
;
1821 pipe_count
= cpipe
->pipe_buffer
.cnt
;
1824 * since peer's buffer is setup ouside of lock
1825 * we might catch it in transient state
1828 pipe_size
= PIPE_SIZE
;
1830 ub
= &pinfo
->pipe_stat
;
1832 bzero(ub
, sizeof(*ub
));
1833 ub
->vst_mode
= S_IFIFO
| S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1834 ub
->vst_blksize
= pipe_size
;
1835 ub
->vst_size
= pipe_count
;
1836 if (ub
->vst_blksize
!= 0)
1837 ub
->vst_blocks
= (ub
->vst_size
+ ub
->vst_blksize
- 1) / ub
->vst_blksize
;
1840 ub
->vst_uid
= kauth_getuid();
1841 ub
->vst_gid
= kauth_getgid();
1844 ub
->vst_atime
= now
.tv_sec
;
1845 ub
->vst_atimensec
= now
.tv_usec
* 1000;
1847 ub
->vst_mtime
= now
.tv_sec
;
1848 ub
->vst_mtimensec
= now
.tv_usec
* 1000;
1850 ub
->vst_ctime
= now
.tv_sec
;
1851 ub
->vst_ctimensec
= now
.tv_usec
* 1000;
1854 * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid.
1855 * XXX (st_dev, st_ino) should be unique.
1858 pinfo
->pipe_handle
= (uint64_t)((uintptr_t)cpipe
);
1859 pinfo
->pipe_peerhandle
= (uint64_t)((uintptr_t)(cpipe
->pipe_peer
));
1860 pinfo
->pipe_status
= cpipe
->pipe_state
;