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