xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 25632)
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*25632Skarels  *	@(#)uipc_usrreq.c	6.18 (Berkeley) 12/19/85
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 };
32*25632Skarels 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:
8213115Ssam 		error = unp_connect2(so, (struct mbuf *)0,
8313115Ssam 		    (struct socket *)nam);
8412760Ssam 		break;
8512760Ssam 
868925Sroot 	case PRU_DISCONNECT:
878925Sroot 		unp_disconnect(unp);
888925Sroot 		break;
898925Sroot 
909169Ssam 	case PRU_ACCEPT:
91*25632Skarels 		if (unp->unp_conn->unp_addr) {
92*25632Skarels 			nam->m_len = unp->unp_conn->unp_addr->m_len;
93*25632Skarels 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
94*25632Skarels 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
95*25632Skarels 		} else {
96*25632Skarels 			nam->m_len = sizeof(sun_noname);
97*25632Skarels 			*(mtod(nam, struct sockaddr *)) = sun_noname;
98*25632Skarels 		}
998925Sroot 		break;
1008925Sroot 
1018925Sroot 	case PRU_SHUTDOWN:
1028925Sroot 		socantsendmore(so);
1038925Sroot 		unp_usrclosed(unp);
1048925Sroot 		break;
1058925Sroot 
1068925Sroot 	case PRU_RCVD:
1078925Sroot 		switch (so->so_type) {
1088925Sroot 
1098925Sroot 		case SOCK_DGRAM:
1108925Sroot 			panic("uipc 1");
11110139Ssam 			/*NOTREACHED*/
1128925Sroot 
11310139Ssam 		case SOCK_STREAM:
1148925Sroot #define	rcv (&so->so_rcv)
1158925Sroot #define snd (&so2->so_snd)
1168925Sroot 			if (unp->unp_conn == 0)
1178925Sroot 				break;
1188925Sroot 			so2 = unp->unp_conn->unp_socket;
1198925Sroot 			/*
120*25632Skarels 			 * Adjust backpressure on sender
1218925Sroot 			 * and wakeup any waiting to write.
1228925Sroot 			 */
123*25632Skarels 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
124*25632Skarels 			unp->unp_mbcnt = rcv->sb_mbcnt;
125*25632Skarels 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
126*25632Skarels 			unp->unp_cc = rcv->sb_cc;
12717543Skarels 			sowwakeup(so2);
1288925Sroot #undef snd
1298925Sroot #undef rcv
1308925Sroot 			break;
1318925Sroot 
1328925Sroot 		default:
1338925Sroot 			panic("uipc 2");
1348925Sroot 		}
1358925Sroot 		break;
1368925Sroot 
1378925Sroot 	case PRU_SEND:
138*25632Skarels 		if (rights) {
139*25632Skarels 			error = unp_internalize(rights);
140*25632Skarels 			if (error)
141*25632Skarels 				break;
142*25632Skarels 		}
1438925Sroot 		switch (so->so_type) {
1448925Sroot 
145*25632Skarels 		case SOCK_DGRAM: {
146*25632Skarels 			struct sockaddr *from;
147*25632Skarels 
1489028Sroot 			if (nam) {
1498925Sroot 				if (unp->unp_conn) {
1508925Sroot 					error = EISCONN;
1518925Sroot 					break;
1528925Sroot 				}
1539028Sroot 				error = unp_connect(so, nam);
1548925Sroot 				if (error)
1558925Sroot 					break;
1568925Sroot 			} else {
1578925Sroot 				if (unp->unp_conn == 0) {
1588925Sroot 					error = ENOTCONN;
1598925Sroot 					break;
1608925Sroot 				}
1618925Sroot 			}
1628925Sroot 			so2 = unp->unp_conn->unp_socket;
163*25632Skarels 			if (unp->unp_addr)
164*25632Skarels 				from = mtod(unp->unp_addr, struct sockaddr *);
165*25632Skarels 			else
166*25632Skarels 				from = &sun_noname;
167*25632Skarels 			if (sbspace(&so2->so_rcv) > 0 &&
168*25632Skarels 			    sbappendaddr(&so2->so_rcv, from, m, rights)) {
169*25632Skarels 				sorwakeup(so2);
170*25632Skarels 				m = 0;
171*25632Skarels 			} else
172*25632Skarels 				error = ENOBUFS;
1739028Sroot 			if (nam)
1749169Ssam 				unp_disconnect(unp);
1758925Sroot 			break;
176*25632Skarels 		}
1778925Sroot 
1788925Sroot 		case SOCK_STREAM:
1798925Sroot #define	rcv (&so2->so_rcv)
1808925Sroot #define	snd (&so->so_snd)
18123524Skarels 			if (so->so_state & SS_CANTSENDMORE) {
18223524Skarels 				error = EPIPE;
18323524Skarels 				break;
18423524Skarels 			}
1858925Sroot 			if (unp->unp_conn == 0)
1868925Sroot 				panic("uipc 3");
1878925Sroot 			so2 = unp->unp_conn->unp_socket;
1888925Sroot 			/*
189*25632Skarels 			 * Send to paired receive port, and then reduce
190*25632Skarels 			 * send buffer hiwater marks to maintain backpressure.
1918925Sroot 			 * Wake up readers.
1928925Sroot 			 */
193*25632Skarels 			if (rights)
194*25632Skarels 				sbappendrights(rcv, m, rights);
195*25632Skarels 			else
196*25632Skarels 				sbappend(rcv, m);
197*25632Skarels 			snd->sb_mbmax -=
198*25632Skarels 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
199*25632Skarels 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
200*25632Skarels 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
201*25632Skarels 			unp->unp_conn->unp_cc = rcv->sb_cc;
20217543Skarels 			sorwakeup(so2);
20317543Skarels 			m = 0;
2048925Sroot #undef snd
2058925Sroot #undef rcv
2068925Sroot 			break;
2078925Sroot 
2088925Sroot 		default:
2098925Sroot 			panic("uipc 4");
2108925Sroot 		}
2118925Sroot 		break;
2128925Sroot 
2138925Sroot 	case PRU_ABORT:
2148925Sroot 		unp_drop(unp, ECONNABORTED);
2158925Sroot 		break;
2168925Sroot 
2178925Sroot 	case PRU_SENSE:
21816973Skarels 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
21916973Skarels 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
22016973Skarels 			so2 = unp->unp_conn->unp_socket;
22116973Skarels 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
22216973Skarels 		}
22321110Skarels 		((struct stat *) m)->st_dev = NODEV;
224*25632Skarels 		if (unp->unp_ino == 0)
225*25632Skarels 			unp->unp_ino = unp_ino++;
226*25632Skarels 		((struct stat *) m)->st_ino = unp->unp_ino;
22716973Skarels 		return (0);
2288925Sroot 
2298925Sroot 	case PRU_RCVOOB:
23016774Sbloom 		return (EOPNOTSUPP);
2318925Sroot 
2328925Sroot 	case PRU_SENDOOB:
23317543Skarels 		error = EOPNOTSUPP;
2348925Sroot 		break;
2358925Sroot 
2368925Sroot 	case PRU_SOCKADDR:
2378925Sroot 		break;
2388925Sroot 
23914121Ssam 	case PRU_PEERADDR:
24014121Ssam 		break;
24114121Ssam 
2428925Sroot 	case PRU_SLOWTIMO:
2438925Sroot 		break;
2448925Sroot 
2458925Sroot 	default:
2468925Sroot 		panic("piusrreq");
2478925Sroot 	}
24812760Ssam release:
24912760Ssam 	if (m)
25012760Ssam 		m_freem(m);
25111709Ssam 	return (error);
2528925Sroot }
2538925Sroot 
25416973Skarels /*
255*25632Skarels  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
256*25632Skarels  * for stream sockets, although the total for sender and receiver is
257*25632Skarels  * actually only PIPSIZ.
25816973Skarels  * Datagram sockets really use the sendspace as the maximum datagram size,
25916973Skarels  * and don't really want to reserve the sendspace.  Their recvspace should
26016973Skarels  * be large enough for at least one max-size datagram plus address.
26116973Skarels  */
26216973Skarels #define	PIPSIZ	4096
26316973Skarels int	unpst_sendspace = PIPSIZ;
264*25632Skarels int	unpst_recvspace = PIPSIZ;
26516973Skarels int	unpdg_sendspace = 2*1024;	/* really max datagram size */
26616973Skarels int	unpdg_recvspace = 4*1024;
2678925Sroot 
268*25632Skarels int	unp_rights;			/* file descriptors in flight */
269*25632Skarels 
2709169Ssam unp_attach(so)
2718925Sroot 	struct socket *so;
2728925Sroot {
2739169Ssam 	register struct mbuf *m;
2748925Sroot 	register struct unpcb *unp;
2758925Sroot 	int error;
2768925Sroot 
27716973Skarels 	switch (so->so_type) {
27816973Skarels 
27916973Skarels 	case SOCK_STREAM:
28016973Skarels 		error = soreserve(so, unpst_sendspace, unpst_recvspace);
28116973Skarels 		break;
28216973Skarels 
28316973Skarels 	case SOCK_DGRAM:
28416973Skarels 		error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
28516973Skarels 		break;
28616973Skarels 	}
2878925Sroot 	if (error)
28810139Ssam 		return (error);
2899637Ssam 	m = m_getclr(M_DONTWAIT, MT_PCB);
29010139Ssam 	if (m == NULL)
29110139Ssam 		return (ENOBUFS);
2928925Sroot 	unp = mtod(m, struct unpcb *);
2938925Sroot 	so->so_pcb = (caddr_t)unp;
2948925Sroot 	unp->unp_socket = so;
2958925Sroot 	return (0);
2968925Sroot }
2978925Sroot 
2988925Sroot unp_detach(unp)
2999169Ssam 	register struct unpcb *unp;
3008925Sroot {
3018925Sroot 
3028925Sroot 	if (unp->unp_inode) {
30317020Skarels 		unp->unp_inode->i_socket = 0;
3048925Sroot 		irele(unp->unp_inode);
3058925Sroot 		unp->unp_inode = 0;
3068925Sroot 	}
3078925Sroot 	if (unp->unp_conn)
3088925Sroot 		unp_disconnect(unp);
3098925Sroot 	while (unp->unp_refs)
3108925Sroot 		unp_drop(unp->unp_refs, ECONNRESET);
3118925Sroot 	soisdisconnected(unp->unp_socket);
3128925Sroot 	unp->unp_socket->so_pcb = 0;
313*25632Skarels 	m_freem(unp->unp_addr);
3149169Ssam 	(void) m_free(dtom(unp));
315*25632Skarels 	if (unp_rights)
316*25632Skarels 		unp_gc();
3178925Sroot }
3188925Sroot 
3199169Ssam unp_bind(unp, nam)
3208925Sroot 	struct unpcb *unp;
3219169Ssam 	struct mbuf *nam;
3228925Sroot {
3239169Ssam 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3248925Sroot 	register struct inode *ip;
32516695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3268925Sroot 	int error;
3278925Sroot 
32816695Smckusick 	ndp->ni_dirp = soun->sun_path;
32925232Smckusick 	if (unp->unp_inode != NULL || nam->m_len == MLEN)
33012760Ssam 		return (EINVAL);
33112760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
33212760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
33316695Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
33416695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
33516695Smckusick 	ip = namei(ndp);
3368925Sroot 	if (ip) {
3378925Sroot 		iput(ip);
33810139Ssam 		return (EADDRINUSE);
3398925Sroot 	}
34011828Ssam 	if (error = u.u_error) {
34111828Ssam 		u.u_error = 0;			/* XXX */
34211828Ssam 		return (error);
34311828Ssam 	}
34416695Smckusick 	ip = maknode(IFSOCK | 0777, ndp);
3458925Sroot 	if (ip == NULL) {
3468925Sroot 		error = u.u_error;		/* XXX */
3478925Sroot 		u.u_error = 0;			/* XXX */
3488925Sroot 		return (error);
3498925Sroot 	}
3508925Sroot 	ip->i_socket = unp->unp_socket;
3518925Sroot 	unp->unp_inode = ip;
352*25632Skarels 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
3538925Sroot 	iunlock(ip);			/* but keep reference */
3548925Sroot 	return (0);
3558925Sroot }
3568925Sroot 
3579169Ssam unp_connect(so, nam)
3588925Sroot 	struct socket *so;
3599169Ssam 	struct mbuf *nam;
3608925Sroot {
3619169Ssam 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3629169Ssam 	register struct inode *ip;
3638925Sroot 	int error;
36412760Ssam 	register struct socket *so2;
36516695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3668925Sroot 
36716695Smckusick 	ndp->ni_dirp = soun->sun_path;
36812760Ssam 	if (nam->m_len + (nam->m_off - MMINOFF) == MLEN)
36912760Ssam 		return (EMSGSIZE);
37012760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
37116695Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
37216695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
37316695Smckusick 	ip = namei(ndp);
3748925Sroot 	if (ip == 0) {
3758925Sroot 		error = u.u_error;
3768925Sroot 		u.u_error = 0;
37710139Ssam 		return (error);		/* XXX */
3788925Sroot 	}
37917543Skarels 	if (access(ip, IWRITE)) {
38017543Skarels 		error = u.u_error;
38117543Skarels 		u.u_error = 0; 		/* XXX */
38217543Skarels 		goto bad;
38317543Skarels 	}
3848925Sroot 	if ((ip->i_mode&IFMT) != IFSOCK) {
3858925Sroot 		error = ENOTSOCK;
3868925Sroot 		goto bad;
3878925Sroot 	}
3888925Sroot 	so2 = ip->i_socket;
3898925Sroot 	if (so2 == 0) {
3908925Sroot 		error = ECONNREFUSED;
3918925Sroot 		goto bad;
3928925Sroot 	}
39313115Ssam 	if (so->so_type != so2->so_type) {
39413115Ssam 		error = EPROTOTYPE;
39513115Ssam 		goto bad;
39613115Ssam 	}
39713115Ssam 	if (so->so_proto->pr_flags & PR_CONNREQUIRED &&
39813115Ssam 	    ((so2->so_options&SO_ACCEPTCONN) == 0 ||
39913115Ssam 	     (so2 = sonewconn(so2)) == 0)) {
40013115Ssam 		error = ECONNREFUSED;
40113115Ssam 		goto bad;
40213115Ssam 	}
40312760Ssam 	error = unp_connect2(so, nam, so2);
40412760Ssam bad:
40512760Ssam 	iput(ip);
40612760Ssam 	return (error);
40712760Ssam }
40812760Ssam 
40912760Ssam unp_connect2(so, sonam, so2)
41012760Ssam 	register struct socket *so;
41112760Ssam 	struct mbuf *sonam;
41212760Ssam 	register struct socket *so2;
41312760Ssam {
41412760Ssam 	register struct unpcb *unp = sotounpcb(so);
41512760Ssam 	register struct unpcb *unp2;
41612760Ssam 
41712760Ssam 	if (so2->so_type != so->so_type)
41812760Ssam 		return (EPROTOTYPE);
41914049Ssam 	unp2 = sotounpcb(so2);
42014049Ssam 	unp->unp_conn = unp2;
4218925Sroot 	switch (so->so_type) {
4228925Sroot 
4238925Sroot 	case SOCK_DGRAM:
4248925Sroot 		unp->unp_nextref = unp2->unp_refs;
4258925Sroot 		unp2->unp_refs = unp;
42617543Skarels 		soisconnected(so);
4278925Sroot 		break;
4288925Sroot 
4298925Sroot 	case SOCK_STREAM:
4309169Ssam 		unp2->unp_conn = unp;
43114049Ssam 		soisconnected(so2);
43214049Ssam 		soisconnected(so);
4338925Sroot 		break;
4348925Sroot 
4358925Sroot 	default:
43612760Ssam 		panic("unp_connect2");
4378925Sroot 	}
4388925Sroot 	return (0);
4398925Sroot }
4409169Ssam 
4419169Ssam unp_disconnect(unp)
4429169Ssam 	struct unpcb *unp;
4439169Ssam {
4449169Ssam 	register struct unpcb *unp2 = unp->unp_conn;
4459169Ssam 
4469169Ssam 	if (unp2 == 0)
4479169Ssam 		return;
4489169Ssam 	unp->unp_conn = 0;
4499169Ssam 	switch (unp->unp_socket->so_type) {
4509169Ssam 
4519169Ssam 	case SOCK_DGRAM:
4529169Ssam 		if (unp2->unp_refs == unp)
4539169Ssam 			unp2->unp_refs = unp->unp_nextref;
4549169Ssam 		else {
4559169Ssam 			unp2 = unp2->unp_refs;
4569169Ssam 			for (;;) {
4579169Ssam 				if (unp2 == 0)
4589169Ssam 					panic("unp_disconnect");
4599169Ssam 				if (unp2->unp_nextref == unp)
4609169Ssam 					break;
4619169Ssam 				unp2 = unp2->unp_nextref;
4629169Ssam 			}
4639169Ssam 			unp2->unp_nextref = unp->unp_nextref;
4649169Ssam 		}
4659169Ssam 		unp->unp_nextref = 0;
46621768Skarels 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
4679169Ssam 		break;
4689169Ssam 
4699169Ssam 	case SOCK_STREAM:
47014049Ssam 		soisdisconnected(unp->unp_socket);
4719169Ssam 		unp2->unp_conn = 0;
4729169Ssam 		soisdisconnected(unp2->unp_socket);
4739169Ssam 		break;
4749169Ssam 	}
4759169Ssam }
4769169Ssam 
47712760Ssam #ifdef notdef
4789169Ssam unp_abort(unp)
4799169Ssam 	struct unpcb *unp;
4809169Ssam {
4819169Ssam 
4829169Ssam 	unp_detach(unp);
4839169Ssam }
48412760Ssam #endif
4859169Ssam 
4869169Ssam /*ARGSUSED*/
4879169Ssam unp_usrclosed(unp)
4889169Ssam 	struct unpcb *unp;
4899169Ssam {
4909169Ssam 
4919169Ssam }
4929169Ssam 
4939169Ssam unp_drop(unp, errno)
4949169Ssam 	struct unpcb *unp;
4959169Ssam 	int errno;
4969169Ssam {
49716054Skarels 	struct socket *so = unp->unp_socket;
4989169Ssam 
49916054Skarels 	so->so_error = errno;
5009169Ssam 	unp_disconnect(unp);
50116054Skarels 	if (so->so_head) {
50216054Skarels 		so->so_pcb = (caddr_t) 0;
503*25632Skarels 		m_freem(unp->unp_addr);
50416054Skarels 		(void) m_free(dtom(unp));
50516054Skarels 		sofree(so);
50616054Skarels 	}
5079169Ssam }
5089169Ssam 
50912760Ssam #ifdef notdef
5109169Ssam unp_drain()
5119169Ssam {
5129169Ssam 
5139169Ssam }
51412760Ssam #endif
51512760Ssam 
51612760Ssam unp_externalize(rights)
51712760Ssam 	struct mbuf *rights;
51812760Ssam {
51912760Ssam 	int newfds = rights->m_len / sizeof (int);
52012760Ssam 	register int i;
52112760Ssam 	register struct file **rp = mtod(rights, struct file **);
52212760Ssam 	register struct file *fp;
52312760Ssam 	int f;
52412760Ssam 
52512760Ssam 	if (newfds > ufavail()) {
52612760Ssam 		for (i = 0; i < newfds; i++) {
52712760Ssam 			fp = *rp;
52812760Ssam 			unp_discard(fp);
52912760Ssam 			*rp++ = 0;
53012760Ssam 		}
53112760Ssam 		return (EMSGSIZE);
53212760Ssam 	}
53312760Ssam 	for (i = 0; i < newfds; i++) {
53412760Ssam 		f = ufalloc(0);
53512760Ssam 		if (f < 0)
53612760Ssam 			panic("unp_externalize");
53712760Ssam 		fp = *rp;
53812760Ssam 		u.u_ofile[f] = fp;
53912760Ssam 		fp->f_msgcount--;
540*25632Skarels 		unp_rights--;
54114927Smckusick 		*(int *)rp++ = f;
54212760Ssam 	}
54312760Ssam 	return (0);
54412760Ssam }
54512760Ssam 
54612760Ssam unp_internalize(rights)
54712760Ssam 	struct mbuf *rights;
54812760Ssam {
54912760Ssam 	register struct file **rp;
55012760Ssam 	int oldfds = rights->m_len / sizeof (int);
55112760Ssam 	register int i;
55212760Ssam 	register struct file *fp;
55312760Ssam 
55412760Ssam 	rp = mtod(rights, struct file **);
55513084Ssam 	for (i = 0; i < oldfds; i++)
55612760Ssam 		if (getf(*(int *)rp++) == 0)
55712760Ssam 			return (EBADF);
55812760Ssam 	rp = mtod(rights, struct file **);
55913084Ssam 	for (i = 0; i < oldfds; i++) {
56012760Ssam 		fp = getf(*(int *)rp);
56112760Ssam 		*rp++ = fp;
56212760Ssam 		fp->f_count++;
56312760Ssam 		fp->f_msgcount++;
564*25632Skarels 		unp_rights++;
56512760Ssam 	}
56612760Ssam 	return (0);
56712760Ssam }
56812760Ssam 
56912760Ssam int	unp_defer, unp_gcing;
57012760Ssam int	unp_mark();
57116995Skarels extern	struct domain unixdomain;
57212760Ssam 
57312760Ssam unp_gc()
57412760Ssam {
57512760Ssam 	register struct file *fp;
57612760Ssam 	register struct socket *so;
57712760Ssam 
57812760Ssam 	if (unp_gcing)
57912760Ssam 		return;
58012760Ssam 	unp_gcing = 1;
58112760Ssam restart:
58212760Ssam 	unp_defer = 0;
58312760Ssam 	for (fp = file; fp < fileNFILE; fp++)
58412760Ssam 		fp->f_flag &= ~(FMARK|FDEFER);
58512760Ssam 	do {
58612760Ssam 		for (fp = file; fp < fileNFILE; fp++) {
58712760Ssam 			if (fp->f_count == 0)
58812760Ssam 				continue;
58912760Ssam 			if (fp->f_flag & FDEFER) {
59012760Ssam 				fp->f_flag &= ~FDEFER;
59112760Ssam 				unp_defer--;
59212760Ssam 			} else {
59312760Ssam 				if (fp->f_flag & FMARK)
59412760Ssam 					continue;
59512760Ssam 				if (fp->f_count == fp->f_msgcount)
59612760Ssam 					continue;
59712760Ssam 				fp->f_flag |= FMARK;
59812760Ssam 			}
59912760Ssam 			if (fp->f_type != DTYPE_SOCKET)
60012760Ssam 				continue;
60112760Ssam 			so = (struct socket *)fp->f_data;
60216995Skarels 			if (so->so_proto->pr_domain != &unixdomain ||
60321768Skarels 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
60412760Ssam 				continue;
60512760Ssam 			if (so->so_rcv.sb_flags & SB_LOCK) {
60612760Ssam 				sbwait(&so->so_rcv);
60712760Ssam 				goto restart;
60812760Ssam 			}
60912760Ssam 			unp_scan(so->so_rcv.sb_mb, unp_mark);
61012760Ssam 		}
61112760Ssam 	} while (unp_defer);
61212760Ssam 	for (fp = file; fp < fileNFILE; fp++) {
61312760Ssam 		if (fp->f_count == 0)
61412760Ssam 			continue;
615*25632Skarels 		if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
616*25632Skarels 			while (fp->f_msgcount)
617*25632Skarels 				unp_discard(fp);
61812760Ssam 	}
61912760Ssam 	unp_gcing = 0;
62012760Ssam }
62112760Ssam 
62216995Skarels unp_dispose(m)
62316995Skarels 	struct mbuf *m;
62416995Skarels {
62516995Skarels 	int unp_discard();
62616995Skarels 
62717020Skarels 	if (m)
62817020Skarels 		unp_scan(m, unp_discard);
62916995Skarels }
63016995Skarels 
63116995Skarels unp_scan(m0, op)
63216995Skarels 	register struct mbuf *m0;
63312760Ssam 	int (*op)();
63412760Ssam {
63516995Skarels 	register struct mbuf *m;
63612760Ssam 	register struct file **rp;
63712760Ssam 	register int i;
63817020Skarels 	int qfds;
63912760Ssam 
64016995Skarels 	while (m0) {
64116995Skarels 		for (m = m0; m; m = m->m_next)
64216995Skarels 			if (m->m_type == MT_RIGHTS && m->m_len) {
64316995Skarels 				qfds = m->m_len / sizeof (struct file *);
64416995Skarels 				rp = mtod(m, struct file **);
64516995Skarels 				for (i = 0; i < qfds; i++)
64616995Skarels 					(*op)(*rp++);
64716995Skarels 				break;		/* XXX, but saves time */
64816995Skarels 			}
64917020Skarels 		m0 = m0->m_act;
65012760Ssam 	}
65112760Ssam }
65212760Ssam 
65312760Ssam unp_mark(fp)
65412760Ssam 	struct file *fp;
65512760Ssam {
65612760Ssam 
65712760Ssam 	if (fp->f_flag & FMARK)
65812760Ssam 		return;
65912760Ssam 	unp_defer++;
66012760Ssam 	fp->f_flag |= (FMARK|FDEFER);
66112760Ssam }
66212760Ssam 
66312760Ssam unp_discard(fp)
66412760Ssam 	struct file *fp;
66512760Ssam {
66612760Ssam 
66712760Ssam 	fp->f_msgcount--;
668*25632Skarels 	unp_rights--;
66913084Ssam 	closef(fp);
67012760Ssam }
671