Cleanups for 4.4BSD-Lite
[unix-history] / usr / src / sys / kern / sys_generic.c
index e4e6085..a5db90f 100644 (file)
@@ -1,38 +1,39 @@
 /*
 /*
- * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1982, 1986, 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)sys_generic.c       7.28 (Berkeley) %G%
+ *     @(#)sys_generic.c       8.2 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "filedesc.h"
-#include "ioctl.h"
-#include "file.h"
-#include "socketvar.h"
-#include "proc.h"
-#include "uio.h"
-#include "kernel.h"
-#include "stat.h"
-#include "malloc.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/filedesc.h>
+#include <sys/ioctl.h>
+#include <sys/file.h>
+#include <sys/proc.h>
+#include <sys/socketvar.h>
+#include <sys/uio.h>
+#include <sys/kernel.h>
+#include <sys/stat.h>
+#include <sys/malloc.h>
 #ifdef KTRACE
 #ifdef KTRACE
-#include "ktrace.h"
+#include <sys/ktrace.h>
 #endif
 
 /*
  * Read system call.
  */
 #endif
 
 /*
  * Read system call.
  */
+struct read_args {
+       int     fd;
+       char    *buf;
+       u_int   nbyte;
+};
 /* ARGSUSED */
 read(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 read(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fdes;
-               char    *cbuf;
-               unsigned count;
-       } *uap;
+       register struct read_args *uap;
        int *retval;
 {
        register struct file *fp;
        int *retval;
 {
        register struct file *fp;
@@ -44,15 +45,15 @@ read(p, uap, retval)
        struct iovec ktriov;
 #endif
 
        struct iovec ktriov;
 #endif
 
-       if (((unsigned)uap->fdes) >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
+       if (((u_int)uap->fd) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
            (fp->f_flag & FREAD) == 0)
                return (EBADF);
            (fp->f_flag & FREAD) == 0)
                return (EBADF);
-       aiov.iov_base = (caddr_t)uap->cbuf;
-       aiov.iov_len = uap->count;
+       aiov.iov_base = (caddr_t)uap->buf;
+       aiov.iov_len = uap->nbyte;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
-       auio.uio_resid = uap->count;
+       auio.uio_resid = uap->nbyte;
        auio.uio_rw = UIO_READ;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_procp = p;
        auio.uio_rw = UIO_READ;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_procp = p;
@@ -63,7 +64,7 @@ read(p, uap, retval)
        if (KTRPOINT(p, KTR_GENIO))
                ktriov = aiov;
 #endif
        if (KTRPOINT(p, KTR_GENIO))
                ktriov = aiov;
 #endif
-       cnt = uap->count;
+       cnt = uap->nbyte;
        if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
                if (auio.uio_resid != cnt && (error == ERESTART ||
                    error == EINTR || error == EWOULDBLOCK))
        if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
                if (auio.uio_resid != cnt && (error == ERESTART ||
                    error == EINTR || error == EWOULDBLOCK))
@@ -71,7 +72,7 @@ read(p, uap, retval)
        cnt -= auio.uio_resid;
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_GENIO) && error == 0)
        cnt -= auio.uio_resid;
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_GENIO) && error == 0)
-               ktrgenio(p->p_tracep, uap->fdes, UIO_READ, &ktriov, cnt, error);
+               ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error);
 #endif
        *retval = cnt;
        return (error);
 #endif
        *retval = cnt;
        return (error);
@@ -80,29 +81,29 @@ read(p, uap, retval)
 /*
  * Scatter read system call.
  */
 /*
  * Scatter read system call.
  */
-/* ARGSUSED */
+struct readv_args {
+       int     fdes;
+       struct  iovec *iovp;
+       u_int   iovcnt;
+};
 readv(p, uap, retval)
        struct proc *p;
 readv(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fdes;
-               struct  iovec *iovp;
-               unsigned iovcnt;
-       } *uap;
+       register struct readv_args *uap;
        int *retval;
 {
        register struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        struct uio auio;
        register struct iovec *iov;
        int *retval;
 {
        register struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        struct uio auio;
        register struct iovec *iov;
-       struct iovec *saveiov;
+       struct iovec *needfree;
        struct iovec aiov[UIO_SMALLIOV];
        long i, cnt, error = 0;
        struct iovec aiov[UIO_SMALLIOV];
        long i, cnt, error = 0;
-       unsigned iovlen;
+       u_int iovlen;
 #ifdef KTRACE
        struct iovec *ktriov = NULL;
 #endif
 
 #ifdef KTRACE
        struct iovec *ktriov = NULL;
 #endif
 
-       if (((unsigned)uap->fdes) >= fdp->fd_nfiles ||
+       if (((u_int)uap->fdes) >= fdp->fd_nfiles ||
            (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
            (fp->f_flag & FREAD) == 0)
                return (EBADF);
            (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
            (fp->f_flag & FREAD) == 0)
                return (EBADF);
@@ -112,9 +113,11 @@ readv(p, uap, retval)
                if (uap->iovcnt > UIO_MAXIOV)
                        return (EINVAL);
                MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
                if (uap->iovcnt > UIO_MAXIOV)
                        return (EINVAL);
                MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
-               saveiov = iov;
-       } else
+               needfree = iov;
+       } else {
                iov = aiov;
                iov = aiov;
+               needfree = NULL;
+       }
        auio.uio_iov = iov;
        auio.uio_iovcnt = uap->iovcnt;
        auio.uio_rw = UIO_READ;
        auio.uio_iov = iov;
        auio.uio_iovcnt = uap->iovcnt;
        auio.uio_rw = UIO_READ;
@@ -160,21 +163,22 @@ readv(p, uap, retval)
 #endif
        *retval = cnt;
 done:
 #endif
        *retval = cnt;
 done:
-       if (uap->iovcnt > UIO_SMALLIOV)
-               FREE(saveiov, M_IOV);
+       if (needfree)
+               FREE(needfree, M_IOV);
        return (error);
 }
 
 /*
  * Write system call
  */
        return (error);
 }
 
 /*
  * Write system call
  */
+struct write_args {
+       int     fd;
+       char    *buf;
+       u_int   nbyte;
+};
 write(p, uap, retval)
        struct proc *p;
 write(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fdes;
-               char    *cbuf;
-               unsigned count;
-       } *uap;
+       register struct write_args *uap;
        int *retval;
 {
        register struct file *fp;
        int *retval;
 {
        register struct file *fp;
@@ -186,15 +190,15 @@ write(p, uap, retval)
        struct iovec ktriov;
 #endif
 
        struct iovec ktriov;
 #endif
 
-       if (((unsigned)uap->fdes) >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
+       if (((u_int)uap->fd) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
            (fp->f_flag & FWRITE) == 0)
                return (EBADF);
            (fp->f_flag & FWRITE) == 0)
                return (EBADF);
-       aiov.iov_base = (caddr_t)uap->cbuf;
-       aiov.iov_len = uap->count;
+       aiov.iov_base = (caddr_t)uap->buf;
+       aiov.iov_len = uap->nbyte;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
-       auio.uio_resid = uap->count;
+       auio.uio_resid = uap->nbyte;
        auio.uio_rw = UIO_WRITE;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_procp = p;
        auio.uio_rw = UIO_WRITE;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_procp = p;
@@ -205,7 +209,7 @@ write(p, uap, retval)
        if (KTRPOINT(p, KTR_GENIO))
                ktriov = aiov;
 #endif
        if (KTRPOINT(p, KTR_GENIO))
                ktriov = aiov;
 #endif
-       cnt = uap->count;
+       cnt = uap->nbyte;
        if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
                if (auio.uio_resid != cnt && (error == ERESTART ||
                    error == EINTR || error == EWOULDBLOCK))
        if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
                if (auio.uio_resid != cnt && (error == ERESTART ||
                    error == EINTR || error == EWOULDBLOCK))
@@ -216,7 +220,7 @@ write(p, uap, retval)
        cnt -= auio.uio_resid;
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_GENIO) && error == 0)
        cnt -= auio.uio_resid;
 #ifdef KTRACE
        if (KTRPOINT(p, KTR_GENIO) && error == 0)
-               ktrgenio(p->p_tracep, uap->fdes, UIO_WRITE,
+               ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
                    &ktriov, cnt, error);
 #endif
        *retval = cnt;
                    &ktriov, cnt, error);
 #endif
        *retval = cnt;
@@ -226,29 +230,30 @@ write(p, uap, retval)
 /*
  * Gather write system call
  */
 /*
  * Gather write system call
  */
+struct writev_args {
+       int     fd;
+       struct  iovec *iovp;
+       u_int   iovcnt;
+};
 writev(p, uap, retval)
        struct proc *p;
 writev(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fdes;
-               struct  iovec *iovp;
-               unsigned iovcnt;
-       } *uap;
+       register struct writev_args *uap;
        int *retval;
 {
        register struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        struct uio auio;
        register struct iovec *iov;
        int *retval;
 {
        register struct file *fp;
        register struct filedesc *fdp = p->p_fd;
        struct uio auio;
        register struct iovec *iov;
-       struct iovec *saveiov;
+       struct iovec *needfree;
        struct iovec aiov[UIO_SMALLIOV];
        long i, cnt, error = 0;
        struct iovec aiov[UIO_SMALLIOV];
        long i, cnt, error = 0;
-       unsigned iovlen;
+       u_int iovlen;
 #ifdef KTRACE
        struct iovec *ktriov = NULL;
 #endif
 
 #ifdef KTRACE
        struct iovec *ktriov = NULL;
 #endif
 
-       if (((unsigned)uap->fdes) >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
+       if (((u_int)uap->fd) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
            (fp->f_flag & FWRITE) == 0)
                return (EBADF);
        /* note: can't use iovlen until iovcnt is validated */
            (fp->f_flag & FWRITE) == 0)
                return (EBADF);
        /* note: can't use iovlen until iovcnt is validated */
@@ -257,9 +262,11 @@ writev(p, uap, retval)
                if (uap->iovcnt > UIO_MAXIOV)
                        return (EINVAL);
                MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
                if (uap->iovcnt > UIO_MAXIOV)
                        return (EINVAL);
                MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
-               saveiov = iov;
-       } else
+               needfree = iov;
+       } else {
                iov = aiov;
                iov = aiov;
+               needfree = NULL;
+       }
        auio.uio_iov = iov;
        auio.uio_iovcnt = uap->iovcnt;
        auio.uio_rw = UIO_WRITE;
        auio.uio_iov = iov;
        auio.uio_iovcnt = uap->iovcnt;
        auio.uio_rw = UIO_WRITE;
@@ -301,29 +308,30 @@ writev(p, uap, retval)
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
-                       ktrgenio(p->p_tracep, uap->fdes, UIO_WRITE,
+                       ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
                                ktriov, cnt, error);
                FREE(ktriov, M_TEMP);
        }
 #endif
        *retval = cnt;
 done:
                                ktriov, cnt, error);
                FREE(ktriov, M_TEMP);
        }
 #endif
        *retval = cnt;
 done:
-       if (uap->iovcnt > UIO_SMALLIOV)
-               FREE(saveiov, M_IOV);
+       if (needfree)
+               FREE(needfree, M_IOV);
        return (error);
 }
 
 /*
  * Ioctl system call
  */
        return (error);
 }
 
 /*
  * Ioctl system call
  */
+struct ioctl_args {
+       int     fd;
+       int     com;
+       caddr_t data;
+};
 /* ARGSUSED */
 ioctl(p, uap, retval)
        struct proc *p;
 /* ARGSUSED */
 ioctl(p, uap, retval)
        struct proc *p;
-       register struct args {
-               int     fdes;
-               int     cmd;
-               caddr_t cmarg;
-       } *uap;
+       register struct ioctl_args *uap;
        int *retval;
 {
        register struct file *fp;
        int *retval;
 {
        register struct file *fp;
@@ -336,19 +344,19 @@ ioctl(p, uap, retval)
        caddr_t data = stkbuf;
        int tmp;
 
        caddr_t data = stkbuf;
        int tmp;
 
-       if ((unsigned)uap->fdes >= fdp->fd_nfiles ||
-           (fp = fdp->fd_ofiles[uap->fdes]) == NULL)
+       if ((u_int)uap->fd >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[uap->fd]) == NULL)
                return (EBADF);
        if ((fp->f_flag & (FREAD|FWRITE)) == 0)
                return (EBADF);
                return (EBADF);
        if ((fp->f_flag & (FREAD|FWRITE)) == 0)
                return (EBADF);
-       com = uap->cmd;
+       com = uap->com;
 
        if (com == FIOCLEX) {
 
        if (com == FIOCLEX) {
-               fdp->fd_ofileflags[uap->fdes] |= UF_EXCLOSE;
+               fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
                return (0);
        }
        if (com == FIONCLEX) {
                return (0);
        }
        if (com == FIONCLEX) {
-               fdp->fd_ofileflags[uap->fdes] &= ~UF_EXCLOSE;
+               fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
                return (0);
        }
 
                return (0);
        }
 
@@ -366,14 +374,14 @@ ioctl(p, uap, retval)
        }
        if (com&IOC_IN) {
                if (size) {
        }
        if (com&IOC_IN) {
                if (size) {
-                       error = copyin(uap->cmarg, data, (u_int)size);
+                       error = copyin(uap->data, data, (u_int)size);
                        if (error) {
                                if (memp)
                                        free(memp, M_IOCTLOPS);
                                return (error);
                        }
                } else
                        if (error) {
                                if (memp)
                                        free(memp, M_IOCTLOPS);
                                return (error);
                        }
                } else
-                       *(caddr_t *)data = uap->cmarg;
+                       *(caddr_t *)data = uap->data;
        } else if ((com&IOC_OUT) && size)
                /*
                 * Zero the buffer so the user always
        } else if ((com&IOC_OUT) && size)
                /*
                 * Zero the buffer so the user always
@@ -381,15 +389,15 @@ ioctl(p, uap, retval)
                 */
                bzero(data, size);
        else if (com&IOC_VOID)
                 */
                bzero(data, size);
        else if (com&IOC_VOID)
-               *(caddr_t *)data = uap->cmarg;
+               *(caddr_t *)data = uap->data;
 
        switch (com) {
 
        case FIONBIO:
                if (tmp = *(int *)data)
 
        switch (com) {
 
        case FIONBIO:
                if (tmp = *(int *)data)
-                       fp->f_flag |= FNDELAY;
+                       fp->f_flag |= FNONBLOCK;
                else
                else
-                       fp->f_flag &= ~FNDELAY;
+                       fp->f_flag &= ~FNONBLOCK;
                error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                break;
 
                error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
                break;
 
@@ -439,7 +447,7 @@ ioctl(p, uap, retval)
                 * already set and checked above.
                 */
                if (error == 0 && (com&IOC_OUT) && size)
                 * already set and checked above.
                 */
                if (error == 0 && (com&IOC_OUT) && size)
-                       error = copyout(data, uap->cmarg, (u_int)size);
+                       error = copyout(data, uap->data, (u_int)size);
                break;
        }
        if (memp)
                break;
        }
        if (memp)
@@ -447,37 +455,38 @@ ioctl(p, uap, retval)
        return (error);
 }
 
        return (error);
 }
 
-int    nselcoll;
+int    selwait, nselcoll;
 
 /*
  * Select system call.
  */
 
 /*
  * Select system call.
  */
+struct select_args {
+       u_int   nd;
+       fd_set  *in, *ou, *ex;
+       struct  timeval *tv;
+};
 select(p, uap, retval)
        register struct proc *p;
 select(p, uap, retval)
        register struct proc *p;
-       register struct args {
-               int     nd;
-               fd_set  *in, *ou, *ex;
-               struct  timeval *tv;
-       } *uap;
+       register struct select_args *uap;
        int *retval;
 {
        fd_set ibits[3], obits[3];
        struct timeval atv;
        int *retval;
 {
        fd_set ibits[3], obits[3];
        struct timeval atv;
-       int s, ncoll, ni, error = 0, timo;
+       int s, ncoll, error = 0, timo;
+       u_int ni;
 
        bzero((caddr_t)ibits, sizeof(ibits));
        bzero((caddr_t)obits, sizeof(obits));
 
        bzero((caddr_t)ibits, sizeof(ibits));
        bzero((caddr_t)obits, sizeof(obits));
+       if (uap->nd > FD_SETSIZE)
+               return (EINVAL);
        if (uap->nd > p->p_fd->fd_nfiles)
                uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
        if (uap->nd > p->p_fd->fd_nfiles)
                uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
-       ni = howmany(uap->nd, NFDBITS);
+       ni = howmany(uap->nd, NFDBITS) * sizeof(fd_mask);
 
 #define        getbits(name, x) \
 
 #define        getbits(name, x) \
-       if (uap->name) { \
-               error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], \
-                   (unsigned)(ni * sizeof(fd_mask))); \
-               if (error) \
-                       goto done; \
-       }
+       if (uap->name && \
+           (error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], ni))) \
+               goto done;
        getbits(in, 0);
        getbits(ou, 1);
        getbits(ex, 2);
        getbits(in, 0);
        getbits(ou, 1);
        getbits(ex, 2);
@@ -492,8 +501,15 @@ select(p, uap, retval)
                        error = EINVAL;
                        goto done;
                }
                        error = EINVAL;
                        goto done;
                }
-               s = splhigh(); timevaladd(&atv, &time); splx(s);
+               s = splclock();
+               timevaladd(&atv, (struct timeval *)&time);
                timo = hzto(&atv);
                timo = hzto(&atv);
+               /*
+                * Avoid inadvertently sleeping forever.
+                */
+               if (timo == 0)
+                       timo = 1;
+               splx(s);
        } else
                timo = 0;
 retry:
        } else
                timo = 0;
 retry:
@@ -526,13 +542,12 @@ done:
        if (error == EWOULDBLOCK)
                error = 0;
 #define        putbits(name, x) \
        if (error == EWOULDBLOCK)
                error = 0;
 #define        putbits(name, x) \
-       if (uap->name) { \
-               int error2 = copyout((caddr_t)&obits[x], (caddr_t)uap->name, \
-                   (unsigned)(ni * sizeof(fd_mask))); \
-               if (error2) \
-                       error = error2; \
-       }
+       if (uap->name && \
+           (error2 = copyout((caddr_t)&obits[x], (caddr_t)uap->name, ni))) \
+               error = error2;
        if (error == 0) {
        if (error == 0) {
+               int error2;
+
                putbits(in, 0);
                putbits(ou, 1);
                putbits(ex, 2);
                putbits(in, 0);
                putbits(ou, 1);
                putbits(ex, 2);
@@ -547,69 +562,83 @@ selscan(p, ibits, obits, nfd, retval)
        int nfd, *retval;
 {
        register struct filedesc *fdp = p->p_fd;
        int nfd, *retval;
 {
        register struct filedesc *fdp = p->p_fd;
-       register int which, i, j;
+       register int msk, i, j, fd;
        register fd_mask bits;
        register fd_mask bits;
-       int flag;
        struct file *fp;
        struct file *fp;
-       int error = 0, n = 0;
-
-       for (which = 0; which < 3; which++) {
-               switch (which) {
+       int n = 0;
+       static int flag[3] = { FREAD, FWRITE, 0 };
 
 
-               case 0:
-                       flag = FREAD; break;
-
-               case 1:
-                       flag = FWRITE; break;
-
-               case 2:
-                       flag = 0; break;
-               }
+       for (msk = 0; msk < 3; msk++) {
                for (i = 0; i < nfd; i += NFDBITS) {
                for (i = 0; i < nfd; i += NFDBITS) {
-                       bits = ibits[which].fds_bits[i/NFDBITS];
-                       while ((j = ffs(bits)) && i + --j < nfd) {
+                       bits = ibits[msk].fds_bits[i/NFDBITS];
+                       while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
                                bits &= ~(1 << j);
                                bits &= ~(1 << j);
-                               fp = fdp->fd_ofiles[i + j];
-                               if (fp == NULL) {
-                                       error = EBADF;
-                                       break;
-                               }
-                               if ((*fp->f_ops->fo_select)(fp, flag, p)) {
-                                       FD_SET(i + j, &obits[which]);
+                               fp = fdp->fd_ofiles[fd];
+                               if (fp == NULL)
+                                       return (EBADF);
+                               if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
+                                       FD_SET(fd, &obits[msk]);
                                        n++;
                                }
                        }
                }
        }
        *retval = n;
                                        n++;
                                }
                        }
                }
        }
        *retval = n;
-       return (error);
+       return (0);
 }
 
 /*ARGSUSED*/
 }
 
 /*ARGSUSED*/
-#ifdef __STDC__
-seltrue(dev_t dev, int which, struct proc *p)
-#else
 seltrue(dev, flag, p)
        dev_t dev;
        int flag;
        struct proc *p;
 seltrue(dev, flag, p)
        dev_t dev;
        int flag;
        struct proc *p;
-#endif
 {
 
        return (1);
 }
 
 {
 
        return (1);
 }
 
-selwakeup(p, coll)
-       register struct proc *p;
-       int coll;
+/*
+ * Record a select request.
+ */
+void
+selrecord(selector, sip)
+       struct proc *selector;
+       struct selinfo *sip;
+{
+       struct proc *p;
+       pid_t mypid;
+
+       mypid = selector->p_pid;
+       if (sip->si_pid == mypid)
+               return;
+       if (sip->si_pid && (p = pfind(sip->si_pid)) &&
+           p->p_wchan == (caddr_t)&selwait)
+               sip->si_flags |= SI_COLL;
+       else
+               sip->si_pid = mypid;
+}
+
+/*
+ * Do a wakeup when a selectable event occurs.
+ */
+void
+selwakeup(sip)
+       register struct selinfo *sip;
 {
 {
+       register struct proc *p;
+       int s;
 
 
-       if (coll) {
+       if (sip->si_pid == 0)
+               return;
+       if (sip->si_flags & SI_COLL) {
                nselcoll++;
                nselcoll++;
+               sip->si_flags &= ~SI_COLL;
                wakeup((caddr_t)&selwait);
        }
                wakeup((caddr_t)&selwait);
        }
-       if (p) {
-               int s = splhigh();
+       p = pfind(sip->si_pid);
+       sip->si_pid = 0;
+       if (p != NULL) {
+               s = splhigh();
                if (p->p_wchan == (caddr_t)&selwait) {
                        if (p->p_stat == SSLEEP)
                                setrun(p);
                if (p->p_wchan == (caddr_t)&selwait) {
                        if (p->p_stat == SSLEEP)
                                setrun(p);