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