| 1 | /* |
| 2 | * Copyright 2019 - Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * Copyright 2016 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 4 | * |
| 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: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in |
| 13 | * all copies or substantial portions of the Software. |
| 14 | * |
| 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 |
| 21 | * SOFTWARE. |
| 22 | */ |
| 23 | |
| 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" |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | #include <glib.h> |
| 38 | |
| 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> |
| 44 | |
| 45 | #include "lttng-live.h" |
| 46 | #include "viewer-connection.h" |
| 47 | #include "lttng-viewer-abi.h" |
| 48 | #include "data-stream.h" |
| 49 | #include "metadata.h" |
| 50 | |
| 51 | #define viewer_handle_send_recv_status(_self_comp, _self_comp_class, \ |
| 52 | _status, _action, _msg_str) \ |
| 53 | do { \ |
| 54 | switch (_status) { \ |
| 55 | case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: \ |
| 56 | break; \ |
| 57 | case LTTNG_LIVE_VIEWER_STATUS_ERROR: \ |
| 58 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, \ |
| 59 | _self_comp_class, "Error " _action " " _msg_str); \ |
| 60 | break; \ |
| 61 | default: \ |
| 62 | bt_common_abort(); \ |
| 63 | } \ |
| 64 | } while (0) |
| 65 | |
| 66 | #define viewer_handle_send_status(_self_comp, _self_comp_class, _status, _msg_str) \ |
| 67 | viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, \ |
| 68 | "sending", _msg_str) |
| 69 | |
| 70 | #define viewer_handle_recv_status(_self_comp, _self_comp_class, _status, _msg_str) \ |
| 71 | viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, \ |
| 72 | "receiving", _msg_str) |
| 73 | |
| 74 | #define LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp, \ |
| 75 | _self_comp_class, _msg, _fmt, ...) \ |
| 76 | do { \ |
| 77 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, \ |
| 78 | _msg ": %s" _fmt, bt_socket_errormsg(), ##__VA_ARGS__); \ |
| 79 | } while (0) |
| 80 | |
| 81 | static inline |
| 82 | enum lttng_live_iterator_status viewer_status_to_live_iterator_status( |
| 83 | enum lttng_live_viewer_status viewer_status) |
| 84 | { |
| 85 | switch (viewer_status) { |
| 86 | case LTTNG_LIVE_VIEWER_STATUS_OK: |
| 87 | return LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 88 | case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: |
| 89 | return LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 90 | case LTTNG_LIVE_VIEWER_STATUS_ERROR: |
| 91 | return LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 92 | default: |
| 93 | bt_common_abort(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static inline |
| 98 | enum ctf_msg_iter_medium_status viewer_status_to_ctf_msg_iter_medium_status( |
| 99 | enum lttng_live_viewer_status viewer_status) |
| 100 | { |
| 101 | switch (viewer_status) { |
| 102 | case LTTNG_LIVE_VIEWER_STATUS_OK: |
| 103 | return CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 104 | case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: |
| 105 | return CTF_MSG_ITER_MEDIUM_STATUS_AGAIN; |
| 106 | case LTTNG_LIVE_VIEWER_STATUS_ERROR: |
| 107 | return CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 108 | default: |
| 109 | bt_common_abort(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * This function receives a message from the Relay daemon. |
| 115 | * If it received the entire message, it returns _OK, |
| 116 | * If it's interrupted, it returns _INTERRUPTED, |
| 117 | * otherwise, it returns _ERROR. |
| 118 | */ |
| 119 | static |
| 120 | enum lttng_live_viewer_status lttng_live_recv( |
| 121 | struct live_viewer_connection *viewer_connection, |
| 122 | void *buf, size_t len) |
| 123 | { |
| 124 | ssize_t received; |
| 125 | bt_self_component_class *self_comp_class = |
| 126 | viewer_connection->self_comp_class; |
| 127 | bt_self_component *self_comp = |
| 128 | viewer_connection->self_comp; |
| 129 | size_t total_received = 0, to_receive = len; |
| 130 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 131 | viewer_connection->lttng_live_msg_iter; |
| 132 | enum lttng_live_viewer_status status; |
| 133 | BT_SOCKET sock = viewer_connection->control_sock; |
| 134 | |
| 135 | /* |
| 136 | * Receive a message from the Relay. |
| 137 | */ |
| 138 | do { |
| 139 | received = bt_socket_recv(sock, buf + total_received, to_receive, 0); |
| 140 | if (received == BT_SOCKET_ERROR) { |
| 141 | if (bt_socket_interrupted()) { |
| 142 | if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { |
| 143 | /* |
| 144 | * This interruption was due to a |
| 145 | * SIGINT and the graph is being torn |
| 146 | * down. |
| 147 | */ |
| 148 | status = LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED; |
| 149 | lttng_live_msg_iter->was_interrupted = true; |
| 150 | goto end; |
| 151 | } else { |
| 152 | /* |
| 153 | * A signal was received, but the graph |
| 154 | * is not being torn down. Carry on. |
| 155 | */ |
| 156 | continue; |
| 157 | } |
| 158 | } else { |
| 159 | /* |
| 160 | * For any other types of socket error, returng |
| 161 | * an error. |
| 162 | */ |
| 163 | LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO( |
| 164 | self_comp, self_comp_class, |
| 165 | "Error receiving from Relay", "."); |
| 166 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 167 | goto end; |
| 168 | } |
| 169 | } else if (received == 0) { |
| 170 | /* |
| 171 | * The recv() call returned 0. This means the |
| 172 | * connection was orderly shutdown from the other peer. |
| 173 | * If that happens when we are trying to receive |
| 174 | * a message from it, it means something when wrong. |
| 175 | */ |
| 176 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 177 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 178 | self_comp_class, "Remote side has closed connection"); |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | BT_ASSERT(received <= to_receive); |
| 183 | total_received += received; |
| 184 | to_receive -= received; |
| 185 | |
| 186 | } while (to_receive > 0); |
| 187 | |
| 188 | BT_ASSERT(total_received == len); |
| 189 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 190 | |
| 191 | end: |
| 192 | return status; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * This function sends a message to the Relay daemon. |
| 197 | * If it send the message, it returns _OK, |
| 198 | * If it's interrupted, it returns _INTERRUPTED, |
| 199 | * otherwise, it returns _ERROR. |
| 200 | */ |
| 201 | static |
| 202 | enum lttng_live_viewer_status lttng_live_send( |
| 203 | struct live_viewer_connection *viewer_connection, |
| 204 | const void *buf, size_t len) |
| 205 | { |
| 206 | enum lttng_live_viewer_status status; |
| 207 | bt_self_component_class *self_comp_class = |
| 208 | viewer_connection->self_comp_class; |
| 209 | bt_self_component *self_comp = |
| 210 | viewer_connection->self_comp; |
| 211 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 212 | viewer_connection->lttng_live_msg_iter; |
| 213 | BT_SOCKET sock = viewer_connection->control_sock; |
| 214 | size_t to_send = len; |
| 215 | ssize_t total_sent = 0; |
| 216 | |
| 217 | do { |
| 218 | ssize_t sent = bt_socket_send_nosigpipe(sock, buf + total_sent, |
| 219 | to_send); |
| 220 | if (sent == BT_SOCKET_ERROR) { |
| 221 | if (bt_socket_interrupted()) { |
| 222 | if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { |
| 223 | /* |
| 224 | * This interruption was a SIGINT and |
| 225 | * the graph is being teared down. |
| 226 | */ |
| 227 | status = LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED; |
| 228 | lttng_live_msg_iter->was_interrupted = true; |
| 229 | goto end; |
| 230 | } else { |
| 231 | /* |
| 232 | * A signal was received, but the graph |
| 233 | * is not being teared down. Carry on. |
| 234 | */ |
| 235 | continue; |
| 236 | } |
| 237 | } else { |
| 238 | /* |
| 239 | * The send() call returned an error. |
| 240 | */ |
| 241 | LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO( |
| 242 | self_comp, self_comp_class, |
| 243 | "Error sending to Relay", "."); |
| 244 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 245 | goto end; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | BT_ASSERT(sent <= to_send); |
| 250 | total_sent += sent; |
| 251 | to_send -= sent; |
| 252 | |
| 253 | } while (to_send > 0); |
| 254 | |
| 255 | BT_ASSERT(total_sent == len); |
| 256 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 257 | |
| 258 | end: |
| 259 | return status; |
| 260 | } |
| 261 | |
| 262 | static |
| 263 | int parse_url(struct live_viewer_connection *viewer_connection) |
| 264 | { |
| 265 | char error_buf[256] = { 0 }; |
| 266 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 267 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 268 | struct bt_common_lttng_live_url_parts lttng_live_url_parts = { 0 }; |
| 269 | int ret = -1; |
| 270 | const char *path = viewer_connection->url->str; |
| 271 | |
| 272 | if (!path) { |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | lttng_live_url_parts = bt_common_parse_lttng_live_url(path, error_buf, |
| 277 | sizeof(error_buf)); |
| 278 | if (!lttng_live_url_parts.proto) { |
| 279 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 280 | self_comp_class,"Invalid LTTng live URL format: %s", |
| 281 | error_buf); |
| 282 | goto end; |
| 283 | } |
| 284 | viewer_connection->proto = lttng_live_url_parts.proto; |
| 285 | lttng_live_url_parts.proto = NULL; |
| 286 | |
| 287 | viewer_connection->relay_hostname = lttng_live_url_parts.hostname; |
| 288 | lttng_live_url_parts.hostname = NULL; |
| 289 | |
| 290 | if (lttng_live_url_parts.port >= 0) { |
| 291 | viewer_connection->port = lttng_live_url_parts.port; |
| 292 | } else { |
| 293 | viewer_connection->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT; |
| 294 | } |
| 295 | |
| 296 | viewer_connection->target_hostname = lttng_live_url_parts.target_hostname; |
| 297 | lttng_live_url_parts.target_hostname = NULL; |
| 298 | |
| 299 | if (lttng_live_url_parts.session_name) { |
| 300 | viewer_connection->session_name = lttng_live_url_parts.session_name; |
| 301 | lttng_live_url_parts.session_name = NULL; |
| 302 | } |
| 303 | |
| 304 | ret = 0; |
| 305 | |
| 306 | end: |
| 307 | bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts); |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static |
| 312 | enum lttng_live_viewer_status lttng_live_handshake( |
| 313 | struct live_viewer_connection *viewer_connection) |
| 314 | { |
| 315 | struct lttng_viewer_cmd cmd; |
| 316 | struct lttng_viewer_connect connect; |
| 317 | enum lttng_live_viewer_status status; |
| 318 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 319 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 320 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(connect); |
| 321 | char cmd_buf[cmd_buf_len]; |
| 322 | |
| 323 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 324 | "Handshaking with the Relay: " |
| 325 | "major-version=%u, minor-version=%u", |
| 326 | LTTNG_LIVE_MAJOR, LTTNG_LIVE_MINOR); |
| 327 | |
| 328 | cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); |
| 329 | cmd.data_size = htobe64((uint64_t) sizeof(connect)); |
| 330 | cmd.cmd_version = htobe32(0); |
| 331 | |
| 332 | connect.viewer_session_id = -1ULL; /* will be set on recv */ |
| 333 | connect.major = htobe32(LTTNG_LIVE_MAJOR); |
| 334 | connect.minor = htobe32(LTTNG_LIVE_MINOR); |
| 335 | connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); |
| 336 | |
| 337 | /* |
| 338 | * Merge the cmd and connection request to prevent a write-write |
| 339 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 340 | * second write to be performed quickly in presence of Nagle's algorithm |
| 341 | */ |
| 342 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 343 | memcpy(cmd_buf + sizeof(cmd), &connect, sizeof(connect)); |
| 344 | |
| 345 | status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 346 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 347 | viewer_handle_send_status(self_comp, self_comp_class, |
| 348 | status, "viewer connect command"); |
| 349 | goto end; |
| 350 | } |
| 351 | |
| 352 | status = lttng_live_recv(viewer_connection, &connect, sizeof(connect)); |
| 353 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 354 | viewer_handle_recv_status(self_comp, self_comp_class, |
| 355 | status, "viewer connect reply"); |
| 356 | goto end; |
| 357 | } |
| 358 | |
| 359 | BT_COMP_OR_COMP_CLASS_LOGI(self_comp, self_comp_class, |
| 360 | "Received viewer session ID : %" PRIu64, |
| 361 | (uint64_t) be64toh(connect.viewer_session_id)); |
| 362 | BT_COMP_OR_COMP_CLASS_LOGI(self_comp, self_comp_class, |
| 363 | "Relayd version : %u.%u", be32toh(connect.major), |
| 364 | be32toh(connect.minor)); |
| 365 | |
| 366 | if (LTTNG_LIVE_MAJOR != be32toh(connect.major)) { |
| 367 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 368 | self_comp_class, "Incompatible lttng-relayd protocol"); |
| 369 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 370 | goto end; |
| 371 | } |
| 372 | /* Use the smallest protocol version implemented. */ |
| 373 | if (LTTNG_LIVE_MINOR > be32toh(connect.minor)) { |
| 374 | viewer_connection->minor = be32toh(connect.minor); |
| 375 | } else { |
| 376 | viewer_connection->minor = LTTNG_LIVE_MINOR; |
| 377 | } |
| 378 | viewer_connection->major = LTTNG_LIVE_MAJOR; |
| 379 | |
| 380 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 381 | |
| 382 | goto end; |
| 383 | |
| 384 | end: |
| 385 | return status; |
| 386 | } |
| 387 | |
| 388 | static |
| 389 | enum lttng_live_viewer_status lttng_live_connect_viewer( |
| 390 | struct live_viewer_connection *viewer_connection) |
| 391 | { |
| 392 | struct hostent *host; |
| 393 | struct sockaddr_in server_addr; |
| 394 | enum lttng_live_viewer_status status; |
| 395 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 396 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 397 | |
| 398 | if (parse_url(viewer_connection)) { |
| 399 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 400 | self_comp_class, "Failed to parse URL"); |
| 401 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 402 | goto error; |
| 403 | } |
| 404 | |
| 405 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 406 | "Connecting to hostname : %s, port : %d, " |
| 407 | "target hostname : %s, session name : %s, proto : %s", |
| 408 | viewer_connection->relay_hostname->str, |
| 409 | viewer_connection->port, |
| 410 | !viewer_connection->target_hostname ? |
| 411 | "<none>" : viewer_connection->target_hostname->str, |
| 412 | !viewer_connection->session_name ? |
| 413 | "<none>" : viewer_connection->session_name->str, |
| 414 | viewer_connection->proto->str); |
| 415 | |
| 416 | host = gethostbyname(viewer_connection->relay_hostname->str); |
| 417 | if (!host) { |
| 418 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 419 | self_comp_class, "Cannot lookup hostname: hostname=\"%s\"", |
| 420 | viewer_connection->relay_hostname->str); |
| 421 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 422 | goto error; |
| 423 | } |
| 424 | |
| 425 | if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == BT_INVALID_SOCKET) { |
| 426 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 427 | self_comp_class, "Socket creation failed: %s", bt_socket_errormsg()); |
| 428 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 429 | goto error; |
| 430 | } |
| 431 | |
| 432 | server_addr.sin_family = AF_INET; |
| 433 | server_addr.sin_port = htons(viewer_connection->port); |
| 434 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); |
| 435 | memset(&(server_addr.sin_zero), 0, 8); |
| 436 | |
| 437 | if (connect(viewer_connection->control_sock, (struct sockaddr *) &server_addr, |
| 438 | sizeof(struct sockaddr)) == BT_SOCKET_ERROR) { |
| 439 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 440 | self_comp_class, "Connection failed: %s", |
| 441 | bt_socket_errormsg()); |
| 442 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 443 | goto error; |
| 444 | } |
| 445 | |
| 446 | status = lttng_live_handshake(viewer_connection); |
| 447 | |
| 448 | /* |
| 449 | * Only print error and append cause in case of error. not in case of |
| 450 | * interruption. |
| 451 | */ |
| 452 | if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 453 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 454 | self_comp_class, "Viewer handshake failed"); |
| 455 | goto error; |
| 456 | } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 457 | goto end; |
| 458 | } |
| 459 | |
| 460 | goto end; |
| 461 | |
| 462 | error: |
| 463 | if (viewer_connection->control_sock != BT_INVALID_SOCKET) { |
| 464 | if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { |
| 465 | BT_COMP_OR_COMP_CLASS_LOGW(self_comp, self_comp_class, |
| 466 | "Error closing socket: %s.", bt_socket_errormsg()); |
| 467 | } |
| 468 | } |
| 469 | viewer_connection->control_sock = BT_INVALID_SOCKET; |
| 470 | end: |
| 471 | return status; |
| 472 | } |
| 473 | |
| 474 | static |
| 475 | void lttng_live_disconnect_viewer( |
| 476 | struct live_viewer_connection *viewer_connection) |
| 477 | { |
| 478 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 479 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 480 | |
| 481 | if (viewer_connection->control_sock == BT_INVALID_SOCKET) { |
| 482 | return; |
| 483 | } |
| 484 | if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { |
| 485 | BT_COMP_OR_COMP_CLASS_LOGW(self_comp, self_comp_class, |
| 486 | "Error closing socket: %s", bt_socket_errormsg()); |
| 487 | viewer_connection->control_sock = BT_INVALID_SOCKET; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static |
| 492 | int list_update_session(bt_value *results, |
| 493 | const struct lttng_viewer_session *session, |
| 494 | bool *_found, struct live_viewer_connection *viewer_connection) |
| 495 | { |
| 496 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 497 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 498 | int ret = 0; |
| 499 | uint64_t i, len; |
| 500 | bt_value *map = NULL; |
| 501 | bt_value *hostname = NULL; |
| 502 | bt_value *session_name = NULL; |
| 503 | bt_value *btval = NULL; |
| 504 | bool found = false; |
| 505 | |
| 506 | len = bt_value_array_get_length(results); |
| 507 | for (i = 0; i < len; i++) { |
| 508 | const char *hostname_str = NULL; |
| 509 | const char *session_name_str = NULL; |
| 510 | |
| 511 | map = bt_value_array_borrow_element_by_index(results, i); |
| 512 | hostname = bt_value_map_borrow_entry_value(map, "target-hostname"); |
| 513 | if (!hostname) { |
| 514 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 515 | self_comp_class, |
| 516 | "Error borrowing \"target-hostname\" entry."); |
| 517 | ret = -1; |
| 518 | goto end; |
| 519 | } |
| 520 | session_name = bt_value_map_borrow_entry_value(map, "session-name"); |
| 521 | if (!session_name) { |
| 522 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 523 | self_comp_class, |
| 524 | "Error borrowing \"session-name\" entry."); |
| 525 | ret = -1; |
| 526 | goto end; |
| 527 | } |
| 528 | hostname_str = bt_value_string_get(hostname); |
| 529 | session_name_str = bt_value_string_get(session_name); |
| 530 | |
| 531 | if (strcmp(session->hostname, hostname_str) == 0 |
| 532 | && strcmp(session->session_name, session_name_str) == 0) { |
| 533 | int64_t val; |
| 534 | uint32_t streams = be32toh(session->streams); |
| 535 | uint32_t clients = be32toh(session->clients); |
| 536 | |
| 537 | found = true; |
| 538 | |
| 539 | btval = bt_value_map_borrow_entry_value(map, "stream-count"); |
| 540 | if (!btval) { |
| 541 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 542 | self_comp, self_comp_class, |
| 543 | "Error borrowing \"stream-count\" entry."); |
| 544 | ret = -1; |
| 545 | goto end; |
| 546 | } |
| 547 | val = bt_value_integer_unsigned_get(btval); |
| 548 | /* sum */ |
| 549 | val += streams; |
| 550 | bt_value_integer_unsigned_set(btval, val); |
| 551 | |
| 552 | btval = bt_value_map_borrow_entry_value(map, "client-count"); |
| 553 | if (!btval) { |
| 554 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 555 | self_comp, self_comp_class, |
| 556 | "Error borrowing \"client-count\" entry."); |
| 557 | ret = -1; |
| 558 | goto end; |
| 559 | } |
| 560 | val = bt_value_integer_unsigned_get(btval); |
| 561 | /* max */ |
| 562 | val = bt_max_t(int64_t, clients, val); |
| 563 | bt_value_integer_unsigned_set(btval, val); |
| 564 | } |
| 565 | |
| 566 | if (found) { |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | end: |
| 571 | *_found = found; |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | static |
| 576 | int list_append_session(bt_value *results, |
| 577 | GString *base_url, |
| 578 | const struct lttng_viewer_session *session, |
| 579 | struct live_viewer_connection *viewer_connection) |
| 580 | { |
| 581 | int ret = 0; |
| 582 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 583 | bt_value_map_insert_entry_status insert_status; |
| 584 | bt_value_array_append_element_status append_status; |
| 585 | bt_value *map = NULL; |
| 586 | GString *url = NULL; |
| 587 | bool found = false; |
| 588 | |
| 589 | /* |
| 590 | * If the session already exists, add the stream count to it, |
| 591 | * and do max of client counts. |
| 592 | */ |
| 593 | ret = list_update_session(results, session, &found, viewer_connection); |
| 594 | if (ret || found) { |
| 595 | goto end; |
| 596 | } |
| 597 | |
| 598 | map = bt_value_map_create(); |
| 599 | if (!map) { |
| 600 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 601 | "Error creating map value."); |
| 602 | ret = -1; |
| 603 | goto end; |
| 604 | } |
| 605 | |
| 606 | if (base_url->len < 1) { |
| 607 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 608 | "Error: base_url length smaller than 1."); |
| 609 | ret = -1; |
| 610 | goto end; |
| 611 | } |
| 612 | /* |
| 613 | * key = "url", |
| 614 | * value = <string>, |
| 615 | */ |
| 616 | url = g_string_new(base_url->str); |
| 617 | g_string_append(url, "/host/"); |
| 618 | g_string_append(url, session->hostname); |
| 619 | g_string_append_c(url, '/'); |
| 620 | g_string_append(url, session->session_name); |
| 621 | |
| 622 | insert_status = bt_value_map_insert_string_entry(map, "url", url->str); |
| 623 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 624 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 625 | "Error inserting \"url\" entry."); |
| 626 | ret = -1; |
| 627 | goto end; |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * key = "target-hostname", |
| 632 | * value = <string>, |
| 633 | */ |
| 634 | insert_status = bt_value_map_insert_string_entry(map, "target-hostname", |
| 635 | session->hostname); |
| 636 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 637 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 638 | "Error inserting \"target-hostname\" entry."); |
| 639 | ret = -1; |
| 640 | goto end; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * key = "session-name", |
| 645 | * value = <string>, |
| 646 | */ |
| 647 | insert_status = bt_value_map_insert_string_entry(map, "session-name", |
| 648 | session->session_name); |
| 649 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 650 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 651 | "Error inserting \"session-name\" entry."); |
| 652 | ret = -1; |
| 653 | goto end; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * key = "timer-us", |
| 658 | * value = <integer>, |
| 659 | */ |
| 660 | { |
| 661 | uint32_t live_timer = be32toh(session->live_timer); |
| 662 | |
| 663 | insert_status = bt_value_map_insert_unsigned_integer_entry( |
| 664 | map, "timer-us", live_timer); |
| 665 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 666 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 667 | "Error inserting \"timer-us\" entry."); |
| 668 | ret = -1; |
| 669 | goto end; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * key = "stream-count", |
| 675 | * value = <integer>, |
| 676 | */ |
| 677 | { |
| 678 | uint32_t streams = be32toh(session->streams); |
| 679 | |
| 680 | insert_status = bt_value_map_insert_unsigned_integer_entry(map, |
| 681 | "stream-count", streams); |
| 682 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 683 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 684 | "Error inserting \"stream-count\" entry."); |
| 685 | ret = -1; |
| 686 | goto end; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * key = "client-count", |
| 692 | * value = <integer>, |
| 693 | */ |
| 694 | { |
| 695 | uint32_t clients = be32toh(session->clients); |
| 696 | |
| 697 | insert_status = bt_value_map_insert_unsigned_integer_entry(map, |
| 698 | "client-count", clients); |
| 699 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 700 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 701 | "Error inserting \"client-count\" entry."); |
| 702 | ret = -1; |
| 703 | goto end; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | append_status = bt_value_array_append_element(results, map); |
| 708 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 709 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 710 | "Error appending map to results."); |
| 711 | ret = -1; |
| 712 | } |
| 713 | |
| 714 | end: |
| 715 | if (url) { |
| 716 | g_string_free(url, true); |
| 717 | } |
| 718 | BT_VALUE_PUT_REF_AND_RESET(map); |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Data structure returned: |
| 724 | * |
| 725 | * { |
| 726 | * <array> = { |
| 727 | * [n] = { |
| 728 | * <map> = { |
| 729 | * { |
| 730 | * key = "url", |
| 731 | * value = <string>, |
| 732 | * }, |
| 733 | * { |
| 734 | * key = "target-hostname", |
| 735 | * value = <string>, |
| 736 | * }, |
| 737 | * { |
| 738 | * key = "session-name", |
| 739 | * value = <string>, |
| 740 | * }, |
| 741 | * { |
| 742 | * key = "timer-us", |
| 743 | * value = <integer>, |
| 744 | * }, |
| 745 | * { |
| 746 | * key = "stream-count", |
| 747 | * value = <integer>, |
| 748 | * }, |
| 749 | * { |
| 750 | * key = "client-count", |
| 751 | * value = <integer>, |
| 752 | * }, |
| 753 | * }, |
| 754 | * } |
| 755 | * } |
| 756 | */ |
| 757 | |
| 758 | BT_HIDDEN |
| 759 | bt_component_class_query_method_status live_viewer_connection_list_sessions( |
| 760 | struct live_viewer_connection *viewer_connection, |
| 761 | const bt_value **user_result) |
| 762 | { |
| 763 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 764 | bt_component_class_query_method_status status = |
| 765 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 766 | bt_value *result = NULL; |
| 767 | enum lttng_live_viewer_status viewer_status; |
| 768 | struct lttng_viewer_cmd cmd; |
| 769 | struct lttng_viewer_list_sessions list; |
| 770 | uint32_t i, sessions_count; |
| 771 | |
| 772 | result = bt_value_array_create(); |
| 773 | if (!result) { |
| 774 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 775 | "Error creating array"); |
| 776 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 777 | goto error; |
| 778 | } |
| 779 | |
| 780 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); |
| 781 | cmd.data_size = htobe64((uint64_t) 0); |
| 782 | cmd.cmd_version = htobe32(0); |
| 783 | |
| 784 | viewer_status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); |
| 785 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 786 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 787 | "Error sending list sessions command"); |
| 788 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 789 | goto error; |
| 790 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 791 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; |
| 792 | goto error; |
| 793 | } |
| 794 | |
| 795 | viewer_status = lttng_live_recv(viewer_connection, &list, sizeof(list)); |
| 796 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 797 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 798 | "Error receiving session list"); |
| 799 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 800 | goto error; |
| 801 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 802 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; |
| 803 | goto error; |
| 804 | } |
| 805 | |
| 806 | sessions_count = be32toh(list.sessions_count); |
| 807 | for (i = 0; i < sessions_count; i++) { |
| 808 | struct lttng_viewer_session lsession; |
| 809 | |
| 810 | viewer_status = lttng_live_recv(viewer_connection, &lsession, |
| 811 | sizeof(lsession)); |
| 812 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 813 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 814 | "Error receiving session:"); |
| 815 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 816 | goto error; |
| 817 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 818 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; |
| 819 | goto error; |
| 820 | } |
| 821 | |
| 822 | lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0'; |
| 823 | lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; |
| 824 | if (list_append_session(result, viewer_connection->url, |
| 825 | &lsession, viewer_connection)) { |
| 826 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 827 | "Error appending session"); |
| 828 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 829 | goto error; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | *user_result = result; |
| 834 | goto end; |
| 835 | error: |
| 836 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 837 | end: |
| 838 | return status; |
| 839 | } |
| 840 | |
| 841 | static |
| 842 | enum lttng_live_viewer_status lttng_live_query_session_ids( |
| 843 | struct lttng_live_msg_iter *lttng_live_msg_iter) |
| 844 | { |
| 845 | struct lttng_viewer_cmd cmd; |
| 846 | struct lttng_viewer_list_sessions list; |
| 847 | struct lttng_viewer_session lsession; |
| 848 | uint32_t i, sessions_count; |
| 849 | uint64_t session_id; |
| 850 | enum lttng_live_viewer_status status; |
| 851 | struct live_viewer_connection *viewer_connection = |
| 852 | lttng_live_msg_iter->viewer_connection; |
| 853 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 854 | bt_self_component_class *self_comp_class = |
| 855 | viewer_connection->self_comp_class; |
| 856 | |
| 857 | BT_COMP_LOGD("Asking the Relay for the list of sessions"); |
| 858 | |
| 859 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); |
| 860 | cmd.data_size = htobe64((uint64_t) 0); |
| 861 | cmd.cmd_version = htobe32(0); |
| 862 | |
| 863 | status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); |
| 864 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 865 | viewer_handle_send_status(self_comp, self_comp_class, |
| 866 | status, "list sessions command"); |
| 867 | goto end; |
| 868 | } |
| 869 | |
| 870 | status = lttng_live_recv(viewer_connection, &list, sizeof(list)); |
| 871 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 872 | viewer_handle_recv_status(self_comp, self_comp_class, |
| 873 | status, "session list reply"); |
| 874 | goto end; |
| 875 | } |
| 876 | |
| 877 | sessions_count = be32toh(list.sessions_count); |
| 878 | for (i = 0; i < sessions_count; i++) { |
| 879 | status = lttng_live_recv(viewer_connection, &lsession, |
| 880 | sizeof(lsession)); |
| 881 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 882 | viewer_handle_recv_status(self_comp, self_comp_class, |
| 883 | status, "session reply"); |
| 884 | goto end; |
| 885 | } |
| 886 | lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0'; |
| 887 | lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; |
| 888 | session_id = be64toh(lsession.id); |
| 889 | |
| 890 | BT_COMP_LOGI("Adding session to internal list: " |
| 891 | "session-id=%" PRIu64 ", hostname=\"%s\", session-name=\"%s\"", |
| 892 | session_id, lsession.hostname, lsession.session_name); |
| 893 | |
| 894 | if ((strncmp(lsession.session_name, |
| 895 | viewer_connection->session_name->str, |
| 896 | LTTNG_VIEWER_NAME_MAX) == 0) && (strncmp(lsession.hostname, |
| 897 | viewer_connection->target_hostname->str, |
| 898 | LTTNG_VIEWER_HOST_NAME_MAX) == 0)) { |
| 899 | |
| 900 | if (lttng_live_add_session(lttng_live_msg_iter, session_id, |
| 901 | lsession.hostname, |
| 902 | lsession.session_name)) { |
| 903 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 904 | "Failed to add live session"); |
| 905 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 906 | goto end; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 912 | |
| 913 | end: |
| 914 | return status; |
| 915 | } |
| 916 | |
| 917 | BT_HIDDEN |
| 918 | enum lttng_live_viewer_status lttng_live_create_viewer_session( |
| 919 | struct lttng_live_msg_iter *lttng_live_msg_iter) |
| 920 | { |
| 921 | struct lttng_viewer_cmd cmd; |
| 922 | struct lttng_viewer_create_session_response resp; |
| 923 | enum lttng_live_viewer_status status; |
| 924 | struct live_viewer_connection *viewer_connection = |
| 925 | lttng_live_msg_iter->viewer_connection; |
| 926 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 927 | bt_self_component_class *self_comp_class = |
| 928 | viewer_connection->self_comp_class; |
| 929 | |
| 930 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 931 | "Creating a viewer session"); |
| 932 | |
| 933 | cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION); |
| 934 | cmd.data_size = htobe64((uint64_t) 0); |
| 935 | cmd.cmd_version = htobe32(0); |
| 936 | |
| 937 | status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); |
| 938 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 939 | viewer_handle_send_status(self_comp, self_comp_class, |
| 940 | status, "create session command"); |
| 941 | goto end; |
| 942 | } |
| 943 | |
| 944 | status = lttng_live_recv(viewer_connection, &resp, sizeof(resp)); |
| 945 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 946 | viewer_handle_recv_status(self_comp, self_comp_class, |
| 947 | status, "create session reply"); |
| 948 | goto end; |
| 949 | } |
| 950 | |
| 951 | if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) { |
| 952 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 953 | "Error creating viewer session"); |
| 954 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 955 | goto end; |
| 956 | } |
| 957 | |
| 958 | status = lttng_live_query_session_ids(lttng_live_msg_iter); |
| 959 | if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 960 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 961 | "Failed to query live viewer session ids"); |
| 962 | goto end; |
| 963 | } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 964 | goto end; |
| 965 | } |
| 966 | |
| 967 | end: |
| 968 | return status; |
| 969 | } |
| 970 | |
| 971 | static |
| 972 | enum lttng_live_viewer_status receive_streams(struct lttng_live_session *session, |
| 973 | uint32_t stream_count, |
| 974 | bt_self_message_iterator *self_msg_iter) |
| 975 | { |
| 976 | uint32_t i; |
| 977 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 978 | session->lttng_live_msg_iter; |
| 979 | enum lttng_live_viewer_status status; |
| 980 | struct live_viewer_connection *viewer_connection = |
| 981 | lttng_live_msg_iter->viewer_connection; |
| 982 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 983 | |
| 984 | BT_COMP_LOGI("Getting %" PRIu32 " new streams:", stream_count); |
| 985 | for (i = 0; i < stream_count; i++) { |
| 986 | struct lttng_viewer_stream stream; |
| 987 | struct lttng_live_stream_iterator *live_stream; |
| 988 | uint64_t stream_id; |
| 989 | uint64_t ctf_trace_id; |
| 990 | |
| 991 | status = lttng_live_recv(viewer_connection, &stream, |
| 992 | sizeof(stream)); |
| 993 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 994 | viewer_handle_recv_status(self_comp, NULL, |
| 995 | status, "stream reply"); |
| 996 | goto end; |
| 997 | } |
| 998 | stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0'; |
| 999 | stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; |
| 1000 | stream_id = be64toh(stream.id); |
| 1001 | ctf_trace_id = be64toh(stream.ctf_trace_id); |
| 1002 | |
| 1003 | if (stream.metadata_flag) { |
| 1004 | BT_COMP_LOGI(" metadata stream %" PRIu64 " : %s/%s", |
| 1005 | stream_id, stream.path_name, stream.channel_name); |
| 1006 | if (lttng_live_metadata_create_stream(session, |
| 1007 | ctf_trace_id, stream_id, |
| 1008 | stream.path_name)) { |
| 1009 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1010 | "Error creating metadata stream"); |
| 1011 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1012 | goto end; |
| 1013 | } |
| 1014 | session->lazy_stream_msg_init = true; |
| 1015 | } else { |
| 1016 | BT_COMP_LOGI(" stream %" PRIu64 " : %s/%s", |
| 1017 | stream_id, stream.path_name, stream.channel_name); |
| 1018 | live_stream = lttng_live_stream_iterator_create(session, |
| 1019 | ctf_trace_id, stream_id, self_msg_iter); |
| 1020 | if (!live_stream) { |
| 1021 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1022 | "Error creating stream"); |
| 1023 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1024 | goto end; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 1029 | |
| 1030 | end: |
| 1031 | return status; |
| 1032 | } |
| 1033 | |
| 1034 | BT_HIDDEN |
| 1035 | enum lttng_live_viewer_status lttng_live_attach_session( |
| 1036 | struct lttng_live_session *session, |
| 1037 | bt_self_message_iterator *self_msg_iter) |
| 1038 | { |
| 1039 | struct lttng_viewer_cmd cmd; |
| 1040 | enum lttng_live_viewer_status status; |
| 1041 | struct lttng_viewer_attach_session_request rq; |
| 1042 | struct lttng_viewer_attach_session_response rp; |
| 1043 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 1044 | session->lttng_live_msg_iter; |
| 1045 | struct live_viewer_connection *viewer_connection = |
| 1046 | lttng_live_msg_iter->viewer_connection; |
| 1047 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1048 | uint64_t session_id = session->id; |
| 1049 | uint32_t streams_count; |
| 1050 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1051 | char cmd_buf[cmd_buf_len]; |
| 1052 | |
| 1053 | BT_COMP_LOGD("Attaching to session: session-id=%"PRIu64, session_id); |
| 1054 | |
| 1055 | cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); |
| 1056 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1057 | cmd.cmd_version = htobe32(0); |
| 1058 | |
| 1059 | memset(&rq, 0, sizeof(rq)); |
| 1060 | rq.session_id = htobe64(session_id); |
| 1061 | // TODO: add cmd line parameter to select seek beginning |
| 1062 | // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING); |
| 1063 | rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST); |
| 1064 | |
| 1065 | /* |
| 1066 | * Merge the cmd and connection request to prevent a write-write |
| 1067 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1068 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1069 | */ |
| 1070 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1071 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1072 | status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1073 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1074 | viewer_handle_send_status(self_comp, NULL, |
| 1075 | status, "attach session command"); |
| 1076 | goto end; |
| 1077 | } |
| 1078 | |
| 1079 | status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1080 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1081 | viewer_handle_recv_status(self_comp, NULL, |
| 1082 | status, "attach session reply"); |
| 1083 | goto end; |
| 1084 | } |
| 1085 | |
| 1086 | streams_count = be32toh(rp.streams_count); |
| 1087 | switch(be32toh(rp.status)) { |
| 1088 | case LTTNG_VIEWER_ATTACH_OK: |
| 1089 | break; |
| 1090 | case LTTNG_VIEWER_ATTACH_UNK: |
| 1091 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1092 | "Session id %" PRIu64 " is unknown", session_id); |
| 1093 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1094 | goto end; |
| 1095 | case LTTNG_VIEWER_ATTACH_ALREADY: |
| 1096 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1097 | "There is already a viewer attached to this session"); |
| 1098 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1099 | goto end; |
| 1100 | case LTTNG_VIEWER_ATTACH_NOT_LIVE: |
| 1101 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Not a live session"); |
| 1102 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1103 | goto end; |
| 1104 | case LTTNG_VIEWER_ATTACH_SEEK_ERR: |
| 1105 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Wrong seek parameter"); |
| 1106 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1107 | goto end; |
| 1108 | default: |
| 1109 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1110 | "Unknown attach return code %u", be32toh(rp.status)); |
| 1111 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1112 | goto end; |
| 1113 | } |
| 1114 | |
| 1115 | /* We receive the initial list of streams. */ |
| 1116 | status = receive_streams(session, streams_count, self_msg_iter); |
| 1117 | switch (status) { |
| 1118 | case LTTNG_LIVE_VIEWER_STATUS_OK: |
| 1119 | break; |
| 1120 | case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: |
| 1121 | goto end; |
| 1122 | case LTTNG_LIVE_VIEWER_STATUS_ERROR: |
| 1123 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error receiving streams"); |
| 1124 | goto end; |
| 1125 | default: |
| 1126 | bt_common_abort(); |
| 1127 | } |
| 1128 | |
| 1129 | session->attached = true; |
| 1130 | session->new_streams_needed = false; |
| 1131 | |
| 1132 | end: |
| 1133 | return status; |
| 1134 | } |
| 1135 | |
| 1136 | BT_HIDDEN |
| 1137 | enum lttng_live_viewer_status lttng_live_detach_session( |
| 1138 | struct lttng_live_session *session) |
| 1139 | { |
| 1140 | struct lttng_viewer_cmd cmd; |
| 1141 | enum lttng_live_viewer_status status; |
| 1142 | struct lttng_viewer_detach_session_request rq; |
| 1143 | struct lttng_viewer_detach_session_response rp; |
| 1144 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 1145 | session->lttng_live_msg_iter; |
| 1146 | bt_self_component *self_comp = session->self_comp; |
| 1147 | struct live_viewer_connection *viewer_connection = |
| 1148 | lttng_live_msg_iter->viewer_connection; |
| 1149 | uint64_t session_id = session->id; |
| 1150 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1151 | char cmd_buf[cmd_buf_len]; |
| 1152 | |
| 1153 | if (!session->attached) { |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION); |
| 1158 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1159 | cmd.cmd_version = htobe32(0); |
| 1160 | |
| 1161 | memset(&rq, 0, sizeof(rq)); |
| 1162 | rq.session_id = htobe64(session_id); |
| 1163 | |
| 1164 | /* |
| 1165 | * Merge the cmd and connection request to prevent a write-write |
| 1166 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1167 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1168 | */ |
| 1169 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1170 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1171 | status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1172 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1173 | viewer_handle_send_status(self_comp, NULL, |
| 1174 | status, "detach session command"); |
| 1175 | goto end; |
| 1176 | } |
| 1177 | |
| 1178 | status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1179 | if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1180 | viewer_handle_recv_status(self_comp, NULL, |
| 1181 | status, "detach session reply"); |
| 1182 | goto end; |
| 1183 | } |
| 1184 | |
| 1185 | switch(be32toh(rp.status)) { |
| 1186 | case LTTNG_VIEWER_DETACH_SESSION_OK: |
| 1187 | break; |
| 1188 | case LTTNG_VIEWER_DETACH_SESSION_UNK: |
| 1189 | BT_COMP_LOGW("Session id %" PRIu64 " is unknown", session_id); |
| 1190 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1191 | goto end; |
| 1192 | case LTTNG_VIEWER_DETACH_SESSION_ERR: |
| 1193 | BT_COMP_LOGW("Error detaching session id %" PRIu64 "", session_id); |
| 1194 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1195 | goto end; |
| 1196 | default: |
| 1197 | BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp.status)); |
| 1198 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1199 | goto end; |
| 1200 | } |
| 1201 | |
| 1202 | session->attached = false; |
| 1203 | |
| 1204 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 1205 | |
| 1206 | end: |
| 1207 | return status; |
| 1208 | } |
| 1209 | |
| 1210 | BT_HIDDEN |
| 1211 | enum lttng_live_get_one_metadata_status lttng_live_get_one_metadata_packet( |
| 1212 | struct lttng_live_trace *trace, FILE *fp, size_t *reply_len) |
| 1213 | { |
| 1214 | uint64_t len = 0; |
| 1215 | enum lttng_live_get_one_metadata_status status; |
| 1216 | enum lttng_live_viewer_status viewer_status; |
| 1217 | struct lttng_viewer_cmd cmd; |
| 1218 | struct lttng_viewer_get_metadata rq; |
| 1219 | struct lttng_viewer_metadata_packet rp; |
| 1220 | char *data = NULL; |
| 1221 | ssize_t ret_len; |
| 1222 | struct lttng_live_session *session = trace->session; |
| 1223 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 1224 | session->lttng_live_msg_iter; |
| 1225 | struct lttng_live_metadata *metadata = trace->metadata; |
| 1226 | struct live_viewer_connection *viewer_connection = |
| 1227 | lttng_live_msg_iter->viewer_connection; |
| 1228 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1229 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1230 | char cmd_buf[cmd_buf_len]; |
| 1231 | |
| 1232 | BT_COMP_LOGD("Requesting new metadata for trace: " |
| 1233 | "trace-id=%"PRIu64", metadata-stream-id=%"PRIu64, |
| 1234 | trace->id, metadata->stream_id); |
| 1235 | |
| 1236 | rq.stream_id = htobe64(metadata->stream_id); |
| 1237 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA); |
| 1238 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1239 | cmd.cmd_version = htobe32(0); |
| 1240 | |
| 1241 | /* |
| 1242 | * Merge the cmd and connection request to prevent a write-write |
| 1243 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1244 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1245 | */ |
| 1246 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1247 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1248 | viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1249 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1250 | viewer_handle_send_status(self_comp, NULL, |
| 1251 | viewer_status, "get metadata command"); |
| 1252 | status = (enum lttng_live_get_one_metadata_status) viewer_status; |
| 1253 | goto end; |
| 1254 | } |
| 1255 | |
| 1256 | viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1257 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1258 | viewer_handle_recv_status(self_comp, NULL, |
| 1259 | viewer_status, "get metadata reply"); |
| 1260 | status = (enum lttng_live_get_one_metadata_status) viewer_status; |
| 1261 | goto end; |
| 1262 | } |
| 1263 | |
| 1264 | switch (be32toh(rp.status)) { |
| 1265 | case LTTNG_VIEWER_METADATA_OK: |
| 1266 | BT_COMP_LOGD("Received get_metadata response: ok"); |
| 1267 | break; |
| 1268 | case LTTNG_VIEWER_NO_NEW_METADATA: |
| 1269 | BT_COMP_LOGD("Received get_metadata response: no new"); |
| 1270 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_END; |
| 1271 | goto end; |
| 1272 | case LTTNG_VIEWER_METADATA_ERR: |
| 1273 | /* |
| 1274 | * The Relayd cannot find this stream id. Maybe its |
| 1275 | * gone already. This can happen in short lived UST app |
| 1276 | * in a per-pid session. |
| 1277 | */ |
| 1278 | BT_COMP_LOGD("Received get_metadata response: error"); |
| 1279 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED; |
| 1280 | goto end; |
| 1281 | default: |
| 1282 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1283 | "Received get_metadata response: unknown"); |
| 1284 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; |
| 1285 | goto error; |
| 1286 | } |
| 1287 | |
| 1288 | len = be64toh(rp.len); |
| 1289 | BT_COMP_LOGD("Writing %" PRIu64" bytes to metadata", len); |
| 1290 | if (len <= 0) { |
| 1291 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1292 | "Erroneous response length"); |
| 1293 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; |
| 1294 | goto error; |
| 1295 | } |
| 1296 | |
| 1297 | data = calloc(1, len); |
| 1298 | if (!data) { |
| 1299 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, |
| 1300 | "Failed to allocate data buffer", "."); |
| 1301 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; |
| 1302 | goto error; |
| 1303 | } |
| 1304 | |
| 1305 | viewer_status = lttng_live_recv(viewer_connection, data, len); |
| 1306 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1307 | viewer_handle_recv_status(self_comp, NULL, |
| 1308 | viewer_status, "get metadata packet"); |
| 1309 | status = (enum lttng_live_get_one_metadata_status) viewer_status; |
| 1310 | goto error; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * Write the metadata to the file handle. |
| 1315 | */ |
| 1316 | do { |
| 1317 | ret_len = fwrite(data, 1, len, fp); |
| 1318 | } while (ret_len < 0 && errno == EINTR); |
| 1319 | if (ret_len < 0) { |
| 1320 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1321 | "Writing in the metadata file stream"); |
| 1322 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; |
| 1323 | goto error; |
| 1324 | } |
| 1325 | BT_ASSERT(ret_len == len); |
| 1326 | *reply_len = len; |
| 1327 | status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK; |
| 1328 | |
| 1329 | error: |
| 1330 | free(data); |
| 1331 | |
| 1332 | end: |
| 1333 | return status; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Assign the fields from a lttng_viewer_index to a packet_index. |
| 1338 | */ |
| 1339 | static |
| 1340 | void lttng_index_to_packet_index(struct lttng_viewer_index *lindex, |
| 1341 | struct packet_index *pindex) |
| 1342 | { |
| 1343 | BT_ASSERT(lindex); |
| 1344 | BT_ASSERT(pindex); |
| 1345 | |
| 1346 | pindex->offset = be64toh(lindex->offset); |
| 1347 | pindex->packet_size = be64toh(lindex->packet_size); |
| 1348 | pindex->content_size = be64toh(lindex->content_size); |
| 1349 | pindex->ts_cycles.timestamp_begin = be64toh(lindex->timestamp_begin); |
| 1350 | pindex->ts_cycles.timestamp_end = be64toh(lindex->timestamp_end); |
| 1351 | pindex->events_discarded = be64toh(lindex->events_discarded); |
| 1352 | } |
| 1353 | |
| 1354 | BT_HIDDEN |
| 1355 | enum lttng_live_iterator_status lttng_live_get_next_index( |
| 1356 | struct lttng_live_msg_iter *lttng_live_msg_iter, |
| 1357 | struct lttng_live_stream_iterator *stream, |
| 1358 | struct packet_index *index) |
| 1359 | { |
| 1360 | struct lttng_viewer_cmd cmd; |
| 1361 | struct lttng_viewer_get_next_index rq; |
| 1362 | enum lttng_live_viewer_status viewer_status; |
| 1363 | struct lttng_viewer_index rp; |
| 1364 | enum lttng_live_iterator_status status; |
| 1365 | struct live_viewer_connection *viewer_connection = |
| 1366 | lttng_live_msg_iter->viewer_connection; |
| 1367 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1368 | struct lttng_live_trace *trace = stream->trace; |
| 1369 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1370 | char cmd_buf[cmd_buf_len]; |
| 1371 | uint32_t flags, rp_status; |
| 1372 | |
| 1373 | BT_COMP_LOGD("Requesting next index for stream: " |
| 1374 | "stream-id=%"PRIu64, stream->viewer_stream_id); |
| 1375 | |
| 1376 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX); |
| 1377 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1378 | cmd.cmd_version = htobe32(0); |
| 1379 | |
| 1380 | |
| 1381 | memset(&rq, 0, sizeof(rq)); |
| 1382 | rq.stream_id = htobe64(stream->viewer_stream_id); |
| 1383 | |
| 1384 | /* |
| 1385 | * Merge the cmd and connection request to prevent a write-write |
| 1386 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1387 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1388 | */ |
| 1389 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1390 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1391 | viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1392 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1393 | viewer_handle_send_status(self_comp, NULL, |
| 1394 | viewer_status, "get next index command"); |
| 1395 | goto error; |
| 1396 | } |
| 1397 | |
| 1398 | viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1399 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1400 | viewer_handle_recv_status(self_comp, NULL, |
| 1401 | viewer_status, "get next index reply"); |
| 1402 | goto error; |
| 1403 | } |
| 1404 | |
| 1405 | flags = be32toh(rp.flags); |
| 1406 | rp_status = be32toh(rp.status); |
| 1407 | |
| 1408 | switch (rp_status) { |
| 1409 | case LTTNG_VIEWER_INDEX_INACTIVE: |
| 1410 | { |
| 1411 | uint64_t ctf_stream_class_id; |
| 1412 | |
| 1413 | BT_COMP_LOGD("Received get_next_index response: inactive"); |
| 1414 | memset(index, 0, sizeof(struct packet_index)); |
| 1415 | index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end); |
| 1416 | stream->current_inactivity_ts = index->ts_cycles.timestamp_end; |
| 1417 | ctf_stream_class_id = be64toh(rp.stream_id); |
| 1418 | if (stream->ctf_stream_class_id != -1ULL) { |
| 1419 | BT_ASSERT(stream->ctf_stream_class_id == |
| 1420 | ctf_stream_class_id); |
| 1421 | } else { |
| 1422 | stream->ctf_stream_class_id = ctf_stream_class_id; |
| 1423 | } |
| 1424 | stream->state = LTTNG_LIVE_STREAM_QUIESCENT; |
| 1425 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 1426 | break; |
| 1427 | } |
| 1428 | case LTTNG_VIEWER_INDEX_OK: |
| 1429 | { |
| 1430 | uint64_t ctf_stream_class_id; |
| 1431 | |
| 1432 | BT_COMP_LOGD("Received get_next_index response: OK"); |
| 1433 | lttng_index_to_packet_index(&rp, index); |
| 1434 | ctf_stream_class_id = be64toh(rp.stream_id); |
| 1435 | if (stream->ctf_stream_class_id != -1ULL) { |
| 1436 | BT_ASSERT(stream->ctf_stream_class_id == |
| 1437 | ctf_stream_class_id); |
| 1438 | } else { |
| 1439 | stream->ctf_stream_class_id = ctf_stream_class_id; |
| 1440 | } |
| 1441 | |
| 1442 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_DATA; |
| 1443 | |
| 1444 | if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { |
| 1445 | BT_COMP_LOGD("Received get_next_index response: new metadata needed"); |
| 1446 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; |
| 1447 | } |
| 1448 | if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { |
| 1449 | BT_COMP_LOGD("Received get_next_index response: new streams needed"); |
| 1450 | lttng_live_need_new_streams(lttng_live_msg_iter); |
| 1451 | } |
| 1452 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 1453 | break; |
| 1454 | } |
| 1455 | case LTTNG_VIEWER_INDEX_RETRY: |
| 1456 | BT_COMP_LOGD("Received get_next_index response: retry"); |
| 1457 | memset(index, 0, sizeof(struct packet_index)); |
| 1458 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; |
| 1459 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
| 1460 | goto end; |
| 1461 | case LTTNG_VIEWER_INDEX_HUP: |
| 1462 | BT_COMP_LOGD("Received get_next_index response: stream hung up"); |
| 1463 | memset(index, 0, sizeof(struct packet_index)); |
| 1464 | index->offset = EOF; |
| 1465 | stream->state = LTTNG_LIVE_STREAM_EOF; |
| 1466 | stream->has_stream_hung_up = true; |
| 1467 | status = LTTNG_LIVE_ITERATOR_STATUS_END; |
| 1468 | break; |
| 1469 | case LTTNG_VIEWER_INDEX_ERR: |
| 1470 | BT_COMP_LOGD("Received get_next_index response: error"); |
| 1471 | memset(index, 0, sizeof(struct packet_index)); |
| 1472 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; |
| 1473 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 1474 | goto end; |
| 1475 | default: |
| 1476 | BT_COMP_LOGD("Received get_next_index response: unknown value"); |
| 1477 | memset(index, 0, sizeof(struct packet_index)); |
| 1478 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; |
| 1479 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 1480 | goto end; |
| 1481 | } |
| 1482 | goto end; |
| 1483 | |
| 1484 | error: |
| 1485 | status = viewer_status_to_live_iterator_status(viewer_status); |
| 1486 | end: |
| 1487 | return status; |
| 1488 | } |
| 1489 | |
| 1490 | BT_HIDDEN |
| 1491 | enum ctf_msg_iter_medium_status lttng_live_get_stream_bytes( |
| 1492 | struct lttng_live_msg_iter *lttng_live_msg_iter, |
| 1493 | struct lttng_live_stream_iterator *stream, uint8_t *buf, |
| 1494 | uint64_t offset, uint64_t req_len, uint64_t *recv_len) |
| 1495 | { |
| 1496 | enum ctf_msg_iter_medium_status status; |
| 1497 | enum lttng_live_viewer_status viewer_status; |
| 1498 | struct lttng_viewer_trace_packet rp; |
| 1499 | struct lttng_viewer_cmd cmd; |
| 1500 | struct lttng_viewer_get_packet rq; |
| 1501 | struct live_viewer_connection *viewer_connection = |
| 1502 | lttng_live_msg_iter->viewer_connection; |
| 1503 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1504 | struct lttng_live_trace *trace = stream->trace; |
| 1505 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1506 | char cmd_buf[cmd_buf_len]; |
| 1507 | uint32_t flags, rp_status; |
| 1508 | |
| 1509 | BT_COMP_LOGD("lttng_live_get_stream_bytes: offset=%" PRIu64 ", req_len=%" PRIu64, |
| 1510 | offset, req_len); |
| 1511 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET); |
| 1512 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1513 | cmd.cmd_version = htobe32(0); |
| 1514 | |
| 1515 | memset(&rq, 0, sizeof(rq)); |
| 1516 | rq.stream_id = htobe64(stream->viewer_stream_id); |
| 1517 | rq.offset = htobe64(offset); |
| 1518 | rq.len = htobe32(req_len); |
| 1519 | |
| 1520 | /* |
| 1521 | * Merge the cmd and connection request to prevent a write-write |
| 1522 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1523 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1524 | */ |
| 1525 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1526 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1527 | viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1528 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1529 | viewer_handle_send_status(self_comp, NULL, |
| 1530 | viewer_status, "get data packet command"); |
| 1531 | goto error; |
| 1532 | } |
| 1533 | |
| 1534 | viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1535 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1536 | viewer_handle_recv_status(self_comp, NULL, |
| 1537 | viewer_status, "get data packet reply"); |
| 1538 | goto error; |
| 1539 | } |
| 1540 | |
| 1541 | flags = be32toh(rp.flags); |
| 1542 | rp_status = be32toh(rp.status); |
| 1543 | |
| 1544 | switch (rp_status) { |
| 1545 | case LTTNG_VIEWER_GET_PACKET_OK: |
| 1546 | req_len = be32toh(rp.len); |
| 1547 | BT_COMP_LOGD("Received get_data_packet response: Ok, " |
| 1548 | "packet size : %" PRIu64 "", req_len); |
| 1549 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 1550 | break; |
| 1551 | case LTTNG_VIEWER_GET_PACKET_RETRY: |
| 1552 | /* Unimplemented by relay daemon */ |
| 1553 | BT_COMP_LOGD("Received get_data_packet response: retry"); |
| 1554 | status = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN; |
| 1555 | goto end; |
| 1556 | case LTTNG_VIEWER_GET_PACKET_ERR: |
| 1557 | if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { |
| 1558 | BT_COMP_LOGD("get_data_packet: new metadata needed, try again later"); |
| 1559 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; |
| 1560 | } |
| 1561 | if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { |
| 1562 | BT_COMP_LOGD("get_data_packet: new streams needed, try again later"); |
| 1563 | lttng_live_need_new_streams(lttng_live_msg_iter); |
| 1564 | } |
| 1565 | if (flags & (LTTNG_VIEWER_FLAG_NEW_METADATA |
| 1566 | | LTTNG_VIEWER_FLAG_NEW_STREAM)) { |
| 1567 | status = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN; |
| 1568 | goto end; |
| 1569 | } |
| 1570 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1571 | "Received get_data_packet response: error"); |
| 1572 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 1573 | goto end; |
| 1574 | case LTTNG_VIEWER_GET_PACKET_EOF: |
| 1575 | status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; |
| 1576 | goto end; |
| 1577 | default: |
| 1578 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1579 | "Received get_data_packet response: unknown"); |
| 1580 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 1581 | goto error; |
| 1582 | } |
| 1583 | |
| 1584 | if (req_len == 0) { |
| 1585 | status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; |
| 1586 | goto error; |
| 1587 | } |
| 1588 | |
| 1589 | viewer_status = lttng_live_recv(viewer_connection, buf, req_len); |
| 1590 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1591 | viewer_handle_recv_status(self_comp, NULL, |
| 1592 | viewer_status, "get data packet"); |
| 1593 | goto error; |
| 1594 | } |
| 1595 | *recv_len = req_len; |
| 1596 | |
| 1597 | status = CTF_MSG_ITER_MEDIUM_STATUS_OK; |
| 1598 | goto end; |
| 1599 | error: |
| 1600 | |
| 1601 | status = viewer_status_to_ctf_msg_iter_medium_status(viewer_status); |
| 1602 | end: |
| 1603 | return status; |
| 1604 | } |
| 1605 | |
| 1606 | /* |
| 1607 | * Request new streams for a session. |
| 1608 | */ |
| 1609 | BT_HIDDEN |
| 1610 | enum lttng_live_iterator_status lttng_live_get_new_streams( |
| 1611 | struct lttng_live_session *session, |
| 1612 | bt_self_message_iterator *self_msg_iter) |
| 1613 | { |
| 1614 | enum lttng_live_iterator_status status = |
| 1615 | LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 1616 | struct lttng_viewer_cmd cmd; |
| 1617 | struct lttng_viewer_new_streams_request rq; |
| 1618 | struct lttng_viewer_new_streams_response rp; |
| 1619 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
| 1620 | session->lttng_live_msg_iter; |
| 1621 | enum lttng_live_viewer_status viewer_status; |
| 1622 | struct live_viewer_connection *viewer_connection = |
| 1623 | lttng_live_msg_iter->viewer_connection; |
| 1624 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1625 | uint32_t streams_count; |
| 1626 | const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); |
| 1627 | char cmd_buf[cmd_buf_len]; |
| 1628 | |
| 1629 | if (!session->new_streams_needed) { |
| 1630 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 1631 | goto end; |
| 1632 | } |
| 1633 | |
| 1634 | BT_COMP_LOGD("Requesting new streams for session: " |
| 1635 | "session-id=%"PRIu64, session->id); |
| 1636 | |
| 1637 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS); |
| 1638 | cmd.data_size = htobe64((uint64_t) sizeof(rq)); |
| 1639 | cmd.cmd_version = htobe32(0); |
| 1640 | |
| 1641 | memset(&rq, 0, sizeof(rq)); |
| 1642 | rq.session_id = htobe64(session->id); |
| 1643 | |
| 1644 | /* |
| 1645 | * Merge the cmd and connection request to prevent a write-write |
| 1646 | * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the |
| 1647 | * second write to be performed quickly in presence of Nagle's algorithm. |
| 1648 | */ |
| 1649 | memcpy(cmd_buf, &cmd, sizeof(cmd)); |
| 1650 | memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); |
| 1651 | viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); |
| 1652 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1653 | viewer_handle_send_status(self_comp, NULL, |
| 1654 | viewer_status, "get new streams command"); |
| 1655 | status = viewer_status_to_live_iterator_status(viewer_status); |
| 1656 | goto end; |
| 1657 | } |
| 1658 | |
| 1659 | viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); |
| 1660 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1661 | viewer_handle_recv_status(self_comp, NULL, |
| 1662 | viewer_status, "get new streams reply"); |
| 1663 | status = viewer_status_to_live_iterator_status(viewer_status); |
| 1664 | goto end; |
| 1665 | } |
| 1666 | |
| 1667 | streams_count = be32toh(rp.streams_count); |
| 1668 | |
| 1669 | switch(be32toh(rp.status)) { |
| 1670 | case LTTNG_VIEWER_NEW_STREAMS_OK: |
| 1671 | session->new_streams_needed = false; |
| 1672 | break; |
| 1673 | case LTTNG_VIEWER_NEW_STREAMS_NO_NEW: |
| 1674 | session->new_streams_needed = false; |
| 1675 | goto end; |
| 1676 | case LTTNG_VIEWER_NEW_STREAMS_HUP: |
| 1677 | session->new_streams_needed = false; |
| 1678 | session->closed = true; |
| 1679 | status = LTTNG_LIVE_ITERATOR_STATUS_END; |
| 1680 | goto end; |
| 1681 | case LTTNG_VIEWER_NEW_STREAMS_ERR: |
| 1682 | BT_COMP_LOGD("Received get_new_streams response: error"); |
| 1683 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 1684 | goto end; |
| 1685 | default: |
| 1686 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
| 1687 | "Received get_new_streams response: Unknown:" |
| 1688 | "return code %u", be32toh(rp.status)); |
| 1689 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
| 1690 | goto end; |
| 1691 | } |
| 1692 | |
| 1693 | viewer_status = receive_streams(session, streams_count, self_msg_iter); |
| 1694 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
| 1695 | viewer_handle_recv_status(self_comp, NULL, |
| 1696 | viewer_status, "new streams"); |
| 1697 | status = viewer_status_to_live_iterator_status(viewer_status); |
| 1698 | goto end; |
| 1699 | } |
| 1700 | |
| 1701 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
| 1702 | end: |
| 1703 | return status; |
| 1704 | } |
| 1705 | |
| 1706 | BT_HIDDEN |
| 1707 | enum lttng_live_viewer_status live_viewer_connection_create( |
| 1708 | bt_self_component *self_comp, |
| 1709 | bt_self_component_class *self_comp_class, |
| 1710 | bt_logging_level log_level, |
| 1711 | const char *url, bool in_query, |
| 1712 | struct lttng_live_msg_iter *lttng_live_msg_iter, |
| 1713 | struct live_viewer_connection **viewer) |
| 1714 | { |
| 1715 | struct live_viewer_connection *viewer_connection; |
| 1716 | enum lttng_live_viewer_status status; |
| 1717 | |
| 1718 | viewer_connection = g_new0(struct live_viewer_connection, 1); |
| 1719 | |
| 1720 | if (bt_socket_init(log_level) != 0) { |
| 1721 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 1722 | self_comp_class, "Failed to init socket"); |
| 1723 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1724 | goto error; |
| 1725 | } |
| 1726 | |
| 1727 | viewer_connection->log_level = log_level; |
| 1728 | |
| 1729 | viewer_connection->self_comp = self_comp; |
| 1730 | viewer_connection->self_comp_class = self_comp_class; |
| 1731 | |
| 1732 | viewer_connection->control_sock = BT_INVALID_SOCKET; |
| 1733 | viewer_connection->port = -1; |
| 1734 | viewer_connection->in_query = in_query; |
| 1735 | viewer_connection->lttng_live_msg_iter = lttng_live_msg_iter; |
| 1736 | viewer_connection->url = g_string_new(url); |
| 1737 | if (!viewer_connection->url) { |
| 1738 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 1739 | self_comp_class, "Failed to allocate URL buffer"); |
| 1740 | status = LTTNG_LIVE_VIEWER_STATUS_ERROR; |
| 1741 | goto error; |
| 1742 | } |
| 1743 | |
| 1744 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 1745 | "Establishing connection to url \"%s\"...", url); |
| 1746 | status = lttng_live_connect_viewer(viewer_connection); |
| 1747 | /* |
| 1748 | * Only print error and append cause in case of error. not in case of |
| 1749 | * interruption. |
| 1750 | */ |
| 1751 | if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { |
| 1752 | BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, |
| 1753 | self_comp_class, "Failed to establish connection: " |
| 1754 | "url=\"%s\"", url); |
| 1755 | goto error; |
| 1756 | } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { |
| 1757 | goto error; |
| 1758 | } |
| 1759 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 1760 | "Connection to url \"%s\" is established", url); |
| 1761 | |
| 1762 | *viewer = viewer_connection; |
| 1763 | status = LTTNG_LIVE_VIEWER_STATUS_OK; |
| 1764 | goto end; |
| 1765 | |
| 1766 | error: |
| 1767 | if (viewer_connection) { |
| 1768 | live_viewer_connection_destroy(viewer_connection); |
| 1769 | } |
| 1770 | end: |
| 1771 | return status; |
| 1772 | } |
| 1773 | |
| 1774 | BT_HIDDEN |
| 1775 | void live_viewer_connection_destroy( |
| 1776 | struct live_viewer_connection *viewer_connection) |
| 1777 | { |
| 1778 | bt_self_component *self_comp = viewer_connection->self_comp; |
| 1779 | bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; |
| 1780 | |
| 1781 | if (!viewer_connection) { |
| 1782 | goto end; |
| 1783 | } |
| 1784 | |
| 1785 | BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, |
| 1786 | "Closing connection to relay:" |
| 1787 | "relay-url=\"%s\"", viewer_connection->url->str); |
| 1788 | |
| 1789 | lttng_live_disconnect_viewer(viewer_connection); |
| 1790 | |
| 1791 | if (viewer_connection->url) { |
| 1792 | g_string_free(viewer_connection->url, true); |
| 1793 | } |
| 1794 | |
| 1795 | if (viewer_connection->relay_hostname) { |
| 1796 | g_string_free(viewer_connection->relay_hostname, true); |
| 1797 | } |
| 1798 | |
| 1799 | if (viewer_connection->target_hostname) { |
| 1800 | g_string_free(viewer_connection->target_hostname, true); |
| 1801 | } |
| 1802 | |
| 1803 | if (viewer_connection->session_name) { |
| 1804 | g_string_free(viewer_connection->session_name, true); |
| 1805 | } |
| 1806 | |
| 1807 | if (viewer_connection->proto) { |
| 1808 | g_string_free(viewer_connection->proto, true); |
| 1809 | } |
| 1810 | |
| 1811 | g_free(viewer_connection); |
| 1812 | |
| 1813 | bt_socket_fini(); |
| 1814 | |
| 1815 | end: |
| 1816 | return; |
| 1817 | } |