xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 17020)
1*17020Skarels /*	uipc_usrreq.c	6.9	84/08/27	*/
28925Sroot 
38925Sroot #include "../h/param.h"
48925Sroot #include "../h/dir.h"
58925Sroot #include "../h/user.h"
68925Sroot #include "../h/mbuf.h"
716995Skarels #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) {
281*17020Skarels 		unp->unp_inode->i_socket = 0;
2828925Sroot 		irele(unp->unp_inode);
2838925Sroot 		unp->unp_inode = 0;
2848925Sroot 	}
2858925Sroot 	if (unp->unp_conn)
2868925Sroot 		unp_disconnect(unp);
2878925Sroot 	while (unp->unp_refs)
2888925Sroot 		unp_drop(unp->unp_refs, ECONNRESET);
2898925Sroot 	soisdisconnected(unp->unp_socket);
2908925Sroot 	unp->unp_socket->so_pcb = 0;
2919169Ssam 	m_freem(unp->unp_remaddr);
2929169Ssam 	(void) m_free(dtom(unp));
2938925Sroot }
2948925Sroot 
2959169Ssam unp_bind(unp, nam)
2968925Sroot 	struct unpcb *unp;
2979169Ssam 	struct mbuf *nam;
2988925Sroot {
2999169Ssam 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3008925Sroot 	register struct inode *ip;
30116695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3028925Sroot 	int error;
3038925Sroot 
30416695Smckusick 	ndp->ni_dirp = soun->sun_path;
30512760Ssam 	if (nam->m_len == MLEN)
30612760Ssam 		return (EINVAL);
30712760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
30812760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
30916695Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
31016695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
31116695Smckusick 	ip = namei(ndp);
3128925Sroot 	if (ip) {
3138925Sroot 		iput(ip);
31410139Ssam 		return (EADDRINUSE);
3158925Sroot 	}
31611828Ssam 	if (error = u.u_error) {
31711828Ssam 		u.u_error = 0;			/* XXX */
31811828Ssam 		return (error);
31911828Ssam 	}
32016695Smckusick 	ip = maknode(IFSOCK | 0777, ndp);
3218925Sroot 	if (ip == NULL) {
3228925Sroot 		error = u.u_error;		/* XXX */
3238925Sroot 		u.u_error = 0;			/* XXX */
3248925Sroot 		return (error);
3258925Sroot 	}
3268925Sroot 	ip->i_socket = unp->unp_socket;
3278925Sroot 	unp->unp_inode = ip;
3288925Sroot 	iunlock(ip);			/* but keep reference */
3298925Sroot 	return (0);
3308925Sroot }
3318925Sroot 
3329169Ssam unp_connect(so, nam)
3338925Sroot 	struct socket *so;
3349169Ssam 	struct mbuf *nam;
3358925Sroot {
3369169Ssam 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3379169Ssam 	register struct inode *ip;
3388925Sroot 	int error;
33912760Ssam 	register struct socket *so2;
34016695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3418925Sroot 
34216695Smckusick 	ndp->ni_dirp = soun->sun_path;
34312760Ssam 	if (nam->m_len + (nam->m_off - MMINOFF) == MLEN)
34412760Ssam 		return (EMSGSIZE);
34512760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
34616695Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
34716695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
34816695Smckusick 	ip = namei(ndp);
3498925Sroot 	if (ip == 0) {
3508925Sroot 		error = u.u_error;
3518925Sroot 		u.u_error = 0;
35210139Ssam 		return (error);		/* XXX */
3538925Sroot 	}
3548925Sroot 	if ((ip->i_mode&IFMT) != IFSOCK) {
3558925Sroot 		error = ENOTSOCK;
3568925Sroot 		goto bad;
3578925Sroot 	}
3588925Sroot 	so2 = ip->i_socket;
3598925Sroot 	if (so2 == 0) {
3608925Sroot 		error = ECONNREFUSED;
3618925Sroot 		goto bad;
3628925Sroot 	}
36313115Ssam 	if (so->so_type != so2->so_type) {
36413115Ssam 		error = EPROTOTYPE;
36513115Ssam 		goto bad;
36613115Ssam 	}
36713115Ssam 	if (so->so_proto->pr_flags & PR_CONNREQUIRED &&
36813115Ssam 	    ((so2->so_options&SO_ACCEPTCONN) == 0 ||
36913115Ssam 	     (so2 = sonewconn(so2)) == 0)) {
37013115Ssam 		error = ECONNREFUSED;
37113115Ssam 		goto bad;
37213115Ssam 	}
37312760Ssam 	error = unp_connect2(so, nam, so2);
37412760Ssam bad:
37512760Ssam 	iput(ip);
37612760Ssam 	return (error);
37712760Ssam }
37812760Ssam 
37912760Ssam unp_connect2(so, sonam, so2)
38012760Ssam 	register struct socket *so;
38112760Ssam 	struct mbuf *sonam;
38212760Ssam 	register struct socket *so2;
38312760Ssam {
38412760Ssam 	register struct unpcb *unp = sotounpcb(so);
38512760Ssam 	register struct unpcb *unp2;
38612760Ssam 
38712760Ssam 	if (so2->so_type != so->so_type)
38812760Ssam 		return (EPROTOTYPE);
38914049Ssam 	unp2 = sotounpcb(so2);
39014049Ssam 	unp->unp_conn = unp2;
3918925Sroot 	switch (so->so_type) {
3928925Sroot 
3938925Sroot 	case SOCK_DGRAM:
3948925Sroot 		unp->unp_nextref = unp2->unp_refs;
3958925Sroot 		unp2->unp_refs = unp;
3968925Sroot 		break;
3978925Sroot 
3988925Sroot 	case SOCK_STREAM:
3999169Ssam 		unp2->unp_conn = unp;
40012760Ssam 		if (sonam)
40112760Ssam 			unp2->unp_remaddr = m_copy(sonam, 0, (int)M_COPYALL);
40214049Ssam 		soisconnected(so2);
40314049Ssam 		soisconnected(so);
4048925Sroot 		break;
4058925Sroot 
4068925Sroot 	default:
40712760Ssam 		panic("unp_connect2");
4088925Sroot 	}
4098925Sroot 	return (0);
4108925Sroot }
4119169Ssam 
4129169Ssam unp_disconnect(unp)
4139169Ssam 	struct unpcb *unp;
4149169Ssam {
4159169Ssam 	register struct unpcb *unp2 = unp->unp_conn;
4169169Ssam 
4179169Ssam 	if (unp2 == 0)
4189169Ssam 		return;
4199169Ssam 	unp->unp_conn = 0;
4209169Ssam 	switch (unp->unp_socket->so_type) {
4219169Ssam 
4229169Ssam 	case SOCK_DGRAM:
4239169Ssam 		if (unp2->unp_refs == unp)
4249169Ssam 			unp2->unp_refs = unp->unp_nextref;
4259169Ssam 		else {
4269169Ssam 			unp2 = unp2->unp_refs;
4279169Ssam 			for (;;) {
4289169Ssam 				if (unp2 == 0)
4299169Ssam 					panic("unp_disconnect");
4309169Ssam 				if (unp2->unp_nextref == unp)
4319169Ssam 					break;
4329169Ssam 				unp2 = unp2->unp_nextref;
4339169Ssam 			}
4349169Ssam 			unp2->unp_nextref = unp->unp_nextref;
4359169Ssam 		}
4369169Ssam 		unp->unp_nextref = 0;
4379169Ssam 		break;
4389169Ssam 
4399169Ssam 	case SOCK_STREAM:
44014049Ssam 		soisdisconnected(unp->unp_socket);
4419169Ssam 		unp2->unp_conn = 0;
4429169Ssam 		soisdisconnected(unp2->unp_socket);
4439169Ssam 		break;
4449169Ssam 	}
4459169Ssam }
4469169Ssam 
44712760Ssam #ifdef notdef
4489169Ssam unp_abort(unp)
4499169Ssam 	struct unpcb *unp;
4509169Ssam {
4519169Ssam 
4529169Ssam 	unp_detach(unp);
4539169Ssam }
45412760Ssam #endif
4559169Ssam 
4569169Ssam /*ARGSUSED*/
4579169Ssam unp_usrclosed(unp)
4589169Ssam 	struct unpcb *unp;
4599169Ssam {
4609169Ssam 
4619169Ssam }
4629169Ssam 
4639169Ssam unp_drop(unp, errno)
4649169Ssam 	struct unpcb *unp;
4659169Ssam 	int errno;
4669169Ssam {
46716054Skarels 	struct socket *so = unp->unp_socket;
4689169Ssam 
46916054Skarels 	so->so_error = errno;
4709169Ssam 	unp_disconnect(unp);
47116054Skarels 	if (so->so_head) {
47216054Skarels 		so->so_pcb = (caddr_t) 0;
47316431Skarels 		m_freem(unp->unp_remaddr);
47416054Skarels 		(void) m_free(dtom(unp));
47516054Skarels 		sofree(so);
47616054Skarels 	}
4779169Ssam }
4789169Ssam 
47912760Ssam #ifdef notdef
4809169Ssam unp_drain()
4819169Ssam {
4829169Ssam 
4839169Ssam }
48412760Ssam #endif
48512760Ssam 
48612760Ssam unp_externalize(rights)
48712760Ssam 	struct mbuf *rights;
48812760Ssam {
48912760Ssam 	int newfds = rights->m_len / sizeof (int);
49012760Ssam 	register int i;
49112760Ssam 	register struct file **rp = mtod(rights, struct file **);
49212760Ssam 	register struct file *fp;
49312760Ssam 	int f;
49412760Ssam 
49512760Ssam 	if (newfds > ufavail()) {
49612760Ssam 		for (i = 0; i < newfds; i++) {
49712760Ssam 			fp = *rp;
49812760Ssam 			unp_discard(fp);
49912760Ssam 			*rp++ = 0;
50012760Ssam 		}
50112760Ssam 		return (EMSGSIZE);
50212760Ssam 	}
50312760Ssam 	for (i = 0; i < newfds; i++) {
50412760Ssam 		f = ufalloc(0);
50512760Ssam 		if (f < 0)
50612760Ssam 			panic("unp_externalize");
50712760Ssam 		fp = *rp;
50812760Ssam 		u.u_ofile[f] = fp;
50912760Ssam 		fp->f_msgcount--;
51014927Smckusick 		*(int *)rp++ = f;
51112760Ssam 	}
51212760Ssam 	return (0);
51312760Ssam }
51412760Ssam 
51512760Ssam unp_internalize(rights)
51612760Ssam 	struct mbuf *rights;
51712760Ssam {
51812760Ssam 	register struct file **rp;
51912760Ssam 	int oldfds = rights->m_len / sizeof (int);
52012760Ssam 	register int i;
52112760Ssam 	register struct file *fp;
52212760Ssam 
52312760Ssam 	rp = mtod(rights, struct file **);
52413084Ssam 	for (i = 0; i < oldfds; i++)
52512760Ssam 		if (getf(*(int *)rp++) == 0)
52612760Ssam 			return (EBADF);
52712760Ssam 	rp = mtod(rights, struct file **);
52813084Ssam 	for (i = 0; i < oldfds; i++) {
52912760Ssam 		fp = getf(*(int *)rp);
53012760Ssam 		*rp++ = fp;
53112760Ssam 		fp->f_count++;
53212760Ssam 		fp->f_msgcount++;
53312760Ssam 	}
53412760Ssam 	return (0);
53512760Ssam }
53612760Ssam 
53712760Ssam int	unp_defer, unp_gcing;
53812760Ssam int	unp_mark();
53916995Skarels extern	struct domain unixdomain;
54012760Ssam 
54112760Ssam unp_gc()
54212760Ssam {
54312760Ssam 	register struct file *fp;
54412760Ssam 	register struct socket *so;
54512760Ssam 
54612760Ssam 	if (unp_gcing)
54712760Ssam 		return;
54812760Ssam 	unp_gcing = 1;
54912760Ssam restart:
55012760Ssam 	unp_defer = 0;
55112760Ssam 	for (fp = file; fp < fileNFILE; fp++)
55212760Ssam 		fp->f_flag &= ~(FMARK|FDEFER);
55312760Ssam 	do {
55412760Ssam 		for (fp = file; fp < fileNFILE; fp++) {
55512760Ssam 			if (fp->f_count == 0)
55612760Ssam 				continue;
55712760Ssam 			if (fp->f_flag & FDEFER) {
55812760Ssam 				fp->f_flag &= ~FDEFER;
55912760Ssam 				unp_defer--;
56012760Ssam 			} else {
56112760Ssam 				if (fp->f_flag & FMARK)
56212760Ssam 					continue;
56312760Ssam 				if (fp->f_count == fp->f_msgcount)
56412760Ssam 					continue;
56512760Ssam 				fp->f_flag |= FMARK;
56612760Ssam 			}
56712760Ssam 			if (fp->f_type != DTYPE_SOCKET)
56812760Ssam 				continue;
56912760Ssam 			so = (struct socket *)fp->f_data;
57016995Skarels 			if (so->so_proto->pr_domain != &unixdomain ||
57112760Ssam 			    (so->so_proto->pr_flags&PR_ADDR) == 0)
57212760Ssam 				continue;
57312760Ssam 			if (so->so_rcv.sb_flags & SB_LOCK) {
57412760Ssam 				sbwait(&so->so_rcv);
57512760Ssam 				goto restart;
57612760Ssam 			}
57712760Ssam 			unp_scan(so->so_rcv.sb_mb, unp_mark);
57812760Ssam 		}
57912760Ssam 	} while (unp_defer);
58012760Ssam 	for (fp = file; fp < fileNFILE; fp++) {
58112760Ssam 		if (fp->f_count == 0)
58212760Ssam 			continue;
58312760Ssam 		if (fp->f_count == fp->f_msgcount && (fp->f_flag&FMARK)==0) {
58412760Ssam 			if (fp->f_type != DTYPE_SOCKET)
58512760Ssam 				panic("unp_gc");
58612760Ssam 			(void) soshutdown((struct socket *)fp->f_data, 0);
58712760Ssam 		}
58812760Ssam 	}
58912760Ssam 	unp_gcing = 0;
59012760Ssam }
59112760Ssam 
59216995Skarels unp_dispose(m)
59316995Skarels 	struct mbuf *m;
59416995Skarels {
59516995Skarels 	int unp_discard();
59616995Skarels 
597*17020Skarels 	if (m)
598*17020Skarels 		unp_scan(m, unp_discard);
59916995Skarels }
60016995Skarels 
60116995Skarels unp_scan(m0, op)
60216995Skarels 	register struct mbuf *m0;
60312760Ssam 	int (*op)();
60412760Ssam {
60516995Skarels 	register struct mbuf *m;
60612760Ssam 	register struct file **rp;
60712760Ssam 	register int i;
608*17020Skarels 	int qfds;
60912760Ssam 
61016995Skarels 	while (m0) {
61116995Skarels 		for (m = m0; m; m = m->m_next)
61216995Skarels 			if (m->m_type == MT_RIGHTS && m->m_len) {
61316995Skarels 				qfds = m->m_len / sizeof (struct file *);
61416995Skarels 				rp = mtod(m, struct file **);
61516995Skarels 				for (i = 0; i < qfds; i++)
61616995Skarels 					(*op)(*rp++);
61716995Skarels 				break;		/* XXX, but saves time */
61816995Skarels 			}
619*17020Skarels 		m0 = m0->m_act;
62012760Ssam 	}
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