2 * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com>
3 * Copyright 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_COMP_LOG_SELF_COMP (viewer_connection->self_comp)
25 #define BT_LOG_OUTPUT_LEVEL (viewer_connection->log_level)
26 #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/VIEWER"
27 #include "logging/comp-logging.h"
36 #include <sys/types.h>
39 #include "compat/socket.h"
40 #include "compat/endian.h"
41 #include "compat/compiler.h"
42 #include "common/common.h"
43 #include <babeltrace2/babeltrace.h>
45 #include "lttng-live.h"
46 #include "viewer-connection.h"
47 #include "lttng-viewer-abi.h"
48 #include "data-stream.h"
52 ssize_t
lttng_live_recv(struct live_viewer_connection
*viewer_connection
,
53 void *buf
, size_t len
)
56 size_t copied
= 0, to_copy
= len
;
57 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
58 viewer_connection
->lttng_live_msg_iter
;
59 BT_SOCKET sock
= viewer_connection
->control_sock
;
62 ret
= bt_socket_recv(sock
, buf
+ copied
, to_copy
, 0);
64 BT_ASSERT(ret
<= to_copy
);
68 if (ret
== BT_SOCKET_ERROR
&& bt_socket_interrupted()) {
69 if (!viewer_connection
->in_query
&&
70 lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
76 } while (ret
> 0 && to_copy
> 0);
79 /* ret = 0 means orderly shutdown, ret == BT_SOCKET_ERROR is error. */
84 ssize_t
lttng_live_send(struct live_viewer_connection
*viewer_connection
,
85 const void *buf
, size_t len
)
87 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
88 viewer_connection
->lttng_live_msg_iter
;
89 BT_SOCKET sock
= viewer_connection
->control_sock
;
93 ret
= bt_socket_send_nosigpipe(sock
, buf
, len
);
94 if (ret
== BT_SOCKET_ERROR
&& bt_socket_interrupted()) {
95 if (!viewer_connection
->in_query
&&
96 lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
109 int parse_url(struct live_viewer_connection
*viewer_connection
)
111 char error_buf
[256] = { 0 };
112 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
114 const char *path
= viewer_connection
->url
->str
;
120 lttng_live_url_parts
= bt_common_parse_lttng_live_url(path
,
121 error_buf
, sizeof(error_buf
));
122 if (!lttng_live_url_parts
.proto
) {
123 BT_COMP_LOGW("Invalid LTTng live URL format: %s", error_buf
);
127 viewer_connection
->relay_hostname
=
128 lttng_live_url_parts
.hostname
;
129 lttng_live_url_parts
.hostname
= NULL
;
131 if (lttng_live_url_parts
.port
>= 0) {
132 viewer_connection
->port
= lttng_live_url_parts
.port
;
134 viewer_connection
->port
= LTTNG_DEFAULT_NETWORK_VIEWER_PORT
;
137 viewer_connection
->target_hostname
=
138 lttng_live_url_parts
.target_hostname
;
139 lttng_live_url_parts
.target_hostname
= NULL
;
141 if (lttng_live_url_parts
.session_name
) {
142 viewer_connection
->session_name
=
143 lttng_live_url_parts
.session_name
;
144 lttng_live_url_parts
.session_name
= NULL
;
147 BT_COMP_LOGI("Connecting to hostname : %s, port : %d, "
148 "target hostname : %s, session name : %s, "
150 viewer_connection
->relay_hostname
->str
,
151 viewer_connection
->port
,
152 !viewer_connection
->target_hostname
?
153 "<none>" : viewer_connection
->target_hostname
->str
,
154 !viewer_connection
->session_name
?
155 "<none>" : viewer_connection
->session_name
->str
,
156 lttng_live_url_parts
.proto
->str
);
160 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
165 int lttng_live_handshake(struct live_viewer_connection
*viewer_connection
)
167 struct lttng_viewer_cmd cmd
;
168 struct lttng_viewer_connect connect
;
169 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(connect
);
170 char cmd_buf
[cmd_buf_len
];
174 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
175 cmd
.data_size
= htobe64((uint64_t) sizeof(connect
));
176 cmd
.cmd_version
= htobe32(0);
178 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
179 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
180 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
181 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
184 * Merge the cmd and connection request to prevent a write-write
185 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
186 * second write to be performed quickly in presence of Nagle's algorithm
188 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
189 memcpy(cmd_buf
+ sizeof(cmd
), &connect
, sizeof(connect
));
190 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
191 if (ret_len
== BT_SOCKET_ERROR
) {
192 BT_COMP_LOGE("Error sending version: %s", bt_socket_errormsg());
196 BT_ASSERT(ret_len
== cmd_buf_len
);
198 ret_len
= lttng_live_recv(viewer_connection
, &connect
, sizeof(connect
));
200 BT_COMP_LOGI("Remote side has closed connection");
203 if (ret_len
== BT_SOCKET_ERROR
) {
204 BT_COMP_LOGE("Error receiving version: %s", bt_socket_errormsg());
207 BT_ASSERT(ret_len
== sizeof(connect
));
209 BT_COMP_LOGI("Received viewer session ID : %" PRIu64
,
210 (uint64_t) be64toh(connect
.viewer_session_id
));
211 BT_COMP_LOGI("Relayd version : %u.%u", be32toh(connect
.major
),
212 be32toh(connect
.minor
));
214 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
215 BT_COMP_LOGE("Incompatible lttng-relayd protocol");
218 /* Use the smallest protocol version implemented. */
219 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
220 viewer_connection
->minor
= be32toh(connect
.minor
);
222 viewer_connection
->minor
= LTTNG_LIVE_MINOR
;
224 viewer_connection
->major
= LTTNG_LIVE_MAJOR
;
229 BT_COMP_LOGE("Unable to establish connection");
234 int lttng_live_connect_viewer(struct live_viewer_connection
*viewer_connection
)
236 struct hostent
*host
;
237 struct sockaddr_in server_addr
;
240 if (parse_url(viewer_connection
)) {
244 host
= gethostbyname(viewer_connection
->relay_hostname
->str
);
246 BT_COMP_LOGE("Cannot lookup hostname %s",
247 viewer_connection
->relay_hostname
->str
);
251 if ((viewer_connection
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == BT_INVALID_SOCKET
) {
252 BT_COMP_LOGE("Socket creation failed: %s", bt_socket_errormsg());
256 server_addr
.sin_family
= AF_INET
;
257 server_addr
.sin_port
= htons(viewer_connection
->port
);
258 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
259 memset(&(server_addr
.sin_zero
), 0, 8);
261 if (connect(viewer_connection
->control_sock
, (struct sockaddr
*) &server_addr
,
262 sizeof(struct sockaddr
)) == BT_SOCKET_ERROR
) {
263 BT_COMP_LOGE("Connection failed: %s", bt_socket_errormsg());
266 if (lttng_live_handshake(viewer_connection
)) {
275 if (viewer_connection
->control_sock
!= BT_INVALID_SOCKET
) {
276 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
277 BT_COMP_LOGE("Close: %s", bt_socket_errormsg());
280 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
285 void lttng_live_disconnect_viewer(
286 struct live_viewer_connection
*viewer_connection
)
288 if (viewer_connection
->control_sock
== BT_INVALID_SOCKET
) {
291 if (bt_socket_close(viewer_connection
->control_sock
) == BT_SOCKET_ERROR
) {
292 BT_COMP_LOGE("Close: %s", bt_socket_errormsg());
293 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
298 void connection_release(bt_object
*obj
)
300 struct live_viewer_connection
*conn
=
301 container_of(obj
, struct live_viewer_connection
, obj
);
303 live_viewer_connection_destroy(conn
);
307 int list_update_session(bt_value
*results
,
308 const struct lttng_viewer_session
*session
,
309 bool *_found
, struct live_viewer_connection
*viewer_connection
)
312 bt_value
*map
= NULL
;
313 bt_value
*hostname
= NULL
;
314 bt_value
*session_name
= NULL
;
315 bt_value
*btval
= NULL
;
319 len
= bt_value_array_get_size(results
);
321 BT_COMP_LOGE_STR("Error getting size of array.");
325 for (i
= 0; i
< len
; i
++) {
326 const char *hostname_str
= NULL
;
327 const char *session_name_str
= NULL
;
329 map
= bt_value_array_borrow_element_by_index(results
, (size_t) i
);
331 BT_COMP_LOGE_STR("Error borrowing map.");
335 hostname
= bt_value_map_borrow_entry_value(map
, "target-hostname");
337 BT_COMP_LOGE_STR("Error borrowing \"target-hostname\" entry.");
341 session_name
= bt_value_map_borrow_entry_value(map
, "session-name");
343 BT_COMP_LOGE_STR("Error borrowing \"session-name\" entry.");
347 hostname_str
= bt_value_string_get(hostname
);
348 session_name_str
= bt_value_string_get(session_name
);
350 if (strcmp(session
->hostname
, hostname_str
) == 0
351 && strcmp(session
->session_name
, session_name_str
) == 0) {
353 uint32_t streams
= be32toh(session
->streams
);
354 uint32_t clients
= be32toh(session
->clients
);
358 btval
= bt_value_map_borrow_entry_value(map
, "stream-count");
360 BT_COMP_LOGE_STR("Error borrowing \"stream-count\" entry.");
364 val
= bt_value_integer_signed_get(btval
);
367 bt_value_integer_signed_set(btval
, val
);
369 btval
= bt_value_map_borrow_entry_value(map
, "client-count");
371 BT_COMP_LOGE_STR("Error borrowing \"client-count\" entry.");
375 val
= bt_value_integer_signed_get(btval
);
377 val
= bt_max_t(int64_t, clients
, val
);
378 bt_value_integer_signed_set(btval
, val
);
391 int list_append_session(bt_value
*results
,
393 const struct lttng_viewer_session
*session
,
394 struct live_viewer_connection
*viewer_connection
)
397 bt_value_map_insert_entry_status insert_status
;
398 bt_value_array_append_element_status append_status
;
399 bt_value
*map
= NULL
;
404 * If the session already exists, add the stream count to it,
405 * and do max of client counts.
407 ret
= list_update_session(results
, session
, &found
, viewer_connection
);
412 map
= bt_value_map_create();
414 BT_COMP_LOGE_STR("Error creating map value.");
419 if (base_url
->len
< 1) {
420 BT_COMP_LOGE_STR("Error: base_url length smaller than 1.");
428 url
= g_string_new(base_url
->str
);
429 g_string_append(url
, "/host/");
430 g_string_append(url
, session
->hostname
);
431 g_string_append_c(url
, '/');
432 g_string_append(url
, session
->session_name
);
434 insert_status
= bt_value_map_insert_string_entry(map
, "url", url
->str
);
435 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
436 BT_COMP_LOGE_STR("Error inserting \"url\" entry.");
442 * key = "target-hostname",
445 insert_status
= bt_value_map_insert_string_entry(map
, "target-hostname",
447 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
448 BT_COMP_LOGE_STR("Error inserting \"target-hostname\" entry.");
454 * key = "session-name",
457 insert_status
= bt_value_map_insert_string_entry(map
, "session-name",
458 session
->session_name
);
459 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
460 BT_COMP_LOGE_STR("Error inserting \"session-name\" entry.");
470 uint32_t live_timer
= be32toh(session
->live_timer
);
472 insert_status
= bt_value_map_insert_signed_integer_entry(
473 map
, "timer-us", live_timer
);
474 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
475 BT_COMP_LOGE_STR("Error inserting \"timer-us\" entry.");
482 * key = "stream-count",
486 uint32_t streams
= be32toh(session
->streams
);
488 insert_status
= bt_value_map_insert_signed_integer_entry(map
,
489 "stream-count", streams
);
490 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
491 BT_COMP_LOGE_STR("Error inserting \"stream-count\" entry.");
498 * key = "client-count",
502 uint32_t clients
= be32toh(session
->clients
);
504 insert_status
= bt_value_map_insert_signed_integer_entry(map
,
505 "client-count", clients
);
506 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
507 BT_COMP_LOGE_STR("Error inserting \"client-count\" entry.");
513 append_status
= bt_value_array_append_element(results
, map
);
514 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
515 BT_COMP_LOGE_STR("Error appending map to results.");
521 g_string_free(url
, true);
523 BT_VALUE_PUT_REF_AND_RESET(map
);
528 * Data structure returned:
539 * key = "target-hostname",
543 * key = "session-name",
551 * key = "stream-count",
555 * key = "client-count",
564 bt_component_class_query_method_status
live_viewer_connection_list_sessions(
565 struct live_viewer_connection
*viewer_connection
,
566 const bt_value
**user_result
)
568 bt_component_class_query_method_status status
=
569 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
570 bt_value
*result
= NULL
;
571 struct lttng_viewer_cmd cmd
;
572 struct lttng_viewer_list_sessions list
;
573 uint32_t i
, sessions_count
;
576 result
= bt_value_array_create();
578 BT_COMP_LOGE("Error creating array");
579 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
583 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
584 cmd
.data_size
= htobe64((uint64_t) 0);
585 cmd
.cmd_version
= htobe32(0);
587 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
588 if (ret_len
== BT_SOCKET_ERROR
) {
589 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
590 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
593 BT_ASSERT(ret_len
== sizeof(cmd
));
595 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
597 BT_COMP_LOGI("Remote side has closed connection");
598 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
601 if (ret_len
== BT_SOCKET_ERROR
) {
602 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
603 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
606 BT_ASSERT(ret_len
== sizeof(list
));
608 sessions_count
= be32toh(list
.sessions_count
);
609 for (i
= 0; i
< sessions_count
; i
++) {
610 struct lttng_viewer_session lsession
;
612 ret_len
= lttng_live_recv(viewer_connection
, &lsession
,
615 BT_COMP_LOGI("Remote side has closed connection");
616 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
619 if (ret_len
== BT_SOCKET_ERROR
) {
620 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
621 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
624 BT_ASSERT(ret_len
== sizeof(lsession
));
625 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
626 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
627 if (list_append_session(result
, viewer_connection
->url
,
628 &lsession
, viewer_connection
)) {
629 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
634 *user_result
= result
;
637 BT_VALUE_PUT_REF_AND_RESET(result
);
643 int lttng_live_query_session_ids(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
645 struct lttng_viewer_cmd cmd
;
646 struct lttng_viewer_list_sessions list
;
647 struct lttng_viewer_session lsession
;
648 uint32_t i
, sessions_count
;
651 struct live_viewer_connection
*viewer_connection
=
652 lttng_live_msg_iter
->viewer_connection
;
654 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
655 cmd
.data_size
= htobe64((uint64_t) 0);
656 cmd
.cmd_version
= htobe32(0);
658 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
659 if (ret_len
== BT_SOCKET_ERROR
) {
660 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
663 BT_ASSERT(ret_len
== sizeof(cmd
));
665 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
667 BT_COMP_LOGI("Remote side has closed connection");
670 if (ret_len
== BT_SOCKET_ERROR
) {
671 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
674 BT_ASSERT(ret_len
== sizeof(list
));
676 sessions_count
= be32toh(list
.sessions_count
);
677 for (i
= 0; i
< sessions_count
; i
++) {
678 ret_len
= lttng_live_recv(viewer_connection
,
679 &lsession
, sizeof(lsession
));
681 BT_COMP_LOGI("Remote side has closed connection");
684 if (ret_len
== BT_SOCKET_ERROR
) {
685 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
688 BT_ASSERT(ret_len
== sizeof(lsession
));
689 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
690 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
691 session_id
= be64toh(lsession
.id
);
693 BT_COMP_LOGI("Adding session %" PRIu64
" hostname: %s session_name: %s",
694 session_id
, lsession
.hostname
, lsession
.session_name
);
696 if ((strncmp(lsession
.session_name
,
697 viewer_connection
->session_name
->str
,
698 LTTNG_VIEWER_NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
699 viewer_connection
->target_hostname
->str
,
700 LTTNG_VIEWER_HOST_NAME_MAX
) == 0)) {
701 if (lttng_live_add_session(lttng_live_msg_iter
, session_id
,
703 lsession
.session_name
)) {
712 BT_COMP_LOGE("Unable to query session ids");
717 int lttng_live_create_viewer_session(
718 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
720 struct lttng_viewer_cmd cmd
;
721 struct lttng_viewer_create_session_response resp
;
723 struct live_viewer_connection
*viewer_connection
=
724 lttng_live_msg_iter
->viewer_connection
;
726 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
727 cmd
.data_size
= htobe64((uint64_t) 0);
728 cmd
.cmd_version
= htobe32(0);
730 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
731 if (ret_len
== BT_SOCKET_ERROR
) {
732 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
735 BT_ASSERT(ret_len
== sizeof(cmd
));
737 ret_len
= lttng_live_recv(viewer_connection
, &resp
, sizeof(resp
));
739 BT_COMP_LOGI("Remote side has closed connection");
742 if (ret_len
== BT_SOCKET_ERROR
) {
743 BT_COMP_LOGE("Error receiving create session reply: %s", bt_socket_errormsg());
746 BT_ASSERT(ret_len
== sizeof(resp
));
748 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
749 BT_COMP_LOGE("Error creating viewer session");
752 if (lttng_live_query_session_ids(lttng_live_msg_iter
)) {
763 int receive_streams(struct lttng_live_session
*session
,
764 uint32_t stream_count
)
768 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
769 session
->lttng_live_msg_iter
;
770 struct live_viewer_connection
*viewer_connection
=
771 lttng_live_msg_iter
->viewer_connection
;
773 BT_COMP_LOGI("Getting %" PRIu32
" new streams:", stream_count
);
774 for (i
= 0; i
< stream_count
; i
++) {
775 struct lttng_viewer_stream stream
;
776 struct lttng_live_stream_iterator
*live_stream
;
778 uint64_t ctf_trace_id
;
780 ret_len
= lttng_live_recv(viewer_connection
, &stream
, sizeof(stream
));
782 BT_COMP_LOGI("Remote side has closed connection");
785 if (ret_len
== BT_SOCKET_ERROR
) {
786 BT_COMP_LOGE("Error receiving stream");
789 BT_ASSERT(ret_len
== sizeof(stream
));
790 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
791 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
792 stream_id
= be64toh(stream
.id
);
793 ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
795 if (stream
.metadata_flag
) {
796 BT_COMP_LOGI(" metadata stream %" PRIu64
" : %s/%s",
797 stream_id
, stream
.path_name
,
798 stream
.channel_name
);
799 if (lttng_live_metadata_create_stream(session
,
800 ctf_trace_id
, stream_id
,
802 BT_COMP_LOGE("Error creating metadata stream");
806 session
->lazy_stream_msg_init
= true;
808 BT_COMP_LOGI(" stream %" PRIu64
" : %s/%s",
809 stream_id
, stream
.path_name
,
810 stream
.channel_name
);
811 live_stream
= lttng_live_stream_iterator_create(session
,
812 ctf_trace_id
, stream_id
);
814 BT_COMP_LOGE("Error creating streamn");
826 int lttng_live_attach_session(struct lttng_live_session
*session
)
828 struct lttng_viewer_cmd cmd
;
829 struct lttng_viewer_attach_session_request rq
;
830 struct lttng_viewer_attach_session_response rp
;
832 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
833 struct live_viewer_connection
*viewer_connection
=
834 lttng_live_msg_iter
->viewer_connection
;
835 uint64_t session_id
= session
->id
;
836 uint32_t streams_count
;
837 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
838 char cmd_buf
[cmd_buf_len
];
840 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
841 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
842 cmd
.cmd_version
= htobe32(0);
844 memset(&rq
, 0, sizeof(rq
));
845 rq
.session_id
= htobe64(session_id
);
846 // TODO: add cmd line parameter to select seek beginning
847 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
848 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
851 * Merge the cmd and connection request to prevent a write-write
852 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
853 * second write to be performed quickly in presence of Nagle's algorithm.
855 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
856 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
857 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
858 if (ret_len
== BT_SOCKET_ERROR
) {
859 BT_COMP_LOGE("Error sending attach request: %s", bt_socket_errormsg());
863 BT_ASSERT(ret_len
== cmd_buf_len
);
864 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
866 BT_COMP_LOGI("Remote side has closed connection");
869 if (ret_len
== BT_SOCKET_ERROR
) {
870 BT_COMP_LOGE("Error receiving attach response: %s", bt_socket_errormsg());
873 BT_ASSERT(ret_len
== sizeof(rp
));
875 streams_count
= be32toh(rp
.streams_count
);
876 switch(be32toh(rp
.status
)) {
877 case LTTNG_VIEWER_ATTACH_OK
:
879 case LTTNG_VIEWER_ATTACH_UNK
:
880 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
882 case LTTNG_VIEWER_ATTACH_ALREADY
:
883 BT_COMP_LOGW("There is already a viewer attached to this session");
885 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
886 BT_COMP_LOGW("Not a live session");
888 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
889 BT_COMP_LOGE("Wrong seek parameter");
892 BT_COMP_LOGE("Unknown attach return code %u", be32toh(rp
.status
));
896 /* We receive the initial list of streams. */
897 if (receive_streams(session
, streams_count
)) {
901 session
->attached
= true;
902 session
->new_streams_needed
= false;
911 int lttng_live_detach_session(struct lttng_live_session
*session
)
913 struct lttng_viewer_cmd cmd
;
914 struct lttng_viewer_detach_session_request rq
;
915 struct lttng_viewer_detach_session_response rp
;
917 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
918 struct live_viewer_connection
*viewer_connection
=
919 lttng_live_msg_iter
->viewer_connection
;
920 uint64_t session_id
= session
->id
;
921 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
922 char cmd_buf
[cmd_buf_len
];
924 if (!session
->attached
) {
928 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
929 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
930 cmd
.cmd_version
= htobe32(0);
932 memset(&rq
, 0, sizeof(rq
));
933 rq
.session_id
= htobe64(session_id
);
936 * Merge the cmd and connection request to prevent a write-write
937 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
938 * second write to be performed quickly in presence of Nagle's algorithm.
940 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
941 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
942 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
943 if (ret_len
== BT_SOCKET_ERROR
) {
944 BT_COMP_LOGE("Error sending detach request: %s", bt_socket_errormsg());
948 BT_ASSERT(ret_len
== cmd_buf_len
);
949 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
951 BT_COMP_LOGI("Remote side has closed connection");
954 if (ret_len
== BT_SOCKET_ERROR
) {
955 BT_COMP_LOGE("Error receiving detach response: %s", bt_socket_errormsg());
958 BT_ASSERT(ret_len
== sizeof(rp
));
960 switch(be32toh(rp
.status
)) {
961 case LTTNG_VIEWER_DETACH_SESSION_OK
:
963 case LTTNG_VIEWER_DETACH_SESSION_UNK
:
964 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
966 case LTTNG_VIEWER_DETACH_SESSION_ERR
:
967 BT_COMP_LOGW("Error detaching session id %" PRIu64
"", session_id
);
970 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp
.status
));
974 session
->attached
= false;
983 ssize_t
lttng_live_get_one_metadata_packet(struct lttng_live_trace
*trace
,
988 struct lttng_viewer_cmd cmd
;
989 struct lttng_viewer_get_metadata rq
;
990 struct lttng_viewer_metadata_packet rp
;
993 struct lttng_live_session
*session
= trace
->session
;
994 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
995 struct lttng_live_metadata
*metadata
= trace
->metadata
;
996 struct live_viewer_connection
*viewer_connection
=
997 lttng_live_msg_iter
->viewer_connection
;
998 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
999 char cmd_buf
[cmd_buf_len
];
1001 rq
.stream_id
= htobe64(metadata
->stream_id
);
1002 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
1003 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1004 cmd
.cmd_version
= htobe32(0);
1007 * Merge the cmd and connection request to prevent a write-write
1008 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1009 * second write to be performed quickly in presence of Nagle's algorithm.
1011 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1012 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1013 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1014 if (ret_len
== BT_SOCKET_ERROR
) {
1015 BT_COMP_LOGE("Error sending get_metadata request: %s", bt_socket_errormsg());
1019 BT_ASSERT(ret_len
== cmd_buf_len
);
1020 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1022 BT_COMP_LOGI("Remote side has closed connection");
1025 if (ret_len
== BT_SOCKET_ERROR
) {
1026 BT_COMP_LOGE("Error receiving get_metadata response: %s", bt_socket_errormsg());
1029 BT_ASSERT(ret_len
== sizeof(rp
));
1031 switch (be32toh(rp
.status
)) {
1032 case LTTNG_VIEWER_METADATA_OK
:
1033 BT_COMP_LOGD("get_metadata : OK");
1035 case LTTNG_VIEWER_NO_NEW_METADATA
:
1036 BT_COMP_LOGD("get_metadata : NO NEW");
1039 case LTTNG_VIEWER_METADATA_ERR
:
1040 BT_COMP_LOGD("get_metadata : ERR");
1043 BT_COMP_LOGD("get_metadata : UNKNOWN");
1047 len
= be64toh(rp
.len
);
1048 BT_COMP_LOGD("Writing %" PRIu64
" bytes to metadata", len
);
1053 data
= calloc(1, len
);
1055 BT_COMP_LOGE("relay data calloc: %s", strerror(errno
));
1058 ret_len
= lttng_live_recv(viewer_connection
, data
, len
);
1060 BT_COMP_LOGI("Remote side has closed connection");
1061 goto error_free_data
;
1063 if (ret_len
== BT_SOCKET_ERROR
) {
1064 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1065 goto error_free_data
;
1067 BT_ASSERT(ret_len
== len
);
1070 ret_len
= fwrite(data
, 1, len
, fp
);
1071 } while (ret_len
< 0 && errno
== EINTR
);
1073 BT_COMP_LOGE("Writing in the metadata fp");
1074 goto error_free_data
;
1076 BT_ASSERT(ret_len
== len
);
1089 * Assign the fields from a lttng_viewer_index to a packet_index.
1092 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
1093 struct packet_index
*pindex
)
1098 pindex
->offset
= be64toh(lindex
->offset
);
1099 pindex
->packet_size
= be64toh(lindex
->packet_size
);
1100 pindex
->content_size
= be64toh(lindex
->content_size
);
1101 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
1102 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
1103 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1107 enum lttng_live_iterator_status
lttng_live_get_next_index(
1108 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1109 struct lttng_live_stream_iterator
*stream
,
1110 struct packet_index
*index
)
1112 struct lttng_viewer_cmd cmd
;
1113 struct lttng_viewer_get_next_index rq
;
1115 struct lttng_viewer_index rp
;
1116 uint32_t flags
, status
;
1117 enum lttng_live_iterator_status retstatus
=
1118 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1119 struct live_viewer_connection
*viewer_connection
=
1120 lttng_live_msg_iter
->viewer_connection
;
1121 struct lttng_live_trace
*trace
= stream
->trace
;
1122 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1123 char cmd_buf
[cmd_buf_len
];
1125 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1126 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1127 cmd
.cmd_version
= htobe32(0);
1130 memset(&rq
, 0, sizeof(rq
));
1131 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1134 * Merge the cmd and connection request to prevent a write-write
1135 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1136 * second write to be performed quickly in presence of Nagle's algorithm.
1138 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1139 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1140 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1141 if (ret_len
== BT_SOCKET_ERROR
) {
1142 BT_COMP_LOGE("Error sending get_next_index request: %s",
1143 bt_socket_errormsg());
1147 BT_ASSERT(ret_len
== cmd_buf_len
);
1148 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1150 BT_COMP_LOGI("Remote side has closed connection");
1153 if (ret_len
== BT_SOCKET_ERROR
) {
1154 BT_COMP_LOGE("Error receiving get_next_index response: %s",
1155 bt_socket_errormsg());
1158 BT_ASSERT(ret_len
== sizeof(rp
));
1160 flags
= be32toh(rp
.flags
);
1161 status
= be32toh(rp
.status
);
1164 case LTTNG_VIEWER_INDEX_INACTIVE
:
1166 uint64_t ctf_stream_class_id
;
1168 BT_COMP_LOGD("get_next_index: inactive");
1169 memset(index
, 0, sizeof(struct packet_index
));
1170 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
1171 stream
->current_inactivity_ts
= index
->ts_cycles
.timestamp_end
;
1172 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1173 if (stream
->ctf_stream_class_id
!= -1ULL) {
1174 BT_ASSERT(stream
->ctf_stream_class_id
==
1175 ctf_stream_class_id
);
1177 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1179 stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT
;
1182 case LTTNG_VIEWER_INDEX_OK
:
1184 uint64_t ctf_stream_class_id
;
1186 BT_COMP_LOGD("get_next_index: OK");
1187 lttng_index_to_packet_index(&rp
, index
);
1188 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1189 if (stream
->ctf_stream_class_id
!= -1ULL) {
1190 BT_ASSERT(stream
->ctf_stream_class_id
==
1191 ctf_stream_class_id
);
1193 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1196 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_DATA
;
1198 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1199 BT_COMP_LOGD("get_next_index: new metadata needed");
1200 trace
->new_metadata_needed
= true;
1202 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1203 BT_COMP_LOGD("get_next_index: new streams needed");
1204 lttng_live_need_new_streams(lttng_live_msg_iter
);
1208 case LTTNG_VIEWER_INDEX_RETRY
:
1209 BT_COMP_LOGD("get_next_index: retry");
1210 memset(index
, 0, sizeof(struct packet_index
));
1211 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1212 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1214 case LTTNG_VIEWER_INDEX_HUP
:
1215 BT_COMP_LOGD("get_next_index: stream hung up");
1216 memset(index
, 0, sizeof(struct packet_index
));
1217 index
->offset
= EOF
;
1218 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1219 stream
->state
= LTTNG_LIVE_STREAM_EOF
;
1221 case LTTNG_VIEWER_INDEX_ERR
:
1222 BT_COMP_LOGE("get_next_index: error");
1223 memset(index
, 0, sizeof(struct packet_index
));
1224 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1227 BT_COMP_LOGE("get_next_index: unknown value");
1228 memset(index
, 0, sizeof(struct packet_index
));
1229 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1236 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1237 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1239 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1245 enum bt_msg_iter_medium_status
lttng_live_get_stream_bytes(
1246 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1247 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
1248 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
)
1250 enum bt_msg_iter_medium_status retstatus
= BT_MSG_ITER_MEDIUM_STATUS_OK
;
1251 struct lttng_viewer_cmd cmd
;
1252 struct lttng_viewer_get_packet rq
;
1253 struct lttng_viewer_trace_packet rp
;
1255 uint32_t flags
, status
;
1256 struct live_viewer_connection
*viewer_connection
=
1257 lttng_live_msg_iter
->viewer_connection
;
1258 struct lttng_live_trace
*trace
= stream
->trace
;
1259 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1260 char cmd_buf
[cmd_buf_len
];
1262 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64
", req_len=%" PRIu64
,
1264 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
1265 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1266 cmd
.cmd_version
= htobe32(0);
1268 memset(&rq
, 0, sizeof(rq
));
1269 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1270 rq
.offset
= htobe64(offset
);
1271 rq
.len
= htobe32(req_len
);
1274 * Merge the cmd and connection request to prevent a write-write
1275 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1276 * second write to be performed quickly in presence of Nagle's algorithm.
1278 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1279 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1280 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1281 if (ret_len
== BT_SOCKET_ERROR
) {
1282 BT_COMP_LOGE("Error sending get_data request: %s", bt_socket_errormsg());
1286 BT_ASSERT(ret_len
== cmd_buf_len
);
1287 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1289 BT_COMP_LOGI("Remote side has closed connection");
1292 if (ret_len
== BT_SOCKET_ERROR
) {
1293 BT_COMP_LOGE("Error receiving get_data response: %s", bt_socket_errormsg());
1296 if (ret_len
!= sizeof(rp
)) {
1297 BT_COMP_LOGE("get_data_packet: expected %zu"
1298 ", received %zd", sizeof(rp
),
1303 flags
= be32toh(rp
.flags
);
1304 status
= be32toh(rp
.status
);
1307 case LTTNG_VIEWER_GET_PACKET_OK
:
1308 req_len
= be32toh(rp
.len
);
1309 BT_COMP_LOGD("get_data_packet: Ok, packet size : %" PRIu64
"", req_len
);
1311 case LTTNG_VIEWER_GET_PACKET_RETRY
:
1312 /* Unimplemented by relay daemon */
1313 BT_COMP_LOGD("get_data_packet: retry");
1314 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1316 case LTTNG_VIEWER_GET_PACKET_ERR
:
1317 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1318 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
1319 trace
->new_metadata_needed
= true;
1321 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1322 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
1323 lttng_live_need_new_streams(lttng_live_msg_iter
);
1325 if (flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
1326 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
1327 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1330 BT_COMP_LOGE("get_data_packet: error");
1332 case LTTNG_VIEWER_GET_PACKET_EOF
:
1333 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_EOF
;
1336 BT_COMP_LOGE("get_data_packet: unknown");
1344 ret_len
= lttng_live_recv(viewer_connection
, buf
, req_len
);
1346 BT_COMP_LOGI("Remote side has closed connection");
1349 if (ret_len
== BT_SOCKET_ERROR
) {
1350 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1353 BT_ASSERT(ret_len
== req_len
);
1354 *recv_len
= ret_len
;
1359 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1360 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1362 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
1368 * Request new streams for a session.
1371 enum lttng_live_iterator_status
lttng_live_get_new_streams(
1372 struct lttng_live_session
*session
)
1374 enum lttng_live_iterator_status status
=
1375 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1376 struct lttng_viewer_cmd cmd
;
1377 struct lttng_viewer_new_streams_request rq
;
1378 struct lttng_viewer_new_streams_response rp
;
1380 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1381 session
->lttng_live_msg_iter
;
1382 struct live_viewer_connection
*viewer_connection
=
1383 lttng_live_msg_iter
->viewer_connection
;
1384 uint32_t streams_count
;
1385 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1386 char cmd_buf
[cmd_buf_len
];
1388 if (!session
->new_streams_needed
) {
1389 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
1392 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1393 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1394 cmd
.cmd_version
= htobe32(0);
1396 memset(&rq
, 0, sizeof(rq
));
1397 rq
.session_id
= htobe64(session
->id
);
1400 * Merge the cmd and connection request to prevent a write-write
1401 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1402 * second write to be performed quickly in presence of Nagle's algorithm.
1404 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1405 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1406 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1407 if (ret_len
== BT_SOCKET_ERROR
) {
1408 BT_COMP_LOGE("Error sending get_new_streams request: %s",
1409 bt_socket_errormsg());
1413 BT_ASSERT(ret_len
== cmd_buf_len
);
1414 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1416 BT_COMP_LOGI("Remote side has closed connection");
1419 if (ret_len
== BT_SOCKET_ERROR
) {
1420 BT_COMP_LOGE("Error receiving get_new_streams response");
1423 BT_ASSERT(ret_len
== sizeof(rp
));
1425 streams_count
= be32toh(rp
.streams_count
);
1427 switch(be32toh(rp
.status
)) {
1428 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1429 session
->new_streams_needed
= false;
1431 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1432 session
->new_streams_needed
= false;
1434 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1435 session
->new_streams_needed
= false;
1436 session
->closed
= true;
1437 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1439 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1440 BT_COMP_LOGE("get_new_streams error");
1443 BT_COMP_LOGE("Unknown return code %u", be32toh(rp
.status
));
1447 if (receive_streams(session
, streams_count
)) {
1454 if (lttng_live_graph_is_canceled(lttng_live_msg_iter
)) {
1455 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1457 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1463 struct live_viewer_connection
*live_viewer_connection_create(
1464 const char *url
, bool in_query
,
1465 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1466 bt_logging_level log_level
)
1468 struct live_viewer_connection
*viewer_connection
;
1470 viewer_connection
= g_new0(struct live_viewer_connection
, 1);
1472 if (bt_socket_init(log_level
) != 0) {
1476 viewer_connection
->log_level
= log_level
;
1478 if (lttng_live_msg_iter
) {
1479 viewer_connection
->self_comp
= lttng_live_msg_iter
->self_comp
;
1482 bt_object_init_shared(&viewer_connection
->obj
, connection_release
);
1483 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
1484 viewer_connection
->port
= -1;
1485 viewer_connection
->in_query
= in_query
;
1486 viewer_connection
->lttng_live_msg_iter
= lttng_live_msg_iter
;
1487 viewer_connection
->url
= g_string_new(url
);
1488 if (!viewer_connection
->url
) {
1492 BT_COMP_LOGI("Establishing connection to url \"%s\"...", url
);
1493 if (lttng_live_connect_viewer(viewer_connection
)) {
1496 BT_COMP_LOGI("Connection to url \"%s\" is established", url
);
1497 return viewer_connection
;
1500 BT_COMP_LOGW("Failure to establish connection to url \"%s\"", url
);
1502 g_free(viewer_connection
);
1507 void live_viewer_connection_destroy(
1508 struct live_viewer_connection
*viewer_connection
)
1510 BT_COMP_LOGI("Closing connection to url \"%s\"", viewer_connection
->url
->str
);
1511 lttng_live_disconnect_viewer(viewer_connection
);
1512 g_string_free(viewer_connection
->url
, true);
1513 if (viewer_connection
->relay_hostname
) {
1514 g_string_free(viewer_connection
->relay_hostname
, true);
1516 if (viewer_connection
->target_hostname
) {
1517 g_string_free(viewer_connection
->target_hostname
, true);
1519 if (viewer_connection
->session_name
) {
1520 g_string_free(viewer_connection
->session_name
, true);
1522 g_free(viewer_connection
);