sleep doesn't need spl0 (idle does it)
[unix-history] / usr / src / sys / kern / uipc_usrreq.c
index 48a9146..c30a7bf 100644 (file)
@@ -2,17 +2,19 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)uipc_usrreq.c       7.20 (Berkeley) %G%
+ *     @(#)uipc_usrreq.c       7.26 (Berkeley) %G%
  */
 
 #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"
@@ -38,6 +40,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);
@@ -64,7 +67,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:
@@ -73,7 +76,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:
@@ -102,7 +105,7 @@ uipc_usrreq(so, req, m, nam, control)
 
        case PRU_SHUTDOWN:
                socantsendmore(so);
 
        case PRU_SHUTDOWN:
                socantsendmore(so);
-               unp_usrclosed(unp);
+               unp_shutdown(unp);
                break;
 
        case PRU_RCVD:
                break;
 
        case PRU_RCVD:
@@ -137,7 +140,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) {
 
@@ -149,7 +152,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 {
@@ -190,8 +193,8 @@ uipc_usrreq(so, req, m, nam, control)
                         * Wake up readers.
                         */
                        if (control) {
                         * Wake up readers.
                         */
                        if (control) {
-                               (void)sbappendcontrol(rcv, m, control);
-                               control = 0;
+                               if (sbappendcontrol(rcv, m, control))
+                                       control = 0;
                        } else
                                sbappend(rcv, m);
                        snd->sb_mbmax -=
                        } else
                                sbappend(rcv, m);
                        snd->sb_mbmax -=
@@ -332,16 +335,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);
@@ -353,7 +359,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) {
@@ -368,7 +374,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;
@@ -378,17 +384,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)
@@ -397,14 +406,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) {
@@ -510,11 +519,14 @@ unp_abort(unp)
 }
 #endif
 
 }
 #endif
 
-/*ARGSUSED*/
-unp_usrclosed(unp)
+unp_shutdown(unp)
        struct unpcb *unp;
 {
        struct unpcb *unp;
 {
+       struct socket *so;
 
 
+       if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
+           (so = unp->unp_conn->unp_socket))
+               socantrcvmore(so);
 }
 
 unp_drop(unp, errno)
 }
 
 unp_drop(unp, errno)
@@ -543,6 +555,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);
@@ -550,7 +563,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);
@@ -559,10 +572,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;
@@ -570,9 +583,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;
@@ -586,12 +601,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++;
@@ -614,10 +630,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) {
@@ -636,14 +652,26 @@ restart:
                        if (so->so_proto->pr_domain != &unixdomain ||
                            (so->so_proto->pr_flags&PR_RIGHTS) == 0)
                                continue;
                        if (so->so_proto->pr_domain != &unixdomain ||
                            (so->so_proto->pr_flags&PR_RIGHTS) == 0)
                                continue;
+#ifdef notdef
                        if (so->so_rcv.sb_flags & SB_LOCK) {
                        if (so->so_rcv.sb_flags & SB_LOCK) {
-                               sbwait(&so->so_rcv);
+                               /*
+                                * This is problematical; it's not clear
+                                * we need to wait for the sockbuf to be
+                                * unlocked (on a uniprocessor, at least),
+                                * and it's also not clear what to do
+                                * if sbwait returns an error due to receipt
+                                * of a signal.  If sbwait does return
+                                * an error, we'll go into an infinite
+                                * loop.  Delete all of this for now.
+                                */
+                               (void) sbwait(&so->so_rcv);
                                goto restart;
                        }
                                goto restart;
                        }
+#endif
                        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)