3 * $Id: fdpass.c,v 1.1 2003/11/29 11:58:49 mdw Exp $
5 * File descriptor passing
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2003/11/29 11:58:49 mdw
34 * File descriptor passing.
38 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
48 #include <sys/socket.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @fdpass_send@ --- *
58 * Arguments: @int sock@ = socket to send over
59 * @int fd@ = file descriptor to send
60 * @const void *p@ = pointer to data to send
61 * @size_t sz@ = size of buffer to send
63 * Returns: On error, @-1@, otherwise number of bytes transferred from
66 * Use: Sends a copy of file descriptor @fd@ to the other end of
70 ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz)
74 #ifndef HAVE_MSG_ACCRIGHTS
75 char buf[CMSG_SPACE(sizeof(fd))];
79 iov.iov_base = (/*unconst*/ void *)p;
86 #ifdef HAVE_MSG_ACCRIGHTS
87 msg.msg_accrights = &fd;
88 msg.msg_accrightslen = sizeof(fd);
90 msg.msg_control = buf;
91 msg.msg_controllen = sizeof(buf);
92 cmsg = CMSG_FIRSTHDR(&msg);
93 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
94 cmsg->cmsg_level = SOL_SOCKET;
95 cmsg->cmsg_type = SCM_RIGHTS;
96 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
98 return (sendmsg(sock, &msg, 0));
101 /* --- @fdpass_recv@ --- *
103 * Arguments: @int sock@ = socket to send over
104 * @int *fd@ = where to put received descriptor
105 * @void *p@ = pointer to where to put data
106 * @size_t sz@ = size of buffer
108 * Returns: On error, @-1@, otherwise number of bytes transferred.
110 * Use: Receives a file descriptor. If the call succeeds, and there
111 * was a file descriptor, then @fd@ won't be @-1@ on exit;
112 * otherwise it will. At most one descriptor will be collected.
115 ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz)
120 #ifndef HAVE_MSG_ACCRIGHTS
121 char buf[CMSG_SPACE(sizeof(fd))];
122 struct cmsghdr *cmsg;
134 #ifdef HAVE_MSG_ACCRIGHTS
135 msg.msg_accrights = fd;
136 msg.msg_accrightslen = sizeof(*fd);
138 msg.msg_control = buf;
139 msg.msg_controllen = sizeof(buf);
141 if ((rc = recvmsg(sock, &msg, 0)) < 0)
143 #ifdef HAVE_MSG_ACCRIGHTS
144 if (msg.msg_accrightslen < sizeof(*fd))
147 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
148 if (cmsg->cmsg_level == SOL_SOCKET &&
149 cmsg->cmsg_type == SCM_RIGHTS &&
150 cmsg->cmsg_len >= CMSG_LEN(sizeof(fd))) {
151 memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp));
162 /*----- That's all, folks -------------------------------------------------*/