Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.50
      1  1.50   thorpej /*	$NetBSD: uipc_usrreq.c,v 1.50 2001/06/07 01:29:16 thorpej Exp $	*/
      2  1.30   thorpej 
      3  1.30   thorpej /*-
      4  1.47   thorpej  * Copyright (c) 1998, 2000 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  * 3. All advertising materials mentioning features or use of this software
     20  1.30   thorpej  *    must display the following acknowledgement:
     21  1.30   thorpej  *	This product includes software developed by the NetBSD
     22  1.30   thorpej  *	Foundation, Inc. and its contributors.
     23  1.30   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.30   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.30   thorpej  *    from this software without specific prior written permission.
     26  1.30   thorpej  *
     27  1.30   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.30   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.30   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.30   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.30   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.30   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.30   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.30   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.30   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.30   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.30   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.30   thorpej  */
     39  1.10       cgd 
     40   1.1       cgd /*
     41  1.24       cgd  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
     42   1.8   mycroft  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     43   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     44   1.1       cgd  *
     45   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     46   1.1       cgd  * modification, are permitted provided that the following conditions
     47   1.1       cgd  * are met:
     48   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     50   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     51   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     52   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     53   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     54   1.1       cgd  *    must display the following acknowledgement:
     55   1.1       cgd  *	This product includes software developed by the University of
     56   1.1       cgd  *	California, Berkeley and its contributors.
     57   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     58   1.1       cgd  *    may be used to endorse or promote products derived from this software
     59   1.1       cgd  *    without specific prior written permission.
     60   1.1       cgd  *
     61   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     62   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     63   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     64   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     65   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     66   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     67   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     68   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     69   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     70   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     71   1.1       cgd  * SUCH DAMAGE.
     72   1.1       cgd  *
     73  1.31      fvdl  *	@(#)uipc_usrreq.c	8.9 (Berkeley) 5/14/95
     74   1.1       cgd  */
     75   1.1       cgd 
     76   1.7   mycroft #include <sys/param.h>
     77   1.8   mycroft #include <sys/systm.h>
     78   1.7   mycroft #include <sys/proc.h>
     79   1.7   mycroft #include <sys/filedesc.h>
     80   1.7   mycroft #include <sys/domain.h>
     81   1.7   mycroft #include <sys/protosw.h>
     82   1.7   mycroft #include <sys/socket.h>
     83   1.7   mycroft #include <sys/socketvar.h>
     84   1.7   mycroft #include <sys/unpcb.h>
     85   1.7   mycroft #include <sys/un.h>
     86   1.7   mycroft #include <sys/namei.h>
     87   1.7   mycroft #include <sys/vnode.h>
     88   1.7   mycroft #include <sys/file.h>
     89   1.7   mycroft #include <sys/stat.h>
     90   1.7   mycroft #include <sys/mbuf.h>
     91   1.1       cgd 
     92   1.1       cgd /*
     93   1.1       cgd  * Unix communications domain.
     94   1.1       cgd  *
     95   1.1       cgd  * TODO:
     96   1.1       cgd  *	SEQPACKET, RDM
     97   1.1       cgd  *	rethink name space problems
     98   1.1       cgd  *	need a proper out-of-band
     99   1.1       cgd  */
    100  1.34     lukem struct	sockaddr_un sun_noname = { sizeof(sun_noname), AF_LOCAL };
    101   1.1       cgd ino_t	unp_ino;			/* prototype for fake inode numbers */
    102   1.1       cgd 
    103  1.30   thorpej struct mbuf *unp_addsockcred __P((struct proc *, struct mbuf *));
    104  1.30   thorpej 
    105  1.20   mycroft int
    106  1.30   thorpej unp_output(m, control, unp, p)
    107  1.20   mycroft 	struct mbuf *m, *control;
    108  1.20   mycroft 	struct unpcb *unp;
    109  1.30   thorpej 	struct proc *p;
    110  1.20   mycroft {
    111  1.20   mycroft 	struct socket *so2;
    112  1.20   mycroft 	struct sockaddr_un *sun;
    113  1.20   mycroft 
    114  1.20   mycroft 	so2 = unp->unp_conn->unp_socket;
    115  1.20   mycroft 	if (unp->unp_addr)
    116  1.20   mycroft 		sun = unp->unp_addr;
    117  1.20   mycroft 	else
    118  1.20   mycroft 		sun = &sun_noname;
    119  1.30   thorpej 	if (unp->unp_conn->unp_flags & UNP_WANTCRED)
    120  1.30   thorpej 		control = unp_addsockcred(p, control);
    121  1.20   mycroft 	if (sbappendaddr(&so2->so_rcv, (struct sockaddr *)sun, m,
    122  1.20   mycroft 	    control) == 0) {
    123  1.20   mycroft 		m_freem(control);
    124  1.20   mycroft 		m_freem(m);
    125  1.20   mycroft 		return (EINVAL);
    126  1.20   mycroft 	} else {
    127  1.20   mycroft 		sorwakeup(so2);
    128  1.20   mycroft 		return (0);
    129  1.20   mycroft 	}
    130  1.20   mycroft }
    131  1.20   mycroft 
    132  1.20   mycroft void
    133  1.20   mycroft unp_setsockaddr(unp, nam)
    134  1.46  augustss 	struct unpcb *unp;
    135  1.20   mycroft 	struct mbuf *nam;
    136  1.20   mycroft {
    137  1.20   mycroft 	struct sockaddr_un *sun;
    138  1.20   mycroft 
    139  1.20   mycroft 	if (unp->unp_addr)
    140  1.20   mycroft 		sun = unp->unp_addr;
    141  1.20   mycroft 	else
    142  1.20   mycroft 		sun = &sun_noname;
    143  1.20   mycroft 	nam->m_len = sun->sun_len;
    144  1.27   thorpej 	if (nam->m_len > MLEN)
    145  1.27   thorpej 		MEXTMALLOC(nam, nam->m_len, M_WAITOK);
    146  1.36     perry 	memcpy(mtod(nam, caddr_t), sun, (size_t)nam->m_len);
    147  1.20   mycroft }
    148  1.20   mycroft 
    149  1.20   mycroft void
    150  1.20   mycroft unp_setpeeraddr(unp, nam)
    151  1.46  augustss 	struct unpcb *unp;
    152  1.20   mycroft 	struct mbuf *nam;
    153  1.20   mycroft {
    154  1.20   mycroft 	struct sockaddr_un *sun;
    155  1.20   mycroft 
    156  1.20   mycroft 	if (unp->unp_conn && unp->unp_conn->unp_addr)
    157  1.20   mycroft 		sun = unp->unp_conn->unp_addr;
    158  1.20   mycroft 	else
    159  1.20   mycroft 		sun = &sun_noname;
    160  1.20   mycroft 	nam->m_len = sun->sun_len;
    161  1.27   thorpej 	if (nam->m_len > MLEN)
    162  1.27   thorpej 		MEXTMALLOC(nam, nam->m_len, M_WAITOK);
    163  1.36     perry 	memcpy(mtod(nam, caddr_t), sun, (size_t)nam->m_len);
    164  1.20   mycroft }
    165  1.20   mycroft 
    166   1.1       cgd /*ARGSUSED*/
    167   1.5    andrew int
    168  1.19   mycroft uipc_usrreq(so, req, m, nam, control, p)
    169   1.1       cgd 	struct socket *so;
    170   1.1       cgd 	int req;
    171   1.1       cgd 	struct mbuf *m, *nam, *control;
    172  1.19   mycroft 	struct proc *p;
    173   1.1       cgd {
    174   1.1       cgd 	struct unpcb *unp = sotounpcb(so);
    175  1.46  augustss 	struct socket *so2;
    176  1.46  augustss 	int error = 0;
    177   1.1       cgd 
    178   1.1       cgd 	if (req == PRU_CONTROL)
    179   1.1       cgd 		return (EOPNOTSUPP);
    180  1.20   mycroft 
    181  1.22   mycroft #ifdef DIAGNOSTIC
    182  1.22   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    183  1.22   mycroft 		panic("uipc_usrreq: unexpected control mbuf");
    184  1.22   mycroft #endif
    185   1.1       cgd 	if (unp == 0 && req != PRU_ATTACH) {
    186   1.1       cgd 		error = EINVAL;
    187   1.1       cgd 		goto release;
    188   1.1       cgd 	}
    189  1.20   mycroft 
    190   1.1       cgd 	switch (req) {
    191   1.1       cgd 
    192   1.1       cgd 	case PRU_ATTACH:
    193  1.20   mycroft 		if (unp != 0) {
    194   1.1       cgd 			error = EISCONN;
    195   1.1       cgd 			break;
    196   1.1       cgd 		}
    197   1.1       cgd 		error = unp_attach(so);
    198   1.1       cgd 		break;
    199   1.1       cgd 
    200   1.1       cgd 	case PRU_DETACH:
    201   1.1       cgd 		unp_detach(unp);
    202   1.1       cgd 		break;
    203   1.1       cgd 
    204   1.1       cgd 	case PRU_BIND:
    205   1.1       cgd 		error = unp_bind(unp, nam, p);
    206   1.1       cgd 		break;
    207   1.1       cgd 
    208   1.1       cgd 	case PRU_LISTEN:
    209   1.1       cgd 		if (unp->unp_vnode == 0)
    210   1.1       cgd 			error = EINVAL;
    211   1.1       cgd 		break;
    212   1.1       cgd 
    213   1.1       cgd 	case PRU_CONNECT:
    214   1.1       cgd 		error = unp_connect(so, nam, p);
    215   1.1       cgd 		break;
    216   1.1       cgd 
    217   1.1       cgd 	case PRU_CONNECT2:
    218   1.1       cgd 		error = unp_connect2(so, (struct socket *)nam);
    219   1.1       cgd 		break;
    220   1.1       cgd 
    221   1.1       cgd 	case PRU_DISCONNECT:
    222   1.1       cgd 		unp_disconnect(unp);
    223   1.1       cgd 		break;
    224   1.1       cgd 
    225   1.1       cgd 	case PRU_ACCEPT:
    226  1.20   mycroft 		unp_setpeeraddr(unp, nam);
    227   1.1       cgd 		break;
    228   1.1       cgd 
    229   1.1       cgd 	case PRU_SHUTDOWN:
    230   1.1       cgd 		socantsendmore(so);
    231   1.1       cgd 		unp_shutdown(unp);
    232   1.1       cgd 		break;
    233   1.1       cgd 
    234   1.1       cgd 	case PRU_RCVD:
    235   1.1       cgd 		switch (so->so_type) {
    236   1.1       cgd 
    237   1.1       cgd 		case SOCK_DGRAM:
    238   1.1       cgd 			panic("uipc 1");
    239   1.1       cgd 			/*NOTREACHED*/
    240   1.1       cgd 
    241   1.1       cgd 		case SOCK_STREAM:
    242   1.1       cgd #define	rcv (&so->so_rcv)
    243   1.1       cgd #define snd (&so2->so_snd)
    244   1.1       cgd 			if (unp->unp_conn == 0)
    245   1.1       cgd 				break;
    246   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    247   1.1       cgd 			/*
    248   1.1       cgd 			 * Adjust backpressure on sender
    249   1.1       cgd 			 * and wakeup any waiting to write.
    250   1.1       cgd 			 */
    251   1.1       cgd 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    252   1.1       cgd 			unp->unp_mbcnt = rcv->sb_mbcnt;
    253   1.1       cgd 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
    254   1.1       cgd 			unp->unp_cc = rcv->sb_cc;
    255   1.1       cgd 			sowwakeup(so2);
    256   1.1       cgd #undef snd
    257   1.1       cgd #undef rcv
    258   1.1       cgd 			break;
    259   1.1       cgd 
    260   1.1       cgd 		default:
    261   1.1       cgd 			panic("uipc 2");
    262   1.1       cgd 		}
    263   1.1       cgd 		break;
    264   1.1       cgd 
    265   1.1       cgd 	case PRU_SEND:
    266  1.30   thorpej 		/*
    267  1.30   thorpej 		 * Note: unp_internalize() rejects any control message
    268  1.30   thorpej 		 * other than SCM_RIGHTS, and only allows one.  This
    269  1.30   thorpej 		 * has the side-effect of preventing a caller from
    270  1.30   thorpej 		 * forging SCM_CREDS.
    271  1.30   thorpej 		 */
    272   1.1       cgd 		if (control && (error = unp_internalize(control, p)))
    273   1.1       cgd 			break;
    274   1.1       cgd 		switch (so->so_type) {
    275   1.1       cgd 
    276   1.1       cgd 		case SOCK_DGRAM: {
    277   1.1       cgd 			if (nam) {
    278  1.20   mycroft 				if ((so->so_state & SS_ISCONNECTED) != 0) {
    279   1.1       cgd 					error = EISCONN;
    280  1.21   mycroft 					goto die;
    281   1.1       cgd 				}
    282   1.1       cgd 				error = unp_connect(so, nam, p);
    283  1.20   mycroft 				if (error) {
    284  1.23   mycroft 				die:
    285  1.21   mycroft 					m_freem(control);
    286  1.20   mycroft 					m_freem(m);
    287   1.1       cgd 					break;
    288  1.20   mycroft 				}
    289   1.1       cgd 			} else {
    290  1.20   mycroft 				if ((so->so_state & SS_ISCONNECTED) == 0) {
    291   1.1       cgd 					error = ENOTCONN;
    292  1.21   mycroft 					goto die;
    293   1.1       cgd 				}
    294   1.1       cgd 			}
    295  1.30   thorpej 			error = unp_output(m, control, unp, p);
    296   1.1       cgd 			if (nam)
    297   1.1       cgd 				unp_disconnect(unp);
    298   1.1       cgd 			break;
    299   1.1       cgd 		}
    300   1.1       cgd 
    301   1.1       cgd 		case SOCK_STREAM:
    302   1.1       cgd #define	rcv (&so2->so_rcv)
    303   1.1       cgd #define	snd (&so->so_snd)
    304   1.1       cgd 			if (unp->unp_conn == 0)
    305   1.1       cgd 				panic("uipc 3");
    306   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    307  1.30   thorpej 			if (unp->unp_conn->unp_flags & UNP_WANTCRED) {
    308  1.30   thorpej 				/*
    309  1.30   thorpej 				 * Credentials are passed only once on
    310  1.30   thorpej 				 * SOCK_STREAM.
    311  1.30   thorpej 				 */
    312  1.30   thorpej 				unp->unp_conn->unp_flags &= ~UNP_WANTCRED;
    313  1.30   thorpej 				control = unp_addsockcred(p, control);
    314  1.30   thorpej 			}
    315   1.1       cgd 			/*
    316   1.1       cgd 			 * Send to paired receive port, and then reduce
    317   1.1       cgd 			 * send buffer hiwater marks to maintain backpressure.
    318   1.1       cgd 			 * Wake up readers.
    319   1.1       cgd 			 */
    320   1.1       cgd 			if (control) {
    321  1.21   mycroft 				if (sbappendcontrol(rcv, m, control) == 0)
    322  1.21   mycroft 					m_freem(control);
    323   1.1       cgd 			} else
    324   1.1       cgd 				sbappend(rcv, m);
    325   1.1       cgd 			snd->sb_mbmax -=
    326   1.1       cgd 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    327   1.1       cgd 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    328   1.1       cgd 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
    329   1.1       cgd 			unp->unp_conn->unp_cc = rcv->sb_cc;
    330   1.1       cgd 			sorwakeup(so2);
    331   1.1       cgd #undef snd
    332   1.1       cgd #undef rcv
    333   1.1       cgd 			break;
    334   1.1       cgd 
    335   1.1       cgd 		default:
    336   1.1       cgd 			panic("uipc 4");
    337   1.1       cgd 		}
    338   1.1       cgd 		break;
    339   1.1       cgd 
    340   1.1       cgd 	case PRU_ABORT:
    341   1.1       cgd 		unp_drop(unp, ECONNABORTED);
    342  1.39  sommerfe 
    343  1.39  sommerfe #ifdef DIAGNOSTIC
    344  1.39  sommerfe 		if (so->so_pcb == 0)
    345  1.39  sommerfe 			panic("uipc 5: drop killed pcb");
    346  1.39  sommerfe #endif
    347  1.39  sommerfe 		unp_detach(unp);
    348   1.1       cgd 		break;
    349   1.1       cgd 
    350   1.1       cgd 	case PRU_SENSE:
    351   1.1       cgd 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    352   1.1       cgd 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    353   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    354   1.1       cgd 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    355   1.1       cgd 		}
    356   1.1       cgd 		((struct stat *) m)->st_dev = NODEV;
    357   1.1       cgd 		if (unp->unp_ino == 0)
    358   1.1       cgd 			unp->unp_ino = unp_ino++;
    359  1.25    kleink 		((struct stat *) m)->st_atimespec =
    360  1.25    kleink 		    ((struct stat *) m)->st_mtimespec =
    361  1.25    kleink 		    ((struct stat *) m)->st_ctimespec = unp->unp_ctime;
    362   1.1       cgd 		((struct stat *) m)->st_ino = unp->unp_ino;
    363   1.1       cgd 		return (0);
    364   1.1       cgd 
    365   1.1       cgd 	case PRU_RCVOOB:
    366  1.20   mycroft 		error = EOPNOTSUPP;
    367  1.20   mycroft 		break;
    368   1.1       cgd 
    369   1.1       cgd 	case PRU_SENDOOB:
    370  1.22   mycroft 		m_freem(control);
    371  1.20   mycroft 		m_freem(m);
    372   1.1       cgd 		error = EOPNOTSUPP;
    373   1.1       cgd 		break;
    374   1.1       cgd 
    375   1.1       cgd 	case PRU_SOCKADDR:
    376  1.20   mycroft 		unp_setsockaddr(unp, nam);
    377   1.1       cgd 		break;
    378   1.1       cgd 
    379   1.1       cgd 	case PRU_PEERADDR:
    380  1.20   mycroft 		unp_setpeeraddr(unp, nam);
    381   1.1       cgd 		break;
    382   1.1       cgd 
    383   1.1       cgd 	default:
    384   1.1       cgd 		panic("piusrreq");
    385   1.1       cgd 	}
    386  1.20   mycroft 
    387   1.1       cgd release:
    388   1.1       cgd 	return (error);
    389   1.1       cgd }
    390   1.1       cgd 
    391   1.1       cgd /*
    392  1.30   thorpej  * Unix domain socket option processing.
    393  1.30   thorpej  */
    394  1.30   thorpej int
    395  1.30   thorpej uipc_ctloutput(op, so, level, optname, mp)
    396  1.30   thorpej 	int op;
    397  1.30   thorpej 	struct socket *so;
    398  1.30   thorpej 	int level, optname;
    399  1.30   thorpej 	struct mbuf **mp;
    400  1.30   thorpej {
    401  1.30   thorpej 	struct unpcb *unp = sotounpcb(so);
    402  1.30   thorpej 	struct mbuf *m = *mp;
    403  1.30   thorpej 	int optval = 0, error = 0;
    404  1.30   thorpej 
    405  1.30   thorpej 	if (level != 0) {
    406  1.30   thorpej 		error = EINVAL;
    407  1.30   thorpej 		if (op == PRCO_SETOPT && m)
    408  1.30   thorpej 			(void) m_free(m);
    409  1.30   thorpej 	} else switch (op) {
    410  1.30   thorpej 
    411  1.30   thorpej 	case PRCO_SETOPT:
    412  1.30   thorpej 		switch (optname) {
    413  1.30   thorpej 		case LOCAL_CREDS:
    414  1.30   thorpej 			if (m == NULL || m->m_len != sizeof(int))
    415  1.30   thorpej 				error = EINVAL;
    416  1.30   thorpej 			else {
    417  1.30   thorpej 				optval = *mtod(m, int *);
    418  1.30   thorpej 				switch (optname) {
    419  1.30   thorpej #define	OPTSET(bit) \
    420  1.30   thorpej 	if (optval) \
    421  1.30   thorpej 		unp->unp_flags |= (bit); \
    422  1.30   thorpej 	else \
    423  1.30   thorpej 		unp->unp_flags &= ~(bit);
    424  1.30   thorpej 
    425  1.30   thorpej 				case LOCAL_CREDS:
    426  1.30   thorpej 					OPTSET(UNP_WANTCRED);
    427  1.30   thorpej 					break;
    428  1.30   thorpej 				}
    429  1.30   thorpej 			}
    430  1.30   thorpej 			break;
    431  1.30   thorpej #undef OPTSET
    432  1.30   thorpej 
    433  1.30   thorpej 		default:
    434  1.30   thorpej 			error = ENOPROTOOPT;
    435  1.30   thorpej 			break;
    436  1.30   thorpej 		}
    437  1.30   thorpej 		if (m)
    438  1.30   thorpej 			(void) m_free(m);
    439  1.30   thorpej 		break;
    440  1.30   thorpej 
    441  1.30   thorpej 	case PRCO_GETOPT:
    442  1.30   thorpej 		switch (optname) {
    443  1.30   thorpej 		case LOCAL_CREDS:
    444  1.30   thorpej 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    445  1.30   thorpej 			m->m_len = sizeof(int);
    446  1.30   thorpej 			switch (optname) {
    447  1.30   thorpej 
    448  1.30   thorpej #define	OPTBIT(bit)	(unp->unp_flags & (bit) ? 1 : 0)
    449  1.30   thorpej 
    450  1.30   thorpej 			case LOCAL_CREDS:
    451  1.30   thorpej 				optval = OPTBIT(UNP_WANTCRED);
    452  1.30   thorpej 				break;
    453  1.30   thorpej 			}
    454  1.30   thorpej 			*mtod(m, int *) = optval;
    455  1.30   thorpej 			break;
    456  1.30   thorpej #undef OPTBIT
    457  1.30   thorpej 
    458  1.30   thorpej 		default:
    459  1.30   thorpej 			error = ENOPROTOOPT;
    460  1.30   thorpej 			break;
    461  1.30   thorpej 		}
    462  1.30   thorpej 		break;
    463  1.30   thorpej 	}
    464  1.30   thorpej 	return (error);
    465  1.30   thorpej }
    466  1.30   thorpej 
    467  1.30   thorpej /*
    468   1.1       cgd  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    469   1.1       cgd  * for stream sockets, although the total for sender and receiver is
    470   1.1       cgd  * actually only PIPSIZ.
    471   1.1       cgd  * Datagram sockets really use the sendspace as the maximum datagram size,
    472   1.1       cgd  * and don't really want to reserve the sendspace.  Their recvspace should
    473   1.1       cgd  * be large enough for at least one max-size datagram plus address.
    474   1.1       cgd  */
    475   1.1       cgd #define	PIPSIZ	4096
    476   1.1       cgd u_long	unpst_sendspace = PIPSIZ;
    477   1.1       cgd u_long	unpst_recvspace = PIPSIZ;
    478   1.1       cgd u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    479   1.1       cgd u_long	unpdg_recvspace = 4*1024;
    480   1.1       cgd 
    481   1.1       cgd int	unp_rights;			/* file descriptors in flight */
    482   1.1       cgd 
    483   1.5    andrew int
    484   1.1       cgd unp_attach(so)
    485   1.1       cgd 	struct socket *so;
    486   1.1       cgd {
    487  1.46  augustss 	struct unpcb *unp;
    488  1.25    kleink 	struct timeval tv;
    489   1.1       cgd 	int error;
    490   1.1       cgd 
    491   1.1       cgd 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    492   1.1       cgd 		switch (so->so_type) {
    493   1.1       cgd 
    494   1.1       cgd 		case SOCK_STREAM:
    495   1.1       cgd 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    496   1.1       cgd 			break;
    497   1.1       cgd 
    498   1.1       cgd 		case SOCK_DGRAM:
    499   1.1       cgd 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    500   1.1       cgd 			break;
    501   1.8   mycroft 
    502   1.8   mycroft 		default:
    503   1.8   mycroft 			panic("unp_attach");
    504   1.1       cgd 		}
    505   1.1       cgd 		if (error)
    506   1.1       cgd 			return (error);
    507   1.1       cgd 	}
    508  1.14   mycroft 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
    509  1.14   mycroft 	if (unp == NULL)
    510   1.1       cgd 		return (ENOBUFS);
    511  1.36     perry 	memset((caddr_t)unp, 0, sizeof(*unp));
    512  1.14   mycroft 	unp->unp_socket = so;
    513  1.15   mycroft 	so->so_pcb = unp;
    514  1.25    kleink 	microtime(&tv);
    515  1.25    kleink 	TIMEVAL_TO_TIMESPEC(&tv, &unp->unp_ctime);
    516   1.1       cgd 	return (0);
    517   1.1       cgd }
    518   1.1       cgd 
    519  1.17        pk void
    520   1.1       cgd unp_detach(unp)
    521  1.46  augustss 	struct unpcb *unp;
    522   1.1       cgd {
    523   1.1       cgd 
    524   1.1       cgd 	if (unp->unp_vnode) {
    525   1.1       cgd 		unp->unp_vnode->v_socket = 0;
    526   1.1       cgd 		vrele(unp->unp_vnode);
    527   1.1       cgd 		unp->unp_vnode = 0;
    528   1.1       cgd 	}
    529   1.1       cgd 	if (unp->unp_conn)
    530   1.1       cgd 		unp_disconnect(unp);
    531   1.1       cgd 	while (unp->unp_refs)
    532   1.1       cgd 		unp_drop(unp->unp_refs, ECONNRESET);
    533   1.1       cgd 	soisdisconnected(unp->unp_socket);
    534   1.1       cgd 	unp->unp_socket->so_pcb = 0;
    535  1.20   mycroft 	if (unp->unp_addr)
    536  1.26   thorpej 		free(unp->unp_addr, M_SONAME);
    537   1.8   mycroft 	if (unp_rights) {
    538   1.8   mycroft 		/*
    539   1.8   mycroft 		 * Normally the receive buffer is flushed later,
    540   1.8   mycroft 		 * in sofree, but if our receive buffer holds references
    541   1.8   mycroft 		 * to descriptors that are now garbage, we will dispose
    542   1.8   mycroft 		 * of those descriptor references after the garbage collector
    543   1.8   mycroft 		 * gets them (resulting in a "panic: closef: count < 0").
    544   1.8   mycroft 		 */
    545   1.8   mycroft 		sorflush(unp->unp_socket);
    546  1.14   mycroft 		free(unp, M_PCB);
    547   1.1       cgd 		unp_gc();
    548  1.14   mycroft 	} else
    549  1.14   mycroft 		free(unp, M_PCB);
    550   1.1       cgd }
    551   1.1       cgd 
    552   1.5    andrew int
    553   1.1       cgd unp_bind(unp, nam, p)
    554   1.1       cgd 	struct unpcb *unp;
    555   1.1       cgd 	struct mbuf *nam;
    556   1.1       cgd 	struct proc *p;
    557   1.1       cgd {
    558  1.27   thorpej 	struct sockaddr_un *sun;
    559  1.46  augustss 	struct vnode *vp;
    560   1.1       cgd 	struct vattr vattr;
    561  1.27   thorpej 	size_t addrlen;
    562   1.1       cgd 	int error;
    563   1.1       cgd 	struct nameidata nd;
    564   1.1       cgd 
    565  1.20   mycroft 	if (unp->unp_vnode != 0)
    566  1.20   mycroft 		return (EINVAL);
    567  1.27   thorpej 
    568  1.27   thorpej 	/*
    569  1.27   thorpej 	 * Allocate the new sockaddr.  We have to allocate one
    570  1.27   thorpej 	 * extra byte so that we can ensure that the pathname
    571  1.27   thorpej 	 * is nul-terminated.
    572  1.27   thorpej 	 */
    573  1.27   thorpej 	addrlen = nam->m_len + 1;
    574  1.27   thorpej 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    575  1.27   thorpej 	m_copydata(nam, 0, nam->m_len, (caddr_t)sun);
    576  1.27   thorpej 	*(((char *)sun) + nam->m_len) = '\0';
    577  1.27   thorpej 
    578   1.9   mycroft 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
    579  1.20   mycroft 	    sun->sun_path, p);
    580  1.27   thorpej 
    581   1.1       cgd /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    582  1.16  christos 	if ((error = namei(&nd)) != 0)
    583  1.27   thorpej 		goto bad;
    584   1.9   mycroft 	vp = nd.ni_vp;
    585   1.1       cgd 	if (vp != NULL) {
    586   1.9   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    587   1.9   mycroft 		if (nd.ni_dvp == vp)
    588   1.9   mycroft 			vrele(nd.ni_dvp);
    589   1.1       cgd 		else
    590   1.9   mycroft 			vput(nd.ni_dvp);
    591   1.1       cgd 		vrele(vp);
    592  1.27   thorpej 		error = EADDRINUSE;
    593  1.27   thorpej 		goto bad;
    594   1.1       cgd 	}
    595   1.1       cgd 	VATTR_NULL(&vattr);
    596   1.1       cgd 	vattr.va_type = VSOCK;
    597   1.9   mycroft 	vattr.va_mode = ACCESSPERMS;
    598  1.12   mycroft 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    599  1.16  christos 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    600  1.16  christos 	if (error)
    601  1.27   thorpej 		goto bad;
    602   1.9   mycroft 	vp = nd.ni_vp;
    603   1.1       cgd 	vp->v_socket = unp->unp_socket;
    604   1.1       cgd 	unp->unp_vnode = vp;
    605  1.27   thorpej 	unp->unp_addrlen = addrlen;
    606  1.27   thorpej 	unp->unp_addr = sun;
    607  1.31      fvdl 	VOP_UNLOCK(vp, 0);
    608   1.1       cgd 	return (0);
    609  1.27   thorpej 
    610  1.27   thorpej  bad:
    611  1.27   thorpej 	free(sun, M_SONAME);
    612  1.27   thorpej 	return (error);
    613   1.1       cgd }
    614   1.1       cgd 
    615   1.5    andrew int
    616   1.1       cgd unp_connect(so, nam, p)
    617   1.1       cgd 	struct socket *so;
    618   1.1       cgd 	struct mbuf *nam;
    619   1.1       cgd 	struct proc *p;
    620   1.1       cgd {
    621  1.46  augustss 	struct sockaddr_un *sun;
    622  1.46  augustss 	struct vnode *vp;
    623  1.46  augustss 	struct socket *so2, *so3;
    624   1.1       cgd 	struct unpcb *unp2, *unp3;
    625  1.27   thorpej 	size_t addrlen;
    626   1.1       cgd 	int error;
    627   1.1       cgd 	struct nameidata nd;
    628   1.1       cgd 
    629  1.27   thorpej 	/*
    630  1.27   thorpej 	 * Allocate a temporary sockaddr.  We have to allocate one extra
    631  1.27   thorpej 	 * byte so that we can ensure that the pathname is nul-terminated.
    632  1.27   thorpej 	 * When we establish the connection, we copy the other PCB's
    633  1.27   thorpej 	 * sockaddr to our own.
    634  1.27   thorpej 	 */
    635  1.27   thorpej 	addrlen = nam->m_len + 1;
    636  1.27   thorpej 	sun = malloc(addrlen, M_SONAME, M_WAITOK);
    637  1.27   thorpej 	m_copydata(nam, 0, nam->m_len, (caddr_t)sun);
    638  1.27   thorpej 	*(((char *)sun) + nam->m_len) = '\0';
    639  1.27   thorpej 
    640  1.20   mycroft 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, sun->sun_path, p);
    641  1.27   thorpej 
    642  1.16  christos 	if ((error = namei(&nd)) != 0)
    643  1.27   thorpej 		goto bad2;
    644   1.9   mycroft 	vp = nd.ni_vp;
    645   1.1       cgd 	if (vp->v_type != VSOCK) {
    646   1.1       cgd 		error = ENOTSOCK;
    647   1.1       cgd 		goto bad;
    648   1.1       cgd 	}
    649  1.16  christos 	if ((error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) != 0)
    650   1.1       cgd 		goto bad;
    651   1.1       cgd 	so2 = vp->v_socket;
    652   1.1       cgd 	if (so2 == 0) {
    653   1.1       cgd 		error = ECONNREFUSED;
    654   1.1       cgd 		goto bad;
    655   1.1       cgd 	}
    656   1.1       cgd 	if (so->so_type != so2->so_type) {
    657   1.1       cgd 		error = EPROTOTYPE;
    658   1.1       cgd 		goto bad;
    659   1.1       cgd 	}
    660   1.1       cgd 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    661   1.1       cgd 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    662   1.1       cgd 		    (so3 = sonewconn(so2, 0)) == 0) {
    663   1.1       cgd 			error = ECONNREFUSED;
    664   1.1       cgd 			goto bad;
    665   1.1       cgd 		}
    666   1.1       cgd 		unp2 = sotounpcb(so2);
    667   1.1       cgd 		unp3 = sotounpcb(so3);
    668  1.26   thorpej 		if (unp2->unp_addr) {
    669  1.26   thorpej 			unp3->unp_addr = malloc(unp2->unp_addrlen,
    670  1.26   thorpej 			    M_SONAME, M_WAITOK);
    671  1.36     perry 			memcpy(unp3->unp_addr, unp2->unp_addr,
    672  1.26   thorpej 			    unp2->unp_addrlen);
    673  1.26   thorpej 			unp3->unp_addrlen = unp2->unp_addrlen;
    674  1.26   thorpej 		}
    675  1.30   thorpej 		unp3->unp_flags = unp2->unp_flags;
    676  1.33   thorpej 		so2 = so3;
    677  1.33   thorpej 	}
    678  1.33   thorpej 	error = unp_connect2(so, so2);
    679  1.27   thorpej  bad:
    680   1.1       cgd 	vput(vp);
    681  1.27   thorpej  bad2:
    682  1.27   thorpej 	free(sun, M_SONAME);
    683   1.1       cgd 	return (error);
    684   1.1       cgd }
    685   1.1       cgd 
    686   1.5    andrew int
    687   1.1       cgd unp_connect2(so, so2)
    688  1.46  augustss 	struct socket *so;
    689  1.46  augustss 	struct socket *so2;
    690   1.1       cgd {
    691  1.46  augustss 	struct unpcb *unp = sotounpcb(so);
    692  1.46  augustss 	struct unpcb *unp2;
    693   1.1       cgd 
    694   1.1       cgd 	if (so2->so_type != so->so_type)
    695   1.1       cgd 		return (EPROTOTYPE);
    696   1.1       cgd 	unp2 = sotounpcb(so2);
    697   1.1       cgd 	unp->unp_conn = unp2;
    698   1.1       cgd 	switch (so->so_type) {
    699   1.1       cgd 
    700   1.1       cgd 	case SOCK_DGRAM:
    701   1.1       cgd 		unp->unp_nextref = unp2->unp_refs;
    702   1.1       cgd 		unp2->unp_refs = unp;
    703   1.1       cgd 		soisconnected(so);
    704   1.1       cgd 		break;
    705   1.1       cgd 
    706   1.1       cgd 	case SOCK_STREAM:
    707   1.1       cgd 		unp2->unp_conn = unp;
    708   1.1       cgd 		soisconnected(so);
    709   1.1       cgd 		soisconnected(so2);
    710   1.1       cgd 		break;
    711   1.1       cgd 
    712   1.1       cgd 	default:
    713   1.1       cgd 		panic("unp_connect2");
    714   1.1       cgd 	}
    715   1.1       cgd 	return (0);
    716   1.1       cgd }
    717   1.1       cgd 
    718   1.5    andrew void
    719   1.1       cgd unp_disconnect(unp)
    720   1.1       cgd 	struct unpcb *unp;
    721   1.1       cgd {
    722  1.46  augustss 	struct unpcb *unp2 = unp->unp_conn;
    723   1.1       cgd 
    724   1.1       cgd 	if (unp2 == 0)
    725   1.1       cgd 		return;
    726   1.1       cgd 	unp->unp_conn = 0;
    727   1.1       cgd 	switch (unp->unp_socket->so_type) {
    728   1.1       cgd 
    729   1.1       cgd 	case SOCK_DGRAM:
    730   1.1       cgd 		if (unp2->unp_refs == unp)
    731   1.1       cgd 			unp2->unp_refs = unp->unp_nextref;
    732   1.1       cgd 		else {
    733   1.1       cgd 			unp2 = unp2->unp_refs;
    734   1.1       cgd 			for (;;) {
    735   1.1       cgd 				if (unp2 == 0)
    736   1.1       cgd 					panic("unp_disconnect");
    737   1.1       cgd 				if (unp2->unp_nextref == unp)
    738   1.1       cgd 					break;
    739   1.1       cgd 				unp2 = unp2->unp_nextref;
    740   1.1       cgd 			}
    741   1.1       cgd 			unp2->unp_nextref = unp->unp_nextref;
    742   1.1       cgd 		}
    743   1.1       cgd 		unp->unp_nextref = 0;
    744   1.1       cgd 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
    745   1.1       cgd 		break;
    746   1.1       cgd 
    747   1.1       cgd 	case SOCK_STREAM:
    748   1.1       cgd 		soisdisconnected(unp->unp_socket);
    749   1.1       cgd 		unp2->unp_conn = 0;
    750   1.1       cgd 		soisdisconnected(unp2->unp_socket);
    751   1.1       cgd 		break;
    752   1.1       cgd 	}
    753   1.1       cgd }
    754   1.1       cgd 
    755   1.1       cgd #ifdef notdef
    756   1.1       cgd unp_abort(unp)
    757   1.1       cgd 	struct unpcb *unp;
    758   1.1       cgd {
    759   1.1       cgd 
    760   1.1       cgd 	unp_detach(unp);
    761   1.1       cgd }
    762   1.1       cgd #endif
    763   1.1       cgd 
    764   1.5    andrew void
    765   1.1       cgd unp_shutdown(unp)
    766   1.1       cgd 	struct unpcb *unp;
    767   1.1       cgd {
    768   1.1       cgd 	struct socket *so;
    769   1.1       cgd 
    770   1.1       cgd 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
    771   1.1       cgd 	    (so = unp->unp_conn->unp_socket))
    772   1.1       cgd 		socantrcvmore(so);
    773   1.1       cgd }
    774   1.1       cgd 
    775   1.5    andrew void
    776   1.1       cgd unp_drop(unp, errno)
    777   1.1       cgd 	struct unpcb *unp;
    778   1.1       cgd 	int errno;
    779   1.1       cgd {
    780   1.1       cgd 	struct socket *so = unp->unp_socket;
    781   1.1       cgd 
    782   1.1       cgd 	so->so_error = errno;
    783   1.1       cgd 	unp_disconnect(unp);
    784   1.1       cgd 	if (so->so_head) {
    785  1.15   mycroft 		so->so_pcb = 0;
    786  1.14   mycroft 		sofree(so);
    787  1.20   mycroft 		if (unp->unp_addr)
    788  1.26   thorpej 			free(unp->unp_addr, M_SONAME);
    789  1.14   mycroft 		free(unp, M_PCB);
    790   1.1       cgd 	}
    791   1.1       cgd }
    792   1.1       cgd 
    793   1.1       cgd #ifdef notdef
    794   1.1       cgd unp_drain()
    795   1.1       cgd {
    796   1.1       cgd 
    797   1.1       cgd }
    798   1.1       cgd #endif
    799   1.1       cgd 
    800   1.5    andrew int
    801   1.1       cgd unp_externalize(rights)
    802   1.1       cgd 	struct mbuf *rights;
    803   1.1       cgd {
    804   1.1       cgd 	struct proc *p = curproc;		/* XXX */
    805  1.46  augustss 	struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
    806  1.47   thorpej 	int i, *fdp;
    807  1.46  augustss 	struct file **rp;
    808  1.46  augustss 	struct file *fp;
    809  1.50   thorpej 	int nfds, error = 0;
    810  1.47   thorpej 
    811  1.47   thorpej 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) /
    812  1.47   thorpej 	    sizeof(struct file *);
    813  1.47   thorpej 	rp = (struct file **)CMSG_DATA(cm);
    814   1.1       cgd 
    815  1.50   thorpej 	fdp = malloc(nfds * sizeof(int), M_TEMP, M_WAITOK);
    816  1.50   thorpej 
    817  1.39  sommerfe 	/* Make sure the recipient should be able to see the descriptors.. */
    818  1.42   thorpej 	if (p->p_cwdi->cwdi_rdir != NULL) {
    819  1.48   thorpej 		rp = (struct file **)CMSG_DATA(cm);
    820  1.39  sommerfe 		for (i = 0; i < nfds; i++) {
    821  1.39  sommerfe 			fp = *rp++;
    822  1.39  sommerfe 			/*
    823  1.39  sommerfe 			 * If we are in a chroot'ed directory, and
    824  1.39  sommerfe 			 * someone wants to pass us a directory, make
    825  1.39  sommerfe 			 * sure it's inside the subtree we're allowed
    826  1.39  sommerfe 			 * to access.
    827  1.39  sommerfe 			 */
    828  1.39  sommerfe 			if (fp->f_type == DTYPE_VNODE) {
    829  1.39  sommerfe 				struct vnode *vp = (struct vnode *)fp->f_data;
    830  1.39  sommerfe 				if ((vp->v_type == VDIR) &&
    831  1.42   thorpej 				    !vn_isunder(vp, p->p_cwdi->cwdi_rdir, p)) {
    832  1.39  sommerfe 					error = EPERM;
    833  1.39  sommerfe 					break;
    834  1.39  sommerfe 				}
    835  1.39  sommerfe 			}
    836  1.39  sommerfe 		}
    837  1.39  sommerfe 	}
    838  1.50   thorpej 
    839  1.50   thorpej  restart:
    840  1.47   thorpej 	rp = (struct file **)CMSG_DATA(cm);
    841  1.50   thorpej 	if (error != 0) {
    842  1.24       cgd 		for (i = 0; i < nfds; i++) {
    843   1.1       cgd 			fp = *rp;
    844  1.39  sommerfe 			/*
    845  1.39  sommerfe 			 * zero the pointer before calling unp_discard,
    846  1.39  sommerfe 			 * since it may end up in unp_gc()..
    847  1.39  sommerfe 			 */
    848  1.39  sommerfe 			*rp++ = 0;
    849   1.1       cgd 			unp_discard(fp);
    850   1.1       cgd 		}
    851  1.50   thorpej 		goto out;
    852   1.1       cgd 	}
    853  1.50   thorpej 
    854  1.24       cgd 	/*
    855  1.50   thorpej 	 * First loop -- allocate file descriptor table slots for the
    856  1.50   thorpej 	 * new descriptors.
    857  1.24       cgd 	 */
    858  1.24       cgd 	for (i = 0; i < nfds; i++) {
    859  1.39  sommerfe 		fp = *rp++;
    860  1.50   thorpej 		if ((error = fdalloc(p, 0, &fdp[i])) != 0) {
    861  1.49   thorpej 			/*
    862  1.50   thorpej 			 * Back out what we've done so far.
    863  1.49   thorpej 			 */
    864  1.50   thorpej 			for (--i; i >= 0; i--)
    865  1.50   thorpej 				fdremove(p->p_fd, fdp[i]);
    866  1.50   thorpej 
    867  1.50   thorpej 			if (error == ENOSPC) {
    868  1.50   thorpej 				fdexpand(p);
    869  1.50   thorpej 				error = 0;
    870  1.50   thorpej 			} else {
    871  1.50   thorpej 				/*
    872  1.50   thorpej 				 * This is the error that has historically
    873  1.50   thorpej 				 * been returned, and some callers may
    874  1.50   thorpej 				 * expect it.
    875  1.50   thorpej 				 */
    876  1.50   thorpej 				error = EMSGSIZE;
    877  1.50   thorpej 			}
    878  1.50   thorpej 			goto restart;
    879  1.49   thorpej 		}
    880  1.50   thorpej 
    881  1.50   thorpej 		/*
    882  1.50   thorpej 		 * Make the slot reference the descriptor so that
    883  1.50   thorpej 		 * fdalloc() works properly.. We finalize it all
    884  1.50   thorpej 		 * in the loop below.
    885  1.50   thorpej 		 */
    886  1.50   thorpej 		p->p_fd->fd_ofiles[fdp[i]] = fp;
    887   1.1       cgd 	}
    888  1.24       cgd 
    889  1.24       cgd 	/*
    890  1.50   thorpej 	 * Now that adding them has succeeded, update all of the
    891  1.50   thorpej 	 * descriptor passing state.
    892  1.24       cgd 	 */
    893  1.50   thorpej 	rp = (struct file **)CMSG_DATA(cm);
    894  1.50   thorpej 	for (i = 0; i < nfds; i++) {
    895  1.50   thorpej 		fp = *rp++;
    896  1.50   thorpej 		fp->f_msgcount--;
    897  1.50   thorpej 		unp_rights--;
    898  1.50   thorpej 	}
    899  1.50   thorpej 
    900  1.50   thorpej 	/*
    901  1.50   thorpej 	 * Copy temporary array to message and adjust length, in case of
    902  1.50   thorpej 	 * transition from large struct file pointers to ints.
    903  1.50   thorpej 	 */
    904  1.50   thorpej 	memcpy(CMSG_DATA(cm), fdp, nfds * sizeof(int));
    905  1.47   thorpej 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
    906  1.47   thorpej 	rights->m_len = CMSG_SPACE(nfds * sizeof(int));
    907  1.50   thorpej  out:
    908  1.50   thorpej 	free(fdp, M_TEMP);
    909  1.50   thorpej 	return (error);
    910   1.1       cgd }
    911   1.1       cgd 
    912   1.5    andrew int
    913   1.1       cgd unp_internalize(control, p)
    914   1.1       cgd 	struct mbuf *control;
    915   1.1       cgd 	struct proc *p;
    916   1.1       cgd {
    917  1.24       cgd 	struct filedesc *fdescp = p->p_fd;
    918  1.46  augustss 	struct cmsghdr *cm = mtod(control, struct cmsghdr *);
    919  1.46  augustss 	struct file **rp;
    920  1.46  augustss 	struct file *fp;
    921  1.46  augustss 	int i, fd, *fdp;
    922  1.24       cgd 	int nfds;
    923  1.24       cgd 	u_int neededspace;
    924  1.38   thorpej 
    925  1.24       cgd 	/* Sanity check the control message header */
    926   1.1       cgd 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
    927   1.1       cgd 	    cm->cmsg_len != control->m_len)
    928   1.1       cgd 		return (EINVAL);
    929  1.24       cgd 
    930  1.24       cgd 	/* Verify that the file descriptors are valid */
    931  1.47   thorpej 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
    932  1.47   thorpej 	fdp = (int *)CMSG_DATA(cm);
    933  1.24       cgd 	for (i = 0; i < nfds; i++) {
    934  1.24       cgd 		fd = *fdp++;
    935  1.24       cgd 		if ((unsigned)fd >= fdescp->fd_nfiles ||
    936  1.44   thorpej 		    fdescp->fd_ofiles[fd] == NULL ||
    937  1.44   thorpej 		    (fdescp->fd_ofiles[fd]->f_iflags & FIF_WANTCLOSE) != 0)
    938   1.1       cgd 			return (EBADF);
    939   1.1       cgd 	}
    940  1.24       cgd 
    941  1.24       cgd 	/* Make sure we have room for the struct file pointers */
    942  1.47   thorpej  morespace:
    943  1.47   thorpej 	neededspace = CMSG_SPACE(nfds * sizeof(struct file *)) -
    944  1.47   thorpej 	    control->m_len;
    945  1.24       cgd 	if (neededspace > M_TRAILINGSPACE(control)) {
    946  1.24       cgd 
    947  1.24       cgd 		/* if we already have a cluster, the message is just too big */
    948  1.24       cgd 		if (control->m_flags & M_EXT)
    949  1.24       cgd 			return (E2BIG);
    950  1.24       cgd 
    951  1.24       cgd 		/* allocate a cluster and try again */
    952  1.24       cgd 		MCLGET(control, M_WAIT);
    953  1.24       cgd 		if ((control->m_flags & M_EXT) == 0)
    954  1.24       cgd 			return (ENOBUFS);	/* allocation failed */
    955  1.24       cgd 
    956  1.24       cgd 		/* copy the data to the cluster */
    957  1.36     perry 		memcpy(mtod(control, char *), cm, cm->cmsg_len);
    958  1.24       cgd 		cm = mtod(control, struct cmsghdr *);
    959  1.24       cgd 		goto morespace;
    960  1.24       cgd 	}
    961  1.24       cgd 
    962  1.24       cgd 	/* adjust message & mbuf to note amount of space actually used. */
    963  1.47   thorpej 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(struct file *));
    964  1.47   thorpej 	control->m_len = CMSG_SPACE(nfds * sizeof(struct file *));
    965  1.24       cgd 
    966  1.24       cgd 	/*
    967  1.24       cgd 	 * Transform the file descriptors into struct file pointers, in
    968  1.24       cgd 	 * reverse order so that if pointers are bigger than ints, the
    969  1.24       cgd 	 * int won't get until we're done.
    970  1.24       cgd 	 */
    971  1.47   thorpej 	fdp = ((int *)CMSG_DATA(cm)) + nfds - 1;
    972  1.47   thorpej 	rp = ((struct file **)CMSG_DATA(cm)) + nfds - 1;
    973  1.24       cgd 	for (i = 0; i < nfds; i++) {
    974  1.28  christos 		fp = fdescp->fd_ofiles[*fdp--];
    975  1.44   thorpej 		FILE_USE(fp);
    976  1.24       cgd 		*rp-- = fp;
    977   1.1       cgd 		fp->f_count++;
    978   1.1       cgd 		fp->f_msgcount++;
    979  1.44   thorpej 		FILE_UNUSE(fp, NULL);
    980   1.1       cgd 		unp_rights++;
    981   1.1       cgd 	}
    982   1.1       cgd 	return (0);
    983  1.30   thorpej }
    984  1.30   thorpej 
    985  1.30   thorpej struct mbuf *
    986  1.30   thorpej unp_addsockcred(p, control)
    987  1.30   thorpej 	struct proc *p;
    988  1.30   thorpej 	struct mbuf *control;
    989  1.30   thorpej {
    990  1.30   thorpej 	struct cmsghdr *cmp;
    991  1.30   thorpej 	struct sockcred *sc;
    992  1.30   thorpej 	struct mbuf *m, *n;
    993  1.47   thorpej 	int len, space, i;
    994  1.30   thorpej 
    995  1.47   thorpej 	len = CMSG_LEN(SOCKCREDSIZE(p->p_ucred->cr_ngroups));
    996  1.47   thorpej 	space = CMSG_SPACE(SOCKCREDSIZE(p->p_ucred->cr_ngroups));
    997  1.30   thorpej 
    998  1.30   thorpej 	m = m_get(M_WAIT, MT_CONTROL);
    999  1.47   thorpej 	if (space > MLEN) {
   1000  1.47   thorpej 		if (space > MCLBYTES)
   1001  1.47   thorpej 			MEXTMALLOC(m, space, M_WAITOK);
   1002  1.30   thorpej 		else
   1003  1.30   thorpej 			MCLGET(m, M_WAIT);
   1004  1.30   thorpej 		if ((m->m_flags & M_EXT) == 0) {
   1005  1.30   thorpej 			m_free(m);
   1006  1.30   thorpej 			return (control);
   1007  1.30   thorpej 		}
   1008  1.30   thorpej 	}
   1009  1.30   thorpej 
   1010  1.47   thorpej 	m->m_len = space;
   1011  1.30   thorpej 	m->m_next = NULL;
   1012  1.30   thorpej 	cmp = mtod(m, struct cmsghdr *);
   1013  1.30   thorpej 	sc = (struct sockcred *)CMSG_DATA(cmp);
   1014  1.30   thorpej 	cmp->cmsg_len = len;
   1015  1.30   thorpej 	cmp->cmsg_level = SOL_SOCKET;
   1016  1.30   thorpej 	cmp->cmsg_type = SCM_CREDS;
   1017  1.30   thorpej 	sc->sc_uid = p->p_cred->p_ruid;
   1018  1.30   thorpej 	sc->sc_euid = p->p_ucred->cr_uid;
   1019  1.30   thorpej 	sc->sc_gid = p->p_cred->p_rgid;
   1020  1.30   thorpej 	sc->sc_egid = p->p_ucred->cr_gid;
   1021  1.30   thorpej 	sc->sc_ngroups = p->p_ucred->cr_ngroups;
   1022  1.30   thorpej 	for (i = 0; i < sc->sc_ngroups; i++)
   1023  1.30   thorpej 		sc->sc_groups[i] = p->p_ucred->cr_groups[i];
   1024  1.30   thorpej 
   1025  1.30   thorpej 	/*
   1026  1.30   thorpej 	 * If a control message already exists, append us to the end.
   1027  1.30   thorpej 	 */
   1028  1.30   thorpej 	if (control != NULL) {
   1029  1.30   thorpej 		for (n = control; n->m_next != NULL; n = n->m_next)
   1030  1.30   thorpej 			;
   1031  1.30   thorpej 		n->m_next = m;
   1032  1.30   thorpej 	} else
   1033  1.30   thorpej 		control = m;
   1034  1.30   thorpej 
   1035  1.30   thorpej 	return (control);
   1036   1.1       cgd }
   1037   1.1       cgd 
   1038   1.1       cgd int	unp_defer, unp_gcing;
   1039   1.1       cgd extern	struct domain unixdomain;
   1040   1.1       cgd 
   1041  1.39  sommerfe /*
   1042  1.39  sommerfe  * Comment added long after the fact explaining what's going on here.
   1043  1.39  sommerfe  * Do a mark-sweep GC of file descriptors on the system, to free up
   1044  1.39  sommerfe  * any which are caught in flight to an about-to-be-closed socket.
   1045  1.39  sommerfe  *
   1046  1.39  sommerfe  * Traditional mark-sweep gc's start at the "root", and mark
   1047  1.39  sommerfe  * everything reachable from the root (which, in our case would be the
   1048  1.39  sommerfe  * process table).  The mark bits are cleared during the sweep.
   1049  1.39  sommerfe  *
   1050  1.39  sommerfe  * XXX For some inexplicable reason (perhaps because the file
   1051  1.39  sommerfe  * descriptor tables used to live in the u area which could be swapped
   1052  1.39  sommerfe  * out and thus hard to reach), we do multiple scans over the set of
   1053  1.39  sommerfe  * descriptors, using use *two* mark bits per object (DEFER and MARK).
   1054  1.39  sommerfe  * Whenever we find a descriptor which references other descriptors,
   1055  1.39  sommerfe  * the ones it references are marked with both bits, and we iterate
   1056  1.39  sommerfe  * over the whole file table until there are no more DEFER bits set.
   1057  1.39  sommerfe  * We also make an extra pass *before* the GC to clear the mark bits,
   1058  1.39  sommerfe  * which could have been cleared at almost no cost during the previous
   1059  1.39  sommerfe  * sweep.
   1060  1.39  sommerfe  *
   1061  1.39  sommerfe  * XXX MP: this needs to run with locks such that no other thread of
   1062  1.39  sommerfe  * control can create or destroy references to file descriptors. it
   1063  1.39  sommerfe  * may be necessary to defer the GC until later (when the locking
   1064  1.39  sommerfe  * situation is more hospitable); it may be necessary to push this
   1065  1.39  sommerfe  * into a separate thread.
   1066  1.39  sommerfe  */
   1067   1.5    andrew void
   1068   1.1       cgd unp_gc()
   1069   1.1       cgd {
   1070  1.46  augustss 	struct file *fp, *nextfp;
   1071  1.46  augustss 	struct socket *so, *so1;
   1072   1.8   mycroft 	struct file **extra_ref, **fpp;
   1073   1.8   mycroft 	int nunref, i;
   1074   1.1       cgd 
   1075   1.1       cgd 	if (unp_gcing)
   1076   1.1       cgd 		return;
   1077   1.1       cgd 	unp_gcing = 1;
   1078   1.1       cgd 	unp_defer = 0;
   1079  1.39  sommerfe 
   1080  1.39  sommerfe 	/* Clear mark bits */
   1081  1.11   mycroft 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next)
   1082   1.1       cgd 		fp->f_flag &= ~(FMARK|FDEFER);
   1083  1.39  sommerfe 
   1084  1.39  sommerfe 	/*
   1085  1.39  sommerfe 	 * Iterate over the set of descriptors, marking ones believed
   1086  1.39  sommerfe 	 * (based on refcount) to be referenced from a process, and
   1087  1.39  sommerfe 	 * marking for rescan descriptors which are queued on a socket.
   1088  1.39  sommerfe 	 */
   1089   1.1       cgd 	do {
   1090  1.11   mycroft 		for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
   1091   1.1       cgd 			if (fp->f_flag & FDEFER) {
   1092   1.1       cgd 				fp->f_flag &= ~FDEFER;
   1093   1.1       cgd 				unp_defer--;
   1094  1.39  sommerfe #ifdef DIAGNOSTIC
   1095  1.39  sommerfe 				if (fp->f_count == 0)
   1096  1.39  sommerfe 					panic("unp_gc: deferred unreferenced socket");
   1097  1.39  sommerfe #endif
   1098   1.1       cgd 			} else {
   1099  1.39  sommerfe 				if (fp->f_count == 0)
   1100  1.39  sommerfe 					continue;
   1101   1.1       cgd 				if (fp->f_flag & FMARK)
   1102   1.1       cgd 					continue;
   1103   1.1       cgd 				if (fp->f_count == fp->f_msgcount)
   1104   1.1       cgd 					continue;
   1105   1.1       cgd 			}
   1106  1.39  sommerfe 			fp->f_flag |= FMARK;
   1107  1.39  sommerfe 
   1108   1.1       cgd 			if (fp->f_type != DTYPE_SOCKET ||
   1109   1.1       cgd 			    (so = (struct socket *)fp->f_data) == 0)
   1110   1.1       cgd 				continue;
   1111   1.1       cgd 			if (so->so_proto->pr_domain != &unixdomain ||
   1112   1.1       cgd 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
   1113   1.1       cgd 				continue;
   1114   1.1       cgd #ifdef notdef
   1115   1.1       cgd 			if (so->so_rcv.sb_flags & SB_LOCK) {
   1116   1.1       cgd 				/*
   1117   1.1       cgd 				 * This is problematical; it's not clear
   1118   1.1       cgd 				 * we need to wait for the sockbuf to be
   1119   1.1       cgd 				 * unlocked (on a uniprocessor, at least),
   1120   1.1       cgd 				 * and it's also not clear what to do
   1121   1.1       cgd 				 * if sbwait returns an error due to receipt
   1122   1.1       cgd 				 * of a signal.  If sbwait does return
   1123   1.1       cgd 				 * an error, we'll go into an infinite
   1124   1.1       cgd 				 * loop.  Delete all of this for now.
   1125   1.1       cgd 				 */
   1126   1.1       cgd 				(void) sbwait(&so->so_rcv);
   1127   1.1       cgd 				goto restart;
   1128   1.1       cgd 			}
   1129   1.1       cgd #endif
   1130  1.39  sommerfe 			unp_scan(so->so_rcv.sb_mb, unp_mark, 0);
   1131  1.39  sommerfe 			/*
   1132  1.39  sommerfe 			 * mark descriptors referenced from sockets queued on the accept queue as well.
   1133  1.39  sommerfe 			 */
   1134  1.39  sommerfe 			if (so->so_options & SO_ACCEPTCONN) {
   1135  1.39  sommerfe 				for (so1 = so->so_q0.tqh_first;
   1136  1.39  sommerfe 				     so1 != 0;
   1137  1.39  sommerfe 				     so1 = so1->so_qe.tqe_next) {
   1138  1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1139  1.39  sommerfe 				}
   1140  1.39  sommerfe 				for (so1 = so->so_q.tqh_first;
   1141  1.39  sommerfe 				     so1 != 0;
   1142  1.39  sommerfe 				     so1 = so1->so_qe.tqe_next) {
   1143  1.39  sommerfe 					unp_scan(so1->so_rcv.sb_mb, unp_mark, 0);
   1144  1.39  sommerfe 				}
   1145  1.39  sommerfe 			}
   1146  1.39  sommerfe 
   1147   1.1       cgd 		}
   1148   1.1       cgd 	} while (unp_defer);
   1149   1.8   mycroft 	/*
   1150  1.39  sommerfe 	 * Sweep pass.  Find unmarked descriptors, and free them.
   1151  1.39  sommerfe 	 *
   1152   1.8   mycroft 	 * We grab an extra reference to each of the file table entries
   1153   1.8   mycroft 	 * that are not otherwise accessible and then free the rights
   1154   1.8   mycroft 	 * that are stored in messages on them.
   1155   1.8   mycroft 	 *
   1156   1.8   mycroft 	 * The bug in the orginal code is a little tricky, so I'll describe
   1157   1.8   mycroft 	 * what's wrong with it here.
   1158   1.8   mycroft 	 *
   1159   1.8   mycroft 	 * It is incorrect to simply unp_discard each entry for f_msgcount
   1160   1.8   mycroft 	 * times -- consider the case of sockets A and B that contain
   1161   1.8   mycroft 	 * references to each other.  On a last close of some other socket,
   1162   1.8   mycroft 	 * we trigger a gc since the number of outstanding rights (unp_rights)
   1163   1.8   mycroft 	 * is non-zero.  If during the sweep phase the gc code un_discards,
   1164   1.8   mycroft 	 * we end up doing a (full) closef on the descriptor.  A closef on A
   1165   1.8   mycroft 	 * results in the following chain.  Closef calls soo_close, which
   1166   1.8   mycroft 	 * calls soclose.   Soclose calls first (through the switch
   1167   1.8   mycroft 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
   1168   1.8   mycroft 	 * returns because the previous instance had set unp_gcing, and
   1169   1.8   mycroft 	 * we return all the way back to soclose, which marks the socket
   1170   1.8   mycroft 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
   1171   1.8   mycroft 	 * to free up the rights that are queued in messages on the socket A,
   1172   1.8   mycroft 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
   1173   1.8   mycroft 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
   1174   1.8   mycroft 	 * instance of unp_discard just calls closef on B.
   1175   1.8   mycroft 	 *
   1176   1.8   mycroft 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
   1177   1.8   mycroft 	 * which results in another closef on A.  Unfortunately, A is already
   1178   1.8   mycroft 	 * being closed, and the descriptor has already been marked with
   1179   1.8   mycroft 	 * SS_NOFDREF, and soclose panics at this point.
   1180   1.8   mycroft 	 *
   1181   1.8   mycroft 	 * Here, we first take an extra reference to each inaccessible
   1182  1.39  sommerfe 	 * descriptor.  Then, if the inaccessible descriptor is a
   1183  1.39  sommerfe 	 * socket, we call sorflush in case it is a Unix domain
   1184  1.39  sommerfe 	 * socket.  After we destroy all the rights carried in
   1185  1.39  sommerfe 	 * messages, we do a last closef to get rid of our extra
   1186  1.39  sommerfe 	 * reference.  This is the last close, and the unp_detach etc
   1187  1.39  sommerfe 	 * will shut down the socket.
   1188   1.8   mycroft 	 *
   1189   1.8   mycroft 	 * 91/09/19, bsy (at) cs.cmu.edu
   1190   1.8   mycroft 	 */
   1191   1.8   mycroft 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
   1192  1.11   mycroft 	for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0;
   1193  1.11   mycroft 	    fp = nextfp) {
   1194  1.11   mycroft 		nextfp = fp->f_list.le_next;
   1195   1.1       cgd 		if (fp->f_count == 0)
   1196   1.1       cgd 			continue;
   1197   1.8   mycroft 		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
   1198   1.8   mycroft 			*fpp++ = fp;
   1199   1.8   mycroft 			nunref++;
   1200   1.8   mycroft 			fp->f_count++;
   1201   1.8   mycroft 		}
   1202   1.1       cgd 	}
   1203  1.39  sommerfe 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1204  1.45   thorpej 		fp = *fpp;
   1205  1.44   thorpej 		FILE_USE(fp);
   1206  1.39  sommerfe 		if (fp->f_type == DTYPE_SOCKET)
   1207  1.39  sommerfe 			sorflush((struct socket *)fp->f_data);
   1208  1.44   thorpej 		FILE_UNUSE(fp, NULL);
   1209  1.39  sommerfe 	}
   1210  1.44   thorpej 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp) {
   1211  1.45   thorpej 		fp = *fpp;
   1212  1.44   thorpej 		FILE_USE(fp);
   1213  1.45   thorpej 		(void) closef(fp, (struct proc *)0);
   1214  1.44   thorpej 	}
   1215   1.8   mycroft 	free((caddr_t)extra_ref, M_FILE);
   1216   1.1       cgd 	unp_gcing = 0;
   1217   1.1       cgd }
   1218   1.1       cgd 
   1219   1.5    andrew void
   1220   1.1       cgd unp_dispose(m)
   1221   1.1       cgd 	struct mbuf *m;
   1222   1.1       cgd {
   1223   1.8   mycroft 
   1224   1.1       cgd 	if (m)
   1225  1.39  sommerfe 		unp_scan(m, unp_discard, 1);
   1226   1.1       cgd }
   1227   1.1       cgd 
   1228   1.5    andrew void
   1229  1.39  sommerfe unp_scan(m0, op, discard)
   1230  1.46  augustss 	struct mbuf *m0;
   1231   1.5    andrew 	void (*op) __P((struct file *));
   1232  1.39  sommerfe 	int discard;
   1233   1.1       cgd {
   1234  1.46  augustss 	struct mbuf *m;
   1235  1.46  augustss 	struct file **rp;
   1236  1.46  augustss 	struct cmsghdr *cm;
   1237  1.46  augustss 	int i;
   1238   1.1       cgd 	int qfds;
   1239   1.1       cgd 
   1240   1.1       cgd 	while (m0) {
   1241  1.48   thorpej 		for (m = m0; m; m = m->m_next) {
   1242   1.1       cgd 			if (m->m_type == MT_CONTROL &&
   1243   1.1       cgd 			    m->m_len >= sizeof(*cm)) {
   1244   1.1       cgd 				cm = mtod(m, struct cmsghdr *);
   1245   1.1       cgd 				if (cm->cmsg_level != SOL_SOCKET ||
   1246   1.1       cgd 				    cm->cmsg_type != SCM_RIGHTS)
   1247   1.1       cgd 					continue;
   1248  1.48   thorpej 				qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm)))
   1249  1.48   thorpej 				    / sizeof(struct file *);
   1250  1.48   thorpej 				rp = (struct file **)CMSG_DATA(cm);
   1251  1.39  sommerfe 				for (i = 0; i < qfds; i++) {
   1252  1.39  sommerfe 					struct file *fp = *rp;
   1253  1.39  sommerfe 					if (discard)
   1254  1.39  sommerfe 						*rp = 0;
   1255  1.39  sommerfe 					(*op)(fp);
   1256  1.39  sommerfe 					rp++;
   1257  1.39  sommerfe 				}
   1258   1.1       cgd 				break;		/* XXX, but saves time */
   1259   1.1       cgd 			}
   1260  1.48   thorpej 		}
   1261   1.1       cgd 		m0 = m0->m_act;
   1262   1.1       cgd 	}
   1263   1.1       cgd }
   1264   1.1       cgd 
   1265   1.5    andrew void
   1266   1.1       cgd unp_mark(fp)
   1267   1.1       cgd 	struct file *fp;
   1268   1.1       cgd {
   1269  1.39  sommerfe 	if (fp == NULL)
   1270  1.39  sommerfe 		return;
   1271  1.39  sommerfe 
   1272  1.39  sommerfe 	if (fp->f_flag & FMARK)
   1273  1.39  sommerfe 		return;
   1274   1.1       cgd 
   1275  1.39  sommerfe 	/* If we're already deferred, don't screw up the defer count */
   1276  1.39  sommerfe 	if (fp->f_flag & FDEFER)
   1277   1.1       cgd 		return;
   1278  1.39  sommerfe 
   1279  1.39  sommerfe 	/*
   1280  1.39  sommerfe 	 * Minimize the number of deferrals...  Sockets are the only
   1281  1.39  sommerfe 	 * type of descriptor which can hold references to another
   1282  1.39  sommerfe 	 * descriptor, so just mark other descriptors, and defer
   1283  1.39  sommerfe 	 * unmarked sockets for the next pass.
   1284  1.39  sommerfe 	 */
   1285  1.39  sommerfe 	if (fp->f_type == DTYPE_SOCKET) {
   1286  1.39  sommerfe 		unp_defer++;
   1287  1.39  sommerfe 		if (fp->f_count == 0)
   1288  1.39  sommerfe 			panic("unp_mark: queued unref");
   1289  1.39  sommerfe 		fp->f_flag |= FDEFER;
   1290  1.39  sommerfe 	} else {
   1291  1.39  sommerfe 		fp->f_flag |= FMARK;
   1292  1.39  sommerfe 	}
   1293  1.39  sommerfe 	return;
   1294   1.1       cgd }
   1295   1.1       cgd 
   1296   1.5    andrew void
   1297   1.1       cgd unp_discard(fp)
   1298   1.1       cgd 	struct file *fp;
   1299   1.1       cgd {
   1300  1.39  sommerfe 	if (fp == NULL)
   1301  1.39  sommerfe 		return;
   1302  1.44   thorpej 	FILE_USE(fp);
   1303   1.1       cgd 	fp->f_msgcount--;
   1304   1.1       cgd 	unp_rights--;
   1305  1.13   mycroft 	(void) closef(fp, (struct proc *)0);
   1306   1.1       cgd }
   1307