]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2016 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1989, 1991, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * (c) UNIX System Laboratories, Inc. | |
33 | * All or some portions of this file are derived from material licensed | |
34 | * to the University of California by American Telephone and Telegraph | |
35 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
36 | * the permission of UNIX System Laboratories, Inc. | |
37 | * | |
38 | * Redistribution and use in source and binary forms, with or without | |
39 | * modification, are permitted provided that the following conditions | |
40 | * are met: | |
41 | * 1. Redistributions of source code must retain the above copyright | |
42 | * notice, this list of conditions and the following disclaimer. | |
43 | * 2. Redistributions in binary form must reproduce the above copyright | |
44 | * notice, this list of conditions and the following disclaimer in the | |
45 | * documentation and/or other materials provided with the distribution. | |
46 | * 3. All advertising materials mentioning features or use of this software | |
47 | * must display the following acknowledgement: | |
48 | * This product includes software developed by the University of | |
49 | * California, Berkeley and its contributors. | |
50 | * 4. Neither the name of the University nor the names of its contributors | |
51 | * may be used to endorse or promote products derived from this software | |
52 | * without specific prior written permission. | |
53 | * | |
54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
64 | * SUCH DAMAGE. | |
65 | * | |
66 | * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95 | |
67 | */ | |
68 | /* | |
69 | * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce | |
70 | * support for mandatory and extensible security protections. This notice | |
71 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
72 | * Version 2.0. | |
73 | */ | |
74 | ||
75 | #include <sys/param.h> | |
76 | #include <sys/systm.h> | |
77 | #include <sys/filedesc.h> | |
78 | #include <sys/kernel.h> | |
79 | #include <sys/vnode_internal.h> | |
80 | #include <sys/proc_internal.h> | |
81 | #include <sys/kauth.h> | |
82 | #include <sys/file_internal.h> | |
83 | #include <sys/guarded.h> | |
84 | #include <sys/priv.h> | |
85 | #include <sys/socket.h> | |
86 | #include <sys/socketvar.h> | |
87 | #include <sys/stat.h> | |
88 | #include <sys/ioctl.h> | |
89 | #include <sys/fcntl.h> | |
90 | #include <sys/malloc.h> | |
91 | #include <sys/mman.h> | |
92 | #include <sys/syslog.h> | |
93 | #include <sys/unistd.h> | |
94 | #include <sys/resourcevar.h> | |
95 | #include <sys/aio_kern.h> | |
96 | #include <sys/ev.h> | |
97 | #include <kern/locks.h> | |
98 | #include <sys/uio_internal.h> | |
99 | #include <sys/codesign.h> | |
100 | #include <sys/codedir_internal.h> | |
101 | ||
102 | #include <security/audit/audit.h> | |
103 | ||
104 | #include <sys/mount_internal.h> | |
105 | #include <sys/kdebug.h> | |
106 | #include <sys/sysproto.h> | |
107 | #include <sys/pipe.h> | |
108 | #include <sys/spawn.h> | |
109 | #include <sys/cprotect.h> | |
110 | #include <kern/kern_types.h> | |
111 | #include <kern/kalloc.h> | |
112 | #include <kern/waitq.h> | |
113 | #include <libkern/OSAtomic.h> | |
114 | ||
115 | #include <sys/ubc_internal.h> | |
116 | ||
117 | #include <kern/ipc_misc.h> | |
118 | #include <vm/vm_protos.h> | |
119 | ||
120 | #include <mach/mach_port.h> | |
121 | #include <stdbool.h> | |
122 | ||
123 | kern_return_t ipc_object_copyin(ipc_space_t, mach_port_name_t, | |
124 | mach_msg_type_name_t, ipc_port_t *); | |
125 | void ipc_port_release_send(ipc_port_t); | |
126 | ||
127 | struct psemnode; | |
128 | struct pshmnode; | |
129 | ||
130 | static int finishdup(proc_t p, | |
131 | struct filedesc *fdp, int old, int new, int flags, int32_t *retval); | |
132 | ||
133 | int falloc_locked(proc_t p, struct fileproc **resultfp, int *resultfd, vfs_context_t ctx, int locked); | |
134 | void fg_drop(struct fileproc * fp); | |
135 | void fg_free(struct fileglob *fg); | |
136 | void fg_ref(struct fileproc * fp); | |
137 | void fileport_releasefg(struct fileglob *fg); | |
138 | ||
139 | /* flags for close_internal_locked */ | |
140 | #define FD_DUP2RESV 1 | |
141 | ||
142 | /* We don't want these exported */ | |
143 | ||
144 | __private_extern__ | |
145 | int unlink1(vfs_context_t, vnode_t, user_addr_t, enum uio_seg, int); | |
146 | ||
147 | static void _fdrelse(struct proc * p, int fd); | |
148 | ||
149 | ||
150 | extern void file_lock_init(void); | |
151 | ||
152 | extern kauth_scope_t kauth_scope_fileop; | |
153 | ||
154 | /* Conflict wait queue for when selects collide (opaque type) */ | |
155 | extern struct waitq select_conflict_queue; | |
156 | ||
157 | #define f_flag f_fglob->fg_flag | |
158 | #define f_type f_fglob->fg_ops->fo_type | |
159 | #define f_msgcount f_fglob->fg_msgcount | |
160 | #define f_cred f_fglob->fg_cred | |
161 | #define f_ops f_fglob->fg_ops | |
162 | #define f_offset f_fglob->fg_offset | |
163 | #define f_data f_fglob->fg_data | |
164 | #define CHECK_ADD_OVERFLOW_INT64L(x, y) \ | |
165 | (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \ | |
166 | (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \ | |
167 | ? 1 : 0) | |
168 | /* | |
169 | * Descriptor management. | |
170 | */ | |
171 | struct fmsglist fmsghead; /* head of list of open files */ | |
172 | struct fmsglist fmsg_ithead; /* head of list of open files */ | |
173 | int nfiles; /* actual number of open files */ | |
174 | ||
175 | ||
176 | lck_grp_attr_t * file_lck_grp_attr; | |
177 | lck_grp_t * file_lck_grp; | |
178 | lck_attr_t * file_lck_attr; | |
179 | ||
180 | lck_mtx_t * uipc_lock; | |
181 | ||
182 | ||
183 | /* | |
184 | * check_file_seek_range | |
185 | * | |
186 | * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX. | |
187 | * | |
188 | * Parameters: fl Flock structure. | |
189 | * cur_file_offset Current offset in the file. | |
190 | * | |
191 | * Returns: 0 on Success. | |
192 | * EOVERFLOW on overflow. | |
193 | * EINVAL on offset less than zero. | |
194 | */ | |
195 | ||
196 | static int | |
197 | check_file_seek_range(struct flock *fl, off_t cur_file_offset) | |
198 | { | |
199 | if (fl->l_whence == SEEK_CUR) { | |
200 | /* Check if the start marker is beyond LLONG_MAX. */ | |
201 | if (CHECK_ADD_OVERFLOW_INT64L(fl->l_start, cur_file_offset)) { | |
202 | /* Check if start marker is negative */ | |
203 | if (fl->l_start < 0) { | |
204 | return EINVAL; | |
205 | } | |
206 | return EOVERFLOW; | |
207 | } | |
208 | /* Check if the start marker is negative. */ | |
209 | if (fl->l_start + cur_file_offset < 0) { | |
210 | return EINVAL; | |
211 | } | |
212 | /* Check if end marker is beyond LLONG_MAX. */ | |
213 | if ((fl->l_len > 0) && (CHECK_ADD_OVERFLOW_INT64L(fl->l_start + | |
214 | cur_file_offset, fl->l_len - 1))) { | |
215 | return EOVERFLOW; | |
216 | } | |
217 | /* Check if the end marker is negative. */ | |
218 | if ((fl->l_len <= 0) && (fl->l_start + cur_file_offset + | |
219 | fl->l_len < 0)) { | |
220 | return EINVAL; | |
221 | } | |
222 | } else if (fl->l_whence == SEEK_SET) { | |
223 | /* Check if the start marker is negative. */ | |
224 | if (fl->l_start < 0) { | |
225 | return EINVAL; | |
226 | } | |
227 | /* Check if the end marker is beyond LLONG_MAX. */ | |
228 | if ((fl->l_len > 0) && | |
229 | CHECK_ADD_OVERFLOW_INT64L(fl->l_start, fl->l_len - 1)) { | |
230 | return EOVERFLOW; | |
231 | } | |
232 | /* Check if the end marker is negative. */ | |
233 | if ((fl->l_len < 0) && fl->l_start + fl->l_len < 0) { | |
234 | return EINVAL; | |
235 | } | |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
240 | ||
241 | /* | |
242 | * file_lock_init | |
243 | * | |
244 | * Description: Initialize the file lock group and the uipc and flist locks | |
245 | * | |
246 | * Parameters: (void) | |
247 | * | |
248 | * Returns: void | |
249 | * | |
250 | * Notes: Called at system startup from bsd_init(). | |
251 | */ | |
252 | void | |
253 | file_lock_init(void) | |
254 | { | |
255 | /* allocate file lock group attribute and group */ | |
256 | file_lck_grp_attr= lck_grp_attr_alloc_init(); | |
257 | ||
258 | file_lck_grp = lck_grp_alloc_init("file", file_lck_grp_attr); | |
259 | ||
260 | /* Allocate file lock attribute */ | |
261 | file_lck_attr = lck_attr_alloc_init(); | |
262 | ||
263 | uipc_lock = lck_mtx_alloc_init(file_lck_grp, file_lck_attr); | |
264 | } | |
265 | ||
266 | ||
267 | /* | |
268 | * proc_fdlock, proc_fdlock_spin | |
269 | * | |
270 | * Description: Lock to control access to the per process struct fileproc | |
271 | * and struct filedesc | |
272 | * | |
273 | * Parameters: p Process to take the lock on | |
274 | * | |
275 | * Returns: void | |
276 | * | |
277 | * Notes: The lock is initialized in forkproc() and destroyed in | |
278 | * reap_child_process(). | |
279 | */ | |
280 | void | |
281 | proc_fdlock(proc_t p) | |
282 | { | |
283 | lck_mtx_lock(&p->p_fdmlock); | |
284 | } | |
285 | ||
286 | void | |
287 | proc_fdlock_spin(proc_t p) | |
288 | { | |
289 | lck_mtx_lock_spin(&p->p_fdmlock); | |
290 | } | |
291 | ||
292 | void | |
293 | proc_fdlock_assert(proc_t p, int assertflags) | |
294 | { | |
295 | lck_mtx_assert(&p->p_fdmlock, assertflags); | |
296 | } | |
297 | ||
298 | ||
299 | /* | |
300 | * proc_fdunlock | |
301 | * | |
302 | * Description: Unlock the lock previously locked by a call to proc_fdlock() | |
303 | * | |
304 | * Parameters: p Process to drop the lock on | |
305 | * | |
306 | * Returns: void | |
307 | */ | |
308 | void | |
309 | proc_fdunlock(proc_t p) | |
310 | { | |
311 | lck_mtx_unlock(&p->p_fdmlock); | |
312 | } | |
313 | ||
314 | ||
315 | /* | |
316 | * System calls on descriptors. | |
317 | */ | |
318 | ||
319 | ||
320 | /* | |
321 | * getdtablesize | |
322 | * | |
323 | * Description: Returns the per process maximum size of the descriptor table | |
324 | * | |
325 | * Parameters: p Process being queried | |
326 | * retval Pointer to the call return area | |
327 | * | |
328 | * Returns: 0 Success | |
329 | * | |
330 | * Implicit returns: | |
331 | * *retval (modified) Size of dtable | |
332 | */ | |
333 | int | |
334 | getdtablesize(proc_t p, __unused struct getdtablesize_args *uap, int32_t *retval) | |
335 | { | |
336 | proc_fdlock_spin(p); | |
337 | *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); | |
338 | proc_fdunlock(p); | |
339 | ||
340 | return (0); | |
341 | } | |
342 | ||
343 | ||
344 | void | |
345 | procfdtbl_reservefd(struct proc * p, int fd) | |
346 | { | |
347 | p->p_fd->fd_ofiles[fd] = NULL; | |
348 | p->p_fd->fd_ofileflags[fd] |= UF_RESERVED; | |
349 | } | |
350 | ||
351 | void | |
352 | procfdtbl_markclosefd(struct proc * p, int fd) | |
353 | { | |
354 | p->p_fd->fd_ofileflags[fd] |= (UF_RESERVED | UF_CLOSING); | |
355 | } | |
356 | ||
357 | void | |
358 | procfdtbl_releasefd(struct proc * p, int fd, struct fileproc * fp) | |
359 | { | |
360 | if (fp != NULL) | |
361 | p->p_fd->fd_ofiles[fd] = fp; | |
362 | p->p_fd->fd_ofileflags[fd] &= ~UF_RESERVED; | |
363 | if ((p->p_fd->fd_ofileflags[fd] & UF_RESVWAIT) == UF_RESVWAIT) { | |
364 | p->p_fd->fd_ofileflags[fd] &= ~UF_RESVWAIT; | |
365 | wakeup(&p->p_fd); | |
366 | } | |
367 | } | |
368 | ||
369 | void | |
370 | procfdtbl_waitfd(struct proc * p, int fd) | |
371 | { | |
372 | p->p_fd->fd_ofileflags[fd] |= UF_RESVWAIT; | |
373 | msleep(&p->p_fd, &p->p_fdmlock, PRIBIO, "ftbl_waitfd", NULL); | |
374 | } | |
375 | ||
376 | ||
377 | void | |
378 | procfdtbl_clearfd(struct proc * p, int fd) | |
379 | { | |
380 | int waiting; | |
381 | ||
382 | waiting = (p->p_fd->fd_ofileflags[fd] & UF_RESVWAIT); | |
383 | p->p_fd->fd_ofiles[fd] = NULL; | |
384 | p->p_fd->fd_ofileflags[fd] = 0; | |
385 | if ( waiting == UF_RESVWAIT) { | |
386 | wakeup(&p->p_fd); | |
387 | } | |
388 | } | |
389 | ||
390 | /* | |
391 | * _fdrelse | |
392 | * | |
393 | * Description: Inline utility function to free an fd in a filedesc | |
394 | * | |
395 | * Parameters: fdp Pointer to filedesc fd lies in | |
396 | * fd fd to free | |
397 | * reserv fd should be reserved | |
398 | * | |
399 | * Returns: void | |
400 | * | |
401 | * Locks: Assumes proc_fdlock for process pointing to fdp is held by | |
402 | * the caller | |
403 | */ | |
404 | static void | |
405 | _fdrelse(struct proc * p, int fd) | |
406 | { | |
407 | struct filedesc *fdp = p->p_fd; | |
408 | int nfd = 0; | |
409 | ||
410 | if (fd < fdp->fd_freefile) | |
411 | fdp->fd_freefile = fd; | |
412 | #if DIAGNOSTIC | |
413 | if (fd > fdp->fd_lastfile) | |
414 | panic("fdrelse: fd_lastfile inconsistent"); | |
415 | #endif | |
416 | procfdtbl_clearfd(p, fd); | |
417 | ||
418 | while ((nfd = fdp->fd_lastfile) > 0 && | |
419 | fdp->fd_ofiles[nfd] == NULL && | |
420 | !(fdp->fd_ofileflags[nfd] & UF_RESERVED)) | |
421 | fdp->fd_lastfile--; | |
422 | } | |
423 | ||
424 | ||
425 | int | |
426 | fd_rdwr( | |
427 | int fd, | |
428 | enum uio_rw rw, | |
429 | uint64_t base, | |
430 | int64_t len, | |
431 | enum uio_seg segflg, | |
432 | off_t offset, | |
433 | int io_flg, | |
434 | int64_t *aresid) | |
435 | { | |
436 | struct fileproc *fp; | |
437 | proc_t p; | |
438 | int error = 0; | |
439 | int flags = 0; | |
440 | int spacetype; | |
441 | uio_t auio = NULL; | |
442 | char uio_buf[ UIO_SIZEOF(1) ]; | |
443 | struct vfs_context context = *(vfs_context_current()); | |
444 | bool wrote_some = false; | |
445 | ||
446 | p = current_proc(); | |
447 | ||
448 | error = fp_lookup(p, fd, &fp, 0); | |
449 | if (error) | |
450 | return(error); | |
451 | ||
452 | if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_PIPE && fp->f_type != DTYPE_SOCKET) { | |
453 | error = EINVAL; | |
454 | goto out; | |
455 | } | |
456 | if (rw == UIO_WRITE && !(fp->f_flag & FWRITE)) { | |
457 | error = EBADF; | |
458 | goto out; | |
459 | } | |
460 | ||
461 | if (rw == UIO_READ && !(fp->f_flag & FREAD)) { | |
462 | error = EBADF; | |
463 | goto out; | |
464 | } | |
465 | ||
466 | context.vc_ucred = fp->f_fglob->fg_cred; | |
467 | ||
468 | if (UIO_SEG_IS_USER_SPACE(segflg)) | |
469 | spacetype = proc_is64bit(p) ? UIO_USERSPACE64 : UIO_USERSPACE32; | |
470 | else | |
471 | spacetype = UIO_SYSSPACE; | |
472 | ||
473 | auio = uio_createwithbuffer(1, offset, spacetype, rw, &uio_buf[0], sizeof(uio_buf)); | |
474 | ||
475 | uio_addiov(auio, base, len); | |
476 | ||
477 | if ( !(io_flg & IO_APPEND)) | |
478 | flags = FOF_OFFSET; | |
479 | ||
480 | if (rw == UIO_WRITE) { | |
481 | user_ssize_t orig_resid = uio_resid(auio); | |
482 | error = fo_write(fp, auio, flags, &context); | |
483 | wrote_some = uio_resid(auio) < orig_resid; | |
484 | } else | |
485 | error = fo_read(fp, auio, flags, &context); | |
486 | ||
487 | if (aresid) | |
488 | *aresid = uio_resid(auio); | |
489 | else { | |
490 | if (uio_resid(auio) && error == 0) | |
491 | error = EIO; | |
492 | } | |
493 | out: | |
494 | if (wrote_some) | |
495 | fp_drop_written(p, fd, fp); | |
496 | else | |
497 | fp_drop(p, fd, fp, 0); | |
498 | ||
499 | return error; | |
500 | } | |
501 | ||
502 | ||
503 | ||
504 | /* | |
505 | * dup | |
506 | * | |
507 | * Description: Duplicate a file descriptor. | |
508 | * | |
509 | * Parameters: p Process performing the dup | |
510 | * uap->fd The fd to dup | |
511 | * retval Pointer to the call return area | |
512 | * | |
513 | * Returns: 0 Success | |
514 | * !0 Errno | |
515 | * | |
516 | * Implicit returns: | |
517 | * *retval (modified) The new descriptor | |
518 | */ | |
519 | int | |
520 | dup(proc_t p, struct dup_args *uap, int32_t *retval) | |
521 | { | |
522 | struct filedesc *fdp = p->p_fd; | |
523 | int old = uap->fd; | |
524 | int new, error; | |
525 | struct fileproc *fp; | |
526 | ||
527 | proc_fdlock(p); | |
528 | if ( (error = fp_lookup(p, old, &fp, 1)) ) { | |
529 | proc_fdunlock(p); | |
530 | return(error); | |
531 | } | |
532 | if (FP_ISGUARDED(fp, GUARD_DUP)) { | |
533 | error = fp_guard_exception(p, old, fp, kGUARD_EXC_DUP); | |
534 | (void) fp_drop(p, old, fp, 1); | |
535 | proc_fdunlock(p); | |
536 | return (error); | |
537 | } | |
538 | if ( (error = fdalloc(p, 0, &new)) ) { | |
539 | fp_drop(p, old, fp, 1); | |
540 | proc_fdunlock(p); | |
541 | return (error); | |
542 | } | |
543 | error = finishdup(p, fdp, old, new, 0, retval); | |
544 | fp_drop(p, old, fp, 1); | |
545 | proc_fdunlock(p); | |
546 | ||
547 | if (ENTR_SHOULDTRACE && fp->f_type == DTYPE_SOCKET) { | |
548 | KERNEL_ENERGYTRACE(kEnTrActKernSocket, DBG_FUNC_START, | |
549 | new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp->f_data)); | |
550 | } | |
551 | ||
552 | return (error); | |
553 | } | |
554 | ||
555 | /* | |
556 | * dup2 | |
557 | * | |
558 | * Description: Duplicate a file descriptor to a particular value. | |
559 | * | |
560 | * Parameters: p Process performing the dup | |
561 | * uap->from The fd to dup | |
562 | * uap->to The fd to dup it to | |
563 | * retval Pointer to the call return area | |
564 | * | |
565 | * Returns: 0 Success | |
566 | * !0 Errno | |
567 | * | |
568 | * Implicit returns: | |
569 | * *retval (modified) The new descriptor | |
570 | */ | |
571 | int | |
572 | dup2(proc_t p, struct dup2_args *uap, int32_t *retval) | |
573 | { | |
574 | struct filedesc *fdp = p->p_fd; | |
575 | int old = uap->from, new = uap->to; | |
576 | int i, error; | |
577 | struct fileproc *fp, *nfp; | |
578 | ||
579 | proc_fdlock(p); | |
580 | ||
581 | startover: | |
582 | if ( (error = fp_lookup(p, old, &fp, 1)) ) { | |
583 | proc_fdunlock(p); | |
584 | return(error); | |
585 | } | |
586 | if (FP_ISGUARDED(fp, GUARD_DUP)) { | |
587 | error = fp_guard_exception(p, old, fp, kGUARD_EXC_DUP); | |
588 | (void) fp_drop(p, old, fp, 1); | |
589 | proc_fdunlock(p); | |
590 | return (error); | |
591 | } | |
592 | if (new < 0 || | |
593 | (rlim_t)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || | |
594 | new >= maxfiles) { | |
595 | fp_drop(p, old, fp, 1); | |
596 | proc_fdunlock(p); | |
597 | return (EBADF); | |
598 | } | |
599 | if (old == new) { | |
600 | fp_drop(p, old, fp, 1); | |
601 | *retval = new; | |
602 | proc_fdunlock(p); | |
603 | return (0); | |
604 | } | |
605 | if (new < 0 || new >= fdp->fd_nfiles) { | |
606 | if ( (error = fdalloc(p, new, &i)) ) { | |
607 | fp_drop(p, old, fp, 1); | |
608 | proc_fdunlock(p); | |
609 | return (error); | |
610 | } | |
611 | if (new != i) { | |
612 | fdrelse(p, i); | |
613 | goto closeit; | |
614 | } | |
615 | } else { | |
616 | closeit: | |
617 | while ((fdp->fd_ofileflags[new] & UF_RESERVED) == UF_RESERVED) { | |
618 | fp_drop(p, old, fp, 1); | |
619 | procfdtbl_waitfd(p, new); | |
620 | #if DIAGNOSTIC | |
621 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
622 | #endif | |
623 | goto startover; | |
624 | } | |
625 | ||
626 | if ((fdp->fd_ofiles[new] != NULL) && | |
627 | ((error = fp_lookup(p, new, &nfp, 1)) == 0)) { | |
628 | fp_drop(p, old, fp, 1); | |
629 | if (FP_ISGUARDED(nfp, GUARD_CLOSE)) { | |
630 | error = fp_guard_exception(p, | |
631 | new, nfp, kGUARD_EXC_CLOSE); | |
632 | (void) fp_drop(p, new, nfp, 1); | |
633 | proc_fdunlock(p); | |
634 | return (error); | |
635 | } | |
636 | (void)close_internal_locked(p, new, nfp, FD_DUP2RESV); | |
637 | #if DIAGNOSTIC | |
638 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
639 | #endif | |
640 | procfdtbl_clearfd(p, new); | |
641 | goto startover; | |
642 | } else { | |
643 | #if DIAGNOSTIC | |
644 | if (fdp->fd_ofiles[new] != NULL) | |
645 | panic("dup2: no ref on fileproc %d", new); | |
646 | #endif | |
647 | procfdtbl_reservefd(p, new); | |
648 | } | |
649 | ||
650 | #if DIAGNOSTIC | |
651 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
652 | #endif | |
653 | ||
654 | } | |
655 | #if DIAGNOSTIC | |
656 | if (fdp->fd_ofiles[new] != 0) | |
657 | panic("dup2: overwriting fd_ofiles with new %d", new); | |
658 | if ((fdp->fd_ofileflags[new] & UF_RESERVED) == 0) | |
659 | panic("dup2: unreserved fileflags with new %d", new); | |
660 | #endif | |
661 | error = finishdup(p, fdp, old, new, 0, retval); | |
662 | fp_drop(p, old, fp, 1); | |
663 | proc_fdunlock(p); | |
664 | ||
665 | return(error); | |
666 | } | |
667 | ||
668 | ||
669 | /* | |
670 | * fcntl | |
671 | * | |
672 | * Description: The file control system call. | |
673 | * | |
674 | * Parameters: p Process performing the fcntl | |
675 | * uap->fd The fd to operate against | |
676 | * uap->cmd The command to perform | |
677 | * uap->arg Pointer to the command argument | |
678 | * retval Pointer to the call return area | |
679 | * | |
680 | * Returns: 0 Success | |
681 | * !0 Errno (see fcntl_nocancel) | |
682 | * | |
683 | * Implicit returns: | |
684 | * *retval (modified) fcntl return value (if any) | |
685 | * | |
686 | * Notes: This system call differs from fcntl_nocancel() in that it | |
687 | * tests for cancellation prior to performing a potentially | |
688 | * blocking operation. | |
689 | */ | |
690 | int | |
691 | fcntl(proc_t p, struct fcntl_args *uap, int32_t *retval) | |
692 | { | |
693 | __pthread_testcancel(1); | |
694 | return(fcntl_nocancel(p, (struct fcntl_nocancel_args *)uap, retval)); | |
695 | } | |
696 | ||
697 | ||
698 | /* | |
699 | * fcntl_nocancel | |
700 | * | |
701 | * Description: A non-cancel-testing file control system call. | |
702 | * | |
703 | * Parameters: p Process performing the fcntl | |
704 | * uap->fd The fd to operate against | |
705 | * uap->cmd The command to perform | |
706 | * uap->arg Pointer to the command argument | |
707 | * retval Pointer to the call return area | |
708 | * | |
709 | * Returns: 0 Success | |
710 | * EINVAL | |
711 | * fp_lookup:EBADF Bad file descriptor | |
712 | * [F_DUPFD] | |
713 | * fdalloc:EMFILE | |
714 | * fdalloc:ENOMEM | |
715 | * finishdup:EBADF | |
716 | * finishdup:ENOMEM | |
717 | * [F_SETOWN] | |
718 | * ESRCH | |
719 | * [F_SETLK] | |
720 | * EBADF | |
721 | * EOVERFLOW | |
722 | * copyin:EFAULT | |
723 | * vnode_getwithref:??? | |
724 | * VNOP_ADVLOCK:??? | |
725 | * msleep:ETIMEDOUT | |
726 | * [F_GETLK] | |
727 | * EBADF | |
728 | * EOVERFLOW | |
729 | * copyin:EFAULT | |
730 | * copyout:EFAULT | |
731 | * vnode_getwithref:??? | |
732 | * VNOP_ADVLOCK:??? | |
733 | * [F_PREALLOCATE] | |
734 | * EBADF | |
735 | * EINVAL | |
736 | * copyin:EFAULT | |
737 | * copyout:EFAULT | |
738 | * vnode_getwithref:??? | |
739 | * VNOP_ALLOCATE:??? | |
740 | * [F_SETSIZE,F_RDADVISE] | |
741 | * EBADF | |
742 | * copyin:EFAULT | |
743 | * vnode_getwithref:??? | |
744 | * [F_RDAHEAD,F_NOCACHE] | |
745 | * EBADF | |
746 | * vnode_getwithref:??? | |
747 | * [???] | |
748 | * | |
749 | * Implicit returns: | |
750 | * *retval (modified) fcntl return value (if any) | |
751 | */ | |
752 | int | |
753 | fcntl_nocancel(proc_t p, struct fcntl_nocancel_args *uap, int32_t *retval) | |
754 | { | |
755 | int fd = uap->fd; | |
756 | struct filedesc *fdp = p->p_fd; | |
757 | struct fileproc *fp; | |
758 | char *pop; | |
759 | struct vnode *vp = NULLVP; /* for AUDIT_ARG() at end */ | |
760 | int i, tmp, error, error2, flg = 0; | |
761 | struct flock fl; | |
762 | struct flocktimeout fltimeout; | |
763 | struct timespec *timeout = NULL; | |
764 | struct vfs_context context; | |
765 | off_t offset; | |
766 | int newmin; | |
767 | daddr64_t lbn, bn; | |
768 | unsigned int fflag; | |
769 | user_addr_t argp; | |
770 | boolean_t is64bit; | |
771 | ||
772 | AUDIT_ARG(fd, uap->fd); | |
773 | AUDIT_ARG(cmd, uap->cmd); | |
774 | ||
775 | proc_fdlock(p); | |
776 | if ( (error = fp_lookup(p, fd, &fp, 1)) ) { | |
777 | proc_fdunlock(p); | |
778 | return(error); | |
779 | } | |
780 | context.vc_thread = current_thread(); | |
781 | context.vc_ucred = fp->f_cred; | |
782 | ||
783 | is64bit = proc_is64bit(p); | |
784 | if (is64bit) { | |
785 | argp = uap->arg; | |
786 | } | |
787 | else { | |
788 | /* | |
789 | * Since the arg parameter is defined as a long but may be | |
790 | * either a long or a pointer we must take care to handle | |
791 | * sign extension issues. Our sys call munger will sign | |
792 | * extend a long when we are called from a 32-bit process. | |
793 | * Since we can never have an address greater than 32-bits | |
794 | * from a 32-bit process we lop off the top 32-bits to avoid | |
795 | * getting the wrong address | |
796 | */ | |
797 | argp = CAST_USER_ADDR_T((uint32_t)uap->arg); | |
798 | } | |
799 | ||
800 | pop = &fdp->fd_ofileflags[fd]; | |
801 | ||
802 | #if CONFIG_MACF | |
803 | error = mac_file_check_fcntl(proc_ucred(p), fp->f_fglob, uap->cmd, | |
804 | uap->arg); | |
805 | if (error) | |
806 | goto out; | |
807 | #endif | |
808 | ||
809 | switch (uap->cmd) { | |
810 | ||
811 | case F_DUPFD: | |
812 | case F_DUPFD_CLOEXEC: | |
813 | if (FP_ISGUARDED(fp, GUARD_DUP)) { | |
814 | error = fp_guard_exception(p, fd, fp, kGUARD_EXC_DUP); | |
815 | goto out; | |
816 | } | |
817 | newmin = CAST_DOWN_EXPLICIT(int, uap->arg); /* arg is an int, so we won't lose bits */ | |
818 | AUDIT_ARG(value32, newmin); | |
819 | if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur || | |
820 | newmin >= maxfiles) { | |
821 | error = EINVAL; | |
822 | goto out; | |
823 | } | |
824 | if ( (error = fdalloc(p, newmin, &i)) ) | |
825 | goto out; | |
826 | error = finishdup(p, fdp, fd, i, | |
827 | uap->cmd == F_DUPFD_CLOEXEC ? UF_EXCLOSE : 0, retval); | |
828 | goto out; | |
829 | ||
830 | case F_GETFD: | |
831 | *retval = (*pop & UF_EXCLOSE)? FD_CLOEXEC : 0; | |
832 | error = 0; | |
833 | goto out; | |
834 | ||
835 | case F_SETFD: | |
836 | AUDIT_ARG(value32, uap->arg); | |
837 | if (uap->arg & FD_CLOEXEC) | |
838 | *pop |= UF_EXCLOSE; | |
839 | else { | |
840 | if (FILEPROC_TYPE(fp) == FTYPE_GUARDED) { | |
841 | error = fp_guard_exception(p, | |
842 | fd, fp, kGUARD_EXC_NOCLOEXEC); | |
843 | goto out; | |
844 | } | |
845 | *pop &= ~UF_EXCLOSE; | |
846 | } | |
847 | error = 0; | |
848 | goto out; | |
849 | ||
850 | case F_GETFL: | |
851 | *retval = OFLAGS(fp->f_flag); | |
852 | error = 0; | |
853 | goto out; | |
854 | ||
855 | case F_SETFL: | |
856 | fp->f_flag &= ~FCNTLFLAGS; | |
857 | tmp = CAST_DOWN_EXPLICIT(int, uap->arg); /* arg is an int, so we won't lose bits */ | |
858 | AUDIT_ARG(value32, tmp); | |
859 | fp->f_flag |= FFLAGS(tmp) & FCNTLFLAGS; | |
860 | tmp = fp->f_flag & FNONBLOCK; | |
861 | error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, &context); | |
862 | if (error) | |
863 | goto out; | |
864 | tmp = fp->f_flag & FASYNC; | |
865 | error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, &context); | |
866 | if (!error) | |
867 | goto out; | |
868 | fp->f_flag &= ~FNONBLOCK; | |
869 | tmp = 0; | |
870 | (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, &context); | |
871 | goto out; | |
872 | ||
873 | case F_GETOWN: | |
874 | if (fp->f_type == DTYPE_SOCKET) { | |
875 | *retval = ((struct socket *)fp->f_data)->so_pgid; | |
876 | error = 0; | |
877 | goto out; | |
878 | } | |
879 | error = fo_ioctl(fp, (int)TIOCGPGRP, (caddr_t)retval, &context); | |
880 | *retval = -*retval; | |
881 | goto out; | |
882 | ||
883 | case F_SETOWN: | |
884 | tmp = CAST_DOWN_EXPLICIT(pid_t, uap->arg); /* arg is an int, so we won't lose bits */ | |
885 | AUDIT_ARG(value32, tmp); | |
886 | if (fp->f_type == DTYPE_SOCKET) { | |
887 | ((struct socket *)fp->f_data)->so_pgid = tmp; | |
888 | error =0; | |
889 | goto out; | |
890 | } | |
891 | if (fp->f_type == DTYPE_PIPE) { | |
892 | error = fo_ioctl(fp, TIOCSPGRP, (caddr_t)&tmp, &context); | |
893 | goto out; | |
894 | } | |
895 | ||
896 | if (tmp <= 0) { | |
897 | tmp = -tmp; | |
898 | } else { | |
899 | proc_t p1 = proc_find(tmp); | |
900 | if (p1 == 0) { | |
901 | error = ESRCH; | |
902 | goto out; | |
903 | } | |
904 | tmp = (int)p1->p_pgrpid; | |
905 | proc_rele(p1); | |
906 | } | |
907 | error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, &context); | |
908 | goto out; | |
909 | ||
910 | case F_SETNOSIGPIPE: | |
911 | tmp = CAST_DOWN_EXPLICIT(int, uap->arg); | |
912 | if (fp->f_type == DTYPE_SOCKET) { | |
913 | #if SOCKETS | |
914 | error = sock_setsockopt((struct socket *)fp->f_data, | |
915 | SOL_SOCKET, SO_NOSIGPIPE, &tmp, sizeof (tmp)); | |
916 | #else | |
917 | error = EINVAL; | |
918 | #endif | |
919 | } else { | |
920 | struct fileglob *fg = fp->f_fglob; | |
921 | ||
922 | lck_mtx_lock_spin(&fg->fg_lock); | |
923 | if (tmp) | |
924 | fg->fg_lflags |= FG_NOSIGPIPE; | |
925 | else | |
926 | fg->fg_lflags &= FG_NOSIGPIPE; | |
927 | lck_mtx_unlock(&fg->fg_lock); | |
928 | error = 0; | |
929 | } | |
930 | goto out; | |
931 | ||
932 | case F_GETNOSIGPIPE: | |
933 | if (fp->f_type == DTYPE_SOCKET) { | |
934 | #if SOCKETS | |
935 | int retsize = sizeof (*retval); | |
936 | error = sock_getsockopt((struct socket *)fp->f_data, | |
937 | SOL_SOCKET, SO_NOSIGPIPE, retval, &retsize); | |
938 | #else | |
939 | error = EINVAL; | |
940 | #endif | |
941 | } else { | |
942 | *retval = (fp->f_fglob->fg_lflags & FG_NOSIGPIPE) ? | |
943 | 1 : 0; | |
944 | error = 0; | |
945 | } | |
946 | goto out; | |
947 | ||
948 | case F_SETCONFINED: | |
949 | /* | |
950 | * If this is the only reference to this fglob in the process | |
951 | * and it's already marked as close-on-fork then mark it as | |
952 | * (immutably) "confined" i.e. any fd that points to it will | |
953 | * forever be close-on-fork, and attempts to use an IPC | |
954 | * mechanism to move the descriptor elsewhere will fail. | |
955 | */ | |
956 | if (CAST_DOWN_EXPLICIT(int, uap->arg)) { | |
957 | struct fileglob *fg = fp->f_fglob; | |
958 | ||
959 | lck_mtx_lock_spin(&fg->fg_lock); | |
960 | if (fg->fg_lflags & FG_CONFINED) | |
961 | error = 0; | |
962 | else if (1 != fg->fg_count) | |
963 | error = EAGAIN; /* go close the dup .. */ | |
964 | else if (UF_FORKCLOSE == (*pop & UF_FORKCLOSE)) { | |
965 | fg->fg_lflags |= FG_CONFINED; | |
966 | error = 0; | |
967 | } else | |
968 | error = EBADF; /* open without O_CLOFORK? */ | |
969 | lck_mtx_unlock(&fg->fg_lock); | |
970 | } else { | |
971 | /* | |
972 | * Other subsystems may have built on the immutability | |
973 | * of FG_CONFINED; clearing it may be tricky. | |
974 | */ | |
975 | error = EPERM; /* immutable */ | |
976 | } | |
977 | goto out; | |
978 | ||
979 | case F_GETCONFINED: | |
980 | *retval = (fp->f_fglob->fg_lflags & FG_CONFINED) ? 1 : 0; | |
981 | error = 0; | |
982 | goto out; | |
983 | ||
984 | case F_SETLKWTIMEOUT: | |
985 | case F_SETLKW: | |
986 | case F_OFD_SETLKWTIMEOUT: | |
987 | case F_OFD_SETLKW: | |
988 | flg |= F_WAIT; | |
989 | /* Fall into F_SETLK */ | |
990 | ||
991 | case F_SETLK: | |
992 | case F_OFD_SETLK: | |
993 | if (fp->f_type != DTYPE_VNODE) { | |
994 | error = EBADF; | |
995 | goto out; | |
996 | } | |
997 | vp = (struct vnode *)fp->f_data; | |
998 | ||
999 | fflag = fp->f_flag; | |
1000 | offset = fp->f_offset; | |
1001 | proc_fdunlock(p); | |
1002 | ||
1003 | /* Copy in the lock structure */ | |
1004 | if (F_SETLKWTIMEOUT == uap->cmd || | |
1005 | F_OFD_SETLKWTIMEOUT == uap->cmd) { | |
1006 | error = copyin(argp, (caddr_t) &fltimeout, sizeof(fltimeout)); | |
1007 | if (error) { | |
1008 | goto outdrop; | |
1009 | } | |
1010 | fl = fltimeout.fl; | |
1011 | timeout = &fltimeout.timeout; | |
1012 | } else { | |
1013 | error = copyin(argp, (caddr_t)&fl, sizeof(fl)); | |
1014 | if (error) { | |
1015 | goto outdrop; | |
1016 | } | |
1017 | } | |
1018 | ||
1019 | /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */ | |
1020 | /* and ending byte for EOVERFLOW in SEEK_SET */ | |
1021 | error = check_file_seek_range(&fl, offset); | |
1022 | if (error) { | |
1023 | goto outdrop; | |
1024 | } | |
1025 | ||
1026 | if ( (error = vnode_getwithref(vp)) ) { | |
1027 | goto outdrop; | |
1028 | } | |
1029 | if (fl.l_whence == SEEK_CUR) | |
1030 | fl.l_start += offset; | |
1031 | ||
1032 | #if CONFIG_MACF | |
1033 | error = mac_file_check_lock(proc_ucred(p), fp->f_fglob, | |
1034 | F_SETLK, &fl); | |
1035 | if (error) { | |
1036 | (void)vnode_put(vp); | |
1037 | goto outdrop; | |
1038 | } | |
1039 | #endif | |
1040 | switch (uap->cmd) { | |
1041 | case F_OFD_SETLK: | |
1042 | case F_OFD_SETLKW: | |
1043 | case F_OFD_SETLKWTIMEOUT: | |
1044 | flg |= F_OFD_LOCK; | |
1045 | switch (fl.l_type) { | |
1046 | case F_RDLCK: | |
1047 | if ((fflag & FREAD) == 0) { | |
1048 | error = EBADF; | |
1049 | break; | |
1050 | } | |
1051 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, | |
1052 | F_SETLK, &fl, flg, &context, timeout); | |
1053 | break; | |
1054 | case F_WRLCK: | |
1055 | if ((fflag & FWRITE) == 0) { | |
1056 | error = EBADF; | |
1057 | break; | |
1058 | } | |
1059 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, | |
1060 | F_SETLK, &fl, flg, &context, timeout); | |
1061 | break; | |
1062 | case F_UNLCK: | |
1063 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, | |
1064 | F_UNLCK, &fl, F_OFD_LOCK, &context, | |
1065 | timeout); | |
1066 | break; | |
1067 | default: | |
1068 | error = EINVAL; | |
1069 | break; | |
1070 | } | |
1071 | if (0 == error && | |
1072 | (F_RDLCK == fl.l_type || F_WRLCK == fl.l_type)) { | |
1073 | struct fileglob *fg = fp->f_fglob; | |
1074 | ||
1075 | /* | |
1076 | * arrange F_UNLCK on last close (once | |
1077 | * set, FG_HAS_OFDLOCK is immutable) | |
1078 | */ | |
1079 | if ((fg->fg_lflags & FG_HAS_OFDLOCK) == 0) { | |
1080 | lck_mtx_lock_spin(&fg->fg_lock); | |
1081 | fg->fg_lflags |= FG_HAS_OFDLOCK; | |
1082 | lck_mtx_unlock(&fg->fg_lock); | |
1083 | } | |
1084 | } | |
1085 | break; | |
1086 | default: | |
1087 | flg |= F_POSIX; | |
1088 | switch (fl.l_type) { | |
1089 | case F_RDLCK: | |
1090 | if ((fflag & FREAD) == 0) { | |
1091 | error = EBADF; | |
1092 | break; | |
1093 | } | |
1094 | // XXX UInt32 unsafe for LP64 kernel | |
1095 | OSBitOrAtomic(P_LADVLOCK, &p->p_ladvflag); | |
1096 | error = VNOP_ADVLOCK(vp, (caddr_t)p, | |
1097 | F_SETLK, &fl, flg, &context, timeout); | |
1098 | break; | |
1099 | case F_WRLCK: | |
1100 | if ((fflag & FWRITE) == 0) { | |
1101 | error = EBADF; | |
1102 | break; | |
1103 | } | |
1104 | // XXX UInt32 unsafe for LP64 kernel | |
1105 | OSBitOrAtomic(P_LADVLOCK, &p->p_ladvflag); | |
1106 | error = VNOP_ADVLOCK(vp, (caddr_t)p, | |
1107 | F_SETLK, &fl, flg, &context, timeout); | |
1108 | break; | |
1109 | case F_UNLCK: | |
1110 | error = VNOP_ADVLOCK(vp, (caddr_t)p, | |
1111 | F_UNLCK, &fl, F_POSIX, &context, timeout); | |
1112 | break; | |
1113 | default: | |
1114 | error = EINVAL; | |
1115 | break; | |
1116 | } | |
1117 | break; | |
1118 | } | |
1119 | (void) vnode_put(vp); | |
1120 | goto outdrop; | |
1121 | ||
1122 | case F_GETLK: | |
1123 | case F_OFD_GETLK: | |
1124 | if (fp->f_type != DTYPE_VNODE) { | |
1125 | error = EBADF; | |
1126 | goto out; | |
1127 | } | |
1128 | vp = (struct vnode *)fp->f_data; | |
1129 | ||
1130 | offset = fp->f_offset; | |
1131 | proc_fdunlock(p); | |
1132 | ||
1133 | /* Copy in the lock structure */ | |
1134 | error = copyin(argp, (caddr_t)&fl, sizeof(fl)); | |
1135 | if (error) | |
1136 | goto outdrop; | |
1137 | ||
1138 | /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */ | |
1139 | /* and ending byte for EOVERFLOW in SEEK_SET */ | |
1140 | error = check_file_seek_range(&fl, offset); | |
1141 | if (error) { | |
1142 | goto outdrop; | |
1143 | } | |
1144 | ||
1145 | if ((fl.l_whence == SEEK_SET) && (fl.l_start < 0)) { | |
1146 | error = EINVAL; | |
1147 | goto outdrop; | |
1148 | } | |
1149 | ||
1150 | switch (fl.l_type) { | |
1151 | case F_RDLCK: | |
1152 | case F_UNLCK: | |
1153 | case F_WRLCK: | |
1154 | break; | |
1155 | default: | |
1156 | error = EINVAL; | |
1157 | goto outdrop; | |
1158 | } | |
1159 | ||
1160 | switch (fl.l_whence) { | |
1161 | case SEEK_CUR: | |
1162 | case SEEK_SET: | |
1163 | case SEEK_END: | |
1164 | break; | |
1165 | default: | |
1166 | error = EINVAL; | |
1167 | goto outdrop; | |
1168 | } | |
1169 | ||
1170 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1171 | if (fl.l_whence == SEEK_CUR) | |
1172 | fl.l_start += offset; | |
1173 | ||
1174 | #if CONFIG_MACF | |
1175 | error = mac_file_check_lock(proc_ucred(p), fp->f_fglob, | |
1176 | uap->cmd, &fl); | |
1177 | if (error == 0) | |
1178 | #endif | |
1179 | switch (uap->cmd) { | |
1180 | case F_OFD_GETLK: | |
1181 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, | |
1182 | F_GETLK, &fl, F_OFD_LOCK, &context, NULL); | |
1183 | break; | |
1184 | case F_OFD_GETLKPID: | |
1185 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, | |
1186 | F_GETLKPID, &fl, F_OFD_LOCK, &context, NULL); | |
1187 | break; | |
1188 | default: | |
1189 | error = VNOP_ADVLOCK(vp, (caddr_t)p, | |
1190 | uap->cmd, &fl, F_POSIX, &context, NULL); | |
1191 | break; | |
1192 | } | |
1193 | ||
1194 | (void)vnode_put(vp); | |
1195 | ||
1196 | if (error == 0) | |
1197 | error = copyout((caddr_t)&fl, argp, sizeof(fl)); | |
1198 | } | |
1199 | goto outdrop; | |
1200 | ||
1201 | case F_PREALLOCATE: { | |
1202 | fstore_t alloc_struct; /* structure for allocate command */ | |
1203 | u_int32_t alloc_flags = 0; | |
1204 | ||
1205 | if (fp->f_type != DTYPE_VNODE) { | |
1206 | error = EBADF; | |
1207 | goto out; | |
1208 | } | |
1209 | ||
1210 | vp = (struct vnode *)fp->f_data; | |
1211 | proc_fdunlock(p); | |
1212 | ||
1213 | /* make sure that we have write permission */ | |
1214 | if ((fp->f_flag & FWRITE) == 0) { | |
1215 | error = EBADF; | |
1216 | goto outdrop; | |
1217 | } | |
1218 | ||
1219 | error = copyin(argp, (caddr_t)&alloc_struct, sizeof(alloc_struct)); | |
1220 | if (error) | |
1221 | goto outdrop; | |
1222 | ||
1223 | /* now set the space allocated to 0 */ | |
1224 | alloc_struct.fst_bytesalloc = 0; | |
1225 | ||
1226 | /* | |
1227 | * Do some simple parameter checking | |
1228 | */ | |
1229 | ||
1230 | /* set up the flags */ | |
1231 | ||
1232 | alloc_flags |= PREALLOCATE; | |
1233 | ||
1234 | if (alloc_struct.fst_flags & F_ALLOCATECONTIG) | |
1235 | alloc_flags |= ALLOCATECONTIG; | |
1236 | ||
1237 | if (alloc_struct.fst_flags & F_ALLOCATEALL) | |
1238 | alloc_flags |= ALLOCATEALL; | |
1239 | ||
1240 | /* | |
1241 | * Do any position mode specific stuff. The only | |
1242 | * position mode supported now is PEOFPOSMODE | |
1243 | */ | |
1244 | ||
1245 | switch (alloc_struct.fst_posmode) { | |
1246 | ||
1247 | case F_PEOFPOSMODE: | |
1248 | if (alloc_struct.fst_offset != 0) { | |
1249 | error = EINVAL; | |
1250 | goto outdrop; | |
1251 | } | |
1252 | ||
1253 | alloc_flags |= ALLOCATEFROMPEOF; | |
1254 | break; | |
1255 | ||
1256 | case F_VOLPOSMODE: | |
1257 | if (alloc_struct.fst_offset <= 0) { | |
1258 | error = EINVAL; | |
1259 | goto outdrop; | |
1260 | } | |
1261 | ||
1262 | alloc_flags |= ALLOCATEFROMVOL; | |
1263 | break; | |
1264 | ||
1265 | default: { | |
1266 | error = EINVAL; | |
1267 | goto outdrop; | |
1268 | } | |
1269 | } | |
1270 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1271 | /* | |
1272 | * call allocate to get the space | |
1273 | */ | |
1274 | error = VNOP_ALLOCATE(vp,alloc_struct.fst_length,alloc_flags, | |
1275 | &alloc_struct.fst_bytesalloc, alloc_struct.fst_offset, | |
1276 | &context); | |
1277 | (void)vnode_put(vp); | |
1278 | ||
1279 | error2 = copyout((caddr_t)&alloc_struct, argp, sizeof(alloc_struct)); | |
1280 | ||
1281 | if (error == 0) | |
1282 | error = error2; | |
1283 | } | |
1284 | goto outdrop; | |
1285 | ||
1286 | } | |
1287 | case F_SETSIZE: | |
1288 | if (fp->f_type != DTYPE_VNODE) { | |
1289 | error = EBADF; | |
1290 | goto out; | |
1291 | } | |
1292 | vp = (struct vnode *)fp->f_data; | |
1293 | proc_fdunlock(p); | |
1294 | ||
1295 | error = copyin(argp, (caddr_t)&offset, sizeof (off_t)); | |
1296 | if (error) | |
1297 | goto outdrop; | |
1298 | AUDIT_ARG(value64, offset); | |
1299 | ||
1300 | error = vnode_getwithref(vp); | |
1301 | if (error) | |
1302 | goto outdrop; | |
1303 | ||
1304 | #if CONFIG_MACF | |
1305 | error = mac_vnode_check_truncate(&context, | |
1306 | fp->f_fglob->fg_cred, vp); | |
1307 | if (error) { | |
1308 | (void)vnode_put(vp); | |
1309 | goto outdrop; | |
1310 | } | |
1311 | #endif | |
1312 | /* | |
1313 | * Make sure that we are root. Growing a file | |
1314 | * without zero filling the data is a security hole | |
1315 | * root would have access anyway so we'll allow it | |
1316 | */ | |
1317 | if (!kauth_cred_issuser(kauth_cred_get())) { | |
1318 | error = EACCES; | |
1319 | } else { | |
1320 | /* | |
1321 | * set the file size | |
1322 | */ | |
1323 | error = vnode_setsize(vp, offset, IO_NOZEROFILL, | |
1324 | &context); | |
1325 | ||
1326 | #if CONFIG_MACF | |
1327 | if (error == 0) | |
1328 | mac_vnode_notify_truncate(&context, fp->f_fglob->fg_cred, vp); | |
1329 | #endif | |
1330 | } | |
1331 | ||
1332 | (void)vnode_put(vp); | |
1333 | goto outdrop; | |
1334 | ||
1335 | case F_RDAHEAD: | |
1336 | if (fp->f_type != DTYPE_VNODE) { | |
1337 | error = EBADF; | |
1338 | goto out; | |
1339 | } | |
1340 | if (uap->arg) | |
1341 | fp->f_fglob->fg_flag &= ~FNORDAHEAD; | |
1342 | else | |
1343 | fp->f_fglob->fg_flag |= FNORDAHEAD; | |
1344 | ||
1345 | goto out; | |
1346 | ||
1347 | case F_NOCACHE: | |
1348 | if (fp->f_type != DTYPE_VNODE) { | |
1349 | error = EBADF; | |
1350 | goto out; | |
1351 | } | |
1352 | if (uap->arg) | |
1353 | fp->f_fglob->fg_flag |= FNOCACHE; | |
1354 | else | |
1355 | fp->f_fglob->fg_flag &= ~FNOCACHE; | |
1356 | ||
1357 | goto out; | |
1358 | ||
1359 | case F_NODIRECT: | |
1360 | if (fp->f_type != DTYPE_VNODE) { | |
1361 | error = EBADF; | |
1362 | goto out; | |
1363 | } | |
1364 | if (uap->arg) | |
1365 | fp->f_fglob->fg_flag |= FNODIRECT; | |
1366 | else | |
1367 | fp->f_fglob->fg_flag &= ~FNODIRECT; | |
1368 | ||
1369 | goto out; | |
1370 | ||
1371 | case F_SINGLE_WRITER: | |
1372 | if (fp->f_type != DTYPE_VNODE) { | |
1373 | error = EBADF; | |
1374 | goto out; | |
1375 | } | |
1376 | if (uap->arg) | |
1377 | fp->f_fglob->fg_flag |= FSINGLE_WRITER; | |
1378 | else | |
1379 | fp->f_fglob->fg_flag &= ~FSINGLE_WRITER; | |
1380 | ||
1381 | goto out; | |
1382 | ||
1383 | case F_GLOBAL_NOCACHE: | |
1384 | if (fp->f_type != DTYPE_VNODE) { | |
1385 | error = EBADF; | |
1386 | goto out; | |
1387 | } | |
1388 | vp = (struct vnode *)fp->f_data; | |
1389 | proc_fdunlock(p); | |
1390 | ||
1391 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1392 | ||
1393 | *retval = vnode_isnocache(vp); | |
1394 | ||
1395 | if (uap->arg) | |
1396 | vnode_setnocache(vp); | |
1397 | else | |
1398 | vnode_clearnocache(vp); | |
1399 | ||
1400 | (void)vnode_put(vp); | |
1401 | } | |
1402 | goto outdrop; | |
1403 | ||
1404 | case F_CHECK_OPENEVT: | |
1405 | if (fp->f_type != DTYPE_VNODE) { | |
1406 | error = EBADF; | |
1407 | goto out; | |
1408 | } | |
1409 | vp = (struct vnode *)fp->f_data; | |
1410 | proc_fdunlock(p); | |
1411 | ||
1412 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1413 | ||
1414 | *retval = vnode_is_openevt(vp); | |
1415 | ||
1416 | if (uap->arg) | |
1417 | vnode_set_openevt(vp); | |
1418 | else | |
1419 | vnode_clear_openevt(vp); | |
1420 | ||
1421 | (void)vnode_put(vp); | |
1422 | } | |
1423 | goto outdrop; | |
1424 | ||
1425 | case F_RDADVISE: { | |
1426 | struct radvisory ra_struct; | |
1427 | ||
1428 | if (fp->f_type != DTYPE_VNODE) { | |
1429 | error = EBADF; | |
1430 | goto out; | |
1431 | } | |
1432 | vp = (struct vnode *)fp->f_data; | |
1433 | proc_fdunlock(p); | |
1434 | ||
1435 | if ( (error = copyin(argp, (caddr_t)&ra_struct, sizeof(ra_struct))) ) | |
1436 | goto outdrop; | |
1437 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1438 | error = VNOP_IOCTL(vp, F_RDADVISE, (caddr_t)&ra_struct, 0, &context); | |
1439 | ||
1440 | (void)vnode_put(vp); | |
1441 | } | |
1442 | goto outdrop; | |
1443 | } | |
1444 | ||
1445 | case F_FLUSH_DATA: | |
1446 | ||
1447 | if (fp->f_type != DTYPE_VNODE) { | |
1448 | error = EBADF; | |
1449 | goto out; | |
1450 | } | |
1451 | vp = (struct vnode *)fp->f_data; | |
1452 | proc_fdunlock(p); | |
1453 | ||
1454 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1455 | error = cluster_push(vp, 0); | |
1456 | ||
1457 | (void)vnode_put(vp); | |
1458 | } | |
1459 | goto outdrop; | |
1460 | ||
1461 | case F_LOG2PHYS: | |
1462 | case F_LOG2PHYS_EXT: { | |
1463 | struct log2phys l2p_struct; /* structure for allocate command */ | |
1464 | int devBlockSize; | |
1465 | ||
1466 | off_t file_offset = 0; | |
1467 | size_t a_size = 0; | |
1468 | size_t run = 0; | |
1469 | ||
1470 | if (uap->cmd == F_LOG2PHYS_EXT) { | |
1471 | error = copyin(argp, (caddr_t)&l2p_struct, sizeof(l2p_struct)); | |
1472 | if (error) | |
1473 | goto out; | |
1474 | file_offset = l2p_struct.l2p_devoffset; | |
1475 | } else { | |
1476 | file_offset = fp->f_offset; | |
1477 | } | |
1478 | if (fp->f_type != DTYPE_VNODE) { | |
1479 | error = EBADF; | |
1480 | goto out; | |
1481 | } | |
1482 | vp = (struct vnode *)fp->f_data; | |
1483 | proc_fdunlock(p); | |
1484 | if ( (error = vnode_getwithref(vp)) ) { | |
1485 | goto outdrop; | |
1486 | } | |
1487 | error = VNOP_OFFTOBLK(vp, file_offset, &lbn); | |
1488 | if (error) { | |
1489 | (void)vnode_put(vp); | |
1490 | goto outdrop; | |
1491 | } | |
1492 | error = VNOP_BLKTOOFF(vp, lbn, &offset); | |
1493 | if (error) { | |
1494 | (void)vnode_put(vp); | |
1495 | goto outdrop; | |
1496 | } | |
1497 | devBlockSize = vfs_devblocksize(vnode_mount(vp)); | |
1498 | if (uap->cmd == F_LOG2PHYS_EXT) { | |
1499 | if (l2p_struct.l2p_contigbytes < 0) { | |
1500 | vnode_put(vp); | |
1501 | error = EINVAL; | |
1502 | goto outdrop; | |
1503 | } | |
1504 | ||
1505 | a_size = MIN((uint64_t)l2p_struct.l2p_contigbytes, SIZE_MAX); | |
1506 | } else { | |
1507 | a_size = devBlockSize; | |
1508 | } | |
1509 | ||
1510 | error = VNOP_BLOCKMAP(vp, offset, a_size, &bn, &run, NULL, 0, &context); | |
1511 | ||
1512 | (void)vnode_put(vp); | |
1513 | ||
1514 | if (!error) { | |
1515 | l2p_struct.l2p_flags = 0; /* for now */ | |
1516 | if (uap->cmd == F_LOG2PHYS_EXT) { | |
1517 | l2p_struct.l2p_contigbytes = run - (file_offset - offset); | |
1518 | } else { | |
1519 | l2p_struct.l2p_contigbytes = 0; /* for now */ | |
1520 | } | |
1521 | ||
1522 | /* | |
1523 | * The block number being -1 suggests that the file offset is not backed | |
1524 | * by any real blocks on-disk. As a result, just let it be passed back up wholesale. | |
1525 | */ | |
1526 | if (bn == -1) { | |
1527 | /* Don't multiply it by the block size */ | |
1528 | l2p_struct.l2p_devoffset = bn; | |
1529 | } | |
1530 | else { | |
1531 | l2p_struct.l2p_devoffset = bn * devBlockSize; | |
1532 | l2p_struct.l2p_devoffset += file_offset - offset; | |
1533 | } | |
1534 | error = copyout((caddr_t)&l2p_struct, argp, sizeof(l2p_struct)); | |
1535 | } | |
1536 | goto outdrop; | |
1537 | } | |
1538 | case F_GETPATH: { | |
1539 | char *pathbufp; | |
1540 | int pathlen; | |
1541 | ||
1542 | if (fp->f_type != DTYPE_VNODE) { | |
1543 | error = EBADF; | |
1544 | goto out; | |
1545 | } | |
1546 | vp = (struct vnode *)fp->f_data; | |
1547 | proc_fdunlock(p); | |
1548 | ||
1549 | pathlen = MAXPATHLEN; | |
1550 | MALLOC(pathbufp, char *, pathlen, M_TEMP, M_WAITOK); | |
1551 | if (pathbufp == NULL) { | |
1552 | error = ENOMEM; | |
1553 | goto outdrop; | |
1554 | } | |
1555 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1556 | error = vn_getpath(vp, pathbufp, &pathlen); | |
1557 | (void)vnode_put(vp); | |
1558 | ||
1559 | if (error == 0) | |
1560 | error = copyout((caddr_t)pathbufp, argp, pathlen); | |
1561 | } | |
1562 | FREE(pathbufp, M_TEMP); | |
1563 | goto outdrop; | |
1564 | } | |
1565 | ||
1566 | case F_PATHPKG_CHECK: { | |
1567 | char *pathbufp; | |
1568 | size_t pathlen; | |
1569 | ||
1570 | if (fp->f_type != DTYPE_VNODE) { | |
1571 | error = EBADF; | |
1572 | goto out; | |
1573 | } | |
1574 | vp = (struct vnode *)fp->f_data; | |
1575 | proc_fdunlock(p); | |
1576 | ||
1577 | pathlen = MAXPATHLEN; | |
1578 | pathbufp = kalloc(MAXPATHLEN); | |
1579 | ||
1580 | if ( (error = copyinstr(argp, pathbufp, MAXPATHLEN, &pathlen)) == 0 ) { | |
1581 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1582 | AUDIT_ARG(text, pathbufp); | |
1583 | error = vn_path_package_check(vp, pathbufp, pathlen, retval); | |
1584 | ||
1585 | (void)vnode_put(vp); | |
1586 | } | |
1587 | } | |
1588 | kfree(pathbufp, MAXPATHLEN); | |
1589 | goto outdrop; | |
1590 | } | |
1591 | ||
1592 | case F_CHKCLEAN: // used by regression tests to see if all dirty pages got cleaned by fsync() | |
1593 | case F_FULLFSYNC: // fsync + flush the journal + DKIOCSYNCHRONIZE | |
1594 | case F_BARRIERFSYNC: // fsync + barrier | |
1595 | case F_FREEZE_FS: // freeze all other fs operations for the fs of this fd | |
1596 | case F_THAW_FS: { // thaw all frozen fs operations for the fs of this fd | |
1597 | if (fp->f_type != DTYPE_VNODE) { | |
1598 | error = EBADF; | |
1599 | goto out; | |
1600 | } | |
1601 | vp = (struct vnode *)fp->f_data; | |
1602 | proc_fdunlock(p); | |
1603 | ||
1604 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
1605 | error = VNOP_IOCTL(vp, uap->cmd, (caddr_t)NULL, 0, &context); | |
1606 | ||
1607 | (void)vnode_put(vp); | |
1608 | } | |
1609 | break; | |
1610 | } | |
1611 | ||
1612 | /* | |
1613 | * SPI (private) for opening a file starting from a dir fd | |
1614 | */ | |
1615 | case F_OPENFROM: { | |
1616 | struct user_fopenfrom fopen; | |
1617 | struct vnode_attr va; | |
1618 | struct nameidata nd; | |
1619 | int cmode; | |
1620 | ||
1621 | /* Check if this isn't a valid file descriptor */ | |
1622 | if ((fp->f_type != DTYPE_VNODE) || | |
1623 | (fp->f_flag & FREAD) == 0) { | |
1624 | error = EBADF; | |
1625 | goto out; | |
1626 | } | |
1627 | vp = (struct vnode *)fp->f_data; | |
1628 | proc_fdunlock(p); | |
1629 | ||
1630 | if (vnode_getwithref(vp)) { | |
1631 | error = ENOENT; | |
1632 | goto outdrop; | |
1633 | } | |
1634 | ||
1635 | /* Only valid for directories */ | |
1636 | if (vp->v_type != VDIR) { | |
1637 | vnode_put(vp); | |
1638 | error = ENOTDIR; | |
1639 | goto outdrop; | |
1640 | } | |
1641 | ||
1642 | /* Get flags, mode and pathname arguments. */ | |
1643 | if (IS_64BIT_PROCESS(p)) { | |
1644 | error = copyin(argp, &fopen, sizeof(fopen)); | |
1645 | } else { | |
1646 | struct user32_fopenfrom fopen32; | |
1647 | ||
1648 | error = copyin(argp, &fopen32, sizeof(fopen32)); | |
1649 | fopen.o_flags = fopen32.o_flags; | |
1650 | fopen.o_mode = fopen32.o_mode; | |
1651 | fopen.o_pathname = CAST_USER_ADDR_T(fopen32.o_pathname); | |
1652 | } | |
1653 | if (error) { | |
1654 | vnode_put(vp); | |
1655 | goto outdrop; | |
1656 | } | |
1657 | AUDIT_ARG(fflags, fopen.o_flags); | |
1658 | AUDIT_ARG(mode, fopen.o_mode); | |
1659 | VATTR_INIT(&va); | |
1660 | /* Mask off all but regular access permissions */ | |
1661 | cmode = ((fopen.o_mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; | |
1662 | VATTR_SET(&va, va_mode, cmode & ACCESSPERMS); | |
1663 | ||
1664 | /* Start the lookup relative to the file descriptor's vnode. */ | |
1665 | NDINIT(&nd, LOOKUP, OP_OPEN, USEDVP | FOLLOW | AUDITVNPATH1, UIO_USERSPACE, | |
1666 | fopen.o_pathname, &context); | |
1667 | nd.ni_dvp = vp; | |
1668 | ||
1669 | error = open1(&context, &nd, fopen.o_flags, &va, | |
1670 | fileproc_alloc_init, NULL, retval); | |
1671 | ||
1672 | vnode_put(vp); | |
1673 | break; | |
1674 | } | |
1675 | /* | |
1676 | * SPI (private) for unlinking a file starting from a dir fd | |
1677 | */ | |
1678 | case F_UNLINKFROM: { | |
1679 | user_addr_t pathname; | |
1680 | ||
1681 | /* Check if this isn't a valid file descriptor */ | |
1682 | if ((fp->f_type != DTYPE_VNODE) || | |
1683 | (fp->f_flag & FREAD) == 0) { | |
1684 | error = EBADF; | |
1685 | goto out; | |
1686 | } | |
1687 | vp = (struct vnode *)fp->f_data; | |
1688 | proc_fdunlock(p); | |
1689 | ||
1690 | if (vnode_getwithref(vp)) { | |
1691 | error = ENOENT; | |
1692 | goto outdrop; | |
1693 | } | |
1694 | ||
1695 | /* Only valid for directories */ | |
1696 | if (vp->v_type != VDIR) { | |
1697 | vnode_put(vp); | |
1698 | error = ENOTDIR; | |
1699 | goto outdrop; | |
1700 | } | |
1701 | ||
1702 | /* Get flags, mode and pathname arguments. */ | |
1703 | if (IS_64BIT_PROCESS(p)) { | |
1704 | pathname = (user_addr_t)argp; | |
1705 | } else { | |
1706 | pathname = CAST_USER_ADDR_T(argp); | |
1707 | } | |
1708 | ||
1709 | /* Start the lookup relative to the file descriptor's vnode. */ | |
1710 | error = unlink1(&context, vp, pathname, UIO_USERSPACE, 0); | |
1711 | ||
1712 | vnode_put(vp); | |
1713 | break; | |
1714 | ||
1715 | } | |
1716 | ||
1717 | case F_ADDSIGS: | |
1718 | case F_ADDFILESIGS: | |
1719 | case F_ADDFILESIGS_FOR_DYLD_SIM: | |
1720 | case F_ADDFILESIGS_RETURN: | |
1721 | { | |
1722 | struct cs_blob *blob = NULL; | |
1723 | struct user_fsignatures fs; | |
1724 | kern_return_t kr; | |
1725 | vm_offset_t kernel_blob_addr; | |
1726 | vm_size_t kernel_blob_size; | |
1727 | int blob_add_flags = 0; | |
1728 | ||
1729 | if (fp->f_type != DTYPE_VNODE) { | |
1730 | error = EBADF; | |
1731 | goto out; | |
1732 | } | |
1733 | vp = (struct vnode *)fp->f_data; | |
1734 | proc_fdunlock(p); | |
1735 | ||
1736 | if (uap->cmd == F_ADDFILESIGS_FOR_DYLD_SIM) { | |
1737 | blob_add_flags |= MAC_VNODE_CHECK_DYLD_SIM; | |
1738 | if ((p->p_csflags & CS_KILL) == 0) { | |
1739 | proc_lock(p); | |
1740 | p->p_csflags |= CS_KILL; | |
1741 | proc_unlock(p); | |
1742 | } | |
1743 | } | |
1744 | ||
1745 | error = vnode_getwithref(vp); | |
1746 | if (error) | |
1747 | goto outdrop; | |
1748 | ||
1749 | if (IS_64BIT_PROCESS(p)) { | |
1750 | error = copyin(argp, &fs, sizeof (fs)); | |
1751 | } else { | |
1752 | struct user32_fsignatures fs32; | |
1753 | ||
1754 | error = copyin(argp, &fs32, sizeof (fs32)); | |
1755 | fs.fs_file_start = fs32.fs_file_start; | |
1756 | fs.fs_blob_start = CAST_USER_ADDR_T(fs32.fs_blob_start); | |
1757 | fs.fs_blob_size = fs32.fs_blob_size; | |
1758 | } | |
1759 | ||
1760 | if (error) { | |
1761 | vnode_put(vp); | |
1762 | goto outdrop; | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | * First check if we have something loaded a this offset | |
1767 | */ | |
1768 | blob = ubc_cs_blob_get(vp, CPU_TYPE_ANY, fs.fs_file_start); | |
1769 | if (blob != NULL) | |
1770 | { | |
1771 | /* If this is for dyld_sim revalidate the blob */ | |
1772 | if (uap->cmd == F_ADDFILESIGS_FOR_DYLD_SIM) { | |
1773 | error = ubc_cs_blob_revalidate(vp, blob, NULL, blob_add_flags); | |
1774 | if (error) { | |
1775 | vnode_put(vp); | |
1776 | goto outdrop; | |
1777 | } | |
1778 | } | |
1779 | ||
1780 | } else { | |
1781 | /* | |
1782 | * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover | |
1783 | * our use cases for the immediate future, but note that at the time of this commit, some | |
1784 | * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB). | |
1785 | * | |
1786 | * We should consider how we can manage this more effectively; the above means that some | |
1787 | * platforms are using megabytes of memory for signing data; it merely hasn't crossed the | |
1788 | * threshold considered ridiculous at the time of this change. | |
1789 | */ | |
1790 | #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL) | |
1791 | if (fs.fs_blob_size > CS_MAX_BLOB_SIZE) { | |
1792 | error = E2BIG; | |
1793 | vnode_put(vp); | |
1794 | goto outdrop; | |
1795 | } | |
1796 | ||
1797 | kernel_blob_size = CAST_DOWN(vm_size_t, fs.fs_blob_size); | |
1798 | kr = ubc_cs_blob_allocate(&kernel_blob_addr, &kernel_blob_size); | |
1799 | if (kr != KERN_SUCCESS) { | |
1800 | error = ENOMEM; | |
1801 | vnode_put(vp); | |
1802 | goto outdrop; | |
1803 | } | |
1804 | ||
1805 | if(uap->cmd == F_ADDSIGS) { | |
1806 | error = copyin(fs.fs_blob_start, | |
1807 | (void *) kernel_blob_addr, | |
1808 | kernel_blob_size); | |
1809 | } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ { | |
1810 | int resid; | |
1811 | ||
1812 | error = vn_rdwr(UIO_READ, | |
1813 | vp, | |
1814 | (caddr_t) kernel_blob_addr, | |
1815 | kernel_blob_size, | |
1816 | fs.fs_file_start + fs.fs_blob_start, | |
1817 | UIO_SYSSPACE, | |
1818 | 0, | |
1819 | kauth_cred_get(), | |
1820 | &resid, | |
1821 | p); | |
1822 | if ((error == 0) && resid) { | |
1823 | /* kernel_blob_size rounded to a page size, but signature may be at end of file */ | |
1824 | memset((void *)(kernel_blob_addr + (kernel_blob_size - resid)), 0x0, resid); | |
1825 | } | |
1826 | } | |
1827 | ||
1828 | if (error) { | |
1829 | ubc_cs_blob_deallocate(kernel_blob_addr, | |
1830 | kernel_blob_size); | |
1831 | vnode_put(vp); | |
1832 | goto outdrop; | |
1833 | } | |
1834 | ||
1835 | blob = NULL; | |
1836 | error = ubc_cs_blob_add(vp, | |
1837 | CPU_TYPE_ANY, /* not for a specific architecture */ | |
1838 | fs.fs_file_start, | |
1839 | &kernel_blob_addr, | |
1840 | kernel_blob_size, | |
1841 | NULL, | |
1842 | blob_add_flags, | |
1843 | &blob); | |
1844 | ||
1845 | /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */ | |
1846 | if (error) { | |
1847 | if (kernel_blob_addr) { | |
1848 | ubc_cs_blob_deallocate(kernel_blob_addr, | |
1849 | kernel_blob_size); | |
1850 | } | |
1851 | vnode_put(vp); | |
1852 | goto outdrop; | |
1853 | } else { | |
1854 | #if CHECK_CS_VALIDATION_BITMAP | |
1855 | ubc_cs_validation_bitmap_allocate( vp ); | |
1856 | #endif | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | if (uap->cmd == F_ADDFILESIGS_RETURN || uap->cmd == F_ADDFILESIGS_FOR_DYLD_SIM) { | |
1861 | /* | |
1862 | * The first element of the structure is a | |
1863 | * off_t that happen to have the same size for | |
1864 | * all archs. Lets overwrite that. | |
1865 | */ | |
1866 | off_t end_offset = 0; | |
1867 | if (blob) | |
1868 | end_offset = blob->csb_end_offset; | |
1869 | error = copyout(&end_offset, argp, sizeof (end_offset)); | |
1870 | } | |
1871 | ||
1872 | (void) vnode_put(vp); | |
1873 | break; | |
1874 | } | |
1875 | case F_GETCODEDIR: | |
1876 | case F_FINDSIGS: { | |
1877 | error = ENOTSUP; | |
1878 | goto out; | |
1879 | } | |
1880 | case F_CHECK_LV: { | |
1881 | struct fileglob *fg; | |
1882 | fchecklv_t lv; | |
1883 | ||
1884 | if (fp->f_type != DTYPE_VNODE) { | |
1885 | error = EBADF; | |
1886 | goto out; | |
1887 | } | |
1888 | fg = fp->f_fglob; | |
1889 | proc_fdunlock(p); | |
1890 | ||
1891 | if (IS_64BIT_PROCESS(p)) { | |
1892 | error = copyin(argp, &lv, sizeof (lv)); | |
1893 | } else { | |
1894 | struct user32_fchecklv lv32; | |
1895 | ||
1896 | error = copyin(argp, &lv32, sizeof (lv32)); | |
1897 | lv.lv_file_start = lv32.lv_file_start; | |
1898 | lv.lv_error_message = CAST_USER_ADDR_T(lv32.lv_error_message); | |
1899 | lv.lv_error_message_size = lv32.lv_error_message; | |
1900 | } | |
1901 | if (error) | |
1902 | goto outdrop; | |
1903 | ||
1904 | #if CONFIG_MACF | |
1905 | error = mac_file_check_library_validation(p, fg, lv.lv_file_start, | |
1906 | lv.lv_error_message, lv.lv_error_message_size); | |
1907 | #endif | |
1908 | ||
1909 | break; | |
1910 | } | |
1911 | #if CONFIG_PROTECT | |
1912 | case F_GETPROTECTIONCLASS: { | |
1913 | if (fp->f_type != DTYPE_VNODE) { | |
1914 | error = EBADF; | |
1915 | goto out; | |
1916 | } | |
1917 | vp = (struct vnode *)fp->f_data; | |
1918 | ||
1919 | proc_fdunlock(p); | |
1920 | ||
1921 | if (vnode_getwithref(vp)) { | |
1922 | error = ENOENT; | |
1923 | goto outdrop; | |
1924 | } | |
1925 | ||
1926 | struct vnode_attr va; | |
1927 | ||
1928 | VATTR_INIT(&va); | |
1929 | VATTR_WANTED(&va, va_dataprotect_class); | |
1930 | error = VNOP_GETATTR(vp, &va, &context); | |
1931 | if (!error) { | |
1932 | if (VATTR_IS_SUPPORTED(&va, va_dataprotect_class)) | |
1933 | *retval = va.va_dataprotect_class; | |
1934 | else | |
1935 | error = ENOTSUP; | |
1936 | } | |
1937 | ||
1938 | vnode_put(vp); | |
1939 | break; | |
1940 | } | |
1941 | ||
1942 | case F_SETPROTECTIONCLASS: { | |
1943 | /* tmp must be a valid PROTECTION_CLASS_* */ | |
1944 | tmp = CAST_DOWN_EXPLICIT(uint32_t, uap->arg); | |
1945 | ||
1946 | if (fp->f_type != DTYPE_VNODE) { | |
1947 | error = EBADF; | |
1948 | goto out; | |
1949 | } | |
1950 | vp = (struct vnode *)fp->f_data; | |
1951 | ||
1952 | proc_fdunlock(p); | |
1953 | ||
1954 | if (vnode_getwithref(vp)) { | |
1955 | error = ENOENT; | |
1956 | goto outdrop; | |
1957 | } | |
1958 | ||
1959 | /* Only go forward if you have write access */ | |
1960 | vfs_context_t ctx = vfs_context_current(); | |
1961 | if(vnode_authorize(vp, NULLVP, (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), ctx) != 0) { | |
1962 | vnode_put(vp); | |
1963 | error = EBADF; | |
1964 | goto outdrop; | |
1965 | } | |
1966 | ||
1967 | struct vnode_attr va; | |
1968 | ||
1969 | VATTR_INIT(&va); | |
1970 | VATTR_SET(&va, va_dataprotect_class, tmp); | |
1971 | ||
1972 | error = VNOP_SETATTR(vp, &va, ctx); | |
1973 | ||
1974 | vnode_put(vp); | |
1975 | break; | |
1976 | } | |
1977 | ||
1978 | case F_TRANSCODEKEY: { | |
1979 | if (fp->f_type != DTYPE_VNODE) { | |
1980 | error = EBADF; | |
1981 | goto out; | |
1982 | } | |
1983 | ||
1984 | vp = (struct vnode *)fp->f_data; | |
1985 | proc_fdunlock(p); | |
1986 | ||
1987 | if (vnode_getwithref(vp)) { | |
1988 | error = ENOENT; | |
1989 | goto outdrop; | |
1990 | } | |
1991 | ||
1992 | cp_key_t k = { | |
1993 | .len = CP_MAX_WRAPPEDKEYSIZE, | |
1994 | }; | |
1995 | ||
1996 | MALLOC(k.key, char *, k.len, M_TEMP, M_WAITOK); | |
1997 | ||
1998 | error = VNOP_IOCTL(vp, F_TRANSCODEKEY, (caddr_t)&k, 1, &context); | |
1999 | ||
2000 | vnode_put(vp); | |
2001 | ||
2002 | if (error == 0) { | |
2003 | error = copyout(k.key, argp, k.len); | |
2004 | *retval = k.len; | |
2005 | } | |
2006 | ||
2007 | FREE(k.key, M_TEMP); | |
2008 | ||
2009 | break; | |
2010 | } | |
2011 | ||
2012 | case F_GETPROTECTIONLEVEL: { | |
2013 | if (fp->f_type != DTYPE_VNODE) { | |
2014 | error = EBADF; | |
2015 | goto out; | |
2016 | } | |
2017 | ||
2018 | vp = (struct vnode*) fp->f_data; | |
2019 | proc_fdunlock (p); | |
2020 | ||
2021 | if (vnode_getwithref(vp)) { | |
2022 | error = ENOENT; | |
2023 | goto outdrop; | |
2024 | } | |
2025 | ||
2026 | error = VNOP_IOCTL(vp, F_GETPROTECTIONLEVEL, (caddr_t)retval, 0, &context); | |
2027 | ||
2028 | vnode_put (vp); | |
2029 | break; | |
2030 | } | |
2031 | ||
2032 | case F_GETDEFAULTPROTLEVEL: { | |
2033 | if (fp->f_type != DTYPE_VNODE) { | |
2034 | error = EBADF; | |
2035 | goto out; | |
2036 | } | |
2037 | ||
2038 | vp = (struct vnode*) fp->f_data; | |
2039 | proc_fdunlock (p); | |
2040 | ||
2041 | if (vnode_getwithref(vp)) { | |
2042 | error = ENOENT; | |
2043 | goto outdrop; | |
2044 | } | |
2045 | ||
2046 | /* | |
2047 | * if cp_get_major_vers fails, error will be set to proper errno | |
2048 | * and cp_version will still be 0. | |
2049 | */ | |
2050 | ||
2051 | error = VNOP_IOCTL(vp, F_GETDEFAULTPROTLEVEL, (caddr_t)retval, 0, &context); | |
2052 | ||
2053 | vnode_put (vp); | |
2054 | break; | |
2055 | } | |
2056 | ||
2057 | #endif /* CONFIG_PROTECT */ | |
2058 | ||
2059 | case F_MOVEDATAEXTENTS: { | |
2060 | struct fileproc *fp2 = NULL; | |
2061 | struct vnode *src_vp = NULLVP; | |
2062 | struct vnode *dst_vp = NULLVP; | |
2063 | /* We need to grab the 2nd FD out of the argments before moving on. */ | |
2064 | int fd2 = CAST_DOWN_EXPLICIT(int32_t, uap->arg); | |
2065 | ||
2066 | error = priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS, 0); | |
2067 | if (error) | |
2068 | goto out; | |
2069 | ||
2070 | if (fp->f_type != DTYPE_VNODE) { | |
2071 | error = EBADF; | |
2072 | goto out; | |
2073 | } | |
2074 | ||
2075 | /* For now, special case HFS+ only, since this is SPI. */ | |
2076 | src_vp = (struct vnode *)fp->f_data; | |
2077 | if (src_vp->v_tag != VT_HFS) { | |
2078 | error = EINVAL; | |
2079 | goto out; | |
2080 | } | |
2081 | ||
2082 | /* | |
2083 | * Get the references before we start acquiring iocounts on the vnodes, | |
2084 | * while we still hold the proc fd lock | |
2085 | */ | |
2086 | if ( (error = fp_lookup(p, fd2, &fp2, 1)) ) { | |
2087 | error = EBADF; | |
2088 | goto out; | |
2089 | } | |
2090 | if (fp2->f_type != DTYPE_VNODE) { | |
2091 | fp_drop(p, fd2, fp2, 1); | |
2092 | error = EBADF; | |
2093 | goto out; | |
2094 | } | |
2095 | dst_vp = (struct vnode *)fp2->f_data; | |
2096 | if (dst_vp->v_tag != VT_HFS) { | |
2097 | fp_drop(p, fd2, fp2, 1); | |
2098 | error = EINVAL; | |
2099 | goto out; | |
2100 | } | |
2101 | ||
2102 | #if CONFIG_MACF | |
2103 | /* Re-do MAC checks against the new FD, pass in a fake argument */ | |
2104 | error = mac_file_check_fcntl(proc_ucred(p), fp2->f_fglob, uap->cmd, 0); | |
2105 | if (error) { | |
2106 | fp_drop(p, fd2, fp2, 1); | |
2107 | goto out; | |
2108 | } | |
2109 | #endif | |
2110 | /* Audit the 2nd FD */ | |
2111 | AUDIT_ARG(fd, fd2); | |
2112 | ||
2113 | proc_fdunlock(p); | |
2114 | ||
2115 | if (vnode_getwithref(src_vp)) { | |
2116 | fp_drop(p, fd2, fp2, 0); | |
2117 | error = ENOENT; | |
2118 | goto outdrop; | |
2119 | } | |
2120 | if (vnode_getwithref(dst_vp)) { | |
2121 | vnode_put (src_vp); | |
2122 | fp_drop(p, fd2, fp2, 0); | |
2123 | error = ENOENT; | |
2124 | goto outdrop; | |
2125 | } | |
2126 | ||
2127 | /* | |
2128 | * Basic asserts; validate they are not the same and that | |
2129 | * both live on the same filesystem. | |
2130 | */ | |
2131 | if (dst_vp == src_vp) { | |
2132 | vnode_put (src_vp); | |
2133 | vnode_put (dst_vp); | |
2134 | fp_drop (p, fd2, fp2, 0); | |
2135 | error = EINVAL; | |
2136 | goto outdrop; | |
2137 | } | |
2138 | ||
2139 | if (dst_vp->v_mount != src_vp->v_mount) { | |
2140 | vnode_put (src_vp); | |
2141 | vnode_put (dst_vp); | |
2142 | fp_drop (p, fd2, fp2, 0); | |
2143 | error = EXDEV; | |
2144 | goto outdrop; | |
2145 | } | |
2146 | ||
2147 | /* Now we have a legit pair of FDs. Go to work */ | |
2148 | ||
2149 | /* Now check for write access to the target files */ | |
2150 | if(vnode_authorize(src_vp, NULLVP, | |
2151 | (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), &context) != 0) { | |
2152 | vnode_put(src_vp); | |
2153 | vnode_put(dst_vp); | |
2154 | fp_drop(p, fd2, fp2, 0); | |
2155 | error = EBADF; | |
2156 | goto outdrop; | |
2157 | } | |
2158 | ||
2159 | if(vnode_authorize(dst_vp, NULLVP, | |
2160 | (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), &context) != 0) { | |
2161 | vnode_put(src_vp); | |
2162 | vnode_put(dst_vp); | |
2163 | fp_drop(p, fd2, fp2, 0); | |
2164 | error = EBADF; | |
2165 | goto outdrop; | |
2166 | } | |
2167 | ||
2168 | /* Verify that both vps point to files and not directories */ | |
2169 | if ( !vnode_isreg(src_vp) || !vnode_isreg(dst_vp)) { | |
2170 | error = EINVAL; | |
2171 | vnode_put (src_vp); | |
2172 | vnode_put (dst_vp); | |
2173 | fp_drop (p, fd2, fp2, 0); | |
2174 | goto outdrop; | |
2175 | } | |
2176 | ||
2177 | /* | |
2178 | * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE. | |
2179 | * We'll pass in our special bit indicating that the new behavior is expected | |
2180 | */ | |
2181 | ||
2182 | error = VNOP_EXCHANGE(src_vp, dst_vp, FSOPT_EXCHANGE_DATA_ONLY, &context); | |
2183 | ||
2184 | vnode_put (src_vp); | |
2185 | vnode_put (dst_vp); | |
2186 | fp_drop(p, fd2, fp2, 0); | |
2187 | break; | |
2188 | } | |
2189 | ||
2190 | /* | |
2191 | * SPI for making a file compressed. | |
2192 | */ | |
2193 | case F_MAKECOMPRESSED: { | |
2194 | uint32_t gcounter = CAST_DOWN_EXPLICIT(uint32_t, uap->arg); | |
2195 | ||
2196 | if (fp->f_type != DTYPE_VNODE) { | |
2197 | error = EBADF; | |
2198 | goto out; | |
2199 | } | |
2200 | ||
2201 | vp = (struct vnode*) fp->f_data; | |
2202 | proc_fdunlock (p); | |
2203 | ||
2204 | /* get the vnode */ | |
2205 | if (vnode_getwithref(vp)) { | |
2206 | error = ENOENT; | |
2207 | goto outdrop; | |
2208 | } | |
2209 | ||
2210 | /* Is it a file? */ | |
2211 | if ((vnode_isreg(vp) == 0) && (vnode_islnk(vp) == 0)) { | |
2212 | vnode_put(vp); | |
2213 | error = EBADF; | |
2214 | goto outdrop; | |
2215 | } | |
2216 | ||
2217 | /* invoke ioctl to pass off to FS */ | |
2218 | /* Only go forward if you have write access */ | |
2219 | vfs_context_t ctx = vfs_context_current(); | |
2220 | if(vnode_authorize(vp, NULLVP, (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), ctx) != 0) { | |
2221 | vnode_put(vp); | |
2222 | error = EBADF; | |
2223 | goto outdrop; | |
2224 | } | |
2225 | ||
2226 | error = VNOP_IOCTL(vp, uap->cmd, (caddr_t)&gcounter, 0, &context); | |
2227 | ||
2228 | vnode_put (vp); | |
2229 | break; | |
2230 | } | |
2231 | ||
2232 | /* | |
2233 | * SPI (private) for indicating to a filesystem that subsequent writes to | |
2234 | * the open FD will written to the Fastflow. | |
2235 | */ | |
2236 | case F_SET_GREEDY_MODE: | |
2237 | /* intentionally drop through to the same handler as F_SETSTATIC. | |
2238 | * both fcntls should pass the argument and their selector into VNOP_IOCTL. | |
2239 | */ | |
2240 | ||
2241 | /* | |
2242 | * SPI (private) for indicating to a filesystem that subsequent writes to | |
2243 | * the open FD will represent static content. | |
2244 | */ | |
2245 | case F_SETSTATICCONTENT: { | |
2246 | caddr_t ioctl_arg = NULL; | |
2247 | ||
2248 | if (uap->arg) { | |
2249 | ioctl_arg = (caddr_t) 1; | |
2250 | } | |
2251 | ||
2252 | if (fp->f_type != DTYPE_VNODE) { | |
2253 | error = EBADF; | |
2254 | goto out; | |
2255 | } | |
2256 | vp = (struct vnode *)fp->f_data; | |
2257 | proc_fdunlock(p); | |
2258 | ||
2259 | error = vnode_getwithref(vp); | |
2260 | if (error) { | |
2261 | error = ENOENT; | |
2262 | goto outdrop; | |
2263 | } | |
2264 | ||
2265 | /* Only go forward if you have write access */ | |
2266 | vfs_context_t ctx = vfs_context_current(); | |
2267 | if(vnode_authorize(vp, NULLVP, (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), ctx) != 0) { | |
2268 | vnode_put(vp); | |
2269 | error = EBADF; | |
2270 | goto outdrop; | |
2271 | } | |
2272 | ||
2273 | error = VNOP_IOCTL(vp, uap->cmd, ioctl_arg, 0, &context); | |
2274 | (void)vnode_put(vp); | |
2275 | ||
2276 | break; | |
2277 | } | |
2278 | ||
2279 | /* | |
2280 | * SPI (private) for indicating to the lower level storage driver that the | |
2281 | * subsequent writes should be of a particular IO type (burst, greedy, static), | |
2282 | * or other flavors that may be necessary. | |
2283 | */ | |
2284 | case F_SETIOTYPE: { | |
2285 | caddr_t param_ptr; | |
2286 | uint32_t param; | |
2287 | ||
2288 | if (uap->arg) { | |
2289 | /* extract 32 bits of flags from userland */ | |
2290 | param_ptr = (caddr_t) uap->arg; | |
2291 | param = (uint32_t) param_ptr; | |
2292 | } | |
2293 | else { | |
2294 | /* If no argument is specified, error out */ | |
2295 | error = EINVAL; | |
2296 | goto out; | |
2297 | } | |
2298 | ||
2299 | /* | |
2300 | * Validate the different types of flags that can be specified: | |
2301 | * all of them are mutually exclusive for now. | |
2302 | */ | |
2303 | switch (param) { | |
2304 | case F_IOTYPE_ISOCHRONOUS: | |
2305 | break; | |
2306 | ||
2307 | default: | |
2308 | error = EINVAL; | |
2309 | goto out; | |
2310 | } | |
2311 | ||
2312 | ||
2313 | if (fp->f_type != DTYPE_VNODE) { | |
2314 | error = EBADF; | |
2315 | goto out; | |
2316 | } | |
2317 | vp = (struct vnode *)fp->f_data; | |
2318 | proc_fdunlock(p); | |
2319 | ||
2320 | error = vnode_getwithref(vp); | |
2321 | if (error) { | |
2322 | error = ENOENT; | |
2323 | goto outdrop; | |
2324 | } | |
2325 | ||
2326 | /* Only go forward if you have write access */ | |
2327 | vfs_context_t ctx = vfs_context_current(); | |
2328 | if(vnode_authorize(vp, NULLVP, (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), ctx) != 0) { | |
2329 | vnode_put(vp); | |
2330 | error = EBADF; | |
2331 | goto outdrop; | |
2332 | } | |
2333 | ||
2334 | error = VNOP_IOCTL(vp, uap->cmd, param_ptr, 0, &context); | |
2335 | (void)vnode_put(vp); | |
2336 | ||
2337 | break; | |
2338 | } | |
2339 | ||
2340 | /* | |
2341 | * Set the vnode pointed to by 'fd' | |
2342 | * and tag it as the (potentially future) backing store | |
2343 | * for another filesystem | |
2344 | */ | |
2345 | case F_SETBACKINGSTORE: { | |
2346 | if (fp->f_type != DTYPE_VNODE) { | |
2347 | error = EBADF; | |
2348 | goto out; | |
2349 | } | |
2350 | ||
2351 | vp = (struct vnode *)fp->f_data; | |
2352 | ||
2353 | if (vp->v_tag != VT_HFS) { | |
2354 | error = EINVAL; | |
2355 | goto out; | |
2356 | } | |
2357 | proc_fdunlock(p); | |
2358 | ||
2359 | if (vnode_getwithref(vp)) { | |
2360 | error = ENOENT; | |
2361 | goto outdrop; | |
2362 | } | |
2363 | ||
2364 | /* only proceed if you have write access */ | |
2365 | vfs_context_t ctx = vfs_context_current(); | |
2366 | if(vnode_authorize(vp, NULLVP, (KAUTH_VNODE_ACCESS | KAUTH_VNODE_WRITE_DATA), ctx) != 0) { | |
2367 | vnode_put(vp); | |
2368 | error = EBADF; | |
2369 | goto outdrop; | |
2370 | } | |
2371 | ||
2372 | ||
2373 | /* If arg != 0, set, otherwise unset */ | |
2374 | if (uap->arg) { | |
2375 | error = VNOP_IOCTL (vp, uap->cmd, (caddr_t)1, 0, &context); | |
2376 | } | |
2377 | else { | |
2378 | error = VNOP_IOCTL (vp, uap->cmd, (caddr_t)NULL, 0, &context); | |
2379 | } | |
2380 | ||
2381 | vnode_put(vp); | |
2382 | break; | |
2383 | } | |
2384 | ||
2385 | /* | |
2386 | * like F_GETPATH, but special semantics for | |
2387 | * the mobile time machine handler. | |
2388 | */ | |
2389 | case F_GETPATH_MTMINFO: { | |
2390 | char *pathbufp; | |
2391 | int pathlen; | |
2392 | ||
2393 | if (fp->f_type != DTYPE_VNODE) { | |
2394 | error = EBADF; | |
2395 | goto out; | |
2396 | } | |
2397 | vp = (struct vnode *)fp->f_data; | |
2398 | proc_fdunlock(p); | |
2399 | ||
2400 | pathlen = MAXPATHLEN; | |
2401 | MALLOC(pathbufp, char *, pathlen, M_TEMP, M_WAITOK); | |
2402 | if (pathbufp == NULL) { | |
2403 | error = ENOMEM; | |
2404 | goto outdrop; | |
2405 | } | |
2406 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
2407 | int backingstore = 0; | |
2408 | ||
2409 | /* Check for error from vn_getpath before moving on */ | |
2410 | if ((error = vn_getpath(vp, pathbufp, &pathlen)) == 0) { | |
2411 | if (vp->v_tag == VT_HFS) { | |
2412 | error = VNOP_IOCTL (vp, uap->cmd, (caddr_t) &backingstore, 0, &context); | |
2413 | } | |
2414 | (void)vnode_put(vp); | |
2415 | ||
2416 | if (error == 0) { | |
2417 | error = copyout((caddr_t)pathbufp, argp, pathlen); | |
2418 | } | |
2419 | if (error == 0) { | |
2420 | /* | |
2421 | * If the copyout was successful, now check to ensure | |
2422 | * that this vnode is not a BACKINGSTORE vnode. mtmd | |
2423 | * wants the path regardless. | |
2424 | */ | |
2425 | if (backingstore) { | |
2426 | error = EBUSY; | |
2427 | } | |
2428 | } | |
2429 | } else | |
2430 | (void)vnode_put(vp); | |
2431 | } | |
2432 | FREE(pathbufp, M_TEMP); | |
2433 | goto outdrop; | |
2434 | } | |
2435 | ||
2436 | #if DEBUG || DEVELOPMENT | |
2437 | case F_RECYCLE: | |
2438 | if (fp->f_type != DTYPE_VNODE) { | |
2439 | error = EBADF; | |
2440 | goto out; | |
2441 | } | |
2442 | vp = (struct vnode *)fp->f_data; | |
2443 | proc_fdunlock(p); | |
2444 | ||
2445 | vnode_recycle(vp); | |
2446 | break; | |
2447 | #endif | |
2448 | ||
2449 | default: | |
2450 | /* | |
2451 | * This is an fcntl() that we d not recognize at this level; | |
2452 | * if this is a vnode, we send it down into the VNOP_IOCTL | |
2453 | * for this vnode; this can include special devices, and will | |
2454 | * effectively overload fcntl() to send ioctl()'s. | |
2455 | */ | |
2456 | if((uap->cmd & IOC_VOID) && (uap->cmd & IOC_INOUT)){ | |
2457 | error = EINVAL; | |
2458 | goto out; | |
2459 | } | |
2460 | ||
2461 | /* Catch any now-invalid fcntl() selectors */ | |
2462 | switch (uap->cmd) { | |
2463 | case F_MARKDEPENDENCY: | |
2464 | error = EINVAL; | |
2465 | goto out; | |
2466 | default: | |
2467 | break; | |
2468 | } | |
2469 | ||
2470 | if (fp->f_type != DTYPE_VNODE) { | |
2471 | error = EBADF; | |
2472 | goto out; | |
2473 | } | |
2474 | vp = (struct vnode *)fp->f_data; | |
2475 | proc_fdunlock(p); | |
2476 | ||
2477 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
2478 | #define STK_PARAMS 128 | |
2479 | char stkbuf[STK_PARAMS]; | |
2480 | unsigned int size; | |
2481 | caddr_t data, memp; | |
2482 | /* | |
2483 | * For this to work properly, we have to copy in the | |
2484 | * ioctl() cmd argument if there is one; we must also | |
2485 | * check that a command parameter, if present, does | |
2486 | * not exceed the maximum command length dictated by | |
2487 | * the number of bits we have available in the command | |
2488 | * to represent a structure length. Finally, we have | |
2489 | * to copy the results back out, if it is that type of | |
2490 | * ioctl(). | |
2491 | */ | |
2492 | size = IOCPARM_LEN(uap->cmd); | |
2493 | if (size > IOCPARM_MAX) { | |
2494 | (void)vnode_put(vp); | |
2495 | error = EINVAL; | |
2496 | break; | |
2497 | } | |
2498 | ||
2499 | memp = NULL; | |
2500 | if (size > sizeof (stkbuf)) { | |
2501 | if ((memp = (caddr_t)kalloc(size)) == 0) { | |
2502 | (void)vnode_put(vp); | |
2503 | error = ENOMEM; | |
2504 | goto outdrop; | |
2505 | } | |
2506 | data = memp; | |
2507 | } else { | |
2508 | data = &stkbuf[0]; | |
2509 | } | |
2510 | ||
2511 | if (uap->cmd & IOC_IN) { | |
2512 | if (size) { | |
2513 | /* structure */ | |
2514 | error = copyin(argp, data, size); | |
2515 | if (error) { | |
2516 | (void)vnode_put(vp); | |
2517 | if (memp) | |
2518 | kfree(memp, size); | |
2519 | goto outdrop; | |
2520 | } | |
2521 | ||
2522 | /* Bzero the section beyond that which was needed */ | |
2523 | if (size <= sizeof(stkbuf)) { | |
2524 | bzero ( (((uint8_t*)data) + size), (sizeof(stkbuf) - size)); | |
2525 | } | |
2526 | } else { | |
2527 | /* int */ | |
2528 | if (is64bit) { | |
2529 | *(user_addr_t *)data = argp; | |
2530 | } else { | |
2531 | *(uint32_t *)data = (uint32_t)argp; | |
2532 | } | |
2533 | }; | |
2534 | } else if ((uap->cmd & IOC_OUT) && size) { | |
2535 | /* | |
2536 | * Zero the buffer so the user always | |
2537 | * gets back something deterministic. | |
2538 | */ | |
2539 | bzero(data, size); | |
2540 | } else if (uap->cmd & IOC_VOID) { | |
2541 | if (is64bit) { | |
2542 | *(user_addr_t *)data = argp; | |
2543 | } else { | |
2544 | *(uint32_t *)data = (uint32_t)argp; | |
2545 | } | |
2546 | } | |
2547 | ||
2548 | error = VNOP_IOCTL(vp, uap->cmd, CAST_DOWN(caddr_t, data), 0, &context); | |
2549 | ||
2550 | (void)vnode_put(vp); | |
2551 | ||
2552 | /* Copy any output data to user */ | |
2553 | if (error == 0 && (uap->cmd & IOC_OUT) && size) | |
2554 | error = copyout(data, argp, size); | |
2555 | if (memp) | |
2556 | kfree(memp, size); | |
2557 | } | |
2558 | break; | |
2559 | } | |
2560 | ||
2561 | outdrop: | |
2562 | AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1); | |
2563 | fp_drop(p, fd, fp, 0); | |
2564 | return(error); | |
2565 | out: | |
2566 | fp_drop(p, fd, fp, 1); | |
2567 | proc_fdunlock(p); | |
2568 | return(error); | |
2569 | } | |
2570 | ||
2571 | ||
2572 | /* | |
2573 | * finishdup | |
2574 | * | |
2575 | * Description: Common code for dup, dup2, and fcntl(F_DUPFD). | |
2576 | * | |
2577 | * Parameters: p Process performing the dup | |
2578 | * old The fd to dup | |
2579 | * new The fd to dup it to | |
2580 | * fd_flags Flags to augment the new fd | |
2581 | * retval Pointer to the call return area | |
2582 | * | |
2583 | * Returns: 0 Success | |
2584 | * EBADF | |
2585 | * ENOMEM | |
2586 | * | |
2587 | * Implicit returns: | |
2588 | * *retval (modified) The new descriptor | |
2589 | * | |
2590 | * Locks: Assumes proc_fdlock for process pointing to fdp is held by | |
2591 | * the caller | |
2592 | * | |
2593 | * Notes: This function may drop and reacquire this lock; it is unsafe | |
2594 | * for a caller to assume that other state protected by the lock | |
2595 | * has not been subsequently changed out from under it. | |
2596 | */ | |
2597 | int | |
2598 | finishdup(proc_t p, | |
2599 | struct filedesc *fdp, int old, int new, int fd_flags, int32_t *retval) | |
2600 | { | |
2601 | struct fileproc *nfp; | |
2602 | struct fileproc *ofp; | |
2603 | #if CONFIG_MACF | |
2604 | int error; | |
2605 | #endif | |
2606 | ||
2607 | #if DIAGNOSTIC | |
2608 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
2609 | #endif | |
2610 | if ((ofp = fdp->fd_ofiles[old]) == NULL || | |
2611 | (fdp->fd_ofileflags[old] & UF_RESERVED)) { | |
2612 | fdrelse(p, new); | |
2613 | return (EBADF); | |
2614 | } | |
2615 | fg_ref(ofp); | |
2616 | ||
2617 | #if CONFIG_MACF | |
2618 | error = mac_file_check_dup(proc_ucred(p), ofp->f_fglob, new); | |
2619 | if (error) { | |
2620 | fg_drop(ofp); | |
2621 | fdrelse(p, new); | |
2622 | return (error); | |
2623 | } | |
2624 | #endif | |
2625 | ||
2626 | proc_fdunlock(p); | |
2627 | ||
2628 | nfp = fileproc_alloc_init(NULL); | |
2629 | ||
2630 | proc_fdlock(p); | |
2631 | ||
2632 | if (nfp == NULL) { | |
2633 | fg_drop(ofp); | |
2634 | fdrelse(p, new); | |
2635 | return (ENOMEM); | |
2636 | } | |
2637 | ||
2638 | nfp->f_fglob = ofp->f_fglob; | |
2639 | ||
2640 | #if DIAGNOSTIC | |
2641 | if (fdp->fd_ofiles[new] != 0) | |
2642 | panic("finishdup: overwriting fd_ofiles with new %d", new); | |
2643 | if ((fdp->fd_ofileflags[new] & UF_RESERVED) == 0) | |
2644 | panic("finishdup: unreserved fileflags with new %d", new); | |
2645 | #endif | |
2646 | ||
2647 | if (new > fdp->fd_lastfile) | |
2648 | fdp->fd_lastfile = new; | |
2649 | *fdflags(p, new) |= fd_flags; | |
2650 | procfdtbl_releasefd(p, new, nfp); | |
2651 | *retval = new; | |
2652 | return (0); | |
2653 | } | |
2654 | ||
2655 | ||
2656 | /* | |
2657 | * close | |
2658 | * | |
2659 | * Description: The implementation of the close(2) system call | |
2660 | * | |
2661 | * Parameters: p Process in whose per process file table | |
2662 | * the close is to occur | |
2663 | * uap->fd fd to be closed | |
2664 | * retval <unused> | |
2665 | * | |
2666 | * Returns: 0 Success | |
2667 | * fp_lookup:EBADF Bad file descriptor | |
2668 | * fp_guard_exception:??? Guarded file descriptor | |
2669 | * close_internal:EBADF | |
2670 | * close_internal:??? Anything returnable by a per-fileops | |
2671 | * close function | |
2672 | */ | |
2673 | int | |
2674 | close(proc_t p, struct close_args *uap, int32_t *retval) | |
2675 | { | |
2676 | __pthread_testcancel(1); | |
2677 | return(close_nocancel(p, (struct close_nocancel_args *)uap, retval)); | |
2678 | } | |
2679 | ||
2680 | ||
2681 | int | |
2682 | close_nocancel(proc_t p, struct close_nocancel_args *uap, __unused int32_t *retval) | |
2683 | { | |
2684 | struct fileproc *fp; | |
2685 | int fd = uap->fd; | |
2686 | int error; | |
2687 | ||
2688 | AUDIT_SYSCLOSE(p, fd); | |
2689 | ||
2690 | proc_fdlock(p); | |
2691 | ||
2692 | if ( (error = fp_lookup(p,fd,&fp, 1)) ) { | |
2693 | proc_fdunlock(p); | |
2694 | return(error); | |
2695 | } | |
2696 | ||
2697 | if (FP_ISGUARDED(fp, GUARD_CLOSE)) { | |
2698 | error = fp_guard_exception(p, fd, fp, kGUARD_EXC_CLOSE); | |
2699 | (void) fp_drop(p, fd, fp, 1); | |
2700 | proc_fdunlock(p); | |
2701 | return (error); | |
2702 | } | |
2703 | ||
2704 | error = close_internal_locked(p, fd, fp, 0); | |
2705 | ||
2706 | proc_fdunlock(p); | |
2707 | ||
2708 | return (error); | |
2709 | } | |
2710 | ||
2711 | ||
2712 | /* | |
2713 | * close_internal_locked | |
2714 | * | |
2715 | * Close a file descriptor. | |
2716 | * | |
2717 | * Parameters: p Process in whose per process file table | |
2718 | * the close is to occur | |
2719 | * fd fd to be closed | |
2720 | * fp fileproc associated with the fd | |
2721 | * | |
2722 | * Returns: 0 Success | |
2723 | * EBADF fd already in close wait state | |
2724 | * closef_locked:??? Anything returnable by a per-fileops | |
2725 | * close function | |
2726 | * | |
2727 | * Locks: Assumes proc_fdlock for process is held by the caller and returns | |
2728 | * with lock held | |
2729 | * | |
2730 | * Notes: This function may drop and reacquire this lock; it is unsafe | |
2731 | * for a caller to assume that other state protected by the lock | |
2732 | * has not been subsequently changed out from under it. | |
2733 | */ | |
2734 | int | |
2735 | close_internal_locked(proc_t p, int fd, struct fileproc *fp, int flags) | |
2736 | { | |
2737 | struct filedesc *fdp = p->p_fd; | |
2738 | int error =0; | |
2739 | int resvfd = flags & FD_DUP2RESV; | |
2740 | ||
2741 | ||
2742 | #if DIAGNOSTIC | |
2743 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
2744 | #endif | |
2745 | ||
2746 | /* Keep people from using the filedesc while we are closing it */ | |
2747 | procfdtbl_markclosefd(p, fd); | |
2748 | ||
2749 | ||
2750 | if ((fp->f_flags & FP_CLOSING) == FP_CLOSING) { | |
2751 | panic("close_internal_locked: being called on already closing fd"); | |
2752 | } | |
2753 | ||
2754 | ||
2755 | #if DIAGNOSTIC | |
2756 | if ((fdp->fd_ofileflags[fd] & UF_RESERVED) == 0) | |
2757 | panic("close_internal: unreserved fileflags with fd %d", fd); | |
2758 | #endif | |
2759 | ||
2760 | fp->f_flags |= FP_CLOSING; | |
2761 | ||
2762 | if ( (fp->f_flags & FP_AIOISSUED) || kauth_authorize_fileop_has_listeners() ) { | |
2763 | ||
2764 | proc_fdunlock(p); | |
2765 | ||
2766 | if ( (fp->f_type == DTYPE_VNODE) && kauth_authorize_fileop_has_listeners() ) { | |
2767 | /* | |
2768 | * call out to allow 3rd party notification of close. | |
2769 | * Ignore result of kauth_authorize_fileop call. | |
2770 | */ | |
2771 | if (vnode_getwithref((vnode_t)fp->f_data) == 0) { | |
2772 | u_int fileop_flags = 0; | |
2773 | if ((fp->f_flags & FP_WRITTEN) != 0) | |
2774 | fileop_flags |= KAUTH_FILEOP_CLOSE_MODIFIED; | |
2775 | kauth_authorize_fileop(fp->f_fglob->fg_cred, KAUTH_FILEOP_CLOSE, | |
2776 | (uintptr_t)fp->f_data, (uintptr_t)fileop_flags); | |
2777 | vnode_put((vnode_t)fp->f_data); | |
2778 | } | |
2779 | } | |
2780 | if (fp->f_flags & FP_AIOISSUED) | |
2781 | /* | |
2782 | * cancel all async IO requests that can be cancelled. | |
2783 | */ | |
2784 | _aio_close( p, fd ); | |
2785 | ||
2786 | proc_fdlock(p); | |
2787 | } | |
2788 | ||
2789 | if (fd < fdp->fd_knlistsize) | |
2790 | knote_fdclose(p, fd, FALSE); | |
2791 | ||
2792 | if (fp->f_flags & FP_WAITEVENT) | |
2793 | (void)waitevent_close(p, fp); | |
2794 | ||
2795 | fileproc_drain(p, fp); | |
2796 | ||
2797 | if (resvfd == 0) { | |
2798 | _fdrelse(p, fd); | |
2799 | } else { | |
2800 | procfdtbl_reservefd(p, fd); | |
2801 | } | |
2802 | ||
2803 | if (ENTR_SHOULDTRACE && fp->f_type == DTYPE_SOCKET) | |
2804 | KERNEL_ENERGYTRACE(kEnTrActKernSocket, DBG_FUNC_END, | |
2805 | fd, 0, (int64_t)VM_KERNEL_ADDRPERM(fp->f_data)); | |
2806 | ||
2807 | error = closef_locked(fp, fp->f_fglob, p); | |
2808 | if ((fp->f_flags & FP_WAITCLOSE) == FP_WAITCLOSE) | |
2809 | wakeup(&fp->f_flags); | |
2810 | fp->f_flags &= ~(FP_WAITCLOSE | FP_CLOSING); | |
2811 | ||
2812 | proc_fdunlock(p); | |
2813 | ||
2814 | fileproc_free(fp); | |
2815 | ||
2816 | proc_fdlock(p); | |
2817 | ||
2818 | #if DIAGNOSTIC | |
2819 | if (resvfd != 0) { | |
2820 | if ((fdp->fd_ofileflags[fd] & UF_RESERVED) == 0) | |
2821 | panic("close with reserved fd returns with freed fd:%d: proc: %p", fd, p); | |
2822 | } | |
2823 | #endif | |
2824 | ||
2825 | return(error); | |
2826 | } | |
2827 | ||
2828 | ||
2829 | /* | |
2830 | * fstat1 | |
2831 | * | |
2832 | * Description: Return status information about a file descriptor. | |
2833 | * | |
2834 | * Parameters: p The process doing the fstat | |
2835 | * fd The fd to stat | |
2836 | * ub The user stat buffer | |
2837 | * xsecurity The user extended security | |
2838 | * buffer, or 0 if none | |
2839 | * xsecurity_size The size of xsecurity, or 0 | |
2840 | * if no xsecurity | |
2841 | * isstat64 Flag to indicate 64 bit version | |
2842 | * for inode size, etc. | |
2843 | * | |
2844 | * Returns: 0 Success | |
2845 | * EBADF | |
2846 | * EFAULT | |
2847 | * fp_lookup:EBADF Bad file descriptor | |
2848 | * vnode_getwithref:??? | |
2849 | * copyout:EFAULT | |
2850 | * vnode_getwithref:??? | |
2851 | * vn_stat:??? | |
2852 | * soo_stat:??? | |
2853 | * pipe_stat:??? | |
2854 | * pshm_stat:??? | |
2855 | * kqueue_stat:??? | |
2856 | * | |
2857 | * Notes: Internal implementation for all other fstat() related | |
2858 | * functions | |
2859 | * | |
2860 | * XXX switch on node type is bogus; need a stat in struct | |
2861 | * XXX fileops instead. | |
2862 | */ | |
2863 | static int | |
2864 | fstat1(proc_t p, int fd, user_addr_t ub, user_addr_t xsecurity, user_addr_t xsecurity_size, int isstat64) | |
2865 | { | |
2866 | struct fileproc *fp; | |
2867 | union { | |
2868 | struct stat sb; | |
2869 | struct stat64 sb64; | |
2870 | } source; | |
2871 | union { | |
2872 | struct user64_stat user64_sb; | |
2873 | struct user32_stat user32_sb; | |
2874 | struct user64_stat64 user64_sb64; | |
2875 | struct user32_stat64 user32_sb64; | |
2876 | } dest; | |
2877 | int error, my_size; | |
2878 | file_type_t type; | |
2879 | caddr_t data; | |
2880 | kauth_filesec_t fsec; | |
2881 | user_size_t xsecurity_bufsize; | |
2882 | vfs_context_t ctx = vfs_context_current(); | |
2883 | void * sbptr; | |
2884 | ||
2885 | ||
2886 | AUDIT_ARG(fd, fd); | |
2887 | ||
2888 | if ((error = fp_lookup(p, fd, &fp, 0)) != 0) { | |
2889 | return(error); | |
2890 | } | |
2891 | type = fp->f_type; | |
2892 | data = fp->f_data; | |
2893 | fsec = KAUTH_FILESEC_NONE; | |
2894 | ||
2895 | sbptr = (void *)&source; | |
2896 | ||
2897 | switch (type) { | |
2898 | ||
2899 | case DTYPE_VNODE: | |
2900 | if ((error = vnode_getwithref((vnode_t)data)) == 0) { | |
2901 | /* | |
2902 | * If the caller has the file open, and is not | |
2903 | * requesting extended security information, we are | |
2904 | * going to let them get the basic stat information. | |
2905 | */ | |
2906 | if (xsecurity == USER_ADDR_NULL) { | |
2907 | error = vn_stat_noauth((vnode_t)data, sbptr, NULL, isstat64, ctx, | |
2908 | fp->f_fglob->fg_cred); | |
2909 | } else { | |
2910 | error = vn_stat((vnode_t)data, sbptr, &fsec, isstat64, ctx); | |
2911 | } | |
2912 | ||
2913 | AUDIT_ARG(vnpath, (struct vnode *)data, ARG_VNODE1); | |
2914 | (void)vnode_put((vnode_t)data); | |
2915 | } | |
2916 | break; | |
2917 | ||
2918 | #if SOCKETS | |
2919 | case DTYPE_SOCKET: | |
2920 | error = soo_stat((struct socket *)data, sbptr, isstat64); | |
2921 | break; | |
2922 | #endif /* SOCKETS */ | |
2923 | ||
2924 | case DTYPE_PIPE: | |
2925 | error = pipe_stat((void *)data, sbptr, isstat64); | |
2926 | break; | |
2927 | ||
2928 | case DTYPE_PSXSHM: | |
2929 | error = pshm_stat((void *)data, sbptr, isstat64); | |
2930 | break; | |
2931 | ||
2932 | case DTYPE_KQUEUE: | |
2933 | error = kqueue_stat((void *)data, sbptr, isstat64, p); | |
2934 | break; | |
2935 | ||
2936 | default: | |
2937 | error = EBADF; | |
2938 | goto out; | |
2939 | } | |
2940 | if (error == 0) { | |
2941 | caddr_t sbp; | |
2942 | ||
2943 | if (isstat64 != 0) { | |
2944 | source.sb64.st_lspare = 0; | |
2945 | source.sb64.st_qspare[0] = 0LL; | |
2946 | source.sb64.st_qspare[1] = 0LL; | |
2947 | ||
2948 | if (IS_64BIT_PROCESS(current_proc())) { | |
2949 | munge_user64_stat64(&source.sb64, &dest.user64_sb64); | |
2950 | my_size = sizeof(dest.user64_sb64); | |
2951 | sbp = (caddr_t)&dest.user64_sb64; | |
2952 | } else { | |
2953 | munge_user32_stat64(&source.sb64, &dest.user32_sb64); | |
2954 | my_size = sizeof(dest.user32_sb64); | |
2955 | sbp = (caddr_t)&dest.user32_sb64; | |
2956 | } | |
2957 | } else { | |
2958 | source.sb.st_lspare = 0; | |
2959 | source.sb.st_qspare[0] = 0LL; | |
2960 | source.sb.st_qspare[1] = 0LL; | |
2961 | if (IS_64BIT_PROCESS(current_proc())) { | |
2962 | munge_user64_stat(&source.sb, &dest.user64_sb); | |
2963 | my_size = sizeof(dest.user64_sb); | |
2964 | sbp = (caddr_t)&dest.user64_sb; | |
2965 | } else { | |
2966 | munge_user32_stat(&source.sb, &dest.user32_sb); | |
2967 | my_size = sizeof(dest.user32_sb); | |
2968 | sbp = (caddr_t)&dest.user32_sb; | |
2969 | } | |
2970 | } | |
2971 | ||
2972 | error = copyout(sbp, ub, my_size); | |
2973 | } | |
2974 | ||
2975 | /* caller wants extended security information? */ | |
2976 | if (xsecurity != USER_ADDR_NULL) { | |
2977 | ||
2978 | /* did we get any? */ | |
2979 | if (fsec == KAUTH_FILESEC_NONE) { | |
2980 | if (susize(xsecurity_size, 0) != 0) { | |
2981 | error = EFAULT; | |
2982 | goto out; | |
2983 | } | |
2984 | } else { | |
2985 | /* find the user buffer size */ | |
2986 | xsecurity_bufsize = fusize(xsecurity_size); | |
2987 | ||
2988 | /* copy out the actual data size */ | |
2989 | if (susize(xsecurity_size, KAUTH_FILESEC_COPYSIZE(fsec)) != 0) { | |
2990 | error = EFAULT; | |
2991 | goto out; | |
2992 | } | |
2993 | ||
2994 | /* if the caller supplied enough room, copy out to it */ | |
2995 | if (xsecurity_bufsize >= KAUTH_FILESEC_COPYSIZE(fsec)) | |
2996 | error = copyout(fsec, xsecurity, KAUTH_FILESEC_COPYSIZE(fsec)); | |
2997 | } | |
2998 | } | |
2999 | out: | |
3000 | fp_drop(p, fd, fp, 0); | |
3001 | if (fsec != NULL) | |
3002 | kauth_filesec_free(fsec); | |
3003 | return (error); | |
3004 | } | |
3005 | ||
3006 | ||
3007 | /* | |
3008 | * fstat_extended | |
3009 | * | |
3010 | * Description: Extended version of fstat supporting returning extended | |
3011 | * security information | |
3012 | * | |
3013 | * Parameters: p The process doing the fstat | |
3014 | * uap->fd The fd to stat | |
3015 | * uap->ub The user stat buffer | |
3016 | * uap->xsecurity The user extended security | |
3017 | * buffer, or 0 if none | |
3018 | * uap->xsecurity_size The size of xsecurity, or 0 | |
3019 | * | |
3020 | * Returns: 0 Success | |
3021 | * !0 Errno (see fstat1) | |
3022 | */ | |
3023 | int | |
3024 | fstat_extended(proc_t p, struct fstat_extended_args *uap, __unused int32_t *retval) | |
3025 | { | |
3026 | return(fstat1(p, uap->fd, uap->ub, uap->xsecurity, uap->xsecurity_size, 0)); | |
3027 | } | |
3028 | ||
3029 | ||
3030 | /* | |
3031 | * fstat | |
3032 | * | |
3033 | * Description: Get file status for the file associated with fd | |
3034 | * | |
3035 | * Parameters: p The process doing the fstat | |
3036 | * uap->fd The fd to stat | |
3037 | * uap->ub The user stat buffer | |
3038 | * | |
3039 | * Returns: 0 Success | |
3040 | * !0 Errno (see fstat1) | |
3041 | */ | |
3042 | int | |
3043 | fstat(proc_t p, struct fstat_args *uap, __unused int32_t *retval) | |
3044 | { | |
3045 | return(fstat1(p, uap->fd, uap->ub, 0, 0, 0)); | |
3046 | } | |
3047 | ||
3048 | ||
3049 | /* | |
3050 | * fstat64_extended | |
3051 | * | |
3052 | * Description: Extended version of fstat64 supporting returning extended | |
3053 | * security information | |
3054 | * | |
3055 | * Parameters: p The process doing the fstat | |
3056 | * uap->fd The fd to stat | |
3057 | * uap->ub The user stat buffer | |
3058 | * uap->xsecurity The user extended security | |
3059 | * buffer, or 0 if none | |
3060 | * uap->xsecurity_size The size of xsecurity, or 0 | |
3061 | * | |
3062 | * Returns: 0 Success | |
3063 | * !0 Errno (see fstat1) | |
3064 | */ | |
3065 | int | |
3066 | fstat64_extended(proc_t p, struct fstat64_extended_args *uap, __unused int32_t *retval) | |
3067 | { | |
3068 | return(fstat1(p, uap->fd, uap->ub, uap->xsecurity, uap->xsecurity_size, 1)); | |
3069 | } | |
3070 | ||
3071 | ||
3072 | /* | |
3073 | * fstat64 | |
3074 | * | |
3075 | * Description: Get 64 bit version of the file status for the file associated | |
3076 | * with fd | |
3077 | * | |
3078 | * Parameters: p The process doing the fstat | |
3079 | * uap->fd The fd to stat | |
3080 | * uap->ub The user stat buffer | |
3081 | * | |
3082 | * Returns: 0 Success | |
3083 | * !0 Errno (see fstat1) | |
3084 | */ | |
3085 | int | |
3086 | fstat64(proc_t p, struct fstat64_args *uap, __unused int32_t *retval) | |
3087 | { | |
3088 | return(fstat1(p, uap->fd, uap->ub, 0, 0, 1)); | |
3089 | } | |
3090 | ||
3091 | ||
3092 | /* | |
3093 | * fpathconf | |
3094 | * | |
3095 | * Description: Return pathconf information about a file descriptor. | |
3096 | * | |
3097 | * Parameters: p Process making the request | |
3098 | * uap->fd fd to get information about | |
3099 | * uap->name Name of information desired | |
3100 | * retval Pointer to the call return area | |
3101 | * | |
3102 | * Returns: 0 Success | |
3103 | * EINVAL | |
3104 | * fp_lookup:EBADF Bad file descriptor | |
3105 | * vnode_getwithref:??? | |
3106 | * vn_pathconf:??? | |
3107 | * | |
3108 | * Implicit returns: | |
3109 | * *retval (modified) Returned information (numeric) | |
3110 | */ | |
3111 | int | |
3112 | fpathconf(proc_t p, struct fpathconf_args *uap, int32_t *retval) | |
3113 | { | |
3114 | int fd = uap->fd; | |
3115 | struct fileproc *fp; | |
3116 | struct vnode *vp; | |
3117 | int error = 0; | |
3118 | file_type_t type; | |
3119 | caddr_t data; | |
3120 | ||
3121 | ||
3122 | AUDIT_ARG(fd, uap->fd); | |
3123 | if ( (error = fp_lookup(p, fd, &fp, 0)) ) | |
3124 | return(error); | |
3125 | type = fp->f_type; | |
3126 | data = fp->f_data; | |
3127 | ||
3128 | switch (type) { | |
3129 | ||
3130 | case DTYPE_SOCKET: | |
3131 | if (uap->name != _PC_PIPE_BUF) { | |
3132 | error = EINVAL; | |
3133 | goto out; | |
3134 | } | |
3135 | *retval = PIPE_BUF; | |
3136 | error = 0; | |
3137 | goto out; | |
3138 | ||
3139 | case DTYPE_PIPE: | |
3140 | if (uap->name != _PC_PIPE_BUF) { | |
3141 | error = EINVAL; | |
3142 | goto out; | |
3143 | } | |
3144 | *retval = PIPE_BUF; | |
3145 | error = 0; | |
3146 | goto out; | |
3147 | ||
3148 | case DTYPE_VNODE: | |
3149 | vp = (struct vnode *)data; | |
3150 | ||
3151 | if ( (error = vnode_getwithref(vp)) == 0) { | |
3152 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); | |
3153 | ||
3154 | error = vn_pathconf(vp, uap->name, retval, vfs_context_current()); | |
3155 | ||
3156 | (void)vnode_put(vp); | |
3157 | } | |
3158 | goto out; | |
3159 | ||
3160 | default: | |
3161 | error = EINVAL; | |
3162 | goto out; | |
3163 | ||
3164 | } | |
3165 | /*NOTREACHED*/ | |
3166 | out: | |
3167 | fp_drop(p, fd, fp, 0); | |
3168 | return(error); | |
3169 | } | |
3170 | ||
3171 | /* | |
3172 | * Statistics counter for the number of times a process calling fdalloc() | |
3173 | * has resulted in an expansion of the per process open file table. | |
3174 | * | |
3175 | * XXX This would likely be of more use if it were per process | |
3176 | */ | |
3177 | int fdexpand; | |
3178 | ||
3179 | ||
3180 | /* | |
3181 | * fdalloc | |
3182 | * | |
3183 | * Description: Allocate a file descriptor for the process. | |
3184 | * | |
3185 | * Parameters: p Process to allocate the fd in | |
3186 | * want The fd we would prefer to get | |
3187 | * result Pointer to fd we got | |
3188 | * | |
3189 | * Returns: 0 Success | |
3190 | * EMFILE | |
3191 | * ENOMEM | |
3192 | * | |
3193 | * Implicit returns: | |
3194 | * *result (modified) The fd which was allocated | |
3195 | */ | |
3196 | int | |
3197 | fdalloc(proc_t p, int want, int *result) | |
3198 | { | |
3199 | struct filedesc *fdp = p->p_fd; | |
3200 | int i; | |
3201 | int lim, last, numfiles, oldnfiles; | |
3202 | struct fileproc **newofiles, **ofiles; | |
3203 | char *newofileflags; | |
3204 | ||
3205 | /* | |
3206 | * Search for a free descriptor starting at the higher | |
3207 | * of want or fd_freefile. If that fails, consider | |
3208 | * expanding the ofile array. | |
3209 | */ | |
3210 | #if DIAGNOSTIC | |
3211 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
3212 | #endif | |
3213 | ||
3214 | lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); | |
3215 | for (;;) { | |
3216 | last = min(fdp->fd_nfiles, lim); | |
3217 | if ((i = want) < fdp->fd_freefile) | |
3218 | i = fdp->fd_freefile; | |
3219 | for (; i < last; i++) { | |
3220 | if (fdp->fd_ofiles[i] == NULL && !(fdp->fd_ofileflags[i] & UF_RESERVED)) { | |
3221 | procfdtbl_reservefd(p, i); | |
3222 | if (i > fdp->fd_lastfile) | |
3223 | fdp->fd_lastfile = i; | |
3224 | if (want <= fdp->fd_freefile) | |
3225 | fdp->fd_freefile = i; | |
3226 | *result = i; | |
3227 | return (0); | |
3228 | } | |
3229 | } | |
3230 | ||
3231 | /* | |
3232 | * No space in current array. Expand? | |
3233 | */ | |
3234 | if (fdp->fd_nfiles >= lim) | |
3235 | return (EMFILE); | |
3236 | if (fdp->fd_nfiles < NDEXTENT) | |
3237 | numfiles = NDEXTENT; | |
3238 | else | |
3239 | numfiles = 2 * fdp->fd_nfiles; | |
3240 | /* Enforce lim */ | |
3241 | if (numfiles > lim) | |
3242 | numfiles = lim; | |
3243 | proc_fdunlock(p); | |
3244 | MALLOC_ZONE(newofiles, struct fileproc **, | |
3245 | numfiles * OFILESIZE, M_OFILETABL, M_WAITOK); | |
3246 | proc_fdlock(p); | |
3247 | if (newofiles == NULL) { | |
3248 | return (ENOMEM); | |
3249 | } | |
3250 | if (fdp->fd_nfiles >= numfiles) { | |
3251 | FREE_ZONE(newofiles, numfiles * OFILESIZE, M_OFILETABL); | |
3252 | continue; | |
3253 | } | |
3254 | newofileflags = (char *) &newofiles[numfiles]; | |
3255 | /* | |
3256 | * Copy the existing ofile and ofileflags arrays | |
3257 | * and zero the new portion of each array. | |
3258 | */ | |
3259 | oldnfiles = fdp->fd_nfiles; | |
3260 | (void) memcpy(newofiles, fdp->fd_ofiles, | |
3261 | oldnfiles * sizeof(*fdp->fd_ofiles)); | |
3262 | (void) memset(&newofiles[oldnfiles], 0, | |
3263 | (numfiles - oldnfiles) * sizeof(*fdp->fd_ofiles)); | |
3264 | ||
3265 | (void) memcpy(newofileflags, fdp->fd_ofileflags, | |
3266 | oldnfiles * sizeof(*fdp->fd_ofileflags)); | |
3267 | (void) memset(&newofileflags[oldnfiles], 0, | |
3268 | (numfiles - oldnfiles) * | |
3269 | sizeof(*fdp->fd_ofileflags)); | |
3270 | ofiles = fdp->fd_ofiles; | |
3271 | fdp->fd_ofiles = newofiles; | |
3272 | fdp->fd_ofileflags = newofileflags; | |
3273 | fdp->fd_nfiles = numfiles; | |
3274 | FREE_ZONE(ofiles, oldnfiles * OFILESIZE, M_OFILETABL); | |
3275 | fdexpand++; | |
3276 | } | |
3277 | } | |
3278 | ||
3279 | ||
3280 | /* | |
3281 | * fdavail | |
3282 | * | |
3283 | * Description: Check to see whether n user file descriptors are available | |
3284 | * to the process p. | |
3285 | * | |
3286 | * Parameters: p Process to check in | |
3287 | * n The number of fd's desired | |
3288 | * | |
3289 | * Returns: 0 No | |
3290 | * 1 Yes | |
3291 | * | |
3292 | * Locks: Assumes proc_fdlock for process is held by the caller | |
3293 | * | |
3294 | * Notes: The answer only remains valid so long as the proc_fdlock is | |
3295 | * held by the caller. | |
3296 | */ | |
3297 | int | |
3298 | fdavail(proc_t p, int n) | |
3299 | { | |
3300 | struct filedesc *fdp = p->p_fd; | |
3301 | struct fileproc **fpp; | |
3302 | char *flags; | |
3303 | int i, lim; | |
3304 | ||
3305 | lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); | |
3306 | if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0) | |
3307 | return (1); | |
3308 | fpp = &fdp->fd_ofiles[fdp->fd_freefile]; | |
3309 | flags = &fdp->fd_ofileflags[fdp->fd_freefile]; | |
3310 | for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++, flags++) | |
3311 | if (*fpp == NULL && !(*flags & UF_RESERVED) && --n <= 0) | |
3312 | return (1); | |
3313 | return (0); | |
3314 | } | |
3315 | ||
3316 | ||
3317 | /* | |
3318 | * fdrelse | |
3319 | * | |
3320 | * Description: Legacy KPI wrapper function for _fdrelse | |
3321 | * | |
3322 | * Parameters: p Process in which fd lives | |
3323 | * fd fd to free | |
3324 | * | |
3325 | * Returns: void | |
3326 | * | |
3327 | * Locks: Assumes proc_fdlock for process is held by the caller | |
3328 | */ | |
3329 | void | |
3330 | fdrelse(proc_t p, int fd) | |
3331 | { | |
3332 | _fdrelse(p, fd); | |
3333 | } | |
3334 | ||
3335 | ||
3336 | /* | |
3337 | * fdgetf_noref | |
3338 | * | |
3339 | * Description: Get the fileproc pointer for the given fd from the per process | |
3340 | * open file table without taking an explicit reference on it. | |
3341 | * | |
3342 | * Parameters: p Process containing fd | |
3343 | * fd fd to obtain fileproc for | |
3344 | * resultfp Pointer to pointer return area | |
3345 | * | |
3346 | * Returns: 0 Success | |
3347 | * EBADF | |
3348 | * | |
3349 | * Implicit returns: | |
3350 | * *resultfp (modified) Pointer to fileproc pointer | |
3351 | * | |
3352 | * Locks: Assumes proc_fdlock for process is held by the caller | |
3353 | * | |
3354 | * Notes: Because there is no reference explicitly taken, the returned | |
3355 | * fileproc pointer is only valid so long as the proc_fdlock | |
3356 | * remains held by the caller. | |
3357 | */ | |
3358 | int | |
3359 | fdgetf_noref(proc_t p, int fd, struct fileproc **resultfp) | |
3360 | { | |
3361 | struct filedesc *fdp = p->p_fd; | |
3362 | struct fileproc *fp; | |
3363 | ||
3364 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3365 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3366 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3367 | return (EBADF); | |
3368 | } | |
3369 | if (resultfp) | |
3370 | *resultfp = fp; | |
3371 | return (0); | |
3372 | } | |
3373 | ||
3374 | ||
3375 | /* | |
3376 | * fp_getfvp | |
3377 | * | |
3378 | * Description: Get fileproc and vnode pointer for a given fd from the per | |
3379 | * process open file table of the specified process, and if | |
3380 | * successful, increment the f_iocount | |
3381 | * | |
3382 | * Parameters: p Process in which fd lives | |
3383 | * fd fd to get information for | |
3384 | * resultfp Pointer to result fileproc | |
3385 | * pointer area, or 0 if none | |
3386 | * resultvp Pointer to result vnode pointer | |
3387 | * area, or 0 if none | |
3388 | * | |
3389 | * Returns: 0 Success | |
3390 | * EBADF Bad file descriptor | |
3391 | * ENOTSUP fd does not refer to a vnode | |
3392 | * | |
3393 | * Implicit returns: | |
3394 | * *resultfp (modified) Fileproc pointer | |
3395 | * *resultvp (modified) vnode pointer | |
3396 | * | |
3397 | * Notes: The resultfp and resultvp fields are optional, and may be | |
3398 | * independently specified as NULL to skip returning information | |
3399 | * | |
3400 | * Locks: Internally takes and releases proc_fdlock | |
3401 | */ | |
3402 | int | |
3403 | fp_getfvp(proc_t p, int fd, struct fileproc **resultfp, struct vnode **resultvp) | |
3404 | { | |
3405 | struct filedesc *fdp = p->p_fd; | |
3406 | struct fileproc *fp; | |
3407 | ||
3408 | proc_fdlock_spin(p); | |
3409 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3410 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3411 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3412 | proc_fdunlock(p); | |
3413 | return (EBADF); | |
3414 | } | |
3415 | if (fp->f_type != DTYPE_VNODE) { | |
3416 | proc_fdunlock(p); | |
3417 | return(ENOTSUP); | |
3418 | } | |
3419 | fp->f_iocount++; | |
3420 | ||
3421 | if (resultfp) | |
3422 | *resultfp = fp; | |
3423 | if (resultvp) | |
3424 | *resultvp = (struct vnode *)fp->f_data; | |
3425 | proc_fdunlock(p); | |
3426 | ||
3427 | return (0); | |
3428 | } | |
3429 | ||
3430 | ||
3431 | /* | |
3432 | * fp_getfvpandvid | |
3433 | * | |
3434 | * Description: Get fileproc, vnode pointer, and vid for a given fd from the | |
3435 | * per process open file table of the specified process, and if | |
3436 | * successful, increment the f_iocount | |
3437 | * | |
3438 | * Parameters: p Process in which fd lives | |
3439 | * fd fd to get information for | |
3440 | * resultfp Pointer to result fileproc | |
3441 | * pointer area, or 0 if none | |
3442 | * resultvp Pointer to result vnode pointer | |
3443 | * area, or 0 if none | |
3444 | * vidp Pointer to resuld vid area | |
3445 | * | |
3446 | * Returns: 0 Success | |
3447 | * EBADF Bad file descriptor | |
3448 | * ENOTSUP fd does not refer to a vnode | |
3449 | * | |
3450 | * Implicit returns: | |
3451 | * *resultfp (modified) Fileproc pointer | |
3452 | * *resultvp (modified) vnode pointer | |
3453 | * *vidp vid value | |
3454 | * | |
3455 | * Notes: The resultfp and resultvp fields are optional, and may be | |
3456 | * independently specified as NULL to skip returning information | |
3457 | * | |
3458 | * Locks: Internally takes and releases proc_fdlock | |
3459 | */ | |
3460 | int | |
3461 | fp_getfvpandvid(proc_t p, int fd, struct fileproc **resultfp, | |
3462 | struct vnode **resultvp, uint32_t *vidp) | |
3463 | { | |
3464 | struct filedesc *fdp = p->p_fd; | |
3465 | struct fileproc *fp; | |
3466 | ||
3467 | proc_fdlock_spin(p); | |
3468 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3469 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3470 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3471 | proc_fdunlock(p); | |
3472 | return (EBADF); | |
3473 | } | |
3474 | if (fp->f_type != DTYPE_VNODE) { | |
3475 | proc_fdunlock(p); | |
3476 | return(ENOTSUP); | |
3477 | } | |
3478 | fp->f_iocount++; | |
3479 | ||
3480 | if (resultfp) | |
3481 | *resultfp = fp; | |
3482 | if (resultvp) | |
3483 | *resultvp = (struct vnode *)fp->f_data; | |
3484 | if (vidp) | |
3485 | *vidp = (uint32_t)vnode_vid((struct vnode *)fp->f_data); | |
3486 | proc_fdunlock(p); | |
3487 | ||
3488 | return (0); | |
3489 | } | |
3490 | ||
3491 | ||
3492 | /* | |
3493 | * fp_getfsock | |
3494 | * | |
3495 | * Description: Get fileproc and socket pointer for a given fd from the | |
3496 | * per process open file table of the specified process, and if | |
3497 | * successful, increment the f_iocount | |
3498 | * | |
3499 | * Parameters: p Process in which fd lives | |
3500 | * fd fd to get information for | |
3501 | * resultfp Pointer to result fileproc | |
3502 | * pointer area, or 0 if none | |
3503 | * results Pointer to result socket | |
3504 | * pointer area, or 0 if none | |
3505 | * | |
3506 | * Returns: EBADF The file descriptor is invalid | |
3507 | * EOPNOTSUPP The file descriptor is not a socket | |
3508 | * 0 Success | |
3509 | * | |
3510 | * Implicit returns: | |
3511 | * *resultfp (modified) Fileproc pointer | |
3512 | * *results (modified) socket pointer | |
3513 | * | |
3514 | * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only | |
3515 | * ever called from accept1(). | |
3516 | */ | |
3517 | int | |
3518 | fp_getfsock(proc_t p, int fd, struct fileproc **resultfp, | |
3519 | struct socket **results) | |
3520 | { | |
3521 | struct filedesc *fdp = p->p_fd; | |
3522 | struct fileproc *fp; | |
3523 | ||
3524 | proc_fdlock_spin(p); | |
3525 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3526 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3527 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3528 | proc_fdunlock(p); | |
3529 | return (EBADF); | |
3530 | } | |
3531 | if (fp->f_type != DTYPE_SOCKET) { | |
3532 | proc_fdunlock(p); | |
3533 | return(EOPNOTSUPP); | |
3534 | } | |
3535 | fp->f_iocount++; | |
3536 | ||
3537 | if (resultfp) | |
3538 | *resultfp = fp; | |
3539 | if (results) | |
3540 | *results = (struct socket *)fp->f_data; | |
3541 | proc_fdunlock(p); | |
3542 | ||
3543 | return (0); | |
3544 | } | |
3545 | ||
3546 | ||
3547 | /* | |
3548 | * fp_getfkq | |
3549 | * | |
3550 | * Description: Get fileproc and kqueue pointer for a given fd from the | |
3551 | * per process open file table of the specified process, and if | |
3552 | * successful, increment the f_iocount | |
3553 | * | |
3554 | * Parameters: p Process in which fd lives | |
3555 | * fd fd to get information for | |
3556 | * resultfp Pointer to result fileproc | |
3557 | * pointer area, or 0 if none | |
3558 | * resultkq Pointer to result kqueue | |
3559 | * pointer area, or 0 if none | |
3560 | * | |
3561 | * Returns: EBADF The file descriptor is invalid | |
3562 | * EBADF The file descriptor is not a socket | |
3563 | * 0 Success | |
3564 | * | |
3565 | * Implicit returns: | |
3566 | * *resultfp (modified) Fileproc pointer | |
3567 | * *resultkq (modified) kqueue pointer | |
3568 | * | |
3569 | * Notes: The second EBADF should probably be something else to make | |
3570 | * the error condition distinct. | |
3571 | */ | |
3572 | int | |
3573 | fp_getfkq(proc_t p, int fd, struct fileproc **resultfp, | |
3574 | struct kqueue **resultkq) | |
3575 | { | |
3576 | struct filedesc *fdp = p->p_fd; | |
3577 | struct fileproc *fp; | |
3578 | ||
3579 | proc_fdlock_spin(p); | |
3580 | if ( fd < 0 || fd >= fdp->fd_nfiles || | |
3581 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3582 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3583 | proc_fdunlock(p); | |
3584 | return (EBADF); | |
3585 | } | |
3586 | if (fp->f_type != DTYPE_KQUEUE) { | |
3587 | proc_fdunlock(p); | |
3588 | return(EBADF); | |
3589 | } | |
3590 | fp->f_iocount++; | |
3591 | ||
3592 | if (resultfp) | |
3593 | *resultfp = fp; | |
3594 | if (resultkq) | |
3595 | *resultkq = (struct kqueue *)fp->f_data; | |
3596 | proc_fdunlock(p); | |
3597 | ||
3598 | return (0); | |
3599 | } | |
3600 | ||
3601 | ||
3602 | /* | |
3603 | * fp_getfpshm | |
3604 | * | |
3605 | * Description: Get fileproc and POSIX shared memory pointer for a given fd | |
3606 | * from the per process open file table of the specified process | |
3607 | * and if successful, increment the f_iocount | |
3608 | * | |
3609 | * Parameters: p Process in which fd lives | |
3610 | * fd fd to get information for | |
3611 | * resultfp Pointer to result fileproc | |
3612 | * pointer area, or 0 if none | |
3613 | * resultpshm Pointer to result POSIX | |
3614 | * shared memory pointer | |
3615 | * pointer area, or 0 if none | |
3616 | * | |
3617 | * Returns: EBADF The file descriptor is invalid | |
3618 | * EBADF The file descriptor is not a POSIX | |
3619 | * shared memory area | |
3620 | * 0 Success | |
3621 | * | |
3622 | * Implicit returns: | |
3623 | * *resultfp (modified) Fileproc pointer | |
3624 | * *resultpshm (modified) POSIX shared memory pointer | |
3625 | * | |
3626 | * Notes: The second EBADF should probably be something else to make | |
3627 | * the error condition distinct. | |
3628 | */ | |
3629 | int | |
3630 | fp_getfpshm(proc_t p, int fd, struct fileproc **resultfp, | |
3631 | struct pshmnode **resultpshm) | |
3632 | { | |
3633 | struct filedesc *fdp = p->p_fd; | |
3634 | struct fileproc *fp; | |
3635 | ||
3636 | proc_fdlock_spin(p); | |
3637 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3638 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3639 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3640 | proc_fdunlock(p); | |
3641 | return (EBADF); | |
3642 | } | |
3643 | if (fp->f_type != DTYPE_PSXSHM) { | |
3644 | ||
3645 | proc_fdunlock(p); | |
3646 | return(EBADF); | |
3647 | } | |
3648 | fp->f_iocount++; | |
3649 | ||
3650 | if (resultfp) | |
3651 | *resultfp = fp; | |
3652 | if (resultpshm) | |
3653 | *resultpshm = (struct pshmnode *)fp->f_data; | |
3654 | proc_fdunlock(p); | |
3655 | ||
3656 | return (0); | |
3657 | } | |
3658 | ||
3659 | ||
3660 | /* | |
3661 | * fp_getfsem | |
3662 | * | |
3663 | * Description: Get fileproc and POSIX semaphore pointer for a given fd from | |
3664 | * the per process open file table of the specified process | |
3665 | * and if successful, increment the f_iocount | |
3666 | * | |
3667 | * Parameters: p Process in which fd lives | |
3668 | * fd fd to get information for | |
3669 | * resultfp Pointer to result fileproc | |
3670 | * pointer area, or 0 if none | |
3671 | * resultpsem Pointer to result POSIX | |
3672 | * semaphore pointer area, or | |
3673 | * 0 if none | |
3674 | * | |
3675 | * Returns: EBADF The file descriptor is invalid | |
3676 | * EBADF The file descriptor is not a POSIX | |
3677 | * semaphore | |
3678 | * 0 Success | |
3679 | * | |
3680 | * Implicit returns: | |
3681 | * *resultfp (modified) Fileproc pointer | |
3682 | * *resultpsem (modified) POSIX semaphore pointer | |
3683 | * | |
3684 | * Notes: The second EBADF should probably be something else to make | |
3685 | * the error condition distinct. | |
3686 | * | |
3687 | * In order to support unnamed POSIX semaphores, the named | |
3688 | * POSIX semaphores will have to move out of the per-process | |
3689 | * open filetable, and into a global table that is shared with | |
3690 | * unnamed POSIX semaphores, since unnamed POSIX semaphores | |
3691 | * are typically used by declaring instances in shared memory, | |
3692 | * and there's no other way to do this without changing the | |
3693 | * underlying type, which would introduce binary compatibility | |
3694 | * issues. | |
3695 | */ | |
3696 | int | |
3697 | fp_getfpsem(proc_t p, int fd, struct fileproc **resultfp, | |
3698 | struct psemnode **resultpsem) | |
3699 | { | |
3700 | struct filedesc *fdp = p->p_fd; | |
3701 | struct fileproc *fp; | |
3702 | ||
3703 | proc_fdlock_spin(p); | |
3704 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3705 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3706 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3707 | proc_fdunlock(p); | |
3708 | return (EBADF); | |
3709 | } | |
3710 | if (fp->f_type != DTYPE_PSXSEM) { | |
3711 | proc_fdunlock(p); | |
3712 | return(EBADF); | |
3713 | } | |
3714 | fp->f_iocount++; | |
3715 | ||
3716 | if (resultfp) | |
3717 | *resultfp = fp; | |
3718 | if (resultpsem) | |
3719 | *resultpsem = (struct psemnode *)fp->f_data; | |
3720 | proc_fdunlock(p); | |
3721 | ||
3722 | return (0); | |
3723 | } | |
3724 | ||
3725 | ||
3726 | /* | |
3727 | * fp_getfpipe | |
3728 | * | |
3729 | * Description: Get fileproc and pipe pointer for a given fd from the | |
3730 | * per process open file table of the specified process | |
3731 | * and if successful, increment the f_iocount | |
3732 | * | |
3733 | * Parameters: p Process in which fd lives | |
3734 | * fd fd to get information for | |
3735 | * resultfp Pointer to result fileproc | |
3736 | * pointer area, or 0 if none | |
3737 | * resultpipe Pointer to result pipe | |
3738 | * pointer area, or 0 if none | |
3739 | * | |
3740 | * Returns: EBADF The file descriptor is invalid | |
3741 | * EBADF The file descriptor is not a socket | |
3742 | * 0 Success | |
3743 | * | |
3744 | * Implicit returns: | |
3745 | * *resultfp (modified) Fileproc pointer | |
3746 | * *resultpipe (modified) pipe pointer | |
3747 | * | |
3748 | * Notes: The second EBADF should probably be something else to make | |
3749 | * the error condition distinct. | |
3750 | */ | |
3751 | int | |
3752 | fp_getfpipe(proc_t p, int fd, struct fileproc **resultfp, | |
3753 | struct pipe **resultpipe) | |
3754 | { | |
3755 | struct filedesc *fdp = p->p_fd; | |
3756 | struct fileproc *fp; | |
3757 | ||
3758 | proc_fdlock_spin(p); | |
3759 | if (fd < 0 || fd >= fdp->fd_nfiles || | |
3760 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3761 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3762 | proc_fdunlock(p); | |
3763 | return (EBADF); | |
3764 | } | |
3765 | if (fp->f_type != DTYPE_PIPE) { | |
3766 | proc_fdunlock(p); | |
3767 | return(EBADF); | |
3768 | } | |
3769 | fp->f_iocount++; | |
3770 | ||
3771 | if (resultfp) | |
3772 | *resultfp = fp; | |
3773 | if (resultpipe) | |
3774 | *resultpipe = (struct pipe *)fp->f_data; | |
3775 | proc_fdunlock(p); | |
3776 | ||
3777 | return (0); | |
3778 | } | |
3779 | ||
3780 | ||
3781 | /* | |
3782 | * fp_lookup | |
3783 | * | |
3784 | * Description: Get fileproc pointer for a given fd from the per process | |
3785 | * open file table of the specified process and if successful, | |
3786 | * increment the f_iocount | |
3787 | * | |
3788 | * Parameters: p Process in which fd lives | |
3789 | * fd fd to get information for | |
3790 | * resultfp Pointer to result fileproc | |
3791 | * pointer area, or 0 if none | |
3792 | * locked !0 if the caller holds the | |
3793 | * proc_fdlock, 0 otherwise | |
3794 | * | |
3795 | * Returns: 0 Success | |
3796 | * EBADF Bad file descriptor | |
3797 | * | |
3798 | * Implicit returns: | |
3799 | * *resultfp (modified) Fileproc pointer | |
3800 | * | |
3801 | * Locks: If the argument 'locked' is non-zero, then the caller is | |
3802 | * expected to have taken and held the proc_fdlock; if it is | |
3803 | * zero, than this routine internally takes and drops this lock. | |
3804 | */ | |
3805 | int | |
3806 | fp_lookup(proc_t p, int fd, struct fileproc **resultfp, int locked) | |
3807 | { | |
3808 | struct filedesc *fdp = p->p_fd; | |
3809 | struct fileproc *fp; | |
3810 | ||
3811 | if (!locked) | |
3812 | proc_fdlock_spin(p); | |
3813 | if (fd < 0 || fdp == NULL || fd >= fdp->fd_nfiles || | |
3814 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
3815 | (fdp->fd_ofileflags[fd] & UF_RESERVED)) { | |
3816 | if (!locked) | |
3817 | proc_fdunlock(p); | |
3818 | return (EBADF); | |
3819 | } | |
3820 | fp->f_iocount++; | |
3821 | ||
3822 | if (resultfp) | |
3823 | *resultfp = fp; | |
3824 | if (!locked) | |
3825 | proc_fdunlock(p); | |
3826 | ||
3827 | return (0); | |
3828 | } | |
3829 | ||
3830 | ||
3831 | /* | |
3832 | * fp_tryswap | |
3833 | * | |
3834 | * Description: Swap the fileproc pointer for a given fd with a new | |
3835 | * fileproc pointer in the per-process open file table of | |
3836 | * the specified process. The fdlock must be held at entry. | |
3837 | * | |
3838 | * Parameters: p Process containing the fd | |
3839 | * fd The fd of interest | |
3840 | * nfp Pointer to the newfp | |
3841 | * | |
3842 | * Returns: 0 Success | |
3843 | * EBADF Bad file descriptor | |
3844 | * EINTR Interrupted | |
3845 | * EKEEPLOOKING f_iocount changed while lock was dropped. | |
3846 | */ | |
3847 | int | |
3848 | fp_tryswap(proc_t p, int fd, struct fileproc *nfp) | |
3849 | { | |
3850 | struct fileproc *fp; | |
3851 | int error; | |
3852 | ||
3853 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
3854 | ||
3855 | if (0 != (error = fp_lookup(p, fd, &fp, 1))) | |
3856 | return (error); | |
3857 | /* | |
3858 | * At this point, our caller (change_guardedfd_np) has | |
3859 | * one f_iocount reference, and we just took another | |
3860 | * one to begin the replacement. | |
3861 | */ | |
3862 | if (fp->f_iocount < 2) { | |
3863 | panic("f_iocount too small %d", fp->f_iocount); | |
3864 | } else if (2 == fp->f_iocount) { | |
3865 | ||
3866 | /* Copy the contents of *fp, preserving the "type" of *nfp */ | |
3867 | ||
3868 | nfp->f_flags = (nfp->f_flags & FP_TYPEMASK) | | |
3869 | (fp->f_flags & ~FP_TYPEMASK); | |
3870 | nfp->f_iocount = fp->f_iocount; | |
3871 | nfp->f_fglob = fp->f_fglob; | |
3872 | nfp->f_wset = fp->f_wset; | |
3873 | ||
3874 | p->p_fd->fd_ofiles[fd] = nfp; | |
3875 | (void) fp_drop(p, fd, nfp, 1); | |
3876 | } else { | |
3877 | /* | |
3878 | * Wait for all other active references to evaporate. | |
3879 | */ | |
3880 | p->p_fpdrainwait = 1; | |
3881 | error = msleep(&p->p_fpdrainwait, &p->p_fdmlock, | |
3882 | PRIBIO | PCATCH, "tryswap fpdrain", NULL); | |
3883 | if (0 == error) { | |
3884 | /* | |
3885 | * Return an "internal" errno to trigger a full | |
3886 | * reevaluation of the change-guard attempt. | |
3887 | */ | |
3888 | error = EKEEPLOOKING; | |
3889 | printf("%s: lookup collision fd %d\n", __func__, fd); | |
3890 | } | |
3891 | (void) fp_drop(p, fd, fp, 1); | |
3892 | } | |
3893 | return (error); | |
3894 | } | |
3895 | ||
3896 | ||
3897 | /* | |
3898 | * fp_drop_written | |
3899 | * | |
3900 | * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O | |
3901 | * reference previously taken by calling fp_lookup et. al. | |
3902 | * | |
3903 | * Parameters: p Process in which the fd lives | |
3904 | * fd fd associated with the fileproc | |
3905 | * fp fileproc on which to set the | |
3906 | * flag and drop the reference | |
3907 | * | |
3908 | * Returns: 0 Success | |
3909 | * fp_drop:EBADF Bad file descriptor | |
3910 | * | |
3911 | * Locks: This function internally takes and drops the proc_fdlock for | |
3912 | * the supplied process | |
3913 | * | |
3914 | * Notes: The fileproc must correspond to the fd in the supplied proc | |
3915 | */ | |
3916 | int | |
3917 | fp_drop_written(proc_t p, int fd, struct fileproc *fp) | |
3918 | { | |
3919 | int error; | |
3920 | ||
3921 | proc_fdlock_spin(p); | |
3922 | ||
3923 | fp->f_flags |= FP_WRITTEN; | |
3924 | ||
3925 | error = fp_drop(p, fd, fp, 1); | |
3926 | ||
3927 | proc_fdunlock(p); | |
3928 | ||
3929 | return (error); | |
3930 | } | |
3931 | ||
3932 | ||
3933 | /* | |
3934 | * fp_drop_event | |
3935 | * | |
3936 | * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O | |
3937 | * reference previously taken by calling fp_lookup et. al. | |
3938 | * | |
3939 | * Parameters: p Process in which the fd lives | |
3940 | * fd fd associated with the fileproc | |
3941 | * fp fileproc on which to set the | |
3942 | * flag and drop the reference | |
3943 | * | |
3944 | * Returns: 0 Success | |
3945 | * fp_drop:EBADF Bad file descriptor | |
3946 | * | |
3947 | * Locks: This function internally takes and drops the proc_fdlock for | |
3948 | * the supplied process | |
3949 | * | |
3950 | * Notes: The fileproc must correspond to the fd in the supplied proc | |
3951 | */ | |
3952 | int | |
3953 | fp_drop_event(proc_t p, int fd, struct fileproc *fp) | |
3954 | { | |
3955 | int error; | |
3956 | ||
3957 | proc_fdlock_spin(p); | |
3958 | ||
3959 | fp->f_flags |= FP_WAITEVENT; | |
3960 | ||
3961 | error = fp_drop(p, fd, fp, 1); | |
3962 | ||
3963 | proc_fdunlock(p); | |
3964 | ||
3965 | return (error); | |
3966 | } | |
3967 | ||
3968 | ||
3969 | /* | |
3970 | * fp_drop | |
3971 | * | |
3972 | * Description: Drop the I/O reference previously taken by calling fp_lookup | |
3973 | * et. al. | |
3974 | * | |
3975 | * Parameters: p Process in which the fd lives | |
3976 | * fd fd associated with the fileproc | |
3977 | * fp fileproc on which to set the | |
3978 | * flag and drop the reference | |
3979 | * locked flag to internally take and | |
3980 | * drop proc_fdlock if it is not | |
3981 | * already held by the caller | |
3982 | * | |
3983 | * Returns: 0 Success | |
3984 | * EBADF Bad file descriptor | |
3985 | * | |
3986 | * Locks: This function internally takes and drops the proc_fdlock for | |
3987 | * the supplied process if 'locked' is non-zero, and assumes that | |
3988 | * the caller already holds this lock if 'locked' is non-zero. | |
3989 | * | |
3990 | * Notes: The fileproc must correspond to the fd in the supplied proc | |
3991 | */ | |
3992 | int | |
3993 | fp_drop(proc_t p, int fd, struct fileproc *fp, int locked) | |
3994 | { | |
3995 | struct filedesc *fdp = p->p_fd; | |
3996 | int needwakeup = 0; | |
3997 | ||
3998 | if (!locked) | |
3999 | proc_fdlock_spin(p); | |
4000 | if ((fp == FILEPROC_NULL) && (fd < 0 || fd >= fdp->fd_nfiles || | |
4001 | (fp = fdp->fd_ofiles[fd]) == NULL || | |
4002 | ((fdp->fd_ofileflags[fd] & UF_RESERVED) && | |
4003 | !(fdp->fd_ofileflags[fd] & UF_CLOSING)))) { | |
4004 | if (!locked) | |
4005 | proc_fdunlock(p); | |
4006 | return (EBADF); | |
4007 | } | |
4008 | fp->f_iocount--; | |
4009 | ||
4010 | if (fp->f_iocount == 0) { | |
4011 | if (fp->f_flags & FP_SELCONFLICT) | |
4012 | fp->f_flags &= ~FP_SELCONFLICT; | |
4013 | ||
4014 | if (p->p_fpdrainwait) { | |
4015 | p->p_fpdrainwait = 0; | |
4016 | needwakeup = 1; | |
4017 | } | |
4018 | } | |
4019 | if (!locked) | |
4020 | proc_fdunlock(p); | |
4021 | if (needwakeup) | |
4022 | wakeup(&p->p_fpdrainwait); | |
4023 | ||
4024 | return (0); | |
4025 | } | |
4026 | ||
4027 | ||
4028 | /* | |
4029 | * file_vnode | |
4030 | * | |
4031 | * Description: Given an fd, look it up in the current process's per process | |
4032 | * open file table, and return its internal vnode pointer. | |
4033 | * | |
4034 | * Parameters: fd fd to obtain vnode from | |
4035 | * vpp pointer to vnode return area | |
4036 | * | |
4037 | * Returns: 0 Success | |
4038 | * EINVAL The fd does not refer to a | |
4039 | * vnode fileproc entry | |
4040 | * fp_lookup:EBADF Bad file descriptor | |
4041 | * | |
4042 | * Implicit returns: | |
4043 | * *vpp (modified) Returned vnode pointer | |
4044 | * | |
4045 | * Locks: This function internally takes and drops the proc_fdlock for | |
4046 | * the current process | |
4047 | * | |
4048 | * Notes: If successful, this function increments the f_iocount on the | |
4049 | * fd's corresponding fileproc. | |
4050 | * | |
4051 | * The fileproc referenced is not returned; because of this, care | |
4052 | * must be taken to not drop the last reference (e.g. by closing | |
4053 | * the file). This is inherently unsafe, since the reference may | |
4054 | * not be recoverable from the vnode, if there is a subsequent | |
4055 | * close that destroys the associate fileproc. The caller should | |
4056 | * therefore retain their own reference on the fileproc so that | |
4057 | * the f_iocount can be dropped subsequently. Failure to do this | |
4058 | * can result in the returned pointer immediately becoming invalid | |
4059 | * following the call. | |
4060 | * | |
4061 | * Use of this function is discouraged. | |
4062 | */ | |
4063 | int | |
4064 | file_vnode(int fd, struct vnode **vpp) | |
4065 | { | |
4066 | proc_t p = current_proc(); | |
4067 | struct fileproc *fp; | |
4068 | int error; | |
4069 | ||
4070 | proc_fdlock_spin(p); | |
4071 | if ( (error = fp_lookup(p, fd, &fp, 1)) ) { | |
4072 | proc_fdunlock(p); | |
4073 | return(error); | |
4074 | } | |
4075 | if (fp->f_type != DTYPE_VNODE) { | |
4076 | fp_drop(p, fd, fp,1); | |
4077 | proc_fdunlock(p); | |
4078 | return(EINVAL); | |
4079 | } | |
4080 | if (vpp != NULL) | |
4081 | *vpp = (struct vnode *)fp->f_data; | |
4082 | proc_fdunlock(p); | |
4083 | ||
4084 | return(0); | |
4085 | } | |
4086 | ||
4087 | ||
4088 | /* | |
4089 | * file_vnode_withvid | |
4090 | * | |
4091 | * Description: Given an fd, look it up in the current process's per process | |
4092 | * open file table, and return its internal vnode pointer. | |
4093 | * | |
4094 | * Parameters: fd fd to obtain vnode from | |
4095 | * vpp pointer to vnode return area | |
4096 | * vidp pointer to vid of the returned vnode | |
4097 | * | |
4098 | * Returns: 0 Success | |
4099 | * EINVAL The fd does not refer to a | |
4100 | * vnode fileproc entry | |
4101 | * fp_lookup:EBADF Bad file descriptor | |
4102 | * | |
4103 | * Implicit returns: | |
4104 | * *vpp (modified) Returned vnode pointer | |
4105 | * | |
4106 | * Locks: This function internally takes and drops the proc_fdlock for | |
4107 | * the current process | |
4108 | * | |
4109 | * Notes: If successful, this function increments the f_iocount on the | |
4110 | * fd's corresponding fileproc. | |
4111 | * | |
4112 | * The fileproc referenced is not returned; because of this, care | |
4113 | * must be taken to not drop the last reference (e.g. by closing | |
4114 | * the file). This is inherently unsafe, since the reference may | |
4115 | * not be recoverable from the vnode, if there is a subsequent | |
4116 | * close that destroys the associate fileproc. The caller should | |
4117 | * therefore retain their own reference on the fileproc so that | |
4118 | * the f_iocount can be dropped subsequently. Failure to do this | |
4119 | * can result in the returned pointer immediately becoming invalid | |
4120 | * following the call. | |
4121 | * | |
4122 | * Use of this function is discouraged. | |
4123 | */ | |
4124 | int | |
4125 | file_vnode_withvid(int fd, struct vnode **vpp, uint32_t * vidp) | |
4126 | { | |
4127 | proc_t p = current_proc(); | |
4128 | struct fileproc *fp; | |
4129 | vnode_t vp; | |
4130 | int error; | |
4131 | ||
4132 | proc_fdlock_spin(p); | |
4133 | if ( (error = fp_lookup(p, fd, &fp, 1)) ) { | |
4134 | proc_fdunlock(p); | |
4135 | return(error); | |
4136 | } | |
4137 | if (fp->f_type != DTYPE_VNODE) { | |
4138 | fp_drop(p, fd, fp,1); | |
4139 | proc_fdunlock(p); | |
4140 | return(EINVAL); | |
4141 | } | |
4142 | vp = (struct vnode *)fp->f_data; | |
4143 | if (vpp != NULL) | |
4144 | *vpp = vp; | |
4145 | ||
4146 | if ((vidp != NULL) && (vp != NULLVP)) | |
4147 | *vidp = (uint32_t)vp->v_id; | |
4148 | ||
4149 | proc_fdunlock(p); | |
4150 | ||
4151 | return(0); | |
4152 | } | |
4153 | ||
4154 | ||
4155 | /* | |
4156 | * file_socket | |
4157 | * | |
4158 | * Description: Given an fd, look it up in the current process's per process | |
4159 | * open file table, and return its internal socket pointer. | |
4160 | * | |
4161 | * Parameters: fd fd to obtain vnode from | |
4162 | * sp pointer to socket return area | |
4163 | * | |
4164 | * Returns: 0 Success | |
4165 | * ENOTSOCK Not a socket | |
4166 | * fp_lookup:EBADF Bad file descriptor | |
4167 | * | |
4168 | * Implicit returns: | |
4169 | * *sp (modified) Returned socket pointer | |
4170 | * | |
4171 | * Locks: This function internally takes and drops the proc_fdlock for | |
4172 | * the current process | |
4173 | * | |
4174 | * Notes: If successful, this function increments the f_iocount on the | |
4175 | * fd's corresponding fileproc. | |
4176 | * | |
4177 | * The fileproc referenced is not returned; because of this, care | |
4178 | * must be taken to not drop the last reference (e.g. by closing | |
4179 | * the file). This is inherently unsafe, since the reference may | |
4180 | * not be recoverable from the socket, if there is a subsequent | |
4181 | * close that destroys the associate fileproc. The caller should | |
4182 | * therefore retain their own reference on the fileproc so that | |
4183 | * the f_iocount can be dropped subsequently. Failure to do this | |
4184 | * can result in the returned pointer immediately becoming invalid | |
4185 | * following the call. | |
4186 | * | |
4187 | * Use of this function is discouraged. | |
4188 | */ | |
4189 | int | |
4190 | file_socket(int fd, struct socket **sp) | |
4191 | { | |
4192 | proc_t p = current_proc(); | |
4193 | struct fileproc *fp; | |
4194 | int error; | |
4195 | ||
4196 | proc_fdlock_spin(p); | |
4197 | if ( (error = fp_lookup(p, fd, &fp, 1)) ) { | |
4198 | proc_fdunlock(p); | |
4199 | return(error); | |
4200 | } | |
4201 | if (fp->f_type != DTYPE_SOCKET) { | |
4202 | fp_drop(p, fd, fp,1); | |
4203 | proc_fdunlock(p); | |
4204 | return(ENOTSOCK); | |
4205 | } | |
4206 | *sp = (struct socket *)fp->f_data; | |
4207 | proc_fdunlock(p); | |
4208 | ||
4209 | return(0); | |
4210 | } | |
4211 | ||
4212 | ||
4213 | /* | |
4214 | * file_flags | |
4215 | * | |
4216 | * Description: Given an fd, look it up in the current process's per process | |
4217 | * open file table, and return its fileproc's flags field. | |
4218 | * | |
4219 | * Parameters: fd fd whose flags are to be | |
4220 | * retrieved | |
4221 | * flags pointer to flags data area | |
4222 | * | |
4223 | * Returns: 0 Success | |
4224 | * ENOTSOCK Not a socket | |
4225 | * fp_lookup:EBADF Bad file descriptor | |
4226 | * | |
4227 | * Implicit returns: | |
4228 | * *flags (modified) Returned flags field | |
4229 | * | |
4230 | * Locks: This function internally takes and drops the proc_fdlock for | |
4231 | * the current process | |
4232 | * | |
4233 | * Notes: This function will internally increment and decrement the | |
4234 | * f_iocount of the fileproc as part of its operation. | |
4235 | */ | |
4236 | int | |
4237 | file_flags(int fd, int *flags) | |
4238 | { | |
4239 | ||
4240 | proc_t p = current_proc(); | |
4241 | struct fileproc *fp; | |
4242 | int error; | |
4243 | ||
4244 | proc_fdlock_spin(p); | |
4245 | if ( (error = fp_lookup(p, fd, &fp, 1)) ) { | |
4246 | proc_fdunlock(p); | |
4247 | return(error); | |
4248 | } | |
4249 | *flags = (int)fp->f_flag; | |
4250 | fp_drop(p, fd, fp,1); | |
4251 | proc_fdunlock(p); | |
4252 | ||
4253 | return(0); | |
4254 | } | |
4255 | ||
4256 | ||
4257 | /* | |
4258 | * file_drop | |
4259 | * | |
4260 | * Description: Drop an iocount reference on an fd, and wake up any waiters | |
4261 | * for draining (i.e. blocked in fileproc_drain() called during | |
4262 | * the last attempt to close a file). | |
4263 | * | |
4264 | * Parameters: fd fd on which an ioreference is | |
4265 | * to be dropped | |
4266 | * | |
4267 | * Returns: 0 Success | |
4268 | * EBADF Bad file descriptor | |
4269 | * | |
4270 | * Description: Given an fd, look it up in the current process's per process | |
4271 | * open file table, and drop it's fileproc's f_iocount by one | |
4272 | * | |
4273 | * Notes: This is intended as a corresponding operation to the functions | |
4274 | * file_vnode() and file_socket() operations. | |
4275 | * | |
4276 | * Technically, the close reference is supposed to be protected | |
4277 | * by a fileproc_drain(), however, a drain will only block if | |
4278 | * the fd refers to a character device, and that device has had | |
4279 | * preparefileread() called on it. If it refers to something | |
4280 | * other than a character device, then the drain will occur and | |
4281 | * block each close attempt, rather than merely the last close. | |
4282 | * | |
4283 | * Since it's possible for an fd that refers to a character | |
4284 | * device to have an intermediate close followed by an open to | |
4285 | * cause a different file to correspond to that descriptor, | |
4286 | * unless there was a cautionary reference taken on the fileproc, | |
4287 | * this is an inherently unsafe function. This happens in the | |
4288 | * case where multiple fd's in a process refer to the same | |
4289 | * character device (e.g. stdin/out/err pointing to a tty, etc.). | |
4290 | * | |
4291 | * Use of this function is discouraged. | |
4292 | */ | |
4293 | int | |
4294 | file_drop(int fd) | |
4295 | { | |
4296 | struct fileproc *fp; | |
4297 | proc_t p = current_proc(); | |
4298 | int needwakeup = 0; | |
4299 | ||
4300 | proc_fdlock_spin(p); | |
4301 | if (fd < 0 || fd >= p->p_fd->fd_nfiles || | |
4302 | (fp = p->p_fd->fd_ofiles[fd]) == NULL || | |
4303 | ((p->p_fd->fd_ofileflags[fd] & UF_RESERVED) && | |
4304 | !(p->p_fd->fd_ofileflags[fd] & UF_CLOSING))) { | |
4305 | proc_fdunlock(p); | |
4306 | return (EBADF); | |
4307 | } | |
4308 | fp->f_iocount --; | |
4309 | ||
4310 | if (fp->f_iocount == 0) { | |
4311 | if (fp->f_flags & FP_SELCONFLICT) | |
4312 | fp->f_flags &= ~FP_SELCONFLICT; | |
4313 | ||
4314 | if (p->p_fpdrainwait) { | |
4315 | p->p_fpdrainwait = 0; | |
4316 | needwakeup = 1; | |
4317 | } | |
4318 | } | |
4319 | proc_fdunlock(p); | |
4320 | ||
4321 | if (needwakeup) | |
4322 | wakeup(&p->p_fpdrainwait); | |
4323 | return(0); | |
4324 | } | |
4325 | ||
4326 | ||
4327 | static int falloc_withalloc_locked(proc_t, struct fileproc **, int *, | |
4328 | vfs_context_t, struct fileproc * (*)(void *), void *, int); | |
4329 | ||
4330 | /* | |
4331 | * falloc | |
4332 | * | |
4333 | * Description: Allocate an entry in the per process open file table and | |
4334 | * return the corresponding fileproc and fd. | |
4335 | * | |
4336 | * Parameters: p The process in whose open file | |
4337 | * table the fd is to be allocated | |
4338 | * resultfp Pointer to fileproc pointer | |
4339 | * return area | |
4340 | * resultfd Pointer to fd return area | |
4341 | * ctx VFS context | |
4342 | * | |
4343 | * Returns: 0 Success | |
4344 | * falloc:ENFILE Too many open files in system | |
4345 | * falloc:EMFILE Too many open files in process | |
4346 | * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone | |
4347 | * exhausted | |
4348 | * | |
4349 | * Implicit returns: | |
4350 | * *resultfd (modified) Returned fileproc pointer | |
4351 | * *resultfd (modified) Returned fd | |
4352 | * | |
4353 | * Locks: This function takes and drops the proc_fdlock; if this lock | |
4354 | * is already held, use falloc_locked() instead. | |
4355 | * | |
4356 | * Notes: This function takes separate process and context arguments | |
4357 | * solely to support kern_exec.c; otherwise, it would take | |
4358 | * neither, and expect falloc_locked() to use the | |
4359 | * vfs_context_current() routine internally. | |
4360 | */ | |
4361 | int | |
4362 | falloc(proc_t p, struct fileproc **resultfp, int *resultfd, vfs_context_t ctx) | |
4363 | { | |
4364 | return (falloc_withalloc(p, resultfp, resultfd, ctx, | |
4365 | fileproc_alloc_init, NULL)); | |
4366 | } | |
4367 | ||
4368 | /* | |
4369 | * Like falloc, but including the fileproc allocator and create-args | |
4370 | */ | |
4371 | int | |
4372 | falloc_withalloc(proc_t p, struct fileproc **resultfp, int *resultfd, | |
4373 | vfs_context_t ctx, fp_allocfn_t fp_zalloc, void *arg) | |
4374 | { | |
4375 | int error; | |
4376 | ||
4377 | proc_fdlock(p); | |
4378 | error = falloc_withalloc_locked(p, | |
4379 | resultfp, resultfd, ctx, fp_zalloc, arg, 1); | |
4380 | proc_fdunlock(p); | |
4381 | ||
4382 | return (error); | |
4383 | } | |
4384 | ||
4385 | /* | |
4386 | * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists | |
4387 | */ | |
4388 | static const struct fileops uninitops; | |
4389 | ||
4390 | /* | |
4391 | * falloc_locked | |
4392 | * | |
4393 | * Create a new open file structure and allocate | |
4394 | * a file descriptor for the process that refers to it. | |
4395 | * | |
4396 | * Returns: 0 Success | |
4397 | * | |
4398 | * Description: Allocate an entry in the per process open file table and | |
4399 | * return the corresponding fileproc and fd. | |
4400 | * | |
4401 | * Parameters: p The process in whose open file | |
4402 | * table the fd is to be allocated | |
4403 | * resultfp Pointer to fileproc pointer | |
4404 | * return area | |
4405 | * resultfd Pointer to fd return area | |
4406 | * ctx VFS context | |
4407 | * locked Flag to indicate whether the | |
4408 | * caller holds proc_fdlock | |
4409 | * | |
4410 | * Returns: 0 Success | |
4411 | * ENFILE Too many open files in system | |
4412 | * fdalloc:EMFILE Too many open files in process | |
4413 | * ENOMEM M_FILEPROC or M_FILEGLOB zone | |
4414 | * exhausted | |
4415 | * fdalloc:ENOMEM | |
4416 | * | |
4417 | * Implicit returns: | |
4418 | * *resultfd (modified) Returned fileproc pointer | |
4419 | * *resultfd (modified) Returned fd | |
4420 | * | |
4421 | * Locks: If the parameter 'locked' is zero, this function takes and | |
4422 | * drops the proc_fdlock; if non-zero, the caller must hold the | |
4423 | * lock. | |
4424 | * | |
4425 | * Notes: If you intend to use a non-zero 'locked' parameter, use the | |
4426 | * utility function falloc() instead. | |
4427 | * | |
4428 | * This function takes separate process and context arguments | |
4429 | * solely to support kern_exec.c; otherwise, it would take | |
4430 | * neither, and use the vfs_context_current() routine internally. | |
4431 | */ | |
4432 | int | |
4433 | falloc_locked(proc_t p, struct fileproc **resultfp, int *resultfd, | |
4434 | vfs_context_t ctx, int locked) | |
4435 | { | |
4436 | return (falloc_withalloc_locked(p, resultfp, resultfd, ctx, | |
4437 | fileproc_alloc_init, NULL, locked)); | |
4438 | } | |
4439 | ||
4440 | static int | |
4441 | falloc_withalloc_locked(proc_t p, struct fileproc **resultfp, int *resultfd, | |
4442 | vfs_context_t ctx, fp_allocfn_t fp_zalloc, void *crarg, | |
4443 | int locked) | |
4444 | { | |
4445 | struct fileproc *fp; | |
4446 | struct fileglob *fg; | |
4447 | int error, nfd; | |
4448 | ||
4449 | if (!locked) | |
4450 | proc_fdlock(p); | |
4451 | if ( (error = fdalloc(p, 0, &nfd)) ) { | |
4452 | if (!locked) | |
4453 | proc_fdunlock(p); | |
4454 | return (error); | |
4455 | } | |
4456 | if (nfiles >= maxfiles) { | |
4457 | if (!locked) | |
4458 | proc_fdunlock(p); | |
4459 | tablefull("file"); | |
4460 | return (ENFILE); | |
4461 | } | |
4462 | #if CONFIG_MACF | |
4463 | error = mac_file_check_create(proc_ucred(p)); | |
4464 | if (error) { | |
4465 | if (!locked) | |
4466 | proc_fdunlock(p); | |
4467 | return (error); | |
4468 | } | |
4469 | #endif | |
4470 | ||
4471 | /* | |
4472 | * Allocate a new file descriptor. | |
4473 | * If the process has file descriptor zero open, add to the list | |
4474 | * of open files at that point, otherwise put it at the front of | |
4475 | * the list of open files. | |
4476 | */ | |
4477 | proc_fdunlock(p); | |
4478 | ||
4479 | fp = (*fp_zalloc)(crarg); | |
4480 | if (fp == NULL) { | |
4481 | if (locked) | |
4482 | proc_fdlock(p); | |
4483 | return (ENOMEM); | |
4484 | } | |
4485 | MALLOC_ZONE(fg, struct fileglob *, sizeof(struct fileglob), M_FILEGLOB, M_WAITOK); | |
4486 | if (fg == NULL) { | |
4487 | fileproc_free(fp); | |
4488 | if (locked) | |
4489 | proc_fdlock(p); | |
4490 | return (ENOMEM); | |
4491 | } | |
4492 | bzero(fg, sizeof(struct fileglob)); | |
4493 | lck_mtx_init(&fg->fg_lock, file_lck_grp, file_lck_attr); | |
4494 | ||
4495 | fp->f_iocount = 1; | |
4496 | fg->fg_count = 1; | |
4497 | fg->fg_ops = &uninitops; | |
4498 | fp->f_fglob = fg; | |
4499 | #if CONFIG_MACF | |
4500 | mac_file_label_init(fg); | |
4501 | #endif | |
4502 | ||
4503 | kauth_cred_ref(ctx->vc_ucred); | |
4504 | ||
4505 | proc_fdlock(p); | |
4506 | ||
4507 | fp->f_cred = ctx->vc_ucred; | |
4508 | ||
4509 | #if CONFIG_MACF | |
4510 | mac_file_label_associate(fp->f_cred, fg); | |
4511 | #endif | |
4512 | ||
4513 | OSAddAtomic(1, &nfiles); | |
4514 | ||
4515 | p->p_fd->fd_ofiles[nfd] = fp; | |
4516 | ||
4517 | if (!locked) | |
4518 | proc_fdunlock(p); | |
4519 | ||
4520 | if (resultfp) | |
4521 | *resultfp = fp; | |
4522 | if (resultfd) | |
4523 | *resultfd = nfd; | |
4524 | ||
4525 | return (0); | |
4526 | } | |
4527 | ||
4528 | ||
4529 | /* | |
4530 | * fg_free | |
4531 | * | |
4532 | * Description: Free a file structure; drop the global open file count, and | |
4533 | * drop the credential reference, if the fileglob has one, and | |
4534 | * destroy the instance mutex before freeing | |
4535 | * | |
4536 | * Parameters: fg Pointer to fileglob to be | |
4537 | * freed | |
4538 | * | |
4539 | * Returns: void | |
4540 | */ | |
4541 | void | |
4542 | fg_free(struct fileglob *fg) | |
4543 | { | |
4544 | OSAddAtomic(-1, &nfiles); | |
4545 | ||
4546 | if (fg->fg_vn_data) { | |
4547 | fg_vn_data_free(fg->fg_vn_data); | |
4548 | fg->fg_vn_data = NULL; | |
4549 | } | |
4550 | ||
4551 | if (IS_VALID_CRED(fg->fg_cred)) { | |
4552 | kauth_cred_unref(&fg->fg_cred); | |
4553 | } | |
4554 | lck_mtx_destroy(&fg->fg_lock, file_lck_grp); | |
4555 | ||
4556 | #if CONFIG_MACF | |
4557 | mac_file_label_destroy(fg); | |
4558 | #endif | |
4559 | FREE_ZONE(fg, sizeof *fg, M_FILEGLOB); | |
4560 | } | |
4561 | ||
4562 | ||
4563 | /* | |
4564 | * fdexec | |
4565 | * | |
4566 | * Description: Perform close-on-exec processing for all files in a process | |
4567 | * that are either marked as close-on-exec, or which were in the | |
4568 | * process of being opened at the time of the execve | |
4569 | * | |
4570 | * Also handles the case (via posix_spawn()) where -all- | |
4571 | * files except those marked with "inherit" as treated as | |
4572 | * close-on-exec. | |
4573 | * | |
4574 | * Parameters: p Pointer to process calling | |
4575 | * execve | |
4576 | * | |
4577 | * Returns: void | |
4578 | * | |
4579 | * Locks: This function internally takes and drops proc_fdlock() | |
4580 | * | |
4581 | */ | |
4582 | void | |
4583 | fdexec(proc_t p, short flags) | |
4584 | { | |
4585 | struct filedesc *fdp = p->p_fd; | |
4586 | int i; | |
4587 | boolean_t cloexec_default = (flags & POSIX_SPAWN_CLOEXEC_DEFAULT) != 0; | |
4588 | ||
4589 | proc_fdlock(p); | |
4590 | for (i = fdp->fd_lastfile; i >= 0; i--) { | |
4591 | ||
4592 | struct fileproc *fp = fdp->fd_ofiles[i]; | |
4593 | char *flagp = &fdp->fd_ofileflags[i]; | |
4594 | ||
4595 | if (fp && cloexec_default) { | |
4596 | /* | |
4597 | * Reverse the usual semantics of file descriptor | |
4598 | * inheritance - all of them should be closed | |
4599 | * except files marked explicitly as "inherit" and | |
4600 | * not marked close-on-exec. | |
4601 | */ | |
4602 | if ((*flagp & (UF_EXCLOSE|UF_INHERIT)) != UF_INHERIT) | |
4603 | *flagp |= UF_EXCLOSE; | |
4604 | *flagp &= ~UF_INHERIT; | |
4605 | } | |
4606 | ||
4607 | if ( | |
4608 | ((*flagp & (UF_RESERVED|UF_EXCLOSE)) == UF_EXCLOSE) | |
4609 | #if CONFIG_MACF | |
4610 | || (fp && mac_file_check_inherit(proc_ucred(p), fp->f_fglob)) | |
4611 | #endif | |
4612 | ) { | |
4613 | if (i < fdp->fd_knlistsize) | |
4614 | knote_fdclose(p, i, TRUE); | |
4615 | procfdtbl_clearfd(p, i); | |
4616 | if (i == fdp->fd_lastfile && i > 0) | |
4617 | fdp->fd_lastfile--; | |
4618 | if (i < fdp->fd_freefile) | |
4619 | fdp->fd_freefile = i; | |
4620 | ||
4621 | /* | |
4622 | * Wait for any third party viewers (e.g., lsof) | |
4623 | * to release their references to this fileproc. | |
4624 | */ | |
4625 | while (fp->f_iocount > 0) { | |
4626 | p->p_fpdrainwait = 1; | |
4627 | msleep(&p->p_fpdrainwait, &p->p_fdmlock, PRIBIO, | |
4628 | "fpdrain", NULL); | |
4629 | } | |
4630 | ||
4631 | closef_locked(fp, fp->f_fglob, p); | |
4632 | ||
4633 | fileproc_free(fp); | |
4634 | } | |
4635 | } | |
4636 | proc_fdunlock(p); | |
4637 | } | |
4638 | ||
4639 | ||
4640 | /* | |
4641 | * fdcopy | |
4642 | * | |
4643 | * Description: Copy a filedesc structure. This is normally used as part of | |
4644 | * forkproc() when forking a new process, to copy the per process | |
4645 | * open file table over to the new process. | |
4646 | * | |
4647 | * Parameters: p Process whose open file table | |
4648 | * is to be copied (parent) | |
4649 | * uth_cdir Per thread current working | |
4650 | * cirectory, or NULL | |
4651 | * | |
4652 | * Returns: NULL Copy failed | |
4653 | * !NULL Pointer to new struct filedesc | |
4654 | * | |
4655 | * Locks: This function internally takes and drops proc_fdlock() | |
4656 | * | |
4657 | * Notes: Files are copied directly, ignoring the new resource limits | |
4658 | * for the process that's being copied into. Since the descriptor | |
4659 | * references are just additional references, this does not count | |
4660 | * against the number of open files on the system. | |
4661 | * | |
4662 | * The struct filedesc includes the current working directory, | |
4663 | * and the current root directory, if the process is chroot'ed. | |
4664 | * | |
4665 | * If the exec was called by a thread using a per thread current | |
4666 | * working directory, we inherit the working directory from the | |
4667 | * thread making the call, rather than from the process. | |
4668 | * | |
4669 | * In the case of a failure to obtain a reference, for most cases, | |
4670 | * the file entry will be silently dropped. There's an exception | |
4671 | * for the case of a chroot dir, since a failure to to obtain a | |
4672 | * reference there would constitute an "escape" from the chroot | |
4673 | * environment, which must not be allowed. In that case, we will | |
4674 | * deny the execve() operation, rather than allowing the escape. | |
4675 | */ | |
4676 | struct filedesc * | |
4677 | fdcopy(proc_t p, vnode_t uth_cdir) | |
4678 | { | |
4679 | struct filedesc *newfdp, *fdp = p->p_fd; | |
4680 | int i; | |
4681 | struct fileproc *ofp, *fp; | |
4682 | vnode_t v_dir; | |
4683 | ||
4684 | MALLOC_ZONE(newfdp, struct filedesc *, | |
4685 | sizeof(*newfdp), M_FILEDESC, M_WAITOK); | |
4686 | if (newfdp == NULL) | |
4687 | return(NULL); | |
4688 | ||
4689 | proc_fdlock(p); | |
4690 | ||
4691 | /* | |
4692 | * the FD_CHROOT flag will be inherited via this copy | |
4693 | */ | |
4694 | (void) memcpy(newfdp, fdp, sizeof(*newfdp)); | |
4695 | ||
4696 | /* | |
4697 | * If we are running with per-thread current working directories, | |
4698 | * inherit the new current working directory from the current thread | |
4699 | * instead, before we take our references. | |
4700 | */ | |
4701 | if (uth_cdir != NULLVP) | |
4702 | newfdp->fd_cdir = uth_cdir; | |
4703 | ||
4704 | /* | |
4705 | * For both fd_cdir and fd_rdir make sure we get | |
4706 | * a valid reference... if we can't, than set | |
4707 | * set the pointer(s) to NULL in the child... this | |
4708 | * will keep us from using a non-referenced vp | |
4709 | * and allows us to do the vnode_rele only on | |
4710 | * a properly referenced vp | |
4711 | */ | |
4712 | if ( (v_dir = newfdp->fd_cdir) ) { | |
4713 | if (vnode_getwithref(v_dir) == 0) { | |
4714 | if ( (vnode_ref(v_dir)) ) | |
4715 | newfdp->fd_cdir = NULL; | |
4716 | vnode_put(v_dir); | |
4717 | } else | |
4718 | newfdp->fd_cdir = NULL; | |
4719 | } | |
4720 | if (newfdp->fd_cdir == NULL && fdp->fd_cdir) { | |
4721 | /* | |
4722 | * we couldn't get a new reference on | |
4723 | * the current working directory being | |
4724 | * inherited... we might as well drop | |
4725 | * our reference from the parent also | |
4726 | * since the vnode has gone DEAD making | |
4727 | * it useless... by dropping it we'll | |
4728 | * be that much closer to recycling it | |
4729 | */ | |
4730 | vnode_rele(fdp->fd_cdir); | |
4731 | fdp->fd_cdir = NULL; | |
4732 | } | |
4733 | ||
4734 | if ( (v_dir = newfdp->fd_rdir) ) { | |
4735 | if (vnode_getwithref(v_dir) == 0) { | |
4736 | if ( (vnode_ref(v_dir)) ) | |
4737 | newfdp->fd_rdir = NULL; | |
4738 | vnode_put(v_dir); | |
4739 | } else { | |
4740 | newfdp->fd_rdir = NULL; | |
4741 | } | |
4742 | } | |
4743 | /* Coming from a chroot environment and unable to get a reference... */ | |
4744 | if (newfdp->fd_rdir == NULL && fdp->fd_rdir) { | |
4745 | /* | |
4746 | * We couldn't get a new reference on | |
4747 | * the chroot directory being | |
4748 | * inherited... this is fatal, since | |
4749 | * otherwise it would constitute an | |
4750 | * escape from a chroot environment by | |
4751 | * the new process. | |
4752 | */ | |
4753 | if (newfdp->fd_cdir) | |
4754 | vnode_rele(newfdp->fd_cdir); | |
4755 | FREE_ZONE(newfdp, sizeof *newfdp, M_FILEDESC); | |
4756 | return(NULL); | |
4757 | } | |
4758 | ||
4759 | /* | |
4760 | * If the number of open files fits in the internal arrays | |
4761 | * of the open file structure, use them, otherwise allocate | |
4762 | * additional memory for the number of descriptors currently | |
4763 | * in use. | |
4764 | */ | |
4765 | if (newfdp->fd_lastfile < NDFILE) | |
4766 | i = NDFILE; | |
4767 | else { | |
4768 | /* | |
4769 | * Compute the smallest multiple of NDEXTENT needed | |
4770 | * for the file descriptors currently in use, | |
4771 | * allowing the table to shrink. | |
4772 | */ | |
4773 | i = newfdp->fd_nfiles; | |
4774 | while (i > 1 + 2 * NDEXTENT && i > 1 + newfdp->fd_lastfile * 2) | |
4775 | i /= 2; | |
4776 | } | |
4777 | proc_fdunlock(p); | |
4778 | ||
4779 | MALLOC_ZONE(newfdp->fd_ofiles, struct fileproc **, | |
4780 | i * OFILESIZE, M_OFILETABL, M_WAITOK); | |
4781 | if (newfdp->fd_ofiles == NULL) { | |
4782 | if (newfdp->fd_cdir) | |
4783 | vnode_rele(newfdp->fd_cdir); | |
4784 | if (newfdp->fd_rdir) | |
4785 | vnode_rele(newfdp->fd_rdir); | |
4786 | ||
4787 | FREE_ZONE(newfdp, sizeof(*newfdp), M_FILEDESC); | |
4788 | return(NULL); | |
4789 | } | |
4790 | (void) memset(newfdp->fd_ofiles, 0, i * OFILESIZE); | |
4791 | proc_fdlock(p); | |
4792 | ||
4793 | newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i]; | |
4794 | newfdp->fd_nfiles = i; | |
4795 | ||
4796 | if (fdp->fd_nfiles > 0) { | |
4797 | struct fileproc **fpp; | |
4798 | char *flags; | |
4799 | ||
4800 | (void) memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, | |
4801 | (newfdp->fd_lastfile + 1) * sizeof(*fdp->fd_ofiles)); | |
4802 | (void) memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, | |
4803 | (newfdp->fd_lastfile + 1) * sizeof(*fdp->fd_ofileflags)); | |
4804 | ||
4805 | /* | |
4806 | * kq descriptors cannot be copied. | |
4807 | */ | |
4808 | if (newfdp->fd_knlistsize != -1) { | |
4809 | fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile]; | |
4810 | flags = &newfdp->fd_ofileflags[newfdp->fd_lastfile]; | |
4811 | for (i = newfdp->fd_lastfile; | |
4812 | i >= 0; i--, fpp--, flags--) { | |
4813 | if (*flags & UF_RESERVED) | |
4814 | continue; /* (removed below) */ | |
4815 | if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) { | |
4816 | *fpp = NULL; | |
4817 | *flags = 0; | |
4818 | if (i < newfdp->fd_freefile) | |
4819 | newfdp->fd_freefile = i; | |
4820 | } | |
4821 | if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0) | |
4822 | newfdp->fd_lastfile--; | |
4823 | } | |
4824 | newfdp->fd_knlist = NULL; | |
4825 | newfdp->fd_knlistsize = -1; | |
4826 | newfdp->fd_knhash = NULL; | |
4827 | newfdp->fd_knhashmask = 0; | |
4828 | } | |
4829 | fpp = newfdp->fd_ofiles; | |
4830 | flags = newfdp->fd_ofileflags; | |
4831 | ||
4832 | for (i = newfdp->fd_lastfile + 1; --i >= 0; fpp++, flags++) | |
4833 | if ((ofp = *fpp) != NULL && | |
4834 | 0 == (ofp->f_fglob->fg_lflags & FG_CONFINED) && | |
4835 | 0 == (*flags & (UF_FORKCLOSE|UF_RESERVED))) { | |
4836 | #if DEBUG | |
4837 | if (FILEPROC_TYPE(ofp) != FTYPE_SIMPLE) | |
4838 | panic("complex fileproc"); | |
4839 | #endif | |
4840 | fp = fileproc_alloc_init(NULL); | |
4841 | if (fp == NULL) { | |
4842 | /* | |
4843 | * XXX no room to copy, unable to | |
4844 | * XXX safely unwind state at present | |
4845 | */ | |
4846 | *fpp = NULL; | |
4847 | } else { | |
4848 | fp->f_flags |= | |
4849 | (ofp->f_flags & ~FP_TYPEMASK); | |
4850 | fp->f_fglob = ofp->f_fglob; | |
4851 | (void)fg_ref(fp); | |
4852 | *fpp = fp; | |
4853 | } | |
4854 | } else { | |
4855 | if (i < newfdp->fd_freefile) | |
4856 | newfdp->fd_freefile = i; | |
4857 | *fpp = NULL; | |
4858 | *flags = 0; | |
4859 | } | |
4860 | } | |
4861 | ||
4862 | proc_fdunlock(p); | |
4863 | return (newfdp); | |
4864 | } | |
4865 | ||
4866 | ||
4867 | /* | |
4868 | * fdfree | |
4869 | * | |
4870 | * Description: Release a filedesc (per process open file table) structure; | |
4871 | * this is done on process exit(), or from forkproc_free() if | |
4872 | * the fork fails for some reason subsequent to a successful | |
4873 | * call to fdcopy() | |
4874 | * | |
4875 | * Parameters: p Pointer to process going away | |
4876 | * | |
4877 | * Returns: void | |
4878 | * | |
4879 | * Locks: This function internally takes and drops proc_fdlock() | |
4880 | */ | |
4881 | void | |
4882 | fdfree(proc_t p) | |
4883 | { | |
4884 | struct filedesc *fdp; | |
4885 | struct fileproc *fp; | |
4886 | int i; | |
4887 | ||
4888 | proc_fdlock(p); | |
4889 | ||
4890 | if (p == kernproc || NULL == (fdp = p->p_fd)) { | |
4891 | proc_fdunlock(p); | |
4892 | return; | |
4893 | } | |
4894 | ||
4895 | extern struct filedesc filedesc0; | |
4896 | ||
4897 | if (&filedesc0 == fdp) | |
4898 | panic("filedesc0"); | |
4899 | ||
4900 | if (fdp->fd_nfiles > 0 && fdp->fd_ofiles) { | |
4901 | for (i = fdp->fd_lastfile; i >= 0; i--) { | |
4902 | ||
4903 | /* May still have knotes for fd without open file */ | |
4904 | if (i < fdp->fd_knlistsize) | |
4905 | knote_fdclose(p, i, TRUE); | |
4906 | ||
4907 | if ((fp = fdp->fd_ofiles[i]) != NULL) { | |
4908 | ||
4909 | if (fdp->fd_ofileflags[i] & UF_RESERVED) | |
4910 | panic("fdfree: found fp with UF_RESERVED"); | |
4911 | ||
4912 | procfdtbl_reservefd(p, i); | |
4913 | ||
4914 | if (fp->f_flags & FP_WAITEVENT) | |
4915 | (void)waitevent_close(p, fp); | |
4916 | (void) closef_locked(fp, fp->f_fglob, p); | |
4917 | fileproc_free(fp); | |
4918 | } | |
4919 | } | |
4920 | FREE_ZONE(fdp->fd_ofiles, fdp->fd_nfiles * OFILESIZE, M_OFILETABL); | |
4921 | fdp->fd_ofiles = NULL; | |
4922 | fdp->fd_nfiles = 0; | |
4923 | } | |
4924 | ||
4925 | proc_fdunlock(p); | |
4926 | ||
4927 | if (fdp->fd_cdir) | |
4928 | vnode_rele(fdp->fd_cdir); | |
4929 | if (fdp->fd_rdir) | |
4930 | vnode_rele(fdp->fd_rdir); | |
4931 | ||
4932 | proc_fdlock_spin(p); | |
4933 | p->p_fd = NULL; | |
4934 | proc_fdunlock(p); | |
4935 | ||
4936 | if (fdp->fd_knlist) | |
4937 | FREE(fdp->fd_knlist, M_KQUEUE); | |
4938 | if (fdp->fd_knhash) | |
4939 | FREE(fdp->fd_knhash, M_KQUEUE); | |
4940 | ||
4941 | FREE_ZONE(fdp, sizeof(*fdp), M_FILEDESC); | |
4942 | } | |
4943 | ||
4944 | /* | |
4945 | * closef_locked | |
4946 | * | |
4947 | * Description: Internal form of closef; called with proc_fdlock held | |
4948 | * | |
4949 | * Parameters: fp Pointer to fileproc for fd | |
4950 | * fg Pointer to fileglob for fd | |
4951 | * p Pointer to proc structure | |
4952 | * | |
4953 | * Returns: 0 Success | |
4954 | * closef_finish:??? Anything returnable by a per-fileops | |
4955 | * close function | |
4956 | * | |
4957 | * Note: Decrements reference count on file structure; if this was the | |
4958 | * last reference, then closef_finish() is called | |
4959 | * | |
4960 | * p and fp are allowed to be NULL when closing a file that was | |
4961 | * being passed in a message (but only if we are called when this | |
4962 | * is NOT the last reference). | |
4963 | */ | |
4964 | int | |
4965 | closef_locked(struct fileproc *fp, struct fileglob *fg, proc_t p) | |
4966 | { | |
4967 | struct vnode *vp; | |
4968 | struct flock lf; | |
4969 | struct vfs_context context; | |
4970 | int error; | |
4971 | ||
4972 | if (fg == NULL) { | |
4973 | return (0); | |
4974 | } | |
4975 | ||
4976 | /* Set up context with cred stashed in fg */ | |
4977 | if (p == current_proc()) | |
4978 | context.vc_thread = current_thread(); | |
4979 | else | |
4980 | context.vc_thread = NULL; | |
4981 | context.vc_ucred = fg->fg_cred; | |
4982 | ||
4983 | /* | |
4984 | * POSIX record locking dictates that any close releases ALL | |
4985 | * locks owned by this process. This is handled by setting | |
4986 | * a flag in the unlock to free ONLY locks obeying POSIX | |
4987 | * semantics, and not to free BSD-style file locks. | |
4988 | * If the descriptor was in a message, POSIX-style locks | |
4989 | * aren't passed with the descriptor. | |
4990 | */ | |
4991 | if (p && (p->p_ladvflag & P_LADVLOCK) && | |
4992 | DTYPE_VNODE == FILEGLOB_DTYPE(fg)) { | |
4993 | proc_fdunlock(p); | |
4994 | ||
4995 | lf.l_whence = SEEK_SET; | |
4996 | lf.l_start = 0; | |
4997 | lf.l_len = 0; | |
4998 | lf.l_type = F_UNLCK; | |
4999 | vp = (struct vnode *)fg->fg_data; | |
5000 | ||
5001 | if ( (error = vnode_getwithref(vp)) == 0 ) { | |
5002 | (void) VNOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX, &context, NULL); | |
5003 | (void)vnode_put(vp); | |
5004 | } | |
5005 | proc_fdlock(p); | |
5006 | } | |
5007 | lck_mtx_lock_spin(&fg->fg_lock); | |
5008 | fg->fg_count--; | |
5009 | ||
5010 | if (fg->fg_count > 0) { | |
5011 | lck_mtx_unlock(&fg->fg_lock); | |
5012 | return (0); | |
5013 | } | |
5014 | #if DIAGNOSTIC | |
5015 | if (fg->fg_count != 0) | |
5016 | panic("fg %p: being freed with bad fg_count (%d)", fg, fg->fg_count); | |
5017 | #endif | |
5018 | ||
5019 | if (fp && (fp->f_flags & FP_WRITTEN)) | |
5020 | fg->fg_flag |= FWASWRITTEN; | |
5021 | ||
5022 | fg->fg_lflags |= FG_TERM; | |
5023 | lck_mtx_unlock(&fg->fg_lock); | |
5024 | ||
5025 | if (p) | |
5026 | proc_fdunlock(p); | |
5027 | ||
5028 | /* Since we ensure that fg->fg_ops is always initialized, | |
5029 | * it is safe to invoke fo_close on the fg */ | |
5030 | error = fo_close(fg, &context); | |
5031 | ||
5032 | fg_free(fg); | |
5033 | ||
5034 | if (p) | |
5035 | proc_fdlock(p); | |
5036 | ||
5037 | return(error); | |
5038 | } | |
5039 | ||
5040 | ||
5041 | /* | |
5042 | * fileproc_drain | |
5043 | * | |
5044 | * Description: Drain out pending I/O operations | |
5045 | * | |
5046 | * Parameters: p Process closing this file | |
5047 | * fp fileproc struct for the open | |
5048 | * instance on the file | |
5049 | * | |
5050 | * Returns: void | |
5051 | * | |
5052 | * Locks: Assumes the caller holds the proc_fdlock | |
5053 | * | |
5054 | * Notes: For character devices, this occurs on the last close of the | |
5055 | * device; for all other file descriptors, this occurs on each | |
5056 | * close to prevent fd's from being closed out from under | |
5057 | * operations currently in progress and blocked | |
5058 | * | |
5059 | * See Also: file_vnode(), file_socket(), file_drop(), and the cautions | |
5060 | * regarding their use and interaction with this function. | |
5061 | */ | |
5062 | void | |
5063 | fileproc_drain(proc_t p, struct fileproc * fp) | |
5064 | { | |
5065 | struct vfs_context context; | |
5066 | ||
5067 | context.vc_thread = proc_thread(p); /* XXX */ | |
5068 | context.vc_ucred = fp->f_fglob->fg_cred; | |
5069 | ||
5070 | fp->f_iocount-- ; /* (the one the close holds) */ | |
5071 | ||
5072 | while (fp->f_iocount) { | |
5073 | ||
5074 | lck_mtx_convert_spin(&p->p_fdmlock); | |
5075 | ||
5076 | if (fp->f_fglob->fg_ops->fo_drain) { | |
5077 | (*fp->f_fglob->fg_ops->fo_drain)(fp, &context); | |
5078 | } | |
5079 | if ((fp->f_flags & FP_INSELECT) == FP_INSELECT) { | |
5080 | if (waitq_wakeup64_all((struct waitq *)fp->f_wset, NO_EVENT64, | |
5081 | THREAD_INTERRUPTED, WAITQ_ALL_PRIORITIES) == KERN_INVALID_ARGUMENT) | |
5082 | panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp->f_wset, fp); | |
5083 | } | |
5084 | if ((fp->f_flags & FP_SELCONFLICT) == FP_SELCONFLICT) { | |
5085 | if (waitq_wakeup64_all(&select_conflict_queue, NO_EVENT64, | |
5086 | THREAD_INTERRUPTED, WAITQ_ALL_PRIORITIES) == KERN_INVALID_ARGUMENT) | |
5087 | panic("bad select_conflict_queue"); | |
5088 | } | |
5089 | p->p_fpdrainwait = 1; | |
5090 | ||
5091 | msleep(&p->p_fpdrainwait, &p->p_fdmlock, PRIBIO, "fpdrain", NULL); | |
5092 | ||
5093 | } | |
5094 | #if DIAGNOSTIC | |
5095 | if ((fp->f_flags & FP_INSELECT) != 0) | |
5096 | panic("FP_INSELECT set on drained fp"); | |
5097 | #endif | |
5098 | if ((fp->f_flags & FP_SELCONFLICT) == FP_SELCONFLICT) | |
5099 | fp->f_flags &= ~FP_SELCONFLICT; | |
5100 | } | |
5101 | ||
5102 | ||
5103 | /* | |
5104 | * fp_free | |
5105 | * | |
5106 | * Description: Release the fd and free the fileproc associated with the fd | |
5107 | * in the per process open file table of the specified process; | |
5108 | * these values must correspond. | |
5109 | * | |
5110 | * Parameters: p Process containing fd | |
5111 | * fd fd to be released | |
5112 | * fp fileproc to be freed | |
5113 | * | |
5114 | * Returns: 0 Success | |
5115 | * | |
5116 | * Notes: XXX function should be void - no one interprets the returns | |
5117 | * XXX code | |
5118 | */ | |
5119 | int | |
5120 | fp_free(proc_t p, int fd, struct fileproc * fp) | |
5121 | { | |
5122 | proc_fdlock_spin(p); | |
5123 | fdrelse(p, fd); | |
5124 | proc_fdunlock(p); | |
5125 | ||
5126 | fg_free(fp->f_fglob); | |
5127 | fileproc_free(fp); | |
5128 | return(0); | |
5129 | } | |
5130 | ||
5131 | ||
5132 | /* | |
5133 | * flock | |
5134 | * | |
5135 | * Description: Apply an advisory lock on a file descriptor. | |
5136 | * | |
5137 | * Parameters: p Process making request | |
5138 | * uap->fd fd on which the lock is to be | |
5139 | * attempted | |
5140 | * uap->how (Un)Lock bits, including type | |
5141 | * retval Pointer to the call return area | |
5142 | * | |
5143 | * Returns: 0 Success | |
5144 | * fp_getfvp:EBADF Bad file descriptor | |
5145 | * fp_getfvp:ENOTSUP fd does not refer to a vnode | |
5146 | * vnode_getwithref:??? | |
5147 | * VNOP_ADVLOCK:??? | |
5148 | * | |
5149 | * Implicit returns: | |
5150 | * *retval (modified) Size of dtable | |
5151 | * | |
5152 | * Notes: Just attempt to get a record lock of the requested type on | |
5153 | * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0). | |
5154 | */ | |
5155 | int | |
5156 | flock(proc_t p, struct flock_args *uap, __unused int32_t *retval) | |
5157 | { | |
5158 | int fd = uap->fd; | |
5159 | int how = uap->how; | |
5160 | struct fileproc *fp; | |
5161 | struct vnode *vp; | |
5162 | struct flock lf; | |
5163 | vfs_context_t ctx = vfs_context_current(); | |
5164 | int error=0; | |
5165 | ||
5166 | AUDIT_ARG(fd, uap->fd); | |
5167 | if ( (error = fp_getfvp(p, fd, &fp, &vp)) ) { | |
5168 | return(error); | |
5169 | } | |
5170 | if ( (error = vnode_getwithref(vp)) ) { | |
5171 | goto out1; | |
5172 | } | |
5173 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); | |
5174 | ||
5175 | lf.l_whence = SEEK_SET; | |
5176 | lf.l_start = 0; | |
5177 | lf.l_len = 0; | |
5178 | if (how & LOCK_UN) { | |
5179 | lf.l_type = F_UNLCK; | |
5180 | fp->f_flag &= ~FHASLOCK; | |
5181 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_UNLCK, &lf, F_FLOCK, ctx, NULL); | |
5182 | goto out; | |
5183 | } | |
5184 | if (how & LOCK_EX) | |
5185 | lf.l_type = F_WRLCK; | |
5186 | else if (how & LOCK_SH) | |
5187 | lf.l_type = F_RDLCK; | |
5188 | else { | |
5189 | error = EBADF; | |
5190 | goto out; | |
5191 | } | |
5192 | #if CONFIG_MACF | |
5193 | error = mac_file_check_lock(proc_ucred(p), fp->f_fglob, F_SETLK, &lf); | |
5194 | if (error) | |
5195 | goto out; | |
5196 | #endif | |
5197 | error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_SETLK, &lf, | |
5198 | (how & LOCK_NB ? F_FLOCK : F_FLOCK | F_WAIT), | |
5199 | ctx, NULL); | |
5200 | if (!error) | |
5201 | fp->f_flag |= FHASLOCK; | |
5202 | out: | |
5203 | (void)vnode_put(vp); | |
5204 | out1: | |
5205 | fp_drop(p, fd, fp, 0); | |
5206 | return(error); | |
5207 | ||
5208 | } | |
5209 | ||
5210 | /* | |
5211 | * fileport_makeport | |
5212 | * | |
5213 | * Description: Obtain a Mach send right for a given file descriptor. | |
5214 | * | |
5215 | * Parameters: p Process calling fileport | |
5216 | * uap->fd The fd to reference | |
5217 | * uap->portnamep User address at which to place port name. | |
5218 | * | |
5219 | * Returns: 0 Success. | |
5220 | * EBADF Bad file descriptor. | |
5221 | * EINVAL File descriptor had type that cannot be sent, misc. other errors. | |
5222 | * EFAULT Address at which to store port name is not valid. | |
5223 | * EAGAIN Resource shortage. | |
5224 | * | |
5225 | * Implicit returns: | |
5226 | * On success, name of send right is stored at user-specified address. | |
5227 | */ | |
5228 | int | |
5229 | fileport_makeport(proc_t p, struct fileport_makeport_args *uap, | |
5230 | __unused int *retval) | |
5231 | { | |
5232 | int err; | |
5233 | int fd = uap->fd; | |
5234 | user_addr_t user_portaddr = uap->portnamep; | |
5235 | struct fileproc *fp = FILEPROC_NULL; | |
5236 | struct fileglob *fg = NULL; | |
5237 | ipc_port_t fileport; | |
5238 | mach_port_name_t name = MACH_PORT_NULL; | |
5239 | ||
5240 | proc_fdlock(p); | |
5241 | err = fp_lookup(p, fd, &fp, 1); | |
5242 | if (err != 0) { | |
5243 | goto out_unlock; | |
5244 | } | |
5245 | ||
5246 | if (!file_issendable(p, fp)) { | |
5247 | err = EINVAL; | |
5248 | goto out_unlock; | |
5249 | } | |
5250 | ||
5251 | if (FP_ISGUARDED(fp, GUARD_FILEPORT)) { | |
5252 | err = fp_guard_exception(p, fd, fp, kGUARD_EXC_FILEPORT); | |
5253 | goto out_unlock; | |
5254 | } | |
5255 | ||
5256 | /* Dropped when port is deallocated */ | |
5257 | fg = fp->f_fglob; | |
5258 | fg_ref(fp); | |
5259 | ||
5260 | proc_fdunlock(p); | |
5261 | ||
5262 | /* Allocate and initialize a port */ | |
5263 | fileport = fileport_alloc(fg); | |
5264 | if (fileport == IPC_PORT_NULL) { | |
5265 | err = EAGAIN; | |
5266 | fg_drop(fp); | |
5267 | goto out; | |
5268 | } | |
5269 | ||
5270 | /* Add an entry. Deallocates port on failure. */ | |
5271 | name = ipc_port_copyout_send(fileport, get_task_ipcspace(p->task)); | |
5272 | if (!MACH_PORT_VALID(name)) { | |
5273 | err = EINVAL; | |
5274 | goto out; | |
5275 | } | |
5276 | ||
5277 | err = copyout(&name, user_portaddr, sizeof(mach_port_name_t)); | |
5278 | if (err != 0) { | |
5279 | goto out; | |
5280 | } | |
5281 | ||
5282 | /* Tag the fileglob for debugging purposes */ | |
5283 | lck_mtx_lock_spin(&fg->fg_lock); | |
5284 | fg->fg_lflags |= FG_PORTMADE; | |
5285 | lck_mtx_unlock(&fg->fg_lock); | |
5286 | ||
5287 | fp_drop(p, fd, fp, 0); | |
5288 | ||
5289 | return 0; | |
5290 | ||
5291 | out_unlock: | |
5292 | proc_fdunlock(p); | |
5293 | out: | |
5294 | if (MACH_PORT_VALID(name)) { | |
5295 | /* Don't care if another thread races us to deallocate the entry */ | |
5296 | (void) mach_port_deallocate(get_task_ipcspace(p->task), name); | |
5297 | } | |
5298 | ||
5299 | if (fp != FILEPROC_NULL) { | |
5300 | fp_drop(p, fd, fp, 0); | |
5301 | } | |
5302 | ||
5303 | return err; | |
5304 | } | |
5305 | ||
5306 | void | |
5307 | fileport_releasefg(struct fileglob *fg) | |
5308 | { | |
5309 | (void)closef_locked(NULL, fg, PROC_NULL); | |
5310 | ||
5311 | return; | |
5312 | } | |
5313 | ||
5314 | ||
5315 | /* | |
5316 | * fileport_makefd | |
5317 | * | |
5318 | * Description: Obtain the file descriptor for a given Mach send right. | |
5319 | * | |
5320 | * Parameters: p Process calling fileport | |
5321 | * uap->port Name of send right to file port. | |
5322 | * | |
5323 | * Returns: 0 Success | |
5324 | * EINVAL Invalid Mach port name, or port is not for a file. | |
5325 | * fdalloc:EMFILE | |
5326 | * fdalloc:ENOMEM Unable to allocate fileproc or extend file table. | |
5327 | * | |
5328 | * Implicit returns: | |
5329 | * *retval (modified) The new descriptor | |
5330 | */ | |
5331 | int | |
5332 | fileport_makefd(proc_t p, struct fileport_makefd_args *uap, int32_t *retval) | |
5333 | { | |
5334 | struct fileglob *fg; | |
5335 | struct fileproc *fp = FILEPROC_NULL; | |
5336 | ipc_port_t port = IPC_PORT_NULL; | |
5337 | mach_port_name_t send = uap->port; | |
5338 | kern_return_t res; | |
5339 | int fd; | |
5340 | int err; | |
5341 | ||
5342 | res = ipc_object_copyin(get_task_ipcspace(p->task), | |
5343 | send, MACH_MSG_TYPE_COPY_SEND, &port); | |
5344 | ||
5345 | if (res != KERN_SUCCESS) { | |
5346 | err = EINVAL; | |
5347 | goto out; | |
5348 | } | |
5349 | ||
5350 | fg = fileport_port_to_fileglob(port); | |
5351 | if (fg == NULL) { | |
5352 | err = EINVAL; | |
5353 | goto out; | |
5354 | } | |
5355 | ||
5356 | fp = fileproc_alloc_init(NULL); | |
5357 | if (fp == FILEPROC_NULL) { | |
5358 | err = ENOMEM; | |
5359 | goto out; | |
5360 | } | |
5361 | ||
5362 | fp->f_fglob = fg; | |
5363 | fg_ref(fp); | |
5364 | ||
5365 | proc_fdlock(p); | |
5366 | err = fdalloc(p, 0, &fd); | |
5367 | if (err != 0) { | |
5368 | proc_fdunlock(p); | |
5369 | goto out; | |
5370 | } | |
5371 | *fdflags(p, fd) |= UF_EXCLOSE; | |
5372 | ||
5373 | procfdtbl_releasefd(p, fd, fp); | |
5374 | proc_fdunlock(p); | |
5375 | ||
5376 | *retval = fd; | |
5377 | err = 0; | |
5378 | out: | |
5379 | if ((fp != NULL) && (0 != err)) { | |
5380 | fileproc_free(fp); | |
5381 | } | |
5382 | ||
5383 | if (IPC_PORT_NULL != port) { | |
5384 | ipc_port_release_send(port); | |
5385 | } | |
5386 | ||
5387 | return err; | |
5388 | } | |
5389 | ||
5390 | ||
5391 | /* | |
5392 | * dupfdopen | |
5393 | * | |
5394 | * Description: Duplicate the specified descriptor to a free descriptor; | |
5395 | * this is the second half of fdopen(), above. | |
5396 | * | |
5397 | * Parameters: fdp filedesc pointer to fill in | |
5398 | * indx fd to dup to | |
5399 | * dfd fd to dup from | |
5400 | * mode mode to set on new fd | |
5401 | * error command code | |
5402 | * | |
5403 | * Returns: 0 Success | |
5404 | * EBADF Source fd is bad | |
5405 | * EACCES Requested mode not allowed | |
5406 | * !0 'error', if not ENODEV or | |
5407 | * ENXIO | |
5408 | * | |
5409 | * Notes: XXX This is not thread safe; see fdopen() above | |
5410 | */ | |
5411 | int | |
5412 | dupfdopen(struct filedesc *fdp, int indx, int dfd, int flags, int error) | |
5413 | { | |
5414 | struct fileproc *wfp; | |
5415 | struct fileproc *fp; | |
5416 | #if CONFIG_MACF | |
5417 | int myerror; | |
5418 | #endif | |
5419 | proc_t p = current_proc(); | |
5420 | ||
5421 | /* | |
5422 | * If the to-be-dup'd fd number is greater than the allowed number | |
5423 | * of file descriptors, or the fd to be dup'd has already been | |
5424 | * closed, reject. Note, check for new == old is necessary as | |
5425 | * falloc could allocate an already closed to-be-dup'd descriptor | |
5426 | * as the new descriptor. | |
5427 | */ | |
5428 | proc_fdlock(p); | |
5429 | ||
5430 | fp = fdp->fd_ofiles[indx]; | |
5431 | if (dfd < 0 || dfd >= fdp->fd_nfiles || | |
5432 | (wfp = fdp->fd_ofiles[dfd]) == NULL || wfp == fp || | |
5433 | (fdp->fd_ofileflags[dfd] & UF_RESERVED)) { | |
5434 | ||
5435 | proc_fdunlock(p); | |
5436 | return (EBADF); | |
5437 | } | |
5438 | #if CONFIG_MACF | |
5439 | myerror = mac_file_check_dup(proc_ucred(p), wfp->f_fglob, dfd); | |
5440 | if (myerror) { | |
5441 | proc_fdunlock(p); | |
5442 | return (myerror); | |
5443 | } | |
5444 | #endif | |
5445 | /* | |
5446 | * There are two cases of interest here. | |
5447 | * | |
5448 | * For ENODEV simply dup (dfd) to file descriptor | |
5449 | * (indx) and return. | |
5450 | * | |
5451 | * For ENXIO steal away the file structure from (dfd) and | |
5452 | * store it in (indx). (dfd) is effectively closed by | |
5453 | * this operation. | |
5454 | * | |
5455 | * Any other error code is just returned. | |
5456 | */ | |
5457 | switch (error) { | |
5458 | case ENODEV: | |
5459 | if (FP_ISGUARDED(wfp, GUARD_DUP)) { | |
5460 | proc_fdunlock(p); | |
5461 | return (EPERM); | |
5462 | } | |
5463 | ||
5464 | /* | |
5465 | * Check that the mode the file is being opened for is a | |
5466 | * subset of the mode of the existing descriptor. | |
5467 | */ | |
5468 | if (((flags & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) { | |
5469 | proc_fdunlock(p); | |
5470 | return (EACCES); | |
5471 | } | |
5472 | if (indx > fdp->fd_lastfile) | |
5473 | fdp->fd_lastfile = indx; | |
5474 | (void)fg_ref(wfp); | |
5475 | ||
5476 | if (fp->f_fglob) | |
5477 | fg_free(fp->f_fglob); | |
5478 | fp->f_fglob = wfp->f_fglob; | |
5479 | ||
5480 | fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd] | | |
5481 | (flags & O_CLOEXEC) ? UF_EXCLOSE : 0; | |
5482 | ||
5483 | proc_fdunlock(p); | |
5484 | return (0); | |
5485 | ||
5486 | default: | |
5487 | proc_fdunlock(p); | |
5488 | return (error); | |
5489 | } | |
5490 | /* NOTREACHED */ | |
5491 | } | |
5492 | ||
5493 | ||
5494 | /* | |
5495 | * fg_ref | |
5496 | * | |
5497 | * Description: Add a reference to a fileglob by fileproc | |
5498 | * | |
5499 | * Parameters: fp fileproc containing fileglob | |
5500 | * pointer | |
5501 | * | |
5502 | * Returns: void | |
5503 | * | |
5504 | * Notes: XXX Should use OSAddAtomic? | |
5505 | */ | |
5506 | void | |
5507 | fg_ref(struct fileproc * fp) | |
5508 | { | |
5509 | struct fileglob *fg; | |
5510 | ||
5511 | fg = fp->f_fglob; | |
5512 | ||
5513 | lck_mtx_lock_spin(&fg->fg_lock); | |
5514 | ||
5515 | #if DIAGNOSTIC | |
5516 | if ((fp->f_flags & ~((unsigned int)FP_VALID_FLAGS)) != 0) | |
5517 | panic("fg_ref: invalid bits on fp %p", fp); | |
5518 | ||
5519 | if (fg->fg_count == 0) | |
5520 | panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p", | |
5521 | fp, fg); | |
5522 | #endif | |
5523 | fg->fg_count++; | |
5524 | lck_mtx_unlock(&fg->fg_lock); | |
5525 | } | |
5526 | ||
5527 | ||
5528 | /* | |
5529 | * fg_drop | |
5530 | * | |
5531 | * Description: Remove a reference to a fileglob by fileproc | |
5532 | * | |
5533 | * Parameters: fp fileproc containing fileglob | |
5534 | * pointer | |
5535 | * | |
5536 | * Returns: void | |
5537 | * | |
5538 | * Notes: XXX Should use OSAddAtomic? | |
5539 | */ | |
5540 | void | |
5541 | fg_drop(struct fileproc * fp) | |
5542 | { | |
5543 | struct fileglob *fg; | |
5544 | ||
5545 | fg = fp->f_fglob; | |
5546 | lck_mtx_lock_spin(&fg->fg_lock); | |
5547 | fg->fg_count--; | |
5548 | lck_mtx_unlock(&fg->fg_lock); | |
5549 | } | |
5550 | ||
5551 | #if SOCKETS | |
5552 | /* | |
5553 | * fg_insertuipc_mark | |
5554 | * | |
5555 | * Description: Mark fileglob for insertion onto message queue if needed | |
5556 | * Also takes fileglob reference | |
5557 | * | |
5558 | * Parameters: fg Fileglob pointer to insert | |
5559 | * | |
5560 | * Returns: true, if the fileglob needs to be inserted onto msg queue | |
5561 | * | |
5562 | * Locks: Takes and drops fg_lock, potentially many times | |
5563 | */ | |
5564 | boolean_t | |
5565 | fg_insertuipc_mark(struct fileglob * fg) | |
5566 | { | |
5567 | boolean_t insert = FALSE; | |
5568 | ||
5569 | lck_mtx_lock_spin(&fg->fg_lock); | |
5570 | while (fg->fg_lflags & FG_RMMSGQ) { | |
5571 | lck_mtx_convert_spin(&fg->fg_lock); | |
5572 | ||
5573 | fg->fg_lflags |= FG_WRMMSGQ; | |
5574 | msleep(&fg->fg_lflags, &fg->fg_lock, 0, "fg_insertuipc", NULL); | |
5575 | } | |
5576 | ||
5577 | fg->fg_count++; | |
5578 | fg->fg_msgcount++; | |
5579 | if (fg->fg_msgcount == 1) { | |
5580 | fg->fg_lflags |= FG_INSMSGQ; | |
5581 | insert = TRUE; | |
5582 | } | |
5583 | lck_mtx_unlock(&fg->fg_lock); | |
5584 | return (insert); | |
5585 | } | |
5586 | ||
5587 | /* | |
5588 | * fg_insertuipc | |
5589 | * | |
5590 | * Description: Insert marked fileglob onto message queue | |
5591 | * | |
5592 | * Parameters: fg Fileglob pointer to insert | |
5593 | * | |
5594 | * Returns: void | |
5595 | * | |
5596 | * Locks: Takes and drops fg_lock & uipc_lock | |
5597 | * DO NOT call this function with proc_fdlock held as unp_gc() | |
5598 | * can potentially try to acquire proc_fdlock, which can result | |
5599 | * in a deadlock if this function is in unp_gc_wait(). | |
5600 | */ | |
5601 | void | |
5602 | fg_insertuipc(struct fileglob * fg) | |
5603 | { | |
5604 | if (fg->fg_lflags & FG_INSMSGQ) { | |
5605 | lck_mtx_lock_spin(uipc_lock); | |
5606 | unp_gc_wait(); | |
5607 | LIST_INSERT_HEAD(&fmsghead, fg, f_msglist); | |
5608 | lck_mtx_unlock(uipc_lock); | |
5609 | lck_mtx_lock(&fg->fg_lock); | |
5610 | fg->fg_lflags &= ~FG_INSMSGQ; | |
5611 | if (fg->fg_lflags & FG_WINSMSGQ) { | |
5612 | fg->fg_lflags &= ~FG_WINSMSGQ; | |
5613 | wakeup(&fg->fg_lflags); | |
5614 | } | |
5615 | lck_mtx_unlock(&fg->fg_lock); | |
5616 | } | |
5617 | } | |
5618 | ||
5619 | /* | |
5620 | * fg_removeuipc_mark | |
5621 | * | |
5622 | * Description: Mark the fileglob for removal from message queue if needed | |
5623 | * Also releases fileglob message queue reference | |
5624 | * | |
5625 | * Parameters: fg Fileglob pointer to remove | |
5626 | * | |
5627 | * Returns: true, if the fileglob needs to be removed from msg queue | |
5628 | * | |
5629 | * Locks: Takes and drops fg_lock, potentially many times | |
5630 | */ | |
5631 | boolean_t | |
5632 | fg_removeuipc_mark(struct fileglob * fg) | |
5633 | { | |
5634 | boolean_t remove = FALSE; | |
5635 | ||
5636 | lck_mtx_lock_spin(&fg->fg_lock); | |
5637 | while (fg->fg_lflags & FG_INSMSGQ) { | |
5638 | lck_mtx_convert_spin(&fg->fg_lock); | |
5639 | ||
5640 | fg->fg_lflags |= FG_WINSMSGQ; | |
5641 | msleep(&fg->fg_lflags, &fg->fg_lock, 0, "fg_removeuipc", NULL); | |
5642 | } | |
5643 | fg->fg_msgcount--; | |
5644 | if (fg->fg_msgcount == 0) { | |
5645 | fg->fg_lflags |= FG_RMMSGQ; | |
5646 | remove = TRUE; | |
5647 | } | |
5648 | lck_mtx_unlock(&fg->fg_lock); | |
5649 | return (remove); | |
5650 | } | |
5651 | ||
5652 | /* | |
5653 | * fg_removeuipc | |
5654 | * | |
5655 | * Description: Remove marked fileglob from message queue | |
5656 | * | |
5657 | * Parameters: fg Fileglob pointer to remove | |
5658 | * | |
5659 | * Returns: void | |
5660 | * | |
5661 | * Locks: Takes and drops fg_lock & uipc_lock | |
5662 | * DO NOT call this function with proc_fdlock held as unp_gc() | |
5663 | * can potentially try to acquire proc_fdlock, which can result | |
5664 | * in a deadlock if this function is in unp_gc_wait(). | |
5665 | */ | |
5666 | void | |
5667 | fg_removeuipc(struct fileglob * fg) | |
5668 | { | |
5669 | if (fg->fg_lflags & FG_RMMSGQ) { | |
5670 | lck_mtx_lock_spin(uipc_lock); | |
5671 | unp_gc_wait(); | |
5672 | LIST_REMOVE(fg, f_msglist); | |
5673 | lck_mtx_unlock(uipc_lock); | |
5674 | lck_mtx_lock(&fg->fg_lock); | |
5675 | fg->fg_lflags &= ~FG_RMMSGQ; | |
5676 | if (fg->fg_lflags & FG_WRMMSGQ) { | |
5677 | fg->fg_lflags &= ~FG_WRMMSGQ; | |
5678 | wakeup(&fg->fg_lflags); | |
5679 | } | |
5680 | lck_mtx_unlock(&fg->fg_lock); | |
5681 | } | |
5682 | } | |
5683 | #endif /* SOCKETS */ | |
5684 | ||
5685 | /* | |
5686 | * fo_read | |
5687 | * | |
5688 | * Description: Generic fileops read indirected through the fileops pointer | |
5689 | * in the fileproc structure | |
5690 | * | |
5691 | * Parameters: fp fileproc structure pointer | |
5692 | * uio user I/O structure pointer | |
5693 | * flags FOF_ flags | |
5694 | * ctx VFS context for operation | |
5695 | * | |
5696 | * Returns: 0 Success | |
5697 | * !0 Errno from read | |
5698 | */ | |
5699 | int | |
5700 | fo_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx) | |
5701 | { | |
5702 | return ((*fp->f_ops->fo_read)(fp, uio, flags, ctx)); | |
5703 | } | |
5704 | ||
5705 | ||
5706 | /* | |
5707 | * fo_write | |
5708 | * | |
5709 | * Description: Generic fileops write indirected through the fileops pointer | |
5710 | * in the fileproc structure | |
5711 | * | |
5712 | * Parameters: fp fileproc structure pointer | |
5713 | * uio user I/O structure pointer | |
5714 | * flags FOF_ flags | |
5715 | * ctx VFS context for operation | |
5716 | * | |
5717 | * Returns: 0 Success | |
5718 | * !0 Errno from write | |
5719 | */ | |
5720 | int | |
5721 | fo_write(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx) | |
5722 | { | |
5723 | return((*fp->f_ops->fo_write)(fp, uio, flags, ctx)); | |
5724 | } | |
5725 | ||
5726 | ||
5727 | /* | |
5728 | * fo_ioctl | |
5729 | * | |
5730 | * Description: Generic fileops ioctl indirected through the fileops pointer | |
5731 | * in the fileproc structure | |
5732 | * | |
5733 | * Parameters: fp fileproc structure pointer | |
5734 | * com ioctl command | |
5735 | * data pointer to internalized copy | |
5736 | * of user space ioctl command | |
5737 | * parameter data in kernel space | |
5738 | * ctx VFS context for operation | |
5739 | * | |
5740 | * Returns: 0 Success | |
5741 | * !0 Errno from ioctl | |
5742 | * | |
5743 | * Locks: The caller is assumed to have held the proc_fdlock; this | |
5744 | * function releases and reacquires this lock. If the caller | |
5745 | * accesses data protected by this lock prior to calling this | |
5746 | * function, it will need to revalidate/reacquire any cached | |
5747 | * protected data obtained prior to the call. | |
5748 | */ | |
5749 | int | |
5750 | fo_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx) | |
5751 | { | |
5752 | int error; | |
5753 | ||
5754 | proc_fdunlock(vfs_context_proc(ctx)); | |
5755 | error = (*fp->f_ops->fo_ioctl)(fp, com, data, ctx); | |
5756 | proc_fdlock(vfs_context_proc(ctx)); | |
5757 | return(error); | |
5758 | } | |
5759 | ||
5760 | ||
5761 | /* | |
5762 | * fo_select | |
5763 | * | |
5764 | * Description: Generic fileops select indirected through the fileops pointer | |
5765 | * in the fileproc structure | |
5766 | * | |
5767 | * Parameters: fp fileproc structure pointer | |
5768 | * which select which | |
5769 | * wql pointer to wait queue list | |
5770 | * ctx VFS context for operation | |
5771 | * | |
5772 | * Returns: 0 Success | |
5773 | * !0 Errno from select | |
5774 | */ | |
5775 | int | |
5776 | fo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx) | |
5777 | { | |
5778 | return((*fp->f_ops->fo_select)(fp, which, wql, ctx)); | |
5779 | } | |
5780 | ||
5781 | ||
5782 | /* | |
5783 | * fo_close | |
5784 | * | |
5785 | * Description: Generic fileops close indirected through the fileops pointer | |
5786 | * in the fileproc structure | |
5787 | * | |
5788 | * Parameters: fp fileproc structure pointer for | |
5789 | * file to close | |
5790 | * ctx VFS context for operation | |
5791 | * | |
5792 | * Returns: 0 Success | |
5793 | * !0 Errno from close | |
5794 | */ | |
5795 | int | |
5796 | fo_close(struct fileglob *fg, vfs_context_t ctx) | |
5797 | { | |
5798 | return((*fg->fg_ops->fo_close)(fg, ctx)); | |
5799 | } | |
5800 | ||
5801 | ||
5802 | /* | |
5803 | * fo_kqfilter | |
5804 | * | |
5805 | * Description: Generic fileops kqueue filter indirected through the fileops | |
5806 | * pointer in the fileproc structure | |
5807 | * | |
5808 | * Parameters: fp fileproc structure pointer | |
5809 | * kn pointer to knote to filter on | |
5810 | * ctx VFS context for operation | |
5811 | * | |
5812 | * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data | |
5813 | * 0 Filter is not active | |
5814 | * !0 Filter is active | |
5815 | */ | |
5816 | int | |
5817 | fo_kqfilter(struct fileproc *fp, struct knote *kn, vfs_context_t ctx) | |
5818 | { | |
5819 | return ((*fp->f_ops->fo_kqfilter)(fp, kn, ctx)); | |
5820 | } | |
5821 | ||
5822 | /* | |
5823 | * The ability to send a file descriptor to another | |
5824 | * process is opt-in by file type. | |
5825 | */ | |
5826 | boolean_t | |
5827 | file_issendable(proc_t p, struct fileproc *fp) | |
5828 | { | |
5829 | proc_fdlock_assert(p, LCK_MTX_ASSERT_OWNED); | |
5830 | ||
5831 | switch (fp->f_type) { | |
5832 | case DTYPE_VNODE: | |
5833 | case DTYPE_SOCKET: | |
5834 | case DTYPE_PIPE: | |
5835 | case DTYPE_PSXSHM: | |
5836 | return (0 == (fp->f_fglob->fg_lflags & FG_CONFINED)); | |
5837 | default: | |
5838 | /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */ | |
5839 | return FALSE; | |
5840 | } | |
5841 | } | |
5842 | ||
5843 | ||
5844 | struct fileproc * | |
5845 | fileproc_alloc_init(__unused void *arg) | |
5846 | { | |
5847 | struct fileproc *fp; | |
5848 | ||
5849 | MALLOC_ZONE(fp, struct fileproc *, sizeof (*fp), M_FILEPROC, M_WAITOK); | |
5850 | if (fp) | |
5851 | bzero(fp, sizeof (*fp)); | |
5852 | ||
5853 | return (fp); | |
5854 | } | |
5855 | ||
5856 | void | |
5857 | fileproc_free(struct fileproc *fp) | |
5858 | { | |
5859 | switch (FILEPROC_TYPE(fp)) { | |
5860 | case FTYPE_SIMPLE: | |
5861 | FREE_ZONE(fp, sizeof (*fp), M_FILEPROC); | |
5862 | break; | |
5863 | case FTYPE_GUARDED: | |
5864 | guarded_fileproc_free(fp); | |
5865 | break; | |
5866 | default: | |
5867 | panic("%s: corrupt fp %p flags %x", __func__, fp, fp->f_flags); | |
5868 | } | |
5869 | } |