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