unmap (user) address space early enough to allow sleep (if releasing
[unix-history] / usr / src / sys / kern / uipc_syscalls.c
index 5786379..8d9fbce 100644 (file)
@@ -1,23 +1,12 @@
 /*
  *
 /*
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)uipc_syscalls.c     7.15 (Berkeley) %G%
+ *     @(#)uipc_syscalls.c     7.24 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-/* #include "user.h" */
-#include "syscontext.h" /* XXX */
+#include "filedesc.h"
 #include "proc.h"
 #include "file.h"
 #include "buf.h"
 #include "proc.h"
 #include "file.h"
 #include "buf.h"
  * System call interface to the socket abstraction.
  */
 
  * System call interface to the socket abstraction.
  */
 
-struct file *getsock();
 extern struct fileops socketops;
 
 extern struct fileops socketops;
 
-socket()
-{
-       register struct a {
+socket(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     domain;
                int     type;
                int     protocol;
                int     domain;
                int     type;
                int     protocol;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
+       struct filedesc *fdp = p->p_fd;
        struct socket *so;
        struct file *fp;
        int fd, error;
 
        struct socket *so;
        struct file *fp;
        int fd, error;
 
-       if (error = falloc(&fp, &fd))
-               RETURN (error);
+       if (error = falloc(p, &fp, &fd))
+               return (error);
        fp->f_flag = FREAD|FWRITE;
        fp->f_type = DTYPE_SOCKET;
        fp->f_ops = &socketops;
        if (error = socreate(uap->domain, &so, uap->type, uap->protocol)) {
        fp->f_flag = FREAD|FWRITE;
        fp->f_type = DTYPE_SOCKET;
        fp->f_ops = &socketops;
        if (error = socreate(uap->domain, &so, uap->type, uap->protocol)) {
-               u.u_ofile[fd] = 0;
-               crfree(fp->f_cred);
-               fp->f_count = 0;
+               fdp->fd_ofiles[fd] = 0;
+               ffree(fp);
        } else {
                fp->f_data = (caddr_t)so;
        } else {
                fp->f_data = (caddr_t)so;
-               u.u_r.r_val1 = fd;
+               *retval = fd;
        }
        }
-       RETURN (error);
+       return (error);
 }
 
 }
 
-bind()
-{
-       register struct a {
+/* ARGSUSED */
+bind(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t name;
                int     namelen;
                int     s;
                caddr_t name;
                int     namelen;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
        struct mbuf *nam;
        int error;
 
        struct mbuf *nam;
        int error;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
        if (error = sockargs(&nam, uap->name, uap->namelen, MT_SONAME))
        if (error = sockargs(&nam, uap->name, uap->namelen, MT_SONAME))
-               RETURN (error);
+               return (error);
        error = sobind((struct socket *)fp->f_data, nam);
        m_freem(nam);
        error = sobind((struct socket *)fp->f_data, nam);
        m_freem(nam);
-       RETURN (error);
+       return (error);
 }
 
 }
 
-listen()
-{
-       register struct a {
+/* ARGSUSED */
+listen(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                int     backlog;
                int     s;
                int     backlog;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
        int error;
 
        int error;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
-       RETURN (solisten((struct socket *)fp->f_data, uap->backlog));
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
+       return (solisten((struct socket *)fp->f_data, uap->backlog));
 }
 
 #ifdef COMPAT_43
 }
 
 #ifdef COMPAT_43
-accept()
-{
-       struct a {
+accept(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     s;
                caddr_t name;
                int     *anamelen;
                int     compat_43;
                int     s;
                caddr_t name;
                int     *anamelen;
                int     compat_43;
-       };
-       ((struct a *)u.u_ap)->compat_43 = 0;
+       } *uap;
+       int *retval;
+{
 
 
-       RETURN (accept1());
+       uap->compat_43 = 0;
+       return (accept1(p, uap, retval));
 }
 
 }
 
-oaccept()
-{
-       struct a {
+oaccept(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     s;
                caddr_t name;
                int     *anamelen;
                int     compat_43;
                int     s;
                caddr_t name;
                int     *anamelen;
                int     compat_43;
-       };
-       ((struct a *)u.u_ap)->compat_43 = 1;
+       } *uap;
+       int *retval;
+{
 
 
-       RETURN (accept1());
+       uap->compat_43 = 1;
+       return (accept1(p, uap, retval));
 }
 #else /* COMPAT_43 */
 
 #define        accept1 accept
 #endif
 
 }
 #else /* COMPAT_43 */
 
 #define        accept1 accept
 #endif
 
-accept1()
-{
-       register struct a {
+accept1(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t name;
                int     *anamelen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
                int     s;
                caddr_t name;
                int     *anamelen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct file *fp;
        struct mbuf *nam;
        int namelen, error, s;
        struct file *fp;
        struct mbuf *nam;
        int namelen, error, s;
@@ -149,8 +149,7 @@ accept1()
        if (uap->name && (error = copyin((caddr_t)uap->anamelen,
            (caddr_t)&namelen, sizeof (namelen))))
                return (error);
        if (uap->name && (error = copyin((caddr_t)uap->anamelen,
            (caddr_t)&namelen, sizeof (namelen))))
                return (error);
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
+       if (error = getsock(p->p_fd, uap->s, &fp))
                return (error);
        s = splnet();
        so = (struct socket *)fp->f_data;
                return (error);
        s = splnet();
        so = (struct socket *)fp->f_data;
@@ -179,7 +178,7 @@ accept1()
                splx(s);
                return (error);
        }
                splx(s);
                return (error);
        }
-       if (error = falloc(&fp, &u.u_r.r_val1)) {
+       if (error = falloc(p, &fp, retval)) {
                splx(s);
                return (error);
        }
                splx(s);
                return (error);
        }
@@ -213,32 +212,34 @@ accept1()
        return (error);
 }
 
        return (error);
 }
 
-connect()
-{
-       register struct a {
+/* ARGSUSED */
+connect(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t name;
                int     namelen;
                int     s;
                caddr_t name;
                int     namelen;
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
        register struct socket *so;
        struct mbuf *nam;
        int error, s;
 
        register struct socket *so;
        struct mbuf *nam;
        int error, s;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
        so = (struct socket *)fp->f_data;
        if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
        so = (struct socket *)fp->f_data;
        if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
-               RETURN (EALREADY);
+               return (EALREADY);
        if (error = sockargs(&nam, uap->name, uap->namelen, MT_SONAME))
        if (error = sockargs(&nam, uap->name, uap->namelen, MT_SONAME))
-               RETURN (error);
+               return (error);
        error = soconnect(so, nam);
        if (error)
                goto bad;
        if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
                m_freem(nam);
        error = soconnect(so, nam);
        if (error)
                goto bad;
        if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
                m_freem(nam);
-               RETURN (EINPROGRESS);
+               return (EINPROGRESS);
        }
        s = splnet();
        while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0)
        }
        s = splnet();
        while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0)
@@ -255,33 +256,36 @@ bad:
        m_freem(nam);
        if (error == ERESTART)
                error = EINTR;
        m_freem(nam);
        if (error == ERESTART)
                error = EINTR;
-       RETURN (error);
+       return (error);
 }
 
 }
 
-socketpair()
-{
-       register struct a {
+socketpair(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     domain;
                int     type;
                int     protocol;
                int     *rsv;
                int     domain;
                int     type;
                int     protocol;
                int     *rsv;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int retval[];
+{
+       register struct filedesc *fdp = p->p_fd;
        struct file *fp1, *fp2;
        struct socket *so1, *so2;
        int fd, error, sv[2];
 
        if (error = socreate(uap->domain, &so1, uap->type, uap->protocol))
        struct file *fp1, *fp2;
        struct socket *so1, *so2;
        int fd, error, sv[2];
 
        if (error = socreate(uap->domain, &so1, uap->type, uap->protocol))
-               RETURN (error);
+               return (error);
        if (error = socreate(uap->domain, &so2, uap->type, uap->protocol))
                goto free1;
        if (error = socreate(uap->domain, &so2, uap->type, uap->protocol))
                goto free1;
-       if (error = falloc(&fp1, &fd))
+       if (error = falloc(p, &fp1, &fd))
                goto free2;
        sv[0] = fd;
        fp1->f_flag = FREAD|FWRITE;
        fp1->f_type = DTYPE_SOCKET;
        fp1->f_ops = &socketops;
        fp1->f_data = (caddr_t)so1;
                goto free2;
        sv[0] = fd;
        fp1->f_flag = FREAD|FWRITE;
        fp1->f_type = DTYPE_SOCKET;
        fp1->f_ops = &socketops;
        fp1->f_data = (caddr_t)so1;
-       if (error = falloc(&fp2, &fd))
+       if (error = falloc(p, &fp2, &fd))
                goto free3;
        fp2->f_flag = FREAD|FWRITE;
        fp2->f_type = DTYPE_SOCKET;
                goto free3;
        fp2->f_flag = FREAD|FWRITE;
        fp2->f_type = DTYPE_SOCKET;
@@ -298,36 +302,37 @@ socketpair()
                        goto free4;
        }
        error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
                        goto free4;
        }
        error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
-       u.u_r.r_val1 = sv[0];           /* XXX ??? */
-       u.u_r.r_val2 = sv[1];           /* XXX ??? */
-       RETURN (error);
+       retval[0] = sv[0];              /* XXX ??? */
+       retval[1] = sv[1];              /* XXX ??? */
+       return (error);
 free4:
 free4:
-       crfree(fp2->f_cred);
-       fp2->f_count = 0;
-       u.u_ofile[sv[1]] = 0;
+       ffree(fp2);
+       fdp->fd_ofiles[sv[1]] = 0;
 free3:
 free3:
-       crfree(fp1->f_cred);
-       fp1->f_count = 0;
-       u.u_ofile[sv[0]] = 0;
+       ffree(fp1);
+       fdp->fd_ofiles[sv[0]] = 0;
 free2:
        (void)soclose(so2);
 free1:
        (void)soclose(so1);
 free2:
        (void)soclose(so2);
 free1:
        (void)soclose(so1);
-       RETURN (error);
+       return (error);
 }
 
 }
 
-sendto()
-{
-       register struct a {
+sendto(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t to;
                int     tolen;
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t to;
                int     tolen;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov;
        struct msghdr msg;
        struct iovec aiov;
+       int error;
 
        msg.msg_name = uap->to;
        msg.msg_namelen = uap->tolen;
 
        msg.msg_name = uap->to;
        msg.msg_namelen = uap->tolen;
@@ -339,18 +344,20 @@ sendto()
 #endif
        aiov.iov_base = uap->buf;
        aiov.iov_len = uap->len;
 #endif
        aiov.iov_base = uap->buf;
        aiov.iov_len = uap->len;
-       RETURN (sendit(uap->s, &msg, uap->flags));
+       return (sendit(p, uap->s, &msg, uap->flags, retval));
 }
 
 #ifdef COMPAT_43
 }
 
 #ifdef COMPAT_43
-osend()
-{
-       register struct a {
+osend(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov;
 
        struct msghdr msg;
        struct iovec aiov;
 
@@ -362,26 +369,28 @@ osend()
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = 0;
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = 0;
-       RETURN (sendit(uap->s, &msg, uap->flags));
+       return (sendit(p, uap->s, &msg, uap->flags, retval));
 }
 
 #define MSG_COMPAT     0x8000
 }
 
 #define MSG_COMPAT     0x8000
-osendmsg()
-{
-       register struct a {
+osendmsg(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t msg;
                int     flags;
                int     s;
                caddr_t msg;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr)))
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr)))
-               RETURN (error);
+               return (error);
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
-                       RETURN (EMSGSIZE);
+                       return (EMSGSIZE);
                MALLOC(iov, struct iovec *,
                      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 
                      M_WAITOK);
                MALLOC(iov, struct iovec *,
                      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV, 
                      M_WAITOK);
@@ -392,30 +401,32 @@ osendmsg()
                goto done;
        msg.msg_flags = MSG_COMPAT;
        msg.msg_iov = iov;
                goto done;
        msg.msg_flags = MSG_COMPAT;
        msg.msg_iov = iov;
-       error = sendit(uap->s, &msg, uap->flags);
+       error = sendit(p, uap->s, &msg, uap->flags, retval);
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
-       RETURN (error);
+       return (error);
 }
 #endif
 
 }
 #endif
 
-sendmsg()
-{
-       register struct a {
+sendmsg(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t msg;
                int     flags;
                int     s;
                caddr_t msg;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg)))
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg)))
-               RETURN (error);
+               return (error);
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
-                       RETURN (EMSGSIZE);
+                       return (EMSGSIZE);
                MALLOC(iov, struct iovec *,
                       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                       M_WAITOK);
                MALLOC(iov, struct iovec *,
                       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                       M_WAITOK);
@@ -429,19 +440,20 @@ sendmsg()
 #ifdef COMPAT_43
        msg.msg_flags = 0;
 #endif
 #ifdef COMPAT_43
        msg.msg_flags = 0;
 #endif
-       error = sendit(uap->s, &msg, uap->flags);
+       error = sendit(p, uap->s, &msg, uap->flags, retval);
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
-       RETURN (error);
+       return (error);
 }
 
 }
 
-sendit(s, mp, flags)
+sendit(p, s, mp, flags, retsize)
+       register struct proc *p;
        int s;
        register struct msghdr *mp;
        int s;
        register struct msghdr *mp;
-       int flags;
+       int flags, *retsize;
 {
 {
-       register struct file *fp;
+       struct file *fp;
        struct uio auio;
        register struct iovec *iov;
        register int i;
        struct uio auio;
        register struct iovec *iov;
        register int i;
@@ -451,13 +463,13 @@ sendit(s, mp, flags)
        struct iovec *ktriov = NULL;
 #endif
        
        struct iovec *ktriov = NULL;
 #endif
        
-       fp = getsock(s, &error);
-       if (fp == 0)
+       if (error = getsock(p->p_fd, s, &fp))
                return (error);
        auio.uio_iov = mp->msg_iov;
        auio.uio_iovcnt = mp->msg_iovlen;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_rw = UIO_WRITE;
                return (error);
        auio.uio_iov = mp->msg_iov;
        auio.uio_iovcnt = mp->msg_iovlen;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_rw = UIO_WRITE;
+       auio.uio_procp = p;
        auio.uio_offset = 0;                    /* XXX */
        auio.uio_resid = 0;
        iov = mp->msg_iov;
        auio.uio_offset = 0;                    /* XXX */
        auio.uio_resid = 0;
        iov = mp->msg_iov;
@@ -476,9 +488,9 @@ sendit(s, mp, flags)
        if (mp->msg_control) {
                if (mp->msg_controllen < sizeof(struct cmsghdr)
 #ifdef COMPAT_43
        if (mp->msg_control) {
                if (mp->msg_controllen < sizeof(struct cmsghdr)
 #ifdef COMPAT_43
-                   && mp->msg_flags != MSG_COMPAT)
+                   && mp->msg_flags != MSG_COMPAT
 #endif
 #endif
-               {
+               {
                        error = EINVAL;
                        goto bad;
                }
                        error = EINVAL;
                        goto bad;
                }
@@ -504,7 +516,7 @@ sendit(s, mp, flags)
        } else
                control = 0;
 #ifdef KTRACE
        } else
                control = 0;
 #ifdef KTRACE
-       if (KTRPOINT(u.u_procp, KTR_GENIO)) {
+       if (KTRPOINT(p, KTR_GENIO)) {
                int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
 
                MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
                int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
 
                MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
@@ -518,15 +530,15 @@ sendit(s, mp, flags)
                    error == EINTR || error == EWOULDBLOCK))
                        error = 0;
                if (error == EPIPE)
                    error == EINTR || error == EWOULDBLOCK))
                        error = 0;
                if (error == EPIPE)
-                       psignal(u.u_procp, SIGPIPE);
+                       psignal(p, SIGPIPE);
        }
        if (error == 0)
        }
        if (error == 0)
-               u.u_r.r_val1 = len - auio.uio_resid;
+               *retsize = len - auio.uio_resid;
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
-                       ktrgenio(u.u_procp->p_tracep, s, UIO_WRITE,
-                               ktriov, u.u_r.r_val1);
+                       ktrgenio(p->p_tracep, s, UIO_WRITE,
+                               ktriov, *retsize, error);
                FREE(ktriov, M_TEMP);
        }
 #endif
                FREE(ktriov, M_TEMP);
        }
 #endif
@@ -537,32 +549,36 @@ bad:
 }
 
 #ifdef COMPAT_43
 }
 
 #ifdef COMPAT_43
-orecvfrom()
-{
-       struct a {
+orecvfrom(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t from;
                int     *fromlenaddr;
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t from;
                int     *fromlenaddr;
-       };
+       } *uap;
+       int *retval;
+{
 
 
-       ((struct a *)u.u_ap)->flags |= MSG_COMPAT;
-       RETURN (recvfrom());
+       uap->flags |= MSG_COMPAT;
+       return (recvfrom(p, uap, retval));
 }
 #endif
 
 }
 #endif
 
-recvfrom()
-{
-       register struct a {
+recvfrom(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t from;
                int     *fromlenaddr;
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                caddr_t from;
                int     *fromlenaddr;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov;
        int error;
        struct msghdr msg;
        struct iovec aiov;
        int error;
@@ -580,18 +596,20 @@ recvfrom()
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = uap->flags;
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = uap->flags;
-       return (recvit(uap->s, &msg, (caddr_t)uap->fromlenaddr));
+       return (recvit(p, uap->s, &msg, (caddr_t)uap->fromlenaddr, retval));
 }
 
 #ifdef COMPAT_43
 }
 
 #ifdef COMPAT_43
-orecv()
-{
-       register struct a {
+orecv(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
                int     s;
                caddr_t buf;
                int     len;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov;
 
        struct msghdr msg;
        struct iovec aiov;
 
@@ -603,7 +621,7 @@ orecv()
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = uap->flags;
        aiov.iov_len = uap->len;
        msg.msg_control = 0;
        msg.msg_flags = uap->flags;
-       RETURN (recvit(uap->s, &msg, (caddr_t)0));
+       return (recvit(p, uap->s, &msg, (caddr_t)0, retval));
 }
 
 /*
 }
 
 /*
@@ -611,23 +629,25 @@ orecv()
  * overlays the new one, missing only the flags, and with the (old) access
  * rights where the control fields are now.
  */
  * overlays the new one, missing only the flags, and with the (old) access
  * rights where the control fields are now.
  */
-orecvmsg()
-{
-       register struct a {
+orecvmsg(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                struct  omsghdr *msg;
                int     flags;
                int     s;
                struct  omsghdr *msg;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
            sizeof (struct omsghdr)))
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *iov;
        int error;
 
        if (error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
            sizeof (struct omsghdr)))
-               RETURN (error);
+               return (error);
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
-                       RETURN (EMSGSIZE);
+                       return (EMSGSIZE);
                MALLOC(iov, struct iovec *,
                      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                      M_WAITOK);
                MALLOC(iov, struct iovec *,
                      sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                      M_WAITOK);
@@ -638,7 +658,7 @@ orecvmsg()
            (unsigned)(msg.msg_iovlen * sizeof (struct iovec))))
                goto done;
        msg.msg_iov = iov;
            (unsigned)(msg.msg_iovlen * sizeof (struct iovec))))
                goto done;
        msg.msg_iov = iov;
-       error = recvit(uap->s, &msg, (caddr_t)&uap->msg->msg_namelen);
+       error = recvit(p, uap->s, &msg, (caddr_t)&uap->msg->msg_namelen, retval);
 
        if (msg.msg_controllen && error == 0)
                error = copyout((caddr_t)&msg.msg_controllen,
 
        if (msg.msg_controllen && error == 0)
                error = copyout((caddr_t)&msg.msg_controllen,
@@ -646,26 +666,28 @@ orecvmsg()
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
-       RETURN (error);
+       return (error);
 }
 #endif
 
 }
 #endif
 
-recvmsg()
-{
-       register struct a {
+recvmsg(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                struct  msghdr *msg;
                int     flags;
                int     s;
                struct  msghdr *msg;
                int     flags;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
        register int error;
 
        if (error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg)))
        struct msghdr msg;
        struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
        register int error;
 
        if (error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg)))
-               RETURN (error);
+               return (error);
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
        if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
                if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
-                       RETURN (EMSGSIZE);
+                       return (EMSGSIZE);
                MALLOC(iov, struct iovec *,
                       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                       M_WAITOK);
                MALLOC(iov, struct iovec *,
                       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
                       M_WAITOK);
@@ -681,22 +703,24 @@ recvmsg()
        if (error = copyin((caddr_t)uiov, (caddr_t)iov,
            (unsigned)(msg.msg_iovlen * sizeof (struct iovec))))
                goto done;
        if (error = copyin((caddr_t)uiov, (caddr_t)iov,
            (unsigned)(msg.msg_iovlen * sizeof (struct iovec))))
                goto done;
-       if ((error = recvit(uap->s, &msg, (caddr_t)0)) == 0) {
+       if ((error = recvit(p, uap->s, &msg, (caddr_t)0, retval)) == 0) {
                msg.msg_iov = uiov;
                error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
        }
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
                msg.msg_iov = uiov;
                error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
        }
 done:
        if (iov != aiov)
                FREE(iov, M_IOV);
-       RETURN (error);
+       return (error);
 }
 
 }
 
-recvit(s, mp, namelenp)
+recvit(p, s, mp, namelenp, retsize)
+       register struct proc *p;
        int s;
        register struct msghdr *mp;
        caddr_t namelenp;
        int s;
        register struct msghdr *mp;
        caddr_t namelenp;
+       int *retsize;
 {
 {
-       register struct file *fp;
+       struct file *fp;
        struct uio auio;
        register struct iovec *iov;
        register int i;
        struct uio auio;
        register struct iovec *iov;
        register int i;
@@ -706,13 +730,13 @@ recvit(s, mp, namelenp)
        struct iovec *ktriov = NULL;
 #endif
        
        struct iovec *ktriov = NULL;
 #endif
        
-       fp = getsock(s, &error);
-       if (fp == 0)
+       if (error = getsock(p->p_fd, s, &fp))
                return (error);
        auio.uio_iov = mp->msg_iov;
        auio.uio_iovcnt = mp->msg_iovlen;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_rw = UIO_READ;
                return (error);
        auio.uio_iov = mp->msg_iov;
        auio.uio_iovcnt = mp->msg_iovlen;
        auio.uio_segflg = UIO_USERSPACE;
        auio.uio_rw = UIO_READ;
+       auio.uio_procp = p;
        auio.uio_offset = 0;                    /* XXX */
        auio.uio_resid = 0;
        iov = mp->msg_iov;
        auio.uio_offset = 0;                    /* XXX */
        auio.uio_resid = 0;
        iov = mp->msg_iov;
@@ -723,7 +747,7 @@ recvit(s, mp, namelenp)
                        return (EINVAL);
        }
 #ifdef KTRACE
                        return (EINVAL);
        }
 #ifdef KTRACE
-       if (KTRPOINT(u.u_procp, KTR_GENIO)) {
+       if (KTRPOINT(p, KTR_GENIO)) {
                int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
 
                MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
                int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
 
                MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
@@ -740,14 +764,14 @@ recvit(s, mp, namelenp)
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
 #ifdef KTRACE
        if (ktriov != NULL) {
                if (error == 0)
-                       ktrgenio(u.u_procp->p_tracep, s, UIO_READ,
-                               ktriov, len - auio.uio_resid);
+                       ktrgenio(p->p_tracep, s, UIO_READ,
+                               ktriov, len - auio.uio_resid, error);
                FREE(ktriov, M_TEMP);
        }
 #endif
        if (error)
                goto out;
                FREE(ktriov, M_TEMP);
        }
 #endif
        if (error)
                goto out;
-       u.u_r.r_val1 = len - auio.uio_resid;
+       *retsize = len - auio.uio_resid;
        if (mp->msg_name) {
                len = mp->msg_namelen;
                if (len <= 0 || from == 0)
        if (mp->msg_name) {
                len = mp->msg_namelen;
                if (len <= 0 || from == 0)
@@ -818,74 +842,80 @@ out:
        return (error);
 }
 
        return (error);
 }
 
-shutdown()
-{
-       struct a {
+/* ARGSUSED */
+shutdown(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                int     how;
                int     s;
                int     how;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct file *fp;
        int error;
 
        struct file *fp;
        int error;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
-       RETURN (soshutdown((struct socket *)fp->f_data, uap->how));
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
+       return (soshutdown((struct socket *)fp->f_data, uap->how));
 }
 
 }
 
-setsockopt()
-{
-       struct a {
+/* ARGSUSED */
+setsockopt(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                int     level;
                int     name;
                caddr_t val;
                int     valsize;
                int     s;
                int     level;
                int     name;
                caddr_t val;
                int     valsize;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct file *fp;
        struct mbuf *m = NULL;
        int error;
 
        struct file *fp;
        struct mbuf *m = NULL;
        int error;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
        if (uap->valsize > MLEN)
        if (uap->valsize > MLEN)
-               RETURN (EINVAL);
+               return (EINVAL);
        if (uap->val) {
                m = m_get(M_WAIT, MT_SOOPTS);
                if (m == NULL)
        if (uap->val) {
                m = m_get(M_WAIT, MT_SOOPTS);
                if (m == NULL)
-                       RETURN (ENOBUFS);
+                       return (ENOBUFS);
                if (error = copyin(uap->val, mtod(m, caddr_t),
                    (u_int)uap->valsize)) {
                        (void) m_free(m);
                if (error = copyin(uap->val, mtod(m, caddr_t),
                    (u_int)uap->valsize)) {
                        (void) m_free(m);
-                       RETURN (error);
+                       return (error);
                }
                m->m_len = uap->valsize;
        }
                }
                m->m_len = uap->valsize;
        }
-       RETURN (sosetopt((struct socket *)fp->f_data, uap->level,
+       return (sosetopt((struct socket *)fp->f_data, uap->level,
            uap->name, m));
 }
 
            uap->name, m));
 }
 
-getsockopt()
-{
-       struct a {
+/* ARGSUSED */
+getsockopt(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     s;
                int     level;
                int     name;
                caddr_t val;
                int     *avalsize;
                int     s;
                int     level;
                int     name;
                caddr_t val;
                int     *avalsize;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
        struct file *fp;
        struct mbuf *m = NULL;
        int valsize, error;
 
        struct file *fp;
        struct mbuf *m = NULL;
        int valsize, error;
 
-       fp = getsock(uap->s, &error);
-       if (fp == 0)
-               RETURN (error);
+       if (error = getsock(p->p_fd, uap->s, &fp))
+               return (error);
        if (uap->val) {
                if (error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
                    sizeof (valsize)))
        if (uap->val) {
                if (error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
                    sizeof (valsize)))
-                       RETURN (error);
+                       return (error);
        } else
                valsize = 0;
        if ((error = sogetopt((struct socket *)fp->f_data, uap->level,
        } else
                valsize = 0;
        if ((error = sogetopt((struct socket *)fp->f_data, uap->level,
@@ -899,100 +929,111 @@ getsockopt()
        }
        if (m != NULL)
                (void) m_free(m);
        }
        if (m != NULL)
                (void) m_free(m);
-       RETURN (error);
+       return (error);
 }
 
 }
 
-pipe()
+/* ARGSUSED */
+pipe(p, uap, retval)
+       struct proc *p;
+       struct args *uap;
+       int retval[];
 {
 {
+       register struct filedesc *fdp = p->p_fd;
        struct file *rf, *wf;
        struct socket *rso, *wso;
        int fd, error;
 
        if (error = socreate(AF_UNIX, &rso, SOCK_STREAM, 0))
        struct file *rf, *wf;
        struct socket *rso, *wso;
        int fd, error;
 
        if (error = socreate(AF_UNIX, &rso, SOCK_STREAM, 0))
-               RETURN (error);
+               return (error);
        if (error = socreate(AF_UNIX, &wso, SOCK_STREAM, 0))
                goto free1;
        if (error = socreate(AF_UNIX, &wso, SOCK_STREAM, 0))
                goto free1;
-       if (error = falloc(&rf, &fd))
+       if (error = falloc(p, &rf, &fd))
                goto free2;
                goto free2;
-       u.u_r.r_val1 = fd;
+       retval[0] = fd;
        rf->f_flag = FREAD;
        rf->f_type = DTYPE_SOCKET;
        rf->f_ops = &socketops;
        rf->f_data = (caddr_t)rso;
        rf->f_flag = FREAD;
        rf->f_type = DTYPE_SOCKET;
        rf->f_ops = &socketops;
        rf->f_data = (caddr_t)rso;
-       if (error = falloc(&wf, &fd))
+       if (error = falloc(p, &wf, &fd))
                goto free3;
        wf->f_flag = FWRITE;
        wf->f_type = DTYPE_SOCKET;
        wf->f_ops = &socketops;
        wf->f_data = (caddr_t)wso;
                goto free3;
        wf->f_flag = FWRITE;
        wf->f_type = DTYPE_SOCKET;
        wf->f_ops = &socketops;
        wf->f_data = (caddr_t)wso;
-       u.u_r.r_val2 = fd;
+       retval[1] = fd;
        if (error = unp_connect2(wso, rso))
                goto free4;
        if (error = unp_connect2(wso, rso))
                goto free4;
-       RETURN (0);
+       return (0);
 free4:
 free4:
-       wf->f_count = 0;
-       u.u_ofile[u.u_r.r_val2] = 0;
+       ffree(wf);
+       fdp->fd_ofiles[retval[1]] = 0;
 free3:
 free3:
-       rf->f_count = 0;
-       u.u_ofile[u.u_r.r_val1] = 0;
+       ffree(rf);
+       fdp->fd_ofiles[retval[0]] = 0;
 free2:
        (void)soclose(wso);
 free1:
        (void)soclose(rso);
 free2:
        (void)soclose(wso);
 free1:
        (void)soclose(rso);
-       RETURN (error);
+       return (error);
 }
 
 /*
  * Get socket name.
  */
 #ifdef COMPAT_43
 }
 
 /*
  * Get socket name.
  */
 #ifdef COMPAT_43
-getsockname()
-{
-       struct a {
+getsockname(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
-       };
-       ((struct a *)u.u_ap)->compat_43 = 0;
+       } *uap;
+       int *retval;
+{
 
 
-       RETURN (getsockname1());
+       uap->compat_43 = 0;
+       return (getsockname1(p, uap, retval));
 }
 
 }
 
-ogetsockname()
-{
-       struct a {
+ogetsockname(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
-       };
-       ((struct a *)u.u_ap)->compat_43 = 1;
+       } *uap;
+       int *retval;
+{
 
 
-       RETURN (getsockname1());
+       uap->compat_43 = 1;
+       return (getsockname1(p, uap, retval));
 }
 #else /* COMPAT_43 */
 
 #define        getsockname1    getsockname
 #endif
 
 }
 #else /* COMPAT_43 */
 
 #define        getsockname1    getsockname
 #endif
 
-getsockname1()
-{
-       register struct a {
+/* ARGSUSED */
+getsockname1(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
                int     fdes;
                caddr_t asa;
                int     *alen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
        register struct socket *so;
        struct mbuf *m;
        int len, error;
 
        register struct socket *so;
        struct mbuf *m;
        int len, error;
 
-       fp = getsock(uap->fdes, &error);
-       if (fp == 0)
+       if (error = getsock(p->p_fd, uap->fdes, &fp))
                return (error);
        if (error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len)))
                return (error);
                return (error);
        if (error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len)))
                return (error);
@@ -1022,53 +1063,59 @@ bad:
  * Get name of peer for connected socket.
  */
 #ifdef COMPAT_43
  * Get name of peer for connected socket.
  */
 #ifdef COMPAT_43
-getpeername()
-{
-       register struct a {
+getpeername(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
 
 
-       ((struct a *)u.u_ap)->compat_43 = 0;
-       getpeername1();
+       uap->compat_43 = 0;
+       return (getpeername1(p, uap, retval));
 }
 
 }
 
-ogetpeername()
-{
-       register struct a {
+ogetpeername(p, uap, retval)
+       struct proc *p;
+       struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
                int     fdes;
                caddr_t asa;
                int     *alen;
                int     compat_43;
-       } *uap = (struct a *)u.u_ap;
+       } *uap;
+       int *retval;
+{
 
 
-       ((struct a *)u.u_ap)->compat_43 = 1;
-       getpeername1();
+       uap->compat_43 = 1;
+       return (getpeername1(p, uap, retval));
 }
 #else /* COMPAT_43 */
 
 #define        getpeername1    getpeername
 #endif
 
 }
 #else /* COMPAT_43 */
 
 #define        getpeername1    getpeername
 #endif
 
-getpeername1()
-{
-       register struct a {
+/* ARGSUSED */
+getpeername1(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     fdes;
                caddr_t asa;
                int     *alen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
                int     fdes;
                caddr_t asa;
                int     *alen;
 #ifdef COMPAT_43
                int     compat_43;
 #endif
-       } *uap = (struct a *)u.u_ap;
-       register struct file *fp;
+       } *uap;
+       int *retval;
+{
+       struct file *fp;
        register struct socket *so;
        struct mbuf *m;
        int len, error;
 
        register struct socket *so;
        struct mbuf *m;
        int len, error;
 
-       fp = getsock(uap->fdes, &error);
-       if (fp == 0)
+       if (error = getsock(p->p_fd, uap->fdes, &fp))
                return (error);
        so = (struct socket *)fp->f_data;
        if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
                return (error);
        so = (struct socket *)fp->f_data;
        if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
@@ -1132,19 +1179,18 @@ sockargs(mp, buf, buflen, type)
        return (error);
 }
 
        return (error);
 }
 
-struct file *
-getsock(fdes, errp)
-       int fdes, *errp;
+getsock(fdp, fdes, fpp)
+       struct filedesc *fdp;
+       int fdes;
+       struct file **fpp;
 {
        register struct file *fp;
 
 {
        register struct file *fp;
 
-       if ((unsigned)fdes >= NOFILE || (fp = u.u_ofile[fdes]) == NULL) {
-               *errp = EBADF;
-               return (0);
-       }
-       if (fp->f_type != DTYPE_SOCKET) {
-               *errp = ENOTSOCK;
-               return (0);
-       }
-       return (fp);
+       if ((unsigned)fdes >= fdp->fd_nfiles ||
+           (fp = fdp->fd_ofiles[fdes]) == NULL)
+               return (EBADF);
+       if (fp->f_type != DTYPE_SOCKET)
+               return (ENOTSOCK);
+       *fpp = fp;
+       return (0);
 }
 }