Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.2
      1 /*
      2  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)uipc_usrreq.c	7.26 (Berkeley) 6/3/91
     34  *	$Id: uipc_usrreq.c,v 1.2 1993/05/18 18:19:42 cgd Exp $
     35  */
     36 
     37 #include "param.h"
     38 #include "proc.h"
     39 #include "filedesc.h"
     40 #include "select.h"
     41 #include "domain.h"
     42 #include "protosw.h"
     43 #include "socket.h"
     44 #include "socketvar.h"
     45 #include "unpcb.h"
     46 #include "un.h"
     47 #include "namei.h"
     48 #include "vnode.h"
     49 #include "file.h"
     50 #include "stat.h"
     51 #include "mbuf.h"
     52 
     53 /*
     54  * Unix communications domain.
     55  *
     56  * TODO:
     57  *	SEQPACKET, RDM
     58  *	rethink name space problems
     59  *	need a proper out-of-band
     60  */
     61 struct	sockaddr sun_noname = { sizeof(sun_noname), AF_UNIX };
     62 ino_t	unp_ino;			/* prototype for fake inode numbers */
     63 
     64 /*ARGSUSED*/
     65 uipc_usrreq(so, req, m, nam, control)
     66 	struct socket *so;
     67 	int req;
     68 	struct mbuf *m, *nam, *control;
     69 {
     70 	struct unpcb *unp = sotounpcb(so);
     71 	register struct socket *so2;
     72 	register int error = 0;
     73 	struct proc *p = curproc;	/* XXX */
     74 
     75 	if (req == PRU_CONTROL)
     76 		return (EOPNOTSUPP);
     77 	if (req != PRU_SEND && control && control->m_len) {
     78 		error = EOPNOTSUPP;
     79 		goto release;
     80 	}
     81 	if (unp == 0 && req != PRU_ATTACH) {
     82 		error = EINVAL;
     83 		goto release;
     84 	}
     85 	switch (req) {
     86 
     87 	case PRU_ATTACH:
     88 		if (unp) {
     89 			error = EISCONN;
     90 			break;
     91 		}
     92 		error = unp_attach(so);
     93 		break;
     94 
     95 	case PRU_DETACH:
     96 		unp_detach(unp);
     97 		break;
     98 
     99 	case PRU_BIND:
    100 		error = unp_bind(unp, nam, p);
    101 		break;
    102 
    103 	case PRU_LISTEN:
    104 		if (unp->unp_vnode == 0)
    105 			error = EINVAL;
    106 		break;
    107 
    108 	case PRU_CONNECT:
    109 		error = unp_connect(so, nam, p);
    110 		break;
    111 
    112 	case PRU_CONNECT2:
    113 		error = unp_connect2(so, (struct socket *)nam);
    114 		break;
    115 
    116 	case PRU_DISCONNECT:
    117 		unp_disconnect(unp);
    118 		break;
    119 
    120 	case PRU_ACCEPT:
    121 		/*
    122 		 * Pass back name of connected socket,
    123 		 * if it was bound and we are still connected
    124 		 * (our peer may have closed already!).
    125 		 */
    126 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
    127 			nam->m_len = unp->unp_conn->unp_addr->m_len;
    128 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
    129 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
    130 		} else {
    131 			nam->m_len = sizeof(sun_noname);
    132 			*(mtod(nam, struct sockaddr *)) = sun_noname;
    133 		}
    134 		break;
    135 
    136 	case PRU_SHUTDOWN:
    137 		socantsendmore(so);
    138 		unp_shutdown(unp);
    139 		break;
    140 
    141 	case PRU_RCVD:
    142 		switch (so->so_type) {
    143 
    144 		case SOCK_DGRAM:
    145 			panic("uipc 1");
    146 			/*NOTREACHED*/
    147 
    148 		case SOCK_STREAM:
    149 #define	rcv (&so->so_rcv)
    150 #define snd (&so2->so_snd)
    151 			if (unp->unp_conn == 0)
    152 				break;
    153 			so2 = unp->unp_conn->unp_socket;
    154 			/*
    155 			 * Adjust backpressure on sender
    156 			 * and wakeup any waiting to write.
    157 			 */
    158 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    159 			unp->unp_mbcnt = rcv->sb_mbcnt;
    160 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
    161 			unp->unp_cc = rcv->sb_cc;
    162 			sowwakeup(so2);
    163 #undef snd
    164 #undef rcv
    165 			break;
    166 
    167 		default:
    168 			panic("uipc 2");
    169 		}
    170 		break;
    171 
    172 	case PRU_SEND:
    173 		if (control && (error = unp_internalize(control, p)))
    174 			break;
    175 		switch (so->so_type) {
    176 
    177 		case SOCK_DGRAM: {
    178 			struct sockaddr *from;
    179 
    180 			if (nam) {
    181 				if (unp->unp_conn) {
    182 					error = EISCONN;
    183 					break;
    184 				}
    185 				error = unp_connect(so, nam, p);
    186 				if (error)
    187 					break;
    188 			} else {
    189 				if (unp->unp_conn == 0) {
    190 					error = ENOTCONN;
    191 					break;
    192 				}
    193 			}
    194 			so2 = unp->unp_conn->unp_socket;
    195 			if (unp->unp_addr)
    196 				from = mtod(unp->unp_addr, struct sockaddr *);
    197 			else
    198 				from = &sun_noname;
    199 			if (sbappendaddr(&so2->so_rcv, from, m, control)) {
    200 				sorwakeup(so2);
    201 				m = 0;
    202 				control = 0;
    203 			} else
    204 				error = ENOBUFS;
    205 			if (nam)
    206 				unp_disconnect(unp);
    207 			break;
    208 		}
    209 
    210 		case SOCK_STREAM:
    211 #define	rcv (&so2->so_rcv)
    212 #define	snd (&so->so_snd)
    213 			if (so->so_state & SS_CANTSENDMORE) {
    214 				error = EPIPE;
    215 				break;
    216 			}
    217 			if (unp->unp_conn == 0)
    218 				panic("uipc 3");
    219 			so2 = unp->unp_conn->unp_socket;
    220 			/*
    221 			 * Send to paired receive port, and then reduce
    222 			 * send buffer hiwater marks to maintain backpressure.
    223 			 * Wake up readers.
    224 			 */
    225 			if (control) {
    226 				if (sbappendcontrol(rcv, m, control))
    227 					control = 0;
    228 			} else
    229 				sbappend(rcv, m);
    230 			snd->sb_mbmax -=
    231 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    232 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    233 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
    234 			unp->unp_conn->unp_cc = rcv->sb_cc;
    235 			sorwakeup(so2);
    236 			m = 0;
    237 #undef snd
    238 #undef rcv
    239 			break;
    240 
    241 		default:
    242 			panic("uipc 4");
    243 		}
    244 		break;
    245 
    246 	case PRU_ABORT:
    247 		unp_drop(unp, ECONNABORTED);
    248 		break;
    249 
    250 	case PRU_SENSE:
    251 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    252 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    253 			so2 = unp->unp_conn->unp_socket;
    254 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    255 		}
    256 		((struct stat *) m)->st_dev = NODEV;
    257 		if (unp->unp_ino == 0)
    258 			unp->unp_ino = unp_ino++;
    259 		((struct stat *) m)->st_ino = unp->unp_ino;
    260 		return (0);
    261 
    262 	case PRU_RCVOOB:
    263 		return (EOPNOTSUPP);
    264 
    265 	case PRU_SENDOOB:
    266 		error = EOPNOTSUPP;
    267 		break;
    268 
    269 	case PRU_SOCKADDR:
    270 		if (unp->unp_addr) {
    271 			nam->m_len = unp->unp_addr->m_len;
    272 			bcopy(mtod(unp->unp_addr, caddr_t),
    273 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
    274 		} else
    275 			nam->m_len = 0;
    276 		break;
    277 
    278 	case PRU_PEERADDR:
    279 		if (unp->unp_conn && unp->unp_conn->unp_addr) {
    280 			nam->m_len = unp->unp_conn->unp_addr->m_len;
    281 			bcopy(mtod(unp->unp_conn->unp_addr, caddr_t),
    282 			    mtod(nam, caddr_t), (unsigned)nam->m_len);
    283 		} else
    284 			nam->m_len = 0;
    285 		break;
    286 
    287 	case PRU_SLOWTIMO:
    288 		break;
    289 
    290 	default:
    291 		panic("piusrreq");
    292 	}
    293 release:
    294 	if (control)
    295 		m_freem(control);
    296 	if (m)
    297 		m_freem(m);
    298 	return (error);
    299 }
    300 
    301 /*
    302  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    303  * for stream sockets, although the total for sender and receiver is
    304  * actually only PIPSIZ.
    305  * Datagram sockets really use the sendspace as the maximum datagram size,
    306  * and don't really want to reserve the sendspace.  Their recvspace should
    307  * be large enough for at least one max-size datagram plus address.
    308  */
    309 #define	PIPSIZ	4096
    310 u_long	unpst_sendspace = PIPSIZ;
    311 u_long	unpst_recvspace = PIPSIZ;
    312 u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    313 u_long	unpdg_recvspace = 4*1024;
    314 
    315 int	unp_rights;			/* file descriptors in flight */
    316 
    317 unp_attach(so)
    318 	struct socket *so;
    319 {
    320 	register struct mbuf *m;
    321 	register struct unpcb *unp;
    322 	int error;
    323 
    324 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    325 		switch (so->so_type) {
    326 
    327 		case SOCK_STREAM:
    328 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    329 			break;
    330 
    331 		case SOCK_DGRAM:
    332 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    333 			break;
    334 		}
    335 		if (error)
    336 			return (error);
    337 	}
    338 	m = m_getclr(M_DONTWAIT, MT_PCB);
    339 	if (m == NULL)
    340 		return (ENOBUFS);
    341 	unp = mtod(m, struct unpcb *);
    342 	so->so_pcb = (caddr_t)unp;
    343 	unp->unp_socket = so;
    344 	return (0);
    345 }
    346 
    347 unp_detach(unp)
    348 	register struct unpcb *unp;
    349 {
    350 
    351 	if (unp->unp_vnode) {
    352 		unp->unp_vnode->v_socket = 0;
    353 		vrele(unp->unp_vnode);
    354 		unp->unp_vnode = 0;
    355 	}
    356 	if (unp->unp_conn)
    357 		unp_disconnect(unp);
    358 	while (unp->unp_refs)
    359 		unp_drop(unp->unp_refs, ECONNRESET);
    360 	soisdisconnected(unp->unp_socket);
    361 	unp->unp_socket->so_pcb = 0;
    362 	m_freem(unp->unp_addr);
    363 	(void) m_free(dtom(unp));
    364 	if (unp_rights)
    365 		unp_gc();
    366 }
    367 
    368 unp_bind(unp, nam, p)
    369 	struct unpcb *unp;
    370 	struct mbuf *nam;
    371 	struct proc *p;
    372 {
    373 	struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
    374 	register struct vnode *vp;
    375 	register struct nameidata *ndp;
    376 	struct vattr vattr;
    377 	int error;
    378 	struct nameidata nd;
    379 
    380 	ndp = &nd;
    381 	ndp->ni_dirp = soun->sun_path;
    382 	if (unp->unp_vnode != NULL)
    383 		return (EINVAL);
    384 	if (nam->m_len == MLEN) {
    385 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
    386 			return (EINVAL);
    387 	} else
    388 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
    389 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    390 	ndp->ni_nameiop = CREATE | FOLLOW | LOCKPARENT;
    391 	ndp->ni_segflg = UIO_SYSSPACE;
    392 	if (error = namei(ndp, p))
    393 		return (error);
    394 	vp = ndp->ni_vp;
    395 	if (vp != NULL) {
    396 		VOP_ABORTOP(ndp);
    397 		if (ndp->ni_dvp == vp)
    398 			vrele(ndp->ni_dvp);
    399 		else
    400 			vput(ndp->ni_dvp);
    401 		vrele(vp);
    402 		return (EADDRINUSE);
    403 	}
    404 	VATTR_NULL(&vattr);
    405 	vattr.va_type = VSOCK;
    406 	vattr.va_mode = 0777;
    407 	if (error = VOP_CREATE(ndp, &vattr, p))
    408 		return (error);
    409 	vp = ndp->ni_vp;
    410 	vp->v_socket = unp->unp_socket;
    411 	unp->unp_vnode = vp;
    412 	unp->unp_addr = m_copy(nam, 0, (int)M_COPYALL);
    413 	VOP_UNLOCK(vp);
    414 	return (0);
    415 }
    416 
    417 unp_connect(so, nam, p)
    418 	struct socket *so;
    419 	struct mbuf *nam;
    420 	struct proc *p;
    421 {
    422 	register struct sockaddr_un *soun = mtod(nam, struct sockaddr_un *);
    423 	register struct vnode *vp;
    424 	register struct socket *so2, *so3;
    425 	register struct nameidata *ndp;
    426 	struct unpcb *unp2, *unp3;
    427 	int error;
    428 	struct nameidata nd;
    429 
    430 	ndp = &nd;
    431 	ndp->ni_dirp = soun->sun_path;
    432 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
    433 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
    434 			return (EMSGSIZE);
    435 	} else
    436 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
    437 	ndp->ni_nameiop = LOOKUP | FOLLOW | LOCKLEAF;
    438 	ndp->ni_segflg = UIO_SYSSPACE;
    439 	if (error = namei(ndp, p))
    440 		return (error);
    441 	vp = ndp->ni_vp;
    442 	if (vp->v_type != VSOCK) {
    443 		error = ENOTSOCK;
    444 		goto bad;
    445 	}
    446 	if (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p))
    447 		goto bad;
    448 	so2 = vp->v_socket;
    449 	if (so2 == 0) {
    450 		error = ECONNREFUSED;
    451 		goto bad;
    452 	}
    453 	if (so->so_type != so2->so_type) {
    454 		error = EPROTOTYPE;
    455 		goto bad;
    456 	}
    457 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    458 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    459 		    (so3 = sonewconn(so2, 0)) == 0) {
    460 			error = ECONNREFUSED;
    461 			goto bad;
    462 		}
    463 		unp2 = sotounpcb(so2);
    464 		unp3 = sotounpcb(so3);
    465 		if (unp2->unp_addr)
    466 			unp3->unp_addr =
    467 				  m_copy(unp2->unp_addr, 0, (int)M_COPYALL);
    468 		so2 = so3;
    469 	}
    470 	error = unp_connect2(so, so2);
    471 bad:
    472 	vput(vp);
    473 	return (error);
    474 }
    475 
    476 unp_connect2(so, so2)
    477 	register struct socket *so;
    478 	register struct socket *so2;
    479 {
    480 	register struct unpcb *unp = sotounpcb(so);
    481 	register struct unpcb *unp2;
    482 
    483 	if (so2->so_type != so->so_type)
    484 		return (EPROTOTYPE);
    485 	unp2 = sotounpcb(so2);
    486 	unp->unp_conn = unp2;
    487 	switch (so->so_type) {
    488 
    489 	case SOCK_DGRAM:
    490 		unp->unp_nextref = unp2->unp_refs;
    491 		unp2->unp_refs = unp;
    492 		soisconnected(so);
    493 		break;
    494 
    495 	case SOCK_STREAM:
    496 		unp2->unp_conn = unp;
    497 		soisconnected(so);
    498 		soisconnected(so2);
    499 		break;
    500 
    501 	default:
    502 		panic("unp_connect2");
    503 	}
    504 	return (0);
    505 }
    506 
    507 unp_disconnect(unp)
    508 	struct unpcb *unp;
    509 {
    510 	register struct unpcb *unp2 = unp->unp_conn;
    511 
    512 	if (unp2 == 0)
    513 		return;
    514 	unp->unp_conn = 0;
    515 	switch (unp->unp_socket->so_type) {
    516 
    517 	case SOCK_DGRAM:
    518 		if (unp2->unp_refs == unp)
    519 			unp2->unp_refs = unp->unp_nextref;
    520 		else {
    521 			unp2 = unp2->unp_refs;
    522 			for (;;) {
    523 				if (unp2 == 0)
    524 					panic("unp_disconnect");
    525 				if (unp2->unp_nextref == unp)
    526 					break;
    527 				unp2 = unp2->unp_nextref;
    528 			}
    529 			unp2->unp_nextref = unp->unp_nextref;
    530 		}
    531 		unp->unp_nextref = 0;
    532 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
    533 		break;
    534 
    535 	case SOCK_STREAM:
    536 		soisdisconnected(unp->unp_socket);
    537 		unp2->unp_conn = 0;
    538 		soisdisconnected(unp2->unp_socket);
    539 		break;
    540 	}
    541 }
    542 
    543 #ifdef notdef
    544 unp_abort(unp)
    545 	struct unpcb *unp;
    546 {
    547 
    548 	unp_detach(unp);
    549 }
    550 #endif
    551 
    552 unp_shutdown(unp)
    553 	struct unpcb *unp;
    554 {
    555 	struct socket *so;
    556 
    557 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
    558 	    (so = unp->unp_conn->unp_socket))
    559 		socantrcvmore(so);
    560 }
    561 
    562 unp_drop(unp, errno)
    563 	struct unpcb *unp;
    564 	int errno;
    565 {
    566 	struct socket *so = unp->unp_socket;
    567 
    568 	so->so_error = errno;
    569 	unp_disconnect(unp);
    570 	if (so->so_head) {
    571 		so->so_pcb = (caddr_t) 0;
    572 		m_freem(unp->unp_addr);
    573 		(void) m_free(dtom(unp));
    574 		sofree(so);
    575 	}
    576 }
    577 
    578 #ifdef notdef
    579 unp_drain()
    580 {
    581 
    582 }
    583 #endif
    584 
    585 unp_externalize(rights)
    586 	struct mbuf *rights;
    587 {
    588 	struct proc *p = curproc;		/* XXX */
    589 	register int i;
    590 	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
    591 	register struct file **rp = (struct file **)(cm + 1);
    592 	register struct file *fp;
    593 	int newfds = (cm->cmsg_len - sizeof(*cm)) / sizeof (int);
    594 	int f;
    595 
    596 	if (fdavail(p, newfds)) {
    597 		for (i = 0; i < newfds; i++) {
    598 			fp = *rp;
    599 			unp_discard(fp);
    600 			*rp++ = 0;
    601 		}
    602 		return (EMSGSIZE);
    603 	}
    604 	for (i = 0; i < newfds; i++) {
    605 		if (fdalloc(p, 0, &f))
    606 			panic("unp_externalize");
    607 		fp = *rp;
    608 		p->p_fd->fd_ofiles[f] = fp;
    609 		fp->f_msgcount--;
    610 		unp_rights--;
    611 		*(int *)rp++ = f;
    612 	}
    613 	return (0);
    614 }
    615 
    616 unp_internalize(control, p)
    617 	struct mbuf *control;
    618 	struct proc *p;
    619 {
    620 	struct filedesc *fdp = p->p_fd;
    621 	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
    622 	register struct file **rp;
    623 	register struct file *fp;
    624 	register int i, fd;
    625 	int oldfds;
    626 
    627 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
    628 	    cm->cmsg_len != control->m_len)
    629 		return (EINVAL);
    630 	oldfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
    631 	rp = (struct file **)(cm + 1);
    632 	for (i = 0; i < oldfds; i++) {
    633 		fd = *(int *)rp++;
    634 		if ((unsigned)fd >= fdp->fd_nfiles ||
    635 		    fdp->fd_ofiles[fd] == NULL)
    636 			return (EBADF);
    637 	}
    638 	rp = (struct file **)(cm + 1);
    639 	for (i = 0; i < oldfds; i++) {
    640 		fp = fdp->fd_ofiles[*(int *)rp];
    641 		*rp++ = fp;
    642 		fp->f_count++;
    643 		fp->f_msgcount++;
    644 		unp_rights++;
    645 	}
    646 	return (0);
    647 }
    648 
    649 int	unp_defer, unp_gcing;
    650 int	unp_mark();
    651 extern	struct domain unixdomain;
    652 
    653 unp_gc()
    654 {
    655 	register struct file *fp;
    656 	register struct socket *so;
    657 
    658 	if (unp_gcing)
    659 		return;
    660 	unp_gcing = 1;
    661 restart:
    662 	unp_defer = 0;
    663 	for (fp = filehead; fp; fp = fp->f_filef)
    664 		fp->f_flag &= ~(FMARK|FDEFER);
    665 	do {
    666 		for (fp = filehead; fp; fp = fp->f_filef) {
    667 			if (fp->f_count == 0)
    668 				continue;
    669 			if (fp->f_flag & FDEFER) {
    670 				fp->f_flag &= ~FDEFER;
    671 				unp_defer--;
    672 			} else {
    673 				if (fp->f_flag & FMARK)
    674 					continue;
    675 				if (fp->f_count == fp->f_msgcount)
    676 					continue;
    677 				fp->f_flag |= FMARK;
    678 			}
    679 			if (fp->f_type != DTYPE_SOCKET ||
    680 			    (so = (struct socket *)fp->f_data) == 0)
    681 				continue;
    682 			if (so->so_proto->pr_domain != &unixdomain ||
    683 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
    684 				continue;
    685 #ifdef notdef
    686 			if (so->so_rcv.sb_flags & SB_LOCK) {
    687 				/*
    688 				 * This is problematical; it's not clear
    689 				 * we need to wait for the sockbuf to be
    690 				 * unlocked (on a uniprocessor, at least),
    691 				 * and it's also not clear what to do
    692 				 * if sbwait returns an error due to receipt
    693 				 * of a signal.  If sbwait does return
    694 				 * an error, we'll go into an infinite
    695 				 * loop.  Delete all of this for now.
    696 				 */
    697 				(void) sbwait(&so->so_rcv);
    698 				goto restart;
    699 			}
    700 #endif
    701 			unp_scan(so->so_rcv.sb_mb, unp_mark);
    702 		}
    703 	} while (unp_defer);
    704 	for (fp = filehead; fp; fp = fp->f_filef) {
    705 		if (fp->f_count == 0)
    706 			continue;
    707 		if (fp->f_count == fp->f_msgcount && (fp->f_flag & FMARK) == 0)
    708 			while (fp->f_msgcount)
    709 				unp_discard(fp);
    710 	}
    711 	unp_gcing = 0;
    712 }
    713 
    714 unp_dispose(m)
    715 	struct mbuf *m;
    716 {
    717 	int unp_discard();
    718 
    719 	if (m)
    720 		unp_scan(m, unp_discard);
    721 }
    722 
    723 unp_scan(m0, op)
    724 	register struct mbuf *m0;
    725 	int (*op)();
    726 {
    727 	register struct mbuf *m;
    728 	register struct file **rp;
    729 	register struct cmsghdr *cm;
    730 	register int i;
    731 	int qfds;
    732 
    733 	while (m0) {
    734 		for (m = m0; m; m = m->m_next)
    735 			if (m->m_type == MT_CONTROL &&
    736 			    m->m_len >= sizeof(*cm)) {
    737 				cm = mtod(m, struct cmsghdr *);
    738 				if (cm->cmsg_level != SOL_SOCKET ||
    739 				    cm->cmsg_type != SCM_RIGHTS)
    740 					continue;
    741 				qfds = (cm->cmsg_len - sizeof *cm)
    742 						/ sizeof (struct file *);
    743 				rp = (struct file **)(cm + 1);
    744 				for (i = 0; i < qfds; i++)
    745 					(*op)(*rp++);
    746 				break;		/* XXX, but saves time */
    747 			}
    748 		m0 = m0->m_act;
    749 	}
    750 }
    751 
    752 unp_mark(fp)
    753 	struct file *fp;
    754 {
    755 
    756 	if (fp->f_flag & FMARK)
    757 		return;
    758 	unp_defer++;
    759 	fp->f_flag |= (FMARK|FDEFER);
    760 }
    761 
    762 unp_discard(fp)
    763 	struct file *fp;
    764 {
    765 
    766 	fp->f_msgcount--;
    767 	unp_rights--;
    768 	(void) closef(fp, curproc);
    769 }
    770