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
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
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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/domain.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h> /* XXX must be before <sys/file.h> */
45 #include <sys/filedesc.h>
47 #include <sys/nlookup.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/resourcevar.h>
53 #include <sys/mount.h>
54 #include <sys/sysctl.h>
56 #include <sys/unpcb.h>
57 #include <sys/vnode.h>
59 #include <sys/file2.h>
60 #include <sys/spinlock2.h>
61 #include <sys/socketvar2.h>
62 #include <sys/msgport2.h>
64 typedef struct unp_defdiscard {
65 struct unp_defdiscard *next;
69 static MALLOC_DEFINE(M_UNPCB, "unpcb", "unpcb struct");
70 static unp_gen_t unp_gencnt;
71 static u_int unp_count;
73 static struct unp_head unp_shead, unp_dhead;
75 static struct lwkt_token unp_token = LWKT_TOKEN_INITIALIZER(unp_token);
76 static int unp_defdiscard_nest;
77 static unp_defdiscard_t unp_defdiscard_base;
80 * Unix communications domain.
84 * rethink name space problems
85 * need a proper out-of-band
88 static struct sockaddr sun_noname = { sizeof(sun_noname), AF_LOCAL };
89 static ino_t unp_ino = 1; /* prototype for fake inode numbers */
90 static struct spinlock unp_ino_spin = SPINLOCK_INITIALIZER(&unp_ino_spin);
92 static int unp_attach (struct socket *, struct pru_attach_info *);
93 static void unp_detach (struct unpcb *);
94 static int unp_bind (struct unpcb *,struct sockaddr *, struct thread *);
95 static int unp_connect (struct socket *,struct sockaddr *,
97 static void unp_disconnect (struct unpcb *);
98 static void unp_shutdown (struct unpcb *);
99 static void unp_drop (struct unpcb *, int);
100 static void unp_gc (void);
101 static int unp_gc_clearmarks(struct file *, void *);
102 static int unp_gc_checkmarks(struct file *, void *);
103 static int unp_gc_checkrefs(struct file *, void *);
104 static int unp_revoke_gc_check(struct file *, void *);
105 static void unp_scan (struct mbuf *, void (*)(struct file *, void *),
107 static void unp_mark (struct file *, void *data);
108 static void unp_discard (struct file *, void *);
109 static int unp_internalize (struct mbuf *, struct thread *);
110 static int unp_listen (struct unpcb *, struct thread *);
111 static void unp_fp_externalize(struct lwp *lp, struct file *fp, int fd);
114 * SMP Considerations:
116 * Since unp_token will be automaticly released upon execution of
117 * blocking code, we need to reference unp_conn before any possible
118 * blocking code to prevent it from being ripped behind our back.
120 * Any adjustment to unp->unp_conn requires both the global unp_token
121 * AND the per-unp token (lwkt_token_pool_lookup(unp)) to be held.
123 * Any access to so_pcb to obtain unp requires the pool token for
127 /* NOTE: unp_token MUST be held */
129 unp_reference(struct unpcb *unp)
131 atomic_add_int(&unp->unp_refcnt, 1);
134 /* NOTE: unp_token MUST be held */
136 unp_free(struct unpcb *unp)
138 KKASSERT(unp->unp_refcnt > 0);
139 if (atomic_fetchadd_int(&unp->unp_refcnt, -1) == 1)
144 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
145 * will sofree() it when we return.
148 uipc_abort(netmsg_t msg)
153 lwkt_gettoken(&unp_token);
154 unp = msg->base.nm_so->so_pcb;
156 unp_drop(unp, ECONNABORTED);
162 lwkt_reltoken(&unp_token);
164 lwkt_replymsg(&msg->lmsg, error);
168 uipc_accept(netmsg_t msg)
173 lwkt_gettoken(&unp_token);
174 unp = msg->base.nm_so->so_pcb;
178 struct unpcb *unp2 = unp->unp_conn;
181 * Pass back name of connected socket,
182 * if it was bound and we are still connected
183 * (our peer may have closed already!).
185 if (unp2 && unp2->unp_addr) {
187 *msg->accept.nm_nam = dup_sockaddr(
188 (struct sockaddr *)unp2->unp_addr);
191 *msg->accept.nm_nam = dup_sockaddr(&sun_noname);
195 lwkt_reltoken(&unp_token);
196 lwkt_replymsg(&msg->lmsg, error);
200 uipc_attach(netmsg_t msg)
205 lwkt_gettoken(&unp_token);
206 unp = msg->base.nm_so->so_pcb;
210 error = unp_attach(msg->base.nm_so, msg->attach.nm_ai);
211 lwkt_reltoken(&unp_token);
212 lwkt_replymsg(&msg->lmsg, error);
216 uipc_bind(netmsg_t msg)
221 lwkt_gettoken(&unp_token);
222 unp = msg->base.nm_so->so_pcb;
224 error = unp_bind(unp, msg->bind.nm_nam, msg->bind.nm_td);
227 lwkt_reltoken(&unp_token);
228 lwkt_replymsg(&msg->lmsg, error);
232 uipc_connect(netmsg_t msg)
237 unp = msg->base.nm_so->so_pcb;
239 error = unp_connect(msg->base.nm_so,
245 lwkt_replymsg(&msg->lmsg, error);
249 uipc_connect2(netmsg_t msg)
254 unp = msg->connect2.nm_so1->so_pcb;
256 error = unp_connect2(msg->connect2.nm_so1,
257 msg->connect2.nm_so2);
261 lwkt_replymsg(&msg->lmsg, error);
264 /* control is EOPNOTSUPP */
267 uipc_detach(netmsg_t msg)
272 lwkt_gettoken(&unp_token);
273 unp = msg->base.nm_so->so_pcb;
280 lwkt_reltoken(&unp_token);
281 lwkt_replymsg(&msg->lmsg, error);
285 uipc_disconnect(netmsg_t msg)
290 lwkt_gettoken(&unp_token);
291 unp = msg->base.nm_so->so_pcb;
298 lwkt_reltoken(&unp_token);
299 lwkt_replymsg(&msg->lmsg, error);
303 uipc_listen(netmsg_t msg)
308 lwkt_gettoken(&unp_token);
309 unp = msg->base.nm_so->so_pcb;
310 if (unp == NULL || unp->unp_vnode == NULL)
313 error = unp_listen(unp, msg->listen.nm_td);
314 lwkt_reltoken(&unp_token);
315 lwkt_replymsg(&msg->lmsg, error);
319 uipc_peeraddr(netmsg_t msg)
324 lwkt_gettoken(&unp_token);
325 unp = msg->base.nm_so->so_pcb;
328 } else if (unp->unp_conn && unp->unp_conn->unp_addr) {
329 struct unpcb *unp2 = unp->unp_conn;
332 *msg->peeraddr.nm_nam = dup_sockaddr(
333 (struct sockaddr *)unp2->unp_addr);
338 * XXX: It seems that this test always fails even when
339 * connection is established. So, this else clause is
340 * added as workaround to return PF_LOCAL sockaddr.
342 *msg->peeraddr.nm_nam = dup_sockaddr(&sun_noname);
345 lwkt_reltoken(&unp_token);
346 lwkt_replymsg(&msg->lmsg, error);
350 uipc_rcvd(netmsg_t msg)
352 struct unpcb *unp, *unp2;
358 * so_pcb is only modified with both the global and the unp
359 * pool token held. The unp pointer is invalid until we verify
360 * that it is good by re-checking so_pcb AFTER obtaining the token.
362 so = msg->base.nm_so;
363 while ((unp = so->so_pcb) != NULL) {
364 lwkt_getpooltoken(unp);
365 if (unp == so->so_pcb)
367 lwkt_relpooltoken(unp);
373 /* pool token held */
375 switch (so->so_type) {
377 panic("uipc_rcvd DGRAM?");
381 if (unp->unp_conn == NULL)
383 unp2 = unp->unp_conn; /* protected by pool token */
386 * Because we are transfering mbufs directly to the
387 * peer socket we have to use SSB_STOP on the sender
388 * to prevent it from building up infinite mbufs.
390 * As in several places in this module w ehave to ref unp2
391 * to ensure that it does not get ripped out from under us
392 * if we block on the so2 token or in sowwakeup().
394 so2 = unp2->unp_socket;
396 lwkt_gettoken(&so2->so_rcv.ssb_token);
397 if (so->so_rcv.ssb_cc < so2->so_snd.ssb_hiwat &&
398 so->so_rcv.ssb_mbcnt < so2->so_snd.ssb_mbmax
400 atomic_clear_int(&so2->so_snd.ssb_flags, SSB_STOP);
404 lwkt_reltoken(&so2->so_rcv.ssb_token);
408 panic("uipc_rcvd unknown socktype");
412 lwkt_relpooltoken(unp);
414 lwkt_replymsg(&msg->lmsg, error);
417 /* pru_rcvoob is EOPNOTSUPP */
420 uipc_send(netmsg_t msg)
422 struct unpcb *unp, *unp2;
425 struct mbuf *control;
429 so = msg->base.nm_so;
430 control = msg->send.nm_control;
434 * so_pcb is only modified with both the global and the unp
435 * pool token held. The unp pointer is invalid until we verify
436 * that it is good by re-checking so_pcb AFTER obtaining the token.
438 so = msg->base.nm_so;
439 while ((unp = so->so_pcb) != NULL) {
440 lwkt_getpooltoken(unp);
441 if (unp == so->so_pcb)
443 lwkt_relpooltoken(unp);
449 /* pool token held */
451 if (msg->send.nm_flags & PRUS_OOB) {
456 if (control && (error = unp_internalize(control, msg->send.nm_td)))
459 switch (so->so_type) {
462 struct sockaddr *from;
464 if (msg->send.nm_addr) {
469 error = unp_connect(so,
475 if (unp->unp_conn == NULL) {
480 unp2 = unp->unp_conn;
481 so2 = unp2->unp_socket;
483 from = (struct sockaddr *)unp->unp_addr;
489 lwkt_gettoken(&so2->so_rcv.ssb_token);
490 if (ssb_appendaddr(&so2->so_rcv, from, m, control)) {
497 if (msg->send.nm_addr)
499 lwkt_reltoken(&so2->so_rcv.ssb_token);
507 /* Connect if not connected yet. */
509 * Note: A better implementation would complain
510 * if not equal to the peer's address.
512 if (!(so->so_state & SS_ISCONNECTED)) {
513 if (msg->send.nm_addr) {
514 error = unp_connect(so,
525 if (so->so_state & SS_CANTSENDMORE) {
529 if (unp->unp_conn == NULL)
530 panic("uipc_send connected but no connection?");
531 unp2 = unp->unp_conn;
532 so2 = unp2->unp_socket;
537 * Send to paired receive port, and then reduce
538 * send buffer hiwater marks to maintain backpressure.
541 lwkt_gettoken(&so2->so_rcv.ssb_token);
543 if (ssb_appendcontrol(&so2->so_rcv, m, control)) {
547 } else if (so->so_type == SOCK_SEQPACKET) {
548 sbappendrecord(&so2->so_rcv.sb, m);
551 sbappend(&so2->so_rcv.sb, m);
556 * Because we are transfering mbufs directly to the
557 * peer socket we have to use SSB_STOP on the sender
558 * to prevent it from building up infinite mbufs.
560 if (so2->so_rcv.ssb_cc >= so->so_snd.ssb_hiwat ||
561 so2->so_rcv.ssb_mbcnt >= so->so_snd.ssb_mbmax
563 atomic_set_int(&so->so_snd.ssb_flags, SSB_STOP);
565 lwkt_reltoken(&so2->so_rcv.ssb_token);
572 panic("uipc_send unknown socktype");
576 * SEND_EOF is equivalent to a SEND followed by a SHUTDOWN.
578 if (msg->send.nm_flags & PRUS_EOF) {
583 if (control && error != 0)
584 unp_dispose(control);
587 lwkt_relpooltoken(unp);
594 lwkt_replymsg(&msg->lmsg, error);
601 uipc_sense(netmsg_t msg)
608 so = msg->base.nm_so;
609 sb = msg->sense.nm_stat;
612 * so_pcb is only modified with both the global and the unp
613 * pool token held. The unp pointer is invalid until we verify
614 * that it is good by re-checking so_pcb AFTER obtaining the token.
616 while ((unp = so->so_pcb) != NULL) {
617 lwkt_getpooltoken(unp);
618 if (unp == so->so_pcb)
620 lwkt_relpooltoken(unp);
626 /* pool token held */
628 sb->st_blksize = so->so_snd.ssb_hiwat;
630 if (unp->unp_ino == 0) { /* make up a non-zero inode number */
631 spin_lock(&unp_ino_spin);
632 unp->unp_ino = unp_ino++;
633 spin_unlock(&unp_ino_spin);
635 sb->st_ino = unp->unp_ino;
637 lwkt_relpooltoken(unp);
639 lwkt_replymsg(&msg->lmsg, error);
643 uipc_shutdown(netmsg_t msg)
650 * so_pcb is only modified with both the global and the unp
651 * pool token held. The unp pointer is invalid until we verify
652 * that it is good by re-checking so_pcb AFTER obtaining the token.
654 so = msg->base.nm_so;
655 while ((unp = so->so_pcb) != NULL) {
656 lwkt_getpooltoken(unp);
657 if (unp == so->so_pcb)
659 lwkt_relpooltoken(unp);
662 /* pool token held */
665 lwkt_relpooltoken(unp);
670 lwkt_replymsg(&msg->lmsg, error);
674 uipc_sockaddr(netmsg_t msg)
681 * so_pcb is only modified with both the global and the unp
682 * pool token held. The unp pointer is invalid until we verify
683 * that it is good by re-checking so_pcb AFTER obtaining the token.
685 so = msg->base.nm_so;
686 while ((unp = so->so_pcb) != NULL) {
687 lwkt_getpooltoken(unp);
688 if (unp == so->so_pcb)
690 lwkt_relpooltoken(unp);
693 /* pool token held */
695 *msg->sockaddr.nm_nam =
696 dup_sockaddr((struct sockaddr *)unp->unp_addr);
698 lwkt_relpooltoken(unp);
703 lwkt_replymsg(&msg->lmsg, error);
706 struct pr_usrreqs uipc_usrreqs = {
707 .pru_abort = uipc_abort,
708 .pru_accept = uipc_accept,
709 .pru_attach = uipc_attach,
710 .pru_bind = uipc_bind,
711 .pru_connect = uipc_connect,
712 .pru_connect2 = uipc_connect2,
713 .pru_control = pr_generic_notsupp,
714 .pru_detach = uipc_detach,
715 .pru_disconnect = uipc_disconnect,
716 .pru_listen = uipc_listen,
717 .pru_peeraddr = uipc_peeraddr,
718 .pru_rcvd = uipc_rcvd,
719 .pru_rcvoob = pr_generic_notsupp,
720 .pru_send = uipc_send,
721 .pru_sense = uipc_sense,
722 .pru_shutdown = uipc_shutdown,
723 .pru_sockaddr = uipc_sockaddr,
724 .pru_sosend = sosend,
725 .pru_soreceive = soreceive
729 uipc_ctloutput(netmsg_t msg)
732 struct sockopt *sopt;
736 lwkt_gettoken(&unp_token);
737 so = msg->base.nm_so;
738 sopt = msg->ctloutput.nm_sopt;
741 switch (sopt->sopt_dir) {
743 switch (sopt->sopt_name) {
745 if (unp->unp_flags & UNP_HAVEPC)
746 soopt_from_kbuf(sopt, &unp->unp_peercred,
747 sizeof(unp->unp_peercred));
749 if (so->so_type == SOCK_STREAM)
751 else if (so->so_type == SOCK_SEQPACKET)
767 lwkt_reltoken(&unp_token);
768 lwkt_replymsg(&msg->lmsg, error);
772 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
773 * for stream sockets, although the total for sender and receiver is
774 * actually only PIPSIZ.
776 * Datagram sockets really use the sendspace as the maximum datagram size,
777 * and don't really want to reserve the sendspace. Their recvspace should
778 * be large enough for at least one max-size datagram plus address.
780 * We want the local send/recv space to be significant larger then lo0's
786 static u_long unpst_sendspace = PIPSIZ;
787 static u_long unpst_recvspace = PIPSIZ;
788 static u_long unpdg_sendspace = 2*1024; /* really max datagram size */
789 static u_long unpdg_recvspace = 4*1024;
791 static int unp_rights; /* file descriptors in flight */
792 static struct spinlock unp_spin = SPINLOCK_INITIALIZER(&unp_spin);
794 SYSCTL_DECL(_net_local_seqpacket);
795 SYSCTL_DECL(_net_local_stream);
796 SYSCTL_INT(_net_local_stream, OID_AUTO, sendspace, CTLFLAG_RW,
797 &unpst_sendspace, 0, "Size of stream socket send buffer");
798 SYSCTL_INT(_net_local_stream, OID_AUTO, recvspace, CTLFLAG_RW,
799 &unpst_recvspace, 0, "Size of stream socket receive buffer");
801 SYSCTL_DECL(_net_local_dgram);
802 SYSCTL_INT(_net_local_dgram, OID_AUTO, maxdgram, CTLFLAG_RW,
803 &unpdg_sendspace, 0, "Max datagram socket size");
804 SYSCTL_INT(_net_local_dgram, OID_AUTO, recvspace, CTLFLAG_RW,
805 &unpdg_recvspace, 0, "Size of datagram socket receive buffer");
807 SYSCTL_DECL(_net_local);
808 SYSCTL_INT(_net_local, OID_AUTO, inflight, CTLFLAG_RD, &unp_rights, 0,
809 "File descriptors in flight");
812 unp_attach(struct socket *so, struct pru_attach_info *ai)
817 lwkt_gettoken(&unp_token);
819 if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
820 switch (so->so_type) {
824 error = soreserve(so, unpst_sendspace, unpst_recvspace,
829 error = soreserve(so, unpdg_sendspace, unpdg_recvspace,
839 unp = kmalloc(sizeof(*unp), M_UNPCB, M_WAITOK | M_ZERO | M_NULLOK);
845 unp->unp_gencnt = ++unp_gencnt;
847 LIST_INIT(&unp->unp_refs);
848 unp->unp_socket = so;
849 unp->unp_rvnode = ai->fd_rdir; /* jail cruft XXX JH */
850 LIST_INSERT_HEAD(so->so_type == SOCK_DGRAM ? &unp_dhead
851 : &unp_shead, unp, unp_link);
852 so->so_pcb = (caddr_t)unp;
856 lwkt_reltoken(&unp_token);
861 unp_detach(struct unpcb *unp)
865 lwkt_gettoken(&unp_token);
866 lwkt_getpooltoken(unp);
868 LIST_REMOVE(unp, unp_link); /* both tokens required */
869 unp->unp_gencnt = ++unp_gencnt;
871 if (unp->unp_vnode) {
872 unp->unp_vnode->v_socket = NULL;
873 vrele(unp->unp_vnode);
874 unp->unp_vnode = NULL;
878 while (!LIST_EMPTY(&unp->unp_refs))
879 unp_drop(LIST_FIRST(&unp->unp_refs), ECONNRESET);
880 soisdisconnected(unp->unp_socket);
881 so = unp->unp_socket;
882 soreference(so); /* for delayed sorflush */
883 KKASSERT(so->so_pcb == unp);
884 so->so_pcb = NULL; /* both tokens required */
885 unp->unp_socket = NULL;
886 sofree(so); /* remove pcb ref */
890 * Normally the receive buffer is flushed later,
891 * in sofree, but if our receive buffer holds references
892 * to descriptors that are now garbage, we will dispose
893 * of those descriptor references after the garbage collector
894 * gets them (resulting in a "panic: closef: count < 0").
900 lwkt_relpooltoken(unp);
901 lwkt_reltoken(&unp_token);
904 kfree(unp->unp_addr, M_SONAME);
909 unp_bind(struct unpcb *unp, struct sockaddr *nam, struct thread *td)
911 struct proc *p = td->td_proc;
912 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
916 struct nlookupdata nd;
917 char buf[SOCK_MAXADDRLEN];
919 lwkt_gettoken(&unp_token);
920 if (unp->unp_vnode != NULL) {
924 namelen = soun->sun_len - offsetof(struct sockaddr_un, sun_path);
929 strncpy(buf, soun->sun_path, namelen);
930 buf[namelen] = 0; /* null-terminate the string */
931 error = nlookup_init(&nd, buf, UIO_SYSSPACE,
932 NLC_LOCKVP | NLC_CREATE | NLC_REFDVP);
934 error = nlookup(&nd);
935 if (error == 0 && nd.nl_nch.ncp->nc_vp != NULL)
941 vattr.va_type = VSOCK;
942 vattr.va_mode = (ACCESSPERMS & ~p->p_fd->fd_cmask);
943 error = VOP_NCREATE(&nd.nl_nch, nd.nl_dvp, &vp, nd.nl_cred, &vattr);
945 if (unp->unp_vnode == NULL) {
946 vp->v_socket = unp->unp_socket;
948 unp->unp_addr = (struct sockaddr_un *)dup_sockaddr(nam);
951 vput(vp); /* late race */
958 lwkt_reltoken(&unp_token);
963 unp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
965 struct proc *p = td->td_proc;
966 struct sockaddr_un *soun = (struct sockaddr_un *)nam;
968 struct socket *so2, *so3;
969 struct unpcb *unp, *unp2, *unp3;
971 struct nlookupdata nd;
972 char buf[SOCK_MAXADDRLEN];
974 lwkt_gettoken(&unp_token);
976 len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
981 strncpy(buf, soun->sun_path, len);
985 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
987 error = nlookup(&nd);
989 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
994 if (vp->v_type != VSOCK) {
998 error = VOP_EACCESS(vp, VWRITE, p->p_ucred);
1003 error = ECONNREFUSED;
1006 if (so->so_type != so2->so_type) {
1010 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
1011 if (!(so2->so_options & SO_ACCEPTCONN) ||
1012 (so3 = sonewconn(so2, 0)) == NULL) {
1013 error = ECONNREFUSED;
1017 if (unp->unp_conn) { /* race, already connected! */
1025 unp3->unp_addr = (struct sockaddr_un *)
1026 dup_sockaddr((struct sockaddr *)unp2->unp_addr);
1029 * unp_peercred management:
1031 * The connecter's (client's) credentials are copied
1032 * from its process structure at the time of connect()
1035 cru2x(p->p_ucred, &unp3->unp_peercred);
1036 unp3->unp_flags |= UNP_HAVEPC;
1038 * The receiver's (server's) credentials are copied
1039 * from the unp_peercred member of socket on which the
1040 * former called listen(); unp_listen() cached that
1041 * process's credentials at that time so we can use
1044 KASSERT(unp2->unp_flags & UNP_HAVEPCCACHED,
1045 ("unp_connect: listener without cached peercred"));
1046 memcpy(&unp->unp_peercred, &unp2->unp_peercred,
1047 sizeof(unp->unp_peercred));
1048 unp->unp_flags |= UNP_HAVEPC;
1052 error = unp_connect2(so, so2);
1056 lwkt_reltoken(&unp_token);
1061 * Connect two unix domain sockets together.
1063 * NOTE: Semantics for any change to unp_conn requires that the per-unp
1064 * pool token also be held.
1067 unp_connect2(struct socket *so, struct socket *so2)
1072 lwkt_gettoken(&unp_token);
1074 if (so2->so_type != so->so_type) {
1075 lwkt_reltoken(&unp_token);
1076 return (EPROTOTYPE);
1079 lwkt_getpooltoken(unp);
1080 lwkt_getpooltoken(unp2);
1082 unp->unp_conn = unp2;
1084 switch (so->so_type) {
1086 LIST_INSERT_HEAD(&unp2->unp_refs, unp, unp_reflink);
1091 case SOCK_SEQPACKET:
1092 unp2->unp_conn = unp;
1098 panic("unp_connect2");
1100 lwkt_relpooltoken(unp2);
1101 lwkt_relpooltoken(unp);
1102 lwkt_reltoken(&unp_token);
1107 * Disconnect a unix domain socket pair.
1109 * NOTE: Semantics for any change to unp_conn requires that the per-unp
1110 * pool token also be held.
1113 unp_disconnect(struct unpcb *unp)
1117 lwkt_gettoken(&unp_token);
1118 lwkt_getpooltoken(unp);
1120 while ((unp2 = unp->unp_conn) != NULL) {
1121 lwkt_getpooltoken(unp2);
1122 if (unp2 == unp->unp_conn)
1124 lwkt_relpooltoken(unp2);
1129 unp->unp_conn = NULL;
1131 switch (unp->unp_socket->so_type) {
1133 LIST_REMOVE(unp, unp_reflink);
1134 soclrstate(unp->unp_socket, SS_ISCONNECTED);
1138 case SOCK_SEQPACKET:
1139 unp_reference(unp2);
1140 unp2->unp_conn = NULL;
1142 soisdisconnected(unp->unp_socket);
1143 soisdisconnected(unp2->unp_socket);
1148 lwkt_relpooltoken(unp2);
1149 lwkt_relpooltoken(unp);
1151 lwkt_reltoken(&unp_token);
1156 unp_abort(struct unpcb *unp)
1158 lwkt_gettoken(&unp_token);
1160 lwkt_reltoken(&unp_token);
1165 prison_unpcb(struct thread *td, struct unpcb *unp)
1171 if ((p = td->td_proc) == NULL)
1173 if (!p->p_ucred->cr_prison)
1175 if (p->p_fd->fd_rdir == unp->unp_rvnode)
1181 unp_pcblist(SYSCTL_HANDLER_ARGS)
1184 struct unpcb *unp, **unp_list;
1186 struct unp_head *head;
1188 head = ((intptr_t)arg1 == SOCK_DGRAM ? &unp_dhead : &unp_shead);
1190 KKASSERT(curproc != NULL);
1193 * The process of preparing the PCB list is too time-consuming and
1194 * resource-intensive to repeat twice on every request.
1196 if (req->oldptr == NULL) {
1198 req->oldidx = (n + n/8) * sizeof(struct xunpcb);
1202 if (req->newptr != NULL)
1205 lwkt_gettoken(&unp_token);
1208 * OK, now we're committed to doing something.
1210 gencnt = unp_gencnt;
1213 unp_list = kmalloc(n * sizeof *unp_list, M_TEMP, M_WAITOK);
1215 for (unp = LIST_FIRST(head), i = 0; unp && i < n;
1216 unp = LIST_NEXT(unp, unp_link)) {
1217 if (unp->unp_gencnt <= gencnt && !prison_unpcb(req->td, unp))
1218 unp_list[i++] = unp;
1220 n = i; /* in case we lost some during malloc */
1223 for (i = 0; i < n; i++) {
1225 if (unp->unp_gencnt <= gencnt) {
1227 xu.xu_len = sizeof xu;
1230 * XXX - need more locking here to protect against
1231 * connect/disconnect races for SMP.
1234 bcopy(unp->unp_addr, &xu.xu_addr,
1235 unp->unp_addr->sun_len);
1236 if (unp->unp_conn && unp->unp_conn->unp_addr)
1237 bcopy(unp->unp_conn->unp_addr,
1239 unp->unp_conn->unp_addr->sun_len);
1240 bcopy(unp, &xu.xu_unp, sizeof *unp);
1241 sotoxsocket(unp->unp_socket, &xu.xu_socket);
1242 error = SYSCTL_OUT(req, &xu, sizeof xu);
1245 lwkt_reltoken(&unp_token);
1246 kfree(unp_list, M_TEMP);
1251 SYSCTL_PROC(_net_local_dgram, OID_AUTO, pcblist, CTLFLAG_RD,
1252 (caddr_t)(long)SOCK_DGRAM, 0, unp_pcblist, "S,xunpcb",
1253 "List of active local datagram sockets");
1254 SYSCTL_PROC(_net_local_stream, OID_AUTO, pcblist, CTLFLAG_RD,
1255 (caddr_t)(long)SOCK_STREAM, 0, unp_pcblist, "S,xunpcb",
1256 "List of active local stream sockets");
1257 SYSCTL_PROC(_net_local_seqpacket, OID_AUTO, pcblist, CTLFLAG_RD,
1258 (caddr_t)(long)SOCK_SEQPACKET, 0, unp_pcblist, "S,xunpcb",
1259 "List of active local seqpacket stream sockets");
1262 unp_shutdown(struct unpcb *unp)
1266 if ((unp->unp_socket->so_type == SOCK_STREAM ||
1267 unp->unp_socket->so_type == SOCK_SEQPACKET) &&
1268 unp->unp_conn != NULL && (so = unp->unp_conn->unp_socket)) {
1274 unp_drop(struct unpcb *unp, int err)
1276 struct socket *so = unp->unp_socket;
1279 unp_disconnect(unp);
1286 lwkt_gettoken(&unp_token);
1287 lwkt_reltoken(&unp_token);
1292 unp_externalize(struct mbuf *rights)
1294 struct thread *td = curthread;
1295 struct proc *p = td->td_proc; /* XXX */
1296 struct lwp *lp = td->td_lwp;
1297 struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
1302 int newfds = (cm->cmsg_len - (CMSG_DATA(cm) - (u_char *)cm))
1303 / sizeof (struct file *);
1306 lwkt_gettoken(&unp_token);
1309 * if the new FD's will not fit, then we free them all
1311 if (!fdavail(p, newfds)) {
1312 rp = (struct file **)CMSG_DATA(cm);
1313 for (i = 0; i < newfds; i++) {
1316 * zero the pointer before calling unp_discard,
1317 * since it may end up in unp_gc()..
1320 unp_discard(fp, NULL);
1322 lwkt_reltoken(&unp_token);
1327 * now change each pointer to an fd in the global table to
1328 * an integer that is the index to the local fd table entry
1329 * that we set up to point to the global one we are transferring.
1330 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1331 * then do it in forward order. In that case, an integer will
1332 * always come in the same place or before its corresponding
1333 * struct file pointer.
1334 * If sizeof (struct file *) is smaller than sizeof int, then
1335 * do it in reverse order.
1337 if (sizeof (struct file *) >= sizeof (int)) {
1338 fdp = (int *)CMSG_DATA(cm);
1339 rp = (struct file **)CMSG_DATA(cm);
1340 for (i = 0; i < newfds; i++) {
1341 if (fdalloc(p, 0, &f))
1342 panic("unp_externalize");
1344 unp_fp_externalize(lp, fp, f);
1348 fdp = (int *)CMSG_DATA(cm) + newfds - 1;
1349 rp = (struct file **)CMSG_DATA(cm) + newfds - 1;
1350 for (i = 0; i < newfds; i++) {
1351 if (fdalloc(p, 0, &f))
1352 panic("unp_externalize");
1354 unp_fp_externalize(lp, fp, f);
1360 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1363 cm->cmsg_len = CMSG_LEN(newfds * sizeof(int));
1364 rights->m_len = cm->cmsg_len;
1366 lwkt_reltoken(&unp_token);
1371 unp_fp_externalize(struct lwp *lp, struct file *fp, int fd)
1376 lwkt_gettoken(&unp_token);
1380 if (fp->f_flag & FREVOKED) {
1381 kprintf("Warning: revoked fp exiting unix socket\n");
1383 error = falloc(lp, &fx, NULL);
1385 fsetfd(lp->lwp_proc->p_fd, fx, fd);
1387 fsetfd(lp->lwp_proc->p_fd, NULL, fd);
1390 fsetfd(lp->lwp_proc->p_fd, fp, fd);
1393 spin_lock(&unp_spin);
1396 spin_unlock(&unp_spin);
1399 lwkt_reltoken(&unp_token);
1406 LIST_INIT(&unp_dhead);
1407 LIST_INIT(&unp_shead);
1408 spin_init(&unp_spin);
1412 unp_internalize(struct mbuf *control, struct thread *td)
1414 struct proc *p = td->td_proc;
1415 struct filedesc *fdescp;
1416 struct cmsghdr *cm = mtod(control, struct cmsghdr *);
1420 struct cmsgcred *cmcred;
1426 lwkt_gettoken(&unp_token);
1429 if ((cm->cmsg_type != SCM_RIGHTS && cm->cmsg_type != SCM_CREDS) ||
1430 cm->cmsg_level != SOL_SOCKET ||
1431 CMSG_ALIGN(cm->cmsg_len) != control->m_len) {
1437 * Fill in credential information.
1439 if (cm->cmsg_type == SCM_CREDS) {
1440 cmcred = (struct cmsgcred *)CMSG_DATA(cm);
1441 cmcred->cmcred_pid = p->p_pid;
1442 cmcred->cmcred_uid = p->p_ucred->cr_ruid;
1443 cmcred->cmcred_gid = p->p_ucred->cr_rgid;
1444 cmcred->cmcred_euid = p->p_ucred->cr_uid;
1445 cmcred->cmcred_ngroups = MIN(p->p_ucred->cr_ngroups,
1447 for (i = 0; i < cmcred->cmcred_ngroups; i++)
1448 cmcred->cmcred_groups[i] = p->p_ucred->cr_groups[i];
1454 * cmsghdr may not be aligned, do not allow calculation(s) to
1457 if (cm->cmsg_len < CMSG_LEN(0)) {
1462 oldfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof (int);
1465 * check that all the FDs passed in refer to legal OPEN files
1466 * If not, reject the entire operation.
1468 fdp = (int *)CMSG_DATA(cm);
1469 for (i = 0; i < oldfds; i++) {
1471 if ((unsigned)fd >= fdescp->fd_nfiles ||
1472 fdescp->fd_files[fd].fp == NULL) {
1476 if (fdescp->fd_files[fd].fp->f_type == DTYPE_KQUEUE) {
1482 * Now replace the integer FDs with pointers to
1483 * the associated global file table entry..
1484 * Allocate a bigger buffer as necessary. But if an cluster is not
1485 * enough, return E2BIG.
1487 newlen = CMSG_LEN(oldfds * sizeof(struct file *));
1488 if (newlen > MCLBYTES) {
1492 if (newlen - control->m_len > M_TRAILINGSPACE(control)) {
1493 if (control->m_flags & M_EXT) {
1497 MCLGET(control, MB_WAIT);
1498 if (!(control->m_flags & M_EXT)) {
1503 /* copy the data to the cluster */
1504 memcpy(mtod(control, char *), cm, cm->cmsg_len);
1505 cm = mtod(control, struct cmsghdr *);
1509 * Adjust length, in case sizeof(struct file *) and sizeof(int)
1512 cm->cmsg_len = newlen;
1513 control->m_len = CMSG_ALIGN(newlen);
1516 * Transform the file descriptors into struct file pointers.
1517 * If sizeof (struct file *) is bigger than or equal to sizeof int,
1518 * then do it in reverse order so that the int won't get until
1520 * If sizeof (struct file *) is smaller than sizeof int, then
1521 * do it in forward order.
1523 if (sizeof (struct file *) >= sizeof (int)) {
1524 fdp = (int *)CMSG_DATA(cm) + oldfds - 1;
1525 rp = (struct file **)CMSG_DATA(cm) + oldfds - 1;
1526 for (i = 0; i < oldfds; i++) {
1527 fp = fdescp->fd_files[*fdp--].fp;
1530 spin_lock(&unp_spin);
1533 spin_unlock(&unp_spin);
1536 fdp = (int *)CMSG_DATA(cm);
1537 rp = (struct file **)CMSG_DATA(cm);
1538 for (i = 0; i < oldfds; i++) {
1539 fp = fdescp->fd_files[*fdp++].fp;
1542 spin_lock(&unp_spin);
1545 spin_unlock(&unp_spin);
1550 lwkt_reltoken(&unp_token);
1555 * Garbage collect in-transit file descriptors that get lost due to
1556 * loops (i.e. when a socket is sent to another process over itself,
1557 * and more complex situations).
1559 * NOT MPSAFE - TODO socket flush code and maybe closef. Rest is MPSAFE.
1562 struct unp_gc_info {
1563 struct file **extra_ref;
1564 struct file *locked_fp;
1573 struct unp_gc_info info;
1574 static boolean_t unp_gcing;
1579 * Only one gc can be in-progress at any given moment
1581 spin_lock(&unp_spin);
1583 spin_unlock(&unp_spin);
1587 spin_unlock(&unp_spin);
1589 lwkt_gettoken(&unp_token);
1592 * Before going through all this, set all FDs to be NOT defered
1593 * and NOT externally accessible (not marked). During the scan
1594 * a fd can be marked externally accessible but we may or may not
1595 * be able to immediately process it (controlled by FDEFER).
1597 * If we loop sleep a bit. The complexity of the topology can cause
1598 * multiple loops. Also failure to acquire the socket's so_rcv
1599 * token can cause us to loop.
1601 allfiles_scan_exclusive(unp_gc_clearmarks, NULL);
1604 allfiles_scan_exclusive(unp_gc_checkmarks, &info);
1606 tsleep(&info, 0, "gcagain", 1);
1607 } while (info.defer);
1610 * We grab an extra reference to each of the file table entries
1611 * that are not otherwise accessible and then free the rights
1612 * that are stored in messages on them.
1614 * The bug in the orginal code is a little tricky, so I'll describe
1615 * what's wrong with it here.
1617 * It is incorrect to simply unp_discard each entry for f_msgcount
1618 * times -- consider the case of sockets A and B that contain
1619 * references to each other. On a last close of some other socket,
1620 * we trigger a gc since the number of outstanding rights (unp_rights)
1621 * is non-zero. If during the sweep phase the gc code un_discards,
1622 * we end up doing a (full) closef on the descriptor. A closef on A
1623 * results in the following chain. Closef calls soo_close, which
1624 * calls soclose. Soclose calls first (through the switch
1625 * uipc_usrreq) unp_detach, which re-invokes unp_gc. Unp_gc simply
1626 * returns because the previous instance had set unp_gcing, and
1627 * we return all the way back to soclose, which marks the socket
1628 * with SS_NOFDREF, and then calls sofree. Sofree calls sorflush
1629 * to free up the rights that are queued in messages on the socket A,
1630 * i.e., the reference on B. The sorflush calls via the dom_dispose
1631 * switch unp_dispose, which unp_scans with unp_discard. This second
1632 * instance of unp_discard just calls closef on B.
1634 * Well, a similar chain occurs on B, resulting in a sorflush on B,
1635 * which results in another closef on A. Unfortunately, A is already
1636 * being closed, and the descriptor has already been marked with
1637 * SS_NOFDREF, and soclose panics at this point.
1639 * Here, we first take an extra reference to each inaccessible
1640 * descriptor. Then, we call sorflush ourself, since we know
1641 * it is a Unix domain socket anyhow. After we destroy all the
1642 * rights carried in messages, we do a last closef to get rid
1643 * of our extra reference. This is the last close, and the
1644 * unp_detach etc will shut down the socket.
1646 * 91/09/19, bsy@cs.cmu.edu
1648 info.extra_ref = kmalloc(256 * sizeof(struct file *), M_FILE, M_WAITOK);
1649 info.maxindex = 256;
1656 allfiles_scan_exclusive(unp_gc_checkrefs, &info);
1659 * For each FD on our hit list, do the following two things
1661 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp) {
1662 struct file *tfp = *fpp;
1663 if (tfp->f_type == DTYPE_SOCKET && tfp->f_data != NULL)
1664 sorflush((struct socket *)(tfp->f_data));
1666 for (i = info.index, fpp = info.extra_ref; --i >= 0; ++fpp)
1668 } while (info.index == info.maxindex);
1670 lwkt_reltoken(&unp_token);
1672 kfree((caddr_t)info.extra_ref, M_FILE);
1677 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1680 unp_gc_checkrefs(struct file *fp, void *data)
1682 struct unp_gc_info *info = data;
1684 if (fp->f_count == 0)
1686 if (info->index == info->maxindex)
1690 * If all refs are from msgs, and it's not marked accessible
1691 * then it must be referenced from some unreachable cycle
1692 * of (shut-down) FDs, so include it in our
1693 * list of FDs to remove
1695 if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
1696 info->extra_ref[info->index++] = fp;
1703 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1706 unp_gc_clearmarks(struct file *fp, void *data __unused)
1708 atomic_clear_int(&fp->f_flag, FMARK | FDEFER);
1713 * MPSAFE - NOTE: filehead list and file pointer spinlocked on entry
1716 unp_gc_checkmarks(struct file *fp, void *data)
1718 struct unp_gc_info *info = data;
1722 * If the file is not open, skip it. Make sure it isn't marked
1723 * defered or we could loop forever, in case we somehow race
1726 if (fp->f_count == 0) {
1727 if (fp->f_flag & FDEFER)
1728 atomic_clear_int(&fp->f_flag, FDEFER);
1732 * If we already marked it as 'defer' in a
1733 * previous pass, then try process it this time
1736 if (fp->f_flag & FDEFER) {
1737 atomic_clear_int(&fp->f_flag, FDEFER);
1740 * if it's not defered, then check if it's
1741 * already marked.. if so skip it
1743 if (fp->f_flag & FMARK)
1746 * If all references are from messages
1747 * in transit, then skip it. it's not
1748 * externally accessible.
1750 if (fp->f_count == fp->f_msgcount)
1753 * If it got this far then it must be
1754 * externally accessible.
1756 atomic_set_int(&fp->f_flag, FMARK);
1760 * either it was defered, or it is externally
1761 * accessible and not already marked so.
1762 * Now check if it is possibly one of OUR sockets.
1764 if (fp->f_type != DTYPE_SOCKET ||
1765 (so = (struct socket *)fp->f_data) == NULL) {
1768 if (so->so_proto->pr_domain != &localdomain ||
1769 !(so->so_proto->pr_flags & PR_RIGHTS)) {
1774 * So, Ok, it's one of our sockets and it IS externally accessible
1775 * (or was defered). Now we look to see if we hold any file
1776 * descriptors in its message buffers. Follow those links and mark
1777 * them as accessible too.
1779 * We are holding multiple spinlocks here, if we cannot get the
1780 * token non-blocking defer until the next loop.
1782 info->locked_fp = fp;
1783 if (lwkt_trytoken(&so->so_rcv.ssb_token)) {
1784 unp_scan(so->so_rcv.ssb_mb, unp_mark, info);
1785 lwkt_reltoken(&so->so_rcv.ssb_token);
1787 atomic_set_int(&fp->f_flag, FDEFER);
1794 * Scan all unix domain sockets and replace any revoked file pointers
1795 * found with the dummy file pointer fx. We don't worry about races
1796 * against file pointers being read out as those are handled in the
1800 #define REVOKE_GC_MAXFILES 32
1802 struct unp_revoke_gc_info {
1804 struct file *fary[REVOKE_GC_MAXFILES];
1809 unp_revoke_gc(struct file *fx)
1811 struct unp_revoke_gc_info info;
1814 lwkt_gettoken(&unp_token);
1818 allfiles_scan_exclusive(unp_revoke_gc_check, &info);
1819 for (i = 0; i < info.fcount; ++i)
1820 unp_fp_externalize(NULL, info.fary[i], -1);
1821 } while (info.fcount == REVOKE_GC_MAXFILES);
1822 lwkt_reltoken(&unp_token);
1826 * Check for and replace revoked descriptors.
1828 * WARNING: This routine is not allowed to block.
1831 unp_revoke_gc_check(struct file *fps, void *vinfo)
1833 struct unp_revoke_gc_info *info = vinfo;
1844 * Is this a unix domain socket with rights-passing abilities?
1846 if (fps->f_type != DTYPE_SOCKET)
1848 if ((so = (struct socket *)fps->f_data) == NULL)
1850 if (so->so_proto->pr_domain != &localdomain)
1852 if ((so->so_proto->pr_flags & PR_RIGHTS) == 0)
1856 * Scan the mbufs for control messages and replace any revoked
1857 * descriptors we find.
1859 lwkt_gettoken(&so->so_rcv.ssb_token);
1860 m0 = so->so_rcv.ssb_mb;
1862 for (m = m0; m; m = m->m_next) {
1863 if (m->m_type != MT_CONTROL)
1865 if (m->m_len < sizeof(*cm))
1867 cm = mtod(m, struct cmsghdr *);
1868 if (cm->cmsg_level != SOL_SOCKET ||
1869 cm->cmsg_type != SCM_RIGHTS) {
1872 qfds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(void *);
1873 rp = (struct file **)CMSG_DATA(cm);
1874 for (i = 0; i < qfds; i++) {
1876 if (fp->f_flag & FREVOKED) {
1877 kprintf("Warning: Removing revoked fp from unix domain socket queue\n");
1879 info->fx->f_msgcount++;
1882 info->fary[info->fcount++] = fp;
1884 if (info->fcount == REVOKE_GC_MAXFILES)
1887 if (info->fcount == REVOKE_GC_MAXFILES)
1891 if (info->fcount == REVOKE_GC_MAXFILES)
1894 lwkt_reltoken(&so->so_rcv.ssb_token);
1897 * Stop the scan if we filled up our array.
1899 if (info->fcount == REVOKE_GC_MAXFILES)
1905 * Dispose of the fp's stored in a mbuf.
1907 * The dds loop can cause additional fps to be entered onto the
1908 * list while it is running, flattening out the operation and avoiding
1909 * a deep kernel stack recursion.
1912 unp_dispose(struct mbuf *m)
1914 unp_defdiscard_t dds;
1916 lwkt_gettoken(&unp_token);
1917 ++unp_defdiscard_nest;
1919 unp_scan(m, unp_discard, NULL);
1921 if (unp_defdiscard_nest == 1) {
1922 while ((dds = unp_defdiscard_base) != NULL) {
1923 unp_defdiscard_base = dds->next;
1924 closef(dds->fp, NULL);
1925 kfree(dds, M_UNPCB);
1928 --unp_defdiscard_nest;
1929 lwkt_reltoken(&unp_token);
1933 unp_listen(struct unpcb *unp, struct thread *td)
1935 struct proc *p = td->td_proc;
1938 lwkt_gettoken(&unp_token);
1939 cru2x(p->p_ucred, &unp->unp_peercred);
1940 unp->unp_flags |= UNP_HAVEPCCACHED;
1941 lwkt_reltoken(&unp_token);
1946 unp_scan(struct mbuf *m0, void (*op)(struct file *, void *), void *data)
1955 for (m = m0; m; m = m->m_next) {
1956 if (m->m_type == MT_CONTROL &&
1957 m->m_len >= sizeof(*cm)) {
1958 cm = mtod(m, struct cmsghdr *);
1959 if (cm->cmsg_level != SOL_SOCKET ||
1960 cm->cmsg_type != SCM_RIGHTS)
1962 qfds = (cm->cmsg_len - CMSG_LEN(0)) /
1964 rp = (struct file **)CMSG_DATA(cm);
1965 for (i = 0; i < qfds; i++)
1967 break; /* XXX, but saves time */
1975 * Mark visibility. info->defer is recalculated on every pass.
1978 unp_mark(struct file *fp, void *data)
1980 struct unp_gc_info *info = data;
1982 if ((fp->f_flag & FMARK) == 0) {
1984 atomic_set_int(&fp->f_flag, FMARK | FDEFER);
1985 } else if (fp->f_flag & FDEFER) {
1991 * Discard a fp previously held in a unix domain socket mbuf. To
1992 * avoid blowing out the kernel stack due to contrived chain-reactions
1993 * we may have to defer the operation to a higher procedural level.
1995 * Caller holds unp_token
1998 unp_discard(struct file *fp, void *data __unused)
2000 unp_defdiscard_t dds;
2002 spin_lock(&unp_spin);
2005 spin_unlock(&unp_spin);
2007 if (unp_defdiscard_nest) {
2008 dds = kmalloc(sizeof(*dds), M_UNPCB, M_WAITOK|M_ZERO);
2010 dds->next = unp_defdiscard_base;
2011 unp_defdiscard_base = dds;