summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtur Grabowski <art@cvs.openbsd.org>2001-10-26 12:03:29 +0000
committerArtur Grabowski <art@cvs.openbsd.org>2001-10-26 12:03:29 +0000
commiteb46b9853439c27012a22a86f3d9139930a9e87a (patch)
tree0668f2d444b8c90d902fc4d94aaf484b512bab93
parent322ae2c6b01056eccf34722ee78713e1b3337123 (diff)
- every new fd created by falloc() is marked as larval and should not be used
any anyone. Every caller of falloc matures the fd when it's usable. - Since every lookup in the fd table must now check this flag and all of them do the same thing, move all the necessary checks into a function - fd_getfile.
-rw-r--r--sys/compat/common/vfs_syscalls_43.c5
-rw-r--r--sys/compat/freebsd/freebsd_file.c5
-rw-r--r--sys/compat/hpux/hpux_compat.c5
-rw-r--r--sys/compat/hpux/hpux_file.c5
-rw-r--r--sys/compat/hpux/hpux_tty.c5
-rw-r--r--sys/compat/ibcs2/ibcs2_ioctl.c10
-rw-r--r--sys/compat/linux/linux_blkio.c14
-rw-r--r--sys/compat/linux/linux_cdrom.c5
-rw-r--r--sys/compat/linux/linux_fdio.c14
-rw-r--r--sys/compat/linux/linux_file.c7
-rw-r--r--sys/compat/linux/linux_hdio.c14
-rw-r--r--sys/compat/linux/linux_termios.c5
-rw-r--r--sys/compat/netbsd/netbsd_stat.c5
-rw-r--r--sys/compat/osf1/osf1_descrip.c7
-rw-r--r--sys/compat/ossaudio/ossaudio.c11
-rw-r--r--sys/compat/sunos/sunos_ioctl.c5
-rw-r--r--sys/compat/sunos/sunos_misc.c7
-rw-r--r--sys/compat/svr4/svr4_fcntl.c4
-rw-r--r--sys/compat/svr4/svr4_ioctl.c5
-rw-r--r--sys/compat/svr4/svr4_net.c3
-rw-r--r--sys/compat/svr4/svr4_stream.c14
-rw-r--r--sys/compat/ultrix/ultrix_ioctl.c5
-rw-r--r--sys/compat/ultrix/ultrix_misc.c5
-rw-r--r--sys/crypto/cryptodev.c3
-rw-r--r--sys/kern/exec_script.c3
-rw-r--r--sys/kern/kern_descrip.c69
-rw-r--r--sys/kern/kern_event.c11
-rw-r--r--sys/kern/kern_exec.c12
-rw-r--r--sys/kern/sys_generic.c53
-rw-r--r--sys/kern/sys_pipe.c4
-rw-r--r--sys/kern/uipc_syscalls.c9
-rw-r--r--sys/kern/uipc_usrreq.c5
-rw-r--r--sys/kern/vfs_syscalls.c60
-rw-r--r--sys/miscfs/fdesc/fdesc_vnops.c4
-rw-r--r--sys/sys/file.h13
-rw-r--r--sys/sys/filedesc.h4
-rw-r--r--sys/uvm/uvm_mmap.c7
-rw-r--r--sys/xfs/xfs_vfsops-bsd.c3
38 files changed, 165 insertions, 260 deletions
diff --git a/sys/compat/common/vfs_syscalls_43.c b/sys/compat/common/vfs_syscalls_43.c
index 91e03cf4076..0edb4c3a51f 100644
--- a/sys/compat/common/vfs_syscalls_43.c
+++ b/sys/compat/common/vfs_syscalls_43.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vfs_syscalls_43.c,v 1.14 2001/05/14 13:28:22 art Exp $ */
+/* $OpenBSD: vfs_syscalls_43.c,v 1.15 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: vfs_syscalls_43.c,v 1.4 1996/03/14 19:31:52 christos Exp $ */
/*
@@ -196,8 +196,7 @@ compat_43_sys_fstat(p, v, retval)
struct ostat oub;
int error;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
error = (*fp->f_ops->fo_stat)(fp, &ub, p);
cvtstat(&ub, &oub);
diff --git a/sys/compat/freebsd/freebsd_file.c b/sys/compat/freebsd/freebsd_file.c
index 4a58a9c9b0e..4ef1e246661 100644
--- a/sys/compat/freebsd/freebsd_file.c
+++ b/sys/compat/freebsd/freebsd_file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: freebsd_file.c,v 1.10 2001/02/03 02:45:31 mickey Exp $ */
+/* $OpenBSD: freebsd_file.c,v 1.11 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: freebsd_file.c,v 1.3 1996/05/03 17:03:09 christos Exp $ */
/*
@@ -808,8 +808,7 @@ freebsd_sys_fcntl(p, v, retval)
case F_SETOWN:
/* Our pipes does not understand F_[GS]ETOWN. */
fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
if (fp->f_type == DTYPE_PIPE)
return ((*fp->f_ops->fo_ioctl)(fp,
diff --git a/sys/compat/hpux/hpux_compat.c b/sys/compat/hpux/hpux_compat.c
index 69ffa2435d7..6eb27f95ed4 100644
--- a/sys/compat/hpux/hpux_compat.c
+++ b/sys/compat/hpux/hpux_compat.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hpux_compat.c,v 1.13 2001/10/10 23:43:44 art Exp $ */
+/* $OpenBSD: hpux_compat.c,v 1.14 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: hpux_compat.c,v 1.35 1997/05/08 16:19:48 mycroft Exp $ */
/*
@@ -826,8 +826,7 @@ hpux_sys_ioctl(p, v, retval)
if (com == HPUXTIOCGETP || com == HPUXTIOCSETP)
return (getsettty(p, SCARG(uap, fd), com, SCARG(uap, data)));
- if (((unsigned)SCARG(uap, fd)) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd)) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD|FWRITE)) == 0)
return (EBADF);
diff --git a/sys/compat/hpux/hpux_file.c b/sys/compat/hpux/hpux_file.c
index 09740e2cb32..05097caa7f7 100644
--- a/sys/compat/hpux/hpux_file.c
+++ b/sys/compat/hpux/hpux_file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hpux_file.c,v 1.7 2000/09/07 17:52:22 ericj Exp $ */
+/* $OpenBSD: hpux_file.c,v 1.8 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: hpux_file.c,v 1.5 1997/04/27 21:40:48 thorpej Exp $ */
/*
@@ -219,8 +219,7 @@ hpux_sys_fcntl(p, v, retval)
struct vnode *vp;
struct sys_fcntl_args fa;
- if ((u_int)SCARG(uap, fd) > p->p_fd->fd_nfiles ||
- (fp = p->p_fd->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd)) == NULL)
return (EBADF);
pop = &p->p_fd->fd_ofileflags[SCARG(uap, fd)];
diff --git a/sys/compat/hpux/hpux_tty.c b/sys/compat/hpux/hpux_tty.c
index 29f723af781..4dd401ab892 100644
--- a/sys/compat/hpux/hpux_tty.c
+++ b/sys/compat/hpux/hpux_tty.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hpux_tty.c,v 1.4 1997/04/16 09:18:17 downsj Exp $ */
+/* $OpenBSD: hpux_tty.c,v 1.5 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: hpux_tty.c,v 1.14 1997/04/01 19:59:05 scottr Exp $ */
/*
@@ -518,8 +518,7 @@ getsettty(p, fdes, com, cmarg)
struct sgttyb sb;
int error;
- if (((unsigned)fdes) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fdes]) == NULL)
+ if ((fp = fd_getfile(fdp, fdes)) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD|FWRITE)) == 0)
return (EBADF);
diff --git a/sys/compat/ibcs2/ibcs2_ioctl.c b/sys/compat/ibcs2/ibcs2_ioctl.c
index c6adf20311a..f4c7291e1be 100644
--- a/sys/compat/ibcs2/ibcs2_ioctl.c
+++ b/sys/compat/ibcs2/ibcs2_ioctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ibcs2_ioctl.c,v 1.8 1998/02/19 02:08:45 deraadt Exp $ */
+/* $OpenBSD: ibcs2_ioctl.c,v 1.9 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: ibcs2_ioctl.c,v 1.12 1996/08/10 09:08:26 mycroft Exp $ */
/*
@@ -345,12 +345,8 @@ ibcs2_sys_ioctl(p, v, retval)
int (*ctl) __P((struct file *, u_long, caddr_t, struct proc *));
int error;
- if (SCARG(uap, fd) < 0 || SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) {
- DPRINTF(("ibcs2_ioctl(%d): bad fd %d ", p->p_pid,
- SCARG(uap, fd)));
- return EBADF;
- }
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
+ return (EBADF);
if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
DPRINTF(("ibcs2_ioctl(%d): bad fp flag ", p->p_pid));
diff --git a/sys/compat/linux/linux_blkio.c b/sys/compat/linux/linux_blkio.c
index 41e9e7c8817..b09dc192aa9 100644
--- a/sys/compat/linux/linux_blkio.c
+++ b/sys/compat/linux/linux_blkio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: linux_blkio.c,v 1.1 2001/04/09 06:53:44 tholo Exp $ */
+/* $OpenBSD: linux_blkio.c,v 1.2 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: linux_blkio.c,v 1.3 2001/01/18 17:48:04 tv Exp $ */
/*
@@ -69,18 +69,8 @@ linux_ioctl_blkio(struct proc *p, struct linux_sys_ioctl_args *uap,
struct disklabel label;
fdp = p->p_fd;
-#if 1
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
-#else
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
- (fp->f_iflags & FIF_WANTCLOSE) != 0)
- return (EBADF);
-
- FILE_USE(fp);
-#endif
error = 0;
ioctlf = fp->f_ops->fo_ioctl;
com = SCARG(uap, com);
diff --git a/sys/compat/linux/linux_cdrom.c b/sys/compat/linux/linux_cdrom.c
index 365a198e879..cbcb0256082 100644
--- a/sys/compat/linux/linux_cdrom.c
+++ b/sys/compat/linux/linux_cdrom.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: linux_cdrom.c,v 1.5 2001/05/24 06:00:08 jasoni Exp $ */
+/* $OpenBSD: linux_cdrom.c,v 1.6 2001/10/26 12:03:27 art Exp $ */
/*
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
@@ -108,8 +108,7 @@ linux_ioctl_cdrom(p, v, retval)
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
diff --git a/sys/compat/linux/linux_fdio.c b/sys/compat/linux/linux_fdio.c
index 7c8db69da10..d394d17399a 100644
--- a/sys/compat/linux/linux_fdio.c
+++ b/sys/compat/linux/linux_fdio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: linux_fdio.c,v 1.1 2001/04/09 06:53:44 tholo Exp $ */
+/* $OpenBSD: linux_fdio.c,v 1.2 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: linux_fdio.c,v 1.1 2000/12/10 14:12:16 fvdl Exp $ */
/*
@@ -75,18 +75,8 @@ linux_ioctl_fdio(struct proc *p, struct linux_sys_ioctl_args *uap,
com = (u_long)SCARG(uap, data);
fdp = p->p_fd;
-#if 1
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd)) == NULL)
return (EBADF);
-#else
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
- (fp->f_iflags & FIF_WANTCLOSE) != 0)
- return (EBADF);
-
- FILE_USE(fp);
-#endif
com = SCARG(uap, com);
ioctlf = fp->f_ops->fo_ioctl;
diff --git a/sys/compat/linux/linux_file.c b/sys/compat/linux/linux_file.c
index 03d4f02e776..c7e1ba769cc 100644
--- a/sys/compat/linux/linux_file.c
+++ b/sys/compat/linux/linux_file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: linux_file.c,v 1.17 2001/07/03 15:32:34 jasoni Exp $ */
+/* $OpenBSD: linux_file.c,v 1.18 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: linux_file.c,v 1.15 1996/05/20 01:59:09 fvdl Exp $ */
/*
@@ -390,9 +390,8 @@ linux_sys_fcntl(p, v, retval)
* does not exist.
*/
fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
- return EBADF;
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
if (fp->f_type == DTYPE_SOCKET) {
cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
break;
diff --git a/sys/compat/linux/linux_hdio.c b/sys/compat/linux/linux_hdio.c
index 1e4c6c03d3a..bcc3ac62298 100644
--- a/sys/compat/linux/linux_hdio.c
+++ b/sys/compat/linux/linux_hdio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: linux_hdio.c,v 1.1 2001/04/09 06:53:45 tholo Exp $ */
+/* $OpenBSD: linux_hdio.c,v 1.2 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: linux_hdio.c,v 1.1 2000/12/10 14:12:17 fvdl Exp $ */
/*
@@ -77,18 +77,8 @@ linux_ioctl_hdio(struct proc *p, struct linux_sys_ioctl_args *uap,
struct linux_hd_big_geometry hdg_big;
fdp = p->p_fd;
-#if 1
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
-#else
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
- (fp->f_iflags & FIF_WANTCLOSE) != 0)
- return (EBADF);
-
- FILE_USE(fp);
-#endif
com = SCARG(uap, com);
ioctlf = fp->f_ops->fo_ioctl;
diff --git a/sys/compat/linux/linux_termios.c b/sys/compat/linux/linux_termios.c
index 810f90379f7..e010560fce7 100644
--- a/sys/compat/linux/linux_termios.c
+++ b/sys/compat/linux/linux_termios.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: linux_termios.c,v 1.9 2001/05/24 06:00:09 jasoni Exp $ */
+/* $OpenBSD: linux_termios.c,v 1.10 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: linux_termios.c,v 1.3 1996/04/05 00:01:54 christos Exp $ */
/*
@@ -460,8 +460,7 @@ linux_ioctl_termios(p, v, retval)
int error;
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
diff --git a/sys/compat/netbsd/netbsd_stat.c b/sys/compat/netbsd/netbsd_stat.c
index ae723394700..dbd8c41ab1c 100644
--- a/sys/compat/netbsd/netbsd_stat.c
+++ b/sys/compat/netbsd/netbsd_stat.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: netbsd_stat.c,v 1.11 2001/05/15 08:04:31 deraadt Exp $ */
+/* $OpenBSD: netbsd_stat.c,v 1.12 2001/10/26 12:03:27 art Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
@@ -192,8 +192,7 @@ netbsd_sys___fstat13(p, v, retval)
struct stat sb;
int error;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
error = (*fp->f_ops->fo_stat)(fp, &sb, p);
diff --git a/sys/compat/osf1/osf1_descrip.c b/sys/compat/osf1/osf1_descrip.c
index 784f4ec7511..ea2b3ae3839 100644
--- a/sys/compat/osf1/osf1_descrip.c
+++ b/sys/compat/osf1/osf1_descrip.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: osf1_descrip.c,v 1.6 2001/05/14 13:28:23 art Exp $ */
+/* $OpenBSD: osf1_descrip.c,v 1.7 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: osf1_descrip.c,v 1.5 1999/06/26 01:24:41 cgd Exp $ */
/*
@@ -243,10 +243,7 @@ osf1_sys_fstat(p, v, retval)
struct osf1_stat oub;
int error;
- /* XXX */
- if ((unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL) /* ||
- (fp->f_iflags & FIF_WANTCLOSE) != 0) */
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
error = (*fp->f_ops->fo_stat)(fp, &ub, p);
diff --git a/sys/compat/ossaudio/ossaudio.c b/sys/compat/ossaudio/ossaudio.c
index e61c0fd58eb..8795d62e039 100644
--- a/sys/compat/ossaudio/ossaudio.c
+++ b/sys/compat/ossaudio/ossaudio.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ossaudio.c,v 1.5 2001/05/24 04:21:02 aaron Exp $ */
+/* $OpenBSD: ossaudio.c,v 1.6 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: ossaudio.c,v 1.23 1997/10/19 07:41:52 augustss Exp $ */
/*
@@ -88,8 +88,7 @@ oss_ioctl_audio(p, uap, retval)
int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
@@ -652,8 +651,7 @@ oss_ioctl_mixer(p, uap, retval)
int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
@@ -816,8 +814,7 @@ oss_ioctl_sequencer(p, uap, retval)
#endif
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
diff --git a/sys/compat/sunos/sunos_ioctl.c b/sys/compat/sunos/sunos_ioctl.c
index f942d93e4ed..a3d3c0f4702 100644
--- a/sys/compat/sunos/sunos_ioctl.c
+++ b/sys/compat/sunos/sunos_ioctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sunos_ioctl.c,v 1.10 2000/04/21 15:50:21 millert Exp $ */
+/* $OpenBSD: sunos_ioctl.c,v 1.11 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: sunos_ioctl.c,v 1.23 1996/03/14 19:33:46 christos Exp $ */
/*
@@ -404,8 +404,7 @@ sunos_sys_ioctl(p, v, retval)
register int (*ctl) __P((struct file *, u_long, caddr_t, struct proc *));
int error;
- if ( (unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
if ((fp->f_flag & (FREAD|FWRITE)) == 0)
diff --git a/sys/compat/sunos/sunos_misc.c b/sys/compat/sunos/sunos_misc.c
index b05b8204979..bf09360cb49 100644
--- a/sys/compat/sunos/sunos_misc.c
+++ b/sys/compat/sunos/sunos_misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sunos_misc.c,v 1.27 2001/05/16 12:50:20 ho Exp $ */
+/* $OpenBSD: sunos_misc.c,v 1.28 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: sunos_misc.c,v 1.65 1996/04/22 01:44:31 christos Exp $ */
/*
@@ -490,10 +490,11 @@ sunos_sys_mmap(p, v, retval)
/*
* Special case: if fd refers to /dev/zero, map as MAP_ANON. (XXX)
+ * XXXART - this is probably completly unnecessary. mmap deals with
+ * XXXART - this just fine.
*/
fdp = p->p_fd;
- if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles && /*XXX*/
- (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL && /*XXX*/
+ if ((fp = fd_getfile(fdp, SCARG(&ouap, fd))) != NULL && /*XXX*/
fp->f_type == DTYPE_VNODE && /*XXX*/
(vp = (struct vnode *)fp->f_data)->v_type == VCHR && /*XXX*/
iszerodev(vp->v_rdev)) { /*XXX*/
diff --git a/sys/compat/svr4/svr4_fcntl.c b/sys/compat/svr4/svr4_fcntl.c
index 98fba9d611a..a859b7af903 100644
--- a/sys/compat/svr4/svr4_fcntl.c
+++ b/sys/compat/svr4/svr4_fcntl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: svr4_fcntl.c,v 1.18 2001/03/25 05:20:01 csapuntz Exp $ */
+/* $OpenBSD: svr4_fcntl.c,v 1.19 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: svr4_fcntl.c,v 1.14 1995/10/14 20:24:24 christos Exp $ */
/*
@@ -265,7 +265,7 @@ fd_truncate(p, fd, flp, retval)
/*
* We only support truncating the file.
*/
- if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return EBADF;
vp = (struct vnode *)fp->f_data;
diff --git a/sys/compat/svr4/svr4_ioctl.c b/sys/compat/svr4/svr4_ioctl.c
index 6bda9363f4a..f70b03d6f36 100644
--- a/sys/compat/svr4/svr4_ioctl.c
+++ b/sys/compat/svr4/svr4_ioctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: svr4_ioctl.c,v 1.8 2000/08/29 02:22:13 brad Exp $ */
+/* $OpenBSD: svr4_ioctl.c,v 1.9 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: svr4_ioctl.c,v 1.16 1996/04/11 12:54:41 christos Exp $ */
/*
@@ -109,8 +109,7 @@ svr4_sys_ioctl(p, v, retval)
fdp = p->p_fd;
cmd = SCARG(uap, com);
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
diff --git a/sys/compat/svr4/svr4_net.c b/sys/compat/svr4/svr4_net.c
index 9ba6c01a0ca..3c04d31004d 100644
--- a/sys/compat/svr4/svr4_net.c
+++ b/sys/compat/svr4/svr4_net.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: svr4_net.c,v 1.11 2001/05/14 12:38:48 art Exp $ */
+/* $OpenBSD: svr4_net.c,v 1.12 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: svr4_net.c,v 1.12 1996/09/07 12:40:51 mycroft Exp $ */
/*
@@ -187,6 +187,7 @@ svr4_netopen(dev, flag, mode, p)
DPRINTF(("ok);\n"));
p->p_dupfd = fd;
+ FILE_SET_MATURE(fp);
return ENXIO;
}
diff --git a/sys/compat/svr4/svr4_stream.c b/sys/compat/svr4/svr4_stream.c
index 5b20c9d5d9e..fce64e4a0c1 100644
--- a/sys/compat/svr4/svr4_stream.c
+++ b/sys/compat/svr4/svr4_stream.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: svr4_stream.c,v 1.13 2001/10/02 17:40:20 csapuntz Exp $ */
+/* $OpenBSD: svr4_stream.c,v 1.14 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: svr4_stream.c,v 1.19 1996/12/22 23:00:03 fvdl Exp $ */
/*
@@ -1421,8 +1421,7 @@ svr4_sys_putmsg(p, v, retval)
int error;
caddr_t sg;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
#ifdef DEBUG_SVR4
@@ -1430,8 +1429,7 @@ svr4_sys_putmsg(p, v, retval)
SCARG(uap, dat), SCARG(uap, flags));
#endif /* DEBUG_SVR4 */
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
if (SCARG(uap, ctl) != NULL) {
@@ -1572,8 +1570,7 @@ svr4_sys_getmsg(p, v, retval)
int fl;
caddr_t sg;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
bzero(&sc, sizeof(sc));
@@ -1583,8 +1580,7 @@ svr4_sys_getmsg(p, v, retval)
SCARG(uap, dat), 0);
#endif /* DEBUG_SVR4 */
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
if (SCARG(uap, ctl) != NULL) {
diff --git a/sys/compat/ultrix/ultrix_ioctl.c b/sys/compat/ultrix/ultrix_ioctl.c
index 18a35fec97a..4c80fc8f5f7 100644
--- a/sys/compat/ultrix/ultrix_ioctl.c
+++ b/sys/compat/ultrix/ultrix_ioctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ultrix_ioctl.c,v 1.7 1997/04/12 19:57:59 graichen Exp $ */
+/* $OpenBSD: ultrix_ioctl.c,v 1.8 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: ultrix_ioctl.c,v 1.3.4.1 1996/06/13 18:22:37 jonathan Exp $ */
/* from : NetBSD: sunos_ioctl.c,v 1.21 1995/10/07 06:27:31 mycroft Exp */
@@ -449,8 +449,7 @@ ultrix_sys_ioctl(p, v, retval)
register int (*ctl)();
int error;
- if ( (unsigned)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
if ((fp->f_flag & (FREAD|FWRITE)) == 0)
diff --git a/sys/compat/ultrix/ultrix_misc.c b/sys/compat/ultrix/ultrix_misc.c
index de70e550b4c..c17a4ffb185 100644
--- a/sys/compat/ultrix/ultrix_misc.c
+++ b/sys/compat/ultrix/ultrix_misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ultrix_misc.c,v 1.19 2001/07/27 21:29:53 miod Exp $ */
+/* $OpenBSD: ultrix_misc.c,v 1.20 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: ultrix_misc.c,v 1.23 1996/04/07 17:23:04 jonathan Exp $ */
/*
@@ -344,8 +344,7 @@ ultrix_sys_mmap(p, v, retval)
* Special case: if fd refers to /dev/zero, map as MAP_ANON. (XXX)
*/
fdp = p->p_fd;
- if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles && /*XXX*/
- (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL && /*XXX*/
+ if ((fp = fd_getfile(fdp, SCARG(&ouap, fd))) != NULL && /*XXX*/
fp->f_type == DTYPE_VNODE && /*XXX*/
(vp = (struct vnode *)fp->f_data)->v_type == VCHR && /*XXX*/
iszerodev(vp->v_rdev)) { /*XXX*/
diff --git a/sys/crypto/cryptodev.c b/sys/crypto/cryptodev.c
index e37082e9e10..2291577a0f9 100644
--- a/sys/crypto/cryptodev.c
+++ b/sys/crypto/cryptodev.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cryptodev.c,v 1.24 2001/09/03 10:13:44 deraadt Exp $ */
+/* $OpenBSD: cryptodev.c,v 1.25 2001/10/26 12:03:27 art Exp $ */
/*
* Copyright (c) 2001 Theo de Raadt
@@ -551,6 +551,7 @@ cryptoioctl(dev, cmd, data, flag, p)
f->f_ops = &cryptofops;
f->f_data = (caddr_t)fcr;
*(u_int32_t *)data = fd;
+ FILE_SET_MATURE(f);
break;
default:
error = EINVAL;
diff --git a/sys/kern/exec_script.c b/sys/kern/exec_script.c
index 1ac12a36ef6..ee852e32222 100644
--- a/sys/kern/exec_script.c
+++ b/sys/kern/exec_script.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: exec_script.c,v 1.13 2001/06/22 14:14:08 deraadt Exp $ */
+/* $OpenBSD: exec_script.c,v 1.14 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: exec_script.c,v 1.13 1996/02/04 02:15:06 christos Exp $ */
/*
@@ -180,6 +180,7 @@ check_shell:
fp->f_ops = &vnops;
fp->f_data = (caddr_t) epp->ep_vp;
fp->f_flag = FREAD;
+ FILE_SET_MATURE(fp);
}
#endif
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 77d7d8fdc6a..46ca7af89f6 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_descrip.c,v 1.39 2001/10/26 10:39:31 art Exp $ */
+/* $OpenBSD: kern_descrip.c,v 1.40 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */
/*
@@ -183,6 +183,22 @@ fd_unused(fdp, fd)
fdp->fd_lastfile = find_last_set(fdp, fd);
}
+struct file *
+fd_getfile(fdp, fd)
+ struct filedesc *fdp;
+ int fd;
+{
+ struct file *fp;
+
+ if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
+ return (NULL);
+
+ if (!FILE_IS_USABLE(fp))
+ return (NULL);
+
+ return (fp);
+}
+
/*
* System calls on descriptors.
*/
@@ -206,7 +222,7 @@ sys_dup(p, v, retval)
int error;
restart:
- if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
+ if (fd_getfile(fdp, old) == NULL)
return (EBADF);
if ((error = fdalloc(p, 0, &new)) != 0) {
if (error == ENOSPC) {
@@ -237,8 +253,9 @@ sys_dup2(p, v, retval)
int i, error;
restart:
- if ((u_int)old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL ||
- (u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
+ if (fd_getfile(fdp, old) == NULL)
+ return (EBADF);
+ if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
(u_int)new >= maxfiles)
return (EBADF);
if (old == new) {
@@ -283,8 +300,7 @@ sys_fcntl(p, v, retval)
int newmin;
restart:
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
switch (SCARG(uap, cmd)) {
@@ -519,7 +535,7 @@ sys_close(p, v, retval)
int fd = SCARG(uap, fd);
register struct filedesc *fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles)
+ if (fd_getfile(fdp, fd) == NULL)
return (EBADF);
return (fdrelease(p, fd));
}
@@ -544,8 +560,7 @@ sys_fstat(p, v, retval)
struct stat ub;
int error;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
error = (*fp->f_ops->fo_stat)(fp, &ub, p);
if (error == 0) {
@@ -578,8 +593,7 @@ sys_fpathconf(p, v, retval)
struct file *fp;
struct vnode *vp;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
switch (fp->f_type) {
case DTYPE_PIPE:
@@ -725,29 +739,6 @@ fdexpand(p)
}
/*
- * Check to see whether n user file descriptors
- * are available to the process p.
- */
-int
-fdavail(p, n)
- struct proc *p;
- register int n;
-{
- register struct filedesc *fdp = p->p_fd;
- register struct file **fpp;
- register int i, lim;
-
- lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
- if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
- return (1);
- fpp = &fdp->fd_ofiles[fdp->fd_freefile];
- for (i = min(lim, fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
- if (*fpp == NULL && --n <= 0)
- return (1);
- return (0);
-}
-
-/*
* Create a new open file structure and allocate
* a file decriptor for the process that refers to it.
*/
@@ -782,6 +773,7 @@ restart:
nfiles++;
fp = pool_get(&file_pool, PR_WAITOK);
bzero(fp, sizeof(struct file));
+ fp->f_iflags = FIF_LARVAL;
if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
LIST_INSERT_AFTER(fq, fp, f_list);
} else {
@@ -1066,8 +1058,7 @@ sys_flock(p, v, retval)
struct vnode *vp;
struct flock lf;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
if (fp->f_type != DTYPE_VNODE)
return (EOPNOTSUPP);
@@ -1141,8 +1132,10 @@ dupfdopen(fdp, indx, dfd, mode, error)
* as the new descriptor.
*/
fp = fdp->fd_ofiles[indx];
- if ((u_int)dfd >= fdp->fd_nfiles ||
- (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
+ if ((wfp = fd_getfile(fdp, dfd)) == NULL)
+ return (EBADF);
+
+ if (fp == wfp)
return (EBADF);
/*
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index e7aaffebca1..b5f15fcb6cb 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_event.c,v 1.9 2001/07/17 01:51:37 provos Exp $ */
+/* $OpenBSD: kern_event.c,v 1.10 2001/10/26 12:03:27 art Exp $ */
/*-
* Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
@@ -294,7 +294,8 @@ sys_kqueue(struct proc *p, void *v, register_t *retval)
if (fdp->fd_knlistsize < 0)
fdp->fd_knlistsize = 0; /* this process has a kq */
kq->kq_fdp = fdp;
- return (error);
+ FILE_SET_MATURE(fp);
+ return (0);
}
int
@@ -315,8 +316,7 @@ sys_kevent(struct proc *p, void *v, register_t *retval)
struct timespec ts;
int i, n, nerrors, error;
- if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL ||
(fp->f_type != DTYPE_KQUEUE))
return (EBADF);
@@ -403,8 +403,7 @@ kqueue_register(struct kqueue *kq, struct kevent *kev, struct proc *p)
if (fops->f_isfd) {
/* validate descriptor */
- if ((u_int)kev->ident >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[kev->ident]) == NULL)
+ if ((fp = fd_getfile(fdp, kev->ident)) == NULL)
return (EBADF);
fp->f_count++;
diff --git a/sys/kern/kern_exec.c b/sys/kern/kern_exec.c
index bb4ccaa8db6..ca619f10f2a 100644
--- a/sys/kern/kern_exec.c
+++ b/sys/kern/kern_exec.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_exec.c,v 1.57 2001/09/19 20:50:58 mickey Exp $ */
+/* $OpenBSD: kern_exec.c,v 1.58 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: kern_exec.c,v 1.75 1996/02/09 18:59:28 christos Exp $ */
/*-
@@ -506,9 +506,12 @@ sys_execve(p, v, retval)
for (i = 0; i < 3; i++) {
struct file *fp = NULL;
- if (i < p->p_fd->fd_nfiles)
- fp = p->p_fd->fd_ofiles[i];
-
+ /*
+ * NOTE - This will never return NULL because of
+ * unmature fds. The file descriptor table is not
+ * shared because we're suid.
+ */
+ fp = fd_getfile(p->p_fd, i);
#ifdef PROCFS
/*
* Close descriptors that are writing to procfs.
@@ -558,6 +561,7 @@ sys_execve(p, v, retval)
fp->f_type = DTYPE_VNODE;
fp->f_ops = &vnops;
fp->f_data = (caddr_t)vp;
+ FILE_SET_MATURE(fp);
}
}
} else
diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c
index 5c2b5a3e2c5..278ffc24d97 100644
--- a/sys/kern/sys_generic.c
+++ b/sys/kern/sys_generic.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sys_generic.c,v 1.29 2001/05/16 12:52:58 ho Exp $ */
+/* $OpenBSD: sys_generic.c,v 1.30 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $ */
/*
@@ -86,17 +86,11 @@ sys_read(p, v, retval)
struct file *fp;
struct filedesc *fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FREAD) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FREAD) == 0)
return (EBADF);
-#if notyet
- FILE_USE(fp);
-#endif
/* dofileread() will unuse the descriptor for us */
return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
&fp->f_offset, retval));
@@ -182,17 +176,11 @@ sys_readv(p, v, retval)
struct file *fp;
struct filedesc *fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FREAD) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FREAD) == 0)
return (EBADF);
-#if notyet
- FILE_USE(fp);
-#endif
/* dofilereadv() will unuse the descriptor for us */
return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
&fp->f_offset, retval));
@@ -309,17 +297,11 @@ sys_write(p, v, retval)
struct file *fp;
struct filedesc *fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FWRITE) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FWRITE) == 0)
return (EBADF);
-#if notyet
- FILE_USE(fp);
-#endif
/* dofilewrite() will unuse the descriptor for us */
return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
&fp->f_offset, retval));
@@ -408,17 +390,11 @@ sys_writev(p, v, retval)
struct file *fp;
struct filedesc *fdp = p->p_fd;
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FWRITE) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FWRITE) == 0)
return (EBADF);
-#if notyet
- FILE_USE(fp);
-#endif
/* dofilewritev() will unuse the descriptor for us */
return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
&fp->f_offset, retval));
@@ -544,8 +520,7 @@ sys_ioctl(p, v, retval)
char stkbuf[STK_PARAMS];
fdp = p->p_fd;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
diff --git a/sys/kern/sys_pipe.c b/sys/kern/sys_pipe.c
index 13084a9ed56..48290fb70dd 100644
--- a/sys/kern/sys_pipe.c
+++ b/sys/kern/sys_pipe.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sys_pipe.c,v 1.38 2001/09/19 20:50:58 mickey Exp $ */
+/* $OpenBSD: sys_pipe.c,v 1.39 2001/10/26 12:03:27 art Exp $ */
/*
* Copyright (c) 1996 John S. Dyson
@@ -143,6 +143,8 @@ sys_opipe(p, v, retval)
rpipe->pipe_peer = wpipe;
wpipe->pipe_peer = rpipe;
+ FILE_SET_MATURE(rf);
+ FILE_SET_MATURE(wf);
return (0);
free3:
ffree(rf);
diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c
index 28ef6de78ee..c25ad2915ca 100644
--- a/sys/kern/uipc_syscalls.c
+++ b/sys/kern/uipc_syscalls.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uipc_syscalls.c,v 1.42 2001/09/20 17:02:31 mpech Exp $ */
+/* $OpenBSD: uipc_syscalls.c,v 1.43 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: uipc_syscalls.c,v 1.19 1996/02/09 19:00:48 christos Exp $ */
/*
@@ -91,6 +91,7 @@ sys_socket(p, v, retval)
ffree(fp);
} else {
fp->f_data = (caddr_t)so;
+ FILE_SET_MATURE(fp);
*retval = fd;
}
return (error);
@@ -230,6 +231,7 @@ sys_accept(p, v, retval)
}
m_freem(nam);
splx(s);
+ FILE_SET_MATURE(fp);
return (error);
}
@@ -336,6 +338,8 @@ sys_socketpair(p, v, retval)
}
error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
2 * sizeof (int));
+ FILE_SET_MATURE(fp1);
+ FILE_SET_MATURE(fp2);
if (error == 0)
return (error);
free4:
@@ -1075,8 +1079,7 @@ getsock(fdp, fdes, fpp)
{
register struct file *fp;
- if ((unsigned)fdes >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fdes]) == NULL)
+ if ((fp = fd_getfile(fdp, fdes)) == NULL)
return (EBADF);
if (fp->f_type != DTYPE_SOCKET)
return (ENOTSOCK);
diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index bb11b3d6a0f..46b675b3c96 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uipc_usrreq.c,v 1.14 2001/10/26 10:39:31 art Exp $ */
+/* $OpenBSD: uipc_usrreq.c,v 1.15 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: uipc_usrreq.c,v 1.18 1996/02/09 19:00:50 christos Exp $ */
/*
@@ -751,8 +751,7 @@ unp_internalize(control, p)
ip = (int *)(cm + 1);
for (i = 0; i < oldfds; i++) {
fd = *ip++;
- if ((unsigned)fd >= fdp->fd_nfiles ||
- fdp->fd_ofiles[fd] == NULL)
+ if (fd_getfile(fdp, fd) == NULL)
return (EBADF);
if (fdp->fd_ofiles[fd]->f_count == LONG_MAX-2 ||
fdp->fd_ofiles[fd]->f_msgcount == LONG_MAX-2)
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index 2e1cf5f8103..36f606fe474 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vfs_syscalls.c,v 1.80 2001/07/26 20:24:47 millert Exp $ */
+/* $OpenBSD: vfs_syscalls.c,v 1.81 2001/10/26 12:03:27 art Exp $ */
/* $NetBSD: vfs_syscalls.c,v 1.71 1996/04/23 10:29:02 mycroft Exp $ */
/*
@@ -937,6 +937,7 @@ sys_open(p, v, retval)
}
VOP_UNLOCK(vp, 0, p);
*retval = indx;
+ FILE_SET_MATURE(fp);
return (0);
}
@@ -1098,6 +1099,7 @@ sys_fhopen(p, v, retval)
}
VOP_UNLOCK(vp, 0, p);
*retval = indx;
+ FILE_SET_MATURE(fp);
return (0);
bad:
@@ -1506,8 +1508,7 @@ sys_lseek(p, v, retval)
struct vnode *vp;
int error, special;
- if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+ if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if (fp->f_type != DTYPE_VNODE)
return (ESPIPE);
@@ -2599,7 +2600,7 @@ getvnode(fdp, fd, fpp)
{
struct file *fp;
- if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
if (fp->f_type != DTYPE_VNODE)
return (EINVAL);
@@ -2629,17 +2630,10 @@ sys_pread(p, v, retval)
off_t offset;
int error, fd = SCARG(uap, fd);
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FREAD) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FREAD) == 0)
return (EBADF);
-
-#if notyet
- FILE_USE(fp);
-#endif
vp = (struct vnode *)fp->f_data;
if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
@@ -2682,17 +2676,10 @@ sys_preadv(p, v, retval)
off_t offset;
int error, fd = SCARG(uap, fd);
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FREAD) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FREAD) == 0)
return (EBADF);
-
-#if notyet
- FILE_USE(fp);
-#endif
vp = (struct vnode *)fp->f_data;
if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
@@ -2735,17 +2722,10 @@ sys_pwrite(p, v, retval)
off_t offset;
int error, fd = SCARG(uap, fd);
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FWRITE) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FWRITE) == 0)
return (EBADF);
-
-#if notyet
- FILE_USE(fp);
-#endif
vp = (struct vnode *)fp->f_data;
if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
@@ -2789,17 +2769,11 @@ sys_pwritev(p, v, retval)
off_t offset;
int error, fd = SCARG(uap, fd);
- if ((u_int)fd >= fdp->fd_nfiles ||
- (fp = fdp->fd_ofiles[fd]) == NULL ||
-#if notyet
- (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
-#endif
- (fp->f_flag & FWRITE) == 0)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
+ return (EBADF);
+ if ((fp->f_flag & FWRITE) == 0)
return (EBADF);
-#if notyet
- FILE_USE(fp);
-#endif
vp = (struct vnode *)fp->f_data;
if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
error = ESPIPE;
diff --git a/sys/miscfs/fdesc/fdesc_vnops.c b/sys/miscfs/fdesc/fdesc_vnops.c
index 69655c6f34d..1fb61c30ae1 100644
--- a/sys/miscfs/fdesc/fdesc_vnops.c
+++ b/sys/miscfs/fdesc/fdesc_vnops.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fdesc_vnops.c,v 1.23 2001/06/23 02:14:24 csapuntz Exp $ */
+/* $OpenBSD: fdesc_vnops.c,v 1.24 2001/10/26 12:03:28 art Exp $ */
/* $NetBSD: fdesc_vnops.c,v 1.32 1996/04/11 11:24:29 mrg Exp $ */
/*
@@ -480,7 +480,7 @@ fdesc_getattr(v)
case Fdesc:
fd = VTOFDESC(vp)->fd_fd;
fdp = ap->a_p->p_fd;
- if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return (EBADF);
memset(&stb, 0, sizeof(stb));
error = (*fp->f_ops->fo_stat)(fp, &stb, ap->a_p);
diff --git a/sys/sys/file.h b/sys/sys/file.h
index 8c4856afd8f..9aed80ae382 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.h,v 1.12 2001/05/15 09:00:19 deraadt Exp $ */
+/* $OpenBSD: file.h,v 1.13 2001/10/26 12:03:28 art Exp $ */
/* $NetBSD: file.h,v 1.11 1995/03/26 20:24:13 jtc Exp $ */
/*
@@ -82,8 +82,19 @@ struct file {
} *f_ops;
off_t f_offset;
caddr_t f_data; /* vnode or socket */
+ int f_iflags;
};
+#define FIF_WANTCLOSE 0x01 /* a close is waiting for usecount */
+#define FIF_LARVAL 0x02 /* not fully constructed, don't use */
+
+#define FILE_IS_USABLE(fp) \
+ (((fp)->f_iflags & (FIF_WANTCLOSE|FIF_LARVAL)) == 0)
+
+#define FILE_SET_MATURE(fp) do { \
+ (fp)->f_iflags &= ~FIF_LARVAL; \
+} while (0)
+
LIST_HEAD(filelist, file);
extern struct filelist filehead; /* head of list of open files */
extern int maxfiles; /* kernel limit on number of open files */
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index dc576fce90c..5b737a8f712 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: filedesc.h,v 1.12 2001/10/26 10:39:31 art Exp $ */
+/* $OpenBSD: filedesc.h,v 1.13 2001/10/26 12:03:28 art Exp $ */
/* $NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $ */
/*
@@ -119,7 +119,6 @@ int dupfdopen __P((struct filedesc *fdp, int indx, int dfd, int mode,
int error));
int fdalloc __P((struct proc *p, int want, int *result));
void fdexpand __P((struct proc *));
-int fdavail __P((struct proc *p, int n));
int falloc __P((struct proc *p, struct file **resultfp, int *resultfd));
void ffree __P((struct file *));
struct filedesc *fdinit __P((struct proc *p));
@@ -129,6 +128,7 @@ void fdfree __P((struct proc *p));
int fdrelease __P((struct proc *p, int));
void fdremove __P((struct filedesc *, int));
void fdcloseexec __P((struct proc *));
+struct file *fd_getfile __P((struct filedesc *, int fd));
int closef __P((struct file *, struct proc *));
int getsock __P((struct filedesc *, int, struct file **));
diff --git a/sys/uvm/uvm_mmap.c b/sys/uvm/uvm_mmap.c
index 20311eec008..78ccea49e57 100644
--- a/sys/uvm/uvm_mmap.c
+++ b/sys/uvm/uvm_mmap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: uvm_mmap.c,v 1.20 2001/09/11 20:05:26 miod Exp $ */
+/* $OpenBSD: uvm_mmap.c,v 1.21 2001/10/26 12:03:28 art Exp $ */
/* $NetBSD: uvm_mmap.c,v 1.41 2000/05/23 02:19:20 enami Exp $ */
/*
@@ -390,10 +390,7 @@ sys_mmap(p, v, retval)
if ((flags & MAP_ANON) == 0) {
- if (fd < 0 || fd >= fdp->fd_nfiles)
- return(EBADF); /* failed range check? */
- fp = fdp->fd_ofiles[fd]; /* convert to file pointer */
- if (fp == NULL)
+ if ((fp = fd_getfile(fdp, fd)) == NULL)
return(EBADF);
if (fp->f_type != DTYPE_VNODE)
diff --git a/sys/xfs/xfs_vfsops-bsd.c b/sys/xfs/xfs_vfsops-bsd.c
index 95611728524..3e74bcd637d 100644
--- a/sys/xfs/xfs_vfsops-bsd.c
+++ b/sys/xfs/xfs_vfsops-bsd.c
@@ -38,7 +38,7 @@
#include <xfs/xfs_locl.h>
-RCSID("$Id: xfs_vfsops-bsd.c,v 1.6 2001/02/21 02:45:12 nate Exp $");
+RCSID("$Id: xfs_vfsops-bsd.c,v 1.7 2001/10/26 12:03:28 art Exp $");
/*
* XFS vfs operations.
@@ -477,6 +477,7 @@ xfs_fhopen (struct proc *proc,
#ifdef __APPLE__
*fdflags(proc, index) &= ~UF_RESERVED;
#endif
+ FILE_SET_MATURE(fp);
return 0;
out:
XFSDEB(XDEBVFOPS, ("xfs_fhopen: error = %d\n", error));