From owner-freebsd-hackers Fri Aug 28 18:13:26 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id SAA19987 for freebsd-hackers-outgoing; Fri, 28 Aug 1998 18:13:26 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from skynet.ctr.columbia.edu (skynet.ctr.columbia.edu [128.59.64.70]) by hub.freebsd.org (8.8.8/8.8.8) with SMTP id SAA19969 for ; Fri, 28 Aug 1998 18:13:15 -0700 (PDT) (envelope-from wpaul@skynet.ctr.columbia.edu) Received: (from wpaul@localhost) by skynet.ctr.columbia.edu (8.6.12/8.6.9) id VAA28236; Fri, 28 Aug 1998 21:14:48 -0400 From: Bill Paul Message-Id: <199808290114.VAA28236@skynet.ctr.columbia.edu> Subject: Re: Help with passing fd on FreeBSD To: chanders@timing.com (Craig Anderson) Date: Fri, 28 Aug 1998 21:14:46 -0400 (EDT) Cc: freebsd-hackers@FreeBSD.ORG In-Reply-To: <199808281426.IAA10588@count.timing.com> from "Craig Anderson" at Aug 28, 98 08:26:25 am X-Mailer: ELM [version 2.4 PL24] Content-Type: text Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Of all the gin joints in all the towns in all the world, Craig Anderson had to walk into mine and say: > > Can someone give me pointers on passing open file descriptors on FreeBSD? > This is covered in W. Richard Stevens Advanced programming book, but > the structs in FreeBSD are slightly different, and I've never done this. > > Thanks, > Craig Sure. Appended to this message are two small programs: r.c and s.c. s.c sends a descriptor and r.c receives it. Last time I used this was on FreeBSD 2.1.0 but it should still work. The descriptor being sent is actually the AF_LOCAL socket that s.c creates to talk to r.c, which is kind of pointless but you can use any other descriptor index you want (as long as it refers to a valid open descriptor). -Bill -- ============================================================================= -Bill Paul (212) 854-6020 | System Manager, Master of Unix-Fu Work: wpaul@ctr.columbia.edu | Center for Telecommunications Research Home: wpaul@skynet.ctr.columbia.edu | Columbia University, New York City ============================================================================= "It is not I who am crazy; it is I who am mad!" - Ren Hoek, "Space Madness" ============================================================================= #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef lint static const char rcsid[] = "$Id: r.c,v 1.4 1998/08/29 01:03:52 wpaul Exp $"; #endif struct cmessage { struct cmsghdr cmsg; int fd; }; int main() { struct iovec iov[1]; struct msghdr msg; struct cmessage cm; struct sockaddr_un sun; int s; int sock; int len; fd_set readfds; char recbuf[1024]; if (unlink("/tmp/testsock") == -1) warn("couldn't remove old socket"); strcpy(sun.sun_path, "/tmp/testsock"); sun.sun_family = AF_UNIX; len = sun.sun_len = sizeof(sun.sun_len) + sizeof(sun.sun_family) + strlen(sun.sun_path) + 1; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) err(1, "socket creation failed"); iov[0].iov_base = (char *)&recbuf; iov[0].iov_len = sizeof(recbuf); bzero((char *)&cm, sizeof(cm)); bzero((char *)&msg, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = sizeof(struct cmessage); msg.msg_flags = 0; if (bind(s, (struct sockaddr *)&sun, len) == -1) err(1, "couldn't bind socket"); listen(s, 2); if ((sock = accept(s, (struct sockaddr *)&sun, &len)) == -1) err(1, "accept failed"); FD_ZERO(&readfds); FD_SET(sock, &readfds); select(FD_SETSIZE, &readfds, NULL, NULL, NULL); if ((len = recvmsg(sock, &msg, 0)) == -1) err(1, "recvmsg failed"); printf ("read %d bytes\n", len); printf ("data: %s\n", iov[0].iov_base); if (cm.cmsg.cmsg_type != SCM_RIGHTS) warnx("message didn't contain SCM_RIGHTS"); printf ("FD: %d\n", cm.fd); close(sock); close(s); exit(0); } #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef lint static const char rcsid[] = "$Id: s.c,v 1.4 1998/08/29 01:05:28 wpaul Exp $"; #endif struct cmessage { struct cmsghdr cmsg; int fd; }; int main() { struct iovec iov[1]; struct msghdr msg; struct cmessage cm; struct sockaddr_un sun; int s; int len; char sendbuf[] = "This is a test buffer."; strcpy(sun.sun_path, "/tmp/testsock"); sun.sun_family = AF_UNIX; len = sun.sun_len = sizeof(sun.sun_len) + sizeof(sun.sun_family) + strlen(sun.sun_path) + 1; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) err(1, "socket creation failed"); if (connect(s, (struct sockaddr *)&sun, len) == -1) err(1, "couldn't connect socket"); iov[0].iov_base = (char *)&sendbuf; iov[0].iov_len = sizeof(sendbuf); cm.cmsg.cmsg_type = SCM_RIGHTS; cm.cmsg.cmsg_level = SOL_SOCKET; cm.cmsg.cmsg_len = sizeof(struct cmessage); cm.fd = s; /* file descriptor to be transfered */ msg.msg_iov = iov; msg.msg_iovlen = 1; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_control = (caddr_t)&cm; msg.msg_controllen = sizeof(struct cmessage); msg.msg_flags = 0; if ((len = sendmsg(s, &msg, 0)) == -1) err(1, "sendmsg failed"); printf ("sent message (%d bytes), sending data...\n", len); close(s); exit(0); } To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message