merge of bill's code plus lint (plus, of course, cleanup of bill's bugs)
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 5bd7a0a..81b5652 100644 (file)
@@ -1,4 +1,4 @@
-/*     kern_descrip.c  5.23    83/01/17        */
+/*     kern_descrip.c  5.24    83/05/27        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -13,7 +13,7 @@
 #include "../h/socketvar.h"
 #include "../h/mount.h"
 
 #include "../h/socketvar.h"
 #include "../h/mount.h"
 
-#include "../h/descrip.h"
+#include "../h/ioctl.h"
 
 /*
  * Descriptor management.
 
 /*
  * Descriptor management.
@@ -21,8 +21,8 @@
 
 /*
  * TODO:
 
 /*
  * TODO:
- *     getf should be renamed
- *     ufalloc side effects are gross
+ *     increase NOFILE
+ *     eliminate u.u_error side effects
  */
 
 /*
  */
 
 /*
@@ -34,26 +34,6 @@ getdtablesize()
        u.u_r.r_val1 = NOFILE;
 }
 
        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()
 {
 
 getdopt()
 {
 
@@ -77,10 +57,10 @@ dup()
        fp = getf(uap->i);
        if (fp == 0)
                return;
        fp = getf(uap->i);
        if (fp == 0)
                return;
-       j = ufalloc();
+       j = ufalloc(0);
        if (j < 0)
                return;
        if (j < 0)
                return;
-       dupit(j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
+       dupit(j, fp, u.u_pofile[uap->i]);
 }
 
 dup2()
 }
 
 dup2()
@@ -103,13 +83,13 @@ dup2()
        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]);
+               closef(u.u_ofile[uap->j], u.u_pofile[uap->j]);
                if (u.u_error)
                        return;
                /* u.u_ofile[uap->j] = 0; */
                /* u.u_pofile[uap->j] = 0; */
        }
                if (u.u_error)
                        return;
                /* u.u_ofile[uap->j] = 0; */
                /* u.u_pofile[uap->j] = 0; */
        }
-       dupit(uap->j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
+       dupit(uap->j, fp, u.u_pofile[uap->i]);
 }
 
 dupit(fd, fp, lockflags)
 }
 
 dupit(fd, fp, lockflags)
@@ -121,264 +101,167 @@ dupit(fd, fp, lockflags)
        u.u_ofile[fd] = fp;
        u.u_pofile[fd] = lockflags;
        fp->f_count++;
        u.u_ofile[fd] = fp;
        u.u_pofile[fd] = lockflags;
        fp->f_count++;
+/* THIS DOESN'T BELONG HERE */
        if (lockflags&UF_SHLOCK)
        if (lockflags&UF_SHLOCK)
-               fp->f_inode->i_shlockc++;
+               ((struct inode *)fp->f_data)->i_shlockc++;
        if (lockflags&UF_EXLOCK)
        if (lockflags&UF_EXLOCK)
-               fp->f_inode->i_exlockc++;
+               ((struct inode *)fp->f_data)->i_exlockc++;
+/* END DOESN'T BELONG */
 }
 
 }
 
-close()
+/*
+ * The file control system call.
+ */
+fcntl()
 {
 {
-       register struct a {
-               int     i;
-       } *uap = (struct a *)u.u_ap;
        register struct file *fp;
        register struct file *fp;
-
-       fp = getf(uap->i);
-       if (fp == 0)
-               return;
-       if (u.u_pofile[uap->i] & UF_MAPPED)
-               munmapfd(uap->i);
-       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;
-}
-
-wrap()
-{
        register struct a {
        register struct a {
-               int     d;
-               struct  dtype *dtypeb;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
-       struct dtype adtype;
+               int     fdes;
+               int     cmd;
+               int     arg;
+       } *uap;
+       register i;
+       register char *pop;
 
 
-       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)
+       uap = (struct a *)u.u_ap;
+       fp = getf(uap->fdes);
+       if (fp == NULL)
                return;
                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)) {
+       pop = &u.u_pofile[uap->fdes];
+       switch(uap->cmd) {
+       case 0:
+               i = uap->arg;
+               if (i < 0 || i > NOFILE) {
                        u.u_error = EINVAL;
                        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;
+                       return;
                }
                }
-               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 ((i = ufalloc(i)) < 0)
+                       return;
+               dupit(i, fp, *pop);
+               break;
 
 
-unselect(p)
-       register struct proc *p;
-{
-       register int s = spl6();
+       case 1:
+               u.u_r.r_val1 = *pop & 1;
+               break;
 
 
-       switch (p->p_stat) {
+       case 2:
+               *pop = (*pop &~ 1) | (uap->arg & 1);
+               break;
 
 
-       case SSLEEP:
-               setrun(p);
+       case 3:
+               u.u_r.r_val1 = fp->f_flag+FOPEN;
                break;
 
                break;
 
-       case SSTOP:
-               unsleep(p);
+       case 4:
+               fp->f_flag &= FCNTLCANT;
+               fp->f_flag |= (uap->arg-FOPEN) &~ FCNTLCANT;
+               u.u_error = fset(fp, FNDELAY, fp->f_flag & FNDELAY);
+               if (u.u_error)
+                       break;
+               u.u_error = fset(fp, FASYNC, fp->f_flag & FASYNC);
+               if (u.u_error)
+                       (void) fset(fp, FNDELAY, 0);
                break;
                break;
+
+       case 5:
+               u.u_error = fsetown(fp, uap->arg);
+               break;
+
+       case 6:
+               u.u_error = fgetown(fp, &u.u_r.r_val1);
+               break;
+
+       default:
+               u.u_error = EINVAL;
        }
        }
-       splx(s);
 }
 
 }
 
-selscan(ibits, obits)
-       int *ibits, *obits;
-{
-       register int which, bits, i;
-       int flag;
+fset(fp, bit, value)
        struct file *fp;
        struct file *fp;
-       int able;
-       struct inode *ip;
-       int n = 0;
+       int bit, value;
+{
 
 
-       for (which = 0; which < 3; which++) {
-               bits = ibits[which];
-               obits[which] = 0;
-               switch (which) {
+       if (value)
+               fp->f_flag |= bit;
+       else
+               fp->f_flag &= ~bit;
+       return (fioctl(fp, (int)(bit == FNDELAY ? FIONBIO : FIOASYNC),
+           (caddr_t)&value));
+}
 
 
-               case 0:
-                       flag = FREAD; break;
+fgetown(fp, valuep)
+       struct file *fp;
+       int *valuep;
+{
+       int error;
 
 
-               case 1:
-                       flag = FWRITE; break;
+       switch (fp->f_type) {
 
 
-               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++;
-                       }
-               }
+       case DTYPE_SOCKET:
+               *valuep = ((struct socket *)fp->f_data)->so_pgrp;
+               return (0);
+
+       default:
+               error = fioctl(fp, (int)TIOCGPGRP, (caddr_t)valuep);
+               *valuep = -*valuep;
+               return (error);
        }
        }
-       return (n);
 }
 
 }
 
-/*ARGSUSED*/
-seltrue(dev, flag)
-       dev_t dev;
-       int flag;
+fsetown(fp, value)
+       struct file *fp;
+       int value;
 {
 
 {
 
-       return (1);
+       if (fp->f_type == DTYPE_SOCKET) {
+               ((struct socket *)fp->f_data)->so_pgrp = value;
+               return (0);
+       }
+       if (value > 0) {
+               struct proc *p = pfind(value);
+               if (p == 0)
+                       return (EINVAL);
+               value = p->p_pgrp;
+       } else
+               value = -value;
+       return (fioctl(fp, (int)TIOCSPGRP, (caddr_t)&value));
 }
 
 }
 
-selwakeup(p, coll)
-       register struct proc *p;
-       int coll;
+fioctl(fp, cmd, value)
+       struct file *fp;
+       int cmd;
+       caddr_t value;
 {
 
 {
 
-       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);
-       }
+       return ((*fp->f_ops->fo_ioctl)(fp, cmd, value));
 }
 
 }
 
-revoke()
+close()
 {
 {
+       register struct a {
+               int     i;
+       } *uap = (struct a *)u.u_ap;
+       register struct file *fp;
 
 
-       /* XXX */
+       fp = getf(uap->i);
+       if (fp == 0)
+               return;
+       if (u.u_pofile[uap->i] & UF_MAPPED)
+               munmapfd(uap->i);
+       closef(fp, u.u_pofile[uap->i]);
+       /* WHAT IF u.u_error ? */
+       u.u_ofile[uap->i] = NULL;
+       u.u_pofile[uap->i] = 0;
 }
 
 /*
  * Allocate a user file descriptor.
  */
 }
 
 /*
  * Allocate a user file descriptor.
  */
-ufalloc()
+ufalloc(i)
+       register int i;
 {
 {
-       register i;
 
 
-       for (i=0; i<NOFILE; i++)
+       for (; i < NOFILE; i++)
                if (u.u_ofile[i] == NULL) {
                        u.u_r.r_val1 = i;
                        u.u_pofile[i] = 0;
                if (u.u_ofile[i] == NULL) {
                        u.u_r.r_val1 = i;
                        u.u_pofile[i] = 0;
@@ -388,6 +271,16 @@ ufalloc()
        return (-1);
 }
 
        return (-1);
 }
 
+ufavail()
+{
+       register int i, avail = 0;
+
+       for (i = 0; i < NOFILE; i++)
+               if (u.u_ofile[i] == NULL)
+                       avail++;
+       return (avail);
+}
+
 struct file *lastf;
 /*
  * Allocate a user file descriptor
 struct file *lastf;
 /*
  * Allocate a user file descriptor
@@ -401,7 +294,7 @@ falloc()
        register struct file *fp;
        register i;
 
        register struct file *fp;
        register i;
 
-       i = ufalloc();
+       i = ufalloc(0);
        if (i < 0)
                return (NULL);
        if (lastf == 0)
        if (i < 0)
                return (NULL);
        if (lastf == 0)
@@ -417,16 +310,17 @@ falloc()
        return (NULL);
 slot:
        u.u_ofile[i] = fp;
        return (NULL);
 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;
        lastf = fp + 1;
        return (fp);
 }
        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.
 /*
  * 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.
+ * Critical paths should use the GETF macro.
  */
 struct file *
 getf(f)
  */
 struct file *
 getf(f)
@@ -443,94 +337,26 @@ getf(f)
 
 /*
  * 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.
+ * If last reference not going away, but no more
+ * references except in message queues, run a
+ * garbage collect.  This would better be done by
+ * forcing a gc() to happen sometime soon, rather
+ * than running one each time.
  */
  */
-closef(fp, nouser, flags)
+closef(fp, flags)
        register struct file *fp;
        register struct file *fp;
-       int nouser, flags;
+       int flags;                                      /* XXX */
 {
 {
-       register struct inode *ip;
-       register struct mount *mp;
-       int flag, mode;
-       dev_t dev;
-       register int (*cfunc)();
 
        if (fp == NULL)
                return;
        if (fp->f_count > 1) {
                fp->f_count--;
 
        if (fp == NULL)
                return;
        if (fp->f_count > 1) {
                fp->f_count--;
+               if (fp->f_count == fp->f_msgcount)
+                       unp_gc();
                return;
        }
                return;
        }
-       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);
+       (*fp->f_ops->fo_close)(fp, flags);
        fp->f_count = 0;
        fp->f_count = 0;
-
-       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;
-
-       default:
-               return;
-       }
-       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);
-}
-
-opause()
-{
-
-       for (;;)
-               sleep((caddr_t)&u, PSLEP);
 }
 }