Home | History | Annotate | Line # | Download | only in kern
uipc_usrreq.c revision 1.24
      1  1.24       cgd /*	$NetBSD: uipc_usrreq.c,v 1.24 1997/04/10 01:51:21 cgd Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4  1.24       cgd  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
      5   1.8   mycroft  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      6   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18   1.1       cgd  *	This product includes software developed by the University of
     19   1.1       cgd  *	California, Berkeley and its contributors.
     20   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1       cgd  *    may be used to endorse or promote products derived from this software
     22   1.1       cgd  *    without specific prior written permission.
     23   1.1       cgd  *
     24   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1       cgd  * SUCH DAMAGE.
     35   1.1       cgd  *
     36  1.10       cgd  *	@(#)uipc_usrreq.c	8.3 (Berkeley) 1/4/94
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.7   mycroft #include <sys/param.h>
     40   1.8   mycroft #include <sys/systm.h>
     41   1.7   mycroft #include <sys/proc.h>
     42   1.7   mycroft #include <sys/filedesc.h>
     43   1.7   mycroft #include <sys/domain.h>
     44   1.7   mycroft #include <sys/protosw.h>
     45   1.7   mycroft #include <sys/socket.h>
     46   1.7   mycroft #include <sys/socketvar.h>
     47   1.7   mycroft #include <sys/unpcb.h>
     48   1.7   mycroft #include <sys/un.h>
     49   1.7   mycroft #include <sys/namei.h>
     50   1.7   mycroft #include <sys/vnode.h>
     51   1.7   mycroft #include <sys/file.h>
     52   1.7   mycroft #include <sys/stat.h>
     53   1.7   mycroft #include <sys/mbuf.h>
     54   1.1       cgd 
     55   1.1       cgd /*
     56   1.1       cgd  * Unix communications domain.
     57   1.1       cgd  *
     58   1.1       cgd  * TODO:
     59   1.1       cgd  *	SEQPACKET, RDM
     60   1.1       cgd  *	rethink name space problems
     61   1.1       cgd  *	need a proper out-of-band
     62   1.1       cgd  */
     63  1.20   mycroft struct	sockaddr_un sun_noname = { sizeof(sun_noname), AF_UNIX };
     64   1.1       cgd ino_t	unp_ino;			/* prototype for fake inode numbers */
     65   1.1       cgd 
     66  1.20   mycroft int
     67  1.20   mycroft unp_output(m, control, unp)
     68  1.20   mycroft 	struct mbuf *m, *control;
     69  1.20   mycroft 	struct unpcb *unp;
     70  1.20   mycroft {
     71  1.20   mycroft 	struct socket *so2;
     72  1.20   mycroft 	struct sockaddr_un *sun;
     73  1.20   mycroft 
     74  1.20   mycroft 	so2 = unp->unp_conn->unp_socket;
     75  1.20   mycroft 	if (unp->unp_addr)
     76  1.20   mycroft 		sun = unp->unp_addr;
     77  1.20   mycroft 	else
     78  1.20   mycroft 		sun = &sun_noname;
     79  1.20   mycroft 	if (sbappendaddr(&so2->so_rcv, (struct sockaddr *)sun, m,
     80  1.20   mycroft 	    control) == 0) {
     81  1.20   mycroft 		m_freem(control);
     82  1.20   mycroft 		m_freem(m);
     83  1.20   mycroft 		return (EINVAL);
     84  1.20   mycroft 	} else {
     85  1.20   mycroft 		sorwakeup(so2);
     86  1.20   mycroft 		return (0);
     87  1.20   mycroft 	}
     88  1.20   mycroft }
     89  1.20   mycroft 
     90  1.20   mycroft void
     91  1.20   mycroft unp_setsockaddr(unp, nam)
     92  1.20   mycroft 	register struct unpcb *unp;
     93  1.20   mycroft 	struct mbuf *nam;
     94  1.20   mycroft {
     95  1.20   mycroft 	struct sockaddr_un *sun;
     96  1.20   mycroft 
     97  1.20   mycroft 	if (unp->unp_addr)
     98  1.20   mycroft 		sun = unp->unp_addr;
     99  1.20   mycroft 	else
    100  1.20   mycroft 		sun = &sun_noname;
    101  1.20   mycroft 	nam->m_len = sun->sun_len;
    102  1.20   mycroft 	bcopy(sun, mtod(nam, caddr_t), (size_t)nam->m_len);
    103  1.20   mycroft }
    104  1.20   mycroft 
    105  1.20   mycroft void
    106  1.20   mycroft unp_setpeeraddr(unp, nam)
    107  1.20   mycroft 	register struct unpcb *unp;
    108  1.20   mycroft 	struct mbuf *nam;
    109  1.20   mycroft {
    110  1.20   mycroft 	struct sockaddr_un *sun;
    111  1.20   mycroft 
    112  1.20   mycroft 	if (unp->unp_conn && unp->unp_conn->unp_addr)
    113  1.20   mycroft 		sun = unp->unp_conn->unp_addr;
    114  1.20   mycroft 	else
    115  1.20   mycroft 		sun = &sun_noname;
    116  1.20   mycroft 	nam->m_len = sun->sun_len;
    117  1.20   mycroft 	bcopy(sun, mtod(nam, caddr_t), (size_t)nam->m_len);
    118  1.20   mycroft }
    119  1.20   mycroft 
    120   1.1       cgd /*ARGSUSED*/
    121   1.5    andrew int
    122  1.19   mycroft uipc_usrreq(so, req, m, nam, control, p)
    123   1.1       cgd 	struct socket *so;
    124   1.1       cgd 	int req;
    125   1.1       cgd 	struct mbuf *m, *nam, *control;
    126  1.19   mycroft 	struct proc *p;
    127   1.1       cgd {
    128   1.1       cgd 	struct unpcb *unp = sotounpcb(so);
    129   1.1       cgd 	register struct socket *so2;
    130   1.1       cgd 	register int error = 0;
    131   1.1       cgd 
    132   1.1       cgd 	if (req == PRU_CONTROL)
    133   1.1       cgd 		return (EOPNOTSUPP);
    134  1.20   mycroft 
    135  1.22   mycroft #ifdef DIAGNOSTIC
    136  1.22   mycroft 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    137  1.22   mycroft 		panic("uipc_usrreq: unexpected control mbuf");
    138  1.22   mycroft #endif
    139   1.1       cgd 	if (unp == 0 && req != PRU_ATTACH) {
    140   1.1       cgd 		error = EINVAL;
    141   1.1       cgd 		goto release;
    142   1.1       cgd 	}
    143  1.20   mycroft 
    144   1.1       cgd 	switch (req) {
    145   1.1       cgd 
    146   1.1       cgd 	case PRU_ATTACH:
    147  1.20   mycroft 		if (unp != 0) {
    148   1.1       cgd 			error = EISCONN;
    149   1.1       cgd 			break;
    150   1.1       cgd 		}
    151   1.1       cgd 		error = unp_attach(so);
    152   1.1       cgd 		break;
    153   1.1       cgd 
    154   1.1       cgd 	case PRU_DETACH:
    155   1.1       cgd 		unp_detach(unp);
    156   1.1       cgd 		break;
    157   1.1       cgd 
    158   1.1       cgd 	case PRU_BIND:
    159   1.1       cgd 		error = unp_bind(unp, nam, p);
    160   1.1       cgd 		break;
    161   1.1       cgd 
    162   1.1       cgd 	case PRU_LISTEN:
    163   1.1       cgd 		if (unp->unp_vnode == 0)
    164   1.1       cgd 			error = EINVAL;
    165   1.1       cgd 		break;
    166   1.1       cgd 
    167   1.1       cgd 	case PRU_CONNECT:
    168   1.1       cgd 		error = unp_connect(so, nam, p);
    169   1.1       cgd 		break;
    170   1.1       cgd 
    171   1.1       cgd 	case PRU_CONNECT2:
    172   1.1       cgd 		error = unp_connect2(so, (struct socket *)nam);
    173   1.1       cgd 		break;
    174   1.1       cgd 
    175   1.1       cgd 	case PRU_DISCONNECT:
    176   1.1       cgd 		unp_disconnect(unp);
    177   1.1       cgd 		break;
    178   1.1       cgd 
    179   1.1       cgd 	case PRU_ACCEPT:
    180  1.20   mycroft 		unp_setpeeraddr(unp, nam);
    181   1.1       cgd 		break;
    182   1.1       cgd 
    183   1.1       cgd 	case PRU_SHUTDOWN:
    184   1.1       cgd 		socantsendmore(so);
    185   1.1       cgd 		unp_shutdown(unp);
    186   1.1       cgd 		break;
    187   1.1       cgd 
    188   1.1       cgd 	case PRU_RCVD:
    189   1.1       cgd 		switch (so->so_type) {
    190   1.1       cgd 
    191   1.1       cgd 		case SOCK_DGRAM:
    192   1.1       cgd 			panic("uipc 1");
    193   1.1       cgd 			/*NOTREACHED*/
    194   1.1       cgd 
    195   1.1       cgd 		case SOCK_STREAM:
    196   1.1       cgd #define	rcv (&so->so_rcv)
    197   1.1       cgd #define snd (&so2->so_snd)
    198   1.1       cgd 			if (unp->unp_conn == 0)
    199   1.1       cgd 				break;
    200   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    201   1.1       cgd 			/*
    202   1.1       cgd 			 * Adjust backpressure on sender
    203   1.1       cgd 			 * and wakeup any waiting to write.
    204   1.1       cgd 			 */
    205   1.1       cgd 			snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt;
    206   1.1       cgd 			unp->unp_mbcnt = rcv->sb_mbcnt;
    207   1.1       cgd 			snd->sb_hiwat += unp->unp_cc - rcv->sb_cc;
    208   1.1       cgd 			unp->unp_cc = rcv->sb_cc;
    209   1.1       cgd 			sowwakeup(so2);
    210   1.1       cgd #undef snd
    211   1.1       cgd #undef rcv
    212   1.1       cgd 			break;
    213   1.1       cgd 
    214   1.1       cgd 		default:
    215   1.1       cgd 			panic("uipc 2");
    216   1.1       cgd 		}
    217   1.1       cgd 		break;
    218   1.1       cgd 
    219   1.1       cgd 	case PRU_SEND:
    220   1.1       cgd 		if (control && (error = unp_internalize(control, p)))
    221   1.1       cgd 			break;
    222   1.1       cgd 		switch (so->so_type) {
    223   1.1       cgd 
    224   1.1       cgd 		case SOCK_DGRAM: {
    225   1.1       cgd 			if (nam) {
    226  1.20   mycroft 				if ((so->so_state & SS_ISCONNECTED) != 0) {
    227   1.1       cgd 					error = EISCONN;
    228  1.21   mycroft 					goto die;
    229   1.1       cgd 				}
    230   1.1       cgd 				error = unp_connect(so, nam, p);
    231  1.20   mycroft 				if (error) {
    232  1.23   mycroft 				die:
    233  1.21   mycroft 					m_freem(control);
    234  1.20   mycroft 					m_freem(m);
    235   1.1       cgd 					break;
    236  1.20   mycroft 				}
    237   1.1       cgd 			} else {
    238  1.20   mycroft 				if ((so->so_state & SS_ISCONNECTED) == 0) {
    239   1.1       cgd 					error = ENOTCONN;
    240  1.21   mycroft 					goto die;
    241   1.1       cgd 				}
    242   1.1       cgd 			}
    243  1.20   mycroft 			error = unp_output(m, control, unp);
    244   1.1       cgd 			if (nam)
    245   1.1       cgd 				unp_disconnect(unp);
    246   1.1       cgd 			break;
    247   1.1       cgd 		}
    248   1.1       cgd 
    249   1.1       cgd 		case SOCK_STREAM:
    250   1.1       cgd #define	rcv (&so2->so_rcv)
    251   1.1       cgd #define	snd (&so->so_snd)
    252   1.1       cgd 			if (unp->unp_conn == 0)
    253   1.1       cgd 				panic("uipc 3");
    254   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    255   1.1       cgd 			/*
    256   1.1       cgd 			 * Send to paired receive port, and then reduce
    257   1.1       cgd 			 * send buffer hiwater marks to maintain backpressure.
    258   1.1       cgd 			 * Wake up readers.
    259   1.1       cgd 			 */
    260   1.1       cgd 			if (control) {
    261  1.21   mycroft 				if (sbappendcontrol(rcv, m, control) == 0)
    262  1.21   mycroft 					m_freem(control);
    263   1.1       cgd 			} else
    264   1.1       cgd 				sbappend(rcv, m);
    265   1.1       cgd 			snd->sb_mbmax -=
    266   1.1       cgd 			    rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt;
    267   1.1       cgd 			unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt;
    268   1.1       cgd 			snd->sb_hiwat -= rcv->sb_cc - unp->unp_conn->unp_cc;
    269   1.1       cgd 			unp->unp_conn->unp_cc = rcv->sb_cc;
    270   1.1       cgd 			sorwakeup(so2);
    271   1.1       cgd #undef snd
    272   1.1       cgd #undef rcv
    273   1.1       cgd 			break;
    274   1.1       cgd 
    275   1.1       cgd 		default:
    276   1.1       cgd 			panic("uipc 4");
    277   1.1       cgd 		}
    278   1.1       cgd 		break;
    279   1.1       cgd 
    280   1.1       cgd 	case PRU_ABORT:
    281   1.1       cgd 		unp_drop(unp, ECONNABORTED);
    282   1.1       cgd 		break;
    283   1.1       cgd 
    284   1.1       cgd 	case PRU_SENSE:
    285   1.1       cgd 		((struct stat *) m)->st_blksize = so->so_snd.sb_hiwat;
    286   1.1       cgd 		if (so->so_type == SOCK_STREAM && unp->unp_conn != 0) {
    287   1.1       cgd 			so2 = unp->unp_conn->unp_socket;
    288   1.1       cgd 			((struct stat *) m)->st_blksize += so2->so_rcv.sb_cc;
    289   1.1       cgd 		}
    290   1.1       cgd 		((struct stat *) m)->st_dev = NODEV;
    291   1.1       cgd 		if (unp->unp_ino == 0)
    292   1.1       cgd 			unp->unp_ino = unp_ino++;
    293   1.1       cgd 		((struct stat *) m)->st_ino = unp->unp_ino;
    294   1.1       cgd 		return (0);
    295   1.1       cgd 
    296   1.1       cgd 	case PRU_RCVOOB:
    297  1.20   mycroft 		error = EOPNOTSUPP;
    298  1.20   mycroft 		break;
    299   1.1       cgd 
    300   1.1       cgd 	case PRU_SENDOOB:
    301  1.22   mycroft 		m_freem(control);
    302  1.20   mycroft 		m_freem(m);
    303   1.1       cgd 		error = EOPNOTSUPP;
    304   1.1       cgd 		break;
    305   1.1       cgd 
    306   1.1       cgd 	case PRU_SOCKADDR:
    307  1.20   mycroft 		unp_setsockaddr(unp, nam);
    308   1.1       cgd 		break;
    309   1.1       cgd 
    310   1.1       cgd 	case PRU_PEERADDR:
    311  1.20   mycroft 		unp_setpeeraddr(unp, nam);
    312   1.1       cgd 		break;
    313   1.1       cgd 
    314   1.1       cgd 	default:
    315   1.1       cgd 		panic("piusrreq");
    316   1.1       cgd 	}
    317  1.20   mycroft 
    318   1.1       cgd release:
    319   1.1       cgd 	return (error);
    320   1.1       cgd }
    321   1.1       cgd 
    322   1.1       cgd /*
    323   1.1       cgd  * Both send and receive buffers are allocated PIPSIZ bytes of buffering
    324   1.1       cgd  * for stream sockets, although the total for sender and receiver is
    325   1.1       cgd  * actually only PIPSIZ.
    326   1.1       cgd  * Datagram sockets really use the sendspace as the maximum datagram size,
    327   1.1       cgd  * and don't really want to reserve the sendspace.  Their recvspace should
    328   1.1       cgd  * be large enough for at least one max-size datagram plus address.
    329   1.1       cgd  */
    330   1.1       cgd #define	PIPSIZ	4096
    331   1.1       cgd u_long	unpst_sendspace = PIPSIZ;
    332   1.1       cgd u_long	unpst_recvspace = PIPSIZ;
    333   1.1       cgd u_long	unpdg_sendspace = 2*1024;	/* really max datagram size */
    334   1.1       cgd u_long	unpdg_recvspace = 4*1024;
    335   1.1       cgd 
    336   1.1       cgd int	unp_rights;			/* file descriptors in flight */
    337   1.1       cgd 
    338   1.5    andrew int
    339   1.1       cgd unp_attach(so)
    340   1.1       cgd 	struct socket *so;
    341   1.1       cgd {
    342   1.1       cgd 	register struct unpcb *unp;
    343   1.1       cgd 	int error;
    344   1.1       cgd 
    345   1.1       cgd 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    346   1.1       cgd 		switch (so->so_type) {
    347   1.1       cgd 
    348   1.1       cgd 		case SOCK_STREAM:
    349   1.1       cgd 			error = soreserve(so, unpst_sendspace, unpst_recvspace);
    350   1.1       cgd 			break;
    351   1.1       cgd 
    352   1.1       cgd 		case SOCK_DGRAM:
    353   1.1       cgd 			error = soreserve(so, unpdg_sendspace, unpdg_recvspace);
    354   1.1       cgd 			break;
    355   1.8   mycroft 
    356   1.8   mycroft 		default:
    357   1.8   mycroft 			panic("unp_attach");
    358   1.1       cgd 		}
    359   1.1       cgd 		if (error)
    360   1.1       cgd 			return (error);
    361   1.1       cgd 	}
    362  1.14   mycroft 	unp = malloc(sizeof(*unp), M_PCB, M_NOWAIT);
    363  1.14   mycroft 	if (unp == NULL)
    364   1.1       cgd 		return (ENOBUFS);
    365  1.14   mycroft 	bzero((caddr_t)unp, sizeof(*unp));
    366  1.14   mycroft 	unp->unp_socket = so;
    367  1.15   mycroft 	so->so_pcb = unp;
    368   1.1       cgd 	return (0);
    369   1.1       cgd }
    370   1.1       cgd 
    371  1.17        pk void
    372   1.1       cgd unp_detach(unp)
    373   1.1       cgd 	register struct unpcb *unp;
    374   1.1       cgd {
    375   1.1       cgd 
    376   1.1       cgd 	if (unp->unp_vnode) {
    377   1.1       cgd 		unp->unp_vnode->v_socket = 0;
    378   1.1       cgd 		vrele(unp->unp_vnode);
    379   1.1       cgd 		unp->unp_vnode = 0;
    380   1.1       cgd 	}
    381   1.1       cgd 	if (unp->unp_conn)
    382   1.1       cgd 		unp_disconnect(unp);
    383   1.1       cgd 	while (unp->unp_refs)
    384   1.1       cgd 		unp_drop(unp->unp_refs, ECONNRESET);
    385   1.1       cgd 	soisdisconnected(unp->unp_socket);
    386   1.1       cgd 	unp->unp_socket->so_pcb = 0;
    387  1.20   mycroft 	if (unp->unp_addr)
    388  1.20   mycroft 		m_freem(dtom(unp->unp_addr));
    389   1.8   mycroft 	if (unp_rights) {
    390   1.8   mycroft 		/*
    391   1.8   mycroft 		 * Normally the receive buffer is flushed later,
    392   1.8   mycroft 		 * in sofree, but if our receive buffer holds references
    393   1.8   mycroft 		 * to descriptors that are now garbage, we will dispose
    394   1.8   mycroft 		 * of those descriptor references after the garbage collector
    395   1.8   mycroft 		 * gets them (resulting in a "panic: closef: count < 0").
    396   1.8   mycroft 		 */
    397   1.8   mycroft 		sorflush(unp->unp_socket);
    398  1.14   mycroft 		free(unp, M_PCB);
    399   1.1       cgd 		unp_gc();
    400  1.14   mycroft 	} else
    401  1.14   mycroft 		free(unp, M_PCB);
    402   1.1       cgd }
    403   1.1       cgd 
    404   1.5    andrew int
    405   1.1       cgd unp_bind(unp, nam, p)
    406   1.1       cgd 	struct unpcb *unp;
    407   1.1       cgd 	struct mbuf *nam;
    408   1.1       cgd 	struct proc *p;
    409   1.1       cgd {
    410  1.20   mycroft 	struct sockaddr_un *sun = mtod(nam, struct sockaddr_un *);
    411   1.1       cgd 	register struct vnode *vp;
    412   1.1       cgd 	struct vattr vattr;
    413   1.1       cgd 	int error;
    414   1.1       cgd 	struct nameidata nd;
    415   1.1       cgd 
    416  1.20   mycroft 	if (unp->unp_vnode != 0)
    417  1.20   mycroft 		return (EINVAL);
    418   1.9   mycroft 	NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT, UIO_SYSSPACE,
    419  1.20   mycroft 	    sun->sun_path, p);
    420  1.20   mycroft 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
    421   1.1       cgd 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
    422   1.1       cgd 			return (EINVAL);
    423   1.1       cgd 	} else
    424   1.1       cgd 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
    425   1.1       cgd /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
    426  1.16  christos 	if ((error = namei(&nd)) != 0)
    427   1.1       cgd 		return (error);
    428   1.9   mycroft 	vp = nd.ni_vp;
    429   1.1       cgd 	if (vp != NULL) {
    430   1.9   mycroft 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    431   1.9   mycroft 		if (nd.ni_dvp == vp)
    432   1.9   mycroft 			vrele(nd.ni_dvp);
    433   1.1       cgd 		else
    434   1.9   mycroft 			vput(nd.ni_dvp);
    435   1.1       cgd 		vrele(vp);
    436   1.1       cgd 		return (EADDRINUSE);
    437   1.1       cgd 	}
    438   1.1       cgd 	VATTR_NULL(&vattr);
    439   1.1       cgd 	vattr.va_type = VSOCK;
    440   1.9   mycroft 	vattr.va_mode = ACCESSPERMS;
    441  1.12   mycroft 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    442  1.16  christos 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
    443  1.16  christos 	if (error)
    444   1.1       cgd 		return (error);
    445   1.9   mycroft 	vp = nd.ni_vp;
    446   1.1       cgd 	vp->v_socket = unp->unp_socket;
    447   1.1       cgd 	unp->unp_vnode = vp;
    448  1.20   mycroft 	unp->unp_addr =
    449  1.20   mycroft 	    mtod(m_copy(nam, 0, (int)M_COPYALL), struct sockaddr_un *);
    450   1.1       cgd 	VOP_UNLOCK(vp);
    451   1.1       cgd 	return (0);
    452   1.1       cgd }
    453   1.1       cgd 
    454   1.5    andrew int
    455   1.1       cgd unp_connect(so, nam, p)
    456   1.1       cgd 	struct socket *so;
    457   1.1       cgd 	struct mbuf *nam;
    458   1.1       cgd 	struct proc *p;
    459   1.1       cgd {
    460  1.20   mycroft 	register struct sockaddr_un *sun = mtod(nam, struct sockaddr_un *);
    461   1.1       cgd 	register struct vnode *vp;
    462   1.1       cgd 	register struct socket *so2, *so3;
    463   1.1       cgd 	struct unpcb *unp2, *unp3;
    464   1.1       cgd 	int error;
    465   1.1       cgd 	struct nameidata nd;
    466   1.1       cgd 
    467  1.20   mycroft 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, sun->sun_path, p);
    468   1.1       cgd 	if (nam->m_data + nam->m_len == &nam->m_dat[MLEN]) {	/* XXX */
    469   1.1       cgd 		if (*(mtod(nam, caddr_t) + nam->m_len - 1) != 0)
    470  1.20   mycroft 			return (EINVAL);
    471   1.1       cgd 	} else
    472   1.1       cgd 		*(mtod(nam, caddr_t) + nam->m_len) = 0;
    473  1.16  christos 	if ((error = namei(&nd)) != 0)
    474   1.1       cgd 		return (error);
    475   1.9   mycroft 	vp = nd.ni_vp;
    476   1.1       cgd 	if (vp->v_type != VSOCK) {
    477   1.1       cgd 		error = ENOTSOCK;
    478   1.1       cgd 		goto bad;
    479   1.1       cgd 	}
    480  1.16  christos 	if ((error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) != 0)
    481   1.1       cgd 		goto bad;
    482   1.1       cgd 	so2 = vp->v_socket;
    483   1.1       cgd 	if (so2 == 0) {
    484   1.1       cgd 		error = ECONNREFUSED;
    485   1.1       cgd 		goto bad;
    486   1.1       cgd 	}
    487   1.1       cgd 	if (so->so_type != so2->so_type) {
    488   1.1       cgd 		error = EPROTOTYPE;
    489   1.1       cgd 		goto bad;
    490   1.1       cgd 	}
    491   1.1       cgd 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    492   1.1       cgd 		if ((so2->so_options & SO_ACCEPTCONN) == 0 ||
    493   1.1       cgd 		    (so3 = sonewconn(so2, 0)) == 0) {
    494   1.1       cgd 			error = ECONNREFUSED;
    495   1.1       cgd 			goto bad;
    496   1.1       cgd 		}
    497   1.1       cgd 		unp2 = sotounpcb(so2);
    498   1.1       cgd 		unp3 = sotounpcb(so3);
    499   1.1       cgd 		if (unp2->unp_addr)
    500  1.20   mycroft 			unp3->unp_addr = mtod(m_copy(dtom(unp2->unp_addr), 0,
    501  1.20   mycroft 			    (int)M_COPYALL), struct sockaddr_un *);
    502   1.1       cgd 		so2 = so3;
    503   1.1       cgd 	}
    504   1.1       cgd 	error = unp_connect2(so, so2);
    505   1.1       cgd bad:
    506   1.1       cgd 	vput(vp);
    507   1.1       cgd 	return (error);
    508   1.1       cgd }
    509   1.1       cgd 
    510   1.5    andrew int
    511   1.1       cgd unp_connect2(so, so2)
    512   1.1       cgd 	register struct socket *so;
    513   1.1       cgd 	register struct socket *so2;
    514   1.1       cgd {
    515   1.1       cgd 	register struct unpcb *unp = sotounpcb(so);
    516   1.1       cgd 	register struct unpcb *unp2;
    517   1.1       cgd 
    518   1.1       cgd 	if (so2->so_type != so->so_type)
    519   1.1       cgd 		return (EPROTOTYPE);
    520   1.1       cgd 	unp2 = sotounpcb(so2);
    521   1.1       cgd 	unp->unp_conn = unp2;
    522   1.1       cgd 	switch (so->so_type) {
    523   1.1       cgd 
    524   1.1       cgd 	case SOCK_DGRAM:
    525   1.1       cgd 		unp->unp_nextref = unp2->unp_refs;
    526   1.1       cgd 		unp2->unp_refs = unp;
    527   1.1       cgd 		soisconnected(so);
    528   1.1       cgd 		break;
    529   1.1       cgd 
    530   1.1       cgd 	case SOCK_STREAM:
    531   1.1       cgd 		unp2->unp_conn = unp;
    532   1.1       cgd 		soisconnected(so);
    533   1.1       cgd 		soisconnected(so2);
    534   1.1       cgd 		break;
    535   1.1       cgd 
    536   1.1       cgd 	default:
    537   1.1       cgd 		panic("unp_connect2");
    538   1.1       cgd 	}
    539   1.1       cgd 	return (0);
    540   1.1       cgd }
    541   1.1       cgd 
    542   1.5    andrew void
    543   1.1       cgd unp_disconnect(unp)
    544   1.1       cgd 	struct unpcb *unp;
    545   1.1       cgd {
    546   1.1       cgd 	register struct unpcb *unp2 = unp->unp_conn;
    547   1.1       cgd 
    548   1.1       cgd 	if (unp2 == 0)
    549   1.1       cgd 		return;
    550   1.1       cgd 	unp->unp_conn = 0;
    551   1.1       cgd 	switch (unp->unp_socket->so_type) {
    552   1.1       cgd 
    553   1.1       cgd 	case SOCK_DGRAM:
    554   1.1       cgd 		if (unp2->unp_refs == unp)
    555   1.1       cgd 			unp2->unp_refs = unp->unp_nextref;
    556   1.1       cgd 		else {
    557   1.1       cgd 			unp2 = unp2->unp_refs;
    558   1.1       cgd 			for (;;) {
    559   1.1       cgd 				if (unp2 == 0)
    560   1.1       cgd 					panic("unp_disconnect");
    561   1.1       cgd 				if (unp2->unp_nextref == unp)
    562   1.1       cgd 					break;
    563   1.1       cgd 				unp2 = unp2->unp_nextref;
    564   1.1       cgd 			}
    565   1.1       cgd 			unp2->unp_nextref = unp->unp_nextref;
    566   1.1       cgd 		}
    567   1.1       cgd 		unp->unp_nextref = 0;
    568   1.1       cgd 		unp->unp_socket->so_state &= ~SS_ISCONNECTED;
    569   1.1       cgd 		break;
    570   1.1       cgd 
    571   1.1       cgd 	case SOCK_STREAM:
    572   1.1       cgd 		soisdisconnected(unp->unp_socket);
    573   1.1       cgd 		unp2->unp_conn = 0;
    574   1.1       cgd 		soisdisconnected(unp2->unp_socket);
    575   1.1       cgd 		break;
    576   1.1       cgd 	}
    577   1.1       cgd }
    578   1.1       cgd 
    579   1.1       cgd #ifdef notdef
    580   1.1       cgd unp_abort(unp)
    581   1.1       cgd 	struct unpcb *unp;
    582   1.1       cgd {
    583   1.1       cgd 
    584   1.1       cgd 	unp_detach(unp);
    585   1.1       cgd }
    586   1.1       cgd #endif
    587   1.1       cgd 
    588   1.5    andrew void
    589   1.1       cgd unp_shutdown(unp)
    590   1.1       cgd 	struct unpcb *unp;
    591   1.1       cgd {
    592   1.1       cgd 	struct socket *so;
    593   1.1       cgd 
    594   1.1       cgd 	if (unp->unp_socket->so_type == SOCK_STREAM && unp->unp_conn &&
    595   1.1       cgd 	    (so = unp->unp_conn->unp_socket))
    596   1.1       cgd 		socantrcvmore(so);
    597   1.1       cgd }
    598   1.1       cgd 
    599   1.5    andrew void
    600   1.1       cgd unp_drop(unp, errno)
    601   1.1       cgd 	struct unpcb *unp;
    602   1.1       cgd 	int errno;
    603   1.1       cgd {
    604   1.1       cgd 	struct socket *so = unp->unp_socket;
    605   1.1       cgd 
    606   1.1       cgd 	so->so_error = errno;
    607   1.1       cgd 	unp_disconnect(unp);
    608   1.1       cgd 	if (so->so_head) {
    609  1.15   mycroft 		so->so_pcb = 0;
    610  1.14   mycroft 		sofree(so);
    611  1.20   mycroft 		if (unp->unp_addr)
    612  1.20   mycroft 			m_freem(dtom(unp->unp_addr));
    613  1.14   mycroft 		free(unp, M_PCB);
    614   1.1       cgd 	}
    615   1.1       cgd }
    616   1.1       cgd 
    617   1.1       cgd #ifdef notdef
    618   1.1       cgd unp_drain()
    619   1.1       cgd {
    620   1.1       cgd 
    621   1.1       cgd }
    622   1.1       cgd #endif
    623   1.1       cgd 
    624   1.5    andrew int
    625   1.1       cgd unp_externalize(rights)
    626   1.1       cgd 	struct mbuf *rights;
    627   1.1       cgd {
    628   1.1       cgd 	struct proc *p = curproc;		/* XXX */
    629   1.1       cgd 	register struct cmsghdr *cm = mtod(rights, struct cmsghdr *);
    630  1.24       cgd 	register int i, *fdp = (int *)(cm + 1);
    631  1.24       cgd 	register struct file **rp = (struct file **)ALIGN(cm + 1);
    632   1.1       cgd 	register struct file *fp;
    633  1.24       cgd 	int nfds = (cm->cmsg_len - ALIGN(sizeof(*cm))) / sizeof (struct file *);
    634   1.1       cgd 	int f;
    635   1.1       cgd 
    636  1.24       cgd 	/* Make sure that the recipient has space */
    637  1.24       cgd 	if (!fdavail(p, nfds)) {
    638  1.24       cgd 		for (i = 0; i < nfds; i++) {
    639   1.1       cgd 			fp = *rp;
    640   1.1       cgd 			unp_discard(fp);
    641   1.1       cgd 			*rp++ = 0;
    642   1.1       cgd 		}
    643   1.1       cgd 		return (EMSGSIZE);
    644   1.1       cgd 	}
    645  1.24       cgd 
    646  1.24       cgd 	/*
    647  1.24       cgd 	 * Add file to the recipient's open file table, converting them
    648  1.24       cgd 	 * to integer file descriptors as we go.  Done in forward order
    649  1.24       cgd 	 * because an integer will always come in the same place or before
    650  1.24       cgd 	 * its corresponding struct file pointer.
    651  1.24       cgd 	 */
    652  1.24       cgd 	for (i = 0; i < nfds; i++) {
    653   1.1       cgd 		if (fdalloc(p, 0, &f))
    654   1.1       cgd 			panic("unp_externalize");
    655   1.1       cgd 		fp = *rp;
    656   1.1       cgd 		p->p_fd->fd_ofiles[f] = fp;
    657   1.1       cgd 		fp->f_msgcount--;
    658   1.1       cgd 		unp_rights--;
    659  1.24       cgd 		*fdp++ = f;
    660   1.1       cgd 	}
    661  1.24       cgd 
    662  1.24       cgd 	/*
    663  1.24       cgd 	 * Adjust length, in case of transition from large struct file
    664  1.24       cgd 	 * pointers to ints.
    665  1.24       cgd 	 */
    666  1.24       cgd 	cm->cmsg_len = sizeof(*cm) + (nfds * sizeof(int));
    667  1.24       cgd 	rights->m_len = cm->cmsg_len;
    668   1.1       cgd 	return (0);
    669   1.1       cgd }
    670   1.1       cgd 
    671   1.5    andrew int
    672   1.1       cgd unp_internalize(control, p)
    673   1.1       cgd 	struct mbuf *control;
    674   1.1       cgd 	struct proc *p;
    675   1.1       cgd {
    676  1.24       cgd 	struct filedesc *fdescp = p->p_fd;
    677   1.1       cgd 	register struct cmsghdr *cm = mtod(control, struct cmsghdr *);
    678   1.1       cgd 	register struct file **rp;
    679   1.1       cgd 	register struct file *fp;
    680  1.24       cgd 	register int i, fd, *fdp;
    681  1.24       cgd 	int nfds;
    682  1.24       cgd 	u_int neededspace;
    683   1.1       cgd 
    684  1.24       cgd 	/* Sanity check the control message header */
    685   1.1       cgd 	if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET ||
    686   1.1       cgd 	    cm->cmsg_len != control->m_len)
    687   1.1       cgd 		return (EINVAL);
    688  1.24       cgd 
    689  1.24       cgd 	/* Verify that the file descriptors are valid */
    690  1.24       cgd 	nfds = (cm->cmsg_len - sizeof (*cm)) / sizeof (int);
    691  1.24       cgd 	fdp = (int *)(cm + 1);
    692  1.24       cgd 	for (i = 0; i < nfds; i++) {
    693  1.24       cgd 		fd = *fdp++;
    694  1.24       cgd 		if ((unsigned)fd >= fdescp->fd_nfiles ||
    695  1.24       cgd 		    fdescp->fd_ofiles[fd] == NULL)
    696   1.1       cgd 			return (EBADF);
    697   1.1       cgd 	}
    698  1.24       cgd 
    699  1.24       cgd 	/* Make sure we have room for the struct file pointers */
    700  1.24       cgd morespace:
    701  1.24       cgd 	neededspace = (ALIGN(sizeof (*cm)) + nfds * sizeof (struct file *)) -
    702  1.24       cgd 		control->m_len;
    703  1.24       cgd 	if (neededspace > M_TRAILINGSPACE(control)) {
    704  1.24       cgd 
    705  1.24       cgd 		/* if we already have a cluster, the message is just too big */
    706  1.24       cgd 		if (control->m_flags & M_EXT)
    707  1.24       cgd 			return (E2BIG);
    708  1.24       cgd 
    709  1.24       cgd 		/* allocate a cluster and try again */
    710  1.24       cgd 		MCLGET(control, M_WAIT);
    711  1.24       cgd 		if ((control->m_flags & M_EXT) == 0)
    712  1.24       cgd 			return (ENOBUFS);	/* allocation failed */
    713  1.24       cgd 
    714  1.24       cgd 		/* copy the data to the cluster */
    715  1.24       cgd 		bcopy(cm, mtod(control, char *), cm->cmsg_len);
    716  1.24       cgd 		cm = mtod(control, struct cmsghdr *);
    717  1.24       cgd 		goto morespace;
    718  1.24       cgd 	}
    719  1.24       cgd 
    720  1.24       cgd 	/* adjust message & mbuf to note amount of space actually used. */
    721  1.24       cgd 	cm->cmsg_len += neededspace;
    722  1.24       cgd 	control->m_len = cm->cmsg_len;
    723  1.24       cgd 
    724  1.24       cgd 	/*
    725  1.24       cgd 	 * Transform the file descriptors into struct file pointers, in
    726  1.24       cgd 	 * reverse order so that if pointers are bigger than ints, the
    727  1.24       cgd 	 * int won't get until we're done.
    728  1.24       cgd 	 */
    729  1.24       cgd 	fdp = ((int *)(cm + 1)) + nfds - 1;
    730  1.24       cgd 	rp = ((struct file **)ALIGN(cm + 1)) + nfds - 1;
    731  1.24       cgd 	for (i = 0; i < nfds; i++) {
    732  1.24       cgd 		fp = fdescp->fd_ofiles[*fdp];
    733  1.24       cgd 		*rp-- = fp;
    734   1.1       cgd 		fp->f_count++;
    735   1.1       cgd 		fp->f_msgcount++;
    736   1.1       cgd 		unp_rights++;
    737   1.1       cgd 	}
    738   1.1       cgd 	return (0);
    739   1.1       cgd }
    740   1.1       cgd 
    741   1.1       cgd int	unp_defer, unp_gcing;
    742   1.1       cgd extern	struct domain unixdomain;
    743   1.1       cgd 
    744   1.5    andrew void
    745   1.1       cgd unp_gc()
    746   1.1       cgd {
    747   1.8   mycroft 	register struct file *fp, *nextfp;
    748   1.1       cgd 	register struct socket *so;
    749   1.8   mycroft 	struct file **extra_ref, **fpp;
    750   1.8   mycroft 	int nunref, i;
    751   1.1       cgd 
    752   1.1       cgd 	if (unp_gcing)
    753   1.1       cgd 		return;
    754   1.1       cgd 	unp_gcing = 1;
    755   1.1       cgd 	unp_defer = 0;
    756  1.11   mycroft 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next)
    757   1.1       cgd 		fp->f_flag &= ~(FMARK|FDEFER);
    758   1.1       cgd 	do {
    759  1.11   mycroft 		for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    760   1.1       cgd 			if (fp->f_count == 0)
    761   1.1       cgd 				continue;
    762   1.1       cgd 			if (fp->f_flag & FDEFER) {
    763   1.1       cgd 				fp->f_flag &= ~FDEFER;
    764   1.1       cgd 				unp_defer--;
    765   1.1       cgd 			} else {
    766   1.1       cgd 				if (fp->f_flag & FMARK)
    767   1.1       cgd 					continue;
    768   1.1       cgd 				if (fp->f_count == fp->f_msgcount)
    769   1.1       cgd 					continue;
    770   1.1       cgd 				fp->f_flag |= FMARK;
    771   1.1       cgd 			}
    772   1.1       cgd 			if (fp->f_type != DTYPE_SOCKET ||
    773   1.1       cgd 			    (so = (struct socket *)fp->f_data) == 0)
    774   1.1       cgd 				continue;
    775   1.1       cgd 			if (so->so_proto->pr_domain != &unixdomain ||
    776   1.1       cgd 			    (so->so_proto->pr_flags&PR_RIGHTS) == 0)
    777   1.1       cgd 				continue;
    778   1.1       cgd #ifdef notdef
    779   1.1       cgd 			if (so->so_rcv.sb_flags & SB_LOCK) {
    780   1.1       cgd 				/*
    781   1.1       cgd 				 * This is problematical; it's not clear
    782   1.1       cgd 				 * we need to wait for the sockbuf to be
    783   1.1       cgd 				 * unlocked (on a uniprocessor, at least),
    784   1.1       cgd 				 * and it's also not clear what to do
    785   1.1       cgd 				 * if sbwait returns an error due to receipt
    786   1.1       cgd 				 * of a signal.  If sbwait does return
    787   1.1       cgd 				 * an error, we'll go into an infinite
    788   1.1       cgd 				 * loop.  Delete all of this for now.
    789   1.1       cgd 				 */
    790   1.1       cgd 				(void) sbwait(&so->so_rcv);
    791   1.1       cgd 				goto restart;
    792   1.1       cgd 			}
    793   1.1       cgd #endif
    794   1.1       cgd 			unp_scan(so->so_rcv.sb_mb, unp_mark);
    795   1.1       cgd 		}
    796   1.1       cgd 	} while (unp_defer);
    797   1.8   mycroft 	/*
    798   1.8   mycroft 	 * We grab an extra reference to each of the file table entries
    799   1.8   mycroft 	 * that are not otherwise accessible and then free the rights
    800   1.8   mycroft 	 * that are stored in messages on them.
    801   1.8   mycroft 	 *
    802   1.8   mycroft 	 * The bug in the orginal code is a little tricky, so I'll describe
    803   1.8   mycroft 	 * what's wrong with it here.
    804   1.8   mycroft 	 *
    805   1.8   mycroft 	 * It is incorrect to simply unp_discard each entry for f_msgcount
    806   1.8   mycroft 	 * times -- consider the case of sockets A and B that contain
    807   1.8   mycroft 	 * references to each other.  On a last close of some other socket,
    808   1.8   mycroft 	 * we trigger a gc since the number of outstanding rights (unp_rights)
    809   1.8   mycroft 	 * is non-zero.  If during the sweep phase the gc code un_discards,
    810   1.8   mycroft 	 * we end up doing a (full) closef on the descriptor.  A closef on A
    811   1.8   mycroft 	 * results in the following chain.  Closef calls soo_close, which
    812   1.8   mycroft 	 * calls soclose.   Soclose calls first (through the switch
    813   1.8   mycroft 	 * uipc_usrreq) unp_detach, which re-invokes unp_gc.  Unp_gc simply
    814   1.8   mycroft 	 * returns because the previous instance had set unp_gcing, and
    815   1.8   mycroft 	 * we return all the way back to soclose, which marks the socket
    816   1.8   mycroft 	 * with SS_NOFDREF, and then calls sofree.  Sofree calls sorflush
    817   1.8   mycroft 	 * to free up the rights that are queued in messages on the socket A,
    818   1.8   mycroft 	 * i.e., the reference on B.  The sorflush calls via the dom_dispose
    819   1.8   mycroft 	 * switch unp_dispose, which unp_scans with unp_discard.  This second
    820   1.8   mycroft 	 * instance of unp_discard just calls closef on B.
    821   1.8   mycroft 	 *
    822   1.8   mycroft 	 * Well, a similar chain occurs on B, resulting in a sorflush on B,
    823   1.8   mycroft 	 * which results in another closef on A.  Unfortunately, A is already
    824   1.8   mycroft 	 * being closed, and the descriptor has already been marked with
    825   1.8   mycroft 	 * SS_NOFDREF, and soclose panics at this point.
    826   1.8   mycroft 	 *
    827   1.8   mycroft 	 * Here, we first take an extra reference to each inaccessible
    828   1.8   mycroft 	 * descriptor.  Then, we call sorflush ourself, since we know
    829   1.8   mycroft 	 * it is a Unix domain socket anyhow.  After we destroy all the
    830   1.8   mycroft 	 * rights carried in messages, we do a last closef to get rid
    831   1.8   mycroft 	 * of our extra reference.  This is the last close, and the
    832   1.8   mycroft 	 * unp_detach etc will shut down the socket.
    833   1.8   mycroft 	 *
    834   1.8   mycroft 	 * 91/09/19, bsy (at) cs.cmu.edu
    835   1.8   mycroft 	 */
    836   1.8   mycroft 	extra_ref = malloc(nfiles * sizeof(struct file *), M_FILE, M_WAITOK);
    837  1.11   mycroft 	for (nunref = 0, fp = filehead.lh_first, fpp = extra_ref; fp != 0;
    838  1.11   mycroft 	    fp = nextfp) {
    839  1.11   mycroft 		nextfp = fp->f_list.le_next;
    840   1.1       cgd 		if (fp->f_count == 0)
    841   1.1       cgd 			continue;
    842   1.8   mycroft 		if (fp->f_count == fp->f_msgcount && !(fp->f_flag & FMARK)) {
    843   1.8   mycroft 			*fpp++ = fp;
    844   1.8   mycroft 			nunref++;
    845   1.8   mycroft 			fp->f_count++;
    846   1.8   mycroft 		}
    847   1.1       cgd 	}
    848   1.8   mycroft 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
    849   1.8   mycroft 		sorflush((struct socket *)(*fpp)->f_data);
    850   1.8   mycroft 	for (i = nunref, fpp = extra_ref; --i >= 0; ++fpp)
    851  1.13   mycroft 		(void) closef(*fpp, (struct proc *)0);
    852   1.8   mycroft 	free((caddr_t)extra_ref, M_FILE);
    853   1.1       cgd 	unp_gcing = 0;
    854   1.1       cgd }
    855   1.1       cgd 
    856   1.5    andrew void
    857   1.1       cgd unp_dispose(m)
    858   1.1       cgd 	struct mbuf *m;
    859   1.1       cgd {
    860   1.8   mycroft 
    861   1.1       cgd 	if (m)
    862   1.1       cgd 		unp_scan(m, unp_discard);
    863   1.1       cgd }
    864   1.1       cgd 
    865   1.5    andrew void
    866   1.1       cgd unp_scan(m0, op)
    867   1.1       cgd 	register struct mbuf *m0;
    868   1.5    andrew 	void (*op) __P((struct file *));
    869   1.1       cgd {
    870   1.1       cgd 	register struct mbuf *m;
    871   1.1       cgd 	register struct file **rp;
    872   1.1       cgd 	register struct cmsghdr *cm;
    873   1.1       cgd 	register int i;
    874   1.1       cgd 	int qfds;
    875   1.1       cgd 
    876   1.1       cgd 	while (m0) {
    877   1.1       cgd 		for (m = m0; m; m = m->m_next)
    878   1.1       cgd 			if (m->m_type == MT_CONTROL &&
    879   1.1       cgd 			    m->m_len >= sizeof(*cm)) {
    880   1.1       cgd 				cm = mtod(m, struct cmsghdr *);
    881   1.1       cgd 				if (cm->cmsg_level != SOL_SOCKET ||
    882   1.1       cgd 				    cm->cmsg_type != SCM_RIGHTS)
    883   1.1       cgd 					continue;
    884   1.1       cgd 				qfds = (cm->cmsg_len - sizeof *cm)
    885   1.1       cgd 						/ sizeof (struct file *);
    886   1.1       cgd 				rp = (struct file **)(cm + 1);
    887   1.1       cgd 				for (i = 0; i < qfds; i++)
    888   1.1       cgd 					(*op)(*rp++);
    889   1.1       cgd 				break;		/* XXX, but saves time */
    890   1.1       cgd 			}
    891   1.1       cgd 		m0 = m0->m_act;
    892   1.1       cgd 	}
    893   1.1       cgd }
    894   1.1       cgd 
    895   1.5    andrew void
    896   1.1       cgd unp_mark(fp)
    897   1.1       cgd 	struct file *fp;
    898   1.1       cgd {
    899   1.1       cgd 
    900   1.1       cgd 	if (fp->f_flag & FMARK)
    901   1.1       cgd 		return;
    902   1.1       cgd 	unp_defer++;
    903   1.1       cgd 	fp->f_flag |= (FMARK|FDEFER);
    904   1.1       cgd }
    905   1.1       cgd 
    906   1.5    andrew void
    907   1.1       cgd unp_discard(fp)
    908   1.1       cgd 	struct file *fp;
    909   1.1       cgd {
    910   1.8   mycroft 
    911   1.1       cgd 	fp->f_msgcount--;
    912   1.1       cgd 	unp_rights--;
    913  1.13   mycroft 	(void) closef(fp, (struct proc *)0);
    914   1.1       cgd }
    915