From: [email-masked]
Date: Wed, 24 Mar 93 12:59:57

# 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