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