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