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