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