add definition for ability to produce a backtrace
[unix-history] / usr / src / sys / kern / sys_generic.c
index 36c99c9..eb7da95 100644 (file)
-/*     sys_generic.c   5.10    82/08/14        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/tty.h"
-#include "../h/file.h"
-#include "../h/inode.h"
-#include "../h/buf.h"
-#include "../h/proc.h"
-#include "../h/inline.h"
-#include "../h/conf.h"
-#include "../h/socket.h"
-#include "../h/socketvar.h"
-#include "../h/cmap.h"
-#include "../h/vlimit.h"
-#include "../h/fs.h"
-#ifdef MUSH
-#include "../h/quota.h"
-#include "../h/share.h"
-#else
-#define        CHARGE(nothing)
+/*
+ * Copyright (c) 1982, 1986, 1989, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)sys_generic.c       8.9 (Berkeley) %G%
+ */
+
+#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
+#include <sys/ktrace.h>
 #endif
 #endif
-#include "../h/descrip.h"
-#include "../h/uio.h"
+
+#include <sys/mount.h>
+#include <sys/syscallargs.h>
 
 /*
  * Read system call.
  */
 
 /*
  * Read system call.
  */
-read()
+/* ARGSUSED */
+int
+read(p, uap, retval)
+       struct proc *p;
+       register struct read_args /* {
+               syscallarg(int) fd;
+               syscallarg(char *) buf;
+               syscallarg(u_int) nbyte;
+       } */ *uap;
+       register_t *retval;
 {
        register struct file *fp;
 {
        register struct file *fp;
-       register struct inode *ip;
-       register struct a {
-               int     fdes;
-               char    *cbuf;
-               unsigned count;
-       } *uap;
+       register struct filedesc *fdp = p->p_fd;
        struct uio auio;
        struct iovec aiov;
        struct uio auio;
        struct iovec aiov;
+       long cnt, error = 0;
+#ifdef KTRACE
+       struct iovec ktriov;
+#endif
 
 
-       uap = (struct a *)u.u_ap;
-       if ((int)uap->count < 0) {
-               u.u_error = EINVAL;
-               return;
-       }
-       GETF(fp, uap->fdes);
-       if ((fp->f_flag&FREAD) == 0) {
-               u.u_error = EBADF;
-               return;
-       }
-       aiov.iov_base = (caddr_t)uap->cbuf;
-       aiov.iov_len = uap->count;
+       if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
+           (fp->f_flag & FREAD) == 0)
+               return (EBADF);
+       aiov.iov_base = (caddr_t)SCARG(uap, buf);
+       aiov.iov_len = SCARG(uap, nbyte);
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
        auio.uio_iov = &aiov;
        auio.uio_iovcnt = 1;
-       auio.uio_segflg = 0;
-       auio.uio_resid = uap->count;
-       u.u_base = (caddr_t)0xc0000000;
-       u.u_count = 0x40000000;
-       if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
-               if (auio.uio_resid == uap->count)
-                       u.u_eosys = RESTARTSYS;
-       } else if (fp->f_type == DTYPE_SOCKET)
-               u.u_error = soreceive(fp->f_socket, (struct sockaddr *)0, &auio);
-       else {
-               ip = fp->f_inode;
-               auio.uio_offset = fp->f_offset;
-               if ((ip->i_mode&IFMT) == IFREG) {
-                       ilock(ip);
-                       u.u_error = readip(ip, &auio);
-                       iunlock(ip);
-               } else
-                       u.u_error = readip(ip, &auio);
-               fp->f_offset += uap->count - auio.uio_resid;
-       }
-       u.u_r.r_val1 = uap->count - auio.uio_resid;
+       auio.uio_resid = SCARG(uap, nbyte);
+       auio.uio_rw = UIO_READ;
+       auio.uio_segflg = UIO_USERSPACE;
+       auio.uio_procp = p;
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(p, KTR_GENIO))
+               ktriov = aiov;
+#endif
+       cnt = SCARG(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))
+                       error = 0;
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_GENIO) && error == 0)
+               ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, &ktriov,
+                   cnt, error);
+#endif
+       *retval = cnt;
+       return (error);
 }
 
 /*
 }
 
 /*
- * Write system call
+ * Scatter read system call.
  */
  */
-write()
+int
+readv(p, uap, retval)
+       struct proc *p;
+       register struct readv_args /* {
+               syscallarg(int) fd;
+               syscallarg(struct iovec *) iovp;
+               syscallarg(u_int) iovcnt;
+       } */ *uap;
+       register_t *retval;
 {
        register struct file *fp;
 {
        register struct file *fp;
-       register struct inode *ip;
-       register struct a {
-               int     fdes;
-               char    *cbuf;
-               unsigned count;
-       } *uap;
-
-       uap = (struct a *)u.u_ap;
-       if ((int)uap->count < 0) {
-               u.u_error = EINVAL;
-               return;
+       register struct filedesc *fdp = p->p_fd;
+       struct uio auio;
+       register struct iovec *iov;
+       struct iovec *needfree;
+       struct iovec aiov[UIO_SMALLIOV];
+       long i, cnt, error = 0;
+       u_int iovlen;
+#ifdef KTRACE
+       struct iovec *ktriov = NULL;
+#endif
+
+       if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
+           (fp->f_flag & FREAD) == 0)
+               return (EBADF);
+       /* note: can't use iovlen until iovcnt is validated */
+       iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
+       if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
+               if (SCARG(uap, iovcnt) > UIO_MAXIOV)
+                       return (EINVAL);
+               MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
+               needfree = iov;
+       } else {
+               iov = aiov;
+               needfree = NULL;
        }
        }
-       GETF(fp, uap->fdes);
-       if ((fp->f_flag&FWRITE) == 0) {
-               u.u_error = EBADF;
-               return;
+       auio.uio_iov = iov;
+       auio.uio_iovcnt = SCARG(uap, iovcnt);
+       auio.uio_rw = UIO_READ;
+       auio.uio_segflg = UIO_USERSPACE;
+       auio.uio_procp = p;
+       if (error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen))
+               goto done;
+       auio.uio_resid = 0;
+       for (i = 0; i < SCARG(uap, iovcnt); i++) {
+               if (auio.uio_resid + iov->iov_len < auio.uio_resid) {
+                       error = EINVAL;
+                       goto done;
+               }
+               auio.uio_resid += iov->iov_len;
+               iov++;
        }
        }
-       u.u_base = (caddr_t)uap->cbuf;
-       u.u_count = uap->count;
-       u.u_segflg = 0;
-       if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
-               if (u.u_count == uap->count)
-                       u.u_eosys = RESTARTSYS;
-       } else if (fp->f_type == DTYPE_SOCKET)
-               u.u_error = sosend(fp->f_socket, (struct sockaddr *)0);
-       else {
-               ip = fp->f_inode;
-               if (fp->f_flag&FAPPEND)
-                       fp->f_offset = ip->i_size;
-               u.u_offset = fp->f_offset;
-               if ((ip->i_mode&IFMT) == IFREG) {
-                       ilock(ip);
-                       writei(ip);
-                       iunlock(ip);
-               } else
-                       writei(ip);
-               fp->f_offset += uap->count - u.u_count;
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(p, KTR_GENIO))  {
+               MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
+               bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
        }
        }
-       u.u_r.r_val1 = uap->count - u.u_count;
-}
-
-readv()
-{
-
+#endif
+       cnt = auio.uio_resid;
+       if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
+               if (auio.uio_resid != cnt && (error == ERESTART ||
+                   error == EINTR || error == EWOULDBLOCK))
+                       error = 0;
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (ktriov != NULL) {
+               if (error == 0)
+                       ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, ktriov,
+                           cnt, error);
+               FREE(ktriov, M_TEMP);
+       }
+#endif
+       *retval = cnt;
+done:
+       if (needfree)
+               FREE(needfree, M_IOV);
+       return (error);
 }
 
 }
 
-writev()
+/*
+ * Write system call
+ */
+int
+write(p, uap, retval)
+       struct proc *p;
+       register struct write_args /* {
+               syscallarg(int) fd;
+               syscallarg(char *) buf;
+               syscallarg(u_int) nbyte;
+       } */ *uap;
+       register_t *retval;
 {
 {
+       register struct file *fp;
+       register struct filedesc *fdp = p->p_fd;
+       struct uio auio;
+       struct iovec aiov;
+       long cnt, error = 0;
+#ifdef KTRACE
+       struct iovec ktriov;
+#endif
 
 
+       if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
+           (fp->f_flag & FWRITE) == 0)
+               return (EBADF);
+       aiov.iov_base = (caddr_t)SCARG(uap, buf);
+       aiov.iov_len = SCARG(uap, nbyte);
+       auio.uio_iov = &aiov;
+       auio.uio_iovcnt = 1;
+       auio.uio_resid = SCARG(uap, nbyte);
+       auio.uio_rw = UIO_WRITE;
+       auio.uio_segflg = UIO_USERSPACE;
+       auio.uio_procp = p;
+#ifdef KTRACE
+       /*
+        * if tracing, save a copy of iovec
+        */
+       if (KTRPOINT(p, KTR_GENIO))
+               ktriov = aiov;
+#endif
+       cnt = SCARG(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))
+                       error = 0;
+               if (error == EPIPE)
+                       psignal(p, SIGPIPE);
+       }
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (KTRPOINT(p, KTR_GENIO) && error == 0)
+               ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
+                   &ktriov, cnt, error);
+#endif
+       *retval = cnt;
+       return (error);
 }
 
 /*
 }
 
 /*
- * Ioctl system call
- * Check legality, execute common code,
- * and switch out to individual device routine.
+ * Gather write system call
  */
  */
-ioctl()
+int
+writev(p, uap, retval)
+       struct proc *p;
+       register struct writev_args /* {
+               syscallarg(int) fd;
+               syscallarg(struct iovec *) iovp;
+               syscallarg(u_int) iovcnt;
+       } */ *uap;
+       register_t *retval;
 {
        register struct file *fp;
 {
        register struct file *fp;
-       struct a {
-               int     fdes;
-               int     cmd;
-               caddr_t cmarg;
-       } *uap;
-       register int com, size;
-       char data[IOCPARM_MASK+1];
-
-       uap = (struct a *)u.u_ap;
-       if ((fp = getf(uap->fdes)) == NULL)
-               return;
-       if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
-               u.u_error = EBADF;
-               return;
-       }
-       com = uap->cmd;
+       register struct filedesc *fdp = p->p_fd;
+       struct uio auio;
+       register struct iovec *iov;
+       struct iovec *needfree;
+       struct iovec aiov[UIO_SMALLIOV];
+       long i, cnt, error = 0;
+       u_int iovlen;
+#ifdef KTRACE
+       struct iovec *ktriov = NULL;
+#endif
 
 
-#ifndef NOCOMPAT
+       if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
+           (fp->f_flag & FWRITE) == 0)
+               return (EBADF);
+       /* note: can't use iovlen until iovcnt is validated */
+       iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
+       if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
+               if (SCARG(uap, iovcnt) > UIO_MAXIOV)
+                       return (EINVAL);
+               MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
+               needfree = iov;
+       } else {
+               iov = aiov;
+               needfree = NULL;
+       }
+       auio.uio_iov = iov;
+       auio.uio_iovcnt = SCARG(uap, iovcnt);
+       auio.uio_rw = UIO_WRITE;
+       auio.uio_segflg = UIO_USERSPACE;
+       auio.uio_procp = p;
+       if (error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen))
+               goto done;
+       auio.uio_resid = 0;
+       for (i = 0; i < SCARG(uap, iovcnt); i++) {
+               if (auio.uio_resid + iov->iov_len < auio.uio_resid) {
+                       error = EINVAL;
+                       goto done;
+               }
+               auio.uio_resid += iov->iov_len;
+               iov++;
+       }
+#ifdef KTRACE
        /*
        /*
-        * Map old style ioctl's into new for the
-        * sake of backwards compatibility (sigh).
+        * if tracing, save a copy of iovec
         */
         */
-       if ((com&~0xffff) == 0) {
-               com = mapioctl(com);
-               if (com == 0) {
-                       u.u_error = EINVAL;
-                       return;
-               }
+       if (KTRPOINT(p, KTR_GENIO))  {
+               MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
+               bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
        }
 #endif
        }
 #endif
-       if (com == FIOCLEX) {
-               u.u_pofile[uap->fdes] |= EXCLOSE;
-               return;
+       cnt = auio.uio_resid;
+       if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
+               if (auio.uio_resid != cnt && (error == ERESTART ||
+                   error == EINTR || error == EWOULDBLOCK))
+                       error = 0;
+               if (error == EPIPE)
+                       psignal(p, SIGPIPE);
        }
        }
-       if (com == FIONCLEX) {
-               u.u_pofile[uap->fdes] &= ~EXCLOSE;
-               return;
+       cnt -= auio.uio_resid;
+#ifdef KTRACE
+       if (ktriov != NULL) {
+               if (error == 0)
+                       ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
+                               ktriov, cnt, error);
+               FREE(ktriov, M_TEMP);
+       }
+#endif
+       *retval = cnt;
+done:
+       if (needfree)
+               FREE(needfree, M_IOV);
+       return (error);
+}
+
+/*
+ * Ioctl system call
+ */
+/* ARGSUSED */
+int
+ioctl(p, uap, retval)
+       struct proc *p;
+       register struct ioctl_args /* {
+               syscallarg(int) fd;
+               syscallarg(u_long) com;
+               syscallarg(caddr_t) data;
+       } */ *uap;
+       register_t *retval;
+{
+       register struct file *fp;
+       register struct filedesc *fdp;
+       register u_long com;
+       register int error;
+       register u_int size;
+       caddr_t data, memp;
+       int tmp;
+#define STK_PARAMS     128
+       char stkbuf[STK_PARAMS];
+
+       fdp = p->p_fd;
+       if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
+               return (EBADF);
+
+       if ((fp->f_flag & (FREAD | FWRITE)) == 0)
+               return (EBADF);
+
+       switch (com = SCARG(uap, com)) {
+       case FIONCLEX:
+               fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
+               return (0);
+       case FIOCLEX:
+               fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
+               return (0);
        }
 
        /*
        }
 
        /*
-        * Interpret high order word to find
-        * amount of data to be copied to/from the
-        * user's address space.
-        * (this'll have to change if we have in+out ioctls)
+        * Interpret high order word to find amount of data to be
+        * copied to/from the user's address space.
         */
         */
-       size = (com &~ (IOC_INOUT|IOC_VOID)) >> 16;
-       if (size > sizeof (data)) {
-               u.u_error = EFAULT;
-               return;
-       }
-       if (com&IOC_IN && size) {
-               if (copyin(uap->cmarg, (caddr_t)data, size)) {
-                       u.u_error = EFAULT;
-                       return;
-               }
+       size = IOCPARM_LEN(com);
+       if (size > IOCPARM_MAX)
+               return (ENOTTY);
+       memp = NULL;
+       if (size > sizeof (stkbuf)) {
+               memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
+               data = memp;
        } else
        } else
-               *(caddr_t *)data = uap->cmarg;
-       /*
-        * Zero the buffer on the stack so the user
-        * always gets back something deterministic.
-        */
-       if ((com&IOC_OUT) && size)
-               bzero((caddr_t)data, size);
-
-       if (fp->f_type == DTYPE_SOCKET)
-               soioctl(fp->f_socket, com, data);
-       else {
-               register struct inode *ip = fp->f_inode;
-               int fmt = ip->i_mode & IFMT;
-               dev_t dev;
-
-               if (fmt != IFCHR) {
-                       if (com == FIONREAD && (fmt == IFREG || fmt == IFDIR)) {
-                               *(off_t *)data = ip->i_size - fp->f_offset;
-                               goto returndata;
+               data = stkbuf;
+       if (com&IOC_IN) {
+               if (size) {
+                       error = copyin(SCARG(uap, data), data, (u_int)size);
+                       if (error) {
+                               if (memp)
+                                       free(memp, M_IOCTLOPS);
+                               return (error);
                        }
                        }
-                       if (com != FIONBIO && com != FIOASYNC)
-                               u.u_error = ENOTTY;
-                       return;
+               } else
+                       *(caddr_t *)data = SCARG(uap, data);
+       } else if ((com&IOC_OUT) && size)
+               /*
+                * Zero the buffer so the user always
+                * gets back something deterministic.
+                */
+               bzero(data, size);
+       else if (com&IOC_VOID)
+               *(caddr_t *)data = SCARG(uap, data);
+
+       switch (com) {
+
+       case FIONBIO:
+               if (tmp = *(int *)data)
+                       fp->f_flag |= FNONBLOCK;
+               else
+                       fp->f_flag &= ~FNONBLOCK;
+               error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
+               break;
+
+       case FIOASYNC:
+               if (tmp = *(int *)data)
+                       fp->f_flag |= FASYNC;
+               else
+                       fp->f_flag &= ~FASYNC;
+               error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
+               break;
+
+       case FIOSETOWN:
+               tmp = *(int *)data;
+               if (fp->f_type == DTYPE_SOCKET) {
+                       ((struct socket *)fp->f_data)->so_pgid = tmp;
+                       error = 0;
+                       break;
                }
                }
-               dev = ip->i_rdev;
-               u.u_r.r_val1 = 0;
-               if ((u.u_procp->p_flag&SNUSIG) && setjmp(u.u_qsav)) {
-                       u.u_eosys = RESTARTSYS;
-                       return;
+               if (tmp <= 0) {
+                       tmp = -tmp;
+               } else {
+                       struct proc *p1 = pfind(tmp);
+                       if (p1 == 0) {
+                               error = ESRCH;
+                               break;
+                       }
+                       tmp = p1->p_pgrp->pg_id;
                }
                }
-               (*cdevsw[major(dev)].d_ioctl)(dev, com, data, 0);
-       }
+               error = (*fp->f_ops->fo_ioctl)
+                       (fp, TIOCSPGRP, (caddr_t)&tmp, p);
+               break;
 
 
-returndata:
-       /*
-        * Copy any data to user, size was
-        * already set and checked above.
-        */
-       if (u.u_error == 0 && com&IOC_OUT)
-               if (size && copyout(data, uap->cmarg, size))
-                       u.u_error = EFAULT;
+       case FIOGETOWN:
+               if (fp->f_type == DTYPE_SOCKET) {
+                       error = 0;
+                       *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
+                       break;
+               }
+               error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
+               *(int *)data = -*(int *)data;
+               break;
+
+       default:
+               error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
+               /*
+                * Copy any data to user, size was
+                * already set and checked above.
+                */
+               if (error == 0 && (com&IOC_OUT) && size)
+                       error = copyout(data, SCARG(uap, data), (u_int)size);
+               break;
+       }
+       if (memp)
+               free(memp, M_IOCTLOPS);
+       return (error);
 }
 
 }
 
+int    selwait, nselcoll;
+
 /*
 /*
- * Do nothing specific version of line
- * discipline specific ioctl command.
+ * Select system call.
  */
  */
-/*ARGSUSED*/
-nullioctl(tp, cmd, data, flags)
-       struct tty *tp;
-       char *data;
-       int flags;
+int
+select(p, uap, retval)
+       register struct proc *p;
+       register struct select_args /* {
+               syscallarg(u_int) nd;
+               syscallarg(fd_set *) in;
+               syscallarg(fd_set *) ou;
+               syscallarg(fd_set *) ex;
+               syscallarg(struct timeval *) tv;
+       } */ *uap;
+       register_t *retval;
 {
 {
+       fd_set ibits[3], obits[3];
+       struct timeval atv;
+       int s, ncoll, error, timo = 0;
+       u_int ni;
 
 
-#ifdef lint
-       tp = tp; data = data; flags = flags;
-#endif
-       return (cmd);
-}
+       bzero((caddr_t)ibits, sizeof(ibits));
+       bzero((caddr_t)obits, sizeof(obits));
+       if (SCARG(uap, nd) > FD_SETSIZE)
+               return (EINVAL);
+       if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
+               /* forgiving; slightly wrong */
+               SCARG(uap, nd) = p->p_fd->fd_nfiles;
+       }
+       ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
 
 
-/*
- * Write the file corresponding to
- * the inode pointed at by the argument.
- * The actual write arguments are found
- * in the variables:
- *     u_base          core address for source
- *     u_offset        byte offset in file
- *     u_count         number of bytes to write
- *     u_segflg        write to kernel/user/user I
- */
-writei(ip)
-       register struct inode *ip;
-{
-       struct buf *bp;
-       register struct fs *fs;
-       dev_t dev;
-       daddr_t lbn, bn;
-       register int on, type;
-       register unsigned n;
-       long bsize;
-       int size, i, count;
-       extern int mem_no;
-
-       dev = (dev_t)ip->i_rdev;
-       if (u.u_offset < 0 && ((ip->i_mode&IFMT) != IFCHR ||
-           mem_no != major(dev)) ) {
-               u.u_error = EINVAL;
-               return;
+#define        getbits(name, x) \
+       if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, name), \
+           (caddr_t)&ibits[x], ni))) \
+               goto done;
+       getbits(in, 0);
+       getbits(ou, 1);
+       getbits(ex, 2);
+#undef getbits
+
+       if (SCARG(uap, tv)) {
+               error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
+                       sizeof (atv));
+               if (error)
+                       goto done;
+               if (itimerfix(&atv)) {
+                       error = EINVAL;
+                       goto done;
+               }
+               s = splclock();
+               timevaladd(&atv, (struct timeval *)&time);
+               splx(s);
        }
        }
-       type = ip->i_mode & IFMT;
-       if (type == IFCHR) {
-               ip->i_flag |= IUPD|ICHG;
-               CHARGE(sc_tio * u.u_count);
-               (*cdevsw[major(dev)].d_write)(dev);
-               return;
+retry:
+       ncoll = nselcoll;
+       p->p_flag |= P_SELECT;
+       error = selscan(p, ibits, obits, SCARG(uap, nd), retval);
+       if (error || *retval)
+               goto done;
+       s = splhigh();
+       if (SCARG(uap, tv)) {
+               if (timercmp(&time, &atv, >=)) {
+                       splx(s);
+                       goto done;
+               }
+               /*
+                * If poll wait was tiny, this could be zero; we will
+                * have to round it up to avoid sleeping forever.  If
+                * we retry below, the timercmp above will get us out.
+                * Note that if wait was 0, the timercmp will prevent
+                * us from getting here the first time.
+                */
+               timo = hzto(&atv);
+               if (timo == 0)
+                       timo = 1;
        }
        }
-       if (u.u_count == 0)
-               return;
-       if ((ip->i_mode & IFMT) == IFREG &&
-           u.u_offset + u.u_count > u.u_limit[LIM_FSIZE]) {
-               psignal(u.u_procp, SIGXFSZ);
-               u.u_error = EMFILE;
-               return;
+       if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
+               splx(s);
+               goto retry;
        }
        }
-       if (type!=IFBLK) {
-               dev = ip->i_dev;
-               fs = ip->i_fs;
-               bsize = fs->fs_bsize;
-       } else
-               bsize = BLKDEV_IOSIZE;
-       do {
-               lbn = u.u_offset / bsize;
-               on = u.u_offset % bsize;
-               n = MIN((unsigned)(bsize - on), u.u_count);
-               if (type != IFBLK) {
-                       bn = fsbtodb(fs, bmap(ip, lbn, B_WRITE, (int)(on + n)));
-                       if (u.u_error || (long)bn<0)
-                               return;
-                       if(u.u_offset + n > ip->i_size &&
-                          (type == IFDIR || type == IFREG || type == IFLNK))
-                               ip->i_size = u.u_offset + n;
-                       size = blksize(fs, ip, lbn);
-               } else {
-                       size = bsize;
-                       bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE);
-               }
-               count = howmany(size, DEV_BSIZE);
-               for (i = 0; i < count; i += CLSIZE)
-                       if (mfind(dev, bn + i))
-                               munhash(dev, bn + i);
-               if (n == bsize) 
-                       bp = getblk(dev, bn, size);
-               else
-                       bp = bread(dev, bn, size);
-               if (u.u_segflg != 1) {
-                       if (copyin(u.u_base, bp->b_un.b_addr + on, n)) {
-                               u.u_error = EFAULT;
-                               goto bad;
-                       }
-               } else
-                       bcopy(u.u_base, bp->b_un.b_addr + on, n);
-               u.u_base += n;
-               u.u_offset += n;
-               u.u_count -= n;
-bad:
-               ;
-               if (u.u_error != 0)
-                       brelse(bp);
-               else {
-                       if ((ip->i_mode&IFMT) == IFDIR)
-                               /*
-                                * Writing to clear a directory entry.
-                                * Must insure the write occurs before
-                                * the inode is freed, or may end up
-                                * pointing at a new (different) file
-                                * if inode is quickly allocated again
-                                * and system crashes.
-                                */
-                               bwrite(bp);
-                       else if (n + on == bsize) {
-                               bp->b_flags |= B_AGE;
-                               bawrite(bp);
-                       } else
-                               bdwrite(bp);
-               }
-               ip->i_flag |= IUPD|ICHG;
-               if (u.u_ruid != 0)
-                       ip->i_mode &= ~(ISUID|ISGID);
-       } while (u.u_error == 0 && u.u_count != 0);
+       p->p_flag &= ~P_SELECT;
+       error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
+       splx(s);
+       if (error == 0)
+               goto retry;
+done:
+       p->p_flag &= ~P_SELECT;
+       /* select is not restarted after signals... */
+       if (error == ERESTART)
+               error = EINTR;
+       if (error == EWOULDBLOCK)
+               error = 0;
+#define        putbits(name, x) \
+       if (SCARG(uap, name) && (error2 = copyout((caddr_t)&obits[x], \
+           (caddr_t)SCARG(uap, name), ni))) \
+               error = error2;
+       if (error == 0) {
+               int error2;
+
+               putbits(in, 0);
+               putbits(ou, 1);
+               putbits(ex, 2);
+#undef putbits
+       }
+       return (error);
 }
 
 }
 
-/*
- * Move n bytes at byte location
- * &bp->b_un.b_addr[o] to/from (flag) the
- * user/kernel (u.segflg) area starting at u.base.
- * Update all the arguments by the number
- * of bytes moved.
- */
-iomove(cp, n, flag)
-       register caddr_t cp;
-       register unsigned n;
+int
+selscan(p, ibits, obits, nfd, retval)
+       struct proc *p;
+       fd_set *ibits, *obits;
+       int nfd;
+       register_t *retval;
 {
 {
-       register int t;
+       register struct filedesc *fdp = p->p_fd;
+       register int msk, i, j, fd;
+       register fd_mask bits;
+       struct file *fp;
+       int n = 0;
+       static int flag[3] = { FREAD, FWRITE, 0 };
 
 
-       if (n==0)
-               return;
-       if (u.u_segflg != 1) {
-               if (flag==B_WRITE)
-                       t = copyin(u.u_base, (caddr_t)cp, n);
-               else
-                       t = copyout((caddr_t)cp, u.u_base, n);
-               if (t) {
-                       u.u_error = EFAULT;
-                       return;
+       for (msk = 0; msk < 3; msk++) {
+               for (i = 0; i < nfd; i += NFDBITS) {
+                       bits = ibits[msk].fds_bits[i/NFDBITS];
+                       while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
+                               bits &= ~(1 << j);
+                               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++;
+                               }
+                       }
                }
                }
-       } else
-               if (flag == B_WRITE)
-                       bcopy(u.u_base, (caddr_t)cp, n);
-               else
-                       bcopy((caddr_t)cp, u.u_base, n);
-       u.u_base += n;
-       u.u_offset += n;
-       u.u_count -= n;
+       }
+       *retval = n;
+       return (0);
 }
 
 }
 
-readip1(ip, base, len, offset, segflg, aresid)
-       struct inode *ip;
-       caddr_t base;
-       int len, offset, segflg;
-       int *aresid;
+/*ARGSUSED*/
+int
+seltrue(dev, flag, p)
+       dev_t dev;
+       int flag;
+       struct proc *p;
 {
 {
-       struct uio auio;
-       struct iovec aiov;
-       int error;
 
 
-       auio.uio_iov = &aiov;
-       auio.uio_iovcnt = 1;
-       aiov.iov_base = base;
-       aiov.iov_len = len;
-       auio.uio_resid = len;
-       auio.uio_offset = offset;
-       auio.uio_segflg = segflg;
-       error = readip(ip, &auio);
-       if (aresid)
-               *aresid = auio.uio_resid;
-       else
-               if (auio.uio_resid)
-                       error = EIO;
-       return (error);
+       return (1);
 }
 
 }
 
-readip(ip, uio)
-       register struct inode *ip;
-       register struct uio *uio;
+/*
+ * Record a select request.
+ */
+void
+selrecord(selector, sip)
+       struct proc *selector;
+       struct selinfo *sip;
 {
 {
-       register struct iovec *iov;
-       struct buf *bp;
-       struct fs *fs;
-       dev_t dev;
-       daddr_t lbn, bn;
-       off_t diff;
-       register int on, type;
-       register unsigned n;
-       int size;
-       long bsize;
-       extern int mem_no;
-       int error = 0;
-
-       dev = (dev_t)ip->i_rdev;
-       if (uio->uio_offset < 0 &&
-           ((ip->i_mode&IFMT) != IFCHR || mem_no != major(dev)))
-               return (EINVAL);
-       ip->i_flag |= IACC;
-       type = ip->i_mode&IFMT;
-       if (type == IFCHR) {
-               register c = u.u_count;
-               (*cdevsw[major(dev)].d_read)(dev, uio);
-               CHARGE(sc_tio * (c - uio->uio_resid));
-               return (u.u_error);
-       }
-       if (type != IFBLK) {
-               dev = ip->i_dev;
-               fs = ip->i_fs;
-               bsize = fs->fs_bsize;
-       } else
-               bsize = BLKDEV_IOSIZE;
-nextiov:
-       if (uio->uio_iovcnt == 0)
-               return (0);
-       iov = uio->uio_iov;
-       if (iov->iov_len > 0)
-       do {
-               lbn = uio->uio_offset / bsize;
-               on = uio->uio_offset % bsize;
-               n = MIN((unsigned)(bsize - on), iov->iov_len);
-               if (type != IFBLK) {
-                       diff = ip->i_size - uio->uio_offset;
-                       if (diff <= 0)
-                               return;
-                       if (diff < n)
-                               n = diff;
-                       bn = fsbtodb(fs, bmap(ip, lbn, B_READ));
-                       if (u.u_error)
-                               return (u.u_error);
-                       size = blksize(fs, ip, lbn);
-               } else {
-                       size = bsize;
-                       bn = lbn * (BLKDEV_IOSIZE/DEV_BSIZE);
-                       rablock = bn + (BLKDEV_IOSIZE/DEV_BSIZE);
-                       rasize = bsize;
-               }
-               if ((long)bn<0) {
-                       bp = geteblk(size);
-                       clrbuf(bp);
-               } else if (ip->i_lastr + 1 == lbn)
-                       bp = breada(dev, bn, size, rablock, rasize);
-               else
-                       bp = bread(dev, bn, size);
-               ip->i_lastr = lbn;
-               n = MIN(n, size - bp->b_resid);
-               if (n != 0) {
-                       if (uio->uio_segflg != 1) {
-                               if (copyout(bp->b_un.b_addr+on, iov->iov_base, n)) {
-                                       brelse(bp);
-                                       return (EFAULT);
-                               }
-                       } else
-                               bcopy(bp->b_un.b_addr + on, iov->iov_base, n);
-                       iov->iov_base += n;
-                       uio->uio_offset += n;
-                       iov->iov_len -= n;
-                       uio->uio_resid -= n;
-               }
-               if (n + on == bsize || uio->uio_offset == ip->i_size)
-                       bp->b_flags |= B_AGE;
-               brelse(bp);
-       } while (u.u_error == 0 && iov->iov_len != 0 && n != 0);
-       if (u.u_error == 0 && iov->iov_len == 0) {
-               uio->uio_iov++;
-               uio->uio_iovcnt--;
-               goto nextiov;
-       }
-       return (error);
+       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;
 }
 
 }
 
-uiomove(cp, n, dir, uio)
-       register caddr_t cp;
-       register unsigned n;
-       enum uio_direction dir;
-       struct uio *uio;
+/*
+ * Do a wakeup when a selectable event occurs.
+ */
+void
+selwakeup(sip)
+       register struct selinfo *sip;
 {
 {
-       register struct iovec *iov;
-       int bad, cnt;
+       register struct proc *p;
+       int s;
 
 
-       if (n == 0)
-               return (0);
-       if (uio->uio_segflg != 1) {
-               if (dir == UIO_READFROM)
-#ifdef notdef
-                       bad = copyuin(uio, (caddr_t)cp, n);
-#else
-                       panic("uiomove");
-#endif
-               else
-                       bad = copyuout((caddr_t)cp, n, uio);
-               if (bad)
-                       return (EFAULT);
-       } else {
-               while (n > 0 && uio->uio_iovcnt) {
-                       iov = uio->uio_iov;
-                       cnt = iov->iov_len;
-                       if (cnt > n)
-                               cnt = n;
-                       if (dir == UIO_READFROM)
-                               bcopy(iov->iov_base, (caddr_t)cp, cnt);
+       if (sip->si_pid == 0)
+               return;
+       if (sip->si_flags & SI_COLL) {
+               nselcoll++;
+               sip->si_flags &= ~SI_COLL;
+               wakeup((caddr_t)&selwait);
+       }
+       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)
+                               setrunnable(p);
                        else
                        else
-                               bcopy((caddr_t)cp, iov->iov_base, cnt);
-                       iov->iov_base += cnt;
-                       iov->iov_len -= cnt;
-                       uio->uio_resid -= cnt;
-                       n -= cnt;
-               }
+                               unsleep(p);
+               } else if (p->p_flag & P_SELECT)
+                       p->p_flag &= ~P_SELECT;
+               splx(s);
        }
        }
-       return (0);
 }
 }