speed-ups using the new constants
[unix-history] / usr / src / sys / kern / kern_descrip.c
index 9af2b7c..44e389c 100644 (file)
@@ -1,4 +1,4 @@
-/*     kern_descrip.c  5.9     82/09/08        */
+/*     kern_descrip.c  5.21    82/12/28        */
 
 #include "../h/param.h"
 #include "../h/systm.h"
 
 #include "../h/param.h"
 #include "../h/systm.h"
@@ -9,7 +9,6 @@
 #include "../h/proc.h"
 #include "../h/conf.h"
 #include "../h/file.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/socket.h"
 #include "../h/socketvar.h"
 #include "../h/mount.h"
@@ -49,11 +48,10 @@ getdprop()
                return;
        adtype.dt_type = 0;             /* XXX */
        adtype.dt_protocol = 0;         /* XXX */
                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;
+       u.u_error = copyout((caddr_t)&adtype, (caddr_t)uap->dtypeb,
+           sizeof (struct dtype))
+       if (u.u_error)
                return;
                return;
-       }
 }
 
 getdopt()
 }
 
 getdopt()
@@ -82,7 +80,7 @@ dup()
        j = ufalloc();
        if (j < 0)
                return;
        j = ufalloc();
        if (j < 0)
                return;
-       dupit(j, fp, u.u_pofile[uap->i] & (RDLOCK|WRLOCK));
+       dupit(j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
 }
 
 dup2()
 }
 
 dup2()
@@ -103,13 +101,15 @@ dup2()
        if (uap->i == uap->j)
                return;
        if (u.u_ofile[uap->j]) {
        if (uap->i == uap->j)
                return;
        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; */
        }
                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; */
        }
-       dupit(uap->j, fp, u.u_pofile[uap->i] & (RDLOCK|WRLOCK));
+       dupit(uap->j, fp, u.u_pofile[uap->i] & (UF_SHLOCK|UF_EXLOCK));
 }
 
 dupit(fd, fp, lockflags)
 }
 
 dupit(fd, fp, lockflags)
@@ -121,10 +121,10 @@ 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++;
-       if (lockflags&RDLOCK)
-               fp->f_inode->i_rdlockc++;
-       if (lockflags&WRLOCK)
-               fp->f_inode->i_wrlockc++;
+       if (lockflags&UF_SHLOCK)
+               fp->f_inode->i_shlockc++;
+       if (lockflags&UF_EXLOCK)
+               fp->f_inode->i_exlockc++;
 }
 
 close()
 }
 
 close()
@@ -137,6 +137,10 @@ close()
        fp = getf(uap->i);
        if (fp == 0)
                return;
        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;
        closef(fp, 0, u.u_pofile[uap->i]);
        /* WHAT IF u.u_error ? */
        u.u_ofile[uap->i] = NULL;
@@ -155,11 +159,10 @@ wrap()
        fp = getf(uap->d);
        if (fp == 0)
                return;
        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;
+       u.u_error = copyin((caddr_t)uap->dtypeb, (caddr_t)&adtype,
+           sizeof (struct dtype));
+       if (u.u_error)
                return;
                return;
-       }
        /* DO WRAP */
 }
 
        /* DO WRAP */
 }
 
@@ -171,40 +174,53 @@ int       nselcoll;
 select()
 {
        register struct uap  {
 select()
 {
        register struct uap  {
-               long    *ibits;
-               long    *obits;
+               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;
                struct  timeval *tv;
        } *uap = (struct uap *)u.u_ap;
        int ibits[3], obits[3];
        struct timeval atv;
-       int s, tsel, ncoll, rem;
+       int s, ncoll;
        label_t lqsave;
 
        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) {
        if (uap->tv) {
-               if (copyin((caddr_t)uap->tv, (caddr_t)&atv, sizeof (atv))) {
-                       u.u_error = EFAULT;
-                       return;
-               }
+               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;
                if (itimerfix(&atv)) {
                        u.u_error = EINVAL;
-                       return;
+                       goto done;
                }
                s = spl7(); timevaladd(&atv, &time); splx(s);
        }
                }
                s = spl7(); timevaladd(&atv, &time); splx(s);
        }
-       if (copyin((caddr_t)uap->ibits, (caddr_t)ibits, sizeof (ibits))) {
-               u.u_error = EFAULT;
-               return;
-       }
 retry:
        ncoll = nselcoll;
        u.u_procp->p_flag |= SSEL;
        u.u_r.r_val1 = selscan(ibits, obits);
 retry:
        ncoll = nselcoll;
        u.u_procp->p_flag |= SSEL;
        u.u_r.r_val1 = selscan(ibits, obits);
-       if (u.u_error)
-               return;
-       if (u.u_r.r_val1)
+       if (u.u_error || u.u_r.r_val1)
                goto done;
        s = spl6();
                goto done;
        s = spl6();
-       if (uap->tv && timercmp(&atv, &time, >=)) {
+       if (uap->tv && timercmp(&time, &atv, >=)) {
                splx(s);
                goto done;
        }
                splx(s);
                goto done;
        }
@@ -217,25 +233,32 @@ retry:
        if (uap->tv) {
                lqsave = u.u_qsave;
                if (setjmp(&u.u_qsave)) {
        if (uap->tv) {
                lqsave = u.u_qsave;
                if (setjmp(&u.u_qsave)) {
-                       untimeout(unselect, u.u_procp);
+                       untimeout(unselect, (caddr_t)u.u_procp);
                        u.u_error = EINTR;
                        splx(s);
                        u.u_error = EINTR;
                        splx(s);
-                       return;
+                       goto done;
                }
                }
-               timeout(unselect, u.u_procp, hzto(&atv));
+               timeout(unselect, (caddr_t)u.u_procp, hzto(&atv));
        }
        sleep((caddr_t)&selwait, PZERO+1);
        if (uap->tv) {
                u.u_qsave = lqsave;
        }
        sleep((caddr_t)&selwait, PZERO+1);
        if (uap->tv) {
                u.u_qsave = lqsave;
-               untimeout(unselect, u.u_procp);
+               untimeout(unselect, (caddr_t)u.u_procp);
        }
        splx(s);
        goto retry;
 done:
        }
        splx(s);
        goto retry;
 done:
-       if (copyout((caddr_t)obits, (caddr_t)uap->obits, sizeof (obits))) {
-               u.u_error = EFAULT;
-               return;
+#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
 }
 
 unselect(p)
 }
 
 unselect(p)
@@ -455,8 +478,7 @@ closef(fp, nouser, flags)
                return;
        }
        if (fp->f_type == DTYPE_SOCKET) {
                return;
        }
        if (fp->f_type == DTYPE_SOCKET) {
-               u.u_error = 0;                  /* XXX */
-               soclose(fp->f_socket, nouser);
+               u.u_error = soclose(fp->f_socket, nouser);
                if (nouser == 0 && u.u_error)
                        return;
                fp->f_socket = 0;
                if (nouser == 0 && u.u_error)
                        return;
                fp->f_socket = 0;
@@ -467,7 +489,7 @@ closef(fp, nouser, flags)
        ip = fp->f_inode;
        dev = (dev_t)ip->i_rdev;
        mode = ip->i_mode & IFMT;
        ip = fp->f_inode;
        dev = (dev_t)ip->i_rdev;
        mode = ip->i_mode & IFMT;
-       flags &= RDLOCK|WRLOCK;                 /* conservative */
+       flags &= UF_SHLOCK|UF_EXLOCK;                   /* conservative */
        if (flags)
                funlocki(ip, flags);
        ilock(ip);
        if (flags)
                funlocki(ip, flags);
        ilock(ip);