Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.119
      1  1.119     pooka /*	$NetBSD: uipc_usrreq.c,v 1.119 2008/10/11 13:40:57 pooka Exp $	*/
      2   1.30   thorpej 
      3   1.30   thorpej /*-
      4  1.106        ad  * Copyright (c) 1998, 2000, 2004, 2008 The NetBSD Foundation, Inc.
      5   1.30   thorpej  * All rights reserved.
      6   1.30   thorpej  *
      7   1.30   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.30   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.30   thorpej  * NASA Ames Research Center.
     10   1.30   thorpej  *
     11   1.30   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.30   thorpej  * modification, are permitted provided that the following conditions
     13   1.30   thorpej  * are met:
     14   1.30   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.30   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.30   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.30   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.30   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.30   thorpej  *
     20   1.30   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.30   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.30   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.30   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.30   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.30   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.30   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.30   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.30   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.30   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.30   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.30   thorpej  */
     32   1.10       cgd 
     33    1.1       cgd /*
     34    1.8   mycroft  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35    1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     36    1.1       cgd  *
     37    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38    1.1       cgd  * modification, are permitted provided that the following conditions
     39    1.1       cgd  * are met:
     40    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45   1.67       agc  * 3. Neither the name of the University nor the names of its contributors
     46   1.67       agc  *    may be used to endorse or promote products derived from this software
     47   1.67       agc  *    without specific prior written permission.
     48   1.67       agc  *
     49   1.67       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50   1.67       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51   1.67       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52   1.67       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53   1.67       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54   1.67       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55   1.67       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56   1.67       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57   1.67       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58   1.67       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59   1.67       agc  * SUCH DAMAGE.
     60   1.67       agc  *
     61   1.67       agc  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     62   1.67       agc  */
     63   1.67       agc 
     64   1.67       agc /*
     65   1.67       agc  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     66   1.67       agc  *
     67   1.67       agc  * Redistribution and use in source and binary forms, with or without
     68   1.67       agc  * modification, are permitted provided that the following conditions
     69   1.67       agc  * are met:
     70   1.67       agc  * 1. Redistributions of source code must retain the above copyright
     71   1.67       agc  *    notice, this list of conditions and the following disclaimer.
     72   1.67       agc  * 2. Redistributions in binary form must reproduce the above copyright
     73   1.67       agc  *    notice, this list of conditions and the following disclaimer in the
     74   1.67       agc  *    documentation and/or other materials provided with the distribution.
     75    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     76    1.1       cgd  *    must display the following acknowledgement:
     77    1.1       cgd  *	This product includes software developed by the University of
     78    1.1       cgd  *	California, Berkeley and its contributors.
     79    1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     80    1.1       cgd  *    may be used to endorse or promote products derived from this software
     81    1.1       cgd  *    without specific prior written permission.
     82    1.1       cgd  *
     83    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     84    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     85    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     86    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     87    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     88    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     89    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     90    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     91    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     92    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     93    1.1       cgd  * SUCH DAMAGE.
     94    1.1       cgd  *
     95   1.31      fvdl  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     96    1.1       cgd  */
     97   1.53     lukem 
     98   1.53     lukem #include <sys/cdefs.h>
     99  1.119     pooka __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.119 2008/10/11 13:40:57 pooka Exp $");
    100    1.1       cgd 
    101    1.7   mycroft #include <sys/param.h>
    102    1.8   mycroft #include <sys/systm.h>
    103    1.7   mycroft #include <sys/proc.h>
    104    1.7   mycroft #include <sys/filedesc.h>
    105    1.7   mycroft #include <sys/domain.h>
    106    1.7   mycroft #include <sys/protosw.h>
    107    1.7   mycroft #include <sys/socket.h>
    108    1.7   mycroft #include <sys/socketvar.h>
    109    1.7   mycroft #include <sys/unpcb.h>
    110    1.7   mycroft #include <sys/un.h>
    111    1.7   mycroft #include <sys/namei.h>
    112    1.7   mycroft #include <sys/vnode.h>
    113    1.7   mycroft #include <sys/file.h>
    114    1.7   mycroft #include <sys/stat.h>
    115    1.7   mycroft #include <sys/mbuf.h>
    116   1.91      elad #include <sys/kauth.h>
    117  1.101        ad #include <sys/kmem.h>
    118  1.106        ad #include <sys/atomic.h>
    119  1.119     pooka #include <sys/uidinfo.h>
    120    1.1       cgd 
    121    1.1       cgd /*
    122    1.1       cgd  * Unix communications domain.
    123    1.1       cgd  *
    124    1.1       cgd  * TODO:
    125    1.1       cgd  *	SEQPACKET, RDM
    126    1.1       cgd  *	rethink name space problems
    127    1.1       cgd  *	need a proper out-of-band
    128  1.112        ad  *
    129  1.112        ad  * Notes on locking:
    130  1.112        ad  *
    131  1.112        ad  * The generic rules noted in uipc_socket2.c apply.  In addition:
    132  1.112        ad  *
    133  1.112        ad  * o We have a global lock, uipc_lock.
    134  1.112        ad  *
    135  1.112        ad  * o All datagram sockets are locked by uipc_lock.
    136  1.112        ad  *
    137  1.112        ad  * o For stream socketpairs, the two endpoints are created sharing the same
    138  1.112        ad  *   independent lock.  Sockets presented to PRU_CONNECT2 must already have
    139  1.112        ad  *   matching locks.
    140  1.112        ad  *
    141  1.112        ad  * o Stream sockets created via socket() start life with their own
    142  1.112        ad  *   independent lock.
    143  1.112        ad  *
    144  1.112        ad  * o Stream connections to a named endpoint are slightly more complicated.
    145  1.112        ad  *   Sockets that have called listen() have their lock pointer mutated to
    146  1.112        ad  *   the global uipc_lock.  When establishing a connection, the connecting
    147  1.112        ad  *   socket also has its lock mutated to uipc_lock, which matches the head
    148  1.112        ad  *   (listening socket).  We create a new socket for accept() to return, and
    149  1.112        ad  *   that also shares the head's lock.  Until the connection is completely
    150  1.112        ad  *   done on both ends, all three sockets are locked by uipc_lock.  Once the
    151  1.112        ad  *   connection is complete, the association with the head's lock is broken.
    152  1.112        ad  *   The connecting socket and the socket returned from accept() have their
    153  1.112        ad  *   lock pointers mutated away from uipc_lock, and back to the connecting
    154  1.112        ad  *   socket's original, independent lock.  The head continues to be locked
    155  1.112        ad  *   by uipc_lock.
    156  1.112        ad  *
    157  1.112        ad  * o If uipc_lock is determined to be a significant source of contention,
    158  1.112        ad  *   it could easily be hashed out.  It is difficult to simply make it an
    159  1.112        ad  *   independent lock because of visibility / garbage collection issues:
    160  1.112        ad  *   if a socket has been associated with a lock at any point, that lock
    161  1.112        ad  *   must remain valid until the socket is no longer visible in the system.
    162  1.112        ad  *   The lock must not be freed or otherwise destroyed until any sockets
    163  1.112        ad  *   that had referenced it have also been destroyed.
    164    1.1       cgd  */
    165   1.93  christos const struct sockaddr_un sun_noname = {
    166   1.93  christos 	.sun_len = sizeof(sun_noname),
    167   1.93  christos 	.sun_family = AF_LOCAL,
    168   1.93  christos };
    169    1.1       cgd ino_t	unp_ino;			/* prototype for fake inode numbers */
    170    1.1       cgd 
    171   1.92        ad struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
    172  1.112        ad static kmutex_t *uipc_lock;
    173  1.112        ad 
    174  1.112        ad /*
    175  1.112        ad  * Initialize Unix protocols.
    176  1.112        ad  */
    177  1.112        ad void
    178  1.112        ad uipc_init(void)
    179  1.112        ad {
    180  1.112        ad 
    181  1.112        ad 	uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    182  1.112        ad }
    183  1.112        ad 
    184  1.112        ad /*
    185  1.112        ad  * A connection succeeded: disassociate both endpoints from the head's
    186  1.112        ad  * lock, and make them share their own lock.  There is a race here: for
    187  1.112        ad  * a very brief time one endpoint will be locked by a different lock
    188  1.112        ad  * than the other end.  However, since the current thread holds the old
    189  1.112        ad  * lock (the listening socket's lock, the head) access can still only be
    190  1.112        ad  * made to one side of the connection.
    191  1.112        ad  */
    192  1.112        ad static void
    193  1.112        ad unp_setpeerlocks(struct socket *so, struct socket *so2)
    194  1.112        ad {
    195  1.112        ad 	struct unpcb *unp;
    196  1.112        ad 	kmutex_t *lock;
    197  1.112        ad 
    198  1.112        ad 	KASSERT(solocked2(so, so2));
    199  1.112        ad 
    200  1.112        ad 	/*
    201  1.112        ad 	 * Bail out if either end of the socket is not yet fully
    202  1.112        ad 	 * connected or accepted.  We only break the lock association
    203  1.112        ad 	 * with the head when the pair of sockets stand completely
    204  1.112        ad 	 * on their own.
    205  1.112        ad 	 */
    206  1.112        ad 	if (so->so_head != NULL || so2->so_head != NULL)
    207  1.112        ad 		return;
    208  1.112        ad 
    209  1.112        ad 	/*
    210  1.112        ad 	 * Drop references to old lock.  A third reference (from the
    211  1.112        ad 	 * queue head) must be held as we still hold its lock.  Bonus:
    212  1.112        ad 	 * we don't need to worry about garbage collecting the lock.
    213  1.112        ad 	 */
    214  1.112        ad 	lock = so->so_lock;
    215  1.112        ad 	KASSERT(lock == uipc_lock);
    216  1.112        ad 	mutex_obj_free(lock);
    217  1.112        ad 	mutex_obj_free(lock);
    218  1.112        ad 
    219  1.112        ad 	/*
    220  1.112        ad 	 * Grab stream lock from the initiator and share between the two
    221  1.112        ad 	 * endpoints.  Issue memory barrier to ensure all modifications
    222  1.112        ad 	 * become globally visible before the lock change.  so2 is
    223  1.112        ad 	 * assumed not to have a stream lock, because it was created
    224  1.112        ad 	 * purely for the server side to accept this connection and
    225  1.112        ad 	 * started out life using the domain-wide lock.
    226  1.112        ad 	 */
    227  1.112        ad 	unp = sotounpcb(so);
    228  1.112        ad 	KASSERT(unp->unp_streamlock != NULL);
    229  1.112        ad 	KASSERT(sotounpcb(so2)->unp_streamlock == NULL);
    230  1.112        ad 	lock = unp->unp_streamlock;
    231  1.112        ad 	unp->unp_streamlock = NULL;
    232  1.112        ad 	mutex_obj_hold(lock);
    233  1.112        ad 	membar_exit();
    234  1.115        ad 	solockreset(so, lock);
    235  1.115        ad 	solockreset(so2, lock);
    236  1.112        ad }
    237  1.112        ad 
    238  1.112        ad /*
    239  1.112        ad  * Reset a socket's lock back to the domain-wide lock.
    240  1.112        ad  */
    241  1.112        ad static void
    242  1.112        ad unp_resetlock(struct socket *so)
    243  1.112        ad {
    244  1.112        ad 	kmutex_t *olock, *nlock;
    245  1.112        ad 	struct unpcb *unp;
    246  1.112        ad 
    247  1.112        ad 	KASSERT(solocked(so));
    248  1.112        ad 
    249  1.112        ad 	olock = so->so_lock;
    250  1.112        ad 	nlock = uipc_lock;
    251  1.112        ad 	if (olock == nlock)
    252  1.112        ad 		return;
    253  1.112        ad 	unp = sotounpcb(so);
    254  1.112        ad 	KASSERT(unp->unp_streamlock == NULL);
    255  1.112        ad 	unp->unp_streamlock = olock;
    256  1.112        ad 	mutex_obj_hold(nlock);
    257  1.112        ad 	mutex_enter(nlock);
    258  1.115        ad 	solockreset(so, nlock);
    259  1.112        ad 	mutex_exit(olock);
    260  1.112        ad }
    261  1.112        ad 
    262  1.112        ad static void
    263  1.112        ad unp_free(struct unpcb *unp)
    264  1.112        ad {
    265  1.112        ad 
    266  1.112        ad 	if (unp->unp_addr)
    267  1.112        ad 		free(unp->unp_addr, M_SONAME);
    268  1.112        ad 	if (unp->unp_streamlock != NULL)
    269  1.112        ad 		mutex_obj_free(unp->unp_streamlock);
    270  1.112        ad 	free(unp, M_PCB);
    271  1.112        ad }
    272   1.30   thorpej 
    273   1.20   mycroft int
    274   1.76      matt unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp,
    275   1.92        ad 	struct lwp *l)
    276   1.20   mycroft {
    277   1.20   mycroft 	struct socket *so2;
    278   1.77      matt 	const struct sockaddr_un *sun;
    279   1.20   mycroft 
    280   1.20   mycroft 	so2 = unp->unp_conn->unp_socket;
    281  1.112        ad 
    282  1.112        ad 	KASSERT(solocked(so2));
    283  1.112        ad 
    284   1.20   mycroft 	if (unp->unp_addr)
    285   1.20   mycroft 		sun = unp->unp_addr;
    286   1.20   mycroft 	else
    287   1.20   mycroft 		sun = &sun_noname;
    288   1.30   thorpej 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
    289   1.92        ad 		control = unp_addsockcred(l, control);
    290   1.82  christos 	if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m,
    291   1.20   mycroft 	    control) == 0) {
    292  1.112        ad 		so2->so_rcv.sb_overflowed++;
    293  1.112        ad 	    	sounlock(so2);
    294   1.98    martin 		unp_dispose(control);
    295   1.20   mycroft 		m_freem(control);
    296   1.20   mycroft 		m_freem(m);
    297  1.112        ad 	    	solock(so2);
    298   1.60  christos 		return (ENOBUFS);
    299   1.20   mycroft 	} else {
    300   1.20   mycroft 		sorwakeup(so2);
    301   1.20   mycroft 		return (0);
    302   1.20   mycroft 	}
    303   1.20   mycroft }
    304   1.20   mycroft 
    305   1.20   mycroft void
    306  1.112        ad unp_setaddr(struct socket *so, struct mbuf *nam, bool peeraddr)
    307   1.20   mycroft {
    308   1.77      matt 	const struct sockaddr_un *sun;
    309  1.112        ad 	struct unpcb *unp;
    310  1.112        ad 	bool ext;
    311   1.20   mycroft 
    312  1.112        ad 	unp = sotounpcb(so);
    313  1.112        ad 	ext = false;
    314   1.20   mycroft 
    315  1.112        ad 	for (;;) {
    316  1.112        ad 		sun = NULL;
    317  1.112        ad 		if (peeraddr) {
    318  1.112        ad 			if (unp->unp_conn && unp->unp_conn->unp_addr)
    319  1.112        ad 				sun = unp->unp_conn->unp_addr;
    320  1.112        ad 		} else {
    321  1.112        ad 			if (unp->unp_addr)
    322  1.112        ad 				sun = unp->unp_addr;
    323  1.112        ad 		}
    324  1.112        ad 		if (sun == NULL)
    325  1.112        ad 			sun = &sun_noname;
    326  1.112        ad 		nam->m_len = sun->sun_len;
    327  1.112        ad 		if (nam->m_len > MLEN && !ext) {
    328  1.112        ad 			sounlock(so);
    329  1.112        ad 			MEXTMALLOC(nam, MAXPATHLEN * 2, M_WAITOK);
    330  1.112        ad 			solock(so);
    331  1.112        ad 			ext = true;
    332  1.112        ad 		} else {
    333  1.112        ad 			KASSERT(nam->m_len <= MAXPATHLEN * 2);
    334  1.112        ad 			memcpy(mtod(nam, void *), sun, (size_t)nam->m_len);
    335  1.112        ad 			break;
    336  1.112        ad 		}
    337  1.112        ad 	}
    338   1.20   mycroft }
    339   1.20   mycroft 
    340    1.1       cgd /*ARGSUSED*/
    341    1.5    andrew int
    342   1.76      matt uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    343   1.86  christos 	struct mbuf *control, struct lwp *l)
    344    1.1       cgd {
    345    1.1       cgd 	struct unpcb *unp = sotounpcb(so);
    346   1.46  augustss 	struct socket *so2;
    347   1.86  christos 	struct proc *p;
    348   1.75  christos 	u_int newhiwat;
    349   1.46  augustss 	int error = 0;
    350    1.1       cgd 
    351    1.1       cgd 	if (req == PRU_CONTROL)
    352    1.1       cgd 		return (EOPNOTSUPP);
    353   1.20   mycroft 
    354   1.22   mycroft #ifdef DIAGNOSTIC
    355   1.22   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    356   1.22   mycroft 		panic("uipc_usrreq: unexpected control mbuf");
    357   1.22   mycroft #endif
    358   1.86  christos 	p = l ? l->l_proc : NULL;
    359  1.112        ad 	if (req != PRU_ATTACH) {
    360  1.112        ad 		if (unp == 0) {
    361  1.112        ad 			error = EINVAL;
    362  1.112        ad 			goto release;
    363  1.112        ad 		}
    364  1.112        ad 		KASSERT(solocked(so));
    365    1.1       cgd 	}
    366   1.20   mycroft 
    367    1.1       cgd 	switch (req) {
    368    1.1       cgd 
    369    1.1       cgd 	case PRU_ATTACH:
    370   1.20   mycroft 		if (unp != 0) {
    371    1.1       cgd 			error = EISCONN;
    372    1.1       cgd 			break;
    373    1.1       cgd 		}
    374    1.1       cgd 		error = unp_attach(so);
    375    1.1       cgd 		break;
    376    1.1       cgd 
    377    1.1       cgd 	case PRU_DETACH:
    378    1.1       cgd 		unp_detach(unp);
    379    1.1       cgd 		break;
    380    1.1       cgd 
    381    1.1       cgd 	case PRU_BIND:
    382   1.90  christos 		KASSERT(l != NULL);
    383  1.112        ad 		error = unp_bind(so, nam, l);
    384    1.1       cgd 		break;
    385    1.1       cgd 
    386    1.1       cgd 	case PRU_LISTEN:
    387  1.112        ad 		/*
    388  1.112        ad 		 * If the socket can accept a connection, it must be
    389  1.112        ad 		 * locked by uipc_lock.
    390  1.112        ad 		 */
    391  1.112        ad 		unp_resetlock(so);
    392    1.1       cgd 		if (unp->unp_vnode == 0)
    393    1.1       cgd 			error = EINVAL;
    394    1.1       cgd 		break;
    395    1.1       cgd 
    396    1.1       cgd 	case PRU_CONNECT:
    397   1.90  christos 		KASSERT(l != NULL);
    398   1.86  christos 		error = unp_connect(so, nam, l);
    399    1.1       cgd 		break;
    400    1.1       cgd 
    401    1.1       cgd 	case PRU_CONNECT2:
    402   1.72      matt 		error = unp_connect2(so, (struct socket *)nam, PRU_CONNECT2);
    403    1.1       cgd 		break;
    404    1.1       cgd 
    405    1.1       cgd 	case PRU_DISCONNECT:
    406    1.1       cgd 		unp_disconnect(unp);
    407    1.1       cgd 		break;
    408    1.1       cgd 
    409    1.1       cgd 	case PRU_ACCEPT:
    410  1.112        ad 		KASSERT(so->so_lock == uipc_lock);
    411   1.72      matt 		/*
    412   1.72      matt 		 * Mark the initiating STREAM socket as connected *ONLY*
    413   1.72      matt 		 * after it's been accepted.  This prevents a client from
    414   1.72      matt 		 * overrunning a server and receiving ECONNREFUSED.
    415   1.72      matt 		 */
    416  1.112        ad 		if (unp->unp_conn == NULL)
    417  1.112        ad 			break;
    418  1.112        ad 		so2 = unp->unp_conn->unp_socket;
    419  1.112        ad 		if (so2->so_state & SS_ISCONNECTING) {
    420  1.112        ad 			KASSERT(solocked2(so, so->so_head));
    421  1.112        ad 			KASSERT(solocked2(so2, so->so_head));
    422  1.112        ad 			soisconnected(so2);
    423  1.112        ad 		}
    424  1.112        ad 		/*
    425  1.112        ad 		 * If the connection is fully established, break the
    426  1.112        ad 		 * association with uipc_lock and give the connected
    427  1.112        ad 		 * pair a seperate lock to share.
    428  1.112        ad 		 */
    429  1.112        ad 		unp_setpeerlocks(so2, so);
    430  1.112        ad 		/*
    431  1.112        ad 		 * Only now return peer's address, as we may need to
    432  1.112        ad 		 * block in order to allocate memory.
    433  1.112        ad 		 *
    434  1.112        ad 		 * XXX Minor race: connection can be broken while
    435  1.112        ad 		 * lock is dropped in unp_setaddr().  We will return
    436  1.112        ad 		 * error == 0 and sun_noname as the peer address.
    437  1.112        ad 		 */
    438  1.112        ad 		unp_setaddr(so, nam, true);
    439    1.1       cgd 		break;
    440    1.1       cgd 
    441    1.1       cgd 	case PRU_SHUTDOWN:
    442    1.1       cgd 		socantsendmore(so);
    443    1.1       cgd 		unp_shutdown(unp);
    444    1.1       cgd 		break;
    445    1.1       cgd 
    446    1.1       cgd 	case PRU_RCVD:
    447    1.1       cgd 		switch (so->so_type) {
    448    1.1       cgd 
    449    1.1       cgd 		case SOCK_DGRAM:
    450    1.1       cgd 			panic("uipc 1");
    451    1.1       cgd 			/*NOTREACHED*/
    452    1.1       cgd 
    453    1.1       cgd 		case SOCK_STREAM:
    454    1.1       cgd #define	rcv (&so->so_rcv)
    455    1.1       cgd #define snd (&so2->so_snd)
    456    1.1       cgd 			if (unp->unp_conn == 0)
    457    1.1       cgd 				break;
    458    1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    459  1.112        ad 			KASSERT(solocked2(so, so2));
    460    1.1       cgd 			/*
    461    1.1       cgd 			 * Adjust backpressure on sender
    462    1.1       cgd 			 * and wakeup any waiting to write.
    463    1.1       cgd 			 */
    464    1.1       cgd 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    465    1.1       cgd 			unp->unp_mbcnt = rcv->sb_mbcnt;
    466   1.75  christos 			newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc;
    467   1.81  christos 			(void)chgsbsize(so2->so_uidinfo,
    468   1.75  christos 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    469    1.1       cgd 			unp->unp_cc = rcv->sb_cc;
    470    1.1       cgd 			sowwakeup(so2);
    471    1.1       cgd #undef snd
    472    1.1       cgd #undef rcv
    473    1.1       cgd 			break;
    474    1.1       cgd 
    475    1.1       cgd 		default:
    476    1.1       cgd 			panic("uipc 2");
    477    1.1       cgd 		}
    478    1.1       cgd 		break;
    479    1.1       cgd 
    480    1.1       cgd 	case PRU_SEND:
    481   1.30   thorpej 		/*
    482   1.30   thorpej 		 * Note: unp_internalize() rejects any control message
    483   1.30   thorpej 		 * other than SCM_RIGHTS, and only allows one.  This
    484   1.30   thorpej 		 * has the side-effect of preventing a caller from
    485   1.30   thorpej 		 * forging SCM_CREDS.
    486   1.30   thorpej 		 */
    487   1.90  christos 		if (control) {
    488  1.112        ad 			sounlock(so);
    489  1.112        ad 			error = unp_internalize(&control);
    490  1.112        ad 			solock(so);
    491  1.112        ad 			if (error != 0) {
    492  1.111   mlelstv 				m_freem(control);
    493  1.111   mlelstv 				m_freem(m);
    494  1.111   mlelstv 				break;
    495  1.111   mlelstv 			}
    496   1.83      yamt 		}
    497    1.1       cgd 		switch (so->so_type) {
    498    1.1       cgd 
    499    1.1       cgd 		case SOCK_DGRAM: {
    500  1.112        ad 			KASSERT(so->so_lock == uipc_lock);
    501    1.1       cgd 			if (nam) {
    502  1.111   mlelstv 				if ((so->so_state & SS_ISCONNECTED) != 0)
    503    1.1       cgd 					error = EISCONN;
    504  1.111   mlelstv 				else {
    505  1.112        ad 					/*
    506  1.112        ad 					 * Note: once connected, the
    507  1.112        ad 					 * socket's lock must not be
    508  1.112        ad 					 * dropped until we have sent
    509  1.112        ad 					 * the message and disconnected.
    510  1.112        ad 					 * This is necessary to prevent
    511  1.112        ad 					 * intervening control ops, like
    512  1.112        ad 					 * another connection.
    513  1.112        ad 					 */
    514  1.111   mlelstv 					error = unp_connect(so, nam, l);
    515   1.20   mycroft 				}
    516    1.1       cgd 			} else {
    517  1.111   mlelstv 				if ((so->so_state & SS_ISCONNECTED) == 0)
    518    1.1       cgd 					error = ENOTCONN;
    519  1.111   mlelstv 			}
    520  1.111   mlelstv 			if (error) {
    521  1.112        ad 				sounlock(so);
    522  1.111   mlelstv 				unp_dispose(control);
    523  1.111   mlelstv 				m_freem(control);
    524  1.111   mlelstv 				m_freem(m);
    525  1.112        ad 				solock(so);
    526  1.111   mlelstv 				break;
    527    1.1       cgd 			}
    528   1.89  christos 			KASSERT(p != NULL);
    529   1.92        ad 			error = unp_output(m, control, unp, l);
    530    1.1       cgd 			if (nam)
    531    1.1       cgd 				unp_disconnect(unp);
    532    1.1       cgd 			break;
    533    1.1       cgd 		}
    534    1.1       cgd 
    535    1.1       cgd 		case SOCK_STREAM:
    536    1.1       cgd #define	rcv (&so2->so_rcv)
    537    1.1       cgd #define	snd (&so->so_snd)
    538   1.87  christos 			if (unp->unp_conn == NULL) {
    539   1.87  christos 				error = ENOTCONN;
    540   1.87  christos 				break;
    541   1.87  christos 			}
    542    1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    543  1.112        ad 			KASSERT(solocked2(so, so2));
    544   1.30   thorpej 			if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
    545   1.30   thorpej 				/*
    546   1.30   thorpej 				 * Credentials are passed only once on
    547   1.30   thorpej 				 * SOCK_STREAM.
    548   1.30   thorpej 				 */
    549   1.30   thorpej 				unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
    550   1.92        ad 				control = unp_addsockcred(l, control);
    551   1.30   thorpej 			}
    552    1.1       cgd 			/*
    553    1.1       cgd 			 * Send to paired receive port, and then reduce
    554    1.1       cgd 			 * send buffer hiwater marks to maintain backpressure.
    555    1.1       cgd 			 * Wake up readers.
    556    1.1       cgd 			 */
    557    1.1       cgd 			if (control) {
    558  1.112        ad 				if (sbappendcontrol(rcv, m, control) != 0)
    559  1.112        ad 					control = NULL;
    560    1.1       cgd 			} else
    561    1.1       cgd 				sbappend(rcv, m);
    562    1.1       cgd 			snd->sb_mbmax -=
    563    1.1       cgd 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    564    1.1       cgd 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    565   1.75  christos 			newhiwat = snd->sb_hiwat -
    566   1.75  christos 			    (rcv->sb_cc - unp->unp_conn->unp_cc);
    567   1.81  christos 			(void)chgsbsize(so->so_uidinfo,
    568   1.75  christos 			    &snd->sb_hiwat, newhiwat, RLIM_INFINITY);
    569    1.1       cgd 			unp->unp_conn->unp_cc = rcv->sb_cc;
    570    1.1       cgd 			sorwakeup(so2);
    571    1.1       cgd #undef snd
    572    1.1       cgd #undef rcv
    573  1.112        ad 			if (control != NULL) {
    574  1.112        ad 				sounlock(so);
    575  1.112        ad 				unp_dispose(control);
    576  1.112        ad 				m_freem(control);
    577  1.112        ad 				solock(so);
    578  1.112        ad 			}
    579    1.1       cgd 			break;
    580    1.1       cgd 
    581    1.1       cgd 		default:
    582    1.1       cgd 			panic("uipc 4");
    583    1.1       cgd 		}
    584    1.1       cgd 		break;
    585    1.1       cgd 
    586    1.1       cgd 	case PRU_ABORT:
    587  1.112        ad 		(void)unp_drop(unp, ECONNABORTED);
    588   1.39  sommerfe 
    589   1.88      matt 		KASSERT(so->so_head == NULL);
    590   1.39  sommerfe #ifdef DIAGNOSTIC
    591   1.39  sommerfe 		if (so->so_pcb == 0)
    592   1.39  sommerfe 			panic("uipc 5: drop killed pcb");
    593   1.39  sommerfe #endif
    594   1.39  sommerfe 		unp_detach(unp);
    595    1.1       cgd 		break;
    596    1.1       cgd 
    597    1.1       cgd 	case PRU_SENSE:
    598    1.1       cgd 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    599    1.1       cgd 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    600    1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    601  1.112        ad 			KASSERT(solocked2(so, so2));
    602    1.1       cgd 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    603    1.1       cgd 		}
    604    1.1       cgd 		((struct stat *) m)->st_dev = NODEV;
    605    1.1       cgd 		if (unp->unp_ino == 0)
    606    1.1       cgd 			unp->unp_ino = unp_ino++;
    607   1.25    kleink 		((struct stat *) m)->st_atimespec =
    608   1.25    kleink 		    ((struct stat *) m)->st_mtimespec =
    609   1.25    kleink 		    ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
    610    1.1       cgd 		((struct stat *) m)->st_ino = unp->unp_ino;
    611    1.1       cgd 		return (0);
    612    1.1       cgd 
    613    1.1       cgd 	case PRU_RCVOOB:
    614   1.20   mycroft 		error = EOPNOTSUPP;
    615   1.20   mycroft 		break;
    616    1.1       cgd 
    617    1.1       cgd 	case PRU_SENDOOB:
    618   1.22   mycroft 		m_freem(control);
    619   1.20   mycroft 		m_freem(m);
    620    1.1       cgd 		error = EOPNOTSUPP;
    621    1.1       cgd 		break;
    622    1.1       cgd 
    623    1.1       cgd 	case PRU_SOCKADDR:
    624  1.112        ad 		unp_setaddr(so, nam, false);
    625    1.1       cgd 		break;
    626    1.1       cgd 
    627    1.1       cgd 	case PRU_PEERADDR:
    628  1.112        ad 		unp_setaddr(so, nam, true);
    629    1.1       cgd 		break;
    630    1.1       cgd 
    631    1.1       cgd 	default:
    632    1.1       cgd 		panic("piusrreq");
    633    1.1       cgd 	}
    634   1.20   mycroft 
    635    1.1       cgd release:
    636    1.1       cgd 	return (error);
    637    1.1       cgd }
    638    1.1       cgd 
    639    1.1       cgd /*
    640   1.30   thorpej  * Unix domain socket option processing.
    641   1.30   thorpej  */
    642   1.30   thorpej int
    643  1.118    plunky uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    644   1.30   thorpej {
    645   1.30   thorpej 	struct unpcb *unp = sotounpcb(so);
    646   1.30   thorpej 	int optval = 0, error = 0;
    647   1.30   thorpej 
    648  1.112        ad 	KASSERT(solocked(so));
    649  1.112        ad 
    650  1.118    plunky 	if (sopt->sopt_level != 0) {
    651  1.100    dyoung 		error = ENOPROTOOPT;
    652   1.30   thorpej 	} else switch (op) {
    653   1.30   thorpej 
    654   1.30   thorpej 	case PRCO_SETOPT:
    655  1.118    plunky 		switch (sopt->sopt_name) {
    656   1.30   thorpej 		case LOCAL_CREDS:
    657   1.72      matt 		case LOCAL_CONNWAIT:
    658  1.118    plunky 			error = sockopt_getint(sopt, &optval);
    659  1.118    plunky 			if (error)
    660  1.118    plunky 				break;
    661  1.118    plunky 			switch (sopt->sopt_name) {
    662   1.30   thorpej #define	OPTSET(bit) \
    663   1.30   thorpej 	if (optval) \
    664   1.30   thorpej 		unp->unp_flags |= (bit); \
    665   1.30   thorpej 	else \
    666   1.30   thorpej 		unp->unp_flags &= ~(bit);
    667   1.30   thorpej 
    668  1.118    plunky 			case LOCAL_CREDS:
    669  1.118    plunky 				OPTSET(UNP_WANTCRED);
    670  1.118    plunky 				break;
    671  1.118    plunky 			case LOCAL_CONNWAIT:
    672  1.118    plunky 				OPTSET(UNP_CONNWAIT);
    673  1.118    plunky 				break;
    674   1.30   thorpej 			}
    675   1.30   thorpej 			break;
    676   1.30   thorpej #undef OPTSET
    677   1.30   thorpej 
    678   1.30   thorpej 		default:
    679   1.30   thorpej 			error = ENOPROTOOPT;
    680   1.30   thorpej 			break;
    681   1.30   thorpej 		}
    682   1.30   thorpej 		break;
    683   1.30   thorpej 
    684   1.30   thorpej 	case PRCO_GETOPT:
    685  1.112        ad 		sounlock(so);
    686  1.118    plunky 		switch (sopt->sopt_name) {
    687   1.99        he 		case LOCAL_PEEREID:
    688   1.99        he 			if (unp->unp_flags & UNP_EIDSVALID) {
    689  1.118    plunky 				error = sockopt_set(sopt,
    690  1.118    plunky 				    &unp->unp_connid, sizeof(unp->unp_connid));
    691   1.99        he 			} else {
    692   1.99        he 				error = EINVAL;
    693   1.99        he 			}
    694   1.99        he 			break;
    695   1.30   thorpej 		case LOCAL_CREDS:
    696   1.30   thorpej #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
    697   1.30   thorpej 
    698   1.99        he 			optval = OPTBIT(UNP_WANTCRED);
    699  1.118    plunky 			error = sockopt_setint(sopt, optval);
    700   1.30   thorpej 			break;
    701   1.30   thorpej #undef OPTBIT
    702   1.30   thorpej 
    703   1.30   thorpej 		default:
    704   1.30   thorpej 			error = ENOPROTOOPT;
    705   1.30   thorpej 			break;
    706   1.30   thorpej 		}
    707  1.112        ad 		solock(so);
    708   1.30   thorpej 		break;
    709   1.30   thorpej 	}
    710   1.30   thorpej 	return (error);
    711   1.30   thorpej }
    712   1.30   thorpej 
    713   1.30   thorpej /*
    714    1.1       cgd  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    715    1.1       cgd  * for stream sockets, although the total for sender and receiver is
    716    1.1       cgd  * actually only PIPSIZ.
    717    1.1       cgd  * Datagram sockets really use the sendspace as the maximum datagram size,
    718    1.1       cgd  * and don't really want to reserve the sendspace.  Their recvspace should
    719    1.1       cgd  * be large enough for at least one max-size datagram plus address.
    720    1.1       cgd  */
    721    1.1       cgd #define	PIPSIZ	4096
    722    1.1       cgd u_long	unpst_sendspace = PIPSIZ;
    723    1.1       cgd u_long	unpst_recvspace = PIPSIZ;
    724    1.1       cgd u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    725    1.1       cgd u_long	unpdg_recvspace = 4*1024;
    726    1.1       cgd 
    727  1.106        ad u_int	unp_rights;			/* file descriptors in flight */
    728    1.1       cgd 
    729    1.5    andrew int
    730   1.76      matt unp_attach(struct socket *so)
    731    1.1       cgd {
    732   1.46  augustss 	struct unpcb *unp;
    733    1.1       cgd 	int error;
    734   1.80     perry 
    735  1.112        ad 	switch (so->so_type) {
    736  1.112        ad 	case SOCK_STREAM:
    737  1.112        ad 		if (so->so_lock == NULL) {
    738  1.112        ad 			/*
    739  1.112        ad 			 * XXX Assuming that no socket locks are held,
    740  1.112        ad 			 * as this call may sleep.
    741  1.112        ad 			 */
    742  1.112        ad 			so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    743  1.112        ad 			solock(so);
    744  1.112        ad 		}
    745  1.112        ad 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    746    1.1       cgd 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    747  1.112        ad 			if (error != 0)
    748  1.112        ad 				return (error);
    749  1.112        ad 		}
    750  1.112        ad 		break;
    751    1.1       cgd 
    752  1.112        ad 	case SOCK_DGRAM:
    753  1.112        ad 		if (so->so_lock == NULL) {
    754  1.112        ad 			mutex_obj_hold(uipc_lock);
    755  1.112        ad 			so->so_lock = uipc_lock;
    756  1.112        ad 			solock(so);
    757  1.112        ad 		}
    758  1.112        ad 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    759    1.1       cgd 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    760  1.112        ad 			if (error != 0)
    761  1.112        ad 				return (error);
    762  1.112        ad 		}
    763  1.112        ad 		break;
    764    1.8   mycroft 
    765  1.112        ad 	default:
    766  1.112        ad 		panic("unp_attach");
    767    1.1       cgd 	}
    768  1.112        ad 	KASSERT(solocked(so));
    769   1.14   mycroft 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
    770   1.14   mycroft 	if (unp == NULL)
    771    1.1       cgd 		return (ENOBUFS);
    772   1.95  christos 	memset((void *)unp, 0, sizeof(*unp));
    773   1.14   mycroft 	unp->unp_socket = so;
    774   1.15   mycroft 	so->so_pcb = unp;
    775   1.85    simonb 	nanotime(&unp->unp_ctime);
    776    1.1       cgd 	return (0);
    777    1.1       cgd }
    778    1.1       cgd 
    779   1.17        pk void
    780   1.76      matt unp_detach(struct unpcb *unp)
    781    1.1       cgd {
    782  1.112        ad 	struct socket *so;
    783  1.112        ad 	vnode_t *vp;
    784  1.112        ad 
    785  1.112        ad 	so = unp->unp_socket;
    786   1.80     perry 
    787  1.112        ad  retry:
    788  1.112        ad 	if ((vp = unp->unp_vnode) != NULL) {
    789  1.112        ad 		sounlock(so);
    790  1.112        ad 		/* Acquire v_interlock to protect against unp_connect(). */
    791  1.113        ad 		/* XXXAD racy */
    792  1.112        ad 		mutex_enter(&vp->v_interlock);
    793  1.112        ad 		vp->v_socket = NULL;
    794  1.112        ad 		vrelel(vp, 0);
    795  1.112        ad 		solock(so);
    796  1.112        ad 		unp->unp_vnode = NULL;
    797    1.1       cgd 	}
    798    1.1       cgd 	if (unp->unp_conn)
    799    1.1       cgd 		unp_disconnect(unp);
    800  1.112        ad 	while (unp->unp_refs) {
    801  1.112        ad 		KASSERT(solocked2(so, unp->unp_refs->unp_socket));
    802  1.112        ad 		if (unp_drop(unp->unp_refs, ECONNRESET)) {
    803  1.112        ad 			solock(so);
    804  1.112        ad 			goto retry;
    805  1.112        ad 		}
    806  1.112        ad 	}
    807  1.112        ad 	soisdisconnected(so);
    808  1.112        ad 	so->so_pcb = NULL;
    809    1.8   mycroft 	if (unp_rights) {
    810    1.8   mycroft 		/*
    811    1.8   mycroft 		 * Normally the receive buffer is flushed later,
    812    1.8   mycroft 		 * in sofree, but if our receive buffer holds references
    813    1.8   mycroft 		 * to descriptors that are now garbage, we will dispose
    814    1.8   mycroft 		 * of those descriptor references after the garbage collector
    815    1.8   mycroft 		 * gets them (resulting in a "panic: closef: count < 0").
    816    1.8   mycroft 		 */
    817  1.112        ad 		sorflush(so);
    818  1.112        ad 		unp_free(unp);
    819  1.112        ad 		sounlock(so);
    820    1.1       cgd 		unp_gc();
    821  1.112        ad 		solock(so);
    822   1.14   mycroft 	} else
    823  1.112        ad 		unp_free(unp);
    824    1.1       cgd }
    825    1.1       cgd 
    826    1.5    andrew int
    827  1.112        ad unp_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
    828    1.1       cgd {
    829   1.27   thorpej 	struct sockaddr_un *sun;
    830  1.112        ad 	struct unpcb *unp;
    831  1.106        ad 	vnode_t *vp;
    832    1.1       cgd 	struct vattr vattr;
    833   1.27   thorpej 	size_t addrlen;
    834    1.1       cgd 	int error;
    835    1.1       cgd 	struct nameidata nd;
    836  1.112        ad 	proc_t *p;
    837    1.1       cgd 
    838  1.112        ad 	unp = sotounpcb(so);
    839  1.112        ad 	if (unp->unp_vnode != NULL)
    840   1.20   mycroft 		return (EINVAL);
    841  1.109        ad 	if ((unp->unp_flags & UNP_BUSY) != 0) {
    842  1.109        ad 		/*
    843  1.109        ad 		 * EALREADY may not be strictly accurate, but since this
    844  1.109        ad 		 * is a major application error it's hardly a big deal.
    845  1.109        ad 		 */
    846  1.109        ad 		return (EALREADY);
    847  1.109        ad 	}
    848  1.109        ad 	unp->unp_flags |= UNP_BUSY;
    849  1.112        ad 	sounlock(so);
    850  1.109        ad 
    851   1.27   thorpej 	/*
    852   1.27   thorpej 	 * Allocate the new sockaddr.  We have to allocate one
    853   1.27   thorpej 	 * extra byte so that we can ensure that the pathname
    854   1.27   thorpej 	 * is nul-terminated.
    855   1.27   thorpej 	 */
    856  1.112        ad 	p = l->l_proc;
    857   1.27   thorpej 	addrlen = nam->m_len + 1;
    858   1.27   thorpej 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    859   1.95  christos 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    860   1.27   thorpej 	*(((char *)sun) + nam->m_len) = '\0';
    861   1.27   thorpej 
    862   1.97       dsl 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, UIO_SYSSPACE,
    863  1.103     pooka 	    sun->sun_path);
    864   1.27   thorpej 
    865    1.1       cgd /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    866   1.16  christos 	if ((error = namei(&nd)) != 0)
    867   1.27   thorpej 		goto bad;
    868    1.9   mycroft 	vp = nd.ni_vp;
    869   1.96   hannken 	if (vp != NULL) {
    870    1.9   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    871    1.9   mycroft 		if (nd.ni_dvp == vp)
    872    1.9   mycroft 			vrele(nd.ni_dvp);
    873    1.1       cgd 		else
    874    1.9   mycroft 			vput(nd.ni_dvp);
    875    1.1       cgd 		vrele(vp);
    876   1.96   hannken 		error = EADDRINUSE;
    877   1.96   hannken 		goto bad;
    878    1.1       cgd 	}
    879    1.1       cgd 	VATTR_NULL(&vattr);
    880    1.1       cgd 	vattr.va_type = VSOCK;
    881   1.84      jmmv 	vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask);
    882   1.16  christos 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    883   1.16  christos 	if (error)
    884   1.27   thorpej 		goto bad;
    885    1.9   mycroft 	vp = nd.ni_vp;
    886  1.112        ad 	solock(so);
    887    1.1       cgd 	vp->v_socket = unp->unp_socket;
    888    1.1       cgd 	unp->unp_vnode = vp;
    889   1.27   thorpej 	unp->unp_addrlen = addrlen;
    890   1.27   thorpej 	unp->unp_addr = sun;
    891   1.99        he 	unp->unp_connid.unp_pid = p->p_pid;
    892  1.112        ad 	unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
    893  1.112        ad 	unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
    894   1.99        he 	unp->unp_flags |= UNP_EIDSBIND;
    895   1.31      fvdl 	VOP_UNLOCK(vp, 0);
    896  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
    897    1.1       cgd 	return (0);
    898   1.27   thorpej 
    899   1.27   thorpej  bad:
    900   1.27   thorpej 	free(sun, M_SONAME);
    901  1.112        ad 	solock(so);
    902  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
    903   1.27   thorpej 	return (error);
    904    1.1       cgd }
    905    1.1       cgd 
    906    1.5    andrew int
    907   1.86  christos unp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
    908    1.1       cgd {
    909   1.46  augustss 	struct sockaddr_un *sun;
    910  1.106        ad 	vnode_t *vp;
    911   1.46  augustss 	struct socket *so2, *so3;
    912   1.99        he 	struct unpcb *unp, *unp2, *unp3;
    913   1.27   thorpej 	size_t addrlen;
    914    1.1       cgd 	int error;
    915    1.1       cgd 	struct nameidata nd;
    916    1.1       cgd 
    917  1.109        ad 	unp = sotounpcb(so);
    918  1.109        ad 	if ((unp->unp_flags & UNP_BUSY) != 0) {
    919  1.109        ad 		/*
    920  1.109        ad 		 * EALREADY may not be strictly accurate, but since this
    921  1.109        ad 		 * is a major application error it's hardly a big deal.
    922  1.109        ad 		 */
    923  1.109        ad 		return (EALREADY);
    924  1.109        ad 	}
    925  1.109        ad 	unp->unp_flags |= UNP_BUSY;
    926  1.112        ad 	sounlock(so);
    927  1.109        ad 
    928   1.27   thorpej 	/*
    929   1.27   thorpej 	 * Allocate a temporary sockaddr.  We have to allocate one extra
    930   1.27   thorpej 	 * byte so that we can ensure that the pathname is nul-terminated.
    931   1.27   thorpej 	 * When we establish the connection, we copy the other PCB's
    932   1.27   thorpej 	 * sockaddr to our own.
    933   1.27   thorpej 	 */
    934   1.27   thorpej 	addrlen = nam->m_len + 1;
    935   1.27   thorpej 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    936   1.95  christos 	m_copydata(nam, 0, nam->m_len, (void *)sun);
    937   1.27   thorpej 	*(((char *)sun) + nam->m_len) = '\0';
    938   1.27   thorpej 
    939  1.103     pooka 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_SYSSPACE,
    940  1.103     pooka 	    sun->sun_path);
    941   1.27   thorpej 
    942   1.16  christos 	if ((error = namei(&nd)) != 0)
    943   1.27   thorpej 		goto bad2;
    944    1.9   mycroft 	vp = nd.ni_vp;
    945    1.1       cgd 	if (vp->v_type != VSOCK) {
    946    1.1       cgd 		error = ENOTSOCK;
    947    1.1       cgd 		goto bad;
    948    1.1       cgd 	}
    949  1.102     pooka 	if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0)
    950    1.1       cgd 		goto bad;
    951  1.112        ad 	/* Acquire v_interlock to protect against unp_detach(). */
    952  1.112        ad 	mutex_enter(&vp->v_interlock);
    953    1.1       cgd 	so2 = vp->v_socket;
    954  1.112        ad 	if (so2 == NULL) {
    955  1.112        ad 		mutex_exit(&vp->v_interlock);
    956    1.1       cgd 		error = ECONNREFUSED;
    957    1.1       cgd 		goto bad;
    958    1.1       cgd 	}
    959    1.1       cgd 	if (so->so_type != so2->so_type) {
    960  1.112        ad 		mutex_exit(&vp->v_interlock);
    961    1.1       cgd 		error = EPROTOTYPE;
    962    1.1       cgd 		goto bad;
    963    1.1       cgd 	}
    964  1.112        ad 	solock(so);
    965  1.112        ad 	unp_resetlock(so);
    966  1.112        ad 	mutex_exit(&vp->v_interlock);
    967  1.112        ad 	if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) {
    968  1.112        ad 		/*
    969  1.112        ad 		 * This may seem somewhat fragile but is OK: if we can
    970  1.112        ad 		 * see SO_ACCEPTCONN set on the endpoint, then it must
    971  1.112        ad 		 * be locked by the domain-wide uipc_lock.
    972  1.112        ad 		 */
    973  1.112        ad 		KASSERT((so->so_options & SO_ACCEPTCONN) == 0 ||
    974  1.112        ad 		    so2->so_lock == uipc_lock);
    975    1.1       cgd 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    976    1.1       cgd 		    (so3 = sonewconn(so2, 0)) == 0) {
    977    1.1       cgd 			error = ECONNREFUSED;
    978  1.112        ad 			sounlock(so);
    979    1.1       cgd 			goto bad;
    980    1.1       cgd 		}
    981    1.1       cgd 		unp2 = sotounpcb(so2);
    982    1.1       cgd 		unp3 = sotounpcb(so3);
    983   1.26   thorpej 		if (unp2->unp_addr) {
    984   1.26   thorpej 			unp3->unp_addr = malloc(unp2->unp_addrlen,
    985   1.26   thorpej 			    M_SONAME, M_WAITOK);
    986   1.36     perry 			memcpy(unp3->unp_addr, unp2->unp_addr,
    987   1.26   thorpej 			    unp2->unp_addrlen);
    988   1.26   thorpej 			unp3->unp_addrlen = unp2->unp_addrlen;
    989   1.26   thorpej 		}
    990   1.30   thorpej 		unp3->unp_flags = unp2->unp_flags;
    991  1.112        ad 		unp3->unp_connid.unp_pid = l->l_proc->p_pid;
    992  1.112        ad 		unp3->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred);
    993  1.112        ad 		unp3->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred);
    994   1.99        he 		unp3->unp_flags |= UNP_EIDSVALID;
    995   1.99        he 		if (unp2->unp_flags & UNP_EIDSBIND) {
    996   1.99        he 			unp->unp_connid = unp2->unp_connid;
    997   1.99        he 			unp->unp_flags |= UNP_EIDSVALID;
    998   1.99        he 		}
    999  1.112        ad 		so2 = so3;
   1000   1.33   thorpej 	}
   1001   1.72      matt 	error = unp_connect2(so, so2, PRU_CONNECT);
   1002  1.112        ad 	sounlock(so);
   1003   1.27   thorpej  bad:
   1004    1.1       cgd 	vput(vp);
   1005   1.27   thorpej  bad2:
   1006   1.27   thorpej 	free(sun, M_SONAME);
   1007  1.112        ad 	solock(so);
   1008  1.109        ad 	unp->unp_flags &= ~UNP_BUSY;
   1009    1.1       cgd 	return (error);
   1010    1.1       cgd }
   1011    1.1       cgd 
   1012    1.5    andrew int
   1013   1.76      matt unp_connect2(struct socket *so, struct socket *so2, int req)
   1014    1.1       cgd {
   1015   1.46  augustss 	struct unpcb *unp = sotounpcb(so);
   1016   1.46  augustss 	struct unpcb *unp2;
   1017    1.1       cgd 
   1018    1.1       cgd 	if (so2->so_type != so->so_type)
   1019    1.1       cgd 		return (EPROTOTYPE);
   1020  1.112        ad 
   1021  1.112        ad 	/*
   1022  1.112        ad 	 * All three sockets involved must be locked by same lock:
   1023  1.112        ad 	 *
   1024  1.112        ad 	 * local endpoint (so)
   1025  1.112        ad 	 * remote endpoint (so2)
   1026  1.112        ad 	 * queue head (so->so_head, only if PR_CONNREQUIRED)
   1027  1.112        ad 	 */
   1028  1.112        ad 	KASSERT(solocked2(so, so2));
   1029  1.112        ad 	if (so->so_head != NULL) {
   1030  1.112        ad 		KASSERT(so->so_lock == uipc_lock);
   1031  1.112        ad 		KASSERT(solocked2(so, so->so_head));
   1032  1.112        ad 	}
   1033  1.112        ad 
   1034    1.1       cgd 	unp2 = sotounpcb(so2);
   1035    1.1       cgd 	unp->unp_conn = unp2;
   1036    1.1       cgd 	switch (so->so_type) {
   1037    1.1       cgd 
   1038    1.1       cgd 	case SOCK_DGRAM:
   1039    1.1       cgd 		unp->unp_nextref = unp2->unp_refs;
   1040    1.1       cgd 		unp2->unp_refs = unp;
   1041    1.1       cgd 		soisconnected(so);
   1042    1.1       cgd 		break;
   1043    1.1       cgd 
   1044    1.1       cgd 	case SOCK_STREAM:
   1045    1.1       cgd 		unp2->unp_conn = unp;
   1046   1.72      matt 		if (req == PRU_CONNECT &&
   1047   1.72      matt 		    ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT))
   1048   1.72      matt 			soisconnecting(so);
   1049   1.72      matt 		else
   1050   1.72      matt 			soisconnected(so);
   1051    1.1       cgd 		soisconnected(so2);
   1052  1.112        ad 		/*
   1053  1.112        ad 		 * If the connection is fully established, break the
   1054  1.112        ad 		 * association with uipc_lock and give the connected
   1055  1.112        ad 		 * pair a seperate lock to share.  For CONNECT2, we
   1056  1.112        ad 		 * require that the locks already match (the sockets
   1057  1.112        ad 		 * are created that way).
   1058  1.112        ad 		 */
   1059  1.112        ad 		if (req == PRU_CONNECT)
   1060  1.112        ad 			unp_setpeerlocks(so, so2);
   1061    1.1       cgd 		break;
   1062    1.1       cgd 
   1063    1.1       cgd 	default:
   1064    1.1       cgd 		panic("unp_connect2");
   1065    1.1       cgd 	}
   1066    1.1       cgd 	return (0);
   1067    1.1       cgd }
   1068    1.1       cgd 
   1069    1.5    andrew void
   1070   1.76      matt unp_disconnect(struct unpcb *unp)
   1071    1.1       cgd {
   1072   1.46  augustss 	struct unpcb *unp2 = unp->unp_conn;
   1073  1.112        ad 	struct socket *so;
   1074    1.1       cgd 
   1075    1.1       cgd 	if (unp2 == 0)
   1076    1.1       cgd 		return;
   1077    1.1       cgd 	unp->unp_conn = 0;
   1078  1.112        ad 	so = unp->unp_socket;
   1079  1.112        ad 	switch (so->so_type) {
   1080    1.1       cgd 	case SOCK_DGRAM:
   1081    1.1       cgd 		if (unp2->unp_refs == unp)
   1082    1.1       cgd 			unp2->unp_refs = unp->unp_nextref;
   1083    1.1       cgd 		else {
   1084    1.1       cgd 			unp2 = unp2->unp_refs;
   1085    1.1       cgd 			for (;;) {
   1086  1.112        ad 				KASSERT(solocked2(so, unp2->unp_socket));
   1087    1.1       cgd 				if (unp2 == 0)
   1088    1.1       cgd 					panic("unp_disconnect");
   1089    1.1       cgd 				if (unp2->unp_nextref == unp)
   1090    1.1       cgd 					break;
   1091    1.1       cgd 				unp2 = unp2->unp_nextref;
   1092    1.1       cgd 			}
   1093    1.1       cgd 			unp2->unp_nextref = unp->unp_nextref;
   1094    1.1       cgd 		}
   1095    1.1       cgd 		unp->unp_nextref = 0;
   1096  1.112        ad 		so->so_state &= ~SS_ISCONNECTED;
   1097    1.1       cgd 		break;
   1098    1.1       cgd 
   1099    1.1       cgd 	case SOCK_STREAM:
   1100  1.112        ad 		KASSERT(solocked2(so, unp2->unp_socket));
   1101  1.112        ad 		soisdisconnected(so);
   1102    1.1       cgd 		unp2->unp_conn = 0;
   1103    1.1       cgd 		soisdisconnected(unp2->unp_socket);
   1104    1.1       cgd 		break;
   1105    1.1       cgd 	}
   1106    1.1       cgd }
   1107    1.1       cgd 
   1108    1.1       cgd #ifdef notdef
   1109   1.76      matt unp_abort(struct unpcb *unp)
   1110    1.1       cgd {
   1111    1.1       cgd 	unp_detach(unp);
   1112    1.1       cgd }
   1113    1.1       cgd #endif
   1114    1.1       cgd 
   1115    1.5    andrew void
   1116   1.76      matt unp_shutdown(struct unpcb *unp)
   1117    1.1       cgd {
   1118    1.1       cgd 	struct socket *so;
   1119    1.1       cgd 
   1120    1.1       cgd 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
   1121    1.1       cgd 	    (so = unp->unp_conn->unp_socket))
   1122    1.1       cgd 		socantrcvmore(so);
   1123    1.1       cgd }
   1124    1.1       cgd 
   1125  1.112        ad bool
   1126   1.76      matt unp_drop(struct unpcb *unp, int errno)
   1127    1.1       cgd {
   1128    1.1       cgd 	struct socket *so = unp->unp_socket;
   1129    1.1       cgd 
   1130  1.112        ad 	KASSERT(solocked(so));
   1131  1.112        ad 
   1132    1.1       cgd 	so->so_error = errno;
   1133    1.1       cgd 	unp_disconnect(unp);
   1134    1.1       cgd 	if (so->so_head) {
   1135  1.112        ad 		so->so_pcb = NULL;
   1136  1.112        ad 		/* sofree() drops the socket lock */
   1137   1.14   mycroft 		sofree(so);
   1138  1.112        ad 		unp_free(unp);
   1139  1.112        ad 		return true;
   1140    1.1       cgd 	}
   1141  1.112        ad 	return false;
   1142    1.1       cgd }
   1143    1.1       cgd 
   1144    1.1       cgd #ifdef notdef
   1145   1.76      matt unp_drain(void)
   1146    1.1       cgd {
   1147    1.1       cgd 
   1148    1.1       cgd }
   1149    1.1       cgd #endif
   1150    1.1       cgd 
   1151    1.5    andrew int
   1152   1.86  christos unp_externalize(struct mbuf *rights, struct lwp *l)
   1153    1.1       cgd {
   1154   1.46  augustss 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
   1155   1.86  christos 	struct proc *p = l->l_proc;
   1156   1.47   thorpej 	int i, *fdp;
   1157  1.106        ad 	file_t **rp;
   1158  1.106        ad 	file_t *fp;
   1159   1.50   thorpej 	int nfds, error = 0;
   1160   1.47   thorpej 
   1161   1.47   thorpej 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
   1162  1.106        ad 	    sizeof(file_t *);
   1163  1.106        ad 	rp = (file_t **)CMSG_DATA(cm);
   1164    1.1       cgd 
   1165   1.50   thorpej 	fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
   1166  1.101        ad 	rw_enter(&p->p_cwdi->cwdi_lock, RW_READER);
   1167   1.50   thorpej 
   1168   1.39  sommerfe 	/* Make sure the recipient should be able to see the descriptors.. */
   1169   1.42   thorpej 	if (p->p_cwdi->cwdi_rdir != NULL) {
   1170  1.106        ad 		rp = (file_t **)CMSG_DATA(cm);
   1171   1.39  sommerfe 		for (i = 0; i < nfds; i++) {
   1172   1.39  sommerfe 			fp = *rp++;
   1173   1.39  sommerfe 			/*
   1174   1.39  sommerfe 			 * If we are in a chroot'ed directory, and
   1175   1.39  sommerfe 			 * someone wants to pass us a directory, make
   1176   1.39  sommerfe 			 * sure it's inside the subtree we're allowed
   1177   1.39  sommerfe 			 * to access.
   1178   1.39  sommerfe 			 */
   1179   1.39  sommerfe 			if (fp->f_type == DTYPE_VNODE) {
   1180  1.106        ad 				vnode_t *vp = (vnode_t *)fp->f_data;
   1181   1.39  sommerfe 				if ((vp->v_type == VDIR) &&
   1182   1.86  christos 				    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) {
   1183   1.39  sommerfe 					error = EPERM;
   1184   1.39  sommerfe 					break;
   1185   1.39  sommerfe 				}
   1186   1.39  sommerfe 			}
   1187   1.39  sommerfe 		}
   1188   1.39  sommerfe 	}
   1189   1.50   thorpej 
   1190   1.50   thorpej  restart:
   1191  1.106        ad 	rp = (file_t **)CMSG_DATA(cm);
   1192   1.50   thorpej 	if (error != 0) {
   1193   1.24       cgd 		for (i = 0; i < nfds; i++) {
   1194    1.1       cgd 			fp = *rp;
   1195   1.39  sommerfe 			/*
   1196   1.39  sommerfe 			 * zero the pointer before calling unp_discard,
   1197   1.39  sommerfe 			 * since it may end up in unp_gc()..
   1198   1.39  sommerfe 			 */
   1199   1.39  sommerfe 			*rp++ = 0;
   1200    1.1       cgd 			unp_discard(fp);
   1201    1.1       cgd 		}
   1202   1.50   thorpej 		goto out;
   1203    1.1       cgd 	}
   1204   1.50   thorpej 
   1205   1.24       cgd 	/*
   1206   1.50   thorpej 	 * First loop -- allocate file descriptor table slots for the
   1207   1.50   thorpej 	 * new descriptors.
   1208   1.24       cgd 	 */
   1209   1.24       cgd 	for (i = 0; i < nfds; i++) {
   1210   1.39  sommerfe 		fp = *rp++;
   1211  1.106        ad 		if ((error = fd_alloc(p, 0, &fdp[i])) != 0) {
   1212   1.49   thorpej 			/*
   1213   1.50   thorpej 			 * Back out what we've done so far.
   1214   1.49   thorpej 			 */
   1215  1.106        ad 			for (--i; i >= 0; i--) {
   1216  1.106        ad 				fd_abort(p, NULL, fdp[i]);
   1217  1.106        ad 			}
   1218   1.50   thorpej 			if (error == ENOSPC) {
   1219  1.106        ad 				fd_tryexpand(p);
   1220   1.50   thorpej 				error = 0;
   1221   1.50   thorpej 			} else {
   1222   1.50   thorpej 				/*
   1223   1.50   thorpej 				 * This is the error that has historically
   1224   1.50   thorpej 				 * been returned, and some callers may
   1225   1.50   thorpej 				 * expect it.
   1226   1.50   thorpej 				 */
   1227   1.50   thorpej 				error = EMSGSIZE;
   1228   1.50   thorpej 			}
   1229   1.50   thorpej 			goto restart;
   1230   1.49   thorpej 		}
   1231    1.1       cgd 	}
   1232   1.24       cgd 
   1233   1.24       cgd 	/*
   1234   1.50   thorpej 	 * Now that adding them has succeeded, update all of the
   1235  1.112        ad 	 * descriptor passing state.
   1236  1.112        ad 	 */
   1237  1.106        ad 	rp = (file_t **)CMSG_DATA(cm);
   1238   1.50   thorpej 	for (i = 0; i < nfds; i++) {
   1239   1.50   thorpej 		fp = *rp++;
   1240  1.106        ad 		atomic_dec_uint(&unp_rights);
   1241  1.106        ad 		fd_affix(p, fp, fdp[i]);
   1242  1.106        ad 		mutex_enter(&fp->f_lock);
   1243   1.50   thorpej 		fp->f_msgcount--;
   1244  1.106        ad 		mutex_exit(&fp->f_lock);
   1245  1.106        ad 		/*
   1246  1.106        ad 		 * Note that fd_affix() adds a reference to the file.
   1247  1.106        ad 		 * The file may already have been closed by another
   1248  1.106        ad 		 * LWP in the process, so we must drop the reference
   1249  1.106        ad 		 * added by unp_internalize() with closef().
   1250  1.106        ad 		 */
   1251  1.106        ad 		closef(fp);
   1252   1.50   thorpej 	}
   1253   1.50   thorpej 
   1254   1.50   thorpej 	/*
   1255   1.50   thorpej 	 * Copy temporary array to message and adjust length, in case of
   1256  1.106        ad 	 * transition from large file_t pointers to ints.
   1257   1.50   thorpej 	 */
   1258   1.50   thorpej 	memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
   1259   1.47   thorpej 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
   1260   1.47   thorpej 	rights->m_len = CMSG_SPACE(nfds * sizeof(int));
   1261   1.50   thorpej  out:
   1262  1.101        ad 	rw_exit(&p->p_cwdi->cwdi_lock);
   1263   1.50   thorpej 	free(fdp, M_TEMP);
   1264   1.50   thorpej 	return (error);
   1265    1.1       cgd }
   1266    1.1       cgd 
   1267    1.5    andrew int
   1268  1.112        ad unp_internalize(struct mbuf **controlp)
   1269    1.1       cgd {
   1270  1.106        ad 	struct filedesc *fdescp = curlwp->l_fd;
   1271  1.108      yamt 	struct mbuf *control = *controlp;
   1272   1.73    martin 	struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *);
   1273  1.106        ad 	file_t **rp, **files;
   1274  1.106        ad 	file_t *fp;
   1275   1.46  augustss 	int i, fd, *fdp;
   1276  1.106        ad 	int nfds, error;
   1277  1.106        ad 
   1278  1.106        ad 	error = 0;
   1279  1.106        ad 	newcm = NULL;
   1280   1.38   thorpej 
   1281  1.106        ad 	/* Sanity check the control message header. */
   1282   1.66  jdolecek 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
   1283  1.117  christos 	    cm->cmsg_len > control->m_len ||
   1284  1.117  christos 	    cm->cmsg_len < CMSG_ALIGN(sizeof(*cm)))
   1285    1.1       cgd 		return (EINVAL);
   1286   1.24       cgd 
   1287  1.106        ad 	/*
   1288  1.106        ad 	 * Verify that the file descriptors are valid, and acquire
   1289  1.106        ad 	 * a reference to each.
   1290  1.106        ad 	 */
   1291   1.47   thorpej 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
   1292   1.47   thorpej 	fdp = (int *)CMSG_DATA(cm);
   1293   1.24       cgd 	for (i = 0; i < nfds; i++) {
   1294   1.24       cgd 		fd = *fdp++;
   1295  1.106        ad 		if ((fp = fd_getfile(fd)) == NULL) {
   1296  1.106        ad 			nfds = i + 1;
   1297  1.106        ad 			error = EBADF;
   1298  1.106        ad 			goto out;
   1299  1.101        ad 		}
   1300   1.24       cgd 	}
   1301   1.24       cgd 
   1302  1.106        ad 	/* Allocate new space and copy header into it. */
   1303  1.106        ad 	newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK);
   1304  1.106        ad 	if (newcm == NULL) {
   1305  1.106        ad 		error = E2BIG;
   1306  1.106        ad 		goto out;
   1307  1.106        ad 	}
   1308  1.106        ad 	memcpy(newcm, cm, sizeof(struct cmsghdr));
   1309  1.106        ad 	files = (file_t **)CMSG_DATA(newcm);
   1310  1.106        ad 
   1311   1.24       cgd 	/*
   1312  1.106        ad 	 * Transform the file descriptors into file_t pointers, in
   1313   1.24       cgd 	 * reverse order so that if pointers are bigger than ints, the
   1314  1.106        ad 	 * int won't get until we're done.  No need to lock, as we have
   1315  1.106        ad 	 * already validated the descriptors with fd_getfile().
   1316   1.24       cgd 	 */
   1317   1.94    cbiere 	fdp = (int *)CMSG_DATA(cm) + nfds;
   1318   1.94    cbiere 	rp = files + nfds;
   1319   1.24       cgd 	for (i = 0; i < nfds; i++) {
   1320  1.106        ad 		fp = fdescp->fd_ofiles[*--fdp]->ff_file;
   1321  1.106        ad 		KASSERT(fp != NULL);
   1322  1.106        ad 		mutex_enter(&fp->f_lock);
   1323   1.94    cbiere 		*--rp = fp;
   1324    1.1       cgd 		fp->f_count++;
   1325    1.1       cgd 		fp->f_msgcount++;
   1326  1.106        ad 		mutex_exit(&fp->f_lock);
   1327  1.106        ad 		atomic_inc_uint(&unp_rights);
   1328  1.106        ad 	}
   1329  1.106        ad 
   1330  1.106        ad  out:
   1331  1.106        ad  	/* Release descriptor references. */
   1332  1.106        ad 	fdp = (int *)CMSG_DATA(cm);
   1333  1.106        ad 	for (i = 0; i < nfds; i++) {
   1334  1.106        ad 		fd_putfile(*fdp++);
   1335    1.1       cgd 	}
   1336   1.73    martin 
   1337  1.106        ad 	if (error == 0) {
   1338  1.108      yamt 		if (control->m_flags & M_EXT) {
   1339  1.108      yamt 			m_freem(control);
   1340  1.108      yamt 			*controlp = control = m_get(M_WAIT, MT_CONTROL);
   1341  1.108      yamt 		}
   1342  1.106        ad 		MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)),
   1343   1.73    martin 		    M_MBUF, NULL, NULL);
   1344   1.73    martin 		cm = newcm;
   1345  1.106        ad 		/*
   1346  1.106        ad 		 * Adjust message & mbuf to note amount of space
   1347  1.106        ad 		 * actually used.
   1348  1.106        ad 		 */
   1349  1.106        ad 		cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *));
   1350  1.106        ad 		control->m_len = CMSG_SPACE(nfds * sizeof(file_t *));
   1351   1.73    martin 	}
   1352   1.73    martin 
   1353  1.106        ad 	return error;
   1354   1.30   thorpej }
   1355   1.30   thorpej 
   1356   1.30   thorpej struct mbuf *
   1357   1.92        ad unp_addsockcred(struct lwp *l, struct mbuf *control)
   1358   1.30   thorpej {
   1359   1.30   thorpej 	struct cmsghdr *cmp;
   1360   1.30   thorpej 	struct sockcred *sc;
   1361   1.30   thorpej 	struct mbuf *m, *n;
   1362   1.47   thorpej 	int len, space, i;
   1363   1.30   thorpej 
   1364   1.92        ad 	len = CMSG_LEN(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1365   1.92        ad 	space = CMSG_SPACE(SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)));
   1366   1.30   thorpej 
   1367   1.30   thorpej 	m = m_get(M_WAIT, MT_CONTROL);
   1368   1.47   thorpej 	if (space > MLEN) {
   1369   1.47   thorpej 		if (space > MCLBYTES)
   1370   1.47   thorpej 			MEXTMALLOC(m, space, M_WAITOK);
   1371   1.30   thorpej 		else
   1372   1.59      matt 			m_clget(m, M_WAIT);
   1373   1.30   thorpej 		if ((m->m_flags & M_EXT) == 0) {
   1374   1.30   thorpej 			m_free(m);
   1375   1.30   thorpej 			return (control);
   1376   1.30   thorpej 		}
   1377   1.30   thorpej 	}
   1378   1.30   thorpej 
   1379   1.47   thorpej 	m->m_len = space;
   1380   1.30   thorpej 	m->m_next = NULL;
   1381   1.30   thorpej 	cmp = mtod(m, struct cmsghdr *);
   1382   1.30   thorpej 	sc = (struct sockcred *)CMSG_DATA(cmp);
   1383   1.30   thorpej 	cmp->cmsg_len = len;
   1384   1.30   thorpej 	cmp->cmsg_level = SOL_SOCKET;
   1385   1.30   thorpej 	cmp->cmsg_type = SCM_CREDS;
   1386   1.92        ad 	sc->sc_uid = kauth_cred_getuid(l->l_cred);
   1387   1.92        ad 	sc->sc_euid = kauth_cred_geteuid(l->l_cred);
   1388   1.92        ad 	sc->sc_gid = kauth_cred_getgid(l->l_cred);
   1389   1.92        ad 	sc->sc_egid = kauth_cred_getegid(l->l_cred);
   1390   1.92        ad 	sc->sc_ngroups = kauth_cred_ngroups(l->l_cred);
   1391   1.30   thorpej 	for (i = 0; i < sc->sc_ngroups; i++)
   1392   1.92        ad 		sc->sc_groups[i] = kauth_cred_group(l->l_cred, i);
   1393   1.30   thorpej 
   1394   1.30   thorpej 	/*
   1395   1.30   thorpej 	 * If a control message already exists, append us to the end.
   1396   1.30   thorpej 	 */
   1397   1.30   thorpej 	if (control != NULL) {
   1398   1.30   thorpej 		for (n = control; n->m_next != NULL; n = n->m_next)
   1399   1.30   thorpej 			;
   1400   1.30   thorpej 		n->m_next = m;
   1401   1.30   thorpej 	} else
   1402   1.30   thorpej 		control = m;
   1403   1.30   thorpej 
   1404   1.30   thorpej 	return (control);
   1405    1.1       cgd }
   1406    1.1       cgd 
   1407    1.1       cgd int	unp_defer, unp_gcing;
   1408    1.1       cgd extern	struct domain unixdomain;
   1409    1.1       cgd 
   1410   1.39  sommerfe /*
   1411   1.39  sommerfe  * Comment added long after the fact explaining what's going on here.
   1412   1.39  sommerfe  * Do a mark-sweep GC of file descriptors on the system, to free up
   1413   1.39  sommerfe  * any which are caught in flight to an about-to-be-closed socket.
   1414   1.39  sommerfe  *
   1415   1.39  sommerfe  * Traditional mark-sweep gc's start at the "root", and mark
   1416   1.39  sommerfe  * everything reachable from the root (which, in our case would be the
   1417   1.39  sommerfe  * process table).  The mark bits are cleared during the sweep.
   1418   1.39  sommerfe  *
   1419   1.39  sommerfe  * XXX For some inexplicable reason (perhaps because the file
   1420   1.39  sommerfe  * descriptor tables used to live in the u area which could be swapped
   1421   1.39  sommerfe  * out and thus hard to reach), we do multiple scans over the set of
   1422   1.39  sommerfe  * descriptors, using use *two* mark bits per object (DEFER and MARK).
   1423   1.39  sommerfe  * Whenever we find a descriptor which references other descriptors,
   1424   1.39  sommerfe  * the ones it references are marked with both bits, and we iterate
   1425   1.39  sommerfe  * over the whole file table until there are no more DEFER bits set.
   1426   1.39  sommerfe  * We also make an extra pass *before* the GC to clear the mark bits,
   1427   1.39  sommerfe  * which could have been cleared at almost no cost during the previous
   1428   1.39  sommerfe  * sweep.
   1429   1.39  sommerfe  */
   1430    1.5    andrew void
   1431   1.76      matt unp_gc(void)
   1432    1.1       cgd {
   1433  1.106        ad 	file_t *fp, *nextfp;
   1434   1.46  augustss 	struct socket *so, *so1;
   1435  1.106        ad 	file_t **extra_ref, **fpp;
   1436  1.106        ad 	int nunref, nslots, i;
   1437    1.1       cgd 
   1438  1.106        ad 	if (atomic_swap_uint(&unp_gcing, 1) == 1)
   1439    1.1       cgd 		return;
   1440  1.106        ad 
   1441  1.106        ad  restart:
   1442  1.106        ad  	nslots = nfiles * 2;
   1443  1.106        ad  	extra_ref = kmem_alloc(nslots * sizeof(file_t *), KM_SLEEP);
   1444   1.39  sommerfe 
   1445  1.101        ad 	mutex_enter(&filelist_lock);
   1446  1.106        ad 	unp_defer = 0;
   1447  1.101        ad 
   1448   1.39  sommerfe 	/* Clear mark bits */
   1449  1.106        ad 	LIST_FOREACH(fp, &filehead, f_list) {
   1450  1.106        ad 		atomic_and_uint(&fp->f_flag, ~(FMARK|FDEFER));
   1451  1.106        ad 	}
   1452   1.39  sommerfe 
   1453   1.39  sommerfe 	/*
   1454   1.39  sommerfe 	 * Iterate over the set of descriptors, marking ones believed
   1455   1.39  sommerfe 	 * (based on refcount) to be referenced from a process, and
   1456   1.39  sommerfe 	 * marking for rescan descriptors which are queued on a socket.
   1457   1.39  sommerfe 	 */
   1458    1.1       cgd 	do {
   1459   1.54      matt 		LIST_FOREACH(fp, &filehead, f_list) {
   1460  1.106        ad 			mutex_enter(&fp->f_lock);
   1461  1.106        ad 			if (fp->f_flag & FDEFER) {
   1462  1.106        ad 				atomic_and_uint(&fp->f_flag, ~FDEFER);
   1463    1.1       cgd 				unp_defer--;
   1464  1.106        ad 				KASSERT(fp->f_count != 0);
   1465    1.1       cgd 			} else {
   1466  1.101        ad 				if (fp->f_count == 0 ||
   1467  1.101        ad 				    (fp->f_flag & FMARK) ||
   1468  1.101        ad 				    fp->f_count == fp->f_msgcount) {
   1469  1.106        ad 					mutex_exit(&fp->f_lock);
   1470    1.1       cgd 					continue;
   1471  1.101        ad 				}
   1472    1.1       cgd 			}
   1473  1.106        ad 			atomic_or_uint(&fp->f_flag, FMARK);
   1474   1.39  sommerfe 
   1475    1.1       cgd 			if (fp->f_type != DTYPE_SOCKET ||
   1476  1.112        ad 			    (so = fp->f_data) == NULL ||
   1477  1.101        ad 			    so->so_proto->pr_domain != &unixdomain ||
   1478  1.101        ad 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0) {
   1479  1.106        ad 				mutex_exit(&fp->f_lock);
   1480    1.1       cgd 				continue;
   1481  1.101        ad 			}
   1482    1.1       cgd #ifdef notdef
   1483    1.1       cgd 			if (so->so_rcv.sb_flags & SB_LOCK) {
   1484  1.107     rmind 				mutex_exit(&fp->f_lock);
   1485  1.107     rmind 				mutex_exit(&filelist_lock);
   1486  1.107     rmind 				kmem_free(extra_ref, nslots * sizeof(file_t *));
   1487    1.1       cgd 				/*
   1488    1.1       cgd 				 * This is problematical; it's not clear
   1489    1.1       cgd 				 * we need to wait for the sockbuf to be
   1490    1.1       cgd 				 * unlocked (on a uniprocessor, at least),
   1491    1.1       cgd 				 * and it's also not clear what to do
   1492    1.1       cgd 				 * if sbwait returns an error due to receipt
   1493    1.1       cgd 				 * of a signal.  If sbwait does return
   1494    1.1       cgd 				 * an error, we'll go into an infinite
   1495    1.1       cgd 				 * loop.  Delete all of this for now.
   1496    1.1       cgd 				 */
   1497    1.1       cgd 				(void) sbwait(&so->so_rcv);
   1498    1.1       cgd 				goto restart;
   1499    1.1       cgd 			}
   1500    1.1       cgd #endif
   1501  1.106        ad 			mutex_exit(&fp->f_lock);
   1502  1.101        ad 
   1503  1.112        ad 			/*
   1504  1.112        ad 			 * XXX Locking a socket with filelist_lock held
   1505  1.112        ad 			 * is ugly.  filelist_lock can be taken by the
   1506  1.112        ad 			 * pagedaemon when reclaiming items from file_cache.
   1507  1.112        ad 			 * Socket activity could delay the pagedaemon.
   1508  1.112        ad 			 */
   1509  1.112        ad 			solock(so);
   1510   1.39  sommerfe 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
   1511   1.39  sommerfe 			/*
   1512  1.106        ad 			 * Mark descriptors referenced from sockets queued
   1513  1.106        ad 			 * on the accept queue as well.
   1514   1.39  sommerfe 			 */
   1515   1.39  sommerfe 			if (so->so_options & SO_ACCEPTCONN) {
   1516   1.54      matt 				TAILQ_FOREACH(so1, &so->so_q0, so_qe) {
   1517   1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1518   1.39  sommerfe 				}
   1519   1.54      matt 				TAILQ_FOREACH(so1, &so->so_q, so_qe) {
   1520   1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1521   1.39  sommerfe 				}
   1522   1.39  sommerfe 			}
   1523  1.112        ad 			sounlock(so);
   1524    1.1       cgd 		}
   1525    1.1       cgd 	} while (unp_defer);
   1526  1.101        ad 
   1527    1.8   mycroft 	/*
   1528   1.39  sommerfe 	 * Sweep pass.  Find unmarked descriptors, and free them.
   1529   1.39  sommerfe 	 *
   1530    1.8   mycroft 	 * We grab an extra reference to each of the file table entries
   1531    1.8   mycroft 	 * that are not otherwise accessible and then free the rights
   1532    1.8   mycroft 	 * that are stored in messages on them.
   1533    1.8   mycroft 	 *
   1534   1.57        pk 	 * The bug in the original code is a little tricky, so I'll describe
   1535    1.8   mycroft 	 * what's wrong with it here.
   1536    1.8   mycroft 	 *
   1537    1.8   mycroft 	 * It is incorrect to simply unp_discard each entry for f_msgcount
   1538    1.8   mycroft 	 * times -- consider the case of sockets A and B that contain
   1539    1.8   mycroft 	 * references to each other.  On a last close of some other socket,
   1540    1.8   mycroft 	 * we trigger a gc since the number of outstanding rights (unp_rights)
   1541    1.8   mycroft 	 * is non-zero.  If during the sweep phase the gc code un_discards,
   1542    1.8   mycroft 	 * we end up doing a (full) closef on the descriptor.  A closef on A
   1543    1.8   mycroft 	 * results in the following chain.  Closef calls soo_close, which
   1544    1.8   mycroft 	 * calls soclose.   Soclose calls first (through the switch
   1545    1.8   mycroft 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
   1546    1.8   mycroft 	 * returns because the previous instance had set unp_gcing, and
   1547    1.8   mycroft 	 * we return all the way back to soclose, which marks the socket
   1548    1.8   mycroft 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
   1549    1.8   mycroft 	 * to free up the rights that are queued in messages on the socket A,
   1550    1.8   mycroft 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
   1551    1.8   mycroft 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
   1552    1.8   mycroft 	 * instance of unp_discard just calls closef on B.
   1553    1.8   mycroft 	 *
   1554    1.8   mycroft 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
   1555    1.8   mycroft 	 * which results in another closef on A.  Unfortunately, A is already
   1556    1.8   mycroft 	 * being closed, and the descriptor has already been marked with
   1557    1.8   mycroft 	 * SS_NOFDREF, and soclose panics at this point.
   1558    1.8   mycroft 	 *
   1559    1.8   mycroft 	 * Here, we first take an extra reference to each inaccessible
   1560   1.39  sommerfe 	 * descriptor.  Then, if the inaccessible descriptor is a
   1561   1.39  sommerfe 	 * socket, we call sorflush in case it is a Unix domain
   1562   1.39  sommerfe 	 * socket.  After we destroy all the rights carried in
   1563   1.39  sommerfe 	 * messages, we do a last closef to get rid of our extra
   1564   1.39  sommerfe 	 * reference.  This is the last close, and the unp_detach etc
   1565   1.39  sommerfe 	 * will shut down the socket.
   1566    1.8   mycroft 	 *
   1567    1.8   mycroft 	 * 91/09/19, bsy (at) cs.cmu.edu
   1568    1.8   mycroft 	 */
   1569  1.106        ad 	if (nslots < nfiles) {
   1570  1.107     rmind 		mutex_exit(&filelist_lock);
   1571  1.107     rmind 		kmem_free(extra_ref, nslots * sizeof(file_t *));
   1572  1.107     rmind 		goto restart;
   1573  1.106        ad 	}
   1574   1.54      matt 	for (nunref = 0, fp = LIST_FIRST(&filehead), fpp = extra_ref; fp != 0;
   1575   1.11   mycroft 	    fp = nextfp) {
   1576   1.54      matt 		nextfp = LIST_NEXT(fp, f_list);
   1577  1.106        ad 		mutex_enter(&fp->f_lock);
   1578   1.57        pk 		if (fp->f_count != 0 &&
   1579   1.57        pk 		    fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
   1580    1.8   mycroft 			*fpp++ = fp;
   1581    1.8   mycroft 			nunref++;
   1582    1.8   mycroft 			fp->f_count++;
   1583    1.8   mycroft 		}
   1584  1.106        ad 		mutex_exit(&fp->f_lock);
   1585    1.1       cgd 	}
   1586  1.101        ad 	mutex_exit(&filelist_lock);
   1587  1.101        ad 
   1588   1.39  sommerfe 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1589   1.45   thorpej 		fp = *fpp;
   1590  1.112        ad 		if (fp->f_type == DTYPE_SOCKET) {
   1591  1.112        ad 			so = fp->f_data;
   1592  1.112        ad 			solock(so);
   1593  1.106        ad 			sorflush(fp->f_data);
   1594  1.112        ad 			sounlock(so);
   1595  1.112        ad 		}
   1596   1.39  sommerfe 	}
   1597   1.44   thorpej 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1598  1.106        ad 		closef(*fpp);
   1599   1.44   thorpej 	}
   1600  1.106        ad 	kmem_free(extra_ref, nslots * sizeof(file_t *));
   1601  1.106        ad 	atomic_swap_uint(&unp_gcing, 0);
   1602    1.1       cgd }
   1603    1.1       cgd 
   1604    1.5    andrew void
   1605   1.76      matt unp_dispose(struct mbuf *m)
   1606    1.1       cgd {
   1607    1.8   mycroft 
   1608    1.1       cgd 	if (m)
   1609   1.39  sommerfe 		unp_scan(m, unp_discard, 1);
   1610    1.1       cgd }
   1611    1.1       cgd 
   1612    1.5    andrew void
   1613  1.106        ad unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard)
   1614    1.1       cgd {
   1615   1.46  augustss 	struct mbuf *m;
   1616  1.106        ad 	file_t **rp;
   1617   1.46  augustss 	struct cmsghdr *cm;
   1618   1.46  augustss 	int i;
   1619    1.1       cgd 	int qfds;
   1620    1.1       cgd 
   1621    1.1       cgd 	while (m0) {
   1622   1.48   thorpej 		for (m = m0; m; m = m->m_next) {
   1623    1.1       cgd 			if (m->m_type == MT_CONTROL &&
   1624    1.1       cgd 			    m->m_len >= sizeof(*cm)) {
   1625    1.1       cgd 				cm = mtod(m, struct cmsghdr *);
   1626    1.1       cgd 				if (cm->cmsg_level != SOL_SOCKET ||
   1627  1.111   mlelstv 				    cm->cmsg_type != SCM_RIGHTS)
   1628    1.1       cgd 					continue;
   1629   1.48   thorpej 				qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
   1630  1.106        ad 				    / sizeof(file_t *);
   1631  1.106        ad 				rp = (file_t **)CMSG_DATA(cm);
   1632   1.39  sommerfe 				for (i = 0; i < qfds; i++) {
   1633  1.106        ad 					file_t *fp = *rp;
   1634   1.39  sommerfe 					if (discard)
   1635   1.39  sommerfe 						*rp = 0;
   1636   1.39  sommerfe 					(*op)(fp);
   1637   1.39  sommerfe 					rp++;
   1638   1.39  sommerfe 				}
   1639    1.1       cgd 				break;		/* XXX, but saves time */
   1640    1.1       cgd 			}
   1641   1.48   thorpej 		}
   1642   1.52   thorpej 		m0 = m0->m_nextpkt;
   1643    1.1       cgd 	}
   1644    1.1       cgd }
   1645    1.1       cgd 
   1646    1.5    andrew void
   1647  1.106        ad unp_mark(file_t *fp)
   1648    1.1       cgd {
   1649  1.101        ad 
   1650   1.39  sommerfe 	if (fp == NULL)
   1651   1.39  sommerfe 		return;
   1652   1.80     perry 
   1653   1.39  sommerfe 	/* If we're already deferred, don't screw up the defer count */
   1654  1.106        ad 	mutex_enter(&fp->f_lock);
   1655  1.101        ad 	if (fp->f_flag & (FMARK | FDEFER)) {
   1656  1.106        ad 		mutex_exit(&fp->f_lock);
   1657    1.1       cgd 		return;
   1658  1.101        ad 	}
   1659   1.39  sommerfe 
   1660   1.39  sommerfe 	/*
   1661   1.39  sommerfe 	 * Minimize the number of deferrals...  Sockets are the only
   1662   1.39  sommerfe 	 * type of descriptor which can hold references to another
   1663   1.39  sommerfe 	 * descriptor, so just mark other descriptors, and defer
   1664   1.39  sommerfe 	 * unmarked sockets for the next pass.
   1665   1.39  sommerfe 	 */
   1666   1.39  sommerfe 	if (fp->f_type == DTYPE_SOCKET) {
   1667   1.39  sommerfe 		unp_defer++;
   1668  1.106        ad 		KASSERT(fp->f_count != 0);
   1669  1.106        ad 		atomic_or_uint(&fp->f_flag, FDEFER);
   1670   1.39  sommerfe 	} else {
   1671  1.106        ad 		atomic_or_uint(&fp->f_flag, FMARK);
   1672   1.39  sommerfe 	}
   1673  1.106        ad 	mutex_exit(&fp->f_lock);
   1674   1.39  sommerfe 	return;
   1675    1.1       cgd }
   1676    1.1       cgd 
   1677    1.5    andrew void
   1678  1.106        ad unp_discard(file_t *fp)
   1679    1.1       cgd {
   1680  1.106        ad 
   1681   1.39  sommerfe 	if (fp == NULL)
   1682   1.39  sommerfe 		return;
   1683  1.106        ad 
   1684  1.106        ad 	mutex_enter(&fp->f_lock);
   1685  1.106        ad 	KASSERT(fp->f_count > 0);
   1686    1.1       cgd 	fp->f_msgcount--;
   1687  1.106        ad 	mutex_exit(&fp->f_lock);
   1688  1.106        ad 	atomic_dec_uint(&unp_rights);
   1689  1.106        ad 	(void)closef(fp);
   1690    1.1       cgd }
   1691