Fix: backward relayd communication compatibility.
[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
6c1c0768 18#define _LGPL_SOURCE
00e2e675
DG
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>
f263b7fd 28#include <common/compat/endian.h>
00e2e675 29#include <common/sessiond-comm/relayd.h>
50adc264 30#include <common/index/ctf-index.h>
00e2e675
DG
31
32#include "relayd.h"
33
34/*
35 * Send command. Fill up the header and append the data.
36 */
6151a90f 37static int send_command(struct lttcomm_relayd_sock *rsock,
76b9afaa 38 enum lttcomm_relayd_command cmd, const void *data, size_t size,
00e2e675
DG
39 int flags)
40{
41 int ret;
42 struct lttcomm_relayd_hdr header;
43 char *buf;
44 uint64_t buf_size = sizeof(header);
45
f96e4545
MD
46 if (rsock->sock.fd < 0) {
47 return -ECONNRESET;
48 }
49
00e2e675
DG
50 if (data) {
51 buf_size += size;
52 }
53
54 buf = zmalloc(buf_size);
55 if (buf == NULL) {
56 PERROR("zmalloc relayd send command buf");
57 ret = -1;
58 goto alloc_error;
59 }
60
53efb85a 61 memset(&header, 0, sizeof(header));
00e2e675
DG
62 header.cmd = htobe32(cmd);
63 header.data_size = htobe64(size);
64
65 /* Zeroed for now since not used. */
66 header.cmd_version = 0;
67 header.circuit_id = 0;
68
69 /* Prepare buffer to send. */
70 memcpy(buf, &header, sizeof(header));
71 if (data) {
72 memcpy(buf + sizeof(header), data, size);
73 }
74
06586bbe 75 DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size);
6151a90f 76 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
00e2e675 77 if (ret < 0) {
06586bbe
JG
78 PERROR("Failed to send command %d of size %" PRIu64,
79 (int) cmd, buf_size);
8994307f 80 ret = -errno;
00e2e675
DG
81 goto error;
82 }
00e2e675
DG
83error:
84 free(buf);
85alloc_error:
86 return ret;
87}
88
89/*
90 * Receive reply data on socket. This MUST be call after send_command or else
91 * could result in unexpected behavior(s).
92 */
6151a90f 93static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
00e2e675
DG
94{
95 int ret;
96
f96e4545
MD
97 if (rsock->sock.fd < 0) {
98 return -ECONNRESET;
99 }
100
8fd623e0 101 DBG3("Relayd waiting for reply of size %zu", size);
00e2e675 102
6151a90f 103 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
20275fe8
DG
104 if (ret <= 0 || ret != size) {
105 if (ret == 0) {
106 /* Orderly shutdown. */
6151a90f 107 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
20275fe8 108 } else {
8fd623e0 109 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
6151a90f 110 rsock->sock.fd, size, ret);
20275fe8
DG
111 }
112 /* Always return -1 here and the caller can use errno. */
113 ret = -1;
00e2e675
DG
114 goto error;
115 }
116
117error:
118 return ret;
119}
120
d3e2ba59
JD
121/*
122 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
123 * support the live reading capability.
124 */
125static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
42e9a27b
JR
126 char *session_name, char *hostname, int session_live_timer,
127 unsigned int snapshot)
d3e2ba59
JD
128{
129 int ret;
130 struct lttcomm_relayd_create_session_2_4 msg;
131
3a13ffd5
MD
132 if (lttng_strncpy(msg.session_name, session_name,
133 sizeof(msg.session_name))) {
246777db
MD
134 ret = -1;
135 goto error;
136 }
3a13ffd5 137 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
246777db
MD
138 ret = -1;
139 goto error;
140 }
d3e2ba59 141 msg.live_timer = htobe32(session_live_timer);
7d2f7452 142 msg.snapshot = htobe32(snapshot);
d3e2ba59
JD
143
144 /* Send command */
145 ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0);
146 if (ret < 0) {
147 goto error;
148 }
149
150error:
151 return ret;
152}
153
154/*
155 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
156 */
42e9a27b 157static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock)
d3e2ba59
JD
158{
159 int ret;
160
161 /* Send command */
162 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
163 if (ret < 0) {
164 goto error;
165 }
166
167error:
168 return ret;
169}
170
c5b6f4f0
DG
171/*
172 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
173 * set session_id of the relayd if we have a successful reply from the relayd.
174 *
20275fe8
DG
175 * On success, return 0 else a negative value which is either an errno error or
176 * a lttng error code from the relayd.
c5b6f4f0 177 */
d3e2ba59 178int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id,
7d2f7452
DG
179 char *session_name, char *hostname, int session_live_timer,
180 unsigned int snapshot)
c5b6f4f0
DG
181{
182 int ret;
183 struct lttcomm_relayd_status_session reply;
184
6151a90f 185 assert(rsock);
c5b6f4f0
DG
186 assert(session_id);
187
188 DBG("Relayd create session");
189
d3e2ba59
JD
190 switch(rsock->minor) {
191 case 1:
192 case 2:
193 case 3:
42e9a27b 194 ret = relayd_create_session_2_1(rsock);
31d74dc3 195 break;
d3e2ba59
JD
196 case 4:
197 default:
42e9a27b 198 ret = relayd_create_session_2_4(rsock, session_name,
7d2f7452 199 hostname, session_live_timer, snapshot);
31d74dc3 200 break;
d3e2ba59
JD
201 }
202
c5b6f4f0
DG
203 if (ret < 0) {
204 goto error;
205 }
206
20275fe8 207 /* Receive response */
6151a90f 208 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c5b6f4f0
DG
209 if (ret < 0) {
210 goto error;
211 }
212
213 reply.session_id = be64toh(reply.session_id);
214 reply.ret_code = be32toh(reply.ret_code);
215
216 /* Return session id or negative ret code. */
217 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
218 ret = -1;
219 ERR("Relayd create session replied error %d", reply.ret_code);
c5b6f4f0
DG
220 goto error;
221 } else {
222 ret = 0;
223 *session_id = reply.session_id;
224 }
225
226 DBG("Relayd session created with id %" PRIu64, reply.session_id);
227
228error:
229 return ret;
230}
231
00e2e675
DG
232/*
233 * Add stream on the relayd and assign stream handle to the stream_id argument.
234 *
235 * On success return 0 else return ret_code negative value.
236 */
6151a90f 237int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
0f907de1
JD
238 const char *pathname, uint64_t *stream_id,
239 uint64_t tracefile_size, uint64_t tracefile_count)
00e2e675
DG
240{
241 int ret;
242 struct lttcomm_relayd_add_stream msg;
0f907de1 243 struct lttcomm_relayd_add_stream_2_2 msg_2_2;
00e2e675
DG
244 struct lttcomm_relayd_status_stream reply;
245
246 /* Code flow error. Safety net. */
6151a90f 247 assert(rsock);
00e2e675
DG
248 assert(channel_name);
249 assert(pathname);
250
251 DBG("Relayd adding stream for channel name %s", channel_name);
252
0f907de1
JD
253 /* Compat with relayd 2.1 */
254 if (rsock->minor == 1) {
53efb85a 255 memset(&msg, 0, sizeof(msg));
a7c918ad
MD
256 if (lttng_strncpy(msg.channel_name, channel_name,
257 sizeof(msg.channel_name))) {
246777db
MD
258 ret = -1;
259 goto error;
260 }
a7c918ad
MD
261 if (lttng_strncpy(msg.pathname, pathname,
262 sizeof(msg.pathname))) {
246777db
MD
263 ret = -1;
264 goto error;
265 }
00e2e675 266
0f907de1
JD
267 /* Send command */
268 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
269 if (ret < 0) {
270 goto error;
271 }
272 } else {
53efb85a 273 memset(&msg_2_2, 0, sizeof(msg_2_2));
0f907de1 274 /* Compat with relayd 2.2+ */
a7c918ad
MD
275 if (lttng_strncpy(msg_2_2.channel_name, channel_name,
276 sizeof(msg_2_2.channel_name))) {
246777db
MD
277 ret = -1;
278 goto error;
279 }
a7c918ad
MD
280 if (lttng_strncpy(msg_2_2.pathname, pathname,
281 sizeof(msg_2_2.pathname))) {
246777db
MD
282 ret = -1;
283 goto error;
284 }
0f907de1
JD
285 msg_2_2.tracefile_size = htobe64(tracefile_size);
286 msg_2_2.tracefile_count = htobe64(tracefile_count);
287
288 /* Send command */
289 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0);
290 if (ret < 0) {
291 goto error;
292 }
00e2e675
DG
293 }
294
633d0084 295 /* Waiting for reply */
6151a90f 296 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
00e2e675
DG
297 if (ret < 0) {
298 goto error;
299 }
300
301 /* Back to host bytes order. */
302 reply.handle = be64toh(reply.handle);
303 reply.ret_code = be32toh(reply.ret_code);
304
305 /* Return session id or negative ret code. */
f73fabfd 306 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
307 ret = -1;
308 ERR("Relayd add stream replied error %d", reply.ret_code);
00e2e675
DG
309 } else {
310 /* Success */
311 ret = 0;
312 *stream_id = reply.handle;
313 }
314
77c7c900
MD
315 DBG("Relayd stream added successfully with handle %" PRIu64,
316 reply.handle);
00e2e675
DG
317
318error:
319 return ret;
320}
321
a4baae1b
JD
322/*
323 * Inform the relay that all the streams for the current channel has been sent.
324 *
325 * On success return 0 else return ret_code negative value.
326 */
327int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
328{
329 int ret;
330 struct lttcomm_relayd_generic_reply reply;
331
332 /* Code flow error. Safety net. */
333 assert(rsock);
334
335 DBG("Relayd sending streams sent.");
336
337 /* This feature was introduced in 2.4, ignore it for earlier versions. */
338 if (rsock->minor < 4) {
339 ret = 0;
340 goto end;
341 }
342
343 /* Send command */
344 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
345 if (ret < 0) {
346 goto error;
347 }
348
349 /* Waiting for reply */
350 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
351 if (ret < 0) {
352 goto error;
353 }
354
355 /* Back to host bytes order. */
356 reply.ret_code = be32toh(reply.ret_code);
357
358 /* Return session id or negative ret code. */
359 if (reply.ret_code != LTTNG_OK) {
360 ret = -1;
361 ERR("Relayd streams sent replied error %d", reply.ret_code);
362 goto error;
363 } else {
364 /* Success */
365 ret = 0;
366 }
367
368 DBG("Relayd streams sent success");
369
370error:
371end:
372 return ret;
373}
374
00e2e675
DG
375/*
376 * Check version numbers on the relayd.
d4519fa3
JD
377 * If major versions are compatible, we assign minor_to_use to the
378 * minor version of the procotol we are going to use for this session.
00e2e675 379 *
67d5aa28
JD
380 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
381 * otherwise, or a negative value on network errors.
00e2e675 382 */
6151a90f 383int relayd_version_check(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
384{
385 int ret;
092b6259 386 struct lttcomm_relayd_version msg;
00e2e675
DG
387
388 /* Code flow error. Safety net. */
6151a90f 389 assert(rsock);
00e2e675 390
6151a90f
JD
391 DBG("Relayd version check for major.minor %u.%u", rsock->major,
392 rsock->minor);
00e2e675 393
53efb85a 394 memset(&msg, 0, sizeof(msg));
092b6259 395 /* Prepare network byte order before transmission. */
6151a90f
JD
396 msg.major = htobe32(rsock->major);
397 msg.minor = htobe32(rsock->minor);
092b6259 398
00e2e675 399 /* Send command */
6151a90f 400 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
00e2e675
DG
401 if (ret < 0) {
402 goto error;
403 }
404
20275fe8 405 /* Receive response */
6151a90f 406 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
00e2e675
DG
407 if (ret < 0) {
408 goto error;
409 }
410
411 /* Set back to host bytes order */
092b6259
DG
412 msg.major = be32toh(msg.major);
413 msg.minor = be32toh(msg.minor);
414
415 /*
416 * Only validate the major version. If the other side is higher,
417 * communication is not possible. Only major version equal can talk to each
418 * other. If the minor version differs, the lowest version is used by both
419 * sides.
092b6259 420 */
6151a90f 421 if (msg.major != rsock->major) {
d4519fa3 422 /* Not compatible */
67d5aa28 423 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
d4519fa3 424 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
6151a90f 425 msg.major, rsock->major);
092b6259 426 goto error;
00e2e675
DG
427 }
428
092b6259 429 /*
6151a90f
JD
430 * If the relayd's minor version is higher, it will adapt to our version so
431 * we can continue to use the latest relayd communication data structure.
432 * If the received minor version is higher, the relayd should adapt to us.
092b6259 433 */
6151a90f
JD
434 if (rsock->minor > msg.minor) {
435 rsock->minor = msg.minor;
d4519fa3 436 }
092b6259 437
d4519fa3
JD
438 /* Version number compatible */
439 DBG2("Relayd version is compatible, using protocol version %u.%u",
6151a90f 440 rsock->major, rsock->minor);
d4519fa3 441 ret = 0;
00e2e675
DG
442
443error:
444 return ret;
445}
446
00e2e675
DG
447/*
448 * Add stream on the relayd and assign stream handle to the stream_id argument.
449 *
450 * On success return 0 else return ret_code negative value.
451 */
6151a90f 452int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
00e2e675
DG
453{
454 int ret;
455
456 /* Code flow error. Safety net. */
6151a90f 457 assert(rsock);
00e2e675 458
77c7c900 459 DBG("Relayd sending metadata of size %zu", len);
00e2e675
DG
460
461 /* Send command */
6151a90f 462 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
00e2e675
DG
463 if (ret < 0) {
464 goto error;
465 }
466
467 DBG2("Relayd metadata added successfully");
468
469 /*
470 * After that call, the metadata data MUST be sent to the relayd so the
471 * receive size on the other end matches the len of the metadata packet
633d0084 472 * header. This is why we don't wait for a reply here.
00e2e675
DG
473 */
474
475error:
476 return ret;
477}
478
479/*
6151a90f 480 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
00e2e675 481 */
6151a90f 482int relayd_connect(struct lttcomm_relayd_sock *rsock)
00e2e675
DG
483{
484 /* Code flow error. Safety net. */
6151a90f 485 assert(rsock);
00e2e675 486
f96e4545
MD
487 if (!rsock->sock.ops) {
488 /*
489 * Attempting a connect on a non-initialized socket.
490 */
491 return -ECONNRESET;
492 }
493
00e2e675
DG
494 DBG3("Relayd connect ...");
495
6151a90f 496 return rsock->sock.ops->connect(&rsock->sock);
00e2e675
DG
497}
498
499/*
6151a90f 500 * Close relayd socket with an allocated lttcomm_relayd_sock.
ffe60014
DG
501 *
502 * If no socket operations are found, simply return 0 meaning that everything
503 * is fine. Without operations, the socket can not possibly be opened or used.
504 * This is possible if the socket was allocated but not created. However, the
505 * caller could simply use it to store a valid file descriptor for instance
506 * passed over a Unix socket and call this to cleanup but still without a valid
507 * ops pointer.
508 *
509 * Return the close returned value. On error, a negative value is usually
510 * returned back from close(2).
00e2e675 511 */
6151a90f 512int relayd_close(struct lttcomm_relayd_sock *rsock)
00e2e675 513{
ffe60014
DG
514 int ret;
515
00e2e675 516 /* Code flow error. Safety net. */
6151a90f 517 assert(rsock);
00e2e675 518
ffe60014 519 /* An invalid fd is fine, return success. */
6151a90f 520 if (rsock->sock.fd < 0) {
ffe60014
DG
521 ret = 0;
522 goto end;
523 }
524
6151a90f 525 DBG3("Relayd closing socket %d", rsock->sock.fd);
00e2e675 526
6151a90f
JD
527 if (rsock->sock.ops) {
528 ret = rsock->sock.ops->close(&rsock->sock);
ffe60014
DG
529 } else {
530 /* Default call if no specific ops found. */
6151a90f 531 ret = close(rsock->sock.fd);
ffe60014
DG
532 if (ret < 0) {
533 PERROR("relayd_close default close");
534 }
535 }
f96e4545 536 rsock->sock.fd = -1;
ffe60014
DG
537
538end:
539 return ret;
00e2e675
DG
540}
541
542/*
543 * Send data header structure to the relayd.
544 */
6151a90f 545int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
00e2e675
DG
546 struct lttcomm_relayd_data_hdr *hdr, size_t size)
547{
548 int ret;
549
550 /* Code flow error. Safety net. */
6151a90f 551 assert(rsock);
00e2e675
DG
552 assert(hdr);
553
f96e4545
MD
554 if (rsock->sock.fd < 0) {
555 return -ECONNRESET;
556 }
557
8fd623e0 558 DBG3("Relayd sending data header of size %zu", size);
00e2e675
DG
559
560 /* Again, safety net */
561 if (size == 0) {
562 size = sizeof(struct lttcomm_relayd_data_hdr);
563 }
564
565 /* Only send data header. */
6151a90f 566 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
00e2e675 567 if (ret < 0) {
8994307f 568 ret = -errno;
00e2e675
DG
569 goto error;
570 }
571
572 /*
573 * The data MUST be sent right after that command for the receive on the
574 * other end to match the size in the header.
575 */
576
577error:
578 return ret;
579}
173af62f
DG
580
581/*
582 * Send close stream command to the relayd.
583 */
6151a90f 584int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
173af62f
DG
585 uint64_t last_net_seq_num)
586{
587 int ret;
588 struct lttcomm_relayd_close_stream msg;
589 struct lttcomm_relayd_generic_reply reply;
590
591 /* Code flow error. Safety net. */
6151a90f 592 assert(rsock);
173af62f 593
77c7c900 594 DBG("Relayd closing stream id %" PRIu64, stream_id);
173af62f 595
53efb85a 596 memset(&msg, 0, sizeof(msg));
173af62f
DG
597 msg.stream_id = htobe64(stream_id);
598 msg.last_net_seq_num = htobe64(last_net_seq_num);
599
600 /* Send command */
6151a90f 601 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
173af62f
DG
602 if (ret < 0) {
603 goto error;
604 }
605
20275fe8 606 /* Receive response */
6151a90f 607 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
173af62f
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. */
f73fabfd 615 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
616 ret = -1;
617 ERR("Relayd close stream replied error %d", reply.ret_code);
173af62f
DG
618 } else {
619 /* Success */
620 ret = 0;
621 }
622
77c7c900 623 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
173af62f
DG
624
625error:
626 return ret;
627}
c8f59ee5
DG
628
629/*
630 * Check for data availability for a given stream id.
631 *
6d805429 632 * Return 0 if NOT pending, 1 if so and a negative value on error.
c8f59ee5 633 */
6151a90f 634int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
c8f59ee5
DG
635 uint64_t last_net_seq_num)
636{
637 int ret;
6d805429 638 struct lttcomm_relayd_data_pending msg;
c8f59ee5
DG
639 struct lttcomm_relayd_generic_reply reply;
640
641 /* Code flow error. Safety net. */
6151a90f 642 assert(rsock);
c8f59ee5 643
6d805429 644 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
c8f59ee5 645
53efb85a 646 memset(&msg, 0, sizeof(msg));
c8f59ee5
DG
647 msg.stream_id = htobe64(stream_id);
648 msg.last_net_seq_num = htobe64(last_net_seq_num);
649
650 /* Send command */
6151a90f 651 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
c8f59ee5
DG
652 sizeof(msg), 0);
653 if (ret < 0) {
654 goto error;
655 }
656
20275fe8 657 /* Receive response */
6151a90f 658 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
659 if (ret < 0) {
660 goto error;
661 }
662
663 reply.ret_code = be32toh(reply.ret_code);
664
665 /* Return session id or negative ret code. */
666 if (reply.ret_code >= LTTNG_OK) {
bb63afd9 667 ERR("Relayd data pending replied error %d", reply.ret_code);
c8f59ee5
DG
668 }
669
670 /* At this point, the ret code is either 1 or 0 */
671 ret = reply.ret_code;
672
6d805429 673 DBG("Relayd data is %s pending for stream id %" PRIu64,
9dd26bb9 674 ret == 1 ? "" : "NOT", stream_id);
c8f59ee5
DG
675
676error:
677 return ret;
678}
679
680/*
681 * Check on the relayd side for a quiescent state on the control socket.
682 */
6151a90f 683int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
ad7051c0 684 uint64_t metadata_stream_id)
c8f59ee5
DG
685{
686 int ret;
ad7051c0 687 struct lttcomm_relayd_quiescent_control msg;
c8f59ee5
DG
688 struct lttcomm_relayd_generic_reply reply;
689
690 /* Code flow error. Safety net. */
6151a90f 691 assert(rsock);
c8f59ee5
DG
692
693 DBG("Relayd checking quiescent control state");
694
53efb85a 695 memset(&msg, 0, sizeof(msg));
ad7051c0
DG
696 msg.stream_id = htobe64(metadata_stream_id);
697
c8f59ee5 698 /* Send command */
6151a90f 699 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
c8f59ee5
DG
700 if (ret < 0) {
701 goto error;
702 }
703
20275fe8 704 /* Receive response */
6151a90f 705 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
c8f59ee5
DG
706 if (ret < 0) {
707 goto error;
708 }
709
710 reply.ret_code = be32toh(reply.ret_code);
711
712 /* Return session id or negative ret code. */
713 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
714 ret = -1;
715 ERR("Relayd quiescent control replied error %d", reply.ret_code);
c8f59ee5
DG
716 goto error;
717 }
718
719 /* Control socket is quiescent */
6d805429 720 return 0;
c8f59ee5
DG
721
722error:
723 return ret;
724}
f7079f67
DG
725
726/*
727 * Begin a data pending command for a specific session id.
728 */
6151a90f 729int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
f7079f67
DG
730{
731 int ret;
732 struct lttcomm_relayd_begin_data_pending msg;
733 struct lttcomm_relayd_generic_reply reply;
734
735 /* Code flow error. Safety net. */
6151a90f 736 assert(rsock);
f7079f67
DG
737
738 DBG("Relayd begin data pending");
739
53efb85a 740 memset(&msg, 0, sizeof(msg));
f7079f67
DG
741 msg.session_id = htobe64(id);
742
743 /* Send command */
6151a90f 744 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
745 if (ret < 0) {
746 goto error;
747 }
748
20275fe8 749 /* Receive response */
6151a90f 750 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
751 if (ret < 0) {
752 goto error;
753 }
754
755 reply.ret_code = be32toh(reply.ret_code);
756
757 /* Return session id or negative ret code. */
758 if (reply.ret_code != LTTNG_OK) {
bb63afd9
DG
759 ret = -1;
760 ERR("Relayd begin data pending replied error %d", reply.ret_code);
f7079f67
DG
761 goto error;
762 }
763
764 return 0;
765
766error:
767 return ret;
768}
769
770/*
771 * End a data pending command for a specific session id.
772 *
773 * Return 0 on success and set is_data_inflight to 0 if no data is being
774 * streamed or 1 if it is the case.
775 */
6151a90f 776int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
f7079f67
DG
777 unsigned int *is_data_inflight)
778{
af6c30b5 779 int ret, recv_ret;
f7079f67
DG
780 struct lttcomm_relayd_end_data_pending msg;
781 struct lttcomm_relayd_generic_reply reply;
782
783 /* Code flow error. Safety net. */
6151a90f 784 assert(rsock);
f7079f67
DG
785
786 DBG("Relayd end data pending");
787
53efb85a 788 memset(&msg, 0, sizeof(msg));
f7079f67
DG
789 msg.session_id = htobe64(id);
790
791 /* Send command */
6151a90f 792 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
f7079f67
DG
793 if (ret < 0) {
794 goto error;
795 }
796
20275fe8 797 /* Receive response */
6151a90f 798 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
f7079f67
DG
799 if (ret < 0) {
800 goto error;
801 }
802
af6c30b5
DG
803 recv_ret = be32toh(reply.ret_code);
804 if (recv_ret < 0) {
805 ret = recv_ret;
f7079f67
DG
806 goto error;
807 }
808
af6c30b5 809 *is_data_inflight = recv_ret;
f7079f67 810
af6c30b5 811 DBG("Relayd end data pending is data inflight: %d", recv_ret);
f7079f67
DG
812
813 return 0;
814
815error:
816 return ret;
817}
1c20f0e2
JD
818
819/*
820 * Send index to the relayd.
821 */
822int relayd_send_index(struct lttcomm_relayd_sock *rsock,
50adc264 823 struct ctf_packet_index *index, uint64_t relay_stream_id,
1c20f0e2
JD
824 uint64_t net_seq_num)
825{
826 int ret;
827 struct lttcomm_relayd_index msg;
828 struct lttcomm_relayd_generic_reply reply;
829
830 /* Code flow error. Safety net. */
831 assert(rsock);
832
833 if (rsock->minor < 4) {
834 DBG("Not sending indexes before protocol 2.4");
835 ret = 0;
836 goto error;
837 }
838
839 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
840
53efb85a 841 memset(&msg, 0, sizeof(msg));
1c20f0e2
JD
842 msg.relay_stream_id = htobe64(relay_stream_id);
843 msg.net_seq_num = htobe64(net_seq_num);
844
845 /* The index is already in big endian. */
846 msg.packet_size = index->packet_size;
847 msg.content_size = index->content_size;
848 msg.timestamp_begin = index->timestamp_begin;
849 msg.timestamp_end = index->timestamp_end;
850 msg.events_discarded = index->events_discarded;
851 msg.stream_id = index->stream_id;
852
234cd636
JD
853 if (rsock->minor >= 8) {
854 msg.stream_instance_id = index->stream_instance_id;
855 msg.packet_seq_num = index->packet_seq_num;
856 }
857
1c20f0e2 858 /* Send command */
f8f3885c
MD
859 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
860 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
861 rsock->minor),
862 lttng_to_index_minor(rsock->major, rsock->minor)),
863 0);
1c20f0e2
JD
864 if (ret < 0) {
865 goto error;
866 }
867
868 /* Receive response */
869 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
870 if (ret < 0) {
871 goto error;
872 }
873
874 reply.ret_code = be32toh(reply.ret_code);
875
876 /* Return session id or negative ret code. */
877 if (reply.ret_code != LTTNG_OK) {
878 ret = -1;
879 ERR("Relayd send index replied error %d", reply.ret_code);
880 } else {
881 /* Success */
882 ret = 0;
883 }
884
885error:
886 return ret;
887}
93ec662e
JD
888
889/*
890 * Ask the relay to reset the metadata trace file (regeneration).
891 */
892int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
893 uint64_t stream_id, uint64_t version)
894{
895 int ret;
896 struct lttcomm_relayd_reset_metadata msg;
897 struct lttcomm_relayd_generic_reply reply;
898
899 /* Code flow error. Safety net. */
900 assert(rsock);
901
902 /* Should have been prevented by the sessiond. */
903 if (rsock->minor < 8) {
904 ERR("Metadata regeneration unsupported before 2.8");
905 ret = -1;
906 goto error;
907 }
908
909 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
910
911 memset(&msg, 0, sizeof(msg));
912 msg.stream_id = htobe64(stream_id);
913 msg.version = htobe64(version);
914
915 /* Send command */
916 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
917 if (ret < 0) {
918 goto error;
919 }
920
921 /* Receive response */
922 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
923 if (ret < 0) {
924 goto error;
925 }
926
927 reply.ret_code = be32toh(reply.ret_code);
928
929 /* Return session id or negative ret code. */
930 if (reply.ret_code != LTTNG_OK) {
931 ret = -1;
932 ERR("Relayd reset metadata replied error %d", reply.ret_code);
933 } else {
934 /* Success */
935 ret = 0;
936 }
937
938 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
939
940error:
941 return ret;
942}
a1ae2ea5 943
d73bf3d7
JD
944int relayd_rotate_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
945 const char *new_pathname, uint64_t new_chunk_id,
946 uint64_t seq_num)
947{
948 int ret;
949 struct lttcomm_relayd_rotate_stream *msg = NULL;
950 struct lttcomm_relayd_generic_reply reply;
951 size_t len;
952 int msg_len;
953
954 /* Code flow error. Safety net. */
955 assert(rsock);
956
957 DBG("Sending rotate stream id %" PRIu64 " command to relayd", stream_id);
958
959 /* Account for the trailing NULL. */
960 len = strnlen(new_pathname, LTTNG_PATH_MAX) + 1;
961 if (len > LTTNG_PATH_MAX) {
962 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
963 ret = -1;
964 goto error;
965 }
966
967 msg_len = offsetof(struct lttcomm_relayd_rotate_stream, new_pathname) + len;
968 msg = zmalloc(msg_len);
969 if (!msg) {
970 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
971 msg_len);
972 ret = -1;
973 goto error;
974 }
975
976 if (lttng_strncpy(msg->new_pathname, new_pathname, len)) {
977 ret = -1;
978 ERR("Failed to copy relayd rotate stream command's new path name");
979 goto error;
980 }
981
982 msg->pathname_length = htobe32(len);
983 msg->stream_id = htobe64(stream_id);
984 msg->new_chunk_id = htobe64(new_chunk_id);
985 /*
986 * The seq_num is invalid for metadata streams, but it is ignored on
987 * the relay.
988 */
989 msg->rotate_at_seq_num = htobe64(seq_num);
990
991 /* Send command. */
992 ret = send_command(rsock, RELAYD_ROTATE_STREAM, (void *) msg, msg_len, 0);
993 if (ret < 0) {
994 ERR("Send rotate command");
995 goto error;
996 }
997
998 /* Receive response. */
999 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1000 if (ret < 0) {
1001 ERR("Receive rotate reply");
1002 goto error;
1003 }
1004
1005 reply.ret_code = be32toh(reply.ret_code);
1006
1007 /* Return session id or negative ret code. */
1008 if (reply.ret_code != LTTNG_OK) {
1009 ret = -1;
1010 ERR("Relayd rotate stream replied error %d", reply.ret_code);
1011 } else {
1012 /* Success. */
1013 ret = 0;
1014 DBG("Relayd rotated stream id %" PRIu64 " successfully", stream_id);
1015 }
1016
1017error:
1018 free(msg);
1019 return ret;
1020}
1021
00fb02ac
JD
1022int relayd_rotate_rename(struct lttcomm_relayd_sock *rsock,
1023 const char *old_path, const char *new_path)
1024{
1025 int ret;
1026 struct lttcomm_relayd_rotate_rename *msg = NULL;
1027 struct lttcomm_relayd_generic_reply reply;
1028 size_t old_path_length, new_path_length;
1029 size_t msg_length;
1030
1031 /* Code flow error. Safety net. */
1032 assert(rsock);
1033
1034 DBG("Relayd rename chunk %s to %s", old_path, new_path);
1035
1036 /* The two paths are sent with a '\0' delimiter between them. */
1037 old_path_length = strlen(old_path) + 1;
1038 new_path_length = strlen(new_path) + 1;
1039
1040 msg_length = sizeof(*msg) + old_path_length + new_path_length;
1041 msg = zmalloc(msg_length);
1042 if (!msg) {
1043 PERROR("zmalloc rotate-rename command message");
1044 ret = -1;
1045 goto error;
1046 }
1047
1048 assert(old_path_length <= UINT32_MAX);
1049 msg->old_path_length = htobe32(old_path_length);
1050
1051 assert(new_path_length <= UINT32_MAX);
1052 msg->new_path_length = htobe32(new_path_length);
1053
1054 strcpy(msg->paths, old_path);
1055 strcpy(msg->paths + old_path_length, new_path);
1056
1057 /* Send command */
1058 ret = send_command(rsock, RELAYD_ROTATE_RENAME, (const void *) msg,
1059 msg_length, 0);
1060 if (ret < 0) {
1061 goto error;
1062 }
1063
1064 /* Receive response */
1065 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1066 if (ret < 0) {
1067 goto error;
1068 }
1069
1070 reply.ret_code = be32toh(reply.ret_code);
1071
1072 /* Return session id or negative ret code. */
1073 if (reply.ret_code != LTTNG_OK) {
1074 ret = -1;
1075 ERR("Relayd rotate rename replied error %d", reply.ret_code);
1076 } else {
1077 /* Success */
1078 ret = 0;
1079 }
1080
1081 DBG("Relayd rotate rename completed successfully");
1082
1083error:
1084 free(msg);
1085 return ret;
1086}
1087
d88744a4
JD
1088int relayd_rotate_pending(struct lttcomm_relayd_sock *rsock, uint64_t chunk_id)
1089{
1090 int ret;
1091 struct lttcomm_relayd_rotate_pending msg;
1092 struct lttcomm_relayd_rotate_pending_reply reply;
1093
1094 /* Code flow error. Safety net. */
1095 assert(rsock);
1096
1097 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64,
1098 chunk_id);
1099
1100 memset(&msg, 0, sizeof(msg));
1101 msg.chunk_id = htobe64(chunk_id);
1102
1103 /* Send command */
1104 ret = send_command(rsock, RELAYD_ROTATE_PENDING, (void *) &msg,
1105 sizeof(msg), 0);
1106 if (ret < 0) {
1107 goto error;
1108 }
1109
1110 /* Receive response */
1111 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1112 if (ret < 0) {
1113 goto error;
1114 }
1115
1116 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1117
1118 /* Return session id or negative ret code. */
1119 if (reply.generic.ret_code != LTTNG_OK) {
1120 ret = -reply.generic.ret_code;
1121 ERR("Relayd rotate pending replied with error %d", ret);
1122 goto error;
1123 } else {
1124 /* No error, just rotate pending state */
1125 if (reply.is_pending == 0 || reply.is_pending == 1) {
1126 ret = reply.is_pending;
1127 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1128 ret ? "rotation pending" : "rotation NOT pending");
1129 } else {
1130 ret = -LTTNG_ERR_UNK;
1131 }
1132 }
1133
1134error:
1135 return ret;
1136}
1137
a1ae2ea5
JD
1138int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path)
1139{
1140 int ret;
1141 struct lttcomm_relayd_mkdir *msg;
1142 struct lttcomm_relayd_generic_reply reply;
1143 size_t len;
1144
1145 /* Code flow error. Safety net. */
1146 assert(rsock);
1147
1148 DBG("Relayd mkdir path %s", path);
1149
1150 len = strlen(path) + 1;
1151 msg = zmalloc(sizeof(msg->length) + len);
1152 if (!msg) {
1153 PERROR("Alloc mkdir msg");
1154 ret = -1;
1155 goto error;
1156 }
1157 msg->length = htobe32((uint32_t) len);
1158
1159 if (lttng_strncpy(msg->path, path, len)) {
1160 ret = -1;
1161 goto error;
1162 }
1163
1164 /* Send command */
1165 ret = send_command(rsock, RELAYD_MKDIR, (void *) msg,
1166 sizeof(msg->length) + len, 0);
1167 if (ret < 0) {
1168 goto error;
1169 }
1170
1171 /* Receive response */
1172 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1173 if (ret < 0) {
1174 goto error;
1175 }
1176
1177 reply.ret_code = be32toh(reply.ret_code);
1178
1179 /* Return session id or negative ret code. */
1180 if (reply.ret_code != LTTNG_OK) {
1181 ret = -1;
1182 ERR("Relayd mkdir replied error %d", reply.ret_code);
1183 } else {
1184 /* Success */
1185 ret = 0;
1186 }
1187
1188 DBG("Relayd mkdir completed successfully");
1189
1190error:
1191 free(msg);
1192 return ret;
a1ae2ea5 1193}
This page took 0.102581 seconds and 4 git commands to generate.