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.12 2003/08/26 21:09:02 rob 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 = {
92 /* strategy */ nostrategy,
97 static int do_dup (struct filedesc *fdp, int old, int new, register_t *retval, struct proc *p);
98 static int badfo_readwrite (struct file *fp, struct uio *uio,
99 struct ucred *cred, int flags, struct thread *td);
100 static int badfo_ioctl (struct file *fp, u_long com, caddr_t data,
102 static int badfo_poll (struct file *fp, int events,
103 struct ucred *cred, struct thread *td);
104 static int badfo_kqfilter (struct file *fp, struct knote *kn);
105 static int badfo_stat (struct file *fp, struct stat *sb, struct thread *td);
106 static int badfo_close (struct file *fp, struct thread *td);
109 * Descriptor management.
111 struct filelist filehead; /* head of list of open files */
112 int nfiles; /* actual number of open files */
116 * System calls on descriptors.
120 getdtablesize(struct getdtablesize_args *uap)
122 struct proc *p = curproc;
125 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
130 * Duplicate a file descriptor to a particular value.
132 * note: keep in mind that a potential race condition exists when closing
133 * descriptors from a shared descriptor table (via rfork).
137 dup2(struct dup2_args *uap)
139 struct proc *p = curproc;
140 struct filedesc *fdp = p->p_fd;
141 u_int old = uap->from, new = uap->to;
145 if (old >= fdp->fd_nfiles ||
146 fdp->fd_ofiles[old] == NULL ||
147 new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
148 new >= maxfilesperproc) {
152 uap->sysmsg_result = new;
155 if (new >= fdp->fd_nfiles) {
156 if ((error = fdalloc(p, new, &i)))
159 * fdalloc() may block, retest everything.
163 return (do_dup(fdp, (int)old, (int)new, uap->sysmsg_fds, p));
167 * Duplicate a file descriptor.
171 dup(struct dup_args *uap)
173 struct proc *p = curproc;
174 struct filedesc *fdp;
180 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
182 if ((error = fdalloc(p, 0, &new)))
184 return (do_dup(fdp, (int)old, new, uap->sysmsg_fds, p));
188 * The file control system call.
192 fcntl(struct fcntl_args *uap)
194 struct thread *td = curthread;
195 struct proc *p = td->td_proc;
196 struct filedesc *fdp = p->p_fd;
200 int i, tmp, error, flg = F_POSIX;
206 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
207 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
209 pop = &fdp->fd_ofileflags[uap->fd];
214 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
215 newmin >= maxfilesperproc)
217 if ((error = fdalloc(p, newmin, &i)))
219 return (do_dup(fdp, uap->fd, i, uap->sysmsg_fds, p));
222 uap->sysmsg_result = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
226 *pop = (*pop &~ UF_EXCLOSE) |
227 (uap->arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
231 uap->sysmsg_result = OFLAGS(fp->f_flag);
236 fp->f_flag &= ~FCNTLFLAGS;
237 fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
238 tmp = fp->f_flag & FNONBLOCK;
239 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
244 tmp = fp->f_flag & FASYNC;
245 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
250 fp->f_flag &= ~FNONBLOCK;
252 (void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
258 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)uap->sysmsg_fds, td);
264 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, td);
270 /* Fall into F_SETLK */
273 if (fp->f_type != DTYPE_VNODE)
275 vp = (struct vnode *)fp->f_data;
278 * copyin/lockop may block
281 /* Copy in the lock structure */
282 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
288 if (fl.l_whence == SEEK_CUR)
289 fl.l_start += fp->f_offset;
293 if ((fp->f_flag & FREAD) == 0) {
297 p->p_leader->p_flag |= P_ADVLOCK;
298 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
302 if ((fp->f_flag & FWRITE) == 0) {
306 p->p_leader->p_flag |= P_ADVLOCK;
307 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
311 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
318 /* Check for race with close */
319 if ((unsigned) uap->fd >= fdp->fd_nfiles ||
320 fp != fdp->fd_ofiles[uap->fd]) {
321 fl.l_whence = SEEK_SET;
325 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
326 F_UNLCK, &fl, F_POSIX);
332 if (fp->f_type != DTYPE_VNODE)
334 vp = (struct vnode *)fp->f_data;
336 * copyin/lockop may block
339 /* Copy in the lock structure */
340 error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
346 if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
347 fl.l_type != F_UNLCK) {
351 if (fl.l_whence == SEEK_CUR)
352 fl.l_start += fp->f_offset;
353 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
357 error = copyout((caddr_t)&fl,
358 (caddr_t)(intptr_t)uap->arg, sizeof(fl));
368 * Common code for dup, dup2, and fcntl(F_DUPFD).
371 do_dup(fdp, old, new, retval, p)
372 struct filedesc *fdp;
377 struct thread *td = p->p_thread;
383 * Save info on the descriptor being overwritten. We have
384 * to do the unmap now, but we cannot close it without
385 * introducing an ownership race for the slot.
387 delfp = fdp->fd_ofiles[new];
388 if (delfp != NULL && p->p_fdtol != NULL) {
390 * Ask fdfree() to sleep to ensure that all relevant
391 * process leaders can be traversed in closef().
393 fdp->fd_holdleaderscount++;
398 if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
399 (void) munmapfd(p, new);
403 * Duplicate the source descriptor, update lastfile
405 fp = fdp->fd_ofiles[old];
406 fdp->fd_ofiles[new] = fp;
407 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
409 if (new > fdp->fd_lastfile)
410 fdp->fd_lastfile = new;
414 * If we dup'd over a valid file, we now own the reference to it
415 * and must dispose of it using closef() semantics (as if a
416 * close() were performed on it).
419 (void) closef(delfp, td);
421 fdp->fd_holdleaderscount--;
422 if (fdp->fd_holdleaderscount == 0 &&
423 fdp->fd_holdleaderswakeup != 0) {
424 fdp->fd_holdleaderswakeup = 0;
425 wakeup(&fdp->fd_holdleaderscount);
433 * If sigio is on the list associated with a process or process group,
434 * disable signalling from the device, remove sigio from the list and
446 *(sigio->sio_myref) = NULL;
448 if (sigio->sio_pgid < 0) {
449 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
451 } else /* if ((*sigiop)->sio_pgid > 0) */ {
452 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
455 crfree(sigio->sio_ucred);
456 FREE(sigio, M_SIGIO);
459 /* Free a list of sigio structures. */
461 funsetownlst(sigiolst)
462 struct sigiolst *sigiolst;
466 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
471 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
473 * After permission checking, add a sigio structure to the sigio list for
474 * the process or process group.
477 fsetown(pgid, sigiop)
479 struct sigio **sigiop;
496 * Policy - Don't allow a process to FSETOWN a process
497 * in another session.
499 * Remove this test to allow maximum flexibility or
500 * restrict FSETOWN to the current process or process
501 * group for maximum safety.
503 if (proc->p_session != curproc->p_session)
507 } else /* if (pgid < 0) */ {
508 pgrp = pgfind(-pgid);
513 * Policy - Don't allow a process to FSETOWN a process
514 * in another session.
516 * Remove this test to allow maximum flexibility or
517 * restrict FSETOWN to the current process or process
518 * group for maximum safety.
520 if (pgrp->pg_session != curproc->p_session)
526 MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
528 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
529 sigio->sio_proc = proc;
531 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
532 sigio->sio_pgrp = pgrp;
534 sigio->sio_pgid = pgid;
535 sigio->sio_ucred = crhold(curproc->p_ucred);
536 /* It would be convenient if p_ruid was in ucred. */
537 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
538 sigio->sio_myref = sigiop;
546 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
552 return (sigio != NULL ? sigio->sio_pgid : 0);
556 * Close a file descriptor.
560 close(struct close_args *uap)
562 struct thread *td = curthread;
563 struct proc *p = td->td_proc;
564 struct filedesc *fdp;
573 if ((unsigned)fd >= fdp->fd_nfiles ||
574 (fp = fdp->fd_ofiles[fd]) == NULL)
577 if (fdp->fd_ofileflags[fd] & UF_MAPPED)
578 (void) munmapfd(p, fd);
580 fdp->fd_ofiles[fd] = NULL;
581 fdp->fd_ofileflags[fd] = 0;
583 if (p->p_fdtol != NULL) {
585 * Ask fdfree() to sleep to ensure that all relevant
586 * process leaders can be traversed in closef().
588 fdp->fd_holdleaderscount++;
593 * we now hold the fp reference that used to be owned by the descriptor
596 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
598 if (fd < fdp->fd_freefile)
599 fdp->fd_freefile = fd;
600 if (fd < fdp->fd_knlistsize)
601 knote_fdclose(p, fd);
602 error = closef(fp, td);
604 fdp->fd_holdleaderscount--;
605 if (fdp->fd_holdleaderscount == 0 &&
606 fdp->fd_holdleaderswakeup != 0) {
607 fdp->fd_holdleaderswakeup = 0;
608 wakeup(&fdp->fd_holdleaderscount);
614 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
616 * Return status information about a file descriptor.
620 ofstat(struct ofstat_args *uap)
622 struct thread *td = curthread;
623 struct proc *p = td->td_proc;
624 struct filedesc *fdp;
632 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
633 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
636 error = fo_stat(fp, &ub, td);
639 error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
644 #endif /* COMPAT_43 || COMPAT_SUNOS */
647 * Return status information about a file descriptor.
651 fstat(struct fstat_args *uap)
653 struct thread *td = curthread;
654 struct proc *p = td->td_proc;
655 struct filedesc *fdp = p->p_fd;
662 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
663 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
666 error = fo_stat(fp, &ub, td);
668 error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
674 * Return status information about a file descriptor.
678 nfstat(struct nfstat_args *uap)
680 struct thread *td = curthread;
681 struct proc *p = td->td_proc;
682 struct filedesc *fdp;
690 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
691 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
694 error = fo_stat(fp, &ub, td);
697 error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
704 * Return pathconf information about a file descriptor.
708 fpathconf(struct fpathconf_args *uap)
710 struct thread *td = curthread;
711 struct proc *p = td->td_proc;
712 struct filedesc *fdp;
719 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
720 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
725 switch (fp->f_type) {
728 if (uap->name != _PC_PIPE_BUF) {
731 uap->sysmsg_result = PIPE_BUF;
737 vp = (struct vnode *)fp->f_data;
738 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
749 * Allocate a file descriptor for the process.
752 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
755 fdalloc(p, want, result)
760 struct filedesc *fdp = p->p_fd;
762 int lim, last, nfiles;
763 struct file **newofile;
767 * Search for a free descriptor starting at the higher
768 * of want or fd_freefile. If that fails, consider
769 * expanding the ofile array.
771 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
773 last = min(fdp->fd_nfiles, lim);
774 if ((i = want) < fdp->fd_freefile)
775 i = fdp->fd_freefile;
776 for (; i < last; i++) {
777 if (fdp->fd_ofiles[i] == NULL) {
778 fdp->fd_ofileflags[i] = 0;
779 if (i > fdp->fd_lastfile)
780 fdp->fd_lastfile = i;
781 if (want <= fdp->fd_freefile)
782 fdp->fd_freefile = i;
789 * No space in current array. Expand?
791 if (fdp->fd_nfiles >= lim)
793 if (fdp->fd_nfiles < NDEXTENT)
796 nfiles = 2 * fdp->fd_nfiles;
797 MALLOC(newofile, struct file **, nfiles * OFILESIZE,
798 M_FILEDESC, M_WAITOK);
801 * deal with file-table extend race that might have occured
802 * when malloc was blocked.
804 if (fdp->fd_nfiles >= nfiles) {
805 FREE(newofile, M_FILEDESC);
808 newofileflags = (char *) &newofile[nfiles];
810 * Copy the existing ofile and ofileflags arrays
811 * and zero the new portion of each array.
813 bcopy(fdp->fd_ofiles, newofile,
814 (i = sizeof(struct file *) * fdp->fd_nfiles));
815 bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
816 bcopy(fdp->fd_ofileflags, newofileflags,
817 (i = sizeof(char) * fdp->fd_nfiles));
818 bzero(newofileflags + i, nfiles * sizeof(char) - i);
819 if (fdp->fd_nfiles > NDFILE)
820 FREE(fdp->fd_ofiles, M_FILEDESC);
821 fdp->fd_ofiles = newofile;
822 fdp->fd_ofileflags = newofileflags;
823 fdp->fd_nfiles = nfiles;
830 * Check to see whether n user file descriptors
831 * are available to the process p.
838 struct filedesc *fdp = p->p_fd;
842 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
843 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
846 last = min(fdp->fd_nfiles, lim);
847 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
848 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
849 if (*fpp == NULL && --n <= 0)
856 * Create a new open file structure and allocate
857 * a file decriptor for the process that refers to it.
860 falloc(p, resultfp, resultfd)
862 struct file **resultfp;
865 struct file *fp, *fq;
868 if (nfiles >= maxfiles) {
873 * Allocate a new file descriptor.
874 * If the process has file descriptor zero open, add to the list
875 * of open files at that point, otherwise put it at the front of
876 * the list of open files.
879 MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
880 bzero(fp, sizeof(struct file));
883 * wait until after malloc (which may have blocked) returns before
884 * allocating the slot, else a race might have shrunk it if we had
885 * allocated it before the malloc.
887 if ((error = fdalloc(p, 0, &i))) {
893 fp->f_cred = crhold(p->p_ucred);
894 fp->f_ops = &badfileops;
896 if ((fq = p->p_fd->fd_ofiles[0])) {
897 LIST_INSERT_AFTER(fq, fp, f_list);
899 LIST_INSERT_HEAD(&filehead, fp, f_list);
901 p->p_fd->fd_ofiles[i] = fp;
910 * Free a file descriptor.
916 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
917 LIST_REMOVE(fp, f_list);
924 * Build a new filedesc structure.
930 struct filedesc0 *newfdp;
931 struct filedesc *fdp = p->p_fd;
933 MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
934 M_FILEDESC, M_WAITOK);
935 bzero(newfdp, sizeof(struct filedesc0));
936 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
937 if (newfdp->fd_fd.fd_cdir)
938 VREF(newfdp->fd_fd.fd_cdir);
939 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
940 if (newfdp->fd_fd.fd_rdir)
941 VREF(newfdp->fd_fd.fd_rdir);
942 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
943 if (newfdp->fd_fd.fd_jdir)
944 VREF(newfdp->fd_fd.fd_jdir);
946 /* Create the file descriptor table. */
947 newfdp->fd_fd.fd_refcnt = 1;
948 newfdp->fd_fd.fd_cmask = cmask;
949 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
950 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
951 newfdp->fd_fd.fd_nfiles = NDFILE;
952 newfdp->fd_fd.fd_knlistsize = -1;
954 return (&newfdp->fd_fd);
958 * Share a filedesc structure.
964 p->p_fd->fd_refcnt++;
969 * Copy a filedesc structure.
975 struct filedesc *newfdp, *fdp = p->p_fd;
979 /* Certain daemons might not have file descriptors. */
983 MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
984 M_FILEDESC, M_WAITOK);
985 bcopy(fdp, newfdp, sizeof(struct filedesc));
987 VREF(newfdp->fd_cdir);
989 VREF(newfdp->fd_rdir);
991 VREF(newfdp->fd_jdir);
992 newfdp->fd_refcnt = 1;
995 * If the number of open files fits in the internal arrays
996 * of the open file structure, use them, otherwise allocate
997 * additional memory for the number of descriptors currently
1000 if (newfdp->fd_lastfile < NDFILE) {
1001 newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1002 newfdp->fd_ofileflags =
1003 ((struct filedesc0 *) newfdp)->fd_dfileflags;
1007 * Compute the smallest multiple of NDEXTENT needed
1008 * for the file descriptors currently in use,
1009 * allowing the table to shrink.
1011 i = newfdp->fd_nfiles;
1012 while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1014 MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
1015 M_FILEDESC, M_WAITOK);
1016 newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1018 newfdp->fd_nfiles = i;
1019 bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1020 bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1023 * kq descriptors cannot be copied.
1025 if (newfdp->fd_knlistsize != -1) {
1026 fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1027 for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1028 if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1030 if (i < newfdp->fd_freefile)
1031 newfdp->fd_freefile = i;
1033 if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1034 newfdp->fd_lastfile--;
1036 newfdp->fd_knlist = NULL;
1037 newfdp->fd_knlistsize = -1;
1038 newfdp->fd_knhash = NULL;
1039 newfdp->fd_knhashmask = 0;
1042 fpp = newfdp->fd_ofiles;
1043 for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1051 * Release a filedesc structure.
1054 fdfree(struct proc *p)
1056 struct thread *td = p->p_thread;
1057 struct filedesc *fdp = p->p_fd;
1060 struct filedesc_to_leader *fdtol;
1065 /* Certain daemons might not have file descriptors. */
1069 /* Check for special need to clear POSIX style locks */
1071 if (fdtol != NULL) {
1072 KASSERT(fdtol->fdl_refcount > 0,
1073 ("filedesc_to_refcount botch: fdl_refcount=%d",
1074 fdtol->fdl_refcount));
1075 if (fdtol->fdl_refcount == 1 &&
1076 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1078 fpp = fdp->fd_ofiles;
1079 for (i = 0, fpp = fdp->fd_ofiles;
1080 i < fdp->fd_lastfile;
1083 (*fpp)->f_type != DTYPE_VNODE)
1087 lf.l_whence = SEEK_SET;
1090 lf.l_type = F_UNLCK;
1091 vp = (struct vnode *)fp->f_data;
1092 (void) VOP_ADVLOCK(vp,
1093 (caddr_t)p->p_leader,
1097 fdrop(fp, p->p_thread);
1098 fpp = fdp->fd_ofiles + i;
1102 if (fdtol->fdl_refcount == 1) {
1103 if (fdp->fd_holdleaderscount > 0 &&
1104 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1106 * close() or do_dup() has cleared a reference
1107 * in a shared file descriptor table.
1109 fdp->fd_holdleaderswakeup = 1;
1110 tsleep(&fdp->fd_holdleaderscount,
1114 if (fdtol->fdl_holdcount > 0) {
1116 * Ensure that fdtol->fdl_leader
1117 * remains valid in closef().
1119 fdtol->fdl_wakeup = 1;
1120 tsleep(fdtol, 0, "fdlhold", 0);
1124 fdtol->fdl_refcount--;
1125 if (fdtol->fdl_refcount == 0 &&
1126 fdtol->fdl_holdcount == 0) {
1127 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1128 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1133 FREE(fdtol, M_FILEDESC_TO_LEADER);
1135 if (--fdp->fd_refcnt > 0)
1138 * we are the last reference to the structure, we can
1139 * safely assume it will not change out from under us.
1141 fpp = fdp->fd_ofiles;
1142 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1144 (void) closef(*fpp, td);
1146 if (fdp->fd_nfiles > NDFILE)
1147 FREE(fdp->fd_ofiles, M_FILEDESC);
1149 vrele(fdp->fd_cdir);
1151 vrele(fdp->fd_rdir);
1153 vrele(fdp->fd_jdir);
1155 FREE(fdp->fd_knlist, M_KQUEUE);
1157 FREE(fdp->fd_knhash, M_KQUEUE);
1158 FREE(fdp, M_FILEDESC);
1162 * For setugid programs, we don't want to people to use that setugidness
1163 * to generate error messages which write to a file which otherwise would
1164 * otherwise be off-limits to the process.
1166 * This is a gross hack to plug the hole. A better solution would involve
1167 * a special vop or other form of generalized access control mechanism. We
1168 * go ahead and just reject all procfs file systems accesses as dangerous.
1170 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1171 * sufficient. We also don't for check setugidness since we know we are.
1174 is_unsafe(struct file *fp)
1176 if (fp->f_type == DTYPE_VNODE &&
1177 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1183 * Make this setguid thing safe, if at all possible.
1186 setugidsafety(struct proc *p)
1188 struct thread *td = p->p_thread;
1189 struct filedesc *fdp = p->p_fd;
1192 /* Certain daemons might not have file descriptors. */
1197 * note: fdp->fd_ofiles may be reallocated out from under us while
1198 * we are blocked in a close. Be careful!
1200 for (i = 0; i <= fdp->fd_lastfile; i++) {
1203 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1207 if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1208 (void) munmapfd(p, i);
1210 if (i < fdp->fd_knlistsize)
1211 knote_fdclose(p, i);
1213 * NULL-out descriptor prior to close to avoid
1214 * a race while close blocks.
1216 fp = fdp->fd_ofiles[i];
1217 fdp->fd_ofiles[i] = NULL;
1218 fdp->fd_ofileflags[i] = 0;
1219 if (i < fdp->fd_freefile)
1220 fdp->fd_freefile = i;
1221 (void) closef(fp, td);
1224 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1229 * Close any files on exec?
1232 fdcloseexec(struct proc *p)
1234 struct thread *td = p->p_thread;
1235 struct filedesc *fdp = p->p_fd;
1238 /* Certain daemons might not have file descriptors. */
1243 * We cannot cache fd_ofiles or fd_ofileflags since operations
1244 * may block and rip them out from under us.
1246 for (i = 0; i <= fdp->fd_lastfile; i++) {
1247 if (fdp->fd_ofiles[i] != NULL &&
1248 (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1252 if (fdp->fd_ofileflags[i] & UF_MAPPED)
1253 (void) munmapfd(p, i);
1255 if (i < fdp->fd_knlistsize)
1256 knote_fdclose(p, i);
1258 * NULL-out descriptor prior to close to avoid
1259 * a race while close blocks.
1261 fp = fdp->fd_ofiles[i];
1262 fdp->fd_ofiles[i] = NULL;
1263 fdp->fd_ofileflags[i] = 0;
1264 if (i < fdp->fd_freefile)
1265 fdp->fd_freefile = i;
1266 (void) closef(fp, td);
1269 while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1274 * It is unsafe for set[ug]id processes to be started with file
1275 * descriptors 0..2 closed, as these descriptors are given implicit
1276 * significance in the Standard C library. fdcheckstd() will create a
1277 * descriptor referencing /dev/null for each of stdin, stdout, and
1278 * stderr that is not already open.
1281 fdcheckstd(struct proc *p)
1283 struct thread *td = p->p_thread;
1284 struct nameidata nd;
1285 struct filedesc *fdp;
1288 int fd, i, error, flags, devnull;
1295 for (i = 0; i < 3; i++) {
1296 if (fdp->fd_ofiles[i] != NULL)
1299 error = falloc(p, &fp, &fd);
1302 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
1304 flags = FREAD | FWRITE;
1305 error = vn_open(&nd, flags, 0);
1307 fdp->fd_ofiles[i] = NULL;
1311 NDFREE(&nd, NDF_ONLY_PNBUF);
1312 fp->f_data = (caddr_t)nd.ni_vp;
1315 fp->f_type = DTYPE_VNODE;
1316 VOP_UNLOCK(nd.ni_vp, 0, td);
1319 error = fdalloc(p, 0, &fd);
1322 error = do_dup(fdp, devnull, fd, &retval, p);
1331 * Internal form of close.
1332 * Decrement reference count on file structure.
1333 * Note: td and/or p may be NULL when closing a file
1334 * that was being passed in a message.
1337 closef(struct file *fp, struct thread *td)
1341 struct filedesc_to_leader *fdtol;
1348 p = NULL; /* allow no proc association */
1350 p = td->td_proc; /* can also be NULL */
1353 * POSIX record locking dictates that any close releases ALL
1354 * locks owned by this process. This is handled by setting
1355 * a flag in the unlock to free ONLY locks obeying POSIX
1356 * semantics, and not to free BSD-style file locks.
1357 * If the descriptor was in a message, POSIX-style locks
1358 * aren't passed with the descriptor.
1361 fp->f_type == DTYPE_VNODE) {
1362 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
1363 lf.l_whence = SEEK_SET;
1366 lf.l_type = F_UNLCK;
1367 vp = (struct vnode *)fp->f_data;
1368 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
1372 if (fdtol != NULL) {
1374 * Handle special case where file descriptor table
1375 * is shared between multiple process leaders.
1377 for (fdtol = fdtol->fdl_next;
1378 fdtol != p->p_fdtol;
1379 fdtol = fdtol->fdl_next) {
1380 if ((fdtol->fdl_leader->p_flag &
1383 fdtol->fdl_holdcount++;
1384 lf.l_whence = SEEK_SET;
1387 lf.l_type = F_UNLCK;
1388 vp = (struct vnode *)fp->f_data;
1389 (void) VOP_ADVLOCK(vp,
1390 (caddr_t)p->p_leader,
1391 F_UNLCK, &lf, F_POSIX);
1392 fdtol->fdl_holdcount--;
1393 if (fdtol->fdl_holdcount == 0 &&
1394 fdtol->fdl_wakeup != 0) {
1395 fdtol->fdl_wakeup = 0;
1401 return (fdrop(fp, td));
1405 fdrop(struct file *fp, struct thread *td)
1411 if (--fp->f_count > 0)
1413 if (fp->f_count < 0)
1414 panic("fdrop: count < 0");
1415 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1416 lf.l_whence = SEEK_SET;
1419 lf.l_type = F_UNLCK;
1420 vp = (struct vnode *)fp->f_data;
1421 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1423 if (fp->f_ops != &badfileops)
1424 error = fo_close(fp, td);
1432 * Apply an advisory lock on a file descriptor.
1434 * Just attempt to get a record lock of the requested type on
1435 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1439 flock(struct flock_args *uap)
1441 struct proc *p = curproc;
1442 struct filedesc *fdp = p->p_fd;
1447 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1448 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1450 if (fp->f_type != DTYPE_VNODE)
1451 return (EOPNOTSUPP);
1452 vp = (struct vnode *)fp->f_data;
1453 lf.l_whence = SEEK_SET;
1456 if (uap->how & LOCK_UN) {
1457 lf.l_type = F_UNLCK;
1458 fp->f_flag &= ~FHASLOCK;
1459 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1461 if (uap->how & LOCK_EX)
1462 lf.l_type = F_WRLCK;
1463 else if (uap->how & LOCK_SH)
1464 lf.l_type = F_RDLCK;
1467 fp->f_flag |= FHASLOCK;
1468 if (uap->how & LOCK_NB)
1469 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1470 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1474 * File Descriptor pseudo-device driver (/dev/fd/).
1476 * Opening minor device N dup()s the file (if any) connected to file
1477 * descriptor N belonging to the calling process. Note that this driver
1478 * consists of only the ``open()'' routine, because all subsequent
1479 * references to this file will be direct to the other driver.
1483 fdopen(dev_t dev, int mode, int type, struct thread *td)
1485 KKASSERT(td->td_proc != NULL);
1488 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1489 * the file descriptor being sought for duplication. The error
1490 * return ensures that the vnode for this device will be released
1491 * by vn_open. Open will detect this special error and take the
1492 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1493 * will simply report the error.
1495 td->td_proc->p_dupfd = minor(dev);
1500 * Duplicate the specified descriptor to a free descriptor.
1503 dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, int error)
1509 * If the to-be-dup'd fd number is greater than the allowed number
1510 * of file descriptors, or the fd to be dup'd has already been
1511 * closed, then reject.
1513 if ((u_int)dfd >= fdp->fd_nfiles ||
1514 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1519 * There are two cases of interest here.
1521 * For ENODEV simply dup (dfd) to file descriptor
1522 * (indx) and return.
1524 * For ENXIO steal away the file structure from (dfd) and
1525 * store it in (indx). (dfd) is effectively closed by
1528 * Any other error code is just returned.
1533 * Check that the mode the file is being opened for is a
1534 * subset of the mode of the existing descriptor.
1536 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1538 fp = fdp->fd_ofiles[indx];
1540 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1541 (void) munmapfd(p, indx);
1543 fdp->fd_ofiles[indx] = wfp;
1544 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1546 if (indx > fdp->fd_lastfile)
1547 fdp->fd_lastfile = indx;
1549 * we now own the reference to fp that the ofiles[] array
1550 * used to own. Release it.
1553 fdrop(fp, curthread);
1558 * Steal away the file pointer from dfd, and stuff it into indx.
1560 fp = fdp->fd_ofiles[indx];
1562 if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1563 (void) munmapfd(p, indx);
1565 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1566 fdp->fd_ofiles[dfd] = NULL;
1567 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1568 fdp->fd_ofileflags[dfd] = 0;
1571 * we now own the reference to fp that the ofiles[] array
1572 * used to own. Release it.
1575 fdrop(fp, curthread);
1577 * Complete the clean up of the filedesc structure by
1578 * recomputing the various hints.
1580 if (indx > fdp->fd_lastfile) {
1581 fdp->fd_lastfile = indx;
1583 while (fdp->fd_lastfile > 0 &&
1584 fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1587 if (dfd < fdp->fd_freefile)
1588 fdp->fd_freefile = dfd;
1599 struct filedesc_to_leader *
1600 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
1601 struct proc *leader)
1603 struct filedesc_to_leader *fdtol;
1605 MALLOC(fdtol, struct filedesc_to_leader *,
1606 sizeof(struct filedesc_to_leader),
1607 M_FILEDESC_TO_LEADER,
1609 fdtol->fdl_refcount = 1;
1610 fdtol->fdl_holdcount = 0;
1611 fdtol->fdl_wakeup = 0;
1612 fdtol->fdl_leader = leader;
1614 fdtol->fdl_next = old->fdl_next;
1615 fdtol->fdl_prev = old;
1616 old->fdl_next = fdtol;
1617 fdtol->fdl_next->fdl_prev = fdtol;
1619 fdtol->fdl_next = fdtol;
1620 fdtol->fdl_prev = fdtol;
1626 * Get file structures.
1629 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1636 * overestimate by 10 files
1638 return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1639 (nfiles + 10) * sizeof(struct file)));
1642 error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1647 * followed by an array of file structures
1649 LIST_FOREACH(fp, &filehead, f_list) {
1650 error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1657 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1658 0, 0, sysctl_kern_file, "S,file", "Entire file table");
1660 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1661 &maxfilesperproc, 0, "Maximum files allowed open per process");
1663 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1664 &maxfiles, 0, "Maximum number of files");
1666 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1667 &nfiles, 0, "System-wide number of open files");
1670 fildesc_drvinit(void *unused)
1674 for (fd = 0; fd < NUMFDESC; fd++)
1675 make_dev(&fildesc_cdevsw, fd,
1676 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
1677 make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
1678 make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
1679 make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
1682 struct fileops badfileops = {
1706 badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct thread *td)
1712 badfo_poll(struct file *fp, int events, struct ucred *cred, struct thread *td)
1718 badfo_kqfilter(struct file *fp, struct knote *kn)
1724 badfo_stat(struct file *fp, struct stat *sb, struct thread *td)
1730 badfo_close(struct file *fp, struct thread *td)
1735 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1736 fildesc_drvinit,NULL)