xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 16995)
1*16995Skarels /*	uipc_usrreq.c	6.8	84/08/21	*/
28925Sroot 
38925Sroot #include "../h/param.h"
48925Sroot #include "../h/dir.h"
58925Sroot #include "../h/user.h"
68925Sroot #include "../h/mbuf.h"
7*16995Skarels #include "../h/domain.h"
88925Sroot #include "../h/protosw.h"
98925Sroot #include "../h/socket.h"
108925Sroot #include "../h/socketvar.h"
118925Sroot #include "../h/unpcb.h"
128925Sroot #include "../h/un.h"
138925Sroot #include "../h/inode.h"
1412760Ssam #include "../h/file.h"
1516973Skarels #include "../h/stat.h"
168925Sroot 
178925Sroot /*
188925Sroot  * Unix communications domain.
1912760Ssam  *
2012760Ssam  * TODO:
2112760Ssam  *	SEQPACKET, RDM
2213119Ssam  *	rethink name space problems
2312760Ssam  *	need a proper out-of-band
248925Sroot  */
2513119Ssam struct	sockaddr sun_noname = { AF_UNIX };
268925Sroot 
278925Sroot /*ARGSUSED*/
2812760Ssam uipc_usrreq(so, req, m, nam, rights)
298925Sroot 	struct socket *so;
308925Sroot 	int req;
3112760Ssam 	struct mbuf *m, *nam, *rights;
328925Sroot {
338925Sroot 	struct unpcb *unp = sotounpcb(so);
348925Sroot 	register struct socket *so2;
358925Sroot 	int error = 0;
368925Sroot 
3712760Ssam 	if (req != PRU_SEND && rights && rights->m_len) {
3812760Ssam 		error = EOPNOTSUPP;
3912760Ssam 		goto release;
4012760Ssam 	}
4112760Ssam 	if (unp == 0 && req != PRU_ATTACH) {
4212760Ssam 		error = EINVAL;
4312760Ssam 		goto release;
4412760Ssam 	}
458925Sroot 	switch (req) {
468925Sroot 
478925Sroot 	case PRU_ATTACH:
488925Sroot 		if (unp) {
499169Ssam 			error = EISCONN;
508925Sroot 			break;
518925Sroot 		}
529028Sroot 		error = unp_attach(so);
538925Sroot 		break;
548925Sroot 
558925Sroot 	case PRU_DETACH:
568925Sroot 		unp_detach(unp);
578925Sroot 		break;
588925Sroot 
599169Ssam 	case PRU_BIND:
609169Ssam 		error = unp_bind(unp, nam);
619169Ssam 		break;
629169Ssam 
639169Ssam 	case PRU_LISTEN:
649169Ssam 		if (unp->unp_inode == 0)
659169Ssam 			error = EINVAL;
669169Ssam 		break;
679169Ssam 
688925Sroot 	case PRU_CONNECT:
699028Sroot 		error = unp_connect(so, nam);
708925Sroot 		break;
718925Sroot 
7212760Ssam 	case PRU_CONNECT2:
7313115Ssam 		error = unp_connect2(so, (struct mbuf *)0,
7413115Ssam 		    (struct socket *)nam);
7512760Ssam 		break;
7612760Ssam 
778925Sroot 	case PRU_DISCONNECT:
788925Sroot 		unp_disconnect(unp);
798925Sroot 		break;
808925Sroot 
819169Ssam 	case PRU_ACCEPT:
829169Ssam 		nam->m_len = unp->unp_remaddr->m_len;
839169Ssam 		bcopy(mtod(unp->unp_remaddr, caddr_t),
849169Ssam 		    mtod(nam, caddr_t), (unsigned)nam->m_len);
858925Sroot 		break;
868925Sroot 
878925Sroot 	case PRU_SHUTDOWN:
888925Sroot 		socantsendmore(so);
898925Sroot 		unp_usrclosed(unp);
908925Sroot 		break;
918925Sroot 
928925Sroot 	case PRU_RCVD:
938925Sroot 		switch (so->so_type) {
948925Sroot 
958925Sroot 		case SOCK_DGRAM:
968925Sroot 			panic("uipc 1");
9710139Ssam 			/*NOTREACHED*/
988925Sroot 
9910139Ssam 		case SOCK_STREAM:
1008925Sroot #define	rcv (&so->so_rcv)
1018925Sroot #define snd (&so2->so_snd)
1028925Sroot 			if (unp->unp_conn == 0)
1038925Sroot 				break;
1048925Sroot 			so2 = unp->unp_conn->unp_socket;
1058925Sroot 			/*
1068925Sroot 			 * Transfer resources back to send port
1078925Sroot 			 * and wakeup any waiting to write.
1088925Sroot 			 */
1098925Sroot 			snd->sb_mbmax += rcv->sb_mbmax - rcv->sb_mbcnt;
1108925Sroot 			rcv->sb_mbmax = rcv->sb_mbcnt;
1118925Sroot 			snd->sb_hiwat += rcv->sb_hiwat - rcv->sb_cc;
1128925Sroot 			rcv->sb_hiwat = rcv->sb_cc;
1138925Sroot 			sbwakeup(snd);
1148925Sroot #undef snd
1158925Sroot #undef rcv
1168925Sroot 			break;
1178925Sroot 
1188925Sroot 		default:
1198925Sroot 			panic("uipc 2");
1208925Sroot 		}
1218925Sroot 		break;
1228925Sroot 
1238925Sroot 	case PRU_SEND:
1248925Sroot 		switch (so->so_type) {
1258925Sroot 
1268925Sroot 		case SOCK_DGRAM:
1279028Sroot 			if (nam) {
1288925Sroot 				if (unp->unp_conn) {
1298925Sroot 					error = EISCONN;
1308925Sroot 					break;
1318925Sroot 				}
1329028Sroot 				error = unp_connect(so, nam);
1338925Sroot 				if (error)
1348925Sroot 					break;
1358925Sroot 			} else {
1368925Sroot 				if (unp->unp_conn == 0) {
1378925Sroot 					error = ENOTCONN;
1388925Sroot 					break;
1398925Sroot 				}
1408925Sroot 			}
1418925Sroot 			so2 = unp->unp_conn->unp_socket;
1429169Ssam 			/* BEGIN XXX */
14312760Ssam 			if (rights) {
14412760Ssam 				error = unp_internalize(rights);
14512760Ssam 				if (error)
14612760Ssam 					break;
14712760Ssam 			}
14812760Ssam 			if (sbspace(&so2->so_rcv) > 0) {
14913119Ssam 				/*
15013119Ssam 				 * There's no record of source socket's
15113119Ssam 				 * name, so send null name for the moment.
15213119Ssam 				 */
1539169Ssam 				(void) sbappendaddr(&so2->so_rcv,
15413119Ssam 				    &sun_noname, m, rights);
15512760Ssam 				sbwakeup(&so2->so_rcv);
15612760Ssam 				m = 0;
15712760Ssam 			}
1589169Ssam 			/* END XXX */
1599028Sroot 			if (nam)
1609169Ssam 				unp_disconnect(unp);
1618925Sroot 			break;
1628925Sroot 
1638925Sroot 		case SOCK_STREAM:
1648925Sroot #define	rcv (&so2->so_rcv)
1658925Sroot #define	snd (&so->so_snd)
16612760Ssam 			if (rights && rights->m_len) {
16712760Ssam 				error = EOPNOTSUPP;
16812760Ssam 				break;
16912760Ssam 			}
1708925Sroot 			if (unp->unp_conn == 0)
1718925Sroot 				panic("uipc 3");
1728925Sroot 			so2 = unp->unp_conn->unp_socket;
1738925Sroot 			/*
1748925Sroot 			 * Send to paired receive port, and then
1758925Sroot 			 * give it enough resources to hold what it already has.
1768925Sroot 			 * Wake up readers.
1778925Sroot 			 */
1788925Sroot 			sbappend(rcv, m);
1798925Sroot 			snd->sb_mbmax -= rcv->sb_mbcnt - rcv->sb_mbmax;
1808925Sroot 			rcv->sb_mbmax = rcv->sb_mbcnt;
1818925Sroot 			snd->sb_hiwat -= rcv->sb_cc - rcv->sb_hiwat;
1828925Sroot 			rcv->sb_hiwat = rcv->sb_cc;
1838925Sroot 			sbwakeup(rcv);
1848925Sroot #undef snd
1858925Sroot #undef rcv
1868925Sroot 			break;
1878925Sroot 
1888925Sroot 		default:
1898925Sroot 			panic("uipc 4");
1908925Sroot 		}
19112760Ssam 		m = 0;
1928925Sroot 		break;
1938925Sroot 
1948925Sroot 	case PRU_ABORT:
1958925Sroot 		unp_drop(unp, ECONNABORTED);
1968925Sroot 		break;
1978925Sroot 
1988925Sroot /* SOME AS YET UNIMPLEMENTED HOOKS */
1998925Sroot 	case PRU_CONTROL:
20013050Ssam 		return (EOPNOTSUPP);
2018925Sroot 
20216973Skarels /* END UNIMPLEMENTED HOOKS */
2038925Sroot 	case PRU_SENSE:
20416973Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
20516973Skarels 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
20616973Skarels 			so2 = unp->unp_conn->unp_socket;
20716973Skarels 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
20816973Skarels 		}
20916973Skarels 		return (0);
2108925Sroot 
2118925Sroot 	case PRU_RCVOOB:
21216774Sbloom 		return (EOPNOTSUPP);
2138925Sroot 
2148925Sroot 	case PRU_SENDOOB:
2158925Sroot 		break;
2168925Sroot 
2178925Sroot 	case PRU_SOCKADDR:
2188925Sroot 		break;
2198925Sroot 
22014121Ssam 	case PRU_PEERADDR:
22114121Ssam 		break;
22214121Ssam 
2238925Sroot 	case PRU_SLOWTIMO:
2248925Sroot 		break;
2258925Sroot 
2268925Sroot 	default:
2278925Sroot 		panic("piusrreq");
2288925Sroot 	}
22912760Ssam release:
23012760Ssam 	if (m)
23112760Ssam 		m_freem(m);
23211709Ssam 	return (error);
2338925Sroot }
2348925Sroot 
23516973Skarels /*
23616973Skarels  * We assign all buffering for stream sockets to the source,
23716973Skarels  * as that is where the flow control is implemented.
23816973Skarels  * Datagram sockets really use the sendspace as the maximum datagram size,
23916973Skarels  * and don't really want to reserve the sendspace.  Their recvspace should
24016973Skarels  * be large enough for at least one max-size datagram plus address.
24116973Skarels  */
24216973Skarels #define	PIPSIZ	4096
24316973Skarels int	unpst_sendspace = PIPSIZ;
24416973Skarels int	unpst_recvspace = 0;
24516973Skarels int	unpdg_sendspace = 2*1024;	/* really max datagram size */
24616973Skarels int	unpdg_recvspace = 4*1024;
2478925Sroot 
2489169Ssam unp_attach(so)
2498925Sroot 	struct socket *so;
2508925Sroot {
2519169Ssam 	register struct mbuf *m;
2528925Sroot 	register struct unpcb *unp;
2538925Sroot 	int error;
2548925Sroot 
25516973Skarels 	switch (so->so_type) {
25616973Skarels 
25716973Skarels 	case SOCK_STREAM:
25816973Skarels 		error = soreserve(so, unpst_sendspace, unpst_recvspace);
25916973Skarels 		break;
26016973Skarels 
26116973Skarels 	case SOCK_DGRAM:
26216973Skarels 		error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
26316973Skarels 		break;
26416973Skarels 	}
2658925Sroot 	if (error)
26610139Ssam 		return (error);
2679637Ssam 	m = m_getclr(M_DONTWAIT, MT_PCB);
26810139Ssam 	if (m == NULL)
26910139Ssam 		return (ENOBUFS);
2708925Sroot 	unp = mtod(m, struct unpcb *);
2718925Sroot 	so->so_pcb = (caddr_t)unp;
2728925Sroot 	unp->unp_socket = so;
2738925Sroot 	return (0);
2748925Sroot }
2758925Sroot 
2768925Sroot unp_detach(unp)
2779169Ssam 	register struct unpcb *unp;
2788925Sroot {
2798925Sroot 
2808925Sroot 	if (unp->unp_inode) {
2818925Sroot 		irele(unp->unp_inode);
2828925Sroot 		unp->unp_inode = 0;
2838925Sroot 	}
2848925Sroot 	if (unp->unp_conn)
2858925Sroot 		unp_disconnect(unp);
2868925Sroot 	while (unp->unp_refs)
2878925Sroot 		unp_drop(unp->unp_refs, ECONNRESET);
2888925Sroot 	soisdisconnected(unp->unp_socket);
2898925Sroot 	unp->unp_socket->so_pcb = 0;
2909169Ssam 	m_freem(unp->unp_remaddr);
2919169Ssam 	(void) m_free(dtom(unp));
2928925Sroot }
2938925Sroot 
2949169Ssam unp_bind(unp, nam)
2958925Sroot 	struct unpcb *unp;
2969169Ssam 	struct mbuf *nam;
2978925Sroot {
2989169Ssam 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
2998925Sroot 	register struct inode *ip;
30016695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3018925Sroot 	int error;
3028925Sroot 
30316695Smckusick 	ndp->ni_dirp = soun->sun_path;
30412760Ssam 	if (nam->m_len == MLEN)
30512760Ssam 		return (EINVAL);
30612760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
30712760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
30816695Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
30916695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
31016695Smckusick 	ip = namei(ndp);
3118925Sroot 	if (ip) {
3128925Sroot 		iput(ip);
31310139Ssam 		return (EADDRINUSE);
3148925Sroot 	}
31511828Ssam 	if (error = u.u_error) {
31611828Ssam 		u.u_error = 0;			/* XXX */
31711828Ssam 		return (error);
31811828Ssam 	}
31916695Smckusick 	ip = maknode(IFSOCK | 0777, ndp);
3208925Sroot 	if (ip == NULL) {
3218925Sroot 		error = u.u_error;		/* XXX */
3228925Sroot 		u.u_error = 0;			/* XXX */
3238925Sroot 		return (error);
3248925Sroot 	}
3258925Sroot 	ip->i_socket = unp->unp_socket;
3268925Sroot 	unp->unp_inode = ip;
3278925Sroot 	iunlock(ip);			/* but keep reference */
3288925Sroot 	return (0);
3298925Sroot }
3308925Sroot 
3319169Ssam unp_connect(so, nam)
3328925Sroot 	struct socket *so;
3339169Ssam 	struct mbuf *nam;
3348925Sroot {
3359169Ssam 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3369169Ssam 	register struct inode *ip;
3378925Sroot 	int error;
33812760Ssam 	register struct socket *so2;
33916695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3408925Sroot 
34116695Smckusick 	ndp->ni_dirp = soun->sun_path;
34212760Ssam 	if (nam->m_len + (nam->m_off - MMINOFF) == MLEN)
34312760Ssam 		return (EMSGSIZE);
34412760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
34516695Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
34616695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
34716695Smckusick 	ip = namei(ndp);
3488925Sroot 	if (ip == 0) {
3498925Sroot 		error = u.u_error;
3508925Sroot 		u.u_error = 0;
35110139Ssam 		return (error);		/* XXX */
3528925Sroot 	}
3538925Sroot 	if ((ip->i_mode&IFMT) != IFSOCK) {
3548925Sroot 		error = ENOTSOCK;
3558925Sroot 		goto bad;
3568925Sroot 	}
3578925Sroot 	so2 = ip->i_socket;
3588925Sroot 	if (so2 == 0) {
3598925Sroot 		error = ECONNREFUSED;
3608925Sroot 		goto bad;
3618925Sroot 	}
36213115Ssam 	if (so->so_type != so2->so_type) {
36313115Ssam 		error = EPROTOTYPE;
36413115Ssam 		goto bad;
36513115Ssam 	}
36613115Ssam 	if (so->so_proto->pr_flags & PR_CONNREQUIRED &&
36713115Ssam 	    ((so2->so_options&SO_ACCEPTCONN) == 0 ||
36813115Ssam 	     (so2 = sonewconn(so2)) == 0)) {
36913115Ssam 		error = ECONNREFUSED;
37013115Ssam 		goto bad;
37113115Ssam 	}
37212760Ssam 	error = unp_connect2(so, nam, so2);
37312760Ssam bad:
37412760Ssam 	iput(ip);
37512760Ssam 	return (error);
37612760Ssam }
37712760Ssam 
37812760Ssam unp_connect2(so, sonam, so2)
37912760Ssam 	register struct socket *so;
38012760Ssam 	struct mbuf *sonam;
38112760Ssam 	register struct socket *so2;
38212760Ssam {
38312760Ssam 	register struct unpcb *unp = sotounpcb(so);
38412760Ssam 	register struct unpcb *unp2;
38512760Ssam 
38612760Ssam 	if (so2->so_type != so->so_type)
38712760Ssam 		return (EPROTOTYPE);
38814049Ssam 	unp2 = sotounpcb(so2);
38914049Ssam 	unp->unp_conn = unp2;
3908925Sroot 	switch (so->so_type) {
3918925Sroot 
3928925Sroot 	case SOCK_DGRAM:
3938925Sroot 		unp->unp_nextref = unp2->unp_refs;
3948925Sroot 		unp2->unp_refs = unp;
3958925Sroot 		break;
3968925Sroot 
3978925Sroot 	case SOCK_STREAM:
3989169Ssam 		unp2->unp_conn = unp;
39912760Ssam 		if (sonam)
40012760Ssam 			unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL);
40114049Ssam 		soisconnected(so2);
40214049Ssam 		soisconnected(so);
4038925Sroot 		break;
4048925Sroot 
4058925Sroot 	default:
40612760Ssam 		panic("unp_connect2");
4078925Sroot 	}
4088925Sroot 	return (0);
4098925Sroot }
4109169Ssam 
4119169Ssam unp_disconnect(unp)
4129169Ssam 	struct unpcb *unp;
4139169Ssam {
4149169Ssam 	register struct unpcb *unp2 = unp->unp_conn;
4159169Ssam 
4169169Ssam 	if (unp2 == 0)
4179169Ssam 		return;
4189169Ssam 	unp->unp_conn = 0;
4199169Ssam 	switch (unp->unp_socket->so_type) {
4209169Ssam 
4219169Ssam 	case SOCK_DGRAM:
4229169Ssam 		if (unp2->unp_refs == unp)
4239169Ssam 			unp2->unp_refs = unp->unp_nextref;
4249169Ssam 		else {
4259169Ssam 			unp2 = unp2->unp_refs;
4269169Ssam 			for (;;) {
4279169Ssam 				if (unp2 == 0)
4289169Ssam 					panic("unp_disconnect");
4299169Ssam 				if (unp2->unp_nextref == unp)
4309169Ssam 					break;
4319169Ssam 				unp2 = unp2->unp_nextref;
4329169Ssam 			}
4339169Ssam 			unp2->unp_nextref = unp->unp_nextref;
4349169Ssam 		}
4359169Ssam 		unp->unp_nextref = 0;
4369169Ssam 		break;
4379169Ssam 
4389169Ssam 	case SOCK_STREAM:
43914049Ssam 		soisdisconnected(unp->unp_socket);
4409169Ssam 		unp2->unp_conn = 0;
4419169Ssam 		soisdisconnected(unp2->unp_socket);
4429169Ssam 		break;
4439169Ssam 	}
4449169Ssam }
4459169Ssam 
44612760Ssam #ifdef notdef
4479169Ssam unp_abort(unp)
4489169Ssam 	struct unpcb *unp;
4499169Ssam {
4509169Ssam 
4519169Ssam 	unp_detach(unp);
4529169Ssam }
45312760Ssam #endif
4549169Ssam 
4559169Ssam /*ARGSUSED*/
4569169Ssam unp_usrclosed(unp)
4579169Ssam 	struct unpcb *unp;
4589169Ssam {
4599169Ssam 
4609169Ssam }
4619169Ssam 
4629169Ssam unp_drop(unp, errno)
4639169Ssam 	struct unpcb *unp;
4649169Ssam 	int errno;
4659169Ssam {
46616054Skarels 	struct socket *so = unp->unp_socket;
4679169Ssam 
46816054Skarels 	so->so_error = errno;
4699169Ssam 	unp_disconnect(unp);
47016054Skarels 	if (so->so_head) {
47116054Skarels 		so->so_pcb = (caddr_t) 0;
47216431Skarels 		m_freem(unp->unp_remaddr);
47316054Skarels 		(void) m_free(dtom(unp));
47416054Skarels 		sofree(so);
47516054Skarels 	}
4769169Ssam }
4779169Ssam 
47812760Ssam #ifdef notdef
4799169Ssam unp_drain()
4809169Ssam {
4819169Ssam 
4829169Ssam }
48312760Ssam #endif
48412760Ssam 
48512760Ssam unp_externalize(rights)
48612760Ssam 	struct mbuf *rights;
48712760Ssam {
48812760Ssam 	int newfds = rights->m_len / sizeof (int);
48912760Ssam 	register int i;
49012760Ssam 	register struct file **rp = mtod(rights, struct file **);
49112760Ssam 	register struct file *fp;
49212760Ssam 	int f;
49312760Ssam 
49412760Ssam 	if (newfds > ufavail()) {
49512760Ssam 		for (i = 0; i < newfds; i++) {
49612760Ssam 			fp = *rp;
49712760Ssam 			unp_discard(fp);
49812760Ssam 			*rp++ = 0;
49912760Ssam 		}
50012760Ssam 		return (EMSGSIZE);
50112760Ssam 	}
50212760Ssam 	for (i = 0; i < newfds; i++) {
50312760Ssam 		f = ufalloc(0);
50412760Ssam 		if (f < 0)
50512760Ssam 			panic("unp_externalize");
50612760Ssam 		fp = *rp;
50712760Ssam 		u.u_ofile[f] = fp;
50812760Ssam 		fp->f_msgcount--;
50914927Smckusick 		*(int *)rp++ = f;
51012760Ssam 	}
51112760Ssam 	return (0);
51212760Ssam }
51312760Ssam 
51412760Ssam unp_internalize(rights)
51512760Ssam 	struct mbuf *rights;
51612760Ssam {
51712760Ssam 	register struct file **rp;
51812760Ssam 	int oldfds = rights->m_len / sizeof (int);
51912760Ssam 	register int i;
52012760Ssam 	register struct file *fp;
52112760Ssam 
52212760Ssam 	rp = mtod(rights, struct file **);
52313084Ssam 	for (i = 0; i < oldfds; i++)
52412760Ssam 		if (getf(*(int *)rp++) == 0)
52512760Ssam 			return (EBADF);
52612760Ssam 	rp = mtod(rights, struct file **);
52713084Ssam 	for (i = 0; i < oldfds; i++) {
52812760Ssam 		fp = getf(*(int *)rp);
52912760Ssam 		*rp++ = fp;
53012760Ssam 		fp->f_count++;
53112760Ssam 		fp->f_msgcount++;
53212760Ssam 	}
53312760Ssam 	return (0);
53412760Ssam }
53512760Ssam 
53612760Ssam int	unp_defer, unp_gcing;
53712760Ssam int	unp_mark();
538*16995Skarels extern	struct domain unixdomain;
53912760Ssam 
54012760Ssam unp_gc()
54112760Ssam {
54212760Ssam 	register struct file *fp;
54312760Ssam 	register struct socket *so;
54412760Ssam 
54512760Ssam 	if (unp_gcing)
54612760Ssam 		return;
54712760Ssam 	unp_gcing = 1;
54812760Ssam restart:
54912760Ssam 	unp_defer = 0;
55012760Ssam 	for (fp = file; fp < fileNFILE; fp++)
55112760Ssam 		fp->f_flag &= ~(FMARK|FDEFER);
55212760Ssam 	do {
55312760Ssam 		for (fp = file; fp < fileNFILE; fp++) {
55412760Ssam 			if (fp->f_count == 0)
55512760Ssam 				continue;
55612760Ssam 			if (fp->f_flag & FDEFER) {
55712760Ssam 				fp->f_flag &= ~FDEFER;
55812760Ssam 				unp_defer--;
55912760Ssam 			} else {
56012760Ssam 				if (fp->f_flag & FMARK)
56112760Ssam 					continue;
56212760Ssam 				if (fp->f_count == fp->f_msgcount)
56312760Ssam 					continue;
56412760Ssam 				fp->f_flag |= FMARK;
56512760Ssam 			}
56612760Ssam 			if (fp->f_type != DTYPE_SOCKET)
56712760Ssam 				continue;
56812760Ssam 			so = (struct socket *)fp->f_data;
569*16995Skarels 			if (so->so_proto->pr_domain != &unixdomain ||
57012760Ssam 			    (so->so_proto->pr_flags&PR_ADDR) == 0)
57112760Ssam 				continue;
57212760Ssam 			if (so->so_rcv.sb_flags & SB_LOCK) {
57312760Ssam 				sbwait(&so->so_rcv);
57412760Ssam 				goto restart;
57512760Ssam 			}
57612760Ssam 			unp_scan(so->so_rcv.sb_mb, unp_mark);
57712760Ssam 		}
57812760Ssam 	} while (unp_defer);
57912760Ssam 	for (fp = file; fp < fileNFILE; fp++) {
58012760Ssam 		if (fp->f_count == 0)
58112760Ssam 			continue;
58212760Ssam 		if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) {
58312760Ssam 			if (fp->f_type != DTYPE_SOCKET)
58412760Ssam 				panic("unp_gc");
58512760Ssam 			(void) soshutdown((struct socket *)fp->f_data, 0);
58612760Ssam 		}
58712760Ssam 	}
58812760Ssam 	unp_gcing = 0;
58912760Ssam }
59012760Ssam 
591*16995Skarels unp_dispose(m)
592*16995Skarels 	struct mbuf *m;
593*16995Skarels {
594*16995Skarels 	int unp_discard();
595*16995Skarels 
596*16995Skarels 	unp_scan(m, unp_discard);
597*16995Skarels }
598*16995Skarels 
599*16995Skarels unp_scan(m0, op)
600*16995Skarels 	register struct mbuf *m0;
60112760Ssam 	int (*op)();
60212760Ssam {
603*16995Skarels 	register struct mbuf *m;
60412760Ssam 	register struct file **rp;
60512760Ssam 	register int i;
606*16995Skarels 	int qfds = 0;
60712760Ssam 
608*16995Skarels 	while (m0) {
609*16995Skarels 		for (m = m0; m; m = m->m_next)
610*16995Skarels 			if (m->m_type == MT_RIGHTS && m->m_len) {
611*16995Skarels 				qfds = m->m_len / sizeof (struct file *);
612*16995Skarels 				rp = mtod(m, struct file **);
613*16995Skarels 				for (i = 0; i < qfds; i++)
614*16995Skarels 					(*op)(*rp++);
615*16995Skarels 				break;		/* XXX, but saves time */
616*16995Skarels 			}
617*16995Skarels 		m0 = m0->m_next;
61812760Ssam 	}
619*16995Skarels 	if (qfds == 0)
620*16995Skarels 		panic("unp_gcscan");
62112760Ssam }
62212760Ssam 
62312760Ssam unp_mark(fp)
62412760Ssam 	struct file *fp;
62512760Ssam {
62612760Ssam 
62712760Ssam 	if (fp->f_flag & FMARK)
62812760Ssam 		return;
62912760Ssam 	unp_defer++;
63012760Ssam 	fp->f_flag |= (FMARK|FDEFER);
63112760Ssam }
63212760Ssam 
63312760Ssam unp_discard(fp)
63412760Ssam 	struct file *fp;
63512760Ssam {
63612760Ssam 
63712760Ssam 	fp->f_msgcount--;
63813084Ssam 	closef(fp);
63912760Ssam }
640