3 * File descriptor passing
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
40 #include <sys/socket.h>
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @fdpass_send@ --- *
50 * Arguments: @int sock@ = socket to send over
51 * @int fd@ = file descriptor to send
52 * @const void *p@ = pointer to data to send
53 * @size_t sz@ = size of buffer to send
55 * Returns: On error, @-1@, otherwise number of bytes transferred from
58 * Use: Sends a copy of file descriptor @fd@ to the other end of
62 ssize_t fdpass_send(int sock, int fd, const void *p, size_t sz)
66 #ifndef HAVE_MSG_ACCRIGHTS
67 char buf[CMSG_SPACE(sizeof(fd))];
71 iov.iov_base = (/*unconst*/ void *)p;
77 #ifdef HAVE_MSG_ACCRIGHTS
78 msg.msg_accrights = &fd;
79 msg.msg_accrightslen = sizeof(fd);
82 msg.msg_control = buf;
83 msg.msg_controllen = sizeof(buf);
84 cmsg = CMSG_FIRSTHDR(&msg);
85 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
86 cmsg->cmsg_level = SOL_SOCKET;
87 cmsg->cmsg_type = SCM_RIGHTS;
88 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
90 return (sendmsg(sock, &msg, 0));
93 /* --- @fdpass_recv@ --- *
95 * Arguments: @int sock@ = socket to send over
96 * @int *fd@ = where to put received descriptor
97 * @void *p@ = pointer to where to put data
98 * @size_t sz@ = size of buffer
100 * Returns: On error, @-1@, otherwise number of bytes transferred.
102 * Use: Receives a file descriptor. If the call succeeds, and there
103 * was a file descriptor, then @fd@ won't be @-1@ on exit;
104 * otherwise it will. At most one descriptor will be collected.
107 ssize_t fdpass_recv(int sock, int *fd, void *p, size_t sz)
112 #ifndef HAVE_MSG_ACCRIGHTS
113 char buf[CMSG_SPACE(sizeof(fd))];
114 struct cmsghdr *cmsg;
125 #ifdef HAVE_MSG_ACCRIGHTS
126 msg.msg_accrights = fd;
127 msg.msg_accrightslen = sizeof(*fd);
130 msg.msg_control = buf;
131 msg.msg_controllen = sizeof(buf);
133 if ((rc = recvmsg(sock, &msg, 0)) < 0)
135 #ifdef HAVE_MSG_ACCRIGHTS
136 if (msg.msg_accrightslen < sizeof(*fd))
139 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
140 if (cmsg->cmsg_level == SOL_SOCKET &&
141 cmsg->cmsg_type == SCM_RIGHTS &&
142 cmsg->cmsg_len >= CMSG_LEN(sizeof(fd))) {
143 memcpy(&fdtmp, CMSG_DATA(cmsg), sizeof(fdtmp));
154 /*----- That's all, folks -------------------------------------------------*/