]>
Commit | Line | Data |
---|---|---|
91447636 A |
1 | /* |
2 | * Copyright (c) 1996 John S. Dyson | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
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 | |
15 | * John S. Dyson. | |
16 | * 4. Modifications may be freely made to this file if the above conditions | |
17 | * are met. | |
18 | */ | |
19 | /* | |
fe8ab488 | 20 | * Copyright (c) 2003-2014 Apple Inc. All rights reserved. |
91447636 | 21 | * |
2d21ac55 | 22 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 23 | * |
2d21ac55 A |
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. | |
0a7de745 | 32 | * |
2d21ac55 A |
33 | * Please obtain a copy of the License at |
34 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 35 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
38 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
39 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 43 | * |
2d21ac55 A |
44 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
45 | */ | |
46 | /* | |
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, | |
50 | * Version 2.0. | |
91447636 A |
51 | */ |
52 | ||
53 | /* | |
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 | |
57 | * do. | |
316670eb A |
58 | * |
59 | * Pipes are implemented as circular buffers. Following are the valid states in pipes operations | |
0a7de745 | 60 | * |
316670eb A |
61 | * _________________________________ |
62 | * 1. |_________________________________| r=w, c=0 | |
0a7de745 | 63 | * |
316670eb A |
64 | * _________________________________ |
65 | * 2. |__r:::::wc_______________________| r <= w , c > 0 | |
66 | * | |
67 | * _________________________________ | |
68 | * 3. |::::wc_____r:::::::::::::::::::::| r>w , c > 0 | |
69 | * | |
70 | * _________________________________ | |
71 | * 4. |:::::::wrc:::::::::::::::::::::::| w=r, c = Max size | |
72 | * | |
73 | * | |
74 | * Nomenclature:- | |
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 | |
0a7de745 | 78 | * |
316670eb A |
79 | * Current:None Action: initialize with size M=200 |
80 | * a. State 1 ( r=0, w=0, c=0) | |
0a7de745 | 81 | * |
316670eb A |
82 | * Current: a Action: write(100) (w < M) |
83 | * b. State 2 (r=0, w=100, c=100) | |
0a7de745 | 84 | * |
316670eb A |
85 | * Current: b Action: write(100) (w = M-w) |
86 | * c. State 4 (r=0,w=0,c=200) | |
0a7de745 | 87 | * |
316670eb A |
88 | * Current: b Action: read(70) ( r < c ) |
89 | * d. State 2(r=70,w=100,c=30) | |
0a7de745 | 90 | * |
316670eb A |
91 | * Current: d Action: write(75) ( w < (m-w)) |
92 | * e. State 2 (r=70,w=175,c=105) | |
0a7de745 | 93 | * |
316670eb A |
94 | * Current: d Action: write(110) ( w > (m-w)) |
95 | * f. State 3 (r=70,w=10,c=140) | |
0a7de745 | 96 | * |
316670eb A |
97 | * Current: d Action: read(30) (r >= c ) |
98 | * g. State 1 (r=100,w=100,c=0) | |
0a7de745 | 99 | * |
91447636 A |
100 | */ |
101 | ||
102 | /* | |
316670eb A |
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 | |
0a7de745 | 108 | * until the pipe buffer empties enough to continue. |
91447636 A |
109 | * |
110 | * In order to limit the resource use of pipes, two sysctls exist: | |
111 | * | |
112 | * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable | |
0a7de745 | 113 | * address space available to us in pipe_map. |
91447636 A |
114 | * |
115 | * Memory usage may be monitored through the sysctls | |
316670eb | 116 | * kern.ipc.pipes, kern.ipc.pipekva. |
91447636 A |
117 | * |
118 | */ | |
119 | ||
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> | |
0c530ab8 | 139 | #include <sys/proc_info.h> |
91447636 | 140 | |
b0d623f7 | 141 | #include <security/audit/audit.h> |
91447636 A |
142 | |
143 | #include <sys/kdebug.h> | |
144 | ||
145 | #include <kern/zalloc.h> | |
316670eb | 146 | #include <kern/kalloc.h> |
91447636 A |
147 | #include <vm/vm_kern.h> |
148 | #include <libkern/OSAtomic.h> | |
5ba3f43e A |
149 | #include <libkern/section_keywords.h> |
150 | ||
151 | #if CONFIG_MACF | |
152 | #include <security/mac_framework.h> | |
153 | #endif | |
91447636 A |
154 | |
155 | #define f_flag f_fglob->fg_flag | |
91447636 A |
156 | #define f_msgcount f_fglob->fg_msgcount |
157 | #define f_cred f_fglob->fg_cred | |
158 | #define f_ops f_fglob->fg_ops | |
159 | #define f_offset f_fglob->fg_offset | |
160 | #define f_data f_fglob->fg_data | |
91447636 | 161 | |
91447636 | 162 | /* |
0a7de745 | 163 | * interfaces to the outside world exported through file operations |
91447636 A |
164 | */ |
165 | static int pipe_read(struct fileproc *fp, struct uio *uio, | |
0a7de745 | 166 | int flags, vfs_context_t ctx); |
91447636 | 167 | static int pipe_write(struct fileproc *fp, struct uio *uio, |
0a7de745 | 168 | int flags, vfs_context_t ctx); |
2d21ac55 | 169 | static int pipe_close(struct fileglob *fg, vfs_context_t ctx); |
2d21ac55 | 170 | static int pipe_select(struct fileproc *fp, int which, void * wql, |
0a7de745 | 171 | vfs_context_t ctx); |
2d21ac55 | 172 | static int pipe_kqfilter(struct fileproc *fp, struct knote *kn, |
0a7de745 | 173 | struct kevent_internal_s *kev, vfs_context_t ctx); |
2d21ac55 | 174 | static int pipe_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, |
0a7de745 A |
175 | vfs_context_t ctx); |
176 | static int pipe_drain(struct fileproc *fp, vfs_context_t ctx); | |
b0d623f7 | 177 | |
39236c6e | 178 | static const struct fileops pipeops = { |
39037602 A |
179 | .fo_type = DTYPE_PIPE, |
180 | .fo_read = pipe_read, | |
181 | .fo_write = pipe_write, | |
182 | .fo_ioctl = pipe_ioctl, | |
183 | .fo_select = pipe_select, | |
184 | .fo_close = pipe_close, | |
185 | .fo_kqfilter = pipe_kqfilter, | |
186 | .fo_drain = pipe_drain, | |
39236c6e | 187 | }; |
91447636 | 188 | |
39037602 | 189 | static void filt_pipedetach(struct knote *kn); |
91447636 | 190 | |
39037602 A |
191 | static int filt_piperead(struct knote *kn, long hint); |
192 | static int filt_pipereadtouch(struct knote *kn, struct kevent_internal_s *kev); | |
193 | static int filt_pipereadprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev); | |
194 | ||
195 | static int filt_pipewrite(struct knote *kn, long hint); | |
196 | static int filt_pipewritetouch(struct knote *kn, struct kevent_internal_s *kev); | |
197 | static int filt_pipewriteprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev); | |
198 | ||
5ba3f43e | 199 | SECURITY_READ_ONLY_EARLY(struct filterops) pipe_rfiltops = { |
0a7de745 A |
200 | .f_isfd = 1, |
201 | .f_detach = filt_pipedetach, | |
202 | .f_event = filt_piperead, | |
39037602 A |
203 | .f_touch = filt_pipereadtouch, |
204 | .f_process = filt_pipereadprocess, | |
b0d623f7 | 205 | }; |
316670eb | 206 | |
5ba3f43e | 207 | SECURITY_READ_ONLY_EARLY(struct filterops) pipe_wfiltops = { |
0a7de745 A |
208 | .f_isfd = 1, |
209 | .f_detach = filt_pipedetach, | |
210 | .f_event = filt_pipewrite, | |
39037602 A |
211 | .f_touch = filt_pipewritetouch, |
212 | .f_process = filt_pipewriteprocess, | |
b0d623f7 | 213 | }; |
91447636 | 214 | |
316670eb A |
215 | static int nbigpipe; /* for compatibility sake. no longer used */ |
216 | static int amountpipes; /* total number of pipes in system */ | |
217 | static int amountpipekva; /* total memory used by pipes */ | |
91447636 | 218 | |
39236c6e | 219 | int maxpipekva __attribute__((used)) = PIPE_KVAMAX; /* allowing 16MB max. */ |
91447636 A |
220 | |
221 | #if PIPE_SYSCTLS | |
222 | SYSCTL_DECL(_kern_ipc); | |
223 | ||
0a7de745 A |
224 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RD | CTLFLAG_LOCKED, |
225 | &maxpipekva, 0, "Pipe KVA limit"); | |
226 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekvawired, CTLFLAG_RW | CTLFLAG_LOCKED, | |
227 | &maxpipekvawired, 0, "Pipe KVA wired limit"); | |
228 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipes, CTLFLAG_RD | CTLFLAG_LOCKED, | |
229 | &amountpipes, 0, "Current # of pipes"); | |
230 | SYSCTL_INT(_kern_ipc, OID_AUTO, bigpipes, CTLFLAG_RD | CTLFLAG_LOCKED, | |
231 | &nbigpipe, 0, "Current # of big pipes"); | |
232 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD | CTLFLAG_LOCKED, | |
233 | &amountpipekva, 0, "Pipe KVA usage"); | |
234 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipekvawired, CTLFLAG_RD | CTLFLAG_LOCKED, | |
235 | &amountpipekvawired, 0, "Pipe wired KVA usage"); | |
91447636 A |
236 | #endif |
237 | ||
91447636 A |
238 | static void pipeclose(struct pipe *cpipe); |
239 | static void pipe_free_kmem(struct pipe *cpipe); | |
240 | static int pipe_create(struct pipe **cpipep); | |
316670eb A |
241 | static int pipespace(struct pipe *cpipe, int size); |
242 | static int choose_pipespace(unsigned long current, unsigned long expected); | |
243 | static int expand_pipespace(struct pipe *p, int target_size); | |
91447636 | 244 | static void pipeselwakeup(struct pipe *cpipe, struct pipe *spipe); |
316670eb A |
245 | static __inline int pipeio_lock(struct pipe *cpipe, int catch); |
246 | static __inline void pipeio_unlock(struct pipe *cpipe); | |
91447636 A |
247 | |
248 | extern int postpipeevent(struct pipe *, int); | |
249 | extern void evpipefree(struct pipe *cpipe); | |
250 | ||
0a7de745 A |
251 | static lck_grp_t *pipe_mtx_grp; |
252 | static lck_attr_t *pipe_mtx_attr; | |
253 | static lck_grp_attr_t *pipe_mtx_grp_attr; | |
91447636 A |
254 | |
255 | static zone_t pipe_zone; | |
256 | ||
0a7de745 | 257 | #define MAX_PIPESIZE(pipe) ( MAX(PIPE_SIZE, (pipe)->pipe_buffer.size) ) |
316670eb | 258 | |
0a7de745 A |
259 | #define PIPE_GARBAGE_AGE_LIMIT 5000 /* In milliseconds */ |
260 | #define PIPE_GARBAGE_QUEUE_LIMIT 32000 | |
ebb1b9f4 A |
261 | |
262 | struct pipe_garbage { | |
0a7de745 A |
263 | struct pipe *pg_pipe; |
264 | struct pipe_garbage *pg_next; | |
265 | uint64_t pg_timestamp; | |
ebb1b9f4 A |
266 | }; |
267 | ||
268 | static zone_t pipe_garbage_zone; | |
269 | static struct pipe_garbage *pipe_garbage_head = NULL; | |
270 | static struct pipe_garbage *pipe_garbage_tail = NULL; | |
271 | static uint64_t pipe_garbage_age_limit = PIPE_GARBAGE_AGE_LIMIT; | |
272 | static int pipe_garbage_count = 0; | |
273 | static lck_mtx_t *pipe_garbage_lock; | |
316670eb | 274 | static void pipe_garbage_collect(struct pipe *cpipe); |
ebb1b9f4 | 275 | |
91447636 A |
276 | SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL); |
277 | ||
316670eb | 278 | /* initial setup done at time of sysinit */ |
91447636 | 279 | void |
2d21ac55 | 280 | pipeinit(void) |
91447636 | 281 | { |
0a7de745 | 282 | nbigpipe = 0; |
ebb1b9f4 | 283 | vm_size_t zone_size; |
0a7de745 | 284 | |
ebb1b9f4 | 285 | zone_size = 8192 * sizeof(struct pipe); |
0a7de745 | 286 | pipe_zone = zinit(sizeof(struct pipe), zone_size, 4096, "pipe zone"); |
91447636 | 287 | |
316670eb A |
288 | |
289 | /* allocate lock group attribute and group for pipe mutexes */ | |
91447636 | 290 | pipe_mtx_grp_attr = lck_grp_attr_alloc_init(); |
91447636 A |
291 | pipe_mtx_grp = lck_grp_alloc_init("pipe", pipe_mtx_grp_attr); |
292 | ||
316670eb | 293 | /* allocate the lock attribute for pipe mutexes */ |
91447636 | 294 | pipe_mtx_attr = lck_attr_alloc_init(); |
ebb1b9f4 A |
295 | |
296 | /* | |
297 | * Set up garbage collection for dead pipes | |
298 | */ | |
299 | zone_size = (PIPE_GARBAGE_QUEUE_LIMIT + 20) * | |
300 | sizeof(struct pipe_garbage); | |
0a7de745 | 301 | pipe_garbage_zone = (zone_t)zinit(sizeof(struct pipe_garbage), |
ebb1b9f4 A |
302 | zone_size, 4096, "pipe garbage zone"); |
303 | pipe_garbage_lock = lck_mtx_alloc_init(pipe_mtx_grp, pipe_mtx_attr); | |
91447636 A |
304 | } |
305 | ||
0a7de745 | 306 | #ifndef CONFIG_EMBEDDED |
2d21ac55 | 307 | /* Bitmap for things to touch in pipe_touch() */ |
0a7de745 A |
308 | #define PIPE_ATIME 0x00000001 /* time of last access */ |
309 | #define PIPE_MTIME 0x00000002 /* time of last modification */ | |
310 | #define PIPE_CTIME 0x00000004 /* time of last status change */ | |
2d21ac55 A |
311 | |
312 | static void | |
313 | pipe_touch(struct pipe *tpipe, int touch) | |
314 | { | |
5ba3f43e | 315 | struct timespec now; |
2d21ac55 | 316 | |
5ba3f43e | 317 | nanotime(&now); |
2d21ac55 A |
318 | |
319 | if (touch & PIPE_ATIME) { | |
320 | tpipe->st_atimespec.tv_sec = now.tv_sec; | |
5ba3f43e | 321 | tpipe->st_atimespec.tv_nsec = now.tv_nsec; |
2d21ac55 A |
322 | } |
323 | ||
324 | if (touch & PIPE_MTIME) { | |
325 | tpipe->st_mtimespec.tv_sec = now.tv_sec; | |
5ba3f43e | 326 | tpipe->st_mtimespec.tv_nsec = now.tv_nsec; |
2d21ac55 A |
327 | } |
328 | ||
329 | if (touch & PIPE_CTIME) { | |
330 | tpipe->st_ctimespec.tv_sec = now.tv_sec; | |
5ba3f43e | 331 | tpipe->st_ctimespec.tv_nsec = now.tv_nsec; |
2d21ac55 A |
332 | } |
333 | } | |
5ba3f43e | 334 | #endif |
2d21ac55 | 335 | |
0a7de745 | 336 | static const unsigned int pipesize_blocks[] = {512, 1024, 2048, 4096, 4096 * 2, PIPE_SIZE, PIPE_SIZE * 4 }; |
316670eb | 337 | |
0a7de745 A |
338 | /* |
339 | * finds the right size from possible sizes in pipesize_blocks | |
340 | * returns the size which matches max(current,expected) | |
316670eb | 341 | */ |
0a7de745 | 342 | static int |
316670eb A |
343 | choose_pipespace(unsigned long current, unsigned long expected) |
344 | { | |
0a7de745 | 345 | int i = sizeof(pipesize_blocks) / sizeof(unsigned int) - 1; |
316670eb A |
346 | unsigned long target; |
347 | ||
3e170ce0 A |
348 | /* |
349 | * assert that we always get an atomic transaction sized pipe buffer, | |
350 | * even if the system pipe buffer high-water mark has been crossed. | |
351 | */ | |
352 | assert(PIPE_BUF == pipesize_blocks[0]); | |
353 | ||
0a7de745 | 354 | if (expected > current) { |
316670eb | 355 | target = expected; |
0a7de745 | 356 | } else { |
316670eb | 357 | target = current; |
0a7de745 | 358 | } |
316670eb | 359 | |
0a7de745 A |
360 | while (i > 0 && pipesize_blocks[i - 1] > target) { |
361 | i = i - 1; | |
316670eb | 362 | } |
0a7de745 | 363 | |
316670eb A |
364 | return pipesize_blocks[i]; |
365 | } | |
91447636 A |
366 | |
367 | ||
316670eb A |
368 | /* |
369 | * expand the size of pipe while there is data to be read, | |
370 | * and then free the old buffer once the current buffered | |
371 | * data has been transferred to new storage. | |
372 | * Required: PIPE_LOCK and io lock to be held by caller. | |
373 | * returns 0 on success or no expansion possible | |
374 | */ | |
0a7de745 | 375 | static int |
316670eb A |
376 | expand_pipespace(struct pipe *p, int target_size) |
377 | { | |
378 | struct pipe tmp, oldpipe; | |
379 | int error; | |
380 | tmp.pipe_buffer.buffer = 0; | |
0a7de745 | 381 | |
316670eb A |
382 | if (p->pipe_buffer.size >= (unsigned) target_size) { |
383 | return 0; /* the existing buffer is max size possible */ | |
384 | } | |
0a7de745 | 385 | |
316670eb A |
386 | /* create enough space in the target */ |
387 | error = pipespace(&tmp, target_size); | |
0a7de745 A |
388 | if (error != 0) { |
389 | return error; | |
390 | } | |
316670eb A |
391 | |
392 | oldpipe.pipe_buffer.buffer = p->pipe_buffer.buffer; | |
393 | oldpipe.pipe_buffer.size = p->pipe_buffer.size; | |
0a7de745 | 394 | |
316670eb | 395 | memcpy(tmp.pipe_buffer.buffer, p->pipe_buffer.buffer, p->pipe_buffer.size); |
0a7de745 | 396 | if (p->pipe_buffer.cnt > 0 && p->pipe_buffer.in <= p->pipe_buffer.out) { |
316670eb A |
397 | /* we are in State 3 and need extra copying for read to be consistent */ |
398 | memcpy(&tmp.pipe_buffer.buffer[p->pipe_buffer.size], p->pipe_buffer.buffer, p->pipe_buffer.size); | |
399 | p->pipe_buffer.in += p->pipe_buffer.size; | |
400 | } | |
401 | ||
402 | p->pipe_buffer.buffer = tmp.pipe_buffer.buffer; | |
403 | p->pipe_buffer.size = tmp.pipe_buffer.size; | |
404 | ||
405 | ||
406 | pipe_free_kmem(&oldpipe); | |
407 | return 0; | |
408 | } | |
409 | ||
91447636 A |
410 | /* |
411 | * The pipe system call for the DTYPE_PIPE type of pipes | |
0a7de745 | 412 | * |
316670eb | 413 | * returns: |
0a7de745 | 414 | * FREAD | fd0 | -->[struct rpipe] --> |~~buffer~~| \ |
316670eb | 415 | * (pipe_mutex) |
0a7de745 | 416 | * FWRITE | fd1 | -->[struct wpipe] --X / |
91447636 A |
417 | */ |
418 | ||
419 | /* ARGSUSED */ | |
420 | int | |
b0d623f7 | 421 | pipe(proc_t p, __unused struct pipe_args *uap, int32_t *retval) |
91447636 A |
422 | { |
423 | struct fileproc *rf, *wf; | |
424 | struct pipe *rpipe, *wpipe; | |
425 | lck_mtx_t *pmtx; | |
426 | int fd, error; | |
427 | ||
0a7de745 A |
428 | if ((pmtx = lck_mtx_alloc_init(pipe_mtx_grp, pipe_mtx_attr)) == NULL) { |
429 | return ENOMEM; | |
430 | } | |
431 | ||
91447636 A |
432 | rpipe = wpipe = NULL; |
433 | if (pipe_create(&rpipe) || pipe_create(&wpipe)) { | |
0a7de745 | 434 | error = ENFILE; |
91447636 A |
435 | goto freepipes; |
436 | } | |
0a7de745 | 437 | /* |
91447636 A |
438 | * allocate the space for the normal I/O direction up |
439 | * front... we'll delay the allocation for the other | |
316670eb | 440 | * direction until a write actually occurs (most likely it won't)... |
0a7de745 | 441 | */ |
316670eb | 442 | error = pipespace(rpipe, choose_pipespace(rpipe->pipe_buffer.size, 0)); |
0a7de745 A |
443 | if (error) { |
444 | goto freepipes; | |
445 | } | |
91447636 | 446 | |
91447636 A |
447 | TAILQ_INIT(&rpipe->pipe_evlist); |
448 | TAILQ_INIT(&wpipe->pipe_evlist); | |
449 | ||
2d21ac55 | 450 | error = falloc(p, &rf, &fd, vfs_context_current()); |
91447636 | 451 | if (error) { |
0a7de745 | 452 | goto freepipes; |
91447636 A |
453 | } |
454 | retval[0] = fd; | |
455 | ||
456 | /* | |
0a7de745 | 457 | * for now we'll create half-duplex pipes(refer returns section above). |
316670eb | 458 | * this is what we've always supported.. |
91447636 A |
459 | */ |
460 | rf->f_flag = FREAD; | |
91447636 A |
461 | rf->f_data = (caddr_t)rpipe; |
462 | rf->f_ops = &pipeops; | |
463 | ||
2d21ac55 | 464 | error = falloc(p, &wf, &fd, vfs_context_current()); |
91447636 A |
465 | if (error) { |
466 | fp_free(p, retval[0], rf); | |
0a7de745 | 467 | goto freepipes; |
91447636 A |
468 | } |
469 | wf->f_flag = FWRITE; | |
91447636 A |
470 | wf->f_data = (caddr_t)wpipe; |
471 | wf->f_ops = &pipeops; | |
472 | ||
6601e61a A |
473 | rpipe->pipe_peer = wpipe; |
474 | wpipe->pipe_peer = rpipe; | |
316670eb | 475 | /* both structures share the same mutex */ |
0a7de745 | 476 | rpipe->pipe_mtxp = wpipe->pipe_mtxp = pmtx; |
2d21ac55 | 477 | |
91447636 | 478 | retval[1] = fd; |
2d21ac55 | 479 | #if CONFIG_MACF |
91447636 A |
480 | /* |
481 | * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX | |
482 | * | |
483 | * struct pipe represents a pipe endpoint. The MAC label is shared | |
2d21ac55 A |
484 | * between the connected endpoints. As a result mac_pipe_label_init() and |
485 | * mac_pipe_label_associate() should only be called on one of the endpoints | |
91447636 A |
486 | * after they have been connected. |
487 | */ | |
2d21ac55 A |
488 | mac_pipe_label_init(rpipe); |
489 | mac_pipe_label_associate(kauth_cred_get(), rpipe); | |
490 | wpipe->pipe_label = rpipe->pipe_label; | |
91447636 | 491 | #endif |
2d21ac55 | 492 | proc_fdlock_spin(p); |
6601e61a A |
493 | procfdtbl_releasefd(p, retval[0], NULL); |
494 | procfdtbl_releasefd(p, retval[1], NULL); | |
91447636 A |
495 | fp_drop(p, retval[0], rf, 1); |
496 | fp_drop(p, retval[1], wf, 1); | |
497 | proc_fdunlock(p); | |
498 | ||
91447636 | 499 | |
0a7de745 | 500 | return 0; |
91447636 A |
501 | |
502 | freepipes: | |
0a7de745 A |
503 | pipeclose(rpipe); |
504 | pipeclose(wpipe); | |
91447636 A |
505 | lck_mtx_free(pmtx, pipe_mtx_grp); |
506 | ||
0a7de745 | 507 | return error; |
91447636 A |
508 | } |
509 | ||
91447636 | 510 | int |
2d21ac55 | 511 | pipe_stat(struct pipe *cpipe, void *ub, int isstat64) |
91447636 | 512 | { |
2d21ac55 | 513 | #if CONFIG_MACF |
0a7de745 | 514 | int error; |
91447636 | 515 | #endif |
0a7de745 A |
516 | int pipe_size = 0; |
517 | int pipe_count; | |
518 | struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */ | |
2d21ac55 | 519 | struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */ |
91447636 | 520 | |
0a7de745 A |
521 | if (cpipe == NULL) { |
522 | return EBADF; | |
523 | } | |
91447636 | 524 | PIPE_LOCK(cpipe); |
2d21ac55 A |
525 | |
526 | #if CONFIG_MACF | |
527 | error = mac_pipe_check_stat(kauth_cred_get(), cpipe); | |
528 | if (error) { | |
529 | PIPE_UNLOCK(cpipe); | |
0a7de745 | 530 | return error; |
2d21ac55 | 531 | } |
91447636 A |
532 | #endif |
533 | if (cpipe->pipe_buffer.buffer == 0) { | |
0a7de745 A |
534 | /* must be stat'ing the write fd */ |
535 | if (cpipe->pipe_peer) { | |
536 | /* the peer still exists, use it's info */ | |
537 | pipe_size = MAX_PIPESIZE(cpipe->pipe_peer); | |
2d21ac55 A |
538 | pipe_count = cpipe->pipe_peer->pipe_buffer.cnt; |
539 | } else { | |
540 | pipe_count = 0; | |
541 | } | |
542 | } else { | |
0a7de745 | 543 | pipe_size = MAX_PIPESIZE(cpipe); |
2d21ac55 | 544 | pipe_count = cpipe->pipe_buffer.cnt; |
91447636 | 545 | } |
2d21ac55 A |
546 | /* |
547 | * since peer's buffer is setup ouside of lock | |
548 | * we might catch it in transient state | |
549 | */ | |
0a7de745 | 550 | if (pipe_size == 0) { |
316670eb | 551 | pipe_size = MAX(PIPE_SIZE, pipesize_blocks[0]); |
0a7de745 | 552 | } |
91447636 | 553 | |
2d21ac55 | 554 | if (isstat64 != 0) { |
0a7de745 | 555 | sb64 = (struct stat64 *)ub; |
91447636 | 556 | |
2d21ac55 A |
557 | bzero(sb64, sizeof(*sb64)); |
558 | sb64->st_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
559 | sb64->st_blksize = pipe_size; | |
560 | sb64->st_size = pipe_count; | |
561 | sb64->st_blocks = (sb64->st_size + sb64->st_blksize - 1) / sb64->st_blksize; | |
0a7de745 | 562 | |
2d21ac55 A |
563 | sb64->st_uid = kauth_getuid(); |
564 | sb64->st_gid = kauth_getgid(); | |
0a7de745 | 565 | |
2d21ac55 A |
566 | sb64->st_atimespec.tv_sec = cpipe->st_atimespec.tv_sec; |
567 | sb64->st_atimespec.tv_nsec = cpipe->st_atimespec.tv_nsec; | |
0a7de745 | 568 | |
2d21ac55 A |
569 | sb64->st_mtimespec.tv_sec = cpipe->st_mtimespec.tv_sec; |
570 | sb64->st_mtimespec.tv_nsec = cpipe->st_mtimespec.tv_nsec; | |
91447636 | 571 | |
2d21ac55 A |
572 | sb64->st_ctimespec.tv_sec = cpipe->st_ctimespec.tv_sec; |
573 | sb64->st_ctimespec.tv_nsec = cpipe->st_ctimespec.tv_nsec; | |
91447636 | 574 | |
2d21ac55 | 575 | /* |
0a7de745 A |
576 | * Return a relatively unique inode number based on the current |
577 | * address of this pipe's struct pipe. This number may be recycled | |
578 | * relatively quickly. | |
579 | */ | |
316670eb | 580 | sb64->st_ino = (ino64_t)VM_KERNEL_ADDRPERM((uintptr_t)cpipe); |
2d21ac55 | 581 | } else { |
0a7de745 | 582 | sb = (struct stat *)ub; |
2d21ac55 A |
583 | |
584 | bzero(sb, sizeof(*sb)); | |
585 | sb->st_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
586 | sb->st_blksize = pipe_size; | |
587 | sb->st_size = pipe_count; | |
588 | sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize; | |
0a7de745 | 589 | |
2d21ac55 A |
590 | sb->st_uid = kauth_getuid(); |
591 | sb->st_gid = kauth_getgid(); | |
0a7de745 | 592 | |
2d21ac55 A |
593 | sb->st_atimespec.tv_sec = cpipe->st_atimespec.tv_sec; |
594 | sb->st_atimespec.tv_nsec = cpipe->st_atimespec.tv_nsec; | |
0a7de745 | 595 | |
2d21ac55 A |
596 | sb->st_mtimespec.tv_sec = cpipe->st_mtimespec.tv_sec; |
597 | sb->st_mtimespec.tv_nsec = cpipe->st_mtimespec.tv_nsec; | |
598 | ||
599 | sb->st_ctimespec.tv_sec = cpipe->st_ctimespec.tv_sec; | |
600 | sb->st_ctimespec.tv_nsec = cpipe->st_ctimespec.tv_nsec; | |
601 | ||
602 | /* | |
0a7de745 A |
603 | * Return a relatively unique inode number based on the current |
604 | * address of this pipe's struct pipe. This number may be recycled | |
605 | * relatively quickly. | |
606 | */ | |
316670eb | 607 | sb->st_ino = (ino_t)VM_KERNEL_ADDRPERM((uintptr_t)cpipe); |
2d21ac55 A |
608 | } |
609 | PIPE_UNLOCK(cpipe); | |
91447636 A |
610 | |
611 | /* | |
2d21ac55 A |
612 | * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen, |
613 | * st_uid, st_gid. | |
614 | * | |
615 | * XXX (st_dev) should be unique, but there is no device driver that | |
616 | * XXX is associated with pipes, since they are implemented via a | |
617 | * XXX struct fileops indirection rather than as FS objects. | |
91447636 | 618 | */ |
0a7de745 | 619 | return 0; |
91447636 A |
620 | } |
621 | ||
622 | ||
623 | /* | |
624 | * Allocate kva for pipe circular buffer, the space is pageable | |
625 | * This routine will 'realloc' the size of a pipe safely, if it fails | |
626 | * it will retain the old buffer. | |
627 | * If it fails it will return ENOMEM. | |
628 | */ | |
629 | static int | |
630 | pipespace(struct pipe *cpipe, int size) | |
631 | { | |
632 | vm_offset_t buffer; | |
633 | ||
0a7de745 A |
634 | if (size <= 0) { |
635 | return EINVAL; | |
636 | } | |
91447636 | 637 | |
0a7de745 A |
638 | if ((buffer = (vm_offset_t)kalloc(size)) == 0) { |
639 | return ENOMEM; | |
640 | } | |
91447636 A |
641 | |
642 | /* free old resources if we're resizing */ | |
643 | pipe_free_kmem(cpipe); | |
644 | cpipe->pipe_buffer.buffer = (caddr_t)buffer; | |
645 | cpipe->pipe_buffer.size = size; | |
646 | cpipe->pipe_buffer.in = 0; | |
647 | cpipe->pipe_buffer.out = 0; | |
648 | cpipe->pipe_buffer.cnt = 0; | |
649 | ||
b0d623f7 A |
650 | OSAddAtomic(1, &amountpipes); |
651 | OSAddAtomic(cpipe->pipe_buffer.size, &amountpipekva); | |
91447636 | 652 | |
0a7de745 | 653 | return 0; |
91447636 A |
654 | } |
655 | ||
656 | /* | |
657 | * initialize and allocate VM and memory for pipe | |
658 | */ | |
659 | static int | |
660 | pipe_create(struct pipe **cpipep) | |
661 | { | |
662 | struct pipe *cpipe; | |
91447636 A |
663 | cpipe = (struct pipe *)zalloc(pipe_zone); |
664 | ||
0a7de745 A |
665 | if ((*cpipep = cpipe) == NULL) { |
666 | return ENOMEM; | |
667 | } | |
91447636 A |
668 | |
669 | /* | |
670 | * protect so pipespace or pipeclose don't follow a junk pointer | |
671 | * if pipespace() fails. | |
672 | */ | |
673 | bzero(cpipe, sizeof *cpipe); | |
674 | ||
0a7de745 | 675 | #ifndef CONFIG_EMBEDDED |
2d21ac55 A |
676 | /* Initial times are all the time of creation of the pipe */ |
677 | pipe_touch(cpipe, PIPE_ATIME | PIPE_MTIME | PIPE_CTIME); | |
5ba3f43e | 678 | #endif |
0a7de745 | 679 | return 0; |
91447636 A |
680 | } |
681 | ||
682 | ||
683 | /* | |
684 | * lock a pipe for I/O, blocking other access | |
685 | */ | |
2d21ac55 | 686 | static inline int |
316670eb | 687 | pipeio_lock(struct pipe *cpipe, int catch) |
91447636 A |
688 | { |
689 | int error; | |
91447636 A |
690 | while (cpipe->pipe_state & PIPE_LOCKFL) { |
691 | cpipe->pipe_state |= PIPE_LWANT; | |
91447636 | 692 | error = msleep(cpipe, PIPE_MTX(cpipe), catch ? (PRIBIO | PCATCH) : PRIBIO, |
0a7de745 A |
693 | "pipelk", 0); |
694 | if (error != 0) { | |
695 | return error; | |
696 | } | |
91447636 A |
697 | } |
698 | cpipe->pipe_state |= PIPE_LOCKFL; | |
0a7de745 | 699 | return 0; |
91447636 A |
700 | } |
701 | ||
702 | /* | |
703 | * unlock a pipe I/O lock | |
704 | */ | |
2d21ac55 | 705 | static inline void |
316670eb | 706 | pipeio_unlock(struct pipe *cpipe) |
91447636 | 707 | { |
91447636 | 708 | cpipe->pipe_state &= ~PIPE_LOCKFL; |
91447636 A |
709 | if (cpipe->pipe_state & PIPE_LWANT) { |
710 | cpipe->pipe_state &= ~PIPE_LWANT; | |
711 | wakeup(cpipe); | |
712 | } | |
713 | } | |
714 | ||
316670eb A |
715 | /* |
716 | * wakeup anyone whos blocked in select | |
717 | */ | |
91447636 | 718 | static void |
2d21ac55 | 719 | pipeselwakeup(struct pipe *cpipe, struct pipe *spipe) |
91447636 | 720 | { |
91447636 A |
721 | if (cpipe->pipe_state & PIPE_SEL) { |
722 | cpipe->pipe_state &= ~PIPE_SEL; | |
723 | selwakeup(&cpipe->pipe_sel); | |
724 | } | |
0a7de745 A |
725 | if (cpipe->pipe_state & PIPE_KNOTE) { |
726 | KNOTE(&cpipe->pipe_sel.si_note, 1); | |
727 | } | |
91447636 A |
728 | |
729 | postpipeevent(cpipe, EV_RWBYTES); | |
730 | ||
731 | if (spipe && (spipe->pipe_state & PIPE_ASYNC) && spipe->pipe_pgid) { | |
0a7de745 A |
732 | if (spipe->pipe_pgid < 0) { |
733 | gsignal(-spipe->pipe_pgid, SIGIO); | |
734 | } else { | |
735 | proc_signal(spipe->pipe_pgid, SIGIO); | |
736 | } | |
737 | } | |
91447636 A |
738 | } |
739 | ||
316670eb A |
740 | /* |
741 | * Read n bytes from the buffer. Semantics are similar to file read. | |
742 | * returns: number of bytes read from the buffer | |
743 | */ | |
91447636 A |
744 | /* ARGSUSED */ |
745 | static int | |
2d21ac55 | 746 | pipe_read(struct fileproc *fp, struct uio *uio, __unused int flags, |
0a7de745 | 747 | __unused vfs_context_t ctx) |
91447636 A |
748 | { |
749 | struct pipe *rpipe = (struct pipe *)fp->f_data; | |
750 | int error; | |
751 | int nread = 0; | |
752 | u_int size; | |
753 | ||
754 | PIPE_LOCK(rpipe); | |
755 | ++rpipe->pipe_busy; | |
756 | ||
316670eb | 757 | error = pipeio_lock(rpipe, 1); |
0a7de745 | 758 | if (error) { |
91447636 | 759 | goto unlocked_error; |
0a7de745 | 760 | } |
91447636 | 761 | |
2d21ac55 A |
762 | #if CONFIG_MACF |
763 | error = mac_pipe_check_read(kauth_cred_get(), rpipe); | |
0a7de745 | 764 | if (error) { |
91447636 | 765 | goto locked_error; |
0a7de745 | 766 | } |
91447636 A |
767 | #endif |
768 | ||
316670eb | 769 | |
91447636 A |
770 | while (uio_resid(uio)) { |
771 | /* | |
772 | * normal pipe buffer receive | |
773 | */ | |
774 | if (rpipe->pipe_buffer.cnt > 0) { | |
316670eb A |
775 | /* |
776 | * # bytes to read is min( bytes from read pointer until end of buffer, | |
0a7de745 | 777 | * total unread bytes, |
316670eb A |
778 | * user requested byte count) |
779 | */ | |
91447636 | 780 | size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; |
0a7de745 | 781 | if (size > rpipe->pipe_buffer.cnt) { |
91447636 | 782 | size = rpipe->pipe_buffer.cnt; |
0a7de745 | 783 | } |
91447636 | 784 | // LP64todo - fix this! |
0a7de745 | 785 | if (size > (u_int) uio_resid(uio)) { |
91447636 | 786 | size = (u_int) uio_resid(uio); |
0a7de745 | 787 | } |
91447636 | 788 | |
316670eb | 789 | PIPE_UNLOCK(rpipe); /* we still hold io lock.*/ |
91447636 | 790 | error = uiomove( |
0a7de745 A |
791 | &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], |
792 | size, uio); | |
91447636 | 793 | PIPE_LOCK(rpipe); |
0a7de745 | 794 | if (error) { |
91447636 | 795 | break; |
0a7de745 | 796 | } |
91447636 A |
797 | |
798 | rpipe->pipe_buffer.out += size; | |
0a7de745 | 799 | if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) { |
91447636 | 800 | rpipe->pipe_buffer.out = 0; |
0a7de745 | 801 | } |
91447636 A |
802 | |
803 | rpipe->pipe_buffer.cnt -= size; | |
0a7de745 | 804 | |
91447636 A |
805 | /* |
806 | * If there is no more to read in the pipe, reset | |
807 | * its pointers to the beginning. This improves | |
808 | * cache hit stats. | |
809 | */ | |
810 | if (rpipe->pipe_buffer.cnt == 0) { | |
811 | rpipe->pipe_buffer.in = 0; | |
812 | rpipe->pipe_buffer.out = 0; | |
813 | } | |
814 | nread += size; | |
91447636 A |
815 | } else { |
816 | /* | |
817 | * detect EOF condition | |
818 | * read returns 0 on EOF, no need to set error | |
819 | */ | |
b0d623f7 | 820 | if (rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 | 821 | break; |
b0d623f7 | 822 | } |
91447636 A |
823 | |
824 | /* | |
825 | * If the "write-side" has been blocked, wake it up now. | |
826 | */ | |
827 | if (rpipe->pipe_state & PIPE_WANTW) { | |
828 | rpipe->pipe_state &= ~PIPE_WANTW; | |
829 | wakeup(rpipe); | |
830 | } | |
831 | ||
832 | /* | |
316670eb | 833 | * Break if some data was read in previous iteration. |
91447636 | 834 | */ |
0a7de745 | 835 | if (nread > 0) { |
91447636 | 836 | break; |
0a7de745 | 837 | } |
91447636 A |
838 | |
839 | /* | |
0a7de745 | 840 | * Unlock the pipe buffer for our remaining processing. |
91447636 A |
841 | * We will either break out with an error or we will |
842 | * sleep and relock to loop. | |
843 | */ | |
316670eb | 844 | pipeio_unlock(rpipe); |
91447636 A |
845 | |
846 | /* | |
847 | * Handle non-blocking mode operation or | |
848 | * wait for more data. | |
849 | */ | |
850 | if (fp->f_flag & FNONBLOCK) { | |
851 | error = EAGAIN; | |
852 | } else { | |
853 | rpipe->pipe_state |= PIPE_WANTR; | |
91447636 | 854 | error = msleep(rpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH, "piperd", 0); |
0a7de745 A |
855 | if (error == 0) { |
856 | error = pipeio_lock(rpipe, 1); | |
857 | } | |
91447636 | 858 | } |
0a7de745 | 859 | if (error) { |
91447636 | 860 | goto unlocked_error; |
0a7de745 | 861 | } |
91447636 A |
862 | } |
863 | } | |
2d21ac55 | 864 | #if CONFIG_MACF |
91447636 A |
865 | locked_error: |
866 | #endif | |
316670eb | 867 | pipeio_unlock(rpipe); |
91447636 A |
868 | |
869 | unlocked_error: | |
870 | --rpipe->pipe_busy; | |
871 | ||
872 | /* | |
873 | * PIPE_WANT processing only makes sense if pipe_busy is 0. | |
874 | */ | |
875 | if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { | |
0a7de745 | 876 | rpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTW); |
91447636 | 877 | wakeup(rpipe); |
316670eb | 878 | } else if (rpipe->pipe_buffer.cnt < rpipe->pipe_buffer.size) { |
91447636 A |
879 | /* |
880 | * Handle write blocking hysteresis. | |
881 | */ | |
882 | if (rpipe->pipe_state & PIPE_WANTW) { | |
883 | rpipe->pipe_state &= ~PIPE_WANTW; | |
884 | wakeup(rpipe); | |
885 | } | |
886 | } | |
887 | ||
0a7de745 | 888 | if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) > 0) { |
91447636 | 889 | pipeselwakeup(rpipe, rpipe->pipe_peer); |
0a7de745 | 890 | } |
91447636 | 891 | |
0a7de745 | 892 | #ifndef CONFIG_EMBEDDED |
2d21ac55 A |
893 | /* update last read time */ |
894 | pipe_touch(rpipe, PIPE_ATIME); | |
5ba3f43e | 895 | #endif |
2d21ac55 | 896 | |
91447636 A |
897 | PIPE_UNLOCK(rpipe); |
898 | ||
0a7de745 | 899 | return error; |
91447636 A |
900 | } |
901 | ||
91447636 | 902 | /* |
0a7de745 | 903 | * perform a write of n bytes into the read side of buffer. Since |
316670eb | 904 | * pipes are unidirectional a write is meant to be read by the otherside only. |
91447636 | 905 | */ |
91447636 | 906 | static int |
2d21ac55 | 907 | pipe_write(struct fileproc *fp, struct uio *uio, __unused int flags, |
0a7de745 | 908 | __unused vfs_context_t ctx) |
91447636 A |
909 | { |
910 | int error = 0; | |
911 | int orig_resid; | |
912 | int pipe_size; | |
913 | struct pipe *wpipe, *rpipe; | |
316670eb A |
914 | // LP64todo - fix this! |
915 | orig_resid = uio_resid(uio); | |
916 | int space; | |
91447636 A |
917 | |
918 | rpipe = (struct pipe *)fp->f_data; | |
919 | ||
920 | PIPE_LOCK(rpipe); | |
921 | wpipe = rpipe->pipe_peer; | |
922 | ||
923 | /* | |
924 | * detect loss of pipe read side, issue SIGPIPE if lost. | |
925 | */ | |
b0d623f7 | 926 | if (wpipe == NULL || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { |
91447636 | 927 | PIPE_UNLOCK(rpipe); |
0a7de745 | 928 | return EPIPE; |
91447636 | 929 | } |
2d21ac55 A |
930 | #if CONFIG_MACF |
931 | error = mac_pipe_check_write(kauth_cred_get(), wpipe); | |
91447636 A |
932 | if (error) { |
933 | PIPE_UNLOCK(rpipe); | |
0a7de745 | 934 | return error; |
91447636 A |
935 | } |
936 | #endif | |
937 | ++wpipe->pipe_busy; | |
938 | ||
939 | pipe_size = 0; | |
940 | ||
91447636 | 941 | /* |
316670eb A |
942 | * need to allocate some storage... we delay the allocation |
943 | * until the first write on fd[0] to avoid allocating storage for both | |
944 | * 'pipe ends'... most pipes are half-duplex with the writes targeting | |
945 | * fd[1], so allocating space for both ends is a waste... | |
91447636 | 946 | */ |
91447636 | 947 | |
0a7de745 A |
948 | if (wpipe->pipe_buffer.buffer == 0 || ( |
949 | (unsigned)orig_resid > wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt && | |
950 | amountpipekva < maxpipekva)) { | |
951 | pipe_size = choose_pipespace(wpipe->pipe_buffer.size, wpipe->pipe_buffer.cnt + orig_resid); | |
91447636 A |
952 | } |
953 | if (pipe_size) { | |
0a7de745 | 954 | /* |
91447636 | 955 | * need to do initial allocation or resizing of pipe |
0a7de745 | 956 | * holding both structure and io locks. |
91447636 | 957 | */ |
316670eb | 958 | if ((error = pipeio_lock(wpipe, 1)) == 0) { |
0a7de745 | 959 | if (wpipe->pipe_buffer.cnt == 0) { |
316670eb | 960 | error = pipespace(wpipe, pipe_size); |
0a7de745 | 961 | } else { |
316670eb | 962 | error = expand_pipespace(wpipe, pipe_size); |
0a7de745 A |
963 | } |
964 | ||
316670eb | 965 | pipeio_unlock(wpipe); |
0a7de745 | 966 | |
316670eb | 967 | /* allocation failed */ |
0a7de745 A |
968 | if (wpipe->pipe_buffer.buffer == 0) { |
969 | error = ENOMEM; | |
970 | } | |
91447636 A |
971 | } |
972 | if (error) { | |
0a7de745 | 973 | /* |
91447636 A |
974 | * If an error occurred unbusy and return, waking up any pending |
975 | * readers. | |
976 | */ | |
0a7de745 A |
977 | --wpipe->pipe_busy; |
978 | if ((wpipe->pipe_busy == 0) && | |
91447636 | 979 | (wpipe->pipe_state & PIPE_WANT)) { |
0a7de745 | 980 | wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); |
91447636 A |
981 | wakeup(wpipe); |
982 | } | |
983 | PIPE_UNLOCK(rpipe); | |
0a7de745 | 984 | return error; |
91447636 A |
985 | } |
986 | } | |
91447636 A |
987 | |
988 | while (uio_resid(uio)) { | |
0a7de745 | 989 | retrywrite: |
91447636 A |
990 | space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; |
991 | ||
316670eb | 992 | /* Writes of size <= PIPE_BUF must be atomic. */ |
0a7de745 | 993 | if ((space < uio_resid(uio)) && (orig_resid <= PIPE_BUF)) { |
91447636 | 994 | space = 0; |
0a7de745 | 995 | } |
91447636 A |
996 | |
997 | if (space > 0) { | |
0a7de745 A |
998 | if ((error = pipeio_lock(wpipe, 1)) == 0) { |
999 | int size; /* Transfer size */ | |
1000 | int segsize; /* first segment to transfer */ | |
91447636 | 1001 | |
b0d623f7 | 1002 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
316670eb | 1003 | pipeio_unlock(wpipe); |
0a7de745 | 1004 | error = EPIPE; |
91447636 A |
1005 | break; |
1006 | } | |
0a7de745 | 1007 | /* |
316670eb | 1008 | * If a process blocked in pipeio_lock, our |
91447636 A |
1009 | * value for space might be bad... the mutex |
1010 | * is dropped while we're blocked | |
1011 | */ | |
0a7de745 | 1012 | if (space > (int)(wpipe->pipe_buffer.size - |
91447636 | 1013 | wpipe->pipe_buffer.cnt)) { |
316670eb | 1014 | pipeio_unlock(wpipe); |
91447636 A |
1015 | goto retrywrite; |
1016 | } | |
1017 | ||
1018 | /* | |
1019 | * Transfer size is minimum of uio transfer | |
1020 | * and free space in pipe buffer. | |
1021 | */ | |
1022 | // LP64todo - fix this! | |
0a7de745 | 1023 | if (space > uio_resid(uio)) { |
91447636 | 1024 | size = uio_resid(uio); |
0a7de745 | 1025 | } else { |
91447636 | 1026 | size = space; |
0a7de745 | 1027 | } |
91447636 | 1028 | /* |
0a7de745 | 1029 | * First segment to transfer is minimum of |
91447636 A |
1030 | * transfer size and contiguous space in |
1031 | * pipe buffer. If first segment to transfer | |
1032 | * is less than the transfer size, we've got | |
1033 | * a wraparound in the buffer. | |
1034 | */ | |
0a7de745 A |
1035 | segsize = wpipe->pipe_buffer.size - |
1036 | wpipe->pipe_buffer.in; | |
1037 | if (segsize > size) { | |
91447636 | 1038 | segsize = size; |
0a7de745 A |
1039 | } |
1040 | ||
91447636 A |
1041 | /* Transfer first segment */ |
1042 | ||
1043 | PIPE_UNLOCK(rpipe); | |
0a7de745 A |
1044 | error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], |
1045 | segsize, uio); | |
91447636 | 1046 | PIPE_LOCK(rpipe); |
0a7de745 | 1047 | |
91447636 | 1048 | if (error == 0 && segsize < size) { |
0a7de745 | 1049 | /* |
91447636 A |
1050 | * Transfer remaining part now, to |
1051 | * support atomic writes. Wraparound | |
316670eb | 1052 | * happened. (State 3) |
91447636 | 1053 | */ |
0a7de745 A |
1054 | if (wpipe->pipe_buffer.in + segsize != |
1055 | wpipe->pipe_buffer.size) { | |
91447636 A |
1056 | panic("Expected pipe buffer " |
1057 | "wraparound disappeared"); | |
0a7de745 A |
1058 | } |
1059 | ||
91447636 A |
1060 | PIPE_UNLOCK(rpipe); |
1061 | error = uiomove( | |
0a7de745 A |
1062 | &wpipe->pipe_buffer.buffer[0], |
1063 | size - segsize, uio); | |
91447636 A |
1064 | PIPE_LOCK(rpipe); |
1065 | } | |
0a7de745 | 1066 | /* |
316670eb A |
1067 | * readers never know to read until count is updated. |
1068 | */ | |
91447636 A |
1069 | if (error == 0) { |
1070 | wpipe->pipe_buffer.in += size; | |
316670eb | 1071 | if (wpipe->pipe_buffer.in > |
91447636 A |
1072 | wpipe->pipe_buffer.size) { |
1073 | if (wpipe->pipe_buffer.in != | |
1074 | size - segsize + | |
0a7de745 | 1075 | wpipe->pipe_buffer.size) { |
91447636 A |
1076 | panic("Expected " |
1077 | "wraparound bad"); | |
0a7de745 | 1078 | } |
91447636 A |
1079 | wpipe->pipe_buffer.in = size - |
1080 | segsize; | |
1081 | } | |
0a7de745 | 1082 | |
91447636 A |
1083 | wpipe->pipe_buffer.cnt += size; |
1084 | if (wpipe->pipe_buffer.cnt > | |
0a7de745 | 1085 | wpipe->pipe_buffer.size) { |
91447636 | 1086 | panic("Pipe buffer overflow"); |
0a7de745 | 1087 | } |
91447636 | 1088 | } |
316670eb | 1089 | pipeio_unlock(wpipe); |
91447636 | 1090 | } |
0a7de745 | 1091 | if (error) { |
91447636 | 1092 | break; |
0a7de745 | 1093 | } |
91447636 A |
1094 | } else { |
1095 | /* | |
1096 | * If the "read-side" has been blocked, wake it up now. | |
1097 | */ | |
1098 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1099 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1100 | wakeup(wpipe); | |
1101 | } | |
1102 | /* | |
1103 | * don't block on non-blocking I/O | |
1104 | * we'll do the pipeselwakeup on the way out | |
1105 | */ | |
1106 | if (fp->f_flag & FNONBLOCK) { | |
1107 | error = EAGAIN; | |
1108 | break; | |
1109 | } | |
6d2010ae A |
1110 | |
1111 | /* | |
1112 | * If read side wants to go away, we just issue a signal | |
1113 | * to ourselves. | |
1114 | */ | |
1115 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { | |
1116 | error = EPIPE; | |
1117 | break; | |
0a7de745 | 1118 | } |
6d2010ae | 1119 | |
91447636 A |
1120 | /* |
1121 | * We have no more space and have something to offer, | |
1122 | * wake up select/poll. | |
1123 | */ | |
1124 | pipeselwakeup(wpipe, wpipe); | |
1125 | ||
1126 | wpipe->pipe_state |= PIPE_WANTW; | |
1127 | ||
1128 | error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, "pipewr", 0); | |
1129 | ||
0a7de745 | 1130 | if (error != 0) { |
91447636 | 1131 | break; |
0a7de745 | 1132 | } |
91447636 A |
1133 | } |
1134 | } | |
1135 | --wpipe->pipe_busy; | |
1136 | ||
1137 | if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { | |
1138 | wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); | |
1139 | wakeup(wpipe); | |
1140 | } | |
1141 | if (wpipe->pipe_buffer.cnt > 0) { | |
1142 | /* | |
1143 | * If there are any characters in the buffer, we wake up | |
1144 | * the reader if it was blocked waiting for data. | |
1145 | */ | |
1146 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1147 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1148 | wakeup(wpipe); | |
1149 | } | |
1150 | /* | |
1151 | * wake up thread blocked in select/poll or post the notification | |
1152 | */ | |
1153 | pipeselwakeup(wpipe, wpipe); | |
1154 | } | |
2d21ac55 | 1155 | |
0a7de745 | 1156 | #ifndef CONFIG_EMBEDDED |
2d21ac55 A |
1157 | /* Update modification, status change (# of bytes in pipe) times */ |
1158 | pipe_touch(rpipe, PIPE_MTIME | PIPE_CTIME); | |
1159 | pipe_touch(wpipe, PIPE_MTIME | PIPE_CTIME); | |
5ba3f43e | 1160 | #endif |
91447636 A |
1161 | PIPE_UNLOCK(rpipe); |
1162 | ||
0a7de745 | 1163 | return error; |
91447636 A |
1164 | } |
1165 | ||
1166 | /* | |
1167 | * we implement a very minimal set of ioctls for compatibility with sockets. | |
1168 | */ | |
1169 | /* ARGSUSED 3 */ | |
1170 | static int | |
2d21ac55 | 1171 | pipe_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, |
0a7de745 | 1172 | __unused vfs_context_t ctx) |
91447636 A |
1173 | { |
1174 | struct pipe *mpipe = (struct pipe *)fp->f_data; | |
2d21ac55 | 1175 | #if CONFIG_MACF |
91447636 A |
1176 | int error; |
1177 | #endif | |
1178 | ||
1179 | PIPE_LOCK(mpipe); | |
1180 | ||
2d21ac55 A |
1181 | #if CONFIG_MACF |
1182 | error = mac_pipe_check_ioctl(kauth_cred_get(), mpipe, cmd); | |
91447636 A |
1183 | if (error) { |
1184 | PIPE_UNLOCK(mpipe); | |
1185 | ||
0a7de745 | 1186 | return error; |
91447636 A |
1187 | } |
1188 | #endif | |
1189 | ||
1190 | switch (cmd) { | |
91447636 A |
1191 | case FIONBIO: |
1192 | PIPE_UNLOCK(mpipe); | |
0a7de745 | 1193 | return 0; |
91447636 A |
1194 | |
1195 | case FIOASYNC: | |
1196 | if (*(int *)data) { | |
1197 | mpipe->pipe_state |= PIPE_ASYNC; | |
1198 | } else { | |
1199 | mpipe->pipe_state &= ~PIPE_ASYNC; | |
1200 | } | |
1201 | PIPE_UNLOCK(mpipe); | |
0a7de745 | 1202 | return 0; |
91447636 A |
1203 | |
1204 | case FIONREAD: | |
316670eb | 1205 | *(int *)data = mpipe->pipe_buffer.cnt; |
91447636 | 1206 | PIPE_UNLOCK(mpipe); |
0a7de745 | 1207 | return 0; |
91447636 A |
1208 | |
1209 | case TIOCSPGRP: | |
1210 | mpipe->pipe_pgid = *(int *)data; | |
1211 | ||
1212 | PIPE_UNLOCK(mpipe); | |
0a7de745 | 1213 | return 0; |
91447636 A |
1214 | |
1215 | case TIOCGPGRP: | |
1216 | *(int *)data = mpipe->pipe_pgid; | |
1217 | ||
1218 | PIPE_UNLOCK(mpipe); | |
0a7de745 | 1219 | return 0; |
91447636 A |
1220 | } |
1221 | PIPE_UNLOCK(mpipe); | |
0a7de745 | 1222 | return ENOTTY; |
91447636 A |
1223 | } |
1224 | ||
1225 | ||
1226 | static int | |
2d21ac55 | 1227 | pipe_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx) |
91447636 A |
1228 | { |
1229 | struct pipe *rpipe = (struct pipe *)fp->f_data; | |
1230 | struct pipe *wpipe; | |
1231 | int retnum = 0; | |
1232 | ||
0a7de745 A |
1233 | if (rpipe == NULL || rpipe == (struct pipe *)-1) { |
1234 | return retnum; | |
1235 | } | |
91447636 A |
1236 | |
1237 | PIPE_LOCK(rpipe); | |
1238 | ||
1239 | wpipe = rpipe->pipe_peer; | |
0a7de745 | 1240 | |
91447636 | 1241 | |
2d21ac55 A |
1242 | #if CONFIG_MACF |
1243 | /* | |
1244 | * XXX We should use a per thread credential here; minimally, the | |
1245 | * XXX process credential should have a persistent reference on it | |
1246 | * XXX before being passed in here. | |
1247 | */ | |
1248 | if (mac_pipe_check_select(vfs_context_ucred(ctx), rpipe, which)) { | |
1249 | PIPE_UNLOCK(rpipe); | |
0a7de745 | 1250 | return 0; |
2d21ac55 A |
1251 | } |
1252 | #endif | |
0a7de745 A |
1253 | switch (which) { |
1254 | case FREAD: | |
91447636 A |
1255 | if ((rpipe->pipe_state & PIPE_DIRECTW) || |
1256 | (rpipe->pipe_buffer.cnt > 0) || | |
b0d623f7 | 1257 | (rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { |
0a7de745 | 1258 | retnum = 1; |
91447636 | 1259 | } else { |
0a7de745 A |
1260 | rpipe->pipe_state |= PIPE_SEL; |
1261 | selrecord(vfs_context_proc(ctx), &rpipe->pipe_sel, wql); | |
91447636 A |
1262 | } |
1263 | break; | |
1264 | ||
0a7de745 A |
1265 | case FWRITE: |
1266 | if (wpipe) { | |
ebb1b9f4 | 1267 | wpipe->pipe_state |= PIPE_WSELECT; |
0a7de745 | 1268 | } |
b0d623f7 | 1269 | if (wpipe == NULL || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) || |
91447636 | 1270 | (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && |
0a7de745 A |
1271 | (MAX_PIPESIZE(wpipe) - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) { |
1272 | retnum = 1; | |
91447636 | 1273 | } else { |
0a7de745 | 1274 | wpipe->pipe_state |= PIPE_SEL; |
2d21ac55 | 1275 | selrecord(vfs_context_proc(ctx), &wpipe->pipe_sel, wql); |
91447636 A |
1276 | } |
1277 | break; | |
0a7de745 A |
1278 | case 0: |
1279 | rpipe->pipe_state |= PIPE_SEL; | |
2d21ac55 | 1280 | selrecord(vfs_context_proc(ctx), &rpipe->pipe_sel, wql); |
91447636 | 1281 | break; |
0a7de745 | 1282 | } |
91447636 A |
1283 | PIPE_UNLOCK(rpipe); |
1284 | ||
0a7de745 | 1285 | return retnum; |
91447636 A |
1286 | } |
1287 | ||
1288 | ||
1289 | /* ARGSUSED 1 */ | |
1290 | static int | |
2d21ac55 | 1291 | pipe_close(struct fileglob *fg, __unused vfs_context_t ctx) |
91447636 | 1292 | { |
0a7de745 | 1293 | struct pipe *cpipe; |
91447636 | 1294 | |
2d21ac55 | 1295 | proc_fdlock_spin(vfs_context_proc(ctx)); |
91447636 A |
1296 | cpipe = (struct pipe *)fg->fg_data; |
1297 | fg->fg_data = NULL; | |
2d21ac55 | 1298 | proc_fdunlock(vfs_context_proc(ctx)); |
0a7de745 A |
1299 | if (cpipe) { |
1300 | pipeclose(cpipe); | |
1301 | } | |
91447636 | 1302 | |
0a7de745 | 1303 | return 0; |
91447636 A |
1304 | } |
1305 | ||
1306 | static void | |
1307 | pipe_free_kmem(struct pipe *cpipe) | |
1308 | { | |
91447636 | 1309 | if (cpipe->pipe_buffer.buffer != NULL) { |
b0d623f7 A |
1310 | OSAddAtomic(-(cpipe->pipe_buffer.size), &amountpipekva); |
1311 | OSAddAtomic(-1, &amountpipes); | |
0a7de745 A |
1312 | kfree(cpipe->pipe_buffer.buffer, |
1313 | cpipe->pipe_buffer.size); | |
91447636 | 1314 | cpipe->pipe_buffer.buffer = NULL; |
316670eb | 1315 | cpipe->pipe_buffer.size = 0; |
91447636 | 1316 | } |
ebb1b9f4 A |
1317 | } |
1318 | ||
91447636 A |
1319 | /* |
1320 | * shutdown the pipe | |
1321 | */ | |
1322 | static void | |
1323 | pipeclose(struct pipe *cpipe) | |
1324 | { | |
1325 | struct pipe *ppipe; | |
1326 | ||
0a7de745 | 1327 | if (cpipe == NULL) { |
91447636 | 1328 | return; |
0a7de745 | 1329 | } |
91447636 | 1330 | /* partially created pipes won't have a valid mutex. */ |
0a7de745 | 1331 | if (PIPE_MTX(cpipe) != NULL) { |
91447636 | 1332 | PIPE_LOCK(cpipe); |
0a7de745 A |
1333 | } |
1334 | ||
91447636 A |
1335 | |
1336 | /* | |
1337 | * If the other side is blocked, wake it up saying that | |
1338 | * we want to close it down. | |
1339 | */ | |
b0d623f7 | 1340 | cpipe->pipe_state &= ~PIPE_DRAIN; |
2d21ac55 A |
1341 | cpipe->pipe_state |= PIPE_EOF; |
1342 | pipeselwakeup(cpipe, cpipe); | |
0a7de745 | 1343 | |
91447636 | 1344 | while (cpipe->pipe_busy) { |
2d21ac55 | 1345 | cpipe->pipe_state |= PIPE_WANT; |
91447636 A |
1346 | |
1347 | wakeup(cpipe); | |
0a7de745 | 1348 | msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0); |
91447636 A |
1349 | } |
1350 | ||
2d21ac55 A |
1351 | #if CONFIG_MACF |
1352 | /* | |
1353 | * Free the shared pipe label only after the two ends are disconnected. | |
1354 | */ | |
0a7de745 | 1355 | if (cpipe->pipe_label != NULL && cpipe->pipe_peer == NULL) { |
2d21ac55 | 1356 | mac_pipe_label_destroy(cpipe); |
0a7de745 | 1357 | } |
91447636 A |
1358 | #endif |
1359 | ||
1360 | /* | |
1361 | * Disconnect from peer | |
1362 | */ | |
1363 | if ((ppipe = cpipe->pipe_peer) != NULL) { | |
b0d623f7 | 1364 | ppipe->pipe_state &= ~(PIPE_DRAIN); |
91447636 A |
1365 | ppipe->pipe_state |= PIPE_EOF; |
1366 | ||
1367 | pipeselwakeup(ppipe, ppipe); | |
1368 | wakeup(ppipe); | |
1369 | ||
0a7de745 A |
1370 | if (cpipe->pipe_state & PIPE_KNOTE) { |
1371 | KNOTE(&ppipe->pipe_sel.si_note, 1); | |
1372 | } | |
91447636 A |
1373 | |
1374 | postpipeevent(ppipe, EV_RCLOSED); | |
1375 | ||
1376 | ppipe->pipe_peer = NULL; | |
1377 | } | |
1378 | evpipefree(cpipe); | |
1379 | ||
1380 | /* | |
1381 | * free resources | |
1382 | */ | |
1383 | if (PIPE_MTX(cpipe) != NULL) { | |
39236c6e A |
1384 | if (ppipe != NULL) { |
1385 | /* | |
91447636 A |
1386 | * since the mutex is shared and the peer is still |
1387 | * alive, we need to release the mutex, not free it | |
1388 | */ | |
39236c6e | 1389 | PIPE_UNLOCK(cpipe); |
91447636 | 1390 | } else { |
39236c6e | 1391 | /* |
91447636 | 1392 | * peer is gone, so we're the sole party left with |
39236c6e | 1393 | * interest in this mutex... unlock and free it |
91447636 | 1394 | */ |
39236c6e | 1395 | PIPE_UNLOCK(cpipe); |
91447636 A |
1396 | lck_mtx_free(PIPE_MTX(cpipe), pipe_mtx_grp); |
1397 | } | |
1398 | } | |
1399 | pipe_free_kmem(cpipe); | |
ebb1b9f4 A |
1400 | if (cpipe->pipe_state & PIPE_WSELECT) { |
1401 | pipe_garbage_collect(cpipe); | |
1402 | } else { | |
1403 | zfree(pipe_zone, cpipe); | |
1404 | pipe_garbage_collect(NULL); | |
1405 | } | |
91447636 A |
1406 | } |
1407 | ||
91447636 A |
1408 | /*ARGSUSED*/ |
1409 | static int | |
39037602 | 1410 | filt_piperead_common(struct knote *kn, struct pipe *rpipe) |
91447636 | 1411 | { |
39037602 A |
1412 | struct pipe *wpipe; |
1413 | int retval; | |
1414 | ||
1415 | /* | |
1416 | * we're being called back via the KNOTE post | |
1417 | * we made in pipeselwakeup, and we already hold the mutex... | |
1418 | */ | |
1419 | ||
1420 | wpipe = rpipe->pipe_peer; | |
1421 | kn->kn_data = rpipe->pipe_buffer.cnt; | |
1422 | if ((rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) || | |
1423 | (wpipe == NULL) || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { | |
1424 | kn->kn_flags |= EV_EOF; | |
1425 | retval = 1; | |
1426 | } else { | |
1427 | int64_t lowwat = 1; | |
1428 | if (kn->kn_sfflags & NOTE_LOWAT) { | |
0a7de745 | 1429 | if (rpipe->pipe_buffer.size && kn->kn_sdata > MAX_PIPESIZE(rpipe)) { |
39037602 | 1430 | lowwat = MAX_PIPESIZE(rpipe); |
0a7de745 | 1431 | } else if (kn->kn_sdata > lowwat) { |
39037602 | 1432 | lowwat = kn->kn_sdata; |
0a7de745 | 1433 | } |
39037602 A |
1434 | } |
1435 | retval = kn->kn_data >= lowwat; | |
1436 | } | |
0a7de745 | 1437 | return retval; |
39037602 | 1438 | } |
91447636 | 1439 | |
39037602 A |
1440 | static int |
1441 | filt_piperead(struct knote *kn, long hint) | |
1442 | { | |
1443 | #pragma unused(hint) | |
1444 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1445 | ||
1446 | return filt_piperead_common(kn, rpipe); | |
1447 | } | |
d9a64523 | 1448 | |
39037602 A |
1449 | static int |
1450 | filt_pipereadtouch(struct knote *kn, struct kevent_internal_s *kev) | |
1451 | { | |
1452 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1453 | int retval; | |
1454 | ||
1455 | PIPE_LOCK(rpipe); | |
1456 | ||
1457 | /* accept new inputs (and save the low water threshold and flag) */ | |
1458 | kn->kn_sdata = kev->data; | |
1459 | kn->kn_sfflags = kev->fflags; | |
39037602 A |
1460 | |
1461 | /* identify if any events are now fired */ | |
1462 | retval = filt_piperead_common(kn, rpipe); | |
1463 | ||
1464 | PIPE_UNLOCK(rpipe); | |
1465 | ||
1466 | return retval; | |
1467 | } | |
1468 | ||
1469 | static int | |
1470 | filt_pipereadprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev) | |
1471 | { | |
1472 | #pragma unused(data) | |
1473 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1474 | int retval; | |
1475 | ||
1476 | PIPE_LOCK(rpipe); | |
1477 | retval = filt_piperead_common(kn, rpipe); | |
1478 | if (retval) { | |
1479 | *kev = kn->kn_kevent; | |
1480 | if (kn->kn_flags & EV_CLEAR) { | |
1481 | kn->kn_fflags = 0; | |
1482 | kn->kn_data = 0; | |
1483 | } | |
1484 | } | |
1485 | PIPE_UNLOCK(rpipe); | |
1486 | ||
0a7de745 | 1487 | return retval; |
39037602 A |
1488 | } |
1489 | ||
1490 | /*ARGSUSED*/ | |
1491 | static int | |
1492 | filt_pipewrite_common(struct knote *kn, struct pipe *rpipe) | |
1493 | { | |
1494 | struct pipe *wpipe; | |
1495 | ||
1496 | /* | |
1497 | * we're being called back via the KNOTE post | |
1498 | * we made in pipeselwakeup, and we already hold the mutex... | |
1499 | */ | |
1500 | wpipe = rpipe->pipe_peer; | |
1501 | ||
1502 | if ((wpipe == NULL) || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { | |
1503 | kn->kn_data = 0; | |
0a7de745 A |
1504 | kn->kn_flags |= EV_EOF; |
1505 | return 1; | |
39037602 A |
1506 | } |
1507 | kn->kn_data = MAX_PIPESIZE(wpipe) - wpipe->pipe_buffer.cnt; | |
1508 | ||
1509 | int64_t lowwat = PIPE_BUF; | |
1510 | if (kn->kn_sfflags & NOTE_LOWAT) { | |
0a7de745 | 1511 | if (wpipe->pipe_buffer.size && kn->kn_sdata > MAX_PIPESIZE(wpipe)) { |
39037602 | 1512 | lowwat = MAX_PIPESIZE(wpipe); |
0a7de745 | 1513 | } else if (kn->kn_sdata > lowwat) { |
39037602 | 1514 | lowwat = kn->kn_sdata; |
0a7de745 | 1515 | } |
39037602 A |
1516 | } |
1517 | ||
0a7de745 | 1518 | return kn->kn_data >= lowwat; |
39037602 A |
1519 | } |
1520 | ||
1521 | /*ARGSUSED*/ | |
1522 | static int | |
1523 | filt_pipewrite(struct knote *kn, long hint) | |
1524 | { | |
1525 | #pragma unused(hint) | |
1526 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1527 | ||
1528 | return filt_pipewrite_common(kn, rpipe); | |
1529 | } | |
1530 | ||
1531 | ||
1532 | static int | |
1533 | filt_pipewritetouch(struct knote *kn, struct kevent_internal_s *kev) | |
1534 | { | |
1535 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1536 | int res; | |
1537 | ||
1538 | PIPE_LOCK(rpipe); | |
1539 | ||
1540 | /* accept new kevent data (and save off lowat threshold and flag) */ | |
1541 | kn->kn_sfflags = kev->fflags; | |
1542 | kn->kn_sdata = kev->data; | |
39037602 A |
1543 | |
1544 | /* determine if any event is now deemed fired */ | |
1545 | res = filt_pipewrite_common(kn, rpipe); | |
1546 | ||
1547 | PIPE_UNLOCK(rpipe); | |
1548 | ||
1549 | return res; | |
1550 | } | |
1551 | ||
1552 | static int | |
1553 | filt_pipewriteprocess(struct knote *kn, struct filt_process_s *data, struct kevent_internal_s *kev) | |
1554 | { | |
1555 | #pragma unused(data) | |
1556 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1557 | int res; | |
1558 | ||
1559 | PIPE_LOCK(rpipe); | |
1560 | res = filt_pipewrite_common(kn, rpipe); | |
1561 | if (res) { | |
1562 | *kev = kn->kn_kevent; | |
1563 | if (kn->kn_flags & EV_CLEAR) { | |
1564 | kn->kn_fflags = 0; | |
1565 | kn->kn_data = 0; | |
1566 | } | |
1567 | } | |
1568 | PIPE_UNLOCK(rpipe); | |
1569 | ||
1570 | return res; | |
1571 | } | |
1572 | ||
1573 | /*ARGSUSED*/ | |
1574 | static int | |
5ba3f43e | 1575 | pipe_kqfilter(__unused struct fileproc *fp, struct knote *kn, |
0a7de745 | 1576 | __unused struct kevent_internal_s *kev, __unused vfs_context_t ctx) |
39037602 A |
1577 | { |
1578 | struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data; | |
1579 | int res; | |
91447636 A |
1580 | |
1581 | PIPE_LOCK(cpipe); | |
2d21ac55 A |
1582 | #if CONFIG_MACF |
1583 | /* | |
1584 | * XXX We should use a per thread credential here; minimally, the | |
1585 | * XXX process credential should have a persistent reference on it | |
1586 | * XXX before being passed in here. | |
1587 | */ | |
1588 | if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx), kn, cpipe) != 0) { | |
1589 | PIPE_UNLOCK(cpipe); | |
39037602 A |
1590 | kn->kn_flags = EV_ERROR; |
1591 | kn->kn_data = EPERM; | |
1592 | return 0; | |
2d21ac55 A |
1593 | } |
1594 | #endif | |
91447636 A |
1595 | |
1596 | switch (kn->kn_filter) { | |
1597 | case EVFILT_READ: | |
39037602 | 1598 | kn->kn_filtid = EVFILTID_PIPE_R; |
2d21ac55 | 1599 | |
39037602 A |
1600 | /* determine initial state */ |
1601 | res = filt_piperead_common(kn, cpipe); | |
91447636 | 1602 | break; |
39037602 | 1603 | |
91447636 | 1604 | case EVFILT_WRITE: |
39037602 | 1605 | kn->kn_filtid = EVFILTID_PIPE_W; |
91447636 A |
1606 | |
1607 | if (cpipe->pipe_peer == NULL) { | |
1608 | /* | |
1609 | * other end of pipe has been closed | |
1610 | */ | |
0a7de745 | 1611 | PIPE_UNLOCK(cpipe); |
39037602 A |
1612 | kn->kn_flags = EV_ERROR; |
1613 | kn->kn_data = EPIPE; | |
1614 | return 0; | |
91447636 | 1615 | } |
0a7de745 A |
1616 | if (cpipe->pipe_peer) { |
1617 | cpipe = cpipe->pipe_peer; | |
1618 | } | |
39037602 A |
1619 | |
1620 | /* determine inital state */ | |
1621 | res = filt_pipewrite_common(kn, cpipe); | |
91447636 A |
1622 | break; |
1623 | default: | |
0a7de745 | 1624 | PIPE_UNLOCK(cpipe); |
39037602 A |
1625 | kn->kn_flags = EV_ERROR; |
1626 | kn->kn_data = EINVAL; | |
1627 | return 0; | |
91447636 A |
1628 | } |
1629 | ||
0a7de745 A |
1630 | if (KNOTE_ATTACH(&cpipe->pipe_sel.si_note, kn)) { |
1631 | cpipe->pipe_state |= PIPE_KNOTE; | |
1632 | } | |
91447636 A |
1633 | |
1634 | PIPE_UNLOCK(cpipe); | |
39037602 | 1635 | return res; |
91447636 A |
1636 | } |
1637 | ||
1638 | static void | |
1639 | filt_pipedetach(struct knote *kn) | |
1640 | { | |
1641 | struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data; | |
1642 | ||
1643 | PIPE_LOCK(cpipe); | |
1644 | ||
1645 | if (kn->kn_filter == EVFILT_WRITE) { | |
0a7de745 A |
1646 | if (cpipe->pipe_peer == NULL) { |
1647 | PIPE_UNLOCK(cpipe); | |
91447636 A |
1648 | return; |
1649 | } | |
1650 | cpipe = cpipe->pipe_peer; | |
1651 | } | |
1652 | if (cpipe->pipe_state & PIPE_KNOTE) { | |
0a7de745 A |
1653 | if (KNOTE_DETACH(&cpipe->pipe_sel.si_note, kn)) { |
1654 | cpipe->pipe_state &= ~PIPE_KNOTE; | |
1655 | } | |
91447636 A |
1656 | } |
1657 | PIPE_UNLOCK(cpipe); | |
1658 | } | |
1659 | ||
0c530ab8 A |
1660 | int |
1661 | fill_pipeinfo(struct pipe * cpipe, struct pipe_info * pinfo) | |
1662 | { | |
2d21ac55 | 1663 | #if CONFIG_MACF |
0a7de745 | 1664 | int error; |
0c530ab8 | 1665 | #endif |
5ba3f43e | 1666 | struct timespec now; |
2d21ac55 A |
1667 | struct vinfo_stat * ub; |
1668 | int pipe_size = 0; | |
1669 | int pipe_count; | |
0c530ab8 | 1670 | |
0a7de745 A |
1671 | if (cpipe == NULL) { |
1672 | return EBADF; | |
1673 | } | |
0c530ab8 | 1674 | PIPE_LOCK(cpipe); |
2d21ac55 A |
1675 | |
1676 | #if CONFIG_MACF | |
1677 | error = mac_pipe_check_stat(kauth_cred_get(), cpipe); | |
1678 | if (error) { | |
1679 | PIPE_UNLOCK(cpipe); | |
0a7de745 | 1680 | return error; |
2d21ac55 | 1681 | } |
0c530ab8 A |
1682 | #endif |
1683 | if (cpipe->pipe_buffer.buffer == 0) { | |
0a7de745 | 1684 | /* |
0c530ab8 A |
1685 | * must be stat'ing the write fd |
1686 | */ | |
0a7de745 A |
1687 | if (cpipe->pipe_peer) { |
1688 | /* | |
2d21ac55 A |
1689 | * the peer still exists, use it's info |
1690 | */ | |
0a7de745 | 1691 | pipe_size = MAX_PIPESIZE(cpipe->pipe_peer); |
2d21ac55 A |
1692 | pipe_count = cpipe->pipe_peer->pipe_buffer.cnt; |
1693 | } else { | |
1694 | pipe_count = 0; | |
1695 | } | |
1696 | } else { | |
0a7de745 | 1697 | pipe_size = MAX_PIPESIZE(cpipe); |
2d21ac55 | 1698 | pipe_count = cpipe->pipe_buffer.cnt; |
0c530ab8 | 1699 | } |
2d21ac55 A |
1700 | /* |
1701 | * since peer's buffer is setup ouside of lock | |
1702 | * we might catch it in transient state | |
1703 | */ | |
0a7de745 | 1704 | if (pipe_size == 0) { |
2d21ac55 | 1705 | pipe_size = PIPE_SIZE; |
0a7de745 | 1706 | } |
0c530ab8 A |
1707 | |
1708 | ub = &pinfo->pipe_stat; | |
1709 | ||
1710 | bzero(ub, sizeof(*ub)); | |
2d21ac55 A |
1711 | ub->vst_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
1712 | ub->vst_blksize = pipe_size; | |
1713 | ub->vst_size = pipe_count; | |
0a7de745 | 1714 | if (ub->vst_blksize != 0) { |
2d21ac55 | 1715 | ub->vst_blocks = (ub->vst_size + ub->vst_blksize - 1) / ub->vst_blksize; |
0a7de745 | 1716 | } |
2d21ac55 | 1717 | ub->vst_nlink = 1; |
0c530ab8 | 1718 | |
2d21ac55 A |
1719 | ub->vst_uid = kauth_getuid(); |
1720 | ub->vst_gid = kauth_getgid(); | |
0c530ab8 | 1721 | |
5ba3f43e | 1722 | nanotime(&now); |
2d21ac55 | 1723 | ub->vst_atime = now.tv_sec; |
5ba3f43e | 1724 | ub->vst_atimensec = now.tv_nsec; |
0c530ab8 | 1725 | |
2d21ac55 | 1726 | ub->vst_mtime = now.tv_sec; |
5ba3f43e | 1727 | ub->vst_mtimensec = now.tv_nsec; |
0c530ab8 | 1728 | |
2d21ac55 | 1729 | ub->vst_ctime = now.tv_sec; |
5ba3f43e | 1730 | ub->vst_ctimensec = now.tv_nsec; |
0c530ab8 A |
1731 | |
1732 | /* | |
1733 | * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid. | |
1734 | * XXX (st_dev, st_ino) should be unique. | |
1735 | */ | |
1736 | ||
39236c6e A |
1737 | pinfo->pipe_handle = (uint64_t)VM_KERNEL_ADDRPERM((uintptr_t)cpipe); |
1738 | pinfo->pipe_peerhandle = (uint64_t)VM_KERNEL_ADDRPERM((uintptr_t)(cpipe->pipe_peer)); | |
0c530ab8 | 1739 | pinfo->pipe_status = cpipe->pipe_state; |
2d21ac55 A |
1740 | |
1741 | PIPE_UNLOCK(cpipe); | |
1742 | ||
0a7de745 | 1743 | return 0; |
0c530ab8 | 1744 | } |
b0d623f7 A |
1745 | |
1746 | ||
0a7de745 | 1747 | static int |
b0d623f7 A |
1748 | pipe_drain(struct fileproc *fp, __unused vfs_context_t ctx) |
1749 | { | |
b0d623f7 A |
1750 | /* Note: fdlock already held */ |
1751 | struct pipe *ppipe, *cpipe = (struct pipe *)(fp->f_fglob->fg_data); | |
1752 | ||
1753 | if (cpipe) { | |
1754 | PIPE_LOCK(cpipe); | |
0a7de745 | 1755 | cpipe->pipe_state |= PIPE_DRAIN; |
b0d623f7 A |
1756 | cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW); |
1757 | wakeup(cpipe); | |
0a7de745 | 1758 | |
b0d623f7 A |
1759 | /* Must wake up peer: a writer sleeps on the read side */ |
1760 | if ((ppipe = cpipe->pipe_peer)) { | |
1761 | ppipe->pipe_state |= PIPE_DRAIN; | |
1762 | ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW); | |
1763 | wakeup(ppipe); | |
1764 | } | |
0a7de745 | 1765 | |
b0d623f7 A |
1766 | PIPE_UNLOCK(cpipe); |
1767 | return 0; | |
1768 | } | |
1769 | ||
1770 | return 1; | |
1771 | } | |
1772 | ||
1773 | ||
0a7de745 | 1774 | /* |
316670eb A |
1775 | * When a thread sets a write-select on a pipe, it creates an implicit, |
1776 | * untracked dependency between that thread and the peer of the pipe | |
1777 | * on which the select is set. If the peer pipe is closed and freed | |
1778 | * before the select()ing thread wakes up, the system will panic as | |
1779 | * it attempts to unwind the dangling select(). To avoid that panic, | |
1780 | * we notice whenever a dangerous select() is set on a pipe, and | |
1781 | * defer the final deletion of the pipe until that select()s are all | |
1782 | * resolved. Since we can't currently detect exactly when that | |
0a7de745 | 1783 | * resolution happens, we use a simple garbage collection queue to |
316670eb A |
1784 | * reap the at-risk pipes 'later'. |
1785 | */ | |
1786 | static void | |
1787 | pipe_garbage_collect(struct pipe *cpipe) | |
1788 | { | |
1789 | uint64_t old, now; | |
1790 | struct pipe_garbage *pgp; | |
1791 | ||
1792 | /* Convert msecs to nsecs and then to abstime */ | |
1793 | old = pipe_garbage_age_limit * 1000000; | |
1794 | nanoseconds_to_absolutetime(old, &old); | |
1795 | ||
1796 | lck_mtx_lock(pipe_garbage_lock); | |
1797 | ||
1798 | /* Free anything that's been on the queue for <mumble> seconds */ | |
1799 | now = mach_absolute_time(); | |
1800 | old = now - old; | |
1801 | while ((pgp = pipe_garbage_head) && pgp->pg_timestamp < old) { | |
1802 | pipe_garbage_head = pgp->pg_next; | |
0a7de745 | 1803 | if (pipe_garbage_head == NULL) { |
316670eb | 1804 | pipe_garbage_tail = NULL; |
0a7de745 | 1805 | } |
316670eb A |
1806 | pipe_garbage_count--; |
1807 | zfree(pipe_zone, pgp->pg_pipe); | |
1808 | zfree(pipe_garbage_zone, pgp); | |
1809 | } | |
1810 | ||
1811 | /* Add the new pipe (if any) to the tail of the garbage queue */ | |
1812 | if (cpipe) { | |
1813 | cpipe->pipe_state = PIPE_DEAD; | |
1814 | pgp = (struct pipe_garbage *)zalloc(pipe_garbage_zone); | |
1815 | if (pgp == NULL) { | |
1816 | /* | |
1817 | * We're too low on memory to garbage collect the | |
1818 | * pipe. Freeing it runs the risk of panicing the | |
1819 | * system. All we can do is leak it and leave | |
1820 | * a breadcrumb behind. The good news, such as it | |
1821 | * is, is that this will probably never happen. | |
1822 | * We will probably hit the panic below first. | |
1823 | */ | |
1824 | printf("Leaking pipe %p - no room left in the queue", | |
1825 | cpipe); | |
1826 | lck_mtx_unlock(pipe_garbage_lock); | |
1827 | return; | |
1828 | } | |
1829 | ||
1830 | pgp->pg_pipe = cpipe; | |
1831 | pgp->pg_timestamp = now; | |
1832 | pgp->pg_next = NULL; | |
b0d623f7 | 1833 | |
0a7de745 | 1834 | if (pipe_garbage_tail) { |
316670eb | 1835 | pipe_garbage_tail->pg_next = pgp; |
0a7de745 | 1836 | } |
316670eb | 1837 | pipe_garbage_tail = pgp; |
0a7de745 | 1838 | if (pipe_garbage_head == NULL) { |
316670eb | 1839 | pipe_garbage_head = pipe_garbage_tail; |
0a7de745 | 1840 | } |
b0d623f7 | 1841 | |
0a7de745 | 1842 | if (pipe_garbage_count++ >= PIPE_GARBAGE_QUEUE_LIMIT) { |
316670eb A |
1843 | panic("Length of pipe garbage queue exceeded %d", |
1844 | PIPE_GARBAGE_QUEUE_LIMIT); | |
0a7de745 | 1845 | } |
316670eb A |
1846 | } |
1847 | lck_mtx_unlock(pipe_garbage_lock); | |
1848 | } |