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.19 2004/02/28 00:43:31 tegge Exp $
40 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.34 2004/11/24 22:51:01 joerg 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/nlookup.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>
62 #include <sys/kern_syscall.h>
63 #include <sys/kcore.h>
64 #include <sys/kinfo.h>
67 #include <vm/vm_extern.h>
69 #include <sys/file2.h>
71 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
72 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
73 "file desc to leader structures");
74 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
75 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
77 static d_open_t fdopen;
81 static struct cdevsw fildesc_cdevsw = {
95 /* strategy */ nostrategy,
100 static int badfo_readwrite (struct file *fp, struct uio *uio,
101 struct ucred *cred, int flags, struct thread *td);
102 static int badfo_ioctl (struct file *fp, u_long com, caddr_t data,
104 static int badfo_poll (struct file *fp, int events,
105 struct ucred *cred, struct thread *td);
106 static int badfo_kqfilter (struct file *fp, struct knote *kn);
107 static int badfo_stat (struct file *fp, struct stat *sb, struct thread *td);
108 static int badfo_close (struct file *fp, struct thread *td);
111 * Descriptor management.
113 struct filelist filehead; /* head of list of open files */
114 int nfiles; /* actual number of open files */
118 * System calls on descriptors.
122 getdtablesize(struct getdtablesize_args *uap)
124 struct proc *p = curproc;
127 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
132 * Duplicate a file descriptor to a particular value.
134 * note: keep in mind that a potential race condition exists when closing
135 * descriptors from a shared descriptor table (via rfork).
139 dup2(struct dup2_args *uap)
143 error = kern_dup(DUP_FIXED, uap->from, uap->to, uap->sysmsg_fds);
149 * Duplicate a file descriptor.
153 dup(struct dup_args *uap)
157 error = kern_dup(DUP_VARIABLE, uap->fd, 0, uap->sysmsg_fds);
163 kern_fcntl(int fd, int cmd, union fcntl_dat *dat)
165 struct thread *td = curthread;
166 struct proc *p = td->td_proc;
167 struct filedesc *fdp = p->p_fd;
172 int tmp, error, flg = F_POSIX;
176 if ((unsigned)fd >= fdp->fd_nfiles ||
177 (fp = fdp->fd_ofiles[fd]) == NULL)
179 pop = &fdp->fd_ofileflags[fd];
184 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
185 newmin > maxfilesperproc)
187 error = kern_dup(DUP_VARIABLE, fd, newmin, &dat->fc_fd);
191 dat->fc_cloexec = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
195 *pop = (*pop &~ UF_EXCLOSE) |
196 (dat->fc_cloexec & FD_CLOEXEC ? UF_EXCLOSE : 0);
200 dat->fc_flags = OFLAGS(fp->f_flag);
205 fp->f_flag &= ~FCNTLFLAGS;
206 fp->f_flag |= FFLAGS(dat->fc_flags & ~O_ACCMODE) & FCNTLFLAGS;
207 tmp = fp->f_flag & FNONBLOCK;
208 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
213 tmp = fp->f_flag & FASYNC;
214 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
219 fp->f_flag &= ~FNONBLOCK;
221 fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
227 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)&dat->fc_owner, td);
233 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&dat->fc_owner, td);
239 /* Fall into F_SETLK */
242 if (fp->f_type != DTYPE_VNODE)
244 vp = (struct vnode *)fp->f_data;
247 * copyin/lockop may block
250 if (dat->fc_flock.l_whence == SEEK_CUR)
251 dat->fc_flock.l_start += fp->f_offset;
253 switch (dat->fc_flock.l_type) {
255 if ((fp->f_flag & FREAD) == 0) {
259 p->p_leader->p_flag |= P_ADVLOCK;
260 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
261 &dat->fc_flock, flg);
264 if ((fp->f_flag & FWRITE) == 0) {
268 p->p_leader->p_flag |= P_ADVLOCK;
269 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
270 &dat->fc_flock, flg);
273 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
274 &dat->fc_flock, F_POSIX);
280 /* Check for race with close */
281 if ((unsigned) fd >= fdp->fd_nfiles ||
282 fp != fdp->fd_ofiles[fd]) {
283 dat->fc_flock.l_whence = SEEK_SET;
284 dat->fc_flock.l_start = 0;
285 dat->fc_flock.l_len = 0;
286 dat->fc_flock.l_type = F_UNLCK;
287 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
288 F_UNLCK, &dat->fc_flock, F_POSIX);
294 if (fp->f_type != DTYPE_VNODE)
296 vp = (struct vnode *)fp->f_data;
298 * copyin/lockop may block
301 if (dat->fc_flock.l_type != F_RDLCK &&
302 dat->fc_flock.l_type != F_WRLCK &&
303 dat->fc_flock.l_type != F_UNLCK) {
307 if (dat->fc_flock.l_whence == SEEK_CUR)
308 dat->fc_flock.l_start += fp->f_offset;
309 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
310 &dat->fc_flock, F_POSIX);
320 * The file control system call.
323 fcntl(struct fcntl_args *uap)
330 dat.fc_fd = uap->arg;
333 dat.fc_cloexec = uap->arg;
336 dat.fc_flags = uap->arg;
339 dat.fc_owner = uap->arg;
344 error = copyin((caddr_t)uap->arg, &dat.fc_flock,
345 sizeof(struct flock));
351 error = kern_fcntl(uap->fd, uap->cmd, &dat);
356 uap->sysmsg_result = dat.fc_fd;
359 uap->sysmsg_result = dat.fc_cloexec;
362 uap->sysmsg_result = dat.fc_flags;
365 uap->sysmsg_result = dat.fc_owner;
367 error = copyout(&dat.fc_flock, (caddr_t)uap->arg,
368 sizeof(struct flock));
377 * Common code for dup, dup2, and fcntl(F_DUPFD).
379 * The type flag can be either DUP_FIXED or DUP_VARIABLE. DUP_FIXED tells
380 * kern_dup() to destructively dup over an existing file descriptor if new
381 * is already open. DUP_VARIABLE tells kern_dup() to find the lowest
382 * unused file descriptor that is greater than or equal to new.
385 kern_dup(enum dup_type type, int old, int new, int *res)
387 struct thread *td = curthread;
388 struct proc *p = td->td_proc;
389 struct filedesc *fdp = p->p_fd;
396 * Verify that we have a valid descriptor to dup from and
397 * possibly to dup to.
399 if (old < 0 || new < 0 || new > p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
400 new >= maxfilesperproc)
402 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
404 if (type == DUP_FIXED && old == new) {
408 fp = fdp->fd_ofiles[old];
412 * Expand the table for the new descriptor if needed. This may
413 * block and drop and reacquire the fidedesc lock.
415 if (type == DUP_VARIABLE || new >= fdp->fd_nfiles) {
416 error = fdalloc(p, new, &newfd);
422 if (type == DUP_VARIABLE)
426 * If the old file changed out from under us then treat it as a
427 * bad file descriptor. Userland should do its own locking to
430 if (fdp->fd_ofiles[old] != fp) {
431 if (fdp->fd_ofiles[new] == NULL) {
432 if (new < fdp->fd_freefile)
433 fdp->fd_freefile = new;
434 while (fdp->fd_lastfile > 0 &&
435 fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
441 KASSERT(old != new, ("new fd is same as old"));
444 * Save info on the descriptor being overwritten. We have
445 * to do the unmap now, but we cannot close it without
446 * introducing an ownership race for the slot.
448 delfp = fdp->fd_ofiles[new];
449 if (delfp != NULL && p->p_fdtol != NULL) {
451 * Ask fdfree() to sleep to ensure that all relevant
452 * process leaders can be traversed in closef().
454 fdp->fd_holdleaderscount++;
458 KASSERT(delfp == NULL || type == DUP_FIXED,
459 ("dup() picked an open file"));
461 if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
462 (void) munmapfd(p, new);
466 * Duplicate the source descriptor, update lastfile
468 fdp->fd_ofiles[new] = fp;
469 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
470 if (new > fdp->fd_lastfile)
471 fdp->fd_lastfile = new;
475 * If we dup'd over a valid file, we now own the reference to it
476 * and must dispose of it using closef() semantics (as if a
477 * close() were performed on it).
480 (void) closef(delfp, td);
482 fdp->fd_holdleaderscount--;
483 if (fdp->fd_holdleaderscount == 0 &&
484 fdp->fd_holdleaderswakeup != 0) {
485 fdp->fd_holdleaderswakeup = 0;
486 wakeup(&fdp->fd_holdleaderscount);
494 * If sigio is on the list associated with a process or process group,
495 * disable signalling from the device, remove sigio from the list and
499 funsetown(struct sigio *sigio)
506 *(sigio->sio_myref) = NULL;
508 if (sigio->sio_pgid < 0) {
509 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
511 } else /* if ((*sigiop)->sio_pgid > 0) */ {
512 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
515 crfree(sigio->sio_ucred);
516 free(sigio, M_SIGIO);
519 /* Free a list of sigio structures. */
521 funsetownlst(struct sigiolst *sigiolst)
525 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
530 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
532 * After permission checking, add a sigio structure to the sigio list for
533 * the process or process group.
536 fsetown(pid_t pgid, struct sigio **sigiop)
553 * Policy - Don't allow a process to FSETOWN a process
554 * in another session.
556 * Remove this test to allow maximum flexibility or
557 * restrict FSETOWN to the current process or process
558 * group for maximum safety.
560 if (proc->p_session != curproc->p_session)
564 } else /* if (pgid < 0) */ {
565 pgrp = pgfind(-pgid);
570 * Policy - Don't allow a process to FSETOWN a process
571 * in another session.
573 * Remove this test to allow maximum flexibility or
574 * restrict FSETOWN to the current process or process
575 * group for maximum safety.
577 if (pgrp->pg_session != curproc->p_session)
583 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
585 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
586 sigio->sio_proc = proc;
588 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
589 sigio->sio_pgrp = pgrp;
591 sigio->sio_pgid = pgid;
592 sigio->sio_ucred = crhold(curproc->p_ucred);
593 /* It would be convenient if p_ruid was in ucred. */
594 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
595 sigio->sio_myref = sigiop;
603 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
606 fgetown(struct sigio *sigio)
608 return (sigio != NULL ? sigio->sio_pgid : 0);
612 * Close a file descriptor.
616 close(struct close_args *uap)
618 return(kern_close(uap->fd));
624 struct thread *td = curthread;
625 struct proc *p = td->td_proc;
626 struct filedesc *fdp;
634 if ((unsigned)fd >= fdp->fd_nfiles ||
635 (fp = fdp->fd_ofiles[fd]) == NULL)
638 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
639 (void) munmapfd(p, fd);
641 fdp->fd_ofiles[fd] = NULL;
642 fdp->fd_ofileflags[fd] = 0;
644 if (p->p_fdtol != NULL) {
646 * Ask fdfree() to sleep to ensure that all relevant
647 * process leaders can be traversed in closef().
649 fdp->fd_holdleaderscount++;
654 * we now hold the fp reference that used to be owned by the descriptor
657 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
659 if (fd < fdp->fd_freefile)
660 fdp->fd_freefile = fd;
661 if (fd < fdp->fd_knlistsize)
662 knote_fdclose(p, fd);
663 error = closef(fp, td);
665 fdp->fd_holdleaderscount--;
666 if (fdp->fd_holdleaderscount == 0 &&
667 fdp->fd_holdleaderswakeup != 0) {
668 fdp->fd_holdleaderswakeup = 0;
669 wakeup(&fdp->fd_holdleaderscount);
676 kern_fstat(int fd, struct stat *ub)
678 struct thread *td = curthread;
679 struct proc *p = td->td_proc;
680 struct filedesc *fdp;
687 if ((unsigned)fd >= fdp->fd_nfiles ||
688 (fp = fdp->fd_ofiles[fd]) == NULL)
691 error = fo_stat(fp, ub, td);
698 * Return status information about a file descriptor.
701 fstat(struct fstat_args *uap)
706 error = kern_fstat(uap->fd, &st);
709 error = copyout(&st, uap->sb, sizeof(st));
714 * XXX: This is for source compatibility with NetBSD. Probably doesn't
718 nfstat(struct nfstat_args *uap)
724 error = kern_fstat(uap->fd, &st);
728 error = copyout(&nst, uap->sb, sizeof (nst));
734 * Return pathconf information about a file descriptor.
738 fpathconf(struct fpathconf_args *uap)
740 struct thread *td = curthread;
741 struct proc *p = td->td_proc;
742 struct filedesc *fdp;
749 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
750 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
755 switch (fp->f_type) {
758 if (uap->name != _PC_PIPE_BUF) {
761 uap->sysmsg_result = PIPE_BUF;
767 vp = (struct vnode *)fp->f_data;
768 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
779 * Allocate a file descriptor for the process.
782 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
785 fdalloc(struct proc *p, int want, int *result)
787 struct filedesc *fdp = p->p_fd;
789 int lim, last, nfiles;
790 struct file **newofile;
794 * Search for a free descriptor starting at the higher
795 * of want or fd_freefile. If that fails, consider
796 * expanding the ofile array.
798 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
800 last = min(fdp->fd_nfiles, lim);
801 if ((i = want) < fdp->fd_freefile)
802 i = fdp->fd_freefile;
803 for (; i < last; i++) {
804 if (fdp->fd_ofiles[i] == NULL) {
805 fdp->fd_ofileflags[i] = 0;
806 if (i > fdp->fd_lastfile)
807 fdp->fd_lastfile = i;
808 if (want <= fdp->fd_freefile)
809 fdp->fd_freefile = i;
816 * No space in current array. Expand?
818 if (fdp->fd_nfiles >= lim)
820 if (fdp->fd_nfiles < NDEXTENT)
823 nfiles = 2 * fdp->fd_nfiles;
824 newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
827 * deal with file-table extend race that might have occured
828 * when malloc was blocked.
830 if (fdp->fd_nfiles >= nfiles) {
831 free(newofile, M_FILEDESC);
834 newofileflags = (char *) &newofile[nfiles];
836 * Copy the existing ofile and ofileflags arrays
837 * and zero the new portion of each array.
839 bcopy(fdp->fd_ofiles, newofile,
840 (i = sizeof(struct file *) * fdp->fd_nfiles));
841 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
842 bcopy(fdp->fd_ofileflags, newofileflags,
843 (i = sizeof(char) * fdp->fd_nfiles));
844 bzero(newofileflags + i, nfiles * sizeof(char) - i);
845 if (fdp->fd_nfiles > NDFILE)
846 free(fdp->fd_ofiles, M_FILEDESC);
847 fdp->fd_ofiles = newofile;
848 fdp->fd_ofileflags = newofileflags;
849 fdp->fd_nfiles = nfiles;
856 * Check to see whether n user file descriptors
857 * are available to the process p.
860 fdavail(struct proc *p, int n)
862 struct filedesc *fdp = p->p_fd;
866 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
867 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
870 last = min(fdp->fd_nfiles, lim);
871 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
872 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
873 if (*fpp == NULL && --n <= 0)
881 * Create a new open file structure and allocate a file decriptor
882 * for the process that refers to it. If p is NULL, no descriptor
883 * is allocated and the file pointer is returned unassociated with
884 * any process. resultfd is only used if p is not NULL and may
885 * separately be NULL indicating that you don't need the returned fd.
887 * A held file pointer is returned. If a descriptor has been allocated
888 * an additional hold on the fp will be made due to the fd_ofiles[]
892 falloc(struct proc *p, struct file **resultfp, int *resultfd)
894 static struct timeval lastfail;
902 * Handle filetable full issues and root overfill.
904 if (nfiles >= maxfiles - maxfilesrootres &&
905 ((p && p->p_ucred->cr_ruid != 0) || nfiles >= maxfiles)) {
906 if (ppsratecheck(&lastfail, &curfail, 1)) {
907 printf("kern.maxfiles limit exceeded by uid %d, please see tuning(7).\n",
908 (p ? p->p_ucred->cr_ruid : -1));
915 * Allocate a new file descriptor.
918 fp = malloc(sizeof(struct file), M_FILE, M_WAITOK | M_ZERO);
920 fp->f_ops = &badfileops;
923 fp->f_cred = crhold(p->p_ucred);
925 fp->f_cred = crhold(proc0.p_ucred);
926 LIST_INSERT_HEAD(&filehead, fp, f_list);
928 if ((error = fsetfd(p, fp, resultfd)) != 0) {
929 fdrop(fp, p->p_thread);
941 * Associate a file pointer with a file descriptor. On success the fp
942 * will have an additional ref representing the fd_ofiles[] association.
945 fsetfd(struct proc *p, struct file *fp, int *resultfd)
953 if ((error = fdalloc(p, 0, &i)) == 0) {
955 p->p_fd->fd_ofiles[i] = fp;
962 fsetcred(struct file *fp, struct ucred *cr)
970 * Free a file descriptor.
973 ffree(struct file *fp)
975 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
976 LIST_REMOVE(fp, f_list);
979 cache_drop(fp->f_ncp);
987 * Build a new filedesc structure.
990 fdinit(struct proc *p)
992 struct filedesc0 *newfdp;
993 struct filedesc *fdp = p->p_fd;
995 newfdp = malloc(sizeof(struct filedesc0), M_FILEDESC, M_WAITOK|M_ZERO);
996 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
997 if (newfdp->fd_fd.fd_cdir) {
998 vref(newfdp->fd_fd.fd_cdir);
999 newfdp->fd_fd.fd_ncdir = cache_hold(fdp->fd_ncdir);
1001 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1002 newfdp->fd_fd.fd_nrdir = cache_hold(fdp->fd_nrdir);
1003 vref(newfdp->fd_fd.fd_rdir);
1004 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1005 if (newfdp->fd_fd.fd_jdir) {
1006 vref(newfdp->fd_fd.fd_jdir);
1007 newfdp->fd_fd.fd_njdir = cache_hold(fdp->fd_njdir);
1011 /* Create the file descriptor table. */
1012 newfdp->fd_fd.fd_refcnt = 1;
1013 newfdp->fd_fd.fd_cmask = cmask;
1014 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1015 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1016 newfdp->fd_fd.fd_nfiles = NDFILE;
1017 newfdp->fd_fd.fd_knlistsize = -1;
1019 return (&newfdp->fd_fd);
1023 * Share a filedesc structure.
1026 fdshare(struct proc *p)
1028 p->p_fd->fd_refcnt++;
1033 * Copy a filedesc structure.
1036 fdcopy(struct proc *p)
1038 struct filedesc *newfdp, *fdp = p->p_fd;
1042 /* Certain daemons might not have file descriptors. */
1046 newfdp = malloc(sizeof(struct filedesc0), M_FILEDESC, M_WAITOK);
1047 bcopy(fdp, newfdp, sizeof(struct filedesc));
1048 if (newfdp->fd_cdir) {
1049 vref(newfdp->fd_cdir);
1050 newfdp->fd_ncdir = cache_hold(newfdp->fd_ncdir);
1053 * We must check for fd_rdir here, at least for now because
1054 * the init process is created before we have access to the
1055 * rootvode to take a reference to it.
1057 if (newfdp->fd_rdir) {
1058 vref(newfdp->fd_rdir);
1059 newfdp->fd_nrdir = cache_hold(newfdp->fd_nrdir);
1061 if (newfdp->fd_jdir) {
1062 vref(newfdp->fd_jdir);
1063 newfdp->fd_njdir = cache_hold(newfdp->fd_njdir);
1065 newfdp->fd_refcnt = 1;
1068 * If the number of open files fits in the internal arrays
1069 * of the open file structure, use them, otherwise allocate
1070 * additional memory for the number of descriptors currently
1073 if (newfdp->fd_lastfile < NDFILE) {
1074 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1075 newfdp->fd_ofileflags =
1076 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1080 * Compute the smallest multiple of NDEXTENT needed
1081 * for the file descriptors currently in use,
1082 * allowing the table to shrink.
1084 i = newfdp->fd_nfiles;
1085 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1087 newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
1088 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1090 newfdp->fd_nfiles = i;
1091 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1092 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1095 * kq descriptors cannot be copied.
1097 if (newfdp->fd_knlistsize != -1) {
1098 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1099 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1100 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1102 if (i < newfdp->fd_freefile)
1103 newfdp->fd_freefile = i;
1105 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1106 newfdp->fd_lastfile--;
1108 newfdp->fd_knlist = NULL;
1109 newfdp->fd_knlistsize = -1;
1110 newfdp->fd_knhash = NULL;
1111 newfdp->fd_knhashmask = 0;
1114 fpp = newfdp->fd_ofiles;
1115 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1123 * Release a filedesc structure.
1126 fdfree(struct proc *p)
1128 struct thread *td = p->p_thread;
1129 struct filedesc *fdp = p->p_fd;
1132 struct filedesc_to_leader *fdtol;
1137 /* Certain daemons might not have file descriptors. */
1141 /* Check for special need to clear POSIX style locks */
1143 if (fdtol != NULL) {
1144 KASSERT(fdtol->fdl_refcount > 0,
1145 ("filedesc_to_refcount botch: fdl_refcount=%d",
1146 fdtol->fdl_refcount));
1147 if (fdtol->fdl_refcount == 1 &&
1148 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1150 fpp = fdp->fd_ofiles;
1151 for (i = 0, fpp = fdp->fd_ofiles;
1152 i <= fdp->fd_lastfile;
1155 (*fpp)->f_type != DTYPE_VNODE)
1159 lf.l_whence = SEEK_SET;
1162 lf.l_type = F_UNLCK;
1163 vp = (struct vnode *)fp->f_data;
1164 (void) VOP_ADVLOCK(vp,
1165 (caddr_t)p->p_leader,
1169 fdrop(fp, p->p_thread);
1170 fpp = fdp->fd_ofiles + i;
1174 if (fdtol->fdl_refcount == 1) {
1175 if (fdp->fd_holdleaderscount > 0 &&
1176 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1178 * close() or do_dup() has cleared a reference
1179 * in a shared file descriptor table.
1181 fdp->fd_holdleaderswakeup = 1;
1182 tsleep(&fdp->fd_holdleaderscount,
1186 if (fdtol->fdl_holdcount > 0) {
1188 * Ensure that fdtol->fdl_leader
1189 * remains valid in closef().
1191 fdtol->fdl_wakeup = 1;
1192 tsleep(fdtol, 0, "fdlhold", 0);
1196 fdtol->fdl_refcount--;
1197 if (fdtol->fdl_refcount == 0 &&
1198 fdtol->fdl_holdcount == 0) {
1199 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1200 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1205 free(fdtol, M_FILEDESC_TO_LEADER);
1207 if (--fdp->fd_refcnt > 0)
1210 * we are the last reference to the structure, we can
1211 * safely assume it will not change out from under us.
1213 fpp = fdp->fd_ofiles;
1214 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1216 (void) closef(*fpp, td);
1218 if (fdp->fd_nfiles > NDFILE)
1219 free(fdp->fd_ofiles, M_FILEDESC);
1221 cache_drop(fdp->fd_ncdir);
1222 vrele(fdp->fd_cdir);
1224 cache_drop(fdp->fd_nrdir);
1225 vrele(fdp->fd_rdir);
1227 cache_drop(fdp->fd_njdir);
1228 vrele(fdp->fd_jdir);
1231 free(fdp->fd_knlist, M_KQUEUE);
1233 free(fdp->fd_knhash, M_KQUEUE);
1234 free(fdp, M_FILEDESC);
1238 * For setugid programs, we don't want to people to use that setugidness
1239 * to generate error messages which write to a file which otherwise would
1240 * otherwise be off-limits to the process.
1242 * This is a gross hack to plug the hole. A better solution would involve
1243 * a special vop or other form of generalized access control mechanism. We
1244 * go ahead and just reject all procfs file systems accesses as dangerous.
1246 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1247 * sufficient. We also don't for check setugidness since we know we are.
1250 is_unsafe(struct file *fp)
1252 if (fp->f_type == DTYPE_VNODE &&
1253 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1259 * Make this setguid thing safe, if at all possible.
1262 setugidsafety(struct proc *p)
1264 struct thread *td = p->p_thread;
1265 struct filedesc *fdp = p->p_fd;
1268 /* Certain daemons might not have file descriptors. */
1273 * note: fdp->fd_ofiles may be reallocated out from under us while
1274 * we are blocked in a close. Be careful!
1276 for (i = 0; i <= fdp->fd_lastfile; i++) {
1279 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1283 if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1284 (void) munmapfd(p, i);
1286 if (i < fdp->fd_knlistsize)
1287 knote_fdclose(p, i);
1289 * NULL-out descriptor prior to close to avoid
1290 * a race while close blocks.
1292 fp = fdp->fd_ofiles[i];
1293 fdp->fd_ofiles[i] = NULL;
1294 fdp->fd_ofileflags[i] = 0;
1295 if (i < fdp->fd_freefile)
1296 fdp->fd_freefile = i;
1297 (void) closef(fp, td);
1300 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1305 * Close any files on exec?
1308 fdcloseexec(struct proc *p)
1310 struct thread *td = p->p_thread;
1311 struct filedesc *fdp = p->p_fd;
1314 /* Certain daemons might not have file descriptors. */
1319 * We cannot cache fd_ofiles or fd_ofileflags since operations
1320 * may block and rip them out from under us.
1322 for (i = 0; i <= fdp->fd_lastfile; i++) {
1323 if (fdp->fd_ofiles[i] != NULL &&
1324 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1328 if (fdp->fd_ofileflags[i] & UF_MAPPED)
1329 (void) munmapfd(p, i);
1331 if (i < fdp->fd_knlistsize)
1332 knote_fdclose(p, i);
1334 * NULL-out descriptor prior to close to avoid
1335 * a race while close blocks.
1337 fp = fdp->fd_ofiles[i];
1338 fdp->fd_ofiles[i] = NULL;
1339 fdp->fd_ofileflags[i] = 0;
1340 if (i < fdp->fd_freefile)
1341 fdp->fd_freefile = i;
1342 (void) closef(fp, td);
1345 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1350 * It is unsafe for set[ug]id processes to be started with file
1351 * descriptors 0..2 closed, as these descriptors are given implicit
1352 * significance in the Standard C library. fdcheckstd() will create a
1353 * descriptor referencing /dev/null for each of stdin, stdout, and
1354 * stderr that is not already open.
1357 fdcheckstd(struct proc *p)
1359 struct thread *td = p->p_thread;
1360 struct nlookupdata nd;
1361 struct filedesc *fdp;
1364 int fd, i, error, flags, devnull;
1371 for (i = 0; i < 3; i++) {
1372 if (fdp->fd_ofiles[i] != NULL)
1375 if ((error = falloc(p, &fp, NULL)) != 0)
1378 error = nlookup_init(&nd, "/dev/null", UIO_SYSSPACE,
1379 NLC_FOLLOW|NLC_LOCKVP);
1380 flags = FREAD | FWRITE;
1382 error = vn_open(&nd, fp, flags, 0);
1384 error = fsetfd(p, fp, &fd);
1392 error = kern_dup(DUP_FIXED, devnull, i, &retval);
1401 * Internal form of close.
1402 * Decrement reference count on file structure.
1403 * Note: td and/or p may be NULL when closing a file
1404 * that was being passed in a message.
1407 closef(struct file *fp, struct thread *td)
1411 struct filedesc_to_leader *fdtol;
1418 p = NULL; /* allow no proc association */
1420 p = td->td_proc; /* can also be NULL */
1423 * POSIX record locking dictates that any close releases ALL
1424 * locks owned by this process. This is handled by setting
1425 * a flag in the unlock to free ONLY locks obeying POSIX
1426 * semantics, and not to free BSD-style file locks.
1427 * If the descriptor was in a message, POSIX-style locks
1428 * aren't passed with the descriptor.
1431 fp->f_type == DTYPE_VNODE) {
1432 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
1433 lf.l_whence = SEEK_SET;
1436 lf.l_type = F_UNLCK;
1437 vp = (struct vnode *)fp->f_data;
1438 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
1442 if (fdtol != NULL) {
1444 * Handle special case where file descriptor table
1445 * is shared between multiple process leaders.
1447 for (fdtol = fdtol->fdl_next;
1448 fdtol != p->p_fdtol;
1449 fdtol = fdtol->fdl_next) {
1450 if ((fdtol->fdl_leader->p_flag &
1453 fdtol->fdl_holdcount++;
1454 lf.l_whence = SEEK_SET;
1457 lf.l_type = F_UNLCK;
1458 vp = (struct vnode *)fp->f_data;
1459 (void) VOP_ADVLOCK(vp,
1460 (caddr_t)p->p_leader,
1461 F_UNLCK, &lf, F_POSIX);
1462 fdtol->fdl_holdcount--;
1463 if (fdtol->fdl_holdcount == 0 &&
1464 fdtol->fdl_wakeup != 0) {
1465 fdtol->fdl_wakeup = 0;
1471 return (fdrop(fp, td));
1475 fdrop(struct file *fp, struct thread *td)
1481 if (--fp->f_count > 0)
1483 if (fp->f_count < 0)
1484 panic("fdrop: count < 0");
1485 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1486 lf.l_whence = SEEK_SET;
1489 lf.l_type = F_UNLCK;
1490 vp = (struct vnode *)fp->f_data;
1491 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1493 if (fp->f_ops != &badfileops)
1494 error = fo_close(fp, td);
1502 * Apply an advisory lock on a file descriptor.
1504 * Just attempt to get a record lock of the requested type on
1505 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1509 flock(struct flock_args *uap)
1511 struct proc *p = curproc;
1512 struct filedesc *fdp = p->p_fd;
1517 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1518 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1520 if (fp->f_type != DTYPE_VNODE)
1521 return (EOPNOTSUPP);
1522 vp = (struct vnode *)fp->f_data;
1523 lf.l_whence = SEEK_SET;
1526 if (uap->how & LOCK_UN) {
1527 lf.l_type = F_UNLCK;
1528 fp->f_flag &= ~FHASLOCK;
1529 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1531 if (uap->how & LOCK_EX)
1532 lf.l_type = F_WRLCK;
1533 else if (uap->how & LOCK_SH)
1534 lf.l_type = F_RDLCK;
1537 fp->f_flag |= FHASLOCK;
1538 if (uap->how & LOCK_NB)
1539 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1540 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1544 * File Descriptor pseudo-device driver (/dev/fd/).
1546 * Opening minor device N dup()s the file (if any) connected to file
1547 * descriptor N belonging to the calling process. Note that this driver
1548 * consists of only the ``open()'' routine, because all subsequent
1549 * references to this file will be direct to the other driver.
1553 fdopen(dev_t dev, int mode, int type, struct thread *td)
1555 KKASSERT(td->td_proc != NULL);
1558 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1559 * the file descriptor being sought for duplication. The error
1560 * return ensures that the vnode for this device will be released
1561 * by vn_open. Open will detect this special error and take the
1562 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1563 * will simply report the error.
1565 td->td_proc->p_dupfd = minor(dev);
1570 * Duplicate the specified descriptor to a free descriptor.
1573 dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, int error)
1579 * If the to-be-dup'd fd number is greater than the allowed number
1580 * of file descriptors, or the fd to be dup'd has already been
1581 * closed, then reject.
1583 if ((u_int)dfd >= fdp->fd_nfiles ||
1584 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1589 * There are two cases of interest here.
1591 * For ENODEV simply dup (dfd) to file descriptor
1592 * (indx) and return.
1594 * For ENXIO steal away the file structure from (dfd) and
1595 * store it in (indx). (dfd) is effectively closed by
1598 * Any other error code is just returned.
1603 * Check that the mode the file is being opened for is a
1604 * subset of the mode of the existing descriptor.
1606 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1608 fp = fdp->fd_ofiles[indx];
1610 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1611 (void) munmapfd(p, indx);
1613 fdp->fd_ofiles[indx] = wfp;
1614 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1616 if (indx > fdp->fd_lastfile)
1617 fdp->fd_lastfile = indx;
1619 * we now own the reference to fp that the ofiles[] array
1620 * used to own. Release it.
1623 fdrop(fp, curthread);
1628 * Steal away the file pointer from dfd, and stuff it into indx.
1630 fp = fdp->fd_ofiles[indx];
1632 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1633 (void) munmapfd(p, indx);
1635 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1636 fdp->fd_ofiles[dfd] = NULL;
1637 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1638 fdp->fd_ofileflags[dfd] = 0;
1641 * we now own the reference to fp that the ofiles[] array
1642 * used to own. Release it.
1645 fdrop(fp, curthread);
1647 * Complete the clean up of the filedesc structure by
1648 * recomputing the various hints.
1650 if (indx > fdp->fd_lastfile) {
1651 fdp->fd_lastfile = indx;
1653 while (fdp->fd_lastfile > 0 &&
1654 fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1657 if (dfd < fdp->fd_freefile)
1658 fdp->fd_freefile = dfd;
1669 struct filedesc_to_leader *
1670 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
1671 struct proc *leader)
1673 struct filedesc_to_leader *fdtol;
1675 fdtol = malloc(sizeof(struct filedesc_to_leader),
1676 M_FILEDESC_TO_LEADER, M_WAITOK);
1677 fdtol->fdl_refcount = 1;
1678 fdtol->fdl_holdcount = 0;
1679 fdtol->fdl_wakeup = 0;
1680 fdtol->fdl_leader = leader;
1682 fdtol->fdl_next = old->fdl_next;
1683 fdtol->fdl_prev = old;
1684 old->fdl_next = fdtol;
1685 fdtol->fdl_next->fdl_prev = fdtol;
1687 fdtol->fdl_next = fdtol;
1688 fdtol->fdl_prev = fdtol;
1694 * Get file structures.
1697 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1699 struct kinfo_file kf;
1700 struct filedesc *fdp;
1706 * Note: because the number of file descriptors is calculated
1707 * in different ways for sizing vs returning the data,
1708 * there is information leakage from the first loop. However,
1709 * it is of a similar order of magnitude to the leakage from
1710 * global system statistics such as kern.openfiles.
1712 if (req->oldptr == NULL) {
1713 n = 16; /* A slight overestimate. */
1714 LIST_FOREACH(fp, &filehead, f_list)
1716 return (SYSCTL_OUT(req, 0, n * sizeof(kf)));
1719 LIST_FOREACH(p, &allproc, p_list) {
1720 if (p->p_stat == SIDL)
1722 if (!PRISON_CHECK(req->td->td_proc->p_ucred, p->p_ucred) != 0) {
1725 if ((fdp = p->p_fd) == NULL) {
1728 for (n = 0; n < fdp->fd_nfiles; ++n) {
1729 if ((fp = fdp->fd_ofiles[n]) == NULL)
1731 kcore_make_file(&kf, fp, p->p_pid,
1732 p->p_ucred->cr_uid, n);
1733 error = SYSCTL_OUT(req, &kf, sizeof(kf));
1743 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1744 0, 0, sysctl_kern_file, "S,file", "Entire file table");
1746 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1747 &maxfilesperproc, 0, "Maximum files allowed open per process");
1749 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1750 &maxfiles, 0, "Maximum number of files");
1752 SYSCTL_INT(_kern, OID_AUTO, maxfilesrootres, CTLFLAG_RW,
1753 &maxfilesrootres, 0, "Descriptors reserved for root use");
1755 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1756 &nfiles, 0, "System-wide number of open files");
1759 fildesc_drvinit(void *unused)
1763 cdevsw_add(&fildesc_cdevsw, 0, 0);
1764 for (fd = 0; fd < NUMFDESC; fd++) {
1765 make_dev(&fildesc_cdevsw, fd,
1766 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
1768 make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
1769 make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
1770 make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
1773 struct fileops badfileops = {
1797 badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
1803 badfo_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1809 badfo_kqfilter(struct file *fp, struct knote *kn)
1815 badfo_stat(struct file *fp, struct stat *sb, struct thread *td)
1821 badfo_close(struct file *fp, struct thread *td)
1826 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1827 fildesc_drvinit,NULL)