]>
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 | /* | |
2d21ac55 | 20 | * Copyright (c) 2003-2007 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. | |
58 | */ | |
59 | ||
60 | /* | |
61 | * This code has two modes of operation, a small write mode and a large | |
62 | * write mode. The small write mode acts like conventional pipes with | |
63 | * a kernel buffer. If the buffer is less than PIPE_MINDIRECT, then the | |
64 | * "normal" pipe buffering is done. If the buffer is between PIPE_MINDIRECT | |
65 | * and PIPE_SIZE in size, it is fully mapped and wired into the kernel, and | |
66 | * the receiving process can copy it directly from the pages in the sending | |
67 | * process. | |
68 | * | |
69 | * If the sending process receives a signal, it is possible that it will | |
70 | * go away, and certainly its address space can change, because control | |
71 | * is returned back to the user-mode side. In that case, the pipe code | |
72 | * arranges to copy the buffer supplied by the user process, to a pageable | |
73 | * kernel buffer, and the receiving process will grab the data from the | |
74 | * pageable kernel buffer. Since signals don't happen all that often, | |
75 | * the copy operation is normally eliminated. | |
76 | * | |
77 | * The constant PIPE_MINDIRECT is chosen to make sure that buffering will | |
78 | * happen for small transfers so that the system will not spend all of | |
79 | * its time context switching. | |
80 | * | |
81 | * In order to limit the resource use of pipes, two sysctls exist: | |
82 | * | |
83 | * kern.ipc.maxpipekva - This is a hard limit on the amount of pageable | |
84 | * address space available to us in pipe_map. Whenever the amount in use | |
85 | * exceeds half of this value, all new pipes will be created with size | |
86 | * SMALL_PIPE_SIZE, rather than PIPE_SIZE. Big pipe creation will be limited | |
87 | * as well. This value is loader tunable only. | |
88 | * | |
89 | * kern.ipc.maxpipekvawired - This value limits the amount of memory that may | |
90 | * be wired in order to facilitate direct copies using page flipping. | |
91 | * Whenever this value is exceeded, pipes will fall back to using regular | |
92 | * copies. This value is sysctl controllable at all times. | |
93 | * | |
94 | * These values are autotuned in subr_param.c. | |
95 | * | |
96 | * Memory usage may be monitored through the sysctls | |
97 | * kern.ipc.pipes, kern.ipc.pipekva and kern.ipc.pipekvawired. | |
98 | * | |
99 | */ | |
100 | ||
101 | #include <sys/param.h> | |
102 | #include <sys/systm.h> | |
103 | #include <sys/filedesc.h> | |
104 | #include <sys/kernel.h> | |
105 | #include <sys/vnode.h> | |
106 | #include <sys/proc_internal.h> | |
107 | #include <sys/kauth.h> | |
108 | #include <sys/file_internal.h> | |
109 | #include <sys/stat.h> | |
110 | #include <sys/ioctl.h> | |
111 | #include <sys/fcntl.h> | |
112 | #include <sys/malloc.h> | |
113 | #include <sys/syslog.h> | |
114 | #include <sys/unistd.h> | |
115 | #include <sys/resourcevar.h> | |
116 | #include <sys/aio_kern.h> | |
117 | #include <sys/signalvar.h> | |
118 | #include <sys/pipe.h> | |
119 | #include <sys/sysproto.h> | |
0c530ab8 | 120 | #include <sys/proc_info.h> |
91447636 | 121 | |
b0d623f7 | 122 | #include <security/audit/audit.h> |
91447636 A |
123 | |
124 | #include <sys/kdebug.h> | |
125 | ||
126 | #include <kern/zalloc.h> | |
127 | #include <vm/vm_kern.h> | |
128 | #include <libkern/OSAtomic.h> | |
129 | ||
130 | #define f_flag f_fglob->fg_flag | |
131 | #define f_type f_fglob->fg_type | |
132 | #define f_msgcount f_fglob->fg_msgcount | |
133 | #define f_cred f_fglob->fg_cred | |
134 | #define f_ops f_fglob->fg_ops | |
135 | #define f_offset f_fglob->fg_offset | |
136 | #define f_data f_fglob->fg_data | |
137 | /* | |
138 | * Use this define if you want to disable *fancy* VM things. Expect an | |
139 | * approx 30% decrease in transfer rate. This could be useful for | |
140 | * NetBSD or OpenBSD. | |
141 | * | |
142 | * this needs to be ported to X and the performance measured | |
143 | * before committing to supporting it | |
144 | */ | |
145 | #define PIPE_NODIRECT 1 | |
146 | ||
147 | #ifndef PIPE_NODIRECT | |
148 | ||
149 | #include <vm/vm.h> | |
150 | #include <vm/vm_param.h> | |
151 | #include <vm/vm_object.h> | |
152 | #include <vm/vm_kern.h> | |
153 | #include <vm/vm_extern.h> | |
154 | #include <vm/pmap.h> | |
155 | #include <vm/vm_map.h> | |
156 | #include <vm/vm_page.h> | |
157 | #include <vm/uma.h> | |
158 | ||
159 | #endif | |
160 | ||
91447636 A |
161 | /* |
162 | * interfaces to the outside world | |
163 | */ | |
164 | static int pipe_read(struct fileproc *fp, struct uio *uio, | |
2d21ac55 | 165 | int flags, vfs_context_t ctx); |
91447636 A |
166 | |
167 | static int pipe_write(struct fileproc *fp, struct uio *uio, | |
2d21ac55 | 168 | int flags, vfs_context_t ctx); |
91447636 | 169 | |
2d21ac55 | 170 | static int pipe_close(struct fileglob *fg, vfs_context_t ctx); |
91447636 | 171 | |
2d21ac55 A |
172 | static int pipe_select(struct fileproc *fp, int which, void * wql, |
173 | vfs_context_t ctx); | |
91447636 | 174 | |
2d21ac55 A |
175 | static int pipe_kqfilter(struct fileproc *fp, struct knote *kn, |
176 | vfs_context_t ctx); | |
91447636 | 177 | |
2d21ac55 A |
178 | static int pipe_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, |
179 | vfs_context_t ctx); | |
91447636 | 180 | |
b0d623f7 A |
181 | static int pipe_drain(struct fileproc *fp,vfs_context_t ctx); |
182 | ||
91447636 A |
183 | |
184 | struct fileops pipeops = | |
185 | { pipe_read, | |
186 | pipe_write, | |
187 | pipe_ioctl, | |
188 | pipe_select, | |
189 | pipe_close, | |
190 | pipe_kqfilter, | |
b0d623f7 | 191 | pipe_drain }; |
91447636 A |
192 | |
193 | ||
194 | static void filt_pipedetach(struct knote *kn); | |
195 | static int filt_piperead(struct knote *kn, long hint); | |
196 | static int filt_pipewrite(struct knote *kn, long hint); | |
197 | ||
b0d623f7 A |
198 | static struct filterops pipe_rfiltops = { |
199 | .f_isfd = 1, | |
200 | .f_detach = filt_pipedetach, | |
201 | .f_event = filt_piperead, | |
202 | }; | |
203 | static struct filterops pipe_wfiltops = { | |
204 | .f_isfd = 1, | |
205 | .f_detach = filt_pipedetach, | |
206 | .f_event = filt_pipewrite, | |
207 | }; | |
91447636 A |
208 | |
209 | /* | |
210 | * Default pipe buffer size(s), this can be kind-of large now because pipe | |
211 | * space is pageable. The pipe code will try to maintain locality of | |
212 | * reference for performance reasons, so small amounts of outstanding I/O | |
213 | * will not wipe the cache. | |
214 | */ | |
215 | #define MINPIPESIZE (PIPE_SIZE/3) | |
216 | ||
217 | /* | |
218 | * Limit the number of "big" pipes | |
219 | */ | |
220 | #define LIMITBIGPIPES 32 | |
221 | static int nbigpipe; | |
222 | ||
223 | static int amountpipes; | |
224 | static int amountpipekva; | |
225 | ||
226 | #ifndef PIPE_NODIRECT | |
227 | static int amountpipekvawired; | |
228 | #endif | |
229 | int maxpipekva = 1024 * 1024 * 16; | |
230 | ||
231 | #if PIPE_SYSCTLS | |
232 | SYSCTL_DECL(_kern_ipc); | |
233 | ||
6d2010ae | 234 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekva, CTLFLAG_RD|CTLFLAG_LOCKED, |
91447636 | 235 | &maxpipekva, 0, "Pipe KVA limit"); |
6d2010ae | 236 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxpipekvawired, CTLFLAG_RW|CTLFLAG_LOCKED, |
91447636 | 237 | &maxpipekvawired, 0, "Pipe KVA wired limit"); |
6d2010ae | 238 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipes, CTLFLAG_RD|CTLFLAG_LOCKED, |
91447636 | 239 | &amountpipes, 0, "Current # of pipes"); |
6d2010ae | 240 | SYSCTL_INT(_kern_ipc, OID_AUTO, bigpipes, CTLFLAG_RD|CTLFLAG_LOCKED, |
91447636 | 241 | &nbigpipe, 0, "Current # of big pipes"); |
6d2010ae | 242 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipekva, CTLFLAG_RD|CTLFLAG_LOCKED, |
91447636 | 243 | &amountpipekva, 0, "Pipe KVA usage"); |
6d2010ae | 244 | SYSCTL_INT(_kern_ipc, OID_AUTO, pipekvawired, CTLFLAG_RD|CTLFLAG_LOCKED, |
91447636 A |
245 | &amountpipekvawired, 0, "Pipe wired KVA usage"); |
246 | #endif | |
247 | ||
91447636 A |
248 | static void pipeclose(struct pipe *cpipe); |
249 | static void pipe_free_kmem(struct pipe *cpipe); | |
250 | static int pipe_create(struct pipe **cpipep); | |
251 | static void pipeselwakeup(struct pipe *cpipe, struct pipe *spipe); | |
252 | static __inline int pipelock(struct pipe *cpipe, int catch); | |
253 | static __inline void pipeunlock(struct pipe *cpipe); | |
254 | ||
255 | #ifndef PIPE_NODIRECT | |
256 | static int pipe_build_write_buffer(struct pipe *wpipe, struct uio *uio); | |
257 | static void pipe_destroy_write_buffer(struct pipe *wpipe); | |
258 | static int pipe_direct_write(struct pipe *wpipe, struct uio *uio); | |
259 | static void pipe_clone_write_buffer(struct pipe *wpipe); | |
260 | #endif | |
261 | ||
262 | extern int postpipeevent(struct pipe *, int); | |
263 | extern void evpipefree(struct pipe *cpipe); | |
264 | ||
265 | ||
266 | static int pipespace(struct pipe *cpipe, int size); | |
267 | ||
268 | static lck_grp_t *pipe_mtx_grp; | |
269 | static lck_attr_t *pipe_mtx_attr; | |
270 | static lck_grp_attr_t *pipe_mtx_grp_attr; | |
271 | ||
272 | static zone_t pipe_zone; | |
273 | ||
274 | SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_ANY, pipeinit, NULL); | |
275 | ||
276 | void | |
2d21ac55 | 277 | pipeinit(void) |
91447636 A |
278 | { |
279 | pipe_zone = (zone_t)zinit(sizeof(struct pipe), 8192 * sizeof(struct pipe), 4096, "pipe zone"); | |
280 | ||
281 | /* | |
282 | * allocate lock group attribute and group for pipe mutexes | |
283 | */ | |
284 | pipe_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
285 | pipe_mtx_grp = lck_grp_alloc_init("pipe", pipe_mtx_grp_attr); |
286 | ||
287 | /* | |
288 | * allocate the lock attribute for pipe mutexes | |
289 | */ | |
290 | pipe_mtx_attr = lck_attr_alloc_init(); | |
91447636 A |
291 | } |
292 | ||
2d21ac55 A |
293 | /* Bitmap for things to touch in pipe_touch() */ |
294 | #define PIPE_ATIME 0x00000001 /* time of last access */ | |
295 | #define PIPE_MTIME 0x00000002 /* time of last modification */ | |
296 | #define PIPE_CTIME 0x00000004 /* time of last status change */ | |
297 | ||
298 | static void | |
299 | pipe_touch(struct pipe *tpipe, int touch) | |
300 | { | |
301 | struct timeval now; | |
302 | ||
303 | microtime(&now); | |
304 | ||
305 | if (touch & PIPE_ATIME) { | |
306 | tpipe->st_atimespec.tv_sec = now.tv_sec; | |
307 | tpipe->st_atimespec.tv_nsec = now.tv_usec * 1000; | |
308 | } | |
309 | ||
310 | if (touch & PIPE_MTIME) { | |
311 | tpipe->st_mtimespec.tv_sec = now.tv_sec; | |
312 | tpipe->st_mtimespec.tv_nsec = now.tv_usec * 1000; | |
313 | } | |
314 | ||
315 | if (touch & PIPE_CTIME) { | |
316 | tpipe->st_ctimespec.tv_sec = now.tv_sec; | |
317 | tpipe->st_ctimespec.tv_nsec = now.tv_usec * 1000; | |
318 | } | |
319 | } | |
320 | ||
91447636 A |
321 | |
322 | ||
323 | /* | |
324 | * The pipe system call for the DTYPE_PIPE type of pipes | |
325 | */ | |
326 | ||
327 | /* ARGSUSED */ | |
328 | int | |
b0d623f7 | 329 | pipe(proc_t p, __unused struct pipe_args *uap, int32_t *retval) |
91447636 A |
330 | { |
331 | struct fileproc *rf, *wf; | |
332 | struct pipe *rpipe, *wpipe; | |
333 | lck_mtx_t *pmtx; | |
334 | int fd, error; | |
335 | ||
336 | if ((pmtx = lck_mtx_alloc_init(pipe_mtx_grp, pipe_mtx_attr)) == NULL) | |
337 | return (ENOMEM); | |
338 | ||
339 | rpipe = wpipe = NULL; | |
340 | if (pipe_create(&rpipe) || pipe_create(&wpipe)) { | |
341 | error = ENFILE; | |
342 | goto freepipes; | |
343 | } | |
344 | /* | |
345 | * allocate the space for the normal I/O direction up | |
346 | * front... we'll delay the allocation for the other | |
347 | * direction until a write actually occurs (most | |
348 | * likely it won't)... | |
349 | * | |
350 | * Reduce to 1/4th pipe size if we're over our global max. | |
351 | */ | |
352 | if (amountpipekva > maxpipekva / 2) | |
353 | error = pipespace(rpipe, SMALL_PIPE_SIZE); | |
354 | else | |
355 | error = pipespace(rpipe, PIPE_SIZE); | |
356 | if (error) | |
357 | goto freepipes; | |
358 | ||
359 | #ifndef PIPE_NODIRECT | |
360 | rpipe->pipe_state |= PIPE_DIRECTOK; | |
361 | wpipe->pipe_state |= PIPE_DIRECTOK; | |
362 | #endif | |
363 | TAILQ_INIT(&rpipe->pipe_evlist); | |
364 | TAILQ_INIT(&wpipe->pipe_evlist); | |
365 | ||
2d21ac55 | 366 | error = falloc(p, &rf, &fd, vfs_context_current()); |
91447636 A |
367 | if (error) { |
368 | goto freepipes; | |
369 | } | |
370 | retval[0] = fd; | |
371 | ||
372 | /* | |
373 | * for now we'll create half-duplex | |
374 | * pipes... this is what we've always | |
375 | * supported.. | |
376 | */ | |
377 | rf->f_flag = FREAD; | |
378 | rf->f_type = DTYPE_PIPE; | |
379 | rf->f_data = (caddr_t)rpipe; | |
380 | rf->f_ops = &pipeops; | |
381 | ||
2d21ac55 | 382 | error = falloc(p, &wf, &fd, vfs_context_current()); |
91447636 A |
383 | if (error) { |
384 | fp_free(p, retval[0], rf); | |
385 | goto freepipes; | |
386 | } | |
387 | wf->f_flag = FWRITE; | |
388 | wf->f_type = DTYPE_PIPE; | |
389 | wf->f_data = (caddr_t)wpipe; | |
390 | wf->f_ops = &pipeops; | |
391 | ||
6601e61a A |
392 | rpipe->pipe_peer = wpipe; |
393 | wpipe->pipe_peer = rpipe; | |
6601e61a | 394 | rpipe->pipe_mtxp = wpipe->pipe_mtxp = pmtx; |
2d21ac55 | 395 | |
91447636 | 396 | retval[1] = fd; |
2d21ac55 | 397 | #if CONFIG_MACF |
91447636 A |
398 | /* |
399 | * XXXXXXXX SHOULD NOT HOLD FILE_LOCK() XXXXXXXXXXXX | |
400 | * | |
401 | * struct pipe represents a pipe endpoint. The MAC label is shared | |
2d21ac55 A |
402 | * between the connected endpoints. As a result mac_pipe_label_init() and |
403 | * mac_pipe_label_associate() should only be called on one of the endpoints | |
91447636 A |
404 | * after they have been connected. |
405 | */ | |
2d21ac55 A |
406 | mac_pipe_label_init(rpipe); |
407 | mac_pipe_label_associate(kauth_cred_get(), rpipe); | |
408 | wpipe->pipe_label = rpipe->pipe_label; | |
91447636 | 409 | #endif |
2d21ac55 | 410 | proc_fdlock_spin(p); |
6601e61a A |
411 | procfdtbl_releasefd(p, retval[0], NULL); |
412 | procfdtbl_releasefd(p, retval[1], NULL); | |
91447636 A |
413 | fp_drop(p, retval[0], rf, 1); |
414 | fp_drop(p, retval[1], wf, 1); | |
415 | proc_fdunlock(p); | |
416 | ||
91447636 A |
417 | |
418 | return (0); | |
419 | ||
420 | freepipes: | |
421 | pipeclose(rpipe); | |
422 | pipeclose(wpipe); | |
423 | lck_mtx_free(pmtx, pipe_mtx_grp); | |
424 | ||
425 | return (error); | |
426 | } | |
427 | ||
91447636 | 428 | int |
2d21ac55 | 429 | pipe_stat(struct pipe *cpipe, void *ub, int isstat64) |
91447636 | 430 | { |
2d21ac55 | 431 | #if CONFIG_MACF |
91447636 A |
432 | int error; |
433 | #endif | |
2d21ac55 A |
434 | int pipe_size = 0; |
435 | int pipe_count; | |
436 | struct stat *sb = (struct stat *)0; /* warning avoidance ; protected by isstat64 */ | |
437 | struct stat64 * sb64 = (struct stat64 *)0; /* warning avoidance ; protected by isstat64 */ | |
91447636 A |
438 | |
439 | if (cpipe == NULL) | |
440 | return (EBADF); | |
91447636 | 441 | PIPE_LOCK(cpipe); |
2d21ac55 A |
442 | |
443 | #if CONFIG_MACF | |
444 | error = mac_pipe_check_stat(kauth_cred_get(), cpipe); | |
445 | if (error) { | |
446 | PIPE_UNLOCK(cpipe); | |
91447636 | 447 | return (error); |
2d21ac55 | 448 | } |
91447636 A |
449 | #endif |
450 | if (cpipe->pipe_buffer.buffer == 0) { | |
451 | /* | |
452 | * must be stat'ing the write fd | |
453 | */ | |
2d21ac55 A |
454 | if (cpipe->pipe_peer) { |
455 | /* | |
456 | * the peer still exists, use it's info | |
457 | */ | |
458 | pipe_size = cpipe->pipe_peer->pipe_buffer.size; | |
459 | pipe_count = cpipe->pipe_peer->pipe_buffer.cnt; | |
460 | } else { | |
461 | pipe_count = 0; | |
462 | } | |
463 | } else { | |
464 | pipe_size = cpipe->pipe_buffer.size; | |
465 | pipe_count = cpipe->pipe_buffer.cnt; | |
91447636 | 466 | } |
2d21ac55 A |
467 | /* |
468 | * since peer's buffer is setup ouside of lock | |
469 | * we might catch it in transient state | |
470 | */ | |
471 | if (pipe_size == 0) | |
472 | pipe_size = PIPE_SIZE; | |
91447636 | 473 | |
2d21ac55 A |
474 | if (isstat64 != 0) { |
475 | sb64 = (struct stat64 *)ub; | |
91447636 | 476 | |
2d21ac55 A |
477 | bzero(sb64, sizeof(*sb64)); |
478 | sb64->st_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
479 | sb64->st_blksize = pipe_size; | |
480 | sb64->st_size = pipe_count; | |
481 | sb64->st_blocks = (sb64->st_size + sb64->st_blksize - 1) / sb64->st_blksize; | |
482 | ||
483 | sb64->st_uid = kauth_getuid(); | |
484 | sb64->st_gid = kauth_getgid(); | |
485 | ||
486 | sb64->st_atimespec.tv_sec = cpipe->st_atimespec.tv_sec; | |
487 | sb64->st_atimespec.tv_nsec = cpipe->st_atimespec.tv_nsec; | |
488 | ||
489 | sb64->st_mtimespec.tv_sec = cpipe->st_mtimespec.tv_sec; | |
490 | sb64->st_mtimespec.tv_nsec = cpipe->st_mtimespec.tv_nsec; | |
91447636 | 491 | |
2d21ac55 A |
492 | sb64->st_ctimespec.tv_sec = cpipe->st_ctimespec.tv_sec; |
493 | sb64->st_ctimespec.tv_nsec = cpipe->st_ctimespec.tv_nsec; | |
91447636 | 494 | |
2d21ac55 A |
495 | /* |
496 | * Return a relatively unique inode number based on the current | |
497 | * address of this pipe's struct pipe. This number may be recycled | |
498 | * relatively quickly. | |
499 | */ | |
b0d623f7 | 500 | sb64->st_ino = (ino64_t)((uintptr_t)cpipe); |
2d21ac55 A |
501 | } else { |
502 | sb = (struct stat *)ub; | |
503 | ||
504 | bzero(sb, sizeof(*sb)); | |
505 | sb->st_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
506 | sb->st_blksize = pipe_size; | |
507 | sb->st_size = pipe_count; | |
508 | sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize; | |
509 | ||
510 | sb->st_uid = kauth_getuid(); | |
511 | sb->st_gid = kauth_getgid(); | |
512 | ||
513 | sb->st_atimespec.tv_sec = cpipe->st_atimespec.tv_sec; | |
514 | sb->st_atimespec.tv_nsec = cpipe->st_atimespec.tv_nsec; | |
515 | ||
516 | sb->st_mtimespec.tv_sec = cpipe->st_mtimespec.tv_sec; | |
517 | sb->st_mtimespec.tv_nsec = cpipe->st_mtimespec.tv_nsec; | |
518 | ||
519 | sb->st_ctimespec.tv_sec = cpipe->st_ctimespec.tv_sec; | |
520 | sb->st_ctimespec.tv_nsec = cpipe->st_ctimespec.tv_nsec; | |
521 | ||
522 | /* | |
523 | * Return a relatively unique inode number based on the current | |
524 | * address of this pipe's struct pipe. This number may be recycled | |
525 | * relatively quickly. | |
526 | */ | |
b0d623f7 | 527 | sb->st_ino = (ino_t)(uintptr_t)cpipe; |
2d21ac55 A |
528 | } |
529 | PIPE_UNLOCK(cpipe); | |
91447636 A |
530 | |
531 | /* | |
2d21ac55 A |
532 | * POSIX: Left as 0: st_dev, st_nlink, st_rdev, st_flags, st_gen, |
533 | * st_uid, st_gid. | |
534 | * | |
535 | * XXX (st_dev) should be unique, but there is no device driver that | |
536 | * XXX is associated with pipes, since they are implemented via a | |
537 | * XXX struct fileops indirection rather than as FS objects. | |
91447636 A |
538 | */ |
539 | return (0); | |
540 | } | |
541 | ||
542 | ||
543 | /* | |
544 | * Allocate kva for pipe circular buffer, the space is pageable | |
545 | * This routine will 'realloc' the size of a pipe safely, if it fails | |
546 | * it will retain the old buffer. | |
547 | * If it fails it will return ENOMEM. | |
548 | */ | |
549 | static int | |
550 | pipespace(struct pipe *cpipe, int size) | |
551 | { | |
552 | vm_offset_t buffer; | |
553 | ||
554 | size = round_page(size); | |
555 | ||
556 | if (kmem_alloc(kernel_map, &buffer, size) != KERN_SUCCESS) | |
557 | return(ENOMEM); | |
558 | ||
559 | /* free old resources if we're resizing */ | |
560 | pipe_free_kmem(cpipe); | |
561 | cpipe->pipe_buffer.buffer = (caddr_t)buffer; | |
562 | cpipe->pipe_buffer.size = size; | |
563 | cpipe->pipe_buffer.in = 0; | |
564 | cpipe->pipe_buffer.out = 0; | |
565 | cpipe->pipe_buffer.cnt = 0; | |
566 | ||
b0d623f7 A |
567 | OSAddAtomic(1, &amountpipes); |
568 | OSAddAtomic(cpipe->pipe_buffer.size, &amountpipekva); | |
91447636 A |
569 | |
570 | return (0); | |
571 | } | |
572 | ||
573 | /* | |
574 | * initialize and allocate VM and memory for pipe | |
575 | */ | |
576 | static int | |
577 | pipe_create(struct pipe **cpipep) | |
578 | { | |
579 | struct pipe *cpipe; | |
580 | ||
581 | cpipe = (struct pipe *)zalloc(pipe_zone); | |
582 | ||
583 | if ((*cpipep = cpipe) == NULL) | |
584 | return (ENOMEM); | |
585 | ||
586 | /* | |
587 | * protect so pipespace or pipeclose don't follow a junk pointer | |
588 | * if pipespace() fails. | |
589 | */ | |
590 | bzero(cpipe, sizeof *cpipe); | |
591 | ||
2d21ac55 A |
592 | /* Initial times are all the time of creation of the pipe */ |
593 | pipe_touch(cpipe, PIPE_ATIME | PIPE_MTIME | PIPE_CTIME); | |
594 | ||
91447636 A |
595 | return (0); |
596 | } | |
597 | ||
598 | ||
599 | /* | |
600 | * lock a pipe for I/O, blocking other access | |
601 | */ | |
2d21ac55 A |
602 | static inline int |
603 | pipelock(struct pipe *cpipe, int catch) | |
91447636 A |
604 | { |
605 | int error; | |
606 | ||
607 | while (cpipe->pipe_state & PIPE_LOCKFL) { | |
608 | cpipe->pipe_state |= PIPE_LWANT; | |
609 | ||
610 | error = msleep(cpipe, PIPE_MTX(cpipe), catch ? (PRIBIO | PCATCH) : PRIBIO, | |
611 | "pipelk", 0); | |
612 | if (error != 0) | |
613 | return (error); | |
614 | } | |
615 | cpipe->pipe_state |= PIPE_LOCKFL; | |
616 | ||
617 | return (0); | |
618 | } | |
619 | ||
620 | /* | |
621 | * unlock a pipe I/O lock | |
622 | */ | |
2d21ac55 A |
623 | static inline void |
624 | pipeunlock(struct pipe *cpipe) | |
91447636 | 625 | { |
91447636 A |
626 | cpipe->pipe_state &= ~PIPE_LOCKFL; |
627 | ||
628 | if (cpipe->pipe_state & PIPE_LWANT) { | |
629 | cpipe->pipe_state &= ~PIPE_LWANT; | |
630 | wakeup(cpipe); | |
631 | } | |
632 | } | |
633 | ||
634 | static void | |
2d21ac55 | 635 | pipeselwakeup(struct pipe *cpipe, struct pipe *spipe) |
91447636 | 636 | { |
91447636 A |
637 | if (cpipe->pipe_state & PIPE_SEL) { |
638 | cpipe->pipe_state &= ~PIPE_SEL; | |
639 | selwakeup(&cpipe->pipe_sel); | |
640 | } | |
641 | if (cpipe->pipe_state & PIPE_KNOTE) | |
642 | KNOTE(&cpipe->pipe_sel.si_note, 1); | |
643 | ||
644 | postpipeevent(cpipe, EV_RWBYTES); | |
645 | ||
646 | if (spipe && (spipe->pipe_state & PIPE_ASYNC) && spipe->pipe_pgid) { | |
91447636 A |
647 | if (spipe->pipe_pgid < 0) |
648 | gsignal(-spipe->pipe_pgid, SIGIO); | |
2d21ac55 A |
649 | else |
650 | proc_signal(spipe->pipe_pgid, SIGIO); | |
91447636 A |
651 | } |
652 | } | |
653 | ||
654 | /* ARGSUSED */ | |
655 | static int | |
2d21ac55 A |
656 | pipe_read(struct fileproc *fp, struct uio *uio, __unused int flags, |
657 | __unused vfs_context_t ctx) | |
91447636 A |
658 | { |
659 | struct pipe *rpipe = (struct pipe *)fp->f_data; | |
660 | int error; | |
661 | int nread = 0; | |
662 | u_int size; | |
663 | ||
664 | PIPE_LOCK(rpipe); | |
665 | ++rpipe->pipe_busy; | |
666 | ||
667 | error = pipelock(rpipe, 1); | |
668 | if (error) | |
669 | goto unlocked_error; | |
670 | ||
2d21ac55 A |
671 | #if CONFIG_MACF |
672 | error = mac_pipe_check_read(kauth_cred_get(), rpipe); | |
91447636 A |
673 | if (error) |
674 | goto locked_error; | |
675 | #endif | |
676 | ||
677 | while (uio_resid(uio)) { | |
678 | /* | |
679 | * normal pipe buffer receive | |
680 | */ | |
681 | if (rpipe->pipe_buffer.cnt > 0) { | |
682 | size = rpipe->pipe_buffer.size - rpipe->pipe_buffer.out; | |
683 | if (size > rpipe->pipe_buffer.cnt) | |
684 | size = rpipe->pipe_buffer.cnt; | |
685 | // LP64todo - fix this! | |
686 | if (size > (u_int) uio_resid(uio)) | |
687 | size = (u_int) uio_resid(uio); | |
688 | ||
689 | PIPE_UNLOCK(rpipe); | |
690 | error = uiomove( | |
691 | &rpipe->pipe_buffer.buffer[rpipe->pipe_buffer.out], | |
692 | size, uio); | |
693 | PIPE_LOCK(rpipe); | |
694 | if (error) | |
695 | break; | |
696 | ||
697 | rpipe->pipe_buffer.out += size; | |
698 | if (rpipe->pipe_buffer.out >= rpipe->pipe_buffer.size) | |
699 | rpipe->pipe_buffer.out = 0; | |
700 | ||
701 | rpipe->pipe_buffer.cnt -= size; | |
702 | ||
703 | /* | |
704 | * If there is no more to read in the pipe, reset | |
705 | * its pointers to the beginning. This improves | |
706 | * cache hit stats. | |
707 | */ | |
708 | if (rpipe->pipe_buffer.cnt == 0) { | |
709 | rpipe->pipe_buffer.in = 0; | |
710 | rpipe->pipe_buffer.out = 0; | |
711 | } | |
712 | nread += size; | |
713 | #ifndef PIPE_NODIRECT | |
714 | /* | |
715 | * Direct copy, bypassing a kernel buffer. | |
716 | */ | |
717 | } else if ((size = rpipe->pipe_map.cnt) && | |
718 | (rpipe->pipe_state & PIPE_DIRECTW)) { | |
719 | caddr_t va; | |
720 | // LP64todo - fix this! | |
721 | if (size > (u_int) uio_resid(uio)) | |
722 | size = (u_int) uio_resid(uio); | |
723 | ||
724 | va = (caddr_t) rpipe->pipe_map.kva + | |
725 | rpipe->pipe_map.pos; | |
726 | PIPE_UNLOCK(rpipe); | |
727 | error = uiomove(va, size, uio); | |
728 | PIPE_LOCK(rpipe); | |
729 | if (error) | |
730 | break; | |
731 | nread += size; | |
732 | rpipe->pipe_map.pos += size; | |
733 | rpipe->pipe_map.cnt -= size; | |
734 | if (rpipe->pipe_map.cnt == 0) { | |
735 | rpipe->pipe_state &= ~PIPE_DIRECTW; | |
736 | wakeup(rpipe); | |
737 | } | |
738 | #endif | |
739 | } else { | |
740 | /* | |
741 | * detect EOF condition | |
742 | * read returns 0 on EOF, no need to set error | |
743 | */ | |
b0d623f7 | 744 | if (rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 | 745 | break; |
b0d623f7 | 746 | } |
91447636 A |
747 | |
748 | /* | |
749 | * If the "write-side" has been blocked, wake it up now. | |
750 | */ | |
751 | if (rpipe->pipe_state & PIPE_WANTW) { | |
752 | rpipe->pipe_state &= ~PIPE_WANTW; | |
753 | wakeup(rpipe); | |
754 | } | |
755 | ||
756 | /* | |
757 | * Break if some data was read. | |
758 | */ | |
759 | if (nread > 0) | |
760 | break; | |
761 | ||
762 | /* | |
763 | * Unlock the pipe buffer for our remaining processing. | |
764 | * We will either break out with an error or we will | |
765 | * sleep and relock to loop. | |
766 | */ | |
767 | pipeunlock(rpipe); | |
768 | ||
769 | /* | |
770 | * Handle non-blocking mode operation or | |
771 | * wait for more data. | |
772 | */ | |
773 | if (fp->f_flag & FNONBLOCK) { | |
774 | error = EAGAIN; | |
775 | } else { | |
776 | rpipe->pipe_state |= PIPE_WANTR; | |
777 | ||
778 | error = msleep(rpipe, PIPE_MTX(rpipe), PRIBIO | PCATCH, "piperd", 0); | |
779 | ||
780 | if (error == 0) | |
781 | error = pipelock(rpipe, 1); | |
782 | } | |
783 | if (error) | |
784 | goto unlocked_error; | |
785 | } | |
786 | } | |
2d21ac55 | 787 | #if CONFIG_MACF |
91447636 A |
788 | locked_error: |
789 | #endif | |
790 | pipeunlock(rpipe); | |
791 | ||
792 | unlocked_error: | |
793 | --rpipe->pipe_busy; | |
794 | ||
795 | /* | |
796 | * PIPE_WANT processing only makes sense if pipe_busy is 0. | |
797 | */ | |
798 | if ((rpipe->pipe_busy == 0) && (rpipe->pipe_state & PIPE_WANT)) { | |
799 | rpipe->pipe_state &= ~(PIPE_WANT|PIPE_WANTW); | |
800 | wakeup(rpipe); | |
801 | } else if (rpipe->pipe_buffer.cnt < MINPIPESIZE) { | |
802 | /* | |
803 | * Handle write blocking hysteresis. | |
804 | */ | |
805 | if (rpipe->pipe_state & PIPE_WANTW) { | |
806 | rpipe->pipe_state &= ~PIPE_WANTW; | |
807 | wakeup(rpipe); | |
808 | } | |
809 | } | |
810 | ||
811 | if ((rpipe->pipe_buffer.size - rpipe->pipe_buffer.cnt) >= PIPE_BUF) | |
812 | pipeselwakeup(rpipe, rpipe->pipe_peer); | |
813 | ||
2d21ac55 A |
814 | /* update last read time */ |
815 | pipe_touch(rpipe, PIPE_ATIME); | |
816 | ||
91447636 A |
817 | PIPE_UNLOCK(rpipe); |
818 | ||
819 | return (error); | |
820 | } | |
821 | ||
822 | ||
823 | ||
824 | #ifndef PIPE_NODIRECT | |
825 | /* | |
826 | * Map the sending processes' buffer into kernel space and wire it. | |
827 | * This is similar to a physical write operation. | |
828 | */ | |
829 | static int | |
830 | pipe_build_write_buffer(wpipe, uio) | |
831 | struct pipe *wpipe; | |
832 | struct uio *uio; | |
833 | { | |
834 | pmap_t pmap; | |
835 | u_int size; | |
836 | int i, j; | |
837 | vm_offset_t addr, endaddr; | |
838 | ||
839 | ||
840 | size = (u_int) uio->uio_iov->iov_len; | |
841 | if (size > wpipe->pipe_buffer.size) | |
842 | size = wpipe->pipe_buffer.size; | |
843 | ||
844 | pmap = vmspace_pmap(curproc->p_vmspace); | |
845 | endaddr = round_page((vm_offset_t)uio->uio_iov->iov_base + size); | |
846 | addr = trunc_page((vm_offset_t)uio->uio_iov->iov_base); | |
847 | for (i = 0; addr < endaddr; addr += PAGE_SIZE, i++) { | |
848 | /* | |
849 | * vm_fault_quick() can sleep. Consequently, | |
850 | * vm_page_lock_queue() and vm_page_unlock_queue() | |
851 | * should not be performed outside of this loop. | |
852 | */ | |
853 | race: | |
854 | if (vm_fault_quick((caddr_t)addr, VM_PROT_READ) < 0) { | |
855 | vm_page_lock_queues(); | |
856 | for (j = 0; j < i; j++) | |
857 | vm_page_unhold(wpipe->pipe_map.ms[j]); | |
858 | vm_page_unlock_queues(); | |
859 | return (EFAULT); | |
860 | } | |
861 | wpipe->pipe_map.ms[i] = pmap_extract_and_hold(pmap, addr, | |
862 | VM_PROT_READ); | |
863 | if (wpipe->pipe_map.ms[i] == NULL) | |
864 | goto race; | |
865 | } | |
866 | ||
867 | /* | |
868 | * set up the control block | |
869 | */ | |
870 | wpipe->pipe_map.npages = i; | |
871 | wpipe->pipe_map.pos = | |
872 | ((vm_offset_t) uio->uio_iov->iov_base) & PAGE_MASK; | |
873 | wpipe->pipe_map.cnt = size; | |
874 | ||
875 | /* | |
876 | * and map the buffer | |
877 | */ | |
878 | if (wpipe->pipe_map.kva == 0) { | |
879 | /* | |
880 | * We need to allocate space for an extra page because the | |
881 | * address range might (will) span pages at times. | |
882 | */ | |
883 | wpipe->pipe_map.kva = kmem_alloc_nofault(kernel_map, | |
884 | wpipe->pipe_buffer.size + PAGE_SIZE); | |
885 | atomic_add_int(&amountpipekvawired, | |
886 | wpipe->pipe_buffer.size + PAGE_SIZE); | |
887 | } | |
888 | pmap_qenter(wpipe->pipe_map.kva, wpipe->pipe_map.ms, | |
889 | wpipe->pipe_map.npages); | |
890 | ||
891 | /* | |
892 | * and update the uio data | |
893 | */ | |
894 | ||
895 | uio->uio_iov->iov_len -= size; | |
896 | uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + size; | |
897 | if (uio->uio_iov->iov_len == 0) | |
898 | uio->uio_iov++; | |
899 | uio_setresid(uio, (uio_resid(uio) - size)); | |
900 | uio->uio_offset += size; | |
901 | return (0); | |
902 | } | |
903 | ||
904 | /* | |
905 | * unmap and unwire the process buffer | |
906 | */ | |
907 | static void | |
908 | pipe_destroy_write_buffer(wpipe) | |
909 | struct pipe *wpipe; | |
910 | { | |
911 | int i; | |
912 | ||
913 | if (wpipe->pipe_map.kva) { | |
914 | pmap_qremove(wpipe->pipe_map.kva, wpipe->pipe_map.npages); | |
915 | ||
916 | if (amountpipekvawired > maxpipekvawired / 2) { | |
917 | /* Conserve address space */ | |
918 | vm_offset_t kva = wpipe->pipe_map.kva; | |
919 | wpipe->pipe_map.kva = 0; | |
920 | kmem_free(kernel_map, kva, | |
921 | wpipe->pipe_buffer.size + PAGE_SIZE); | |
922 | atomic_subtract_int(&amountpipekvawired, | |
923 | wpipe->pipe_buffer.size + PAGE_SIZE); | |
924 | } | |
925 | } | |
926 | vm_page_lock_queues(); | |
927 | for (i = 0; i < wpipe->pipe_map.npages; i++) { | |
928 | vm_page_unhold(wpipe->pipe_map.ms[i]); | |
929 | } | |
930 | vm_page_unlock_queues(); | |
931 | wpipe->pipe_map.npages = 0; | |
932 | } | |
933 | ||
934 | /* | |
935 | * In the case of a signal, the writing process might go away. This | |
936 | * code copies the data into the circular buffer so that the source | |
937 | * pages can be freed without loss of data. | |
938 | */ | |
939 | static void | |
940 | pipe_clone_write_buffer(wpipe) | |
941 | struct pipe *wpipe; | |
942 | { | |
943 | int size; | |
944 | int pos; | |
945 | ||
946 | size = wpipe->pipe_map.cnt; | |
947 | pos = wpipe->pipe_map.pos; | |
948 | ||
949 | wpipe->pipe_buffer.in = size; | |
950 | wpipe->pipe_buffer.out = 0; | |
951 | wpipe->pipe_buffer.cnt = size; | |
952 | wpipe->pipe_state &= ~PIPE_DIRECTW; | |
953 | ||
954 | PIPE_UNLOCK(wpipe); | |
955 | bcopy((caddr_t) wpipe->pipe_map.kva + pos, | |
956 | wpipe->pipe_buffer.buffer, size); | |
957 | pipe_destroy_write_buffer(wpipe); | |
958 | PIPE_LOCK(wpipe); | |
959 | } | |
960 | ||
961 | /* | |
962 | * This implements the pipe buffer write mechanism. Note that only | |
963 | * a direct write OR a normal pipe write can be pending at any given time. | |
964 | * If there are any characters in the pipe buffer, the direct write will | |
965 | * be deferred until the receiving process grabs all of the bytes from | |
966 | * the pipe buffer. Then the direct mapping write is set-up. | |
967 | */ | |
968 | static int | |
969 | pipe_direct_write(wpipe, uio) | |
970 | struct pipe *wpipe; | |
971 | struct uio *uio; | |
972 | { | |
973 | int error; | |
974 | ||
975 | retry: | |
976 | while (wpipe->pipe_state & PIPE_DIRECTW) { | |
977 | if (wpipe->pipe_state & PIPE_WANTR) { | |
978 | wpipe->pipe_state &= ~PIPE_WANTR; | |
979 | wakeup(wpipe); | |
980 | } | |
981 | wpipe->pipe_state |= PIPE_WANTW; | |
982 | error = msleep(wpipe, PIPE_MTX(wpipe), | |
983 | PRIBIO | PCATCH, "pipdww", 0); | |
984 | if (error) | |
985 | goto error1; | |
b0d623f7 | 986 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 A |
987 | error = EPIPE; |
988 | goto error1; | |
989 | } | |
990 | } | |
991 | wpipe->pipe_map.cnt = 0; /* transfer not ready yet */ | |
992 | if (wpipe->pipe_buffer.cnt > 0) { | |
993 | if (wpipe->pipe_state & PIPE_WANTR) { | |
994 | wpipe->pipe_state &= ~PIPE_WANTR; | |
995 | wakeup(wpipe); | |
996 | } | |
997 | ||
998 | wpipe->pipe_state |= PIPE_WANTW; | |
999 | error = msleep(wpipe, PIPE_MTX(wpipe), | |
1000 | PRIBIO | PCATCH, "pipdwc", 0); | |
1001 | if (error) | |
1002 | goto error1; | |
b0d623f7 | 1003 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 A |
1004 | error = EPIPE; |
1005 | goto error1; | |
1006 | } | |
1007 | goto retry; | |
1008 | } | |
1009 | ||
1010 | wpipe->pipe_state |= PIPE_DIRECTW; | |
1011 | ||
1012 | pipelock(wpipe, 0); | |
1013 | PIPE_UNLOCK(wpipe); | |
1014 | error = pipe_build_write_buffer(wpipe, uio); | |
1015 | PIPE_LOCK(wpipe); | |
1016 | pipeunlock(wpipe); | |
1017 | if (error) { | |
1018 | wpipe->pipe_state &= ~PIPE_DIRECTW; | |
1019 | goto error1; | |
1020 | } | |
1021 | ||
1022 | error = 0; | |
1023 | while (!error && (wpipe->pipe_state & PIPE_DIRECTW)) { | |
b0d623f7 | 1024 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 A |
1025 | pipelock(wpipe, 0); |
1026 | PIPE_UNLOCK(wpipe); | |
1027 | pipe_destroy_write_buffer(wpipe); | |
1028 | PIPE_LOCK(wpipe); | |
1029 | pipeselwakeup(wpipe, wpipe); | |
1030 | pipeunlock(wpipe); | |
1031 | error = EPIPE; | |
1032 | goto error1; | |
1033 | } | |
1034 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1035 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1036 | wakeup(wpipe); | |
1037 | } | |
1038 | pipeselwakeup(wpipe, wpipe); | |
1039 | error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, | |
1040 | "pipdwt", 0); | |
1041 | } | |
1042 | ||
1043 | pipelock(wpipe,0); | |
1044 | if (wpipe->pipe_state & PIPE_DIRECTW) { | |
1045 | /* | |
1046 | * this bit of trickery substitutes a kernel buffer for | |
1047 | * the process that might be going away. | |
1048 | */ | |
1049 | pipe_clone_write_buffer(wpipe); | |
1050 | } else { | |
1051 | PIPE_UNLOCK(wpipe); | |
1052 | pipe_destroy_write_buffer(wpipe); | |
1053 | PIPE_LOCK(wpipe); | |
1054 | } | |
1055 | pipeunlock(wpipe); | |
1056 | return (error); | |
1057 | ||
1058 | error1: | |
1059 | wakeup(wpipe); | |
1060 | return (error); | |
1061 | } | |
1062 | #endif | |
1063 | ||
1064 | ||
1065 | ||
1066 | static int | |
2d21ac55 A |
1067 | pipe_write(struct fileproc *fp, struct uio *uio, __unused int flags, |
1068 | __unused vfs_context_t ctx) | |
91447636 A |
1069 | { |
1070 | int error = 0; | |
1071 | int orig_resid; | |
1072 | int pipe_size; | |
1073 | struct pipe *wpipe, *rpipe; | |
1074 | ||
1075 | rpipe = (struct pipe *)fp->f_data; | |
1076 | ||
1077 | PIPE_LOCK(rpipe); | |
1078 | wpipe = rpipe->pipe_peer; | |
1079 | ||
1080 | /* | |
1081 | * detect loss of pipe read side, issue SIGPIPE if lost. | |
1082 | */ | |
b0d623f7 | 1083 | if (wpipe == NULL || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { |
91447636 A |
1084 | PIPE_UNLOCK(rpipe); |
1085 | return (EPIPE); | |
1086 | } | |
2d21ac55 A |
1087 | #if CONFIG_MACF |
1088 | error = mac_pipe_check_write(kauth_cred_get(), wpipe); | |
91447636 A |
1089 | if (error) { |
1090 | PIPE_UNLOCK(rpipe); | |
1091 | return (error); | |
1092 | } | |
1093 | #endif | |
1094 | ++wpipe->pipe_busy; | |
1095 | ||
1096 | pipe_size = 0; | |
1097 | ||
1098 | if (wpipe->pipe_buffer.buffer == 0) { | |
1099 | /* | |
1100 | * need to allocate some storage... we delay the allocation | |
1101 | * until the first write on fd[0] to avoid allocating storage for both | |
1102 | * 'pipe ends'... most pipes are half-duplex with the writes targeting | |
1103 | * fd[1], so allocating space for both ends is a waste... | |
1104 | * | |
1105 | * Reduce to 1/4th pipe size if we're over our global max. | |
1106 | */ | |
1107 | if (amountpipekva > maxpipekva / 2) | |
1108 | pipe_size = SMALL_PIPE_SIZE; | |
1109 | else | |
1110 | pipe_size = PIPE_SIZE; | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * If it is advantageous to resize the pipe buffer, do | |
1115 | * so. | |
1116 | */ | |
1117 | if ((uio_resid(uio) > PIPE_SIZE) && | |
1118 | (wpipe->pipe_buffer.size <= PIPE_SIZE) && | |
1119 | (amountpipekva < maxpipekva / 2) && | |
1120 | (nbigpipe < LIMITBIGPIPES) && | |
1121 | #ifndef PIPE_NODIRECT | |
1122 | (wpipe->pipe_state & PIPE_DIRECTW) == 0 && | |
1123 | #endif | |
1124 | (wpipe->pipe_buffer.cnt == 0)) { | |
1125 | ||
1126 | pipe_size = BIG_PIPE_SIZE; | |
1127 | ||
1128 | } | |
1129 | if (pipe_size) { | |
1130 | /* | |
1131 | * need to do initial allocation or resizing of pipe | |
1132 | */ | |
1133 | if ((error = pipelock(wpipe, 1)) == 0) { | |
1134 | PIPE_UNLOCK(wpipe); | |
1135 | if (pipespace(wpipe, pipe_size) == 0) | |
b0d623f7 | 1136 | OSAddAtomic(1, &nbigpipe); |
91447636 A |
1137 | PIPE_LOCK(wpipe); |
1138 | pipeunlock(wpipe); | |
1139 | ||
1140 | if (wpipe->pipe_buffer.buffer == 0) { | |
1141 | /* | |
1142 | * initial allocation failed | |
1143 | */ | |
1144 | error = ENOMEM; | |
1145 | } | |
1146 | } | |
1147 | if (error) { | |
1148 | /* | |
1149 | * If an error occurred unbusy and return, waking up any pending | |
1150 | * readers. | |
1151 | */ | |
1152 | --wpipe->pipe_busy; | |
1153 | if ((wpipe->pipe_busy == 0) && | |
1154 | (wpipe->pipe_state & PIPE_WANT)) { | |
1155 | wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); | |
1156 | wakeup(wpipe); | |
1157 | } | |
1158 | PIPE_UNLOCK(rpipe); | |
1159 | return(error); | |
1160 | } | |
1161 | } | |
1162 | // LP64todo - fix this! | |
1163 | orig_resid = uio_resid(uio); | |
1164 | ||
1165 | while (uio_resid(uio)) { | |
1166 | int space; | |
1167 | ||
1168 | #ifndef PIPE_NODIRECT | |
1169 | /* | |
1170 | * If the transfer is large, we can gain performance if | |
1171 | * we do process-to-process copies directly. | |
1172 | * If the write is non-blocking, we don't use the | |
1173 | * direct write mechanism. | |
1174 | * | |
1175 | * The direct write mechanism will detect the reader going | |
1176 | * away on us. | |
1177 | */ | |
1178 | if ((uio->uio_iov->iov_len >= PIPE_MINDIRECT) && | |
1179 | (fp->f_flag & FNONBLOCK) == 0 && | |
b0d623f7 | 1180 | amountpipekvawired + uio_resid(uio) < maxpipekvawired) { |
91447636 A |
1181 | error = pipe_direct_write(wpipe, uio); |
1182 | if (error) | |
1183 | break; | |
1184 | continue; | |
1185 | } | |
1186 | ||
1187 | /* | |
1188 | * Pipe buffered writes cannot be coincidental with | |
1189 | * direct writes. We wait until the currently executing | |
1190 | * direct write is completed before we start filling the | |
1191 | * pipe buffer. We break out if a signal occurs or the | |
1192 | * reader goes away. | |
1193 | */ | |
1194 | retrywrite: | |
1195 | while (wpipe->pipe_state & PIPE_DIRECTW) { | |
1196 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1197 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1198 | wakeup(wpipe); | |
1199 | } | |
1200 | error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, "pipbww", 0); | |
1201 | ||
b0d623f7 | 1202 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) |
91447636 A |
1203 | break; |
1204 | if (error) | |
1205 | break; | |
1206 | } | |
1207 | #else | |
1208 | retrywrite: | |
1209 | #endif | |
1210 | space = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; | |
1211 | ||
1212 | /* | |
1213 | * Writes of size <= PIPE_BUF must be atomic. | |
1214 | */ | |
1215 | if ((space < uio_resid(uio)) && (orig_resid <= PIPE_BUF)) | |
1216 | space = 0; | |
1217 | ||
1218 | if (space > 0) { | |
1219 | ||
1220 | if ((error = pipelock(wpipe,1)) == 0) { | |
1221 | int size; /* Transfer size */ | |
1222 | int segsize; /* first segment to transfer */ | |
1223 | ||
b0d623f7 | 1224 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { |
91447636 A |
1225 | pipeunlock(wpipe); |
1226 | error = EPIPE; | |
1227 | break; | |
1228 | } | |
1229 | #ifndef PIPE_NODIRECT | |
1230 | /* | |
1231 | * It is possible for a direct write to | |
1232 | * slip in on us... handle it here... | |
1233 | */ | |
1234 | if (wpipe->pipe_state & PIPE_DIRECTW) { | |
1235 | pipeunlock(wpipe); | |
1236 | goto retrywrite; | |
1237 | } | |
1238 | #endif | |
1239 | /* | |
1240 | * If a process blocked in pipelock, our | |
1241 | * value for space might be bad... the mutex | |
1242 | * is dropped while we're blocked | |
1243 | */ | |
1244 | if (space > (int)(wpipe->pipe_buffer.size - | |
1245 | wpipe->pipe_buffer.cnt)) { | |
1246 | pipeunlock(wpipe); | |
1247 | goto retrywrite; | |
1248 | } | |
1249 | ||
1250 | /* | |
1251 | * Transfer size is minimum of uio transfer | |
1252 | * and free space in pipe buffer. | |
1253 | */ | |
1254 | // LP64todo - fix this! | |
1255 | if (space > uio_resid(uio)) | |
1256 | size = uio_resid(uio); | |
1257 | else | |
1258 | size = space; | |
1259 | /* | |
1260 | * First segment to transfer is minimum of | |
1261 | * transfer size and contiguous space in | |
1262 | * pipe buffer. If first segment to transfer | |
1263 | * is less than the transfer size, we've got | |
1264 | * a wraparound in the buffer. | |
1265 | */ | |
1266 | segsize = wpipe->pipe_buffer.size - | |
1267 | wpipe->pipe_buffer.in; | |
1268 | if (segsize > size) | |
1269 | segsize = size; | |
1270 | ||
1271 | /* Transfer first segment */ | |
1272 | ||
1273 | PIPE_UNLOCK(rpipe); | |
1274 | error = uiomove(&wpipe->pipe_buffer.buffer[wpipe->pipe_buffer.in], | |
1275 | segsize, uio); | |
1276 | PIPE_LOCK(rpipe); | |
1277 | ||
1278 | if (error == 0 && segsize < size) { | |
1279 | /* | |
1280 | * Transfer remaining part now, to | |
1281 | * support atomic writes. Wraparound | |
1282 | * happened. | |
1283 | */ | |
1284 | if (wpipe->pipe_buffer.in + segsize != | |
1285 | wpipe->pipe_buffer.size) | |
1286 | panic("Expected pipe buffer " | |
1287 | "wraparound disappeared"); | |
1288 | ||
1289 | PIPE_UNLOCK(rpipe); | |
1290 | error = uiomove( | |
1291 | &wpipe->pipe_buffer.buffer[0], | |
1292 | size - segsize, uio); | |
1293 | PIPE_LOCK(rpipe); | |
1294 | } | |
1295 | if (error == 0) { | |
1296 | wpipe->pipe_buffer.in += size; | |
1297 | if (wpipe->pipe_buffer.in >= | |
1298 | wpipe->pipe_buffer.size) { | |
1299 | if (wpipe->pipe_buffer.in != | |
1300 | size - segsize + | |
1301 | wpipe->pipe_buffer.size) | |
1302 | panic("Expected " | |
1303 | "wraparound bad"); | |
1304 | wpipe->pipe_buffer.in = size - | |
1305 | segsize; | |
1306 | } | |
1307 | ||
1308 | wpipe->pipe_buffer.cnt += size; | |
1309 | if (wpipe->pipe_buffer.cnt > | |
1310 | wpipe->pipe_buffer.size) | |
1311 | panic("Pipe buffer overflow"); | |
1312 | ||
1313 | } | |
1314 | pipeunlock(wpipe); | |
1315 | } | |
1316 | if (error) | |
1317 | break; | |
1318 | ||
1319 | } else { | |
1320 | /* | |
1321 | * If the "read-side" has been blocked, wake it up now. | |
1322 | */ | |
1323 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1324 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1325 | wakeup(wpipe); | |
1326 | } | |
1327 | /* | |
1328 | * don't block on non-blocking I/O | |
1329 | * we'll do the pipeselwakeup on the way out | |
1330 | */ | |
1331 | if (fp->f_flag & FNONBLOCK) { | |
1332 | error = EAGAIN; | |
1333 | break; | |
1334 | } | |
6d2010ae A |
1335 | |
1336 | /* | |
1337 | * If read side wants to go away, we just issue a signal | |
1338 | * to ourselves. | |
1339 | */ | |
1340 | if (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) { | |
1341 | error = EPIPE; | |
1342 | break; | |
1343 | } | |
1344 | ||
91447636 A |
1345 | /* |
1346 | * We have no more space and have something to offer, | |
1347 | * wake up select/poll. | |
1348 | */ | |
1349 | pipeselwakeup(wpipe, wpipe); | |
1350 | ||
1351 | wpipe->pipe_state |= PIPE_WANTW; | |
1352 | ||
1353 | error = msleep(wpipe, PIPE_MTX(wpipe), PRIBIO | PCATCH, "pipewr", 0); | |
1354 | ||
1355 | if (error != 0) | |
1356 | break; | |
91447636 A |
1357 | } |
1358 | } | |
1359 | --wpipe->pipe_busy; | |
1360 | ||
1361 | if ((wpipe->pipe_busy == 0) && (wpipe->pipe_state & PIPE_WANT)) { | |
1362 | wpipe->pipe_state &= ~(PIPE_WANT | PIPE_WANTR); | |
1363 | wakeup(wpipe); | |
1364 | } | |
1365 | if (wpipe->pipe_buffer.cnt > 0) { | |
1366 | /* | |
1367 | * If there are any characters in the buffer, we wake up | |
1368 | * the reader if it was blocked waiting for data. | |
1369 | */ | |
1370 | if (wpipe->pipe_state & PIPE_WANTR) { | |
1371 | wpipe->pipe_state &= ~PIPE_WANTR; | |
1372 | wakeup(wpipe); | |
1373 | } | |
1374 | /* | |
1375 | * wake up thread blocked in select/poll or post the notification | |
1376 | */ | |
1377 | pipeselwakeup(wpipe, wpipe); | |
1378 | } | |
2d21ac55 A |
1379 | |
1380 | /* Update modification, status change (# of bytes in pipe) times */ | |
1381 | pipe_touch(rpipe, PIPE_MTIME | PIPE_CTIME); | |
1382 | pipe_touch(wpipe, PIPE_MTIME | PIPE_CTIME); | |
91447636 A |
1383 | PIPE_UNLOCK(rpipe); |
1384 | ||
1385 | return (error); | |
1386 | } | |
1387 | ||
1388 | /* | |
1389 | * we implement a very minimal set of ioctls for compatibility with sockets. | |
1390 | */ | |
1391 | /* ARGSUSED 3 */ | |
1392 | static int | |
2d21ac55 A |
1393 | pipe_ioctl(struct fileproc *fp, u_long cmd, caddr_t data, |
1394 | __unused vfs_context_t ctx) | |
91447636 A |
1395 | { |
1396 | struct pipe *mpipe = (struct pipe *)fp->f_data; | |
2d21ac55 | 1397 | #if CONFIG_MACF |
91447636 A |
1398 | int error; |
1399 | #endif | |
1400 | ||
1401 | PIPE_LOCK(mpipe); | |
1402 | ||
2d21ac55 A |
1403 | #if CONFIG_MACF |
1404 | error = mac_pipe_check_ioctl(kauth_cred_get(), mpipe, cmd); | |
91447636 A |
1405 | if (error) { |
1406 | PIPE_UNLOCK(mpipe); | |
1407 | ||
1408 | return (error); | |
1409 | } | |
1410 | #endif | |
1411 | ||
1412 | switch (cmd) { | |
1413 | ||
1414 | case FIONBIO: | |
1415 | PIPE_UNLOCK(mpipe); | |
1416 | return (0); | |
1417 | ||
1418 | case FIOASYNC: | |
1419 | if (*(int *)data) { | |
1420 | mpipe->pipe_state |= PIPE_ASYNC; | |
1421 | } else { | |
1422 | mpipe->pipe_state &= ~PIPE_ASYNC; | |
1423 | } | |
1424 | PIPE_UNLOCK(mpipe); | |
1425 | return (0); | |
1426 | ||
1427 | case FIONREAD: | |
1428 | #ifndef PIPE_NODIRECT | |
1429 | if (mpipe->pipe_state & PIPE_DIRECTW) | |
1430 | *(int *)data = mpipe->pipe_map.cnt; | |
1431 | else | |
1432 | #endif | |
1433 | *(int *)data = mpipe->pipe_buffer.cnt; | |
1434 | PIPE_UNLOCK(mpipe); | |
1435 | return (0); | |
1436 | ||
1437 | case TIOCSPGRP: | |
1438 | mpipe->pipe_pgid = *(int *)data; | |
1439 | ||
1440 | PIPE_UNLOCK(mpipe); | |
1441 | return (0); | |
1442 | ||
1443 | case TIOCGPGRP: | |
1444 | *(int *)data = mpipe->pipe_pgid; | |
1445 | ||
1446 | PIPE_UNLOCK(mpipe); | |
1447 | return (0); | |
1448 | ||
1449 | } | |
1450 | PIPE_UNLOCK(mpipe); | |
1451 | return (ENOTTY); | |
1452 | } | |
1453 | ||
1454 | ||
1455 | static int | |
2d21ac55 | 1456 | pipe_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx) |
91447636 A |
1457 | { |
1458 | struct pipe *rpipe = (struct pipe *)fp->f_data; | |
1459 | struct pipe *wpipe; | |
1460 | int retnum = 0; | |
1461 | ||
1462 | if (rpipe == NULL || rpipe == (struct pipe *)-1) | |
1463 | return (retnum); | |
1464 | ||
1465 | PIPE_LOCK(rpipe); | |
1466 | ||
1467 | wpipe = rpipe->pipe_peer; | |
1468 | ||
2d21ac55 A |
1469 | #if CONFIG_MACF |
1470 | /* | |
1471 | * XXX We should use a per thread credential here; minimally, the | |
1472 | * XXX process credential should have a persistent reference on it | |
1473 | * XXX before being passed in here. | |
1474 | */ | |
1475 | if (mac_pipe_check_select(vfs_context_ucred(ctx), rpipe, which)) { | |
1476 | PIPE_UNLOCK(rpipe); | |
1477 | return (0); | |
1478 | } | |
1479 | #endif | |
91447636 A |
1480 | switch (which) { |
1481 | ||
1482 | case FREAD: | |
1483 | if ((rpipe->pipe_state & PIPE_DIRECTW) || | |
1484 | (rpipe->pipe_buffer.cnt > 0) || | |
b0d623f7 | 1485 | (rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { |
91447636 A |
1486 | |
1487 | retnum = 1; | |
1488 | } else { | |
1489 | rpipe->pipe_state |= PIPE_SEL; | |
2d21ac55 | 1490 | selrecord(vfs_context_proc(ctx), &rpipe->pipe_sel, wql); |
91447636 A |
1491 | } |
1492 | break; | |
1493 | ||
1494 | case FWRITE: | |
b0d623f7 | 1495 | if (wpipe == NULL || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) || |
91447636 A |
1496 | (((wpipe->pipe_state & PIPE_DIRECTW) == 0) && |
1497 | (wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt) >= PIPE_BUF)) { | |
1498 | ||
1499 | retnum = 1; | |
1500 | } else { | |
1501 | wpipe->pipe_state |= PIPE_SEL; | |
2d21ac55 | 1502 | selrecord(vfs_context_proc(ctx), &wpipe->pipe_sel, wql); |
91447636 A |
1503 | } |
1504 | break; | |
1505 | case 0: | |
1506 | rpipe->pipe_state |= PIPE_SEL; | |
2d21ac55 | 1507 | selrecord(vfs_context_proc(ctx), &rpipe->pipe_sel, wql); |
91447636 A |
1508 | break; |
1509 | } | |
1510 | PIPE_UNLOCK(rpipe); | |
1511 | ||
1512 | return (retnum); | |
1513 | } | |
1514 | ||
1515 | ||
1516 | /* ARGSUSED 1 */ | |
1517 | static int | |
2d21ac55 | 1518 | pipe_close(struct fileglob *fg, __unused vfs_context_t ctx) |
91447636 A |
1519 | { |
1520 | struct pipe *cpipe; | |
1521 | ||
2d21ac55 | 1522 | proc_fdlock_spin(vfs_context_proc(ctx)); |
91447636 A |
1523 | cpipe = (struct pipe *)fg->fg_data; |
1524 | fg->fg_data = NULL; | |
2d21ac55 | 1525 | proc_fdunlock(vfs_context_proc(ctx)); |
91447636 A |
1526 | |
1527 | if (cpipe) | |
1528 | pipeclose(cpipe); | |
1529 | ||
1530 | return (0); | |
1531 | } | |
1532 | ||
1533 | static void | |
1534 | pipe_free_kmem(struct pipe *cpipe) | |
1535 | { | |
1536 | ||
1537 | if (cpipe->pipe_buffer.buffer != NULL) { | |
1538 | if (cpipe->pipe_buffer.size > PIPE_SIZE) | |
b0d623f7 A |
1539 | OSAddAtomic(-1, &nbigpipe); |
1540 | OSAddAtomic(-(cpipe->pipe_buffer.size), &amountpipekva); | |
1541 | OSAddAtomic(-1, &amountpipes); | |
91447636 A |
1542 | |
1543 | kmem_free(kernel_map, (vm_offset_t)cpipe->pipe_buffer.buffer, | |
1544 | cpipe->pipe_buffer.size); | |
1545 | cpipe->pipe_buffer.buffer = NULL; | |
1546 | } | |
1547 | #ifndef PIPE_NODIRECT | |
1548 | if (cpipe->pipe_map.kva != 0) { | |
1549 | atomic_subtract_int(&amountpipekvawired, | |
1550 | cpipe->pipe_buffer.size + PAGE_SIZE); | |
1551 | kmem_free(kernel_map, | |
1552 | cpipe->pipe_map.kva, | |
1553 | cpipe->pipe_buffer.size + PAGE_SIZE); | |
1554 | cpipe->pipe_map.cnt = 0; | |
1555 | cpipe->pipe_map.kva = 0; | |
1556 | cpipe->pipe_map.pos = 0; | |
1557 | cpipe->pipe_map.npages = 0; | |
1558 | } | |
1559 | #endif | |
1560 | } | |
1561 | ||
1562 | /* | |
1563 | * shutdown the pipe | |
1564 | */ | |
1565 | static void | |
1566 | pipeclose(struct pipe *cpipe) | |
1567 | { | |
1568 | struct pipe *ppipe; | |
1569 | ||
1570 | if (cpipe == NULL) | |
1571 | return; | |
1572 | ||
1573 | /* partially created pipes won't have a valid mutex. */ | |
1574 | if (PIPE_MTX(cpipe) != NULL) | |
1575 | PIPE_LOCK(cpipe); | |
1576 | ||
91447636 A |
1577 | |
1578 | /* | |
1579 | * If the other side is blocked, wake it up saying that | |
1580 | * we want to close it down. | |
1581 | */ | |
b0d623f7 | 1582 | cpipe->pipe_state &= ~PIPE_DRAIN; |
2d21ac55 A |
1583 | cpipe->pipe_state |= PIPE_EOF; |
1584 | pipeselwakeup(cpipe, cpipe); | |
1585 | ||
91447636 | 1586 | while (cpipe->pipe_busy) { |
2d21ac55 | 1587 | cpipe->pipe_state |= PIPE_WANT; |
91447636 A |
1588 | |
1589 | wakeup(cpipe); | |
91447636 A |
1590 | msleep(cpipe, PIPE_MTX(cpipe), PRIBIO, "pipecl", 0); |
1591 | } | |
1592 | ||
2d21ac55 A |
1593 | #if CONFIG_MACF |
1594 | /* | |
1595 | * Free the shared pipe label only after the two ends are disconnected. | |
1596 | */ | |
91447636 | 1597 | if (cpipe->pipe_label != NULL && cpipe->pipe_peer == NULL) |
2d21ac55 | 1598 | mac_pipe_label_destroy(cpipe); |
91447636 A |
1599 | #endif |
1600 | ||
1601 | /* | |
1602 | * Disconnect from peer | |
1603 | */ | |
1604 | if ((ppipe = cpipe->pipe_peer) != NULL) { | |
1605 | ||
b0d623f7 | 1606 | ppipe->pipe_state &= ~(PIPE_DRAIN); |
91447636 A |
1607 | ppipe->pipe_state |= PIPE_EOF; |
1608 | ||
1609 | pipeselwakeup(ppipe, ppipe); | |
1610 | wakeup(ppipe); | |
1611 | ||
1612 | if (cpipe->pipe_state & PIPE_KNOTE) | |
1613 | KNOTE(&ppipe->pipe_sel.si_note, 1); | |
1614 | ||
1615 | postpipeevent(ppipe, EV_RCLOSED); | |
1616 | ||
1617 | ppipe->pipe_peer = NULL; | |
1618 | } | |
1619 | evpipefree(cpipe); | |
1620 | ||
1621 | /* | |
1622 | * free resources | |
1623 | */ | |
1624 | if (PIPE_MTX(cpipe) != NULL) { | |
1625 | if (ppipe != NULL) { | |
1626 | /* | |
1627 | * since the mutex is shared and the peer is still | |
1628 | * alive, we need to release the mutex, not free it | |
1629 | */ | |
1630 | PIPE_UNLOCK(cpipe); | |
1631 | } else { | |
1632 | /* | |
1633 | * peer is gone, so we're the sole party left with | |
1634 | * interest in this mutex... we can just free it | |
1635 | */ | |
1636 | lck_mtx_free(PIPE_MTX(cpipe), pipe_mtx_grp); | |
1637 | } | |
1638 | } | |
1639 | pipe_free_kmem(cpipe); | |
1640 | ||
1641 | zfree(pipe_zone, cpipe); | |
b0d623f7 | 1642 | |
91447636 A |
1643 | } |
1644 | ||
91447636 A |
1645 | /*ARGSUSED*/ |
1646 | static int | |
2d21ac55 | 1647 | pipe_kqfilter(__unused struct fileproc *fp, struct knote *kn, __unused vfs_context_t ctx) |
91447636 A |
1648 | { |
1649 | struct pipe *cpipe; | |
1650 | ||
1651 | cpipe = (struct pipe *)kn->kn_fp->f_data; | |
1652 | ||
1653 | PIPE_LOCK(cpipe); | |
2d21ac55 A |
1654 | #if CONFIG_MACF |
1655 | /* | |
1656 | * XXX We should use a per thread credential here; minimally, the | |
1657 | * XXX process credential should have a persistent reference on it | |
1658 | * XXX before being passed in here. | |
1659 | */ | |
1660 | if (mac_pipe_check_kqfilter(vfs_context_ucred(ctx), kn, cpipe) != 0) { | |
1661 | PIPE_UNLOCK(cpipe); | |
1662 | return (1); | |
1663 | } | |
1664 | #endif | |
91447636 A |
1665 | |
1666 | switch (kn->kn_filter) { | |
1667 | case EVFILT_READ: | |
1668 | kn->kn_fop = &pipe_rfiltops; | |
2d21ac55 | 1669 | |
91447636 A |
1670 | break; |
1671 | case EVFILT_WRITE: | |
1672 | kn->kn_fop = &pipe_wfiltops; | |
1673 | ||
1674 | if (cpipe->pipe_peer == NULL) { | |
1675 | /* | |
1676 | * other end of pipe has been closed | |
1677 | */ | |
1678 | PIPE_UNLOCK(cpipe); | |
1679 | return (EPIPE); | |
1680 | } | |
2d21ac55 | 1681 | if (cpipe->pipe_peer) |
91447636 A |
1682 | cpipe = cpipe->pipe_peer; |
1683 | break; | |
1684 | default: | |
1685 | PIPE_UNLOCK(cpipe); | |
1686 | return (1); | |
1687 | } | |
1688 | ||
1689 | if (KNOTE_ATTACH(&cpipe->pipe_sel.si_note, kn)) | |
1690 | cpipe->pipe_state |= PIPE_KNOTE; | |
1691 | ||
1692 | PIPE_UNLOCK(cpipe); | |
1693 | return (0); | |
1694 | } | |
1695 | ||
1696 | static void | |
1697 | filt_pipedetach(struct knote *kn) | |
1698 | { | |
1699 | struct pipe *cpipe = (struct pipe *)kn->kn_fp->f_data; | |
1700 | ||
1701 | PIPE_LOCK(cpipe); | |
1702 | ||
1703 | if (kn->kn_filter == EVFILT_WRITE) { | |
1704 | if (cpipe->pipe_peer == NULL) { | |
1705 | PIPE_UNLOCK(cpipe); | |
1706 | return; | |
1707 | } | |
1708 | cpipe = cpipe->pipe_peer; | |
1709 | } | |
1710 | if (cpipe->pipe_state & PIPE_KNOTE) { | |
1711 | if (KNOTE_DETACH(&cpipe->pipe_sel.si_note, kn)) | |
1712 | cpipe->pipe_state &= ~PIPE_KNOTE; | |
1713 | } | |
1714 | PIPE_UNLOCK(cpipe); | |
1715 | } | |
1716 | ||
1717 | /*ARGSUSED*/ | |
1718 | static int | |
1719 | filt_piperead(struct knote *kn, long hint) | |
1720 | { | |
1721 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1722 | struct pipe *wpipe; | |
1723 | int retval; | |
1724 | ||
1725 | /* | |
1726 | * if hint == 0, then we've been called from the kevent | |
1727 | * world directly and do not currently hold the pipe mutex... | |
1728 | * if hint == 1, we're being called back via the KNOTE post | |
1729 | * we made in pipeselwakeup, and we already hold the mutex... | |
1730 | */ | |
1731 | if (hint == 0) | |
1732 | PIPE_LOCK(rpipe); | |
1733 | ||
1734 | wpipe = rpipe->pipe_peer; | |
1735 | kn->kn_data = rpipe->pipe_buffer.cnt; | |
1736 | ||
1737 | #ifndef PIPE_NODIRECT | |
1738 | if ((kn->kn_data == 0) && (rpipe->pipe_state & PIPE_DIRECTW)) | |
1739 | kn->kn_data = rpipe->pipe_map.cnt; | |
1740 | #endif | |
b0d623f7 A |
1741 | if ((rpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF)) || |
1742 | (wpipe == NULL) || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { | |
91447636 A |
1743 | kn->kn_flags |= EV_EOF; |
1744 | retval = 1; | |
2d21ac55 | 1745 | } else { |
6d2010ae A |
1746 | int64_t lowwat = 1; |
1747 | if (kn->kn_sfflags & NOTE_LOWAT) { | |
1748 | if (rpipe->pipe_buffer.size && kn->kn_sdata > rpipe->pipe_buffer.size) | |
1749 | lowwat = rpipe->pipe_buffer.size; | |
1750 | else if (kn->kn_sdata > lowwat) | |
1751 | lowwat = kn->kn_sdata; | |
1752 | } | |
1753 | retval = kn->kn_data >= lowwat; | |
2d21ac55 | 1754 | } |
91447636 A |
1755 | |
1756 | if (hint == 0) | |
1757 | PIPE_UNLOCK(rpipe); | |
1758 | ||
1759 | return (retval); | |
1760 | } | |
1761 | ||
1762 | /*ARGSUSED*/ | |
1763 | static int | |
1764 | filt_pipewrite(struct knote *kn, long hint) | |
1765 | { | |
1766 | struct pipe *rpipe = (struct pipe *)kn->kn_fp->f_data; | |
1767 | struct pipe *wpipe; | |
1768 | ||
1769 | /* | |
1770 | * if hint == 0, then we've been called from the kevent | |
1771 | * world directly and do not currently hold the pipe mutex... | |
1772 | * if hint == 1, we're being called back via the KNOTE post | |
1773 | * we made in pipeselwakeup, and we already hold the mutex... | |
1774 | */ | |
1775 | if (hint == 0) | |
1776 | PIPE_LOCK(rpipe); | |
1777 | ||
1778 | wpipe = rpipe->pipe_peer; | |
1779 | ||
b0d623f7 | 1780 | if ((wpipe == NULL) || (wpipe->pipe_state & (PIPE_DRAIN | PIPE_EOF))) { |
91447636 A |
1781 | kn->kn_data = 0; |
1782 | kn->kn_flags |= EV_EOF; | |
1783 | ||
1784 | if (hint == 0) | |
1785 | PIPE_UNLOCK(rpipe); | |
1786 | return (1); | |
1787 | } | |
1788 | kn->kn_data = wpipe->pipe_buffer.size - wpipe->pipe_buffer.cnt; | |
2d21ac55 | 1789 | if (!kn->kn_data && wpipe->pipe_buffer.size == 0) |
6d2010ae | 1790 | kn->kn_data = PIPE_BUF; /* unwritten pipe is ready for write */ |
91447636 A |
1791 | |
1792 | #ifndef PIPE_NODIRECT | |
1793 | if (wpipe->pipe_state & PIPE_DIRECTW) | |
1794 | kn->kn_data = 0; | |
1795 | #endif | |
6d2010ae A |
1796 | int64_t lowwat = PIPE_BUF; |
1797 | if (kn->kn_sfflags & NOTE_LOWAT) { | |
1798 | if (wpipe->pipe_buffer.size && kn->kn_sdata > wpipe->pipe_buffer.size) | |
1799 | lowwat = wpipe->pipe_buffer.size; | |
1800 | else if (kn->kn_sdata > lowwat) | |
1801 | lowwat = kn->kn_sdata; | |
1802 | } | |
1803 | ||
91447636 A |
1804 | if (hint == 0) |
1805 | PIPE_UNLOCK(rpipe); | |
1806 | ||
6d2010ae | 1807 | return (kn->kn_data >= lowwat); |
91447636 | 1808 | } |
0c530ab8 A |
1809 | |
1810 | int | |
1811 | fill_pipeinfo(struct pipe * cpipe, struct pipe_info * pinfo) | |
1812 | { | |
2d21ac55 | 1813 | #if CONFIG_MACF |
0c530ab8 A |
1814 | int error; |
1815 | #endif | |
1816 | struct timeval now; | |
2d21ac55 A |
1817 | struct vinfo_stat * ub; |
1818 | int pipe_size = 0; | |
1819 | int pipe_count; | |
0c530ab8 A |
1820 | |
1821 | if (cpipe == NULL) | |
1822 | return (EBADF); | |
0c530ab8 | 1823 | PIPE_LOCK(cpipe); |
2d21ac55 A |
1824 | |
1825 | #if CONFIG_MACF | |
1826 | error = mac_pipe_check_stat(kauth_cred_get(), cpipe); | |
1827 | if (error) { | |
1828 | PIPE_UNLOCK(cpipe); | |
0c530ab8 | 1829 | return (error); |
2d21ac55 | 1830 | } |
0c530ab8 A |
1831 | #endif |
1832 | if (cpipe->pipe_buffer.buffer == 0) { | |
1833 | /* | |
1834 | * must be stat'ing the write fd | |
1835 | */ | |
2d21ac55 A |
1836 | if (cpipe->pipe_peer) { |
1837 | /* | |
1838 | * the peer still exists, use it's info | |
1839 | */ | |
1840 | pipe_size = cpipe->pipe_peer->pipe_buffer.size; | |
1841 | pipe_count = cpipe->pipe_peer->pipe_buffer.cnt; | |
1842 | } else { | |
1843 | pipe_count = 0; | |
1844 | } | |
1845 | } else { | |
1846 | pipe_size = cpipe->pipe_buffer.size; | |
1847 | pipe_count = cpipe->pipe_buffer.cnt; | |
0c530ab8 | 1848 | } |
2d21ac55 A |
1849 | /* |
1850 | * since peer's buffer is setup ouside of lock | |
1851 | * we might catch it in transient state | |
1852 | */ | |
1853 | if (pipe_size == 0) | |
1854 | pipe_size = PIPE_SIZE; | |
0c530ab8 A |
1855 | |
1856 | ub = &pinfo->pipe_stat; | |
1857 | ||
1858 | bzero(ub, sizeof(*ub)); | |
2d21ac55 A |
1859 | ub->vst_mode = S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
1860 | ub->vst_blksize = pipe_size; | |
1861 | ub->vst_size = pipe_count; | |
1862 | if (ub->vst_blksize != 0) | |
1863 | ub->vst_blocks = (ub->vst_size + ub->vst_blksize - 1) / ub->vst_blksize; | |
1864 | ub->vst_nlink = 1; | |
0c530ab8 | 1865 | |
2d21ac55 A |
1866 | ub->vst_uid = kauth_getuid(); |
1867 | ub->vst_gid = kauth_getgid(); | |
0c530ab8 A |
1868 | |
1869 | microtime(&now); | |
2d21ac55 A |
1870 | ub->vst_atime = now.tv_sec; |
1871 | ub->vst_atimensec = now.tv_usec * 1000; | |
0c530ab8 | 1872 | |
2d21ac55 A |
1873 | ub->vst_mtime = now.tv_sec; |
1874 | ub->vst_mtimensec = now.tv_usec * 1000; | |
0c530ab8 | 1875 | |
2d21ac55 A |
1876 | ub->vst_ctime = now.tv_sec; |
1877 | ub->vst_ctimensec = now.tv_usec * 1000; | |
0c530ab8 A |
1878 | |
1879 | /* | |
1880 | * Left as 0: st_dev, st_ino, st_nlink, st_rdev, st_flags, st_gen, st_uid, st_gid. | |
1881 | * XXX (st_dev, st_ino) should be unique. | |
1882 | */ | |
1883 | ||
1884 | pinfo->pipe_handle = (uint64_t)((uintptr_t)cpipe); | |
1885 | pinfo->pipe_peerhandle = (uint64_t)((uintptr_t)(cpipe->pipe_peer)); | |
1886 | pinfo->pipe_status = cpipe->pipe_state; | |
2d21ac55 A |
1887 | |
1888 | PIPE_UNLOCK(cpipe); | |
1889 | ||
0c530ab8 A |
1890 | return (0); |
1891 | } | |
b0d623f7 A |
1892 | |
1893 | ||
1894 | static int | |
1895 | pipe_drain(struct fileproc *fp, __unused vfs_context_t ctx) | |
1896 | { | |
1897 | ||
1898 | /* Note: fdlock already held */ | |
1899 | struct pipe *ppipe, *cpipe = (struct pipe *)(fp->f_fglob->fg_data); | |
1900 | ||
1901 | if (cpipe) { | |
1902 | PIPE_LOCK(cpipe); | |
1903 | cpipe->pipe_state |= PIPE_DRAIN; | |
1904 | cpipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW); | |
1905 | wakeup(cpipe); | |
1906 | ||
1907 | /* Must wake up peer: a writer sleeps on the read side */ | |
1908 | if ((ppipe = cpipe->pipe_peer)) { | |
1909 | ppipe->pipe_state |= PIPE_DRAIN; | |
1910 | ppipe->pipe_state &= ~(PIPE_WANTR | PIPE_WANTW); | |
1911 | wakeup(ppipe); | |
1912 | } | |
1913 | ||
1914 | PIPE_UNLOCK(cpipe); | |
1915 | return 0; | |
1916 | } | |
1917 | ||
1918 | return 1; | |
1919 | } | |
1920 | ||
1921 | ||
1922 | ||
1923 | ||
1924 |