Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 26 Feb 2005 12:04:10 +0000 (GMT)
From:      Robert Watson <rwatson@FreeBSD.org>
To:        Yan Yu <yanyu@CS.UCLA.EDU>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: send file descriptor via ipc
Message-ID:  <Pine.NEB.3.96L.1050226115506.62606D-100000@fledge.watson.org>
In-Reply-To: <Pine.GSO.4.58.0502260043200.12442@panther.cs.ucla.edu>

next in thread | previous in thread | raw e-mail | index | archive | help

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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.96L.1050226115506.62606D-100000>