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