BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / uipc_usrreq.c
index 4b10f3a..7acde42 100644 (file)
@@ -1,33 +1,48 @@
 /*
 /*
- * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
+ * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution is only permitted until one year after the first shipment
- * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
- * binary forms are permitted provided that: (1) source distributions retain
- * this entire copyright notice and comment, and (2) distributions including
- * binaries display the following acknowledgement:  This product includes
- * software developed by the University of California, Berkeley and its
- * contributors'' in the documentation or other materials provided with the
- * distribution and in all advertising materials mentioning features or use
- * of this software.  Neither the name of the University nor the names of
- * its contributors may 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
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)uipc_usrreq.c       7.21 (Berkeley) 7/27/90
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)uipc_usrreq.c       7.26 (Berkeley) 6/3/91
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "user.h"
+#include "proc.h"
+#include "filedesc.h"
 #include "domain.h"
 #include "protosw.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "unpcb.h"
 #include "un.h"
 #include "domain.h"
 #include "protosw.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "unpcb.h"
 #include "un.h"
+#include "namei.h"
 #include "vnode.h"
 #include "file.h"
 #include "stat.h"
 #include "vnode.h"
 #include "file.h"
 #include "stat.h"
@@ -53,6 +68,7 @@ uipc_usrreq(so, req, m, nam, control)
        struct unpcb *unp = sotounpcb(so);
        register struct socket *so2;
        register int error = 0;
        struct unpcb *unp = sotounpcb(so);
        register struct socket *so2;
        register int error = 0;
+       struct proc *p = curproc;       /* XXX */
 
        if (req == PRU_CONTROL)
                return (EOPNOTSUPP);
 
        if (req == PRU_CONTROL)
                return (EOPNOTSUPP);
@@ -79,7 +95,7 @@ uipc_usrreq(so, req, m, nam, control)
                break;
 
        case PRU_BIND:
                break;
 
        case PRU_BIND:
-               error = unp_bind(unp, nam);
+               error = unp_bind(unp, nam, p);
                break;
 
        case PRU_LISTEN:
                break;
 
        case PRU_LISTEN:
@@ -88,7 +104,7 @@ uipc_usrreq(so, req, m, nam, control)
                break;
 
        case PRU_CONNECT:
                break;
 
        case PRU_CONNECT:
-               error = unp_connect(so, nam);
+               error = unp_connect(so, nam, p);
                break;
 
        case PRU_CONNECT2:
                break;
 
        case PRU_CONNECT2:
@@ -152,7 +168,7 @@ uipc_usrreq(so, req, m, nam, control)
                break;
 
        case PRU_SEND:
                break;
 
        case PRU_SEND:
-               if (control && (error = unp_internalize(control)))
+               if (control && (error = unp_internalize(control, p)))
                        break;
                switch (so->so_type) {
 
                        break;
                switch (so->so_type) {
 
@@ -164,7 +180,7 @@ uipc_usrreq(so, req, m, nam, control)
                                        error = EISCONN;
                                        break;
                                }
                                        error = EISCONN;
                                        break;
                                }
-                               error = unp_connect(so, nam);
+                               error = unp_connect(so, nam, p);
                                if (error)
                                        break;
                        } else {
                                if (error)
                                        break;
                        } else {
@@ -347,16 +363,19 @@ unp_detach(unp)
                unp_gc();
 }
 
                unp_gc();
 }
 
-unp_bind(unp, nam)
+unp_bind(unp, nam, p)
        struct unpcb *unp;
        struct mbuf *nam;
        struct unpcb *unp;
        struct mbuf *nam;
+       struct proc *p;
 {
        struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
        register struct vnode *vp;
 {
        struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
        register struct vnode *vp;
-       register struct nameidata *ndp = &u.u_nd;
+       register struct nameidata *ndp;
        struct vattr vattr;
        int error;
        struct vattr vattr;
        int error;
+       struct nameidata nd;
 
 
+       ndp = &nd;
        ndp->ni_dirp = soun->sun_path;
        if (unp->unp_vnode != NULL)
                return (EINVAL);
        ndp->ni_dirp = soun->sun_path;
        if (unp->unp_vnode != NULL)
                return (EINVAL);
@@ -368,7 +387,7 @@ unp_bind(unp, nam)
 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
        ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT;
        ndp->ni_segflg = UIO_SYSSPACE;
 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
        ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT;
        ndp->ni_segflg = UIO_SYSSPACE;
-       if (error = namei(ndp))
+       if (error = namei(ndp, p))
                return (error);
        vp = ndp->ni_vp;
        if (vp != NULL) {
                return (error);
        vp = ndp->ni_vp;
        if (vp != NULL) {
@@ -383,7 +402,7 @@ unp_bind(unp, nam)
        VATTR_NULL(&vattr);
        vattr.va_type = VSOCK;
        vattr.va_mode = 0777;
        VATTR_NULL(&vattr);
        vattr.va_type = VSOCK;
        vattr.va_mode = 0777;
-       if (error = VOP_CREATE(ndp, &vattr))
+       if (error = VOP_CREATE(ndp, &vattr, p))
                return (error);
        vp = ndp->ni_vp;
        vp->v_socket = unp->unp_socket;
                return (error);
        vp = ndp->ni_vp;
        vp->v_socket = unp->unp_socket;
@@ -393,17 +412,20 @@ unp_bind(unp, nam)
        return (0);
 }
 
        return (0);
 }
 
-unp_connect(so, nam)
+unp_connect(so, nam, p)
        struct socket *so;
        struct mbuf *nam;
        struct socket *so;
        struct mbuf *nam;
+       struct proc *p;
 {
        register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
        register struct vnode *vp;
        register struct socket *so2, *so3;
 {
        register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
        register struct vnode *vp;
        register struct socket *so2, *so3;
-       register struct nameidata *ndp = &u.u_nd;
+       register struct nameidata *ndp;
        struct unpcb *unp2, *unp3;
        int error;
        struct unpcb *unp2, *unp3;
        int error;
+       struct nameidata nd;
 
 
+       ndp = &nd;
        ndp->ni_dirp = soun->sun_path;
        if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {    /* XXX */
                if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
        ndp->ni_dirp = soun->sun_path;
        if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {    /* XXX */
                if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
@@ -412,14 +434,14 @@ unp_connect(so, nam)
                *(mtod(nam, caddr_t) + nam->m_len) = 0;
        ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
        ndp->ni_segflg = UIO_SYSSPACE;
                *(mtod(nam, caddr_t) + nam->m_len) = 0;
        ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
        ndp->ni_segflg = UIO_SYSSPACE;
-       if (error = namei(ndp))
+       if (error = namei(ndp, p))
                return (error);
        vp = ndp->ni_vp;
        if (vp->v_type != VSOCK) {
                error = ENOTSOCK;
                goto bad;
        }
                return (error);
        vp = ndp->ni_vp;
        if (vp->v_type != VSOCK) {
                error = ENOTSOCK;
                goto bad;
        }
-       if (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred))
+       if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p))
                goto bad;
        so2 = vp->v_socket;
        if (so2 == 0) {
                goto bad;
        so2 = vp->v_socket;
        if (so2 == 0) {
@@ -561,6 +583,7 @@ unp_drain()
 unp_externalize(rights)
        struct mbuf *rights;
 {
 unp_externalize(rights)
        struct mbuf *rights;
 {
+       struct proc *p = curproc;               /* XXX */
        register int i;
        register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
        register struct file **rp = (struct file **)(cm + 1);
        register int i;
        register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
        register struct file **rp = (struct file **)(cm + 1);
@@ -568,7 +591,7 @@ unp_externalize(rights)
        int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
        int f;
 
        int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
        int f;
 
-       if (newfds > ufavail()) {
+       if (fdavail(p, newfds)) {
                for (i = 0; i < newfds; i++) {
                        fp = *rp;
                        unp_discard(fp);
                for (i = 0; i < newfds; i++) {
                        fp = *rp;
                        unp_discard(fp);
@@ -577,10 +600,10 @@ unp_externalize(rights)
                return (EMSGSIZE);
        }
        for (i = 0; i < newfds; i++) {
                return (EMSGSIZE);
        }
        for (i = 0; i < newfds; i++) {
-               if (ufalloc(0, &f))
+               if (fdalloc(p, 0, &f))
                        panic("unp_externalize");
                fp = *rp;
                        panic("unp_externalize");
                fp = *rp;
-               u.u_ofile[f] = fp;
+               p->p_fd->fd_ofiles[f] = fp;
                fp->f_msgcount--;
                unp_rights--;
                *(int *)rp++ = f;
                fp->f_msgcount--;
                unp_rights--;
                *(int *)rp++ = f;
@@ -588,9 +611,11 @@ unp_externalize(rights)
        return (0);
 }
 
        return (0);
 }
 
-unp_internalize(control)
+unp_internalize(control, p)
        struct mbuf *control;
        struct mbuf *control;
+       struct proc *p;
 {
 {
+       struct filedesc *fdp = p->p_fd;
        register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
        register struct file **rp;
        register struct file *fp;
        register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
        register struct file **rp;
        register struct file *fp;
@@ -604,12 +629,13 @@ unp_internalize(control)
        rp = (struct file **)(cm + 1);
        for (i = 0; i < oldfds; i++) {
                fd = *(int *)rp++;
        rp = (struct file **)(cm + 1);
        for (i = 0; i < oldfds; i++) {
                fd = *(int *)rp++;
-               if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL)
+               if ((unsigned)fd >= fdp->fd_nfiles ||
+                   fdp->fd_ofiles[fd] == NULL)
                        return (EBADF);
        }
        rp = (struct file **)(cm + 1);
        for (i = 0; i < oldfds; i++) {
                        return (EBADF);
        }
        rp = (struct file **)(cm + 1);
        for (i = 0; i < oldfds; i++) {
-               fp = u.u_ofile[*(int *)rp];
+               fp = fdp->fd_ofiles[*(int *)rp];
                *rp++ = fp;
                fp->f_count++;
                fp->f_msgcount++;
                *rp++ = fp;
                fp->f_count++;
                fp->f_msgcount++;
@@ -632,10 +658,10 @@ unp_gc()
        unp_gcing = 1;
 restart:
        unp_defer = 0;
        unp_gcing = 1;
 restart:
        unp_defer = 0;
-       for (fp = file; fp < fileNFILE; fp++)
+       for (fp = filehead; fp; fp = fp->f_filef)
                fp->f_flag &= ~(FMARK|FDEFER);
        do {
                fp->f_flag &= ~(FMARK|FDEFER);
        do {
-               for (fp = file; fp < fileNFILE; fp++) {
+               for (fp = filehead; fp; fp = fp->f_filef) {
                        if (fp->f_count == 0)
                                continue;
                        if (fp->f_flag & FDEFER) {
                        if (fp->f_count == 0)
                                continue;
                        if (fp->f_flag & FDEFER) {
@@ -673,7 +699,7 @@ restart:
                        unp_scan(so->so_rcv.sb_mb, unp_mark);
                }
        } while (unp_defer);
                        unp_scan(so->so_rcv.sb_mb, unp_mark);
                }
        } while (unp_defer);
-       for (fp = file; fp < fileNFILE; fp++) {
+       for (fp = filehead; fp; fp = fp->f_filef) {
                if (fp->f_count == 0)
                        continue;
                if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
                if (fp->f_count == 0)
                        continue;
                if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)