2
* Copyright © 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
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 Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Kevin DuBois <kevin.dubois@canonical.com>
19
#include "mir/fd_socket_transmission.h"
20
#include "mir/variable_length_array.h"
21
#include <sys/types.h>
22
#include <sys/socket.h>
24
#include <boost/throw_exception.hpp>
25
#include <boost/exception/errinfo_errno.hpp>
28
mir::socket_error::socket_error(std::string const& message) :
29
std::system_error(errno, std::system_category(), message)
33
mir::socket_disconnected_error::socket_disconnected_error(std::string const& message) :
34
std::system_error(errno, std::system_category(), message)
38
mir::fd_reception_error::fd_reception_error() :
39
std::runtime_error("Invalid control message for receiving file descriptors")
44
mir::Fd const& socket,
45
std::vector<mir::Fd> const& fds)
51
char dummy_iov_data = 'M';
52
iov.iov_base = &dummy_iov_data;
55
// Allocate space for control message
56
static auto const builtin_n_fds = 5;
57
static auto const builtin_cmsg_space = CMSG_SPACE(builtin_n_fds * sizeof(int));
58
auto const fds_bytes = fds.size() * sizeof(int);
59
mir::VariableLengthArray<builtin_cmsg_space> control{CMSG_SPACE(fds_bytes)};
60
// Silence valgrind uninitialized memory complaint
61
memset(control.data(), 0, control.size());
65
header.msg_name = NULL;
66
header.msg_namelen = 0;
67
header.msg_iov = &iov;
68
header.msg_iovlen = 1;
69
header.msg_controllen = control.size();
70
header.msg_control = control.data();
73
// Control message contains file descriptors
74
struct cmsghdr *message = CMSG_FIRSTHDR(&header);
75
message->cmsg_len = CMSG_LEN(fds_bytes);
76
message->cmsg_level = SOL_SOCKET;
77
message->cmsg_type = SCM_RIGHTS;
79
int* const data = reinterpret_cast<int*>(CMSG_DATA(message));
84
auto const sent = sendmsg(socket, &header, 0);
86
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to send fds: " + std::string(strerror(errno))));
90
bool mir::socket_error_is_transient(int error_code)
92
return (error_code == EINTR);
95
void mir::receive_data(mir::Fd const& socket, void* buffer, size_t bytes_requested, std::vector<mir::Fd>& fds)
97
if (bytes_requested == 0)
98
BOOST_THROW_EXCEPTION(std::logic_error("Attempted to receive 0 bytes"));
100
size_t bytes_read{0};
101
unsigned fds_read{0};
102
while (bytes_read < bytes_requested)
104
// Store the data in the buffer requested
106
iov.iov_base = static_cast<uint8_t*>(buffer) + bytes_read;
107
iov.iov_len = bytes_requested - bytes_read;
109
// Allocate space for control message
110
static auto const builtin_n_fds = 5;
111
static auto const builtin_cmsg_space = CMSG_SPACE(builtin_n_fds * sizeof(int));
112
auto const fds_bytes = (fds.size() - fds_read) * sizeof(int);
113
mir::VariableLengthArray<builtin_cmsg_space> control{CMSG_SPACE(fds_bytes)};
116
struct msghdr header;
117
header.msg_name = NULL;
118
header.msg_namelen = 0;
119
header.msg_iov = &iov;
120
header.msg_iovlen = 1;
121
header.msg_controllen = control.size();
122
header.msg_control = control.data();
123
header.msg_flags = 0;
125
ssize_t const result = recvmsg(socket, &header, MSG_NOSIGNAL | MSG_WAITALL);
127
BOOST_THROW_EXCEPTION(socket_disconnected_error("Failed to read message from server: server has shutdown"));
130
if (socket_error_is_transient(errno))
133
BOOST_THROW_EXCEPTION(
134
boost::enable_error_info(socket_disconnected_error("Failed to read message from server"))
135
<< boost::errinfo_errno(errno));
137
BOOST_THROW_EXCEPTION(
138
boost::enable_error_info(socket_error("Failed to read message from server"))
139
<< boost::errinfo_errno(errno));
142
bytes_read += result;
144
// If we get a proper control message, copy the received
145
// file descriptors back to the caller
146
struct cmsghdr const* const cmsg = CMSG_FIRSTHDR(&header);
149
// NOTE: This relies on the file descriptor cmsg being read
150
// (and written) atomically.
151
if (cmsg->cmsg_len > CMSG_LEN(fds_bytes) || (header.msg_flags & MSG_CTRUNC))
153
BOOST_THROW_EXCEPTION(std::runtime_error("Received more fds than expected"));
155
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
157
BOOST_THROW_EXCEPTION(fd_reception_error());
159
int const* const data = reinterpret_cast<int const*>CMSG_DATA(cmsg);
160
ptrdiff_t const header_size = reinterpret_cast<char const*>(data) - reinterpret_cast<char const*>(cmsg);
161
int const nfds = (cmsg->cmsg_len - header_size) / sizeof(int);
163
// We can't properly pass mir::Fds through google::protobuf::Message,
164
// which is where these get shoved.
166
// When we have our own RPC generator plugin and aren't using deprecated
167
// Protobuf features this can go away.
168
for (int i = 0; i < nfds; i++)
169
fds[fds_read + i] = mir::Fd{mir::IntOwnedFd{data[i]}};
175
if (fds_read < fds.size())
176
BOOST_THROW_EXCEPTION(std::runtime_error("Receieved fewer fds than expected"));