RETURN => return, remove syscontext.h
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 32dadca..8b270ce 100644 (file)
-/*     kern_descrip.c  5.22    83/01/12        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/kernel.h"
-#include "../h/inode.h"
-#include "../h/proc.h"
-#include "../h/conf.h"
-#include "../h/file.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.
  */
-getdtablesize()
-{
-
-       u.u_r.r_val1 = NOFILE;
-}
-
-getdprop()
-{
-       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 */
-       u.u_error = copyout((caddr_t)&adtype, (caddr_t)uap->dtypeb,
-           sizeof (struct dtype)); 
-       if (u.u_error)
-               return;
-}
-
-getdopt()
+/* ARGSUSED */
+getdtablesize(p, uap, retval)
+       struct proc *p;
+       struct args *uap;
+       int *retval;
 {
 
 {
 
+       *retval = NOFILE;
+       return (0);
 }
 
 }
 
-setdopt()
-{
-
-}
-
-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;
+       } *uap;
+       int *retval;
+{
        struct file *fp;
        struct file *fp;
-       int j;
+       int fd, error;
 
 
-       if (uap->i &~ 077) { uap->i &= 077; dup2(); return; }   /* XXX */
+       /*
+        * XXX Compatibility
+        */
+       if (uap->i &~ 077) { uap->i &= 077; return (dup2(p, uap, retval)); }
 
 
-       fp = getf(uap->i);
-       if (fp == 0)
-               return;
-       j = ufalloc();
-       if (j < 0)
-               return;
-       dupit(j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
+       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++;
+       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_pofile[uap->j] & UF_MAPPED)
                        munmapfd(uap->j);
        if (u.u_ofile[uap->j]) {
                if (u.u_pofile[uap->j] & UF_MAPPED)
                        munmapfd(uap->j);
-               closef(u.u_ofile[uap->j], 0, u.u_pofile[uap->j]);
-               if (u.u_error)
-                       return;
-               /* u.u_ofile[uap->j] = 0; */
-               /* u.u_pofile[uap->j] = 0; */
+               error = closef(u.u_ofile[uap->j]);
        }
        }
-       dupit(uap->j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
-}
-
-dupit(fd, fp, lockflags)
-       int fd;
-       register struct file *fp;
-       register int lockflags;
-{
-
-       u.u_ofile[fd] = fp;
-       u.u_pofile[fd] = lockflags;
+       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 (lockflags&UF_SHLOCK)
-               fp->f_inode->i_shlockc++;
-       if (lockflags&UF_EXLOCK)
-               fp->f_inode->i_exlockc++;
+       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;
-#ifdef SUNMMAP
-       if (u.u_pofile[uap->i] & UF_MAPPED)
-               munmapfd(uap->i);
-#endif
-       closef(fp, 0, u.u_pofile[uap->i]);
-       /* WHAT IF u.u_error ? */
-       u.u_ofile[uap->i] = NULL;
-       u.u_pofile[uap->i] = 0;
+       default:
+               return (EINVAL);
+       }
+       /* NOTREACHED */
 }
 
 }
 
-wrap()
+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;
-       u.u_error = copyin((caddr_t)uap->dtypeb, (caddr_t)&adtype,
-           sizeof (struct dtype));
-       if (u.u_error)
-               return;
-       /* DO WRAP */
-}
 
 
-int    unselect();
-int    nselcoll;
-/*
- * Select system call.
- */
-select()
-{
-       register struct uap  {
-               int     nd;
-               long    *in;
-               long    *ou;
-               long    *ex;
-               struct  timeval *tv;
-       } *uap = (struct uap *)u.u_ap;
-       int ibits[3], obits[3];
-       struct timeval atv;
-       int s, ncoll;
-       label_t lqsave;
-
-       obits[0] = obits[1] = obits[2] = 0;
-       if (uap->nd > NOFILE)
-               uap->nd = NOFILE;       /* forgiving, if slightly wrong */
-
-#define        getbits(name, x) \
-       if (uap->name) { \
-               u.u_error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], \
-                   sizeof (ibits[x])); \
-               if (u.u_error) \
-                       goto done; \
-       } else \
-               ibits[x] = 0;
-       getbits(in, 0);
-       getbits(ou, 1);
-       getbits(ex, 2);
-#undef getbits
-
-       if (uap->tv) {
-               u.u_error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
-                       sizeof (atv));
-               if (u.u_error)
-                       goto done;
-               if (itimerfix(&atv)) {
-                       u.u_error = EINVAL;
-                       goto done;
-               }
-               s = spl7(); timevaladd(&atv, &time); splx(s);
-       }
-retry:
-       ncoll = nselcoll;
-       u.u_procp->p_flag |= SSEL;
-       u.u_r.r_val1 = selscan(ibits, obits);
-       if (u.u_error || u.u_r.r_val1)
-               goto done;
-       s = spl6();
-       if (uap->tv && timercmp(&time, &atv, >=)) {
-               splx(s);
-               goto done;
-       }
-       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;
-       if (uap->tv) {
-               lqsave = u.u_qsave;
-               if (setjmp(&u.u_qsave)) {
-                       untimeout(unselect, (caddr_t)u.u_procp);
-                       u.u_error = EINTR;
-                       splx(s);
-                       goto done;
-               }
-               timeout(unselect, (caddr_t)u.u_procp, hzto(&atv));
-       }
-       sleep((caddr_t)&selwait, PZERO+1);
-       if (uap->tv) {
-               u.u_qsave = lqsave;
-               untimeout(unselect, (caddr_t)u.u_procp);
-       }
-       splx(s);
-       goto retry;
-done:
-#define        putbits(name, x) \
-       if (uap->name) { \
-               int error = copyout((caddr_t)&obits[x], (caddr_t)uap->name, \
-                   sizeof (obits[x])); \
-               if (error) \
-                       u.u_error = error; \
-       }
-       putbits(in, 0);
-       putbits(ou, 1);
-       putbits(ex, 2);
-#undef putbits
+       if (value)
+               fp->f_flag |= bit;
+       else
+               fp->f_flag &= ~bit;
+       return (fioctl(fp, (int)(bit == FNDELAY ? FIONBIO : FIOASYNC),
+           (caddr_t)&value));
 }
 
 }
 
-unselect(p)
-       register struct proc *p;
+fgetown(fp, valuep)
+       struct file *fp;
+       int *valuep;
 {
 {
-       register int s = spl6();
+       int error;
 
 
-       switch (p->p_stat) {
+       switch (fp->f_type) {
 
 
-       case SSLEEP:
-               setrun(p);
-               break;
+       case DTYPE_SOCKET:
+               *valuep = ((struct socket *)fp->f_data)->so_pgid;
+               return (0);
 
 
-       case SSTOP:
-               unsleep(p);
-               break;
+       default:
+               error = fioctl(fp, (int)TIOCGPGRP, (caddr_t)valuep);
+               *valuep = -*valuep;
+               return (error);
        }
        }
-       splx(s);
 }
 
 }
 
-selscan(ibits, obits)
-       int *ibits, *obits;
-{
-       register int which, bits, i;
-       int flag;
+fsetown(fp, value)
        struct file *fp;
        struct file *fp;
-       int able;
-       struct inode *ip;
-       int n = 0;
-
-       for (which = 0; which < 3; which++) {
-               bits = ibits[which];
-               obits[which] = 0;
-               switch (which) {
-
-               case 0:
-                       flag = FREAD; break;
-
-               case 1:
-                       flag = FWRITE; break;
+       int value;
+{
 
 
-               case 2:
-                       flag = 0; break;
-               }
-               while (i = ffs(bits)) {
-                       bits &= ~(1<<(i-1));
-                       fp = u.u_ofile[i-1];
-                       if (fp == NULL) {
-                               u.u_error = EBADF;
-                               break;
-                       }
-                       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;
-                               }
-
-                       }
-                       if (able) {
-                               obits[which] |= (1<<(i-1));
-                               n++;
-                       }
-               }
+       if (fp->f_type == DTYPE_SOCKET) {
+               ((struct socket *)fp->f_data)->so_pgid = value;
+               return (0);
        }
        }
-       return (n);
+       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));
 }
 
 }
 
-/*ARGSUSED*/
-seltrue(dev, flag)
-       dev_t dev;
-       int flag;
+fioctl(fp, cmd, value)
+       struct file *fp;
+       int cmd;
+       caddr_t value;
 {
 
 {
 
-       return (1);
+       return ((*fp->f_ops->fo_ioctl)(fp, cmd, value));
 }
 
 }
 
-selwakeup(p, coll)
-       register struct proc *p;
-       int coll;
+/*
+ * Close a file descriptor.
+ */
+/* ARGSUSED */
+close(p, uap, retval)
+       struct proc *p;
+       struct args {
+               int     fdes;
+       } *uap;
+       int *retval;
 {
 {
-
-       if (coll) {
-               nselcoll++;
-               wakeup((caddr_t)&selwait);
-       }
-       if (p) {
-               int s = spl6();
-               if (p->p_wchan == (caddr_t)&selwait)
-                       setrun(p);
-               else if (p->p_flag & SSEL)
-                       p->p_flag &= ~SSEL;
-               splx(s);
-       }
+       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));
 }
 
 }
 
-revoke()
+/*
+ * Return status information about a file descriptor.
+ */
+/* ARGSUSED */
+fstat(p, uap, retval)
+       struct proc *p;
+       register struct args {
+               int     fdes;
+               struct  stat *sb;
+       } *uap;
+       int *retval;
 {
 {
+       register struct file *fp;
+       struct stat ub;
+       int error;
 
 
-       /* XXX */
+       if ((unsigned)uap->fdes >= NOFILE ||
+           (fp = u.u_ofile[uap->fdes]) == NULL)
+               return (EBADF);
+       switch (fp->f_type) {
+
+       case DTYPE_VNODE:
+               error = vn_stat((struct vnode *)fp->f_data, &ub);
+               break;
+
+       case DTYPE_SOCKET:
+               error = soo_stat((struct socket *)fp->f_data, &ub);
+               break;
+
+       default:
+               panic("fstat");
+               /*NOTREACHED*/
+       }
+       if (error == 0)
+               error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
+       return (error);
 }
 
 /*
  * Allocate a user file descriptor.
  */
 }
 
 /*
  * Allocate a user file descriptor.
  */
-ufalloc()
+ufalloc(want, result)
+       register int want;
+       int *result;
 {
 {
-       register i;
 
 
-       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);
+       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);
                }
                }
-       u.u_error = EMFILE;
-       return (-1);
+       }
+       return (EMFILE);
+}
+
+/*
+ * Check to see if any user file descriptors are available.
+ */
+ufavail()
+{
+       register int i, avail = 0;
+
+       for (i = 0; i < NOFILE; i++)
+               if (u.u_ofile[i] == NULL)
+                       avail++;
+       return (avail);
 }
 
 struct file *lastf;
 }
 
 struct file *lastf;
@@ -397,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++)
@@ -415,124 +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.
- *
- * Handling locking at this level is RIDICULOUS.
+ * Decrement reference count on file structure.
  */
  */
-closef(fp, nouser, flags)
+closef(fp)
        register struct file *fp;
        register struct file *fp;
-       int nouser, flags;
 {
 {
-       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;
+               return (0);
        }
        }
-       if (fp->f_type == DTYPE_SOCKET) {
-               u.u_error = soclose(fp->f_socket, nouser);
-               if (nouser == 0 && u.u_error)
-                       return;
-               fp->f_socket = 0;
-               fp->f_count = 0;
-               return;
-       }
-       flag = fp->f_flag;
-       ip = fp->f_inode;
-       dev = (dev_t)ip->i_rdev;
-       mode = ip->i_mode & IFMT;
-       flags &= UF_SHLOCK|UF_EXLOCK;                   /* conservative */
-       if (flags)
-               funlocki(ip, flags);
-       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));
 }
 
 }
 
-opause()
+/*
+ * 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.
+ */
+/* ARGSUSED */
+fdopen(dev, mode, type)
+       dev_t dev;
+       int mode, type;
 {
 {
+       struct proc *p = u.u_procp;             /* XXX */
+
+       /*
+        * 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.
+        */
+       p->p_dupfd = minor(dev);
+       return (ENODEV);
+}
 
 
-       for (;;)
-               sleep((caddr_t)&u, PSLEP);
+/*
+ * 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;
+       
+       /*
+        * 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);
+
+       /*
+        * 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);
 }
 }