nlookup - introduce nlookup_init_root
[dragonfly.git] / sys / kern / uipc_usrreq.c
blobef89a2187c5bc3f256e89d9983b6fcc780108acd
1 /*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * From: @(#)uipc_usrreq.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/kern/uipc_usrreq.c,v 1.54.2.10 2003/03/04 17:28:09 nectar Exp $
35 * $DragonFly: src/sys/kern/uipc_usrreq.c,v 1.44 2008/09/06 05:44:58 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
44 #include <sys/proc.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/mbuf.h>
48 #include <sys/nlookup.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
56 #include <sys/un.h>
57 #include <sys/unpcb.h>
58 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
63 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct");
64 static unp_gen_t unp_gencnt;
65 static u_int unp_count;
67 static struct unp_head unp_shead, unp_dhead;
70 * Unix communications domain.
72 * TODO:
73 * RDM
74 * rethink name space problems
75 * need a proper out-of-band
76 * lock pushdown
78 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
79 static ino_t unp_ino = 1; /* prototype for fake inode numbers */
80 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin);
82 static int unp_attach (struct socket *, struct pru_attach_info *);
83 static void unp_detach (struct unpcb *);
84 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
85 static int unp_connect (struct socket *,struct sockaddr *,
86 struct thread *);
87 static void unp_disconnect (struct unpcb *);
88 static void unp_shutdown (struct unpcb *);
89 static void unp_drop (struct unpcb *, int);
90 static void unp_gc (void);
91 static int unp_gc_clearmarks(struct file *, void *);
92 static int unp_gc_checkmarks(struct file *, void *);
93 static int unp_gc_checkrefs(struct file *, void *);
94 static int unp_revoke_gc_check(struct file *, void *);
95 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
96 void *data);
97 static void unp_mark (struct file *, void *data);
98 static void unp_discard (struct file *, void *);
99 static int unp_internalize (struct mbuf *, struct thread *);
100 static int unp_listen (struct unpcb *, struct thread *);
101 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd);
103 static int
104 uipc_abort(struct socket *so)
106 struct unpcb *unp = so->so_pcb;
108 if (unp == NULL)
109 return EINVAL;
110 unp_drop(unp, ECONNABORTED);
111 unp_detach(unp);
112 sofree(so);
113 return 0;
116 static int
117 uipc_accept(struct socket *so, struct sockaddr **nam)
119 struct unpcb *unp = so->so_pcb;
121 if (unp == NULL)
122 return EINVAL;
125 * Pass back name of connected socket,
126 * if it was bound and we are still connected
127 * (our peer may have closed already!).
129 if (unp->unp_conn && unp->unp_conn->unp_addr) {
130 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
131 } else {
132 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
134 return 0;
137 static int
138 uipc_attach(struct socket *so, int proto, struct pru_attach_info *ai)
140 struct unpcb *unp = so->so_pcb;
142 if (unp != NULL)
143 return EISCONN;
144 return unp_attach(so, ai);
147 static int
148 uipc_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
150 struct unpcb *unp = so->so_pcb;
152 if (unp == NULL)
153 return EINVAL;
154 return unp_bind(unp, nam, td);
157 static int
158 uipc_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
160 struct unpcb *unp = so->so_pcb;
162 if (unp == NULL)
163 return EINVAL;
164 return unp_connect(so, nam, td);
167 static int
168 uipc_connect2(struct socket *so1, struct socket *so2)
170 struct unpcb *unp = so1->so_pcb;
172 if (unp == NULL)
173 return EINVAL;
175 return unp_connect2(so1, so2);
178 /* control is EOPNOTSUPP */
180 static int
181 uipc_detach(struct socket *so)
183 struct unpcb *unp = so->so_pcb;
185 if (unp == NULL)
186 return EINVAL;
188 unp_detach(unp);
189 return 0;
192 static int
193 uipc_disconnect(struct socket *so)
195 struct unpcb *unp = so->so_pcb;
197 if (unp == NULL)
198 return EINVAL;
199 unp_disconnect(unp);
200 return 0;
203 static int
204 uipc_listen(struct socket *so, struct thread *td)
206 struct unpcb *unp = so->so_pcb;
208 if (unp == NULL || unp->unp_vnode == NULL)
209 return EINVAL;
210 return unp_listen(unp, td);
213 static int
214 uipc_peeraddr(struct socket *so, struct sockaddr **nam)
216 struct unpcb *unp = so->so_pcb;
218 if (unp == NULL)
219 return EINVAL;
220 if (unp->unp_conn && unp->unp_conn->unp_addr)
221 *nam = dup_sockaddr((struct sockaddr *)unp->unp_conn->unp_addr);
222 else {
224 * XXX: It seems that this test always fails even when
225 * connection is established. So, this else clause is
226 * added as workaround to return PF_LOCAL sockaddr.
228 *nam = dup_sockaddr((struct sockaddr *)&sun_noname);
230 return 0;
233 static int
234 uipc_rcvd(struct socket *so, int flags)
236 struct unpcb *unp = so->so_pcb;
237 struct socket *so2;
239 if (unp == NULL)
240 return EINVAL;
241 switch (so->so_type) {
242 case SOCK_DGRAM:
243 panic("uipc_rcvd DGRAM?");
244 /*NOTREACHED*/
246 case SOCK_STREAM:
247 case SOCK_SEQPACKET:
248 if (unp->unp_conn == NULL)
249 break;
251 * Because we are transfering mbufs directly to the
252 * peer socket we have to use SSB_STOP on the sender
253 * to prevent it from building up infinite mbufs.
255 so2 = unp->unp_conn->unp_socket;
256 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat &&
257 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax
259 so2->so_snd.ssb_flags &= ~SSB_STOP;
260 sowwakeup(so2);
262 break;
264 default:
265 panic("uipc_rcvd unknown socktype");
267 return 0;
270 /* pru_rcvoob is EOPNOTSUPP */
272 static int
273 uipc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
274 struct mbuf *control, struct thread *td)
276 int error = 0;
277 struct unpcb *unp = so->so_pcb;
278 struct socket *so2;
280 if (unp == NULL) {
281 error = EINVAL;
282 goto release;
284 if (flags & PRUS_OOB) {
285 error = EOPNOTSUPP;
286 goto release;
289 if (control && (error = unp_internalize(control, td)))
290 goto release;
292 switch (so->so_type) {
293 case SOCK_DGRAM:
295 struct sockaddr *from;
297 if (nam) {
298 if (unp->unp_conn) {
299 error = EISCONN;
300 break;
302 error = unp_connect(so, nam, td);
303 if (error)
304 break;
305 } else {
306 if (unp->unp_conn == NULL) {
307 error = ENOTCONN;
308 break;
311 so2 = unp->unp_conn->unp_socket;
312 if (unp->unp_addr)
313 from = (struct sockaddr *)unp->unp_addr;
314 else
315 from = &sun_noname;
316 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
317 sorwakeup(so2);
318 m = NULL;
319 control = NULL;
320 } else {
321 error = ENOBUFS;
323 if (nam)
324 unp_disconnect(unp);
325 break;
328 case SOCK_STREAM:
329 case SOCK_SEQPACKET:
330 /* Connect if not connected yet. */
332 * Note: A better implementation would complain
333 * if not equal to the peer's address.
335 if (!(so->so_state & SS_ISCONNECTED)) {
336 if (nam) {
337 error = unp_connect(so, nam, td);
338 if (error)
339 break; /* XXX */
340 } else {
341 error = ENOTCONN;
342 break;
346 if (so->so_state & SS_CANTSENDMORE) {
347 error = EPIPE;
348 break;
350 if (unp->unp_conn == NULL)
351 panic("uipc_send connected but no connection?");
352 so2 = unp->unp_conn->unp_socket;
354 * Send to paired receive port, and then reduce
355 * send buffer hiwater marks to maintain backpressure.
356 * Wake up readers.
358 if (control) {
359 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
360 control = NULL;
361 m = NULL;
363 } else if (so->so_type == SOCK_SEQPACKET) {
364 sbappendrecord(&so2->so_rcv.sb, m);
365 m = NULL;
366 } else {
367 sbappend(&so2->so_rcv.sb, m);
368 m = NULL;
372 * Because we are transfering mbufs directly to the
373 * peer socket we have to use SSB_STOP on the sender
374 * to prevent it from building up infinite mbufs.
376 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat ||
377 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax
379 so->so_snd.ssb_flags |= SSB_STOP;
381 sorwakeup(so2);
382 break;
384 default:
385 panic("uipc_send unknown socktype");
389 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
391 if (flags & PRUS_EOF) {
392 socantsendmore(so);
393 unp_shutdown(unp);
396 if (control && error != 0)
397 unp_dispose(control);
399 release:
400 if (control)
401 m_freem(control);
402 if (m)
403 m_freem(m);
404 return error;
408 * MPSAFE
410 static int
411 uipc_sense(struct socket *so, struct stat *sb)
413 struct unpcb *unp = so->so_pcb;
415 if (unp == NULL)
416 return EINVAL;
417 sb->st_blksize = so->so_snd.ssb_hiwat;
418 sb->st_dev = NOUDEV;
419 if (unp->unp_ino == 0) { /* make up a non-zero inode number */
420 spin_lock_wr(&unp_ino_spin);
421 unp->unp_ino = unp_ino++;
422 spin_unlock_wr(&unp_ino_spin);
424 sb->st_ino = unp->unp_ino;
425 return (0);
428 static int
429 uipc_shutdown(struct socket *so)
431 struct unpcb *unp = so->so_pcb;
433 if (unp == NULL)
434 return EINVAL;
435 socantsendmore(so);
436 unp_shutdown(unp);
437 return 0;
440 static int
441 uipc_sockaddr(struct socket *so, struct sockaddr **nam)
443 struct unpcb *unp = so->so_pcb;
445 if (unp == NULL)
446 return EINVAL;
447 if (unp->unp_addr)
448 *nam = dup_sockaddr((struct sockaddr *)unp->unp_addr);
449 return 0;
452 struct pr_usrreqs uipc_usrreqs = {
453 .pru_abort = uipc_abort,
454 .pru_accept = uipc_accept,
455 .pru_attach = uipc_attach,
456 .pru_bind = uipc_bind,
457 .pru_connect = uipc_connect,
458 .pru_connect2 = uipc_connect2,
459 .pru_control = pru_control_notsupp,
460 .pru_detach = uipc_detach,
461 .pru_disconnect = uipc_disconnect,
462 .pru_listen = uipc_listen,
463 .pru_peeraddr = uipc_peeraddr,
464 .pru_rcvd = uipc_rcvd,
465 .pru_rcvoob = pru_rcvoob_notsupp,
466 .pru_send = uipc_send,
467 .pru_sense = uipc_sense,
468 .pru_shutdown = uipc_shutdown,
469 .pru_sockaddr = uipc_sockaddr,
470 .pru_sosend = sosend,
471 .pru_soreceive = soreceive,
472 .pru_sopoll = sopoll
476 uipc_ctloutput(struct socket *so, struct sockopt *sopt)
478 struct unpcb *unp = so->so_pcb;
479 int error = 0;
481 switch (sopt->sopt_dir) {
482 case SOPT_GET:
483 switch (sopt->sopt_name) {
484 case LOCAL_PEERCRED:
485 if (unp->unp_flags & UNP_HAVEPC)
486 soopt_from_kbuf(sopt, &unp->unp_peercred,
487 sizeof(unp->unp_peercred));
488 else {
489 if (so->so_type == SOCK_STREAM)
490 error = ENOTCONN;
491 else if (so->so_type == SOCK_SEQPACKET)
492 error = ENOTCONN;
493 else
494 error = EINVAL;
496 break;
497 default:
498 error = EOPNOTSUPP;
499 break;
501 break;
502 case SOPT_SET:
503 default:
504 error = EOPNOTSUPP;
505 break;
507 return (error);
511 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
512 * for stream sockets, although the total for sender and receiver is
513 * actually only PIPSIZ.
515 * Datagram sockets really use the sendspace as the maximum datagram size,
516 * and don't really want to reserve the sendspace. Their recvspace should
517 * be large enough for at least one max-size datagram plus address.
519 * We want the local send/recv space to be significant larger then lo0's
520 * mtu of 16384.
522 #ifndef PIPSIZ
523 #define PIPSIZ 57344
524 #endif
525 static u_long unpst_sendspace = PIPSIZ;
526 static u_long unpst_recvspace = PIPSIZ;
527 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
528 static u_long unpdg_recvspace = 4*1024;
530 static int unp_rights; /* file descriptors in flight */
531 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
533 SYSCTL_DECL(_net_local_seqpacket);
534 SYSCTL_DECL(_net_local_stream);
535 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
536 &unpst_sendspace, 0, "");
537 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
538 &unpst_recvspace, 0, "");
540 SYSCTL_DECL(_net_local_dgram);
541 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
542 &unpdg_sendspace, 0, "");
543 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
544 &unpdg_recvspace, 0, "");
546 SYSCTL_DECL(_net_local);
547 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0, "");
549 static int
550 unp_attach(struct socket *so, struct pru_attach_info *ai)
552 struct unpcb *unp;
553 int error;
555 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
556 switch (so->so_type) {
558 case SOCK_STREAM:
559 case SOCK_SEQPACKET:
560 error = soreserve(so, unpst_sendspace, unpst_recvspace,
561 ai->sb_rlimit);
562 break;
564 case SOCK_DGRAM:
565 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
566 ai->sb_rlimit);
567 break;
569 default:
570 panic("unp_attach");
572 if (error)
573 return (error);
575 unp = kmalloc(sizeof(*unp), M_UNPCB, M_NOWAIT|M_ZERO);
576 if (unp == NULL)
577 return (ENOBUFS);
578 unp->unp_gencnt = ++unp_gencnt;
579 unp_count++;
580 LIST_INIT(&unp->unp_refs);
581 unp->unp_socket = so;
582 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
583 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
584 : &unp_shead, unp, unp_link);
585 so->so_pcb = (caddr_t)unp;
586 so->so_port = sync_soport(so, NULL, NULL);
587 return (0);
590 static void
591 unp_detach(struct unpcb *unp)
593 LIST_REMOVE(unp, unp_link);
594 unp->unp_gencnt = ++unp_gencnt;
595 --unp_count;
596 if (unp->unp_vnode) {
597 unp->unp_vnode->v_socket = NULL;
598 vrele(unp->unp_vnode);
599 unp->unp_vnode = NULL;
601 if (unp->unp_conn)
602 unp_disconnect(unp);
603 while (!LIST_EMPTY(&unp->unp_refs))
604 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
605 soisdisconnected(unp->unp_socket);
606 unp->unp_socket->so_pcb = NULL;
607 if (unp_rights) {
609 * Normally the receive buffer is flushed later,
610 * in sofree, but if our receive buffer holds references
611 * to descriptors that are now garbage, we will dispose
612 * of those descriptor references after the garbage collector
613 * gets them (resulting in a "panic: closef: count < 0").
615 sorflush(unp->unp_socket);
616 unp_gc();
618 if (unp->unp_addr)
619 kfree(unp->unp_addr, M_SONAME);
620 kfree(unp, M_UNPCB);
623 static int
624 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
626 struct proc *p = td->td_proc;
627 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
628 struct vnode *vp;
629 struct vattr vattr;
630 int error, namelen;
631 struct nlookupdata nd;
632 char buf[SOCK_MAXADDRLEN];
634 if (unp->unp_vnode != NULL)
635 return (EINVAL);
636 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
637 if (namelen <= 0)
638 return (EINVAL);
639 strncpy(buf, soun->sun_path, namelen);
640 buf[namelen] = 0; /* null-terminate the string */
641 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
642 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
643 if (error == 0)
644 error = nlookup(&nd);
645 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
646 error = EADDRINUSE;
647 if (error)
648 goto done;
650 VATTR_NULL(&vattr);
651 vattr.va_type = VSOCK;
652 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
653 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
654 if (error == 0) {
655 vp->v_socket = unp->unp_socket;
656 unp->unp_vnode = vp;
657 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
658 vn_unlock(vp);
660 done:
661 nlookup_done(&nd);
662 return (error);
665 static int
666 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
668 struct proc *p = td->td_proc;
669 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
670 struct vnode *vp;
671 struct socket *so2, *so3;
672 struct unpcb *unp, *unp2, *unp3;
673 int error, len;
674 struct nlookupdata nd;
675 char buf[SOCK_MAXADDRLEN];
677 KKASSERT(p);
679 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
680 if (len <= 0)
681 return EINVAL;
682 strncpy(buf, soun->sun_path, len);
683 buf[len] = 0;
685 vp = NULL;
686 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
687 if (error == 0)
688 error = nlookup(&nd);
689 if (error == 0)
690 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
691 nlookup_done(&nd);
692 if (error)
693 return (error);
695 if (vp->v_type != VSOCK) {
696 error = ENOTSOCK;
697 goto bad;
699 error = VOP_ACCESS(vp, VWRITE, p->p_ucred);
700 if (error)
701 goto bad;
702 so2 = vp->v_socket;
703 if (so2 == NULL) {
704 error = ECONNREFUSED;
705 goto bad;
707 if (so->so_type != so2->so_type) {
708 error = EPROTOTYPE;
709 goto bad;
711 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
712 if (!(so2->so_options & SO_ACCEPTCONN) ||
713 (so3 = sonewconn(so2, 0)) == NULL) {
714 error = ECONNREFUSED;
715 goto bad;
717 unp = so->so_pcb;
718 unp2 = so2->so_pcb;
719 unp3 = so3->so_pcb;
720 if (unp2->unp_addr)
721 unp3->unp_addr = (struct sockaddr_un *)
722 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
725 * unp_peercred management:
727 * The connecter's (client's) credentials are copied
728 * from its process structure at the time of connect()
729 * (which is now).
731 cru2x(p->p_ucred, &unp3->unp_peercred);
732 unp3->unp_flags |= UNP_HAVEPC;
734 * The receiver's (server's) credentials are copied
735 * from the unp_peercred member of socket on which the
736 * former called listen(); unp_listen() cached that
737 * process's credentials at that time so we can use
738 * them now.
740 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
741 ("unp_connect: listener without cached peercred"));
742 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
743 sizeof(unp->unp_peercred));
744 unp->unp_flags |= UNP_HAVEPC;
746 so2 = so3;
748 error = unp_connect2(so, so2);
749 bad:
750 vput(vp);
751 return (error);
755 unp_connect2(struct socket *so, struct socket *so2)
757 struct unpcb *unp = so->so_pcb;
758 struct unpcb *unp2;
760 if (so2->so_type != so->so_type)
761 return (EPROTOTYPE);
762 unp2 = so2->so_pcb;
763 unp->unp_conn = unp2;
764 switch (so->so_type) {
766 case SOCK_DGRAM:
767 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
768 soisconnected(so);
769 break;
771 case SOCK_STREAM:
772 case SOCK_SEQPACKET:
773 unp2->unp_conn = unp;
774 soisconnected(so);
775 soisconnected(so2);
776 break;
778 default:
779 panic("unp_connect2");
781 return (0);
784 static void
785 unp_disconnect(struct unpcb *unp)
787 struct unpcb *unp2 = unp->unp_conn;
789 if (unp2 == NULL)
790 return;
792 unp->unp_conn = NULL;
794 switch (unp->unp_socket->so_type) {
795 case SOCK_DGRAM:
796 LIST_REMOVE(unp, unp_reflink);
797 unp->unp_socket->so_state &= ~SS_ISCONNECTED;
798 break;
799 case SOCK_STREAM:
800 case SOCK_SEQPACKET:
801 soisdisconnected(unp->unp_socket);
802 unp2->unp_conn = NULL;
803 soisdisconnected(unp2->unp_socket);
804 break;
808 #ifdef notdef
809 void
810 unp_abort(struct unpcb *unp)
813 unp_detach(unp);
815 #endif
817 static int
818 prison_unpcb(struct thread *td, struct unpcb *unp)
820 struct proc *p;
822 if (td == NULL)
823 return (0);
824 if ((p = td->td_proc) == NULL)
825 return (0);
826 if (!p->p_ucred->cr_prison)
827 return (0);
828 if (p->p_fd->fd_rdir == unp->unp_rvnode)
829 return (0);
830 return (1);
833 static int
834 unp_pcblist(SYSCTL_HANDLER_ARGS)
836 int error, i, n;
837 struct unpcb *unp, **unp_list;
838 unp_gen_t gencnt;
839 struct unp_head *head;
841 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
843 KKASSERT(curproc != NULL);
846 * The process of preparing the PCB list is too time-consuming and
847 * resource-intensive to repeat twice on every request.
849 if (req->oldptr == NULL) {
850 n = unp_count;
851 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
852 return 0;
855 if (req->newptr != NULL)
856 return EPERM;
859 * OK, now we're committed to doing something.
861 gencnt = unp_gencnt;
862 n = unp_count;
864 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
866 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
867 unp = LIST_NEXT(unp, unp_link)) {
868 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
869 unp_list[i++] = unp;
871 n = i; /* in case we lost some during malloc */
873 error = 0;
874 for (i = 0; i < n; i++) {
875 unp = unp_list[i];
876 if (unp->unp_gencnt <= gencnt) {
877 struct xunpcb xu;
878 xu.xu_len = sizeof xu;
879 xu.xu_unpp = unp;
881 * XXX - need more locking here to protect against
882 * connect/disconnect races for SMP.
884 if (unp->unp_addr)
885 bcopy(unp->unp_addr, &xu.xu_addr,
886 unp->unp_addr->sun_len);
887 if (unp->unp_conn && unp->unp_conn->unp_addr)
888 bcopy(unp->unp_conn->unp_addr,
889 &xu.xu_caddr,
890 unp->unp_conn->unp_addr->sun_len);
891 bcopy(unp, &xu.xu_unp, sizeof *unp);
892 sotoxsocket(unp->unp_socket, &xu.xu_socket);
893 error = SYSCTL_OUT(req, &xu, sizeof xu);
896 kfree(unp_list, M_TEMP);
897 return error;
900 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
901 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
902 "List of active local datagram sockets");
903 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
904 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
905 "List of active local stream sockets");
906 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD,
907 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
908 "List of active local seqpacket stream sockets");
910 static void
911 unp_shutdown(struct unpcb *unp)
913 struct socket *so;
915 if ((unp->unp_socket->so_type == SOCK_STREAM ||
916 unp->unp_socket->so_type == SOCK_SEQPACKET) &&
917 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) {
918 socantrcvmore(so);
922 static void
923 unp_drop(struct unpcb *unp, int err)
925 struct socket *so = unp->unp_socket;
927 so->so_error = err;
928 unp_disconnect(unp);
931 #ifdef notdef
932 void
933 unp_drain(void)
937 #endif
940 unp_externalize(struct mbuf *rights)
942 struct thread *td = curthread;
943 struct proc *p = td->td_proc; /* XXX */
944 struct lwp *lp = td->td_lwp;
945 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
946 int *fdp;
947 int i;
948 struct file **rp;
949 struct file *fp;
950 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
951 / sizeof (struct file *);
952 int f;
955 * if the new FD's will not fit, then we free them all
957 if (!fdavail(p, newfds)) {
958 rp = (struct file **)CMSG_DATA(cm);
959 for (i = 0; i < newfds; i++) {
960 fp = *rp;
962 * zero the pointer before calling unp_discard,
963 * since it may end up in unp_gc()..
965 *rp++ = 0;
966 unp_discard(fp, NULL);
968 return (EMSGSIZE);
972 * now change each pointer to an fd in the global table to
973 * an integer that is the index to the local fd table entry
974 * that we set up to point to the global one we are transferring.
975 * If sizeof (struct file *) is bigger than or equal to sizeof int,
976 * then do it in forward order. In that case, an integer will
977 * always come in the same place or before its corresponding
978 * struct file pointer.
979 * If sizeof (struct file *) is smaller than sizeof int, then
980 * do it in reverse order.
982 if (sizeof (struct file *) >= sizeof (int)) {
983 fdp = (int *)CMSG_DATA(cm);
984 rp = (struct file **)CMSG_DATA(cm);
985 for (i = 0; i < newfds; i++) {
986 if (fdalloc(p, 0, &f))
987 panic("unp_externalize");
988 fp = *rp++;
989 unp_fp_externalize(lp, fp, f);
990 *fdp++ = f;
992 } else {
993 fdp = (int *)CMSG_DATA(cm) + newfds - 1;
994 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
995 for (i = 0; i < newfds; i++) {
996 if (fdalloc(p, 0, &f))
997 panic("unp_externalize");
998 fp = *rp--;
999 unp_fp_externalize(lp, fp, f);
1000 *fdp-- = f;
1005 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1006 * differs.
1008 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1009 rights->m_len = cm->cmsg_len;
1010 return (0);
1013 static void
1014 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd)
1016 struct file *fx;
1017 int error;
1019 if (lp) {
1020 KKASSERT(fd >= 0);
1021 if (fp->f_flag & FREVOKED) {
1022 kprintf("Warning: revoked fp exiting unix socket\n");
1023 fx = NULL;
1024 error = falloc(lp, &fx, NULL);
1025 if (error == 0)
1026 fsetfd(lp->lwp_proc->p_fd, fx, fd);
1027 else
1028 fsetfd(lp->lwp_proc->p_fd, NULL, fd);
1029 fdrop(fx);
1030 } else {
1031 fsetfd(lp->lwp_proc->p_fd, fp, fd);
1034 spin_lock_wr(&unp_spin);
1035 fp->f_msgcount--;
1036 unp_rights--;
1037 spin_unlock_wr(&unp_spin);
1038 fdrop(fp);
1042 void
1043 unp_init(void)
1045 LIST_INIT(&unp_dhead);
1046 LIST_INIT(&unp_shead);
1047 spin_init(&unp_spin);
1050 static int
1051 unp_internalize(struct mbuf *control, struct thread *td)
1053 struct proc *p = td->td_proc;
1054 struct filedesc *fdescp;
1055 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1056 struct file **rp;
1057 struct file *fp;
1058 int i, fd, *fdp;
1059 struct cmsgcred *cmcred;
1060 int oldfds;
1061 u_int newlen;
1063 KKASSERT(p);
1064 fdescp = p->p_fd;
1065 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1066 cm->cmsg_level != SOL_SOCKET ||
1067 CMSG_ALIGN(cm->cmsg_len) != control->m_len) {
1068 return (EINVAL);
1072 * Fill in credential information.
1074 if (cm->cmsg_type == SCM_CREDS) {
1075 cmcred = (struct cmsgcred *)CMSG_DATA(cm);
1076 cmcred->cmcred_pid = p->p_pid;
1077 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1078 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1079 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1080 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1081 CMGROUP_MAX);
1082 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1083 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1084 return(0);
1088 * cmsghdr may not be aligned, do not allow calculation(s) to
1089 * go negative.
1091 if (cm->cmsg_len < CMSG_LEN(0))
1092 return(EINVAL);
1094 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int);
1097 * check that all the FDs passed in refer to legal OPEN files
1098 * If not, reject the entire operation.
1100 fdp = (int *)CMSG_DATA(cm);
1101 for (i = 0; i < oldfds; i++) {
1102 fd = *fdp++;
1103 if ((unsigned)fd >= fdescp->fd_nfiles ||
1104 fdescp->fd_files[fd].fp == NULL)
1105 return (EBADF);
1106 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE)
1107 return (EOPNOTSUPP);
1110 * Now replace the integer FDs with pointers to
1111 * the associated global file table entry..
1112 * Allocate a bigger buffer as necessary. But if an cluster is not
1113 * enough, return E2BIG.
1115 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1116 if (newlen > MCLBYTES)
1117 return (E2BIG);
1118 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1119 if (control->m_flags & M_EXT)
1120 return (E2BIG);
1121 MCLGET(control, MB_WAIT);
1122 if (!(control->m_flags & M_EXT))
1123 return (ENOBUFS);
1125 /* copy the data to the cluster */
1126 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1127 cm = mtod(control, struct cmsghdr *);
1131 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1132 * differs.
1134 cm->cmsg_len = newlen;
1135 control->m_len = CMSG_ALIGN(newlen);
1138 * Transform the file descriptors into struct file pointers.
1139 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1140 * then do it in reverse order so that the int won't get until
1141 * we're done.
1142 * If sizeof (struct file *) is smaller than sizeof int, then
1143 * do it in forward order.
1145 if (sizeof (struct file *) >= sizeof (int)) {
1146 fdp = (int *)CMSG_DATA(cm) + oldfds - 1;
1147 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1148 for (i = 0; i < oldfds; i++) {
1149 fp = fdescp->fd_files[*fdp--].fp;
1150 *rp-- = fp;
1151 fhold(fp);
1152 spin_lock_wr(&unp_spin);
1153 fp->f_msgcount++;
1154 unp_rights++;
1155 spin_unlock_wr(&unp_spin);
1157 } else {
1158 fdp = (int *)CMSG_DATA(cm);
1159 rp = (struct file **)CMSG_DATA(cm);
1160 for (i = 0; i < oldfds; i++) {
1161 fp = fdescp->fd_files[*fdp++].fp;
1162 *rp++ = fp;
1163 fhold(fp);
1164 spin_lock_wr(&unp_spin);
1165 fp->f_msgcount++;
1166 unp_rights++;
1167 spin_unlock_wr(&unp_spin);
1170 return (0);
1174 * Garbage collect in-transit file descriptors that get lost due to
1175 * loops (i.e. when a socket is sent to another process over itself,
1176 * and more complex situations).
1178 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1181 struct unp_gc_info {
1182 struct file **extra_ref;
1183 struct file *locked_fp;
1184 int defer;
1185 int index;
1186 int maxindex;
1189 static void
1190 unp_gc(void)
1192 struct unp_gc_info info;
1193 static boolean_t unp_gcing;
1194 struct file **fpp;
1195 int i;
1197 spin_lock_wr(&unp_spin);
1198 if (unp_gcing) {
1199 spin_unlock_wr(&unp_spin);
1200 return;
1202 unp_gcing = TRUE;
1203 spin_unlock_wr(&unp_spin);
1206 * before going through all this, set all FDs to
1207 * be NOT defered and NOT externally accessible
1209 info.defer = 0;
1210 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1211 do {
1212 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1213 } while (info.defer);
1216 * We grab an extra reference to each of the file table entries
1217 * that are not otherwise accessible and then free the rights
1218 * that are stored in messages on them.
1220 * The bug in the orginal code is a little tricky, so I'll describe
1221 * what's wrong with it here.
1223 * It is incorrect to simply unp_discard each entry for f_msgcount
1224 * times -- consider the case of sockets A and B that contain
1225 * references to each other. On a last close of some other socket,
1226 * we trigger a gc since the number of outstanding rights (unp_rights)
1227 * is non-zero. If during the sweep phase the gc code un_discards,
1228 * we end up doing a (full) closef on the descriptor. A closef on A
1229 * results in the following chain. Closef calls soo_close, which
1230 * calls soclose. Soclose calls first (through the switch
1231 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1232 * returns because the previous instance had set unp_gcing, and
1233 * we return all the way back to soclose, which marks the socket
1234 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1235 * to free up the rights that are queued in messages on the socket A,
1236 * i.e., the reference on B. The sorflush calls via the dom_dispose
1237 * switch unp_dispose, which unp_scans with unp_discard. This second
1238 * instance of unp_discard just calls closef on B.
1240 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1241 * which results in another closef on A. Unfortunately, A is already
1242 * being closed, and the descriptor has already been marked with
1243 * SS_NOFDREF, and soclose panics at this point.
1245 * Here, we first take an extra reference to each inaccessible
1246 * descriptor. Then, we call sorflush ourself, since we know
1247 * it is a Unix domain socket anyhow. After we destroy all the
1248 * rights carried in messages, we do a last closef to get rid
1249 * of our extra reference. This is the last close, and the
1250 * unp_detach etc will shut down the socket.
1252 * 91/09/19, bsy@cs.cmu.edu
1254 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1255 info.maxindex = 256;
1257 do {
1259 * Look for matches
1261 info.index = 0;
1262 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1265 * For each FD on our hit list, do the following two things
1267 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1268 struct file *tfp = *fpp;
1269 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1270 sorflush((struct socket *)(tfp->f_data));
1272 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1273 closef(*fpp, NULL);
1274 } while (info.index == info.maxindex);
1275 kfree((caddr_t)info.extra_ref, M_FILE);
1276 unp_gcing = FALSE;
1280 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1282 static int
1283 unp_gc_checkrefs(struct file *fp, void *data)
1285 struct unp_gc_info *info = data;
1287 if (fp->f_count == 0)
1288 return(0);
1289 if (info->index == info->maxindex)
1290 return(-1);
1293 * If all refs are from msgs, and it's not marked accessible
1294 * then it must be referenced from some unreachable cycle
1295 * of (shut-down) FDs, so include it in our
1296 * list of FDs to remove
1298 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1299 info->extra_ref[info->index++] = fp;
1300 fhold(fp);
1302 return(0);
1306 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1308 static int
1309 unp_gc_clearmarks(struct file *fp, void *data __unused)
1311 atomic_clear_int(&fp->f_flag, FMARK | FDEFER);
1312 return(0);
1316 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1318 static int
1319 unp_gc_checkmarks(struct file *fp, void *data)
1321 struct unp_gc_info *info = data;
1322 struct socket *so;
1325 * If the file is not open, skip it
1327 if (fp->f_count == 0)
1328 return(0);
1330 * If we already marked it as 'defer' in a
1331 * previous pass, then try process it this time
1332 * and un-mark it
1334 if (fp->f_flag & FDEFER) {
1335 atomic_clear_int(&fp->f_flag, FDEFER);
1336 --info->defer;
1337 } else {
1339 * if it's not defered, then check if it's
1340 * already marked.. if so skip it
1342 if (fp->f_flag & FMARK)
1343 return(0);
1345 * If all references are from messages
1346 * in transit, then skip it. it's not
1347 * externally accessible.
1349 if (fp->f_count == fp->f_msgcount)
1350 return(0);
1352 * If it got this far then it must be
1353 * externally accessible.
1355 atomic_set_int(&fp->f_flag, FMARK);
1359 * either it was defered, or it is externally
1360 * accessible and not already marked so.
1361 * Now check if it is possibly one of OUR sockets.
1363 if (fp->f_type != DTYPE_SOCKET ||
1364 (so = (struct socket *)fp->f_data) == NULL)
1365 return(0);
1366 if (so->so_proto->pr_domain != &localdomain ||
1367 !(so->so_proto->pr_flags & PR_RIGHTS))
1368 return(0);
1369 #ifdef notdef
1370 if (so->so_rcv.ssb_flags & SSB_LOCK) {
1372 * This is problematical; it's not clear
1373 * we need to wait for the sockbuf to be
1374 * unlocked (on a uniprocessor, at least),
1375 * and it's also not clear what to do
1376 * if sbwait returns an error due to receipt
1377 * of a signal. If sbwait does return
1378 * an error, we'll go into an infinite
1379 * loop. Delete all of this for now.
1381 sbwait(&so->so_rcv);
1382 goto restart;
1384 #endif
1386 * So, Ok, it's one of our sockets and it IS externally
1387 * accessible (or was defered). Now we look
1388 * to see if we hold any file descriptors in its
1389 * message buffers. Follow those links and mark them
1390 * as accessible too.
1392 info->locked_fp = fp;
1393 /* spin_lock_wr(&so->so_rcv.sb_spin); */
1394 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1395 /* spin_unlock_wr(&so->so_rcv.sb_spin);*/
1396 return (0);
1400 * Scan all unix domain sockets and replace any revoked file pointers
1401 * found with the dummy file pointer fx. We don't worry about races
1402 * against file pointers being read out as those are handled in the
1403 * externalize code.
1406 #define REVOKE_GC_MAXFILES 32
1408 struct unp_revoke_gc_info {
1409 struct file *fx;
1410 struct file *fary[REVOKE_GC_MAXFILES];
1411 int fcount;
1414 void
1415 unp_revoke_gc(struct file *fx)
1417 struct unp_revoke_gc_info info;
1418 int i;
1420 info.fx = fx;
1421 do {
1422 info.fcount = 0;
1423 allfiles_scan_exclusive(unp_revoke_gc_check, &info);
1424 for (i = 0; i < info.fcount; ++i)
1425 unp_fp_externalize(NULL, info.fary[i], -1);
1426 } while (info.fcount == REVOKE_GC_MAXFILES);
1430 * Check for and replace revoked descriptors.
1432 * WARNING: This routine is not allowed to block.
1434 static int
1435 unp_revoke_gc_check(struct file *fps, void *vinfo)
1437 struct unp_revoke_gc_info *info = vinfo;
1438 struct file *fp;
1439 struct socket *so;
1440 struct mbuf *m0;
1441 struct mbuf *m;
1442 struct file **rp;
1443 struct cmsghdr *cm;
1444 int i;
1445 int qfds;
1448 * Is this a unix domain socket with rights-passing abilities?
1450 if (fps->f_type != DTYPE_SOCKET)
1451 return (0);
1452 if ((so = (struct socket *)fps->f_data) == NULL)
1453 return(0);
1454 if (so->so_proto->pr_domain != &localdomain)
1455 return(0);
1456 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0)
1457 return(0);
1460 * Scan the mbufs for control messages and replace any revoked
1461 * descriptors we find.
1463 m0 = so->so_rcv.ssb_mb;
1464 while (m0) {
1465 for (m = m0; m; m = m->m_next) {
1466 if (m->m_type != MT_CONTROL)
1467 continue;
1468 if (m->m_len < sizeof(*cm))
1469 continue;
1470 cm = mtod(m, struct cmsghdr *);
1471 if (cm->cmsg_level != SOL_SOCKET ||
1472 cm->cmsg_type != SCM_RIGHTS) {
1473 continue;
1475 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *);
1476 rp = (struct file **)CMSG_DATA(cm);
1477 for (i = 0; i < qfds; i++) {
1478 fp = rp[i];
1479 if (fp->f_flag & FREVOKED) {
1480 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1481 fhold(info->fx);
1482 info->fx->f_msgcount++;
1483 unp_rights++;
1484 rp[i] = info->fx;
1485 info->fary[info->fcount++] = fp;
1487 if (info->fcount == REVOKE_GC_MAXFILES)
1488 break;
1490 if (info->fcount == REVOKE_GC_MAXFILES)
1491 break;
1493 m0 = m0->m_nextpkt;
1494 if (info->fcount == REVOKE_GC_MAXFILES)
1495 break;
1499 * Stop the scan if we filled up our array.
1501 if (info->fcount == REVOKE_GC_MAXFILES)
1502 return(-1);
1503 return(0);
1506 void
1507 unp_dispose(struct mbuf *m)
1509 if (m)
1510 unp_scan(m, unp_discard, NULL);
1513 static int
1514 unp_listen(struct unpcb *unp, struct thread *td)
1516 struct proc *p = td->td_proc;
1518 KKASSERT(p);
1519 cru2x(p->p_ucred, &unp->unp_peercred);
1520 unp->unp_flags |= UNP_HAVEPCCACHED;
1521 return (0);
1524 static void
1525 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1527 struct mbuf *m;
1528 struct file **rp;
1529 struct cmsghdr *cm;
1530 int i;
1531 int qfds;
1533 while (m0) {
1534 for (m = m0; m; m = m->m_next) {
1535 if (m->m_type == MT_CONTROL &&
1536 m->m_len >= sizeof(*cm)) {
1537 cm = mtod(m, struct cmsghdr *);
1538 if (cm->cmsg_level != SOL_SOCKET ||
1539 cm->cmsg_type != SCM_RIGHTS)
1540 continue;
1541 qfds = (cm->cmsg_len - CMSG_LEN(0)) /
1542 sizeof(void *);
1543 rp = (struct file **)CMSG_DATA(cm);
1544 for (i = 0; i < qfds; i++)
1545 (*op)(*rp++, data);
1546 break; /* XXX, but saves time */
1549 m0 = m0->m_nextpkt;
1553 static void
1554 unp_mark(struct file *fp, void *data)
1556 struct unp_gc_info *info = data;
1558 if ((fp->f_flag & FMARK) == 0) {
1559 ++info->defer;
1560 atomic_set_int(&fp->f_flag, FMARK | FDEFER);
1564 static void
1565 unp_discard(struct file *fp, void *data __unused)
1567 spin_lock_wr(&unp_spin);
1568 fp->f_msgcount--;
1569 unp_rights--;
1570 spin_unlock_wr(&unp_spin);
1571 closef(fp, NULL);