Fix: forbid session name creation if contains /
[lttng-tools.git] / src / common / relayd / relayd.c
CommitLineData
00e2e675
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#define _GNU_SOURCE
19#include <assert.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
77c7c900 24#include <inttypes.h>
00e2e675
DG
25
26#include <common/common.h>
27#include <common/defaults.h>
28#include <common/sessiond-comm/relayd.h>
29
30#include "relayd.h"
31
32/*
33 * Send command. Fill up the header and append the data.
34 */
6151a90f 35static int send_command(struct lttcomm_relayd_sock *rsock,
7c9534d6 36 enum lttcomm_relayd_command cmd, void *data, size_t size,
00e2e675
DG
37 int flags)
38{
39 int ret;
40 struct lttcomm_relayd_hdr header;
41 char *buf;
42 uint64_t buf_size = sizeof(header);
43
f96e4545
MD
44 if (rsock->sock.fd < 0) {
45 return -ECONNRESET;
46 }
47
00e2e675
DG
48 if (data) {
49 buf_size += size;
50 }
51
52 buf = zmalloc(buf_size);
53 if (buf == NULL) {
54 PERROR("zmalloc relayd send command buf");
55 ret = -1;
56 goto alloc_error;
57 }
58
59 header.cmd = htobe32(cmd);
60 header.data_size = htobe64(size);
61
62 /* Zeroed for now since not used. */
63 header.cmd_version = 0;
64 header.circuit_id = 0;
65
66 /* Prepare buffer to send. */
67 memcpy(buf, &header, sizeof(header));
68 if (data) {
69 memcpy(buf + sizeof(header), data, size);
70 }
71
6151a90f 72 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
00e2e675 73 if (ret < 0) {
8994307f 74 ret = -errno;
00e2e675
DG
75 goto error;
76 }
77
633d0084 78 DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size);
00e2e675
DG
79
80error:
81 free(buf);
82alloc_error:
83 return ret;
84}
85
86/*
87 * Receive reply data on socket. This MUST be call after send_command or else
88 * could result in unexpected behavior(s).
89 */
6151a90f 90static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
00e2e675
DG
91{
92 int ret;
93
f96e4545
MD
94 if (rsock->sock.fd < 0) {
95 return -ECONNRESET;
96 }
97
8fd623e0 98 DBG3("Relayd waiting for reply of size %zu", size);
00e2e675 99
6151a90f 100 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
20275fe8
DG
101 if (ret <= 0 || ret != size) {
102 if (ret == 0) {
103 /* Orderly shutdown. */
6151a90f 104 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
20275fe8 105 } else {
8fd623e0 106 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
6151a90f 107 rsock->sock.fd, size, ret);
20275fe8
DG
108 }
109 /* Always return -1 here and the caller can use errno. */
110 ret = -1;
00e2e675
DG
111 goto error;
112 }
113
114error:
115 return ret;
116}
117
c5b6f4f0
DG
118/*
119 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
120 * set session_id of the relayd if we have a successful reply from the relayd.
121 *
20275fe8
DG
122 * On success, return 0 else a negative value which is either an errno error or
123 * a lttng error code from the relayd.
c5b6f4f0 124 */
6151a90f 125int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id)
c5b6f4f0
DG
126{
127 int ret;
128 struct lttcomm_relayd_status_session reply;
129
6151a90f 130 assert(rsock);
c5b6f4f0
DG
131 assert(session_id);
132
133 DBG("Relayd create session");
134
135 /* Send command */
6151a90f 136 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
c5b6f4f0
DG
137 if (ret < 0) {
138 goto error;
139 }
140
20275fe8 141 /* Receive response */
6151a90f 142 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c5b6f4f0
DG
143 if (ret < 0) {
144 goto error;
145 }
146
147 reply.session_id = be64toh(reply.session_id);
148 reply.ret_code = be32toh(reply.ret_code);
149
150 /* Return session id or negative ret code. */
151 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
152 ret = -1;
153 ERR("Relayd create session replied error %d", reply.ret_code);
c5b6f4f0
DG
154 goto error;
155 } else {
156 ret = 0;
157 *session_id = reply.session_id;
158 }
159
160 DBG("Relayd session created with id %" PRIu64, reply.session_id);
161
162error:
163 return ret;
164}
165
00e2e675
DG
166/*
167 * Add stream on the relayd and assign stream handle to the stream_id argument.
168 *
169 * On success return 0 else return ret_code negative value.
170 */
6151a90f 171int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
0f907de1
JD
172 const char *pathname, uint64_t *stream_id,
173 uint64_t tracefile_size, uint64_t tracefile_count)
00e2e675
DG
174{
175 int ret;
176 struct lttcomm_relayd_add_stream msg;
0f907de1 177 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
00e2e675
DG
178 struct lttcomm_relayd_status_stream reply;
179
180 /* Code flow error. Safety net. */
6151a90f 181 assert(rsock);
00e2e675
DG
182 assert(channel_name);
183 assert(pathname);
184
185 DBG("Relayd adding stream for channel name %s", channel_name);
186
0f907de1
JD
187 /* Compat with relayd 2.1 */
188 if (rsock->minor == 1) {
189 strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name));
190 strncpy(msg.pathname, pathname, sizeof(msg.pathname));
00e2e675 191
0f907de1
JD
192 /* Send command */
193 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
194 if (ret < 0) {
195 goto error;
196 }
197 } else {
198 /* Compat with relayd 2.2+ */
199 strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name));
200 strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname));
201 msg_2_2.tracefile_size = htobe64(tracefile_size);
202 msg_2_2.tracefile_count = htobe64(tracefile_count);
203
204 /* Send command */
205 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
206 if (ret < 0) {
207 goto error;
208 }
00e2e675
DG
209 }
210
633d0084 211 /* Waiting for reply */
6151a90f 212 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
213 if (ret < 0) {
214 goto error;
215 }
216
217 /* Back to host bytes order. */
218 reply.handle = be64toh(reply.handle);
219 reply.ret_code = be32toh(reply.ret_code);
220
221 /* Return session id or negative ret code. */
f73fabfd 222 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
223 ret = -1;
224 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
225 } else {
226 /* Success */
227 ret = 0;
228 *stream_id = reply.handle;
229 }
230
77c7c900
MD
231 DBG("Relayd stream added successfully with handle %" PRIu64,
232 reply.handle);
00e2e675
DG
233
234error:
235 return ret;
236}
237
238/*
239 * Check version numbers on the relayd.
d4519fa3
JD
240 * If major versions are compatible, we assign minor_to_use to the
241 * minor version of the procotol we are going to use for this session.
00e2e675
DG
242 *
243 * Return 0 if compatible else negative value.
244 */
6151a90f 245int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
246{
247 int ret;
092b6259 248 struct lttcomm_relayd_version msg;
00e2e675
DG
249
250 /* Code flow error. Safety net. */
6151a90f 251 assert(rsock);
00e2e675 252
6151a90f
JD
253 DBG("Relayd version check for major.minor %u.%u", rsock->major,
254 rsock->minor);
00e2e675 255
092b6259 256 /* Prepare network byte order before transmission. */
6151a90f
JD
257 msg.major = htobe32(rsock->major);
258 msg.minor = htobe32(rsock->minor);
092b6259 259
00e2e675 260 /* Send command */
6151a90f 261 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
262 if (ret < 0) {
263 goto error;
264 }
265
20275fe8 266 /* Receive response */
6151a90f 267 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
268 if (ret < 0) {
269 goto error;
270 }
271
272 /* Set back to host bytes order */
092b6259
DG
273 msg.major = be32toh(msg.major);
274 msg.minor = be32toh(msg.minor);
275
276 /*
277 * Only validate the major version. If the other side is higher,
278 * communication is not possible. Only major version equal can talk to each
279 * other. If the minor version differs, the lowest version is used by both
280 * sides.
092b6259 281 */
6151a90f 282 if (msg.major != rsock->major) {
d4519fa3
JD
283 /* Not compatible */
284 ret = -1;
285 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 286 msg.major, rsock->major);
092b6259 287 goto error;
00e2e675
DG
288 }
289
092b6259 290 /*
6151a90f
JD
291 * If the relayd's minor version is higher, it will adapt to our version so
292 * we can continue to use the latest relayd communication data structure.
293 * If the received minor version is higher, the relayd should adapt to us.
092b6259 294 */
6151a90f
JD
295 if (rsock->minor > msg.minor) {
296 rsock->minor = msg.minor;
d4519fa3 297 }
092b6259 298
d4519fa3
JD
299 /* Version number compatible */
300 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 301 rsock->major, rsock->minor);
d4519fa3 302 ret = 0;
00e2e675
DG
303
304error:
305 return ret;
306}
307
00e2e675
DG
308/*
309 * Add stream on the relayd and assign stream handle to the stream_id argument.
310 *
311 * On success return 0 else return ret_code negative value.
312 */
6151a90f 313int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
314{
315 int ret;
316
317 /* Code flow error. Safety net. */
6151a90f 318 assert(rsock);
00e2e675 319
77c7c900 320 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
321
322 /* Send command */
6151a90f 323 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
324 if (ret < 0) {
325 goto error;
326 }
327
328 DBG2("Relayd metadata added successfully");
329
330 /*
331 * After that call, the metadata data MUST be sent to the relayd so the
332 * receive size on the other end matches the len of the metadata packet
633d0084 333 * header. This is why we don't wait for a reply here.
00e2e675
DG
334 */
335
336error:
337 return ret;
338}
339
340/*
6151a90f 341 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 342 */
6151a90f 343int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
344{
345 /* Code flow error. Safety net. */
6151a90f 346 assert(rsock);
00e2e675 347
f96e4545
MD
348 if (!rsock->sock.ops) {
349 /*
350 * Attempting a connect on a non-initialized socket.
351 */
352 return -ECONNRESET;
353 }
354
00e2e675
DG
355 DBG3("Relayd connect ...");
356
6151a90f 357 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
358}
359
360/*
6151a90f 361 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
362 *
363 * If no socket operations are found, simply return 0 meaning that everything
364 * is fine. Without operations, the socket can not possibly be opened or used.
365 * This is possible if the socket was allocated but not created. However, the
366 * caller could simply use it to store a valid file descriptor for instance
367 * passed over a Unix socket and call this to cleanup but still without a valid
368 * ops pointer.
369 *
370 * Return the close returned value. On error, a negative value is usually
371 * returned back from close(2).
00e2e675 372 */
6151a90f 373int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 374{
ffe60014
DG
375 int ret;
376
00e2e675 377 /* Code flow error. Safety net. */
6151a90f 378 assert(rsock);
00e2e675 379
ffe60014 380 /* An invalid fd is fine, return success. */
6151a90f 381 if (rsock->sock.fd < 0) {
ffe60014
DG
382 ret = 0;
383 goto end;
384 }
385
6151a90f 386 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 387
6151a90f
JD
388 if (rsock->sock.ops) {
389 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
390 } else {
391 /* Default call if no specific ops found. */
6151a90f 392 ret = close(rsock->sock.fd);
ffe60014
DG
393 if (ret < 0) {
394 PERROR("relayd_close default close");
395 }
396 }
f96e4545 397 rsock->sock.fd = -1;
ffe60014
DG
398
399end:
400 return ret;
00e2e675
DG
401}
402
403/*
404 * Send data header structure to the relayd.
405 */
6151a90f 406int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
407 struct lttcomm_relayd_data_hdr *hdr, size_t size)
408{
409 int ret;
410
411 /* Code flow error. Safety net. */
6151a90f 412 assert(rsock);
00e2e675
DG
413 assert(hdr);
414
f96e4545
MD
415 if (rsock->sock.fd < 0) {
416 return -ECONNRESET;
417 }
418
8fd623e0 419 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
420
421 /* Again, safety net */
422 if (size == 0) {
423 size = sizeof(struct lttcomm_relayd_data_hdr);
424 }
425
426 /* Only send data header. */
6151a90f 427 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 428 if (ret < 0) {
8994307f 429 ret = -errno;
00e2e675
DG
430 goto error;
431 }
432
433 /*
434 * The data MUST be sent right after that command for the receive on the
435 * other end to match the size in the header.
436 */
437
438error:
439 return ret;
440}
173af62f
DG
441
442/*
443 * Send close stream command to the relayd.
444 */
6151a90f 445int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
446 uint64_t last_net_seq_num)
447{
448 int ret;
449 struct lttcomm_relayd_close_stream msg;
450 struct lttcomm_relayd_generic_reply reply;
451
452 /* Code flow error. Safety net. */
6151a90f 453 assert(rsock);
173af62f 454
77c7c900 455 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f
DG
456
457 msg.stream_id = htobe64(stream_id);
458 msg.last_net_seq_num = htobe64(last_net_seq_num);
459
460 /* Send command */
6151a90f 461 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
462 if (ret < 0) {
463 goto error;
464 }
465
20275fe8 466 /* Receive response */
6151a90f 467 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
DG
468 if (ret < 0) {
469 goto error;
470 }
471
472 reply.ret_code = be32toh(reply.ret_code);
473
474 /* Return session id or negative ret code. */
f73fabfd 475 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
476 ret = -1;
477 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
478 } else {
479 /* Success */
480 ret = 0;
481 }
482
77c7c900 483 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
484
485error:
486 return ret;
487}
c8f59ee5
DG
488
489/*
490 * Check for data availability for a given stream id.
491 *
6d805429 492 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 493 */
6151a90f 494int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
495 uint64_t last_net_seq_num)
496{
497 int ret;
6d805429 498 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
499 struct lttcomm_relayd_generic_reply reply;
500
501 /* Code flow error. Safety net. */
6151a90f 502 assert(rsock);
c8f59ee5 503
6d805429 504 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5
DG
505
506 msg.stream_id = htobe64(stream_id);
507 msg.last_net_seq_num = htobe64(last_net_seq_num);
508
509 /* Send command */
6151a90f 510 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
511 sizeof(msg), 0);
512 if (ret < 0) {
513 goto error;
514 }
515
20275fe8 516 /* Receive response */
6151a90f 517 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
518 if (ret < 0) {
519 goto error;
520 }
521
522 reply.ret_code = be32toh(reply.ret_code);
523
524 /* Return session id or negative ret code. */
525 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 526 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
527 }
528
529 /* At this point, the ret code is either 1 or 0 */
530 ret = reply.ret_code;
531
6d805429 532 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 533 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
534
535error:
536 return ret;
537}
538
539/*
540 * Check on the relayd side for a quiescent state on the control socket.
541 */
6151a90f 542int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 543 uint64_t metadata_stream_id)
c8f59ee5
DG
544{
545 int ret;
ad7051c0 546 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
547 struct lttcomm_relayd_generic_reply reply;
548
549 /* Code flow error. Safety net. */
6151a90f 550 assert(rsock);
c8f59ee5
DG
551
552 DBG("Relayd checking quiescent control state");
553
ad7051c0
DG
554 msg.stream_id = htobe64(metadata_stream_id);
555
c8f59ee5 556 /* Send command */
6151a90f 557 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
558 if (ret < 0) {
559 goto error;
560 }
561
20275fe8 562 /* Receive response */
6151a90f 563 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
564 if (ret < 0) {
565 goto error;
566 }
567
568 reply.ret_code = be32toh(reply.ret_code);
569
570 /* Return session id or negative ret code. */
571 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
572 ret = -1;
573 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
574 goto error;
575 }
576
577 /* Control socket is quiescent */
6d805429 578 return 0;
c8f59ee5
DG
579
580error:
581 return ret;
582}
f7079f67
DG
583
584/*
585 * Begin a data pending command for a specific session id.
586 */
6151a90f 587int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
588{
589 int ret;
590 struct lttcomm_relayd_begin_data_pending msg;
591 struct lttcomm_relayd_generic_reply reply;
592
593 /* Code flow error. Safety net. */
6151a90f 594 assert(rsock);
f7079f67
DG
595
596 DBG("Relayd begin data pending");
597
598 msg.session_id = htobe64(id);
599
600 /* Send command */
6151a90f 601 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
602 if (ret < 0) {
603 goto error;
604 }
605
20275fe8 606 /* Receive response */
6151a90f 607 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
608 if (ret < 0) {
609 goto error;
610 }
611
612 reply.ret_code = be32toh(reply.ret_code);
613
614 /* Return session id or negative ret code. */
615 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
616 ret = -1;
617 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
618 goto error;
619 }
620
621 return 0;
622
623error:
624 return ret;
625}
626
627/*
628 * End a data pending command for a specific session id.
629 *
630 * Return 0 on success and set is_data_inflight to 0 if no data is being
631 * streamed or 1 if it is the case.
632 */
6151a90f 633int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
634 unsigned int *is_data_inflight)
635{
3062a258 636 int ret, recv_ret;
f7079f67
DG
637 struct lttcomm_relayd_end_data_pending msg;
638 struct lttcomm_relayd_generic_reply reply;
639
640 /* Code flow error. Safety net. */
6151a90f 641 assert(rsock);
f7079f67
DG
642
643 DBG("Relayd end data pending");
644
645 msg.session_id = htobe64(id);
646
647 /* Send command */
6151a90f 648 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
649 if (ret < 0) {
650 goto error;
651 }
652
20275fe8 653 /* Receive response */
6151a90f 654 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
655 if (ret < 0) {
656 goto error;
657 }
658
3062a258
DG
659 recv_ret = be32toh(reply.ret_code);
660 if (recv_ret < 0) {
661 ret = recv_ret;
f7079f67
DG
662 goto error;
663 }
664
3062a258 665 *is_data_inflight = recv_ret;
f7079f67 666
3062a258 667 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
668
669 return 0;
670
671error:
672 return ret;
673}
This page took 0.084279 seconds and 4 git commands to generate.