relayd: Implement get_configuration relayd protocol command
[lttng-tools.git] / src / common / relayd / relayd.c
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 _LGPL_SOURCE
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/compat/string.h>
30 #include <common/sessiond-comm/relayd.h>
31 #include <common/index/ctf-index.h>
32 #include <common/trace-chunk.h>
33 #include <common/string-utils/format.h>
34
35 #include "relayd.h"
36
37 static
38 bool relayd_supports_chunks(const struct lttcomm_relayd_sock *sock)
39 {
40 if (sock->major > 2) {
41 return true;
42 } else if (sock->major == 2 && sock->minor >= 11) {
43 return true;
44 }
45 return false;
46 }
47
48 static
49 bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock *sock)
50 {
51 if (sock->major > 2) {
52 return true;
53 } else if (sock->major == 2 && sock->minor >= 12) {
54 return true;
55 }
56 return false;
57 }
58
59 /*
60 * Send command. Fill up the header and append the data.
61 */
62 static int send_command(struct lttcomm_relayd_sock *rsock,
63 enum lttcomm_relayd_command cmd, const void *data, size_t size,
64 int flags)
65 {
66 int ret;
67 struct lttcomm_relayd_hdr header;
68 char *buf;
69 uint64_t buf_size = sizeof(header);
70
71 if (rsock->sock.fd < 0) {
72 return -ECONNRESET;
73 }
74
75 if (data) {
76 buf_size += size;
77 }
78
79 buf = zmalloc(buf_size);
80 if (buf == NULL) {
81 PERROR("zmalloc relayd send command buf");
82 ret = -1;
83 goto alloc_error;
84 }
85
86 memset(&header, 0, sizeof(header));
87 header.cmd = htobe32(cmd);
88 header.data_size = htobe64(size);
89
90 /* Zeroed for now since not used. */
91 header.cmd_version = 0;
92 header.circuit_id = 0;
93
94 /* Prepare buffer to send. */
95 memcpy(buf, &header, sizeof(header));
96 if (data) {
97 memcpy(buf + sizeof(header), data, size);
98 }
99
100 DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size);
101 ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags);
102 if (ret < 0) {
103 PERROR("Failed to send command %d of size %" PRIu64,
104 (int) cmd, buf_size);
105 ret = -errno;
106 goto error;
107 }
108 error:
109 free(buf);
110 alloc_error:
111 return ret;
112 }
113
114 /*
115 * Receive reply data on socket. This MUST be call after send_command or else
116 * could result in unexpected behavior(s).
117 */
118 static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size)
119 {
120 int ret;
121
122 if (rsock->sock.fd < 0) {
123 return -ECONNRESET;
124 }
125
126 DBG3("Relayd waiting for reply of size %zu", size);
127
128 ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0);
129 if (ret <= 0 || ret != size) {
130 if (ret == 0) {
131 /* Orderly shutdown. */
132 DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd);
133 } else {
134 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
135 rsock->sock.fd, size, ret);
136 }
137 /* Always return -1 here and the caller can use errno. */
138 ret = -1;
139 goto error;
140 }
141
142 error:
143 return ret;
144 }
145
146 /*
147 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
148 * hostname, and base_path) have no length restriction on the sender side.
149 * Length for both payloads is stored in the msg struct. A new dynamic size
150 * payload size is introduced.
151 */
152 static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock,
153 const char *session_name, const char *hostname,
154 const char *base_path, int session_live_timer,
155 unsigned int snapshot, uint64_t sessiond_session_id,
156 const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id,
157 time_t creation_time, bool session_name_contains_creation_time,
158 struct lttcomm_relayd_create_session_reply_2_11 *reply,
159 char *output_path)
160 {
161 int ret;
162 struct lttcomm_relayd_create_session_2_11 *msg = NULL;
163 size_t session_name_len;
164 size_t hostname_len;
165 size_t base_path_len;
166 size_t msg_length;
167 char *dst;
168
169 if (!base_path) {
170 base_path = "";
171 }
172 /* The three names are sent with a '\0' delimiter between them. */
173 session_name_len = strlen(session_name) + 1;
174 hostname_len = strlen(hostname) + 1;
175 base_path_len = strlen(base_path) + 1;
176
177 msg_length = sizeof(*msg) + session_name_len + hostname_len + base_path_len;
178 msg = zmalloc(msg_length);
179 if (!msg) {
180 PERROR("zmalloc create_session_2_11 command message");
181 ret = -1;
182 goto error;
183 }
184
185 assert(session_name_len <= UINT32_MAX);
186 msg->session_name_len = htobe32(session_name_len);
187
188 assert(hostname_len <= UINT32_MAX);
189 msg->hostname_len = htobe32(hostname_len);
190
191 assert(base_path_len <= UINT32_MAX);
192 msg->base_path_len = htobe32(base_path_len);
193
194 dst = msg->names;
195 if (lttng_strncpy(dst, session_name, session_name_len)) {
196 ret = -1;
197 goto error;
198 }
199 dst += session_name_len;
200 if (lttng_strncpy(dst, hostname, hostname_len)) {
201 ret = -1;
202 goto error;
203 }
204 dst += hostname_len;
205 if (lttng_strncpy(dst, base_path, base_path_len)) {
206 ret = -1;
207 goto error;
208 }
209
210 msg->live_timer = htobe32(session_live_timer);
211 msg->snapshot = !!snapshot;
212
213 lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid);
214 msg->session_id = htobe64(sessiond_session_id);
215 msg->session_name_contains_creation_time = session_name_contains_creation_time;
216 if (current_chunk_id) {
217 LTTNG_OPTIONAL_SET(&msg->current_chunk_id,
218 htobe64(*current_chunk_id));
219 }
220
221 msg->creation_time = htobe64((uint64_t) creation_time);
222
223 /* Send command */
224 ret = send_command(rsock, RELAYD_CREATE_SESSION, msg, msg_length, 0);
225 if (ret < 0) {
226 goto error;
227 }
228 /* Receive response */
229 ret = recv_reply(rsock, reply, sizeof(*reply));
230 if (ret < 0) {
231 goto error;
232 }
233 reply->generic.session_id = be64toh(reply->generic.session_id);
234 reply->generic.ret_code = be32toh(reply->generic.ret_code);
235 reply->output_path_length = be32toh(reply->output_path_length);
236 if (reply->output_path_length >= LTTNG_PATH_MAX) {
237 ERR("Invalid session output path length in reply (%" PRIu32 " bytes) exceeds maximal allowed length (%d bytes)",
238 reply->output_path_length, LTTNG_PATH_MAX);
239 ret = -1;
240 goto error;
241 }
242 ret = recv_reply(rsock, output_path, reply->output_path_length);
243 if (ret < 0) {
244 goto error;
245 }
246 error:
247 free(msg);
248 return ret;
249 }
250 /*
251 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
252 * support the live reading capability.
253 */
254 static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock,
255 const char *session_name, const char *hostname,
256 int session_live_timer, unsigned int snapshot,
257 struct lttcomm_relayd_status_session *reply)
258 {
259 int ret;
260 struct lttcomm_relayd_create_session_2_4 msg;
261
262 if (lttng_strncpy(msg.session_name, session_name,
263 sizeof(msg.session_name))) {
264 ret = -1;
265 goto error;
266 }
267 if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) {
268 ret = -1;
269 goto error;
270 }
271 msg.live_timer = htobe32(session_live_timer);
272 msg.snapshot = htobe32(snapshot);
273
274 /* Send command */
275 ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0);
276 if (ret < 0) {
277 goto error;
278 }
279
280 /* Receive response */
281 ret = recv_reply(rsock, reply, sizeof(*reply));
282 if (ret < 0) {
283 goto error;
284 }
285 reply->session_id = be64toh(reply->session_id);
286 reply->ret_code = be32toh(reply->ret_code);
287 error:
288 return ret;
289 }
290
291 /*
292 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
293 */
294 static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock,
295 struct lttcomm_relayd_status_session *reply)
296 {
297 int ret;
298
299 /* Send command */
300 ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0);
301 if (ret < 0) {
302 goto error;
303 }
304
305 /* Receive response */
306 ret = recv_reply(rsock, reply, sizeof(*reply));
307 if (ret < 0) {
308 goto error;
309 }
310 reply->session_id = be64toh(reply->session_id);
311 reply->ret_code = be32toh(reply->ret_code);
312 error:
313 return ret;
314 }
315
316 /*
317 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
318 * set session_id of the relayd if we have a successful reply from the relayd.
319 *
320 * On success, return 0 else a negative value which is either an errno error or
321 * a lttng error code from the relayd.
322 */
323 int relayd_create_session(struct lttcomm_relayd_sock *rsock,
324 uint64_t *relayd_session_id,
325 const char *session_name, const char *hostname,
326 const char *base_path, int session_live_timer,
327 unsigned int snapshot, uint64_t sessiond_session_id,
328 const lttng_uuid sessiond_uuid,
329 const uint64_t *current_chunk_id,
330 time_t creation_time, bool session_name_contains_creation_time,
331 char *output_path)
332 {
333 int ret;
334 struct lttcomm_relayd_create_session_reply_2_11 reply = {};
335
336 assert(rsock);
337 assert(relayd_session_id);
338
339 DBG("Relayd create session");
340
341 if (rsock->minor < 4) {
342 /* From 2.1 to 2.3 */
343 ret = relayd_create_session_2_1(rsock, &reply.generic);
344 } else if (rsock->minor >= 4 && rsock->minor < 11) {
345 /* From 2.4 to 2.10 */
346 ret = relayd_create_session_2_4(rsock, session_name,
347 hostname, session_live_timer, snapshot,
348 &reply.generic);
349 } else {
350 /* From 2.11 to ... */
351 ret = relayd_create_session_2_11(rsock, session_name,
352 hostname, base_path, session_live_timer, snapshot,
353 sessiond_session_id, sessiond_uuid,
354 current_chunk_id, creation_time,
355 session_name_contains_creation_time,
356 &reply, output_path);
357 }
358
359 if (ret < 0) {
360 goto error;
361 }
362
363 /* Return session id or negative ret code. */
364 if (reply.generic.ret_code != LTTNG_OK) {
365 ret = -1;
366 ERR("Relayd create session replied error %d",
367 reply.generic.ret_code);
368 goto error;
369 } else {
370 ret = 0;
371 *relayd_session_id = reply.generic.session_id;
372 }
373
374 DBG("Relayd session created with id %" PRIu64, reply.generic.session_id);
375
376 error:
377 return ret;
378 }
379
380 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock *rsock,
381 const char *channel_name, const char *pathname)
382 {
383 int ret;
384 struct lttcomm_relayd_add_stream msg;
385
386 memset(&msg, 0, sizeof(msg));
387 if (lttng_strncpy(msg.channel_name, channel_name,
388 sizeof(msg.channel_name))) {
389 ret = -1;
390 goto error;
391 }
392
393 if (lttng_strncpy(msg.pathname, pathname,
394 sizeof(msg.pathname))) {
395 ret = -1;
396 goto error;
397 }
398
399 /* Send command */
400 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
401 if (ret < 0) {
402 ret = -1;
403 goto error;
404 }
405 ret = 0;
406 error:
407 return ret;
408 }
409
410 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock *rsock,
411 const char *channel_name, const char *pathname,
412 uint64_t tracefile_size, uint64_t tracefile_count)
413 {
414 int ret;
415 struct lttcomm_relayd_add_stream_2_2 msg;
416
417 memset(&msg, 0, sizeof(msg));
418 /* Compat with relayd 2.2 to 2.10 */
419 if (lttng_strncpy(msg.channel_name, channel_name,
420 sizeof(msg.channel_name))) {
421 ret = -1;
422 goto error;
423 }
424 if (lttng_strncpy(msg.pathname, pathname,
425 sizeof(msg.pathname))) {
426 ret = -1;
427 goto error;
428 }
429 msg.tracefile_size = htobe64(tracefile_size);
430 msg.tracefile_count = htobe64(tracefile_count);
431
432 /* Send command */
433 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0);
434 if (ret < 0) {
435 goto error;
436 }
437 ret = 0;
438 error:
439 return ret;
440 }
441
442 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock *rsock,
443 const char *channel_name, const char *pathname,
444 uint64_t tracefile_size, uint64_t tracefile_count,
445 uint64_t trace_archive_id)
446 {
447 int ret;
448 struct lttcomm_relayd_add_stream_2_11 *msg = NULL;
449 size_t channel_name_len;
450 size_t pathname_len;
451 size_t msg_length;
452
453 /* The two names are sent with a '\0' delimiter between them. */
454 channel_name_len = strlen(channel_name) + 1;
455 pathname_len = strlen(pathname) + 1;
456
457 msg_length = sizeof(*msg) + channel_name_len + pathname_len;
458 msg = zmalloc(msg_length);
459 if (!msg) {
460 PERROR("zmalloc add_stream_2_11 command message");
461 ret = -1;
462 goto error;
463 }
464
465 assert(channel_name_len <= UINT32_MAX);
466 msg->channel_name_len = htobe32(channel_name_len);
467
468 assert(pathname_len <= UINT32_MAX);
469 msg->pathname_len = htobe32(pathname_len);
470
471 if (lttng_strncpy(msg->names, channel_name, channel_name_len)) {
472 ret = -1;
473 goto error;
474 }
475 if (lttng_strncpy(msg->names + channel_name_len, pathname, pathname_len)) {
476 ret = -1;
477 goto error;
478 }
479
480 msg->tracefile_size = htobe64(tracefile_size);
481 msg->tracefile_count = htobe64(tracefile_count);
482 msg->trace_chunk_id = htobe64(trace_archive_id);
483
484 /* Send command */
485 ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) msg, msg_length, 0);
486 if (ret < 0) {
487 goto error;
488 }
489 ret = 0;
490 error:
491 free(msg);
492 return ret;
493 }
494
495 /*
496 * Add stream on the relayd and assign stream handle to the stream_id argument.
497 *
498 * Chunks are not supported by relayd prior to 2.11, but are used to
499 * internally between session daemon and consumer daemon to keep track
500 * of the channel and stream output path.
501 *
502 * On success return 0 else return ret_code negative value.
503 */
504 int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name,
505 const char *domain_name, const char *_pathname, uint64_t *stream_id,
506 uint64_t tracefile_size, uint64_t tracefile_count,
507 struct lttng_trace_chunk *trace_chunk)
508 {
509 int ret;
510 struct lttcomm_relayd_status_stream reply;
511 char pathname[RELAYD_COMM_LTTNG_PATH_MAX];
512 char *separator;
513
514 /* Code flow error. Safety net. */
515 assert(rsock);
516 assert(channel_name);
517 assert(domain_name);
518 assert(_pathname);
519 assert(trace_chunk);
520
521 DBG("Relayd adding stream for channel name %s", channel_name);
522
523 if (_pathname[0] == '\0') {
524 separator = "";
525 } else {
526 separator = "/";
527 }
528 ret = snprintf(pathname, RELAYD_COMM_LTTNG_PATH_MAX, "%s%s%s",
529 domain_name, separator, _pathname);
530 if (ret <= 0 || ret >= RELAYD_COMM_LTTNG_PATH_MAX) {
531 ERR("stream path too long.");
532 ret = -1;
533 goto error;
534 }
535
536 /* Compat with relayd 2.1 */
537 if (rsock->minor == 1) {
538 /* For 2.1 */
539 ret = relayd_add_stream_2_1(rsock, channel_name, pathname);
540
541 } else if (rsock->minor > 1 && rsock->minor < 11) {
542 /* From 2.2 to 2.10 */
543 ret = relayd_add_stream_2_2(rsock, channel_name, pathname,
544 tracefile_size, tracefile_count);
545 } else {
546 enum lttng_trace_chunk_status chunk_status;
547 uint64_t chunk_id;
548
549 chunk_status = lttng_trace_chunk_get_id(trace_chunk,
550 &chunk_id);
551 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
552
553 /* From 2.11 to ...*/
554 ret = relayd_add_stream_2_11(rsock, channel_name, pathname,
555 tracefile_size, tracefile_count,
556 chunk_id);
557 }
558
559 if (ret) {
560 ret = -1;
561 goto error;
562 }
563
564 /* Waiting for reply */
565 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
566 if (ret < 0) {
567 goto error;
568 }
569
570 /* Back to host bytes order. */
571 reply.handle = be64toh(reply.handle);
572 reply.ret_code = be32toh(reply.ret_code);
573
574 /* Return session id or negative ret code. */
575 if (reply.ret_code != LTTNG_OK) {
576 ret = -1;
577 ERR("Relayd add stream replied error %d", reply.ret_code);
578 } else {
579 /* Success */
580 ret = 0;
581 *stream_id = reply.handle;
582 }
583
584 DBG("Relayd stream added successfully with handle %" PRIu64,
585 reply.handle);
586
587 error:
588 return ret;
589 }
590
591 /*
592 * Inform the relay that all the streams for the current channel has been sent.
593 *
594 * On success return 0 else return ret_code negative value.
595 */
596 int relayd_streams_sent(struct lttcomm_relayd_sock *rsock)
597 {
598 int ret;
599 struct lttcomm_relayd_generic_reply reply;
600
601 /* Code flow error. Safety net. */
602 assert(rsock);
603
604 DBG("Relayd sending streams sent.");
605
606 /* This feature was introduced in 2.4, ignore it for earlier versions. */
607 if (rsock->minor < 4) {
608 ret = 0;
609 goto end;
610 }
611
612 /* Send command */
613 ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0);
614 if (ret < 0) {
615 goto error;
616 }
617
618 /* Waiting for reply */
619 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
620 if (ret < 0) {
621 goto error;
622 }
623
624 /* Back to host bytes order. */
625 reply.ret_code = be32toh(reply.ret_code);
626
627 /* Return session id or negative ret code. */
628 if (reply.ret_code != LTTNG_OK) {
629 ret = -1;
630 ERR("Relayd streams sent replied error %d", reply.ret_code);
631 goto error;
632 } else {
633 /* Success */
634 ret = 0;
635 }
636
637 DBG("Relayd streams sent success");
638
639 error:
640 end:
641 return ret;
642 }
643
644 /*
645 * Check version numbers on the relayd.
646 * If major versions are compatible, we assign minor_to_use to the
647 * minor version of the procotol we are going to use for this session.
648 *
649 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
650 * otherwise, or a negative value on network errors.
651 */
652 int relayd_version_check(struct lttcomm_relayd_sock *rsock)
653 {
654 int ret;
655 struct lttcomm_relayd_version msg;
656
657 /* Code flow error. Safety net. */
658 assert(rsock);
659
660 DBG("Relayd version check for major.minor %u.%u", rsock->major,
661 rsock->minor);
662
663 memset(&msg, 0, sizeof(msg));
664 /* Prepare network byte order before transmission. */
665 msg.major = htobe32(rsock->major);
666 msg.minor = htobe32(rsock->minor);
667
668 /* Send command */
669 ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0);
670 if (ret < 0) {
671 goto error;
672 }
673
674 /* Receive response */
675 ret = recv_reply(rsock, (void *) &msg, sizeof(msg));
676 if (ret < 0) {
677 goto error;
678 }
679
680 /* Set back to host bytes order */
681 msg.major = be32toh(msg.major);
682 msg.minor = be32toh(msg.minor);
683
684 /*
685 * Only validate the major version. If the other side is higher,
686 * communication is not possible. Only major version equal can talk to each
687 * other. If the minor version differs, the lowest version is used by both
688 * sides.
689 */
690 if (msg.major != rsock->major) {
691 /* Not compatible */
692 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
693 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
694 msg.major, rsock->major);
695 goto error;
696 }
697
698 /*
699 * If the relayd's minor version is higher, it will adapt to our version so
700 * we can continue to use the latest relayd communication data structure.
701 * If the received minor version is higher, the relayd should adapt to us.
702 */
703 if (rsock->minor > msg.minor) {
704 rsock->minor = msg.minor;
705 }
706
707 /* Version number compatible */
708 DBG2("Relayd version is compatible, using protocol version %u.%u",
709 rsock->major, rsock->minor);
710 ret = 0;
711
712 error:
713 return ret;
714 }
715
716 /*
717 * Add stream on the relayd and assign stream handle to the stream_id argument.
718 *
719 * On success return 0 else return ret_code negative value.
720 */
721 int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len)
722 {
723 int ret;
724
725 /* Code flow error. Safety net. */
726 assert(rsock);
727
728 DBG("Relayd sending metadata of size %zu", len);
729
730 /* Send command */
731 ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0);
732 if (ret < 0) {
733 goto error;
734 }
735
736 DBG2("Relayd metadata added successfully");
737
738 /*
739 * After that call, the metadata data MUST be sent to the relayd so the
740 * receive size on the other end matches the len of the metadata packet
741 * header. This is why we don't wait for a reply here.
742 */
743
744 error:
745 return ret;
746 }
747
748 /*
749 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
750 */
751 int relayd_connect(struct lttcomm_relayd_sock *rsock)
752 {
753 /* Code flow error. Safety net. */
754 assert(rsock);
755
756 if (!rsock->sock.ops) {
757 /*
758 * Attempting a connect on a non-initialized socket.
759 */
760 return -ECONNRESET;
761 }
762
763 DBG3("Relayd connect ...");
764
765 return rsock->sock.ops->connect(&rsock->sock);
766 }
767
768 /*
769 * Close relayd socket with an allocated lttcomm_relayd_sock.
770 *
771 * If no socket operations are found, simply return 0 meaning that everything
772 * is fine. Without operations, the socket can not possibly be opened or used.
773 * This is possible if the socket was allocated but not created. However, the
774 * caller could simply use it to store a valid file descriptor for instance
775 * passed over a Unix socket and call this to cleanup but still without a valid
776 * ops pointer.
777 *
778 * Return the close returned value. On error, a negative value is usually
779 * returned back from close(2).
780 */
781 int relayd_close(struct lttcomm_relayd_sock *rsock)
782 {
783 int ret;
784
785 /* Code flow error. Safety net. */
786 assert(rsock);
787
788 /* An invalid fd is fine, return success. */
789 if (rsock->sock.fd < 0) {
790 ret = 0;
791 goto end;
792 }
793
794 DBG3("Relayd closing socket %d", rsock->sock.fd);
795
796 if (rsock->sock.ops) {
797 ret = rsock->sock.ops->close(&rsock->sock);
798 } else {
799 /* Default call if no specific ops found. */
800 ret = close(rsock->sock.fd);
801 if (ret < 0) {
802 PERROR("relayd_close default close");
803 }
804 }
805 rsock->sock.fd = -1;
806
807 end:
808 return ret;
809 }
810
811 /*
812 * Send data header structure to the relayd.
813 */
814 int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock,
815 struct lttcomm_relayd_data_hdr *hdr, size_t size)
816 {
817 int ret;
818
819 /* Code flow error. Safety net. */
820 assert(rsock);
821 assert(hdr);
822
823 if (rsock->sock.fd < 0) {
824 return -ECONNRESET;
825 }
826
827 DBG3("Relayd sending data header of size %zu", size);
828
829 /* Again, safety net */
830 if (size == 0) {
831 size = sizeof(struct lttcomm_relayd_data_hdr);
832 }
833
834 /* Only send data header. */
835 ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0);
836 if (ret < 0) {
837 ret = -errno;
838 goto error;
839 }
840
841 /*
842 * The data MUST be sent right after that command for the receive on the
843 * other end to match the size in the header.
844 */
845
846 error:
847 return ret;
848 }
849
850 /*
851 * Send close stream command to the relayd.
852 */
853 int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
854 uint64_t last_net_seq_num)
855 {
856 int ret;
857 struct lttcomm_relayd_close_stream msg;
858 struct lttcomm_relayd_generic_reply reply;
859
860 /* Code flow error. Safety net. */
861 assert(rsock);
862
863 DBG("Relayd closing stream id %" PRIu64, stream_id);
864
865 memset(&msg, 0, sizeof(msg));
866 msg.stream_id = htobe64(stream_id);
867 msg.last_net_seq_num = htobe64(last_net_seq_num);
868
869 /* Send command */
870 ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0);
871 if (ret < 0) {
872 goto error;
873 }
874
875 /* Receive response */
876 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
877 if (ret < 0) {
878 goto error;
879 }
880
881 reply.ret_code = be32toh(reply.ret_code);
882
883 /* Return session id or negative ret code. */
884 if (reply.ret_code != LTTNG_OK) {
885 ret = -1;
886 ERR("Relayd close stream replied error %d", reply.ret_code);
887 } else {
888 /* Success */
889 ret = 0;
890 }
891
892 DBG("Relayd close stream id %" PRIu64 " successfully", stream_id);
893
894 error:
895 return ret;
896 }
897
898 /*
899 * Check for data availability for a given stream id.
900 *
901 * Return 0 if NOT pending, 1 if so and a negative value on error.
902 */
903 int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id,
904 uint64_t last_net_seq_num)
905 {
906 int ret;
907 struct lttcomm_relayd_data_pending msg;
908 struct lttcomm_relayd_generic_reply reply;
909
910 /* Code flow error. Safety net. */
911 assert(rsock);
912
913 DBG("Relayd data pending for stream id %" PRIu64, stream_id);
914
915 memset(&msg, 0, sizeof(msg));
916 msg.stream_id = htobe64(stream_id);
917 msg.last_net_seq_num = htobe64(last_net_seq_num);
918
919 /* Send command */
920 ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg,
921 sizeof(msg), 0);
922 if (ret < 0) {
923 goto error;
924 }
925
926 /* Receive response */
927 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
928 if (ret < 0) {
929 goto error;
930 }
931
932 reply.ret_code = be32toh(reply.ret_code);
933
934 /* Return session id or negative ret code. */
935 if (reply.ret_code >= LTTNG_OK) {
936 ERR("Relayd data pending replied error %d", reply.ret_code);
937 }
938
939 /* At this point, the ret code is either 1 or 0 */
940 ret = reply.ret_code;
941
942 DBG("Relayd data is %s pending for stream id %" PRIu64,
943 ret == 1 ? "" : "NOT", stream_id);
944
945 error:
946 return ret;
947 }
948
949 /*
950 * Check on the relayd side for a quiescent state on the control socket.
951 */
952 int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock,
953 uint64_t metadata_stream_id)
954 {
955 int ret;
956 struct lttcomm_relayd_quiescent_control msg;
957 struct lttcomm_relayd_generic_reply reply;
958
959 /* Code flow error. Safety net. */
960 assert(rsock);
961
962 DBG("Relayd checking quiescent control state");
963
964 memset(&msg, 0, sizeof(msg));
965 msg.stream_id = htobe64(metadata_stream_id);
966
967 /* Send command */
968 ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
969 if (ret < 0) {
970 goto error;
971 }
972
973 /* Receive response */
974 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
975 if (ret < 0) {
976 goto error;
977 }
978
979 reply.ret_code = be32toh(reply.ret_code);
980
981 /* Return session id or negative ret code. */
982 if (reply.ret_code != LTTNG_OK) {
983 ret = -1;
984 ERR("Relayd quiescent control replied error %d", reply.ret_code);
985 goto error;
986 }
987
988 /* Control socket is quiescent */
989 return 0;
990
991 error:
992 return ret;
993 }
994
995 /*
996 * Begin a data pending command for a specific session id.
997 */
998 int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id)
999 {
1000 int ret;
1001 struct lttcomm_relayd_begin_data_pending msg;
1002 struct lttcomm_relayd_generic_reply reply;
1003
1004 /* Code flow error. Safety net. */
1005 assert(rsock);
1006
1007 DBG("Relayd begin data pending");
1008
1009 memset(&msg, 0, sizeof(msg));
1010 msg.session_id = htobe64(id);
1011
1012 /* Send command */
1013 ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0);
1014 if (ret < 0) {
1015 goto error;
1016 }
1017
1018 /* Receive response */
1019 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1020 if (ret < 0) {
1021 goto error;
1022 }
1023
1024 reply.ret_code = be32toh(reply.ret_code);
1025
1026 /* Return session id or negative ret code. */
1027 if (reply.ret_code != LTTNG_OK) {
1028 ret = -1;
1029 ERR("Relayd begin data pending replied error %d", reply.ret_code);
1030 goto error;
1031 }
1032
1033 return 0;
1034
1035 error:
1036 return ret;
1037 }
1038
1039 /*
1040 * End a data pending command for a specific session id.
1041 *
1042 * Return 0 on success and set is_data_inflight to 0 if no data is being
1043 * streamed or 1 if it is the case.
1044 */
1045 int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id,
1046 unsigned int *is_data_inflight)
1047 {
1048 int ret, recv_ret;
1049 struct lttcomm_relayd_end_data_pending msg;
1050 struct lttcomm_relayd_generic_reply reply;
1051
1052 /* Code flow error. Safety net. */
1053 assert(rsock);
1054
1055 DBG("Relayd end data pending");
1056
1057 memset(&msg, 0, sizeof(msg));
1058 msg.session_id = htobe64(id);
1059
1060 /* Send command */
1061 ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0);
1062 if (ret < 0) {
1063 goto error;
1064 }
1065
1066 /* Receive response */
1067 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1068 if (ret < 0) {
1069 goto error;
1070 }
1071
1072 recv_ret = be32toh(reply.ret_code);
1073 if (recv_ret < 0) {
1074 ret = recv_ret;
1075 goto error;
1076 }
1077
1078 *is_data_inflight = recv_ret;
1079
1080 DBG("Relayd end data pending is data inflight: %d", recv_ret);
1081
1082 return 0;
1083
1084 error:
1085 return ret;
1086 }
1087
1088 /*
1089 * Send index to the relayd.
1090 */
1091 int relayd_send_index(struct lttcomm_relayd_sock *rsock,
1092 struct ctf_packet_index *index, uint64_t relay_stream_id,
1093 uint64_t net_seq_num)
1094 {
1095 int ret;
1096 struct lttcomm_relayd_index msg;
1097 struct lttcomm_relayd_generic_reply reply;
1098
1099 /* Code flow error. Safety net. */
1100 assert(rsock);
1101
1102 if (rsock->minor < 4) {
1103 DBG("Not sending indexes before protocol 2.4");
1104 ret = 0;
1105 goto error;
1106 }
1107
1108 DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id);
1109
1110 memset(&msg, 0, sizeof(msg));
1111 msg.relay_stream_id = htobe64(relay_stream_id);
1112 msg.net_seq_num = htobe64(net_seq_num);
1113
1114 /* The index is already in big endian. */
1115 msg.packet_size = index->packet_size;
1116 msg.content_size = index->content_size;
1117 msg.timestamp_begin = index->timestamp_begin;
1118 msg.timestamp_end = index->timestamp_end;
1119 msg.events_discarded = index->events_discarded;
1120 msg.stream_id = index->stream_id;
1121
1122 if (rsock->minor >= 8) {
1123 msg.stream_instance_id = index->stream_instance_id;
1124 msg.packet_seq_num = index->packet_seq_num;
1125 }
1126
1127 /* Send command */
1128 ret = send_command(rsock, RELAYD_SEND_INDEX, &msg,
1129 lttcomm_relayd_index_len(lttng_to_index_major(rsock->major,
1130 rsock->minor),
1131 lttng_to_index_minor(rsock->major, rsock->minor)),
1132 0);
1133 if (ret < 0) {
1134 goto error;
1135 }
1136
1137 /* Receive response */
1138 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1139 if (ret < 0) {
1140 goto error;
1141 }
1142
1143 reply.ret_code = be32toh(reply.ret_code);
1144
1145 /* Return session id or negative ret code. */
1146 if (reply.ret_code != LTTNG_OK) {
1147 ret = -1;
1148 ERR("Relayd send index replied error %d", reply.ret_code);
1149 } else {
1150 /* Success */
1151 ret = 0;
1152 }
1153
1154 error:
1155 return ret;
1156 }
1157
1158 /*
1159 * Ask the relay to reset the metadata trace file (regeneration).
1160 */
1161 int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock,
1162 uint64_t stream_id, uint64_t version)
1163 {
1164 int ret;
1165 struct lttcomm_relayd_reset_metadata msg;
1166 struct lttcomm_relayd_generic_reply reply;
1167
1168 /* Code flow error. Safety net. */
1169 assert(rsock);
1170
1171 /* Should have been prevented by the sessiond. */
1172 if (rsock->minor < 8) {
1173 ERR("Metadata regeneration unsupported before 2.8");
1174 ret = -1;
1175 goto error;
1176 }
1177
1178 DBG("Relayd reset metadata stream id %" PRIu64, stream_id);
1179
1180 memset(&msg, 0, sizeof(msg));
1181 msg.stream_id = htobe64(stream_id);
1182 msg.version = htobe64(version);
1183
1184 /* Send command */
1185 ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0);
1186 if (ret < 0) {
1187 goto error;
1188 }
1189
1190 /* Receive response */
1191 ret = recv_reply(rsock, (void *) &reply, sizeof(reply));
1192 if (ret < 0) {
1193 goto error;
1194 }
1195
1196 reply.ret_code = be32toh(reply.ret_code);
1197
1198 /* Return session id or negative ret code. */
1199 if (reply.ret_code != LTTNG_OK) {
1200 ret = -1;
1201 ERR("Relayd reset metadata replied error %d", reply.ret_code);
1202 } else {
1203 /* Success */
1204 ret = 0;
1205 }
1206
1207 DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id);
1208
1209 error:
1210 return ret;
1211 }
1212
1213 int relayd_rotate_streams(struct lttcomm_relayd_sock *sock,
1214 unsigned int stream_count, const uint64_t *new_chunk_id,
1215 const struct relayd_stream_rotation_position *positions)
1216 {
1217 int ret;
1218 unsigned int i;
1219 struct lttng_dynamic_buffer payload;
1220 struct lttcomm_relayd_generic_reply reply = {};
1221 const struct lttcomm_relayd_rotate_streams msg = {
1222 .stream_count = htobe32((uint32_t) stream_count),
1223 .new_chunk_id = (typeof(msg.new_chunk_id)) {
1224 .is_set = !!new_chunk_id,
1225 .value = htobe64(new_chunk_id ? *new_chunk_id : 0),
1226 },
1227 };
1228 char new_chunk_id_buf[MAX_INT_DEC_LEN(*new_chunk_id)] = {};
1229 const char *new_chunk_id_str;
1230
1231 if (!relayd_supports_chunks(sock)) {
1232 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1233 return 0;
1234 }
1235
1236 lttng_dynamic_buffer_init(&payload);
1237
1238 /* Code flow error. Safety net. */
1239 assert(sock);
1240
1241 if (new_chunk_id) {
1242 ret = snprintf(new_chunk_id_buf, sizeof(new_chunk_id_buf),
1243 "%" PRIu64, *new_chunk_id);
1244 if (ret == -1 || ret >= sizeof(new_chunk_id_buf)) {
1245 new_chunk_id_str = "formatting error";
1246 } else {
1247 new_chunk_id_str = new_chunk_id_buf;
1248 }
1249 } else {
1250 new_chunk_id_str = "none";
1251 }
1252
1253 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1254 new_chunk_id_str, stream_count);
1255
1256 ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg));
1257 if (ret) {
1258 ERR("Failed to allocate \"rotate streams\" command payload");
1259 goto error;
1260 }
1261
1262 for (i = 0; i < stream_count; i++) {
1263 const struct relayd_stream_rotation_position *position =
1264 &positions[i];
1265 const struct lttcomm_relayd_stream_rotation_position comm_position = {
1266 .stream_id = htobe64(position->stream_id),
1267 .rotate_at_seq_num = htobe64(
1268 position->rotate_at_seq_num),
1269 };
1270
1271 DBG("Rotate stream %" PRIu64 "at sequence number %" PRIu64,
1272 position->stream_id,
1273 position->rotate_at_seq_num);
1274 ret = lttng_dynamic_buffer_append(&payload, &comm_position,
1275 sizeof(comm_position));
1276 if (ret) {
1277 ERR("Failed to allocate \"rotate streams\" command payload");
1278 goto error;
1279 }
1280 }
1281
1282 /* Send command. */
1283 ret = send_command(sock, RELAYD_ROTATE_STREAMS, payload.data,
1284 payload.size, 0);
1285 if (ret < 0) {
1286 ERR("Failed to send \"rotate stream\" command");
1287 goto error;
1288 }
1289
1290 /* Receive response. */
1291 ret = recv_reply(sock, &reply, sizeof(reply));
1292 if (ret < 0) {
1293 ERR("Failed to receive \"rotate streams\" command reply");
1294 goto error;
1295 }
1296
1297 reply.ret_code = be32toh(reply.ret_code);
1298 if (reply.ret_code != LTTNG_OK) {
1299 ret = -1;
1300 ERR("Relayd rotate streams replied error %d", reply.ret_code);
1301 } else {
1302 /* Success. */
1303 ret = 0;
1304 DBG("Relayd rotated streams successfully");
1305 }
1306
1307 error:
1308 lttng_dynamic_buffer_reset(&payload);
1309 return ret;
1310 }
1311
1312 int relayd_create_trace_chunk(struct lttcomm_relayd_sock *sock,
1313 struct lttng_trace_chunk *chunk)
1314 {
1315 int ret = 0;
1316 enum lttng_trace_chunk_status status;
1317 struct lttcomm_relayd_create_trace_chunk msg = {};
1318 struct lttcomm_relayd_generic_reply reply = {};
1319 struct lttng_dynamic_buffer payload;
1320 uint64_t chunk_id;
1321 time_t creation_timestamp;
1322 const char *chunk_name;
1323 size_t chunk_name_length;
1324 bool overridden_name;
1325
1326 lttng_dynamic_buffer_init(&payload);
1327
1328 if (!relayd_supports_chunks(sock)) {
1329 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1330 goto end;
1331 }
1332
1333 status = lttng_trace_chunk_get_id(chunk, &chunk_id);
1334 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1335 ret = -1;
1336 goto end;
1337 }
1338
1339 status = lttng_trace_chunk_get_creation_timestamp(
1340 chunk, &creation_timestamp);
1341 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1342 ret = -1;
1343 goto end;
1344 }
1345
1346 status = lttng_trace_chunk_get_name(
1347 chunk, &chunk_name, &overridden_name);
1348 if (status != LTTNG_TRACE_CHUNK_STATUS_OK &&
1349 status != LTTNG_TRACE_CHUNK_STATUS_NONE) {
1350 ret = -1;
1351 goto end;
1352 }
1353
1354 chunk_name_length = overridden_name ? (strlen(chunk_name) + 1) : 0;
1355 msg = (typeof(msg)){
1356 .chunk_id = htobe64(chunk_id),
1357 .creation_timestamp = htobe64((uint64_t) creation_timestamp),
1358 .override_name_length = htobe32((uint32_t) chunk_name_length),
1359 };
1360
1361 ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg));
1362 if (ret) {
1363 goto end;
1364 }
1365 if (chunk_name_length) {
1366 ret = lttng_dynamic_buffer_append(
1367 &payload, chunk_name, chunk_name_length);
1368 if (ret) {
1369 goto end;
1370 }
1371 }
1372
1373 ret = send_command(sock, RELAYD_CREATE_TRACE_CHUNK, payload.data,
1374 payload.size, 0);
1375 if (ret < 0) {
1376 ERR("Failed to send trace chunk creation command to relay daemon");
1377 goto end;
1378 }
1379
1380 ret = recv_reply(sock, &reply, sizeof(reply));
1381 if (ret < 0) {
1382 ERR("Failed to receive relay daemon trace chunk creation command reply");
1383 goto end;
1384 }
1385
1386 reply.ret_code = be32toh(reply.ret_code);
1387 if (reply.ret_code != LTTNG_OK) {
1388 ret = -1;
1389 ERR("Relayd trace chunk create replied error %d",
1390 reply.ret_code);
1391 } else {
1392 ret = 0;
1393 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64,
1394 chunk_id);
1395 }
1396
1397 end:
1398 lttng_dynamic_buffer_reset(&payload);
1399 return ret;
1400 }
1401
1402 int relayd_close_trace_chunk(struct lttcomm_relayd_sock *sock,
1403 struct lttng_trace_chunk *chunk,
1404 char *path)
1405 {
1406 int ret = 0;
1407 enum lttng_trace_chunk_status status;
1408 struct lttcomm_relayd_close_trace_chunk msg = {};
1409 struct lttcomm_relayd_close_trace_chunk_reply reply = {};
1410 uint64_t chunk_id;
1411 time_t close_timestamp;
1412 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {};
1413
1414 if (!relayd_supports_chunks(sock)) {
1415 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1416 goto end;
1417 }
1418
1419 status = lttng_trace_chunk_get_id(chunk, &chunk_id);
1420 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1421 ERR("Failed to get trace chunk id");
1422 ret = -1;
1423 goto end;
1424 }
1425
1426 status = lttng_trace_chunk_get_close_timestamp(chunk, &close_timestamp);
1427 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1428 ERR("Failed to get trace chunk close timestamp");
1429 ret = -1;
1430 goto end;
1431 }
1432
1433 status = lttng_trace_chunk_get_close_command(chunk,
1434 &close_command.value);
1435 switch (status) {
1436 case LTTNG_TRACE_CHUNK_STATUS_OK:
1437 close_command.is_set = 1;
1438 break;
1439 case LTTNG_TRACE_CHUNK_STATUS_NONE:
1440 break;
1441 default:
1442 ERR("Failed to get trace chunk close command");
1443 ret = -1;
1444 goto end;
1445 }
1446
1447 msg = (typeof(msg)){
1448 .chunk_id = htobe64(chunk_id),
1449 .close_timestamp = htobe64((uint64_t) close_timestamp),
1450 .close_command = {
1451 .value = htobe32((uint32_t) close_command.value),
1452 .is_set = close_command.is_set,
1453 },
1454 };
1455
1456 ret = send_command(sock, RELAYD_CLOSE_TRACE_CHUNK, &msg, sizeof(msg),
1457 0);
1458 if (ret < 0) {
1459 ERR("Failed to send trace chunk close command to relay daemon");
1460 goto end;
1461 }
1462
1463 ret = recv_reply(sock, &reply, sizeof(reply));
1464 if (ret < 0) {
1465 ERR("Failed to receive relay daemon trace chunk close command reply");
1466 goto end;
1467 }
1468
1469 reply.path_length = be32toh(reply.path_length);
1470 if (reply.path_length >= LTTNG_PATH_MAX) {
1471 ERR("Chunk path too long");
1472 ret = -1;
1473 goto end;
1474 }
1475
1476 ret = recv_reply(sock, path, reply.path_length);
1477 if (ret < 0) {
1478 ERR("Failed to receive relay daemon trace chunk close command reply");
1479 goto end;
1480 }
1481 if (path[reply.path_length - 1] != '\0') {
1482 ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)");
1483 ret = -1;
1484 goto end;
1485 }
1486
1487 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1488 if (reply.generic.ret_code != LTTNG_OK) {
1489 ret = -1;
1490 ERR("Relayd trace chunk close replied error %d",
1491 reply.generic.ret_code);
1492 } else {
1493 ret = 0;
1494 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64,
1495 chunk_id);
1496 }
1497 end:
1498 return ret;
1499 }
1500
1501 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock *sock,
1502 uint64_t chunk_id, bool *chunk_exists)
1503 {
1504 int ret = 0;
1505 struct lttcomm_relayd_trace_chunk_exists msg = {};
1506 struct lttcomm_relayd_trace_chunk_exists_reply reply = {};
1507
1508 if (!relayd_supports_chunks(sock)) {
1509 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1510 /* The chunk will never exist */
1511 *chunk_exists = false;
1512 goto end;
1513 }
1514
1515 msg = (typeof(msg)){
1516 .chunk_id = htobe64(chunk_id),
1517 };
1518
1519 ret = send_command(sock, RELAYD_TRACE_CHUNK_EXISTS, &msg, sizeof(msg),
1520 0);
1521 if (ret < 0) {
1522 ERR("Failed to send trace chunk exists command to relay daemon");
1523 goto end;
1524 }
1525
1526 ret = recv_reply(sock, &reply, sizeof(reply));
1527 if (ret < 0) {
1528 ERR("Failed to receive relay daemon trace chunk close command reply");
1529 goto end;
1530 }
1531
1532 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1533 if (reply.generic.ret_code != LTTNG_OK) {
1534 ret = -1;
1535 ERR("Relayd trace chunk close replied error %d",
1536 reply.generic.ret_code);
1537 } else {
1538 ret = 0;
1539 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1540 ", exists = %s", chunk_id,
1541 reply.trace_chunk_exists ? "true" : "false");
1542 *chunk_exists = !!reply.trace_chunk_exists;
1543 }
1544 end:
1545 return ret;
1546 }
1547
1548 int relayd_get_configuration(struct lttcomm_relayd_sock *sock,
1549 uint64_t query_flags,
1550 uint64_t *result_flags)
1551 {
1552 int ret = 0;
1553 struct lttcomm_relayd_get_configuration msg = (typeof(msg)) {
1554 .query_flags = htobe64(query_flags),
1555 };
1556 struct lttcomm_relayd_get_configuration_reply reply = {};
1557
1558 if (!relayd_supports_get_configuration(sock)) {
1559 DBG("Refusing to get relayd configuration (unsupported by relayd)");
1560 if (query_flags) {
1561 ret = -1;
1562 goto end;
1563 }
1564 *result_flags = 0;
1565 goto end;
1566 }
1567
1568 ret = send_command(sock, RELAYD_GET_CONFIGURATION, &msg, sizeof(msg),
1569 0);
1570 if (ret < 0) {
1571 ERR("Failed to send get configuration command to relay daemon");
1572 goto end;
1573 }
1574
1575 ret = recv_reply(sock, &reply, sizeof(reply));
1576 if (ret < 0) {
1577 ERR("Failed to receive relay daemon get configuration command reply");
1578 goto end;
1579 }
1580
1581 reply.generic.ret_code = be32toh(reply.generic.ret_code);
1582 if (reply.generic.ret_code != LTTNG_OK) {
1583 ret = -1;
1584 ERR("Relayd get configuration replied error %d",
1585 reply.generic.ret_code);
1586 } else {
1587 reply.relayd_configuration_flags =
1588 be64toh(reply.relayd_configuration_flags);
1589 ret = 0;
1590 DBG("Relayd successfully got configuration: query_flags = %" PRIu64
1591 ", results_flags = %" PRIu64, query_flags,
1592 reply.relayd_configuration_flags);
1593 *result_flags = reply.relayd_configuration_flags;
1594 }
1595 end:
1596 return ret;
1597 }
This page took 0.10969 seconds and 5 git commands to generate.