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