2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
39 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.17 2003/06/06 20:21:32 tegge Exp $
40 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.6 2003/07/19 21:14:38 dillon Exp $
43 #include "opt_compat.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/sysproto.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
54 #include <sys/namei.h>
57 #include <sys/filio.h>
58 #include <sys/fcntl.h>
59 #include <sys/unistd.h>
60 #include <sys/resourcevar.h>
61 #include <sys/event.h>
64 #include <vm/vm_extern.h>
66 #include <sys/file2.h>
68 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
69 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
70 "file desc to leader structures");
71 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
72 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
74 static d_open_t fdopen;
78 static struct cdevsw fildesc_cdevsw = {
86 /* strategy */ nostrategy,
95 static int do_dup __P((struct filedesc *fdp, int old, int new, register_t *retval, struct proc *p));
96 static int badfo_readwrite __P((struct file *fp, struct uio *uio,
97 struct ucred *cred, int flags, struct thread *td));
98 static int badfo_ioctl __P((struct file *fp, u_long com, caddr_t data,
100 static int badfo_poll __P((struct file *fp, int events,
101 struct ucred *cred, struct thread *td));
102 static int badfo_kqfilter __P((struct file *fp, struct knote *kn));
103 static int badfo_stat __P((struct file *fp, struct stat *sb, struct thread *td));
104 static int badfo_close __P((struct file *fp, struct thread *td));
107 * Descriptor management.
109 struct filelist filehead; /* head of list of open files */
110 int nfiles; /* actual number of open files */
114 * System calls on descriptors.
116 #ifndef _SYS_SYSPROTO_H_
117 struct getdtablesize_args {
123 getdtablesize(struct getdtablesize_args *uap)
125 struct proc *p = curproc;
128 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
133 * Duplicate a file descriptor to a particular value.
135 * note: keep in mind that a potential race condition exists when closing
136 * descriptors from a shared descriptor table (via rfork).
138 #ifndef _SYS_SYSPROTO_H_
146 dup2(struct dup2_args *uap)
148 struct proc *p = curproc;
149 struct filedesc *fdp = p->p_fd;
150 u_int old = uap->from, new = uap->to;
154 if (old >= fdp->fd_nfiles ||
155 fdp->fd_ofiles[old] == NULL ||
156 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
157 new >= maxfilesperproc) {
161 p->p_retval[0] = new;
164 if (new >= fdp->fd_nfiles) {
165 if ((error = fdalloc(p, new, &i)))
168 * fdalloc() may block, retest everything.
172 return (do_dup(fdp, (int)old, (int)new, p->p_retval, p));
176 * Duplicate a file descriptor.
178 #ifndef _SYS_SYSPROTO_H_
185 dup(struct dup_args *uap)
187 struct proc *p = curproc;
188 struct filedesc *fdp;
194 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
196 if ((error = fdalloc(p, 0, &new)))
198 return (do_dup(fdp, (int)old, new, p->p_retval, p));
202 * The file control system call.
204 #ifndef _SYS_SYSPROTO_H_
213 fcntl(struct fcntl_args *uap)
215 struct thread *td = curthread;
216 struct proc *p = td->td_proc;
217 struct filedesc *fdp = p->p_fd;
221 int i, tmp, error, flg = F_POSIX;
227 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
228 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
230 pop = &fdp->fd_ofileflags[uap->fd];
235 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
236 newmin >= maxfilesperproc)
238 if ((error = fdalloc(p, newmin, &i)))
240 return (do_dup(fdp, uap->fd, i, p->p_retval, p));
243 p->p_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
247 *pop = (*pop &~ UF_EXCLOSE) |
248 (uap->arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
252 p->p_retval[0] = OFLAGS(fp->f_flag);
257 fp->f_flag &= ~FCNTLFLAGS;
258 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
259 tmp = fp->f_flag & FNONBLOCK;
260 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
265 tmp = fp->f_flag & FASYNC;
266 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
271 fp->f_flag &= ~FNONBLOCK;
273 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
279 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, td);
285 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, td);
291 /* Fall into F_SETLK */
294 if (fp->f_type != DTYPE_VNODE)
296 vp = (struct vnode *)fp->f_data;
299 * copyin/lockop may block
302 /* Copy in the lock structure */
303 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
309 if (fl.l_whence == SEEK_CUR)
310 fl.l_start += fp->f_offset;
314 if ((fp->f_flag & FREAD) == 0) {
318 p->p_leader->p_flag |= P_ADVLOCK;
319 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
323 if ((fp->f_flag & FWRITE) == 0) {
327 p->p_leader->p_flag |= P_ADVLOCK;
328 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
332 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
339 /* Check for race with close */
340 if ((unsigned) uap->fd >= fdp->fd_nfiles ||
341 fp != fdp->fd_ofiles[uap->fd]) {
342 fl.l_whence = SEEK_SET;
346 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
347 F_UNLCK, &fl, F_POSIX);
353 if (fp->f_type != DTYPE_VNODE)
355 vp = (struct vnode *)fp->f_data;
357 * copyin/lockop may block
360 /* Copy in the lock structure */
361 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
367 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
368 fl.l_type != F_UNLCK) {
372 if (fl.l_whence == SEEK_CUR)
373 fl.l_start += fp->f_offset;
374 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
378 error = copyout((caddr_t)&fl,
379 (caddr_t)(intptr_t)uap->arg, sizeof(fl));
389 * Common code for dup, dup2, and fcntl(F_DUPFD).
392 do_dup(fdp, old, new, retval, p)
393 struct filedesc *fdp;
398 struct thread *td = p->p_thread;
404 * Save info on the descriptor being overwritten. We have
405 * to do the unmap now, but we cannot close it without
406 * introducing an ownership race for the slot.
408 delfp = fdp->fd_ofiles[new];
409 if (delfp != NULL && p->p_fdtol != NULL) {
411 * Ask fdfree() to sleep to ensure that all relevant
412 * process leaders can be traversed in closef().
414 fdp->fd_holdleaderscount++;
419 if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
420 (void) munmapfd(p, new);
424 * Duplicate the source descriptor, update lastfile
426 fp = fdp->fd_ofiles[old];
427 fdp->fd_ofiles[new] = fp;
428 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
430 if (new > fdp->fd_lastfile)
431 fdp->fd_lastfile = new;
435 * If we dup'd over a valid file, we now own the reference to it
436 * and must dispose of it using closef() semantics (as if a
437 * close() were performed on it).
440 (void) closef(delfp, td);
442 fdp->fd_holdleaderscount--;
443 if (fdp->fd_holdleaderscount == 0 &&
444 fdp->fd_holdleaderswakeup != 0) {
445 fdp->fd_holdleaderswakeup = 0;
446 wakeup(&fdp->fd_holdleaderscount);
454 * If sigio is on the list associated with a process or process group,
455 * disable signalling from the device, remove sigio from the list and
467 *(sigio->sio_myref) = NULL;
469 if (sigio->sio_pgid < 0) {
470 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
472 } else /* if ((*sigiop)->sio_pgid > 0) */ {
473 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
476 crfree(sigio->sio_ucred);
477 FREE(sigio, M_SIGIO);
480 /* Free a list of sigio structures. */
482 funsetownlst(sigiolst)
483 struct sigiolst *sigiolst;
487 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
492 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
494 * After permission checking, add a sigio structure to the sigio list for
495 * the process or process group.
498 fsetown(pgid, sigiop)
500 struct sigio **sigiop;
517 * Policy - Don't allow a process to FSETOWN a process
518 * in another session.
520 * Remove this test to allow maximum flexibility or
521 * restrict FSETOWN to the current process or process
522 * group for maximum safety.
524 if (proc->p_session != curproc->p_session)
528 } else /* if (pgid < 0) */ {
529 pgrp = pgfind(-pgid);
534 * Policy - Don't allow a process to FSETOWN a process
535 * in another session.
537 * Remove this test to allow maximum flexibility or
538 * restrict FSETOWN to the current process or process
539 * group for maximum safety.
541 if (pgrp->pg_session != curproc->p_session)
547 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
549 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
550 sigio->sio_proc = proc;
552 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
553 sigio->sio_pgrp = pgrp;
555 sigio->sio_pgid = pgid;
556 sigio->sio_ucred = crhold(curproc->p_ucred);
557 /* It would be convenient if p_ruid was in ucred. */
558 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
559 sigio->sio_myref = sigiop;
567 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
573 return (sigio != NULL ? sigio->sio_pgid : 0);
577 * Close a file descriptor.
579 #ifndef _SYS_SYSPROTO_H_
586 close(struct close_args *uap)
588 struct thread *td = curthread;
589 struct proc *p = td->td_proc;
590 struct filedesc *fdp;
599 if ((unsigned)fd >= fdp->fd_nfiles ||
600 (fp = fdp->fd_ofiles[fd]) == NULL)
603 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
604 (void) munmapfd(p, fd);
606 fdp->fd_ofiles[fd] = NULL;
607 fdp->fd_ofileflags[fd] = 0;
609 if (p->p_fdtol != NULL) {
611 * Ask fdfree() to sleep to ensure that all relevant
612 * process leaders can be traversed in closef().
614 fdp->fd_holdleaderscount++;
619 * we now hold the fp reference that used to be owned by the descriptor
622 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
624 if (fd < fdp->fd_freefile)
625 fdp->fd_freefile = fd;
626 if (fd < fdp->fd_knlistsize)
627 knote_fdclose(p, fd);
628 error = closef(fp, td);
630 fdp->fd_holdleaderscount--;
631 if (fdp->fd_holdleaderscount == 0 &&
632 fdp->fd_holdleaderswakeup != 0) {
633 fdp->fd_holdleaderswakeup = 0;
634 wakeup(&fdp->fd_holdleaderscount);
640 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
642 * Return status information about a file descriptor.
644 #ifndef _SYS_SYSPROTO_H_
652 ofstat(struct ofstat_args *uap)
654 struct thread *td = curthread;
655 struct proc *p = td->td_proc;
656 struct filedesc *fdp;
664 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
665 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
668 error = fo_stat(fp, &ub, td);
671 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
676 #endif /* COMPAT_43 || COMPAT_SUNOS */
679 * Return status information about a file descriptor.
681 #ifndef _SYS_SYSPROTO_H_
689 fstat(struct fstat_args *uap)
691 struct thread *td = curthread;
692 struct proc *p = td->td_proc;
693 struct filedesc *fdp = p->p_fd;
700 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
701 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
704 error = fo_stat(fp, &ub, td);
706 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
712 * Return status information about a file descriptor.
714 #ifndef _SYS_SYSPROTO_H_
722 nfstat(struct nfstat_args *uap)
724 struct thread *td = curthread;
725 struct proc *p = td->td_proc;
726 struct filedesc *fdp;
734 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
735 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
738 error = fo_stat(fp, &ub, td);
741 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
748 * Return pathconf information about a file descriptor.
750 #ifndef _SYS_SYSPROTO_H_
751 struct fpathconf_args {
758 fpathconf(struct fpathconf_args *uap)
760 struct thread *td = curthread;
761 struct proc *p = td->td_proc;
762 struct filedesc *fdp;
769 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
770 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
775 switch (fp->f_type) {
778 if (uap->name != _PC_PIPE_BUF) {
781 p->p_retval[0] = PIPE_BUF;
787 vp = (struct vnode *)fp->f_data;
788 error = VOP_PATHCONF(vp, uap->name, p->p_retval);
799 * Allocate a file descriptor for the process.
802 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
805 fdalloc(p, want, result)
810 struct filedesc *fdp = p->p_fd;
812 int lim, last, nfiles;
813 struct file **newofile;
817 * Search for a free descriptor starting at the higher
818 * of want or fd_freefile. If that fails, consider
819 * expanding the ofile array.
821 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
823 last = min(fdp->fd_nfiles, lim);
824 if ((i = want) < fdp->fd_freefile)
825 i = fdp->fd_freefile;
826 for (; i < last; i++) {
827 if (fdp->fd_ofiles[i] == NULL) {
828 fdp->fd_ofileflags[i] = 0;
829 if (i > fdp->fd_lastfile)
830 fdp->fd_lastfile = i;
831 if (want <= fdp->fd_freefile)
832 fdp->fd_freefile = i;
839 * No space in current array. Expand?
841 if (fdp->fd_nfiles >= lim)
843 if (fdp->fd_nfiles < NDEXTENT)
846 nfiles = 2 * fdp->fd_nfiles;
847 MALLOC(newofile, struct file **, nfiles * OFILESIZE,
848 M_FILEDESC, M_WAITOK);
851 * deal with file-table extend race that might have occured
852 * when malloc was blocked.
854 if (fdp->fd_nfiles >= nfiles) {
855 FREE(newofile, M_FILEDESC);
858 newofileflags = (char *) &newofile[nfiles];
860 * Copy the existing ofile and ofileflags arrays
861 * and zero the new portion of each array.
863 bcopy(fdp->fd_ofiles, newofile,
864 (i = sizeof(struct file *) * fdp->fd_nfiles));
865 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
866 bcopy(fdp->fd_ofileflags, newofileflags,
867 (i = sizeof(char) * fdp->fd_nfiles));
868 bzero(newofileflags + i, nfiles * sizeof(char) - i);
869 if (fdp->fd_nfiles > NDFILE)
870 FREE(fdp->fd_ofiles, M_FILEDESC);
871 fdp->fd_ofiles = newofile;
872 fdp->fd_ofileflags = newofileflags;
873 fdp->fd_nfiles = nfiles;
880 * Check to see whether n user file descriptors
881 * are available to the process p.
888 struct filedesc *fdp = p->p_fd;
892 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
893 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
896 last = min(fdp->fd_nfiles, lim);
897 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
898 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
899 if (*fpp == NULL && --n <= 0)
906 * Create a new open file structure and allocate
907 * a file decriptor for the process that refers to it.
910 falloc(p, resultfp, resultfd)
912 struct file **resultfp;
915 struct file *fp, *fq;
918 if (nfiles >= maxfiles) {
923 * Allocate a new file descriptor.
924 * If the process has file descriptor zero open, add to the list
925 * of open files at that point, otherwise put it at the front of
926 * the list of open files.
929 MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
930 bzero(fp, sizeof(struct file));
933 * wait until after malloc (which may have blocked) returns before
934 * allocating the slot, else a race might have shrunk it if we had
935 * allocated it before the malloc.
937 if ((error = fdalloc(p, 0, &i))) {
943 fp->f_cred = crhold(p->p_ucred);
944 fp->f_ops = &badfileops;
946 if ((fq = p->p_fd->fd_ofiles[0])) {
947 LIST_INSERT_AFTER(fq, fp, f_list);
949 LIST_INSERT_HEAD(&filehead, fp, f_list);
951 p->p_fd->fd_ofiles[i] = fp;
960 * Free a file descriptor.
966 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
967 LIST_REMOVE(fp, f_list);
974 * Build a new filedesc structure.
980 struct filedesc0 *newfdp;
981 struct filedesc *fdp = p->p_fd;
983 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
984 M_FILEDESC, M_WAITOK);
985 bzero(newfdp, sizeof(struct filedesc0));
986 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
987 if (newfdp->fd_fd.fd_cdir)
988 VREF(newfdp->fd_fd.fd_cdir);
989 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
990 if (newfdp->fd_fd.fd_rdir)
991 VREF(newfdp->fd_fd.fd_rdir);
992 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
993 if (newfdp->fd_fd.fd_jdir)
994 VREF(newfdp->fd_fd.fd_jdir);
996 /* Create the file descriptor table. */
997 newfdp->fd_fd.fd_refcnt = 1;
998 newfdp->fd_fd.fd_cmask = cmask;
999 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1000 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1001 newfdp->fd_fd.fd_nfiles = NDFILE;
1002 newfdp->fd_fd.fd_knlistsize = -1;
1004 return (&newfdp->fd_fd);
1008 * Share a filedesc structure.
1014 p->p_fd->fd_refcnt++;
1019 * Copy a filedesc structure.
1025 struct filedesc *newfdp, *fdp = p->p_fd;
1029 /* Certain daemons might not have file descriptors. */
1033 MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
1034 M_FILEDESC, M_WAITOK);
1035 bcopy(fdp, newfdp, sizeof(struct filedesc));
1036 if (newfdp->fd_cdir)
1037 VREF(newfdp->fd_cdir);
1038 if (newfdp->fd_rdir)
1039 VREF(newfdp->fd_rdir);
1040 if (newfdp->fd_jdir)
1041 VREF(newfdp->fd_jdir);
1042 newfdp->fd_refcnt = 1;
1045 * If the number of open files fits in the internal arrays
1046 * of the open file structure, use them, otherwise allocate
1047 * additional memory for the number of descriptors currently
1050 if (newfdp->fd_lastfile < NDFILE) {
1051 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1052 newfdp->fd_ofileflags =
1053 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1057 * Compute the smallest multiple of NDEXTENT needed
1058 * for the file descriptors currently in use,
1059 * allowing the table to shrink.
1061 i = newfdp->fd_nfiles;
1062 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1064 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
1065 M_FILEDESC, M_WAITOK);
1066 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1068 newfdp->fd_nfiles = i;
1069 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1070 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1073 * kq descriptors cannot be copied.
1075 if (newfdp->fd_knlistsize != -1) {
1076 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1077 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1078 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1080 if (i < newfdp->fd_freefile)
1081 newfdp->fd_freefile = i;
1083 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1084 newfdp->fd_lastfile--;
1086 newfdp->fd_knlist = NULL;
1087 newfdp->fd_knlistsize = -1;
1088 newfdp->fd_knhash = NULL;
1089 newfdp->fd_knhashmask = 0;
1092 fpp = newfdp->fd_ofiles;
1093 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1101 * Release a filedesc structure.
1104 fdfree(struct proc *p)
1106 struct thread *td = p->p_thread;
1107 struct filedesc *fdp = p->p_fd;
1110 struct filedesc_to_leader *fdtol;
1115 /* Certain daemons might not have file descriptors. */
1119 /* Check for special need to clear POSIX style locks */
1121 if (fdtol != NULL) {
1122 KASSERT(fdtol->fdl_refcount > 0,
1123 ("filedesc_to_refcount botch: fdl_refcount=%d",
1124 fdtol->fdl_refcount));
1125 if (fdtol->fdl_refcount == 1 &&
1126 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1128 fpp = fdp->fd_ofiles;
1129 for (i = 0, fpp = fdp->fd_ofiles;
1130 i < fdp->fd_lastfile;
1133 (*fpp)->f_type != DTYPE_VNODE)
1137 lf.l_whence = SEEK_SET;
1140 lf.l_type = F_UNLCK;
1141 vp = (struct vnode *)fp->f_data;
1142 (void) VOP_ADVLOCK(vp,
1143 (caddr_t)p->p_leader,
1147 fdrop(fp, p->p_thread);
1148 fpp = fdp->fd_ofiles + i;
1152 if (fdtol->fdl_refcount == 1) {
1153 if (fdp->fd_holdleaderscount > 0 &&
1154 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1156 * close() or do_dup() has cleared a reference
1157 * in a shared file descriptor table.
1159 fdp->fd_holdleaderswakeup = 1;
1160 tsleep(&fdp->fd_holdleaderscount,
1164 if (fdtol->fdl_holdcount > 0) {
1166 * Ensure that fdtol->fdl_leader
1167 * remains valid in closef().
1169 fdtol->fdl_wakeup = 1;
1170 tsleep(fdtol, 0, "fdlhold", 0);
1174 fdtol->fdl_refcount--;
1175 if (fdtol->fdl_refcount == 0 &&
1176 fdtol->fdl_holdcount == 0) {
1177 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1178 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1183 FREE(fdtol, M_FILEDESC_TO_LEADER);
1185 if (--fdp->fd_refcnt > 0)
1188 * we are the last reference to the structure, we can
1189 * safely assume it will not change out from under us.
1191 fpp = fdp->fd_ofiles;
1192 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1194 (void) closef(*fpp, td);
1196 if (fdp->fd_nfiles > NDFILE)
1197 FREE(fdp->fd_ofiles, M_FILEDESC);
1199 vrele(fdp->fd_cdir);
1201 vrele(fdp->fd_rdir);
1203 vrele(fdp->fd_jdir);
1205 FREE(fdp->fd_knlist, M_KQUEUE);
1207 FREE(fdp->fd_knhash, M_KQUEUE);
1208 FREE(fdp, M_FILEDESC);
1212 * For setugid programs, we don't want to people to use that setugidness
1213 * to generate error messages which write to a file which otherwise would
1214 * otherwise be off-limits to the process.
1216 * This is a gross hack to plug the hole. A better solution would involve
1217 * a special vop or other form of generalized access control mechanism. We
1218 * go ahead and just reject all procfs file systems accesses as dangerous.
1220 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1221 * sufficient. We also don't for check setugidness since we know we are.
1224 is_unsafe(struct file *fp)
1226 if (fp->f_type == DTYPE_VNODE &&
1227 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1233 * Make this setguid thing safe, if at all possible.
1236 setugidsafety(struct proc *p)
1238 struct thread *td = p->p_thread;
1239 struct filedesc *fdp = p->p_fd;
1242 /* Certain daemons might not have file descriptors. */
1247 * note: fdp->fd_ofiles may be reallocated out from under us while
1248 * we are blocked in a close. Be careful!
1250 for (i = 0; i <= fdp->fd_lastfile; i++) {
1253 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1257 if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1258 (void) munmapfd(p, i);
1260 if (i < fdp->fd_knlistsize)
1261 knote_fdclose(p, i);
1263 * NULL-out descriptor prior to close to avoid
1264 * a race while close blocks.
1266 fp = fdp->fd_ofiles[i];
1267 fdp->fd_ofiles[i] = NULL;
1268 fdp->fd_ofileflags[i] = 0;
1269 if (i < fdp->fd_freefile)
1270 fdp->fd_freefile = i;
1271 (void) closef(fp, td);
1274 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1279 * Close any files on exec?
1282 fdcloseexec(struct proc *p)
1284 struct thread *td = p->p_thread;
1285 struct filedesc *fdp = p->p_fd;
1288 /* Certain daemons might not have file descriptors. */
1293 * We cannot cache fd_ofiles or fd_ofileflags since operations
1294 * may block and rip them out from under us.
1296 for (i = 0; i <= fdp->fd_lastfile; i++) {
1297 if (fdp->fd_ofiles[i] != NULL &&
1298 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1302 if (fdp->fd_ofileflags[i] & UF_MAPPED)
1303 (void) munmapfd(p, i);
1305 if (i < fdp->fd_knlistsize)
1306 knote_fdclose(p, i);
1308 * NULL-out descriptor prior to close to avoid
1309 * a race while close blocks.
1311 fp = fdp->fd_ofiles[i];
1312 fdp->fd_ofiles[i] = NULL;
1313 fdp->fd_ofileflags[i] = 0;
1314 if (i < fdp->fd_freefile)
1315 fdp->fd_freefile = i;
1316 (void) closef(fp, td);
1319 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1324 * It is unsafe for set[ug]id processes to be started with file
1325 * descriptors 0..2 closed, as these descriptors are given implicit
1326 * significance in the Standard C library. fdcheckstd() will create a
1327 * descriptor referencing /dev/null for each of stdin, stdout, and
1328 * stderr that is not already open.
1331 fdcheckstd(struct proc *p)
1333 struct thread *td = p->p_thread;
1334 struct nameidata nd;
1335 struct filedesc *fdp;
1338 int fd, i, error, flags, devnull;
1345 for (i = 0; i < 3; i++) {
1346 if (fdp->fd_ofiles[i] != NULL)
1349 error = falloc(p, &fp, &fd);
1352 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
1354 flags = FREAD | FWRITE;
1355 error = vn_open(&nd, flags, 0);
1357 fdp->fd_ofiles[i] = NULL;
1361 NDFREE(&nd, NDF_ONLY_PNBUF);
1362 fp->f_data = (caddr_t)nd.ni_vp;
1365 fp->f_type = DTYPE_VNODE;
1366 VOP_UNLOCK(nd.ni_vp, 0, td);
1369 error = fdalloc(p, 0, &fd);
1372 error = do_dup(fdp, devnull, fd, &retval, p);
1381 * Internal form of close.
1382 * Decrement reference count on file structure.
1383 * Note: td and/or p may be NULL when closing a file
1384 * that was being passed in a message.
1387 closef(struct file *fp, struct thread *td)
1391 struct filedesc_to_leader *fdtol;
1398 p = NULL; /* allow no proc association */
1400 p = td->td_proc; /* can also be NULL */
1403 * POSIX record locking dictates that any close releases ALL
1404 * locks owned by this process. This is handled by setting
1405 * a flag in the unlock to free ONLY locks obeying POSIX
1406 * semantics, and not to free BSD-style file locks.
1407 * If the descriptor was in a message, POSIX-style locks
1408 * aren't passed with the descriptor.
1411 fp->f_type == DTYPE_VNODE) {
1412 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
1413 lf.l_whence = SEEK_SET;
1416 lf.l_type = F_UNLCK;
1417 vp = (struct vnode *)fp->f_data;
1418 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
1422 if (fdtol != NULL) {
1424 * Handle special case where file descriptor table
1425 * is shared between multiple process leaders.
1427 for (fdtol = fdtol->fdl_next;
1428 fdtol != p->p_fdtol;
1429 fdtol = fdtol->fdl_next) {
1430 if ((fdtol->fdl_leader->p_flag &
1433 fdtol->fdl_holdcount++;
1434 lf.l_whence = SEEK_SET;
1437 lf.l_type = F_UNLCK;
1438 vp = (struct vnode *)fp->f_data;
1439 (void) VOP_ADVLOCK(vp,
1440 (caddr_t)p->p_leader,
1441 F_UNLCK, &lf, F_POSIX);
1442 fdtol->fdl_holdcount--;
1443 if (fdtol->fdl_holdcount == 0 &&
1444 fdtol->fdl_wakeup != 0) {
1445 fdtol->fdl_wakeup = 0;
1451 return (fdrop(fp, td));
1455 fdrop(struct file *fp, struct thread *td)
1461 if (--fp->f_count > 0)
1463 if (fp->f_count < 0)
1464 panic("fdrop: count < 0");
1465 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1466 lf.l_whence = SEEK_SET;
1469 lf.l_type = F_UNLCK;
1470 vp = (struct vnode *)fp->f_data;
1471 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1473 if (fp->f_ops != &badfileops)
1474 error = fo_close(fp, td);
1482 * Apply an advisory lock on a file descriptor.
1484 * Just attempt to get a record lock of the requested type on
1485 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1487 #ifndef _SYS_SYSPROTO_H_
1495 flock(struct flock_args *uap)
1497 struct proc *p = curproc;
1498 struct filedesc *fdp = p->p_fd;
1503 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1504 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1506 if (fp->f_type != DTYPE_VNODE)
1507 return (EOPNOTSUPP);
1508 vp = (struct vnode *)fp->f_data;
1509 lf.l_whence = SEEK_SET;
1512 if (uap->how & LOCK_UN) {
1513 lf.l_type = F_UNLCK;
1514 fp->f_flag &= ~FHASLOCK;
1515 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1517 if (uap->how & LOCK_EX)
1518 lf.l_type = F_WRLCK;
1519 else if (uap->how & LOCK_SH)
1520 lf.l_type = F_RDLCK;
1523 fp->f_flag |= FHASLOCK;
1524 if (uap->how & LOCK_NB)
1525 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1526 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1530 * File Descriptor pseudo-device driver (/dev/fd/).
1532 * Opening minor device N dup()s the file (if any) connected to file
1533 * descriptor N belonging to the calling process. Note that this driver
1534 * consists of only the ``open()'' routine, because all subsequent
1535 * references to this file will be direct to the other driver.
1539 fdopen(dev_t dev, int mode, int type, struct thread *td)
1541 KKASSERT(td->td_proc != NULL);
1544 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1545 * the file descriptor being sought for duplication. The error
1546 * return ensures that the vnode for this device will be released
1547 * by vn_open. Open will detect this special error and take the
1548 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1549 * will simply report the error.
1551 td->td_proc->p_dupfd = minor(dev);
1556 * Duplicate the specified descriptor to a free descriptor.
1559 dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, int error)
1565 * If the to-be-dup'd fd number is greater than the allowed number
1566 * of file descriptors, or the fd to be dup'd has already been
1567 * closed, then reject.
1569 if ((u_int)dfd >= fdp->fd_nfiles ||
1570 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1575 * There are two cases of interest here.
1577 * For ENODEV simply dup (dfd) to file descriptor
1578 * (indx) and return.
1580 * For ENXIO steal away the file structure from (dfd) and
1581 * store it in (indx). (dfd) is effectively closed by
1584 * Any other error code is just returned.
1589 * Check that the mode the file is being opened for is a
1590 * subset of the mode of the existing descriptor.
1592 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1594 fp = fdp->fd_ofiles[indx];
1596 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1597 (void) munmapfd(p, indx);
1599 fdp->fd_ofiles[indx] = wfp;
1600 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1602 if (indx > fdp->fd_lastfile)
1603 fdp->fd_lastfile = indx;
1605 * we now own the reference to fp that the ofiles[] array
1606 * used to own. Release it.
1609 fdrop(fp, curthread);
1614 * Steal away the file pointer from dfd, and stuff it into indx.
1616 fp = fdp->fd_ofiles[indx];
1618 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1619 (void) munmapfd(p, indx);
1621 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1622 fdp->fd_ofiles[dfd] = NULL;
1623 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1624 fdp->fd_ofileflags[dfd] = 0;
1627 * we now own the reference to fp that the ofiles[] array
1628 * used to own. Release it.
1631 fdrop(fp, curthread);
1633 * Complete the clean up of the filedesc structure by
1634 * recomputing the various hints.
1636 if (indx > fdp->fd_lastfile) {
1637 fdp->fd_lastfile = indx;
1639 while (fdp->fd_lastfile > 0 &&
1640 fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1643 if (dfd < fdp->fd_freefile)
1644 fdp->fd_freefile = dfd;
1655 struct filedesc_to_leader *
1656 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
1657 struct proc *leader)
1659 struct filedesc_to_leader *fdtol;
1661 MALLOC(fdtol, struct filedesc_to_leader *,
1662 sizeof(struct filedesc_to_leader),
1663 M_FILEDESC_TO_LEADER,
1665 fdtol->fdl_refcount = 1;
1666 fdtol->fdl_holdcount = 0;
1667 fdtol->fdl_wakeup = 0;
1668 fdtol->fdl_leader = leader;
1670 fdtol->fdl_next = old->fdl_next;
1671 fdtol->fdl_prev = old;
1672 old->fdl_next = fdtol;
1673 fdtol->fdl_next->fdl_prev = fdtol;
1675 fdtol->fdl_next = fdtol;
1676 fdtol->fdl_prev = fdtol;
1682 * Get file structures.
1685 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1692 * overestimate by 10 files
1694 return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1695 (nfiles + 10) * sizeof(struct file)));
1698 error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1703 * followed by an array of file structures
1705 LIST_FOREACH(fp, &filehead, f_list) {
1706 error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1713 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1714 0, 0, sysctl_kern_file, "S,file", "Entire file table");
1716 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1717 &maxfilesperproc, 0, "Maximum files allowed open per process");
1719 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1720 &maxfiles, 0, "Maximum number of files");
1722 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1723 &nfiles, 0, "System-wide number of open files");
1726 fildesc_drvinit(void *unused)
1730 for (fd = 0; fd < NUMFDESC; fd++)
1731 make_dev(&fildesc_cdevsw, fd,
1732 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
1733 make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
1734 make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
1735 make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
1738 struct fileops badfileops = {
1760 badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
1766 badfo_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1772 badfo_kqfilter(struct file *fp, struct knote *kn)
1778 badfo_stat(struct file *fp, struct stat *sb, struct thread *td)
1784 badfo_close(struct file *fp, struct thread *td)
1789 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1790 fildesc_drvinit,NULL)