add USL's copyright notice
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 808b95a..75e9027 100644 (file)
 /*
 /*
- * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1982, 1986, 1989, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)kern_descrip.c      7.25 (Berkeley) %G%
+ *     @(#)kern_descrip.c      8.5 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "filedesc.h"
-#include "kernel.h"
-#include "vnode.h"
-#include "proc.h"
-#include "file.h"
-#include "socket.h"
-#include "socketvar.h"
-#include "stat.h"
-#include "ioctl.h"
-#include "fcntl.h"
-#include "malloc.h"
-#include "syslog.h"
-#include "resourcevar.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/filedesc.h>
+#include <sys/kernel.h>
+#include <sys/vnode.h>
+#include <sys/proc.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/fcntl.h>
+#include <sys/malloc.h>
+#include <sys/syslog.h>
+#include <sys/unistd.h>
+#include <sys/resourcevar.h>
 
 /*
  * Descriptor management.
  */
 
 /*
  * Descriptor management.
  */
+struct file *filehead; /* head of list of open files */
+int nfiles;            /* actual number of open files */
 
 /*
  * System calls on descriptors.
  */
 
 /*
  * System calls on descriptors.
  */
+struct getdtablesize_args {
+       int     dummy;
+};
 /* ARGSUSED */
 getdtablesize(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 getdtablesize(p, uap, retval)
        struct proc *p;
-       struct args *uap;
+       struct getdtablesize_args *uap;
        int *retval;
 {
 
        int *retval;
 {
 
-       *retval = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
+       *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
        return (0);
 }
 
 /*
  * Duplicate a file descriptor.
  */
        return (0);
 }
 
 /*
  * Duplicate a file descriptor.
  */
+struct dup_args {
+       u_int   fd;
+};
 /* ARGSUSED */
 dup(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 dup(p, uap, retval)
        struct proc *p;
-       struct args {
-               int     i;
-       } *uap;
+       struct dup_args *uap;
        int *retval;
 {
        int *retval;
 {
-       register struct filedesc *fdp = p->p_fd;
-       struct file *fp;
-       int fd, error;
+       register struct filedesc *fdp;
+       u_int old;
+       int new, error;
 
 
+       old = uap->fd;
        /*
         * XXX Compatibility
         */
        /*
         * XXX Compatibility
         */
-       if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); }
+       if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
 
 
-       if ((unsigned)uap->i >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[uap->i]) == NULL)
+       fdp = p->p_fd;
+       if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
                return (EBADF);
                return (EBADF);
-       if (error = fdalloc(p, 0, &fd))
+       if (error = fdalloc(p, 0, &new))
                return (error);
                return (error);
-       fdp->fd_ofiles[fd] = fp;
-       fdp->fd_ofileflags[fd] = fdp->fd_ofileflags[uap->i] &~ UF_EXCLOSE;
-       fp->f_count++;
-       if (fd > fdp->fd_lastfile)
-               fdp->fd_lastfile = fd;
-       *retval = fd;
-       return (0);
+       return (finishdup(fdp, (int)old, new, retval));
 }
 
 /*
  * Duplicate a file descriptor to a particular value.
  */
 }
 
 /*
  * Duplicate a file descriptor to a particular value.
  */
+struct dup2_args {
+       u_int   from;
+       u_int   to;
+};
 /* ARGSUSED */
 dup2(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 dup2(p, uap, retval)
        struct proc *p;
-       struct args {
-               u_int   from;
-               u_int   to;
-       } *uap;
+       struct dup2_args *uap;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
-       register struct file *fp;
        register u_int old = uap->from, new = uap->to;
        int i, error;
 
        if (old >= fdp->fd_nfiles ||
        register u_int old = uap->from, new = uap->to;
        int i, error;
 
        if (old >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[old]) == NULL ||
-           new >= p->p_rlimit[RLIMIT_OFILE].rlim_cur)
+           fdp->fd_ofiles[old] == NULL ||
+           new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
+           new >= maxfiles)
                return (EBADF);
                return (EBADF);
-       *retval = new;
-       if (old == new)
+       if (old == new) {
+               *retval = new;
                return (0);
                return (0);
+       }
        if (new >= fdp->fd_nfiles) {
                if (error = fdalloc(p, new, &i))
                        return (error);
        if (new >= fdp->fd_nfiles) {
                if (error = fdalloc(p, new, &i))
                        return (error);
@@ -112,25 +121,21 @@ dup2(p, uap, retval)
                 */
                (void) closef(fdp->fd_ofiles[new], p);
        }
                 */
                (void) closef(fdp->fd_ofiles[new], p);
        }
-       fdp->fd_ofiles[new] = fp;
-       fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
-       fp->f_count++;
-       if (new > fdp->fd_lastfile)
-               fdp->fd_lastfile = new;
-       return (0);
+       return (finishdup(fdp, (int)old, (int)new, retval));
 }
 
 /*
  * The file control system call.
  */
 }
 
 /*
  * The file control system call.
  */
+struct fcntl_args {
+       int     fd;
+       int     cmd;
+       int     arg;
+};
 /* ARGSUSED */
 fcntl(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 fcntl(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fd;
-               int     cmd;
-               int     arg;
-       } *uap;
+       register struct fcntl_args *uap;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
@@ -139,24 +144,22 @@ fcntl(p, uap, retval)
        struct vnode *vp;
        int i, tmp, error, flg = F_POSIX;
        struct flock fl;
        struct vnode *vp;
        int i, tmp, error, flg = F_POSIX;
        struct flock fl;
+       u_int newmin;
 
        if ((unsigned)uap->fd >= fdp->fd_nfiles ||
            (fp = fdp->fd_ofiles[uap->fd]) == NULL)
                return (EBADF);
        pop = &fdp->fd_ofileflags[uap->fd];
 
        if ((unsigned)uap->fd >= fdp->fd_nfiles ||
            (fp = fdp->fd_ofiles[uap->fd]) == NULL)
                return (EBADF);
        pop = &fdp->fd_ofileflags[uap->fd];
-       switch(uap->cmd) {
+       switch (uap->cmd) {
+
        case F_DUPFD:
        case F_DUPFD:
-               if ((unsigned)uap->arg >= p->p_rlimit[RLIMIT_OFILE].rlim_cur)
+               newmin = uap->arg;
+               if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
+                   newmin >= maxfiles)
                        return (EINVAL);
                        return (EINVAL);
-               if (error = fdalloc(p, uap->arg, &i))
+               if (error = fdalloc(p, newmin, &i))
                        return (error);
                        return (error);
-               fdp->fd_ofiles[i] = fp;
-               fdp->fd_ofileflags[i] = *pop &~ UF_EXCLOSE;
-               fp->f_count++;
-               if (i > fdp->fd_lastfile)
-                       fdp->fd_lastfile = i;
-               *retval = i;
-               return (0);
+               return (finishdup(fdp, uap->fd, i, retval));
 
        case F_GETFD:
                *retval = *pop & 1;
 
        case F_GETFD:
                *retval = *pop & 1;
@@ -231,11 +234,13 @@ fcntl(p, uap, retval)
                case F_RDLCK:
                        if ((fp->f_flag & FREAD) == 0)
                                return (EBADF);
                case F_RDLCK:
                        if ((fp->f_flag & FREAD) == 0)
                                return (EBADF);
+                       p->p_flag |= P_ADVLOCK;
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_WRLCK:
                        if ((fp->f_flag & FWRITE) == 0)
                                return (EBADF);
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_WRLCK:
                        if ((fp->f_flag & FWRITE) == 0)
                                return (EBADF);
+                       p->p_flag |= P_ADVLOCK;
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_UNLCK:
                        return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
 
                case F_UNLCK:
@@ -266,15 +271,36 @@ fcntl(p, uap, retval)
        /* NOTREACHED */
 }
 
        /* NOTREACHED */
 }
 
+/*
+ * Common code for dup, dup2, and fcntl(F_DUPFD).
+ */
+int
+finishdup(fdp, old, new, retval)
+       register struct filedesc *fdp;
+       register int old, new, *retval;
+{
+       register struct file *fp;
+
+       fp = fdp->fd_ofiles[old];
+       fdp->fd_ofiles[new] = fp;
+       fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
+       fp->f_count++;
+       if (new > fdp->fd_lastfile)
+               fdp->fd_lastfile = new;
+       *retval = new;
+       return (0);
+}
+
 /*
  * Close a file descriptor.
  */
 /*
  * Close a file descriptor.
  */
+struct close_args {
+       int     fd;
+};
 /* ARGSUSED */
 close(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 close(p, uap, retval)
        struct proc *p;
-       struct args {
-               int     fd;
-       } *uap;
+       struct close_args *uap;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
@@ -297,16 +323,61 @@ close(p, uap, retval)
        return (closef(fp, p));
 }
 
        return (closef(fp, p));
 }
 
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
 /*
  * Return status information about a file descriptor.
  */
 /*
  * Return status information about a file descriptor.
  */
+struct ofstat_args {
+       int     fd;
+       struct  ostat *sb;
+};
+/* ARGSUSED */
+ofstat(p, uap, retval)
+       struct proc *p;
+       register struct ofstat_args *uap;
+       int *retval;
+{
+       register struct filedesc *fdp = p->p_fd;
+       register struct file *fp;
+       struct stat ub;
+       struct ostat oub;
+       int error;
+
+       if ((unsigned)uap->fd >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL)
+               return (EBADF);
+       switch (fp->f_type) {
+
+       case DTYPE_VNODE:
+               error = vn_stat((struct vnode *)fp->f_data, &ub, p);
+               break;
+
+       case DTYPE_SOCKET:
+               error = soo_stat((struct socket *)fp->f_data, &ub);
+               break;
+
+       default:
+               panic("ofstat");
+               /*NOTREACHED*/
+       }
+       cvtstat(&ub, &oub);
+       if (error == 0)
+               error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
+       return (error);
+}
+#endif /* COMPAT_43 || COMPAT_SUNOS */
+
+/*
+ * Return status information about a file descriptor.
+ */
+struct fstat_args {
+       int     fd;
+       struct  stat *sb;
+};
 /* ARGSUSED */
 fstat(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 fstat(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fd;
-               struct  stat *sb;
-       } *uap;
+       register struct fstat_args *uap;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
@@ -336,6 +407,44 @@ fstat(p, uap, retval)
        return (error);
 }
 
        return (error);
 }
 
+/*
+ * Return pathconf information about a file descriptor.
+ */
+struct fpathconf_args {
+       int     fd;
+       int     name;
+};
+/* ARGSUSED */
+fpathconf(p, uap, retval)
+       struct proc *p;
+       register struct fpathconf_args *uap;
+       int *retval;
+{
+       struct filedesc *fdp = p->p_fd;
+       struct file *fp;
+       struct vnode *vp;
+
+       if ((unsigned)uap->fd >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL)
+               return (EBADF);
+       switch (fp->f_type) {
+
+       case DTYPE_SOCKET:
+               if (uap->name != _PC_PIPE_BUF)
+                       return (EINVAL);
+               *retval = PIPE_BUF;
+               return (0);
+
+       case DTYPE_VNODE:
+               vp = (struct vnode *)fp->f_data;
+               return (VOP_PATHCONF(vp, uap->name, retval));
+
+       default:
+               panic("fpathconf");
+       }
+       /*NOTREACHED*/
+}
+
 /*
  * Allocate a file descriptor for the process.
  */
 /*
  * Allocate a file descriptor for the process.
  */
@@ -357,7 +466,7 @@ fdalloc(p, want, result)
         * of want or fd_freefile.  If that fails, consider
         * expanding the ofile array.
         */
         * of want or fd_freefile.  If that fails, consider
         * expanding the ofile array.
         */
-       lim = p->p_rlimit[RLIMIT_OFILE].rlim_cur;
+       lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
        for (;;) {
                last = min(fdp->fd_nfiles, lim);
                if ((i = want) < fdp->fd_freefile)
        for (;;) {
                last = min(fdp->fd_nfiles, lim);
                if ((i = want) < fdp->fd_freefile)
@@ -415,10 +524,10 @@ fdavail(p, n)
 {
        register struct filedesc *fdp = p->p_fd;
        register struct file **fpp;
 {
        register struct filedesc *fdp = p->p_fd;
        register struct file **fpp;
-       register int i;
+       register int i, lim;
 
 
-       if ((i = p->p_rlimit[RLIMIT_OFILE].rlim_cur - fdp->fd_nfiles) > 0 &&
-           (n -= i) <= 0)
+       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 = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
                return (1);
        fpp = &fdp->fd_ofiles[fdp->fd_freefile];
        for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
@@ -427,7 +536,6 @@ fdavail(p, n)
        return (0);
 }
 
        return (0);
 }
 
-struct file *lastf;
 /*
  * Create a new open file structure and allocate
  * a file decriptor for the process that refers to it.
 /*
  * Create a new open file structure and allocate
  * a file decriptor for the process that refers to it.
@@ -437,29 +545,38 @@ falloc(p, resultfp, resultfd)
        struct file **resultfp;
        int *resultfd;
 {
        struct file **resultfp;
        int *resultfd;
 {
-       register struct file *fp;
+       register struct file *fp, *fq, **fpp;
        int error, i;
 
        if (error = fdalloc(p, 0, &i))
                return (error);
        int error, i;
 
        if (error = fdalloc(p, 0, &i))
                return (error);
-       if (lastf == 0)
-               lastf = file;
-       for (fp = lastf; fp < fileNFILE; fp++)
-               if (fp->f_count == 0)
-                       goto slot;
-       for (fp = file; fp < lastf; fp++)
-               if (fp->f_count == 0)
-                       goto slot;
-       tablefull("file");
-       return (ENFILE);
-slot:
+       if (nfiles >= maxfiles) {
+               tablefull("file");
+               return (ENFILE);
+       }
+       /*
+        * Allocate a new file descriptor.
+        * If the process has file descriptor zero open, add to the list
+        * of open files at that point, otherwise put it at the front of
+        * the list of open files.
+        */
+       nfiles++;
+       MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
+       if (fq = p->p_fd->fd_ofiles[0])
+               fpp = &fq->f_filef;
+       else
+               fpp = &filehead;
        p->p_fd->fd_ofiles[i] = fp;
        p->p_fd->fd_ofiles[i] = fp;
+       if (fq = *fpp)
+               fq->f_fileb = &fp->f_filef;
+       fp->f_filef = fq;
+       fp->f_fileb = fpp;
+       *fpp = fp;
        fp->f_count = 1;
        fp->f_count = 1;
-       fp->f_data = 0;
+       fp->f_msgcount = 0;
        fp->f_offset = 0;
        fp->f_cred = p->p_ucred;
        crhold(fp->f_cred);
        fp->f_offset = 0;
        fp->f_cred = p->p_ucred;
        crhold(fp->f_cred);
-       lastf = fp + 1;
        if (resultfp)
                *resultfp = fp;
        if (resultfd)
        if (resultfp)
                *resultfp = fp;
        if (resultfd)
@@ -467,6 +584,27 @@ slot:
        return (0);
 }
 
        return (0);
 }
 
+/*
+ * Free a file descriptor.
+ */
+ffree(fp)
+       register struct file *fp;
+{
+       register struct file *fq;
+
+       if (fq = fp->f_filef)
+               fq->f_fileb = fp->f_fileb;
+       *fp->f_fileb = fq;
+       crfree(fp->f_cred);
+#ifdef DIAGNOSTIC
+       fp->f_filef = NULL;
+       fp->f_fileb = NULL;
+       fp->f_count = 0;
+#endif
+       nfiles--;
+       FREE(fp, M_FILE);
+}
+
 /*
  * Copy a filedesc structure.
  */
 /*
  * Copy a filedesc structure.
  */
@@ -504,7 +642,7 @@ fdcopy(p)
                 * allowing the table to shrink.
                 */
                i = newfdp->fd_nfiles;
                 * allowing the table to shrink.
                 */
                i = newfdp->fd_nfiles;
-               while (i > 2 * NDEXTENT && i >= newfdp->fd_lastfile * 2)
+               while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
                        i /= 2;
                MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
                    M_FILEDESC, M_WAITOK);
                        i /= 2;
                MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
                    M_FILEDESC, M_WAITOK);
@@ -548,10 +686,12 @@ fdfree(p)
 /*
  * Internal form of close.
  * Decrement reference count on file structure.
 /*
  * Internal form of close.
  * Decrement reference count on file structure.
+ * Note: p may be NULL when closing a file
+ * that was being passed in a message.
  */
 closef(fp, p)
        register struct file *fp;
  */
 closef(fp, p)
        register struct file *fp;
-       struct proc *p;
+       register struct proc *p;
 {
        struct vnode *vp;
        struct flock lf;
 {
        struct vnode *vp;
        struct flock lf;
@@ -564,8 +704,10 @@ closef(fp, p)
         * locks owned by this process.  This is handled by setting
         * a flag in the unlock to free ONLY locks obeying POSIX
         * semantics, and not to free BSD-style file locks.
         * locks owned by this process.  This is handled by setting
         * a flag in the unlock to free ONLY locks obeying POSIX
         * semantics, and not to free BSD-style file locks.
+        * If the descriptor was in a message, POSIX-style locks
+        * aren't passed with the descriptor.
         */
         */
-       if (fp->f_type == DTYPE_VNODE) {
+       if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
                lf.l_whence = SEEK_SET;
                lf.l_start = 0;
                lf.l_len = 0;
                lf.l_whence = SEEK_SET;
                lf.l_start = 0;
                lf.l_len = 0;
@@ -577,11 +719,16 @@ closef(fp, p)
                return (0);
        if (fp->f_count < 0)
                panic("closef: count < 0");
                return (0);
        if (fp->f_count < 0)
                panic("closef: count < 0");
-       if (fp->f_type == DTYPE_VNODE)
+       if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
+               lf.l_whence = SEEK_SET;
+               lf.l_start = 0;
+               lf.l_len = 0;
+               lf.l_type = F_UNLCK;
+               vp = (struct vnode *)fp->f_data;
                (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
                (void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
+       }
        error = (*fp->f_ops->fo_close)(fp, p);
        error = (*fp->f_ops->fo_close)(fp, p);
-       crfree(fp->f_cred);
-       fp->f_count = 0;
+       ffree(fp);
        return (error);
 }
 
        return (error);
 }
 
@@ -591,21 +738,20 @@ closef(fp, p)
  * Just attempt to get a record lock of the requested type on
  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
  */
  * Just attempt to get a record lock of the requested type on
  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
  */
-
+struct flock_args {
+       int     fd;
+       int     how;
+};
 /* ARGSUSED */
 flock(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 flock(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fd;
-               int     how;
-       } *uap;
+       register struct flock_args *uap;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
        struct vnode *vp;
        struct flock lf;
        int *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        register struct file *fp;
        struct vnode *vp;
        struct flock lf;
-       int error;
 
        if ((unsigned)uap->fd >= fdp->fd_nfiles ||
            (fp = fdp->fd_ofiles[uap->fd]) == NULL)
 
        if ((unsigned)uap->fd >= fdp->fd_nfiles ||
            (fp = fdp->fd_ofiles[uap->fd]) == NULL)
@@ -618,6 +764,7 @@ flock(p, uap, retval)
        lf.l_len = 0;
        if (uap->how & LOCK_UN) {
                lf.l_type = F_UNLCK;
        lf.l_len = 0;
        if (uap->how & LOCK_UN) {
                lf.l_type = F_UNLCK;
+               fp->f_flag &= ~FHASLOCK;
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
        }
        if (uap->how & LOCK_EX)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
        }
        if (uap->how & LOCK_EX)
@@ -626,6 +773,7 @@ flock(p, uap, retval)
                lf.l_type = F_RDLCK;
        else
                return (EBADF);
                lf.l_type = F_RDLCK;
        else
                return (EBADF);
+       fp->f_flag |= FHASLOCK;
        if (uap->how & LOCK_NB)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
        return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
        if (uap->how & LOCK_NB)
                return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
        return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
@@ -640,9 +788,10 @@ flock(p, uap, retval)
  * references to this file will be direct to the other driver.
  */
 /* ARGSUSED */
  * references to this file will be direct to the other driver.
  */
 /* ARGSUSED */
-fdopen(dev, mode, type)
+fdopen(dev, mode, type, p)
        dev_t dev;
        int mode, type;
        dev_t dev;
        int mode, type;
+       struct proc *p;
 {
 
        /*
 {
 
        /*
@@ -653,17 +802,18 @@ fdopen(dev, mode, type)
         * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
         * will simply report the error.
         */
         * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
         * will simply report the error.
         */
-       curproc->p_dupfd = minor(dev);          /* XXX */
+       p->p_dupfd = minor(dev);
        return (ENODEV);
 }
 
 /*
  * Duplicate the specified descriptor to a free descriptor.
  */
        return (ENODEV);
 }
 
 /*
  * Duplicate the specified descriptor to a free descriptor.
  */
-dupfdopen(fdp, indx, dfd, mode)
+dupfdopen(fdp, indx, dfd, mode, error)
        register struct filedesc *fdp;
        register int indx, dfd;
        int mode;
        register struct filedesc *fdp;
        register int indx, dfd;
        int mode;
+       int error;
 {
        register struct file *wfp;
        struct file *fp;
 {
        register struct file *wfp;
        struct file *fp;
@@ -681,15 +831,56 @@ dupfdopen(fdp, indx, dfd, mode)
                return (EBADF);
 
        /*
                return (EBADF);
 
        /*
-        * Check that the mode the file is being opened for is a subset 
-        * of the mode of the existing descriptor.
+        * There are two cases of interest here.
+        *
+        * For ENODEV simply dup (dfd) to file descriptor
+        * (indx) and return.
+        *
+        * For ENXIO steal away the file structure from (dfd) and
+        * store it in (indx).  (dfd) is effectively closed by
+        * this operation.
+        *
+        * Any other error code is just returned.
         */
         */
-       if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
-               return (EACCES);
-       fdp->fd_ofiles[indx] = wfp;
-       fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
-       wfp->f_count++;
-       if (indx > fdp->fd_lastfile)
-               fdp->fd_lastfile = indx;
-       return (0);
+       switch (error) {
+       case ENODEV:
+               /*
+                * Check that the mode the file is being opened for is a
+                * subset of the mode of the existing descriptor.
+                */
+               if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
+                       return (EACCES);
+               fdp->fd_ofiles[indx] = wfp;
+               fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
+               wfp->f_count++;
+               if (indx > fdp->fd_lastfile)
+                       fdp->fd_lastfile = indx;
+               return (0);
+
+       case ENXIO:
+               /*
+                * Steal away the file pointer from dfd, and stuff it into indx.
+                */
+               fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
+               fdp->fd_ofiles[dfd] = NULL;
+               fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
+               fdp->fd_ofileflags[dfd] = 0;
+               /*
+                * Complete the clean up of the filedesc structure by
+                * recomputing the various hints.
+                */
+               if (indx > fdp->fd_lastfile)
+                       fdp->fd_lastfile = indx;
+               else
+                       while (fdp->fd_lastfile > 0 &&
+                              fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
+                               fdp->fd_lastfile--;
+                       if (dfd < fdp->fd_freefile)
+                               fdp->fd_freefile = dfd;
+               return (0);
+
+       default:
+               return (error);
+       }
+       /* NOTREACHED */
 }
 }