123443Smckusick /* 247647Skarels * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California. 333288Sbostic * All rights reserved. 423443Smckusick * 544453Sbostic * %sccs.include.redist.c% 633288Sbostic * 7*52921Smckusick * @(#)uipc_usrreq.c 7.31 (Berkeley) 03/13/92 823443Smckusick */ 98925Sroot 1017105Sbloom #include "param.h" 1145914Smckusick #include "proc.h" 1245914Smckusick #include "filedesc.h" 1317105Sbloom #include "domain.h" 1417105Sbloom #include "protosw.h" 1517105Sbloom #include "socket.h" 1617105Sbloom #include "socketvar.h" 1717105Sbloom #include "unpcb.h" 1817105Sbloom #include "un.h" 1948022Smckusick #include "namei.h" 2037616Smckusick #include "vnode.h" 2117105Sbloom #include "file.h" 2217105Sbloom #include "stat.h" 2341381Smckusick #include "mbuf.h" 248925Sroot 258925Sroot /* 268925Sroot * Unix communications domain. 2712760Ssam * 2812760Ssam * TODO: 2912760Ssam * SEQPACKET, RDM 3013119Ssam * rethink name space problems 3112760Ssam * need a proper out-of-band 328925Sroot */ 3337617Smckusick struct sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX }; 3440800Ssklower ino_t unp_ino; /* prototype for fake inode numbers */ 358925Sroot 368925Sroot /*ARGSUSED*/ 3740800Ssklower uipc_usrreq(so, req, m, nam, control) 388925Sroot struct socket *so; 398925Sroot int req; 4040800Ssklower struct mbuf *m, *nam, *control; 418925Sroot { 428925Sroot struct unpcb *unp = sotounpcb(so); 438925Sroot register struct socket *so2; 4440937Skarels register int error = 0; 4548022Smckusick struct proc *p = curproc; /* XXX */ 468925Sroot 4725555Skarels if (req == PRU_CONTROL) 4825555Skarels return (EOPNOTSUPP); 4940800Ssklower if (req != PRU_SEND && control && control->m_len) { 5012760Ssam error = EOPNOTSUPP; 5112760Ssam goto release; 5212760Ssam } 5312760Ssam if (unp == 0 && req != PRU_ATTACH) { 5412760Ssam error = EINVAL; 5512760Ssam goto release; 5612760Ssam } 578925Sroot switch (req) { 588925Sroot 598925Sroot case PRU_ATTACH: 608925Sroot if (unp) { 619169Ssam error = EISCONN; 628925Sroot break; 638925Sroot } 649028Sroot error = unp_attach(so); 658925Sroot break; 668925Sroot 678925Sroot case PRU_DETACH: 688925Sroot unp_detach(unp); 698925Sroot break; 708925Sroot 719169Ssam case PRU_BIND: 7248022Smckusick error = unp_bind(unp, nam, p); 739169Ssam break; 749169Ssam 759169Ssam case PRU_LISTEN: 7637616Smckusick if (unp->unp_vnode == 0) 779169Ssam error = EINVAL; 789169Ssam break; 799169Ssam 808925Sroot case PRU_CONNECT: 8148022Smckusick error = unp_connect(so, nam, p); 828925Sroot break; 838925Sroot 8412760Ssam case PRU_CONNECT2: 8526281Skarels error = unp_connect2(so, (struct socket *)nam); 8612760Ssam break; 8712760Ssam 888925Sroot case PRU_DISCONNECT: 898925Sroot unp_disconnect(unp); 908925Sroot break; 918925Sroot 929169Ssam case PRU_ACCEPT: 9325899Skarels /* 9425899Skarels * Pass back name of connected socket, 9525899Skarels * if it was bound and we are still connected 9625899Skarels * (our peer may have closed already!). 9725899Skarels */ 9825899Skarels if (unp->unp_conn && unp->unp_conn->unp_addr) { 9925632Skarels nam->m_len = unp->unp_conn->unp_addr->m_len; 10025632Skarels bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 10125632Skarels mtod(nam, caddr_t), (unsigned)nam->m_len); 10225632Skarels } else { 10325632Skarels nam->m_len = sizeof(sun_noname); 10425632Skarels *(mtod(nam, struct sockaddr *)) = sun_noname; 10525632Skarels } 1068925Sroot break; 1078925Sroot 1088925Sroot case PRU_SHUTDOWN: 1098925Sroot socantsendmore(so); 11045004Skarels unp_shutdown(unp); 1118925Sroot break; 1128925Sroot 1138925Sroot case PRU_RCVD: 1148925Sroot switch (so->so_type) { 1158925Sroot 1168925Sroot case SOCK_DGRAM: 1178925Sroot panic("uipc 1"); 11810139Ssam /*NOTREACHED*/ 1198925Sroot 12010139Ssam case SOCK_STREAM: 1218925Sroot #define rcv (&so->so_rcv) 1228925Sroot #define snd (&so2->so_snd) 1238925Sroot if (unp->unp_conn == 0) 1248925Sroot break; 1258925Sroot so2 = unp->unp_conn->unp_socket; 1268925Sroot /* 12725632Skarels * Adjust backpressure on sender 1288925Sroot * and wakeup any waiting to write. 1298925Sroot */ 13025632Skarels snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 13125632Skarels unp->unp_mbcnt = rcv->sb_mbcnt; 13225632Skarels snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 13325632Skarels unp->unp_cc = rcv->sb_cc; 13417543Skarels sowwakeup(so2); 1358925Sroot #undef snd 1368925Sroot #undef rcv 1378925Sroot break; 1388925Sroot 1398925Sroot default: 1408925Sroot panic("uipc 2"); 1418925Sroot } 1428925Sroot break; 1438925Sroot 1448925Sroot case PRU_SEND: 14548022Smckusick if (control && (error = unp_internalize(control, p))) 14640937Skarels break; 1478925Sroot switch (so->so_type) { 1488925Sroot 14925632Skarels case SOCK_DGRAM: { 15025632Skarels struct sockaddr *from; 15125632Skarels 1529028Sroot if (nam) { 1538925Sroot if (unp->unp_conn) { 1548925Sroot error = EISCONN; 1558925Sroot break; 1568925Sroot } 15748022Smckusick error = unp_connect(so, nam, p); 1588925Sroot if (error) 1598925Sroot break; 1608925Sroot } else { 1618925Sroot if (unp->unp_conn == 0) { 1628925Sroot error = ENOTCONN; 1638925Sroot break; 1648925Sroot } 1658925Sroot } 1668925Sroot so2 = unp->unp_conn->unp_socket; 16725632Skarels if (unp->unp_addr) 16825632Skarels from = mtod(unp->unp_addr, struct sockaddr *); 16925632Skarels else 17025632Skarels from = &sun_noname; 17140937Skarels if (sbappendaddr(&so2->so_rcv, from, m, control)) { 17225632Skarels sorwakeup(so2); 17325632Skarels m = 0; 17440937Skarels control = 0; 17525632Skarels } else 17625632Skarels error = ENOBUFS; 1779028Sroot if (nam) 1789169Ssam unp_disconnect(unp); 1798925Sroot break; 18025632Skarels } 1818925Sroot 1828925Sroot case SOCK_STREAM: 1838925Sroot #define rcv (&so2->so_rcv) 1848925Sroot #define snd (&so->so_snd) 18523524Skarels if (so->so_state & SS_CANTSENDMORE) { 18623524Skarels error = EPIPE; 18723524Skarels break; 18823524Skarels } 1898925Sroot if (unp->unp_conn == 0) 1908925Sroot panic("uipc 3"); 1918925Sroot so2 = unp->unp_conn->unp_socket; 1928925Sroot /* 19325632Skarels * Send to paired receive port, and then reduce 19425632Skarels * send buffer hiwater marks to maintain backpressure. 1958925Sroot * Wake up readers. 1968925Sroot */ 19740937Skarels if (control) { 19845004Skarels if (sbappendcontrol(rcv, m, control)) 19945004Skarels control = 0; 20040937Skarels } else 20125632Skarels sbappend(rcv, m); 20225632Skarels snd->sb_mbmax -= 20325632Skarels rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 20425632Skarels unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 20525632Skarels snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 20625632Skarels unp->unp_conn->unp_cc = rcv->sb_cc; 20717543Skarels sorwakeup(so2); 20817543Skarels m = 0; 2098925Sroot #undef snd 2108925Sroot #undef rcv 2118925Sroot break; 2128925Sroot 2138925Sroot default: 2148925Sroot panic("uipc 4"); 2158925Sroot } 2168925Sroot break; 2178925Sroot 2188925Sroot case PRU_ABORT: 2198925Sroot unp_drop(unp, ECONNABORTED); 2208925Sroot break; 2218925Sroot 2228925Sroot case PRU_SENSE: 22316973Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 22416973Skarels if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 22516973Skarels so2 = unp->unp_conn->unp_socket; 22616973Skarels ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 22716973Skarels } 22821110Skarels ((struct stat *) m)->st_dev = NODEV; 22940800Ssklower if (unp->unp_ino == 0) 23040800Ssklower unp->unp_ino = unp_ino++; 23140800Ssklower ((struct stat *) m)->st_ino = unp->unp_ino; 23216973Skarels return (0); 2338925Sroot 2348925Sroot case PRU_RCVOOB: 23516774Sbloom return (EOPNOTSUPP); 2368925Sroot 2378925Sroot case PRU_SENDOOB: 23817543Skarels error = EOPNOTSUPP; 2398925Sroot break; 2408925Sroot 2418925Sroot case PRU_SOCKADDR: 24237617Smckusick if (unp->unp_addr) { 24337617Smckusick nam->m_len = unp->unp_addr->m_len; 24437617Smckusick bcopy(mtod(unp->unp_addr, caddr_t), 24537617Smckusick mtod(nam, caddr_t), (unsigned)nam->m_len); 24637617Smckusick } else 24737617Smckusick nam->m_len = 0; 2488925Sroot break; 2498925Sroot 25014121Ssam case PRU_PEERADDR: 25128292Skarels if (unp->unp_conn && unp->unp_conn->unp_addr) { 25228292Skarels nam->m_len = unp->unp_conn->unp_addr->m_len; 25328292Skarels bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 25433287Sbostic mtod(nam, caddr_t), (unsigned)nam->m_len); 25537617Smckusick } else 25637617Smckusick nam->m_len = 0; 25714121Ssam break; 25814121Ssam 2598925Sroot case PRU_SLOWTIMO: 2608925Sroot break; 2618925Sroot 2628925Sroot default: 2638925Sroot panic("piusrreq"); 2648925Sroot } 26512760Ssam release: 26640937Skarels if (control) 26740937Skarels m_freem(control); 26812760Ssam if (m) 26912760Ssam m_freem(m); 27011709Ssam return (error); 2718925Sroot } 2728925Sroot 27316973Skarels /* 27425632Skarels * Both send and receive buffers are allocated PIPSIZ bytes of buffering 27525632Skarels * for stream sockets, although the total for sender and receiver is 27625632Skarels * actually only PIPSIZ. 27716973Skarels * Datagram sockets really use the sendspace as the maximum datagram size, 27816973Skarels * and don't really want to reserve the sendspace. Their recvspace should 27916973Skarels * be large enough for at least one max-size datagram plus address. 28016973Skarels */ 28116973Skarels #define PIPSIZ 4096 28237617Smckusick u_long unpst_sendspace = PIPSIZ; 28337617Smckusick u_long unpst_recvspace = PIPSIZ; 28437617Smckusick u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 28537617Smckusick u_long unpdg_recvspace = 4*1024; 2868925Sroot 28725632Skarels int unp_rights; /* file descriptors in flight */ 28825632Skarels 2899169Ssam unp_attach(so) 2908925Sroot struct socket *so; 2918925Sroot { 2929169Ssam register struct mbuf *m; 2938925Sroot register struct unpcb *unp; 2948925Sroot int error; 2958925Sroot 29637617Smckusick if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 29737617Smckusick switch (so->so_type) { 29816973Skarels 29937617Smckusick case SOCK_STREAM: 30037617Smckusick error = soreserve(so, unpst_sendspace, unpst_recvspace); 30137617Smckusick break; 30216973Skarels 30337617Smckusick case SOCK_DGRAM: 30437617Smckusick error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 30537617Smckusick break; 306*52921Smckusick 307*52921Smckusick default: 308*52921Smckusick panic("unp_attach"); 30937617Smckusick } 31037617Smckusick if (error) 31137617Smckusick return (error); 31216973Skarels } 3139637Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 31410139Ssam if (m == NULL) 31510139Ssam return (ENOBUFS); 3168925Sroot unp = mtod(m, struct unpcb *); 3178925Sroot so->so_pcb = (caddr_t)unp; 3188925Sroot unp->unp_socket = so; 3198925Sroot return (0); 3208925Sroot } 3218925Sroot 3228925Sroot unp_detach(unp) 3239169Ssam register struct unpcb *unp; 3248925Sroot { 3258925Sroot 32637616Smckusick if (unp->unp_vnode) { 32737616Smckusick unp->unp_vnode->v_socket = 0; 32837616Smckusick vrele(unp->unp_vnode); 32937616Smckusick unp->unp_vnode = 0; 3308925Sroot } 3318925Sroot if (unp->unp_conn) 3328925Sroot unp_disconnect(unp); 3338925Sroot while (unp->unp_refs) 3348925Sroot unp_drop(unp->unp_refs, ECONNRESET); 3358925Sroot soisdisconnected(unp->unp_socket); 3368925Sroot unp->unp_socket->so_pcb = 0; 33725632Skarels m_freem(unp->unp_addr); 3389169Ssam (void) m_free(dtom(unp)); 33925632Skarels if (unp_rights) 34025632Skarels unp_gc(); 3418925Sroot } 3428925Sroot 34348022Smckusick unp_bind(unp, nam, p) 3448925Sroot struct unpcb *unp; 3459169Ssam struct mbuf *nam; 34648022Smckusick struct proc *p; 3478925Sroot { 3489169Ssam struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 34937616Smckusick register struct vnode *vp; 35037616Smckusick struct vattr vattr; 3518925Sroot int error; 35247540Skarels struct nameidata nd; 3538925Sroot 35452309Smckusick NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE, 35552309Smckusick soun->sun_path, p); 35637617Smckusick if (unp->unp_vnode != NULL) 35712760Ssam return (EINVAL); 35837617Smckusick if (nam->m_len == MLEN) { 35937617Smckusick if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 36037617Smckusick return (EINVAL); 36137617Smckusick } else 36237617Smckusick *(mtod(nam, caddr_t) + nam->m_len) = 0; 36312760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 36452309Smckusick if (error = namei(&nd)) 36537616Smckusick return (error); 36652309Smckusick vp = nd.ni_vp; 36737616Smckusick if (vp != NULL) { 36852309Smckusick VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 36952309Smckusick if (nd.ni_dvp == vp) 37052309Smckusick vrele(nd.ni_dvp); 37143342Smckusick else 37252309Smckusick vput(nd.ni_dvp); 37342465Smckusick vrele(vp); 37410139Ssam return (EADDRINUSE); 3758925Sroot } 37641362Smckusick VATTR_NULL(&vattr); 37737616Smckusick vattr.va_type = VSOCK; 37837616Smckusick vattr.va_mode = 0777; 37952309Smckusick LEASE_CHECK(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE); 38052309Smckusick if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr)) 38111828Ssam return (error); 38252309Smckusick vp = nd.ni_vp; 38337616Smckusick vp->v_socket = unp->unp_socket; 38437616Smckusick unp->unp_vnode = vp; 38525632Skarels unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 38637728Smckusick VOP_UNLOCK(vp); 3878925Sroot return (0); 3888925Sroot } 3898925Sroot 39048022Smckusick unp_connect(so, nam, p) 3918925Sroot struct socket *so; 3929169Ssam struct mbuf *nam; 39348022Smckusick struct proc *p; 3948925Sroot { 3959169Ssam register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 39637616Smckusick register struct vnode *vp; 39737617Smckusick register struct socket *so2, *so3; 39837617Smckusick struct unpcb *unp2, *unp3; 39937616Smckusick int error; 40047540Skarels struct nameidata nd; 4018925Sroot 40252309Smckusick NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, soun->sun_path, p); 40337617Smckusick if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) { /* XXX */ 40437617Smckusick if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0) 40537617Smckusick return (EMSGSIZE); 40637617Smckusick } else 40737617Smckusick *(mtod(nam, caddr_t) + nam->m_len) = 0; 40852309Smckusick if (error = namei(&nd)) 40937616Smckusick return (error); 41052309Smckusick vp = nd.ni_vp; 41137616Smckusick if (vp->v_type != VSOCK) { 4128925Sroot error = ENOTSOCK; 4138925Sroot goto bad; 4148925Sroot } 41548022Smckusick if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) 41638396Smckusick goto bad; 41737616Smckusick so2 = vp->v_socket; 4188925Sroot if (so2 == 0) { 4198925Sroot error = ECONNREFUSED; 4208925Sroot goto bad; 4218925Sroot } 42213115Ssam if (so->so_type != so2->so_type) { 42313115Ssam error = EPROTOTYPE; 42413115Ssam goto bad; 42513115Ssam } 42637617Smckusick if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 42737617Smckusick if ((so2->so_options & SO_ACCEPTCONN) == 0 || 42840800Ssklower (so3 = sonewconn(so2, 0)) == 0) { 42937617Smckusick error = ECONNREFUSED; 43037617Smckusick goto bad; 43137617Smckusick } 43237617Smckusick unp2 = sotounpcb(so2); 43337617Smckusick unp3 = sotounpcb(so3); 43437617Smckusick if (unp2->unp_addr) 43537617Smckusick unp3->unp_addr = 43637617Smckusick m_copy(unp2->unp_addr, 0, (int)M_COPYALL); 43737617Smckusick so2 = so3; 43813115Ssam } 43926281Skarels error = unp_connect2(so, so2); 44012760Ssam bad: 44137728Smckusick vput(vp); 44212760Ssam return (error); 44312760Ssam } 44412760Ssam 44526281Skarels unp_connect2(so, so2) 44612760Ssam register struct socket *so; 44712760Ssam register struct socket *so2; 44812760Ssam { 44912760Ssam register struct unpcb *unp = sotounpcb(so); 45012760Ssam register struct unpcb *unp2; 45112760Ssam 45212760Ssam if (so2->so_type != so->so_type) 45312760Ssam return (EPROTOTYPE); 45414049Ssam unp2 = sotounpcb(so2); 45514049Ssam unp->unp_conn = unp2; 4568925Sroot switch (so->so_type) { 4578925Sroot 4588925Sroot case SOCK_DGRAM: 4598925Sroot unp->unp_nextref = unp2->unp_refs; 4608925Sroot unp2->unp_refs = unp; 46117543Skarels soisconnected(so); 4628925Sroot break; 4638925Sroot 4648925Sroot case SOCK_STREAM: 4659169Ssam unp2->unp_conn = unp; 46640800Ssklower soisconnected(so); 46714049Ssam soisconnected(so2); 4688925Sroot break; 4698925Sroot 4708925Sroot default: 47112760Ssam panic("unp_connect2"); 4728925Sroot } 4738925Sroot return (0); 4748925Sroot } 4759169Ssam 4769169Ssam unp_disconnect(unp) 4779169Ssam struct unpcb *unp; 4789169Ssam { 4799169Ssam register struct unpcb *unp2 = unp->unp_conn; 4809169Ssam 4819169Ssam if (unp2 == 0) 4829169Ssam return; 4839169Ssam unp->unp_conn = 0; 4849169Ssam switch (unp->unp_socket->so_type) { 4859169Ssam 4869169Ssam case SOCK_DGRAM: 4879169Ssam if (unp2->unp_refs == unp) 4889169Ssam unp2->unp_refs = unp->unp_nextref; 4899169Ssam else { 4909169Ssam unp2 = unp2->unp_refs; 4919169Ssam for (;;) { 4929169Ssam if (unp2 == 0) 4939169Ssam panic("unp_disconnect"); 4949169Ssam if (unp2->unp_nextref == unp) 4959169Ssam break; 4969169Ssam unp2 = unp2->unp_nextref; 4979169Ssam } 4989169Ssam unp2->unp_nextref = unp->unp_nextref; 4999169Ssam } 5009169Ssam unp->unp_nextref = 0; 50121768Skarels unp->unp_socket->so_state &= ~SS_ISCONNECTED; 5029169Ssam break; 5039169Ssam 5049169Ssam case SOCK_STREAM: 50514049Ssam soisdisconnected(unp->unp_socket); 5069169Ssam unp2->unp_conn = 0; 5079169Ssam soisdisconnected(unp2->unp_socket); 5089169Ssam break; 5099169Ssam } 5109169Ssam } 5119169Ssam 51212760Ssam #ifdef notdef 5139169Ssam unp_abort(unp) 5149169Ssam struct unpcb *unp; 5159169Ssam { 5169169Ssam 5179169Ssam unp_detach(unp); 5189169Ssam } 51912760Ssam #endif 5209169Ssam 52145004Skarels unp_shutdown(unp) 5229169Ssam struct unpcb *unp; 5239169Ssam { 52445004Skarels struct socket *so; 5259169Ssam 52645004Skarels if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn && 52745004Skarels (so = unp->unp_conn->unp_socket)) 52845004Skarels socantrcvmore(so); 5299169Ssam } 5309169Ssam 5319169Ssam unp_drop(unp, errno) 5329169Ssam struct unpcb *unp; 5339169Ssam int errno; 5349169Ssam { 53516054Skarels struct socket *so = unp->unp_socket; 5369169Ssam 53716054Skarels so->so_error = errno; 5389169Ssam unp_disconnect(unp); 53916054Skarels if (so->so_head) { 54016054Skarels so->so_pcb = (caddr_t) 0; 54125632Skarels m_freem(unp->unp_addr); 54216054Skarels (void) m_free(dtom(unp)); 54316054Skarels sofree(so); 54416054Skarels } 5459169Ssam } 5469169Ssam 54712760Ssam #ifdef notdef 5489169Ssam unp_drain() 5499169Ssam { 5509169Ssam 5519169Ssam } 55212760Ssam #endif 55312760Ssam 55412760Ssam unp_externalize(rights) 55512760Ssam struct mbuf *rights; 55612760Ssam { 55747540Skarels struct proc *p = curproc; /* XXX */ 55812760Ssam register int i; 55940800Ssklower register struct cmsghdr *cm = mtod(rights, struct cmsghdr *); 56040800Ssklower register struct file **rp = (struct file **)(cm + 1); 56112760Ssam register struct file *fp; 56240800Ssklower int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int); 56312760Ssam int f; 56412760Ssam 56547540Skarels if (fdavail(p, newfds)) { 56612760Ssam for (i = 0; i < newfds; i++) { 56712760Ssam fp = *rp; 56812760Ssam unp_discard(fp); 56912760Ssam *rp++ = 0; 57012760Ssam } 57112760Ssam return (EMSGSIZE); 57212760Ssam } 57312760Ssam for (i = 0; i < newfds; i++) { 57447540Skarels if (fdalloc(p, 0, &f)) 57512760Ssam panic("unp_externalize"); 57612760Ssam fp = *rp; 57747647Skarels p->p_fd->fd_ofiles[f] = fp; 57812760Ssam fp->f_msgcount--; 57925632Skarels unp_rights--; 58014927Smckusick *(int *)rp++ = f; 58112760Ssam } 58212760Ssam return (0); 58312760Ssam } 58412760Ssam 58548022Smckusick unp_internalize(control, p) 58640937Skarels struct mbuf *control; 58748022Smckusick struct proc *p; 58812760Ssam { 58948022Smckusick struct filedesc *fdp = p->p_fd; 59040937Skarels register struct cmsghdr *cm = mtod(control, struct cmsghdr *); 59112760Ssam register struct file **rp; 59240937Skarels register struct file *fp; 59337728Smckusick register int i, fd; 59440937Skarels int oldfds; 59512760Ssam 59640937Skarels if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 59740937Skarels cm->cmsg_len != control->m_len) 59840800Ssklower return (EINVAL); 59940800Ssklower oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int); 60040800Ssklower rp = (struct file **)(cm + 1); 60137728Smckusick for (i = 0; i < oldfds; i++) { 60237728Smckusick fd = *(int *)rp++; 60347647Skarels if ((unsigned)fd >= fdp->fd_nfiles || 60447647Skarels fdp->fd_ofiles[fd] == NULL) 60512760Ssam return (EBADF); 60637728Smckusick } 60740800Ssklower rp = (struct file **)(cm + 1); 60813084Ssam for (i = 0; i < oldfds; i++) { 60947647Skarels fp = fdp->fd_ofiles[*(int *)rp]; 61012760Ssam *rp++ = fp; 61112760Ssam fp->f_count++; 61212760Ssam fp->f_msgcount++; 61325632Skarels unp_rights++; 61412760Ssam } 61512760Ssam return (0); 61612760Ssam } 61712760Ssam 61812760Ssam int unp_defer, unp_gcing; 61912760Ssam int unp_mark(); 62016995Skarels extern struct domain unixdomain; 62112760Ssam 62212760Ssam unp_gc() 62312760Ssam { 62412760Ssam register struct file *fp; 62512760Ssam register struct socket *so; 62612760Ssam 62712760Ssam if (unp_gcing) 62812760Ssam return; 62912760Ssam unp_gcing = 1; 63012760Ssam restart: 63112760Ssam unp_defer = 0; 63249978Smckusick for (fp = filehead; fp; fp = fp->f_filef) 63312760Ssam fp->f_flag &= ~(FMARK|FDEFER); 63412760Ssam do { 63549978Smckusick for (fp = filehead; fp; fp = fp->f_filef) { 63612760Ssam if (fp->f_count == 0) 63712760Ssam continue; 63812760Ssam if (fp->f_flag & FDEFER) { 63912760Ssam fp->f_flag &= ~FDEFER; 64012760Ssam unp_defer--; 64112760Ssam } else { 64212760Ssam if (fp->f_flag & FMARK) 64312760Ssam continue; 64412760Ssam if (fp->f_count == fp->f_msgcount) 64512760Ssam continue; 64612760Ssam fp->f_flag |= FMARK; 64712760Ssam } 64837617Smckusick if (fp->f_type != DTYPE_SOCKET || 64937617Smckusick (so = (struct socket *)fp->f_data) == 0) 65012760Ssam continue; 65116995Skarels if (so->so_proto->pr_domain != &unixdomain || 65221768Skarels (so->so_proto->pr_flags&PR_RIGHTS) == 0) 65312760Ssam continue; 65445004Skarels #ifdef notdef 65512760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 65645004Skarels /* 65745004Skarels * This is problematical; it's not clear 65845004Skarels * we need to wait for the sockbuf to be 65945004Skarels * unlocked (on a uniprocessor, at least), 66045004Skarels * and it's also not clear what to do 66145004Skarels * if sbwait returns an error due to receipt 66245004Skarels * of a signal. If sbwait does return 66345004Skarels * an error, we'll go into an infinite 66445004Skarels * loop. Delete all of this for now. 66545004Skarels */ 66645004Skarels (void) sbwait(&so->so_rcv); 66712760Ssam goto restart; 66812760Ssam } 66945004Skarels #endif 67012760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 67112760Ssam } 67212760Ssam } while (unp_defer); 67349978Smckusick for (fp = filehead; fp; fp = fp->f_filef) { 67412760Ssam if (fp->f_count == 0) 67512760Ssam continue; 67625632Skarels if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 67725632Skarels while (fp->f_msgcount) 67825632Skarels unp_discard(fp); 67912760Ssam } 68012760Ssam unp_gcing = 0; 68112760Ssam } 68212760Ssam 68316995Skarels unp_dispose(m) 68416995Skarels struct mbuf *m; 68516995Skarels { 68616995Skarels int unp_discard(); 68716995Skarels 68817020Skarels if (m) 68917020Skarels unp_scan(m, unp_discard); 69016995Skarels } 69116995Skarels 69216995Skarels unp_scan(m0, op) 69316995Skarels register struct mbuf *m0; 69412760Ssam int (*op)(); 69512760Ssam { 69616995Skarels register struct mbuf *m; 69712760Ssam register struct file **rp; 69840937Skarels register struct cmsghdr *cm; 69912760Ssam register int i; 70017020Skarels int qfds; 70112760Ssam 70216995Skarels while (m0) { 70316995Skarels for (m = m0; m; m = m->m_next) 70440937Skarels if (m->m_type == MT_CONTROL && 70540937Skarels m->m_len >= sizeof(*cm)) { 70640800Ssklower cm = mtod(m, struct cmsghdr *); 70740937Skarels if (cm->cmsg_level != SOL_SOCKET || 70840937Skarels cm->cmsg_type != SCM_RIGHTS) 70940937Skarels continue; 71040800Ssklower qfds = (cm->cmsg_len - sizeof *cm) 71140800Ssklower / sizeof (struct file *); 71240800Ssklower rp = (struct file **)(cm + 1); 71316995Skarels for (i = 0; i < qfds; i++) 71416995Skarels (*op)(*rp++); 71516995Skarels break; /* XXX, but saves time */ 71616995Skarels } 71717020Skarels m0 = m0->m_act; 71812760Ssam } 71912760Ssam } 72012760Ssam 72112760Ssam unp_mark(fp) 72212760Ssam struct file *fp; 72312760Ssam { 72412760Ssam 72512760Ssam if (fp->f_flag & FMARK) 72612760Ssam return; 72712760Ssam unp_defer++; 72812760Ssam fp->f_flag |= (FMARK|FDEFER); 72912760Ssam } 73012760Ssam 73112760Ssam unp_discard(fp) 73212760Ssam struct file *fp; 73312760Ssam { 73412760Ssam 73512760Ssam fp->f_msgcount--; 73625632Skarels unp_rights--; 73752033Skarels (void) closef(fp, (struct proc *)NULL); 73812760Ssam } 739