RETURN => return, remove syscontext.h
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 195f996..8b270ce 100644 (file)
-/*     kern_descrip.c  5.3     82/08/03        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/inode.h"
-#include "../h/proc.h"
-#include "../h/conf.h"
-#include "../h/file.h"
-#include "../h/inline.h"
-#include "../h/socket.h"
-#include "../h/socketvar.h"
-#include "../h/mount.h"
-
-#include "../h/descrip.h"
-
 /*
 /*
- * Descriptor management.
+ * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)kern_descrip.c      7.15 (Berkeley) %G%
  */
 
  */
 
+#include "param.h"
+#include "systm.h"
+#include "user.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"
+
 /*
 /*
- * TODO:
- *     getf should be renamed
- *     ufalloc side effects are gross
+ * Descriptor management.
  */
 
 /*
  * System calls on descriptors.
  */
  */
 
 /*
  * System calls on descriptors.
  */
-dstd()
+/* ARGSUSED */
+getdtablesize(p, uap, retval)
+       struct proc *p;
+       struct args *uap;
+       int *retval;
 {
 
 {
 
-       u.u_r.r_val1 = NOFILE;
+       *retval = NOFILE;
+       return (0);
 }
 
 }
 
-dup()
-{
-       register struct a {
+/*
+ * Duplicate a file descriptor.
+ */
+/* ARGSUSED */
+dup(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     i;
                int     i;
-       } *uap = (struct a *) u.u_ap;
-       register struct file *fp;
-       register int j;
-
-       if (uap->i &~ 077) { uap->i &= 077; dup2(); return; }   /* XXX */
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
+       int fd, error;
 
 
-       fp = getf(uap->i);
-       if (fp == 0)
-               return;
-       j = ufalloc();
-       if (j < 0)
-               return;
-       u.u_ofile[j] = fp;
+       /*
+        * XXX Compatibility
+        */
+       if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); }
+
+       if ((unsigned)uap->i >= NOFILE || (fp = u.u_ofile[uap->i]) == NULL)
+               return (EBADF);
+       if (error = ufalloc(0, &fd))
+               return (error);
+       u.u_ofile[fd] = fp;
+       u.u_pofile[fd] = u.u_pofile[uap->i] &~ UF_EXCLOSE;
        fp->f_count++;
        fp->f_count++;
+       if (fd > u.u_lastfile)
+               u.u_lastfile = fd;
+       *retval = fd;
+       return (0);
 }
 
 }
 
-dup2()
+/*
+ * Duplicate a file descriptor to a particular value.
+ */
+/* ARGSUSED */
+dup2(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     i;
+               int     j;
+       } *uap;
+       int *retval;
 {
 {
-       register struct a {
-               int     i, j;
-       } *uap = (struct a *) u.u_ap;
        register struct file *fp;
        register struct file *fp;
+       int error;
 
 
-       fp = getf(uap->i);
-       if (fp == 0)
-               return;
-       if (uap->j < 0 || uap->j >= NOFILE) {
-               u.u_error = EBADF;
-               return;
-       }
-       u.u_r.r_val1 = uap->j;
+       if ((unsigned)uap->i >= NOFILE || (fp = u.u_ofile[uap->i]) == NULL)
+               return (EBADF);
+       if (uap->j < 0 || uap->j >= NOFILE)
+               return (EBADF);
+       *retval = uap->j;
        if (uap->i == uap->j)
        if (uap->i == uap->j)
-               return;
+               return (0);
        if (u.u_ofile[uap->j]) {
        if (u.u_ofile[uap->j]) {
-               closef(u.u_ofile[uap->j], 0);
-               if (u.u_error)
-                       return;
-               /* u.u_ofile[uap->j] = 0; */
+               if (u.u_pofile[uap->j] & UF_MAPPED)
+                       munmapfd(uap->j);
+               error = closef(u.u_ofile[uap->j]);
        }
        u.u_ofile[uap->j] = fp;
        }
        u.u_ofile[uap->j] = fp;
+       u.u_pofile[uap->j] = u.u_pofile[uap->i] &~ UF_EXCLOSE;
        fp->f_count++;
        fp->f_count++;
+       if (uap->j > u.u_lastfile)
+               u.u_lastfile = uap->j;
+       /*
+        * dup2() must succeed even though the close had an error.
+        */
+       error = 0;              /* XXX */
+       return (error);
 }
 
 }
 
-close()
+/*
+ * The file control system call.
+ */
+/* ARGSUSED */
+fcntl(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fdes;
+               int     cmd;
+               int     arg;
+       } *uap;
+       int *retval;
 {
 {
-       register struct a {
-               int     i;
-       } *uap = (struct a *)u.u_ap;
        register struct file *fp;
        register struct file *fp;
+       register char *pop;
+       int i, error;
+
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               return (EBADF);
+       pop = &u.u_pofile[uap->fdes];
+       switch(uap->cmd) {
+       case F_DUPFD:
+               if (uap->arg < 0 || uap->arg >= NOFILE)
+                       return (EINVAL);
+               if (error = ufalloc(uap->arg, &i))
+                       return (error);
+               u.u_ofile[i] = fp;
+               u.u_pofile[i] = *pop &~ UF_EXCLOSE;
+               fp->f_count++;
+               if (i > u.u_lastfile)
+                       u.u_lastfile = i;
+               *retval = i;
+               return (0);
+
+       case F_GETFD:
+               *retval = *pop & 1;
+               return (0);
+
+       case F_SETFD:
+               *pop = (*pop &~ 1) | (uap->arg & 1);
+               return (0);
+
+       case F_GETFL:
+               *retval = fp->f_flag + FOPEN;
+               return (0);
+
+       case F_SETFL:
+               fp->f_flag &= FCNTLCANT;
+               fp->f_flag |= (uap->arg-FOPEN) &~ FCNTLCANT;
+               if (error = fset(fp, FNDELAY, fp->f_flag & FNDELAY))
+                       return (error);
+               if (error = fset(fp, FASYNC, fp->f_flag & FASYNC))
+                       (void) fset(fp, FNDELAY, 0);
+               return (error);
+
+       case F_GETOWN:
+               return (fgetown(fp, retval));
+
+       case F_SETOWN:
+               return (fsetown(fp, uap->arg));
 
 
-       fp = getf(uap->i);
-       if (fp == 0)
-               return;
-       u.u_ofile[uap->i] = 0;
-       closef(fp, 0);
-       /* WHAT IF u.u_error ? */
+       default:
+               return (EINVAL);
+       }
+       /* NOTREACHED */
 }
 
 }
 
-dtype()
+fset(fp, bit, value)
+       struct file *fp;
+       int bit, value;
 {
 {
-       register struct a {
-               int     d;
-               struct  dtype *dtypeb;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
-       struct dtype adtype;
-
-       fp = getf(uap->d);
-       if (fp == 0)
-               return;
-       adtype.dt_type = 0;             /* XXX */
-       adtype.dt_protocol = 0;         /* XXX */
-       if (copyout((caddr_t)&adtype, (caddr_t)uap->dtypeb,
-           sizeof (struct dtype)) < 0) {
-               u.u_error = EFAULT;
-               return;
-       }
+
+       if (value)
+               fp->f_flag |= bit;
+       else
+               fp->f_flag &= ~bit;
+       return (fioctl(fp, (int)(bit == FNDELAY ? FIONBIO : FIOASYNC),
+           (caddr_t)&value));
 }
 
 }
 
-dwrap()
+fgetown(fp, valuep)
+       struct file *fp;
+       int *valuep;
 {
 {
-       register struct a {
-               int     d;
-               struct  dtype *dtypeb;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
-       struct dtype adtype;
-
-       fp = getf(uap->d);
-       if (fp == 0)
-               return;
-       if (copyin((caddr_t)uap->dtypeb, (caddr_t)&adtype,
-           sizeof (struct dtype)) < 0) {
-               u.u_error = EFAULT;
-               return;
+       int error;
+
+       switch (fp->f_type) {
+
+       case DTYPE_SOCKET:
+               *valuep = ((struct socket *)fp->f_data)->so_pgid;
+               return (0);
+
+       default:
+               error = fioctl(fp, (int)TIOCGPGRP, (caddr_t)valuep);
+               *valuep = -*valuep;
+               return (error);
        }
        }
-       /* DO WRAP */
 }
 
 }
 
-dselect()
+fsetown(fp, value)
+       struct file *fp;
+       int value;
 {
 
 {
 
+       if (fp->f_type == DTYPE_SOCKET) {
+               ((struct socket *)fp->f_data)->so_pgid = value;
+               return (0);
+       }
+       if (value > 0) {
+               struct proc *p = pfind(value);
+               if (p == 0)
+                       return (ESRCH);
+               value = p->p_pgrp->pg_id;
+       } else
+               value = -value;
+       return (fioctl(fp, (int)TIOCSPGRP, (caddr_t)&value));
 }
 
 }
 
-dnblock()
+fioctl(fp, cmd, value)
+       struct file *fp;
+       int cmd;
+       caddr_t value;
 {
 {
-       register struct a {
-               int     d;
-               int     how;
-       } *uap = (struct a *)u.u_ap;
 
 
-       /* XXX */
+       return ((*fp->f_ops->fo_ioctl)(fp, cmd, value));
 }
 
 }
 
-dsignal()
+/*
+ * Close a file descriptor.
+ */
+/* ARGSUSED */
+close(p, uap, retval)
+       struct proc *p;
+       struct args {
+               int     fdes;
+       } *uap;
+       int *retval;
 {
 {
-       register struct a {
-               int     d;
-               int     how;
-       } *uap = (struct a *)u.u_ap;
-
-       /* XXX */
+       register struct file *fp;
+       register u_char *pf;
+
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               return (EBADF);
+       pf = (u_char *)&u.u_pofile[uap->fdes];
+       if (*pf & UF_MAPPED)
+               munmapfd(uap->fdes);
+       u.u_ofile[uap->fdes] = NULL;
+       while (u.u_lastfile >= 0 && u.u_ofile[u.u_lastfile] == NULL)
+               u.u_lastfile--;
+       *pf = 0;
+       return (closef(fp));
 }
 
 }
 
-int    nselcoll;
 /*
 /*
- * Select system call.
+ * Return status information about a file descriptor.
  */
  */
-oselect()
+/* ARGSUSED */
+fstat(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fdes;
+               struct  stat *sb;
+       } *uap;
+       int *retval;
 {
 {
-       register struct uap  {
-               int     nfd;
-               fd_set  *rp, *wp;
-               int     timo;
-       } *ap = (struct uap *)u.u_ap;
-       fd_set rd, wr;
-       int nfds = 0, readable = 0, writeable = 0;
-       time_t t = time;
-       int s, tsel, ncoll, rem;
-
-       if (ap->nfd > NOFILE)
-               ap->nfd = NOFILE;
-       if (ap->nfd < 0) {
-               u.u_error = EBADF;
-               return;
-       }
-       if (ap->rp && copyin((caddr_t)ap->rp,(caddr_t)&rd,sizeof(fd_set)))
-               return;
-       if (ap->wp && copyin((caddr_t)ap->wp,(caddr_t)&wr,sizeof(fd_set)))
-               return;
-retry:
-       ncoll = nselcoll;
-       u.u_procp->p_flag |= SSEL;
-       if (ap->rp)
-               readable = selscan(ap->nfd, rd, &nfds, FREAD);
-       if (ap->wp)
-               writeable = selscan(ap->nfd, wr, &nfds, FWRITE);
-       if (u.u_error)
-               goto done;
-       if (readable || writeable)
-               goto done;
-       rem = (ap->timo+999)/1000 - (time - t);
-       if (ap->timo == 0 || rem <= 0)
-               goto done;
-       s = spl6();
-       if ((u.u_procp->p_flag & SSEL) == 0 || nselcoll != ncoll) {
-               u.u_procp->p_flag &= ~SSEL;
-               splx(s);
-               goto retry;
-       }
-       u.u_procp->p_flag &= ~SSEL;
-       tsel = tsleep((caddr_t)&selwait, PZERO+1, rem);
-       splx(s);
-       switch (tsel) {
-
-       case TS_OK:
-               goto retry;
+       register struct file *fp;
+       struct stat ub;
+       int error;
 
 
-       case TS_SIG:
-               u.u_error = EINTR;
-               return;
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               return (EBADF);
+       switch (fp->f_type) {
 
 
-       case TS_TIME:
+       case DTYPE_VNODE:
+               error = vn_stat((struct vnode *)fp->f_data, &ub);
                break;
                break;
-       }
-done:
-       rd.fds_bits[0] = readable;
-       wr.fds_bits[0] = writeable;
-       s = sizeof (fd_set);
-       if (s * NBBY > ap->nfd)
-               s = (ap->nfd + NBBY - 1) / NBBY;
-       u.u_r.r_val1 = nfds;
-       if (ap->rp)
-               (void) copyout((caddr_t)&rd, (caddr_t)ap->rp, sizeof(fd_set));
-       if (ap->wp)
-               (void) copyout((caddr_t)&wr, (caddr_t)ap->wp, sizeof(fd_set));
-}
 
 
-selscan(nfd, fds, nfdp, flag)
-       int nfd;
-       fd_set fds;
-       int *nfdp, flag;
-{
-       struct file *fp;
-       struct inode *ip;
-       register int bits;
-       int i, able, res = 0;
-               
-       bits = fds.fds_bits[0];
-       while (i = ffs(bits)) {
-               if (i > nfd)
-                       break;
-               bits &= ~(1<<(i-1));
-               fp = u.u_ofile[i-1];
-               if (fp == NULL) {
-                       u.u_error = EBADF;
-                       return (0);
-               }
-               if (fp->f_type == DTYPE_SOCKET)
-                       able = soselect(fp->f_socket, flag);
-               else {
-                       ip = fp->f_inode;
-                       switch (ip->i_mode & IFMT) {
-
-                       case IFCHR:
-                               able =
-                                   (*cdevsw[major(ip->i_rdev)].d_select)
-                                       (ip->i_rdev, flag);
-                               break;
-
-                       case IFBLK:
-                       case IFREG:
-                       case IFDIR:
-                               able = 1;
-                               break;
-                       }
+       case DTYPE_SOCKET:
+               error = soo_stat((struct socket *)fp->f_data, &ub);
+               break;
 
 
-               }
-               if (able) {
-                       res |= (1<<(i-1));
-                       (*nfdp)++;
-               }
+       default:
+               panic("fstat");
+               /*NOTREACHED*/
        }
        }
-       return (res);
+       if (error == 0)
+               error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
+       return (error);
 }
 
 }
 
-/*ARGSUSED*/
-seltrue(dev, flag)
-       dev_t dev;
-       int flag;
-{
-
-       return (1);
-}
-
-selwakeup(p, coll)
-       register struct proc *p;
-       int coll;
+/*
+ * Allocate a user file descriptor.
+ */
+ufalloc(want, result)
+       register int want;
+       int *result;
 {
 {
-       int s;
 
 
-       if (coll) {
-               nselcoll++;
-               wakeup((caddr_t)&selwait);
-       }
-       if (p) {
-               if (p->p_wchan == (caddr_t)&selwait)
-                       setrun(p);
-               else {
-                       s = spl6();
-                       if (p->p_flag & SSEL)
-                               p->p_flag &= ~SSEL;
-                       splx(s);
+       for (; want < NOFILE; want++) {
+               if (u.u_ofile[want] == NULL) {
+                       u.u_pofile[want] = 0;
+                       if (want > u.u_lastfile)
+                               u.u_lastfile = want;
+                       *result = want;
+                       return (0);
                }
        }
                }
        }
+       return (EMFILE);
 }
 
 }
 
-
 /*
 /*
- * Allocate a user file descriptor.
+ * Check to see if any user file descriptors are available.
  */
  */
-ufalloc()
+ufavail()
 {
 {
-       register i;
+       register int i, avail = 0;
 
 
-       for (i=0; i<NOFILE; i++)
-               if (u.u_ofile[i] == NULL) {
-                       u.u_r.r_val1 = i;
-                       u.u_pofile[i] = 0;
-                       return (i);
-               }
-       u.u_error = EMFILE;
-       return (-1);
+       for (i = 0; i < NOFILE; i++)
+               if (u.u_ofile[i] == NULL)
+                       avail++;
+       return (avail);
 }
 
 struct file *lastf;
 }
 
 struct file *lastf;
@@ -340,15 +351,15 @@ struct    file *lastf;
  * Initialize the descriptor
  * to point at the file structure.
  */
  * Initialize the descriptor
  * to point at the file structure.
  */
-struct file *
-falloc()
+falloc(resultfp, resultfd)
+       struct file **resultfp;
+       int *resultfd;
 {
        register struct file *fp;
 {
        register struct file *fp;
-       register i;
+       int error, i;
 
 
-       i = ufalloc();
-       if (i < 0)
-               return (NULL);
+       if (error = ufalloc(0, &i))
+               return (error);
        if (lastf == 0)
                lastf = file;
        for (fp = lastf; fp < fileNFILE; fp++)
        if (lastf == 0)
                lastf = file;
        for (fp = lastf; fp < fileNFILE; fp++)
@@ -358,189 +369,138 @@ falloc()
                if (fp->f_count == 0)
                        goto slot;
        tablefull("file");
                if (fp->f_count == 0)
                        goto slot;
        tablefull("file");
-       u.u_error = ENFILE;
-       return (NULL);
+       return (ENFILE);
 slot:
        u.u_ofile[i] = fp;
 slot:
        u.u_ofile[i] = fp;
-       fp->f_count++;
+       fp->f_count = 1;
+       fp->f_data = 0;
        fp->f_offset = 0;
        fp->f_offset = 0;
-       fp->f_inode = 0;
+       fp->f_cred = u.u_cred;
+       crhold(fp->f_cred);
        lastf = fp + 1;
        lastf = fp + 1;
-       return (fp);
-}
-/*
- * Convert a user supplied file descriptor into a pointer
- * to a file structure.  Only task is to check range of the descriptor.
- * Critical paths should use the GETF macro, defined in inline.h.
- */
-struct file *
-getf(f)
-       register int f;
-{
-       register struct file *fp;
-
-       if ((unsigned)f >= NOFILE || (fp = u.u_ofile[f]) == NULL) {
-               u.u_error = EBADF;
-               return (NULL);
-       }
-       return (fp);
+       if (resultfp)
+               *resultfp = fp;
+       if (resultfd)
+               *resultfd = i;
+       return (0);
 }
 
 /*
  * Internal form of close.
 }
 
 /*
  * Internal form of close.
- * Decrement reference count on
- * file structure.
- * Also make sure the pipe protocol
- * does not constipate.
- *
- * Decrement reference count on the inode following
- * removal to the referencing file structure.
- * Call device handler on last close.
- * Nouser indicates that the user isn't available to present
- * errors to.
+ * Decrement reference count on file structure.
  */
  */
-closef(fp, nouser)
+closef(fp)
        register struct file *fp;
 {
        register struct file *fp;
 {
-       register struct inode *ip;
-       register struct mount *mp;
-       int flag, mode;
-       dev_t dev;
-       register int (*cfunc)();
+       int error;
 
        if (fp == NULL)
 
        if (fp == NULL)
-               return;
+               return (0);
        if (fp->f_count > 1) {
                fp->f_count--;
        if (fp->f_count > 1) {
                fp->f_count--;
-               return;
-       }
-       if (fp->f_type == DTYPE_SOCKET) {
-               u.u_error = 0;                  /* XXX */
-               soclose(fp->f_socket, nouser);
-               if (nouser == 0 && u.u_error)
-                       return;
-               fp->f_socket = 0;
-               fp->f_count = 0;
-               return;
+               return (0);
        }
        }
-       flag = fp->f_flag;
-       ip = fp->f_inode;
-       dev = (dev_t)ip->i_rdev;
-       mode = ip->i_mode & IFMT;
-       ilock(ip);
-       iput(ip);
+       if (fp->f_count < 1)
+               panic("closef: count < 1");
+       error = (*fp->f_ops->fo_close)(fp);
+       crfree(fp->f_cred);
        fp->f_count = 0;
        fp->f_count = 0;
+       return (error);
+}
 
 
-       switch (mode) {
-
-       case IFCHR:
-               cfunc = cdevsw[major(dev)].d_close;
-               break;
-
-       case IFBLK:
-               /*
-                * We don't want to really close the device if it is mounted
-                */
-               for (mp = mount; mp < &mount[NMOUNT]; mp++)
-                       if (mp->m_bufp != NULL && mp->m_dev == dev)
-                               return;
-               cfunc = bdevsw[major(dev)].d_close;
-               break;
+/*
+ * Apply an advisory lock on a file descriptor.
+ */
+/* ARGSUSED */
+flock(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fdes;
+               int     how;
+       } *uap;
+       int *retval;
+{
+       register struct file *fp;
 
 
-       default:
-               return;
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               return (EBADF);
+       if (fp->f_type != DTYPE_VNODE)
+               return (EOPNOTSUPP);
+       if (uap->how & LOCK_UN) {
+               vn_unlock(fp, FSHLOCK|FEXLOCK);
+               return (0);
        }
        }
-       for (fp = file; fp < fileNFILE; fp++) {
-               if (fp->f_type == DTYPE_SOCKET)         /* XXX */
-                       continue;
-               if (fp->f_count && (ip = fp->f_inode) &&
-                   ip->i_rdev == dev && (ip->i_mode&IFMT) == mode)
-                       return;
-       }
-       if (mode == IFBLK) {
-               /*
-                * On last close of a block device (that isn't mounted)
-                * we must invalidate any in core blocks
-                */
-               bflush(dev);
-               binval(dev);
-       }
-       (*cfunc)(dev, flag, fp);
+       if ((uap->how & (LOCK_SH | LOCK_EX)) == 0)
+               return (0);                             /* error? */
+       if (uap->how & LOCK_EX)
+               uap->how &= ~LOCK_SH;
+       /* avoid work... */
+       if ((fp->f_flag & FEXLOCK) && (uap->how & LOCK_EX) ||
+           (fp->f_flag & FSHLOCK) && (uap->how & LOCK_SH))
+               return (0);
+       return (vn_lock(fp, uap->how));
 }
 
 }
 
-#ifdef CAD
 /*
 /*
- * chfile -- change all references to the inode named by
- *          device/inum to the file referred to by fd.
- * Used by init to remove all references to the device.
+ * File Descriptor pseudo-device driver (/dev/fd/).
+ *
+ * Opening minor device N dup()s the file (if any) connected to file
+ * descriptor N belonging to the calling process.  Note that this driver
+ * consists of only the ``open()'' routine, because all subsequent
+ * references to this file will be direct to the other driver.
  */
  */
-chfile()
-{
-       register struct file *fp;
-       register struct inode *from;
-       register struct inode *to;
-       off_t offset;
+/* ARGSUSED */
+fdopen(dev, mode, type)
        dev_t dev;
        dev_t dev;
-       int rw;
-       struct a {
-               int     device;         /* actually dev_t */
-               int     inum;           /* actually ino_t */
-               int     fd;
-       } *uap;
+       int mode, type;
+{
+       struct proc *p = u.u_procp;             /* XXX */
 
 
-       if (!suser()) {
-               u.u_error = EPERM;
-               return;
-       }
-       uap = (struct a *) u.u_ap;
-       fp = getf(uap->fd);
-       if (fp == NULL) {
-               u.u_error = EBADF;
-               return;
-       }
-       if (fp->f_type == DTYPE_SOCKET) {
-               u.u_error = EINVAL;
-               return;
-       }
-       for (from = &inode[0]; from < &inode[ninode]; from++)
-               if (from->i_number == (ino_t)uap->inum
-                  && from->i_dev == (dev_t)uap->device)
-                       break;
-       if (from >= &inode[ninode]) {
-               u.u_error = ENXIO;
-               return;
-       }
-       offset = fp->f_offset;
-       to = fp->f_inode;
-       from->i_count++;
-       for (fp = &file[0]; fp < &file[nfile]; fp++) {
-               if (fp->f_count > 0 && fp->f_inode == from) {
-                       fp->f_inode = to;
-                       to->i_count++;
-                       fp->f_offset = offset;
-                       rw |= fp->f_flag & FWRITE;
-                       iput(from);
-               }
-       }
        /*
        /*
-        * This inode is no longer referenced.
-        * Switch out to the appropriate close
-        * routine, if required
+        * XXX Kludge: set p->p_dupfd to contain the value of the
+        * the file descriptor being sought for duplication. The error 
+        * return ensures that the vnode for this device will be released
+        * by vn_open. Open will detect this special error and take the
+        * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
+        * will simply report the error.
         */
         */
-       dev = (dev_t)from->i_un.i_rdev;
-       switch(from->i_mode & IFMT) {
+       p->p_dupfd = minor(dev);
+       return (ENODEV);
+}
 
 
-       case IFCHR:
-               (*cdevsw[major(dev)].d_close)(dev, rw);
-               break;
+/*
+ * Duplicate the specified descriptor to a free descriptor.
+ */
+dupfdopen(indx, dfd, mode)
+       register int indx, dfd;
+       int mode;
+{
+       register struct file *wfp;
+       struct file *fp;
        
        
-       case IFBLK:
-               (*bdevsw[major(dev)].d_close)(dev, rw);
-               break;
+       /*
+        * If the to-be-dup'd fd number is greater than the allowed number
+        * of file descriptors, or the fd to be dup'd has already been
+        * closed, reject.  Note, check for new == old is necessary as
+        * falloc could allocate an already closed to-be-dup'd descriptor
+        * as the new descriptor.
+        */
+       fp = u.u_ofile[indx];
+       if ((u_int)dfd >= NOFILE || (wfp = u.u_ofile[dfd]) == NULL ||
+           fp == wfp)
+               return (EBADF);
 
 
-       default:
-               break;
-       }
-       iput(from);
+       /*
+        * 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);
+       u.u_ofile[indx] = wfp;
+       u.u_pofile[indx] = u.u_pofile[dfd];
+       wfp->f_count++;
+       if (indx > u.u_lastfile)
+               u.u_lastfile = indx;
+       return (0);
 }
 }
-#endif