]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kern_descrip.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / kern / kern_descrip.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
61 */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/filedesc.h>
66#include <sys/kernel.h>
67#include <sys/vnode.h>
68#include <sys/proc.h>
69#include <sys/file.h>
70#include <sys/socket.h>
71#include <sys/socketvar.h>
72#include <sys/stat.h>
73#include <sys/ioctl.h>
74#include <sys/fcntl.h>
75#include <sys/malloc.h>
76#include <sys/syslog.h>
77#include <sys/unistd.h>
78#include <sys/resourcevar.h>
79#include <sys/aio_kern.h>
80
81#include <bsm/audit_kernel.h>
82
83#include <sys/mount.h>
84
85/*
86 * Descriptor management.
87 */
88struct filelist filehead; /* head of list of open files */
89int nfiles; /* actual number of open files */
90
91static int frele_internal(struct file *);
92
93/*
94 * System calls on descriptors.
95 */
96/* ARGSUSED */
97int
98getdtablesize(p, uap, retval)
99 struct proc *p;
100 void *uap;
101 register_t *retval;
102{
103 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
104 return (0);
105}
106
107/* ARGSUSED */
108int
109ogetdtablesize(p, uap, retval)
110 struct proc *p;
111 void *uap;
112 register_t *retval;
113{
114 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, NOFILE);
115 return (0);
116}
117
118static __inline__
119void _fdrelse(fdp, fd)
120 register struct filedesc *fdp;
121 register int fd;
122{
123 if (fd < fdp->fd_freefile)
124 fdp->fd_freefile = fd;
125#if DIAGNOSTIC
126 if (fd > fdp->fd_lastfile)
127 panic("fdrelse: fd_lastfile inconsistent");
128#endif
129 fdp->fd_ofiles[fd] = NULL;
130 fdp->fd_ofileflags[fd] = 0;
131 while ((fd = fdp->fd_lastfile) > 0 &&
132 fdp->fd_ofiles[fd] == NULL &&
133 !(fdp->fd_ofileflags[fd] & UF_RESERVED))
134 fdp->fd_lastfile--;
135}
136
137/*
138 * Duplicate a file descriptor.
139 */
140struct dup_args {
141 u_int fd;
142};
143/* ARGSUSED */
144int
145dup(p, uap, retval)
146 struct proc *p;
147 struct dup_args *uap;
148 register_t *retval;
149{
150 register struct filedesc *fdp = p->p_fd;
151 register int old = uap->fd;
152 int new, error;
153
154 if ((u_int)old >= fdp->fd_nfiles ||
155 fdp->fd_ofiles[old] == NULL ||
156 (fdp->fd_ofileflags[old] & UF_RESERVED))
157 return (EBADF);
158 if (error = fdalloc(p, 0, &new))
159 return (error);
160 return (finishdup(fdp, old, new, retval));
161}
162
163/*
164 * Duplicate a file descriptor to a particular value.
165 */
166struct dup2_args {
167 u_int from;
168 u_int to;
169};
170/* ARGSUSED */
171int
172dup2(p, uap, retval)
173 struct proc *p;
174 struct dup2_args *uap;
175 register_t *retval;
176{
177 register struct filedesc *fdp = p->p_fd;
178 register int old = uap->from, new = uap->to;
179 int i, error;
180
181 if ((u_int)old >= fdp->fd_nfiles ||
182 fdp->fd_ofiles[old] == NULL ||
183 (fdp->fd_ofileflags[old] & UF_RESERVED) ||
184 (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
185 (u_int)new >= maxfiles)
186 return (EBADF);
187 if (old == new) {
188 *retval = new;
189 return (0);
190 }
191 if ((u_int)new >= fdp->fd_nfiles) {
192 if (error = fdalloc(p, new, &i))
193 return (error);
194 if (new != i) {
195 _fdrelse(fdp, i);
196 goto closeit;
197 }
198 } else {
199 struct file **fpp;
200 char flags;
201closeit:
202 if ((flags = fdp->fd_ofileflags[new]) & UF_RESERVED)
203 return (EBADF);
204 fdp->fd_ofileflags[new] = (flags & ~UF_MAPPED) | UF_RESERVED;
205 /*
206 * dup2() must succeed even if the close has an error.
207 */
208 if (*(fpp = &fdp->fd_ofiles[new])) {
209 struct file *fp = *fpp;
210
211 *fpp = NULL;
212 (void) closef(fp, p);
213 }
214 }
215 return (finishdup(fdp, old, new, retval));
216}
217
218/*
219 * The file control system call.
220 */
221struct fcntl_args {
222 int fd;
223 int cmd;
224 int arg;
225};
226/* ARGSUSED */
227int
228fcntl(p, uap, retval)
229 struct proc *p;
230 register struct fcntl_args *uap;
231 register_t *retval;
232{
233 int fd = uap->fd;
234 register struct filedesc *fdp = p->p_fd;
235 register struct file *fp;
236 register char *pop;
237 struct vnode *vp, *devvp;
238 int i, tmp, error, error2, flg = F_POSIX;
239 struct flock fl;
240 fstore_t alloc_struct; /* structure for allocate command */
241 u_int32_t alloc_flags = 0;
242 off_t offset; /* used for F_SETSIZE */
243 int newmin;
244 struct radvisory ra_struct;
245 fbootstraptransfer_t fbt_struct; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
246 struct log2phys l2p_struct; /* structure for allocate command */
247 daddr_t lbn, bn;
248 int devBlockSize = 0;
249
250 AUDIT_ARG(fd, uap->fd);
251 AUDIT_ARG(cmd, uap->cmd);
252 if ((u_int)fd >= fdp->fd_nfiles ||
253 (fp = fdp->fd_ofiles[fd]) == NULL ||
254 (fdp->fd_ofileflags[fd] & UF_RESERVED))
255 return (EBADF);
256 pop = &fdp->fd_ofileflags[fd];
257
258 switch (uap->cmd) {
259
260 case F_DUPFD:
261 newmin = (long)uap->arg;
262 if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
263 (u_int)newmin >= maxfiles)
264 return (EINVAL);
265 if (error = fdalloc(p, newmin, &i))
266 return (error);
267 return (finishdup(fdp, fd, i, retval));
268
269 case F_GETFD:
270 *retval = (*pop & UF_EXCLOSE)? 1 : 0;
271 return (0);
272
273 case F_SETFD:
274 *pop = (*pop &~ UF_EXCLOSE) |
275 ((long)(uap->arg) & 1)? UF_EXCLOSE : 0;
276 return (0);
277
278 case F_GETFL:
279 *retval = OFLAGS(fp->f_flag);
280 return (0);
281
282 case F_SETFL:
283 fp->f_flag &= ~FCNTLFLAGS;
284 fp->f_flag |= FFLAGS((long)uap->arg) & FCNTLFLAGS;
285 tmp = fp->f_flag & FNONBLOCK;
286 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
287 if (error)
288 return (error);
289 tmp = fp->f_flag & FASYNC;
290 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
291 if (!error)
292 return (0);
293 fp->f_flag &= ~FNONBLOCK;
294 tmp = 0;
295 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
296 return (error);
297
298 case F_GETOWN:
299 if (fp->f_type == DTYPE_SOCKET) {
300 *retval = ((struct socket *)fp->f_data)->so_pgid;
301 return (0);
302 }
303 error = fo_ioctl(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
304 *retval = -*retval;
305 return (error);
306
307 case F_SETOWN:
308 if (fp->f_type == DTYPE_SOCKET) {
309 ((struct socket *)fp->f_data)->so_pgid =
310 (long)uap->arg;
311 return (0);
312 }
313 if ((long)uap->arg <= 0) {
314 uap->arg = (int)(-(long)(uap->arg));
315 } else {
316 struct proc *p1 = pfind((long)uap->arg);
317 if (p1 == 0)
318 return (ESRCH);
319 uap->arg = (int)p1->p_pgrp->pg_id;
320 }
321 return (fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
322
323 case F_SETLKW:
324 flg |= F_WAIT;
325 /* Fall into F_SETLK */
326
327 case F_SETLK:
328 if (fp->f_type != DTYPE_VNODE)
329 return (EBADF);
330 vp = (struct vnode *)fp->f_data;
331
332 /* Copy in the lock structure */
333 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
334 if (error)
335 break;
336 if (fl.l_whence == SEEK_CUR)
337 fl.l_start += fp->f_offset;
338 switch (fl.l_type) {
339
340 case F_RDLCK:
341 if ((fp->f_flag & FREAD) != 0) {
342 p->p_flag |= P_ADVLOCK;
343 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
344 } else
345 error = EBADF;
346 break;
347
348 case F_WRLCK:
349 if ((fp->f_flag & FWRITE) != 0) {
350 p->p_flag |= P_ADVLOCK;
351 error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
352 } else
353 error = EBADF;
354 break;
355
356 case F_UNLCK:
357 error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl, F_POSIX);
358 break;
359
360 default:
361 error = EINVAL;
362 break;
363 }
364 break;
365
366 case F_GETLK:
367 if (fp->f_type != DTYPE_VNODE)
368 return (EBADF);
369 vp = (struct vnode *)fp->f_data;
370
371 /* Copy in the lock structure */
372 error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
373 if (error)
374 break;
375 if (fl.l_whence == SEEK_CUR)
376 fl.l_start += fp->f_offset;
377 error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
378 if (error)
379 break;
380 error = copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl));
381 break;
382
383 case F_PREALLOCATE:
384 if (fp->f_type != DTYPE_VNODE)
385 return (EBADF);
386 vp = (struct vnode *)fp->f_data;
387
388 /* make sure that we have write permission */
389 if ((fp->f_flag & FWRITE) == 0) {
390 error = EBADF;
391 break;
392 }
393
394 error = copyin((caddr_t)uap->arg, (caddr_t)&alloc_struct,
395 sizeof (alloc_struct));
396 if (error)
397 break;
398
399 /* now set the space allocated to 0 */
400 alloc_struct.fst_bytesalloc = 0;
401
402 /*
403 * Do some simple parameter checking
404 */
405
406 /* set up the flags */
407
408 alloc_flags |= PREALLOCATE;
409
410 if (alloc_struct.fst_flags & F_ALLOCATECONTIG)
411 alloc_flags |= ALLOCATECONTIG;
412
413 if (alloc_struct.fst_flags & F_ALLOCATEALL)
414 alloc_flags |= ALLOCATEALL;
415
416 /*
417 * Do any position mode specific stuff. The only
418 * position mode supported now is PEOFPOSMODE
419 */
420
421 switch (alloc_struct.fst_posmode) {
422
423 case F_PEOFPOSMODE:
424 if (alloc_struct.fst_offset == 0)
425 alloc_flags |= ALLOCATEFROMPEOF;
426 else
427 error = EINVAL;
428 break;
429
430 case F_VOLPOSMODE:
431 if (alloc_struct.fst_offset > 0)
432 alloc_flags |= ALLOCATEFROMVOL;
433 else
434 error = EINVAL;
435 break;
436
437 default:
438 error = EINVAL;
439 break;
440 }
441
442 if (error)
443 break;
444
445 /* lock the vnode and call allocate to get the space */
446 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
447 if (error)
448 break;
449 error = VOP_ALLOCATE(vp,alloc_struct.fst_length,alloc_flags,
450 &alloc_struct.fst_bytesalloc, alloc_struct.fst_offset,
451 fp->f_cred, p);
452 VOP_UNLOCK(vp, 0, p);
453
454 if (error2 = copyout((caddr_t)&alloc_struct,
455 (caddr_t)uap->arg,
456 sizeof (alloc_struct))) {
457 if (!error)
458 error = error2;
459 }
460 break;
461
462 case F_SETSIZE:
463 if (fp->f_type != DTYPE_VNODE)
464 return (EBADF);
465 vp = (struct vnode *)fp->f_data;
466
467 error = copyin((caddr_t)uap->arg, (caddr_t)&offset,
468 sizeof (off_t));
469 if (error)
470 break;
471
472 /*
473 * Make sure that we are root. Growing a file
474 * without zero filling the data is a security hole
475 * root would have access anyway so we'll allow it
476 */
477
478 if (!is_suser()) {
479 error = EACCES;
480 break;
481 }
482
483 /* lock the vnode and call allocate to get the space */
484 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
485 if (error)
486 break;
487 error = VOP_TRUNCATE(vp,offset,IO_NOZEROFILL,fp->f_cred,p);
488 VOP_UNLOCK(vp,0,p);
489 break;
490
491 case F_RDAHEAD:
492 if (fp->f_type != DTYPE_VNODE)
493 return (EBADF);
494 vp = (struct vnode *)fp->f_data;
495
496 simple_lock(&vp->v_interlock);
497 if (uap->arg)
498 vp->v_flag &= ~VRAOFF;
499 else
500 vp->v_flag |= VRAOFF;
501 simple_unlock(&vp->v_interlock);
502 error = 0;
503 break;
504
505 case F_NOCACHE:
506 if (fp->f_type != DTYPE_VNODE)
507 return (EBADF);
508 vp = (struct vnode *)fp->f_data;
509
510 simple_lock(&vp->v_interlock);
511 if (uap->arg)
512 vp->v_flag |= VNOCACHE_DATA;
513 else
514 vp->v_flag &= ~VNOCACHE_DATA;
515 simple_unlock(&vp->v_interlock);
516 error = 0;
517 break;
518
519 case F_RDADVISE:
520 if (fp->f_type != DTYPE_VNODE)
521 return (EBADF);
522 vp = (struct vnode *)fp->f_data;
523
524 if (error = copyin((caddr_t)uap->arg,
525 (caddr_t)&ra_struct, sizeof (ra_struct)))
526 break;
527 error = VOP_IOCTL(vp, 1, (caddr_t)&ra_struct, 0, fp->f_cred, p);
528 break;
529
530 case F_CHKCLEAN:
531 /*
532 * used by regression test to determine if
533 * all the dirty pages (via write) have been cleaned
534 * after a call to 'fsysnc'.
535 */
536 if (fp->f_type != DTYPE_VNODE)
537 return (EBADF);
538 vp = (struct vnode *)fp->f_data;
539
540 error = VOP_IOCTL(vp, 5, 0, 0, fp->f_cred, p);
541 break;
542
543 case F_READBOOTSTRAP:
544 case F_WRITEBOOTSTRAP:
545 if (fp->f_type != DTYPE_VNODE)
546 return (EBADF);
547 vp = (struct vnode *)fp->f_data;
548
549 error = copyin((caddr_t)uap->arg, (caddr_t)&fbt_struct,
550 sizeof (fbt_struct));
551 if (error)
552 break;
553
554 if (uap->cmd == F_WRITEBOOTSTRAP) {
555 /*
556 * Make sure that we are root. Updating the
557 * bootstrap on a disk could be a security hole
558 */
559 if (!is_suser()) {
560 error = EACCES;
561 break;
562 }
563 }
564
565 if (vp->v_tag != VT_HFS) /* XXX */
566 error = EINVAL;
567 else {
568 /* lock the vnode and call VOP_IOCTL to handle the I/O */
569 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
570 if (error)
571 break;
572 error = VOP_IOCTL(vp, (uap->cmd == F_WRITEBOOTSTRAP) ? 3 : 2,
573 (caddr_t)&fbt_struct, 0, fp->f_cred, p);
574 VOP_UNLOCK(vp,0,p);
575 }
576 break;
577
578 case F_LOG2PHYS:
579 if (fp->f_type != DTYPE_VNODE)
580 return (EBADF);
581 vp = (struct vnode *)fp->f_data;
582
583 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
584 if (error)
585 break;
586 error = VOP_OFFTOBLK(vp, fp->f_offset, &lbn);
587 if (error)
588 break;
589 error = VOP_BLKTOOFF(vp, lbn, &offset);
590 if (error)
591 break;
592 error = VOP_BMAP(vp, lbn, &devvp, &bn, 0);
593 VOP_DEVBLOCKSIZE(devvp, &devBlockSize);
594 VOP_UNLOCK(vp, 0, p);
595 if (!error) {
596 l2p_struct.l2p_flags = 0; /* for now */
597 l2p_struct.l2p_contigbytes = 0; /* for now */
598 l2p_struct.l2p_devoffset = bn * devBlockSize;
599 l2p_struct.l2p_devoffset += fp->f_offset - offset;
600 error = copyout((caddr_t)&l2p_struct,
601 (caddr_t)uap->arg,
602 sizeof (l2p_struct));
603 }
604 break;
605
606 case F_GETPATH: {
607 char *pathbuf;
608 int len;
609 extern int vn_getpath(struct vnode *vp, char *pathbuf, int *len);
610
611 if (fp->f_type != DTYPE_VNODE)
612 return (EBADF);
613 vp = (struct vnode *)fp->f_data;
614
615 len = MAXPATHLEN;
616 MALLOC(pathbuf, char *, len, M_TEMP, M_WAITOK);
617
618 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
619 if (error) {
620 FREE(pathbuf, M_TEMP);
621 break;
622 }
623 error = vn_getpath(vp, pathbuf, &len);
624 if (error == 0)
625 error = copyout((caddr_t)pathbuf, (caddr_t)uap->arg, len);
626 VOP_UNLOCK(vp, 0, p);
627 FREE(pathbuf, M_TEMP);
628 break;
629 }
630
631 case F_FULLFSYNC: {
632 if (fp->f_type != DTYPE_VNODE)
633 return (EBADF);
634 vp = (struct vnode *)fp->f_data;
635
636 error = vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, p);
637 if (error)
638 break;
639
640 error = VOP_IOCTL(vp, 6, (caddr_t)NULL, 0, fp->f_cred, p);
641 VOP_UNLOCK(vp, 0, p);
642 break;
643 }
644
645 default:
646 return (EINVAL);
647 }
648
649 /*
650 * Fall thru to here for all vnode operations.
651 * We audit the path after the call to avoid
652 * triggering file table state changes during
653 * the audit pathname allocation.
654 */
655 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
656 return error;
657}
658
659/*
660 * Common code for dup, dup2, and fcntl(F_DUPFD).
661 */
662int
663finishdup(fdp, old, new, retval)
664 register struct filedesc *fdp;
665 register int old, new;
666 register_t *retval;
667{
668 register struct file *fp;
669
670 if ((fp = fdp->fd_ofiles[old]) == NULL ||
671 (fdp->fd_ofileflags[old] & UF_RESERVED)) {
672 _fdrelse(fdp, new);
673 return (EBADF);
674 }
675 fdp->fd_ofiles[new] = fp;
676 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
677 (void)fref(fp);
678 if (new > fdp->fd_lastfile)
679 fdp->fd_lastfile = new;
680 *retval = new;
681 return (0);
682}
683
684/*
685 * Close a file descriptor.
686 */
687struct close_args {
688 int fd;
689};
690/* ARGSUSED */
691int
692close(p, uap, retval)
693 struct proc *p;
694 struct close_args *uap;
695 register_t *retval;
696{
697 int fd = uap->fd;
698 register struct filedesc *fdp = p->p_fd;
699 register struct file *fp;
700
701 AUDIT_SYSCLOSE(p, fd);
702 if ((u_int)fd >= fdp->fd_nfiles ||
703 (fp = fdp->fd_ofiles[fd]) == NULL ||
704 (fdp->fd_ofileflags[fd] & UF_RESERVED))
705 return (EBADF);
706
707 /* Keep people from using the filedesc while we are closing it */
708 fdp->fd_ofileflags[fd] |= UF_RESERVED;
709
710 /* cancel all async IO requests that can be cancelled. */
711 _aio_close( p, fd );
712
713 if (fd < fdp->fd_knlistsize)
714 knote_fdclose(p, fd);
715
716 _fdrelse(fdp, fd);
717 return (closef(fp, p));
718}
719
720/*
721 * Return status information about a file descriptor.
722 */
723struct fstat_args {
724 int fd;
725 struct stat *sb;
726};
727/* ARGSUSED */
728int
729fstat(p, uap, retval)
730 struct proc *p;
731 register struct fstat_args *uap;
732 register_t *retval;
733{
734 int fd = uap->fd;
735 register struct filedesc *fdp = p->p_fd;
736 register struct file *fp;
737 struct stat ub;
738 int error;
739
740 AUDIT_ARG(fd, uap->fd);
741 if ((u_int)fd >= fdp->fd_nfiles ||
742 (fp = fdp->fd_ofiles[fd]) == NULL ||
743 (fdp->fd_ofileflags[fd] & UF_RESERVED))
744 return (EBADF);
745 switch (fp->f_type) {
746
747 case DTYPE_VNODE:
748 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
749 if (error == 0) {
750 AUDIT_ARG(vnpath, (struct vnode *)fp->f_data, ARG_VNODE1);
751 }
752 break;
753
754 case DTYPE_SOCKET:
755 error = soo_stat((struct socket *)fp->f_data, &ub);
756 break;
757
758 case DTYPE_PSXSHM:
759 error = pshm_stat((void *)fp->f_data, &ub);
760 break;
761
762 case DTYPE_KQUEUE:
763 error = kqueue_stat(fp, &ub, p);
764 break;
765
766 default:
767 panic("fstat");
768 /*NOTREACHED*/
769 }
770 if (error == 0)
771 error = copyout((caddr_t)&ub, (caddr_t)uap->sb,
772 sizeof (ub));
773 return (error);
774}
775
776#if COMPAT_43
777/*
778 * Return status information about a file descriptor.
779 */
780struct ofstat_args {
781 int fd;
782 struct ostat *sb;
783};
784/* ARGSUSED */
785ofstat(p, uap, retval)
786 struct proc *p;
787 register struct ofstat_args *uap;
788 register_t *retval;
789{
790 int fd = uap->fd;
791 register struct filedesc *fdp = p->p_fd;
792 register struct file *fp;
793 struct stat ub;
794 struct ostat oub;
795 int error;
796
797 if ((u_int)fd >= fdp->fd_nfiles ||
798 (fp = fdp->fd_ofiles[fd]) == NULL ||
799 (fdp->fd_ofileflags[fd] & UF_RESERVED))
800 return (EBADF);
801 switch (fp->f_type) {
802
803 case DTYPE_VNODE:
804 error = vn_stat((struct vnode *)fp->f_data, &ub, p);
805 break;
806
807 case DTYPE_SOCKET:
808 error = soo_stat((struct socket *)fp->f_data, &ub);
809 break;
810
811 default:
812 panic("ofstat");
813 /*NOTREACHED*/
814 }
815 cvtstat(&ub, &oub);
816 if (error == 0)
817 error = copyout((caddr_t)&oub, (caddr_t)uap->sb,
818 sizeof (oub));
819 return (error);
820}
821#endif /* COMPAT_43 */
822
823/*
824 * Return pathconf information about a file descriptor.
825 */
826struct fpathconf_args {
827 int fd;
828 int name;
829};
830/* ARGSUSED */
831fpathconf(p, uap, retval)
832 struct proc *p;
833 register struct fpathconf_args *uap;
834 register_t *retval;
835{
836 int fd = uap->fd;
837 struct filedesc *fdp = p->p_fd;
838 struct file *fp;
839 struct vnode *vp;
840
841 AUDIT_ARG(fd, uap->fd);
842 if ((u_int)fd >= fdp->fd_nfiles ||
843 (fp = fdp->fd_ofiles[fd]) == NULL ||
844 (fdp->fd_ofileflags[fd] & UF_RESERVED))
845 return (EBADF);
846 switch (fp->f_type) {
847
848 case DTYPE_SOCKET:
849 if (uap->name != _PC_PIPE_BUF)
850 return (EINVAL);
851 *retval = PIPE_BUF;
852 return (0);
853
854 case DTYPE_VNODE:
855 vp = (struct vnode *)fp->f_data;
856 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
857
858 return (VOP_PATHCONF(vp, uap->name, retval));
859
860 default:
861 panic("fpathconf");
862 }
863 /*NOTREACHED*/
864}
865
866/*
867 * Allocate a file descriptor for the process.
868 */
869int fdexpand;
870
871int
872fdalloc(p, want, result)
873 struct proc *p;
874 int want;
875 int *result;
876{
877 register struct filedesc *fdp = p->p_fd;
878 register int i;
879 int lim, last, nfiles, oldnfiles;
880 struct file **newofiles, **ofiles;
881 char *newofileflags, *ofileflags;
882
883 /*
884 * Search for a free descriptor starting at the higher
885 * of want or fd_freefile. If that fails, consider
886 * expanding the ofile array.
887 */
888 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
889 for (;;) {
890 last = min(fdp->fd_nfiles, lim);
891 if ((i = want) < fdp->fd_freefile)
892 i = fdp->fd_freefile;
893 ofiles = &fdp->fd_ofiles[i];
894 ofileflags = &fdp->fd_ofileflags[i];
895 for (; i < last; i++) {
896 if (*ofiles == NULL && !(*ofileflags & UF_RESERVED)) {
897 *ofileflags = UF_RESERVED;
898 if (i > fdp->fd_lastfile)
899 fdp->fd_lastfile = i;
900 if (want <= fdp->fd_freefile)
901 fdp->fd_freefile = i;
902 *result = i;
903 return (0);
904 }
905 ofiles++; ofileflags++;
906 }
907
908 /*
909 * No space in current array. Expand?
910 */
911 if (fdp->fd_nfiles >= lim)
912 return (EMFILE);
913 if (fdp->fd_nfiles < NDEXTENT)
914 nfiles = NDEXTENT;
915 else
916 nfiles = 2 * fdp->fd_nfiles;
917 /* Enforce lim */
918 if (nfiles > lim)
919 nfiles = lim;
920 MALLOC_ZONE(newofiles, struct file **,
921 nfiles * OFILESIZE, M_OFILETABL, M_WAITOK);
922 if (fdp->fd_nfiles >= nfiles) {
923 FREE_ZONE(newofiles, nfiles * OFILESIZE, M_OFILETABL);
924 continue;
925 }
926 newofileflags = (char *) &newofiles[nfiles];
927 /*
928 * Copy the existing ofile and ofileflags arrays
929 * and zero the new portion of each array.
930 */
931 oldnfiles = fdp->fd_nfiles;
932 (void) memcpy(newofiles, fdp->fd_ofiles,
933 oldnfiles * sizeof *fdp->fd_ofiles);
934 (void) memset(&newofiles[oldnfiles], 0,
935 (nfiles - oldnfiles) * sizeof *fdp->fd_ofiles);
936
937 (void) memcpy(newofileflags, fdp->fd_ofileflags,
938 oldnfiles * sizeof *fdp->fd_ofileflags);
939 (void) memset(&newofileflags[oldnfiles], 0,
940 (nfiles - oldnfiles) *
941 sizeof *fdp->fd_ofileflags);
942 ofiles = fdp->fd_ofiles;
943 fdp->fd_ofiles = newofiles;
944 fdp->fd_ofileflags = newofileflags;
945 fdp->fd_nfiles = nfiles;
946 FREE_ZONE(ofiles, oldnfiles * OFILESIZE, M_OFILETABL);
947 fdexpand++;
948 }
949}
950
951/*
952 * Check to see whether n user file descriptors
953 * are available to the process p.
954 */
955int
956fdavail(p, n)
957 struct proc *p;
958 register int n;
959{
960 register struct filedesc *fdp = p->p_fd;
961 register struct file **fpp;
962 register char *flags;
963 register int i, lim;
964
965 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
966 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
967 return (1);
968 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
969 flags = &fdp->fd_ofileflags[fdp->fd_freefile];
970 for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++, flags++)
971 if (*fpp == NULL && !(*flags & UF_RESERVED) && --n <= 0)
972 return (1);
973 return (0);
974}
975
976void
977fdrelse(p, fd)
978 struct proc *p;
979 int fd;
980{
981 _fdrelse(p->p_fd, fd);
982}
983
984int
985fdgetf(p, fd, resultfp)
986 register struct proc *p;
987 register int fd;
988 struct file **resultfp;
989{
990 register struct filedesc *fdp = p->p_fd;
991 struct file *fp;
992
993 if ((u_int)fd >= fdp->fd_nfiles ||
994 (fp = fdp->fd_ofiles[fd]) == NULL ||
995 (fdp->fd_ofileflags[fd] & UF_RESERVED))
996 return (EBADF);
997
998 if (resultfp)
999 *resultfp = fp;
1000 return (0);
1001}
1002
1003/*
1004 * Create a new open file structure and allocate
1005 * a file decriptor for the process that refers to it.
1006 */
1007int
1008falloc(p, resultfp, resultfd)
1009 register struct proc *p;
1010 struct file **resultfp;
1011 int *resultfd;
1012{
1013 register struct file *fp, *fq;
1014 int error, i;
1015
1016 if (error = fdalloc(p, 0, &i))
1017 return (error);
1018 if (nfiles >= maxfiles) {
1019 tablefull("file");
1020 return (ENFILE);
1021 }
1022 /*
1023 * Allocate a new file descriptor.
1024 * If the process has file descriptor zero open, add to the list
1025 * of open files at that point, otherwise put it at the front of
1026 * the list of open files.
1027 */
1028 nfiles++;
1029 MALLOC_ZONE(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
1030 bzero(fp, sizeof(struct file));
1031 p->p_fd->fd_ofiles[i] = fp;
1032 fp->f_count = 1;
1033 fp->f_cred = p->p_ucred;
1034 crhold(fp->f_cred);
1035 if (resultfp)
1036 *resultfp = fp;
1037 if (resultfd)
1038 *resultfd = i;
1039 if (fq = p->p_fd->fd_ofiles[0]) {
1040 LIST_INSERT_AFTER(fq, fp, f_list);
1041 } else {
1042 LIST_INSERT_HEAD(&filehead, fp, f_list);
1043 }
1044 return (0);
1045}
1046
1047/*
1048 * Free a file structure.
1049 */
1050void
1051ffree(fp)
1052 register struct file *fp;
1053{
1054 register struct file *fq;
1055 struct ucred *cred;
1056
1057 LIST_REMOVE(fp, f_list);
1058 cred = fp->f_cred;
1059 if (cred != NOCRED) {
1060 fp->f_cred = NOCRED;
1061 crfree(cred);
1062 }
1063
1064 nfiles--;
1065 memset(fp, 0xff, sizeof *fp);
1066 fp->f_count = (short)0xffff;
1067
1068 FREE_ZONE(fp, sizeof *fp, M_FILE);
1069}
1070
1071void
1072fdexec(p)
1073 struct proc *p;
1074{
1075 register struct filedesc *fdp = p->p_fd;
1076 register int i = fdp->fd_lastfile;
1077 register struct file **fpp = &fdp->fd_ofiles[i];
1078 register char *flags = &fdp->fd_ofileflags[i];
1079
1080 while (i >= 0) {
1081 if ((*flags & (UF_RESERVED|UF_EXCLOSE)) == UF_EXCLOSE) {
1082 register struct file *fp = *fpp;
1083
1084 if (i < fdp->fd_knlistsize)
1085 knote_fdclose(p, i);
1086
1087 *fpp = NULL; *flags = 0;
1088 if (i == fdp->fd_lastfile && i > 0)
1089 fdp->fd_lastfile--;
1090 closef(fp, p);
1091 }
1092 else
1093 *flags &= ~UF_MAPPED;
1094
1095 i--; fpp--; flags--;
1096 }
1097}
1098
1099/*
1100 * Copy a filedesc structure.
1101 */
1102struct filedesc *
1103fdcopy(p)
1104 struct proc *p;
1105{
1106 register struct filedesc *newfdp, *fdp = p->p_fd;
1107 register int i;
1108
1109 MALLOC_ZONE(newfdp, struct filedesc *,
1110 sizeof *newfdp, M_FILEDESC, M_WAITOK);
1111 (void) memcpy(newfdp, fdp, sizeof *newfdp);
1112 VREF(newfdp->fd_cdir);
1113 if (newfdp->fd_rdir)
1114 VREF(newfdp->fd_rdir);
1115 newfdp->fd_refcnt = 1;
1116
1117 /*
1118 * If the number of open files fits in the internal arrays
1119 * of the open file structure, use them, otherwise allocate
1120 * additional memory for the number of descriptors currently
1121 * in use.
1122 */
1123 if (newfdp->fd_lastfile < NDFILE)
1124 i = NDFILE;
1125 else {
1126 /*
1127 * Compute the smallest multiple of NDEXTENT needed
1128 * for the file descriptors currently in use,
1129 * allowing the table to shrink.
1130 */
1131 i = newfdp->fd_nfiles;
1132 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1133 i /= 2;
1134 }
1135 MALLOC_ZONE(newfdp->fd_ofiles, struct file **,
1136 i * OFILESIZE, M_OFILETABL, M_WAITOK);
1137 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1138 newfdp->fd_nfiles = i;
1139 if (fdp->fd_nfiles > 0) {
1140 register struct file **fpp;
1141 register char *flags;
1142
1143 (void) memcpy(newfdp->fd_ofiles, fdp->fd_ofiles,
1144 i * sizeof *fdp->fd_ofiles);
1145 (void) memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags,
1146 i * sizeof *fdp->fd_ofileflags);
1147
1148 /*
1149 * kq descriptors cannot be copied.
1150 */
1151 if (newfdp->fd_knlistsize != -1) {
1152 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1153 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1154 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1155 *fpp = NULL;
1156 if (i < newfdp->fd_freefile)
1157 newfdp->fd_freefile = i;
1158 }
1159 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1160 newfdp->fd_lastfile--;
1161 }
1162 newfdp->fd_knlist = NULL;
1163 newfdp->fd_knlistsize = -1;
1164 newfdp->fd_knhash = NULL;
1165 newfdp->fd_knhashmask = 0;
1166 }
1167
1168 fpp = newfdp->fd_ofiles;
1169 flags = newfdp->fd_ofileflags;
1170 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++, flags++)
1171 if (*fpp != NULL && !(*flags & UF_RESERVED)) {
1172 (void)fref(*fpp);
1173 } else {
1174 *fpp = NULL;
1175 *flags = 0;
1176 }
1177 } else
1178 (void) memset(newfdp->fd_ofiles, 0, i * OFILESIZE);
1179
1180 return (newfdp);
1181}
1182
1183/*
1184 * Release a filedesc structure.
1185 */
1186void
1187fdfree(p)
1188 struct proc *p;
1189{
1190 struct filedesc *fdp;
1191 struct file *fp;
1192 int i;
1193 struct vnode *tvp;
1194
1195 /* Certain daemons might not have file descriptors */
1196 if ((fdp = p->p_fd) == NULL)
1197 return;
1198
1199 if (--fdp->fd_refcnt > 0)
1200 return;
1201
1202 /* Last reference: the structure can't change out from under us */
1203 if (fdp->fd_nfiles > 0) {
1204 for (i = fdp->fd_lastfile; i >= 0; i--)
1205#if 1 /* WORKAROUND */
1206 /*
1207 * Merlot: need to remove the bogus f_data check
1208 * from the following "if" statement. It's there
1209 * because of the network/kernel funnel race on a
1210 * close of a socket vs. fdfree on exit. See
1211 * Radar rdar://problem/3365650 for details, but
1212 * the sort version is the commment before the "if"
1213 * above is wrong under certain circumstances.
1214 *
1215 * We have to do this twice, in case knote_fdclose()
1216 * results in a block.
1217 *
1218 * This works because an fdfree() will set all fields
1219 * in the struct file to -1.
1220 */
1221 if ((fp = fdp->fd_ofiles[i]) != NULL &&
1222 fp->f_data != (caddr_t)-1) {
1223 if (i < fdp->fd_knlistsize)
1224 knote_fdclose(p, i);
1225 if (fp->f_data != (caddr_t)-1)
1226 (void) closef(fp, p);
1227 }
1228#else /* !WORKAROUND */
1229 if ((fp = fdp->fd_ofiles[i]) != NULL) {
1230 if (i < fdp->fd_knlistsize)
1231 knote_fdclose(p, i);
1232 (void) closef(fp, p);
1233 }
1234#endif /* !WORKAROUND */
1235 FREE_ZONE(fdp->fd_ofiles,
1236 fdp->fd_nfiles * OFILESIZE, M_OFILETABL);
1237 }
1238
1239 tvp = fdp->fd_cdir;
1240 fdp->fd_cdir = NULL;
1241 vrele(tvp);
1242
1243 if (fdp->fd_rdir) {
1244 tvp = fdp->fd_rdir;
1245 fdp->fd_rdir = NULL;
1246 vrele(tvp);
1247 }
1248
1249 if (fdp->fd_knlist)
1250 FREE(fdp->fd_knlist, M_KQUEUE);
1251 if (fdp->fd_knhash)
1252 FREE(fdp->fd_knhash, M_KQUEUE);
1253
1254 FREE_ZONE(fdp, sizeof *fdp, M_FILEDESC);
1255
1256 // XXXdbg
1257 {
1258 void clean_up_fmod_watch(struct proc *p);
1259 clean_up_fmod_watch(p);
1260 }
1261}
1262
1263static int
1264closef_finish(fp, p)
1265 register struct file *fp;
1266 register struct proc *p;
1267{
1268 struct vnode *vp;
1269 struct flock lf;
1270 int error;
1271
1272 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1273 lf.l_whence = SEEK_SET;
1274 lf.l_start = 0;
1275 lf.l_len = 0;
1276 lf.l_type = F_UNLCK;
1277 vp = (struct vnode *)fp->f_data;
1278 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1279 }
1280 if (fp->f_ops)
1281 error = fo_close(fp, p);
1282 else
1283 error = 0;
1284 ffree(fp);
1285 return (error);
1286}
1287
1288/*
1289 * Internal form of close.
1290 * Decrement reference count on file structure.
1291 * Note: p may be NULL when closing a file
1292 * that was being passed in a message.
1293 */
1294int
1295closef(fp, p)
1296 register struct file *fp;
1297 register struct proc *p;
1298{
1299 struct vnode *vp;
1300 struct flock lf;
1301 int error;
1302
1303 if (fp == NULL)
1304 return (0);
1305 /*
1306 * POSIX record locking dictates that any close releases ALL
1307 * locks owned by this process. This is handled by setting
1308 * a flag in the unlock to free ONLY locks obeying POSIX
1309 * semantics, and not to free BSD-style file locks.
1310 * If the descriptor was in a message, POSIX-style locks
1311 * aren't passed with the descriptor.
1312 */
1313 if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1314 lf.l_whence = SEEK_SET;
1315 lf.l_start = 0;
1316 lf.l_len = 0;
1317 lf.l_type = F_UNLCK;
1318 vp = (struct vnode *)fp->f_data;
1319 (void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
1320 }
1321 if (frele_internal(fp) > 0)
1322 return (0);
1323 return(closef_finish(fp, p));
1324}
1325
1326/*
1327 * Apply an advisory lock on a file descriptor.
1328 *
1329 * Just attempt to get a record lock of the requested type on
1330 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1331 */
1332struct flock_args {
1333 int fd;
1334 int how;
1335};
1336/* ARGSUSED */
1337int
1338flock(p, uap, retval)
1339 struct proc *p;
1340 register struct flock_args *uap;
1341 register_t *retval;
1342{
1343 int fd = uap->fd;
1344 int how = uap->how;
1345 register struct filedesc *fdp = p->p_fd;
1346 register struct file *fp;
1347 struct vnode *vp;
1348 struct flock lf;
1349
1350 AUDIT_ARG(fd, uap->fd);
1351 if ((u_int)fd >= fdp->fd_nfiles ||
1352 (fp = fdp->fd_ofiles[fd]) == NULL ||
1353 (fdp->fd_ofileflags[fd] & UF_RESERVED))
1354 return (EBADF);
1355 if (fp->f_type != DTYPE_VNODE)
1356 return (EOPNOTSUPP);
1357 vp = (struct vnode *)fp->f_data;
1358 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
1359 lf.l_whence = SEEK_SET;
1360 lf.l_start = 0;
1361 lf.l_len = 0;
1362 if (how & LOCK_UN) {
1363 lf.l_type = F_UNLCK;
1364 fp->f_flag &= ~FHASLOCK;
1365 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1366 }
1367 if (how & LOCK_EX)
1368 lf.l_type = F_WRLCK;
1369 else if (how & LOCK_SH)
1370 lf.l_type = F_RDLCK;
1371 else
1372 return (EBADF);
1373 fp->f_flag |= FHASLOCK;
1374 if (how & LOCK_NB)
1375 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1376 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1377}
1378
1379/*
1380 * File Descriptor pseudo-device driver (/dev/fd/).
1381 *
1382 * Opening minor device N dup()s the file (if any) connected to file
1383 * descriptor N belonging to the calling process. Note that this driver
1384 * consists of only the ``open()'' routine, because all subsequent
1385 * references to this file will be direct to the other driver.
1386 */
1387/* ARGSUSED */
1388int
1389fdopen(dev, mode, type, p)
1390 dev_t dev;
1391 int mode, type;
1392 struct proc *p;
1393{
1394
1395 /*
1396 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1397 * the file descriptor being sought for duplication. The error
1398 * return ensures that the vnode for this device will be released
1399 * by vn_open. Open will detect this special error and take the
1400 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1401 * will simply report the error.
1402 */
1403 p->p_dupfd = minor(dev);
1404 return (ENODEV);
1405}
1406
1407/*
1408 * Duplicate the specified descriptor to a free descriptor.
1409 */
1410int
1411dupfdopen(fdp, indx, dfd, mode, error)
1412 register struct filedesc *fdp;
1413 register int indx, dfd;
1414 int mode;
1415 int error;
1416{
1417 register struct file *wfp;
1418 struct file *fp;
1419
1420 /*
1421 * If the to-be-dup'd fd number is greater than the allowed number
1422 * of file descriptors, or the fd to be dup'd has already been
1423 * closed, reject. Note, check for new == old is necessary as
1424 * falloc could allocate an already closed to-be-dup'd descriptor
1425 * as the new descriptor.
1426 */
1427 fp = fdp->fd_ofiles[indx];
1428 if ((u_int)dfd >= fdp->fd_nfiles ||
1429 (wfp = fdp->fd_ofiles[dfd]) == NULL || wfp == fp ||
1430 (fdp->fd_ofileflags[dfd] & UF_RESERVED))
1431 return (EBADF);
1432
1433 /*
1434 * There are two cases of interest here.
1435 *
1436 * For ENODEV simply dup (dfd) to file descriptor
1437 * (indx) and return.
1438 *
1439 * For ENXIO steal away the file structure from (dfd) and
1440 * store it in (indx). (dfd) is effectively closed by
1441 * this operation.
1442 *
1443 * Any other error code is just returned.
1444 */
1445 switch (error) {
1446 case ENODEV:
1447 /*
1448 * Check that the mode the file is being opened for is a
1449 * subset of the mode of the existing descriptor.
1450 */
1451 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1452 return (EACCES);
1453 (void)fref(wfp);
1454 if (indx > fdp->fd_lastfile)
1455 fdp->fd_lastfile = indx;;
1456 fdp->fd_ofiles[indx] = wfp;
1457 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1458 return (0);
1459
1460 case ENXIO:
1461 /*
1462 * Steal away the file pointer from dfd, and stuff it into indx.
1463 */
1464 if (indx > fdp->fd_lastfile)
1465 fdp->fd_lastfile = indx;;
1466 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1467 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1468 _fdrelse(fdp, dfd);
1469 return (0);
1470
1471 default:
1472 return (error);
1473 }
1474 /* NOTREACHED */
1475}
1476
1477/* Reference manipulation routines for the file structure */
1478
1479int
1480fref(struct file *fp)
1481{
1482 if (fp->f_count == (short)0xffff)
1483 return (-1);
1484 if (++fp->f_count <= 0)
1485 panic("fref: f_count");
1486 return ((int)fp->f_count);
1487}
1488
1489static int
1490frele_internal(struct file *fp)
1491{
1492 if (fp->f_count == (short)0xffff)
1493 panic("frele: stale");
1494 if (--fp->f_count < 0)
1495 panic("frele: count < 0");
1496 return ((int)fp->f_count);
1497}
1498
1499
1500int
1501frele(struct file *fp)
1502{
1503 int count;
1504 funnel_t * fnl;
1505 extern int disable_funnel;
1506
1507 fnl = thread_funnel_get();
1508 /*
1509 * If the funnels are merged then atleast a funnel should be held
1510 * else frele should come in with kernel funnel only
1511 */
1512 if (!disable_funnel && (fnl != kernel_flock)) {
1513 panic("frele: kernel funnel not held");
1514
1515 } else if (fnl == THR_FUNNEL_NULL) {
1516 panic("frele: no funnel held");
1517 }
1518
1519 if ((count = frele_internal(fp)) == 0) {
1520 /* some one closed the fd while we were blocked */
1521 (void)closef_finish(fp, current_proc());
1522 }
1523 return(count);
1524}
1525
1526int
1527fcount(struct file *fp)
1528{
1529 if (fp->f_count == (short)0xffff)
1530 panic("fcount: stale");
1531 return ((int)fp->f_count);
1532}
1533