Re: How do you passing an fd over a pipe?

Alex Belits (abelits@genesyslab.com)
Thu, 16 Jul 1998 18:02:50 -0700 (PDT)


On Thu, 16 Jul 1998, Marty Leisner wrote:

> > Can someone tell me how you'd pass an fd over a pipe in 2.0.34 or 2.1.108? My
> > company does some nifty stuff with this facility on Solaris and Hpux, and we
> > can emulate it on Win32 with DuplicateHandle(), but I can't work out how to do
> > it under Linux.
> >
> > Many thanks,
> > David Howells
> >
>
> Passing fd over pipes is a streams thing (I think).

SysV STREAMS, 4.3BSD and 4.4BSD implementations of fd passing differ,
but it's present in all three. Linux is compatible with 4.4BSD, at least
by formats.

> You can use sendmsg/recvmsg to pass fds between processes (you
> need to make a rendezvous point).
>
> I don't know if this works over socketpair (i.e. a pipe) (does it??)

pipe() creates an anonymous pipe that can't pass anything but data, at
least this is how it's supposed to work. socketpair() can create AF_UNIX
socket that can be used for fd passing using SOL_SOCKET level SCM_RIGHTS
type with sendmsg(). See R. Stevens' UNP *and* TCP/IP Illustrated for
details.

IIRC Linux implementation requires data to be present in the same
message.

There is a known bug in include files from early glibc2 versions --
SCM_RIGHTS is in enum that makes it equal to 0 while correct value is 1.
Also include files that come with glibc2 define cmsg_data as a
macro CMSG_DATA(cm) while others have it as a structure member
cm->cmsg_data. Change to macro in glibc2 includes is done for ANSI C
compatibility because the original definition of cmsg_data is a
zero-length array.

While there aren't many examples of code that uses fd passing, fhttpd
(http://phobos.illtel.denver.co.us/pub/fhttpd/) does it, so you can look
at its sources.

--
Alex

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html