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 "plugins/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
->lttng_live_comp
)) {
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
->lttng_live_comp
)) {
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
== NULL
?
153 "<none>" : viewer_connection
->target_hostname
->str
,
154 viewer_connection
->session_name
== NULL
?
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
)
351 && !strcmp(session
->session_name
,
354 uint32_t streams
= be32toh(session
->streams
);
355 uint32_t clients
= be32toh(session
->clients
);
359 btval
= bt_value_map_borrow_entry_value(map
, "stream-count");
361 BT_COMP_LOGE_STR("Error borrowing \"stream-count\" entry.");
365 val
= bt_value_signed_integer_get(btval
);
368 bt_value_signed_integer_set(btval
, val
);
370 btval
= bt_value_map_borrow_entry_value(map
, "client-count");
372 BT_COMP_LOGE_STR("Error borrowing \"client-count\" entry.");
376 val
= bt_value_signed_integer_get(btval
);
378 val
= bt_max_t(int64_t, clients
, val
);
379 bt_value_signed_integer_set(btval
, val
);
392 int list_append_session(bt_value
*results
,
394 const struct lttng_viewer_session
*session
,
395 struct live_viewer_connection
*viewer_connection
)
398 bt_value_map_insert_entry_status insert_status
;
399 bt_value_array_append_element_status append_status
;
400 bt_value
*map
= NULL
;
405 * If the session already exists, add the stream count to it,
406 * and do max of client counts.
408 ret
= list_update_session(results
, session
, &found
, viewer_connection
);
413 map
= bt_value_map_create();
415 BT_COMP_LOGE_STR("Error creating map value.");
420 if (base_url
->len
< 1) {
421 BT_COMP_LOGE_STR("Error: base_url length smaller than 1.");
429 url
= g_string_new(base_url
->str
);
430 g_string_append(url
, "/host/");
431 g_string_append(url
, session
->hostname
);
432 g_string_append_c(url
, '/');
433 g_string_append(url
, session
->session_name
);
435 insert_status
= bt_value_map_insert_string_entry(map
, "url", url
->str
);
436 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
437 BT_COMP_LOGE_STR("Error inserting \"url\" entry.");
443 * key = "target-hostname",
446 insert_status
= bt_value_map_insert_string_entry(map
, "target-hostname",
448 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
449 BT_COMP_LOGE_STR("Error inserting \"target-hostname\" entry.");
455 * key = "session-name",
458 insert_status
= bt_value_map_insert_string_entry(map
, "session-name",
459 session
->session_name
);
460 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
461 BT_COMP_LOGE_STR("Error inserting \"session-name\" entry.");
471 uint32_t live_timer
= be32toh(session
->live_timer
);
473 insert_status
= bt_value_map_insert_signed_integer_entry(
474 map
, "timer-us", live_timer
);
475 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
476 BT_COMP_LOGE_STR("Error inserting \"timer-us\" entry.");
483 * key = "stream-count",
487 uint32_t streams
= be32toh(session
->streams
);
489 insert_status
= bt_value_map_insert_signed_integer_entry(map
,
490 "stream-count", streams
);
491 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
492 BT_COMP_LOGE_STR("Error inserting \"stream-count\" entry.");
499 * key = "client-count",
503 uint32_t clients
= be32toh(session
->clients
);
505 insert_status
= bt_value_map_insert_signed_integer_entry(map
,
506 "client-count", clients
);
507 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
508 BT_COMP_LOGE_STR("Error inserting \"client-count\" entry.");
514 append_status
= bt_value_array_append_element(results
, map
);
515 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
516 BT_COMP_LOGE_STR("Error appending map to results.");
522 g_string_free(url
, true);
524 BT_VALUE_PUT_REF_AND_RESET(map
);
529 * Data structure returned:
540 * key = "target-hostname",
544 * key = "session-name",
552 * key = "stream-count",
556 * key = "client-count",
565 bt_component_class_query_method_status
live_viewer_connection_list_sessions(
566 struct live_viewer_connection
*viewer_connection
,
567 const bt_value
**user_result
)
569 bt_component_class_query_method_status status
=
570 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
571 bt_value
*result
= NULL
;
572 struct lttng_viewer_cmd cmd
;
573 struct lttng_viewer_list_sessions list
;
574 uint32_t i
, sessions_count
;
577 if (lttng_live_handshake(viewer_connection
)) {
581 result
= bt_value_array_create();
583 BT_COMP_LOGE("Error creating array");
584 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
588 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
589 cmd
.data_size
= htobe64((uint64_t) 0);
590 cmd
.cmd_version
= htobe32(0);
592 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
593 if (ret_len
== BT_SOCKET_ERROR
) {
594 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
595 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
598 BT_ASSERT(ret_len
== sizeof(cmd
));
600 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
602 BT_COMP_LOGI("Remote side has closed connection");
603 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
606 if (ret_len
== BT_SOCKET_ERROR
) {
607 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
608 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
611 BT_ASSERT(ret_len
== sizeof(list
));
613 sessions_count
= be32toh(list
.sessions_count
);
614 for (i
= 0; i
< sessions_count
; i
++) {
615 struct lttng_viewer_session lsession
;
617 ret_len
= lttng_live_recv(viewer_connection
, &lsession
,
620 BT_COMP_LOGI("Remote side has closed connection");
621 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
624 if (ret_len
== BT_SOCKET_ERROR
) {
625 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
626 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
629 BT_ASSERT(ret_len
== sizeof(lsession
));
630 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
631 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
632 if (list_append_session(result
, viewer_connection
->url
,
633 &lsession
, viewer_connection
)) {
634 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
639 *user_result
= result
;
642 BT_VALUE_PUT_REF_AND_RESET(result
);
648 int lttng_live_query_session_ids(struct lttng_live_msg_iter
*lttng_live_msg_iter
)
650 struct lttng_viewer_cmd cmd
;
651 struct lttng_viewer_list_sessions list
;
652 struct lttng_viewer_session lsession
;
653 uint32_t i
, sessions_count
;
656 struct live_viewer_connection
*viewer_connection
=
657 lttng_live_msg_iter
->viewer_connection
;
659 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
660 cmd
.data_size
= htobe64((uint64_t) 0);
661 cmd
.cmd_version
= htobe32(0);
663 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
664 if (ret_len
== BT_SOCKET_ERROR
) {
665 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
668 BT_ASSERT(ret_len
== sizeof(cmd
));
670 ret_len
= lttng_live_recv(viewer_connection
, &list
, sizeof(list
));
672 BT_COMP_LOGI("Remote side has closed connection");
675 if (ret_len
== BT_SOCKET_ERROR
) {
676 BT_COMP_LOGE("Error receiving session list: %s", bt_socket_errormsg());
679 BT_ASSERT(ret_len
== sizeof(list
));
681 sessions_count
= be32toh(list
.sessions_count
);
682 for (i
= 0; i
< sessions_count
; i
++) {
683 ret_len
= lttng_live_recv(viewer_connection
,
684 &lsession
, sizeof(lsession
));
686 BT_COMP_LOGI("Remote side has closed connection");
689 if (ret_len
== BT_SOCKET_ERROR
) {
690 BT_COMP_LOGE("Error receiving session: %s", bt_socket_errormsg());
693 BT_ASSERT(ret_len
== sizeof(lsession
));
694 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
695 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
696 session_id
= be64toh(lsession
.id
);
698 BT_COMP_LOGI("Adding session %" PRIu64
" hostname: %s session_name: %s",
699 session_id
, lsession
.hostname
, lsession
.session_name
);
701 if ((strncmp(lsession
.session_name
,
702 viewer_connection
->session_name
->str
,
703 LTTNG_VIEWER_NAME_MAX
) == 0) && (strncmp(lsession
.hostname
,
704 viewer_connection
->target_hostname
->str
,
705 LTTNG_VIEWER_HOST_NAME_MAX
) == 0)) {
706 if (lttng_live_add_session(lttng_live_msg_iter
, session_id
,
708 lsession
.session_name
)) {
717 BT_COMP_LOGE("Unable to query session ids");
722 int lttng_live_create_viewer_session(
723 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
725 struct lttng_viewer_cmd cmd
;
726 struct lttng_viewer_create_session_response resp
;
728 struct live_viewer_connection
*viewer_connection
=
729 lttng_live_msg_iter
->viewer_connection
;
731 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
732 cmd
.data_size
= htobe64((uint64_t) 0);
733 cmd
.cmd_version
= htobe32(0);
735 ret_len
= lttng_live_send(viewer_connection
, &cmd
, sizeof(cmd
));
736 if (ret_len
== BT_SOCKET_ERROR
) {
737 BT_COMP_LOGE("Error sending cmd: %s", bt_socket_errormsg());
740 BT_ASSERT(ret_len
== sizeof(cmd
));
742 ret_len
= lttng_live_recv(viewer_connection
, &resp
, sizeof(resp
));
744 BT_COMP_LOGI("Remote side has closed connection");
747 if (ret_len
== BT_SOCKET_ERROR
) {
748 BT_COMP_LOGE("Error receiving create session reply: %s", bt_socket_errormsg());
751 BT_ASSERT(ret_len
== sizeof(resp
));
753 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
754 BT_COMP_LOGE("Error creating viewer session");
757 if (lttng_live_query_session_ids(lttng_live_msg_iter
)) {
768 int receive_streams(struct lttng_live_session
*session
,
769 uint32_t stream_count
)
773 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
774 session
->lttng_live_msg_iter
;
775 struct live_viewer_connection
*viewer_connection
=
776 lttng_live_msg_iter
->viewer_connection
;
778 BT_COMP_LOGI("Getting %" PRIu32
" new streams:", stream_count
);
779 for (i
= 0; i
< stream_count
; i
++) {
780 struct lttng_viewer_stream stream
;
781 struct lttng_live_stream_iterator
*live_stream
;
783 uint64_t ctf_trace_id
;
785 ret_len
= lttng_live_recv(viewer_connection
, &stream
, sizeof(stream
));
787 BT_COMP_LOGI("Remote side has closed connection");
790 if (ret_len
== BT_SOCKET_ERROR
) {
791 BT_COMP_LOGE("Error receiving stream");
794 BT_ASSERT(ret_len
== sizeof(stream
));
795 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
796 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
797 stream_id
= be64toh(stream
.id
);
798 ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
800 if (stream
.metadata_flag
) {
801 BT_COMP_LOGI(" metadata stream %" PRIu64
" : %s/%s",
802 stream_id
, stream
.path_name
,
803 stream
.channel_name
);
804 if (lttng_live_metadata_create_stream(session
,
805 ctf_trace_id
, stream_id
,
807 BT_COMP_LOGE("Error creating metadata stream");
811 session
->lazy_stream_msg_init
= true;
813 BT_COMP_LOGI(" stream %" PRIu64
" : %s/%s",
814 stream_id
, stream
.path_name
,
815 stream
.channel_name
);
816 live_stream
= lttng_live_stream_iterator_create(session
,
817 ctf_trace_id
, stream_id
);
819 BT_COMP_LOGE("Error creating streamn");
831 int lttng_live_attach_session(struct lttng_live_session
*session
)
833 struct lttng_viewer_cmd cmd
;
834 struct lttng_viewer_attach_session_request rq
;
835 struct lttng_viewer_attach_session_response rp
;
837 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
838 struct live_viewer_connection
*viewer_connection
=
839 lttng_live_msg_iter
->viewer_connection
;
840 uint64_t session_id
= session
->id
;
841 uint32_t streams_count
;
842 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
843 char cmd_buf
[cmd_buf_len
];
845 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
846 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
847 cmd
.cmd_version
= htobe32(0);
849 memset(&rq
, 0, sizeof(rq
));
850 rq
.session_id
= htobe64(session_id
);
851 // TODO: add cmd line parameter to select seek beginning
852 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
853 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
856 * Merge the cmd and connection request to prevent a write-write
857 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
858 * second write to be performed quickly in presence of Nagle's algorithm.
860 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
861 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
862 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
863 if (ret_len
== BT_SOCKET_ERROR
) {
864 BT_COMP_LOGE("Error sending attach request: %s", bt_socket_errormsg());
868 BT_ASSERT(ret_len
== cmd_buf_len
);
869 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
871 BT_COMP_LOGI("Remote side has closed connection");
874 if (ret_len
== BT_SOCKET_ERROR
) {
875 BT_COMP_LOGE("Error receiving attach response: %s", bt_socket_errormsg());
878 BT_ASSERT(ret_len
== sizeof(rp
));
880 streams_count
= be32toh(rp
.streams_count
);
881 switch(be32toh(rp
.status
)) {
882 case LTTNG_VIEWER_ATTACH_OK
:
884 case LTTNG_VIEWER_ATTACH_UNK
:
885 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
887 case LTTNG_VIEWER_ATTACH_ALREADY
:
888 BT_COMP_LOGW("There is already a viewer attached to this session");
890 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
891 BT_COMP_LOGW("Not a live session");
893 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
894 BT_COMP_LOGE("Wrong seek parameter");
897 BT_COMP_LOGE("Unknown attach return code %u", be32toh(rp
.status
));
901 /* We receive the initial list of streams. */
902 if (receive_streams(session
, streams_count
)) {
906 session
->attached
= true;
907 session
->new_streams_needed
= false;
916 int lttng_live_detach_session(struct lttng_live_session
*session
)
918 struct lttng_viewer_cmd cmd
;
919 struct lttng_viewer_detach_session_request rq
;
920 struct lttng_viewer_detach_session_response rp
;
922 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
923 struct live_viewer_connection
*viewer_connection
=
924 lttng_live_msg_iter
->viewer_connection
;
925 uint64_t session_id
= session
->id
;
926 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
927 char cmd_buf
[cmd_buf_len
];
929 if (!session
->attached
) {
933 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
934 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
935 cmd
.cmd_version
= htobe32(0);
937 memset(&rq
, 0, sizeof(rq
));
938 rq
.session_id
= htobe64(session_id
);
941 * Merge the cmd and connection request to prevent a write-write
942 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
943 * second write to be performed quickly in presence of Nagle's algorithm.
945 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
946 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
947 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
948 if (ret_len
== BT_SOCKET_ERROR
) {
949 BT_COMP_LOGE("Error sending detach request: %s", bt_socket_errormsg());
953 BT_ASSERT(ret_len
== cmd_buf_len
);
954 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
956 BT_COMP_LOGI("Remote side has closed connection");
959 if (ret_len
== BT_SOCKET_ERROR
) {
960 BT_COMP_LOGE("Error receiving detach response: %s", bt_socket_errormsg());
963 BT_ASSERT(ret_len
== sizeof(rp
));
965 switch(be32toh(rp
.status
)) {
966 case LTTNG_VIEWER_DETACH_SESSION_OK
:
968 case LTTNG_VIEWER_DETACH_SESSION_UNK
:
969 BT_COMP_LOGW("Session id %" PRIu64
" is unknown", session_id
);
971 case LTTNG_VIEWER_DETACH_SESSION_ERR
:
972 BT_COMP_LOGW("Error detaching session id %" PRIu64
"", session_id
);
975 BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp
.status
));
979 session
->attached
= false;
988 ssize_t
lttng_live_get_one_metadata_packet(struct lttng_live_trace
*trace
,
993 struct lttng_viewer_cmd cmd
;
994 struct lttng_viewer_get_metadata rq
;
995 struct lttng_viewer_metadata_packet rp
;
998 struct lttng_live_session
*session
= trace
->session
;
999 struct lttng_live_msg_iter
*lttng_live_msg_iter
= session
->lttng_live_msg_iter
;
1000 struct lttng_live_metadata
*metadata
= trace
->metadata
;
1001 struct live_viewer_connection
*viewer_connection
=
1002 lttng_live_msg_iter
->viewer_connection
;
1003 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1004 char cmd_buf
[cmd_buf_len
];
1006 rq
.stream_id
= htobe64(metadata
->stream_id
);
1007 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
1008 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1009 cmd
.cmd_version
= htobe32(0);
1012 * Merge the cmd and connection request to prevent a write-write
1013 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1014 * second write to be performed quickly in presence of Nagle's algorithm.
1016 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1017 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1018 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1019 if (ret_len
== BT_SOCKET_ERROR
) {
1020 BT_COMP_LOGE("Error sending get_metadata request: %s", bt_socket_errormsg());
1024 BT_ASSERT(ret_len
== cmd_buf_len
);
1025 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1027 BT_COMP_LOGI("Remote side has closed connection");
1030 if (ret_len
== BT_SOCKET_ERROR
) {
1031 BT_COMP_LOGE("Error receiving get_metadata response: %s", bt_socket_errormsg());
1034 BT_ASSERT(ret_len
== sizeof(rp
));
1036 switch (be32toh(rp
.status
)) {
1037 case LTTNG_VIEWER_METADATA_OK
:
1038 BT_COMP_LOGD("get_metadata : OK");
1040 case LTTNG_VIEWER_NO_NEW_METADATA
:
1041 BT_COMP_LOGD("get_metadata : NO NEW");
1044 case LTTNG_VIEWER_METADATA_ERR
:
1045 BT_COMP_LOGD("get_metadata : ERR");
1048 BT_COMP_LOGD("get_metadata : UNKNOWN");
1052 len
= be64toh(rp
.len
);
1053 BT_COMP_LOGD("Writing %" PRIu64
" bytes to metadata", len
);
1058 data
= calloc(1, len
);
1060 BT_COMP_LOGE("relay data calloc: %s", strerror(errno
));
1063 ret_len
= lttng_live_recv(viewer_connection
, data
, len
);
1065 BT_COMP_LOGI("Remote side has closed connection");
1066 goto error_free_data
;
1068 if (ret_len
== BT_SOCKET_ERROR
) {
1069 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1070 goto error_free_data
;
1072 BT_ASSERT(ret_len
== len
);
1075 ret_len
= fwrite(data
, 1, len
, fp
);
1076 } while (ret_len
< 0 && errno
== EINTR
);
1078 BT_COMP_LOGE("Writing in the metadata fp");
1079 goto error_free_data
;
1081 BT_ASSERT(ret_len
== len
);
1094 * Assign the fields from a lttng_viewer_index to a packet_index.
1097 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
1098 struct packet_index
*pindex
)
1103 pindex
->offset
= be64toh(lindex
->offset
);
1104 pindex
->packet_size
= be64toh(lindex
->packet_size
);
1105 pindex
->content_size
= be64toh(lindex
->content_size
);
1106 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
1107 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
1108 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1112 enum lttng_live_iterator_status
lttng_live_get_next_index(
1113 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1114 struct lttng_live_stream_iterator
*stream
,
1115 struct packet_index
*index
)
1117 struct lttng_viewer_cmd cmd
;
1118 struct lttng_viewer_get_next_index rq
;
1120 struct lttng_viewer_index rp
;
1121 uint32_t flags
, status
;
1122 enum lttng_live_iterator_status retstatus
=
1123 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1124 struct live_viewer_connection
*viewer_connection
=
1125 lttng_live_msg_iter
->viewer_connection
;
1126 struct lttng_live_trace
*trace
= stream
->trace
;
1127 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1128 char cmd_buf
[cmd_buf_len
];
1129 struct lttng_live_component
*lttng_live
=
1130 lttng_live_msg_iter
->lttng_live_comp
;
1132 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1133 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1134 cmd
.cmd_version
= htobe32(0);
1137 memset(&rq
, 0, sizeof(rq
));
1138 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1141 * Merge the cmd and connection request to prevent a write-write
1142 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1143 * second write to be performed quickly in presence of Nagle's algorithm.
1145 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1146 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1147 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1148 if (ret_len
== BT_SOCKET_ERROR
) {
1149 BT_COMP_LOGE("Error sending get_next_index request: %s",
1150 bt_socket_errormsg());
1154 BT_ASSERT(ret_len
== cmd_buf_len
);
1155 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1157 BT_COMP_LOGI("Remote side has closed connection");
1160 if (ret_len
== BT_SOCKET_ERROR
) {
1161 BT_COMP_LOGE("Error receiving get_next_index response: %s",
1162 bt_socket_errormsg());
1165 BT_ASSERT(ret_len
== sizeof(rp
));
1167 flags
= be32toh(rp
.flags
);
1168 status
= be32toh(rp
.status
);
1171 case LTTNG_VIEWER_INDEX_INACTIVE
:
1173 uint64_t ctf_stream_class_id
;
1175 BT_COMP_LOGD("get_next_index: inactive");
1176 memset(index
, 0, sizeof(struct packet_index
));
1177 index
->ts_cycles
.timestamp_end
= be64toh(rp
.timestamp_end
);
1178 stream
->current_inactivity_ts
= index
->ts_cycles
.timestamp_end
;
1179 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1180 if (stream
->ctf_stream_class_id
!= -1ULL) {
1181 BT_ASSERT(stream
->ctf_stream_class_id
==
1182 ctf_stream_class_id
);
1184 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1186 stream
->state
= LTTNG_LIVE_STREAM_QUIESCENT
;
1189 case LTTNG_VIEWER_INDEX_OK
:
1191 uint64_t ctf_stream_class_id
;
1193 BT_COMP_LOGD("get_next_index: OK");
1194 lttng_index_to_packet_index(&rp
, index
);
1195 ctf_stream_class_id
= be64toh(rp
.stream_id
);
1196 if (stream
->ctf_stream_class_id
!= -1ULL) {
1197 BT_ASSERT(stream
->ctf_stream_class_id
==
1198 ctf_stream_class_id
);
1200 stream
->ctf_stream_class_id
= ctf_stream_class_id
;
1203 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_DATA
;
1205 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1206 BT_COMP_LOGD("get_next_index: new metadata needed");
1207 trace
->new_metadata_needed
= true;
1209 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1210 BT_COMP_LOGD("get_next_index: new streams needed");
1211 lttng_live_need_new_streams(lttng_live_msg_iter
);
1215 case LTTNG_VIEWER_INDEX_RETRY
:
1216 BT_COMP_LOGD("get_next_index: retry");
1217 memset(index
, 0, sizeof(struct packet_index
));
1218 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1219 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1221 case LTTNG_VIEWER_INDEX_HUP
:
1222 BT_COMP_LOGD("get_next_index: stream hung up");
1223 memset(index
, 0, sizeof(struct packet_index
));
1224 index
->offset
= EOF
;
1225 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1226 stream
->state
= LTTNG_LIVE_STREAM_EOF
;
1228 case LTTNG_VIEWER_INDEX_ERR
:
1229 BT_COMP_LOGE("get_next_index: error");
1230 memset(index
, 0, sizeof(struct packet_index
));
1231 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1234 BT_COMP_LOGE("get_next_index: unknown value");
1235 memset(index
, 0, sizeof(struct packet_index
));
1236 stream
->state
= LTTNG_LIVE_STREAM_ACTIVE_NO_DATA
;
1243 if (lttng_live_graph_is_canceled(lttng_live
)) {
1244 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1246 retstatus
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1252 enum bt_msg_iter_medium_status
lttng_live_get_stream_bytes(
1253 struct lttng_live_msg_iter
*lttng_live_msg_iter
,
1254 struct lttng_live_stream_iterator
*stream
, uint8_t *buf
,
1255 uint64_t offset
, uint64_t req_len
, uint64_t *recv_len
)
1257 enum bt_msg_iter_medium_status retstatus
= BT_MSG_ITER_MEDIUM_STATUS_OK
;
1258 struct lttng_viewer_cmd cmd
;
1259 struct lttng_viewer_get_packet rq
;
1260 struct lttng_viewer_trace_packet rp
;
1262 uint32_t flags
, status
;
1263 struct live_viewer_connection
*viewer_connection
=
1264 lttng_live_msg_iter
->viewer_connection
;
1265 struct lttng_live_trace
*trace
= stream
->trace
;
1266 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1267 char cmd_buf
[cmd_buf_len
];
1268 struct lttng_live_component
*lttng_live
=
1269 lttng_live_msg_iter
->lttng_live_comp
;
1271 BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64
", req_len=%" PRIu64
,
1273 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
1274 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1275 cmd
.cmd_version
= htobe32(0);
1277 memset(&rq
, 0, sizeof(rq
));
1278 rq
.stream_id
= htobe64(stream
->viewer_stream_id
);
1279 rq
.offset
= htobe64(offset
);
1280 rq
.len
= htobe32(req_len
);
1283 * Merge the cmd and connection request to prevent a write-write
1284 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1285 * second write to be performed quickly in presence of Nagle's algorithm.
1287 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1288 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1289 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1290 if (ret_len
== BT_SOCKET_ERROR
) {
1291 BT_COMP_LOGE("Error sending get_data request: %s", bt_socket_errormsg());
1295 BT_ASSERT(ret_len
== cmd_buf_len
);
1296 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1298 BT_COMP_LOGI("Remote side has closed connection");
1301 if (ret_len
== BT_SOCKET_ERROR
) {
1302 BT_COMP_LOGE("Error receiving get_data response: %s", bt_socket_errormsg());
1305 if (ret_len
!= sizeof(rp
)) {
1306 BT_COMP_LOGE("get_data_packet: expected %zu"
1307 ", received %zd", sizeof(rp
),
1312 flags
= be32toh(rp
.flags
);
1313 status
= be32toh(rp
.status
);
1316 case LTTNG_VIEWER_GET_PACKET_OK
:
1317 req_len
= be32toh(rp
.len
);
1318 BT_COMP_LOGD("get_data_packet: Ok, packet size : %" PRIu64
"", req_len
);
1320 case LTTNG_VIEWER_GET_PACKET_RETRY
:
1321 /* Unimplemented by relay daemon */
1322 BT_COMP_LOGD("get_data_packet: retry");
1323 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1325 case LTTNG_VIEWER_GET_PACKET_ERR
:
1326 if (flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1327 BT_COMP_LOGD("get_data_packet: new metadata needed, try again later");
1328 trace
->new_metadata_needed
= true;
1330 if (flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1331 BT_COMP_LOGD("get_data_packet: new streams needed, try again later");
1332 lttng_live_need_new_streams(lttng_live_msg_iter
);
1334 if (flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
1335 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
1336 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1339 BT_COMP_LOGE("get_data_packet: error");
1341 case LTTNG_VIEWER_GET_PACKET_EOF
:
1342 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_EOF
;
1345 BT_COMP_LOGE("get_data_packet: unknown");
1353 ret_len
= lttng_live_recv(viewer_connection
, buf
, req_len
);
1355 BT_COMP_LOGI("Remote side has closed connection");
1358 if (ret_len
== BT_SOCKET_ERROR
) {
1359 BT_COMP_LOGE("Error receiving trace packet: %s", bt_socket_errormsg());
1362 BT_ASSERT(ret_len
== req_len
);
1363 *recv_len
= ret_len
;
1368 if (lttng_live_graph_is_canceled(lttng_live
)) {
1369 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_AGAIN
;
1371 retstatus
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
1377 * Request new streams for a session.
1380 enum lttng_live_iterator_status
lttng_live_get_new_streams(
1381 struct lttng_live_session
*session
)
1383 enum lttng_live_iterator_status status
=
1384 LTTNG_LIVE_ITERATOR_STATUS_OK
;
1385 struct lttng_viewer_cmd cmd
;
1386 struct lttng_viewer_new_streams_request rq
;
1387 struct lttng_viewer_new_streams_response rp
;
1389 struct lttng_live_msg_iter
*lttng_live_msg_iter
=
1390 session
->lttng_live_msg_iter
;
1391 struct live_viewer_connection
*viewer_connection
=
1392 lttng_live_msg_iter
->viewer_connection
;
1393 struct lttng_live_component
*lttng_live
=
1394 lttng_live_msg_iter
->lttng_live_comp
;
1395 uint32_t streams_count
;
1396 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1397 char cmd_buf
[cmd_buf_len
];
1399 if (!session
->new_streams_needed
) {
1400 return LTTNG_LIVE_ITERATOR_STATUS_OK
;
1403 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1404 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1405 cmd
.cmd_version
= htobe32(0);
1407 memset(&rq
, 0, sizeof(rq
));
1408 rq
.session_id
= htobe64(session
->id
);
1411 * Merge the cmd and connection request to prevent a write-write
1412 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1413 * second write to be performed quickly in presence of Nagle's algorithm.
1415 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1416 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1417 ret_len
= lttng_live_send(viewer_connection
, &cmd_buf
, cmd_buf_len
);
1418 if (ret_len
== BT_SOCKET_ERROR
) {
1419 BT_COMP_LOGE("Error sending get_new_streams request: %s",
1420 bt_socket_errormsg());
1424 BT_ASSERT(ret_len
== cmd_buf_len
);
1425 ret_len
= lttng_live_recv(viewer_connection
, &rp
, sizeof(rp
));
1427 BT_COMP_LOGI("Remote side has closed connection");
1430 if (ret_len
== BT_SOCKET_ERROR
) {
1431 BT_COMP_LOGE("Error receiving get_new_streams response");
1434 BT_ASSERT(ret_len
== sizeof(rp
));
1436 streams_count
= be32toh(rp
.streams_count
);
1438 switch(be32toh(rp
.status
)) {
1439 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1440 session
->new_streams_needed
= false;
1442 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1443 session
->new_streams_needed
= false;
1445 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1446 session
->new_streams_needed
= false;
1447 session
->closed
= true;
1448 status
= LTTNG_LIVE_ITERATOR_STATUS_END
;
1450 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1451 BT_COMP_LOGE("get_new_streams error");
1454 BT_COMP_LOGE("Unknown return code %u", be32toh(rp
.status
));
1458 if (receive_streams(session
, streams_count
)) {
1465 if (lttng_live_graph_is_canceled(lttng_live
)) {
1466 status
= LTTNG_LIVE_ITERATOR_STATUS_AGAIN
;
1468 status
= LTTNG_LIVE_ITERATOR_STATUS_ERROR
;
1474 struct live_viewer_connection
*live_viewer_connection_create(
1475 const char *url
, bool in_query
,
1476 struct lttng_live_msg_iter
*lttng_live_msg_iter
)
1478 struct live_viewer_connection
*viewer_connection
;
1480 viewer_connection
= g_new0(struct live_viewer_connection
, 1);
1482 if (bt_socket_init() != 0) {
1486 viewer_connection
->log_level
= lttng_live_msg_iter
->log_level
;
1487 viewer_connection
->self_comp
= lttng_live_msg_iter
->self_comp
;
1488 bt_object_init_shared(&viewer_connection
->obj
, connection_release
);
1489 viewer_connection
->control_sock
= BT_INVALID_SOCKET
;
1490 viewer_connection
->port
= -1;
1491 viewer_connection
->in_query
= in_query
;
1492 viewer_connection
->lttng_live_msg_iter
= lttng_live_msg_iter
;
1493 viewer_connection
->url
= g_string_new(url
);
1494 if (!viewer_connection
->url
) {
1498 BT_COMP_LOGI("Establishing connection to url \"%s\"...", url
);
1499 if (lttng_live_connect_viewer(viewer_connection
)) {
1502 BT_COMP_LOGI("Connection to url \"%s\" is established", url
);
1503 return viewer_connection
;
1506 BT_COMP_LOGW("Failure to establish connection to url \"%s\"", url
);
1508 g_free(viewer_connection
);
1513 void live_viewer_connection_destroy(
1514 struct live_viewer_connection
*viewer_connection
)
1516 BT_COMP_LOGI("Closing connection to url \"%s\"", viewer_connection
->url
->str
);
1517 lttng_live_disconnect_viewer(viewer_connection
);
1518 g_string_free(viewer_connection
->url
, true);
1519 if (viewer_connection
->relay_hostname
) {
1520 g_string_free(viewer_connection
->relay_hostname
, true);
1522 if (viewer_connection
->target_hostname
) {
1523 g_string_free(viewer_connection
->target_hostname
, true);
1525 if (viewer_connection
->session_name
) {
1526 g_string_free(viewer_connection
->session_name
, true);
1528 g_free(viewer_connection
);