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