]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_descrip.c
f69db0d52e11b4d1b87df54827f88827d2bbf72f
[apple/xnu.git] / bsd / kern / kern_descrip.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
69 */
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/filedesc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode_internal.h>
76 #include <sys/proc_internal.h>
77 #include <sys/kauth.h>
78 #include <sys/file_internal.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/stat.h>
82 #include <sys/ioctl.h>
83 #include <sys/fcntl.h>
84 #include <sys/malloc.h>
85 #include <sys/mman.h>
86 #include <sys/syslog.h>
87 #include <sys/unistd.h>
88 #include <sys/resourcevar.h>
89 #include <sys/aio_kern.h>
90 #include <sys/ev.h>
91 #include <kern/lock.h>
92
93 #include <bsm/audit_kernel.h>
94
95 #include <sys/mount_internal.h>
96 #include <sys/kdebug.h>
97 #include <sys/sysproto.h>
98 #include <sys/pipe.h>
99 #include <kern/kern_types.h>
100 #include <kern/kalloc.h>
101 #include <libkern/OSAtomic.h>
102
103 struct psemnode;
104 struct pshmnode;
105
106 int fdopen(dev_t dev, int mode, int type, struct proc *p);
107 int ogetdtablesize(struct proc *p, void *uap, register_t *retval);
108 int finishdup(struct proc * p, struct filedesc *fdp, int old, int new, register_t *retval);
109
110 int closef(struct fileglob *fg, struct proc *p);
111 int falloc_locked(struct proc *p, struct fileproc **resultfp, int *resultfd, int locked);
112 void fddrop(struct proc *p, int fd);
113 int fdgetf_noref(struct proc *p, int fd, struct fileproc **resultfp);
114 void fg_drop(struct fileproc * fp);
115 void fg_free(struct fileglob *fg);
116 void fg_ref(struct fileproc * fp);
117
118 static int closef_finish(struct fileproc *fp, struct fileglob *fg, struct proc *p);
119
120 extern void file_lock_init(void);
121 extern int is_suser(void);
122 extern int kqueue_stat(struct fileproc *fp, struct stat *st, struct proc *p);
123 extern int soo_stat(struct socket *so, struct stat *ub);
124 extern int vn_path_package_check(vnode_t vp, char *path, int pathlen, int *component);
125
126 extern kauth_scope_t kauth_scope_fileop;
127
128 #define f_flag f_fglob->fg_flag
129 #define f_type f_fglob->fg_type
130 #define f_msgcount f_fglob->fg_msgcount
131 #define f_cred f_fglob->fg_cred
132 #define f_ops f_fglob->fg_ops
133 #define f_offset f_fglob->fg_offset
134 #define f_data f_fglob->fg_data
135 /*
136 * Descriptor management.
137 */
138 struct filelist filehead; /* head of list of open files */
139 struct fmsglist fmsghead; /* head of list of open files */
140 struct fmsglist fmsg_ithead; /* head of list of open files */
141 int nfiles; /* actual number of open files */
142
143
144 lck_grp_attr_t * file_lck_grp_attr;
145 lck_grp_t * file_lck_grp;
146 lck_attr_t * file_lck_attr;
147
148 lck_mtx_t * uipc_lock;
149 lck_mtx_t * file_iterate_lcok;
150 lck_mtx_t * file_flist_lock;
151
152
153 void
154 file_lock_init(void)
155 {
156
157 /* allocate file lock group attribute and group */
158 file_lck_grp_attr= lck_grp_attr_alloc_init();
159
160 file_lck_grp = lck_grp_alloc_init("file", file_lck_grp_attr);
161
162 /* Allocate file lock attribute */
163 file_lck_attr = lck_attr_alloc_init();
164
165 uipc_lock = lck_mtx_alloc_init(file_lck_grp, file_lck_attr);
166 file_iterate_lcok = lck_mtx_alloc_init(file_lck_grp, file_lck_attr);
167 file_flist_lock = lck_mtx_alloc_init(file_lck_grp, file_lck_attr);
168
169
170
171 }
172
173
174 void
175 proc_fdlock(struct proc *p)
176 {
177 lck_mtx_lock(&p->p_fdmlock);
178 }
179
180 void
181 proc_fdunlock(struct proc *p)
182 {
183 lck_mtx_unlock(&p->p_fdmlock);
184 }
185
186 /*
187 * System calls on descriptors.
188 */
189
190 int
191 getdtablesize(struct proc *p, __unused struct getdtablesize_args *uap, register_t *retval)
192 {
193 proc_fdlock(p);
194 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
195 proc_fdunlock(p);
196
197 return (0);
198 }
199
200 int
201 ogetdtablesize(struct proc *p, __unused void *uap, register_t *retval)
202 {
203 proc_fdlock(p);
204 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, NOFILE);
205 proc_fdunlock(p);
206
207 return (0);
208 }
209
210 static __inline__ void
211 _fdrelse(struct filedesc *fdp, int fd)
212 {
213 if (fd < fdp->fd_freefile)
214 fdp->fd_freefile = fd;
215 #if DIAGNOSTIC
216 if (fd > fdp->fd_lastfile)
217 panic("fdrelse: fd_lastfile inconsistent");
218 #endif
219 fdp->fd_ofiles[fd] = NULL;
220 fdp->fd_ofileflags[fd] = 0;
221
222 while ((fd = fdp->fd_lastfile) > 0 &&
223 fdp->fd_ofiles[fd] == NULL &&
224 !(fdp->fd_ofileflags[fd] & UF_RESERVED))
225 fdp->fd_lastfile--;
226 }
227
228 /*
229 * Duplicate a file descriptor.
230 */
231 /* ARGSUSED */
232 int
233 dup(p, uap, retval)
234 struct proc *p;
235 struct dup_args *uap;
236 register_t *retval;
237 {
238 register struct filedesc *fdp = p->p_fd;
239 register int old = uap->fd;
240 int new, error;
241 struct fileproc *fp;
242
243 proc_fdlock(p);
244 if ( (error = fp_lookup(p, old, &fp, 1)) ) {
245 proc_fdunlock(p);
246 return(error);
247 }
248 if ( (error = fdalloc(p, 0, &new)) ) {
249 fp_drop(p, old, fp, 1);
250 proc_fdunlock(p);
251 return (error);
252 }
253 error = finishdup(p, fdp, old, new, retval);
254 fp_drop(p, old, fp, 1);
255 proc_fdunlock(p);
256
257 return (error);
258 }
259
260 /*
261 * Duplicate a file descriptor to a particular value.
262 */
263 /* ARGSUSED */
264 int
265 dup2(p, uap, retval)
266 struct proc *p;
267 struct dup2_args *uap;
268 register_t *retval;
269 {
270 register struct filedesc *fdp = p->p_fd;
271 register int old = uap->from, new = uap->to;
272 int i, error;
273 struct fileproc *fp;
274
275 proc_fdlock(p);
276
277 if ( (error = fp_lookup(p, old, &fp, 1)) ) {
278 proc_fdunlock(p);
279 return(error);
280 }
281 if (new < 0 ||
282 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
283 new >= maxfiles) {
284 fp_drop(p, old, fp, 1);
285 proc_fdunlock(p);
286 return (EBADF);
287 }
288 if (old == new) {
289 fp_drop(p, old, fp, 1);
290 *retval = new;
291 proc_fdunlock(p);
292 return (0);
293 }
294 if (new < 0 || new >= fdp->fd_nfiles) {
295 if ( (error = fdalloc(p, new, &i)) ) {
296 fp_drop(p, old, fp, 1);
297 proc_fdunlock(p);
298 return (error);
299 }
300 if (new != i) {
301 _fdrelse(fdp, i);
302 goto closeit;
303 }
304 } else {
305 struct fileproc **fpp;
306 char flags;
307 closeit:
308 flags = fdp->fd_ofileflags[new];
309 if ((flags & (UF_RESERVED | UF_CLOSING)) == UF_RESERVED) {
310 fp_drop(p, old, fp, 1);
311 proc_fdunlock(p);
312 return (EBADF);
313 }
314
315 /*
316 * dup2() must succeed even if the close has an error.
317 */
318 if (*(fpp = &fdp->fd_ofiles[new])) {
319 struct fileproc *nfp = *fpp;
320
321 close_internal(p, new, nfp, (CLOSEINT_LOCKED | CLOSEINT_WAITONCLOSE | CLOSEINT_NOFDRELSE | CLOSEINT_NOFDNOREF));
322 *fpp = NULL;
323 }
324 }
325 error = finishdup(p, fdp, old, new, retval);
326 fp_drop(p, old, fp, 1);
327 proc_fdunlock(p);
328
329 return(error);
330 }
331
332 /*
333 * The file control system call.
334 */
335 int
336 fcntl(p, uap, retval)
337 struct proc *p;
338 struct fcntl_args *uap;
339 register_t *retval;
340 {
341 int fd = uap->fd;
342 struct filedesc *fdp = p->p_fd;
343 struct fileproc *fp;
344 char *pop;
345 struct vnode *vp;
346 int i, tmp, error, error2, flg = F_POSIX;
347 struct flock fl;
348 struct vfs_context context;
349 off_t offset;
350 int newmin;
351 daddr64_t lbn, bn;
352 int devBlockSize = 0;
353 unsigned int fflag;
354 user_addr_t argp;
355
356 AUDIT_ARG(fd, uap->fd);
357 AUDIT_ARG(cmd, uap->cmd);
358
359 proc_fdlock(p);
360 if ( (error = fp_lookup(p, fd, &fp, 1)) ) {
361 proc_fdunlock(p);
362 return(error);
363 }
364 context.vc_proc = p;
365 context.vc_ucred = fp->f_cred;
366 if (proc_is64bit(p)) {
367 argp = uap->arg;
368 }
369 else {
370 /* since the arg parameter is defined as a long but may be either
371 * a long or a pointer we must take care to handle sign extension
372 * issues. Our sys call munger will sign extend a long when we are
373 * called from a 32-bit process. Since we can never have an address
374 * greater than 32-bits from a 32-bit process we lop off the top
375 * 32-bits to avoid getting the wrong address
376 */
377 argp = CAST_USER_ADDR_T(uap->arg);
378 }
379
380 pop = &fdp->fd_ofileflags[fd];
381
382 switch (uap->cmd) {
383
384 case F_DUPFD:
385 newmin = CAST_DOWN(int, uap->arg);
386 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
387 newmin >= maxfiles) {
388 error = EINVAL;
389 goto out;
390 }
391 if ( (error = fdalloc(p, newmin, &i)) )
392 goto out;
393 error = finishdup(p, fdp, fd, i, retval);
394 goto out;
395
396 case F_GETFD:
397 *retval = (*pop & UF_EXCLOSE)? 1 : 0;
398 error = 0;
399 goto out;
400
401 case F_SETFD:
402 *pop = (*pop &~ UF_EXCLOSE) |
403 (uap->arg & 1)? UF_EXCLOSE : 0;
404 error = 0;
405 goto out;
406
407 case F_GETFL:
408 *retval = OFLAGS(fp->f_flag);
409 error = 0;
410 goto out;
411
412 case F_SETFL:
413 fp->f_flag &= ~FCNTLFLAGS;
414 tmp = CAST_DOWN(int, uap->arg);
415 fp->f_flag |= FFLAGS(tmp) & FCNTLFLAGS;
416 tmp = fp->f_flag & FNONBLOCK;
417 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
418 if (error)
419 goto out;
420 tmp = fp->f_flag & FASYNC;
421 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
422 if (!error)
423 goto out;
424 fp->f_flag &= ~FNONBLOCK;
425 tmp = 0;
426 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
427 goto out;
428
429 case F_GETOWN:
430 if (fp->f_type == DTYPE_SOCKET) {
431 *retval = ((struct socket *)fp->f_data)->so_pgid;
432 error = 0;
433 goto out;
434 }
435 error = fo_ioctl(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
436 *retval = -*retval;
437 goto out;
438
439 case F_SETOWN:
440 tmp = CAST_DOWN(pid_t, uap->arg);
441 if (fp->f_type == DTYPE_SOCKET) {
442 ((struct socket *)fp->f_data)->so_pgid = tmp;
443 error =0;
444 goto out;
445 }
446 if (fp->f_type == DTYPE_PIPE) {
447 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
448 goto out;
449 }
450
451 if (tmp <= 0) {
452 tmp = -tmp;
453 } else {
454 struct proc *p1 = pfind(tmp);
455 if (p1 == 0) {
456 error = ESRCH;
457 goto out;
458 }
459 tmp = (int)p1->p_pgrp->pg_id;
460 }
461 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
462 goto out;
463
464 case F_SETLKW:
465 flg |= F_WAIT;
466 /* Fall into F_SETLK */
467
468 case F_SETLK:
469 if (fp->f_type != DTYPE_VNODE) {
470 error = EBADF;
471 goto out;
472 }
473 vp = (struct vnode *)fp->f_data;
474
475 fflag = fp->f_flag;
476 offset = fp->f_offset;
477 proc_fdunlock(p);
478
479 /* Copy in the lock structure */
480 error = copyin(argp, (caddr_t)&fl, sizeof (fl));
481 if (error) {
482 goto outdrop;
483 }
484 if ( (error = vnode_getwithref(vp)) ) {
485 goto outdrop;
486 }
487 if (fl.l_whence == SEEK_CUR)
488 fl.l_start += offset;
489
490 switch (fl.l_type) {
491
492 case F_RDLCK:
493 if ((fflag & FREAD) == 0) {
494 (void)vnode_put(vp);
495 error = EBADF;
496 goto outdrop;
497 }
498 OSBitOrAtomic(P_LADVLOCK, &p->p_ladvflag);
499 error = VNOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg, &context);
500 (void)vnode_put(vp);
501 goto outdrop;
502
503 case F_WRLCK:
504 if ((fflag & FWRITE) == 0) {
505 (void)vnode_put(vp);
506 error = EBADF;
507 goto outdrop;
508 }
509 OSBitOrAtomic(P_LADVLOCK, &p->p_ladvflag);
510 error = VNOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg, &context);
511 (void)vnode_put(vp);
512 goto outdrop;
513
514 case F_UNLCK:
515 error = VNOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
516 F_POSIX, &context);
517 (void)vnode_put(vp);
518 goto outdrop;
519
520 default:
521 (void)vnode_put(vp);
522 error = EINVAL;
523 goto outdrop;
524 }
525
526 case F_GETLK:
527 if (fp->f_type != DTYPE_VNODE) {
528 error = EBADF;
529 goto out;
530 }
531 vp = (struct vnode *)fp->f_data;
532
533 offset = fp->f_offset;
534 proc_fdunlock(p);
535
536 /* Copy in the lock structure */
537 error = copyin(argp, (caddr_t)&fl, sizeof (fl));
538 if (error)
539 goto outdrop;
540
541 if ( (error = vnode_getwithref(vp)) == 0 ) {
542 if (fl.l_whence == SEEK_CUR)
543 fl.l_start += offset;
544
545 error = VNOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX, &context);
546
547 (void)vnode_put(vp);
548
549 if (error == 0)
550 error = copyout((caddr_t)&fl, argp, sizeof (fl));
551 }
552 goto outdrop;
553
554 case F_PREALLOCATE: {
555 fstore_t alloc_struct; /* structure for allocate command */
556 u_int32_t alloc_flags = 0;
557
558 if (fp->f_type != DTYPE_VNODE) {
559 error = EBADF;
560 goto out;
561 }
562
563 vp = (struct vnode *)fp->f_data;
564 proc_fdunlock(p);
565
566 /* make sure that we have write permission */
567 if ((fp->f_flag & FWRITE) == 0) {
568 error = EBADF;
569 goto outdrop;
570 }
571
572 error = copyin(argp, (caddr_t)&alloc_struct, sizeof (alloc_struct));
573 if (error)
574 goto outdrop;
575
576 /* now set the space allocated to 0 */
577 alloc_struct.fst_bytesalloc = 0;
578
579 /*
580 * Do some simple parameter checking
581 */
582
583 /* set up the flags */
584
585 alloc_flags |= PREALLOCATE;
586
587 if (alloc_struct.fst_flags & F_ALLOCATECONTIG)
588 alloc_flags |= ALLOCATECONTIG;
589
590 if (alloc_struct.fst_flags & F_ALLOCATEALL)
591 alloc_flags |= ALLOCATEALL;
592
593 /*
594 * Do any position mode specific stuff. The only
595 * position mode supported now is PEOFPOSMODE
596 */
597
598 switch (alloc_struct.fst_posmode) {
599
600 case F_PEOFPOSMODE:
601 if (alloc_struct.fst_offset != 0) {
602 error = EINVAL;
603 goto outdrop;
604 }
605
606 alloc_flags |= ALLOCATEFROMPEOF;
607 break;
608
609 case F_VOLPOSMODE:
610 if (alloc_struct.fst_offset <= 0) {
611 error = EINVAL;
612 goto outdrop;
613 }
614
615 alloc_flags |= ALLOCATEFROMVOL;
616 break;
617
618 default: {
619 error = EINVAL;
620 goto outdrop;
621 }
622 }
623 if ( (error = vnode_getwithref(vp)) == 0 ) {
624 /*
625 * call allocate to get the space
626 */
627 error = VNOP_ALLOCATE(vp,alloc_struct.fst_length,alloc_flags,
628 &alloc_struct.fst_bytesalloc, alloc_struct.fst_offset,
629 &context);
630 (void)vnode_put(vp);
631
632 error2 = copyout((caddr_t)&alloc_struct, argp, sizeof (alloc_struct));
633
634 if (error == 0)
635 error = error2;
636 }
637 goto outdrop;
638
639 }
640 case F_SETSIZE:
641 if (fp->f_type != DTYPE_VNODE) {
642 error = EBADF;
643 goto out;
644 }
645 proc_fdunlock(p);
646
647 error = copyin(argp, (caddr_t)&offset, sizeof (off_t));
648 if (error)
649 goto outdrop;
650
651 /*
652 * Make sure that we are root. Growing a file
653 * without zero filling the data is a security hole
654 * root would have access anyway so we'll allow it
655 */
656
657 if (!is_suser()) {
658 error = EACCES;
659 goto outdrop;
660 }
661 vp = (struct vnode *)fp->f_data;
662
663 if ( (error = vnode_getwithref(vp)) == 0 ) {
664 /*
665 * set the file size
666 */
667 error = vnode_setsize(vp, offset, IO_NOZEROFILL, &context);
668
669 (void)vnode_put(vp);
670 }
671 goto outdrop;
672
673 case F_RDAHEAD:
674 if (fp->f_type != DTYPE_VNODE) {
675 error = EBADF;
676 goto out;
677 }
678 vp = (struct vnode *)fp->f_data;
679 proc_fdunlock(p);
680
681 if ( (error = vnode_getwithref(vp)) == 0) {
682 if (uap->arg)
683 vnode_clearnoreadahead(vp);
684 else
685 vnode_setnoreadahead(vp);
686
687 (void)vnode_put(vp);
688 }
689 goto outdrop;
690
691 case F_NOCACHE:
692 if (fp->f_type != DTYPE_VNODE) {
693 error = EBADF;
694 goto out;
695 }
696 vp = (struct vnode *)fp->f_data;
697 proc_fdunlock(p);
698
699 if ( (error = vnode_getwithref(vp)) == 0 ) {
700 if (uap->arg)
701 vnode_setnocache(vp);
702 else
703 vnode_clearnocache(vp);
704
705 (void)vnode_put(vp);
706 }
707 goto outdrop;
708
709 case F_GLOBAL_NOCACHE:
710 if (fp->f_type != DTYPE_VNODE) {
711 error = EBADF;
712 goto out;
713 }
714 vp = (struct vnode *)fp->f_data;
715 proc_fdunlock(p);
716
717 if ( (error = vnode_getwithref(vp)) == 0 ) {
718
719 *retval = vnode_isnocache(vp);
720
721 if (uap->arg)
722 vnode_setnocache(vp);
723 else
724 vnode_clearnocache(vp);
725
726 (void)vnode_put(vp);
727 }
728 goto outdrop;
729
730 case F_RDADVISE: {
731 struct radvisory ra_struct;
732
733 if (fp->f_type != DTYPE_VNODE) {
734 error = EBADF;
735 goto out;
736 }
737 vp = (struct vnode *)fp->f_data;
738 proc_fdunlock(p);
739
740 if ( (error = copyin(argp, (caddr_t)&ra_struct, sizeof (ra_struct))) )
741 goto outdrop;
742 if ( (error = vnode_getwithref(vp)) == 0 ) {
743 error = VNOP_IOCTL(vp, F_RDADVISE, (caddr_t)&ra_struct, 0, &context);
744
745 (void)vnode_put(vp);
746 }
747 goto outdrop;
748 }
749
750 case F_READBOOTSTRAP:
751 case F_WRITEBOOTSTRAP: {
752 fbootstraptransfer_t fbt_struct;
753 user_fbootstraptransfer_t user_fbt_struct;
754 int sizeof_struct;
755 caddr_t boot_structp;
756
757 if (fp->f_type != DTYPE_VNODE) {
758 error = EBADF;
759 goto out;
760 }
761 vp = (struct vnode *)fp->f_data;
762 proc_fdunlock(p);
763
764 if (IS_64BIT_PROCESS(p)) {
765 sizeof_struct = sizeof(user_fbt_struct);
766 boot_structp = (caddr_t) &user_fbt_struct;
767 }
768 else {
769 sizeof_struct = sizeof(fbt_struct);
770 boot_structp = (caddr_t) &fbt_struct;
771 }
772 error = copyin(argp, boot_structp, sizeof_struct);
773 if (error)
774 goto outdrop;
775 if ( (error = vnode_getwithref(vp)) ) {
776 goto outdrop;
777 }
778 if (uap->cmd == F_WRITEBOOTSTRAP) {
779 /*
780 * Make sure that we are root. Updating the
781 * bootstrap on a disk could be a security hole
782 */
783 if (!is_suser()) {
784 (void)vnode_put(vp);
785 error = EACCES;
786 goto outdrop;
787 }
788 }
789 if (strcmp(vnode_mount(vp)->mnt_vfsstat.f_fstypename, "hfs") != 0) {
790 error = EINVAL;
791 } else {
792 /*
793 * call vnop_ioctl to handle the I/O
794 */
795 error = VNOP_IOCTL(vp, uap->cmd, boot_structp, 0, &context);
796 }
797 (void)vnode_put(vp);
798 goto outdrop;
799 }
800 case F_LOG2PHYS: {
801 struct log2phys l2p_struct; /* structure for allocate command */
802
803 if (fp->f_type != DTYPE_VNODE) {
804 error = EBADF;
805 goto out;
806 }
807 vp = (struct vnode *)fp->f_data;
808 proc_fdunlock(p);
809 if ( (error = vnode_getwithref(vp)) ) {
810 goto outdrop;
811 }
812 error = VNOP_OFFTOBLK(vp, fp->f_offset, &lbn);
813 if (error) {
814 (void)vnode_put(vp);
815 goto outdrop;
816 }
817 error = VNOP_BLKTOOFF(vp, lbn, &offset);
818 if (error) {
819 (void)vnode_put(vp);
820 goto outdrop;
821 }
822 devBlockSize = vfs_devblocksize(vnode_mount(vp));
823
824 error = VNOP_BLOCKMAP(vp, offset, devBlockSize, &bn, NULL, NULL, 0, &context);
825
826 (void)vnode_put(vp);
827
828 if (!error) {
829 l2p_struct.l2p_flags = 0; /* for now */
830 l2p_struct.l2p_contigbytes = 0; /* for now */
831 l2p_struct.l2p_devoffset = bn * devBlockSize;
832 l2p_struct.l2p_devoffset += fp->f_offset - offset;
833 error = copyout((caddr_t)&l2p_struct, argp, sizeof (l2p_struct));
834 }
835 goto outdrop;
836 }
837 case F_GETPATH: {
838 char *pathbufp;
839 int pathlen;
840
841 if (fp->f_type != DTYPE_VNODE) {
842 error = EBADF;
843 goto out;
844 }
845 vp = (struct vnode *)fp->f_data;
846 proc_fdunlock(p);
847
848 pathlen = MAXPATHLEN;
849 MALLOC(pathbufp, char *, pathlen, M_TEMP, M_WAITOK);
850 if (pathbufp == NULL) {
851 error = ENOMEM;
852 goto outdrop;
853 }
854 if ( (error = vnode_getwithref(vp)) == 0 ) {
855 error = vn_getpath(vp, pathbufp, &pathlen);
856 (void)vnode_put(vp);
857
858 if (error == 0)
859 error = copyout((caddr_t)pathbufp, argp, pathlen);
860 }
861 FREE(pathbufp, M_TEMP);
862 goto outdrop;
863 }
864
865 case F_PATHPKG_CHECK: {
866 char *pathbufp;
867 size_t pathlen;
868
869 if (fp->f_type != DTYPE_VNODE) {
870 error = EBADF;
871 goto out;
872 }
873 vp = (struct vnode *)fp->f_data;
874 proc_fdunlock(p);
875
876 pathlen = MAXPATHLEN;
877 pathbufp = kalloc(MAXPATHLEN);
878
879 if ( (error = copyinstr(argp, pathbufp, MAXPATHLEN, &pathlen)) == 0 ) {
880 if ( (error = vnode_getwithref(vp)) == 0 ) {
881 error = vn_path_package_check(vp, pathbufp, pathlen, retval);
882
883 (void)vnode_put(vp);
884 }
885 }
886 kfree(pathbufp, MAXPATHLEN);
887 goto outdrop;
888 }
889
890 case F_CHKCLEAN: // used by regression tests to see if all dirty pages got cleaned by fsync()
891 case F_FULLFSYNC: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
892 case F_FREEZE_FS: // freeze all other fs operations for the fs of this fd
893 case F_THAW_FS: { // thaw all frozen fs operations for the fs of this fd
894 if (fp->f_type != DTYPE_VNODE) {
895 error = EBADF;
896 goto out;
897 }
898 vp = (struct vnode *)fp->f_data;
899 proc_fdunlock(p);
900
901 if ( (error = vnode_getwithref(vp)) == 0 ) {
902 error = VNOP_IOCTL(vp, uap->cmd, (caddr_t)NULL, 0, &context);
903
904 (void)vnode_put(vp);
905 }
906 break;
907 }
908
909 default:
910 if (uap->cmd < FCNTL_FS_SPECIFIC_BASE) {
911 error = EINVAL;
912 goto out;
913 }
914
915 // if it's a fs-specific fcntl() then just pass it through
916
917 if (fp->f_type != DTYPE_VNODE) {
918 error = EBADF;
919 goto out;
920 }
921 vp = (struct vnode *)fp->f_data;
922 proc_fdunlock(p);
923
924 if ( (error = vnode_getwithref(vp)) == 0 ) {
925 error = VNOP_IOCTL(vp, uap->cmd, CAST_DOWN(caddr_t, argp), 0, &context);
926
927 (void)vnode_put(vp);
928 }
929 break;
930
931 }
932
933 outdrop:
934 AUDIT_ARG(vnpath_withref, vp, ARG_VNODE1);
935 fp_drop(p, fd, fp, 0);
936 return(error);
937 out:
938 fp_drop(p, fd, fp, 1);
939 proc_fdunlock(p);
940 return(error);
941 }
942
943 /*
944 * Common code for dup, dup2, and fcntl(F_DUPFD).
945 */
946 int
947 finishdup(struct proc * p, struct filedesc *fdp, int old, int new, register_t *retval)
948 {
949 struct fileproc *nfp;
950 struct fileproc *ofp;
951
952 if ((ofp = fdp->fd_ofiles[old]) == NULL ||
953 (fdp->fd_ofileflags[old] & UF_RESERVED)) {
954 _fdrelse(fdp, new);
955 return (EBADF);
956 }
957 fg_ref(ofp);
958 proc_fdunlock(p);
959
960 MALLOC_ZONE(nfp, struct fileproc *, sizeof(struct fileproc), M_FILEPROC, M_WAITOK);
961 bzero(nfp, sizeof(struct fileproc));
962
963 proc_fdlock(p);
964 nfp->f_flags = ofp->f_flags;
965 nfp->f_fglob = ofp->f_fglob;
966 nfp->f_iocount = 0;
967
968 fdp->fd_ofiles[new] = nfp;
969 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
970 if (new > fdp->fd_lastfile)
971 fdp->fd_lastfile = new;
972 *retval = new;
973 return (0);
974 }
975
976
977 int
978 close(struct proc *p, struct close_args *uap, __unused register_t *retval)
979 {
980 struct fileproc *fp;
981 int fd = uap->fd;
982 int error =0;
983
984 AUDIT_SYSCLOSE(p, fd);
985
986 proc_fdlock(p);
987
988 if ( (error = fp_lookup(p,fd,&fp, 1)) ) {
989 proc_fdunlock(p);
990 return(error);
991 }
992
993 error = close_internal(p, fd, fp, CLOSEINT_LOCKED | CLOSEINT_WAITONCLOSE);
994
995 proc_fdunlock(p);
996
997 return(error);
998 }
999
1000
1001 /*
1002 * Close a file descriptor.
1003 */
1004 int
1005 close_internal(struct proc *p, int fd, struct fileproc *fp, int flags)
1006 {
1007 struct filedesc *fdp = p->p_fd;
1008 int error =0;
1009 int locked = flags & CLOSEINT_LOCKED;
1010 int waitonclose = flags & CLOSEINT_WAITONCLOSE;
1011 int norelse = flags & CLOSEINT_NOFDRELSE;
1012 int nofdref = flags & CLOSEINT_NOFDNOREF;
1013 int slpstate = PRIBIO;
1014
1015 if (!locked)
1016 proc_fdlock(p);
1017
1018 /* Keep people from using the filedesc while we are closing it */
1019 fdp->fd_ofileflags[fd] |= UF_RESERVED;
1020
1021 fdp->fd_ofileflags[fd] |= UF_CLOSING;
1022
1023
1024 if ((waitonclose && ((fp->f_flags & FP_CLOSING) == FP_CLOSING))) {
1025 if (nofdref == 0)
1026 fp_drop(p, fd, fp, 1);
1027 fp->f_flags |= FP_WAITCLOSE;
1028 if (!locked)
1029 slpstate |= PDROP;
1030 msleep(&fp->f_flags, &p->p_fdmlock, slpstate, "close wait",0) ;
1031 return(EBADF);
1032 }
1033
1034 fp->f_flags |= FP_CLOSING;
1035 if (nofdref)
1036 fp->f_iocount++;
1037
1038 if ( (fp->f_flags & FP_AIOISSUED) || kauth_authorize_fileop_has_listeners() ) {
1039
1040 proc_fdunlock(p);
1041
1042 if ( (fp->f_type == DTYPE_VNODE) && kauth_authorize_fileop_has_listeners() ) {
1043 /*
1044 * call out to allow 3rd party notification of close.
1045 * Ignore result of kauth_authorize_fileop call.
1046 */
1047 if (vnode_getwithref((vnode_t)fp->f_data) == 0) {
1048 u_int fileop_flags = 0;
1049 if ((fp->f_flags & FP_WRITTEN) != 0)
1050 fileop_flags |= KAUTH_FILEOP_CLOSE_MODIFIED;
1051 kauth_authorize_fileop(fp->f_fglob->fg_cred, KAUTH_FILEOP_CLOSE,
1052 (uintptr_t)fp->f_data, (uintptr_t)fileop_flags);
1053 vnode_put((vnode_t)fp->f_data);
1054 }
1055 }
1056 if (fp->f_flags & FP_AIOISSUED)
1057 /*
1058 * cancel all async IO requests that can be cancelled.
1059 */
1060 _aio_close( p, fd );
1061
1062 proc_fdlock(p);
1063 }
1064
1065 if (fd < fdp->fd_knlistsize)
1066 knote_fdclose(p, fd);
1067
1068 if (fp->f_flags & FP_WAITEVENT)
1069 (void)waitevent_close(p, fp);
1070
1071 if ((fp->f_flags & FP_INCHRREAD) == 0)
1072 fileproc_drain(p, fp);
1073 if (norelse == 0)
1074 _fdrelse(fdp, fd);
1075 error = closef_locked(fp, fp->f_fglob, p);
1076 if ((fp->f_flags & FP_WAITCLOSE) == FP_WAITCLOSE)
1077 wakeup(&fp->f_flags);
1078 fp->f_flags &= ~(FP_WAITCLOSE | FP_CLOSING);
1079
1080 if (!locked)
1081 proc_fdunlock(p);
1082
1083 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
1084 return(error);
1085 }
1086
1087 /*
1088 * Return status information about a file descriptor.
1089 *
1090 * XXX switch on node type is bogus; need a stat in struct fileops instead.
1091 */
1092 static int
1093 fstat1(struct proc *p, int fd, user_addr_t ub, user_addr_t xsecurity, user_addr_t xsecurity_size)
1094 {
1095 struct fileproc *fp;
1096 struct stat sb;
1097 struct user_stat user_sb;
1098 int error, my_size;
1099 int funnel_state;
1100 short type;
1101 caddr_t data;
1102 kauth_filesec_t fsec;
1103 ssize_t xsecurity_bufsize;
1104 int entrycount;
1105 struct vfs_context context;
1106
1107
1108 AUDIT_ARG(fd, fd);
1109
1110 if ((error = fp_lookup(p, fd, &fp, 0)) != 0)
1111 return(error);
1112 type = fp->f_type;
1113 data = fp->f_data;
1114 fsec = KAUTH_FILESEC_NONE;
1115
1116 switch (type) {
1117
1118 case DTYPE_VNODE:
1119 context.vc_proc = current_proc();
1120 context.vc_ucred = kauth_cred_get();
1121 if ((error = vnode_getwithref((vnode_t)data)) == 0) {
1122 /*
1123 * If the caller has the file open, and is not requesting extended security,
1124 * we are going to let them get the basic stat information.
1125 */
1126 if (xsecurity == USER_ADDR_NULL) {
1127 error = vn_stat_noauth((vnode_t)data, &sb, NULL, &context);
1128 } else {
1129 error = vn_stat((vnode_t)data, &sb, &fsec, &context);
1130 }
1131
1132 AUDIT_ARG(vnpath, (struct vnode *)data, ARG_VNODE1);
1133 (void)vnode_put((vnode_t)data);
1134 }
1135 break;
1136
1137 case DTYPE_SOCKET:
1138 error = soo_stat((struct socket *)data, &sb);
1139 break;
1140
1141 case DTYPE_PIPE:
1142 error = pipe_stat((void *)data, &sb);
1143 break;
1144
1145 case DTYPE_PSXSHM:
1146 error = pshm_stat((void *)data, &sb);
1147 break;
1148
1149 case DTYPE_KQUEUE:
1150 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1151 error = kqueue_stat(fp, &sb, p);
1152 thread_funnel_set(kernel_flock, funnel_state);
1153 break;
1154
1155 default:
1156 error = EBADF;
1157 goto out;
1158 }
1159 /* Zap spare fields */
1160 sb.st_lspare = 0;
1161 sb.st_qspare[0] = 0LL;
1162 sb.st_qspare[1] = 0LL;
1163 if (error == 0) {
1164 caddr_t sbp;
1165 if (IS_64BIT_PROCESS(current_proc())) {
1166 munge_stat(&sb, &user_sb);
1167 my_size = sizeof(user_sb);
1168 sbp = (caddr_t)&user_sb;
1169 }
1170 else {
1171 my_size = sizeof(sb);
1172 sbp = (caddr_t)&sb;
1173 }
1174 error = copyout(sbp, ub, my_size);
1175 }
1176
1177 /* caller wants extended security information? */
1178 if (xsecurity != USER_ADDR_NULL) {
1179
1180 /* did we get any? */
1181 if (fsec == KAUTH_FILESEC_NONE) {
1182 if (susize(xsecurity_size, 0) != 0) {
1183 error = EFAULT;
1184 goto out;
1185 }
1186 } else {
1187 /* find the user buffer size */
1188 xsecurity_bufsize = fusize(xsecurity_size);
1189
1190 /* copy out the actual data size */
1191 if (susize(xsecurity_size, KAUTH_FILESEC_COPYSIZE(fsec)) != 0) {
1192 error = EFAULT;
1193 goto out;
1194 }
1195
1196 /* if the caller supplied enough room, copy out to it */
1197 if (xsecurity_bufsize >= KAUTH_FILESEC_COPYSIZE(fsec))
1198 error = copyout(fsec, xsecurity, KAUTH_FILESEC_COPYSIZE(fsec));
1199 }
1200 }
1201 out:
1202 fp_drop(p, fd, fp, 0);
1203 if (fsec != NULL)
1204 kauth_filesec_free(fsec);
1205 return (error);
1206 }
1207
1208 int
1209 fstat_extended(struct proc *p, struct fstat_extended_args *uap, __unused register_t *retval)
1210 {
1211 return(fstat1(p, uap->fd, uap->ub, uap->xsecurity, uap->xsecurity_size));
1212 }
1213
1214 int
1215 fstat(struct proc *p, register struct fstat_args *uap, __unused register_t *retval)
1216 {
1217 return(fstat1(p, uap->fd, uap->ub, 0, 0));
1218 }
1219
1220 /*
1221 * Return pathconf information about a file descriptor.
1222 */
1223 int
1224 fpathconf(p, uap, retval)
1225 struct proc *p;
1226 register struct fpathconf_args *uap;
1227 register_t *retval;
1228 {
1229 int fd = uap->fd;
1230 struct fileproc *fp;
1231 struct vnode *vp;
1232 struct vfs_context context;
1233 int error = 0;
1234 short type;
1235 caddr_t data;
1236
1237
1238 AUDIT_ARG(fd, uap->fd);
1239 if ( (error = fp_lookup(p, fd, &fp, 0)) )
1240 return(error);
1241 type = fp->f_type;
1242 data = fp->f_data;
1243
1244 switch (type) {
1245
1246 case DTYPE_SOCKET:
1247 if (uap->name != _PC_PIPE_BUF) {
1248 error = EINVAL;
1249 goto out;
1250 }
1251 *retval = PIPE_BUF;
1252 error = 0;
1253 goto out;
1254
1255 case DTYPE_PIPE:
1256 *retval = PIPE_BUF;
1257 error = 0;
1258 goto out;
1259
1260 case DTYPE_VNODE:
1261 vp = (struct vnode *)data;
1262
1263 if ( (error = vnode_getwithref(vp)) == 0) {
1264 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
1265
1266 context.vc_proc = p;
1267 context.vc_ucred = kauth_cred_get();
1268
1269 error = vn_pathconf(vp, uap->name, retval, &context);
1270
1271 (void)vnode_put(vp);
1272 }
1273 goto out;
1274
1275 case DTYPE_PSXSHM:
1276 case DTYPE_KQUEUE:
1277 error = EINVAL;
1278 goto out;
1279
1280 default:
1281 panic("fpathconf (unrecognized - %d)", type);
1282 }
1283 /*NOTREACHED*/
1284 out:
1285 fp_drop(p, fd, fp, 0);
1286 return(error);
1287 }
1288
1289 /*
1290 * Allocate a file descriptor for the process.
1291 */
1292 int fdexpand;
1293
1294 int
1295 fdalloc(p, want, result)
1296 struct proc *p;
1297 int want;
1298 int *result;
1299 {
1300 register struct filedesc *fdp = p->p_fd;
1301 register int i;
1302 int lim, last, numfiles, oldnfiles;
1303 struct fileproc **newofiles, **ofiles;
1304 char *newofileflags, *ofileflags;
1305
1306 /*
1307 * Search for a free descriptor starting at the higher
1308 * of want or fd_freefile. If that fails, consider
1309 * expanding the ofile array.
1310 */
1311 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1312 for (;;) {
1313 last = min(fdp->fd_nfiles, lim);
1314 if ((i = want) < fdp->fd_freefile)
1315 i = fdp->fd_freefile;
1316 ofiles = &fdp->fd_ofiles[i];
1317 ofileflags = &fdp->fd_ofileflags[i];
1318 for (; i < last; i++) {
1319 if (*ofiles == NULL && !(*ofileflags & UF_RESERVED)) {
1320 *ofileflags = UF_RESERVED;
1321 if (i > fdp->fd_lastfile)
1322 fdp->fd_lastfile = i;
1323 if (want <= fdp->fd_freefile)
1324 fdp->fd_freefile = i;
1325 *result = i;
1326 return (0);
1327 }
1328 ofiles++; ofileflags++;
1329 }
1330
1331 /*
1332 * No space in current array. Expand?
1333 */
1334 if (fdp->fd_nfiles >= lim)
1335 return (EMFILE);
1336 if (fdp->fd_nfiles < NDEXTENT)
1337 numfiles = NDEXTENT;
1338 else
1339 numfiles = 2 * fdp->fd_nfiles;
1340 /* Enforce lim */
1341 if (numfiles > lim)
1342 numfiles = lim;
1343 proc_fdunlock(p);
1344 MALLOC_ZONE(newofiles, struct fileproc **,
1345 numfiles * OFILESIZE, M_OFILETABL, M_WAITOK);
1346 proc_fdlock(p);
1347 if (newofiles == NULL) {
1348 return (ENOMEM);
1349 }
1350 if (fdp->fd_nfiles >= numfiles) {
1351 FREE_ZONE(newofiles, numfiles * OFILESIZE, M_OFILETABL);
1352 continue;
1353 }
1354 newofileflags = (char *) &newofiles[numfiles];
1355 /*
1356 * Copy the existing ofile and ofileflags arrays
1357 * and zero the new portion of each array.
1358 */
1359 oldnfiles = fdp->fd_nfiles;
1360 (void) memcpy(newofiles, fdp->fd_ofiles,
1361 oldnfiles * sizeof *fdp->fd_ofiles);
1362 (void) memset(&newofiles[oldnfiles], 0,
1363 (numfiles - oldnfiles) * sizeof *fdp->fd_ofiles);
1364
1365 (void) memcpy(newofileflags, fdp->fd_ofileflags,
1366 oldnfiles * sizeof *fdp->fd_ofileflags);
1367 (void) memset(&newofileflags[oldnfiles], 0,
1368 (numfiles - oldnfiles) *
1369 sizeof *fdp->fd_ofileflags);
1370 ofiles = fdp->fd_ofiles;
1371 fdp->fd_ofiles = newofiles;
1372 fdp->fd_ofileflags = newofileflags;
1373 fdp->fd_nfiles = numfiles;
1374 FREE_ZONE(ofiles, oldnfiles * OFILESIZE, M_OFILETABL);
1375 fdexpand++;
1376 }
1377 }
1378
1379 /*
1380 * Check to see whether n user file descriptors
1381 * are available to the process p.
1382 */
1383 int
1384 fdavail(p, n)
1385 struct proc *p;
1386 int n;
1387 {
1388 struct filedesc *fdp = p->p_fd;
1389 struct fileproc **fpp;
1390 char *flags;
1391 int i, lim;
1392
1393 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1394 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1395 return (1);
1396 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1397 flags = &fdp->fd_ofileflags[fdp->fd_freefile];
1398 for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++, flags++)
1399 if (*fpp == NULL && !(*flags & UF_RESERVED) && --n <= 0)
1400 return (1);
1401 return (0);
1402 }
1403
1404 void
1405 fdrelse(p, fd)
1406 struct proc *p;
1407 int fd;
1408 {
1409 _fdrelse(p->p_fd, fd);
1410 }
1411
1412 void
1413 fddrop(p, fd)
1414 struct proc *p;
1415 int fd;
1416 {
1417 struct filedesc *fdp = p->p_fd;
1418 struct fileproc *fp;
1419
1420 if (fd < fdp->fd_freefile)
1421 fdp->fd_freefile = fd;
1422 #if DIAGNOSTIC
1423 if (fd > fdp->fd_lastfile)
1424 panic("fdrelse: fd_lastfile inconsistent");
1425 #endif
1426 fp = fdp->fd_ofiles[fd];
1427 fdp->fd_ofiles[fd] = NULL;
1428 fdp->fd_ofileflags[fd] = 0;
1429
1430 while ((fd = fdp->fd_lastfile) > 0 &&
1431 fdp->fd_ofiles[fd] == NULL &&
1432 !(fdp->fd_ofileflags[fd] & UF_RESERVED))
1433 fdp->fd_lastfile--;
1434 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
1435 }
1436
1437
1438 int
1439 fdgetf_noref(p, fd, resultfp)
1440 struct proc *p;
1441 int fd;
1442 struct fileproc **resultfp;
1443 {
1444 struct filedesc *fdp = p->p_fd;
1445 struct fileproc *fp;
1446
1447 if (fd < 0 || fd >= fdp->fd_nfiles ||
1448 (fp = fdp->fd_ofiles[fd]) == NULL ||
1449 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1450 return (EBADF);
1451 }
1452 if (resultfp)
1453 *resultfp = fp;
1454 return (0);
1455 }
1456
1457
1458 /* should be called only when proc_fdlock is held */
1459 void
1460 fp_setflags(proc_t p, struct fileproc * fp, int flags)
1461 {
1462 proc_fdlock(p);
1463 fp->f_flags |= flags;
1464 proc_fdunlock(p);
1465 }
1466
1467 void
1468 fp_clearflags(proc_t p, struct fileproc * fp, int flags)
1469 {
1470
1471 proc_fdlock(p);
1472 if (fp)
1473 fp->f_flags &= ~flags;
1474 proc_fdunlock(p);
1475 }
1476
1477 int
1478 fp_getfvp(p, fd, resultfp, resultvp)
1479 struct proc *p;
1480 int fd;
1481 struct fileproc **resultfp;
1482 struct vnode **resultvp;
1483 {
1484 struct filedesc *fdp = p->p_fd;
1485 struct fileproc *fp;
1486
1487 proc_fdlock(p);
1488 if (fd < 0 || fd >= fdp->fd_nfiles ||
1489 (fp = fdp->fd_ofiles[fd]) == NULL ||
1490 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1491 proc_fdunlock(p);
1492 return (EBADF);
1493 }
1494 if (fp->f_type != DTYPE_VNODE) {
1495 proc_fdunlock(p);
1496 return(ENOTSUP);
1497 }
1498 fp->f_iocount++;
1499
1500 if (resultfp)
1501 *resultfp = fp;
1502 if (resultvp)
1503 *resultvp = (struct vnode *)fp->f_data;
1504 proc_fdunlock(p);
1505
1506 return (0);
1507 }
1508
1509
1510
1511 int
1512 fp_getfvpandvid(p, fd, resultfp, resultvp, vidp)
1513 struct proc *p;
1514 int fd;
1515 struct fileproc **resultfp;
1516 struct vnode **resultvp;
1517 uint32_t * vidp;
1518 {
1519 struct filedesc *fdp = p->p_fd;
1520 struct fileproc *fp;
1521
1522 proc_fdlock(p);
1523 if (fd < 0 || fd >= fdp->fd_nfiles ||
1524 (fp = fdp->fd_ofiles[fd]) == NULL ||
1525 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1526 proc_fdunlock(p);
1527 return (EBADF);
1528 }
1529 if (fp->f_type != DTYPE_VNODE) {
1530 proc_fdunlock(p);
1531 return(ENOTSUP);
1532 }
1533 fp->f_iocount++;
1534
1535 if (resultfp)
1536 *resultfp = fp;
1537 if (resultvp)
1538 *resultvp = (struct vnode *)fp->f_data;
1539 if (vidp)
1540 *vidp = (uint32_t)vnode_vid((struct vnode *)fp->f_data);
1541 proc_fdunlock(p);
1542
1543 return (0);
1544 }
1545
1546 /*
1547 * Returns: EBADF The file descriptor is invalid
1548 * EOPNOTSUPP The file descriptor is not a socket
1549 * 0 Success
1550 *
1551 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
1552 * ever called from accept1().
1553 */
1554 int
1555 fp_getfsock(p, fd, resultfp, results)
1556 struct proc *p;
1557 int fd;
1558 struct fileproc **resultfp;
1559 struct socket **results;
1560 {
1561 struct filedesc *fdp = p->p_fd;
1562 struct fileproc *fp;
1563
1564 proc_fdlock(p);
1565 if (fd < 0 || fd >= fdp->fd_nfiles ||
1566 (fp = fdp->fd_ofiles[fd]) == NULL ||
1567 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1568 proc_fdunlock(p);
1569 return (EBADF);
1570 }
1571 if (fp->f_type != DTYPE_SOCKET) {
1572 proc_fdunlock(p);
1573 return(EOPNOTSUPP);
1574 }
1575 fp->f_iocount++;
1576
1577 if (resultfp)
1578 *resultfp = fp;
1579 if (results)
1580 *results = (struct socket *)fp->f_data;
1581 proc_fdunlock(p);
1582
1583 return (0);
1584 }
1585
1586
1587 int
1588 fp_getfkq(p, fd, resultfp, resultkq)
1589 struct proc *p;
1590 int fd;
1591 struct fileproc **resultfp;
1592 struct kqueue **resultkq;
1593 {
1594 struct filedesc *fdp = p->p_fd;
1595 struct fileproc *fp;
1596
1597 proc_fdlock(p);
1598 if ( fd < 0 || fd >= fdp->fd_nfiles ||
1599 (fp = fdp->fd_ofiles[fd]) == NULL ||
1600 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1601 proc_fdunlock(p);
1602 return (EBADF);
1603 }
1604 if (fp->f_type != DTYPE_KQUEUE) {
1605 proc_fdunlock(p);
1606 return(EBADF);
1607 }
1608 fp->f_iocount++;
1609
1610 if (resultfp)
1611 *resultfp = fp;
1612 if (resultkq)
1613 *resultkq = (struct kqueue *)fp->f_data;
1614 proc_fdunlock(p);
1615
1616 return (0);
1617 }
1618
1619 int
1620 fp_getfpshm(p, fd, resultfp, resultpshm)
1621 struct proc *p;
1622 int fd;
1623 struct fileproc **resultfp;
1624 struct pshmnode **resultpshm;
1625 {
1626 struct filedesc *fdp = p->p_fd;
1627 struct fileproc *fp;
1628
1629 proc_fdlock(p);
1630 if (fd < 0 || fd >= fdp->fd_nfiles ||
1631 (fp = fdp->fd_ofiles[fd]) == NULL ||
1632 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1633 proc_fdunlock(p);
1634 return (EBADF);
1635 }
1636 if (fp->f_type != DTYPE_PSXSHM) {
1637
1638 proc_fdunlock(p);
1639 return(EBADF);
1640 }
1641 fp->f_iocount++;
1642
1643 if (resultfp)
1644 *resultfp = fp;
1645 if (resultpshm)
1646 *resultpshm = (struct pshmnode *)fp->f_data;
1647 proc_fdunlock(p);
1648
1649 return (0);
1650 }
1651
1652
1653 int
1654 fp_getfpsem(p, fd, resultfp, resultpsem)
1655 struct proc *p;
1656 int fd;
1657 struct fileproc **resultfp;
1658 struct psemnode **resultpsem;
1659 {
1660 struct filedesc *fdp = p->p_fd;
1661 struct fileproc *fp;
1662
1663 proc_fdlock(p);
1664 if (fd < 0 || fd >= fdp->fd_nfiles ||
1665 (fp = fdp->fd_ofiles[fd]) == NULL ||
1666 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1667 proc_fdunlock(p);
1668 return (EBADF);
1669 }
1670 if (fp->f_type != DTYPE_PSXSEM) {
1671 proc_fdunlock(p);
1672 return(EBADF);
1673 }
1674 fp->f_iocount++;
1675
1676 if (resultfp)
1677 *resultfp = fp;
1678 if (resultpsem)
1679 *resultpsem = (struct psemnode *)fp->f_data;
1680 proc_fdunlock(p);
1681
1682 return (0);
1683 }
1684
1685
1686 int
1687 fp_getfpipe(p, fd, resultfp, resultpipe)
1688 struct proc *p;
1689 int fd;
1690 struct fileproc **resultfp;
1691 struct pipe **resultpipe;
1692 {
1693 struct filedesc *fdp = p->p_fd;
1694 struct fileproc *fp;
1695
1696 proc_fdlock(p);
1697 if (fd < 0 || fd >= fdp->fd_nfiles ||
1698 (fp = fdp->fd_ofiles[fd]) == NULL ||
1699 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1700 proc_fdunlock(p);
1701 return (EBADF);
1702 }
1703 if (fp->f_type != DTYPE_PIPE) {
1704 proc_fdunlock(p);
1705 return(EBADF);
1706 }
1707 fp->f_iocount++;
1708
1709 if (resultfp)
1710 *resultfp = fp;
1711 if (resultpipe)
1712 *resultpipe = (struct pipe *)fp->f_data;
1713 proc_fdunlock(p);
1714
1715 return (0);
1716 }
1717
1718
1719 #define DTYPE_ATALK -1
1720 int
1721 fp_getfatalk(p, fd, resultfp, resultatalk)
1722 struct proc *p;
1723 int fd;
1724 struct fileproc **resultfp;
1725 struct atalk **resultatalk;
1726 {
1727 struct filedesc *fdp = p->p_fd;
1728 struct fileproc *fp;
1729
1730 proc_fdlock(p);
1731 if (fd < 0 || fd >= fdp->fd_nfiles ||
1732 (fp = fdp->fd_ofiles[fd]) == NULL ||
1733 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1734 proc_fdunlock(p);
1735 return (EBADF);
1736 }
1737 if (fp->f_type != (DTYPE_ATALK+1)) {
1738 proc_fdunlock(p);
1739 return(EBADF);
1740 }
1741 fp->f_iocount++;
1742
1743 if (resultfp)
1744 *resultfp = fp;
1745 if (resultatalk)
1746 *resultatalk = (struct atalk *)fp->f_data;
1747 proc_fdunlock(p);
1748
1749 return (0);
1750 }
1751
1752 int
1753 fp_lookup(p, fd, resultfp, locked)
1754 struct proc *p;
1755 int fd;
1756 struct fileproc **resultfp;
1757 int locked;
1758 {
1759 struct filedesc *fdp = p->p_fd;
1760 struct fileproc *fp;
1761
1762 if (!locked)
1763 proc_fdlock(p);
1764 if (fd < 0 || fd >= fdp->fd_nfiles ||
1765 (fp = fdp->fd_ofiles[fd]) == NULL ||
1766 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1767 if (!locked)
1768 proc_fdunlock(p);
1769 return (EBADF);
1770 }
1771 fp->f_iocount++;
1772
1773 if (resultfp)
1774 *resultfp = fp;
1775 if (!locked)
1776 proc_fdunlock(p);
1777
1778 return (0);
1779 }
1780
1781 int
1782 fp_drop_written(proc_t p, int fd, struct fileproc *fp)
1783 {
1784 int error;
1785
1786 proc_fdlock(p);
1787
1788 fp->f_flags |= FP_WRITTEN;
1789
1790 error = fp_drop(p, fd, fp, 1);
1791
1792 proc_fdunlock(p);
1793
1794 return (error);
1795 }
1796
1797
1798 int
1799 fp_drop_event(proc_t p, int fd, struct fileproc *fp)
1800 {
1801 int error;
1802
1803 proc_fdlock(p);
1804
1805 fp->f_flags |= FP_WAITEVENT;
1806
1807 error = fp_drop(p, fd, fp, 1);
1808
1809 proc_fdunlock(p);
1810
1811 return (error);
1812 }
1813
1814 int
1815 fp_drop(p, fd, fp, locked)
1816 struct proc *p;
1817 int fd;
1818 struct fileproc *fp;
1819 int locked;
1820 {
1821 struct filedesc *fdp = p->p_fd;
1822
1823 if (!locked)
1824 proc_fdlock(p);
1825 if ((fp == FILEPROC_NULL) && (fd < 0 || fd >= fdp->fd_nfiles ||
1826 (fp = fdp->fd_ofiles[fd]) == NULL ||
1827 ((fdp->fd_ofileflags[fd] & UF_RESERVED) &&
1828 !(fdp->fd_ofileflags[fd] & UF_CLOSING)))) {
1829 if (!locked)
1830 proc_fdunlock(p);
1831 return (EBADF);
1832 }
1833 fp->f_iocount--;
1834
1835 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1836 p->p_fpdrainwait = 0;
1837 wakeup(&p->p_fpdrainwait);
1838 }
1839 if (!locked)
1840 proc_fdunlock(p);
1841
1842 return (0);
1843 }
1844
1845 int
1846 file_vnode(int fd, struct vnode **vpp)
1847 {
1848 struct proc * p = current_proc();
1849 struct fileproc *fp;
1850 int error;
1851
1852 proc_fdlock(p);
1853 if ( (error = fp_lookup(p, fd, &fp, 1)) ) {
1854 proc_fdunlock(p);
1855 return(error);
1856 }
1857 if (fp->f_type != DTYPE_VNODE) {
1858 fp_drop(p, fd, fp,1);
1859 proc_fdunlock(p);
1860 return(EINVAL);
1861 }
1862 *vpp = (struct vnode *)fp->f_data;
1863 proc_fdunlock(p);
1864
1865 return(0);
1866 }
1867
1868
1869 int
1870 file_socket(int fd, struct socket **sp)
1871 {
1872 struct proc * p = current_proc();
1873 struct fileproc *fp;
1874 int error;
1875
1876 proc_fdlock(p);
1877 if ( (error = fp_lookup(p, fd, &fp, 1)) ) {
1878 proc_fdunlock(p);
1879 return(error);
1880 }
1881 if (fp->f_type != DTYPE_SOCKET) {
1882 fp_drop(p, fd, fp,1);
1883 proc_fdunlock(p);
1884 return(ENOTSOCK);
1885 }
1886 *sp = (struct socket *)fp->f_data;
1887 proc_fdunlock(p);
1888
1889 return(0);
1890 }
1891
1892 int
1893 file_flags(int fd, int * flags)
1894 {
1895
1896 struct proc * p = current_proc();
1897 struct fileproc *fp;
1898 int error;
1899
1900 proc_fdlock(p);
1901 if ( (error = fp_lookup(p, fd, &fp, 1)) ) {
1902 proc_fdunlock(p);
1903 return(error);
1904 }
1905 *flags = (int)fp->f_flag;
1906 fp_drop(p, fd, fp,1);
1907 proc_fdunlock(p);
1908
1909 return(0);
1910 }
1911
1912
1913 int
1914 file_drop(int fd)
1915 {
1916 struct fileproc *fp;
1917 struct proc *p = current_proc();
1918
1919 proc_fdlock(p);
1920 if (fd < 0 || fd >= p->p_fd->fd_nfiles ||
1921 (fp = p->p_fd->fd_ofiles[fd]) == NULL ||
1922 ((p->p_fd->fd_ofileflags[fd] & UF_RESERVED) &&
1923 !(p->p_fd->fd_ofileflags[fd] & UF_CLOSING))) {
1924 proc_fdunlock(p);
1925 return (EBADF);
1926 }
1927 fp->f_iocount --;
1928
1929 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1930 p->p_fpdrainwait = 0;
1931 wakeup(&p->p_fpdrainwait);
1932 }
1933 proc_fdunlock(p);
1934 return(0);
1935
1936
1937 }
1938
1939 int
1940 falloc(p, resultfp, resultfd )
1941 struct proc *p;
1942 struct fileproc **resultfp;
1943 int *resultfd;
1944 {
1945 int error;
1946
1947 proc_fdlock(p);
1948 error = falloc_locked(p, resultfp, resultfd, 1);
1949 proc_fdunlock(p);
1950
1951 return(error);
1952 }
1953 /*
1954 * Create a new open file structure and allocate
1955 * a file decriptor for the process that refers to it.
1956 */
1957 int
1958 falloc_locked(p, resultfp, resultfd, locked)
1959 struct proc *p;
1960 struct fileproc **resultfp;
1961 int *resultfd;
1962 int locked;
1963 {
1964 struct fileproc *fp, *fq;
1965 struct fileglob *fg;
1966 int error, nfd;
1967
1968 if (!locked)
1969 proc_fdlock(p);
1970 if ( (error = fdalloc(p, 0, &nfd)) ) {
1971 if (!locked)
1972 proc_fdunlock(p);
1973 return (error);
1974 }
1975 if (nfiles >= maxfiles) {
1976 if (!locked)
1977 proc_fdunlock(p);
1978 tablefull("file");
1979 return (ENFILE);
1980 }
1981 /*
1982 * Allocate a new file descriptor.
1983 * If the process has file descriptor zero open, add to the list
1984 * of open files at that point, otherwise put it at the front of
1985 * the list of open files.
1986 */
1987 proc_fdunlock(p);
1988
1989 MALLOC_ZONE(fp, struct fileproc *, sizeof(struct fileproc), M_FILEPROC, M_WAITOK);
1990 MALLOC_ZONE(fg, struct fileglob *, sizeof(struct fileglob), M_FILEGLOB, M_WAITOK);
1991 bzero(fp, sizeof(struct fileproc));
1992 bzero(fg, sizeof(struct fileglob));
1993 lck_mtx_init(&fg->fg_lock, file_lck_grp, file_lck_attr);
1994
1995 fp->f_iocount = 1;
1996 fg->fg_count = 1;
1997 fp->f_fglob = fg;
1998
1999 proc_fdlock(p);
2000
2001 fp->f_cred = kauth_cred_proc_ref(p);
2002
2003 lck_mtx_lock(file_flist_lock);
2004
2005 nfiles++;
2006
2007 if ( (fq = p->p_fd->fd_ofiles[0]) ) {
2008 LIST_INSERT_AFTER(fq->f_fglob, fg, f_list);
2009 } else {
2010 LIST_INSERT_HEAD(&filehead, fg, f_list);
2011 }
2012 lck_mtx_unlock(file_flist_lock);
2013
2014 p->p_fd->fd_ofiles[nfd] = fp;
2015
2016 if (!locked)
2017 proc_fdunlock(p);
2018
2019 if (resultfp)
2020 *resultfp = fp;
2021 if (resultfd)
2022 *resultfd = nfd;
2023
2024 return (0);
2025 }
2026
2027 /*
2028 * Free a file structure.
2029 */
2030 void
2031 fg_free(fg)
2032 struct fileglob *fg;
2033 {
2034 kauth_cred_t cred;
2035
2036 lck_mtx_lock(file_flist_lock);
2037 LIST_REMOVE(fg, f_list);
2038 nfiles--;
2039 lck_mtx_unlock(file_flist_lock);
2040
2041 cred = fg->fg_cred;
2042 if (cred != NOCRED) {
2043 fg->fg_cred = NOCRED;
2044 kauth_cred_rele(cred);
2045 }
2046 lck_mtx_destroy(&fg->fg_lock, file_lck_grp);
2047
2048 FREE_ZONE(fg, sizeof *fg, M_FILEGLOB);
2049 }
2050
2051 void
2052 fdexec(p)
2053 struct proc *p;
2054 {
2055 struct filedesc *fdp = p->p_fd;
2056 int i = fdp->fd_lastfile;
2057 struct fileproc **fpp = &fdp->fd_ofiles[i];
2058 char *flags = &fdp->fd_ofileflags[i];
2059 int funnel_state;
2060
2061 funnel_state = thread_funnel_set(kernel_flock, FALSE);
2062 proc_fdlock(p);
2063
2064 while (i >= 0) {
2065 if ((*flags & (UF_RESERVED|UF_EXCLOSE)) == UF_EXCLOSE) {
2066 struct fileproc *fp = *fpp;
2067
2068 if (i < fdp->fd_knlistsize)
2069 knote_fdclose(p, i);
2070
2071 *fpp = NULL; *flags = 0;
2072 if (i == fdp->fd_lastfile && i > 0)
2073 fdp->fd_lastfile--;
2074 closef_locked(fp, fp->f_fglob, p);
2075 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
2076 }
2077
2078 i--; fpp--; flags--;
2079 }
2080 proc_fdunlock(p);
2081 thread_funnel_set(kernel_flock, funnel_state);
2082 }
2083
2084 /*
2085 * Copy a filedesc structure.
2086 */
2087 struct filedesc *
2088 fdcopy(p)
2089 struct proc *p;
2090 {
2091 struct filedesc *newfdp, *fdp = p->p_fd;
2092 int i;
2093 struct fileproc *ofp, *fp;
2094 vnode_t v_dir;
2095
2096 MALLOC_ZONE(newfdp, struct filedesc *,
2097 sizeof *newfdp, M_FILEDESC, M_WAITOK);
2098 if (newfdp == NULL)
2099 return(NULL);
2100
2101 proc_fdlock(p);
2102
2103 /*
2104 * the FD_CHROOT flag will be inherited via this copy
2105 */
2106 (void) memcpy(newfdp, fdp, sizeof *newfdp);
2107
2108 /*
2109 * for both fd_cdir and fd_rdir make sure we get
2110 * a valid reference... if we can't, than set
2111 * set the pointer(s) to NULL in the child... this
2112 * will keep us from using a non-referenced vp
2113 * and allows us to do the vnode_rele only on
2114 * a properly referenced vp
2115 */
2116 if ( (v_dir = newfdp->fd_cdir) ) {
2117 if (vnode_getwithref(v_dir) == 0) {
2118 if ( (vnode_ref(v_dir)) )
2119 newfdp->fd_cdir = NULL;
2120 vnode_put(v_dir);
2121 } else
2122 newfdp->fd_cdir = NULL;
2123 }
2124 if (newfdp->fd_cdir == NULL && fdp->fd_cdir) {
2125 /*
2126 * we couldn't get a new reference on
2127 * the current working directory being
2128 * inherited... we might as well drop
2129 * our reference from the parent also
2130 * since the vnode has gone DEAD making
2131 * it useless... by dropping it we'll
2132 * be that much closer to recyling it
2133 */
2134 vnode_rele(fdp->fd_cdir);
2135 fdp->fd_cdir = NULL;
2136 }
2137
2138 if ( (v_dir = newfdp->fd_rdir) ) {
2139 if (vnode_getwithref(v_dir) == 0) {
2140 if ( (vnode_ref(v_dir)) )
2141 newfdp->fd_rdir = NULL;
2142 vnode_put(v_dir);
2143 } else
2144 newfdp->fd_rdir = NULL;
2145 }
2146 if (newfdp->fd_rdir == NULL && fdp->fd_rdir) {
2147 /*
2148 * we couldn't get a new reference on
2149 * the root directory being
2150 * inherited... we might as well drop
2151 * our reference from the parent also
2152 * since the vnode has gone DEAD making
2153 * it useless... by dropping it we'll
2154 * be that much closer to recyling it
2155 */
2156 vnode_rele(fdp->fd_rdir);
2157 fdp->fd_rdir = NULL;
2158 }
2159 newfdp->fd_refcnt = 1;
2160
2161 /*
2162 * If the number of open files fits in the internal arrays
2163 * of the open file structure, use them, otherwise allocate
2164 * additional memory for the number of descriptors currently
2165 * in use.
2166 */
2167 if (newfdp->fd_lastfile < NDFILE)
2168 i = NDFILE;
2169 else {
2170 /*
2171 * Compute the smallest multiple of NDEXTENT needed
2172 * for the file descriptors currently in use,
2173 * allowing the table to shrink.
2174 */
2175 i = newfdp->fd_nfiles;
2176 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
2177 i /= 2;
2178 }
2179 proc_fdunlock(p);
2180
2181 MALLOC_ZONE(newfdp->fd_ofiles, struct fileproc **,
2182 i * OFILESIZE, M_OFILETABL, M_WAITOK);
2183 if (newfdp->fd_ofiles == NULL) {
2184 if (newfdp->fd_cdir)
2185 vnode_rele(newfdp->fd_cdir);
2186 if (newfdp->fd_rdir)
2187 vnode_rele(newfdp->fd_rdir);
2188
2189 FREE_ZONE(newfdp, sizeof *newfdp, M_FILEDESC);
2190 return(NULL);
2191 }
2192 proc_fdlock(p);
2193
2194 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
2195 newfdp->fd_nfiles = i;
2196
2197 if (fdp->fd_nfiles > 0) {
2198 struct fileproc **fpp;
2199 char *flags;
2200
2201 (void) memcpy(newfdp->fd_ofiles, fdp->fd_ofiles,
2202 i * sizeof *fdp->fd_ofiles);
2203 (void) memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags,
2204 i * sizeof *fdp->fd_ofileflags);
2205
2206 /*
2207 * kq descriptors cannot be copied.
2208 */
2209 if (newfdp->fd_knlistsize != -1) {
2210 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
2211 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
2212 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
2213 *fpp = NULL;
2214 if (i < newfdp->fd_freefile)
2215 newfdp->fd_freefile = i;
2216 }
2217 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
2218 newfdp->fd_lastfile--;
2219 }
2220 newfdp->fd_knlist = NULL;
2221 newfdp->fd_knlistsize = -1;
2222 newfdp->fd_knhash = NULL;
2223 newfdp->fd_knhashmask = 0;
2224 }
2225 fpp = newfdp->fd_ofiles;
2226 flags = newfdp->fd_ofileflags;
2227
2228 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++, flags++)
2229 if ((ofp = *fpp) != NULL && !(*flags & UF_RESERVED)) {
2230 MALLOC_ZONE(fp, struct fileproc *, sizeof(struct fileproc), M_FILEPROC, M_WAITOK);
2231 bzero(fp, sizeof(struct fileproc));
2232 fp->f_flags = ofp->f_flags;
2233 //fp->f_iocount = ofp->f_iocount;
2234 fp->f_iocount = 0;
2235 fp->f_fglob = ofp->f_fglob;
2236 (void)fg_ref(fp);
2237 *fpp = fp;
2238 } else {
2239 *fpp = NULL;
2240 *flags = 0;
2241 }
2242 } else
2243 (void) memset(newfdp->fd_ofiles, 0, i * OFILESIZE);
2244
2245 proc_fdunlock(p);
2246 return (newfdp);
2247 }
2248
2249 /*
2250 * Release a filedesc structure.
2251 */
2252 void
2253 fdfree(p)
2254 struct proc *p;
2255 {
2256 struct filedesc *fdp;
2257 struct fileproc *fp;
2258 int i;
2259
2260 proc_fdlock(p);
2261
2262 /* Certain daemons might not have file descriptors */
2263 fdp = p->p_fd;
2264
2265 if ((fdp == NULL) || (--fdp->fd_refcnt > 0)) {
2266 proc_fdunlock(p);
2267 return;
2268 }
2269 if (fdp->fd_refcnt == 0xffff)
2270 panic("fdfree: bad fd_refcnt");
2271
2272 /* Last reference: the structure can't change out from under us */
2273
2274 if (fdp->fd_nfiles > 0 && fdp->fd_ofiles) {
2275 for (i = fdp->fd_lastfile; i >= 0; i--) {
2276 if ((fp = fdp->fd_ofiles[i]) != NULL) {
2277
2278 if (fdp->fd_ofileflags[i] & UF_RESERVED)
2279 panic("fdfree: found fp with UF_RESERVED\n");
2280
2281 /* closef drops the iocount ... */
2282 if ((fp->f_flags & FP_INCHRREAD) != 0)
2283 fp->f_iocount++;
2284 fdp->fd_ofiles[i] = NULL;
2285 fdp->fd_ofileflags[i] |= UF_RESERVED;
2286
2287 if (i < fdp->fd_knlistsize)
2288 knote_fdclose(p, i);
2289 if (fp->f_flags & FP_WAITEVENT)
2290 (void)waitevent_close(p, fp);
2291 (void) closef_locked(fp, fp->f_fglob, p);
2292 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
2293 }
2294 }
2295 FREE_ZONE(fdp->fd_ofiles, fdp->fd_nfiles * OFILESIZE, M_OFILETABL);
2296 fdp->fd_ofiles = NULL;
2297 fdp->fd_nfiles = 0;
2298 }
2299
2300 proc_fdunlock(p);
2301
2302 if (fdp->fd_cdir)
2303 vnode_rele(fdp->fd_cdir);
2304 if (fdp->fd_rdir)
2305 vnode_rele(fdp->fd_rdir);
2306
2307 proc_fdlock(p);
2308 p->p_fd = NULL;
2309 proc_fdunlock(p);
2310
2311 if (fdp->fd_knlist)
2312 FREE(fdp->fd_knlist, M_KQUEUE);
2313 if (fdp->fd_knhash)
2314 FREE(fdp->fd_knhash, M_KQUEUE);
2315
2316 FREE_ZONE(fdp, sizeof *fdp, M_FILEDESC);
2317 }
2318
2319 static int
2320 closef_finish(fp, fg, p)
2321 struct fileproc *fp;
2322 struct fileglob *fg;
2323 struct proc *p;
2324 {
2325 struct vnode *vp;
2326 struct flock lf;
2327 int error;
2328 struct vfs_context context;
2329
2330 if ((fg->fg_flag & FHASLOCK) && fg->fg_type == DTYPE_VNODE) {
2331 lf.l_whence = SEEK_SET;
2332 lf.l_start = 0;
2333 lf.l_len = 0;
2334 lf.l_type = F_UNLCK;
2335 vp = (struct vnode *)fg->fg_data;
2336 context.vc_proc = p;
2337 context.vc_ucred = fg->fg_cred;
2338
2339 (void) VNOP_ADVLOCK(vp, (caddr_t)fg, F_UNLCK, &lf, F_FLOCK, &context);
2340 }
2341 if (fg->fg_ops)
2342 error = fo_close(fg, p);
2343 else
2344 error = 0;
2345
2346 if (((fp != (struct fileproc *)0) && ((fp->f_flags & FP_INCHRREAD) != 0))) {
2347 proc_fdlock(p);
2348 if ( ((fp->f_flags & FP_INCHRREAD) != 0) ) {
2349 fileproc_drain(p, fp);
2350 }
2351 proc_fdunlock(p);
2352 }
2353 fg_free(fg);
2354
2355 return (error);
2356 }
2357
2358 int
2359 closef(fg, p)
2360 struct fileglob *fg;
2361 struct proc *p;
2362 {
2363 int error;
2364
2365 proc_fdlock(p);
2366 error = closef_locked((struct fileproc *)0, fg, p);
2367 proc_fdunlock(p);
2368
2369 return(error);
2370 }
2371 /*
2372 * Internal form of close.
2373 * Decrement reference count on file structure.
2374 * Note: p may be NULL when closing a file
2375 * that was being passed in a message.
2376 */
2377 int
2378 closef_locked(fp, fg, p)
2379 struct fileproc *fp;
2380 struct fileglob *fg;
2381 struct proc *p;
2382 {
2383 struct vnode *vp;
2384 struct flock lf;
2385 struct vfs_context context;
2386 int error;
2387
2388 if (fg == NULL) {
2389 return (0);
2390 }
2391 /*
2392 * POSIX record locking dictates that any close releases ALL
2393 * locks owned by this process. This is handled by setting
2394 * a flag in the unlock to free ONLY locks obeying POSIX
2395 * semantics, and not to free BSD-style file locks.
2396 * If the descriptor was in a message, POSIX-style locks
2397 * aren't passed with the descriptor.
2398 */
2399 if (p && (p->p_ladvflag & P_LADVLOCK) && fg->fg_type == DTYPE_VNODE) {
2400 proc_fdunlock(p);
2401
2402 lf.l_whence = SEEK_SET;
2403 lf.l_start = 0;
2404 lf.l_len = 0;
2405 lf.l_type = F_UNLCK;
2406 vp = (struct vnode *)fg->fg_data;
2407
2408 if ( (error = vnode_getwithref(vp)) == 0 ) {
2409 context.vc_proc = p;
2410 context.vc_ucred = fg->fg_cred;
2411 (void) VNOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX, &context);
2412
2413 (void)vnode_put(vp);
2414 }
2415 proc_fdlock(p);
2416 }
2417 lck_mtx_lock(&fg->fg_lock);
2418 fg->fg_count--;
2419
2420 if (fg->fg_count > 0) {
2421 lck_mtx_unlock(&fg->fg_lock);
2422 return (0);
2423 }
2424 if (fg->fg_count != 0)
2425 panic("fg: being freed with bad fg_count (%d)", fg, fg->fg_count);
2426
2427 if (fp && (fp->f_flags & FP_WRITTEN))
2428 fg->fg_flag |= FWASWRITTEN;
2429
2430 fg->fg_lflags |= FG_TERM;
2431 lck_mtx_unlock(&fg->fg_lock);
2432
2433 proc_fdunlock(p);
2434 error = closef_finish(fp, fg, p);
2435 proc_fdlock(p);
2436
2437 return(error);
2438 }
2439
2440
2441 extern int selwait;
2442 void
2443 fileproc_drain(struct proc *p, struct fileproc * fp)
2444 {
2445 fp->f_iocount-- ; /* (the one the close holds) */
2446
2447 while (fp->f_iocount) {
2448 if (((fp->f_flags & FP_INSELECT)== FP_INSELECT)) {
2449 wait_queue_wakeup_all((wait_queue_t)fp->f_waddr, &selwait, THREAD_INTERRUPTED);
2450 } else {
2451 if (fp->f_fglob->fg_ops->fo_drain) {
2452 (*fp->f_fglob->fg_ops->fo_drain)(fp, p);
2453 }
2454 }
2455 p->p_fpdrainwait = 1;
2456
2457 msleep(&p->p_fpdrainwait, &p->p_fdmlock, PRIBIO, "fpdrain",0);
2458
2459 //panic("successful wait after drain\n");
2460 }
2461 }
2462
2463 int
2464 fp_free(struct proc * p, int fd, struct fileproc * fp)
2465 {
2466 proc_fdlock(p);
2467 fdrelse(p, fd);
2468 proc_fdunlock(p);
2469
2470 fg_free(fp->f_fglob);
2471 FREE_ZONE(fp, sizeof *fp, M_FILEPROC);
2472 }
2473
2474
2475 /*
2476 * Apply an advisory lock on a file descriptor.
2477 *
2478 * Just attempt to get a record lock of the requested type on
2479 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2480 */
2481 int
2482 flock(struct proc *p, register struct flock_args *uap, __unused register_t *retval)
2483 {
2484 int fd = uap->fd;
2485 int how = uap->how;
2486 struct fileproc *fp;
2487 struct vnode *vp;
2488 struct flock lf;
2489 struct vfs_context context;
2490 int error=0;
2491
2492 AUDIT_ARG(fd, uap->fd);
2493 if ( (error = fp_getfvp(p, fd, &fp, &vp)) ) {
2494 return(error);
2495 }
2496 if ( (error = vnode_getwithref(vp)) ) {
2497 goto out1;
2498 }
2499 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
2500
2501 context.vc_proc = p;
2502 context.vc_ucred = fp->f_cred;
2503
2504 lf.l_whence = SEEK_SET;
2505 lf.l_start = 0;
2506 lf.l_len = 0;
2507 if (how & LOCK_UN) {
2508 lf.l_type = F_UNLCK;
2509 fp->f_flag &= ~FHASLOCK;
2510 error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_UNLCK, &lf, F_FLOCK, &context);
2511 goto out;
2512 }
2513 if (how & LOCK_EX)
2514 lf.l_type = F_WRLCK;
2515 else if (how & LOCK_SH)
2516 lf.l_type = F_RDLCK;
2517 else {
2518 error = EBADF;
2519 goto out;
2520 }
2521 fp->f_flag |= FHASLOCK;
2522 if (how & LOCK_NB) {
2523 error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_SETLK, &lf, F_FLOCK, &context);
2524 goto out;
2525 }
2526 error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_SETLK, &lf, F_FLOCK|F_WAIT, &context);
2527 out:
2528 (void)vnode_put(vp);
2529 out1:
2530 fp_drop(p, fd, fp, 0);
2531 return(error);
2532
2533 }
2534
2535 /*
2536 * File Descriptor pseudo-device driver (/dev/fd/).
2537 *
2538 * Opening minor device N dup()s the file (if any) connected to file
2539 * descriptor N belonging to the calling process. Note that this driver
2540 * consists of only the ``open()'' routine, because all subsequent
2541 * references to this file will be direct to the other driver.
2542 */
2543 int
2544 fdopen(dev_t dev, __unused int mode, __unused int type, struct proc *p)
2545 {
2546
2547 /*
2548 * XXX Kludge: set curproc->p_dupfd to contain the value of the
2549 * the file descriptor being sought for duplication. The error
2550 * return ensures that the vnode for this device will be released
2551 * by vn_open. Open will detect this special error and take the
2552 * actions in dupfdopen below. Other callers of vn_open or vnop_open
2553 * will simply report the error.
2554 */
2555 p->p_dupfd = minor(dev);
2556 return (ENODEV);
2557 }
2558
2559 /*
2560 * Duplicate the specified descriptor to a free descriptor.
2561 */
2562 int
2563 dupfdopen(fdp, indx, dfd, mode, error)
2564 register struct filedesc *fdp;
2565 register int indx, dfd;
2566 int mode;
2567 int error;
2568 {
2569 struct fileproc *wfp;
2570 struct fileproc *fp;
2571 struct proc * p = current_proc();
2572
2573 /*
2574 * If the to-be-dup'd fd number is greater than the allowed number
2575 * of file descriptors, or the fd to be dup'd has already been
2576 * closed, reject. Note, check for new == old is necessary as
2577 * falloc could allocate an already closed to-be-dup'd descriptor
2578 * as the new descriptor.
2579 */
2580 proc_fdlock(p);
2581
2582 fp = fdp->fd_ofiles[indx];
2583 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2584 (wfp = fdp->fd_ofiles[dfd]) == NULL || wfp == fp ||
2585 (fdp->fd_ofileflags[dfd] & UF_RESERVED)) {
2586
2587 proc_fdunlock(p);
2588 return (EBADF);
2589 }
2590 /*
2591 * There are two cases of interest here.
2592 *
2593 * For ENODEV simply dup (dfd) to file descriptor
2594 * (indx) and return.
2595 *
2596 * For ENXIO steal away the file structure from (dfd) and
2597 * store it in (indx). (dfd) is effectively closed by
2598 * this operation.
2599 *
2600 * Any other error code is just returned.
2601 */
2602 switch (error) {
2603 case ENODEV:
2604 /*
2605 * Check that the mode the file is being opened for is a
2606 * subset of the mode of the existing descriptor.
2607 */
2608 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2609 proc_fdunlock(p);
2610 return (EACCES);
2611 }
2612 if (indx > fdp->fd_lastfile)
2613 fdp->fd_lastfile = indx;
2614 (void)fg_ref(wfp);
2615
2616 if (fp->f_fglob)
2617 fg_free(fp->f_fglob);
2618 fp->f_fglob = wfp->f_fglob;
2619
2620 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2621
2622 proc_fdunlock(p);
2623 return (0);
2624
2625 case ENXIO:
2626 /*
2627 * Steal away the file pointer from dfd, and stuff it into indx.
2628 */
2629 if (indx > fdp->fd_lastfile)
2630 fdp->fd_lastfile = indx;
2631
2632 if (fp->f_fglob)
2633 fg_free(fp->f_fglob);
2634 fp->f_fglob = wfp->f_fglob;
2635
2636 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2637 _fdrelse(fdp, dfd);
2638
2639 proc_fdunlock(p);
2640
2641 FREE_ZONE(wfp, sizeof *fp, M_FILEPROC);
2642
2643 return (0);
2644
2645 default:
2646 proc_fdunlock(p);
2647 return (error);
2648 }
2649 /* NOTREACHED */
2650 }
2651
2652 void
2653 fg_ref(struct fileproc * fp)
2654 {
2655 struct fileglob *fg;
2656
2657 fg = fp->f_fglob;
2658
2659 lck_mtx_lock(&fg->fg_lock);
2660 fg->fg_count++;
2661 lck_mtx_unlock(&fg->fg_lock);
2662 }
2663
2664 void
2665 fg_drop(struct fileproc * fp)
2666 {
2667 struct fileglob *fg;
2668
2669 fg = fp->f_fglob;
2670 lck_mtx_lock(&fg->fg_lock);
2671 fg->fg_count--;
2672 lck_mtx_unlock(&fg->fg_lock);
2673 }
2674
2675
2676 void
2677 fg_insertuipc(struct fileglob * fg)
2678 {
2679 int insertque = 0;
2680
2681 lck_mtx_lock(&fg->fg_lock);
2682
2683 while (fg->fg_lflags & FG_RMMSGQ) {
2684 fg->fg_lflags |= FG_WRMMSGQ;
2685 msleep(&fg->fg_lflags, &fg->fg_lock, 0, "fg_insertuipc", 0);
2686 }
2687
2688 fg->fg_count++;
2689 fg->fg_msgcount++;
2690 if (fg->fg_msgcount == 1) {
2691 fg->fg_lflags |= FG_INSMSGQ;
2692 insertque=1;
2693 }
2694 lck_mtx_unlock(&fg->fg_lock);
2695
2696 if (insertque) {
2697 lck_mtx_lock(uipc_lock);
2698 LIST_INSERT_HEAD(&fmsghead, fg, f_msglist);
2699 lck_mtx_unlock(uipc_lock);
2700 lck_mtx_lock(&fg->fg_lock);
2701 fg->fg_lflags &= ~FG_INSMSGQ;
2702 if (fg->fg_lflags & FG_WINSMSGQ) {
2703 fg->fg_lflags &= ~FG_WINSMSGQ;
2704 wakeup(&fg->fg_lflags);
2705 }
2706 lck_mtx_unlock(&fg->fg_lock);
2707 }
2708
2709 }
2710
2711 void
2712 fg_removeuipc(struct fileglob * fg)
2713 {
2714 int removeque = 0;
2715
2716 lck_mtx_lock(&fg->fg_lock);
2717 while (fg->fg_lflags & FG_INSMSGQ) {
2718 fg->fg_lflags |= FG_WINSMSGQ;
2719 msleep(&fg->fg_lflags, &fg->fg_lock, 0, "fg_removeuipc", 0);
2720 }
2721 fg->fg_msgcount--;
2722 if (fg->fg_msgcount == 0) {
2723 fg->fg_lflags |= FG_RMMSGQ;
2724 removeque=1;
2725 }
2726 lck_mtx_unlock(&fg->fg_lock);
2727
2728 if (removeque) {
2729 lck_mtx_lock(uipc_lock);
2730 LIST_REMOVE(fg, f_msglist);
2731 lck_mtx_unlock(uipc_lock);
2732 lck_mtx_lock(&fg->fg_lock);
2733 fg->fg_lflags &= ~FG_RMMSGQ;
2734 if (fg->fg_lflags & FG_WRMMSGQ) {
2735 fg->fg_lflags &= ~FG_WRMMSGQ;
2736 wakeup(&fg->fg_lflags);
2737 }
2738 lck_mtx_unlock(&fg->fg_lock);
2739 }
2740 }
2741
2742
2743 int
2744 fo_read(struct fileproc *fp, struct uio *uio, kauth_cred_t cred, int flags, struct proc *p)
2745 {
2746 return ((*fp->f_ops->fo_read)(fp, uio, cred, flags, p));
2747 }
2748
2749 int
2750 fo_write(struct fileproc *fp, struct uio *uio, kauth_cred_t cred, int flags, struct proc *p)
2751 {
2752 return((*fp->f_ops->fo_write)(fp, uio, cred, flags, p));
2753 }
2754
2755 int
2756 fo_ioctl(struct fileproc *fp, u_long com, caddr_t data, struct proc *p)
2757 {
2758 int error;
2759
2760 proc_fdunlock(p);
2761 error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
2762 proc_fdlock(p);
2763 return(error);
2764 }
2765
2766 int
2767 fo_select(struct fileproc *fp, int which, void *wql, struct proc *p)
2768 {
2769 return((*fp->f_ops->fo_select)(fp, which, wql, p));
2770 }
2771
2772 int
2773 fo_close(struct fileglob *fg, struct proc *p)
2774 {
2775 return((*fg->fg_ops->fo_close)(fg, p));
2776 }
2777
2778 int
2779 fo_kqfilter(struct fileproc *fp, struct knote *kn, struct proc *p)
2780 {
2781 return ((*fp->f_ops->fo_kqfilter)(fp, kn, p));
2782 }
2783