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>
36 /* sendfd sends the file descriptor fd along the socket
37 to a process calling recvfd on the other end.
39 Return 0 on success, or -1 with errno set in case of error.
42 sendfd (int sock, int fd)
48 /* send at least one char */
49 iov[0].iov_base = &send;
57 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
59 char buf[CMSG_SPACE (sizeof (fd))];
61 msg.msg_control = buf;
62 msg.msg_controllen = sizeof (buf);
63 cmsg = CMSG_FIRSTHDR (&msg);
64 cmsg->cmsg_level = SOL_SOCKET;
65 cmsg->cmsg_type = SCM_RIGHTS;
66 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
67 /* Initialize the payload: */
68 memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
69 msg.msg_controllen = cmsg->cmsg_len;
70 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
71 msg.msg_accrights = &fd;
72 msg.msg_accrightslen = sizeof (fd);
79 if (sendmsg (sock, &msg, 0) != iov[0].iov_len)
84 /* recvfd receives a file descriptor through the socket.
85 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
87 Return 0 on success, or -1 with errno set in case of error.
90 recvfd (int sock, int flags)
96 if ((flags & ~O_CLOEXEC) != 0)
102 /* send at least one char */
103 iov[0].iov_base = &recv;
111 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
113 struct cmsghdr *cmsg;
114 char buf[CMSG_SPACE (sizeof (fd))];
116 # if HAVE_MSG_CMSG_CLOEXEC
117 int flags_recvmsg = (flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0);
119 int flags_recvmsg = 0;
122 msg.msg_control = buf;
123 msg.msg_controllen = sizeof (buf);
124 cmsg = CMSG_FIRSTHDR (&msg);
125 cmsg->cmsg_level = SOL_SOCKET;
126 cmsg->cmsg_type = SCM_RIGHTS;
127 cmsg->cmsg_len = CMSG_LEN (sizeof (int));
128 /* Initialize the payload: */
129 memcpy (CMSG_DATA (cmsg), &mone, sizeof (mone));
130 msg.msg_controllen = cmsg->cmsg_len;
132 if (recvmsg (sock, &msg, flags_recvmsg) < 0)
135 cmsg = CMSG_FIRSTHDR (&msg);
137 if (cmsg == NULL || cmsg->cmsg_len != CMSG_LEN (sizeof (int))
138 || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
140 /* fake errno: at end the file is not available */
145 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
147 # if !HAVE_MSG_CMSG_CLOEXEC
148 /* set close-on-exec flag */
149 if (flags & O_CLOEXEC)
151 if (set_cloexec_flag (fd, true) < 0)
153 int saved_errno = errno;
163 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
166 msg.msg_accrights = &fd;
167 msg.msg_accrightslen = sizeof (fd);
168 if (recvmsg (sock, &msg, 0) < 0)
171 /* set close-on-exec flag */
172 if (flags & O_CLOEXEC)
174 if (set_cloexec_flag (fd, true) < 0)
176 int saved_errno = errno;