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