From @pucc:roeber@vxcrna.cern.ch Tue Mar 23 07:04:57 1993
Received: from pucc.Princeton.EDU by phoenix.princeton.edu (5.65c/1.113/new)
	id AA09460; Tue, 23 Mar 1993 07:04:54 -0500
Received: from PUCC.PRINCETON.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2)
   with BSMTP id 3484; Tue, 23 Mar 93 07:04:21 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 1090; Tue, 23 Mar 93 07:04:20 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 1089; Tue, 23 Mar 93 07:04:20 EST
Received: from Princeton.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2) with TCP;
   Tue, 23 Mar 93 07:04:19 EST
Received: from dxmint.cern.ch by Princeton.EDU (5.65b/princetonevision: 2.96 princeton)
 	id AA24471; Tue, 23 Mar 93 07:04:49 -0500
Received: from VXCERN.DECnet MAIL11D_V3 by dxmint.cern.ch (5.65/DEC-Ultrix/4.3)
 	id AA16785; Tue, 23 Mar 1993 13:04:47 +0100
Date: Tue, 23 Mar 1993 13:04:47 +0100
Message-Id: <9303231204.AA16785@dxmint.cern.ch>
From: roeber@vxcrna.cern.ch (Frederick G.M. Roeber)
X-Vms-To: dxmint::dawagner@princeton.edu
X-Vms-Cc: ROEBER
Subject: Re: Passing file descriptors between processes
X-Mail11-Ostype: VAX/VMS
Apparently-To: <roeber@cern.ch>
Apparently-To: <dawagner@Princeton.EDU>
Status: OR

>How does one pass an open file descriptor from one process to another?
>
>I heard that it might have to do with the msg_accrights field of the
>msghdr structure used by sendmsg(2), but I couldn't find anything in
>the man pages or the FAQs...  I could've sworn I'd seen this answered
>somewhere before, how do you do it?

Under BSD-type systems, this is how you do it.  Look at the recv(2) page.
Under SysV-type systems, there's a streamio(7) command (I_SENDFD).

Examples of both of these are available on the net, in the form of the
sample code of Stevens' book "Unix Network Programming."

One location for these examples is:
    ftp://ftp.uu.net/published/books/stevens.netprog.tar.Z

Or try archie for more.

Regards,
Frederick.

From @pucc:barmar@Think.COM Tue Mar 23 20:11:36 1993
Received: from pucc.Princeton.EDU by phoenix.princeton.edu (5.65c/1.113/new)
	id AA04204; Tue, 23 Mar 1993 20:11:35 -0500
Received: from PUCC.PRINCETON.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2)
   with BSMTP id 9598; Tue, 23 Mar 93 20:11:02 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 9154; Tue, 23 Mar 93 20:11:01 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 9132; Tue, 23 Mar 93 20:10:59 EST
Received: from Princeton.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2) with TCP;
   Tue, 23 Mar 93 20:10:42 EST
Received: from Mail.Think.COM by Princeton.EDU (5.65b/2.96/princeton)
 	id AA02054; Tue, 23 Mar 93 20:11:14 -0500
Received: from Telecaster.Think.COM by mail.think.com; Tue, 23 Mar 93 20:11:12 -0500
From: Barry Margolin <barmar@Think.COM>
Received: by telecaster.think.com (4.1/Think-1.2)
 	id AA06483; Tue, 23 Mar 93 20:11:12 EST
Date: Tue, 23 Mar 93 20:11:12 EST
Message-Id: <9303240111.AA06483@telecaster.think.com>
To: dawagner@Princeton.EDU
Subject: Re: Passing file descriptors between processes
Newsgroups: comp.unix.questions,comp.unix.internals
In-Reply-To: <1993Mar22.222527.23621@Princeton.EDU>
Organization: Thinking Machines Corporation, Cambridge MA, USA
Cc: 
Status: OR

In article <1993Mar22.222527.23621@Princeton.EDU> you write:
>I heard that it might have to do with the msg_accrights field of the
>msghdr structure used by sendmsg(2),

You heard right.

>				      but I couldn't find anything in
>the man pages or the FAQs...  I could've sworn I'd seen this answered
>somewhere before, how do you do it?

It's answered in "Unix Network Programming" by W. Richard Stevens.
--
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar

From @pucc:forsyth@minster.york.ac.uk Wed Mar 24 08:11:06 1993
Received: from pucc.Princeton.EDU by phoenix.princeton.edu (5.65c/1.113/new)
	id AA21246; Wed, 24 Mar 1993 08:11:05 -0500
Received: from PUCC.PRINCETON.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2)
   with BSMTP id 2481; Wed, 24 Mar 93 08:10:31 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 0042; Wed, 24 Mar 93 08:10:31 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 0041; Wed, 24 Mar 93 08:10:30 EST
Received: from Princeton.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2) with TCP;
   Wed, 24 Mar 93 08:10:29 EST
Received: from minster.york.ac.uk by Princeton.EDU (5.65b/2.96/princeton)
 	id AA20147; Wed, 24 Mar 93 08:10:34 -0500
From: forsyth@minster.york.ac.uk
Date: Wed, 24 Mar 93 12:59:57
To: dawagner@Princeton.EDU
Message-Id: <swordfish.732978648@minster.york.ac.uk>
Status: OR

# this works for me on an rs6000.
#  sending some data with the fd is *essential* to avoid crashing some systems, owing to bsd bugs

echo stream.h
sed 's/^-//' >stream.h << '//sysin.dd.end of stream.h'
-#ifdef SOCKIMP
-#include <sys/socket.h>
-#include <sys/un.h>
-struct passfd {
-	union {
-		int	fd;
-		struct {
-			struct	file	*fp;
-			struct	stream	*sp;
-		} s;
-	} f;
-	short	uid;
-	short	gid;
-	short	nice;
-	char	logname[8];
-};
-extern int	sendfile(int, int);
-extern struct passfd recvfile(int);
-#endif
-#ifdef STREAMIMP
-#include <sys/stream.h>
-#endif
//sysin.dd.end of stream.h
echo socksend.c
sed 's/^-//' >socksend.c << '//sysin.dd.end of socksend.c'
-/*
- * send and receive a file descriptor on a stream
- */
-
-#define	_BSD
-#define	NULL	0
-#include <libc.h>
-#ifdef SOCKIMP
-#include <sys/types.h>
-#include "stream.h"
-#include <sys/socket.h>
-#include <sys/uio.h>
-
-static struct msghdr msg;
-
-int
-sendfile(int wfd, int fd)
-{
-	int x;
-	unsigned char b[2];
-	struct iovec iov;
-
-	b[0] = 0xab;
-	b[1] = 0xcd;
-	msg.msg_name = 0;
-	msg.msg_namelen = 0;
-	iov.iov_base = (caddr_t)b;
-	iov.iov_len = sizeof(b);
-	msg.msg_iov = &iov;
-	msg.msg_iovlen = 1;
-	msg.msg_accrights = (caddr_t)&fd;
-	msg.msg_accrightslen = sizeof(fd);
-	return sendmsg(wfd, &msg, 0);
-}
-
-struct passfd
-recvfile(int rfd)
-{
-	struct passfd p;
-	unsigned char b[2];
-	struct iovec iov;
-	int nb;
-
-	p.f.fd = -1;
-	msg.msg_name = 0;
-	msg.msg_namelen = 0;
-	iov.iov_base = (caddr_t)b;
-	iov.iov_len = sizeof(b);
-	msg.msg_iov = &iov;
-	msg.msg_iovlen = 1;
-	msg.msg_accrights = (caddr_t)&p.f.fd;
-	msg.msg_accrightslen = sizeof(p.f.fd);
-	nb = recvmsg(rfd, &msg, MSG_PEEK);
-	if (nb < 0) {
-		p.f.fd = -1;
-		return p;
-	}
-	if (nb == sizeof(b) && b[0] == 0xab && b[1] == 0xcd) {
-		msg.msg_accrights = (caddr_t)&p.f.fd;
-		msg.msg_accrightslen = sizeof(p.f.fd);
-		if (recvmsg(rfd, &msg, 0) < 0)
-			p.f.fd = -1;
-	} else
-		p.f.fd = -1;
-	return p;
-}
-#endif
//sysin.dd.end of socksend.c


From @pucc:forsyth@minster.york.ac.uk Wed Mar 24 08:11:17 1993
Received: from pucc.Princeton.EDU by phoenix.princeton.edu (5.65c/1.113/new)
	id AA21250; Wed, 24 Mar 1993 08:11:17 -0500
Received: from PUCC.PRINCETON.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2)
   with BSMTP id 2483; Wed, 24 Mar 93 08:10:43 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 0047; Wed, 24 Mar 93 08:10:43 EST
Received: from PUCC by PUCC.PRINCETON.EDU (Mailer R2.10 ptf004) with BSMTP id
 0046; Wed, 24 Mar 93 08:10:42 EST
Received: from Princeton.EDU by pucc.Princeton.EDU (IBM VM SMTP V2R2) with TCP;
   Wed, 24 Mar 93 08:10:41 EST
Received: from minster.york.ac.uk by Princeton.EDU (5.65b/2.96/princeton)
 	id AA20805; Wed, 24 Mar 93 08:11:07 -0500
From: forsyth@minster.york.ac.uk
Date: Wed, 24 Mar 93 13:00:40
To: dawagner@Princeton.EDU
Message-Id: <swordfish.732978676@minster.york.ac.uk>
Subject: sending file descriptors
Status: OR

oh yes.  the rfd and wfd are file descriptors referring to
a Unix domain SOCK_STREAM.


