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>
30 #include <sys/socket.h>
37 #ifndef MSG_CMSG_CLOEXEC
38 # define MSG_CMSG_CLOEXEC 0
41 /* sendfd sends the file descriptor fd along the socket
42 to a process calling recvfd on the other end.
44 Return 0 on success, or -1 with errno set in case of error.
47 sendfd (int sock, int fd)
52 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
54 char buf[CMSG_SPACE (sizeof fd)];
57 /* send at least one char */
58 memset (&msg, 0, sizeof msg);
66 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
67 msg.msg_control = buf;
68 msg.msg_controllen = sizeof buf;
69 cmsg = CMSG_FIRSTHDR (&msg);
70 cmsg->cmsg_level = SOL_SOCKET;
71 cmsg->cmsg_type = SCM_RIGHTS;
72 cmsg->cmsg_len = CMSG_LEN (sizeof fd);
73 /* Initialize the payload: */
74 memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
75 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
76 msg.msg_accrights = &fd;
77 msg.msg_accrightslen = sizeof fd;
83 if (sendmsg (sock, &msg, 0) != iov.iov_len)
88 /* recvfd receives a file descriptor through the socket.
89 The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>).
91 Return 0 on success, or -1 with errno set in case of error.
94 recvfd (int sock, int flags)
100 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
101 struct cmsghdr *cmsg;
102 char buf[CMSG_SPACE (sizeof fd)];
103 int flags_recvmsg = flags & O_CLOEXEC ? MSG_CMSG_CLOEXEC : 0;
106 if ((flags & ~O_CLOEXEC) != 0)
112 /* send at least one char */
113 memset (&msg, 0, sizeof msg);
114 iov.iov_base = &recv;
121 #if HAVE_UNIXSOCKET_SCM_RIGHTS_BSD44_WAY
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 fd);
128 /* Initialize the payload: */
129 memcpy (CMSG_DATA (cmsg), &fd, sizeof fd);
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 fd)
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 /* set close-on-exec flag */
148 if (!MSG_CMSG_CLOEXEC && (flags & O_CLOEXEC))
150 if (set_cloexec_flag (fd, true) < 0)
152 int saved_errno = errno;
159 #elif HAVE_UNIXSOCKET_SCM_RIGHTS_BSD43_WAY
160 msg.msg_accrights = &fd;
161 msg.msg_accrightslen = sizeof fd;
162 if (recvmsg (sock, &msg, 0) < 0)
165 /* set close-on-exec flag */
166 if (flags & O_CLOEXEC)
168 if (set_cloexec_flag (fd, true) < 0)
170 int saved_errno = errno;