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