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