1 /* $NetBSD: uipc_usrreq.c,v 1.127 2009/08/26 22:34:47 bouyer Exp $ */
4 * Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, and by Andrew Doran.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1989, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
65 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved.
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
75 * 3. All advertising materials mentioning features or use of this software
76 * must display the following acknowledgement:
77 * This product includes software developed by the University of
78 * California, Berkeley and its contributors.
79 * 4. Neither the name of the University nor the names of its contributors
80 * may be used to endorse or promote products derived from this software
81 * without specific prior written permission.
83 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95
98 #include <sys/cdefs.h>
99 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.127 2009/08/26 22:34:47 bouyer Exp $");
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/proc.h>
104 #include <sys/filedesc.h>
105 #include <sys/domain.h>
106 #include <sys/protosw.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/unpcb.h>
111 #include <sys/namei.h>
112 #include <sys/vnode.h>
113 #include <sys/file.h>
114 #include <sys/stat.h>
115 #include <sys/mbuf.h>
116 #include <sys/kauth.h>
117 #include <sys/kmem.h>
118 #include <sys/atomic.h>
119 #include <sys/uidinfo.h>
120 #include <sys/kernel.h>
121 #include <sys/kthread.h>
124 * Unix communications domain.
128 * rethink name space problems
129 * need a proper out-of-band
133 * The generic rules noted in uipc_socket2.c apply. In addition:
135 * o We have a global lock, uipc_lock.
137 * o All datagram sockets are locked by uipc_lock.
139 * o For stream socketpairs, the two endpoints are created sharing the same
140 * independent lock. Sockets presented to PRU_CONNECT2 must already have
143 * o Stream sockets created via socket() start life with their own
146 * o Stream connections to a named endpoint are slightly more complicated.
147 * Sockets that have called listen() have their lock pointer mutated to
148 * the global uipc_lock. When establishing a connection, the connecting
149 * socket also has its lock mutated to uipc_lock, which matches the head
150 * (listening socket). We create a new socket for accept() to return, and
151 * that also shares the head's lock. Until the connection is completely
152 * done on both ends, all three sockets are locked by uipc_lock. Once the
153 * connection is complete, the association with the head's lock is broken.
154 * The connecting socket and the socket returned from accept() have their
155 * lock pointers mutated away from uipc_lock, and back to the connecting
156 * socket's original, independent lock. The head continues to be locked
159 * o If uipc_lock is determined to be a significant source of contention,
160 * it could easily be hashed out. It is difficult to simply make it an
161 * independent lock because of visibility / garbage collection issues:
162 * if a socket has been associated with a lock at any point, that lock
163 * must remain valid until the socket is no longer visible in the system.
164 * The lock must not be freed or otherwise destroyed until any sockets
165 * that had referenced it have also been destroyed.
167 const struct sockaddr_un sun_noname
= {
168 .sun_len
= sizeof(sun_noname
),
169 .sun_family
= AF_LOCAL
,
171 ino_t unp_ino
; /* prototype for fake inode numbers */
173 struct mbuf
*unp_addsockcred(struct lwp
*, struct mbuf
*);
174 static void unp_mark(file_t
*);
175 static void unp_scan(struct mbuf
*, void (*)(file_t
*), int);
176 static void unp_discard_now(file_t
*);
177 static void unp_discard_later(file_t
*);
178 static void unp_thread(void *);
179 static void unp_thread_kick(void);
180 static kmutex_t
*uipc_lock
;
182 static kcondvar_t unp_thread_cv
;
183 static lwp_t
*unp_thread_lwp
;
184 static SLIST_HEAD(,file
) unp_thread_discard
;
185 static int unp_defer
;
188 * Initialize Unix protocols.
195 uipc_lock
= mutex_obj_alloc(MUTEX_DEFAULT
, IPL_NONE
);
196 cv_init(&unp_thread_cv
, "unpgc");
198 error
= kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
, unp_thread
,
199 NULL
, &unp_thread_lwp
, "unpgc");
201 panic("uipc_init %d", error
);
205 * A connection succeeded: disassociate both endpoints from the head's
206 * lock, and make them share their own lock. There is a race here: for
207 * a very brief time one endpoint will be locked by a different lock
208 * than the other end. However, since the current thread holds the old
209 * lock (the listening socket's lock, the head) access can still only be
210 * made to one side of the connection.
213 unp_setpeerlocks(struct socket
*so
, struct socket
*so2
)
218 KASSERT(solocked2(so
, so2
));
221 * Bail out if either end of the socket is not yet fully
222 * connected or accepted. We only break the lock association
223 * with the head when the pair of sockets stand completely
226 KASSERT(so
->so_head
== NULL
);
227 if (so2
->so_head
!= NULL
)
231 * Drop references to old lock. A third reference (from the
232 * queue head) must be held as we still hold its lock. Bonus:
233 * we don't need to worry about garbage collecting the lock.
236 KASSERT(lock
== uipc_lock
);
237 mutex_obj_free(lock
);
238 mutex_obj_free(lock
);
241 * Grab stream lock from the initiator and share between the two
242 * endpoints. Issue memory barrier to ensure all modifications
243 * become globally visible before the lock change. so2 is
244 * assumed not to have a stream lock, because it was created
245 * purely for the server side to accept this connection and
246 * started out life using the domain-wide lock.
249 KASSERT(unp
->unp_streamlock
!= NULL
);
250 KASSERT(sotounpcb(so2
)->unp_streamlock
== NULL
);
251 lock
= unp
->unp_streamlock
;
252 unp
->unp_streamlock
= NULL
;
253 mutex_obj_hold(lock
);
256 * possible race if lock is not held - see comment in
257 * uipc_usrreq(PRU_ACCEPT).
259 KASSERT(mutex_owned(lock
));
260 solockreset(so
, lock
);
261 solockreset(so2
, lock
);
265 * Reset a socket's lock back to the domain-wide lock.
268 unp_resetlock(struct socket
*so
)
270 kmutex_t
*olock
, *nlock
;
273 KASSERT(solocked(so
));
280 KASSERT(unp
->unp_streamlock
== NULL
);
281 unp
->unp_streamlock
= olock
;
282 mutex_obj_hold(nlock
);
284 solockreset(so
, nlock
);
289 unp_free(struct unpcb
*unp
)
293 free(unp
->unp_addr
, M_SONAME
);
294 if (unp
->unp_streamlock
!= NULL
)
295 mutex_obj_free(unp
->unp_streamlock
);
300 unp_output(struct mbuf
*m
, struct mbuf
*control
, struct unpcb
*unp
,
304 const struct sockaddr_un
*sun
;
306 so2
= unp
->unp_conn
->unp_socket
;
308 KASSERT(solocked(so2
));
314 if (unp
->unp_conn
->unp_flags
& UNP_WANTCRED
)
315 control
= unp_addsockcred(l
, control
);
316 if (sbappendaddr(&so2
->so_rcv
, (const struct sockaddr
*)sun
, m
,
318 so2
->so_rcv
.sb_overflowed
++;
319 unp_dispose(control
);
330 unp_setaddr(struct socket
*so
, struct mbuf
*nam
, bool peeraddr
)
332 const struct sockaddr_un
*sun
;
336 KASSERT(solocked(so
));
343 if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
)
344 sun
= unp
->unp_conn
->unp_addr
;
351 nam
->m_len
= sun
->sun_len
;
352 if (nam
->m_len
> MLEN
&& !ext
) {
354 MEXTMALLOC(nam
, MAXPATHLEN
* 2, M_WAITOK
);
358 KASSERT(nam
->m_len
<= MAXPATHLEN
* 2);
359 memcpy(mtod(nam
, void *), sun
, (size_t)nam
->m_len
);
367 uipc_usrreq(struct socket
*so
, int req
, struct mbuf
*m
, struct mbuf
*nam
,
368 struct mbuf
*control
, struct lwp
*l
)
370 struct unpcb
*unp
= sotounpcb(so
);
376 if (req
== PRU_CONTROL
)
380 if (req
!= PRU_SEND
&& req
!= PRU_SENDOOB
&& control
)
381 panic("uipc_usrreq: unexpected control mbuf");
383 p
= l
? l
->l_proc
: NULL
;
384 if (req
!= PRU_ATTACH
) {
389 KASSERT(solocked(so
));
399 error
= unp_attach(so
);
408 error
= unp_bind(so
, nam
, l
);
413 * If the socket can accept a connection, it must be
414 * locked by uipc_lock.
417 if (unp
->unp_vnode
== NULL
)
423 error
= unp_connect(so
, nam
, l
);
427 error
= unp_connect2(so
, (struct socket
*)nam
, PRU_CONNECT2
);
435 KASSERT(so
->so_lock
== uipc_lock
);
437 * Mark the initiating STREAM socket as connected *ONLY*
438 * after it's been accepted. This prevents a client from
439 * overrunning a server and receiving ECONNREFUSED.
441 if (unp
->unp_conn
== NULL
)
443 so2
= unp
->unp_conn
->unp_socket
;
444 if (so2
->so_state
& SS_ISCONNECTING
) {
445 KASSERT(solocked2(so
, so
->so_head
));
446 KASSERT(solocked2(so2
, so
->so_head
));
450 * If the connection is fully established, break the
451 * association with uipc_lock and give the connected
452 * pair a seperate lock to share.
453 * There is a race here: sotounpcb(so2)->unp_streamlock
454 * is not locked, so when changing so2->so_lock
455 * another thread can grab it while so->so_lock is still
456 * pointing to the (locked) uipc_lock.
457 * this should be harmless, exept that this makes
458 * solocked2() and solocked() unreliable.
459 * Another problem is that unp_setaddr() expects the
460 * the socket locked. Grabing sotounpcb(so2)->unp_streamlock
463 mutex_enter(sotounpcb(so2
)->unp_streamlock
);
464 unp_setpeerlocks(so2
, so
);
466 * Only now return peer's address, as we may need to
467 * block in order to allocate memory.
469 * XXX Minor race: connection can be broken while
470 * lock is dropped in unp_setaddr(). We will return
471 * error == 0 and sun_noname as the peer address.
473 unp_setaddr(so
, nam
, true);
474 /* so_lock now points to unp_streamlock */
475 mutex_exit(so2
->so_lock
);
484 switch (so
->so_type
) {
491 #define rcv (&so->so_rcv)
492 #define snd (&so2->so_snd)
493 if (unp
->unp_conn
== 0)
495 so2
= unp
->unp_conn
->unp_socket
;
496 KASSERT(solocked2(so
, so2
));
498 * Adjust backpressure on sender
499 * and wakeup any waiting to write.
501 snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
502 unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
503 newhiwat
= snd
->sb_hiwat
+ unp
->unp_cc
- rcv
->sb_cc
;
504 (void)chgsbsize(so2
->so_uidinfo
,
505 &snd
->sb_hiwat
, newhiwat
, RLIM_INFINITY
);
506 unp
->unp_cc
= rcv
->sb_cc
;
519 * Note: unp_internalize() rejects any control message
520 * other than SCM_RIGHTS, and only allows one. This
521 * has the side-effect of preventing a caller from
526 error
= unp_internalize(&control
);
534 switch (so
->so_type
) {
537 KASSERT(so
->so_lock
== uipc_lock
);
539 if ((so
->so_state
& SS_ISCONNECTED
) != 0)
543 * Note: once connected, the
544 * socket's lock must not be
545 * dropped until we have sent
546 * the message and disconnected.
547 * This is necessary to prevent
548 * intervening control ops, like
549 * another connection.
551 error
= unp_connect(so
, nam
, l
);
554 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
558 unp_dispose(control
);
564 error
= unp_output(m
, control
, unp
, l
);
571 #define rcv (&so2->so_rcv)
572 #define snd (&so->so_snd)
573 if (unp
->unp_conn
== NULL
) {
577 so2
= unp
->unp_conn
->unp_socket
;
578 KASSERT(solocked2(so
, so2
));
579 if (unp
->unp_conn
->unp_flags
& UNP_WANTCRED
) {
581 * Credentials are passed only once on
584 unp
->unp_conn
->unp_flags
&= ~UNP_WANTCRED
;
585 control
= unp_addsockcred(l
, control
);
588 * Send to paired receive port, and then reduce
589 * send buffer hiwater marks to maintain backpressure.
593 if (sbappendcontrol(rcv
, m
, control
) != 0)
598 rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
599 unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
600 newhiwat
= snd
->sb_hiwat
-
601 (rcv
->sb_cc
- unp
->unp_conn
->unp_cc
);
602 (void)chgsbsize(so
->so_uidinfo
,
603 &snd
->sb_hiwat
, newhiwat
, RLIM_INFINITY
);
604 unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
608 if (control
!= NULL
) {
609 unp_dispose(control
);
620 (void)unp_drop(unp
, ECONNABORTED
);
622 KASSERT(so
->so_head
== NULL
);
624 if (so
->so_pcb
== NULL
)
625 panic("uipc 5: drop killed pcb");
631 ((struct stat
*) m
)->st_blksize
= so
->so_snd
.sb_hiwat
;
632 if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
633 so2
= unp
->unp_conn
->unp_socket
;
634 KASSERT(solocked2(so
, so2
));
635 ((struct stat
*) m
)->st_blksize
+= so2
->so_rcv
.sb_cc
;
637 ((struct stat
*) m
)->st_dev
= NODEV
;
638 if (unp
->unp_ino
== 0)
639 unp
->unp_ino
= unp_ino
++;
640 ((struct stat
*) m
)->st_atimespec
=
641 ((struct stat
*) m
)->st_mtimespec
=
642 ((struct stat
*) m
)->st_ctimespec
= unp
->unp_ctime
;
643 ((struct stat
*) m
)->st_ino
= unp
->unp_ino
;
657 unp_setaddr(so
, nam
, false);
661 unp_setaddr(so
, nam
, true);
673 * Unix domain socket option processing.
676 uipc_ctloutput(int op
, struct socket
*so
, struct sockopt
*sopt
)
678 struct unpcb
*unp
= sotounpcb(so
);
679 int optval
= 0, error
= 0;
681 KASSERT(solocked(so
));
683 if (sopt
->sopt_level
!= 0) {
688 switch (sopt
->sopt_name
) {
691 error
= sockopt_getint(sopt
, &optval
);
694 switch (sopt
->sopt_name
) {
695 #define OPTSET(bit) \
697 unp->unp_flags |= (bit); \
699 unp->unp_flags &= ~(bit);
702 OPTSET(UNP_WANTCRED
);
705 OPTSET(UNP_CONNWAIT
);
719 switch (sopt
->sopt_name
) {
721 if (unp
->unp_flags
& UNP_EIDSVALID
) {
722 error
= sockopt_set(sopt
,
723 &unp
->unp_connid
, sizeof(unp
->unp_connid
));
729 #define OPTBIT(bit) (unp->unp_flags & (bit) ? 1 : 0)
731 optval
= OPTBIT(UNP_WANTCRED
);
732 error
= sockopt_setint(sopt
, optval
);
747 * Both send and receive buffers are allocated PIPSIZ bytes of buffering
748 * for stream sockets, although the total for sender and receiver is
749 * actually only PIPSIZ.
750 * Datagram sockets really use the sendspace as the maximum datagram size,
751 * and don't really want to reserve the sendspace. Their recvspace should
752 * be large enough for at least one max-size datagram plus address.
755 u_long unpst_sendspace
= PIPSIZ
;
756 u_long unpst_recvspace
= PIPSIZ
;
757 u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
758 u_long unpdg_recvspace
= 4*1024;
760 u_int unp_rights
; /* files in flight */
761 u_int unp_rights_ratio
= 2; /* limit, fraction of maxfiles */
764 unp_attach(struct socket
*so
)
769 switch (so
->so_type
) {
771 if (so
->so_lock
== NULL
) {
773 * XXX Assuming that no socket locks are held,
774 * as this call may sleep.
776 so
->so_lock
= mutex_obj_alloc(MUTEX_DEFAULT
, IPL_NONE
);
779 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
780 error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
787 if (so
->so_lock
== NULL
) {
788 mutex_obj_hold(uipc_lock
);
789 so
->so_lock
= uipc_lock
;
792 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
793 error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
802 KASSERT(solocked(so
));
803 unp
= malloc(sizeof(*unp
), M_PCB
, M_NOWAIT
);
806 memset(unp
, 0, sizeof(*unp
));
807 unp
->unp_socket
= so
;
809 nanotime(&unp
->unp_ctime
);
814 unp_detach(struct unpcb
*unp
)
819 so
= unp
->unp_socket
;
822 if ((vp
= unp
->unp_vnode
) != NULL
) {
824 /* Acquire v_interlock to protect against unp_connect(). */
826 mutex_enter(&vp
->v_interlock
);
830 unp
->unp_vnode
= NULL
;
834 while (unp
->unp_refs
) {
835 KASSERT(solocked2(so
, unp
->unp_refs
->unp_socket
));
836 if (unp_drop(unp
->unp_refs
, ECONNRESET
)) {
841 soisdisconnected(so
);
845 * Normally the receive buffer is flushed later, in sofree,
846 * but if our receive buffer holds references to files that
847 * are now garbage, we will enqueue those file references to
848 * the garbage collector and kick it into action.
858 unp_bind(struct socket
*so
, struct mbuf
*nam
, struct lwp
*l
)
860 struct sockaddr_un
*sun
;
870 if (unp
->unp_vnode
!= NULL
)
872 if ((unp
->unp_flags
& UNP_BUSY
) != 0) {
874 * EALREADY may not be strictly accurate, but since this
875 * is a major application error it's hardly a big deal.
879 unp
->unp_flags
|= UNP_BUSY
;
883 * Allocate the new sockaddr. We have to allocate one
884 * extra byte so that we can ensure that the pathname
888 addrlen
= nam
->m_len
+ 1;
889 sun
= malloc(addrlen
, M_SONAME
, M_WAITOK
);
890 m_copydata(nam
, 0, nam
->m_len
, (void *)sun
);
891 *(((char *)sun
) + nam
->m_len
) = '\0';
893 NDINIT(&nd
, CREATE
, FOLLOW
| LOCKPARENT
| TRYEMULROOT
, UIO_SYSSPACE
,
896 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
897 if ((error
= namei(&nd
)) != 0)
901 VOP_ABORTOP(nd
.ni_dvp
, &nd
.ni_cnd
);
911 vattr
.va_type
= VSOCK
;
912 vattr
.va_mode
= ACCESSPERMS
& ~(p
->p_cwdi
->cwdi_cmask
);
913 error
= VOP_CREATE(nd
.ni_dvp
, &nd
.ni_vp
, &nd
.ni_cnd
, &vattr
);
918 vp
->v_socket
= unp
->unp_socket
;
920 unp
->unp_addrlen
= addrlen
;
922 unp
->unp_connid
.unp_pid
= p
->p_pid
;
923 unp
->unp_connid
.unp_euid
= kauth_cred_geteuid(l
->l_cred
);
924 unp
->unp_connid
.unp_egid
= kauth_cred_getegid(l
->l_cred
);
925 unp
->unp_flags
|= UNP_EIDSBIND
;
927 unp
->unp_flags
&= ~UNP_BUSY
;
933 unp
->unp_flags
&= ~UNP_BUSY
;
938 unp_connect(struct socket
*so
, struct mbuf
*nam
, struct lwp
*l
)
940 struct sockaddr_un
*sun
;
942 struct socket
*so2
, *so3
;
943 struct unpcb
*unp
, *unp2
, *unp3
;
949 if ((unp
->unp_flags
& UNP_BUSY
) != 0) {
951 * EALREADY may not be strictly accurate, but since this
952 * is a major application error it's hardly a big deal.
956 unp
->unp_flags
|= UNP_BUSY
;
960 * Allocate a temporary sockaddr. We have to allocate one extra
961 * byte so that we can ensure that the pathname is nul-terminated.
962 * When we establish the connection, we copy the other PCB's
963 * sockaddr to our own.
965 addrlen
= nam
->m_len
+ 1;
966 sun
= malloc(addrlen
, M_SONAME
, M_WAITOK
);
967 m_copydata(nam
, 0, nam
->m_len
, (void *)sun
);
968 *(((char *)sun
) + nam
->m_len
) = '\0';
970 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| TRYEMULROOT
, UIO_SYSSPACE
,
973 if ((error
= namei(&nd
)) != 0)
976 if (vp
->v_type
!= VSOCK
) {
980 if ((error
= VOP_ACCESS(vp
, VWRITE
, l
->l_cred
)) != 0)
982 /* Acquire v_interlock to protect against unp_detach(). */
983 mutex_enter(&vp
->v_interlock
);
986 mutex_exit(&vp
->v_interlock
);
987 error
= ECONNREFUSED
;
990 if (so
->so_type
!= so2
->so_type
) {
991 mutex_exit(&vp
->v_interlock
);
997 mutex_exit(&vp
->v_interlock
);
998 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) != 0) {
1000 * This may seem somewhat fragile but is OK: if we can
1001 * see SO_ACCEPTCONN set on the endpoint, then it must
1002 * be locked by the domain-wide uipc_lock.
1004 KASSERT((so
->so_options
& SO_ACCEPTCONN
) == 0 ||
1005 so2
->so_lock
== uipc_lock
);
1006 if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
1007 (so3
= sonewconn(so2
, 0)) == NULL
) {
1008 error
= ECONNREFUSED
;
1012 unp2
= sotounpcb(so2
);
1013 unp3
= sotounpcb(so3
);
1014 if (unp2
->unp_addr
) {
1015 unp3
->unp_addr
= malloc(unp2
->unp_addrlen
,
1016 M_SONAME
, M_WAITOK
);
1017 memcpy(unp3
->unp_addr
, unp2
->unp_addr
,
1019 unp3
->unp_addrlen
= unp2
->unp_addrlen
;
1021 unp3
->unp_flags
= unp2
->unp_flags
;
1022 unp3
->unp_connid
.unp_pid
= l
->l_proc
->p_pid
;
1023 unp3
->unp_connid
.unp_euid
= kauth_cred_geteuid(l
->l_cred
);
1024 unp3
->unp_connid
.unp_egid
= kauth_cred_getegid(l
->l_cred
);
1025 unp3
->unp_flags
|= UNP_EIDSVALID
;
1026 if (unp2
->unp_flags
& UNP_EIDSBIND
) {
1027 unp
->unp_connid
= unp2
->unp_connid
;
1028 unp
->unp_flags
|= UNP_EIDSVALID
;
1032 error
= unp_connect2(so
, so2
, PRU_CONNECT
);
1037 free(sun
, M_SONAME
);
1039 unp
->unp_flags
&= ~UNP_BUSY
;
1044 unp_connect2(struct socket
*so
, struct socket
*so2
, int req
)
1046 struct unpcb
*unp
= sotounpcb(so
);
1049 if (so2
->so_type
!= so
->so_type
)
1050 return (EPROTOTYPE
);
1053 * All three sockets involved must be locked by same lock:
1055 * local endpoint (so)
1056 * remote endpoint (so2)
1057 * queue head (so->so_head, only if PR_CONNREQUIRED)
1059 KASSERT(solocked2(so
, so2
));
1060 KASSERT(so
->so_head
== NULL
);
1061 if (so2
->so_head
!= NULL
) {
1062 KASSERT(so2
->so_lock
== uipc_lock
);
1063 KASSERT(solocked2(so2
, so2
->so_head
));
1066 unp2
= sotounpcb(so2
);
1067 unp
->unp_conn
= unp2
;
1068 switch (so
->so_type
) {
1071 unp
->unp_nextref
= unp2
->unp_refs
;
1072 unp2
->unp_refs
= unp
;
1077 unp2
->unp_conn
= unp
;
1078 if (req
== PRU_CONNECT
&&
1079 ((unp
->unp_flags
| unp2
->unp_flags
) & UNP_CONNWAIT
))
1085 * If the connection is fully established, break the
1086 * association with uipc_lock and give the connected
1087 * pair a seperate lock to share. For CONNECT2, we
1088 * require that the locks already match (the sockets
1089 * are created that way).
1091 if (req
== PRU_CONNECT
) {
1092 KASSERT(so2
->so_head
!= NULL
);
1093 unp_setpeerlocks(so
, so2
);
1098 panic("unp_connect2");
1104 unp_disconnect(struct unpcb
*unp
)
1106 struct unpcb
*unp2
= unp
->unp_conn
;
1112 so
= unp
->unp_socket
;
1113 switch (so
->so_type
) {
1115 if (unp2
->unp_refs
== unp
)
1116 unp2
->unp_refs
= unp
->unp_nextref
;
1118 unp2
= unp2
->unp_refs
;
1120 KASSERT(solocked2(so
, unp2
->unp_socket
));
1122 panic("unp_disconnect");
1123 if (unp2
->unp_nextref
== unp
)
1125 unp2
= unp2
->unp_nextref
;
1127 unp2
->unp_nextref
= unp
->unp_nextref
;
1129 unp
->unp_nextref
= 0;
1130 so
->so_state
&= ~SS_ISCONNECTED
;
1134 KASSERT(solocked2(so
, unp2
->unp_socket
));
1135 soisdisconnected(so
);
1137 soisdisconnected(unp2
->unp_socket
);
1143 unp_abort(struct unpcb
*unp
)
1150 unp_shutdown(struct unpcb
*unp
)
1154 if (unp
->unp_socket
->so_type
== SOCK_STREAM
&& unp
->unp_conn
&&
1155 (so
= unp
->unp_conn
->unp_socket
))
1160 unp_drop(struct unpcb
*unp
, int errno
)
1162 struct socket
*so
= unp
->unp_socket
;
1164 KASSERT(solocked(so
));
1166 so
->so_error
= errno
;
1167 unp_disconnect(unp
);
1170 /* sofree() drops the socket lock */
1186 unp_externalize(struct mbuf
*rights
, struct lwp
*l
)
1188 struct cmsghdr
*cm
= mtod(rights
, struct cmsghdr
*);
1189 struct proc
*p
= l
->l_proc
;
1193 int nfds
, error
= 0;
1195 nfds
= (cm
->cmsg_len
- CMSG_ALIGN(sizeof(*cm
))) /
1197 rp
= (file_t
**)CMSG_DATA(cm
);
1199 fdp
= malloc(nfds
* sizeof(int), M_TEMP
, M_WAITOK
);
1200 rw_enter(&p
->p_cwdi
->cwdi_lock
, RW_READER
);
1202 /* Make sure the recipient should be able to see the files.. */
1203 if (p
->p_cwdi
->cwdi_rdir
!= NULL
) {
1204 rp
= (file_t
**)CMSG_DATA(cm
);
1205 for (i
= 0; i
< nfds
; i
++) {
1208 * If we are in a chroot'ed directory, and
1209 * someone wants to pass us a directory, make
1210 * sure it's inside the subtree we're allowed
1213 if (fp
->f_type
== DTYPE_VNODE
) {
1214 vnode_t
*vp
= (vnode_t
*)fp
->f_data
;
1215 if ((vp
->v_type
== VDIR
) &&
1216 !vn_isunder(vp
, p
->p_cwdi
->cwdi_rdir
, l
)) {
1225 rp
= (file_t
**)CMSG_DATA(cm
);
1227 for (i
= 0; i
< nfds
; i
++) {
1230 unp_discard_now(fp
);
1236 * First loop -- allocate file descriptor table slots for the
1239 for (i
= 0; i
< nfds
; i
++) {
1241 if ((error
= fd_alloc(p
, 0, &fdp
[i
])) != 0) {
1243 * Back out what we've done so far.
1245 for (--i
; i
>= 0; i
--) {
1246 fd_abort(p
, NULL
, fdp
[i
]);
1248 if (error
== ENOSPC
) {
1253 * This is the error that has historically
1254 * been returned, and some callers may
1264 * Now that adding them has succeeded, update all of the
1265 * file passing state and affix the descriptors.
1267 rp
= (file_t
**)CMSG_DATA(cm
);
1268 for (i
= 0; i
< nfds
; i
++) {
1270 atomic_dec_uint(&unp_rights
);
1271 fd_affix(p
, fp
, fdp
[i
]);
1272 mutex_enter(&fp
->f_lock
);
1274 mutex_exit(&fp
->f_lock
);
1276 * Note that fd_affix() adds a reference to the file.
1277 * The file may already have been closed by another
1278 * LWP in the process, so we must drop the reference
1279 * added by unp_internalize() with closef().
1285 * Copy temporary array to message and adjust length, in case of
1286 * transition from large file_t pointers to ints.
1288 memcpy(CMSG_DATA(cm
), fdp
, nfds
* sizeof(int));
1289 cm
->cmsg_len
= CMSG_LEN(nfds
* sizeof(int));
1290 rights
->m_len
= CMSG_SPACE(nfds
* sizeof(int));
1292 rw_exit(&p
->p_cwdi
->cwdi_lock
);
1298 unp_internalize(struct mbuf
**controlp
)
1300 filedesc_t
*fdescp
= curlwp
->l_fd
;
1301 struct mbuf
*control
= *controlp
;
1302 struct cmsghdr
*newcm
, *cm
= mtod(control
, struct cmsghdr
*);
1303 file_t
**rp
, **files
;
1312 /* Sanity check the control message header. */
1313 if (cm
->cmsg_type
!= SCM_RIGHTS
|| cm
->cmsg_level
!= SOL_SOCKET
||
1314 cm
->cmsg_len
> control
->m_len
||
1315 cm
->cmsg_len
< CMSG_ALIGN(sizeof(*cm
)))
1319 * Verify that the file descriptors are valid, and acquire
1320 * a reference to each.
1322 nfds
= (cm
->cmsg_len
- CMSG_ALIGN(sizeof(*cm
))) / sizeof(int);
1323 fdp
= (int *)CMSG_DATA(cm
);
1324 maxmsg
= maxfiles
/ unp_rights_ratio
;
1325 for (i
= 0; i
< nfds
; i
++) {
1327 if (atomic_inc_uint_nv(&unp_rights
) > maxmsg
) {
1328 atomic_dec_uint(&unp_rights
);
1333 if ((fp
= fd_getfile(fd
)) == NULL
) {
1334 atomic_dec_uint(&unp_rights
);
1341 /* Allocate new space and copy header into it. */
1342 newcm
= malloc(CMSG_SPACE(nfds
* sizeof(file_t
*)), M_MBUF
, M_WAITOK
);
1343 if (newcm
== NULL
) {
1347 memcpy(newcm
, cm
, sizeof(struct cmsghdr
));
1348 files
= (file_t
**)CMSG_DATA(newcm
);
1351 * Transform the file descriptors into file_t pointers, in
1352 * reverse order so that if pointers are bigger than ints, the
1353 * int won't get until we're done. No need to lock, as we have
1354 * already validated the descriptors with fd_getfile().
1356 fdp
= (int *)CMSG_DATA(cm
) + nfds
;
1358 for (i
= 0; i
< nfds
; i
++) {
1359 fp
= fdescp
->fd_dt
->dt_ff
[*--fdp
]->ff_file
;
1360 KASSERT(fp
!= NULL
);
1361 mutex_enter(&fp
->f_lock
);
1365 mutex_exit(&fp
->f_lock
);
1369 /* Release descriptor references. */
1370 fdp
= (int *)CMSG_DATA(cm
);
1371 for (i
= 0; i
< nfds
; i
++) {
1374 atomic_dec_uint(&unp_rights
);
1379 if (control
->m_flags
& M_EXT
) {
1381 *controlp
= control
= m_get(M_WAIT
, MT_CONTROL
);
1383 MEXTADD(control
, newcm
, CMSG_SPACE(nfds
* sizeof(file_t
*)),
1384 M_MBUF
, NULL
, NULL
);
1387 * Adjust message & mbuf to note amount of space
1390 cm
->cmsg_len
= CMSG_LEN(nfds
* sizeof(file_t
*));
1391 control
->m_len
= CMSG_SPACE(nfds
* sizeof(file_t
*));
1398 unp_addsockcred(struct lwp
*l
, struct mbuf
*control
)
1400 struct cmsghdr
*cmp
;
1401 struct sockcred
*sc
;
1405 len
= CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l
->l_cred
)));
1406 space
= CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l
->l_cred
)));
1408 m
= m_get(M_WAIT
, MT_CONTROL
);
1410 if (space
> MCLBYTES
)
1411 MEXTMALLOC(m
, space
, M_WAITOK
);
1414 if ((m
->m_flags
& M_EXT
) == 0) {
1422 cmp
= mtod(m
, struct cmsghdr
*);
1423 sc
= (struct sockcred
*)CMSG_DATA(cmp
);
1424 cmp
->cmsg_len
= len
;
1425 cmp
->cmsg_level
= SOL_SOCKET
;
1426 cmp
->cmsg_type
= SCM_CREDS
;
1427 sc
->sc_uid
= kauth_cred_getuid(l
->l_cred
);
1428 sc
->sc_euid
= kauth_cred_geteuid(l
->l_cred
);
1429 sc
->sc_gid
= kauth_cred_getgid(l
->l_cred
);
1430 sc
->sc_egid
= kauth_cred_getegid(l
->l_cred
);
1431 sc
->sc_ngroups
= kauth_cred_ngroups(l
->l_cred
);
1432 for (i
= 0; i
< sc
->sc_ngroups
; i
++)
1433 sc
->sc_groups
[i
] = kauth_cred_group(l
->l_cred
, i
);
1436 * If a control message already exists, append us to the end.
1438 if (control
!= NULL
) {
1439 for (n
= control
; n
->m_next
!= NULL
; n
= n
->m_next
)
1449 * Do a mark-sweep GC of files in the system, to free up any which are
1450 * caught in flight to an about-to-be-closed socket. Additionally,
1451 * process deferred file closures.
1456 extern struct domain unixdomain
;
1458 struct socket
*so
, *so1
;
1462 KASSERT(curlwp
== unp_thread_lwp
);
1463 KASSERT(mutex_owned(&filelist_lock
));
1466 * First, process deferred file closures.
1468 while (!SLIST_EMPTY(&unp_thread_discard
)) {
1469 fp
= SLIST_FIRST(&unp_thread_discard
);
1470 KASSERT(fp
->f_unpcount
> 0);
1471 KASSERT(fp
->f_count
> 0);
1472 KASSERT(fp
->f_msgcount
> 0);
1473 KASSERT(fp
->f_count
>= fp
->f_unpcount
);
1474 KASSERT(fp
->f_count
>= fp
->f_msgcount
);
1475 KASSERT(fp
->f_msgcount
>= fp
->f_unpcount
);
1476 SLIST_REMOVE_HEAD(&unp_thread_discard
, f_unplist
);
1479 mutex_exit(&filelist_lock
);
1480 for (; i
!= 0; i
--) {
1481 unp_discard_now(fp
);
1483 mutex_enter(&filelist_lock
);
1487 * Clear mark bits. Ensure that we don't consider new files
1488 * entering the file table during this loop (they will not have
1492 LIST_FOREACH(fp
, &filehead
, f_list
) {
1493 for (old
= fp
->f_flag
;; old
= new) {
1494 new = atomic_cas_uint(&fp
->f_flag
, old
,
1495 (old
| FSCAN
) & ~(FMARK
|FDEFER
));
1496 if (__predict_true(old
== new)) {
1503 * Iterate over the set of sockets, marking ones believed (based on
1504 * refcount) to be referenced from a process, and marking for rescan
1505 * sockets which are queued on a socket. Recan continues descending
1506 * and searching for sockets referenced by sockets (FDEFER), until
1507 * there are no more socket->socket references to be discovered.
1511 for (fp
= LIST_FIRST(&filehead
); fp
!= NULL
; fp
= np
) {
1512 KASSERT(mutex_owned(&filelist_lock
));
1513 np
= LIST_NEXT(fp
, f_list
);
1514 mutex_enter(&fp
->f_lock
);
1515 if ((fp
->f_flag
& FDEFER
) != 0) {
1516 atomic_and_uint(&fp
->f_flag
, ~FDEFER
);
1518 KASSERT(fp
->f_count
!= 0);
1520 if (fp
->f_count
== 0 ||
1521 (fp
->f_flag
& FMARK
) != 0 ||
1522 fp
->f_count
== fp
->f_msgcount
||
1523 fp
->f_unpcount
!= 0) {
1524 mutex_exit(&fp
->f_lock
);
1528 atomic_or_uint(&fp
->f_flag
, FMARK
);
1530 if (fp
->f_type
!= DTYPE_SOCKET
||
1531 (so
= fp
->f_data
) == NULL
||
1532 so
->so_proto
->pr_domain
!= &unixdomain
||
1533 (so
->so_proto
->pr_flags
& PR_RIGHTS
) == 0) {
1534 mutex_exit(&fp
->f_lock
);
1538 /* Gain file ref, mark our position, and unlock. */
1540 LIST_INSERT_AFTER(fp
, dp
, f_list
);
1542 mutex_exit(&fp
->f_lock
);
1543 mutex_exit(&filelist_lock
);
1546 * Mark files referenced from sockets queued on the
1547 * accept queue as well.
1550 unp_scan(so
->so_rcv
.sb_mb
, unp_mark
, 0);
1551 if ((so
->so_options
& SO_ACCEPTCONN
) != 0) {
1552 TAILQ_FOREACH(so1
, &so
->so_q0
, so_qe
) {
1553 unp_scan(so1
->so_rcv
.sb_mb
, unp_mark
, 0);
1555 TAILQ_FOREACH(so1
, &so
->so_q
, so_qe
) {
1556 unp_scan(so1
->so_rcv
.sb_mb
, unp_mark
, 0);
1561 /* Re-lock and restart from where we left off. */
1563 mutex_enter(&filelist_lock
);
1564 np
= LIST_NEXT(dp
, f_list
);
1565 LIST_REMOVE(dp
, f_list
);
1568 * Bail early if we did nothing in the loop above. Could
1569 * happen because of concurrent activity causing unp_defer
1570 * to get out of sync.
1572 } while (unp_defer
!= 0 && didwork
);
1577 * We grab an extra reference to each of the files that are
1578 * not otherwise accessible and then free the rights that are
1579 * stored in messages on them.
1581 for (fp
= LIST_FIRST(&filehead
); fp
!= NULL
; fp
= np
) {
1582 KASSERT(mutex_owned(&filelist_lock
));
1583 np
= LIST_NEXT(fp
, f_list
);
1584 mutex_enter(&fp
->f_lock
);
1587 * Ignore non-sockets.
1588 * Ignore dead sockets, or sockets with pending close.
1589 * Ignore sockets obviously referenced elsewhere.
1590 * Ignore sockets marked as referenced by our scan.
1591 * Ignore new sockets that did not exist during the scan.
1593 if (fp
->f_type
!= DTYPE_SOCKET
||
1594 fp
->f_count
== 0 || fp
->f_unpcount
!= 0 ||
1595 fp
->f_count
!= fp
->f_msgcount
||
1596 (fp
->f_flag
& (FMARK
| FSCAN
)) != FSCAN
) {
1597 mutex_exit(&fp
->f_lock
);
1601 /* Gain file ref, mark our position, and unlock. */
1602 LIST_INSERT_AFTER(fp
, dp
, f_list
);
1604 mutex_exit(&fp
->f_lock
);
1605 mutex_exit(&filelist_lock
);
1608 * Flush all data from the socket's receive buffer.
1609 * This will cause files referenced only by the
1610 * socket to be queued for close.
1617 /* Re-lock and restart from where we left off. */
1619 mutex_enter(&filelist_lock
);
1620 np
= LIST_NEXT(dp
, f_list
);
1621 LIST_REMOVE(dp
, f_list
);
1626 * Garbage collector thread. While SCM_RIGHTS messages are in transit,
1627 * wake once per second to garbage collect. Run continually while we
1628 * have deferred closes to process.
1631 unp_thread(void *cookie
)
1635 /* Allocate a dummy file for our scans. */
1636 if ((dp
= fgetdummy()) == NULL
) {
1637 panic("unp_thread");
1640 mutex_enter(&filelist_lock
);
1642 KASSERT(mutex_owned(&filelist_lock
));
1643 if (SLIST_EMPTY(&unp_thread_discard
)) {
1644 if (unp_rights
!= 0) {
1645 (void)cv_timedwait(&unp_thread_cv
,
1646 &filelist_lock
, hz
);
1648 cv_wait(&unp_thread_cv
, &filelist_lock
);
1657 * Kick the garbage collector into action if there is something for
1661 unp_thread_kick(void)
1664 if (!SLIST_EMPTY(&unp_thread_discard
) || unp_rights
!= 0) {
1665 mutex_enter(&filelist_lock
);
1666 cv_signal(&unp_thread_cv
);
1667 mutex_exit(&filelist_lock
);
1672 unp_dispose(struct mbuf
*m
)
1676 unp_scan(m
, unp_discard_later
, 1);
1680 unp_scan(struct mbuf
*m0
, void (*op
)(file_t
*), int discard
)
1688 for (m
= m0
; m
; m
= m
->m_next
) {
1689 if (m
->m_type
!= MT_CONTROL
||
1690 m
->m_len
< sizeof(*cm
)) {
1693 cm
= mtod(m
, struct cmsghdr
*);
1694 if (cm
->cmsg_level
!= SOL_SOCKET
||
1695 cm
->cmsg_type
!= SCM_RIGHTS
)
1697 qfds
= (cm
->cmsg_len
- CMSG_ALIGN(sizeof(*cm
)))
1699 rp
= (file_t
**)CMSG_DATA(cm
);
1700 for (i
= 0; i
< qfds
; i
++) {
1714 unp_mark(file_t
*fp
)
1720 /* If we're already deferred, don't screw up the defer count */
1721 mutex_enter(&fp
->f_lock
);
1722 if (fp
->f_flag
& (FMARK
| FDEFER
)) {
1723 mutex_exit(&fp
->f_lock
);
1728 * Minimize the number of deferrals... Sockets are the only type of
1729 * file which can hold references to another file, so just mark
1730 * other files, and defer unmarked sockets for the next pass.
1732 if (fp
->f_type
== DTYPE_SOCKET
) {
1734 KASSERT(fp
->f_count
!= 0);
1735 atomic_or_uint(&fp
->f_flag
, FDEFER
);
1737 atomic_or_uint(&fp
->f_flag
, FMARK
);
1739 mutex_exit(&fp
->f_lock
);
1743 unp_discard_now(file_t
*fp
)
1749 KASSERT(fp
->f_count
> 0);
1750 KASSERT(fp
->f_msgcount
> 0);
1752 mutex_enter(&fp
->f_lock
);
1754 mutex_exit(&fp
->f_lock
);
1755 atomic_dec_uint(&unp_rights
);
1760 unp_discard_later(file_t
*fp
)
1766 KASSERT(fp
->f_count
> 0);
1767 KASSERT(fp
->f_msgcount
> 0);
1769 mutex_enter(&filelist_lock
);
1770 if (fp
->f_unpcount
++ == 0) {
1771 SLIST_INSERT_HEAD(&unp_thread_discard
, fp
, f_unplist
);
1773 mutex_exit(&filelist_lock
);