| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _LGPL_SOURCE |
| 21 | #include <getopt.h> |
| 22 | #include <grp.h> |
| 23 | #include <limits.h> |
| 24 | #include <pthread.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/mount.h> |
| 31 | #include <sys/resource.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/wait.h> |
| 36 | #include <inttypes.h> |
| 37 | #include <urcu/futex.h> |
| 38 | #include <urcu/uatomic.h> |
| 39 | #include <urcu/rculist.h> |
| 40 | #include <unistd.h> |
| 41 | #include <fcntl.h> |
| 42 | |
| 43 | #include <lttng/lttng.h> |
| 44 | #include <common/common.h> |
| 45 | #include <common/compat/poll.h> |
| 46 | #include <common/compat/socket.h> |
| 47 | #include <common/compat/endian.h> |
| 48 | #include <common/defaults.h> |
| 49 | #include <common/futex.h> |
| 50 | #include <common/index/index.h> |
| 51 | #include <common/sessiond-comm/sessiond-comm.h> |
| 52 | #include <common/sessiond-comm/inet.h> |
| 53 | #include <common/sessiond-comm/relayd.h> |
| 54 | #include <common/uri.h> |
| 55 | #include <common/utils.h> |
| 56 | |
| 57 | #include "cmd.h" |
| 58 | #include "live.h" |
| 59 | #include "lttng-relayd.h" |
| 60 | #include "utils.h" |
| 61 | #include "health-relayd.h" |
| 62 | #include "testpoint.h" |
| 63 | #include "viewer-stream.h" |
| 64 | #include "stream.h" |
| 65 | #include "session.h" |
| 66 | #include "ctf-trace.h" |
| 67 | #include "connection.h" |
| 68 | #include "viewer-session.h" |
| 69 | |
| 70 | #define SESSION_BUF_DEFAULT_COUNT 16 |
| 71 | |
| 72 | static struct lttng_uri *live_uri; |
| 73 | |
| 74 | /* |
| 75 | * This pipe is used to inform the worker thread that a command is queued and |
| 76 | * ready to be processed. |
| 77 | */ |
| 78 | static int live_conn_pipe[2] = { -1, -1 }; |
| 79 | |
| 80 | /* Shared between threads */ |
| 81 | static int live_dispatch_thread_exit; |
| 82 | |
| 83 | static pthread_t live_listener_thread; |
| 84 | static pthread_t live_dispatcher_thread; |
| 85 | static pthread_t live_worker_thread; |
| 86 | |
| 87 | /* |
| 88 | * Relay command queue. |
| 89 | * |
| 90 | * The live_thread_listener and live_thread_dispatcher communicate with this |
| 91 | * queue. |
| 92 | */ |
| 93 | static struct relay_conn_queue viewer_conn_queue; |
| 94 | |
| 95 | static uint64_t last_relay_viewer_session_id; |
| 96 | static pthread_mutex_t last_relay_viewer_session_id_lock = |
| 97 | PTHREAD_MUTEX_INITIALIZER; |
| 98 | |
| 99 | /* |
| 100 | * Cleanup the daemon |
| 101 | */ |
| 102 | static |
| 103 | void cleanup_relayd_live(void) |
| 104 | { |
| 105 | DBG("Cleaning up"); |
| 106 | |
| 107 | free(live_uri); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Receive a request buffer using a given socket, destination allocated buffer |
| 112 | * of length size. |
| 113 | * |
| 114 | * Return the size of the received message or else a negative value on error |
| 115 | * with errno being set by recvmsg() syscall. |
| 116 | */ |
| 117 | static |
| 118 | ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size) |
| 119 | { |
| 120 | ssize_t ret; |
| 121 | |
| 122 | ret = sock->ops->recvmsg(sock, buf, size, 0); |
| 123 | if (ret < 0 || ret != size) { |
| 124 | if (ret == 0) { |
| 125 | /* Orderly shutdown. Not necessary to print an error. */ |
| 126 | DBG("Socket %d did an orderly shutdown", sock->fd); |
| 127 | } else { |
| 128 | ERR("Relay failed to receive request."); |
| 129 | } |
| 130 | ret = -1; |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Send a response buffer using a given socket, source allocated buffer of |
| 138 | * length size. |
| 139 | * |
| 140 | * Return the size of the sent message or else a negative value on error with |
| 141 | * errno being set by sendmsg() syscall. |
| 142 | */ |
| 143 | static |
| 144 | ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size) |
| 145 | { |
| 146 | ssize_t ret; |
| 147 | |
| 148 | ret = sock->ops->sendmsg(sock, buf, size, 0); |
| 149 | if (ret < 0) { |
| 150 | ERR("Relayd failed to send response."); |
| 151 | } |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Atomically check if new streams got added in one of the sessions attached |
| 158 | * and reset the flag to 0. |
| 159 | * |
| 160 | * Returns 1 if new streams got added, 0 if nothing changed, a negative value |
| 161 | * on error. |
| 162 | */ |
| 163 | static |
| 164 | int check_new_streams(struct relay_connection *conn) |
| 165 | { |
| 166 | struct relay_session *session; |
| 167 | unsigned long current_val; |
| 168 | int ret = 0; |
| 169 | |
| 170 | if (!conn->viewer_session) { |
| 171 | goto end; |
| 172 | } |
| 173 | rcu_read_lock(); |
| 174 | cds_list_for_each_entry_rcu(session, |
| 175 | &conn->viewer_session->session_list, |
| 176 | viewer_session_node) { |
| 177 | if (!session_get(session)) { |
| 178 | continue; |
| 179 | } |
| 180 | current_val = uatomic_cmpxchg(&session->new_streams, 1, 0); |
| 181 | ret = current_val; |
| 182 | session_put(session); |
| 183 | if (ret == 1) { |
| 184 | goto end; |
| 185 | } |
| 186 | } |
| 187 | end: |
| 188 | rcu_read_unlock(); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Send viewer streams to the given socket. The ignore_sent_flag indicates if |
| 194 | * this function should ignore the sent flag or not. |
| 195 | * |
| 196 | * Return 0 on success or else a negative value. |
| 197 | */ |
| 198 | static |
| 199 | ssize_t send_viewer_streams(struct lttcomm_sock *sock, |
| 200 | struct relay_session *session, unsigned int ignore_sent_flag) |
| 201 | { |
| 202 | ssize_t ret; |
| 203 | struct lttng_viewer_stream send_stream; |
| 204 | struct lttng_ht_iter iter; |
| 205 | struct relay_viewer_stream *vstream; |
| 206 | |
| 207 | rcu_read_lock(); |
| 208 | |
| 209 | cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream, |
| 210 | stream_n.node) { |
| 211 | struct ctf_trace *ctf_trace; |
| 212 | |
| 213 | health_code_update(); |
| 214 | |
| 215 | if (!viewer_stream_get(vstream)) { |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | pthread_mutex_lock(&vstream->stream->lock); |
| 220 | /* Ignore if not the same session. */ |
| 221 | if (vstream->stream->trace->session->id != session->id || |
| 222 | (!ignore_sent_flag && vstream->sent_flag)) { |
| 223 | pthread_mutex_unlock(&vstream->stream->lock); |
| 224 | viewer_stream_put(vstream); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | ctf_trace = vstream->stream->trace; |
| 229 | send_stream.id = htobe64(vstream->stream->stream_handle); |
| 230 | send_stream.ctf_trace_id = htobe64(ctf_trace->id); |
| 231 | send_stream.metadata_flag = htobe32( |
| 232 | vstream->stream->is_metadata); |
| 233 | strncpy(send_stream.path_name, vstream->path_name, |
| 234 | sizeof(send_stream.path_name)); |
| 235 | strncpy(send_stream.channel_name, vstream->channel_name, |
| 236 | sizeof(send_stream.channel_name)); |
| 237 | |
| 238 | DBG("Sending stream %" PRIu64 " to viewer", |
| 239 | vstream->stream->stream_handle); |
| 240 | vstream->sent_flag = 1; |
| 241 | pthread_mutex_unlock(&vstream->stream->lock); |
| 242 | |
| 243 | ret = send_response(sock, &send_stream, sizeof(send_stream)); |
| 244 | viewer_stream_put(vstream); |
| 245 | if (ret < 0) { |
| 246 | goto end_unlock; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | ret = 0; |
| 251 | |
| 252 | end_unlock: |
| 253 | rcu_read_unlock(); |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Create every viewer stream possible for the given session with the seek |
| 259 | * type. Three counters *can* be return which are in order the total amount of |
| 260 | * viewer stream of the session, the number of unsent stream and the number of |
| 261 | * stream created. Those counters can be NULL and thus will be ignored. |
| 262 | * |
| 263 | * Return 0 on success or else a negative value. |
| 264 | */ |
| 265 | static |
| 266 | int make_viewer_streams(struct relay_session *session, |
| 267 | enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent, |
| 268 | uint32_t *nb_created, bool *closed) |
| 269 | { |
| 270 | int ret; |
| 271 | struct lttng_ht_iter iter; |
| 272 | struct ctf_trace *ctf_trace; |
| 273 | |
| 274 | assert(session); |
| 275 | |
| 276 | /* |
| 277 | * Hold the session lock to ensure that we see either none or |
| 278 | * all initial streams for a session, but no intermediate state. |
| 279 | */ |
| 280 | pthread_mutex_lock(&session->lock); |
| 281 | |
| 282 | if (session->connection_closed) { |
| 283 | *closed = true; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Create viewer streams for relay streams that are ready to be |
| 288 | * used for a the given session id only. |
| 289 | */ |
| 290 | rcu_read_lock(); |
| 291 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace, |
| 292 | node.node) { |
| 293 | struct relay_stream *stream; |
| 294 | |
| 295 | health_code_update(); |
| 296 | |
| 297 | if (!ctf_trace_get(ctf_trace)) { |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | cds_list_for_each_entry_rcu(stream, &ctf_trace->stream_list, stream_node) { |
| 302 | struct relay_viewer_stream *vstream; |
| 303 | |
| 304 | if (!stream_get(stream)) { |
| 305 | continue; |
| 306 | } |
| 307 | /* |
| 308 | * stream published is protected by the session lock. |
| 309 | */ |
| 310 | if (!stream->published) { |
| 311 | goto next; |
| 312 | } |
| 313 | vstream = viewer_stream_get_by_id(stream->stream_handle); |
| 314 | if (!vstream) { |
| 315 | vstream = viewer_stream_create(stream, seek_t); |
| 316 | if (!vstream) { |
| 317 | ret = -1; |
| 318 | ctf_trace_put(ctf_trace); |
| 319 | stream_put(stream); |
| 320 | goto error_unlock; |
| 321 | } |
| 322 | |
| 323 | if (nb_created) { |
| 324 | /* Update number of created stream counter. */ |
| 325 | (*nb_created)++; |
| 326 | } |
| 327 | /* |
| 328 | * Ensure a self-reference is preserved even |
| 329 | * after we have put our local reference. |
| 330 | */ |
| 331 | viewer_stream_get(vstream); |
| 332 | } else { |
| 333 | if (!vstream->sent_flag && nb_unsent) { |
| 334 | /* Update number of unsent stream counter. */ |
| 335 | (*nb_unsent)++; |
| 336 | } |
| 337 | } |
| 338 | /* Update number of total stream counter. */ |
| 339 | if (nb_total) { |
| 340 | if (stream->is_metadata) { |
| 341 | if (!stream->closed || |
| 342 | stream->metadata_received > vstream->metadata_sent) { |
| 343 | (*nb_total)++; |
| 344 | } |
| 345 | } else { |
| 346 | if (!stream->closed || |
| 347 | !(((int64_t) (stream->prev_seq - stream->last_net_seq_num)) >= 0)) { |
| 348 | |
| 349 | (*nb_total)++; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | /* Put local reference. */ |
| 354 | viewer_stream_put(vstream); |
| 355 | next: |
| 356 | stream_put(stream); |
| 357 | } |
| 358 | ctf_trace_put(ctf_trace); |
| 359 | } |
| 360 | |
| 361 | ret = 0; |
| 362 | |
| 363 | error_unlock: |
| 364 | rcu_read_unlock(); |
| 365 | pthread_mutex_unlock(&session->lock); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | int relayd_live_stop(void) |
| 370 | { |
| 371 | /* Stop dispatch thread */ |
| 372 | CMM_STORE_SHARED(live_dispatch_thread_exit, 1); |
| 373 | futex_nto1_wake(&viewer_conn_queue.futex); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. |
| 379 | */ |
| 380 | static |
| 381 | int create_thread_poll_set(struct lttng_poll_event *events, int size) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | if (events == NULL || size == 0) { |
| 386 | ret = -1; |
| 387 | goto error; |
| 388 | } |
| 389 | |
| 390 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); |
| 391 | if (ret < 0) { |
| 392 | goto error; |
| 393 | } |
| 394 | |
| 395 | /* Add quit pipe */ |
| 396 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR); |
| 397 | if (ret < 0) { |
| 398 | goto error; |
| 399 | } |
| 400 | |
| 401 | return 0; |
| 402 | |
| 403 | error: |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Check if the thread quit pipe was triggered. |
| 409 | * |
| 410 | * Return 1 if it was triggered else 0; |
| 411 | */ |
| 412 | static |
| 413 | int check_thread_quit_pipe(int fd, uint32_t events) |
| 414 | { |
| 415 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Create and init socket from uri. |
| 424 | */ |
| 425 | static |
| 426 | struct lttcomm_sock *init_socket(struct lttng_uri *uri) |
| 427 | { |
| 428 | int ret; |
| 429 | struct lttcomm_sock *sock = NULL; |
| 430 | |
| 431 | sock = lttcomm_alloc_sock_from_uri(uri); |
| 432 | if (sock == NULL) { |
| 433 | ERR("Allocating socket"); |
| 434 | goto error; |
| 435 | } |
| 436 | |
| 437 | ret = lttcomm_create_sock(sock); |
| 438 | if (ret < 0) { |
| 439 | goto error; |
| 440 | } |
| 441 | DBG("Listening on sock %d for live", sock->fd); |
| 442 | |
| 443 | ret = sock->ops->bind(sock); |
| 444 | if (ret < 0) { |
| 445 | goto error; |
| 446 | } |
| 447 | |
| 448 | ret = sock->ops->listen(sock, -1); |
| 449 | if (ret < 0) { |
| 450 | goto error; |
| 451 | |
| 452 | } |
| 453 | |
| 454 | return sock; |
| 455 | |
| 456 | error: |
| 457 | if (sock) { |
| 458 | lttcomm_destroy_sock(sock); |
| 459 | } |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * This thread manages the listening for new connections on the network |
| 465 | */ |
| 466 | static |
| 467 | void *thread_listener(void *data) |
| 468 | { |
| 469 | int i, ret, pollfd, err = -1; |
| 470 | uint32_t revents, nb_fd; |
| 471 | struct lttng_poll_event events; |
| 472 | struct lttcomm_sock *live_control_sock; |
| 473 | |
| 474 | DBG("[thread] Relay live listener started"); |
| 475 | |
| 476 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER); |
| 477 | |
| 478 | health_code_update(); |
| 479 | |
| 480 | live_control_sock = init_socket(live_uri); |
| 481 | if (!live_control_sock) { |
| 482 | goto error_sock_control; |
| 483 | } |
| 484 | |
| 485 | /* Pass 2 as size here for the thread quit pipe and control sockets. */ |
| 486 | ret = create_thread_poll_set(&events, 2); |
| 487 | if (ret < 0) { |
| 488 | goto error_create_poll; |
| 489 | } |
| 490 | |
| 491 | /* Add the control socket */ |
| 492 | ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP); |
| 493 | if (ret < 0) { |
| 494 | goto error_poll_add; |
| 495 | } |
| 496 | |
| 497 | lttng_relay_notify_ready(); |
| 498 | |
| 499 | if (testpoint(relayd_thread_live_listener)) { |
| 500 | goto error_testpoint; |
| 501 | } |
| 502 | |
| 503 | while (1) { |
| 504 | health_code_update(); |
| 505 | |
| 506 | DBG("Listener accepting live viewers connections"); |
| 507 | |
| 508 | restart: |
| 509 | health_poll_entry(); |
| 510 | ret = lttng_poll_wait(&events, -1); |
| 511 | health_poll_exit(); |
| 512 | if (ret < 0) { |
| 513 | /* |
| 514 | * Restart interrupted system call. |
| 515 | */ |
| 516 | if (errno == EINTR) { |
| 517 | goto restart; |
| 518 | } |
| 519 | goto error; |
| 520 | } |
| 521 | nb_fd = ret; |
| 522 | |
| 523 | DBG("Relay new viewer connection received"); |
| 524 | for (i = 0; i < nb_fd; i++) { |
| 525 | health_code_update(); |
| 526 | |
| 527 | /* Fetch once the poll data */ |
| 528 | revents = LTTNG_POLL_GETEV(&events, i); |
| 529 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 530 | |
| 531 | if (!revents) { |
| 532 | /* No activity for this FD (poll implementation). */ |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | /* Thread quit pipe has been closed. Killing thread. */ |
| 537 | ret = check_thread_quit_pipe(pollfd, revents); |
| 538 | if (ret) { |
| 539 | err = 0; |
| 540 | goto exit; |
| 541 | } |
| 542 | |
| 543 | if (revents & LPOLLIN) { |
| 544 | /* |
| 545 | * A new connection is requested, therefore a |
| 546 | * viewer connection is allocated in this |
| 547 | * thread, enqueued to a global queue and |
| 548 | * dequeued (and freed) in the worker thread. |
| 549 | */ |
| 550 | int val = 1; |
| 551 | struct relay_connection *new_conn; |
| 552 | struct lttcomm_sock *newsock; |
| 553 | |
| 554 | newsock = live_control_sock->ops->accept(live_control_sock); |
| 555 | if (!newsock) { |
| 556 | PERROR("accepting control sock"); |
| 557 | goto error; |
| 558 | } |
| 559 | DBG("Relay viewer connection accepted socket %d", newsock->fd); |
| 560 | |
| 561 | ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, |
| 562 | sizeof(val)); |
| 563 | if (ret < 0) { |
| 564 | PERROR("setsockopt inet"); |
| 565 | lttcomm_destroy_sock(newsock); |
| 566 | goto error; |
| 567 | } |
| 568 | new_conn = connection_create(newsock, RELAY_CONNECTION_UNKNOWN); |
| 569 | if (!new_conn) { |
| 570 | lttcomm_destroy_sock(newsock); |
| 571 | goto error; |
| 572 | } |
| 573 | /* Ownership assumed by the connection. */ |
| 574 | newsock = NULL; |
| 575 | |
| 576 | /* Enqueue request for the dispatcher thread. */ |
| 577 | cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail, |
| 578 | &new_conn->qnode); |
| 579 | |
| 580 | /* |
| 581 | * Wake the dispatch queue futex. |
| 582 | * Implicit memory barrier with the |
| 583 | * exchange in cds_wfcq_enqueue. |
| 584 | */ |
| 585 | futex_nto1_wake(&viewer_conn_queue.futex); |
| 586 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 587 | ERR("socket poll error"); |
| 588 | goto error; |
| 589 | } else { |
| 590 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 591 | goto error; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | exit: |
| 597 | error: |
| 598 | error_poll_add: |
| 599 | error_testpoint: |
| 600 | lttng_poll_clean(&events); |
| 601 | error_create_poll: |
| 602 | if (live_control_sock->fd >= 0) { |
| 603 | ret = live_control_sock->ops->close(live_control_sock); |
| 604 | if (ret) { |
| 605 | PERROR("close"); |
| 606 | } |
| 607 | } |
| 608 | lttcomm_destroy_sock(live_control_sock); |
| 609 | error_sock_control: |
| 610 | if (err) { |
| 611 | health_error(); |
| 612 | DBG("Live viewer listener thread exited with error"); |
| 613 | } |
| 614 | health_unregister(health_relayd); |
| 615 | DBG("Live viewer listener thread cleanup complete"); |
| 616 | if (lttng_relay_stop_threads()) { |
| 617 | ERR("Error stopping threads"); |
| 618 | } |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | /* |
| 623 | * This thread manages the dispatching of the requests to worker threads |
| 624 | */ |
| 625 | static |
| 626 | void *thread_dispatcher(void *data) |
| 627 | { |
| 628 | int err = -1; |
| 629 | ssize_t ret; |
| 630 | struct cds_wfcq_node *node; |
| 631 | struct relay_connection *conn = NULL; |
| 632 | |
| 633 | DBG("[thread] Live viewer relay dispatcher started"); |
| 634 | |
| 635 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER); |
| 636 | |
| 637 | if (testpoint(relayd_thread_live_dispatcher)) { |
| 638 | goto error_testpoint; |
| 639 | } |
| 640 | |
| 641 | health_code_update(); |
| 642 | |
| 643 | while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) { |
| 644 | health_code_update(); |
| 645 | |
| 646 | /* Atomically prepare the queue futex */ |
| 647 | futex_nto1_prepare(&viewer_conn_queue.futex); |
| 648 | |
| 649 | do { |
| 650 | health_code_update(); |
| 651 | |
| 652 | /* Dequeue commands */ |
| 653 | node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head, |
| 654 | &viewer_conn_queue.tail); |
| 655 | if (node == NULL) { |
| 656 | DBG("Woken up but nothing in the live-viewer " |
| 657 | "relay command queue"); |
| 658 | /* Continue thread execution */ |
| 659 | break; |
| 660 | } |
| 661 | conn = caa_container_of(node, struct relay_connection, qnode); |
| 662 | DBG("Dispatching viewer request waiting on sock %d", |
| 663 | conn->sock->fd); |
| 664 | |
| 665 | /* |
| 666 | * Inform worker thread of the new request. This |
| 667 | * call is blocking so we can be assured that |
| 668 | * the data will be read at some point in time |
| 669 | * or wait to the end of the world :) |
| 670 | */ |
| 671 | ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn)); |
| 672 | if (ret < 0) { |
| 673 | PERROR("write conn pipe"); |
| 674 | connection_put(conn); |
| 675 | goto error; |
| 676 | } |
| 677 | } while (node != NULL); |
| 678 | |
| 679 | /* Futex wait on queue. Blocking call on futex() */ |
| 680 | health_poll_entry(); |
| 681 | futex_nto1_wait(&viewer_conn_queue.futex); |
| 682 | health_poll_exit(); |
| 683 | } |
| 684 | |
| 685 | /* Normal exit, no error */ |
| 686 | err = 0; |
| 687 | |
| 688 | error: |
| 689 | error_testpoint: |
| 690 | if (err) { |
| 691 | health_error(); |
| 692 | ERR("Health error occurred in %s", __func__); |
| 693 | } |
| 694 | health_unregister(health_relayd); |
| 695 | DBG("Live viewer dispatch thread dying"); |
| 696 | if (lttng_relay_stop_threads()) { |
| 697 | ERR("Error stopping threads"); |
| 698 | } |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Establish connection with the viewer and check the versions. |
| 704 | * |
| 705 | * Return 0 on success or else negative value. |
| 706 | */ |
| 707 | static |
| 708 | int viewer_connect(struct relay_connection *conn) |
| 709 | { |
| 710 | int ret; |
| 711 | struct lttng_viewer_connect reply, msg; |
| 712 | |
| 713 | conn->version_check_done = 1; |
| 714 | |
| 715 | health_code_update(); |
| 716 | |
| 717 | DBG("Viewer is establishing a connection to the relayd."); |
| 718 | |
| 719 | ret = recv_request(conn->sock, &msg, sizeof(msg)); |
| 720 | if (ret < 0) { |
| 721 | goto end; |
| 722 | } |
| 723 | |
| 724 | health_code_update(); |
| 725 | |
| 726 | memset(&reply, 0, sizeof(reply)); |
| 727 | reply.major = RELAYD_VERSION_COMM_MAJOR; |
| 728 | reply.minor = RELAYD_VERSION_COMM_MINOR; |
| 729 | |
| 730 | /* Major versions must be the same */ |
| 731 | if (reply.major != be32toh(msg.major)) { |
| 732 | DBG("Incompatible major versions ([relayd] %u vs [client] %u)", |
| 733 | reply.major, be32toh(msg.major)); |
| 734 | ret = -1; |
| 735 | goto end; |
| 736 | } |
| 737 | |
| 738 | conn->major = reply.major; |
| 739 | /* We adapt to the lowest compatible version */ |
| 740 | if (reply.minor <= be32toh(msg.minor)) { |
| 741 | conn->minor = reply.minor; |
| 742 | } else { |
| 743 | conn->minor = be32toh(msg.minor); |
| 744 | } |
| 745 | |
| 746 | if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) { |
| 747 | conn->type = RELAY_VIEWER_COMMAND; |
| 748 | } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) { |
| 749 | conn->type = RELAY_VIEWER_NOTIFICATION; |
| 750 | } else { |
| 751 | ERR("Unknown connection type : %u", be32toh(msg.type)); |
| 752 | ret = -1; |
| 753 | goto end; |
| 754 | } |
| 755 | |
| 756 | reply.major = htobe32(reply.major); |
| 757 | reply.minor = htobe32(reply.minor); |
| 758 | if (conn->type == RELAY_VIEWER_COMMAND) { |
| 759 | /* |
| 760 | * Increment outside of htobe64 macro, because the argument can |
| 761 | * be used more than once within the macro, and thus the |
| 762 | * operation may be undefined. |
| 763 | */ |
| 764 | pthread_mutex_lock(&last_relay_viewer_session_id_lock); |
| 765 | last_relay_viewer_session_id++; |
| 766 | pthread_mutex_unlock(&last_relay_viewer_session_id_lock); |
| 767 | reply.viewer_session_id = htobe64(last_relay_viewer_session_id); |
| 768 | } |
| 769 | |
| 770 | health_code_update(); |
| 771 | |
| 772 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
| 773 | if (ret < 0) { |
| 774 | goto end; |
| 775 | } |
| 776 | |
| 777 | health_code_update(); |
| 778 | |
| 779 | DBG("Version check done using protocol %u.%u", conn->major, conn->minor); |
| 780 | ret = 0; |
| 781 | |
| 782 | end: |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Send the viewer the list of current sessions. |
| 788 | * We need to create a copy of the hash table content because otherwise |
| 789 | * we cannot assume the number of entries stays the same between getting |
| 790 | * the number of HT elements and iteration over the HT. |
| 791 | * |
| 792 | * Return 0 on success or else a negative value. |
| 793 | */ |
| 794 | static |
| 795 | int viewer_list_sessions(struct relay_connection *conn) |
| 796 | { |
| 797 | int ret; |
| 798 | struct lttng_viewer_list_sessions session_list; |
| 799 | struct lttng_ht_iter iter; |
| 800 | struct relay_session *session; |
| 801 | struct lttng_viewer_session *send_session_buf = NULL; |
| 802 | uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT; |
| 803 | uint32_t count = 0; |
| 804 | |
| 805 | DBG("List sessions received"); |
| 806 | |
| 807 | send_session_buf = zmalloc(SESSION_BUF_DEFAULT_COUNT * sizeof(*send_session_buf)); |
| 808 | if (!send_session_buf) { |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | rcu_read_lock(); |
| 813 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
| 814 | session_n.node) { |
| 815 | struct lttng_viewer_session *send_session; |
| 816 | |
| 817 | health_code_update(); |
| 818 | |
| 819 | if (count >= buf_count) { |
| 820 | struct lttng_viewer_session *newbuf; |
| 821 | uint32_t new_buf_count = buf_count << 1; |
| 822 | |
| 823 | newbuf = realloc(send_session_buf, |
| 824 | new_buf_count * sizeof(*send_session_buf)); |
| 825 | if (!newbuf) { |
| 826 | ret = -1; |
| 827 | rcu_read_unlock(); |
| 828 | goto end_free; |
| 829 | } |
| 830 | send_session_buf = newbuf; |
| 831 | buf_count = new_buf_count; |
| 832 | } |
| 833 | send_session = &send_session_buf[count]; |
| 834 | strncpy(send_session->session_name, session->session_name, |
| 835 | sizeof(send_session->session_name)); |
| 836 | strncpy(send_session->hostname, session->hostname, |
| 837 | sizeof(send_session->hostname)); |
| 838 | send_session->id = htobe64(session->id); |
| 839 | send_session->live_timer = htobe32(session->live_timer); |
| 840 | if (session->viewer_attached) { |
| 841 | send_session->clients = htobe32(1); |
| 842 | } else { |
| 843 | send_session->clients = htobe32(0); |
| 844 | } |
| 845 | send_session->streams = htobe32(session->stream_count); |
| 846 | count++; |
| 847 | } |
| 848 | rcu_read_unlock(); |
| 849 | |
| 850 | session_list.sessions_count = htobe32(count); |
| 851 | |
| 852 | health_code_update(); |
| 853 | |
| 854 | ret = send_response(conn->sock, &session_list, sizeof(session_list)); |
| 855 | if (ret < 0) { |
| 856 | goto end_free; |
| 857 | } |
| 858 | |
| 859 | health_code_update(); |
| 860 | |
| 861 | ret = send_response(conn->sock, send_session_buf, |
| 862 | count * sizeof(*send_session_buf)); |
| 863 | if (ret < 0) { |
| 864 | goto end_free; |
| 865 | } |
| 866 | health_code_update(); |
| 867 | |
| 868 | ret = 0; |
| 869 | end_free: |
| 870 | free(send_session_buf); |
| 871 | return ret; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Send the viewer the list of current streams. |
| 876 | */ |
| 877 | static |
| 878 | int viewer_get_new_streams(struct relay_connection *conn) |
| 879 | { |
| 880 | int ret, send_streams = 0; |
| 881 | uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0; |
| 882 | struct lttng_viewer_new_streams_request request; |
| 883 | struct lttng_viewer_new_streams_response response; |
| 884 | struct relay_session *session; |
| 885 | uint64_t session_id; |
| 886 | bool closed = false; |
| 887 | |
| 888 | assert(conn); |
| 889 | |
| 890 | DBG("Get new streams received"); |
| 891 | |
| 892 | health_code_update(); |
| 893 | |
| 894 | /* Receive the request from the connected client. */ |
| 895 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 896 | if (ret < 0) { |
| 897 | goto error; |
| 898 | } |
| 899 | session_id = be64toh(request.session_id); |
| 900 | |
| 901 | health_code_update(); |
| 902 | |
| 903 | memset(&response, 0, sizeof(response)); |
| 904 | |
| 905 | session = session_get_by_id(session_id); |
| 906 | if (!session) { |
| 907 | DBG("Relay session %" PRIu64 " not found", session_id); |
| 908 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
| 909 | goto send_reply; |
| 910 | } |
| 911 | |
| 912 | if (!viewer_session_is_attached(conn->viewer_session, session)) { |
| 913 | send_streams = 0; |
| 914 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR); |
| 915 | goto send_reply; |
| 916 | } |
| 917 | |
| 918 | send_streams = 1; |
| 919 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK); |
| 920 | |
| 921 | ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, &nb_total, &nb_unsent, |
| 922 | &nb_created, &closed); |
| 923 | if (ret < 0) { |
| 924 | goto end_put_session; |
| 925 | } |
| 926 | /* Only send back the newly created streams with the unsent ones. */ |
| 927 | nb_streams = nb_created + nb_unsent; |
| 928 | response.streams_count = htobe32(nb_streams); |
| 929 | |
| 930 | /* |
| 931 | * If the session is closed, HUP when there are no more streams |
| 932 | * with data. |
| 933 | */ |
| 934 | if (closed && nb_total == 0) { |
| 935 | send_streams = 0; |
| 936 | response.streams_count = 0; |
| 937 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP); |
| 938 | goto send_reply; |
| 939 | } |
| 940 | |
| 941 | send_reply: |
| 942 | health_code_update(); |
| 943 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 944 | if (ret < 0) { |
| 945 | goto end_put_session; |
| 946 | } |
| 947 | health_code_update(); |
| 948 | |
| 949 | /* |
| 950 | * Unknown or empty session, just return gracefully, the viewer |
| 951 | * knows what is happening. |
| 952 | */ |
| 953 | if (!send_streams || !nb_streams) { |
| 954 | ret = 0; |
| 955 | goto end_put_session; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Send stream and *DON'T* ignore the sent flag so every viewer |
| 960 | * streams that were not sent from that point will be sent to |
| 961 | * the viewer. |
| 962 | */ |
| 963 | ret = send_viewer_streams(conn->sock, session, 0); |
| 964 | if (ret < 0) { |
| 965 | goto end_put_session; |
| 966 | } |
| 967 | |
| 968 | end_put_session: |
| 969 | if (session) { |
| 970 | session_put(session); |
| 971 | } |
| 972 | error: |
| 973 | return ret; |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Send the viewer the list of current sessions. |
| 978 | */ |
| 979 | static |
| 980 | int viewer_attach_session(struct relay_connection *conn) |
| 981 | { |
| 982 | int send_streams = 0; |
| 983 | ssize_t ret; |
| 984 | uint32_t nb_streams = 0; |
| 985 | enum lttng_viewer_seek seek_type; |
| 986 | struct lttng_viewer_attach_session_request request; |
| 987 | struct lttng_viewer_attach_session_response response; |
| 988 | struct relay_session *session = NULL; |
| 989 | bool closed = false; |
| 990 | |
| 991 | assert(conn); |
| 992 | |
| 993 | health_code_update(); |
| 994 | |
| 995 | /* Receive the request from the connected client. */ |
| 996 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 997 | if (ret < 0) { |
| 998 | goto error; |
| 999 | } |
| 1000 | |
| 1001 | health_code_update(); |
| 1002 | |
| 1003 | memset(&response, 0, sizeof(response)); |
| 1004 | |
| 1005 | if (!conn->viewer_session) { |
| 1006 | DBG("Client trying to attach before creating a live viewer session"); |
| 1007 | response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION); |
| 1008 | goto send_reply; |
| 1009 | } |
| 1010 | |
| 1011 | session = session_get_by_id(be64toh(request.session_id)); |
| 1012 | if (!session) { |
| 1013 | DBG("Relay session %" PRIu64 " not found", |
| 1014 | be64toh(request.session_id)); |
| 1015 | response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK); |
| 1016 | goto send_reply; |
| 1017 | } |
| 1018 | DBG("Attach session ID %" PRIu64 " received", |
| 1019 | be64toh(request.session_id)); |
| 1020 | |
| 1021 | if (session->live_timer == 0) { |
| 1022 | DBG("Not live session"); |
| 1023 | response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE); |
| 1024 | goto send_reply; |
| 1025 | } |
| 1026 | |
| 1027 | send_streams = 1; |
| 1028 | ret = viewer_session_attach(conn->viewer_session, session); |
| 1029 | if (ret) { |
| 1030 | DBG("Already a viewer attached"); |
| 1031 | response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY); |
| 1032 | goto send_reply; |
| 1033 | } |
| 1034 | |
| 1035 | switch (be32toh(request.seek)) { |
| 1036 | case LTTNG_VIEWER_SEEK_BEGINNING: |
| 1037 | case LTTNG_VIEWER_SEEK_LAST: |
| 1038 | response.status = htobe32(LTTNG_VIEWER_ATTACH_OK); |
| 1039 | seek_type = be32toh(request.seek); |
| 1040 | break; |
| 1041 | default: |
| 1042 | ERR("Wrong seek parameter"); |
| 1043 | response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR); |
| 1044 | send_streams = 0; |
| 1045 | goto send_reply; |
| 1046 | } |
| 1047 | |
| 1048 | ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, |
| 1049 | NULL, &closed); |
| 1050 | if (ret < 0) { |
| 1051 | goto end_put_session; |
| 1052 | } |
| 1053 | response.streams_count = htobe32(nb_streams); |
| 1054 | |
| 1055 | /* |
| 1056 | * If the session is closed when the viewer is attaching, it |
| 1057 | * means some of the streams may have been concurrently removed, |
| 1058 | * so we don't allow the viewer to attach, even if there are |
| 1059 | * streams available. |
| 1060 | */ |
| 1061 | if (closed) { |
| 1062 | send_streams = 0; |
| 1063 | response.streams_count = 0; |
| 1064 | response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP); |
| 1065 | goto send_reply; |
| 1066 | } |
| 1067 | |
| 1068 | send_reply: |
| 1069 | health_code_update(); |
| 1070 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 1071 | if (ret < 0) { |
| 1072 | goto end_put_session; |
| 1073 | } |
| 1074 | health_code_update(); |
| 1075 | |
| 1076 | /* |
| 1077 | * Unknown or empty session, just return gracefully, the viewer |
| 1078 | * knows what is happening. |
| 1079 | */ |
| 1080 | if (!send_streams || !nb_streams) { |
| 1081 | ret = 0; |
| 1082 | goto end_put_session; |
| 1083 | } |
| 1084 | |
| 1085 | /* Send stream and ignore the sent flag. */ |
| 1086 | ret = send_viewer_streams(conn->sock, session, 1); |
| 1087 | if (ret < 0) { |
| 1088 | goto end_put_session; |
| 1089 | } |
| 1090 | |
| 1091 | end_put_session: |
| 1092 | if (session) { |
| 1093 | session_put(session); |
| 1094 | } |
| 1095 | error: |
| 1096 | return ret; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Open the index file if needed for the given vstream. |
| 1101 | * |
| 1102 | * If an index file is successfully opened, the vstream index_fd set with |
| 1103 | * it. |
| 1104 | * |
| 1105 | * Return 0 on success, a negative value on error (-ENOENT if not ready yet). |
| 1106 | * |
| 1107 | * Called with rstream lock held. |
| 1108 | */ |
| 1109 | static int try_open_index(struct relay_viewer_stream *vstream, |
| 1110 | struct relay_stream *rstream) |
| 1111 | { |
| 1112 | int ret = 0; |
| 1113 | |
| 1114 | if (vstream->index_fd) { |
| 1115 | goto end; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * First time, we open the index file and at least one index is ready. |
| 1120 | */ |
| 1121 | if (rstream->index_received_seqcount == 0) { |
| 1122 | ret = -ENOENT; |
| 1123 | goto end; |
| 1124 | } |
| 1125 | ret = index_open(vstream->path_name, vstream->channel_name, |
| 1126 | vstream->stream->tracefile_count, |
| 1127 | vstream->current_tracefile_id); |
| 1128 | if (ret >= 0) { |
| 1129 | vstream->index_fd = stream_fd_create(ret); |
| 1130 | if (!vstream->index_fd) { |
| 1131 | if (close(ret)) { |
| 1132 | PERROR("close"); |
| 1133 | } |
| 1134 | ret = -1; |
| 1135 | } else { |
| 1136 | ret = 0; |
| 1137 | } |
| 1138 | goto end; |
| 1139 | } |
| 1140 | |
| 1141 | end: |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Check the status of the index for the given stream. This function |
| 1147 | * updates the index structure if needed and can put (close) the vstream |
| 1148 | * in the HUP situation. |
| 1149 | * |
| 1150 | * Return 0 means that we can proceed with the index. A value of 1 means |
| 1151 | * that the index has been updated and is ready to be sent to the |
| 1152 | * client. A negative value indicates an error that can't be handled. |
| 1153 | * |
| 1154 | * Called with rstream lock held. |
| 1155 | */ |
| 1156 | static int check_index_status(struct relay_viewer_stream *vstream, |
| 1157 | struct relay_stream *rstream, struct ctf_trace *trace, |
| 1158 | struct lttng_viewer_index *index) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | |
| 1162 | if (trace->session->connection_closed |
| 1163 | && rstream->index_received_seqcount |
| 1164 | == vstream->index_sent_seqcount) { |
| 1165 | /* Last index sent and session connection is closed. */ |
| 1166 | index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); |
| 1167 | goto hup; |
| 1168 | } else if (rstream->beacon_ts_end != -1ULL && |
| 1169 | rstream->index_received_seqcount |
| 1170 | == vstream->index_sent_seqcount) { |
| 1171 | /* |
| 1172 | * We've received a synchronization beacon and the last index |
| 1173 | * available has been sent, the index for now is inactive. |
| 1174 | * |
| 1175 | * In this case, we have received a beacon which allows us to |
| 1176 | * inform the client of a time interval during which we can |
| 1177 | * guarantee that there are no events to read (and never will |
| 1178 | * be). |
| 1179 | */ |
| 1180 | index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE); |
| 1181 | index->timestamp_end = htobe64(rstream->beacon_ts_end); |
| 1182 | index->stream_id = htobe64(rstream->ctf_stream_id); |
| 1183 | goto index_ready; |
| 1184 | } else if (rstream->index_received_seqcount |
| 1185 | == vstream->index_sent_seqcount) { |
| 1186 | /* |
| 1187 | * This checks whether received == sent seqcount. In |
| 1188 | * this case, we have not received a beacon. Therefore, |
| 1189 | * we can only ask the client to retry later. |
| 1190 | */ |
| 1191 | index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); |
| 1192 | goto index_ready; |
| 1193 | } else if (!tracefile_array_seq_in_file(rstream->tfa, |
| 1194 | vstream->current_tracefile_id, |
| 1195 | vstream->index_sent_seqcount)) { |
| 1196 | /* |
| 1197 | * The next index we want to send cannot be read either |
| 1198 | * because we need to perform a rotation, or due to |
| 1199 | * the producer having overwritten its trace file. |
| 1200 | */ |
| 1201 | DBG("Viewer stream %" PRIu64 " rotation", |
| 1202 | vstream->stream->stream_handle); |
| 1203 | ret = viewer_stream_rotate(vstream); |
| 1204 | if (ret < 0) { |
| 1205 | goto end; |
| 1206 | } else if (ret == 1) { |
| 1207 | /* EOF across entire stream. */ |
| 1208 | index->status = htobe32(LTTNG_VIEWER_INDEX_HUP); |
| 1209 | goto hup; |
| 1210 | } |
| 1211 | /* |
| 1212 | * If we have been pushed due to overwrite, it |
| 1213 | * necessarily means there is data that can be read in |
| 1214 | * the stream. If we rotated because we reached the end |
| 1215 | * of a tracefile, it means the following tracefile |
| 1216 | * needs to contain at least one index, else we would |
| 1217 | * have already returned LTTNG_VIEWER_INDEX_RETRY to the |
| 1218 | * viewer. The updated index_sent_seqcount needs to |
| 1219 | * point to a readable index entry now. |
| 1220 | * |
| 1221 | * In the case where we "rotate" on a single file, we |
| 1222 | * can end up in a case where the requested index is |
| 1223 | * still unavailable. |
| 1224 | */ |
| 1225 | if (rstream->tracefile_count == 1 && |
| 1226 | !tracefile_array_seq_in_file( |
| 1227 | rstream->tfa, |
| 1228 | vstream->current_tracefile_id, |
| 1229 | vstream->index_sent_seqcount)) { |
| 1230 | index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY); |
| 1231 | goto index_ready; |
| 1232 | } |
| 1233 | assert(tracefile_array_seq_in_file(rstream->tfa, |
| 1234 | vstream->current_tracefile_id, |
| 1235 | vstream->index_sent_seqcount)); |
| 1236 | } |
| 1237 | /* ret == 0 means successful so we continue. */ |
| 1238 | ret = 0; |
| 1239 | end: |
| 1240 | return ret; |
| 1241 | |
| 1242 | hup: |
| 1243 | viewer_stream_put(vstream); |
| 1244 | index_ready: |
| 1245 | return 1; |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Send the next index for a stream. |
| 1250 | * |
| 1251 | * Return 0 on success or else a negative value. |
| 1252 | */ |
| 1253 | static |
| 1254 | int viewer_get_next_index(struct relay_connection *conn) |
| 1255 | { |
| 1256 | int ret; |
| 1257 | ssize_t read_ret; |
| 1258 | struct lttng_viewer_get_next_index request_index; |
| 1259 | struct lttng_viewer_index viewer_index; |
| 1260 | struct ctf_packet_index packet_index; |
| 1261 | struct relay_viewer_stream *vstream = NULL; |
| 1262 | struct relay_stream *rstream = NULL; |
| 1263 | struct ctf_trace *ctf_trace = NULL; |
| 1264 | struct relay_viewer_stream *metadata_viewer_stream = NULL; |
| 1265 | |
| 1266 | assert(conn); |
| 1267 | |
| 1268 | DBG("Viewer get next index"); |
| 1269 | |
| 1270 | memset(&viewer_index, 0, sizeof(viewer_index)); |
| 1271 | health_code_update(); |
| 1272 | |
| 1273 | ret = recv_request(conn->sock, &request_index, sizeof(request_index)); |
| 1274 | if (ret < 0) { |
| 1275 | goto end; |
| 1276 | } |
| 1277 | health_code_update(); |
| 1278 | |
| 1279 | vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id)); |
| 1280 | if (!vstream) { |
| 1281 | DBG("Client requested index of unknown stream id %" PRIu64, |
| 1282 | be64toh(request_index.stream_id)); |
| 1283 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
| 1284 | goto send_reply; |
| 1285 | } |
| 1286 | |
| 1287 | /* Use back. ref. Protected by refcounts. */ |
| 1288 | rstream = vstream->stream; |
| 1289 | ctf_trace = rstream->trace; |
| 1290 | |
| 1291 | /* metadata_viewer_stream may be NULL. */ |
| 1292 | metadata_viewer_stream = |
| 1293 | ctf_trace_get_viewer_metadata_stream(ctf_trace); |
| 1294 | |
| 1295 | pthread_mutex_lock(&rstream->lock); |
| 1296 | |
| 1297 | /* |
| 1298 | * The viewer should not ask for index on metadata stream. |
| 1299 | */ |
| 1300 | if (rstream->is_metadata) { |
| 1301 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP); |
| 1302 | goto send_reply; |
| 1303 | } |
| 1304 | |
| 1305 | /* Try to open an index if one is needed for that stream. */ |
| 1306 | ret = try_open_index(vstream, rstream); |
| 1307 | if (ret < 0) { |
| 1308 | if (ret == -ENOENT) { |
| 1309 | /* |
| 1310 | * The index is created only when the first data |
| 1311 | * packet arrives, it might not be ready at the |
| 1312 | * beginning of the session |
| 1313 | */ |
| 1314 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY); |
| 1315 | } else { |
| 1316 | /* Unhandled error. */ |
| 1317 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
| 1318 | } |
| 1319 | goto send_reply; |
| 1320 | } |
| 1321 | |
| 1322 | ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index); |
| 1323 | if (ret < 0) { |
| 1324 | goto error_put; |
| 1325 | } else if (ret == 1) { |
| 1326 | /* |
| 1327 | * We have no index to send and check_index_status has populated |
| 1328 | * viewer_index's status. |
| 1329 | */ |
| 1330 | goto send_reply; |
| 1331 | } |
| 1332 | /* At this point, ret is 0 thus we will be able to read the index. */ |
| 1333 | assert(!ret); |
| 1334 | |
| 1335 | /* |
| 1336 | * vstream->stream_fd may be NULL if it has been closed by |
| 1337 | * tracefile rotation, or if we are at the beginning of the |
| 1338 | * stream. We open the data stream file here to protect against |
| 1339 | * overwrite caused by tracefile rotation (in association with |
| 1340 | * unlink performed before overwrite). |
| 1341 | */ |
| 1342 | if (!vstream->stream_fd) { |
| 1343 | char fullpath[PATH_MAX]; |
| 1344 | |
| 1345 | if (vstream->stream->tracefile_count > 0) { |
| 1346 | ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, |
| 1347 | vstream->path_name, |
| 1348 | vstream->channel_name, |
| 1349 | vstream->current_tracefile_id); |
| 1350 | } else { |
| 1351 | ret = snprintf(fullpath, PATH_MAX, "%s/%s", |
| 1352 | vstream->path_name, |
| 1353 | vstream->channel_name); |
| 1354 | } |
| 1355 | if (ret < 0) { |
| 1356 | goto error_put; |
| 1357 | } |
| 1358 | ret = open(fullpath, O_RDONLY); |
| 1359 | if (ret < 0) { |
| 1360 | PERROR("Relay opening trace file"); |
| 1361 | goto error_put; |
| 1362 | } |
| 1363 | vstream->stream_fd = stream_fd_create(ret); |
| 1364 | if (!vstream->stream_fd) { |
| 1365 | if (close(ret)) { |
| 1366 | PERROR("close"); |
| 1367 | } |
| 1368 | goto error_put; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | ret = check_new_streams(conn); |
| 1373 | if (ret < 0) { |
| 1374 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
| 1375 | goto send_reply; |
| 1376 | } else if (ret == 1) { |
| 1377 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM; |
| 1378 | } |
| 1379 | |
| 1380 | read_ret = lttng_read(vstream->index_fd->fd, &packet_index, |
| 1381 | sizeof(packet_index)); |
| 1382 | if (read_ret < sizeof(packet_index)) { |
| 1383 | ERR("Relay reading index file %d returned %zd", |
| 1384 | vstream->index_fd->fd, read_ret); |
| 1385 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR); |
| 1386 | goto send_reply; |
| 1387 | } else { |
| 1388 | viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK); |
| 1389 | vstream->index_sent_seqcount++; |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * Indexes are stored in big endian, no need to switch before sending. |
| 1394 | */ |
| 1395 | DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64, |
| 1396 | rstream->stream_handle, |
| 1397 | be64toh(packet_index.offset)); |
| 1398 | viewer_index.offset = packet_index.offset; |
| 1399 | viewer_index.packet_size = packet_index.packet_size; |
| 1400 | viewer_index.content_size = packet_index.content_size; |
| 1401 | viewer_index.timestamp_begin = packet_index.timestamp_begin; |
| 1402 | viewer_index.timestamp_end = packet_index.timestamp_end; |
| 1403 | viewer_index.events_discarded = packet_index.events_discarded; |
| 1404 | viewer_index.stream_id = packet_index.stream_id; |
| 1405 | |
| 1406 | send_reply: |
| 1407 | if (rstream) { |
| 1408 | pthread_mutex_unlock(&rstream->lock); |
| 1409 | } |
| 1410 | |
| 1411 | if (metadata_viewer_stream) { |
| 1412 | pthread_mutex_lock(&metadata_viewer_stream->stream->lock); |
| 1413 | DBG("get next index metadata check: recv %" PRIu64 |
| 1414 | " sent %" PRIu64, |
| 1415 | metadata_viewer_stream->stream->metadata_received, |
| 1416 | metadata_viewer_stream->metadata_sent); |
| 1417 | if (!metadata_viewer_stream->stream->metadata_received || |
| 1418 | metadata_viewer_stream->stream->metadata_received > |
| 1419 | metadata_viewer_stream->metadata_sent) { |
| 1420 | viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA; |
| 1421 | } |
| 1422 | pthread_mutex_unlock(&metadata_viewer_stream->stream->lock); |
| 1423 | } |
| 1424 | |
| 1425 | viewer_index.flags = htobe32(viewer_index.flags); |
| 1426 | health_code_update(); |
| 1427 | |
| 1428 | ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index)); |
| 1429 | if (ret < 0) { |
| 1430 | goto end; |
| 1431 | } |
| 1432 | health_code_update(); |
| 1433 | |
| 1434 | if (vstream) { |
| 1435 | DBG("Index %" PRIu64 " for stream %" PRIu64 " sent", |
| 1436 | vstream->index_sent_seqcount, |
| 1437 | vstream->stream->stream_handle); |
| 1438 | } |
| 1439 | end: |
| 1440 | if (metadata_viewer_stream) { |
| 1441 | viewer_stream_put(metadata_viewer_stream); |
| 1442 | } |
| 1443 | if (vstream) { |
| 1444 | viewer_stream_put(vstream); |
| 1445 | } |
| 1446 | return ret; |
| 1447 | |
| 1448 | error_put: |
| 1449 | pthread_mutex_unlock(&rstream->lock); |
| 1450 | if (metadata_viewer_stream) { |
| 1451 | viewer_stream_put(metadata_viewer_stream); |
| 1452 | } |
| 1453 | viewer_stream_put(vstream); |
| 1454 | return ret; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Send the next index for a stream |
| 1459 | * |
| 1460 | * Return 0 on success or else a negative value. |
| 1461 | */ |
| 1462 | static |
| 1463 | int viewer_get_packet(struct relay_connection *conn) |
| 1464 | { |
| 1465 | int ret, send_data = 0; |
| 1466 | char *data = NULL; |
| 1467 | uint32_t len = 0; |
| 1468 | ssize_t read_len; |
| 1469 | struct lttng_viewer_get_packet get_packet_info; |
| 1470 | struct lttng_viewer_trace_packet reply; |
| 1471 | struct relay_viewer_stream *vstream = NULL; |
| 1472 | |
| 1473 | DBG2("Relay get data packet"); |
| 1474 | |
| 1475 | health_code_update(); |
| 1476 | |
| 1477 | ret = recv_request(conn->sock, &get_packet_info, |
| 1478 | sizeof(get_packet_info)); |
| 1479 | if (ret < 0) { |
| 1480 | goto end; |
| 1481 | } |
| 1482 | health_code_update(); |
| 1483 | |
| 1484 | /* From this point on, the error label can be reached. */ |
| 1485 | memset(&reply, 0, sizeof(reply)); |
| 1486 | |
| 1487 | vstream = viewer_stream_get_by_id(be64toh(get_packet_info.stream_id)); |
| 1488 | if (!vstream) { |
| 1489 | DBG("Client requested packet of unknown stream id %" PRIu64, |
| 1490 | be64toh(get_packet_info.stream_id)); |
| 1491 | reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); |
| 1492 | goto send_reply_nolock; |
| 1493 | } |
| 1494 | |
| 1495 | pthread_mutex_lock(&vstream->stream->lock); |
| 1496 | |
| 1497 | len = be32toh(get_packet_info.len); |
| 1498 | data = zmalloc(len); |
| 1499 | if (!data) { |
| 1500 | PERROR("relay data zmalloc"); |
| 1501 | goto error; |
| 1502 | } |
| 1503 | |
| 1504 | ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset), |
| 1505 | SEEK_SET); |
| 1506 | if (ret < 0) { |
| 1507 | PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd, |
| 1508 | be64toh(get_packet_info.offset)); |
| 1509 | goto error; |
| 1510 | } |
| 1511 | read_len = lttng_read(vstream->stream_fd->fd, data, len); |
| 1512 | if (read_len < len) { |
| 1513 | PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64, |
| 1514 | vstream->stream_fd->fd, |
| 1515 | be64toh(get_packet_info.offset)); |
| 1516 | goto error; |
| 1517 | } |
| 1518 | reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK); |
| 1519 | reply.len = htobe32(len); |
| 1520 | send_data = 1; |
| 1521 | goto send_reply; |
| 1522 | |
| 1523 | error: |
| 1524 | reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR); |
| 1525 | |
| 1526 | send_reply: |
| 1527 | if (vstream) { |
| 1528 | pthread_mutex_unlock(&vstream->stream->lock); |
| 1529 | } |
| 1530 | send_reply_nolock: |
| 1531 | reply.flags = htobe32(reply.flags); |
| 1532 | |
| 1533 | health_code_update(); |
| 1534 | |
| 1535 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
| 1536 | if (ret < 0) { |
| 1537 | goto end_free; |
| 1538 | } |
| 1539 | health_code_update(); |
| 1540 | |
| 1541 | if (send_data) { |
| 1542 | health_code_update(); |
| 1543 | ret = send_response(conn->sock, data, len); |
| 1544 | if (ret < 0) { |
| 1545 | goto end_free; |
| 1546 | } |
| 1547 | health_code_update(); |
| 1548 | } |
| 1549 | |
| 1550 | DBG("Sent %u bytes for stream %" PRIu64, len, |
| 1551 | be64toh(get_packet_info.stream_id)); |
| 1552 | |
| 1553 | end_free: |
| 1554 | free(data); |
| 1555 | end: |
| 1556 | if (vstream) { |
| 1557 | viewer_stream_put(vstream); |
| 1558 | } |
| 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * Send the session's metadata |
| 1564 | * |
| 1565 | * Return 0 on success else a negative value. |
| 1566 | */ |
| 1567 | static |
| 1568 | int viewer_get_metadata(struct relay_connection *conn) |
| 1569 | { |
| 1570 | int ret = 0; |
| 1571 | ssize_t read_len; |
| 1572 | uint64_t len = 0; |
| 1573 | char *data = NULL; |
| 1574 | struct lttng_viewer_get_metadata request; |
| 1575 | struct lttng_viewer_metadata_packet reply; |
| 1576 | struct relay_viewer_stream *vstream = NULL; |
| 1577 | |
| 1578 | assert(conn); |
| 1579 | |
| 1580 | DBG("Relay get metadata"); |
| 1581 | |
| 1582 | health_code_update(); |
| 1583 | |
| 1584 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 1585 | if (ret < 0) { |
| 1586 | goto end; |
| 1587 | } |
| 1588 | health_code_update(); |
| 1589 | |
| 1590 | memset(&reply, 0, sizeof(reply)); |
| 1591 | |
| 1592 | vstream = viewer_stream_get_by_id(be64toh(request.stream_id)); |
| 1593 | if (!vstream) { |
| 1594 | /* |
| 1595 | * The metadata stream can be closed by a CLOSE command |
| 1596 | * just before we attach. It can also be closed by |
| 1597 | * per-pid tracing during tracing. Therefore, it is |
| 1598 | * possible that we cannot find this viewer stream. |
| 1599 | * Reply back to the client with an error if we cannot |
| 1600 | * find it. |
| 1601 | */ |
| 1602 | DBG("Client requested metadata of unknown stream id %" PRIu64, |
| 1603 | be64toh(request.stream_id)); |
| 1604 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 1605 | goto send_reply; |
| 1606 | } |
| 1607 | pthread_mutex_lock(&vstream->stream->lock); |
| 1608 | if (!vstream->stream->is_metadata) { |
| 1609 | ERR("Invalid metadata stream"); |
| 1610 | goto error; |
| 1611 | } |
| 1612 | |
| 1613 | assert(vstream->metadata_sent <= vstream->stream->metadata_received); |
| 1614 | |
| 1615 | len = vstream->stream->metadata_received - vstream->metadata_sent; |
| 1616 | if (len == 0) { |
| 1617 | reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA); |
| 1618 | goto send_reply; |
| 1619 | } |
| 1620 | |
| 1621 | /* first time, we open the metadata file */ |
| 1622 | if (!vstream->stream_fd) { |
| 1623 | char fullpath[PATH_MAX]; |
| 1624 | |
| 1625 | ret = snprintf(fullpath, PATH_MAX, "%s/%s", vstream->path_name, |
| 1626 | vstream->channel_name); |
| 1627 | if (ret < 0) { |
| 1628 | goto error; |
| 1629 | } |
| 1630 | ret = open(fullpath, O_RDONLY); |
| 1631 | if (ret < 0) { |
| 1632 | PERROR("Relay opening metadata file"); |
| 1633 | goto error; |
| 1634 | } |
| 1635 | vstream->stream_fd = stream_fd_create(ret); |
| 1636 | if (!vstream->stream_fd) { |
| 1637 | if (close(ret)) { |
| 1638 | PERROR("close"); |
| 1639 | } |
| 1640 | goto error; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | reply.len = htobe64(len); |
| 1645 | data = zmalloc(len); |
| 1646 | if (!data) { |
| 1647 | PERROR("viewer metadata zmalloc"); |
| 1648 | goto error; |
| 1649 | } |
| 1650 | |
| 1651 | read_len = lttng_read(vstream->stream_fd->fd, data, len); |
| 1652 | if (read_len < len) { |
| 1653 | PERROR("Relay reading metadata file"); |
| 1654 | goto error; |
| 1655 | } |
| 1656 | vstream->metadata_sent += read_len; |
| 1657 | if (vstream->metadata_sent == vstream->stream->metadata_received |
| 1658 | && vstream->stream->closed) { |
| 1659 | /* Release ownership for the viewer metadata stream. */ |
| 1660 | viewer_stream_put(vstream); |
| 1661 | } |
| 1662 | |
| 1663 | reply.status = htobe32(LTTNG_VIEWER_METADATA_OK); |
| 1664 | |
| 1665 | goto send_reply; |
| 1666 | |
| 1667 | error: |
| 1668 | reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR); |
| 1669 | |
| 1670 | send_reply: |
| 1671 | health_code_update(); |
| 1672 | if (vstream) { |
| 1673 | pthread_mutex_unlock(&vstream->stream->lock); |
| 1674 | } |
| 1675 | ret = send_response(conn->sock, &reply, sizeof(reply)); |
| 1676 | if (ret < 0) { |
| 1677 | goto end_free; |
| 1678 | } |
| 1679 | health_code_update(); |
| 1680 | |
| 1681 | if (len > 0) { |
| 1682 | ret = send_response(conn->sock, data, len); |
| 1683 | if (ret < 0) { |
| 1684 | goto end_free; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len, |
| 1689 | be64toh(request.stream_id)); |
| 1690 | |
| 1691 | DBG("Metadata sent"); |
| 1692 | |
| 1693 | end_free: |
| 1694 | free(data); |
| 1695 | end: |
| 1696 | if (vstream) { |
| 1697 | viewer_stream_put(vstream); |
| 1698 | } |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * Create a viewer session. |
| 1704 | * |
| 1705 | * Return 0 on success or else a negative value. |
| 1706 | */ |
| 1707 | static |
| 1708 | int viewer_create_session(struct relay_connection *conn) |
| 1709 | { |
| 1710 | int ret; |
| 1711 | struct lttng_viewer_create_session_response resp; |
| 1712 | |
| 1713 | DBG("Viewer create session received"); |
| 1714 | |
| 1715 | memset(&resp, 0, sizeof(resp)); |
| 1716 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK); |
| 1717 | conn->viewer_session = viewer_session_create(); |
| 1718 | if (!conn->viewer_session) { |
| 1719 | ERR("Allocation viewer session"); |
| 1720 | resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR); |
| 1721 | goto send_reply; |
| 1722 | } |
| 1723 | |
| 1724 | send_reply: |
| 1725 | health_code_update(); |
| 1726 | ret = send_response(conn->sock, &resp, sizeof(resp)); |
| 1727 | if (ret < 0) { |
| 1728 | goto end; |
| 1729 | } |
| 1730 | health_code_update(); |
| 1731 | ret = 0; |
| 1732 | |
| 1733 | end: |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * Detach a viewer session. |
| 1739 | * |
| 1740 | * Return 0 on success or else a negative value. |
| 1741 | */ |
| 1742 | static |
| 1743 | int viewer_detach_session(struct relay_connection *conn) |
| 1744 | { |
| 1745 | int ret; |
| 1746 | struct lttng_viewer_detach_session_response response; |
| 1747 | struct lttng_viewer_detach_session_request request; |
| 1748 | struct relay_session *session = NULL; |
| 1749 | uint64_t viewer_session_to_close; |
| 1750 | |
| 1751 | DBG("Viewer detach session received"); |
| 1752 | |
| 1753 | assert(conn); |
| 1754 | |
| 1755 | health_code_update(); |
| 1756 | |
| 1757 | /* Receive the request from the connected client. */ |
| 1758 | ret = recv_request(conn->sock, &request, sizeof(request)); |
| 1759 | if (ret < 0) { |
| 1760 | goto end; |
| 1761 | } |
| 1762 | viewer_session_to_close = be64toh(request.session_id); |
| 1763 | |
| 1764 | if (!conn->viewer_session) { |
| 1765 | DBG("Client trying to detach before creating a live viewer session"); |
| 1766 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); |
| 1767 | goto send_reply; |
| 1768 | } |
| 1769 | |
| 1770 | health_code_update(); |
| 1771 | |
| 1772 | memset(&response, 0, sizeof(response)); |
| 1773 | DBG("Detaching from session ID %" PRIu64, viewer_session_to_close); |
| 1774 | |
| 1775 | session = session_get_by_id(be64toh(request.session_id)); |
| 1776 | if (!session) { |
| 1777 | DBG("Relay session %" PRIu64 " not found", |
| 1778 | be64toh(request.session_id)); |
| 1779 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK); |
| 1780 | goto send_reply; |
| 1781 | } |
| 1782 | |
| 1783 | ret = viewer_session_is_attached(conn->viewer_session, session); |
| 1784 | if (ret != 1) { |
| 1785 | DBG("Not attached to this session"); |
| 1786 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR); |
| 1787 | goto send_reply_put; |
| 1788 | } |
| 1789 | |
| 1790 | viewer_session_close_one_session(conn->viewer_session, session); |
| 1791 | response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK); |
| 1792 | DBG("Session %" PRIu64 " detached.", viewer_session_to_close); |
| 1793 | |
| 1794 | send_reply_put: |
| 1795 | session_put(session); |
| 1796 | |
| 1797 | send_reply: |
| 1798 | health_code_update(); |
| 1799 | ret = send_response(conn->sock, &response, sizeof(response)); |
| 1800 | if (ret < 0) { |
| 1801 | goto end; |
| 1802 | } |
| 1803 | health_code_update(); |
| 1804 | ret = 0; |
| 1805 | |
| 1806 | end: |
| 1807 | return ret; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * live_relay_unknown_command: send -1 if received unknown command |
| 1812 | */ |
| 1813 | static |
| 1814 | void live_relay_unknown_command(struct relay_connection *conn) |
| 1815 | { |
| 1816 | struct lttcomm_relayd_generic_reply reply; |
| 1817 | |
| 1818 | memset(&reply, 0, sizeof(reply)); |
| 1819 | reply.ret_code = htobe32(LTTNG_ERR_UNK); |
| 1820 | (void) send_response(conn->sock, &reply, sizeof(reply)); |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * Process the commands received on the control socket |
| 1825 | */ |
| 1826 | static |
| 1827 | int process_control(struct lttng_viewer_cmd *recv_hdr, |
| 1828 | struct relay_connection *conn) |
| 1829 | { |
| 1830 | int ret = 0; |
| 1831 | uint32_t msg_value; |
| 1832 | |
| 1833 | msg_value = be32toh(recv_hdr->cmd); |
| 1834 | |
| 1835 | /* |
| 1836 | * Make sure we've done the version check before any command other then a |
| 1837 | * new client connection. |
| 1838 | */ |
| 1839 | if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) { |
| 1840 | ERR("Viewer conn value %" PRIu32 " before version check", msg_value); |
| 1841 | ret = -1; |
| 1842 | goto end; |
| 1843 | } |
| 1844 | |
| 1845 | switch (msg_value) { |
| 1846 | case LTTNG_VIEWER_CONNECT: |
| 1847 | ret = viewer_connect(conn); |
| 1848 | break; |
| 1849 | case LTTNG_VIEWER_LIST_SESSIONS: |
| 1850 | ret = viewer_list_sessions(conn); |
| 1851 | break; |
| 1852 | case LTTNG_VIEWER_ATTACH_SESSION: |
| 1853 | ret = viewer_attach_session(conn); |
| 1854 | break; |
| 1855 | case LTTNG_VIEWER_GET_NEXT_INDEX: |
| 1856 | ret = viewer_get_next_index(conn); |
| 1857 | break; |
| 1858 | case LTTNG_VIEWER_GET_PACKET: |
| 1859 | ret = viewer_get_packet(conn); |
| 1860 | break; |
| 1861 | case LTTNG_VIEWER_GET_METADATA: |
| 1862 | ret = viewer_get_metadata(conn); |
| 1863 | break; |
| 1864 | case LTTNG_VIEWER_GET_NEW_STREAMS: |
| 1865 | ret = viewer_get_new_streams(conn); |
| 1866 | break; |
| 1867 | case LTTNG_VIEWER_CREATE_SESSION: |
| 1868 | ret = viewer_create_session(conn); |
| 1869 | break; |
| 1870 | case LTTNG_VIEWER_DETACH_SESSION: |
| 1871 | ret = viewer_detach_session(conn); |
| 1872 | break; |
| 1873 | default: |
| 1874 | ERR("Received unknown viewer command (%u)", |
| 1875 | be32toh(recv_hdr->cmd)); |
| 1876 | live_relay_unknown_command(conn); |
| 1877 | ret = -1; |
| 1878 | goto end; |
| 1879 | } |
| 1880 | |
| 1881 | end: |
| 1882 | return ret; |
| 1883 | } |
| 1884 | |
| 1885 | static |
| 1886 | void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd) |
| 1887 | { |
| 1888 | int ret; |
| 1889 | |
| 1890 | (void) lttng_poll_del(events, pollfd); |
| 1891 | |
| 1892 | ret = close(pollfd); |
| 1893 | if (ret < 0) { |
| 1894 | ERR("Closing pollfd %d", pollfd); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * This thread does the actual work |
| 1900 | */ |
| 1901 | static |
| 1902 | void *thread_worker(void *data) |
| 1903 | { |
| 1904 | int ret, err = -1; |
| 1905 | uint32_t nb_fd; |
| 1906 | struct lttng_poll_event events; |
| 1907 | struct lttng_ht *viewer_connections_ht; |
| 1908 | struct lttng_ht_iter iter; |
| 1909 | struct lttng_viewer_cmd recv_hdr; |
| 1910 | struct relay_connection *destroy_conn; |
| 1911 | |
| 1912 | DBG("[thread] Live viewer relay worker started"); |
| 1913 | |
| 1914 | rcu_register_thread(); |
| 1915 | |
| 1916 | health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER); |
| 1917 | |
| 1918 | if (testpoint(relayd_thread_live_worker)) { |
| 1919 | goto error_testpoint; |
| 1920 | } |
| 1921 | |
| 1922 | /* table of connections indexed on socket */ |
| 1923 | viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
| 1924 | if (!viewer_connections_ht) { |
| 1925 | goto viewer_connections_ht_error; |
| 1926 | } |
| 1927 | |
| 1928 | ret = create_thread_poll_set(&events, 2); |
| 1929 | if (ret < 0) { |
| 1930 | goto error_poll_create; |
| 1931 | } |
| 1932 | |
| 1933 | ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP); |
| 1934 | if (ret < 0) { |
| 1935 | goto error; |
| 1936 | } |
| 1937 | |
| 1938 | restart: |
| 1939 | while (1) { |
| 1940 | int i; |
| 1941 | |
| 1942 | health_code_update(); |
| 1943 | |
| 1944 | /* Infinite blocking call, waiting for transmission */ |
| 1945 | DBG3("Relayd live viewer worker thread polling..."); |
| 1946 | health_poll_entry(); |
| 1947 | ret = lttng_poll_wait(&events, -1); |
| 1948 | health_poll_exit(); |
| 1949 | if (ret < 0) { |
| 1950 | /* |
| 1951 | * Restart interrupted system call. |
| 1952 | */ |
| 1953 | if (errno == EINTR) { |
| 1954 | goto restart; |
| 1955 | } |
| 1956 | goto error; |
| 1957 | } |
| 1958 | |
| 1959 | nb_fd = ret; |
| 1960 | |
| 1961 | /* |
| 1962 | * Process control. The control connection is prioritised so we don't |
| 1963 | * starve it with high throughput tracing data on the data |
| 1964 | * connection. |
| 1965 | */ |
| 1966 | for (i = 0; i < nb_fd; i++) { |
| 1967 | /* Fetch once the poll data */ |
| 1968 | uint32_t revents = LTTNG_POLL_GETEV(&events, i); |
| 1969 | int pollfd = LTTNG_POLL_GETFD(&events, i); |
| 1970 | |
| 1971 | health_code_update(); |
| 1972 | |
| 1973 | if (!revents) { |
| 1974 | /* No activity for this FD (poll implementation). */ |
| 1975 | continue; |
| 1976 | } |
| 1977 | |
| 1978 | /* Thread quit pipe has been closed. Killing thread. */ |
| 1979 | ret = check_thread_quit_pipe(pollfd, revents); |
| 1980 | if (ret) { |
| 1981 | err = 0; |
| 1982 | goto exit; |
| 1983 | } |
| 1984 | |
| 1985 | /* Inspect the relay conn pipe for new connection. */ |
| 1986 | if (pollfd == live_conn_pipe[0]) { |
| 1987 | if (revents & LPOLLIN) { |
| 1988 | struct relay_connection *conn; |
| 1989 | |
| 1990 | ret = lttng_read(live_conn_pipe[0], |
| 1991 | &conn, sizeof(conn)); |
| 1992 | if (ret < 0) { |
| 1993 | goto error; |
| 1994 | } |
| 1995 | lttng_poll_add(&events, conn->sock->fd, |
| 1996 | LPOLLIN | LPOLLRDHUP); |
| 1997 | connection_ht_add(viewer_connections_ht, conn); |
| 1998 | DBG("Connection socket %d added to poll", conn->sock->fd); |
| 1999 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 2000 | ERR("Relay live pipe error"); |
| 2001 | goto error; |
| 2002 | } else { |
| 2003 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 2004 | goto error; |
| 2005 | } |
| 2006 | } else { |
| 2007 | /* Connection activity. */ |
| 2008 | struct relay_connection *conn; |
| 2009 | |
| 2010 | conn = connection_get_by_sock(viewer_connections_ht, pollfd); |
| 2011 | if (!conn) { |
| 2012 | continue; |
| 2013 | } |
| 2014 | |
| 2015 | if (revents & LPOLLIN) { |
| 2016 | ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr, |
| 2017 | sizeof(recv_hdr), 0); |
| 2018 | if (ret <= 0) { |
| 2019 | /* Connection closed. */ |
| 2020 | cleanup_connection_pollfd(&events, pollfd); |
| 2021 | /* Put "create" ownership reference. */ |
| 2022 | connection_put(conn); |
| 2023 | DBG("Viewer control conn closed with %d", pollfd); |
| 2024 | } else { |
| 2025 | ret = process_control(&recv_hdr, conn); |
| 2026 | if (ret < 0) { |
| 2027 | /* Clear the session on error. */ |
| 2028 | cleanup_connection_pollfd(&events, pollfd); |
| 2029 | /* Put "create" ownership reference. */ |
| 2030 | connection_put(conn); |
| 2031 | DBG("Viewer connection closed with %d", pollfd); |
| 2032 | } |
| 2033 | } |
| 2034 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 2035 | cleanup_connection_pollfd(&events, pollfd); |
| 2036 | /* Put "create" ownership reference. */ |
| 2037 | connection_put(conn); |
| 2038 | } else { |
| 2039 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 2040 | connection_put(conn); |
| 2041 | goto error; |
| 2042 | } |
| 2043 | /* Put local "get_by_sock" reference. */ |
| 2044 | connection_put(conn); |
| 2045 | } |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | exit: |
| 2050 | error: |
| 2051 | lttng_poll_clean(&events); |
| 2052 | |
| 2053 | /* Cleanup reamaining connection object. */ |
| 2054 | rcu_read_lock(); |
| 2055 | cds_lfht_for_each_entry(viewer_connections_ht->ht, &iter.iter, |
| 2056 | destroy_conn, |
| 2057 | sock_n.node) { |
| 2058 | health_code_update(); |
| 2059 | connection_put(destroy_conn); |
| 2060 | } |
| 2061 | rcu_read_unlock(); |
| 2062 | error_poll_create: |
| 2063 | lttng_ht_destroy(viewer_connections_ht); |
| 2064 | viewer_connections_ht_error: |
| 2065 | /* Close relay conn pipes */ |
| 2066 | utils_close_pipe(live_conn_pipe); |
| 2067 | if (err) { |
| 2068 | DBG("Viewer worker thread exited with error"); |
| 2069 | } |
| 2070 | DBG("Viewer worker thread cleanup complete"); |
| 2071 | error_testpoint: |
| 2072 | if (err) { |
| 2073 | health_error(); |
| 2074 | ERR("Health error occurred in %s", __func__); |
| 2075 | } |
| 2076 | health_unregister(health_relayd); |
| 2077 | if (lttng_relay_stop_threads()) { |
| 2078 | ERR("Error stopping threads"); |
| 2079 | } |
| 2080 | rcu_unregister_thread(); |
| 2081 | return NULL; |
| 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | * Create the relay command pipe to wake thread_manage_apps. |
| 2086 | * Closed in cleanup(). |
| 2087 | */ |
| 2088 | static int create_conn_pipe(void) |
| 2089 | { |
| 2090 | return utils_create_pipe_cloexec(live_conn_pipe); |
| 2091 | } |
| 2092 | |
| 2093 | int relayd_live_join(void) |
| 2094 | { |
| 2095 | int ret, retval = 0; |
| 2096 | void *status; |
| 2097 | |
| 2098 | ret = pthread_join(live_listener_thread, &status); |
| 2099 | if (ret) { |
| 2100 | errno = ret; |
| 2101 | PERROR("pthread_join live listener"); |
| 2102 | retval = -1; |
| 2103 | } |
| 2104 | |
| 2105 | ret = pthread_join(live_worker_thread, &status); |
| 2106 | if (ret) { |
| 2107 | errno = ret; |
| 2108 | PERROR("pthread_join live worker"); |
| 2109 | retval = -1; |
| 2110 | } |
| 2111 | |
| 2112 | ret = pthread_join(live_dispatcher_thread, &status); |
| 2113 | if (ret) { |
| 2114 | errno = ret; |
| 2115 | PERROR("pthread_join live dispatcher"); |
| 2116 | retval = -1; |
| 2117 | } |
| 2118 | |
| 2119 | cleanup_relayd_live(); |
| 2120 | |
| 2121 | return retval; |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | * main |
| 2126 | */ |
| 2127 | int relayd_live_create(struct lttng_uri *uri) |
| 2128 | { |
| 2129 | int ret = 0, retval = 0; |
| 2130 | void *status; |
| 2131 | int is_root; |
| 2132 | |
| 2133 | if (!uri) { |
| 2134 | retval = -1; |
| 2135 | goto exit_init_data; |
| 2136 | } |
| 2137 | live_uri = uri; |
| 2138 | |
| 2139 | /* Check if daemon is UID = 0 */ |
| 2140 | is_root = !getuid(); |
| 2141 | |
| 2142 | if (!is_root) { |
| 2143 | if (live_uri->port < 1024) { |
| 2144 | ERR("Need to be root to use ports < 1024"); |
| 2145 | retval = -1; |
| 2146 | goto exit_init_data; |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* Setup the thread apps communication pipe. */ |
| 2151 | if (create_conn_pipe()) { |
| 2152 | retval = -1; |
| 2153 | goto exit_init_data; |
| 2154 | } |
| 2155 | |
| 2156 | /* Init relay command queue. */ |
| 2157 | cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail); |
| 2158 | |
| 2159 | /* Set up max poll set size */ |
| 2160 | if (lttng_poll_set_max_size()) { |
| 2161 | retval = -1; |
| 2162 | goto exit_init_data; |
| 2163 | } |
| 2164 | |
| 2165 | /* Setup the dispatcher thread */ |
| 2166 | ret = pthread_create(&live_dispatcher_thread, NULL, |
| 2167 | thread_dispatcher, (void *) NULL); |
| 2168 | if (ret) { |
| 2169 | errno = ret; |
| 2170 | PERROR("pthread_create viewer dispatcher"); |
| 2171 | retval = -1; |
| 2172 | goto exit_dispatcher_thread; |
| 2173 | } |
| 2174 | |
| 2175 | /* Setup the worker thread */ |
| 2176 | ret = pthread_create(&live_worker_thread, NULL, |
| 2177 | thread_worker, NULL); |
| 2178 | if (ret) { |
| 2179 | errno = ret; |
| 2180 | PERROR("pthread_create viewer worker"); |
| 2181 | retval = -1; |
| 2182 | goto exit_worker_thread; |
| 2183 | } |
| 2184 | |
| 2185 | /* Setup the listener thread */ |
| 2186 | ret = pthread_create(&live_listener_thread, NULL, |
| 2187 | thread_listener, (void *) NULL); |
| 2188 | if (ret) { |
| 2189 | errno = ret; |
| 2190 | PERROR("pthread_create viewer listener"); |
| 2191 | retval = -1; |
| 2192 | goto exit_listener_thread; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * All OK, started all threads. |
| 2197 | */ |
| 2198 | return retval; |
| 2199 | |
| 2200 | /* |
| 2201 | * Join on the live_listener_thread should anything be added after |
| 2202 | * the live_listener thread's creation. |
| 2203 | */ |
| 2204 | |
| 2205 | exit_listener_thread: |
| 2206 | |
| 2207 | ret = pthread_join(live_worker_thread, &status); |
| 2208 | if (ret) { |
| 2209 | errno = ret; |
| 2210 | PERROR("pthread_join live worker"); |
| 2211 | retval = -1; |
| 2212 | } |
| 2213 | exit_worker_thread: |
| 2214 | |
| 2215 | ret = pthread_join(live_dispatcher_thread, &status); |
| 2216 | if (ret) { |
| 2217 | errno = ret; |
| 2218 | PERROR("pthread_join live dispatcher"); |
| 2219 | retval = -1; |
| 2220 | } |
| 2221 | exit_dispatcher_thread: |
| 2222 | |
| 2223 | exit_init_data: |
| 2224 | cleanup_relayd_live(); |
| 2225 | |
| 2226 | return retval; |
| 2227 | } |