123443Smckusick /* 229129Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 3*33288Sbostic * All rights reserved. 423443Smckusick * 5*33288Sbostic * Redistribution and use in source and binary forms are permitted 6*33288Sbostic * provided that this notice is preserved and that due credit is given 7*33288Sbostic * to the University of California at Berkeley. The name of the University 8*33288Sbostic * may not be used to endorse or promote products derived from this 9*33288Sbostic * software without specific prior written permission. This software 10*33288Sbostic * is provided ``as is'' without express or implied warranty. 11*33288Sbostic * 12*33288Sbostic * @(#)uipc_usrreq.c 7.3 (Berkeley) 01/07/88 1323443Smckusick */ 148925Sroot 1517105Sbloom #include "param.h" 1617105Sbloom #include "dir.h" 1717105Sbloom #include "user.h" 1817105Sbloom #include "mbuf.h" 1917105Sbloom #include "domain.h" 2017105Sbloom #include "protosw.h" 2117105Sbloom #include "socket.h" 2217105Sbloom #include "socketvar.h" 2317105Sbloom #include "unpcb.h" 2417105Sbloom #include "un.h" 2517105Sbloom #include "inode.h" 2617105Sbloom #include "file.h" 2717105Sbloom #include "stat.h" 288925Sroot 298925Sroot /* 308925Sroot * Unix communications domain. 3112760Ssam * 3212760Ssam * TODO: 3312760Ssam * SEQPACKET, RDM 3413119Ssam * rethink name space problems 3512760Ssam * need a proper out-of-band 368925Sroot */ 3713119Ssam struct sockaddr sun_noname = { AF_UNIX }; 3825632Skarels ino_t unp_ino; /* prototype for fake inode numbers */ 398925Sroot 408925Sroot /*ARGSUSED*/ 4112760Ssam uipc_usrreq(so, req, m, nam, rights) 428925Sroot struct socket *so; 438925Sroot int req; 4412760Ssam struct mbuf *m, *nam, *rights; 458925Sroot { 468925Sroot struct unpcb *unp = sotounpcb(so); 478925Sroot register struct socket *so2; 488925Sroot int error = 0; 498925Sroot 5025555Skarels if (req == PRU_CONTROL) 5125555Skarels return (EOPNOTSUPP); 5212760Ssam if (req != PRU_SEND && rights && rights->m_len) { 5312760Ssam error = EOPNOTSUPP; 5412760Ssam goto release; 5512760Ssam } 5612760Ssam if (unp == 0 && req != PRU_ATTACH) { 5712760Ssam error = EINVAL; 5812760Ssam goto release; 5912760Ssam } 608925Sroot switch (req) { 618925Sroot 628925Sroot case PRU_ATTACH: 638925Sroot if (unp) { 649169Ssam error = EISCONN; 658925Sroot break; 668925Sroot } 679028Sroot error = unp_attach(so); 688925Sroot break; 698925Sroot 708925Sroot case PRU_DETACH: 718925Sroot unp_detach(unp); 728925Sroot break; 738925Sroot 749169Ssam case PRU_BIND: 759169Ssam error = unp_bind(unp, nam); 769169Ssam break; 779169Ssam 789169Ssam case PRU_LISTEN: 799169Ssam if (unp->unp_inode == 0) 809169Ssam error = EINVAL; 819169Ssam break; 829169Ssam 838925Sroot case PRU_CONNECT: 849028Sroot error = unp_connect(so, nam); 858925Sroot break; 868925Sroot 8712760Ssam case PRU_CONNECT2: 8826281Skarels error = unp_connect2(so, (struct socket *)nam); 8912760Ssam break; 9012760Ssam 918925Sroot case PRU_DISCONNECT: 928925Sroot unp_disconnect(unp); 938925Sroot break; 948925Sroot 959169Ssam case PRU_ACCEPT: 9625899Skarels /* 9725899Skarels * Pass back name of connected socket, 9825899Skarels * if it was bound and we are still connected 9925899Skarels * (our peer may have closed already!). 10025899Skarels */ 10125899Skarels if (unp->unp_conn && unp->unp_conn->unp_addr) { 10225632Skarels nam->m_len = unp->unp_conn->unp_addr->m_len; 10325632Skarels bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 10425632Skarels mtod(nam, caddr_t), (unsigned)nam->m_len); 10525632Skarels } else { 10625632Skarels nam->m_len = sizeof(sun_noname); 10725632Skarels *(mtod(nam, struct sockaddr *)) = sun_noname; 10825632Skarels } 1098925Sroot break; 1108925Sroot 1118925Sroot case PRU_SHUTDOWN: 1128925Sroot socantsendmore(so); 1138925Sroot unp_usrclosed(unp); 1148925Sroot break; 1158925Sroot 1168925Sroot case PRU_RCVD: 1178925Sroot switch (so->so_type) { 1188925Sroot 1198925Sroot case SOCK_DGRAM: 1208925Sroot panic("uipc 1"); 12110139Ssam /*NOTREACHED*/ 1228925Sroot 12310139Ssam case SOCK_STREAM: 1248925Sroot #define rcv (&so->so_rcv) 1258925Sroot #define snd (&so2->so_snd) 1268925Sroot if (unp->unp_conn == 0) 1278925Sroot break; 1288925Sroot so2 = unp->unp_conn->unp_socket; 1298925Sroot /* 13025632Skarels * Adjust backpressure on sender 1318925Sroot * and wakeup any waiting to write. 1328925Sroot */ 13325632Skarels snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 13425632Skarels unp->unp_mbcnt = rcv->sb_mbcnt; 13525632Skarels snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 13625632Skarels unp->unp_cc = rcv->sb_cc; 13717543Skarels sowwakeup(so2); 1388925Sroot #undef snd 1398925Sroot #undef rcv 1408925Sroot break; 1418925Sroot 1428925Sroot default: 1438925Sroot panic("uipc 2"); 1448925Sroot } 1458925Sroot break; 1468925Sroot 1478925Sroot case PRU_SEND: 14825632Skarels if (rights) { 14925632Skarels error = unp_internalize(rights); 15025632Skarels if (error) 15125632Skarels break; 15225632Skarels } 1538925Sroot switch (so->so_type) { 1548925Sroot 15525632Skarels case SOCK_DGRAM: { 15625632Skarels struct sockaddr *from; 15725632Skarels 1589028Sroot if (nam) { 1598925Sroot if (unp->unp_conn) { 1608925Sroot error = EISCONN; 1618925Sroot break; 1628925Sroot } 1639028Sroot error = unp_connect(so, nam); 1648925Sroot if (error) 1658925Sroot break; 1668925Sroot } else { 1678925Sroot if (unp->unp_conn == 0) { 1688925Sroot error = ENOTCONN; 1698925Sroot break; 1708925Sroot } 1718925Sroot } 1728925Sroot so2 = unp->unp_conn->unp_socket; 17325632Skarels if (unp->unp_addr) 17425632Skarels from = mtod(unp->unp_addr, struct sockaddr *); 17525632Skarels else 17625632Skarels from = &sun_noname; 17725632Skarels if (sbspace(&so2->so_rcv) > 0 && 17825632Skarels sbappendaddr(&so2->so_rcv, from, m, rights)) { 17925632Skarels sorwakeup(so2); 18025632Skarels m = 0; 18125632Skarels } else 18225632Skarels error = ENOBUFS; 1839028Sroot if (nam) 1849169Ssam unp_disconnect(unp); 1858925Sroot break; 18625632Skarels } 1878925Sroot 1888925Sroot case SOCK_STREAM: 1898925Sroot #define rcv (&so2->so_rcv) 1908925Sroot #define snd (&so->so_snd) 19123524Skarels if (so->so_state & SS_CANTSENDMORE) { 19223524Skarels error = EPIPE; 19323524Skarels break; 19423524Skarels } 1958925Sroot if (unp->unp_conn == 0) 1968925Sroot panic("uipc 3"); 1978925Sroot so2 = unp->unp_conn->unp_socket; 1988925Sroot /* 19925632Skarels * Send to paired receive port, and then reduce 20025632Skarels * send buffer hiwater marks to maintain backpressure. 2018925Sroot * Wake up readers. 2028925Sroot */ 20325632Skarels if (rights) 20426365Skarels (void)sbappendrights(rcv, m, rights); 20525632Skarels else 20625632Skarels sbappend(rcv, m); 20725632Skarels snd->sb_mbmax -= 20825632Skarels rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 20925632Skarels unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 21025632Skarels snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 21125632Skarels unp->unp_conn->unp_cc = rcv->sb_cc; 21217543Skarels sorwakeup(so2); 21317543Skarels m = 0; 2148925Sroot #undef snd 2158925Sroot #undef rcv 2168925Sroot break; 2178925Sroot 2188925Sroot default: 2198925Sroot panic("uipc 4"); 2208925Sroot } 2218925Sroot break; 2228925Sroot 2238925Sroot case PRU_ABORT: 2248925Sroot unp_drop(unp, ECONNABORTED); 2258925Sroot break; 2268925Sroot 2278925Sroot case PRU_SENSE: 22816973Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 22916973Skarels if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 23016973Skarels so2 = unp->unp_conn->unp_socket; 23116973Skarels ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 23216973Skarels } 23321110Skarels ((struct stat *) m)->st_dev = NODEV; 23425632Skarels if (unp->unp_ino == 0) 23525632Skarels unp->unp_ino = unp_ino++; 23625632Skarels ((struct stat *) m)->st_ino = unp->unp_ino; 23716973Skarels return (0); 2388925Sroot 2398925Sroot case PRU_RCVOOB: 24016774Sbloom return (EOPNOTSUPP); 2418925Sroot 2428925Sroot case PRU_SENDOOB: 24317543Skarels error = EOPNOTSUPP; 2448925Sroot break; 2458925Sroot 2468925Sroot case PRU_SOCKADDR: 2478925Sroot break; 2488925Sroot 24914121Ssam case PRU_PEERADDR: 25028292Skarels if (unp->unp_conn && unp->unp_conn->unp_addr) { 25128292Skarels nam->m_len = unp->unp_conn->unp_addr->m_len; 25228292Skarels bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 25333287Sbostic mtod(nam, caddr_t), (unsigned)nam->m_len); 25428292Skarels } 25514121Ssam break; 25614121Ssam 2578925Sroot case PRU_SLOWTIMO: 2588925Sroot break; 2598925Sroot 2608925Sroot default: 2618925Sroot panic("piusrreq"); 2628925Sroot } 26312760Ssam release: 26412760Ssam if (m) 26512760Ssam m_freem(m); 26611709Ssam return (error); 2678925Sroot } 2688925Sroot 26916973Skarels /* 27025632Skarels * Both send and receive buffers are allocated PIPSIZ bytes of buffering 27125632Skarels * for stream sockets, although the total for sender and receiver is 27225632Skarels * actually only PIPSIZ. 27316973Skarels * Datagram sockets really use the sendspace as the maximum datagram size, 27416973Skarels * and don't really want to reserve the sendspace. Their recvspace should 27516973Skarels * be large enough for at least one max-size datagram plus address. 27616973Skarels */ 27716973Skarels #define PIPSIZ 4096 27816973Skarels int unpst_sendspace = PIPSIZ; 27925632Skarels int unpst_recvspace = PIPSIZ; 28016973Skarels int unpdg_sendspace = 2*1024; /* really max datagram size */ 28116973Skarels int unpdg_recvspace = 4*1024; 2828925Sroot 28325632Skarels int unp_rights; /* file descriptors in flight */ 28425632Skarels 2859169Ssam unp_attach(so) 2868925Sroot struct socket *so; 2878925Sroot { 2889169Ssam register struct mbuf *m; 2898925Sroot register struct unpcb *unp; 2908925Sroot int error; 2918925Sroot 29216973Skarels switch (so->so_type) { 29316973Skarels 29416973Skarels case SOCK_STREAM: 29516973Skarels error = soreserve(so, unpst_sendspace, unpst_recvspace); 29616973Skarels break; 29716973Skarels 29816973Skarels case SOCK_DGRAM: 29916973Skarels error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 30016973Skarels break; 30116973Skarels } 3028925Sroot if (error) 30310139Ssam return (error); 3049637Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 30510139Ssam if (m == NULL) 30610139Ssam return (ENOBUFS); 3078925Sroot unp = mtod(m, struct unpcb *); 3088925Sroot so->so_pcb = (caddr_t)unp; 3098925Sroot unp->unp_socket = so; 3108925Sroot return (0); 3118925Sroot } 3128925Sroot 3138925Sroot unp_detach(unp) 3149169Ssam register struct unpcb *unp; 3158925Sroot { 3168925Sroot 3178925Sroot if (unp->unp_inode) { 31817020Skarels unp->unp_inode->i_socket = 0; 3198925Sroot irele(unp->unp_inode); 3208925Sroot unp->unp_inode = 0; 3218925Sroot } 3228925Sroot if (unp->unp_conn) 3238925Sroot unp_disconnect(unp); 3248925Sroot while (unp->unp_refs) 3258925Sroot unp_drop(unp->unp_refs, ECONNRESET); 3268925Sroot soisdisconnected(unp->unp_socket); 3278925Sroot unp->unp_socket->so_pcb = 0; 32825632Skarels m_freem(unp->unp_addr); 3299169Ssam (void) m_free(dtom(unp)); 33025632Skarels if (unp_rights) 33125632Skarels unp_gc(); 3328925Sroot } 3338925Sroot 3349169Ssam unp_bind(unp, nam) 3358925Sroot struct unpcb *unp; 3369169Ssam struct mbuf *nam; 3378925Sroot { 3389169Ssam struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3398925Sroot register struct inode *ip; 34016695Smckusick register struct nameidata *ndp = &u.u_nd; 3418925Sroot int error; 3428925Sroot 34316695Smckusick ndp->ni_dirp = soun->sun_path; 34425232Smckusick if (unp->unp_inode != NULL || nam->m_len == MLEN) 34512760Ssam return (EINVAL); 34612760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 34712760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 34816695Smckusick ndp->ni_nameiop = CREATE | FOLLOW; 34916695Smckusick ndp->ni_segflg = UIO_SYSSPACE; 35016695Smckusick ip = namei(ndp); 3518925Sroot if (ip) { 3528925Sroot iput(ip); 35310139Ssam return (EADDRINUSE); 3548925Sroot } 35511828Ssam if (error = u.u_error) { 35611828Ssam u.u_error = 0; /* XXX */ 35711828Ssam return (error); 35811828Ssam } 35916695Smckusick ip = maknode(IFSOCK | 0777, ndp); 3608925Sroot if (ip == NULL) { 3618925Sroot error = u.u_error; /* XXX */ 3628925Sroot u.u_error = 0; /* XXX */ 3638925Sroot return (error); 3648925Sroot } 3658925Sroot ip->i_socket = unp->unp_socket; 3668925Sroot unp->unp_inode = ip; 36725632Skarels unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 3688925Sroot iunlock(ip); /* but keep reference */ 3698925Sroot return (0); 3708925Sroot } 3718925Sroot 3729169Ssam unp_connect(so, nam) 3738925Sroot struct socket *so; 3749169Ssam struct mbuf *nam; 3758925Sroot { 3769169Ssam register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3779169Ssam register struct inode *ip; 3788925Sroot int error; 37912760Ssam register struct socket *so2; 38016695Smckusick register struct nameidata *ndp = &u.u_nd; 3818925Sroot 38216695Smckusick ndp->ni_dirp = soun->sun_path; 38312760Ssam if (nam->m_len + (nam->m_off - MMINOFF) == MLEN) 38412760Ssam return (EMSGSIZE); 38512760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 38616695Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 38716695Smckusick ndp->ni_segflg = UIO_SYSSPACE; 38816695Smckusick ip = namei(ndp); 3898925Sroot if (ip == 0) { 3908925Sroot error = u.u_error; 3918925Sroot u.u_error = 0; 39210139Ssam return (error); /* XXX */ 3938925Sroot } 39417543Skarels if (access(ip, IWRITE)) { 39517543Skarels error = u.u_error; 39617543Skarels u.u_error = 0; /* XXX */ 39717543Skarels goto bad; 39817543Skarels } 3998925Sroot if ((ip->i_mode&IFMT) != IFSOCK) { 4008925Sroot error = ENOTSOCK; 4018925Sroot goto bad; 4028925Sroot } 4038925Sroot so2 = ip->i_socket; 4048925Sroot if (so2 == 0) { 4058925Sroot error = ECONNREFUSED; 4068925Sroot goto bad; 4078925Sroot } 40813115Ssam if (so->so_type != so2->so_type) { 40913115Ssam error = EPROTOTYPE; 41013115Ssam goto bad; 41113115Ssam } 41213115Ssam if (so->so_proto->pr_flags & PR_CONNREQUIRED && 41313115Ssam ((so2->so_options&SO_ACCEPTCONN) == 0 || 41413115Ssam (so2 = sonewconn(so2)) == 0)) { 41513115Ssam error = ECONNREFUSED; 41613115Ssam goto bad; 41713115Ssam } 41826281Skarels error = unp_connect2(so, so2); 41912760Ssam bad: 42012760Ssam iput(ip); 42112760Ssam return (error); 42212760Ssam } 42312760Ssam 42426281Skarels unp_connect2(so, so2) 42512760Ssam register struct socket *so; 42612760Ssam register struct socket *so2; 42712760Ssam { 42812760Ssam register struct unpcb *unp = sotounpcb(so); 42912760Ssam register struct unpcb *unp2; 43012760Ssam 43112760Ssam if (so2->so_type != so->so_type) 43212760Ssam return (EPROTOTYPE); 43314049Ssam unp2 = sotounpcb(so2); 43414049Ssam unp->unp_conn = unp2; 4358925Sroot switch (so->so_type) { 4368925Sroot 4378925Sroot case SOCK_DGRAM: 4388925Sroot unp->unp_nextref = unp2->unp_refs; 4398925Sroot unp2->unp_refs = unp; 44017543Skarels soisconnected(so); 4418925Sroot break; 4428925Sroot 4438925Sroot case SOCK_STREAM: 4449169Ssam unp2->unp_conn = unp; 44514049Ssam soisconnected(so2); 44614049Ssam soisconnected(so); 4478925Sroot break; 4488925Sroot 4498925Sroot default: 45012760Ssam panic("unp_connect2"); 4518925Sroot } 4528925Sroot return (0); 4538925Sroot } 4549169Ssam 4559169Ssam unp_disconnect(unp) 4569169Ssam struct unpcb *unp; 4579169Ssam { 4589169Ssam register struct unpcb *unp2 = unp->unp_conn; 4599169Ssam 4609169Ssam if (unp2 == 0) 4619169Ssam return; 4629169Ssam unp->unp_conn = 0; 4639169Ssam switch (unp->unp_socket->so_type) { 4649169Ssam 4659169Ssam case SOCK_DGRAM: 4669169Ssam if (unp2->unp_refs == unp) 4679169Ssam unp2->unp_refs = unp->unp_nextref; 4689169Ssam else { 4699169Ssam unp2 = unp2->unp_refs; 4709169Ssam for (;;) { 4719169Ssam if (unp2 == 0) 4729169Ssam panic("unp_disconnect"); 4739169Ssam if (unp2->unp_nextref == unp) 4749169Ssam break; 4759169Ssam unp2 = unp2->unp_nextref; 4769169Ssam } 4779169Ssam unp2->unp_nextref = unp->unp_nextref; 4789169Ssam } 4799169Ssam unp->unp_nextref = 0; 48021768Skarels unp->unp_socket->so_state &= ~SS_ISCONNECTED; 4819169Ssam break; 4829169Ssam 4839169Ssam case SOCK_STREAM: 48414049Ssam soisdisconnected(unp->unp_socket); 4859169Ssam unp2->unp_conn = 0; 4869169Ssam soisdisconnected(unp2->unp_socket); 4879169Ssam break; 4889169Ssam } 4899169Ssam } 4909169Ssam 49112760Ssam #ifdef notdef 4929169Ssam unp_abort(unp) 4939169Ssam struct unpcb *unp; 4949169Ssam { 4959169Ssam 4969169Ssam unp_detach(unp); 4979169Ssam } 49812760Ssam #endif 4999169Ssam 5009169Ssam /*ARGSUSED*/ 5019169Ssam unp_usrclosed(unp) 5029169Ssam struct unpcb *unp; 5039169Ssam { 5049169Ssam 5059169Ssam } 5069169Ssam 5079169Ssam unp_drop(unp, errno) 5089169Ssam struct unpcb *unp; 5099169Ssam int errno; 5109169Ssam { 51116054Skarels struct socket *so = unp->unp_socket; 5129169Ssam 51316054Skarels so->so_error = errno; 5149169Ssam unp_disconnect(unp); 51516054Skarels if (so->so_head) { 51616054Skarels so->so_pcb = (caddr_t) 0; 51725632Skarels m_freem(unp->unp_addr); 51816054Skarels (void) m_free(dtom(unp)); 51916054Skarels sofree(so); 52016054Skarels } 5219169Ssam } 5229169Ssam 52312760Ssam #ifdef notdef 5249169Ssam unp_drain() 5259169Ssam { 5269169Ssam 5279169Ssam } 52812760Ssam #endif 52912760Ssam 53012760Ssam unp_externalize(rights) 53112760Ssam struct mbuf *rights; 53212760Ssam { 53312760Ssam int newfds = rights->m_len / sizeof (int); 53412760Ssam register int i; 53512760Ssam register struct file **rp = mtod(rights, struct file **); 53612760Ssam register struct file *fp; 53712760Ssam int f; 53812760Ssam 53912760Ssam if (newfds > ufavail()) { 54012760Ssam for (i = 0; i < newfds; i++) { 54112760Ssam fp = *rp; 54212760Ssam unp_discard(fp); 54312760Ssam *rp++ = 0; 54412760Ssam } 54512760Ssam return (EMSGSIZE); 54612760Ssam } 54712760Ssam for (i = 0; i < newfds; i++) { 54812760Ssam f = ufalloc(0); 54912760Ssam if (f < 0) 55012760Ssam panic("unp_externalize"); 55112760Ssam fp = *rp; 55212760Ssam u.u_ofile[f] = fp; 55312760Ssam fp->f_msgcount--; 55425632Skarels unp_rights--; 55514927Smckusick *(int *)rp++ = f; 55612760Ssam } 55712760Ssam return (0); 55812760Ssam } 55912760Ssam 56012760Ssam unp_internalize(rights) 56112760Ssam struct mbuf *rights; 56212760Ssam { 56312760Ssam register struct file **rp; 56412760Ssam int oldfds = rights->m_len / sizeof (int); 56512760Ssam register int i; 56612760Ssam register struct file *fp; 56712760Ssam 56812760Ssam rp = mtod(rights, struct file **); 56913084Ssam for (i = 0; i < oldfds; i++) 57012760Ssam if (getf(*(int *)rp++) == 0) 57112760Ssam return (EBADF); 57212760Ssam rp = mtod(rights, struct file **); 57313084Ssam for (i = 0; i < oldfds; i++) { 57412760Ssam fp = getf(*(int *)rp); 57512760Ssam *rp++ = fp; 57612760Ssam fp->f_count++; 57712760Ssam fp->f_msgcount++; 57825632Skarels unp_rights++; 57912760Ssam } 58012760Ssam return (0); 58112760Ssam } 58212760Ssam 58312760Ssam int unp_defer, unp_gcing; 58412760Ssam int unp_mark(); 58516995Skarels extern struct domain unixdomain; 58612760Ssam 58712760Ssam unp_gc() 58812760Ssam { 58912760Ssam register struct file *fp; 59012760Ssam register struct socket *so; 59112760Ssam 59212760Ssam if (unp_gcing) 59312760Ssam return; 59412760Ssam unp_gcing = 1; 59512760Ssam restart: 59612760Ssam unp_defer = 0; 59712760Ssam for (fp = file; fp < fileNFILE; fp++) 59812760Ssam fp->f_flag &= ~(FMARK|FDEFER); 59912760Ssam do { 60012760Ssam for (fp = file; fp < fileNFILE; fp++) { 60112760Ssam if (fp->f_count == 0) 60212760Ssam continue; 60312760Ssam if (fp->f_flag & FDEFER) { 60412760Ssam fp->f_flag &= ~FDEFER; 60512760Ssam unp_defer--; 60612760Ssam } else { 60712760Ssam if (fp->f_flag & FMARK) 60812760Ssam continue; 60912760Ssam if (fp->f_count == fp->f_msgcount) 61012760Ssam continue; 61112760Ssam fp->f_flag |= FMARK; 61212760Ssam } 61312760Ssam if (fp->f_type != DTYPE_SOCKET) 61412760Ssam continue; 61512760Ssam so = (struct socket *)fp->f_data; 61616995Skarels if (so->so_proto->pr_domain != &unixdomain || 61721768Skarels (so->so_proto->pr_flags&PR_RIGHTS) == 0) 61812760Ssam continue; 61912760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 62012760Ssam sbwait(&so->so_rcv); 62112760Ssam goto restart; 62212760Ssam } 62312760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 62412760Ssam } 62512760Ssam } while (unp_defer); 62612760Ssam for (fp = file; fp < fileNFILE; fp++) { 62712760Ssam if (fp->f_count == 0) 62812760Ssam continue; 62925632Skarels if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 63025632Skarels while (fp->f_msgcount) 63125632Skarels unp_discard(fp); 63212760Ssam } 63312760Ssam unp_gcing = 0; 63412760Ssam } 63512760Ssam 63616995Skarels unp_dispose(m) 63716995Skarels struct mbuf *m; 63816995Skarels { 63916995Skarels int unp_discard(); 64016995Skarels 64117020Skarels if (m) 64217020Skarels unp_scan(m, unp_discard); 64316995Skarels } 64416995Skarels 64516995Skarels unp_scan(m0, op) 64616995Skarels register struct mbuf *m0; 64712760Ssam int (*op)(); 64812760Ssam { 64916995Skarels register struct mbuf *m; 65012760Ssam register struct file **rp; 65112760Ssam register int i; 65217020Skarels int qfds; 65312760Ssam 65416995Skarels while (m0) { 65516995Skarels for (m = m0; m; m = m->m_next) 65616995Skarels if (m->m_type == MT_RIGHTS && m->m_len) { 65716995Skarels qfds = m->m_len / sizeof (struct file *); 65816995Skarels rp = mtod(m, struct file **); 65916995Skarels for (i = 0; i < qfds; i++) 66016995Skarels (*op)(*rp++); 66116995Skarels break; /* XXX, but saves time */ 66216995Skarels } 66317020Skarels m0 = m0->m_act; 66412760Ssam } 66512760Ssam } 66612760Ssam 66712760Ssam unp_mark(fp) 66812760Ssam struct file *fp; 66912760Ssam { 67012760Ssam 67112760Ssam if (fp->f_flag & FMARK) 67212760Ssam return; 67312760Ssam unp_defer++; 67412760Ssam fp->f_flag |= (FMARK|FDEFER); 67512760Ssam } 67612760Ssam 67712760Ssam unp_discard(fp) 67812760Ssam struct file *fp; 67912760Ssam { 68012760Ssam 68112760Ssam fp->f_msgcount--; 68225632Skarels unp_rights--; 68313084Ssam closef(fp); 68412760Ssam } 685