File: dispatcher.cpp

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (325 lines) | stat: -rw-r--r-- 9,314 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
   Copyright (C) 2009-2016 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>

#include <cassert>
#include <cerrno>
#include <cstring>

#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#ifndef _WIN32
#include <poll.h>
#endif

#include "dispatcher.h"

#define DISPATCHER_MESSAGE_TYPE_CUSTOM 0x7fffffffu

/* structure to store message header information.
 * That structure is sent through a socketpair so it's optimized
 * to be transfered via sockets.
 * Is also packaged to not leave holes in both 32 and 64 environments
 * so memory instrumentation tools should not find uninitialised bytes.
 */
struct DispatcherMessage {
    dispatcher_handle_message handler;
    uint32_t size;
    uint32_t type:31;
    uint32_t ack:1;
};

struct DispatcherPrivate {
    SPICE_CXX_GLIB_ALLOCATOR
    explicit DispatcherPrivate(uint32_t init_max_message_type):
        max_message_type(init_max_message_type)
    {
    }
    ~DispatcherPrivate();
    void send_message(const DispatcherMessage& msg, void *payload);
    bool handle_single_read();
    static void handle_event(int fd, int event, DispatcherPrivate* priv);

    int recv_fd;
    int send_fd;
    pthread_mutex_t lock;
    DispatcherMessage *messages;
    const guint max_message_type;
    void *payload; /* allocated as max of message sizes */
    size_t payload_size; /* used to track realloc calls */
    void *opaque;
    dispatcher_handle_any_message any_handler;
};

DispatcherPrivate::~DispatcherPrivate()
{
    while (handle_single_read()) {
        continue;
    }
    g_free(messages);
    socket_close(send_fd);
    socket_close(recv_fd);
    pthread_mutex_destroy(&lock);
    g_free(payload);
}

Dispatcher::~Dispatcher() = default;

Dispatcher::Dispatcher(uint32_t max_message_type):
    priv(new DispatcherPrivate(max_message_type))
{
    int channels[2];

    if (socketpair(AF_LOCAL, SOCK_STREAM, 0, channels) == -1) {
        spice_error("socketpair failed %s", strerror(errno));
        return;
    }
    pthread_mutex_init(&priv->lock, nullptr);
    priv->recv_fd = channels[0];
    priv->send_fd = channels[1];

    priv->messages = g_new0(DispatcherMessage, priv->max_message_type);
}

#define ACK 0xffffffff

/*
 * read_safe
 * helper. reads until size bytes accumulated in buf, if an error other then
 * EINTR is encountered returns -1, otherwise returns 0.
 * @block if true the read will block (the fd is always blocking).
 *        if false poll first, return immediately if no bytes available, otherwise
 *         read size in blocking mode.
 */
static int read_safe(int fd, void *raw_buf, size_t size, bool block)
{
    int read_size = 0;
    int ret;
    auto buf = reinterpret_cast<uint8_t *>(raw_buf);

    if (size == 0) {
        return 0;
    }

    if (!block) {
#ifndef _WIN32
        struct pollfd pollfd = {.fd = fd, .events = POLLIN, .revents = 0};
        while ((ret = poll(&pollfd, 1, 0)) == -1) {
            if (errno == EINTR) {
                spice_debug("EINTR in poll");
                continue;
            }
            spice_error("poll failed");
            return -1;
        }
        if (!(pollfd.revents & POLLIN)) {
            return 0;
        }
#else
        struct timeval tv = { 0, 0 };
        fd_set fds;
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        if (select(1, &fds, NULL, NULL, &tv) < 1) {
            return 0;
        }
#endif
    }
    while (read_size < size) {
        ret = socket_read(fd, buf + read_size, size - read_size);
        if (ret == -1) {
            if (errno == EINTR) {
                spice_debug("EINTR in read");
                continue;
            }
#ifdef _WIN32
            // Windows turns this socket not-blocking
            if (errno == EAGAIN) {
                fd_set fds;
                FD_ZERO(&fds);
                FD_SET(fd, &fds);
                select(1, &fds, NULL, NULL, NULL);
                continue;
            }
#endif
            return -1;
        }
        if (ret == 0) {
            spice_error("broken pipe on read");
            return -1;
        }
        read_size += ret;
    }
    return read_size;
}

/*
 * write_safe
 * @return -1 for error, otherwise number of written bytes. may be zero.
 */
static int write_safe(int fd, const void *raw_buf, size_t size)
{
    int written_size = 0;
    int ret;
    auto buf = reinterpret_cast<const uint8_t *>(raw_buf);

    while (written_size < size) {
        ret = socket_write(fd, buf + written_size, size - written_size);
        if (ret == -1) {
            if (errno != EINTR) {
                return -1;
            }
            spice_debug("EINTR in write");
            continue;
        }
        written_size += ret;
    }
    return written_size;
}

bool DispatcherPrivate::handle_single_read()
{
    int ret;
    DispatcherMessage msg[1];
    uint32_t ack = ACK;

    if ((ret = read_safe(recv_fd, msg, sizeof(msg), false)) == -1) {
        g_warning("error reading from dispatcher: %d", errno);
        return false;
    }
    if (ret == 0) {
        /* no message */
        return false;
    }
    if (G_UNLIKELY(msg->size > payload_size)) {
        payload = g_realloc(payload, msg->size);
        payload_size = msg->size;
    }
    if (read_safe(recv_fd, payload, msg->size, true) == -1) {
        g_warning("error reading from dispatcher: %d", errno);
        /* TODO: close socketpair? */
        return false;
    }
    if (any_handler && msg->type != DISPATCHER_MESSAGE_TYPE_CUSTOM) {
        any_handler(opaque, msg->type, payload);
    }
    if (msg->handler) {
        msg->handler(opaque, payload);
    } else {
        g_warning("error: no handler for message type %d", msg->type);
    }
    if (msg->ack) {
        if (write_safe(recv_fd, &ack, sizeof(ack)) == -1) {
            g_warning("error writing ack for message %d", msg->type);
            /* TODO: close socketpair? */
        }
    }
    return true;
}

/*
 * handle_event
 * doesn't handle being in the middle of a message. all reads are blocking.
 */
void DispatcherPrivate::handle_event(int fd, int event, DispatcherPrivate* priv)
{
    while (priv->handle_single_read()) {
    }
}

void DispatcherPrivate::send_message(const DispatcherMessage& msg, void *msg_payload)
{
    uint32_t ack;

    pthread_mutex_lock(&lock);
    if (write_safe(send_fd, &msg, sizeof(msg)) == -1) {
        g_warning("error: failed to send message header for message %d",
                  msg.type);
        goto unlock;
    }
    if (write_safe(send_fd, msg_payload, msg.size) == -1) {
        g_warning("error: failed to send message body for message %d",
                  msg.type);
        goto unlock;
    }
    if (msg.ack) {
        if (read_safe(send_fd, &ack, sizeof(ack), true) == -1) {
            g_warning("error: failed to read ack");
        } else if (ack != ACK) {
            g_warning("error: got wrong ack value in dispatcher "
                      "for message %d\n", msg.type);
            /* TODO handling error? */
        }
    }
unlock:
    pthread_mutex_unlock(&lock);
}

void Dispatcher::send_message(uint32_t message_type, void *payload)
{
    assert(priv->max_message_type > message_type);
    assert(priv->messages[message_type].handler);
    priv->send_message(priv->messages[message_type], payload);
}

void Dispatcher::send_message_custom(dispatcher_handle_message handler,
                                     void *payload, uint32_t payload_size, bool ack)
{
    DispatcherMessage msg = {
        .handler = handler,
        .size = payload_size,
        .type = DISPATCHER_MESSAGE_TYPE_CUSTOM,
        .ack = ack,
    };
    priv->send_message(msg, payload);
}

void Dispatcher::register_handler(uint32_t message_type,
                                  dispatcher_handle_message handler,
                                  size_t size, bool ack)
{
    DispatcherMessage *msg;

    assert(message_type < priv->max_message_type);
    assert(priv->messages[message_type].handler == nullptr);
    msg = &priv->messages[message_type];
    msg->handler = handler;
    msg->size = size;
    msg->type = message_type;
    msg->ack = ack;
    if (msg->size > priv->payload_size) {
        priv->payload = g_realloc(priv->payload, msg->size);
        priv->payload_size = msg->size;
    }
}

void Dispatcher::register_universal_handler(dispatcher_handle_any_message any_handler)
{
    priv->any_handler = any_handler;
}

SpiceWatch *Dispatcher::create_watch(SpiceCoreInterfaceInternal *core)
{
    return core->watch_new(priv->recv_fd,
                           SPICE_WATCH_EVENT_READ, DispatcherPrivate::handle_event, priv.get());
}

void Dispatcher::set_opaque(void *opaque)
{
    priv->opaque = opaque;
}