xref: /csrg-svn/sys/kern/uipc_usrreq.c (revision 33287)
123443Smckusick /*
229129Smckusick  * Copyright (c) 1982, 1986 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*33287Sbostic  *	@(#)uipc_usrreq.c	7.2 (Berkeley) 01/07/88
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)
19826365Skarels 				(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:
24428292Skarels 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
24528292Skarels 			nam->m_len = unp->unp_conn->unp_addr->m_len;
24628292Skarels 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
247*33287Sbostic 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
24828292Skarels 		}
24914121Ssam 		break;
25014121Ssam 
2518925Sroot 	case PRU_SLOWTIMO:
2528925Sroot 		break;
2538925Sroot 
2548925Sroot 	default:
2558925Sroot 		panic("piusrreq");
2568925Sroot 	}
25712760Ssam release:
25812760Ssam 	if (m)
25912760Ssam 		m_freem(m);
26011709Ssam 	return (error);
2618925Sroot }
2628925Sroot 
26316973Skarels /*
26425632Skarels  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
26525632Skarels  * for stream sockets, although the total for sender and receiver is
26625632Skarels  * actually only PIPSIZ.
26716973Skarels  * Datagram sockets really use the sendspace as the maximum datagram size,
26816973Skarels  * and don't really want to reserve the sendspace.  Their recvspace should
26916973Skarels  * be large enough for at least one max-size datagram plus address.
27016973Skarels  */
27116973Skarels #define	PIPSIZ	4096
27216973Skarels int	unpst_sendspace = PIPSIZ;
27325632Skarels int	unpst_recvspace = PIPSIZ;
27416973Skarels int	unpdg_sendspace = 2*1024;	/* really max datagram size */
27516973Skarels int	unpdg_recvspace = 4*1024;
2768925Sroot 
27725632Skarels int	unp_rights;			/* file descriptors in flight */
27825632Skarels 
2799169Ssam unp_attach(so)
2808925Sroot 	struct socket *so;
2818925Sroot {
2829169Ssam 	register struct mbuf *m;
2838925Sroot 	register struct unpcb *unp;
2848925Sroot 	int error;
2858925Sroot 
28616973Skarels 	switch (so->so_type) {
28716973Skarels 
28816973Skarels 	case SOCK_STREAM:
28916973Skarels 		error = soreserve(so, unpst_sendspace, unpst_recvspace);
29016973Skarels 		break;
29116973Skarels 
29216973Skarels 	case SOCK_DGRAM:
29316973Skarels 		error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
29416973Skarels 		break;
29516973Skarels 	}
2968925Sroot 	if (error)
29710139Ssam 		return (error);
2989637Ssam 	m = m_getclr(M_DONTWAIT, MT_PCB);
29910139Ssam 	if (m == NULL)
30010139Ssam 		return (ENOBUFS);
3018925Sroot 	unp = mtod(m, struct unpcb *);
3028925Sroot 	so->so_pcb = (caddr_t)unp;
3038925Sroot 	unp->unp_socket = so;
3048925Sroot 	return (0);
3058925Sroot }
3068925Sroot 
3078925Sroot unp_detach(unp)
3089169Ssam 	register struct unpcb *unp;
3098925Sroot {
3108925Sroot 
3118925Sroot 	if (unp->unp_inode) {
31217020Skarels 		unp->unp_inode->i_socket = 0;
3138925Sroot 		irele(unp->unp_inode);
3148925Sroot 		unp->unp_inode = 0;
3158925Sroot 	}
3168925Sroot 	if (unp->unp_conn)
3178925Sroot 		unp_disconnect(unp);
3188925Sroot 	while (unp->unp_refs)
3198925Sroot 		unp_drop(unp->unp_refs, ECONNRESET);
3208925Sroot 	soisdisconnected(unp->unp_socket);
3218925Sroot 	unp->unp_socket->so_pcb = 0;
32225632Skarels 	m_freem(unp->unp_addr);
3239169Ssam 	(void) m_free(dtom(unp));
32425632Skarels 	if (unp_rights)
32525632Skarels 		unp_gc();
3268925Sroot }
3278925Sroot 
3289169Ssam unp_bind(unp, nam)
3298925Sroot 	struct unpcb *unp;
3309169Ssam 	struct mbuf *nam;
3318925Sroot {
3329169Ssam 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3338925Sroot 	register struct inode *ip;
33416695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3358925Sroot 	int error;
3368925Sroot 
33716695Smckusick 	ndp->ni_dirp = soun->sun_path;
33825232Smckusick 	if (unp->unp_inode != NULL || nam->m_len == MLEN)
33912760Ssam 		return (EINVAL);
34012760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
34112760Ssam /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
34216695Smckusick 	ndp->ni_nameiop = CREATE | FOLLOW;
34316695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
34416695Smckusick 	ip = namei(ndp);
3458925Sroot 	if (ip) {
3468925Sroot 		iput(ip);
34710139Ssam 		return (EADDRINUSE);
3488925Sroot 	}
34911828Ssam 	if (error = u.u_error) {
35011828Ssam 		u.u_error = 0;			/* XXX */
35111828Ssam 		return (error);
35211828Ssam 	}
35316695Smckusick 	ip = maknode(IFSOCK | 0777, ndp);
3548925Sroot 	if (ip == NULL) {
3558925Sroot 		error = u.u_error;		/* XXX */
3568925Sroot 		u.u_error = 0;			/* XXX */
3578925Sroot 		return (error);
3588925Sroot 	}
3598925Sroot 	ip->i_socket = unp->unp_socket;
3608925Sroot 	unp->unp_inode = ip;
36125632Skarels 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
3628925Sroot 	iunlock(ip);			/* but keep reference */
3638925Sroot 	return (0);
3648925Sroot }
3658925Sroot 
3669169Ssam unp_connect(so, nam)
3678925Sroot 	struct socket *so;
3689169Ssam 	struct mbuf *nam;
3698925Sroot {
3709169Ssam 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
3719169Ssam 	register struct inode *ip;
3728925Sroot 	int error;
37312760Ssam 	register struct socket *so2;
37416695Smckusick 	register struct nameidata *ndp = &u.u_nd;
3758925Sroot 
37616695Smckusick 	ndp->ni_dirp = soun->sun_path;
37712760Ssam 	if (nam->m_len + (nam->m_off - MMINOFF) == MLEN)
37812760Ssam 		return (EMSGSIZE);
37912760Ssam 	*(mtod(nam, caddr_t) + nam->m_len) = 0;
38016695Smckusick 	ndp->ni_nameiop = LOOKUP | FOLLOW;
38116695Smckusick 	ndp->ni_segflg = UIO_SYSSPACE;
38216695Smckusick 	ip = namei(ndp);
3838925Sroot 	if (ip == 0) {
3848925Sroot 		error = u.u_error;
3858925Sroot 		u.u_error = 0;
38610139Ssam 		return (error);		/* XXX */
3878925Sroot 	}
38817543Skarels 	if (access(ip, IWRITE)) {
38917543Skarels 		error = u.u_error;
39017543Skarels 		u.u_error = 0; 		/* XXX */
39117543Skarels 		goto bad;
39217543Skarels 	}
3938925Sroot 	if ((ip->i_mode&IFMT) != IFSOCK) {
3948925Sroot 		error = ENOTSOCK;
3958925Sroot 		goto bad;
3968925Sroot 	}
3978925Sroot 	so2 = ip->i_socket;
3988925Sroot 	if (so2 == 0) {
3998925Sroot 		error = ECONNREFUSED;
4008925Sroot 		goto bad;
4018925Sroot 	}
40213115Ssam 	if (so->so_type != so2->so_type) {
40313115Ssam 		error = EPROTOTYPE;
40413115Ssam 		goto bad;
40513115Ssam 	}
40613115Ssam 	if (so->so_proto->pr_flags & PR_CONNREQUIRED &&
40713115Ssam 	    ((so2->so_options&SO_ACCEPTCONN) == 0 ||
40813115Ssam 	     (so2 = sonewconn(so2)) == 0)) {
40913115Ssam 		error = ECONNREFUSED;
41013115Ssam 		goto bad;
41113115Ssam 	}
41226281Skarels 	error = unp_connect2(so, so2);
41312760Ssam bad:
41412760Ssam 	iput(ip);
41512760Ssam 	return (error);
41612760Ssam }
41712760Ssam 
41826281Skarels unp_connect2(so, so2)
41912760Ssam 	register struct socket *so;
42012760Ssam 	register struct socket *so2;
42112760Ssam {
42212760Ssam 	register struct unpcb *unp = sotounpcb(so);
42312760Ssam 	register struct unpcb *unp2;
42412760Ssam 
42512760Ssam 	if (so2->so_type != so->so_type)
42612760Ssam 		return (EPROTOTYPE);
42714049Ssam 	unp2 = sotounpcb(so2);
42814049Ssam 	unp->unp_conn = unp2;
4298925Sroot 	switch (so->so_type) {
4308925Sroot 
4318925Sroot 	case SOCK_DGRAM:
4328925Sroot 		unp->unp_nextref = unp2->unp_refs;
4338925Sroot 		unp2->unp_refs = unp;
43417543Skarels 		soisconnected(so);
4358925Sroot 		break;
4368925Sroot 
4378925Sroot 	case SOCK_STREAM:
4389169Ssam 		unp2->unp_conn = unp;
43914049Ssam 		soisconnected(so2);
44014049Ssam 		soisconnected(so);
4418925Sroot 		break;
4428925Sroot 
4438925Sroot 	default:
44412760Ssam 		panic("unp_connect2");
4458925Sroot 	}
4468925Sroot 	return (0);
4478925Sroot }
4489169Ssam 
4499169Ssam unp_disconnect(unp)
4509169Ssam 	struct unpcb *unp;
4519169Ssam {
4529169Ssam 	register struct unpcb *unp2 = unp->unp_conn;
4539169Ssam 
4549169Ssam 	if (unp2 == 0)
4559169Ssam 		return;
4569169Ssam 	unp->unp_conn = 0;
4579169Ssam 	switch (unp->unp_socket->so_type) {
4589169Ssam 
4599169Ssam 	case SOCK_DGRAM:
4609169Ssam 		if (unp2->unp_refs == unp)
4619169Ssam 			unp2->unp_refs = unp->unp_nextref;
4629169Ssam 		else {
4639169Ssam 			unp2 = unp2->unp_refs;
4649169Ssam 			for (;;) {
4659169Ssam 				if (unp2 == 0)
4669169Ssam 					panic("unp_disconnect");
4679169Ssam 				if (unp2->unp_nextref == unp)
4689169Ssam 					break;
4699169Ssam 				unp2 = unp2->unp_nextref;
4709169Ssam 			}
4719169Ssam 			unp2->unp_nextref = unp->unp_nextref;
4729169Ssam 		}
4739169Ssam 		unp->unp_nextref = 0;
47421768Skarels 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
4759169Ssam 		break;
4769169Ssam 
4779169Ssam 	case SOCK_STREAM:
47814049Ssam 		soisdisconnected(unp->unp_socket);
4799169Ssam 		unp2->unp_conn = 0;
4809169Ssam 		soisdisconnected(unp2->unp_socket);
4819169Ssam 		break;
4829169Ssam 	}
4839169Ssam }
4849169Ssam 
48512760Ssam #ifdef notdef
4869169Ssam unp_abort(unp)
4879169Ssam 	struct unpcb *unp;
4889169Ssam {
4899169Ssam 
4909169Ssam 	unp_detach(unp);
4919169Ssam }
49212760Ssam #endif
4939169Ssam 
4949169Ssam /*ARGSUSED*/
4959169Ssam unp_usrclosed(unp)
4969169Ssam 	struct unpcb *unp;
4979169Ssam {
4989169Ssam 
4999169Ssam }
5009169Ssam 
5019169Ssam unp_drop(unp, errno)
5029169Ssam 	struct unpcb *unp;
5039169Ssam 	int errno;
5049169Ssam {
50516054Skarels 	struct socket *so = unp->unp_socket;
5069169Ssam 
50716054Skarels 	so->so_error = errno;
5089169Ssam 	unp_disconnect(unp);
50916054Skarels 	if (so->so_head) {
51016054Skarels 		so->so_pcb = (caddr_t) 0;
51125632Skarels 		m_freem(unp->unp_addr);
51216054Skarels 		(void) m_free(dtom(unp));
51316054Skarels 		sofree(so);
51416054Skarels 	}
5159169Ssam }
5169169Ssam 
51712760Ssam #ifdef notdef
5189169Ssam unp_drain()
5199169Ssam {
5209169Ssam 
5219169Ssam }
52212760Ssam #endif
52312760Ssam 
52412760Ssam unp_externalize(rights)
52512760Ssam 	struct mbuf *rights;
52612760Ssam {
52712760Ssam 	int newfds = rights->m_len / sizeof (int);
52812760Ssam 	register int i;
52912760Ssam 	register struct file **rp = mtod(rights, struct file **);
53012760Ssam 	register struct file *fp;
53112760Ssam 	int f;
53212760Ssam 
53312760Ssam 	if (newfds > ufavail()) {
53412760Ssam 		for (i = 0; i < newfds; i++) {
53512760Ssam 			fp = *rp;
53612760Ssam 			unp_discard(fp);
53712760Ssam 			*rp++ = 0;
53812760Ssam 		}
53912760Ssam 		return (EMSGSIZE);
54012760Ssam 	}
54112760Ssam 	for (i = 0; i < newfds; i++) {
54212760Ssam 		f = ufalloc(0);
54312760Ssam 		if (f < 0)
54412760Ssam 			panic("unp_externalize");
54512760Ssam 		fp = *rp;
54612760Ssam 		u.u_ofile[f] = fp;
54712760Ssam 		fp->f_msgcount--;
54825632Skarels 		unp_rights--;
54914927Smckusick 		*(int *)rp++ = f;
55012760Ssam 	}
55112760Ssam 	return (0);
55212760Ssam }
55312760Ssam 
55412760Ssam unp_internalize(rights)
55512760Ssam 	struct mbuf *rights;
55612760Ssam {
55712760Ssam 	register struct file **rp;
55812760Ssam 	int oldfds = rights->m_len / sizeof (int);
55912760Ssam 	register int i;
56012760Ssam 	register struct file *fp;
56112760Ssam 
56212760Ssam 	rp = mtod(rights, struct file **);
56313084Ssam 	for (i = 0; i < oldfds; i++)
56412760Ssam 		if (getf(*(int *)rp++) == 0)
56512760Ssam 			return (EBADF);
56612760Ssam 	rp = mtod(rights, struct file **);
56713084Ssam 	for (i = 0; i < oldfds; i++) {
56812760Ssam 		fp = getf(*(int *)rp);
56912760Ssam 		*rp++ = fp;
57012760Ssam 		fp->f_count++;
57112760Ssam 		fp->f_msgcount++;
57225632Skarels 		unp_rights++;
57312760Ssam 	}
57412760Ssam 	return (0);
57512760Ssam }
57612760Ssam 
57712760Ssam int	unp_defer, unp_gcing;
57812760Ssam int	unp_mark();
57916995Skarels extern	struct domain unixdomain;
58012760Ssam 
58112760Ssam unp_gc()
58212760Ssam {
58312760Ssam 	register struct file *fp;
58412760Ssam 	register struct socket *so;
58512760Ssam 
58612760Ssam 	if (unp_gcing)
58712760Ssam 		return;
58812760Ssam 	unp_gcing = 1;
58912760Ssam restart:
59012760Ssam 	unp_defer = 0;
59112760Ssam 	for (fp = file; fp < fileNFILE; fp++)
59212760Ssam 		fp->f_flag &= ~(FMARK|FDEFER);
59312760Ssam 	do {
59412760Ssam 		for (fp = file; fp < fileNFILE; fp++) {
59512760Ssam 			if (fp->f_count == 0)
59612760Ssam 				continue;
59712760Ssam 			if (fp->f_flag & FDEFER) {
59812760Ssam 				fp->f_flag &= ~FDEFER;
59912760Ssam 				unp_defer--;
60012760Ssam 			} else {
60112760Ssam 				if (fp->f_flag & FMARK)
60212760Ssam 					continue;
60312760Ssam 				if (fp->f_count == fp->f_msgcount)
60412760Ssam 					continue;
60512760Ssam 				fp->f_flag |= FMARK;
60612760Ssam 			}
60712760Ssam 			if (fp->f_type != DTYPE_SOCKET)
60812760Ssam 				continue;
60912760Ssam 			so = (struct socket *)fp->f_data;
61016995Skarels 			if (so->so_proto->pr_domain != &unixdomain ||
61121768Skarels 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
61212760Ssam 				continue;
61312760Ssam 			if (so->so_rcv.sb_flags & SB_LOCK) {
61412760Ssam 				sbwait(&so->so_rcv);
61512760Ssam 				goto restart;
61612760Ssam 			}
61712760Ssam 			unp_scan(so->so_rcv.sb_mb, unp_mark);
61812760Ssam 		}
61912760Ssam 	} while (unp_defer);
62012760Ssam 	for (fp = file; fp < fileNFILE; fp++) {
62112760Ssam 		if (fp->f_count == 0)
62212760Ssam 			continue;
62325632Skarels 		if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
62425632Skarels 			while (fp->f_msgcount)
62525632Skarels 				unp_discard(fp);
62612760Ssam 	}
62712760Ssam 	unp_gcing = 0;
62812760Ssam }
62912760Ssam 
63016995Skarels unp_dispose(m)
63116995Skarels 	struct mbuf *m;
63216995Skarels {
63316995Skarels 	int unp_discard();
63416995Skarels 
63517020Skarels 	if (m)
63617020Skarels 		unp_scan(m, unp_discard);
63716995Skarels }
63816995Skarels 
63916995Skarels unp_scan(m0, op)
64016995Skarels 	register struct mbuf *m0;
64112760Ssam 	int (*op)();
64212760Ssam {
64316995Skarels 	register struct mbuf *m;
64412760Ssam 	register struct file **rp;
64512760Ssam 	register int i;
64617020Skarels 	int qfds;
64712760Ssam 
64816995Skarels 	while (m0) {
64916995Skarels 		for (m = m0; m; m = m->m_next)
65016995Skarels 			if (m->m_type == MT_RIGHTS && m->m_len) {
65116995Skarels 				qfds = m->m_len / sizeof (struct file *);
65216995Skarels 				rp = mtod(m, struct file **);
65316995Skarels 				for (i = 0; i < qfds; i++)
65416995Skarels 					(*op)(*rp++);
65516995Skarels 				break;		/* XXX, but saves time */
65616995Skarels 			}
65717020Skarels 		m0 = m0->m_act;
65812760Ssam 	}
65912760Ssam }
66012760Ssam 
66112760Ssam unp_mark(fp)
66212760Ssam 	struct file *fp;
66312760Ssam {
66412760Ssam 
66512760Ssam 	if (fp->f_flag & FMARK)
66612760Ssam 		return;
66712760Ssam 	unp_defer++;
66812760Ssam 	fp->f_flag |= (FMARK|FDEFER);
66912760Ssam }
67012760Ssam 
67112760Ssam unp_discard(fp)
67212760Ssam 	struct file *fp;
67312760Ssam {
67412760Ssam 
67512760Ssam 	fp->f_msgcount--;
67625632Skarels 	unp_rights--;
67713084Ssam 	closef(fp);
67812760Ssam }
679