~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to src/common/fd/fd_socket_transmission.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-10-10 14:01:26 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20141010140126-n1czko8na1kuz4ll
Tags: upstream-0.8.0+14.10.20141010
Import upstream version 0.8.0+14.10.20141010

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/fd_socket_transmission.h"
 
20
#include "mir/variable_length_array.h"
 
21
#include <sys/types.h>
 
22
#include <sys/socket.h>
 
23
#include <string.h>
 
24
#include <boost/throw_exception.hpp>
 
25
#include <boost/exception/errinfo_errno.hpp>
 
26
#include <stdexcept>
 
27
 
 
28
mir::socket_error::socket_error(std::string const& message) :
 
29
    std::system_error(errno, std::system_category(), message)
 
30
{
 
31
}
 
32
 
 
33
mir::socket_disconnected_error::socket_disconnected_error(std::string const& message) :
 
34
    std::system_error(errno, std::system_category(), message)
 
35
{
 
36
}
 
37
 
 
38
mir::fd_reception_error::fd_reception_error() :
 
39
    std::runtime_error("Invalid control message for receiving file descriptors")
 
40
{
 
41
}
 
42
 
 
43
void mir::send_fds(
 
44
    mir::Fd const& socket,
 
45
    std::vector<mir::Fd> const& fds)
 
46
{
 
47
    if (fds.size() > 0)
 
48
    {
 
49
        // We send dummy data
 
50
        struct iovec iov;
 
51
        char dummy_iov_data = 'M';
 
52
        iov.iov_base = &dummy_iov_data;
 
53
        iov.iov_len = 1;
 
54
 
 
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());
 
62
 
 
63
        // Message to send
 
64
        struct msghdr header;
 
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();
 
71
        header.msg_flags = 0;
 
72
 
 
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;
 
78
 
 
79
        int* const data = reinterpret_cast<int*>(CMSG_DATA(message));
 
80
        int i = 0;
 
81
        for (auto& fd : fds)
 
82
            data[i++] = fd;
 
83
 
 
84
        auto const sent = sendmsg(socket, &header, 0);
 
85
        if (sent < 0)
 
86
            BOOST_THROW_EXCEPTION(std::runtime_error("Failed to send fds: " + std::string(strerror(errno))));
 
87
    }
 
88
}
 
89
 
 
90
bool mir::socket_error_is_transient(int error_code)
 
91
{
 
92
    return (error_code == EINTR);
 
93
}
 
94
 
 
95
void mir::receive_data(mir::Fd const& socket, void* buffer, size_t bytes_requested, std::vector<mir::Fd>& fds)
 
96
{
 
97
    if (bytes_requested == 0)
 
98
        BOOST_THROW_EXCEPTION(std::logic_error("Attempted to receive 0 bytes"));
 
99
 
 
100
    size_t bytes_read{0};
 
101
    unsigned fds_read{0};
 
102
    while (bytes_read < bytes_requested)
 
103
    {
 
104
        // Store the data in the buffer requested
 
105
        struct iovec iov;
 
106
        iov.iov_base = static_cast<uint8_t*>(buffer) + bytes_read;
 
107
        iov.iov_len = bytes_requested - bytes_read;
 
108
        
 
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)};
 
114
        
 
115
        // Message to read
 
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;
 
124
        
 
125
        ssize_t const result = recvmsg(socket, &header, MSG_NOSIGNAL | MSG_WAITALL);
 
126
        if (result == 0)
 
127
            BOOST_THROW_EXCEPTION(socket_disconnected_error("Failed to read message from server: server has shutdown"));
 
128
        if (result < 0)
 
129
        {
 
130
            if (socket_error_is_transient(errno))
 
131
                continue;
 
132
            if (errno == EPIPE)
 
133
                BOOST_THROW_EXCEPTION(
 
134
                    boost::enable_error_info(socket_disconnected_error("Failed to read message from server"))
 
135
                << boost::errinfo_errno(errno));
 
136
 
 
137
            BOOST_THROW_EXCEPTION(
 
138
                        boost::enable_error_info(socket_error("Failed to read message from server"))
 
139
                        << boost::errinfo_errno(errno));
 
140
        }
 
141
 
 
142
        bytes_read += result;
 
143
        
 
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);
 
147
        if (cmsg)
 
148
        {
 
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))
 
152
            {
 
153
                BOOST_THROW_EXCEPTION(std::runtime_error("Received more fds than expected"));
 
154
            }
 
155
            if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
 
156
            {
 
157
                BOOST_THROW_EXCEPTION(fd_reception_error());
 
158
            }
 
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);
 
162
 
 
163
            // We can't properly pass mir::Fds through google::protobuf::Message,
 
164
            // which is where these get shoved.
 
165
            //
 
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]}};
 
170
 
 
171
            fds_read += nfds;
 
172
        }
 
173
    }
 
174
 
 
175
    if (fds_read < fds.size())
 
176
        BOOST_THROW_EXCEPTION(std::runtime_error("Receieved fewer fds than expected"));
 
177
}