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