From owner-freebsd-hackers@FreeBSD.ORG Sat Feb 26 12:05:59 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D901C16A4CE for ; Sat, 26 Feb 2005 12:05:59 +0000 (GMT) Received: from cyrus.watson.org (cyrus.watson.org [204.156.12.53]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8C53343D3F for ; Sat, 26 Feb 2005 12:05:59 +0000 (GMT) (envelope-from robert@fledge.watson.org) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by cyrus.watson.org (Postfix) with SMTP id F365E46B11; Sat, 26 Feb 2005 07:05:58 -0500 (EST) Date: Sat, 26 Feb 2005 12:04:10 +0000 (GMT) From: Robert Watson X-Sender: robert@fledge.watson.org To: Yan Yu In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: freebsd-hackers@freebsd.org Subject: Re: send file descriptor via ipc X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Feb 2005 12:06:00 -0000 On Sat, 26 Feb 2005, Yan Yu wrote: > I am looking for the function that handle send/recv file descriptors > through sendmsg/recvmsg? by looking through source files, > unp_internalize/unp_externalize in kern/uipc-usrreq.c seems relevant.. > > but i am not sure since i could not guess the meaning of the function > name.. (btw, what is "unp", the prefix in the function name stands > for?) Can someone please confirm if it is the right function or point > me to the right one i should look at? You are indeed looking in the right place. When sendmsg() is called with ancillary data, sosend() will copy in the ancillary data into an mbuf chain named 'control', which is passed into pru_send(). pru_send() for UNIX domain sockets is implemented by uipc_send(), which will call unp_internalize() to convert file descriptor numbers into 'struct file' references. sbappendcontrol_locked() is used to append the the message and ancillary data onto the receive socket buffer for the remote socket. When recvmsg() is called, soreceive() will retrieve the regular and control mbufs from the receive socket buffer. It then calls the protocol domain's externalize function, which is implemented by unp_externalize(). unp_externalize() will extract the 'struct file' references and insert them into the file descriptor array of the receiving process. One of the complications in this code has to do with unp_gc() -- the problem is that when you send a file descriptor over a UNIX domain socket, the socket could be closed by both ends before the file descriptor is received. If the file descriptor "in transit" is a reference to the UNIX domain socket that the file descriptor was sent over, there's a cyclic reference. un_gpc() uses a mark-and-sweep graph algorithm to walk the set of file descriptors reachable by processes and identify sets of file descriptors that are referenced only by disconnected UNIX domain sockets so that those sockets can be reclaimed. Robert N M Watson