| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include "cmd.hpp" |
| 12 | #include "connection.hpp" |
| 13 | #include "ctf-trace.hpp" |
| 14 | #include "health-relayd.hpp" |
| 15 | #include "live.hpp" |
| 16 | #include "lttng-relayd.hpp" |
| 17 | #include "session.hpp" |
| 18 | #include "stream.hpp" |
| 19 | #include "testpoint.hpp" |
| 20 | #include "utils.hpp" |
| 21 | #include "viewer-session.hpp" |
| 22 | #include "viewer-stream.hpp" |
| 23 | |
| 24 | #include <common/common.hpp> |
| 25 | #include <common/compat/endian.hpp> |
| 26 | #include <common/compat/poll.hpp> |
| 27 | #include <common/compat/socket.hpp> |
| 28 | #include <common/defaults.hpp> |
| 29 | #include <common/fd-tracker/utils.hpp> |
| 30 | #include <common/fs-handle.hpp> |
| 31 | #include <common/futex.hpp> |
| 32 | #include <common/index/index.hpp> |
| 33 | #include <common/sessiond-comm/inet.hpp> |
| 34 | #include <common/sessiond-comm/relayd.hpp> |
| 35 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 36 | #include <common/urcu.hpp> |
| 37 | #include <common/uri.hpp> |
| 38 | #include <common/utils.hpp> |
| 39 | |
| 40 | #include <lttng/lttng.h> |
| 41 | |
| 42 | #include <fcntl.h> |
| 43 | #include <getopt.h> |
| 44 | #include <grp.h> |
| 45 | #include <inttypes.h> |
| 46 | #include <limits.h> |
| 47 | #include <pthread.h> |
| 48 | #include <signal.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | #include <string> |
| 53 | #include <sys/mman.h> |
| 54 | #include <sys/mount.h> |
| 55 | #include <sys/resource.h> |
| 56 | #include <sys/socket.h> |
| 57 | #include <sys/stat.h> |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/wait.h> |
| 60 | #include <unistd.h> |
| 61 | #include <urcu/futex.h> |
| 62 | #include <urcu/rculist.h> |
| 63 | #include <urcu/uatomic.h> |
| 64 | |
| 65 | #define SESSION_BUF_DEFAULT_COUNT 16 |
| 66 | |
| 67 | static struct lttng_uri *live_uri; |
| 68 | |
| 69 | /* |
| 70 | * This pipe is used to inform the worker thread that a command is queued and |
| 71 | * ready to be processed. |
| 72 | */ |
| 73 | static int live_conn_pipe[2] = { -1, -1 }; |
| 74 | |
| 75 | /* Shared between threads */ |
| 76 | static int live_dispatch_thread_exit; |
| 77 | |
| 78 | static pthread_t live_listener_thread; |
| 79 | static pthread_t live_dispatcher_thread; |
| 80 | static pthread_t live_worker_thread; |
| 81 | |
| 82 | /* |
| 83 | * Relay command queue. |
| 84 | * |
| 85 | * The live_thread_listener and live_thread_dispatcher communicate with this |
| 86 | * queue. |
| 87 | */ |
| 88 | static struct relay_conn_queue viewer_conn_queue; |
| 89 | |
| 90 | static uint64_t last_relay_viewer_session_id; |
| 91 | static pthread_mutex_t last_relay_viewer_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
| 92 | |
| 93 | static const char *lttng_viewer_command_str(lttng_viewer_command cmd) |
| 94 | { |
| 95 | switch (cmd) { |
| 96 | case LTTNG_VIEWER_CONNECT: |
| 97 | return "CONNECT"; |
| 98 | case LTTNG_VIEWER_LIST_SESSIONS: |
| 99 | return "LIST_SESSIONS"; |
| 100 | case LTTNG_VIEWER_ATTACH_SESSION: |
| 101 | return "ATTACH_SESSION"; |
| 102 | case LTTNG_VIEWER_GET_NEXT_INDEX: |
| 103 | return "GET_NEXT_INDEX"; |
| 104 | case LTTNG_VIEWER_GET_PACKET: |
| 105 | return "GET_PACKET"; |
| 106 | case LTTNG_VIEWER_GET_METADATA: |
| 107 | return "GET_METADATA"; |
| 108 | case LTTNG_VIEWER_GET_NEW_STREAMS: |
| 109 | return "GET_NEW_STREAMS"; |
| 110 | case LTTNG_VIEWER_CREATE_SESSION: |
| 111 | return "CREATE_SESSION"; |
| 112 | case LTTNG_VIEWER_DETACH_SESSION: |
| 113 | return "DETACH_SESSION"; |
| 114 | default: |
| 115 | abort(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static const char * |
| 120 | lttng_viewer_next_index_return_code_str(enum lttng_viewer_next_index_return_code code) |
| 121 | { |
| 122 | switch (code) { |
| 123 | case LTTNG_VIEWER_INDEX_OK: |
| 124 | return "INDEX_OK"; |
| 125 | case LTTNG_VIEWER_INDEX_RETRY: |
| 126 | return "INDEX_RETRY"; |
| 127 | case LTTNG_VIEWER_INDEX_HUP: |
| 128 | return "INDEX_HUP"; |
| 129 | case LTTNG_VIEWER_INDEX_ERR: |
| 130 | return "INDEX_ERR"; |
| 131 | case LTTNG_VIEWER_INDEX_INACTIVE: |
| 132 | return "INDEX_INACTIVE"; |
| 133 | case LTTNG_VIEWER_INDEX_EOF: |
| 134 | return "INDEX_EOF"; |
| 135 | default: |
| 136 | abort(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static const char *lttng_viewer_attach_return_code_str(enum lttng_viewer_attach_return_code code) |
| 141 | { |
| 142 | switch (code) { |
| 143 | case LTTNG_VIEWER_ATTACH_OK: |
| 144 | return "ATTACH_OK"; |
| 145 | case LTTNG_VIEWER_ATTACH_ALREADY: |
| 146 | return "ATTACH_ALREADY"; |
| 147 | case LTTNG_VIEWER_ATTACH_UNK: |
| 148 | return "ATTACH_UNK"; |
| 149 | case LTTNG_VIEWER_ATTACH_NOT_LIVE: |
| 150 | return "ATTACH_NOT_LIVE"; |
| 151 | case LTTNG_VIEWER_ATTACH_SEEK_ERR: |
| 152 | return "ATTACH_SEEK_ERR"; |
| 153 | case LTTNG_VIEWER_ATTACH_NO_SESSION: |
| 154 | return "ATTACH_NO_SESSION"; |
| 155 | default: |
| 156 | abort(); |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | static const char * |
| 161 | lttng_viewer_get_packet_return_code_str(enum lttng_viewer_get_packet_return_code code) |
| 162 | { |
| 163 | switch (code) { |
| 164 | case LTTNG_VIEWER_GET_PACKET_OK: |
| 165 | return "GET_PACKET_OK"; |
| 166 | case LTTNG_VIEWER_GET_PACKET_RETRY: |
| 167 | return "GET_PACKET_RETRY"; |
| 168 | case LTTNG_VIEWER_GET_PACKET_ERR: |
| 169 | return "GET_PACKET_ERR"; |
| 170 | case LTTNG_VIEWER_GET_PACKET_EOF: |
| 171 | return "GET_PACKET_EOF"; |
| 172 | default: |
| 173 | abort(); |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | /* |
| 178 | * Cleanup the daemon |
| 179 | */ |
| 180 | static void cleanup_relayd_live() |
| 181 | { |
| 182 | DBG("Cleaning up"); |
| 183 | |
| 184 | free(live_uri); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Receive a request buffer using a given socket, destination allocated buffer |
| 189 | * of length size. |
| 190 | * |
| 191 | * Return the size of the received message or else a negative value on error |
| 192 | * with errno being set by recvmsg() syscall. |
| 193 | */ |
| 194 | static ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size) |
| 195 | { |
| 196 | ssize_t ret; |
| 197 | |
| 198 | ret = sock->ops->recvmsg(sock, buf, size, 0); |
| 199 | if (ret < 0 || ret != size) { |
| 200 | if (ret == 0) { |
| 201 | /* Orderly shutdown. Not necessary to print an error. */ |
| 202 | DBG("Socket %d did an orderly shutdown", sock->fd); |
| 203 | } else { |
| 204 | ERR("Relay failed to receive request."); |
| 205 | } |
| 206 | ret = -1; |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Send a response buffer using a given socket, source allocated buffer of |
| 214 | * length size. |
| 215 | * |
| 216 | * Return the size of the sent message or else a negative value on error with |
| 217 | * errno being set by sendmsg() syscall. |
| 218 | */ |
| 219 | static ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size) |
| 220 | { |
| 221 | ssize_t ret; |
| 222 | |
| 223 | ret = sock->ops->sendmsg(sock, buf, size, 0); |
| 224 | if (ret < 0) { |
| 225 | ERR("Relayd failed to send response."); |
| 226 | } |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Atomically check if new streams got added in one of the sessions attached |
| 233 | * and reset the flag to 0. |
| 234 | * |
| 235 | * Returns 1 if new streams got added, 0 if nothing changed, a negative value |
| 236 | * on error. |
| 237 | */ |
| 238 | static int check_new_streams(struct relay_connection *conn) |
| 239 | { |
| 240 | struct relay_session *session; |
| 241 | int ret = 0; |
| 242 | |
| 243 | if (!conn->viewer_session) { |
| 244 | goto end; |
| 245 | } |
| 246 | |
| 247 | { |
| 248 | lttng::urcu::read_lock_guard read_lock; |
| 249 | cds_list_for_each_entry_rcu( |
| 250 | session, &conn->viewer_session->session_list, viewer_session_node) |
| 251 | { |
| 252 | if (!session_get(session)) { |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | ret = uatomic_read(&session->new_streams); |
| 257 | session_put(session); |
| 258 | if (ret == 1) { |
| 259 | goto end; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | end: |
| 265 | DBG("Viewer connection has%s new streams: socket_fd = %d", |
| 266 | ret == 0 ? " no" : "", |
| 267 | conn->sock->fd); |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Send viewer streams to the given socket. The ignore_sent_flag indicates if |
| 273 | * this function should ignore the sent flag or not. |
| 274 | * |
| 275 | * Return 0 on success or else a negative value. |
| 276 | */ |
| 277 | static ssize_t |
| 278 | send_viewer_streams(struct lttcomm_sock *sock, uint64_t session_id, unsigned int ignore_sent_flag) |
| 279 | { |
| 280 | ssize_t ret; |
| 281 | struct lttng_ht_iter iter; |
| 282 | struct relay_viewer_stream *vstream; |
| 283 | |
| 284 | { |
| 285 | lttng::urcu::read_lock_guard read_lock; |
| 286 | |
| 287 | cds_lfht_for_each_entry ( |
| 288 | viewer_streams_ht->ht, &iter.iter, vstream, stream_n.node) { |
| 289 | struct ctf_trace *ctf_trace; |
| 290 | struct lttng_viewer_stream send_stream = {}; |
| 291 | |
| 292 | health_code_update(); |
| 293 | |
| 294 | if (!viewer_stream_get(vstream)) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | pthread_mutex_lock(&vstream->stream->lock); |
| 299 | /* Ignore if not the same session. */ |
| 300 | if (vstream->stream->trace->session->id != session_id || |
| 301 | (!ignore_sent_flag && vstream->sent_flag)) { |
| 302 | pthread_mutex_unlock(&vstream->stream->lock); |
| 303 | viewer_stream_put(vstream); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | ctf_trace = vstream->stream->trace; |
| 308 | send_stream.id = htobe64(vstream->stream->stream_handle); |
| 309 | send_stream.ctf_trace_id = htobe64(ctf_trace->id); |
| 310 | send_stream.metadata_flag = htobe32(vstream->stream->is_metadata); |
| 311 | if (lttng_strncpy(send_stream.path_name, |
| 312 | vstream->path_name, |
| 313 | sizeof(send_stream.path_name))) { |
| 314 | pthread_mutex_unlock(&vstream->stream->lock); |
| 315 | viewer_stream_put(vstream); |
| 316 | ret = -1; /* Error. */ |
| 317 | goto end; |
| 318 | } |
| 319 | if (lttng_strncpy(send_stream.channel_name, |
| 320 | vstream->channel_name, |
| 321 | sizeof(send_stream.channel_name))) { |
| 322 | pthread_mutex_unlock(&vstream->stream->lock); |
| 323 | viewer_stream_put(vstream); |
| 324 | ret = -1; /* Error. */ |
| 325 | goto end; |
| 326 | } |
| 327 | |
| 328 | DBG("Sending stream %" PRIu64 " to viewer", vstream->stream->stream_handle); |
| 329 | vstream->sent_flag = true; |
| 330 | pthread_mutex_unlock(&vstream->stream->lock); |
| 331 | |
| 332 | ret = send_response(sock, &send_stream, sizeof(send_stream)); |
| 333 | viewer_stream_put(vstream); |
| 334 | if (ret < 0) { |
| 335 | goto end; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | ret = 0; |
| 341 | |
| 342 | end: |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Create every viewer stream possible for the given session with the seek |
| 348 | * type. Three counters *can* be return which are in order the total amount of |
| 349 | * viewer stream of the session, the number of unsent stream and the number of |
| 350 | * stream created. Those counters can be NULL and thus will be ignored. |
| 351 | * |
| 352 | * session must be locked to ensure that we see either none or all initial |
| 353 | * streams for a session, but no intermediate state.. |
| 354 | * |
| 355 | * Return 0 on success or else a negative value. |
| 356 | */ |
| 357 | static int make_viewer_streams(struct relay_session *relay_session, |
| 358 | struct relay_viewer_session *viewer_session, |
| 359 | enum lttng_viewer_seek seek_t, |
| 360 | uint32_t *nb_total, |
| 361 | uint32_t *nb_unsent, |
| 362 | uint32_t *nb_created, |
| 363 | bool *closed) |
| 364 | { |
| 365 | int ret; |
| 366 | struct lttng_ht_iter iter; |
| 367 | struct ctf_trace *ctf_trace; |
| 368 | struct relay_stream *relay_stream = nullptr; |
| 369 | |
| 370 | LTTNG_ASSERT(relay_session); |
| 371 | ASSERT_LOCKED(relay_session->lock); |
| 372 | |
| 373 | if (relay_session->connection_closed) { |
| 374 | *closed = true; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Create viewer streams for relay streams that are ready to be |
| 379 | * used for a the given session id only. |
| 380 | */ |
| 381 | { |
| 382 | lttng::urcu::read_lock_guard read_lock; |
| 383 | |
| 384 | cds_lfht_for_each_entry ( |
| 385 | relay_session->ctf_traces_ht->ht, &iter.iter, ctf_trace, node.node) { |
| 386 | bool trace_has_metadata_stream = false; |
| 387 | |
| 388 | health_code_update(); |
| 389 | |
| 390 | if (!ctf_trace_get(ctf_trace)) { |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Iterate over all the streams of the trace to see if we have a |
| 396 | * metadata stream. |
| 397 | */ |
| 398 | cds_list_for_each_entry_rcu( |
| 399 | relay_stream, &ctf_trace->stream_list, stream_node) |
| 400 | { |
| 401 | bool is_metadata_stream; |
| 402 | |
| 403 | pthread_mutex_lock(&relay_stream->lock); |
| 404 | is_metadata_stream = relay_stream->is_metadata; |
| 405 | pthread_mutex_unlock(&relay_stream->lock); |
| 406 | |
| 407 | if (is_metadata_stream) { |
| 408 | trace_has_metadata_stream = true; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | relay_stream = nullptr; |
| 414 | |
| 415 | /* |
| 416 | * If there is no metadata stream in this trace at the moment |
| 417 | * and we never sent one to the viewer, skip the trace. We |
| 418 | * accept that the viewer will not see this trace at all. |
| 419 | */ |
| 420 | if (!trace_has_metadata_stream && |
| 421 | !ctf_trace->metadata_stream_sent_to_viewer) { |
| 422 | ctf_trace_put(ctf_trace); |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | cds_list_for_each_entry_rcu( |
| 427 | relay_stream, &ctf_trace->stream_list, stream_node) |
| 428 | { |
| 429 | struct relay_viewer_stream *viewer_stream; |
| 430 | |
| 431 | if (!stream_get(relay_stream)) { |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | pthread_mutex_lock(&relay_stream->lock); |
| 436 | /* |
| 437 | * stream published is protected by the session lock. |
| 438 | */ |
| 439 | if (!relay_stream->published) { |
| 440 | goto next; |
| 441 | } |
| 442 | viewer_stream = |
| 443 | viewer_stream_get_by_id(relay_stream->stream_handle); |
| 444 | if (!viewer_stream) { |
| 445 | struct lttng_trace_chunk *viewer_stream_trace_chunk = |
| 446 | nullptr; |
| 447 | |
| 448 | /* |
| 449 | * Save that we sent the metadata stream to the |
| 450 | * viewer. So that we know what trace the viewer |
| 451 | * is aware of. |
| 452 | */ |
| 453 | if (relay_stream->is_metadata) { |
| 454 | ctf_trace->metadata_stream_sent_to_viewer = true; |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * If a rotation is ongoing, use a copy of the |
| 459 | * relay stream's chunk to ensure the stream |
| 460 | * files exist. |
| 461 | * |
| 462 | * Otherwise, the viewer session's current trace |
| 463 | * chunk can be used safely. |
| 464 | */ |
| 465 | if ((relay_stream->ongoing_rotation.is_set || |
| 466 | session_has_ongoing_rotation(relay_session)) && |
| 467 | relay_stream->trace_chunk) { |
| 468 | viewer_stream_trace_chunk = lttng_trace_chunk_copy( |
| 469 | relay_stream->trace_chunk); |
| 470 | if (!viewer_stream_trace_chunk) { |
| 471 | ret = -1; |
| 472 | ctf_trace_put(ctf_trace); |
| 473 | goto error_unlock; |
| 474 | } |
| 475 | } else { |
| 476 | /* |
| 477 | * Transition the viewer session into the newest |
| 478 | * trace chunk available. |
| 479 | */ |
| 480 | if (!lttng_trace_chunk_ids_equal( |
| 481 | viewer_session->current_trace_chunk, |
| 482 | relay_stream->trace_chunk)) { |
| 483 | ret = viewer_session_set_trace_chunk_copy( |
| 484 | viewer_session, |
| 485 | relay_stream->trace_chunk); |
| 486 | if (ret) { |
| 487 | ret = -1; |
| 488 | ctf_trace_put(ctf_trace); |
| 489 | goto error_unlock; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (relay_stream->trace_chunk) { |
| 494 | /* |
| 495 | * If the corresponding relay |
| 496 | * stream's trace chunk is set, |
| 497 | * the viewer stream will be |
| 498 | * created under it. |
| 499 | * |
| 500 | * Note that a relay stream can |
| 501 | * have a NULL output trace |
| 502 | * chunk (for instance, after a |
| 503 | * clear against a stopped |
| 504 | * session). |
| 505 | */ |
| 506 | const bool reference_acquired = |
| 507 | lttng_trace_chunk_get( |
| 508 | viewer_session |
| 509 | ->current_trace_chunk); |
| 510 | |
| 511 | LTTNG_ASSERT(reference_acquired); |
| 512 | viewer_stream_trace_chunk = |
| 513 | viewer_session->current_trace_chunk; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | viewer_stream = viewer_stream_create( |
| 518 | relay_stream, viewer_stream_trace_chunk, seek_t); |
| 519 | lttng_trace_chunk_put(viewer_stream_trace_chunk); |
| 520 | viewer_stream_trace_chunk = nullptr; |
| 521 | if (!viewer_stream) { |
| 522 | ret = -1; |
| 523 | ctf_trace_put(ctf_trace); |
| 524 | goto error_unlock; |
| 525 | } |
| 526 | |
| 527 | if (nb_created) { |
| 528 | /* Update number of created stream counter. */ |
| 529 | (*nb_created)++; |
| 530 | } |
| 531 | /* |
| 532 | * Ensure a self-reference is preserved even |
| 533 | * after we have put our local reference. |
| 534 | */ |
| 535 | if (!viewer_stream_get(viewer_stream)) { |
| 536 | ERR("Unable to get self-reference on viewer stream, logic error."); |
| 537 | abort(); |
| 538 | } |
| 539 | } else { |
| 540 | if (!viewer_stream->sent_flag && nb_unsent) { |
| 541 | /* Update number of unsent stream counter. */ |
| 542 | (*nb_unsent)++; |
| 543 | } |
| 544 | } |
| 545 | /* Update number of total stream counter. */ |
| 546 | if (nb_total) { |
| 547 | if (relay_stream->is_metadata) { |
| 548 | if (!relay_stream->closed || |
| 549 | relay_stream->metadata_received > |
| 550 | viewer_stream->metadata_sent) { |
| 551 | (*nb_total)++; |
| 552 | } |
| 553 | } else { |
| 554 | if (!relay_stream->closed || |
| 555 | !(((int64_t) (relay_stream->prev_data_seq - |
| 556 | relay_stream->last_net_seq_num)) >= |
| 557 | 0)) { |
| 558 | (*nb_total)++; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | /* Put local reference. */ |
| 563 | viewer_stream_put(viewer_stream); |
| 564 | next: |
| 565 | pthread_mutex_unlock(&relay_stream->lock); |
| 566 | stream_put(relay_stream); |
| 567 | } |
| 568 | relay_stream = nullptr; |
| 569 | ctf_trace_put(ctf_trace); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | ret = 0; |
| 574 | |
| 575 | error_unlock: |
| 576 | |
| 577 | if (relay_stream) { |
| 578 | pthread_mutex_unlock(&relay_stream->lock); |
| 579 | stream_put(relay_stream); |
| 580 | } |
| 581 | |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | int relayd_live_stop() |
| 586 | { |
| 587 | /* Stop dispatch thread */ |
| 588 | CMM_STORE_SHARED(live_dispatch_thread_exit, 1); |
| 589 | futex_nto1_wake(&viewer_conn_queue.futex); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int create_sock(void *data, int *out_fd) |
| 594 | { |
| 595 | int ret; |
| 596 | struct lttcomm_sock *sock = (lttcomm_sock *) data; |
| 597 | |
| 598 | ret = lttcomm_create_sock(sock); |
| 599 | if (ret < 0) { |
| 600 | goto end; |
| 601 | } |
| 602 | |
| 603 | *out_fd = sock->fd; |
| 604 | end: |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | static int close_sock(void *data, int *in_fd __attribute__((unused))) |
| 609 | { |
| 610 | struct lttcomm_sock *sock = (lttcomm_sock *) data; |
| 611 | |
| 612 | return sock->ops->close(sock); |
| 613 | } |
| 614 | |
| 615 | static int accept_sock(void *data, int *out_fd) |
| 616 | { |
| 617 | int ret = 0; |
| 618 | /* Socks is an array of in_sock, out_sock. */ |
| 619 | struct lttcomm_sock **socks = (lttcomm_sock **) data; |
| 620 | struct lttcomm_sock *in_sock = socks[0]; |
| 621 | |
| 622 | socks[1] = in_sock->ops->accept(in_sock); |
| 623 | if (!socks[1]) { |
| 624 | ret = -1; |
| 625 | goto end; |
| 626 | } |
| 627 | *out_fd = socks[1]->fd; |
| 628 | end: |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | static struct lttcomm_sock *accept_live_sock(struct lttcomm_sock *listening_sock, const char *name) |
| 633 | { |
| 634 | int out_fd, ret; |
| 635 | struct lttcomm_sock *socks[2] = { listening_sock, nullptr }; |
| 636 | struct lttcomm_sock *new_sock = nullptr; |
| 637 | |
| 638 | ret = fd_tracker_open_unsuspendable_fd( |
| 639 | the_fd_tracker, &out_fd, (const char **) &name, 1, accept_sock, &socks); |
| 640 | if (ret) { |
| 641 | goto end; |
| 642 | } |
| 643 | new_sock = socks[1]; |
| 644 | DBG("%s accepted, socket %d", name, new_sock->fd); |
| 645 | end: |
| 646 | return new_sock; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Create and init socket from uri. |
| 651 | */ |
| 652 | static struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name) |
| 653 | { |
| 654 | int ret, sock_fd; |
| 655 | struct lttcomm_sock *sock = nullptr; |
| 656 | char uri_str[LTTNG_PATH_MAX]; |
| 657 | char *formated_name = nullptr; |
| 658 | |
| 659 | sock = lttcomm_alloc_sock_from_uri(uri); |
| 660 | if (sock == nullptr) { |
| 661 | ERR("Allocating socket"); |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Don't fail to create the socket if the name can't be built as it is |
| 667 | * only used for debugging purposes. |
| 668 | */ |
| 669 | ret = uri_to_str_url(uri, uri_str, sizeof(uri_str)); |
| 670 | uri_str[sizeof(uri_str) - 1] = '\0'; |
| 671 | if (ret >= 0) { |
| 672 | ret = asprintf(&formated_name, "%s socket @ %s", name, uri_str); |
| 673 | if (ret < 0) { |
| 674 | formated_name = nullptr; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker, |
| 679 | &sock_fd, |
| 680 | (const char **) (formated_name ? &formated_name : |
| 681 | nullptr), |
| 682 | 1, |
| 683 | create_sock, |
| 684 | sock); |
| 685 | if (ret) { |
| 686 | PERROR("Failed to create \"%s\" socket", formated_name ?: "Unknown"); |
| 687 | goto error; |
| 688 | } |
| 689 | DBG("Listening on %s socket %d", name, sock->fd); |
| 690 | |
| 691 | ret = sock->ops->bind(sock); |
| 692 | if (ret < 0) { |
| 693 | PERROR("Failed to bind lttng-live socket"); |
| 694 | goto error; |
| 695 | } |
| 696 | |
| 697 | ret = sock->ops->listen(sock, -1); |
| 698 | if (ret < 0) { |
| 699 | goto error; |
| 700 | } |
| 701 | |
| 702 | free(formated_name); |
| 703 | return sock; |
| 704 | |
| 705 | error: |
| 706 | if (sock) { |
| 707 | lttcomm_destroy_sock(sock); |
| 708 | } |
| 709 | free(formated_name); |
| 710 | return nullptr; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * This thread manages the listening for new connections on the network |
| 715 | */ |
| 716 | static void *thread_listener(void *data __attribute__((unused))) |
| 717 | { |
| 718 | int i, ret, err = -1; |
| 719 | uint32_t nb_fd; |
| 720 | struct lttng_poll_event events; |
| 721 | struct lttcomm_sock *live_control_sock; |
| 722 | |
| 723 | DBG("[thread] Relay live listener started"); |
| 724 | |
| 725 | rcu_register_thread(); |
| 726 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER); |
| 727 | |
| 728 | health_code_update(); |
| 729 | |
| 730 | live_control_sock = init_socket(live_uri, "Live listener"); |
| 731 | if (!live_control_sock) { |
| 732 | goto error_sock_control; |
| 733 | } |
| 734 | |
| 735 | /* Pass 2 as size here for the thread quit pipe and control sockets. */ |
| 736 | ret = create_named_thread_poll_set(&events, 2, "Live listener thread epoll"); |
| 737 | if (ret < 0) { |
| 738 | goto error_create_poll; |
| 739 | } |
| 740 | |
| 741 | /* Add the control socket */ |
| 742 | ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP); |
| 743 | if (ret < 0) { |
| 744 | goto error_poll_add; |
| 745 | } |
| 746 | |
| 747 | lttng_relay_notify_ready(); |
| 748 | |
| 749 | if (testpoint(relayd_thread_live_listener)) { |
| 750 | goto error_testpoint; |
| 751 | } |
| 752 | |
| 753 | while (true) { |
| 754 | health_code_update(); |
| 755 | |
| 756 | DBG("Listener accepting live viewers connections"); |
| 757 | |
| 758 | restart: |
| 759 | health_poll_entry(); |
| 760 | ret = lttng_poll_wait(&events, -1); |
| 761 | health_poll_exit(); |
| 762 | if (ret < 0) { |
| 763 | /* |
| 764 | * Restart interrupted system call. |
| 765 | */ |
| 766 | if (errno == EINTR) { |
| 767 | goto restart; |
| 768 | } |
| 769 | goto error; |
| 770 | } |
| 771 | nb_fd = ret; |
| 772 | |
| 773 | DBG("Relay new viewer connection received"); |
| 774 | for (i = 0; i < nb_fd; i++) { |
| 775 | /* Fetch once the poll data */ |
| 776 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
| 777 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); |
| 778 | |
| 779 | health_code_update(); |
| 780 | |
| 781 | /* Activity on thread quit pipe, exiting. */ |
| 782 | if (relayd_is_thread_quit_pipe(pollfd)) { |
| 783 | DBG("Activity on thread quit pipe"); |
| 784 | err = 0; |
| 785 | goto exit; |
| 786 | } |
| 787 | |
| 788 | if (revents & LPOLLIN) { |
| 789 | /* |
| 790 | * A new connection is requested, therefore a |
| 791 | * viewer connection is allocated in this |
| 792 | * thread, enqueued to a global queue and |
| 793 | * dequeued (and freed) in the worker thread. |
| 794 | */ |
| 795 | int val = 1; |
| 796 | struct relay_connection *new_conn; |
| 797 | struct lttcomm_sock *newsock; |
| 798 | |
| 799 | newsock = accept_live_sock(live_control_sock, |
| 800 | "Live socket to client"); |
| 801 | if (!newsock) { |
| 802 | PERROR("accepting control sock"); |
| 803 | goto error; |
| 804 | } |
| 805 | DBG("Relay viewer connection accepted socket %d", newsock->fd); |
| 806 | |
| 807 | ret = setsockopt( |
| 808 | newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
| 809 | if (ret < 0) { |
| 810 | PERROR("setsockopt inet"); |
| 811 | lttcomm_destroy_sock(newsock); |
| 812 | goto error; |
| 813 | } |
| 814 | new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN); |
| 815 | if (!new_conn) { |
| 816 | lttcomm_destroy_sock(newsock); |
| 817 | goto error; |
| 818 | } |
| 819 | /* Ownership assumed by the connection. */ |
| 820 | newsock = nullptr; |
| 821 | |
| 822 | /* Enqueue request for the dispatcher thread. */ |
| 823 | cds_wfcq_head_ptr_t head; |
| 824 | head.h = &viewer_conn_queue.head; |
| 825 | cds_wfcq_enqueue(head, &viewer_conn_queue.tail, &new_conn->qnode); |
| 826 | |
| 827 | /* |
| 828 | * Wake the dispatch queue futex. |
| 829 | * Implicit memory barrier with the |
| 830 | * exchange in cds_wfcq_enqueue. |
| 831 | */ |
| 832 | futex_nto1_wake(&viewer_conn_queue.futex); |
| 833 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 834 | ERR("socket poll error"); |
| 835 | goto error; |
| 836 | } else { |
| 837 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 838 | goto error; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | exit: |
| 844 | error: |
| 845 | error_poll_add: |
| 846 | error_testpoint: |
| 847 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
| 848 | error_create_poll: |
| 849 | if (live_control_sock->fd >= 0) { |
| 850 | int sock_fd = live_control_sock->fd; |
| 851 | |
| 852 | ret = fd_tracker_close_unsuspendable_fd( |
| 853 | the_fd_tracker, &sock_fd, 1, close_sock, live_control_sock); |
| 854 | if (ret) { |
| 855 | PERROR("close"); |
| 856 | } |
| 857 | live_control_sock->fd = -1; |
| 858 | } |
| 859 | lttcomm_destroy_sock(live_control_sock); |
| 860 | error_sock_control: |
| 861 | if (err) { |
| 862 | health_error(); |
| 863 | DBG("Live viewer listener thread exited with error"); |
| 864 | } |
| 865 | health_unregister(health_relayd); |
| 866 | rcu_unregister_thread(); |
| 867 | DBG("Live viewer listener thread cleanup complete"); |
| 868 | if (lttng_relay_stop_threads()) { |
| 869 | ERR("Error stopping threads"); |
| 870 | } |
| 871 | return nullptr; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * This thread manages the dispatching of the requests to worker threads |
| 876 | */ |
| 877 | static void *thread_dispatcher(void *data __attribute__((unused))) |
| 878 | { |
| 879 | int err = -1; |
| 880 | ssize_t ret; |
| 881 | struct cds_wfcq_node *node; |
| 882 | struct relay_connection *conn = nullptr; |
| 883 | |
| 884 | DBG("[thread] Live viewer relay dispatcher started"); |
| 885 | |
| 886 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER); |
| 887 | |
| 888 | if (testpoint(relayd_thread_live_dispatcher)) { |
| 889 | goto error_testpoint; |
| 890 | } |
| 891 | |
| 892 | health_code_update(); |
| 893 | |
| 894 | for (;;) { |
| 895 | health_code_update(); |
| 896 | |
| 897 | /* Atomically prepare the queue futex */ |
| 898 | futex_nto1_prepare(&viewer_conn_queue.futex); |
| 899 | |
| 900 | if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) { |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | do { |
| 905 | health_code_update(); |
| 906 | |
| 907 | /* Dequeue commands */ |
| 908 | node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head, |
| 909 | &viewer_conn_queue.tail); |
| 910 | if (node == nullptr) { |
| 911 | DBG("Woken up but nothing in the live-viewer " |
| 912 | "relay command queue"); |
| 913 | /* Continue thread execution */ |
| 914 | break; |
| 915 | } |
| 916 | conn = lttng::utils::container_of(node, &relay_connection::qnode); |
| 917 | DBG("Dispatching viewer request waiting on sock %d", conn->sock->fd); |
| 918 | |
| 919 | /* |
| 920 | * Inform worker thread of the new request. This |
| 921 | * call is blocking so we can be assured that |
| 922 | * the data will be read at some point in time |
| 923 | * or wait to the end of the world :) |
| 924 | */ |
| 925 | ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn)); /* NOLINT sizeof |
| 926 | used on a |
| 927 | pointer. */ |
| 928 | if (ret < 0) { |
| 929 | PERROR("write conn pipe"); |
| 930 | connection_put(conn); |
| 931 | goto error; |
| 932 | } |
| 933 | } while (node != nullptr); |
| 934 | |
| 935 | /* Futex wait on queue. Blocking call on futex() */ |
| 936 | health_poll_entry(); |
| 937 | futex_nto1_wait(&viewer_conn_queue.futex); |
| 938 | health_poll_exit(); |
| 939 | } |
| 940 | |
| 941 | /* Normal exit, no error */ |
| 942 | err = 0; |
| 943 | |
| 944 | error: |
| 945 | error_testpoint: |
| 946 | if (err) { |
| 947 | health_error(); |
| 948 | ERR("Health error occurred in %s", __func__); |
| 949 | } |
| 950 | health_unregister(health_relayd); |
| 951 | DBG("Live viewer dispatch thread dying"); |
| 952 | if (lttng_relay_stop_threads()) { |
| 953 | ERR("Error stopping threads"); |
| 954 | } |
| 955 | return nullptr; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Establish connection with the viewer and check the versions. |
| 960 | * |
| 961 | * Return 0 on success or else negative value. |
| 962 | */ |
| 963 | static int viewer_connect(struct relay_connection *conn) |
| 964 | { |
| 965 | int ret; |
| 966 | struct lttng_viewer_connect reply, msg; |
| 967 | |
| 968 | conn->version_check_done = true; |
| 969 | |
| 970 | health_code_update(); |
| 971 | |
| 972 | ret = recv_request(conn->sock, &msg, sizeof(msg)); |
| 973 | if (ret < 0) { |
| 974 | goto end; |
| 975 | } |
| 976 | |
| 977 | health_code_update(); |
| 978 | |
| 979 | memset(&reply, 0, sizeof(reply)); |
| 980 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
| 981 | reply.minor = RELAYD_VERSION_COMM_MINOR; |
| 982 | |
| 983 | /* Major versions must be the same */ |
| 984 | if (reply.major != be32toh(msg.major)) { |
| 985 | DBG("Incompatible major versions ([relayd] %u vs [client] %u)", |
| 986 | reply.major, |
| 987 | be32toh(msg.major)); |
| 988 | ret = -1; |
| 989 | goto end; |
| 990 | } |
| 991 | |
| 992 | conn->major = reply.major; |
| 993 | /* We adapt to the lowest compatible version */ |
| 994 | if (reply.minor <= be32toh(msg.minor)) { |
| 995 | conn->minor = reply.minor; |
| 996 | } else { |
| 997 | conn->minor = be32toh(msg.minor); |
| 998 | } |
| 999 | |
| 1000 | if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) { |
| 1001 | conn->type = RELAY_VIEWER_COMMAND; |
| 1002 | } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) { |
| 1003 | conn->type = RELAY_VIEWER_NOTIFICATION; |
| 1004 | } else { |
| 1005 | ERR("Unknown connection type : %u", be32toh(msg.type)); |
| 1006 | ret = -1; |
| 1007 | goto end; |
| 1008 | } |
| 1009 | |
| 1010 | reply.major = htobe32(reply.major); |
| 1011 | reply.minor = htobe32(reply.minor); |
| 1012 | if (conn->type == RELAY_VIEWER_COMMAND) { |
| 1013 | /* |
| 1014 | * Increment outside of htobe64 macro, because the argument can |
| 1015 | * be used more than once within the macro, and thus the |
| 1016 | * operation may be undefined. |
| 1017 | */ |
| 1018 | pthread_mutex_lock(&last_relay_viewer_session_id_lock); |
| 1019 | last_relay_viewer_session_id++; |
| 1020 | pthread_mutex_unlock(&last_relay_viewer_session_id_lock); |
| 1021 | reply.viewer_session_id = htobe64(last_relay_viewer_session_id); |
| 1022 | } |
| 1023 | |
| 1024 | health_code_update(); |
| 1025 | |
| 1026 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
| 1027 | if (ret < 0) { |
| 1028 | goto end; |
| 1029 | } |
| 1030 | |
| 1031 | health_code_update(); |
| 1032 | |
| 1033 | DBG("Version check done using protocol %u.%u", conn->major, conn->minor); |
| 1034 | ret = 0; |
| 1035 | |
| 1036 | end: |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Send the viewer the list of current sessions. |
| 1042 | * We need to create a copy of the hash table content because otherwise |
| 1043 | * we cannot assume the number of entries stays the same between getting |
| 1044 | * the number of HT elements and iteration over the HT. |
| 1045 | * |
| 1046 | * Return 0 on success or else a negative value. |
| 1047 | */ |
| 1048 | static int viewer_list_sessions(struct relay_connection *conn) |
| 1049 | { |
| 1050 | int ret = 0; |
| 1051 | struct lttng_viewer_list_sessions session_list; |
| 1052 | struct lttng_ht_iter iter; |
| 1053 | struct relay_session *session; |
| 1054 | struct lttng_viewer_session *send_session_buf = nullptr; |
| 1055 | uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT; |
| 1056 | uint32_t count = 0; |
| 1057 | |
| 1058 | send_session_buf = calloc<lttng_viewer_session>(SESSION_BUF_DEFAULT_COUNT); |
| 1059 | if (!send_session_buf) { |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | { |
| 1064 | lttng::urcu::read_lock_guard read_lock; |
| 1065 | |
| 1066 | cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) { |
| 1067 | struct lttng_viewer_session *send_session; |
| 1068 | |
| 1069 | health_code_update(); |
| 1070 | |
| 1071 | pthread_mutex_lock(&session->lock); |
| 1072 | if (session->connection_closed) { |
| 1073 | /* Skip closed session */ |
| 1074 | goto next_session; |
| 1075 | } |
| 1076 | |
| 1077 | if (count >= buf_count) { |
| 1078 | struct lttng_viewer_session *newbuf; |
| 1079 | uint32_t new_buf_count = buf_count << 1; |
| 1080 | |
| 1081 | newbuf = (lttng_viewer_session *) realloc( |
| 1082 | send_session_buf, |
| 1083 | new_buf_count * sizeof(*send_session_buf)); |
| 1084 | if (!newbuf) { |
| 1085 | ret = -1; |
| 1086 | goto break_loop; |
| 1087 | } |
| 1088 | send_session_buf = newbuf; |
| 1089 | buf_count = new_buf_count; |
| 1090 | } |
| 1091 | send_session = &send_session_buf[count]; |
| 1092 | if (lttng_strncpy(send_session->session_name, |
| 1093 | session->session_name, |
| 1094 | sizeof(send_session->session_name))) { |
| 1095 | ret = -1; |
| 1096 | goto break_loop; |
| 1097 | } |
| 1098 | if (lttng_strncpy(send_session->hostname, |
| 1099 | session->hostname, |
| 1100 | sizeof(send_session->hostname))) { |
| 1101 | ret = -1; |
| 1102 | goto break_loop; |
| 1103 | } |
| 1104 | send_session->id = htobe64(session->id); |
| 1105 | send_session->live_timer = htobe32(session->live_timer); |
| 1106 | if (session->viewer_attached) { |
| 1107 | send_session->clients = htobe32(1); |
| 1108 | } else { |
| 1109 | send_session->clients = htobe32(0); |
| 1110 | } |
| 1111 | send_session->streams = htobe32(session->stream_count); |
| 1112 | count++; |
| 1113 | next_session: |
| 1114 | pthread_mutex_unlock(&session->lock); |
| 1115 | continue; |
| 1116 | break_loop: |
| 1117 | pthread_mutex_unlock(&session->lock); |
| 1118 | break; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (ret < 0) { |
| 1123 | goto end_free; |
| 1124 | } |
| 1125 | |
| 1126 | session_list.sessions_count = htobe32(count); |
| 1127 | |
| 1128 | health_code_update(); |
| 1129 | |
| 1130 | ret = send_response(conn->sock, &session_list, sizeof(session_list)); |
| 1131 | if (ret < 0) { |
| 1132 | goto end_free; |
| 1133 | } |
| 1134 | |
| 1135 | health_code_update(); |
| 1136 | |
| 1137 | ret = send_response(conn->sock, send_session_buf, count * sizeof(*send_session_buf)); |
| 1138 | if (ret < 0) { |
| 1139 | goto end_free; |
| 1140 | } |
| 1141 | health_code_update(); |
| 1142 | |
| 1143 | ret = 0; |
| 1144 | end_free: |
| 1145 | free(send_session_buf); |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * Send the viewer the list of current streams. |
| 1151 | */ |
| 1152 | static int viewer_get_new_streams(struct relay_connection *conn) |
| 1153 | { |
| 1154 | int ret, send_streams = 0; |
| 1155 | uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0; |
| 1156 | struct lttng_viewer_new_streams_request request; |
| 1157 | struct lttng_viewer_new_streams_response response; |
| 1158 | struct relay_session *session = nullptr; |
| 1159 | uint64_t session_id; |
| 1160 | bool closed = false; |
| 1161 | |
| 1162 | LTTNG_ASSERT(conn); |
| 1163 | |
| 1164 | health_code_update(); |
| 1165 | |
| 1166 | /* Receive the request from the connected client. */ |
| 1167 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 1168 | if (ret < 0) { |
| 1169 | goto error; |
| 1170 | } |
| 1171 | session_id = be64toh(request.session_id); |
| 1172 | |
| 1173 | health_code_update(); |
| 1174 | |
| 1175 | memset(&response, 0, sizeof(response)); |
| 1176 | |
| 1177 | session = session_get_by_id(session_id); |
| 1178 | if (!session) { |
| 1179 | DBG("Relay session %" PRIu64 " not found", session_id); |
| 1180 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
| 1181 | goto send_reply; |
| 1182 | } |
| 1183 | |
| 1184 | if (!viewer_session_is_attached(conn->viewer_session, session)) { |
| 1185 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
| 1186 | goto send_reply; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since |
| 1191 | * that at this point the client is already attached to the session.Aany |
| 1192 | * initial stream will have been created with the seek type at attach |
| 1193 | * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach). |
| 1194 | * Otherwise any event happening in a new stream between the attach and |
| 1195 | * a call to viewer_get_new_streams will be "lost" (never received) from |
| 1196 | * the viewer's point of view. |
| 1197 | */ |
| 1198 | pthread_mutex_lock(&session->lock); |
| 1199 | /* |
| 1200 | * If a session rotation is ongoing, do not attempt to open any |
| 1201 | * stream, because the chunk can be in an intermediate state |
| 1202 | * due to directory renaming. |
| 1203 | */ |
| 1204 | if (session_has_ongoing_rotation(session)) { |
| 1205 | DBG("Relay session %" PRIu64 " rotation ongoing", session_id); |
| 1206 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW); |
| 1207 | goto send_reply_unlock; |
| 1208 | } |
| 1209 | ret = make_viewer_streams(session, |
| 1210 | conn->viewer_session, |
| 1211 | LTTNG_VIEWER_SEEK_BEGINNING, |
| 1212 | &nb_total, |
| 1213 | &nb_unsent, |
| 1214 | &nb_created, |
| 1215 | &closed); |
| 1216 | if (ret < 0) { |
| 1217 | /* |
| 1218 | * This is caused by an internal error; propagate the negative |
| 1219 | * 'ret' to close the connection. |
| 1220 | */ |
| 1221 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
| 1222 | goto send_reply_unlock; |
| 1223 | } |
| 1224 | |
| 1225 | uatomic_set(&session->new_streams, 0); |
| 1226 | send_streams = 1; |
| 1227 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK); |
| 1228 | |
| 1229 | /* Only send back the newly created streams with the unsent ones. */ |
| 1230 | nb_streams = nb_created + nb_unsent; |
| 1231 | response.streams_count = htobe32(nb_streams); |
| 1232 | |
| 1233 | /* |
| 1234 | * If the session is closed, HUP when there are no more streams |
| 1235 | * with data. |
| 1236 | */ |
| 1237 | if (closed && nb_total == 0) { |
| 1238 | send_streams = 0; |
| 1239 | response.streams_count = 0; |
| 1240 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP); |
| 1241 | goto send_reply_unlock; |
| 1242 | } |
| 1243 | send_reply_unlock: |
| 1244 | pthread_mutex_unlock(&session->lock); |
| 1245 | |
| 1246 | send_reply: |
| 1247 | health_code_update(); |
| 1248 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 1249 | if (ret < 0) { |
| 1250 | goto end_put_session; |
| 1251 | } |
| 1252 | health_code_update(); |
| 1253 | |
| 1254 | /* |
| 1255 | * Unknown or empty session, just return gracefully, the viewer |
| 1256 | * knows what is happening. |
| 1257 | */ |
| 1258 | if (!send_streams || !nb_streams) { |
| 1259 | ret = 0; |
| 1260 | goto end_put_session; |
| 1261 | } |
| 1262 | |
| 1263 | /* |
| 1264 | * Send stream and *DON'T* ignore the sent flag so every viewer |
| 1265 | * streams that were not sent from that point will be sent to |
| 1266 | * the viewer. |
| 1267 | */ |
| 1268 | ret = send_viewer_streams(conn->sock, session_id, 0); |
| 1269 | if (ret < 0) { |
| 1270 | goto end_put_session; |
| 1271 | } |
| 1272 | |
| 1273 | end_put_session: |
| 1274 | if (session) { |
| 1275 | session_put(session); |
| 1276 | } |
| 1277 | error: |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Send the viewer the list of current sessions. |
| 1283 | */ |
| 1284 | static int viewer_attach_session(struct relay_connection *conn) |
| 1285 | { |
| 1286 | int send_streams = 0; |
| 1287 | ssize_t ret; |
| 1288 | uint32_t nb_streams = 0; |
| 1289 | enum lttng_viewer_seek seek_type; |
| 1290 | struct lttng_viewer_attach_session_request request; |
| 1291 | struct lttng_viewer_attach_session_response response; |
| 1292 | struct relay_session *session = nullptr; |
| 1293 | enum lttng_viewer_attach_return_code viewer_attach_status; |
| 1294 | bool closed = false; |
| 1295 | uint64_t session_id; |
| 1296 | |
| 1297 | LTTNG_ASSERT(conn); |
| 1298 | |
| 1299 | health_code_update(); |
| 1300 | |
| 1301 | /* Receive the request from the connected client. */ |
| 1302 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 1303 | if (ret < 0) { |
| 1304 | goto error; |
| 1305 | } |
| 1306 | |
| 1307 | session_id = be64toh(request.session_id); |
| 1308 | |
| 1309 | health_code_update(); |
| 1310 | |
| 1311 | memset(&response, 0, sizeof(response)); |
| 1312 | |
| 1313 | if (!conn->viewer_session) { |
| 1314 | viewer_attach_status = LTTNG_VIEWER_ATTACH_NO_SESSION; |
| 1315 | DBG("Client trying to attach before creating a live viewer session, returning status=%s", |
| 1316 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1317 | goto send_reply; |
| 1318 | } |
| 1319 | |
| 1320 | session = session_get_by_id(session_id); |
| 1321 | if (!session) { |
| 1322 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
| 1323 | DBG("Relay session %" PRIu64 " not found, returning status=%s", |
| 1324 | session_id, |
| 1325 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1326 | goto send_reply; |
| 1327 | } |
| 1328 | DBG("Attach relay session ID %" PRIu64 " received", session_id); |
| 1329 | |
| 1330 | pthread_mutex_lock(&session->lock); |
| 1331 | if (session->live_timer == 0) { |
| 1332 | viewer_attach_status = LTTNG_VIEWER_ATTACH_NOT_LIVE; |
| 1333 | DBG("Relay session ID %" PRIu64 " is not a live session, returning status=%s", |
| 1334 | session_id, |
| 1335 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1336 | goto send_reply; |
| 1337 | } |
| 1338 | |
| 1339 | send_streams = 1; |
| 1340 | viewer_attach_status = viewer_session_attach(conn->viewer_session, session); |
| 1341 | if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) { |
| 1342 | DBG("Error attaching to relay session %" PRIu64 ", returning status=%s", |
| 1343 | session_id, |
| 1344 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1345 | goto send_reply; |
| 1346 | } |
| 1347 | |
| 1348 | switch (be32toh(request.seek)) { |
| 1349 | case LTTNG_VIEWER_SEEK_BEGINNING: |
| 1350 | case LTTNG_VIEWER_SEEK_LAST: |
| 1351 | viewer_attach_status = LTTNG_VIEWER_ATTACH_OK; |
| 1352 | seek_type = (lttng_viewer_seek) be32toh(request.seek); |
| 1353 | break; |
| 1354 | default: |
| 1355 | ERR("Wrong seek parameter for relay session %" PRIu64 ", returning status=%s", |
| 1356 | session_id, |
| 1357 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1358 | viewer_attach_status = LTTNG_VIEWER_ATTACH_SEEK_ERR; |
| 1359 | send_streams = 0; |
| 1360 | goto send_reply; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * If a session rotation is ongoing, do not attempt to open any |
| 1365 | * stream, because the chunk can be in an intermediate state |
| 1366 | * due to directory renaming. |
| 1367 | */ |
| 1368 | if (session_has_ongoing_rotation(session)) { |
| 1369 | DBG("Relay session %" PRIu64 " rotation ongoing", session_id); |
| 1370 | send_streams = 0; |
| 1371 | goto send_reply; |
| 1372 | } |
| 1373 | |
| 1374 | ret = make_viewer_streams( |
| 1375 | session, conn->viewer_session, seek_type, &nb_streams, nullptr, nullptr, &closed); |
| 1376 | if (ret < 0) { |
| 1377 | goto end_put_session; |
| 1378 | } |
| 1379 | pthread_mutex_unlock(&session->lock); |
| 1380 | session_put(session); |
| 1381 | session = nullptr; |
| 1382 | |
| 1383 | response.streams_count = htobe32(nb_streams); |
| 1384 | /* |
| 1385 | * If the session is closed when the viewer is attaching, it |
| 1386 | * means some of the streams may have been concurrently removed, |
| 1387 | * so we don't allow the viewer to attach, even if there are |
| 1388 | * streams available. |
| 1389 | */ |
| 1390 | if (closed) { |
| 1391 | send_streams = 0; |
| 1392 | response.streams_count = 0; |
| 1393 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
| 1394 | ERR("Session %" PRIu64 " is closed, returning status=%s", |
| 1395 | session_id, |
| 1396 | lttng_viewer_attach_return_code_str(viewer_attach_status)); |
| 1397 | goto send_reply; |
| 1398 | } |
| 1399 | |
| 1400 | send_reply: |
| 1401 | health_code_update(); |
| 1402 | |
| 1403 | response.status = htobe32((uint32_t) viewer_attach_status); |
| 1404 | |
| 1405 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 1406 | if (ret < 0) { |
| 1407 | goto end_put_session; |
| 1408 | } |
| 1409 | health_code_update(); |
| 1410 | |
| 1411 | /* |
| 1412 | * Unknown or empty session, just return gracefully, the viewer |
| 1413 | * knows what is happening. |
| 1414 | */ |
| 1415 | if (!send_streams || !nb_streams) { |
| 1416 | ret = 0; |
| 1417 | goto end_put_session; |
| 1418 | } |
| 1419 | |
| 1420 | /* Send stream and ignore the sent flag. */ |
| 1421 | ret = send_viewer_streams(conn->sock, session_id, 1); |
| 1422 | if (ret < 0) { |
| 1423 | goto end_put_session; |
| 1424 | } |
| 1425 | |
| 1426 | end_put_session: |
| 1427 | if (session) { |
| 1428 | pthread_mutex_unlock(&session->lock); |
| 1429 | session_put(session); |
| 1430 | } |
| 1431 | error: |
| 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * Open the index file if needed for the given vstream. |
| 1437 | * |
| 1438 | * If an index file is successfully opened, the vstream will set it as its |
| 1439 | * current index file. |
| 1440 | * |
| 1441 | * Return 0 on success, a negative value on error (-ENOENT if not ready yet). |
| 1442 | * |
| 1443 | * Called with rstream lock held. |
| 1444 | */ |
| 1445 | static int try_open_index(struct relay_viewer_stream *vstream, struct relay_stream *rstream) |
| 1446 | { |
| 1447 | int ret = 0; |
| 1448 | const uint32_t connection_major = rstream->trace->session->major; |
| 1449 | const uint32_t connection_minor = rstream->trace->session->minor; |
| 1450 | enum lttng_trace_chunk_status chunk_status; |
| 1451 | |
| 1452 | if (vstream->index_file) { |
| 1453 | goto end; |
| 1454 | } |
| 1455 | |
| 1456 | /* |
| 1457 | * First time, we open the index file and at least one index is ready. |
| 1458 | */ |
| 1459 | if (rstream->index_received_seqcount == 0 || !vstream->stream_file.trace_chunk) { |
| 1460 | ret = -ENOENT; |
| 1461 | goto end; |
| 1462 | } |
| 1463 | |
| 1464 | chunk_status = lttng_index_file_create_from_trace_chunk_read_only( |
| 1465 | vstream->stream_file.trace_chunk, |
| 1466 | rstream->path_name, |
| 1467 | rstream->channel_name, |
| 1468 | rstream->tracefile_size, |
| 1469 | vstream->current_tracefile_id, |
| 1470 | lttng_to_index_major(connection_major, connection_minor), |
| 1471 | lttng_to_index_minor(connection_major, connection_minor), |
| 1472 | true, |
| 1473 | &vstream->index_file); |
| 1474 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1475 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { |
| 1476 | ret = -ENOENT; |
| 1477 | } else { |
| 1478 | ret = -1; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | end: |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Check the status of the index for the given stream. This function |
| 1488 | * updates the index structure if needed and can put (close) the vstream |
| 1489 | * in the HUP situation. |
| 1490 | * |
| 1491 | * Return 0 means that we can proceed with the index. A value of 1 means |
| 1492 | * that the index has been updated and is ready to be sent to the |
| 1493 | * client. A negative value indicates an error that can't be handled. |
| 1494 | * |
| 1495 | * Called with rstream lock held. |
| 1496 | */ |
| 1497 | static int check_index_status(struct relay_viewer_stream *vstream, |
| 1498 | struct relay_stream *rstream, |
| 1499 | struct ctf_trace *trace, |
| 1500 | struct lttng_viewer_index *index) |
| 1501 | { |
| 1502 | int ret; |
| 1503 | |
| 1504 | DBG("Check index status: index_received_seqcount %" PRIu64 " " |
| 1505 | "index_sent_seqcount %" PRIu64 " " |
| 1506 | "for stream %" PRIu64, |
| 1507 | rstream->index_received_seqcount, |
| 1508 | vstream->index_sent_seqcount, |
| 1509 | vstream->stream->stream_handle); |
| 1510 | if ((trace->session->connection_closed || rstream->closed) && |
| 1511 | rstream->index_received_seqcount == vstream->index_sent_seqcount) { |
| 1512 | /* |
| 1513 | * Last index sent and session connection or relay |
| 1514 | * stream are closed. |
| 1515 | */ |
| 1516 | index->status = LTTNG_VIEWER_INDEX_HUP; |
| 1517 | DBG("Check index status: Connection or stream are closed, stream %" PRIu64 |
| 1518 | ",connection-closed=%d, relay-stream-closed=%d, returning status=%s", |
| 1519 | vstream->stream->stream_handle, |
| 1520 | trace->session->connection_closed, |
| 1521 | rstream->closed, |
| 1522 | lttng_viewer_next_index_return_code_str( |
| 1523 | (enum lttng_viewer_next_index_return_code) index->status)); |
| 1524 | goto hup; |
| 1525 | } else if (rstream->beacon_ts_end != -1ULL && |
| 1526 | (rstream->index_received_seqcount == 0 || |
| 1527 | (vstream->index_sent_seqcount != 0 && |
| 1528 | rstream->index_received_seqcount <= vstream->index_sent_seqcount))) { |
| 1529 | /* |
| 1530 | * We've received a synchronization beacon and the last index |
| 1531 | * available has been sent, the index for now is inactive. |
| 1532 | * |
| 1533 | * In this case, we have received a beacon which allows us to |
| 1534 | * inform the client of a time interval during which we can |
| 1535 | * guarantee that there are no events to read (and never will |
| 1536 | * be). |
| 1537 | * |
| 1538 | * The sent seqcount can grow higher than receive seqcount on |
| 1539 | * clear because the rotation performed by clear will push |
| 1540 | * the index_sent_seqcount ahead (see |
| 1541 | * viewer_stream_sync_tracefile_array_tail) and skip over |
| 1542 | * packet sequence numbers. |
| 1543 | */ |
| 1544 | index->status = LTTNG_VIEWER_INDEX_INACTIVE; |
| 1545 | index->timestamp_end = htobe64(rstream->beacon_ts_end); |
| 1546 | index->stream_id = htobe64(rstream->ctf_stream_id); |
| 1547 | DBG("Check index status: inactive with beacon, for stream %" PRIu64 |
| 1548 | ", returning status=%s", |
| 1549 | vstream->stream->stream_handle, |
| 1550 | lttng_viewer_next_index_return_code_str( |
| 1551 | (enum lttng_viewer_next_index_return_code) index->status)); |
| 1552 | goto index_ready; |
| 1553 | } else if (rstream->index_received_seqcount == 0 || |
| 1554 | (vstream->index_sent_seqcount != 0 && |
| 1555 | rstream->index_received_seqcount <= vstream->index_sent_seqcount)) { |
| 1556 | /* |
| 1557 | * This checks whether received <= sent seqcount. In |
| 1558 | * this case, we have not received a beacon. Therefore, |
| 1559 | * we can only ask the client to retry later. |
| 1560 | * |
| 1561 | * The sent seqcount can grow higher than receive seqcount on |
| 1562 | * clear because the rotation performed by clear will push |
| 1563 | * the index_sent_seqcount ahead (see |
| 1564 | * viewer_stream_sync_tracefile_array_tail) and skip over |
| 1565 | * packet sequence numbers. |
| 1566 | */ |
| 1567 | index->status = LTTNG_VIEWER_INDEX_RETRY; |
| 1568 | DBG("Check index status:" |
| 1569 | "did not received beacon for stream %" PRIu64 ", returning status=%s", |
| 1570 | vstream->stream->stream_handle, |
| 1571 | lttng_viewer_next_index_return_code_str( |
| 1572 | (enum lttng_viewer_next_index_return_code) index->status)); |
| 1573 | goto index_ready; |
| 1574 | } else if (!tracefile_array_seq_in_file(rstream->tfa, |
| 1575 | vstream->current_tracefile_id, |
| 1576 | vstream->index_sent_seqcount)) { |
| 1577 | /* |
| 1578 | * The next index we want to send cannot be read either |
| 1579 | * because we need to perform a rotation, or due to |
| 1580 | * the producer having overwritten its trace file. |
| 1581 | */ |
| 1582 | DBG("Viewer stream %" PRIu64 " rotation", vstream->stream->stream_handle); |
| 1583 | ret = viewer_stream_rotate(vstream); |
| 1584 | if (ret == 1) { |
| 1585 | /* EOF across entire stream. */ |
| 1586 | index->status = LTTNG_VIEWER_INDEX_HUP; |
| 1587 | DBG("Check index status:" |
| 1588 | "reached end of file for stream %" PRIu64 ", returning status=%s", |
| 1589 | vstream->stream->stream_handle, |
| 1590 | lttng_viewer_next_index_return_code_str( |
| 1591 | (enum lttng_viewer_next_index_return_code) index->status)); |
| 1592 | goto hup; |
| 1593 | } |
| 1594 | /* |
| 1595 | * If we have been pushed due to overwrite, it |
| 1596 | * necessarily means there is data that can be read in |
| 1597 | * the stream. If we rotated because we reached the end |
| 1598 | * of a tracefile, it means the following tracefile |
| 1599 | * needs to contain at least one index, else we would |
| 1600 | * have already returned LTTNG_VIEWER_INDEX_RETRY to the |
| 1601 | * viewer. The updated index_sent_seqcount needs to |
| 1602 | * point to a readable index entry now. |
| 1603 | * |
| 1604 | * In the case where we "rotate" on a single file, we |
| 1605 | * can end up in a case where the requested index is |
| 1606 | * still unavailable. |
| 1607 | */ |
| 1608 | if (rstream->tracefile_count == 1 && |
| 1609 | !tracefile_array_seq_in_file(rstream->tfa, |
| 1610 | vstream->current_tracefile_id, |
| 1611 | vstream->index_sent_seqcount)) { |
| 1612 | index->status = LTTNG_VIEWER_INDEX_RETRY; |
| 1613 | DBG("Check index status:" |
| 1614 | "tracefile array sequence number %" PRIu64 |
| 1615 | " not in file for stream %" PRIu64 ", returning status=%s", |
| 1616 | vstream->index_sent_seqcount, |
| 1617 | vstream->stream->stream_handle, |
| 1618 | lttng_viewer_next_index_return_code_str( |
| 1619 | (enum lttng_viewer_next_index_return_code) index->status)); |
| 1620 | goto index_ready; |
| 1621 | } |
| 1622 | LTTNG_ASSERT(tracefile_array_seq_in_file( |
| 1623 | rstream->tfa, vstream->current_tracefile_id, vstream->index_sent_seqcount)); |
| 1624 | } |
| 1625 | /* ret == 0 means successful so we continue. */ |
| 1626 | ret = 0; |
| 1627 | return ret; |
| 1628 | |
| 1629 | hup: |
| 1630 | viewer_stream_put(vstream); |
| 1631 | index_ready: |
| 1632 | return 1; |
| 1633 | } |
| 1634 | |
| 1635 | static void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream *vstream, |
| 1636 | struct lttng_trace_chunk *new_trace_chunk) |
| 1637 | { |
| 1638 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
| 1639 | |
| 1640 | if (new_trace_chunk) { |
| 1641 | const bool acquired_reference = lttng_trace_chunk_get(new_trace_chunk); |
| 1642 | |
| 1643 | LTTNG_ASSERT(acquired_reference); |
| 1644 | } |
| 1645 | |
| 1646 | vstream->stream_file.trace_chunk = new_trace_chunk; |
| 1647 | viewer_stream_sync_tracefile_array_tail(vstream); |
| 1648 | viewer_stream_close_files(vstream); |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Send the next index for a stream. |
| 1653 | * |
| 1654 | * Return 0 on success or else a negative value. |
| 1655 | */ |
| 1656 | static int viewer_get_next_index(struct relay_connection *conn) |
| 1657 | { |
| 1658 | int ret; |
| 1659 | struct lttng_viewer_get_next_index request_index; |
| 1660 | struct lttng_viewer_index viewer_index; |
| 1661 | struct ctf_packet_index packet_index; |
| 1662 | struct relay_viewer_stream *vstream = nullptr; |
| 1663 | struct relay_stream *rstream = nullptr; |
| 1664 | struct ctf_trace *ctf_trace = nullptr; |
| 1665 | struct relay_viewer_stream *metadata_viewer_stream = nullptr; |
| 1666 | bool viewer_stream_and_session_in_same_chunk, viewer_stream_one_rotation_behind; |
| 1667 | uint64_t stream_file_chunk_id = -1ULL, viewer_session_chunk_id = -1ULL; |
| 1668 | enum lttng_trace_chunk_status status; |
| 1669 | bool attached_sessions_have_new_streams = false; |
| 1670 | |
| 1671 | LTTNG_ASSERT(conn); |
| 1672 | |
| 1673 | memset(&viewer_index, 0, sizeof(viewer_index)); |
| 1674 | health_code_update(); |
| 1675 | |
| 1676 | ret = recv_request(conn->sock, &request_index, sizeof(request_index)); |
| 1677 | if (ret < 0) { |
| 1678 | goto end; |
| 1679 | } |
| 1680 | health_code_update(); |
| 1681 | |
| 1682 | vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id)); |
| 1683 | if (!vstream) { |
| 1684 | viewer_index.status = LTTNG_VIEWER_INDEX_ERR; |
| 1685 | DBG("Client requested index of unknown stream id %" PRIu64 ", returning status=%s", |
| 1686 | (uint64_t) be64toh(request_index.stream_id), |
| 1687 | lttng_viewer_next_index_return_code_str( |
| 1688 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1689 | goto send_reply; |
| 1690 | } |
| 1691 | |
| 1692 | /* Use back. ref. Protected by refcounts. */ |
| 1693 | rstream = vstream->stream; |
| 1694 | ctf_trace = rstream->trace; |
| 1695 | |
| 1696 | /* metadata_viewer_stream may be NULL. */ |
| 1697 | metadata_viewer_stream = ctf_trace_get_viewer_metadata_stream(ctf_trace); |
| 1698 | |
| 1699 | /* |
| 1700 | * Hold the session lock to protect against concurrent changes |
| 1701 | * to the chunk files (e.g. rename done by clear), which are |
| 1702 | * protected by the session ongoing rotation state. Those are |
| 1703 | * synchronized with the session lock. |
| 1704 | */ |
| 1705 | pthread_mutex_lock(&rstream->trace->session->lock); |
| 1706 | pthread_mutex_lock(&rstream->lock); |
| 1707 | |
| 1708 | /* |
| 1709 | * The viewer should not ask for index on metadata stream. |
| 1710 | */ |
| 1711 | if (rstream->is_metadata) { |
| 1712 | viewer_index.status = LTTNG_VIEWER_INDEX_HUP; |
| 1713 | DBG("Client requested index of a metadata stream id %" PRIu64 |
| 1714 | ", returning status=%s", |
| 1715 | (uint64_t) be64toh(request_index.stream_id), |
| 1716 | lttng_viewer_next_index_return_code_str( |
| 1717 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1718 | goto send_reply; |
| 1719 | } |
| 1720 | |
| 1721 | ret = check_new_streams(conn); |
| 1722 | if (ret < 0) { |
| 1723 | viewer_index.status = LTTNG_VIEWER_INDEX_ERR; |
| 1724 | ERR("Error checking for new streams in the attached sessions, returning status=%s", |
| 1725 | lttng_viewer_next_index_return_code_str( |
| 1726 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1727 | goto send_reply; |
| 1728 | } else if (ret == 1) { |
| 1729 | attached_sessions_have_new_streams = true; |
| 1730 | } |
| 1731 | |
| 1732 | if (rstream->ongoing_rotation.is_set) { |
| 1733 | /* Rotation is ongoing, try again later. */ |
| 1734 | viewer_index.status = LTTNG_VIEWER_INDEX_RETRY; |
| 1735 | DBG("Client requested index for stream id %" PRIu64 |
| 1736 | " while a stream rotation is ongoing, returning status=%s", |
| 1737 | (uint64_t) be64toh(request_index.stream_id), |
| 1738 | lttng_viewer_next_index_return_code_str( |
| 1739 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1740 | goto send_reply; |
| 1741 | } |
| 1742 | |
| 1743 | if (session_has_ongoing_rotation(rstream->trace->session)) { |
| 1744 | /* Rotation is ongoing, try again later. */ |
| 1745 | viewer_index.status = LTTNG_VIEWER_INDEX_RETRY; |
| 1746 | DBG("Client requested index for stream id %" PRIu64 |
| 1747 | " while a session rotation is ongoing, returning status=%s", |
| 1748 | (uint64_t) be64toh(request_index.stream_id), |
| 1749 | lttng_viewer_next_index_return_code_str( |
| 1750 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1751 | goto send_reply; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Transition the viewer session into the newest trace chunk available. |
| 1756 | */ |
| 1757 | if (!lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk, |
| 1758 | rstream->trace_chunk)) { |
| 1759 | DBG("Relay stream and viewer chunk ids differ"); |
| 1760 | |
| 1761 | ret = viewer_session_set_trace_chunk_copy(conn->viewer_session, |
| 1762 | rstream->trace_chunk); |
| 1763 | if (ret) { |
| 1764 | viewer_index.status = LTTNG_VIEWER_INDEX_ERR; |
| 1765 | ERR("Error copying trace chunk for stream id %" PRIu64 |
| 1766 | ", returning status=%s", |
| 1767 | (uint64_t) be64toh(request_index.stream_id), |
| 1768 | lttng_viewer_next_index_return_code_str( |
| 1769 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1770 | goto send_reply; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | /* |
| 1775 | * Transition the viewer stream into the latest trace chunk available. |
| 1776 | * |
| 1777 | * Note that the stream must _not_ rotate in one precise condition: |
| 1778 | * the relay stream has rotated to a NULL trace chunk and the viewer |
| 1779 | * stream is consuming the trace chunk that was active just before |
| 1780 | * that rotation to NULL. |
| 1781 | * |
| 1782 | * This allows clients to consume all the packets of a trace chunk |
| 1783 | * after a session's destruction. |
| 1784 | */ |
| 1785 | if (vstream->stream_file.trace_chunk) { |
| 1786 | status = lttng_trace_chunk_get_id(vstream->stream_file.trace_chunk, |
| 1787 | &stream_file_chunk_id); |
| 1788 | LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 1789 | } |
| 1790 | if (conn->viewer_session->current_trace_chunk) { |
| 1791 | status = lttng_trace_chunk_get_id(conn->viewer_session->current_trace_chunk, |
| 1792 | &viewer_session_chunk_id); |
| 1793 | LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 1794 | } |
| 1795 | |
| 1796 | viewer_stream_and_session_in_same_chunk = lttng_trace_chunk_ids_equal( |
| 1797 | conn->viewer_session->current_trace_chunk, vstream->stream_file.trace_chunk); |
| 1798 | viewer_stream_one_rotation_behind = rstream->completed_rotation_count == |
| 1799 | vstream->last_seen_rotation_count + 1; |
| 1800 | |
| 1801 | if (viewer_stream_and_session_in_same_chunk) { |
| 1802 | DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate", |
| 1803 | vstream->stream_file.trace_chunk ? |
| 1804 | std::to_string(stream_file_chunk_id).c_str() : |
| 1805 | "None", |
| 1806 | conn->viewer_session->current_trace_chunk ? |
| 1807 | std::to_string(viewer_session_chunk_id).c_str() : |
| 1808 | "None"); |
| 1809 | } else if (viewer_stream_one_rotation_behind && !rstream->trace_chunk) { |
| 1810 | DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate", |
| 1811 | vstream->stream_file.trace_chunk ? |
| 1812 | std::to_string(stream_file_chunk_id).c_str() : |
| 1813 | "None", |
| 1814 | conn->viewer_session->current_trace_chunk ? |
| 1815 | std::to_string(viewer_session_chunk_id).c_str() : |
| 1816 | "None"); |
| 1817 | } else { |
| 1818 | DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream", |
| 1819 | vstream->stream_file.trace_chunk ? |
| 1820 | std::to_string(stream_file_chunk_id).c_str() : |
| 1821 | "None", |
| 1822 | conn->viewer_session->current_trace_chunk ? |
| 1823 | std::to_string(viewer_session_chunk_id).c_str() : |
| 1824 | "None"); |
| 1825 | |
| 1826 | viewer_stream_rotate_to_trace_chunk(vstream, |
| 1827 | conn->viewer_session->current_trace_chunk); |
| 1828 | vstream->last_seen_rotation_count = rstream->completed_rotation_count; |
| 1829 | } |
| 1830 | |
| 1831 | ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index); |
| 1832 | if (ret < 0) { |
| 1833 | goto error_put; |
| 1834 | } else if (ret == 1) { |
| 1835 | /* |
| 1836 | * We have no index to send and check_index_status has populated |
| 1837 | * viewer_index's status. |
| 1838 | */ |
| 1839 | goto send_reply; |
| 1840 | } |
| 1841 | |
| 1842 | /* At this point, ret is 0 thus we will be able to read the index. */ |
| 1843 | LTTNG_ASSERT(!ret); |
| 1844 | |
| 1845 | /* Try to open an index if one is needed for that stream. */ |
| 1846 | ret = try_open_index(vstream, rstream); |
| 1847 | if (ret == -ENOENT) { |
| 1848 | if (rstream->closed) { |
| 1849 | viewer_index.status = LTTNG_VIEWER_INDEX_HUP; |
| 1850 | DBG("Cannot open index for stream id %" PRIu64 |
| 1851 | "stream is closed, returning status=%s", |
| 1852 | (uint64_t) be64toh(request_index.stream_id), |
| 1853 | lttng_viewer_next_index_return_code_str( |
| 1854 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1855 | goto send_reply; |
| 1856 | } else { |
| 1857 | viewer_index.status = LTTNG_VIEWER_INDEX_RETRY; |
| 1858 | DBG("Cannot open index for stream id %" PRIu64 ", returning status=%s", |
| 1859 | (uint64_t) be64toh(request_index.stream_id), |
| 1860 | lttng_viewer_next_index_return_code_str( |
| 1861 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1862 | goto send_reply; |
| 1863 | } |
| 1864 | } |
| 1865 | if (ret < 0) { |
| 1866 | viewer_index.status = LTTNG_VIEWER_INDEX_ERR; |
| 1867 | ERR("Error opening index for stream id %" PRIu64 ", returning status=%s", |
| 1868 | (uint64_t) be64toh(request_index.stream_id), |
| 1869 | lttng_viewer_next_index_return_code_str( |
| 1870 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1871 | goto send_reply; |
| 1872 | } |
| 1873 | |
| 1874 | /* |
| 1875 | * vstream->stream_fd may be NULL if it has been closed by |
| 1876 | * tracefile rotation, or if we are at the beginning of the |
| 1877 | * stream. We open the data stream file here to protect against |
| 1878 | * overwrite caused by tracefile rotation (in association with |
| 1879 | * unlink performed before overwrite). |
| 1880 | */ |
| 1881 | if (!vstream->stream_file.handle) { |
| 1882 | char file_path[LTTNG_PATH_MAX]; |
| 1883 | struct fs_handle *fs_handle; |
| 1884 | |
| 1885 | ret = utils_stream_file_path(rstream->path_name, |
| 1886 | rstream->channel_name, |
| 1887 | rstream->tracefile_size, |
| 1888 | vstream->current_tracefile_id, |
| 1889 | nullptr, |
| 1890 | file_path, |
| 1891 | sizeof(file_path)); |
| 1892 | if (ret < 0) { |
| 1893 | goto error_put; |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * It is possible the the file we are trying to open is |
| 1898 | * missing if the stream has been closed (application exits with |
| 1899 | * per-pid buffers) and a clear command has been performed. |
| 1900 | */ |
| 1901 | status = lttng_trace_chunk_open_fs_handle( |
| 1902 | vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true); |
| 1903 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1904 | if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE && rstream->closed) { |
| 1905 | viewer_index.status = LTTNG_VIEWER_INDEX_HUP; |
| 1906 | DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64 |
| 1907 | ", returning status=%s", |
| 1908 | (uint64_t) be64toh(request_index.stream_id), |
| 1909 | lttng_viewer_next_index_return_code_str( |
| 1910 | (enum lttng_viewer_next_index_return_code) |
| 1911 | viewer_index.status)); |
| 1912 | goto send_reply; |
| 1913 | } |
| 1914 | PERROR("Failed to open trace file for viewer stream"); |
| 1915 | goto error_put; |
| 1916 | } |
| 1917 | vstream->stream_file.handle = fs_handle; |
| 1918 | } |
| 1919 | |
| 1920 | ret = lttng_index_file_read(vstream->index_file, &packet_index); |
| 1921 | if (ret) { |
| 1922 | viewer_index.status = LTTNG_VIEWER_INDEX_ERR; |
| 1923 | ERR("Relay error reading index file for stream id %" PRIu64 ", returning status=%s", |
| 1924 | (uint64_t) be64toh(request_index.stream_id), |
| 1925 | lttng_viewer_next_index_return_code_str( |
| 1926 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1927 | goto send_reply; |
| 1928 | } else { |
| 1929 | viewer_index.status = LTTNG_VIEWER_INDEX_OK; |
| 1930 | DBG("Read index file for stream id %" PRIu64 ", returning status=%s", |
| 1931 | (uint64_t) be64toh(request_index.stream_id), |
| 1932 | lttng_viewer_next_index_return_code_str( |
| 1933 | (enum lttng_viewer_next_index_return_code) viewer_index.status)); |
| 1934 | vstream->index_sent_seqcount++; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * Indexes are stored in big endian, no need to switch before sending. |
| 1939 | */ |
| 1940 | DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64, |
| 1941 | rstream->stream_handle, |
| 1942 | (uint64_t) be64toh(packet_index.offset)); |
| 1943 | viewer_index.offset = packet_index.offset; |
| 1944 | viewer_index.packet_size = packet_index.packet_size; |
| 1945 | viewer_index.content_size = packet_index.content_size; |
| 1946 | viewer_index.timestamp_begin = packet_index.timestamp_begin; |
| 1947 | viewer_index.timestamp_end = packet_index.timestamp_end; |
| 1948 | viewer_index.events_discarded = packet_index.events_discarded; |
| 1949 | viewer_index.stream_id = packet_index.stream_id; |
| 1950 | |
| 1951 | send_reply: |
| 1952 | if (rstream) { |
| 1953 | pthread_mutex_unlock(&rstream->lock); |
| 1954 | pthread_mutex_unlock(&rstream->trace->session->lock); |
| 1955 | } |
| 1956 | |
| 1957 | if (metadata_viewer_stream) { |
| 1958 | pthread_mutex_lock(&metadata_viewer_stream->stream->lock); |
| 1959 | DBG("get next index metadata check: recv %" PRIu64 " sent %" PRIu64, |
| 1960 | metadata_viewer_stream->stream->metadata_received, |
| 1961 | metadata_viewer_stream->metadata_sent); |
| 1962 | if (!metadata_viewer_stream->stream->metadata_received || |
| 1963 | metadata_viewer_stream->stream->metadata_received > |
| 1964 | metadata_viewer_stream->metadata_sent) { |
| 1965 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA; |
| 1966 | } |
| 1967 | pthread_mutex_unlock(&metadata_viewer_stream->stream->lock); |
| 1968 | } |
| 1969 | |
| 1970 | if (attached_sessions_have_new_streams) { |
| 1971 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM; |
| 1972 | } |
| 1973 | |
| 1974 | viewer_index.flags = htobe32(viewer_index.flags); |
| 1975 | viewer_index.status = htobe32(viewer_index.status); |
| 1976 | health_code_update(); |
| 1977 | |
| 1978 | ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index)); |
| 1979 | if (ret < 0) { |
| 1980 | goto end; |
| 1981 | } |
| 1982 | health_code_update(); |
| 1983 | |
| 1984 | if (vstream) { |
| 1985 | DBG("Index %" PRIu64 " for stream %" PRIu64 " sent", |
| 1986 | vstream->index_sent_seqcount, |
| 1987 | vstream->stream->stream_handle); |
| 1988 | } |
| 1989 | end: |
| 1990 | if (metadata_viewer_stream) { |
| 1991 | viewer_stream_put(metadata_viewer_stream); |
| 1992 | } |
| 1993 | if (vstream) { |
| 1994 | viewer_stream_put(vstream); |
| 1995 | } |
| 1996 | return ret; |
| 1997 | |
| 1998 | error_put: |
| 1999 | pthread_mutex_unlock(&rstream->lock); |
| 2000 | pthread_mutex_unlock(&rstream->trace->session->lock); |
| 2001 | if (metadata_viewer_stream) { |
| 2002 | viewer_stream_put(metadata_viewer_stream); |
| 2003 | } |
| 2004 | viewer_stream_put(vstream); |
| 2005 | return ret; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * Send the next index for a stream |
| 2010 | * |
| 2011 | * Return 0 on success or else a negative value. |
| 2012 | */ |
| 2013 | static int viewer_get_packet(struct relay_connection *conn) |
| 2014 | { |
| 2015 | int ret; |
| 2016 | off_t lseek_ret; |
| 2017 | char *reply = nullptr; |
| 2018 | struct lttng_viewer_get_packet get_packet_info; |
| 2019 | struct lttng_viewer_trace_packet reply_header; |
| 2020 | struct relay_viewer_stream *vstream = nullptr; |
| 2021 | uint32_t reply_size = sizeof(reply_header); |
| 2022 | uint32_t packet_data_len = 0; |
| 2023 | ssize_t read_len; |
| 2024 | uint64_t stream_id; |
| 2025 | enum lttng_viewer_get_packet_return_code get_packet_status; |
| 2026 | |
| 2027 | health_code_update(); |
| 2028 | |
| 2029 | ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info)); |
| 2030 | if (ret < 0) { |
| 2031 | goto end; |
| 2032 | } |
| 2033 | health_code_update(); |
| 2034 | |
| 2035 | /* From this point on, the error label can be reached. */ |
| 2036 | memset(&reply_header, 0, sizeof(reply_header)); |
| 2037 | stream_id = (uint64_t) be64toh(get_packet_info.stream_id); |
| 2038 | |
| 2039 | vstream = viewer_stream_get_by_id(stream_id); |
| 2040 | if (!vstream) { |
| 2041 | get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR; |
| 2042 | DBG("Client requested packet of unknown stream id %" PRIu64 ", returning status=%s", |
| 2043 | stream_id, |
| 2044 | lttng_viewer_get_packet_return_code_str(get_packet_status)); |
| 2045 | goto send_reply_nolock; |
| 2046 | } else { |
| 2047 | packet_data_len = be32toh(get_packet_info.len); |
| 2048 | reply_size += packet_data_len; |
| 2049 | } |
| 2050 | |
| 2051 | reply = zmalloc<char>(reply_size); |
| 2052 | if (!reply) { |
| 2053 | get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR; |
| 2054 | PERROR("Falled to allocate reply, returning status=%s", |
| 2055 | lttng_viewer_get_packet_return_code_str(get_packet_status)); |
| 2056 | goto error; |
| 2057 | } |
| 2058 | |
| 2059 | pthread_mutex_lock(&vstream->stream->lock); |
| 2060 | lseek_ret = fs_handle_seek( |
| 2061 | vstream->stream_file.handle, be64toh(get_packet_info.offset), SEEK_SET); |
| 2062 | if (lseek_ret < 0) { |
| 2063 | get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR; |
| 2064 | PERROR("Failed to seek file system handle of viewer stream %" PRIu64 |
| 2065 | " to offset %" PRIu64 ", returning status=%s", |
| 2066 | stream_id, |
| 2067 | (uint64_t) be64toh(get_packet_info.offset), |
| 2068 | lttng_viewer_get_packet_return_code_str(get_packet_status)); |
| 2069 | goto error; |
| 2070 | } |
| 2071 | read_len = fs_handle_read( |
| 2072 | vstream->stream_file.handle, reply + sizeof(reply_header), packet_data_len); |
| 2073 | if (read_len < packet_data_len) { |
| 2074 | get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR; |
| 2075 | PERROR("Failed to read from file system handle of viewer stream id %" PRIu64 |
| 2076 | ", offset: %" PRIu64 ", returning status=%s", |
| 2077 | stream_id, |
| 2078 | (uint64_t) be64toh(get_packet_info.offset), |
| 2079 | lttng_viewer_get_packet_return_code_str(get_packet_status)); |
| 2080 | goto error; |
| 2081 | } |
| 2082 | |
| 2083 | get_packet_status = LTTNG_VIEWER_GET_PACKET_OK; |
| 2084 | reply_header.len = htobe32(packet_data_len); |
| 2085 | goto send_reply; |
| 2086 | |
| 2087 | error: |
| 2088 | /* No payload to send on error. */ |
| 2089 | reply_size = sizeof(reply_header); |
| 2090 | |
| 2091 | send_reply: |
| 2092 | if (vstream) { |
| 2093 | pthread_mutex_unlock(&vstream->stream->lock); |
| 2094 | } |
| 2095 | send_reply_nolock: |
| 2096 | |
| 2097 | health_code_update(); |
| 2098 | |
| 2099 | reply_header.status = htobe32(get_packet_status); |
| 2100 | if (reply) { |
| 2101 | memcpy(reply, &reply_header, sizeof(reply_header)); |
| 2102 | ret = send_response(conn->sock, reply, reply_size); |
| 2103 | } else { |
| 2104 | /* No reply to send. */ |
| 2105 | ret = send_response(conn->sock, &reply_header, reply_size); |
| 2106 | } |
| 2107 | |
| 2108 | health_code_update(); |
| 2109 | if (ret < 0) { |
| 2110 | PERROR("sendmsg of packet data failed"); |
| 2111 | goto end_free; |
| 2112 | } |
| 2113 | |
| 2114 | DBG("Sent %u bytes for stream %" PRIu64, reply_size, stream_id); |
| 2115 | |
| 2116 | end_free: |
| 2117 | free(reply); |
| 2118 | end: |
| 2119 | if (vstream) { |
| 2120 | viewer_stream_put(vstream); |
| 2121 | } |
| 2122 | return ret; |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * Send the session's metadata |
| 2127 | * |
| 2128 | * Return 0 on success else a negative value. |
| 2129 | */ |
| 2130 | static int viewer_get_metadata(struct relay_connection *conn) |
| 2131 | { |
| 2132 | int ret = 0; |
| 2133 | int fd = -1; |
| 2134 | ssize_t read_len; |
| 2135 | uint64_t len = 0; |
| 2136 | char *data = nullptr; |
| 2137 | struct lttng_viewer_get_metadata request; |
| 2138 | struct lttng_viewer_metadata_packet reply; |
| 2139 | struct relay_viewer_stream *vstream = nullptr; |
| 2140 | bool dispose_of_stream = false; |
| 2141 | |
| 2142 | LTTNG_ASSERT(conn); |
| 2143 | |
| 2144 | health_code_update(); |
| 2145 | |
| 2146 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 2147 | if (ret < 0) { |
| 2148 | goto end; |
| 2149 | } |
| 2150 | health_code_update(); |
| 2151 | |
| 2152 | memset(&reply, 0, sizeof(reply)); |
| 2153 | |
| 2154 | vstream = viewer_stream_get_by_id(be64toh(request.stream_id)); |
| 2155 | if (!vstream) { |
| 2156 | /* |
| 2157 | * The metadata stream can be closed by a CLOSE command |
| 2158 | * just before we attach. It can also be closed by |
| 2159 | * per-pid tracing during tracing. Therefore, it is |
| 2160 | * possible that we cannot find this viewer stream. |
| 2161 | * Reply back to the client with an error if we cannot |
| 2162 | * find it. |
| 2163 | */ |
| 2164 | DBG("Client requested metadata of unknown stream id %" PRIu64, |
| 2165 | (uint64_t) be64toh(request.stream_id)); |
| 2166 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 2167 | goto send_reply; |
| 2168 | } |
| 2169 | |
| 2170 | pthread_mutex_lock(&vstream->stream->trace->session->lock); |
| 2171 | pthread_mutex_lock(&vstream->stream->trace->lock); |
| 2172 | pthread_mutex_lock(&vstream->stream->lock); |
| 2173 | if (!vstream->stream->is_metadata) { |
| 2174 | ERR("Invalid metadata stream"); |
| 2175 | goto error; |
| 2176 | } |
| 2177 | |
| 2178 | if (vstream->metadata_sent >= vstream->stream->metadata_received) { |
| 2179 | /* |
| 2180 | * Clear feature resets the metadata_received to 0 until the |
| 2181 | * same metadata is received again. |
| 2182 | */ |
| 2183 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); |
| 2184 | /* |
| 2185 | * The live viewer considers a closed 0 byte metadata stream as |
| 2186 | * an error. |
| 2187 | */ |
| 2188 | dispose_of_stream = vstream->metadata_sent > 0 && vstream->stream->closed; |
| 2189 | goto send_reply; |
| 2190 | } |
| 2191 | |
| 2192 | if (vstream->stream->trace_chunk && |
| 2193 | !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk, |
| 2194 | vstream->stream->trace_chunk)) { |
| 2195 | /* A rotation has occurred on the relay stream. */ |
| 2196 | DBG("Metadata relay stream and viewer chunk ids differ"); |
| 2197 | |
| 2198 | ret = viewer_session_set_trace_chunk_copy(conn->viewer_session, |
| 2199 | vstream->stream->trace_chunk); |
| 2200 | if (ret) { |
| 2201 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 2202 | goto send_reply; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | if (conn->viewer_session->current_trace_chunk && |
| 2207 | !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk, |
| 2208 | vstream->stream_file.trace_chunk)) { |
| 2209 | bool acquired_reference; |
| 2210 | |
| 2211 | DBG("Viewer session and viewer stream chunk differ: " |
| 2212 | "vsession chunk %p vstream chunk %p", |
| 2213 | conn->viewer_session->current_trace_chunk, |
| 2214 | vstream->stream_file.trace_chunk); |
| 2215 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
| 2216 | acquired_reference = |
| 2217 | lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk); |
| 2218 | LTTNG_ASSERT(acquired_reference); |
| 2219 | vstream->stream_file.trace_chunk = conn->viewer_session->current_trace_chunk; |
| 2220 | viewer_stream_close_files(vstream); |
| 2221 | } |
| 2222 | |
| 2223 | len = vstream->stream->metadata_received - vstream->metadata_sent; |
| 2224 | |
| 2225 | if (!vstream->stream_file.trace_chunk) { |
| 2226 | if (vstream->stream->trace->session->connection_closed) { |
| 2227 | /* |
| 2228 | * If the connection is closed, there is no way for the metadata stream |
| 2229 | * to ever transition back to an active chunk. As such, signal to the viewer |
| 2230 | * that there is no new metadata available. |
| 2231 | * |
| 2232 | * The stream can be disposed-of. On the next execution of this command, |
| 2233 | * the relay daemon will reply with an error status since the stream can't |
| 2234 | * be found. |
| 2235 | */ |
| 2236 | dispose_of_stream = true; |
| 2237 | } |
| 2238 | |
| 2239 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); |
| 2240 | len = 0; |
| 2241 | goto send_reply; |
| 2242 | } else if (vstream->stream_file.trace_chunk && !vstream->stream_file.handle && len > 0) { |
| 2243 | /* |
| 2244 | * Either this is the first time the metadata file is read, or a |
| 2245 | * rotation of the corresponding relay stream has occurred. |
| 2246 | */ |
| 2247 | struct fs_handle *fs_handle; |
| 2248 | char file_path[LTTNG_PATH_MAX]; |
| 2249 | enum lttng_trace_chunk_status status; |
| 2250 | struct relay_stream *rstream = vstream->stream; |
| 2251 | |
| 2252 | ret = utils_stream_file_path(rstream->path_name, |
| 2253 | rstream->channel_name, |
| 2254 | rstream->tracefile_size, |
| 2255 | vstream->current_tracefile_id, |
| 2256 | nullptr, |
| 2257 | file_path, |
| 2258 | sizeof(file_path)); |
| 2259 | if (ret < 0) { |
| 2260 | goto error; |
| 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * It is possible the the metadata file we are trying to open is |
| 2265 | * missing if the stream has been closed (application exits with |
| 2266 | * per-pid buffers) and a clear command has been performed. |
| 2267 | */ |
| 2268 | status = lttng_trace_chunk_open_fs_handle( |
| 2269 | vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true); |
| 2270 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 2271 | if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { |
| 2272 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); |
| 2273 | len = 0; |
| 2274 | if (vstream->stream->closed) { |
| 2275 | viewer_stream_put(vstream); |
| 2276 | } |
| 2277 | goto send_reply; |
| 2278 | } |
| 2279 | PERROR("Failed to open metadata file for viewer stream"); |
| 2280 | goto error; |
| 2281 | } |
| 2282 | vstream->stream_file.handle = fs_handle; |
| 2283 | |
| 2284 | if (vstream->metadata_sent != 0) { |
| 2285 | /* |
| 2286 | * The client does not expect to receive any metadata |
| 2287 | * it has received and metadata files in successive |
| 2288 | * chunks must be a strict superset of one another. |
| 2289 | * |
| 2290 | * Skip the first `metadata_sent` bytes to ensure |
| 2291 | * they are not sent a second time to the client. |
| 2292 | * |
| 2293 | * Baring a block layer error or an internal error, |
| 2294 | * this seek should not fail as |
| 2295 | * `vstream->stream->metadata_received` is reset when |
| 2296 | * a relay stream is rotated. If this is reached, it is |
| 2297 | * safe to assume that |
| 2298 | * `metadata_received` > `metadata_sent`. |
| 2299 | */ |
| 2300 | const off_t seek_ret = |
| 2301 | fs_handle_seek(fs_handle, vstream->metadata_sent, SEEK_SET); |
| 2302 | |
| 2303 | if (seek_ret < 0) { |
| 2304 | PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64, |
| 2305 | vstream->metadata_sent); |
| 2306 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 2307 | goto send_reply; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | reply.len = htobe64(len); |
| 2313 | data = zmalloc<char>(len); |
| 2314 | if (!data) { |
| 2315 | PERROR("viewer metadata zmalloc"); |
| 2316 | goto error; |
| 2317 | } |
| 2318 | |
| 2319 | fd = fs_handle_get_fd(vstream->stream_file.handle); |
| 2320 | if (fd < 0) { |
| 2321 | ERR("Failed to restore viewer stream file system handle"); |
| 2322 | goto error; |
| 2323 | } |
| 2324 | read_len = lttng_read(fd, data, len); |
| 2325 | fs_handle_put_fd(vstream->stream_file.handle); |
| 2326 | fd = -1; |
| 2327 | if (read_len < len) { |
| 2328 | if (read_len < 0) { |
| 2329 | PERROR("Failed to read metadata file"); |
| 2330 | goto error; |
| 2331 | } else { |
| 2332 | /* |
| 2333 | * A clear has been performed which prevents the relay |
| 2334 | * from sending `len` bytes of metadata. |
| 2335 | * |
| 2336 | * It is important not to send any metadata if we |
| 2337 | * couldn't read all the available metadata in one shot: |
| 2338 | * sending partial metadata can cause the client to |
| 2339 | * attempt to parse an incomplete (incoherent) metadata |
| 2340 | * stream, which would result in an error. |
| 2341 | */ |
| 2342 | const off_t seek_ret = |
| 2343 | fs_handle_seek(vstream->stream_file.handle, -read_len, SEEK_CUR); |
| 2344 | |
| 2345 | DBG("Failed to read metadata: requested = %" PRIu64 ", got = %zd", |
| 2346 | len, |
| 2347 | read_len); |
| 2348 | read_len = 0; |
| 2349 | len = 0; |
| 2350 | if (seek_ret < 0) { |
| 2351 | PERROR("Failed to restore metadata file position after partial read"); |
| 2352 | ret = -1; |
| 2353 | goto error; |
| 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | vstream->metadata_sent += read_len; |
| 2358 | reply.status = htobe32(LTTNG_VIEWER_METADATA_OK); |
| 2359 | |
| 2360 | goto send_reply; |
| 2361 | |
| 2362 | error: |
| 2363 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 2364 | |
| 2365 | send_reply: |
| 2366 | health_code_update(); |
| 2367 | if (vstream) { |
| 2368 | pthread_mutex_unlock(&vstream->stream->lock); |
| 2369 | pthread_mutex_unlock(&vstream->stream->trace->lock); |
| 2370 | pthread_mutex_unlock(&vstream->stream->trace->session->lock); |
| 2371 | } |
| 2372 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
| 2373 | if (ret < 0) { |
| 2374 | goto end_free; |
| 2375 | } |
| 2376 | health_code_update(); |
| 2377 | |
| 2378 | if (len > 0) { |
| 2379 | ret = send_response(conn->sock, data, len); |
| 2380 | if (ret < 0) { |
| 2381 | goto end_free; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, |
| 2386 | len, |
| 2387 | (uint64_t) be64toh(request.stream_id)); |
| 2388 | |
| 2389 | DBG("Metadata sent"); |
| 2390 | |
| 2391 | end_free: |
| 2392 | free(data); |
| 2393 | end: |
| 2394 | if (vstream) { |
| 2395 | viewer_stream_put(vstream); |
| 2396 | if (dispose_of_stream) { |
| 2397 | /* |
| 2398 | * Trigger the destruction of the viewer stream |
| 2399 | * by releasing its global reference. |
| 2400 | * |
| 2401 | * The live viewers expect to receive a NO_NEW_METADATA |
| 2402 | * status before a stream disappears, otherwise they abort the |
| 2403 | * entire live connection when receiving an error status. |
| 2404 | * |
| 2405 | * On the next query for this stream, an error will be reported to the |
| 2406 | * client. |
| 2407 | */ |
| 2408 | viewer_stream_put(vstream); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | return ret; |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * Create a viewer session. |
| 2417 | * |
| 2418 | * Return 0 on success or else a negative value. |
| 2419 | */ |
| 2420 | static int viewer_create_session(struct relay_connection *conn) |
| 2421 | { |
| 2422 | int ret; |
| 2423 | struct lttng_viewer_create_session_response resp; |
| 2424 | |
| 2425 | memset(&resp, 0, sizeof(resp)); |
| 2426 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK); |
| 2427 | conn->viewer_session = viewer_session_create(); |
| 2428 | if (!conn->viewer_session) { |
| 2429 | ERR("Allocation viewer session"); |
| 2430 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR); |
| 2431 | goto send_reply; |
| 2432 | } |
| 2433 | |
| 2434 | send_reply: |
| 2435 | health_code_update(); |
| 2436 | ret = send_response(conn->sock, &resp, sizeof(resp)); |
| 2437 | if (ret < 0) { |
| 2438 | goto end; |
| 2439 | } |
| 2440 | health_code_update(); |
| 2441 | ret = 0; |
| 2442 | |
| 2443 | end: |
| 2444 | return ret; |
| 2445 | } |
| 2446 | |
| 2447 | /* |
| 2448 | * Detach a viewer session. |
| 2449 | * |
| 2450 | * Return 0 on success or else a negative value. |
| 2451 | */ |
| 2452 | static int viewer_detach_session(struct relay_connection *conn) |
| 2453 | { |
| 2454 | int ret; |
| 2455 | struct lttng_viewer_detach_session_response response; |
| 2456 | struct lttng_viewer_detach_session_request request; |
| 2457 | struct relay_session *session = nullptr; |
| 2458 | uint64_t viewer_session_to_close; |
| 2459 | |
| 2460 | LTTNG_ASSERT(conn); |
| 2461 | |
| 2462 | health_code_update(); |
| 2463 | |
| 2464 | /* Receive the request from the connected client. */ |
| 2465 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 2466 | if (ret < 0) { |
| 2467 | goto end; |
| 2468 | } |
| 2469 | viewer_session_to_close = be64toh(request.session_id); |
| 2470 | |
| 2471 | if (!conn->viewer_session) { |
| 2472 | DBG("Client trying to detach before creating a live viewer session"); |
| 2473 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); |
| 2474 | goto send_reply; |
| 2475 | } |
| 2476 | |
| 2477 | health_code_update(); |
| 2478 | |
| 2479 | memset(&response, 0, sizeof(response)); |
| 2480 | DBG("Detaching from session ID %" PRIu64, viewer_session_to_close); |
| 2481 | |
| 2482 | session = session_get_by_id(be64toh(request.session_id)); |
| 2483 | if (!session) { |
| 2484 | DBG("Relay session %" PRIu64 " not found", (uint64_t) be64toh(request.session_id)); |
| 2485 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK); |
| 2486 | goto send_reply; |
| 2487 | } |
| 2488 | |
| 2489 | ret = viewer_session_is_attached(conn->viewer_session, session); |
| 2490 | if (ret != 1) { |
| 2491 | DBG("Not attached to this session"); |
| 2492 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); |
| 2493 | goto send_reply_put; |
| 2494 | } |
| 2495 | |
| 2496 | viewer_session_close_one_session(conn->viewer_session, session); |
| 2497 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK); |
| 2498 | DBG("Session %" PRIu64 " detached.", viewer_session_to_close); |
| 2499 | |
| 2500 | send_reply_put: |
| 2501 | session_put(session); |
| 2502 | |
| 2503 | send_reply: |
| 2504 | health_code_update(); |
| 2505 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 2506 | if (ret < 0) { |
| 2507 | goto end; |
| 2508 | } |
| 2509 | health_code_update(); |
| 2510 | ret = 0; |
| 2511 | |
| 2512 | end: |
| 2513 | return ret; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * live_relay_unknown_command: send -1 if received unknown command |
| 2518 | */ |
| 2519 | static void live_relay_unknown_command(struct relay_connection *conn) |
| 2520 | { |
| 2521 | struct lttcomm_relayd_generic_reply reply; |
| 2522 | |
| 2523 | memset(&reply, 0, sizeof(reply)); |
| 2524 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
| 2525 | (void) send_response(conn->sock, &reply, sizeof(reply)); |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * Process the commands received on the control socket |
| 2530 | */ |
| 2531 | static int process_control(struct lttng_viewer_cmd *recv_hdr, struct relay_connection *conn) |
| 2532 | { |
| 2533 | int ret = 0; |
| 2534 | lttng_viewer_command cmd = (lttng_viewer_command) be32toh(recv_hdr->cmd); |
| 2535 | |
| 2536 | /* |
| 2537 | * Make sure we've done the version check before any command other then |
| 2538 | * a new client connection. |
| 2539 | */ |
| 2540 | if (cmd != LTTNG_VIEWER_CONNECT && !conn->version_check_done) { |
| 2541 | ERR("Viewer on connection %d requested %s command before version check", |
| 2542 | conn->sock->fd, |
| 2543 | lttng_viewer_command_str(cmd)); |
| 2544 | ret = -1; |
| 2545 | goto end; |
| 2546 | } |
| 2547 | |
| 2548 | DBG("Processing %s viewer command from connection %d", |
| 2549 | lttng_viewer_command_str(cmd), |
| 2550 | conn->sock->fd); |
| 2551 | |
| 2552 | switch (cmd) { |
| 2553 | case LTTNG_VIEWER_CONNECT: |
| 2554 | ret = viewer_connect(conn); |
| 2555 | break; |
| 2556 | case LTTNG_VIEWER_LIST_SESSIONS: |
| 2557 | ret = viewer_list_sessions(conn); |
| 2558 | break; |
| 2559 | case LTTNG_VIEWER_ATTACH_SESSION: |
| 2560 | ret = viewer_attach_session(conn); |
| 2561 | break; |
| 2562 | case LTTNG_VIEWER_GET_NEXT_INDEX: |
| 2563 | ret = viewer_get_next_index(conn); |
| 2564 | break; |
| 2565 | case LTTNG_VIEWER_GET_PACKET: |
| 2566 | ret = viewer_get_packet(conn); |
| 2567 | break; |
| 2568 | case LTTNG_VIEWER_GET_METADATA: |
| 2569 | ret = viewer_get_metadata(conn); |
| 2570 | break; |
| 2571 | case LTTNG_VIEWER_GET_NEW_STREAMS: |
| 2572 | ret = viewer_get_new_streams(conn); |
| 2573 | break; |
| 2574 | case LTTNG_VIEWER_CREATE_SESSION: |
| 2575 | ret = viewer_create_session(conn); |
| 2576 | break; |
| 2577 | case LTTNG_VIEWER_DETACH_SESSION: |
| 2578 | ret = viewer_detach_session(conn); |
| 2579 | break; |
| 2580 | default: |
| 2581 | ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd)); |
| 2582 | live_relay_unknown_command(conn); |
| 2583 | ret = -1; |
| 2584 | goto end; |
| 2585 | } |
| 2586 | |
| 2587 | end: |
| 2588 | return ret; |
| 2589 | } |
| 2590 | |
| 2591 | static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
| 2592 | { |
| 2593 | int ret; |
| 2594 | |
| 2595 | (void) lttng_poll_del(events, pollfd); |
| 2596 | |
| 2597 | ret = fd_tracker_close_unsuspendable_fd( |
| 2598 | the_fd_tracker, &pollfd, 1, fd_tracker_util_close_fd, nullptr); |
| 2599 | if (ret < 0) { |
| 2600 | ERR("Closing pollfd %d", pollfd); |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | /* |
| 2605 | * This thread does the actual work |
| 2606 | */ |
| 2607 | static void *thread_worker(void *data __attribute__((unused))) |
| 2608 | { |
| 2609 | int ret, err = -1; |
| 2610 | uint32_t nb_fd; |
| 2611 | struct lttng_poll_event events; |
| 2612 | struct lttng_ht *viewer_connections_ht; |
| 2613 | struct lttng_ht_iter iter; |
| 2614 | struct lttng_viewer_cmd recv_hdr; |
| 2615 | struct relay_connection *destroy_conn; |
| 2616 | |
| 2617 | DBG("[thread] Live viewer relay worker started"); |
| 2618 | |
| 2619 | rcu_register_thread(); |
| 2620 | |
| 2621 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER); |
| 2622 | |
| 2623 | if (testpoint(relayd_thread_live_worker)) { |
| 2624 | goto error_testpoint; |
| 2625 | } |
| 2626 | |
| 2627 | /* table of connections indexed on socket */ |
| 2628 | viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 2629 | if (!viewer_connections_ht) { |
| 2630 | goto viewer_connections_ht_error; |
| 2631 | } |
| 2632 | |
| 2633 | ret = create_named_thread_poll_set(&events, 2, "Live viewer worker thread epoll"); |
| 2634 | if (ret < 0) { |
| 2635 | goto error_poll_create; |
| 2636 | } |
| 2637 | |
| 2638 | ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 2639 | if (ret < 0) { |
| 2640 | goto error; |
| 2641 | } |
| 2642 | |
| 2643 | restart: |
| 2644 | while (true) { |
| 2645 | int i; |
| 2646 | |
| 2647 | health_code_update(); |
| 2648 | |
| 2649 | /* Infinite blocking call, waiting for transmission */ |
| 2650 | DBG3("Relayd live viewer worker thread polling..."); |
| 2651 | health_poll_entry(); |
| 2652 | ret = lttng_poll_wait(&events, -1); |
| 2653 | health_poll_exit(); |
| 2654 | if (ret < 0) { |
| 2655 | /* |
| 2656 | * Restart interrupted system call. |
| 2657 | */ |
| 2658 | if (errno == EINTR) { |
| 2659 | goto restart; |
| 2660 | } |
| 2661 | goto error; |
| 2662 | } |
| 2663 | |
| 2664 | nb_fd = ret; |
| 2665 | |
| 2666 | /* |
| 2667 | * Process control. The control connection is prioritised so we don't |
| 2668 | * starve it with high throughput tracing data on the data |
| 2669 | * connection. |
| 2670 | */ |
| 2671 | for (i = 0; i < nb_fd; i++) { |
| 2672 | /* Fetch once the poll data */ |
| 2673 | const auto revents = LTTNG_POLL_GETEV(&events, i); |
| 2674 | const auto pollfd = LTTNG_POLL_GETFD(&events, i); |
| 2675 | |
| 2676 | health_code_update(); |
| 2677 | |
| 2678 | /* Activity on thread quit pipe, exiting. */ |
| 2679 | if (relayd_is_thread_quit_pipe(pollfd)) { |
| 2680 | DBG("Activity on thread quit pipe"); |
| 2681 | err = 0; |
| 2682 | goto exit; |
| 2683 | } |
| 2684 | |
| 2685 | /* Inspect the relay conn pipe for new connection. */ |
| 2686 | if (pollfd == live_conn_pipe[0]) { |
| 2687 | if (revents & LPOLLIN) { |
| 2688 | struct relay_connection *conn; |
| 2689 | |
| 2690 | ret = lttng_read(live_conn_pipe[0], |
| 2691 | &conn, |
| 2692 | sizeof(conn)); /* NOLINT sizeof used on a |
| 2693 | pointer. */ |
| 2694 | if (ret < 0) { |
| 2695 | goto error; |
| 2696 | } |
| 2697 | ret = lttng_poll_add( |
| 2698 | &events, conn->sock->fd, LPOLLIN | LPOLLRDHUP); |
| 2699 | if (ret) { |
| 2700 | ERR("Failed to add new live connection file descriptor to poll set"); |
| 2701 | goto error; |
| 2702 | } |
| 2703 | connection_ht_add(viewer_connections_ht, conn); |
| 2704 | DBG("Connection socket %d added to poll", conn->sock->fd); |
| 2705 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 2706 | ERR("Relay live pipe error"); |
| 2707 | goto error; |
| 2708 | } else { |
| 2709 | ERR("Unexpected poll events %u for sock %d", |
| 2710 | revents, |
| 2711 | pollfd); |
| 2712 | goto error; |
| 2713 | } |
| 2714 | } else { |
| 2715 | /* Connection activity. */ |
| 2716 | struct relay_connection *conn; |
| 2717 | |
| 2718 | conn = connection_get_by_sock(viewer_connections_ht, pollfd); |
| 2719 | if (!conn) { |
| 2720 | continue; |
| 2721 | } |
| 2722 | |
| 2723 | if (revents & LPOLLIN) { |
| 2724 | ret = conn->sock->ops->recvmsg( |
| 2725 | conn->sock, &recv_hdr, sizeof(recv_hdr), 0); |
| 2726 | if (ret <= 0) { |
| 2727 | /* Connection closed. */ |
| 2728 | cleanup_connection_pollfd(&events, pollfd); |
| 2729 | /* Put "create" ownership reference. */ |
| 2730 | connection_put(conn); |
| 2731 | DBG("Viewer control conn closed with %d", pollfd); |
| 2732 | } else { |
| 2733 | ret = process_control(&recv_hdr, conn); |
| 2734 | if (ret < 0) { |
| 2735 | /* Clear the session on error. */ |
| 2736 | cleanup_connection_pollfd(&events, pollfd); |
| 2737 | /* Put "create" ownership reference. */ |
| 2738 | connection_put(conn); |
| 2739 | DBG("Viewer connection closed with %d", |
| 2740 | pollfd); |
| 2741 | } |
| 2742 | } |
| 2743 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 2744 | cleanup_connection_pollfd(&events, pollfd); |
| 2745 | /* Put "create" ownership reference. */ |
| 2746 | connection_put(conn); |
| 2747 | } else { |
| 2748 | ERR("Unexpected poll events %u for sock %d", |
| 2749 | revents, |
| 2750 | pollfd); |
| 2751 | connection_put(conn); |
| 2752 | goto error; |
| 2753 | } |
| 2754 | /* Put local "get_by_sock" reference. */ |
| 2755 | connection_put(conn); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | exit: |
| 2761 | error: |
| 2762 | (void) fd_tracker_util_poll_clean(the_fd_tracker, &events); |
| 2763 | |
| 2764 | /* Cleanup remaining connection object. */ |
| 2765 | { |
| 2766 | lttng::urcu::read_lock_guard read_lock; |
| 2767 | |
| 2768 | cds_lfht_for_each_entry ( |
| 2769 | viewer_connections_ht->ht, &iter.iter, destroy_conn, sock_n.node) { |
| 2770 | health_code_update(); |
| 2771 | connection_put(destroy_conn); |
| 2772 | } |
| 2773 | } |
| 2774 | error_poll_create: |
| 2775 | lttng_ht_destroy(viewer_connections_ht); |
| 2776 | viewer_connections_ht_error: |
| 2777 | /* Close relay conn pipes */ |
| 2778 | (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe); |
| 2779 | if (err) { |
| 2780 | DBG("Viewer worker thread exited with error"); |
| 2781 | } |
| 2782 | DBG("Viewer worker thread cleanup complete"); |
| 2783 | error_testpoint: |
| 2784 | if (err) { |
| 2785 | health_error(); |
| 2786 | ERR("Health error occurred in %s", __func__); |
| 2787 | } |
| 2788 | health_unregister(health_relayd); |
| 2789 | if (lttng_relay_stop_threads()) { |
| 2790 | ERR("Error stopping threads"); |
| 2791 | } |
| 2792 | rcu_unregister_thread(); |
| 2793 | return nullptr; |
| 2794 | } |
| 2795 | |
| 2796 | /* |
| 2797 | * Create the relay command pipe to wake thread_manage_apps. |
| 2798 | * Closed in cleanup(). |
| 2799 | */ |
| 2800 | static int create_conn_pipe() |
| 2801 | { |
| 2802 | return fd_tracker_util_pipe_open_cloexec( |
| 2803 | the_fd_tracker, "Live connection pipe", live_conn_pipe); |
| 2804 | } |
| 2805 | |
| 2806 | int relayd_live_join() |
| 2807 | { |
| 2808 | int ret, retval = 0; |
| 2809 | void *status; |
| 2810 | |
| 2811 | ret = pthread_join(live_listener_thread, &status); |
| 2812 | if (ret) { |
| 2813 | errno = ret; |
| 2814 | PERROR("pthread_join live listener"); |
| 2815 | retval = -1; |
| 2816 | } |
| 2817 | |
| 2818 | ret = pthread_join(live_worker_thread, &status); |
| 2819 | if (ret) { |
| 2820 | errno = ret; |
| 2821 | PERROR("pthread_join live worker"); |
| 2822 | retval = -1; |
| 2823 | } |
| 2824 | |
| 2825 | ret = pthread_join(live_dispatcher_thread, &status); |
| 2826 | if (ret) { |
| 2827 | errno = ret; |
| 2828 | PERROR("pthread_join live dispatcher"); |
| 2829 | retval = -1; |
| 2830 | } |
| 2831 | |
| 2832 | cleanup_relayd_live(); |
| 2833 | |
| 2834 | return retval; |
| 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * main |
| 2839 | */ |
| 2840 | int relayd_live_create(struct lttng_uri *uri) |
| 2841 | { |
| 2842 | int ret = 0, retval = 0; |
| 2843 | void *status; |
| 2844 | int is_root; |
| 2845 | |
| 2846 | if (!uri) { |
| 2847 | retval = -1; |
| 2848 | goto exit_init_data; |
| 2849 | } |
| 2850 | live_uri = uri; |
| 2851 | |
| 2852 | /* Check if daemon is UID = 0 */ |
| 2853 | is_root = !getuid(); |
| 2854 | |
| 2855 | if (!is_root) { |
| 2856 | if (live_uri->port < 1024) { |
| 2857 | ERR("Need to be root to use ports < 1024"); |
| 2858 | retval = -1; |
| 2859 | goto exit_init_data; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | /* Setup the thread apps communication pipe. */ |
| 2864 | if (create_conn_pipe()) { |
| 2865 | retval = -1; |
| 2866 | goto exit_init_data; |
| 2867 | } |
| 2868 | |
| 2869 | /* Init relay command queue. */ |
| 2870 | cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail); |
| 2871 | |
| 2872 | /* Set up max poll set size */ |
| 2873 | if (lttng_poll_set_max_size()) { |
| 2874 | retval = -1; |
| 2875 | goto exit_init_data; |
| 2876 | } |
| 2877 | |
| 2878 | /* Setup the dispatcher thread */ |
| 2879 | ret = pthread_create(&live_dispatcher_thread, |
| 2880 | default_pthread_attr(), |
| 2881 | thread_dispatcher, |
| 2882 | (void *) nullptr); |
| 2883 | if (ret) { |
| 2884 | errno = ret; |
| 2885 | PERROR("pthread_create viewer dispatcher"); |
| 2886 | retval = -1; |
| 2887 | goto exit_dispatcher_thread; |
| 2888 | } |
| 2889 | |
| 2890 | /* Setup the worker thread */ |
| 2891 | ret = pthread_create(&live_worker_thread, default_pthread_attr(), thread_worker, nullptr); |
| 2892 | if (ret) { |
| 2893 | errno = ret; |
| 2894 | PERROR("pthread_create viewer worker"); |
| 2895 | retval = -1; |
| 2896 | goto exit_worker_thread; |
| 2897 | } |
| 2898 | |
| 2899 | /* Setup the listener thread */ |
| 2900 | ret = pthread_create( |
| 2901 | &live_listener_thread, default_pthread_attr(), thread_listener, (void *) nullptr); |
| 2902 | if (ret) { |
| 2903 | errno = ret; |
| 2904 | PERROR("pthread_create viewer listener"); |
| 2905 | retval = -1; |
| 2906 | goto exit_listener_thread; |
| 2907 | } |
| 2908 | |
| 2909 | /* |
| 2910 | * All OK, started all threads. |
| 2911 | */ |
| 2912 | return retval; |
| 2913 | |
| 2914 | /* |
| 2915 | * Join on the live_listener_thread should anything be added after |
| 2916 | * the live_listener thread's creation. |
| 2917 | */ |
| 2918 | |
| 2919 | exit_listener_thread: |
| 2920 | |
| 2921 | ret = pthread_join(live_worker_thread, &status); |
| 2922 | if (ret) { |
| 2923 | errno = ret; |
| 2924 | PERROR("pthread_join live worker"); |
| 2925 | retval = -1; |
| 2926 | } |
| 2927 | exit_worker_thread: |
| 2928 | |
| 2929 | ret = pthread_join(live_dispatcher_thread, &status); |
| 2930 | if (ret) { |
| 2931 | errno = ret; |
| 2932 | PERROR("pthread_join live dispatcher"); |
| 2933 | retval = -1; |
| 2934 | } |
| 2935 | exit_dispatcher_thread: |
| 2936 | |
| 2937 | exit_init_data: |
| 2938 | cleanup_relayd_live(); |
| 2939 | |
| 2940 | return retval; |
| 2941 | } |