123443Smckusick /* 223443Smckusick * Copyright (c) 1982 Regents of the University of California. 323443Smckusick * All rights reserved. The Berkeley software License Agreement 423443Smckusick * specifies the terms and conditions for redistribution. 523443Smckusick * 6*26365Skarels * @(#)uipc_usrreq.c 6.21 (Berkeley) 02/23/86 723443Smckusick */ 88925Sroot 917105Sbloom #include "param.h" 1017105Sbloom #include "dir.h" 1117105Sbloom #include "user.h" 1217105Sbloom #include "mbuf.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" 1917105Sbloom #include "inode.h" 2017105Sbloom #include "file.h" 2117105Sbloom #include "stat.h" 228925Sroot 238925Sroot /* 248925Sroot * Unix communications domain. 2512760Ssam * 2612760Ssam * TODO: 2712760Ssam * SEQPACKET, RDM 2813119Ssam * rethink name space problems 2912760Ssam * need a proper out-of-band 308925Sroot */ 3113119Ssam struct sockaddr sun_noname = { AF_UNIX }; 3225632Skarels ino_t unp_ino; /* prototype for fake inode numbers */ 338925Sroot 348925Sroot /*ARGSUSED*/ 3512760Ssam uipc_usrreq(so, req, m, nam, rights) 368925Sroot struct socket *so; 378925Sroot int req; 3812760Ssam struct mbuf *m, *nam, *rights; 398925Sroot { 408925Sroot struct unpcb *unp = sotounpcb(so); 418925Sroot register struct socket *so2; 428925Sroot int error = 0; 438925Sroot 4425555Skarels if (req == PRU_CONTROL) 4525555Skarels return (EOPNOTSUPP); 4612760Ssam if (req != PRU_SEND && rights && rights->m_len) { 4712760Ssam error = EOPNOTSUPP; 4812760Ssam goto release; 4912760Ssam } 5012760Ssam if (unp == 0 && req != PRU_ATTACH) { 5112760Ssam error = EINVAL; 5212760Ssam goto release; 5312760Ssam } 548925Sroot switch (req) { 558925Sroot 568925Sroot case PRU_ATTACH: 578925Sroot if (unp) { 589169Ssam error = EISCONN; 598925Sroot break; 608925Sroot } 619028Sroot error = unp_attach(so); 628925Sroot break; 638925Sroot 648925Sroot case PRU_DETACH: 658925Sroot unp_detach(unp); 668925Sroot break; 678925Sroot 689169Ssam case PRU_BIND: 699169Ssam error = unp_bind(unp, nam); 709169Ssam break; 719169Ssam 729169Ssam case PRU_LISTEN: 739169Ssam if (unp->unp_inode == 0) 749169Ssam error = EINVAL; 759169Ssam break; 769169Ssam 778925Sroot case PRU_CONNECT: 789028Sroot error = unp_connect(so, nam); 798925Sroot break; 808925Sroot 8112760Ssam case PRU_CONNECT2: 8226281Skarels error = unp_connect2(so, (struct socket *)nam); 8312760Ssam break; 8412760Ssam 858925Sroot case PRU_DISCONNECT: 868925Sroot unp_disconnect(unp); 878925Sroot break; 888925Sroot 899169Ssam case PRU_ACCEPT: 9025899Skarels /* 9125899Skarels * Pass back name of connected socket, 9225899Skarels * if it was bound and we are still connected 9325899Skarels * (our peer may have closed already!). 9425899Skarels */ 9525899Skarels if (unp->unp_conn && unp->unp_conn->unp_addr) { 9625632Skarels nam->m_len = unp->unp_conn->unp_addr->m_len; 9725632Skarels bcopy(mtod(unp->unp_conn->unp_addr, caddr_t), 9825632Skarels mtod(nam, caddr_t), (unsigned)nam->m_len); 9925632Skarels } else { 10025632Skarels nam->m_len = sizeof(sun_noname); 10125632Skarels *(mtod(nam, struct sockaddr *)) = sun_noname; 10225632Skarels } 1038925Sroot break; 1048925Sroot 1058925Sroot case PRU_SHUTDOWN: 1068925Sroot socantsendmore(so); 1078925Sroot unp_usrclosed(unp); 1088925Sroot break; 1098925Sroot 1108925Sroot case PRU_RCVD: 1118925Sroot switch (so->so_type) { 1128925Sroot 1138925Sroot case SOCK_DGRAM: 1148925Sroot panic("uipc 1"); 11510139Ssam /*NOTREACHED*/ 1168925Sroot 11710139Ssam case SOCK_STREAM: 1188925Sroot #define rcv (&so->so_rcv) 1198925Sroot #define snd (&so2->so_snd) 1208925Sroot if (unp->unp_conn == 0) 1218925Sroot break; 1228925Sroot so2 = unp->unp_conn->unp_socket; 1238925Sroot /* 12425632Skarels * Adjust backpressure on sender 1258925Sroot * and wakeup any waiting to write. 1268925Sroot */ 12725632Skarels snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 12825632Skarels unp->unp_mbcnt = rcv->sb_mbcnt; 12925632Skarels snd->sb_hiwat += unp->unp_cc - rcv->sb_cc; 13025632Skarels unp->unp_cc = rcv->sb_cc; 13117543Skarels sowwakeup(so2); 1328925Sroot #undef snd 1338925Sroot #undef rcv 1348925Sroot break; 1358925Sroot 1368925Sroot default: 1378925Sroot panic("uipc 2"); 1388925Sroot } 1398925Sroot break; 1408925Sroot 1418925Sroot case PRU_SEND: 14225632Skarels if (rights) { 14325632Skarels error = unp_internalize(rights); 14425632Skarels if (error) 14525632Skarels break; 14625632Skarels } 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 } 1579028Sroot error = unp_connect(so, nam); 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; 17125632Skarels if (sbspace(&so2->so_rcv) > 0 && 17225632Skarels sbappendaddr(&so2->so_rcv, from, m, rights)) { 17325632Skarels sorwakeup(so2); 17425632Skarels m = 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 */ 19725632Skarels if (rights) 198*26365Skarels (void)sbappendrights(rcv, m, rights); 19925632Skarels else 20025632Skarels sbappend(rcv, m); 20125632Skarels snd->sb_mbmax -= 20225632Skarels rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 20325632Skarels unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 20425632Skarels snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc; 20525632Skarels unp->unp_conn->unp_cc = rcv->sb_cc; 20617543Skarels sorwakeup(so2); 20717543Skarels m = 0; 2088925Sroot #undef snd 2098925Sroot #undef rcv 2108925Sroot break; 2118925Sroot 2128925Sroot default: 2138925Sroot panic("uipc 4"); 2148925Sroot } 2158925Sroot break; 2168925Sroot 2178925Sroot case PRU_ABORT: 2188925Sroot unp_drop(unp, ECONNABORTED); 2198925Sroot break; 2208925Sroot 2218925Sroot case PRU_SENSE: 22216973Skarels ((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat; 22316973Skarels if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) { 22416973Skarels so2 = unp->unp_conn->unp_socket; 22516973Skarels ((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc; 22616973Skarels } 22721110Skarels ((struct stat *) m)->st_dev = NODEV; 22825632Skarels if (unp->unp_ino == 0) 22925632Skarels unp->unp_ino = unp_ino++; 23025632Skarels ((struct stat *) m)->st_ino = unp->unp_ino; 23116973Skarels return (0); 2328925Sroot 2338925Sroot case PRU_RCVOOB: 23416774Sbloom return (EOPNOTSUPP); 2358925Sroot 2368925Sroot case PRU_SENDOOB: 23717543Skarels error = EOPNOTSUPP; 2388925Sroot break; 2398925Sroot 2408925Sroot case PRU_SOCKADDR: 2418925Sroot break; 2428925Sroot 24314121Ssam case PRU_PEERADDR: 24414121Ssam break; 24514121Ssam 2468925Sroot case PRU_SLOWTIMO: 2478925Sroot break; 2488925Sroot 2498925Sroot default: 2508925Sroot panic("piusrreq"); 2518925Sroot } 25212760Ssam release: 25312760Ssam if (m) 25412760Ssam m_freem(m); 25511709Ssam return (error); 2568925Sroot } 2578925Sroot 25816973Skarels /* 25925632Skarels * Both send and receive buffers are allocated PIPSIZ bytes of buffering 26025632Skarels * for stream sockets, although the total for sender and receiver is 26125632Skarels * actually only PIPSIZ. 26216973Skarels * Datagram sockets really use the sendspace as the maximum datagram size, 26316973Skarels * and don't really want to reserve the sendspace. Their recvspace should 26416973Skarels * be large enough for at least one max-size datagram plus address. 26516973Skarels */ 26616973Skarels #define PIPSIZ 4096 26716973Skarels int unpst_sendspace = PIPSIZ; 26825632Skarels int unpst_recvspace = PIPSIZ; 26916973Skarels int unpdg_sendspace = 2*1024; /* really max datagram size */ 27016973Skarels int unpdg_recvspace = 4*1024; 2718925Sroot 27225632Skarels int unp_rights; /* file descriptors in flight */ 27325632Skarels 2749169Ssam unp_attach(so) 2758925Sroot struct socket *so; 2768925Sroot { 2779169Ssam register struct mbuf *m; 2788925Sroot register struct unpcb *unp; 2798925Sroot int error; 2808925Sroot 28116973Skarels switch (so->so_type) { 28216973Skarels 28316973Skarels case SOCK_STREAM: 28416973Skarels error = soreserve(so, unpst_sendspace, unpst_recvspace); 28516973Skarels break; 28616973Skarels 28716973Skarels case SOCK_DGRAM: 28816973Skarels error = soreserve(so, unpdg_sendspace, unpdg_recvspace); 28916973Skarels break; 29016973Skarels } 2918925Sroot if (error) 29210139Ssam return (error); 2939637Ssam m = m_getclr(M_DONTWAIT, MT_PCB); 29410139Ssam if (m == NULL) 29510139Ssam return (ENOBUFS); 2968925Sroot unp = mtod(m, struct unpcb *); 2978925Sroot so->so_pcb = (caddr_t)unp; 2988925Sroot unp->unp_socket = so; 2998925Sroot return (0); 3008925Sroot } 3018925Sroot 3028925Sroot unp_detach(unp) 3039169Ssam register struct unpcb *unp; 3048925Sroot { 3058925Sroot 3068925Sroot if (unp->unp_inode) { 30717020Skarels unp->unp_inode->i_socket = 0; 3088925Sroot irele(unp->unp_inode); 3098925Sroot unp->unp_inode = 0; 3108925Sroot } 3118925Sroot if (unp->unp_conn) 3128925Sroot unp_disconnect(unp); 3138925Sroot while (unp->unp_refs) 3148925Sroot unp_drop(unp->unp_refs, ECONNRESET); 3158925Sroot soisdisconnected(unp->unp_socket); 3168925Sroot unp->unp_socket->so_pcb = 0; 31725632Skarels m_freem(unp->unp_addr); 3189169Ssam (void) m_free(dtom(unp)); 31925632Skarels if (unp_rights) 32025632Skarels unp_gc(); 3218925Sroot } 3228925Sroot 3239169Ssam unp_bind(unp, nam) 3248925Sroot struct unpcb *unp; 3259169Ssam struct mbuf *nam; 3268925Sroot { 3279169Ssam struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3288925Sroot register struct inode *ip; 32916695Smckusick register struct nameidata *ndp = &u.u_nd; 3308925Sroot int error; 3318925Sroot 33216695Smckusick ndp->ni_dirp = soun->sun_path; 33325232Smckusick if (unp->unp_inode != NULL || nam->m_len == MLEN) 33412760Ssam return (EINVAL); 33512760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 33612760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 33716695Smckusick ndp->ni_nameiop = CREATE | FOLLOW; 33816695Smckusick ndp->ni_segflg = UIO_SYSSPACE; 33916695Smckusick ip = namei(ndp); 3408925Sroot if (ip) { 3418925Sroot iput(ip); 34210139Ssam return (EADDRINUSE); 3438925Sroot } 34411828Ssam if (error = u.u_error) { 34511828Ssam u.u_error = 0; /* XXX */ 34611828Ssam return (error); 34711828Ssam } 34816695Smckusick ip = maknode(IFSOCK | 0777, ndp); 3498925Sroot if (ip == NULL) { 3508925Sroot error = u.u_error; /* XXX */ 3518925Sroot u.u_error = 0; /* XXX */ 3528925Sroot return (error); 3538925Sroot } 3548925Sroot ip->i_socket = unp->unp_socket; 3558925Sroot unp->unp_inode = ip; 35625632Skarels unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL); 3578925Sroot iunlock(ip); /* but keep reference */ 3588925Sroot return (0); 3598925Sroot } 3608925Sroot 3619169Ssam unp_connect(so, nam) 3628925Sroot struct socket *so; 3639169Ssam struct mbuf *nam; 3648925Sroot { 3659169Ssam register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *); 3669169Ssam register struct inode *ip; 3678925Sroot int error; 36812760Ssam register struct socket *so2; 36916695Smckusick register struct nameidata *ndp = &u.u_nd; 3708925Sroot 37116695Smckusick ndp->ni_dirp = soun->sun_path; 37212760Ssam if (nam->m_len + (nam->m_off - MMINOFF) == MLEN) 37312760Ssam return (EMSGSIZE); 37412760Ssam *(mtod(nam, caddr_t) + nam->m_len) = 0; 37516695Smckusick ndp->ni_nameiop = LOOKUP | FOLLOW; 37616695Smckusick ndp->ni_segflg = UIO_SYSSPACE; 37716695Smckusick ip = namei(ndp); 3788925Sroot if (ip == 0) { 3798925Sroot error = u.u_error; 3808925Sroot u.u_error = 0; 38110139Ssam return (error); /* XXX */ 3828925Sroot } 38317543Skarels if (access(ip, IWRITE)) { 38417543Skarels error = u.u_error; 38517543Skarels u.u_error = 0; /* XXX */ 38617543Skarels goto bad; 38717543Skarels } 3888925Sroot if ((ip->i_mode&IFMT) != IFSOCK) { 3898925Sroot error = ENOTSOCK; 3908925Sroot goto bad; 3918925Sroot } 3928925Sroot so2 = ip->i_socket; 3938925Sroot if (so2 == 0) { 3948925Sroot error = ECONNREFUSED; 3958925Sroot goto bad; 3968925Sroot } 39713115Ssam if (so->so_type != so2->so_type) { 39813115Ssam error = EPROTOTYPE; 39913115Ssam goto bad; 40013115Ssam } 40113115Ssam if (so->so_proto->pr_flags & PR_CONNREQUIRED && 40213115Ssam ((so2->so_options&SO_ACCEPTCONN) == 0 || 40313115Ssam (so2 = sonewconn(so2)) == 0)) { 40413115Ssam error = ECONNREFUSED; 40513115Ssam goto bad; 40613115Ssam } 40726281Skarels error = unp_connect2(so, so2); 40812760Ssam bad: 40912760Ssam iput(ip); 41012760Ssam return (error); 41112760Ssam } 41212760Ssam 41326281Skarels unp_connect2(so, so2) 41412760Ssam register struct socket *so; 41512760Ssam register struct socket *so2; 41612760Ssam { 41712760Ssam register struct unpcb *unp = sotounpcb(so); 41812760Ssam register struct unpcb *unp2; 41912760Ssam 42012760Ssam if (so2->so_type != so->so_type) 42112760Ssam return (EPROTOTYPE); 42214049Ssam unp2 = sotounpcb(so2); 42314049Ssam unp->unp_conn = unp2; 4248925Sroot switch (so->so_type) { 4258925Sroot 4268925Sroot case SOCK_DGRAM: 4278925Sroot unp->unp_nextref = unp2->unp_refs; 4288925Sroot unp2->unp_refs = unp; 42917543Skarels soisconnected(so); 4308925Sroot break; 4318925Sroot 4328925Sroot case SOCK_STREAM: 4339169Ssam unp2->unp_conn = unp; 43414049Ssam soisconnected(so2); 43514049Ssam soisconnected(so); 4368925Sroot break; 4378925Sroot 4388925Sroot default: 43912760Ssam panic("unp_connect2"); 4408925Sroot } 4418925Sroot return (0); 4428925Sroot } 4439169Ssam 4449169Ssam unp_disconnect(unp) 4459169Ssam struct unpcb *unp; 4469169Ssam { 4479169Ssam register struct unpcb *unp2 = unp->unp_conn; 4489169Ssam 4499169Ssam if (unp2 == 0) 4509169Ssam return; 4519169Ssam unp->unp_conn = 0; 4529169Ssam switch (unp->unp_socket->so_type) { 4539169Ssam 4549169Ssam case SOCK_DGRAM: 4559169Ssam if (unp2->unp_refs == unp) 4569169Ssam unp2->unp_refs = unp->unp_nextref; 4579169Ssam else { 4589169Ssam unp2 = unp2->unp_refs; 4599169Ssam for (;;) { 4609169Ssam if (unp2 == 0) 4619169Ssam panic("unp_disconnect"); 4629169Ssam if (unp2->unp_nextref == unp) 4639169Ssam break; 4649169Ssam unp2 = unp2->unp_nextref; 4659169Ssam } 4669169Ssam unp2->unp_nextref = unp->unp_nextref; 4679169Ssam } 4689169Ssam unp->unp_nextref = 0; 46921768Skarels unp->unp_socket->so_state &= ~SS_ISCONNECTED; 4709169Ssam break; 4719169Ssam 4729169Ssam case SOCK_STREAM: 47314049Ssam soisdisconnected(unp->unp_socket); 4749169Ssam unp2->unp_conn = 0; 4759169Ssam soisdisconnected(unp2->unp_socket); 4769169Ssam break; 4779169Ssam } 4789169Ssam } 4799169Ssam 48012760Ssam #ifdef notdef 4819169Ssam unp_abort(unp) 4829169Ssam struct unpcb *unp; 4839169Ssam { 4849169Ssam 4859169Ssam unp_detach(unp); 4869169Ssam } 48712760Ssam #endif 4889169Ssam 4899169Ssam /*ARGSUSED*/ 4909169Ssam unp_usrclosed(unp) 4919169Ssam struct unpcb *unp; 4929169Ssam { 4939169Ssam 4949169Ssam } 4959169Ssam 4969169Ssam unp_drop(unp, errno) 4979169Ssam struct unpcb *unp; 4989169Ssam int errno; 4999169Ssam { 50016054Skarels struct socket *so = unp->unp_socket; 5019169Ssam 50216054Skarels so->so_error = errno; 5039169Ssam unp_disconnect(unp); 50416054Skarels if (so->so_head) { 50516054Skarels so->so_pcb = (caddr_t) 0; 50625632Skarels m_freem(unp->unp_addr); 50716054Skarels (void) m_free(dtom(unp)); 50816054Skarels sofree(so); 50916054Skarels } 5109169Ssam } 5119169Ssam 51212760Ssam #ifdef notdef 5139169Ssam unp_drain() 5149169Ssam { 5159169Ssam 5169169Ssam } 51712760Ssam #endif 51812760Ssam 51912760Ssam unp_externalize(rights) 52012760Ssam struct mbuf *rights; 52112760Ssam { 52212760Ssam int newfds = rights->m_len / sizeof (int); 52312760Ssam register int i; 52412760Ssam register struct file **rp = mtod(rights, struct file **); 52512760Ssam register struct file *fp; 52612760Ssam int f; 52712760Ssam 52812760Ssam if (newfds > ufavail()) { 52912760Ssam for (i = 0; i < newfds; i++) { 53012760Ssam fp = *rp; 53112760Ssam unp_discard(fp); 53212760Ssam *rp++ = 0; 53312760Ssam } 53412760Ssam return (EMSGSIZE); 53512760Ssam } 53612760Ssam for (i = 0; i < newfds; i++) { 53712760Ssam f = ufalloc(0); 53812760Ssam if (f < 0) 53912760Ssam panic("unp_externalize"); 54012760Ssam fp = *rp; 54112760Ssam u.u_ofile[f] = fp; 54212760Ssam fp->f_msgcount--; 54325632Skarels unp_rights--; 54414927Smckusick *(int *)rp++ = f; 54512760Ssam } 54612760Ssam return (0); 54712760Ssam } 54812760Ssam 54912760Ssam unp_internalize(rights) 55012760Ssam struct mbuf *rights; 55112760Ssam { 55212760Ssam register struct file **rp; 55312760Ssam int oldfds = rights->m_len / sizeof (int); 55412760Ssam register int i; 55512760Ssam register struct file *fp; 55612760Ssam 55712760Ssam rp = mtod(rights, struct file **); 55813084Ssam for (i = 0; i < oldfds; i++) 55912760Ssam if (getf(*(int *)rp++) == 0) 56012760Ssam return (EBADF); 56112760Ssam rp = mtod(rights, struct file **); 56213084Ssam for (i = 0; i < oldfds; i++) { 56312760Ssam fp = getf(*(int *)rp); 56412760Ssam *rp++ = fp; 56512760Ssam fp->f_count++; 56612760Ssam fp->f_msgcount++; 56725632Skarels unp_rights++; 56812760Ssam } 56912760Ssam return (0); 57012760Ssam } 57112760Ssam 57212760Ssam int unp_defer, unp_gcing; 57312760Ssam int unp_mark(); 57416995Skarels extern struct domain unixdomain; 57512760Ssam 57612760Ssam unp_gc() 57712760Ssam { 57812760Ssam register struct file *fp; 57912760Ssam register struct socket *so; 58012760Ssam 58112760Ssam if (unp_gcing) 58212760Ssam return; 58312760Ssam unp_gcing = 1; 58412760Ssam restart: 58512760Ssam unp_defer = 0; 58612760Ssam for (fp = file; fp < fileNFILE; fp++) 58712760Ssam fp->f_flag &= ~(FMARK|FDEFER); 58812760Ssam do { 58912760Ssam for (fp = file; fp < fileNFILE; fp++) { 59012760Ssam if (fp->f_count == 0) 59112760Ssam continue; 59212760Ssam if (fp->f_flag & FDEFER) { 59312760Ssam fp->f_flag &= ~FDEFER; 59412760Ssam unp_defer--; 59512760Ssam } else { 59612760Ssam if (fp->f_flag & FMARK) 59712760Ssam continue; 59812760Ssam if (fp->f_count == fp->f_msgcount) 59912760Ssam continue; 60012760Ssam fp->f_flag |= FMARK; 60112760Ssam } 60212760Ssam if (fp->f_type != DTYPE_SOCKET) 60312760Ssam continue; 60412760Ssam so = (struct socket *)fp->f_data; 60516995Skarels if (so->so_proto->pr_domain != &unixdomain || 60621768Skarels (so->so_proto->pr_flags&PR_RIGHTS) == 0) 60712760Ssam continue; 60812760Ssam if (so->so_rcv.sb_flags & SB_LOCK) { 60912760Ssam sbwait(&so->so_rcv); 61012760Ssam goto restart; 61112760Ssam } 61212760Ssam unp_scan(so->so_rcv.sb_mb, unp_mark); 61312760Ssam } 61412760Ssam } while (unp_defer); 61512760Ssam for (fp = file; fp < fileNFILE; fp++) { 61612760Ssam if (fp->f_count == 0) 61712760Ssam continue; 61825632Skarels if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0) 61925632Skarels while (fp->f_msgcount) 62025632Skarels unp_discard(fp); 62112760Ssam } 62212760Ssam unp_gcing = 0; 62312760Ssam } 62412760Ssam 62516995Skarels unp_dispose(m) 62616995Skarels struct mbuf *m; 62716995Skarels { 62816995Skarels int unp_discard(); 62916995Skarels 63017020Skarels if (m) 63117020Skarels unp_scan(m, unp_discard); 63216995Skarels } 63316995Skarels 63416995Skarels unp_scan(m0, op) 63516995Skarels register struct mbuf *m0; 63612760Ssam int (*op)(); 63712760Ssam { 63816995Skarels register struct mbuf *m; 63912760Ssam register struct file **rp; 64012760Ssam register int i; 64117020Skarels int qfds; 64212760Ssam 64316995Skarels while (m0) { 64416995Skarels for (m = m0; m; m = m->m_next) 64516995Skarels if (m->m_type == MT_RIGHTS && m->m_len) { 64616995Skarels qfds = m->m_len / sizeof (struct file *); 64716995Skarels rp = mtod(m, struct file **); 64816995Skarels for (i = 0; i < qfds; i++) 64916995Skarels (*op)(*rp++); 65016995Skarels break; /* XXX, but saves time */ 65116995Skarels } 65217020Skarels m0 = m0->m_act; 65312760Ssam } 65412760Ssam } 65512760Ssam 65612760Ssam unp_mark(fp) 65712760Ssam struct file *fp; 65812760Ssam { 65912760Ssam 66012760Ssam if (fp->f_flag & FMARK) 66112760Ssam return; 66212760Ssam unp_defer++; 66312760Ssam fp->f_flag |= (FMARK|FDEFER); 66412760Ssam } 66512760Ssam 66612760Ssam unp_discard(fp) 66712760Ssam struct file *fp; 66812760Ssam { 66912760Ssam 67012760Ssam fp->f_msgcount--; 67125632Skarels unp_rights--; 67213084Ssam closef(fp); 67312760Ssam } 674