2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1989, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
72 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.19 2004/02/28 00:43:31 tegge Exp $
73 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.58 2006/05/19 07:33:45 dillon Exp $
76 #include "opt_compat.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
82 #include <sys/filedesc.h>
83 #include <sys/kernel.h>
84 #include <sys/sysctl.h>
85 #include <sys/vnode.h>
87 #include <sys/nlookup.h>
90 #include <sys/filio.h>
91 #include <sys/fcntl.h>
92 #include <sys/unistd.h>
93 #include <sys/resourcevar.h>
94 #include <sys/event.h>
95 #include <sys/kern_syscall.h>
96 #include <sys/kcore.h>
97 #include <sys/kinfo.h>
100 #include <vm/vm_extern.h>
102 #include <sys/thread2.h>
103 #include <sys/file2.h>
105 static void fdreserve (struct filedesc *fdp, int fd0, int incr);
106 static void funsetfd (struct filedesc *fdp, int fd);
108 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
109 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
110 "file desc to leader structures");
111 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
112 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
114 static d_open_t fdopen;
117 #define CDEV_MAJOR 22
118 static struct cdevsw fildesc_cdevsw = {
120 /* maj */ CDEV_MAJOR,
132 /* strategy */ nostrategy,
137 static int badfo_readwrite (struct file *fp, struct uio *uio,
138 struct ucred *cred, int flags);
139 static int badfo_ioctl (struct file *fp, u_long com, caddr_t data,
141 static int badfo_poll (struct file *fp, int events, struct ucred *cred);
142 static int badfo_kqfilter (struct file *fp, struct knote *kn);
143 static int badfo_stat (struct file *fp, struct stat *sb, struct ucred *cred);
144 static int badfo_close (struct file *fp);
145 static int badfo_shutdown (struct file *fp, int how);
148 * Descriptor management.
150 struct filelist filehead; /* head of list of open files */
151 int nfiles; /* actual number of open files */
155 * System calls on descriptors.
159 getdtablesize(struct getdtablesize_args *uap)
161 struct proc *p = curproc;
164 min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
169 * Duplicate a file descriptor to a particular value.
171 * note: keep in mind that a potential race condition exists when closing
172 * descriptors from a shared descriptor table (via rfork).
176 dup2(struct dup2_args *uap)
180 error = kern_dup(DUP_FIXED, uap->from, uap->to, uap->sysmsg_fds);
186 * Duplicate a file descriptor.
190 dup(struct dup_args *uap)
194 error = kern_dup(DUP_VARIABLE, uap->fd, 0, uap->sysmsg_fds);
200 kern_fcntl(int fd, int cmd, union fcntl_dat *dat, struct ucred *cred)
202 struct thread *td = curthread;
203 struct proc *p = td->td_proc;
204 struct filedesc *fdp = p->p_fd;
209 int tmp, error, flg = F_POSIX;
213 if ((unsigned)fd >= fdp->fd_nfiles ||
214 (fp = fdp->fd_files[fd].fp) == NULL)
216 pop = &fdp->fd_files[fd].fileflags;
221 if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
222 newmin > maxfilesperproc)
224 error = kern_dup(DUP_VARIABLE, fd, newmin, &dat->fc_fd);
228 dat->fc_cloexec = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
232 *pop = (*pop &~ UF_EXCLOSE) |
233 (dat->fc_cloexec & FD_CLOEXEC ? UF_EXCLOSE : 0);
237 dat->fc_flags = OFLAGS(fp->f_flag);
242 fp->f_flag &= ~FCNTLFLAGS;
243 fp->f_flag |= FFLAGS(dat->fc_flags & ~O_ACCMODE) & FCNTLFLAGS;
244 tmp = fp->f_flag & FNONBLOCK;
245 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, cred);
250 tmp = fp->f_flag & FASYNC;
251 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
256 fp->f_flag &= ~FNONBLOCK;
258 fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, cred);
264 error = fo_ioctl(fp, FIOGETOWN, (caddr_t)&dat->fc_owner, cred);
270 error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&dat->fc_owner, cred);
276 /* Fall into F_SETLK */
279 if (fp->f_type != DTYPE_VNODE)
281 vp = (struct vnode *)fp->f_data;
284 * copyin/lockop may block
287 if (dat->fc_flock.l_whence == SEEK_CUR)
288 dat->fc_flock.l_start += fp->f_offset;
290 switch (dat->fc_flock.l_type) {
292 if ((fp->f_flag & FREAD) == 0) {
296 p->p_leader->p_flag |= P_ADVLOCK;
297 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
298 &dat->fc_flock, flg);
301 if ((fp->f_flag & FWRITE) == 0) {
305 p->p_leader->p_flag |= P_ADVLOCK;
306 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
307 &dat->fc_flock, flg);
310 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
311 &dat->fc_flock, F_POSIX);
317 /* Check for race with close */
318 if ((unsigned) fd >= fdp->fd_nfiles ||
319 fp != fdp->fd_files[fd].fp) {
320 dat->fc_flock.l_whence = SEEK_SET;
321 dat->fc_flock.l_start = 0;
322 dat->fc_flock.l_len = 0;
323 dat->fc_flock.l_type = F_UNLCK;
324 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
325 F_UNLCK, &dat->fc_flock, F_POSIX);
331 if (fp->f_type != DTYPE_VNODE)
333 vp = (struct vnode *)fp->f_data;
335 * copyin/lockop may block
338 if (dat->fc_flock.l_type != F_RDLCK &&
339 dat->fc_flock.l_type != F_WRLCK &&
340 dat->fc_flock.l_type != F_UNLCK) {
344 if (dat->fc_flock.l_whence == SEEK_CUR)
345 dat->fc_flock.l_start += fp->f_offset;
346 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
347 &dat->fc_flock, F_POSIX);
357 * The file control system call.
360 fcntl(struct fcntl_args *uap)
367 dat.fc_fd = uap->arg;
370 dat.fc_cloexec = uap->arg;
373 dat.fc_flags = uap->arg;
376 dat.fc_owner = uap->arg;
381 error = copyin((caddr_t)uap->arg, &dat.fc_flock,
382 sizeof(struct flock));
388 error = kern_fcntl(uap->fd, uap->cmd, &dat, curproc->p_ucred);
393 uap->sysmsg_result = dat.fc_fd;
396 uap->sysmsg_result = dat.fc_cloexec;
399 uap->sysmsg_result = dat.fc_flags;
402 uap->sysmsg_result = dat.fc_owner;
404 error = copyout(&dat.fc_flock, (caddr_t)uap->arg,
405 sizeof(struct flock));
414 * Common code for dup, dup2, and fcntl(F_DUPFD).
416 * The type flag can be either DUP_FIXED or DUP_VARIABLE. DUP_FIXED tells
417 * kern_dup() to destructively dup over an existing file descriptor if new
418 * is already open. DUP_VARIABLE tells kern_dup() to find the lowest
419 * unused file descriptor that is greater than or equal to new.
422 kern_dup(enum dup_type type, int old, int new, int *res)
424 struct thread *td = curthread;
425 struct proc *p = td->td_proc;
426 struct filedesc *fdp = p->p_fd;
430 boolean_t fdalloced = FALSE;
434 * Verify that we have a valid descriptor to dup from and
435 * possibly to dup to.
437 if (old < 0 || new < 0 || new > p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
438 new >= maxfilesperproc)
440 if (old >= fdp->fd_nfiles || fdp->fd_files[old].fp == NULL)
442 if (type == DUP_FIXED && old == new) {
446 fp = fdp->fd_files[old].fp;
450 * Expand the table for the new descriptor if needed. This may
451 * block and drop and reacquire the fidedesc lock.
453 if (type == DUP_VARIABLE || new >= fdp->fd_nfiles) {
454 error = fdalloc(p, new, &newfd);
461 if (type == DUP_VARIABLE)
465 * If the old file changed out from under us then treat it as a
466 * bad file descriptor. Userland should do its own locking to
469 if (fdp->fd_files[old].fp != fp) {
470 if (fdp->fd_files[new].fp == NULL) {
472 fdreserve(fdp, newfd, -1);
473 if (new < fdp->fd_freefile)
474 fdp->fd_freefile = new;
475 while (fdp->fd_lastfile > 0 &&
476 fdp->fd_files[fdp->fd_lastfile].fp == NULL)
482 KASSERT(old != new, ("new fd is same as old"));
485 * Save info on the descriptor being overwritten. We have
486 * to do the unmap now, but we cannot close it without
487 * introducing an ownership race for the slot.
489 delfp = fdp->fd_files[new].fp;
490 if (delfp != NULL && p->p_fdtol != NULL) {
492 * Ask fdfree() to sleep to ensure that all relevant
493 * process leaders can be traversed in closef().
495 fdp->fd_holdleaderscount++;
499 KASSERT(delfp == NULL || type == DUP_FIXED,
500 ("dup() picked an open file"));
503 * Duplicate the source descriptor, update lastfile
505 if (new > fdp->fd_lastfile)
506 fdp->fd_lastfile = new;
507 if (!fdalloced && fdp->fd_files[new].fp == NULL)
508 fdreserve(fdp, new, 1);
509 fdp->fd_files[new].fp = fp;
510 fdp->fd_files[new].fileflags =
511 fdp->fd_files[old].fileflags & ~UF_EXCLOSE;
515 * If we dup'd over a valid file, we now own the reference to it
516 * and must dispose of it using closef() semantics (as if a
517 * close() were performed on it).
520 (void) closef(delfp, td);
522 fdp->fd_holdleaderscount--;
523 if (fdp->fd_holdleaderscount == 0 &&
524 fdp->fd_holdleaderswakeup != 0) {
525 fdp->fd_holdleaderswakeup = 0;
526 wakeup(&fdp->fd_holdleaderscount);
534 * If sigio is on the list associated with a process or process group,
535 * disable signalling from the device, remove sigio from the list and
539 funsetown(struct sigio *sigio)
544 *(sigio->sio_myref) = NULL;
546 if (sigio->sio_pgid < 0) {
547 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
549 } else /* if ((*sigiop)->sio_pgid > 0) */ {
550 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
553 crfree(sigio->sio_ucred);
554 free(sigio, M_SIGIO);
557 /* Free a list of sigio structures. */
559 funsetownlst(struct sigiolst *sigiolst)
563 while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
568 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
570 * After permission checking, add a sigio structure to the sigio list for
571 * the process or process group.
574 fsetown(pid_t pgid, struct sigio **sigiop)
590 * Policy - Don't allow a process to FSETOWN a process
591 * in another session.
593 * Remove this test to allow maximum flexibility or
594 * restrict FSETOWN to the current process or process
595 * group for maximum safety.
597 if (proc->p_session != curproc->p_session)
601 } else /* if (pgid < 0) */ {
602 pgrp = pgfind(-pgid);
607 * Policy - Don't allow a process to FSETOWN a process
608 * in another session.
610 * Remove this test to allow maximum flexibility or
611 * restrict FSETOWN to the current process or process
612 * group for maximum safety.
614 if (pgrp->pg_session != curproc->p_session)
620 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
622 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
623 sigio->sio_proc = proc;
625 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
626 sigio->sio_pgrp = pgrp;
628 sigio->sio_pgid = pgid;
629 sigio->sio_ucred = crhold(curproc->p_ucred);
630 /* It would be convenient if p_ruid was in ucred. */
631 sigio->sio_ruid = curproc->p_ucred->cr_ruid;
632 sigio->sio_myref = sigiop;
640 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
643 fgetown(struct sigio *sigio)
645 return (sigio != NULL ? sigio->sio_pgid : 0);
649 * Close many file descriptors.
654 closefrom(struct closefrom_args *uap)
656 return(kern_closefrom(uap->fd));
660 kern_closefrom(int fd)
662 struct thread *td = curthread;
663 struct proc *p = td->td_proc;
664 struct filedesc *fdp;
669 if (fd < 0 || fd > fdp->fd_lastfile)
673 if (kern_close(fdp->fd_lastfile) == EINTR)
675 } while (fdp->fd_lastfile > fd);
681 * Close a file descriptor.
686 close(struct close_args *uap)
688 return(kern_close(uap->fd));
694 struct thread *td = curthread;
695 struct proc *p = td->td_proc;
696 struct filedesc *fdp;
704 if ((unsigned)fd >= fdp->fd_nfiles ||
705 (fp = fdp->fd_files[fd].fp) == NULL)
709 if (p->p_fdtol != NULL) {
711 * Ask fdfree() to sleep to ensure that all relevant
712 * process leaders can be traversed in closef().
714 fdp->fd_holdleaderscount++;
719 * we now hold the fp reference that used to be owned by the descriptor
722 while (fdp->fd_lastfile > 0 && fdp->fd_files[fdp->fd_lastfile].fp == NULL)
724 if (fd < fdp->fd_knlistsize)
725 knote_fdclose(p, fd);
726 error = closef(fp, td);
728 fdp->fd_holdleaderscount--;
729 if (fdp->fd_holdleaderscount == 0 &&
730 fdp->fd_holdleaderswakeup != 0) {
731 fdp->fd_holdleaderswakeup = 0;
732 wakeup(&fdp->fd_holdleaderscount);
739 * shutdown_args(int fd, int how)
742 kern_shutdown(int fd, int how)
744 struct thread *td = curthread;
745 struct proc *p = td->td_proc;
746 struct filedesc *fdp;
753 if ((unsigned)fd >= fdp->fd_nfiles ||
754 (fp = fdp->fd_files[fd].fp) == NULL)
757 error = fo_shutdown(fp, how);
764 shutdown(struct shutdown_args *uap)
768 error = kern_shutdown(uap->s, uap->how);
774 kern_fstat(int fd, struct stat *ub)
776 struct thread *td = curthread;
777 struct proc *p = td->td_proc;
778 struct filedesc *fdp;
785 if ((unsigned)fd >= fdp->fd_nfiles ||
786 (fp = fdp->fd_files[fd].fp) == NULL)
789 error = fo_stat(fp, ub, p->p_ucred);
796 * Return status information about a file descriptor.
799 fstat(struct fstat_args *uap)
804 error = kern_fstat(uap->fd, &st);
807 error = copyout(&st, uap->sb, sizeof(st));
812 * Return pathconf information about a file descriptor.
816 fpathconf(struct fpathconf_args *uap)
818 struct thread *td = curthread;
819 struct proc *p = td->td_proc;
820 struct filedesc *fdp;
827 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
828 (fp = fdp->fd_files[uap->fd].fp) == NULL)
833 switch (fp->f_type) {
836 if (uap->name != _PC_PIPE_BUF) {
839 uap->sysmsg_result = PIPE_BUF;
845 vp = (struct vnode *)fp->f_data;
846 error = VOP_PATHCONF(vp, uap->name, uap->sysmsg_fds);
857 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
860 fdgrow(struct filedesc *fdp, int want)
862 struct fdnode *newfiles;
863 struct fdnode *oldfiles;
868 /* nf has to be of the form 2^n - 1 */
870 } while (nf <= want);
872 newfiles = malloc(nf * sizeof(struct fdnode), M_FILEDESC, M_WAITOK);
875 * deal with file-table extend race that might have occured
876 * when malloc was blocked.
878 if (fdp->fd_nfiles >= nf) {
879 free(newfiles, M_FILEDESC);
883 * Copy the existing ofile and ofileflags arrays
884 * and zero the new portion of each array.
886 extra = nf - fdp->fd_nfiles;
887 bcopy(fdp->fd_files, newfiles, fdp->fd_nfiles * sizeof(struct fdnode));
888 bzero(&newfiles[fdp->fd_nfiles], extra * sizeof(struct fdnode));
890 oldfiles = fdp->fd_files;
891 fdp->fd_files = newfiles;
894 if (oldfiles != fdp->fd_builtin_files)
895 free(oldfiles, M_FILEDESC);
900 * Number of nodes in right subtree, including the root.
903 right_subtree_size(int n)
905 return (n ^ (n | (n + 1)));
912 right_ancestor(int n)
914 return (n | (n + 1));
923 return ((n & (n + 1)) - 1);
928 fdreserve(struct filedesc *fdp, int fd, int incr)
931 fdp->fd_files[fd].allocated += incr;
932 KKASSERT(fdp->fd_files[fd].allocated >= 0);
933 fd = left_ancestor(fd);
938 * Allocate a file descriptor for the process.
941 fdalloc(struct proc *p, int want, int *result)
943 struct filedesc *fdp = p->p_fd;
944 int fd, rsize, rsum, node, lim;
946 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
949 if (want >= fdp->fd_nfiles)
953 * Search for a free descriptor starting at the higher
954 * of want or fd_freefile. If that fails, consider
955 * expanding the ofile array.
958 /* move up the tree looking for a subtree with a free node */
959 for (fd = max(want, fdp->fd_freefile); fd < min(fdp->fd_nfiles, lim);
960 fd = right_ancestor(fd)) {
961 if (fdp->fd_files[fd].allocated == 0)
964 rsize = right_subtree_size(fd);
965 if (fdp->fd_files[fd].allocated == rsize)
966 continue; /* right subtree full */
969 * Free fd is in the right subtree of the tree rooted at fd.
970 * Call that subtree R. Look for the smallest (leftmost)
971 * subtree of R with an unallocated fd: continue moving
972 * down the left branch until encountering a full left
973 * subtree, then move to the right.
975 for (rsum = 0, rsize /= 2; rsize > 0; rsize /= 2) {
977 rsum += fdp->fd_files[node].allocated;
978 if (fdp->fd_files[fd].allocated == rsum + rsize) {
979 fd = node; /* move to the right */
980 if (fdp->fd_files[node].allocated == 0)
989 * No space in current array. Expand?
991 if (fdp->fd_nfiles >= lim)
997 KKASSERT(fd < fdp->fd_nfiles);
998 fdp->fd_files[fd].fileflags = 0;
999 if (fd > fdp->fd_lastfile)
1000 fdp->fd_lastfile = fd;
1001 if (want <= fdp->fd_freefile)
1002 fdp->fd_freefile = fd;
1004 KKASSERT(fdp->fd_files[fd].fp == NULL);
1005 fdreserve(fdp, fd, 1);
1010 * Check to see whether n user file descriptors
1011 * are available to the process p.
1014 fdavail(struct proc *p, int n)
1016 struct filedesc *fdp = p->p_fd;
1017 struct fdnode *fdnode;
1020 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
1021 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1024 last = min(fdp->fd_nfiles, lim);
1025 fdnode = &fdp->fd_files[fdp->fd_freefile];
1026 for (i = last - fdp->fd_freefile; --i >= 0; ++fdnode) {
1027 if (fdnode->fp == NULL && --n <= 0)
1035 * Create a new open file structure and allocate a file decriptor
1036 * for the process that refers to it. If p is NULL, no descriptor
1037 * is allocated and the file pointer is returned unassociated with
1038 * any process. resultfd is only used if p is not NULL and may
1039 * separately be NULL indicating that you don't need the returned fd.
1041 * A held file pointer is returned. If a descriptor has been allocated
1042 * an additional hold on the fp will be made due to the fd_files[]
1046 falloc(struct proc *p, struct file **resultfp, int *resultfd)
1048 static struct timeval lastfail;
1056 * Handle filetable full issues and root overfill.
1058 if (nfiles >= maxfiles - maxfilesrootres &&
1059 ((p && p->p_ucred->cr_ruid != 0) || nfiles >= maxfiles)) {
1060 if (ppsratecheck(&lastfail, &curfail, 1)) {
1061 printf("kern.maxfiles limit exceeded by uid %d, please see tuning(7).\n",
1062 (p ? p->p_ucred->cr_ruid : -1));
1069 * Allocate a new file descriptor.
1072 fp = malloc(sizeof(struct file), M_FILE, M_WAITOK | M_ZERO);
1074 fp->f_ops = &badfileops;
1077 fp->f_cred = crhold(p->p_ucred);
1079 fp->f_cred = crhold(proc0.p_ucred);
1080 LIST_INSERT_HEAD(&filehead, fp, f_list);
1082 if ((error = fsetfd(p, fp, resultfd)) != 0) {
1095 * fdealloc - deallocate a descriptor allocated with falloc. The deallocation
1096 * only occurs if the passed file pointer matches the descriptor.
1099 fdealloc(struct proc *p, struct file *fp, int fd)
1101 struct filedesc *fdp = p->p_fd;
1103 if (((u_int)fd) >= fdp->fd_nfiles || fdp->fd_files[fd].fp != fp)
1105 fdp->fd_files[fd].fp = NULL;
1106 fdp->fd_files[fd].fileflags = 0;
1107 fdreserve(fdp, fd, -1);
1108 if (fd < fdp->fd_freefile)
1109 fdp->fd_freefile = fd;
1115 * Associate a file pointer with a file descriptor. On success the fp
1116 * will have an additional ref representing the fd_files[] association.
1119 fsetfd(struct proc *p, struct file *fp, int *resultfd)
1124 if ((error = fdalloc(p, 0, &fd)) == 0) {
1126 p->p_fd->fd_files[fd].fp = fp;
1134 funsetfd(struct filedesc *fdp, int fd)
1136 fdp->fd_files[fd].fp = NULL;
1137 fdp->fd_files[fd].fileflags = 0;
1138 fdreserve(fdp, fd, -1);
1139 if (fd < fdp->fd_freefile)
1140 fdp->fd_freefile = fd;
1144 fsetfdflags(struct filedesc *fdp, int fd, int add_flags)
1146 if (((u_int)fd) >= fdp->fd_nfiles)
1148 if (fdp->fd_files[fd].fp == NULL)
1150 fdp->fd_files[fd].fileflags |= add_flags;
1155 fclrfdflags(struct filedesc *fdp, int fd, int rem_flags)
1157 if (((u_int)fd) >= fdp->fd_nfiles)
1159 if (fdp->fd_files[fd].fp == NULL)
1161 fdp->fd_files[fd].fileflags &= ~rem_flags;
1166 fsetcred(struct file *fp, struct ucred *cr)
1174 * Free a file descriptor.
1177 ffree(struct file *fp)
1179 KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
1180 LIST_REMOVE(fp, f_list);
1183 cache_drop(fp->f_ncp);
1191 * called from init_main, initialize filedesc0 for proc0.
1194 fdinit_bootstrap(struct proc *p0, struct filedesc *fdp0, int cmask)
1198 fdp0->fd_refcnt = 1;
1199 fdp0->fd_cmask = cmask;
1200 fdp0->fd_files = fdp0->fd_builtin_files;
1201 fdp0->fd_nfiles = NDFILE;
1205 * Build a new filedesc structure.
1208 fdinit(struct proc *p)
1210 struct filedesc *newfdp;
1211 struct filedesc *fdp = p->p_fd;
1213 newfdp = malloc(sizeof(struct filedesc), M_FILEDESC, M_WAITOK|M_ZERO);
1215 newfdp->fd_cdir = fdp->fd_cdir;
1216 vref(newfdp->fd_cdir);
1217 newfdp->fd_ncdir = cache_hold(fdp->fd_ncdir);
1221 * rdir may not be set in e.g. proc0 or anything vm_fork'd off of
1222 * proc0, but should unconditionally exist in other processes.
1225 newfdp->fd_rdir = fdp->fd_rdir;
1226 vref(newfdp->fd_rdir);
1227 newfdp->fd_nrdir = cache_hold(fdp->fd_nrdir);
1230 newfdp->fd_jdir = fdp->fd_jdir;
1231 vref(newfdp->fd_jdir);
1232 newfdp->fd_njdir = cache_hold(fdp->fd_njdir);
1235 /* Create the file descriptor table. */
1236 newfdp->fd_refcnt = 1;
1237 newfdp->fd_cmask = cmask;
1238 newfdp->fd_files = newfdp->fd_builtin_files;
1239 newfdp->fd_nfiles = NDFILE;
1240 newfdp->fd_knlistsize = -1;
1246 * Share a filedesc structure.
1249 fdshare(struct proc *p)
1251 p->p_fd->fd_refcnt++;
1256 * Copy a filedesc structure.
1259 fdcopy(struct proc *p)
1261 struct filedesc *newfdp, *fdp = p->p_fd;
1262 struct fdnode *fdnode;
1265 /* Certain daemons might not have file descriptors. */
1269 newfdp = malloc(sizeof(struct filedesc), M_FILEDESC, M_WAITOK);
1271 if (newfdp->fd_cdir) {
1272 vref(newfdp->fd_cdir);
1273 newfdp->fd_ncdir = cache_hold(newfdp->fd_ncdir);
1276 * We must check for fd_rdir here, at least for now because
1277 * the init process is created before we have access to the
1278 * rootvode to take a reference to it.
1280 if (newfdp->fd_rdir) {
1281 vref(newfdp->fd_rdir);
1282 newfdp->fd_nrdir = cache_hold(newfdp->fd_nrdir);
1284 if (newfdp->fd_jdir) {
1285 vref(newfdp->fd_jdir);
1286 newfdp->fd_njdir = cache_hold(newfdp->fd_njdir);
1288 newfdp->fd_refcnt = 1;
1291 * If the number of open files fits in the internal arrays
1292 * of the open file structure, use them, otherwise allocate
1293 * additional memory for the number of descriptors currently
1296 if (newfdp->fd_lastfile < NDFILE) {
1297 newfdp->fd_files = newfdp->fd_builtin_files;
1301 * Compute the smallest file table size
1302 * for the file descriptors currently in use,
1303 * allowing the table to shrink.
1305 i = newfdp->fd_nfiles;
1306 while ((i-1)/2 > newfdp->fd_lastfile && (i-1)/2 > NDFILE)
1308 newfdp->fd_files = malloc(i * sizeof(struct fdnode),
1309 M_FILEDESC, M_WAITOK);
1311 newfdp->fd_nfiles = i;
1313 if (fdp->fd_files != fdp->fd_builtin_files ||
1314 newfdp->fd_files != newfdp->fd_builtin_files
1316 bcopy(fdp->fd_files, newfdp->fd_files,
1317 i * sizeof(struct fdnode));
1321 * kq descriptors cannot be copied.
1323 if (newfdp->fd_knlistsize != -1) {
1324 fdnode = &newfdp->fd_files[newfdp->fd_lastfile];
1325 for (i = newfdp->fd_lastfile; i >= 0; i--, fdnode--) {
1326 if (fdnode->fp != NULL && fdnode->fp->f_type == DTYPE_KQUEUE)
1327 funsetfd(newfdp, i); /* nulls out *fpp */
1328 if (fdnode->fp == NULL && i == newfdp->fd_lastfile && i > 0)
1329 newfdp->fd_lastfile--;
1331 newfdp->fd_knlist = NULL;
1332 newfdp->fd_knlistsize = -1;
1333 newfdp->fd_knhash = NULL;
1334 newfdp->fd_knhashmask = 0;
1337 fdnode = newfdp->fd_files;
1338 for (i = newfdp->fd_lastfile; i-- >= 0; fdnode++) {
1339 if (fdnode->fp != NULL)
1346 * Release a filedesc structure.
1349 fdfree(struct proc *p)
1351 struct thread *td = p->p_thread;
1352 struct filedesc *fdp = p->p_fd;
1353 struct fdnode *fdnode;
1355 struct filedesc_to_leader *fdtol;
1360 /* Certain daemons might not have file descriptors. */
1364 /* Check for special need to clear POSIX style locks */
1366 if (fdtol != NULL) {
1367 KASSERT(fdtol->fdl_refcount > 0,
1368 ("filedesc_to_refcount botch: fdl_refcount=%d",
1369 fdtol->fdl_refcount));
1370 if (fdtol->fdl_refcount == 1 &&
1371 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1373 fdnode = fdp->fd_files;
1374 for (i = 0; i <= fdp->fd_lastfile; i++, fdnode++) {
1375 if (fdnode->fp == NULL ||
1376 fdnode->fp->f_type != DTYPE_VNODE)
1380 lf.l_whence = SEEK_SET;
1383 lf.l_type = F_UNLCK;
1384 vp = (struct vnode *)fp->f_data;
1385 (void) VOP_ADVLOCK(vp,
1386 (caddr_t)p->p_leader,
1391 /* reload due to possible reallocation */
1392 fdnode = &fdp->fd_files[i];
1396 if (fdtol->fdl_refcount == 1) {
1397 if (fdp->fd_holdleaderscount > 0 &&
1398 (p->p_leader->p_flag & P_ADVLOCK) != 0) {
1400 * close() or do_dup() has cleared a reference
1401 * in a shared file descriptor table.
1403 fdp->fd_holdleaderswakeup = 1;
1404 tsleep(&fdp->fd_holdleaderscount,
1408 if (fdtol->fdl_holdcount > 0) {
1410 * Ensure that fdtol->fdl_leader
1411 * remains valid in closef().
1413 fdtol->fdl_wakeup = 1;
1414 tsleep(fdtol, 0, "fdlhold", 0);
1418 fdtol->fdl_refcount--;
1419 if (fdtol->fdl_refcount == 0 &&
1420 fdtol->fdl_holdcount == 0) {
1421 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1422 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1427 free(fdtol, M_FILEDESC_TO_LEADER);
1429 if (--fdp->fd_refcnt > 0)
1432 * we are the last reference to the structure, we can
1433 * safely assume it will not change out from under us.
1435 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1436 if (fdp->fd_files[i].fp)
1437 closef(fdp->fd_files[i].fp, td);
1439 if (fdp->fd_files != fdp->fd_builtin_files)
1440 free(fdp->fd_files, M_FILEDESC);
1442 cache_drop(fdp->fd_ncdir);
1443 vrele(fdp->fd_cdir);
1446 cache_drop(fdp->fd_nrdir);
1447 vrele(fdp->fd_rdir);
1450 cache_drop(fdp->fd_njdir);
1451 vrele(fdp->fd_jdir);
1454 free(fdp->fd_knlist, M_KQUEUE);
1456 free(fdp->fd_knhash, M_KQUEUE);
1457 free(fdp, M_FILEDESC);
1461 * Retrieve and reference the file pointer associated with a descriptor.
1464 holdfp(struct filedesc *fdp, int fd, int flag)
1468 if (((u_int)fd) >= fdp->fd_nfiles)
1470 if ((fp = fdp->fd_files[fd].fp) == NULL)
1472 if ((fp->f_flag & flag) == 0 && flag != -1)
1479 * holdsock() - load the struct file pointer associated
1480 * with a socket into *fpp. If an error occurs, non-zero
1481 * will be returned and *fpp will be set to NULL.
1484 holdsock(struct filedesc *fdp, int fdes, struct file **fpp)
1490 if ((unsigned)fdes >= fdp->fd_nfiles)
1492 if ((fp = fdp->fd_files[fdes].fp) == NULL)
1494 if (fp->f_type != DTYPE_SOCKET)
1502 * Convert a user file descriptor to a kernel file entry.
1505 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
1509 if ((u_int)fd >= fdp->fd_nfiles ||
1510 (fp = fdp->fd_files[fd].fp) == NULL)
1512 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO)
1519 * For setugid programs, we don't want to people to use that setugidness
1520 * to generate error messages which write to a file which otherwise would
1521 * otherwise be off-limits to the process.
1523 * This is a gross hack to plug the hole. A better solution would involve
1524 * a special vop or other form of generalized access control mechanism. We
1525 * go ahead and just reject all procfs file systems accesses as dangerous.
1527 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1528 * sufficient. We also don't for check setugidness since we know we are.
1531 is_unsafe(struct file *fp)
1533 if (fp->f_type == DTYPE_VNODE &&
1534 ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1540 * Make this setguid thing safe, if at all possible.
1543 setugidsafety(struct proc *p)
1545 struct thread *td = p->p_thread;
1546 struct filedesc *fdp = p->p_fd;
1549 /* Certain daemons might not have file descriptors. */
1554 * note: fdp->fd_files may be reallocated out from under us while
1555 * we are blocked in a close. Be careful!
1557 for (i = 0; i <= fdp->fd_lastfile; i++) {
1560 if (fdp->fd_files[i].fp && is_unsafe(fdp->fd_files[i].fp)) {
1563 if (i < fdp->fd_knlistsize)
1564 knote_fdclose(p, i);
1566 * NULL-out descriptor prior to close to avoid
1567 * a race while close blocks.
1569 fp = fdp->fd_files[i].fp;
1574 while (fdp->fd_lastfile > 0 && fdp->fd_files[fdp->fd_lastfile].fp == NULL)
1579 * Close any files on exec?
1582 fdcloseexec(struct proc *p)
1584 struct thread *td = p->p_thread;
1585 struct filedesc *fdp = p->p_fd;
1588 /* Certain daemons might not have file descriptors. */
1593 * We cannot cache fd_files since operations may block and rip
1594 * them out from under us.
1596 for (i = 0; i <= fdp->fd_lastfile; i++) {
1597 if (fdp->fd_files[i].fp != NULL &&
1598 (fdp->fd_files[i].fileflags & UF_EXCLOSE)) {
1601 if (i < fdp->fd_knlistsize)
1602 knote_fdclose(p, i);
1604 * NULL-out descriptor prior to close to avoid
1605 * a race while close blocks.
1607 fp = fdp->fd_files[i].fp;
1612 while (fdp->fd_lastfile > 0 && fdp->fd_files[fdp->fd_lastfile].fp == NULL)
1617 * It is unsafe for set[ug]id processes to be started with file
1618 * descriptors 0..2 closed, as these descriptors are given implicit
1619 * significance in the Standard C library. fdcheckstd() will create a
1620 * descriptor referencing /dev/null for each of stdin, stdout, and
1621 * stderr that is not already open.
1624 fdcheckstd(struct proc *p)
1626 struct nlookupdata nd;
1627 struct filedesc *fdp;
1630 int fd, i, error, flags, devnull;
1637 for (i = 0; i < 3; i++) {
1638 if (fdp->fd_files[i].fp != NULL)
1641 if ((error = falloc(p, &fp, NULL)) != 0)
1644 error = nlookup_init(&nd, "/dev/null", UIO_SYSSPACE,
1645 NLC_FOLLOW|NLC_LOCKVP);
1646 flags = FREAD | FWRITE;
1648 error = vn_open(&nd, fp, flags, 0);
1650 error = fsetfd(p, fp, &fd);
1658 error = kern_dup(DUP_FIXED, devnull, i, &retval);
1667 * Internal form of close.
1668 * Decrement reference count on file structure.
1669 * Note: td and/or p may be NULL when closing a file
1670 * that was being passed in a message.
1673 closef(struct file *fp, struct thread *td)
1677 struct filedesc_to_leader *fdtol;
1684 p = NULL; /* allow no proc association */
1686 p = td->td_proc; /* can also be NULL */
1689 * POSIX record locking dictates that any close releases ALL
1690 * locks owned by this process. This is handled by setting
1691 * a flag in the unlock to free ONLY locks obeying POSIX
1692 * semantics, and not to free BSD-style file locks.
1693 * If the descriptor was in a message, POSIX-style locks
1694 * aren't passed with the descriptor.
1697 fp->f_type == DTYPE_VNODE) {
1698 if ((p->p_leader->p_flag & P_ADVLOCK) != 0) {
1699 lf.l_whence = SEEK_SET;
1702 lf.l_type = F_UNLCK;
1703 vp = (struct vnode *)fp->f_data;
1704 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
1708 if (fdtol != NULL) {
1710 * Handle special case where file descriptor table
1711 * is shared between multiple process leaders.
1713 for (fdtol = fdtol->fdl_next;
1714 fdtol != p->p_fdtol;
1715 fdtol = fdtol->fdl_next) {
1716 if ((fdtol->fdl_leader->p_flag &
1719 fdtol->fdl_holdcount++;
1720 lf.l_whence = SEEK_SET;
1723 lf.l_type = F_UNLCK;
1724 vp = (struct vnode *)fp->f_data;
1725 (void) VOP_ADVLOCK(vp,
1726 (caddr_t)fdtol->fdl_leader,
1727 F_UNLCK, &lf, F_POSIX);
1728 fdtol->fdl_holdcount--;
1729 if (fdtol->fdl_holdcount == 0 &&
1730 fdtol->fdl_wakeup != 0) {
1731 fdtol->fdl_wakeup = 0;
1741 fdrop(struct file *fp)
1747 if (--fp->f_count > 0)
1749 if (fp->f_count < 0)
1750 panic("fdrop: count < 0");
1751 if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1752 lf.l_whence = SEEK_SET;
1755 lf.l_type = F_UNLCK;
1756 vp = (struct vnode *)fp->f_data;
1757 (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0);
1759 if (fp->f_ops != &badfileops)
1760 error = fo_close(fp);
1768 * Apply an advisory lock on a file descriptor.
1770 * Just attempt to get a record lock of the requested type on
1771 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1775 flock(struct flock_args *uap)
1777 struct proc *p = curproc;
1778 struct filedesc *fdp = p->p_fd;
1783 if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1784 (fp = fdp->fd_files[uap->fd].fp) == NULL)
1786 if (fp->f_type != DTYPE_VNODE)
1787 return (EOPNOTSUPP);
1788 vp = (struct vnode *)fp->f_data;
1789 lf.l_whence = SEEK_SET;
1792 if (uap->how & LOCK_UN) {
1793 lf.l_type = F_UNLCK;
1794 fp->f_flag &= ~FHASLOCK;
1795 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, 0));
1797 if (uap->how & LOCK_EX)
1798 lf.l_type = F_WRLCK;
1799 else if (uap->how & LOCK_SH)
1800 lf.l_type = F_RDLCK;
1803 fp->f_flag |= FHASLOCK;
1804 if (uap->how & LOCK_NB)
1805 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 0));
1806 return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_WAIT));
1810 * File Descriptor pseudo-device driver (/dev/fd/).
1812 * Opening minor device N dup()s the file (if any) connected to file
1813 * descriptor N belonging to the calling process. Note that this driver
1814 * consists of only the ``open()'' routine, because all subsequent
1815 * references to this file will be direct to the other driver.
1819 fdopen(dev_t dev, int mode, int type, struct thread *td)
1821 KKASSERT(td->td_lwp != NULL);
1824 * XXX Kludge: set curlwp->lwp_dupfd to contain the value of the
1825 * the file descriptor being sought for duplication. The error
1826 * return ensures that the vnode for this device will be released
1827 * by vn_open. Open will detect this special error and take the
1828 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1829 * will simply report the error.
1831 td->td_lwp->lwp_dupfd = minor(dev);
1836 * Duplicate the specified descriptor to a free descriptor.
1839 dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, int error)
1845 * If the to-be-dup'd fd number is greater than the allowed number
1846 * of file descriptors, or the fd to be dup'd has already been
1847 * closed, then reject.
1849 if ((u_int)dfd >= fdp->fd_nfiles ||
1850 (wfp = fdp->fd_files[dfd].fp) == NULL) {
1855 * There are two cases of interest here.
1857 * For ENODEV simply dup (dfd) to file descriptor
1858 * (indx) and return.
1860 * For ENXIO steal away the file structure from (dfd) and
1861 * store it in (indx). (dfd) is effectively closed by
1864 * Any other error code is just returned.
1869 * Check that the mode the file is being opened for is a
1870 * subset of the mode of the existing descriptor.
1872 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1874 fp = fdp->fd_files[indx].fp;
1875 fdp->fd_files[indx].fp = wfp;
1876 fdp->fd_files[indx].fileflags = fdp->fd_files[dfd].fileflags;
1878 if (indx > fdp->fd_lastfile)
1879 fdp->fd_lastfile = indx;
1881 * we now own the reference to fp that the ofiles[] array
1882 * used to own. Release it.
1890 * Steal away the file pointer from dfd, and stuff it into indx.
1892 fp = fdp->fd_files[indx].fp;
1893 fdp->fd_files[indx].fp = fdp->fd_files[dfd].fp;
1894 fdp->fd_files[indx].fileflags = fdp->fd_files[dfd].fileflags;
1898 * we now own the reference to fp that the files[] array
1899 * used to own. Release it.
1904 * Complete the clean up of the filedesc structure by
1905 * recomputing the various hints.
1907 if (indx > fdp->fd_lastfile) {
1908 fdp->fd_lastfile = indx;
1910 while (fdp->fd_lastfile > 0 &&
1911 fdp->fd_files[fdp->fd_lastfile].fp == NULL) {
1924 struct filedesc_to_leader *
1925 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
1926 struct proc *leader)
1928 struct filedesc_to_leader *fdtol;
1930 fdtol = malloc(sizeof(struct filedesc_to_leader),
1931 M_FILEDESC_TO_LEADER, M_WAITOK);
1932 fdtol->fdl_refcount = 1;
1933 fdtol->fdl_holdcount = 0;
1934 fdtol->fdl_wakeup = 0;
1935 fdtol->fdl_leader = leader;
1937 fdtol->fdl_next = old->fdl_next;
1938 fdtol->fdl_prev = old;
1939 old->fdl_next = fdtol;
1940 fdtol->fdl_next->fdl_prev = fdtol;
1942 fdtol->fdl_next = fdtol;
1943 fdtol->fdl_prev = fdtol;
1949 * Get file structures.
1952 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1954 struct kinfo_file kf;
1955 struct filedesc *fdp;
1964 * Note: because the number of file descriptors is calculated
1965 * in different ways for sizing vs returning the data,
1966 * there is information leakage from the first loop. However,
1967 * it is of a similar order of magnitude to the leakage from
1968 * global system statistics such as kern.openfiles.
1970 * When just doing a count, note that we cannot just count
1971 * the elements and add f_count via the filehead list because
1972 * threaded processes share their descriptor table and f_count might
1973 * still be '1' in that case.
1975 * Since the SYSCTL op can block, we must hold the process to
1976 * prevent it being ripped out from under us either in the
1977 * file descriptor loop or in the greater LIST_FOREACH. The
1978 * process may be in varying states of disrepair. If the process
1979 * is in SZOMB we may have caught it just as it is being removed
1980 * from the allproc list, we must skip it in that case to maintain
1981 * an unbroken chain through the allproc list.
1985 LIST_FOREACH(p, &allproc, p_list) {
1986 if (p->p_stat == SIDL || (p->p_flag & P_ZOMBIE))
1988 if (!PRISON_CHECK(req->td->td_proc->p_ucred, p->p_ucred) != 0)
1990 if ((fdp = p->p_fd) == NULL)
1993 for (n = 0; n < fdp->fd_nfiles; ++n) {
1994 if ((fp = fdp->fd_files[n].fp) == NULL)
1996 if (req->oldptr == NULL) {
1999 uid = p->p_ucred ? p->p_ucred->cr_uid : -1;
2000 kcore_make_file(&kf, fp, p->p_pid, uid, n);
2001 error = SYSCTL_OUT(req, &kf, sizeof(kf));
2012 * When just calculating the size, overestimate a bit to try to
2013 * prevent system activity from causing the buffer-fill call
2016 if (req->oldptr == NULL) {
2017 count = (count + 16) + (count / 10);
2018 error = SYSCTL_OUT(req, NULL, count * sizeof(kf));
2023 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2024 0, 0, sysctl_kern_file, "S,file", "Entire file table");
2026 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2027 &maxfilesperproc, 0, "Maximum files allowed open per process");
2029 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2030 &maxfiles, 0, "Maximum number of files");
2032 SYSCTL_INT(_kern, OID_AUTO, maxfilesrootres, CTLFLAG_RW,
2033 &maxfilesrootres, 0, "Descriptors reserved for root use");
2035 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2036 &nfiles, 0, "System-wide number of open files");
2039 fildesc_drvinit(void *unused)
2043 cdevsw_add(&fildesc_cdevsw, 0, 0);
2044 for (fd = 0; fd < NUMFDESC; fd++) {
2045 make_dev(&fildesc_cdevsw, fd,
2046 UID_BIN, GID_BIN, 0666, "fd/%d", fd);
2048 make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "stdin");
2049 make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "stdout");
2050 make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "stderr");
2053 struct fileops badfileops = {
2077 badfo_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *cred)
2083 badfo_poll(struct file *fp, int events, struct ucred *cred)
2089 badfo_kqfilter(struct file *fp, struct knote *kn)
2095 badfo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
2101 badfo_close(struct file *fp)
2107 badfo_shutdown(struct file *fp, int how)
2113 nofo_shutdown(struct file *fp, int how)
2115 return (EOPNOTSUPP);
2118 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2119 fildesc_drvinit,NULL)