1 /* Copyright (C) 2011 Free Software Foundation, Inc.
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <sys/types.h>
29 #include <sys/socket.h>
33 #ifndef MSG_CMSG_CLOEXEC
34 # define MSG_CMSG_CLOEXEC 0
38 /* sendfd sends the file descriptor fd along the socket
39 to a process calling recvfd on the other end.
41 Return 0 on success, or -1 with errno set in case of error.
44 sendfd (int sock, int fd)
51 char buf[CMSG_SPACE (sizeof fd)];
54 /* send at least one char */
55 memset (&msg, 0, sizeof msg);
64 msg.msg_control = buf;
65 msg.msg_controllen = sizeof buf;
66 cmsg = CMSG_FIRSTHDR (&msg);
67 cmsg->cmsg_level = SOL_SOCKET;
68 cmsg->cmsg_type = SCM_RIGHTS;
69 cmsg->cmsg_len = CMSG_LEN (sizeof fd);
70 /* Initialize the payload: */
71 memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
72 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
73 msg.msg_accrights = &fd;
74 msg.msg_accrightslen = sizeof fd;
80 if (sendmsg (sock, &msg, 0) != iov.iov_len)
86 sendfd (int sock _GL_UNUSED, int fd _GL_UNUSED)
95 /* recvfd receives a file descriptor through the socket.
96 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
98 Return 0 on success, or -1 with errno set in case of error.
101 recvfd (int sock, int flags)
107 # ifdef CMSG_FIRSTHDR
108 struct cmsghdr *cmsg;
109 char buf[CMSG_SPACE (sizeof fd)];
110 int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
113 if ((flags & ~O_CLOEXEC) != 0)
119 /* send at least one char */
120 memset (&msg, 0, sizeof msg);
121 iov.iov_base = &byte;
128 # ifdef CMSG_FIRSTHDR
129 msg.msg_control = buf;
130 msg.msg_controllen = sizeof buf;
131 cmsg = CMSG_FIRSTHDR (&msg);
132 cmsg->cmsg_level = SOL_SOCKET;
133 cmsg->cmsg_type = SCM_RIGHTS;
134 cmsg->cmsg_len = CMSG_LEN (sizeof fd);
135 /* Initialize the payload: */
136 memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
137 msg.msg_controllen = cmsg->cmsg_len;
139 if (recvmsg (sock, &msg, flags_recvmsg) < 0)
142 cmsg = CMSG_FIRSTHDR (&msg);
144 if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof fd)
145 || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
147 /* fake errno: at end the file is not available */
152 memcpy (&fd, CMSG_DATA (cmsg), sizeof fd);
154 /* set close-on-exec flag */
155 if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
157 if (set_cloexec_flag (fd, true) < 0)
159 int saved_errno = errno;
166 # elif HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
167 msg.msg_accrights = &fd;
168 msg.msg_accrightslen = sizeof fd;
169 if (recvmsg (sock, &msg, 0) < 0)
172 /* set close-on-exec flag */
173 if (flags & O_CLOEXEC)
175 if (set_cloexec_flag (fd, true) < 0)
177 int saved_errno = errno;
191 recvfd (int sock _GL_UNUSED, int flags _GL_UNUSED)