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