getkerninfo skipped defaults ``dupedkeyed'' behind the root node;
[unix-history] / usr / src / sys / kern / uipc_usrreq.c
index cdbf93e..b0855b3 100644 (file)
@@ -1,38 +1,69 @@
-/*     uipc_usrreq.c   1.2     82/11/03        */
-
-#include "../h/param.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/mbuf.h"
-#include "../h/protosw.h"
-#include "../h/socket.h"
-#include "../h/socketvar.h"
-#include "../h/unpcb.h"
-#include "../h/un.h"
-#include "../h/inode.h"
+/*
+ *
+ * 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 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)uipc_usrreq.c       7.17 (Berkeley) %G%
+ */
+
+#include "param.h"
+#include "user.h"
+#include "domain.h"
+#include "protosw.h"
+#include "socket.h"
+#include "socketvar.h"
+#include "unpcb.h"
+#include "un.h"
+#include "vnode.h"
+#include "file.h"
+#include "stat.h"
+#include "mbuf.h"
 
 /*
  * Unix communications domain.
 
 /*
  * Unix communications domain.
+ *
+ * TODO:
+ *     SEQPACKET, RDM
+ *     rethink name space problems
+ *     need a proper out-of-band
  */
  */
+struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
+ino_t  unp_ino;                        /* prototype for fake inode numbers */
 
 /*ARGSUSED*/
 
 /*ARGSUSED*/
-uipc_usrreq(so, req, m, nam, opt)
+uipc_usrreq(so, req, m, nam, control)
        struct socket *so;
        int req;
        struct socket *so;
        int req;
-       struct mbuf *m, *nam;
-       struct socketopt *opt;
+       struct mbuf *m, *nam, *control;
 {
        struct unpcb *unp = sotounpcb(so);
        register struct socket *so2;
 {
        struct unpcb *unp = sotounpcb(so);
        register struct socket *so2;
-       int error = 0;
+       register int error = 0;
 
 
-       if (unp == 0 && req != PRU_ATTACH)
-               return (EINVAL);                        /* XXX */
+       if (req == PRU_CONTROL)
+               return (EOPNOTSUPP);
+       if (req != PRU_SEND && control && control->m_len) {
+               error = EOPNOTSUPP;
+               goto release;
+       }
+       if (unp == 0 && req != PRU_ATTACH) {
+               error = EINVAL;
+               goto release;
+       }
        switch (req) {
 
        case PRU_ATTACH:
                if (unp) {
        switch (req) {
 
        case PRU_ATTACH:
                if (unp) {
-                       error = EINVAL;
+                       error = EISCONN;
                        break;
                }
                error = unp_attach(so);
                        break;
                }
                error = unp_attach(so);
@@ -42,23 +73,40 @@ uipc_usrreq(so, req, m, nam, opt)
                unp_detach(unp);
                break;
 
                unp_detach(unp);
                break;
 
+       case PRU_BIND:
+               error = unp_bind(unp, nam);
+               break;
+
+       case PRU_LISTEN:
+               if (unp->unp_vnode == 0)
+                       error = EINVAL;
+               break;
+
        case PRU_CONNECT:
                error = unp_connect(so, nam);
                break;
 
        case PRU_CONNECT:
                error = unp_connect(so, nam);
                break;
 
+       case PRU_CONNECT2:
+               error = unp_connect2(so, (struct socket *)nam);
+               break;
+
        case PRU_DISCONNECT:
                unp_disconnect(unp);
                break;
 
        case PRU_DISCONNECT:
                unp_disconnect(unp);
                break;
 
-/* BEGIN QUESTIONABLE */
-       case PRU_ACCEPT: {
-               struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
-
-               if (soun) {
-                       bzero((caddr_t)soun, sizeof (*soun));
-                       soun->sun_family = AF_UNIX;
-                       /* XXX */
-               }
+       case PRU_ACCEPT:
+               /*
+                * Pass back name of connected socket,
+                * if it was bound and we are still connected
+                * (our peer may have closed already!).
+                */
+               if (unp->unp_conn && unp->unp_conn->unp_addr) {
+                       nam->m_len = unp->unp_conn->unp_addr->m_len;
+                       bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
+                           mtod(nam, caddr_t), (unsigned)nam->m_len);
+               } else {
+                       nam->m_len = sizeof(sun_noname);
+                       *(mtod(nam, struct sockaddr *)) = sun_noname;
                }
                break;
 
                }
                break;
 
@@ -66,32 +114,31 @@ uipc_usrreq(so, req, m, nam, opt)
                socantsendmore(so);
                unp_usrclosed(unp);
                break;
                socantsendmore(so);
                unp_usrclosed(unp);
                break;
-/* END QUESTIONABLE */
 
        case PRU_RCVD:
                switch (so->so_type) {
 
                case SOCK_DGRAM:
                        panic("uipc 1");
 
        case PRU_RCVD:
                switch (so->so_type) {
 
                case SOCK_DGRAM:
                        panic("uipc 1");
+                       /*NOTREACHED*/
 
 
-               case SOCK_STREAM: {
+               case SOCK_STREAM:
 #define        rcv (&so->so_rcv)
 #define snd (&so2->so_snd)
                        if (unp->unp_conn == 0)
                                break;
                        so2 = unp->unp_conn->unp_socket;
                        /*
 #define        rcv (&so->so_rcv)
 #define snd (&so2->so_snd)
                        if (unp->unp_conn == 0)
                                break;
                        so2 = unp->unp_conn->unp_socket;
                        /*
-                        * Transfer resources back to send port
+                        * Adjust backpressure on sender
                         * and wakeup any waiting to write.
                         */
                         * and wakeup any waiting to write.
                         */
-                       snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
-                       rcv->sb_mbmax = rcv->sb_mbcnt;
-                       snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
-                       rcv->sb_hiwat = rcv->sb_cc;
-                       sbwakeup(snd);
+                       snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
+                       unp->unp_mbcnt = rcv->sb_mbcnt;
+                       snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
+                       unp->unp_cc = rcv->sb_cc;
+                       sowwakeup(so2);
 #undef snd
 #undef rcv
 #undef snd
 #undef rcv
-                       }
                        break;
 
                default:
                        break;
 
                default:
@@ -100,9 +147,13 @@ uipc_usrreq(so, req, m, nam, opt)
                break;
 
        case PRU_SEND:
                break;
 
        case PRU_SEND:
+               if (control && (error = unp_internalize(control)))
+                       break;
                switch (so->so_type) {
 
                switch (so->so_type) {
 
-               case SOCK_DGRAM:
+               case SOCK_DGRAM: {
+                       struct sockaddr *from;
+
                        if (nam) {
                                if (unp->unp_conn) {
                                        error = EISCONN;
                        if (nam) {
                                if (unp->unp_conn) {
                                        error = EISCONN;
@@ -118,29 +169,48 @@ uipc_usrreq(so, req, m, nam, opt)
                                }
                        }
                        so2 = unp->unp_conn->unp_socket;
                                }
                        }
                        so2 = unp->unp_conn->unp_socket;
-                       if (sbspace(&so2->so_rcv) > 0)          /* XXX */
-                               sbappendaddr(so2, m, nam);      /* XXX */
+                       if (unp->unp_addr)
+                               from = mtod(unp->unp_addr, struct sockaddr *);
+                       else
+                               from = &sun_noname;
+                       if (sbappendaddr(&so2->so_rcv, from, m, control)) {
+                               sorwakeup(so2);
+                               m = 0;
+                               control = 0;
+                       } else
+                               error = ENOBUFS;
                        if (nam)
                        if (nam)
-                               unp_disconnect(so);
+                               unp_disconnect(unp);
                        break;
                        break;
+               }
 
                case SOCK_STREAM:
 #define        rcv (&so2->so_rcv)
 #define        snd (&so->so_snd)
 
                case SOCK_STREAM:
 #define        rcv (&so2->so_rcv)
 #define        snd (&so->so_snd)
+                       if (so->so_state & SS_CANTSENDMORE) {
+                               error = EPIPE;
+                               break;
+                       }
                        if (unp->unp_conn == 0)
                                panic("uipc 3");
                        so2 = unp->unp_conn->unp_socket;
                        /*
                        if (unp->unp_conn == 0)
                                panic("uipc 3");
                        so2 = unp->unp_conn->unp_socket;
                        /*
-                        * Send to paired receive port, and then
-                        * give it enough resources to hold what it already has.
+                        * Send to paired receive port, and then reduce
+                        * send buffer hiwater marks to maintain backpressure.
                         * Wake up readers.
                         */
                         * Wake up readers.
                         */
-                       sbappend(rcv, m);
-                       snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
-                       rcv->sb_mbmax = rcv->sb_mbcnt;
-                       snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
-                       rcv->sb_hiwat = rcv->sb_cc;
-                       sbwakeup(rcv);
+                       if (control) {
+                               (void)sbappendcontrol(rcv, m, control);
+                               control = 0;
+                       } else
+                               sbappend(rcv, m);
+                       snd->sb_mbmax -=
+                           rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
+                       unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
+                       snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
+                       unp->unp_conn->unp_cc = rcv->sb_cc;
+                       sorwakeup(so2);
+                       m = 0;
 #undef snd
 #undef rcv
                        break;
 #undef snd
 #undef rcv
                        break;
@@ -154,23 +224,41 @@ uipc_usrreq(so, req, m, nam, opt)
                unp_drop(unp, ECONNABORTED);
                break;
 
                unp_drop(unp, ECONNABORTED);
                break;
 
-/* SOME AS YET UNIMPLEMENTED HOOKS */
-       case PRU_CONTROL:
-               error = EOPNOTSUPP;
-               break;
-
        case PRU_SENSE:
        case PRU_SENSE:
-               error = EOPNOTSUPP;
-               break;
-/* END UNIMPLEMENTED HOOKS */
+               ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
+               if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
+                       so2 = unp->unp_conn->unp_socket;
+                       ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
+               }
+               ((struct stat *) m)->st_dev = NODEV;
+               if (unp->unp_ino == 0)
+                       unp->unp_ino = unp_ino++;
+               ((struct stat *) m)->st_ino = unp->unp_ino;
+               return (0);
 
        case PRU_RCVOOB:
 
        case PRU_RCVOOB:
-               break;
+               return (EOPNOTSUPP);
 
        case PRU_SENDOOB:
 
        case PRU_SENDOOB:
+               error = EOPNOTSUPP;
                break;
 
        case PRU_SOCKADDR:
                break;
 
        case PRU_SOCKADDR:
+               if (unp->unp_addr) {
+                       nam->m_len = unp->unp_addr->m_len;
+                       bcopy(mtod(unp->unp_addr, caddr_t),
+                           mtod(nam, caddr_t), (unsigned)nam->m_len);
+               } else
+                       nam->m_len = 0;
+               break;
+
+       case PRU_PEERADDR:
+               if (unp->unp_conn && unp->unp_conn->unp_addr) {
+                       nam->m_len = unp->unp_conn->unp_addr->m_len;
+                       bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
+                           mtod(nam, caddr_t), (unsigned)nam->m_len);
+               } else
+                       nam->m_len = 0;
                break;
 
        case PRU_SLOWTIMO:
                break;
 
        case PRU_SLOWTIMO:
@@ -179,43 +267,209 @@ uipc_usrreq(so, req, m, nam, opt)
        default:
                panic("piusrreq");
        }
        default:
                panic("piusrreq");
        }
-       return (0);
+release:
+       if (control)
+               m_freem(control);
+       if (m)
+               m_freem(m);
+       return (error);
 }
 
 }
 
-int    unp_sendspace = 1024*2;
-int    unp_recvspace = 1024*2;
+/*
+ * Both send and receive buffers are allocated PIPSIZ bytes of buffering
+ * for stream sockets, although the total for sender and receiver is
+ * actually only PIPSIZ.
+ * Datagram sockets really use the sendspace as the maximum datagram size,
+ * and don't really want to reserve the sendspace.  Their recvspace should
+ * be large enough for at least one max-size datagram plus address.
+ */
+#define        PIPSIZ  4096
+u_long unpst_sendspace = PIPSIZ;
+u_long unpst_recvspace = PIPSIZ;
+u_long unpdg_sendspace = 2*1024;       /* really max datagram size */
+u_long unpdg_recvspace = 4*1024;
+
+int    unp_rights;                     /* file descriptors in flight */
 
 
-unp_attach(so, soun)
+unp_attach(so)
        struct socket *so;
        struct socket *so;
-       struct sockaddr_un *soun;
 {
 {
+       register struct mbuf *m;
        register struct unpcb *unp;
        register struct unpcb *unp;
-       struct mbuf *m;
        int error;
        
        int error;
        
-       error = soreserve(so, unp_sendspace, unp_recvspace);
-       if (error)
-               goto bad;
-       m = m_getclr(M_DONTWAIT);
-       if (m == 0) {
-               error = ENOBUFS;
-               goto bad;
+       if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
+               switch (so->so_type) {
+
+               case SOCK_STREAM:
+                       error = soreserve(so, unpst_sendspace, unpst_recvspace);
+                       break;
+
+               case SOCK_DGRAM:
+                       error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
+                       break;
+               }
+               if (error)
+                       return (error);
        }
        }
+       m = m_getclr(M_DONTWAIT, MT_PCB);
+       if (m == NULL)
+               return (ENOBUFS);
        unp = mtod(m, struct unpcb *);
        so->so_pcb = (caddr_t)unp;
        unp->unp_socket = so;
        unp = mtod(m, struct unpcb *);
        so->so_pcb = (caddr_t)unp;
        unp->unp_socket = so;
-       if (soun) {
-               error = unp_bind(unp, soun);
-               if (error) {
-                       unp_detach(unp);
+       return (0);
+}
+
+unp_detach(unp)
+       register struct unpcb *unp;
+{
+       
+       if (unp->unp_vnode) {
+               unp->unp_vnode->v_socket = 0;
+               vrele(unp->unp_vnode);
+               unp->unp_vnode = 0;
+       }
+       if (unp->unp_conn)
+               unp_disconnect(unp);
+       while (unp->unp_refs)
+               unp_drop(unp->unp_refs, ECONNRESET);
+       soisdisconnected(unp->unp_socket);
+       unp->unp_socket->so_pcb = 0;
+       m_freem(unp->unp_addr);
+       (void) m_free(dtom(unp));
+       if (unp_rights)
+               unp_gc();
+}
+
+unp_bind(unp, nam)
+       struct unpcb *unp;
+       struct mbuf *nam;
+{
+       struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
+       register struct vnode *vp;
+       register struct nameidata *ndp = &u.u_nd;
+       struct vattr vattr;
+       int error;
+
+       ndp->ni_dirp = soun->sun_path;
+       if (unp->unp_vnode != NULL)
+               return (EINVAL);
+       if (nam->m_len == MLEN) {
+               if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
+                       return (EINVAL);
+       } else
+               *(mtod(nam, caddr_t) + nam->m_len) = 0;
+/* 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))
+               return (error);
+       vp = ndp->ni_vp;
+       if (vp != NULL) {
+               VOP_ABORTOP(ndp);
+               return (EADDRINUSE);
+       }
+       VATTR_NULL(&vattr);
+       vattr.va_type = VSOCK;
+       vattr.va_mode = 0777;
+       if (error = VOP_CREATE(ndp, &vattr))
+               return (error);
+       vp = ndp->ni_vp;
+       vp->v_socket = unp->unp_socket;
+       unp->unp_vnode = vp;
+       unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
+       VOP_UNLOCK(vp);
+       return (0);
+}
+
+unp_connect(so, nam)
+       struct socket *so;
+       struct mbuf *nam;
+{
+       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;
+       struct unpcb *unp2, *unp3;
+       int error;
+
+       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)
+                       return (EMSGSIZE);
+       } else
+               *(mtod(nam, caddr_t) + nam->m_len) = 0;
+       ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
+       ndp->ni_segflg = UIO_SYSSPACE;
+       if (error = namei(ndp))
+               return (error);
+       vp = ndp->ni_vp;
+       if (vp->v_type != VSOCK) {
+               error = ENOTSOCK;
+               goto bad;
+       }
+       if (error = VOP_ACCESS(vp, VWRITE, ndp->ni_cred))
+               goto bad;
+       so2 = vp->v_socket;
+       if (so2 == 0) {
+               error = ECONNREFUSED;
+               goto bad;
+       }
+       if (so->so_type != so2->so_type) {
+               error = EPROTOTYPE;
+               goto bad;
+       }
+       if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
+               if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
+                   (so3 = sonewconn(so2, 0)) == 0) {
+                       error = ECONNREFUSED;
                        goto bad;
                }
                        goto bad;
                }
+               unp2 = sotounpcb(so2);
+               unp3 = sotounpcb(so3);
+               if (unp2->unp_addr)
+                       unp3->unp_addr =
+                                 m_copy(unp2->unp_addr, 0, (int)M_COPYALL);
+               so2 = so3;
        }
        }
-       return (0);
+       error = unp_connect2(so, so2);
 bad:
 bad:
+       vput(vp);
        return (error);
 }
 
        return (error);
 }
 
+unp_connect2(so, so2)
+       register struct socket *so;
+       register struct socket *so2;
+{
+       register struct unpcb *unp = sotounpcb(so);
+       register struct unpcb *unp2;
+
+       if (so2->so_type != so->so_type)
+               return (EPROTOTYPE);
+       unp2 = sotounpcb(so2);
+       unp->unp_conn = unp2;
+       switch (so->so_type) {
+
+       case SOCK_DGRAM:
+               unp->unp_nextref = unp2->unp_refs;
+               unp2->unp_refs = unp;
+               soisconnected(so);
+               break;
+
+       case SOCK_STREAM:
+               unp2->unp_conn = unp;
+               soisconnected(so);
+               soisconnected(so2);
+               break;
+
+       default:
+               panic("unp_connect2");
+       }
+       return (0);
+}
+
 unp_disconnect(unp)
        struct unpcb *unp;
 {
 unp_disconnect(unp)
        struct unpcb *unp;
 {
@@ -224,7 +478,6 @@ unp_disconnect(unp)
        if (unp2 == 0)
                return;
        unp->unp_conn = 0;
        if (unp2 == 0)
                return;
        unp->unp_conn = 0;
-       soisdisconnected(unp->unp_socket);
        switch (unp->unp_socket->so_type) {
 
        case SOCK_DGRAM:
        switch (unp->unp_socket->so_type) {
 
        case SOCK_DGRAM:
@@ -242,158 +495,222 @@ unp_disconnect(unp)
                        unp2->unp_nextref = unp->unp_nextref;
                }
                unp->unp_nextref = 0;
                        unp2->unp_nextref = unp->unp_nextref;
                }
                unp->unp_nextref = 0;
+               unp->unp_socket->so_state &= ~SS_ISCONNECTED;
                break;
 
        case SOCK_STREAM:
                break;
 
        case SOCK_STREAM:
+               soisdisconnected(unp->unp_socket);
                unp2->unp_conn = 0;
                soisdisconnected(unp2->unp_socket);
                unp2->unp_conn = 0;
                soisdisconnected(unp2->unp_socket);
-               unp_drop(unp2, ECONNRESET);
                break;
        }
 }
 
                break;
        }
 }
 
+#ifdef notdef
 unp_abort(unp)
        struct unpcb *unp;
 {
 
        unp_detach(unp);
 }
 unp_abort(unp)
        struct unpcb *unp;
 {
 
        unp_detach(unp);
 }
+#endif
 
 
-unp_detach(unp)
-       struct unpcb *unp;
-{
-       
-       if (unp->unp_inode) {
-               irele(unp->unp_inode);
-               unp->unp_inode = 0;
-       }
-       if (unp->unp_conn)
-               unp_disconnect(unp);
-       while (unp->unp_refs)
-               unp_drop(unp->unp_refs, ECONNRESET);
-       soisdisconnected(unp->unp_socket);
-       unp->unp_socket->so_pcb = 0;
-       m_free(dtom(unp));
-}
-
+/*ARGSUSED*/
 unp_usrclosed(unp)
        struct unpcb *unp;
 {
 unp_usrclosed(unp)
        struct unpcb *unp;
 {
-       register struct socket *so = unp->unp_socket;
 
 
-#ifdef sometimes /* ??? */
-       soisdisconnected(unp->unp_socket);
-#endif
 }
 
 unp_drop(unp, errno)
        struct unpcb *unp;
        int errno;
 {
 }
 
 unp_drop(unp, errno)
        struct unpcb *unp;
        int errno;
 {
+       struct socket *so = unp->unp_socket;
 
 
-       unp->unp_socket->so_error = errno;
+       so->so_error = errno;
        unp_disconnect(unp);
        unp_disconnect(unp);
+       if (so->so_head) {
+               so->so_pcb = (caddr_t) 0;
+               m_freem(unp->unp_addr);
+               (void) m_free(dtom(unp));
+               sofree(so);
+       }
 }
 
 }
 
+#ifdef notdef
 unp_drain()
 {
 
 }
 unp_drain()
 {
 
 }
+#endif
 
 
-unp_bind(unp, soun)
-       struct unpcb *unp;
-       struct sockaddr_un *soun;
+unp_externalize(rights)
+       struct mbuf *rights;
 {
 {
-       register struct inode *ip;
-       int error;
-       extern schar();
-
-       u.u_dirp = soun->sun_path;
-       soun->sun_path[sizeof(soun->sun_path)-1] = 0;
-       ip = namei(schar, 1, 1);
-       if (ip) {
-               iput(ip);
-               return (EEXIST);
+       register int i;
+       register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
+       register struct file **rp = (struct file **)(cm + 1);
+       register struct file *fp;
+       int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
+       int f;
+
+       if (newfds > ufavail()) {
+               for (i = 0; i < newfds; i++) {
+                       fp = *rp;
+                       unp_discard(fp);
+                       *rp++ = 0;
+               }
+               return (EMSGSIZE);
        }
        }
-       ip = maknode(IFSOCK | 0777);
-       if (ip == NULL) {
-               error = u.u_error;              /* XXX */
-               u.u_error = 0;                  /* XXX */
-               return (error);
+       for (i = 0; i < newfds; i++) {
+               if (ufalloc(0, &f))
+                       panic("unp_externalize");
+               fp = *rp;
+               u.u_ofile[f] = fp;
+               fp->f_msgcount--;
+               unp_rights--;
+               *(int *)rp++ = f;
        }
        }
-       ip->i_socket = unp->unp_socket;
-       unp->unp_inode = ip;
-       iunlock(ip);                    /* but keep reference */
        return (0);
 }
 
        return (0);
 }
 
-unp_connect(so, soun)
-       struct socket *so;
-       struct sockaddr_un *soun;
+unp_internalize(control)
+       struct mbuf *control;
 {
 {
-       struct inode *ip;
-       int error;
-
-       u.u_dirp = soun->sun_path;
-       soun->sun_path[sizeof(soun->sun_path)-1] = 0;
-       ip = namei(schar, 0, 1);
-       if (ip == 0) {
-               error = u.u_error;
-               u.u_error = 0;
-               return (ENOENT);
+       register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
+       register struct file **rp;
+       register struct file *fp;
+       register int i, fd;
+       int oldfds;
+
+       if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
+           cm->cmsg_len != control->m_len)
+               return (EINVAL);
+       oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
+       rp = (struct file **)(cm + 1);
+       for (i = 0; i < oldfds; i++) {
+               fd = *(int *)rp++;
+               if ((unsigned)fd >= NOFILE || u.u_ofile[fd] == NULL)
+                       return (EBADF);
        }
        }
-       error = unp_connectip(so, ip);
-       return (error);
+       rp = (struct file **)(cm + 1);
+       for (i = 0; i < oldfds; i++) {
+               fp = u.u_ofile[*(int *)rp];
+               *rp++ = fp;
+               fp->f_count++;
+               fp->f_msgcount++;
+               unp_rights++;
+       }
+       return (0);
 }
 
 }
 
-unp_connectip(so, ip)
-       struct socket *so;
-       struct inode *ip;
+int    unp_defer, unp_gcing;
+int    unp_mark();
+extern struct domain unixdomain;
+
+unp_gc()
 {
 {
-       struct unpcb *unp = sotounpcb(so);
-       struct socket *so2, *so3;
-       int error;
-       struct unpcb *unp2;
+       register struct file *fp;
+       register struct socket *so;
 
 
-       if ((ip->i_mode&IFMT) != IFSOCK) {
-               error = ENOTSOCK;
-               goto bad;
-       }
-       so2 = ip->i_socket;
-       if (so2 == 0) {
-               error = ECONNREFUSED;
-               goto bad;
-       }
-       if (so2->so_type != so->so_type) {
-               error = EPROTOTYPE;
-               goto bad;
+       if (unp_gcing)
+               return;
+       unp_gcing = 1;
+restart:
+       unp_defer = 0;
+       for (fp = file; fp < fileNFILE; fp++)
+               fp->f_flag &= ~(FMARK|FDEFER);
+       do {
+               for (fp = file; fp < fileNFILE; fp++) {
+                       if (fp->f_count == 0)
+                               continue;
+                       if (fp->f_flag & FDEFER) {
+                               fp->f_flag &= ~FDEFER;
+                               unp_defer--;
+                       } else {
+                               if (fp->f_flag & FMARK)
+                                       continue;
+                               if (fp->f_count == fp->f_msgcount)
+                                       continue;
+                               fp->f_flag |= FMARK;
+                       }
+                       if (fp->f_type != DTYPE_SOCKET ||
+                           (so = (struct socket *)fp->f_data) == 0)
+                               continue;
+                       if (so->so_proto->pr_domain != &unixdomain ||
+                           (so->so_proto->pr_flags&PR_RIGHTS) == 0)
+                               continue;
+                       if (so->so_rcv.sb_flags & SB_LOCK) {
+                               sbwait(&so->so_rcv);
+                               goto restart;
+                       }
+                       unp_scan(so->so_rcv.sb_mb, unp_mark);
+               }
+       } while (unp_defer);
+       for (fp = file; fp < fileNFILE; fp++) {
+               if (fp->f_count == 0)
+                       continue;
+               if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
+                       while (fp->f_msgcount)
+                               unp_discard(fp);
        }
        }
-       switch (so->so_type) {
+       unp_gcing = 0;
+}
 
 
-       case SOCK_DGRAM:
-               unp->unp_conn = sotounpcb(so2);
-               unp2 = sotounpcb(so2);
-               unp->unp_nextref = unp2->unp_refs;
-               unp2->unp_refs = unp;
-               break;
+unp_dispose(m)
+       struct mbuf *m;
+{
+       int unp_discard();
 
 
-       case SOCK_STREAM:
-               if ((so2->so_options&SO_ACCEPTCONN) == 0 ||
-                   (so3 = sonewconn(so2)) == 0) {
-                       error = ECONNREFUSED;
-                       goto bad;
-               }
-               unp->unp_conn = sotounpcb(so3);
-               break;
+       if (m)
+               unp_scan(m, unp_discard);
+}
 
 
-       default:
-               panic("uipc connip");
+unp_scan(m0, op)
+       register struct mbuf *m0;
+       int (*op)();
+{
+       register struct mbuf *m;
+       register struct file **rp;
+       register struct cmsghdr *cm;
+       register int i;
+       int qfds;
+
+       while (m0) {
+               for (m = m0; m; m = m->m_next)
+                       if (m->m_type == MT_CONTROL &&
+                           m->m_len >= sizeof(*cm)) {
+                               cm = mtod(m, struct cmsghdr *);
+                               if (cm->cmsg_level != SOL_SOCKET ||
+                                   cm->cmsg_type != SCM_RIGHTS)
+                                       continue;
+                               qfds = (cm->cmsg_len - sizeof *cm)
+                                               / sizeof (struct file *);
+                               rp = (struct file **)(cm + 1);
+                               for (i = 0; i < qfds; i++)
+                                       (*op)(*rp++);
+                               break;          /* XXX, but saves time */
+                       }
+               m0 = m0->m_act;
        }
        }
-       soisconnected(unp->unp_conn->unp_socket);
-       soisconnected(so);
-       iput(ip);
-       return (0);
-bad:
-       iput(ip);
-       return (error);
+}
+
+unp_mark(fp)
+       struct file *fp;
+{
+
+       if (fp->f_flag & FMARK)
+               return;
+       unp_defer++;
+       fp->f_flag |= (FMARK|FDEFER);
+}
+
+unp_discard(fp)
+       struct file *fp;
+{
+
+       fp->f_msgcount--;
+       unp_rights--;
+       (void) closef(fp);
 }
 }