docs: Add supported versions and fix-backport policy
[lttng-tools.git] / src / bin / lttng-relayd / live.cpp
CommitLineData
d3e2ba59 1/*
ab5be9fa
MJ
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>
d3e2ba59 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
d3e2ba59 7 *
d3e2ba59
JD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
28ab034a
JG
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>
56047f5a 36#include <common/urcu.hpp>
28ab034a
JG
37#include <common/uri.hpp>
38#include <common/utils.hpp>
39
40#include <lttng/lttng.h>
41
8bb66c3c 42#include <fcntl.h>
d3e2ba59
JD
43#include <getopt.h>
44#include <grp.h>
8bb66c3c 45#include <inttypes.h>
d3e2ba59
JD
46#include <limits.h>
47#include <pthread.h>
48#include <signal.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
28ab034a 52#include <string>
d3e2ba59
JD
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>
8bb66c3c 60#include <unistd.h>
d3e2ba59 61#include <urcu/futex.h>
7591bab1 62#include <urcu/rculist.h>
8bb66c3c 63#include <urcu/uatomic.h>
d3e2ba59 64
28ab034a 65#define SESSION_BUF_DEFAULT_COUNT 16
d3e2ba59
JD
66
67static struct lttng_uri *live_uri;
68
d3e2ba59
JD
69/*
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
72 */
58eb9381 73static int live_conn_pipe[2] = { -1, -1 };
d3e2ba59
JD
74
75/* Shared between threads */
76static int live_dispatch_thread_exit;
77
78static pthread_t live_listener_thread;
79static pthread_t live_dispatcher_thread;
80static 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 */
58eb9381 88static struct relay_conn_queue viewer_conn_queue;
d3e2ba59
JD
89
90static uint64_t last_relay_viewer_session_id;
28ab034a 91static pthread_mutex_t last_relay_viewer_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 92
28ab034a 93static const char *lttng_viewer_command_str(lttng_viewer_command cmd)
7201079f
FD
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
28ab034a
JG
119static const char *
120lttng_viewer_next_index_return_code_str(enum lttng_viewer_next_index_return_code code)
7e4fb89b
FD
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
28ab034a 140static const char *lttng_viewer_attach_return_code_str(enum lttng_viewer_attach_return_code code)
07d9fffa
FD
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
28ab034a
JG
160static const char *
161lttng_viewer_get_packet_return_code_str(enum lttng_viewer_get_packet_return_code code)
556d5fae
FD
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
d3e2ba59
JD
177/*
178 * Cleanup the daemon
179 */
cd9adb8b 180static void cleanup_relayd_live()
d3e2ba59
JD
181{
182 DBG("Cleaning up");
183
d3e2ba59
JD
184 free(live_uri);
185}
186
2f8f53af
DG
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 */
28ab034a 194static ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
2f8f53af
DG
195{
196 ssize_t ret;
197
2f8f53af
DG
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 */
28ab034a 219static ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
2f8f53af
DG
220{
221 ssize_t ret;
222
2f8f53af
DG
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/*
f04a971b
JD
232 * Atomically check if new streams got added in one of the sessions attached
233 * and reset the flag to 0.
2f8f53af
DG
234 *
235 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
236 * on error.
237 */
28ab034a 238static int check_new_streams(struct relay_connection *conn)
2f8f53af 239{
2f8f53af 240 struct relay_session *session;
f04a971b 241 int ret = 0;
2f8f53af 242
f04a971b
JD
243 if (!conn->viewer_session) {
244 goto end;
245 }
56047f5a 246
28ab034a 247 {
56047f5a
JG
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 }
283a96e4
JG
255
256 ret = uatomic_read(&session->new_streams);
56047f5a
JG
257 session_put(session);
258 if (ret == 1) {
259 goto end;
260 }
f04a971b 261 }
2f8f53af 262 }
283a96e4 263
f04a971b 264end:
bb9f2140
JG
265 DBG("Viewer connection has%s new streams: socket_fd = %d",
266 ret == 0 ? " no" : "",
267 conn->sock->fd);
2f8f53af
DG
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 */
28ab034a
JG
277static ssize_t
278send_viewer_streams(struct lttcomm_sock *sock, uint64_t session_id, unsigned int ignore_sent_flag)
2f8f53af
DG
279{
280 ssize_t ret;
2f8f53af
DG
281 struct lttng_ht_iter iter;
282 struct relay_viewer_stream *vstream;
283
56047f5a
JG
284 {
285 lttng::urcu::read_lock_guard read_lock;
2f8f53af 286
56047f5a
JG
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 = {};
2a174661 291
56047f5a 292 health_code_update();
2f8f53af 293
56047f5a
JG
294 if (!viewer_stream_get(vstream)) {
295 continue;
296 }
7591bab1 297
56047f5a
JG
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 }
2f8f53af 306
56047f5a
JG
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 }
2f8f53af 327
56047f5a
JG
328 DBG("Sending stream %" PRIu64 " to viewer", vstream->stream->stream_handle);
329 vstream->sent_flag = true;
330 pthread_mutex_unlock(&vstream->stream->lock);
7591bab1 331
56047f5a
JG
332 ret = send_response(sock, &send_stream, sizeof(send_stream));
333 viewer_stream_put(vstream);
334 if (ret < 0) {
335 goto end;
336 }
2f8f53af 337 }
2f8f53af
DG
338 }
339
340 ret = 0;
341
56047f5a 342end:
2f8f53af
DG
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 *
c06fdd95
JG
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 *
2f8f53af
DG
355 * Return 0 on success or else a negative value.
356 */
9edaf114 357static int make_viewer_streams(struct relay_session *relay_session,
28ab034a
JG
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)
2f8f53af
DG
364{
365 int ret;
2f8f53af 366 struct lttng_ht_iter iter;
2a174661 367 struct ctf_trace *ctf_trace;
cd9adb8b 368 struct relay_stream *relay_stream = nullptr;
2f8f53af 369
a0377dfe 370 LTTNG_ASSERT(relay_session);
9edaf114 371 ASSERT_LOCKED(relay_session->lock);
2f8f53af 372
9edaf114 373 if (relay_session->connection_closed) {
bddf80e4
MD
374 *closed = true;
375 }
376
2f8f53af 377 /*
7591bab1
MD
378 * Create viewer streams for relay streams that are ready to be
379 * used for a the given session id only.
2f8f53af 380 */
56047f5a
JG
381 {
382 lttng::urcu::read_lock_guard read_lock;
2f8f53af 383
56047f5a
JG
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;
9edaf114 387
56047f5a 388 health_code_update();
9edaf114 389
56047f5a
JG
390 if (!ctf_trace_get(ctf_trace)) {
391 continue;
123ed7c2 392 }
123ed7c2 393
56047f5a
JG
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 }
2a174661 411 }
9edaf114 412
56047f5a
JG
413 relay_stream = nullptr;
414
7591bab1 415 /*
56047f5a
JG
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.
7591bab1 419 */
56047f5a
JG
420 if (!trace_has_metadata_stream &&
421 !ctf_trace->metadata_stream_sent_to_viewer) {
422 ctf_trace_put(ctf_trace);
423 continue;
7591bab1 424 }
9edaf114 425
56047f5a
JG
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;
9edaf114
JG
433 }
434
56047f5a 435 pthread_mutex_lock(&relay_stream->lock);
9edaf114 436 /*
56047f5a 437 * stream published is protected by the session lock.
9edaf114 438 */
56047f5a
JG
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;
9edaf114 455 }
56047f5a 456
80516611 457 /*
56047f5a
JG
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.
80516611 464 */
56047f5a
JG
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) {
80516611
JG
471 ret = -1;
472 ctf_trace_put(ctf_trace);
473 goto error_unlock;
474 }
56047f5a 475 } else {
87250ba1 476 /*
56047f5a
JG
477 * Transition the viewer session into the newest
478 * trace chunk available.
87250ba1 479 */
56047f5a
JG
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 }
87250ba1 492
56047f5a
JG
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 }
87250ba1 515 }
9edaf114 516
56047f5a
JG
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 }
2a174661 526
56047f5a
JG
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();
2229a09c
MD
538 }
539 } else {
56047f5a
JG
540 if (!viewer_stream->sent_flag && nb_unsent) {
541 /* Update number of unsent stream counter. */
542 (*nb_unsent)++;
2229a09c
MD
543 }
544 }
56047f5a
JG
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);
2f8f53af 567 }
56047f5a
JG
568 relay_stream = nullptr;
569 ctf_trace_put(ctf_trace);
2f8f53af
DG
570 }
571 }
572
573 ret = 0;
574
575error_unlock:
80516611 576
9edaf114
JG
577 if (relay_stream) {
578 pthread_mutex_unlock(&relay_stream->lock);
579 stream_put(relay_stream);
580 }
581
2f8f53af
DG
582 return ret;
583}
584
cd9adb8b 585int relayd_live_stop()
d3e2ba59 586{
b4aacfdc 587 /* Stop dispatch thread */
d3e2ba59 588 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
58eb9381 589 futex_nto1_wake(&viewer_conn_queue.futex);
b4aacfdc 590 return 0;
d3e2ba59
JD
591}
592
28ab034a 593static int create_sock(void *data, int *out_fd)
8855795d
JG
594{
595 int ret;
ac497a37 596 struct lttcomm_sock *sock = (lttcomm_sock *) data;
8855795d
JG
597
598 ret = lttcomm_create_sock(sock);
599 if (ret < 0) {
600 goto end;
601 }
602
603 *out_fd = sock->fd;
604end:
605 return ret;
606}
607
28ab034a 608static int close_sock(void *data, int *in_fd __attribute__((unused)))
8855795d 609{
ac497a37 610 struct lttcomm_sock *sock = (lttcomm_sock *) data;
8855795d
JG
611
612 return sock->ops->close(sock);
613}
614
29555a78
JG
615static int accept_sock(void *data, int *out_fd)
616{
617 int ret = 0;
618 /* Socks is an array of in_sock, out_sock. */
ac497a37 619 struct lttcomm_sock **socks = (lttcomm_sock **) data;
29555a78
JG
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;
628end:
629 return ret;
630}
631
28ab034a 632static struct lttcomm_sock *accept_live_sock(struct lttcomm_sock *listening_sock, const char *name)
29555a78
JG
633{
634 int out_fd, ret;
cd9adb8b
JG
635 struct lttcomm_sock *socks[2] = { listening_sock, nullptr };
636 struct lttcomm_sock *new_sock = nullptr;
29555a78 637
28ab034a
JG
638 ret = fd_tracker_open_unsuspendable_fd(
639 the_fd_tracker, &out_fd, (const char **) &name, 1, accept_sock, &socks);
29555a78
JG
640 if (ret) {
641 goto end;
642 }
643 new_sock = socks[1];
644 DBG("%s accepted, socket %d", name, new_sock->fd);
645end:
646 return new_sock;
647}
648
d3e2ba59
JD
649/*
650 * Create and init socket from uri.
651 */
28ab034a 652static struct lttcomm_sock *init_socket(struct lttng_uri *uri, const char *name)
d3e2ba59 653{
8855795d 654 int ret, sock_fd;
cd9adb8b 655 struct lttcomm_sock *sock = nullptr;
8855795d 656 char uri_str[LTTNG_PATH_MAX];
cd9adb8b 657 char *formated_name = nullptr;
d3e2ba59
JD
658
659 sock = lttcomm_alloc_sock_from_uri(uri);
cd9adb8b 660 if (sock == nullptr) {
d3e2ba59
JD
661 ERR("Allocating socket");
662 goto error;
663 }
664
8855795d
JG
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) {
28ab034a 672 ret = asprintf(&formated_name, "%s socket @ %s", name, uri_str);
8855795d 673 if (ret < 0) {
cd9adb8b 674 formated_name = nullptr;
8855795d 675 }
d3e2ba59 676 }
8855795d 677
28ab034a
JG
678 ret = fd_tracker_open_unsuspendable_fd(the_fd_tracker,
679 &sock_fd,
680 (const char **) (formated_name ? &formated_name :
cd9adb8b 681 nullptr),
28ab034a
JG
682 1,
683 create_sock,
684 sock);
d546eeec 685 if (ret) {
28ab034a 686 PERROR("Failed to create \"%s\" socket", formated_name ?: "Unknown");
d546eeec
JG
687 goto error;
688 }
8855795d 689 DBG("Listening on %s socket %d", name, sock->fd);
d3e2ba59
JD
690
691 ret = sock->ops->bind(sock);
692 if (ret < 0) {
2288467f 693 PERROR("Failed to bind lttng-live socket");
d3e2ba59
JD
694 goto error;
695 }
696
697 ret = sock->ops->listen(sock, -1);
698 if (ret < 0) {
699 goto error;
d3e2ba59
JD
700 }
701
d546eeec 702 free(formated_name);
d3e2ba59
JD
703 return sock;
704
705error:
706 if (sock) {
707 lttcomm_destroy_sock(sock);
708 }
d546eeec 709 free(formated_name);
cd9adb8b 710 return nullptr;
d3e2ba59
JD
711}
712
713/*
714 * This thread manages the listening for new connections on the network
715 */
28ab034a 716static void *thread_listener(void *data __attribute__((unused)))
d3e2ba59 717{
8a00688e
MJ
718 int i, ret, err = -1;
719 uint32_t nb_fd;
d3e2ba59
JD
720 struct lttng_poll_event events;
721 struct lttcomm_sock *live_control_sock;
722
723 DBG("[thread] Relay live listener started");
724
8fba2b8d 725 rcu_register_thread();
eea7556c
MD
726 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
727
728 health_code_update();
729
8855795d 730 live_control_sock = init_socket(live_uri, "Live listener");
d3e2ba59
JD
731 if (!live_control_sock) {
732 goto error_sock_control;
733 }
734
fb4d42ab 735 /* Pass 2 as size here for the thread quit pipe and control sockets. */
28ab034a 736 ret = create_named_thread_poll_set(&events, 2, "Live listener thread epoll");
d3e2ba59
JD
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
3fd27398
MD
747 lttng_relay_notify_ready();
748
9b5e0863
MD
749 if (testpoint(relayd_thread_live_listener)) {
750 goto error_testpoint;
751 }
752
cd9adb8b 753 while (true) {
eea7556c
MD
754 health_code_update();
755
d3e2ba59
JD
756 DBG("Listener accepting live viewers connections");
757
28ab034a 758 restart:
eea7556c 759 health_poll_entry();
d3e2ba59 760 ret = lttng_poll_wait(&events, -1);
eea7556c 761 health_poll_exit();
d3e2ba59
JD
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 */
8a00688e
MJ
776 const auto revents = LTTNG_POLL_GETEV(&events, i);
777 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
778
779 health_code_update();
d3e2ba59 780
8a00688e
MJ
781 /* Activity on thread quit pipe, exiting. */
782 if (relayd_is_thread_quit_pipe(pollfd)) {
783 DBG("Activity on thread quit pipe");
d3e2ba59
JD
784 err = 0;
785 goto exit;
786 }
787
03e43155 788 if (revents & LPOLLIN) {
d3e2ba59 789 /*
7591bab1
MD
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.
d3e2ba59 794 */
58eb9381
DG
795 int val = 1;
796 struct relay_connection *new_conn;
d3e2ba59
JD
797 struct lttcomm_sock *newsock;
798
29555a78 799 newsock = accept_live_sock(live_control_sock,
28ab034a 800 "Live socket to client");
d3e2ba59
JD
801 if (!newsock) {
802 PERROR("accepting control sock");
d3e2ba59
JD
803 goto error;
804 }
805 DBG("Relay viewer connection accepted socket %d", newsock->fd);
58eb9381 806
28ab034a
JG
807 ret = setsockopt(
808 newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
d3e2ba59
JD
809 if (ret < 0) {
810 PERROR("setsockopt inet");
811 lttcomm_destroy_sock(newsock);
d3e2ba59
JD
812 goto error;
813 }
7591bab1
MD
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. */
cd9adb8b 820 newsock = nullptr;
d3e2ba59 821
58eb9381 822 /* Enqueue request for the dispatcher thread. */
ac497a37
SM
823 cds_wfcq_head_ptr_t head;
824 head.h = &viewer_conn_queue.head;
28ab034a 825 cds_wfcq_enqueue(head, &viewer_conn_queue.tail, &new_conn->qnode);
d3e2ba59
JD
826
827 /*
7591bab1
MD
828 * Wake the dispatch queue futex.
829 * Implicit memory barrier with the
830 * exchange in cds_wfcq_enqueue.
d3e2ba59 831 */
58eb9381 832 futex_nto1_wake(&viewer_conn_queue.futex);
03e43155
MD
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;
d3e2ba59
JD
839 }
840 }
841 }
842
843exit:
844error:
845error_poll_add:
9b5e0863 846error_testpoint:
23f940ff 847 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
d3e2ba59
JD
848error_create_poll:
849 if (live_control_sock->fd >= 0) {
8855795d
JG
850 int sock_fd = live_control_sock->fd;
851
28ab034a
JG
852 ret = fd_tracker_close_unsuspendable_fd(
853 the_fd_tracker, &sock_fd, 1, close_sock, live_control_sock);
d3e2ba59
JD
854 if (ret) {
855 PERROR("close");
856 }
8855795d 857 live_control_sock->fd = -1;
d3e2ba59
JD
858 }
859 lttcomm_destroy_sock(live_control_sock);
860error_sock_control:
861 if (err) {
eea7556c 862 health_error();
d3e2ba59
JD
863 DBG("Live viewer listener thread exited with error");
864 }
eea7556c 865 health_unregister(health_relayd);
8fba2b8d 866 rcu_unregister_thread();
d3e2ba59 867 DBG("Live viewer listener thread cleanup complete");
b4aacfdc
MD
868 if (lttng_relay_stop_threads()) {
869 ERR("Error stopping threads");
178a0557 870 }
cd9adb8b 871 return nullptr;
d3e2ba59
JD
872}
873
874/*
875 * This thread manages the dispatching of the requests to worker threads
876 */
28ab034a 877static void *thread_dispatcher(void *data __attribute__((unused)))
d3e2ba59 878{
6cd525e8
MD
879 int err = -1;
880 ssize_t ret;
8bdee6e2 881 struct cds_wfcq_node *node;
cd9adb8b 882 struct relay_connection *conn = nullptr;
d3e2ba59
JD
883
884 DBG("[thread] Live viewer relay dispatcher started");
885
eea7556c
MD
886 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
887
9b5e0863
MD
888 if (testpoint(relayd_thread_live_dispatcher)) {
889 goto error_testpoint;
890 }
891
eea7556c
MD
892 health_code_update();
893
0ed3b1a8 894 for (;;) {
eea7556c
MD
895 health_code_update();
896
d3e2ba59 897 /* Atomically prepare the queue futex */
58eb9381 898 futex_nto1_prepare(&viewer_conn_queue.futex);
d3e2ba59 899
0ed3b1a8
MD
900 if (CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
901 break;
902 }
903
d3e2ba59 904 do {
eea7556c
MD
905 health_code_update();
906
d3e2ba59 907 /* Dequeue commands */
8bdee6e2
SM
908 node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head,
909 &viewer_conn_queue.tail);
cd9adb8b 910 if (node == nullptr) {
d3e2ba59 911 DBG("Woken up but nothing in the live-viewer "
28ab034a 912 "relay command queue");
d3e2ba59
JD
913 /* Continue thread execution */
914 break;
915 }
0114db0e 916 conn = lttng::utils::container_of(node, &relay_connection::qnode);
28ab034a 917 DBG("Dispatching viewer request waiting on sock %d", conn->sock->fd);
d3e2ba59
JD
918
919 /*
7591bab1
MD
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 :)
d3e2ba59 924 */
5c7248cd
JG
925 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn)); /* NOLINT sizeof
926 used on a
927 pointer. */
58eb9381
DG
928 if (ret < 0) {
929 PERROR("write conn pipe");
7591bab1 930 connection_put(conn);
d3e2ba59
JD
931 goto error;
932 }
cd9adb8b 933 } while (node != nullptr);
d3e2ba59
JD
934
935 /* Futex wait on queue. Blocking call on futex() */
eea7556c 936 health_poll_entry();
58eb9381 937 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 938 health_poll_exit();
d3e2ba59
JD
939 }
940
eea7556c
MD
941 /* Normal exit, no error */
942 err = 0;
943
d3e2ba59 944error:
9b5e0863 945error_testpoint:
eea7556c
MD
946 if (err) {
947 health_error();
948 ERR("Health error occurred in %s", __func__);
949 }
950 health_unregister(health_relayd);
d3e2ba59 951 DBG("Live viewer dispatch thread dying");
b4aacfdc
MD
952 if (lttng_relay_stop_threads()) {
953 ERR("Error stopping threads");
178a0557 954 }
cd9adb8b 955 return nullptr;
d3e2ba59
JD
956}
957
958/*
959 * Establish connection with the viewer and check the versions.
960 *
961 * Return 0 on success or else negative value.
962 */
28ab034a 963static int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
964{
965 int ret;
966 struct lttng_viewer_connect reply, msg;
967
cd9adb8b 968 conn->version_check_done = true;
d3e2ba59 969
eea7556c
MD
970 health_code_update();
971
58eb9381 972 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 973 if (ret < 0) {
d3e2ba59
JD
974 goto end;
975 }
976
eea7556c
MD
977 health_code_update();
978
f46b2ce6 979 memset(&reply, 0, sizeof(reply));
d3e2ba59
JD
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)) {
2f8f53af 985 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
28ab034a
JG
986 reply.major,
987 be32toh(msg.major));
72180669 988 ret = -1;
d3e2ba59
JD
989 goto end;
990 }
991
58eb9381 992 conn->major = reply.major;
d3e2ba59
JD
993 /* We adapt to the lowest compatible version */
994 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 995 conn->minor = reply.minor;
d3e2ba59 996 } else {
58eb9381 997 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
998 }
999
c4e361a4 1000 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 1001 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 1002 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 1003 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
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);
58eb9381 1012 if (conn->type == RELAY_VIEWER_COMMAND) {
93b4787b 1013 /*
7591bab1
MD
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.
93b4787b 1017 */
7591bab1 1018 pthread_mutex_lock(&last_relay_viewer_session_id_lock);
93b4787b 1019 last_relay_viewer_session_id++;
7591bab1 1020 pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
93b4787b 1021 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
d3e2ba59 1022 }
eea7556c
MD
1023
1024 health_code_update();
1025
58eb9381 1026 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1027 if (ret < 0) {
2f8f53af 1028 goto end;
d3e2ba59
JD
1029 }
1030
eea7556c
MD
1031 health_code_update();
1032
58eb9381 1033 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
1034 ret = 0;
1035
1036end:
1037 return ret;
1038}
1039
1040/*
1041 * Send the viewer the list of current sessions.
7591bab1
MD
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.
d3e2ba59
JD
1045 *
1046 * Return 0 on success or else a negative value.
1047 */
28ab034a 1048static int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59 1049{
5f10c6b1 1050 int ret = 0;
d3e2ba59 1051 struct lttng_viewer_list_sessions session_list;
d3e2ba59 1052 struct lttng_ht_iter iter;
d3e2ba59 1053 struct relay_session *session;
cd9adb8b 1054 struct lttng_viewer_session *send_session_buf = nullptr;
7591bab1
MD
1055 uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
1056 uint32_t count = 0;
d3e2ba59 1057
64803277 1058 send_session_buf = calloc<lttng_viewer_session>(SESSION_BUF_DEFAULT_COUNT);
7591bab1
MD
1059 if (!send_session_buf) {
1060 return -1;
d3e2ba59
JD
1061 }
1062
56047f5a
JG
1063 {
1064 lttng::urcu::read_lock_guard read_lock;
d3e2ba59 1065
56047f5a
JG
1066 cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) {
1067 struct lttng_viewer_session *send_session;
eea7556c 1068
56047f5a
JG
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 }
7e0a4379 1076
56047f5a
JG
1077 if (count >= buf_count) {
1078 struct lttng_viewer_session *newbuf;
1079 uint32_t new_buf_count = buf_count << 1;
eea7556c 1080
56047f5a
JG
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))) {
7591bab1 1095 ret = -1;
d995f382 1096 goto break_loop;
7591bab1 1097 }
56047f5a
JG
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;
d227d5bd 1119 }
7591bab1 1120 }
56047f5a 1121
5f10c6b1
JG
1122 if (ret < 0) {
1123 goto end_free;
1124 }
d227d5bd 1125
7591bab1 1126 session_list.sessions_count = htobe32(count);
d227d5bd 1127
7591bab1 1128 health_code_update();
d227d5bd 1129
7591bab1
MD
1130 ret = send_response(conn->sock, &session_list, sizeof(session_list));
1131 if (ret < 0) {
1132 goto end_free;
d227d5bd 1133 }
d227d5bd 1134
7591bab1 1135 health_code_update();
d227d5bd 1136
28ab034a 1137 ret = send_response(conn->sock, send_session_buf, count * sizeof(*send_session_buf));
7591bab1
MD
1138 if (ret < 0) {
1139 goto end_free;
d227d5bd 1140 }
7591bab1 1141 health_code_update();
d227d5bd 1142
7591bab1
MD
1143 ret = 0;
1144end_free:
1145 free(send_session_buf);
1146 return ret;
d227d5bd
JD
1147}
1148
80e8027a 1149/*
7591bab1 1150 * Send the viewer the list of current streams.
80e8027a 1151 */
28ab034a 1152static int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
1153{
1154 int ret, send_streams = 0;
7591bab1 1155 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
80e8027a
JD
1156 struct lttng_viewer_new_streams_request request;
1157 struct lttng_viewer_new_streams_response response;
cd9adb8b 1158 struct relay_session *session = nullptr;
6763619c 1159 uint64_t session_id;
bddf80e4 1160 bool closed = false;
80e8027a 1161
a0377dfe 1162 LTTNG_ASSERT(conn);
80e8027a 1163
80e8027a
JD
1164 health_code_update();
1165
2f8f53af 1166 /* Receive the request from the connected client. */
58eb9381 1167 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1168 if (ret < 0) {
80e8027a
JD
1169 goto error;
1170 }
6763619c 1171 session_id = be64toh(request.session_id);
80e8027a
JD
1172
1173 health_code_update();
1174
53efb85a
MD
1175 memset(&response, 0, sizeof(response));
1176
7591bab1 1177 session = session_get_by_id(session_id);
2f8f53af 1178 if (!session) {
6763619c 1179 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 1180 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
1181 goto send_reply;
1182 }
1183
7591bab1 1184 if (!viewer_session_is_attached(conn->viewer_session, session)) {
c4e361a4 1185 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
1186 goto send_reply;
1187 }
1188
c876d657
JR
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 */
c06fdd95 1198 pthread_mutex_lock(&session->lock);
8cd15f6a
MD
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 */
fe88e517 1204 if (session_has_ongoing_rotation(session)) {
8cd15f6a
MD
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 }
b66a15d1 1209 ret = make_viewer_streams(session,
28ab034a
JG
1210 conn->viewer_session,
1211 LTTNG_VIEWER_SEEK_BEGINNING,
1212 &nb_total,
1213 &nb_unsent,
1214 &nb_created,
1215 &closed);
2f8f53af 1216 if (ret < 0) {
14e34907
JG
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);
0283afa9 1222 goto send_reply_unlock;
2f8f53af 1223 }
283a96e4
JG
1224
1225 uatomic_set(&session->new_streams, 0);
3d0bbd40
JG
1226 send_streams = 1;
1227 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
b66a15d1 1228
2f8f53af
DG
1229 /* Only send back the newly created streams with the unsent ones. */
1230 nb_streams = nb_created + nb_unsent;
80e8027a
JD
1231 response.streams_count = htobe32(nb_streams);
1232
4479f682 1233 /*
2229a09c
MD
1234 * If the session is closed, HUP when there are no more streams
1235 * with data.
4479f682 1236 */
bddf80e4 1237 if (closed && nb_total == 0) {
4479f682 1238 send_streams = 0;
bddf80e4 1239 response.streams_count = 0;
4479f682 1240 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
3d0bbd40 1241 goto send_reply_unlock;
4479f682 1242 }
3d0bbd40
JG
1243send_reply_unlock:
1244 pthread_mutex_unlock(&session->lock);
4479f682 1245
80e8027a
JD
1246send_reply:
1247 health_code_update();
58eb9381 1248 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 1249 if (ret < 0) {
7591bab1 1250 goto end_put_session;
80e8027a
JD
1251 }
1252 health_code_update();
1253
1254 /*
7591bab1
MD
1255 * Unknown or empty session, just return gracefully, the viewer
1256 * knows what is happening.
80e8027a
JD
1257 */
1258 if (!send_streams || !nb_streams) {
1259 ret = 0;
7591bab1 1260 goto end_put_session;
80e8027a
JD
1261 }
1262
2f8f53af 1263 /*
7591bab1
MD
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.
2f8f53af 1267 */
c06fdd95 1268 ret = send_viewer_streams(conn->sock, session_id, 0);
2f8f53af 1269 if (ret < 0) {
7591bab1 1270 goto end_put_session;
80e8027a
JD
1271 }
1272
7591bab1
MD
1273end_put_session:
1274 if (session) {
1275 session_put(session);
1276 }
80e8027a
JD
1277error:
1278 return ret;
1279}
1280
d3e2ba59
JD
1281/*
1282 * Send the viewer the list of current sessions.
1283 */
28ab034a 1284static int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1285{
2f8f53af
DG
1286 int send_streams = 0;
1287 ssize_t ret;
80e8027a 1288 uint32_t nb_streams = 0;
2f8f53af 1289 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1290 struct lttng_viewer_attach_session_request request;
1291 struct lttng_viewer_attach_session_response response;
cd9adb8b 1292 struct relay_session *session = nullptr;
dbd6665b 1293 enum lttng_viewer_attach_return_code viewer_attach_status;
bddf80e4 1294 bool closed = false;
c06fdd95 1295 uint64_t session_id;
d3e2ba59 1296
a0377dfe 1297 LTTNG_ASSERT(conn);
d3e2ba59 1298
eea7556c
MD
1299 health_code_update();
1300
2f8f53af 1301 /* Receive the request from the connected client. */
58eb9381 1302 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1303 if (ret < 0) {
d3e2ba59
JD
1304 goto error;
1305 }
1306
c06fdd95 1307 session_id = be64toh(request.session_id);
07d9fffa 1308
eea7556c
MD
1309 health_code_update();
1310
53efb85a
MD
1311 memset(&response, 0, sizeof(response));
1312
c3b7390b 1313 if (!conn->viewer_session) {
07d9fffa
FD
1314 viewer_attach_status = LTTNG_VIEWER_ATTACH_NO_SESSION;
1315 DBG("Client trying to attach before creating a live viewer session, returning status=%s",
28ab034a 1316 lttng_viewer_attach_return_code_str(viewer_attach_status));
c3b7390b
JD
1317 goto send_reply;
1318 }
1319
c06fdd95 1320 session = session_get_by_id(session_id);
2f8f53af 1321 if (!session) {
07d9fffa
FD
1322 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1323 DBG("Relay session %" PRIu64 " not found, returning status=%s",
28ab034a
JG
1324 session_id,
1325 lttng_viewer_attach_return_code_str(viewer_attach_status));
d3e2ba59
JD
1326 goto send_reply;
1327 }
07d9fffa 1328 DBG("Attach relay session ID %" PRIu64 " received", session_id);
d3e2ba59 1329
c06fdd95 1330 pthread_mutex_lock(&session->lock);
7591bab1 1331 if (session->live_timer == 0) {
07d9fffa
FD
1332 viewer_attach_status = LTTNG_VIEWER_ATTACH_NOT_LIVE;
1333 DBG("Relay session ID %" PRIu64 " is not a live session, returning status=%s",
28ab034a
JG
1334 session_id,
1335 lttng_viewer_attach_return_code_str(viewer_attach_status));
d3e2ba59 1336 goto send_reply;
7591bab1
MD
1337 }
1338
1339 send_streams = 1;
28ab034a 1340 viewer_attach_status = viewer_session_attach(conn->viewer_session, session);
dbd6665b 1341 if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) {
07d9fffa 1342 DBG("Error attaching to relay session %" PRIu64 ", returning status=%s",
28ab034a
JG
1343 session_id,
1344 lttng_viewer_attach_return_code_str(viewer_attach_status));
7591bab1 1345 goto send_reply;
d3e2ba59
JD
1346 }
1347
1348 switch (be32toh(request.seek)) {
c4e361a4
JD
1349 case LTTNG_VIEWER_SEEK_BEGINNING:
1350 case LTTNG_VIEWER_SEEK_LAST:
07d9fffa 1351 viewer_attach_status = LTTNG_VIEWER_ATTACH_OK;
ac497a37 1352 seek_type = (lttng_viewer_seek) be32toh(request.seek);
d3e2ba59
JD
1353 break;
1354 default:
28ab034a
JG
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));
07d9fffa 1358 viewer_attach_status = LTTNG_VIEWER_ATTACH_SEEK_ERR;
d3e2ba59
JD
1359 send_streams = 0;
1360 goto send_reply;
1361 }
1362
8cd15f6a
MD
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 */
fe88e517 1368 if (session_has_ongoing_rotation(session)) {
8cd15f6a
MD
1369 DBG("Relay session %" PRIu64 " rotation ongoing", session_id);
1370 send_streams = 0;
1371 goto send_reply;
1372 }
1373
28ab034a 1374 ret = make_viewer_streams(
cd9adb8b 1375 session, conn->viewer_session, seek_type, &nb_streams, nullptr, nullptr, &closed);
2f8f53af 1376 if (ret < 0) {
7591bab1 1377 goto end_put_session;
d3e2ba59 1378 }
c06fdd95
JG
1379 pthread_mutex_unlock(&session->lock);
1380 session_put(session);
cd9adb8b 1381 session = nullptr;
c06fdd95
JG
1382
1383 response.streams_count = htobe32(nb_streams);
bddf80e4
MD
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;
07d9fffa
FD
1393 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1394 ERR("Session %" PRIu64 " is closed, returning status=%s",
28ab034a
JG
1395 session_id,
1396 lttng_viewer_attach_return_code_str(viewer_attach_status));
bddf80e4
MD
1397 goto send_reply;
1398 }
1399
d3e2ba59 1400send_reply:
eea7556c 1401 health_code_update();
07d9fffa
FD
1402
1403 response.status = htobe32((uint32_t) viewer_attach_status);
1404
58eb9381 1405 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1406 if (ret < 0) {
7591bab1 1407 goto end_put_session;
d3e2ba59 1408 }
eea7556c 1409 health_code_update();
d3e2ba59
JD
1410
1411 /*
7591bab1
MD
1412 * Unknown or empty session, just return gracefully, the viewer
1413 * knows what is happening.
d3e2ba59 1414 */
157df586 1415 if (!send_streams || !nb_streams) {
d3e2ba59 1416 ret = 0;
7591bab1 1417 goto end_put_session;
d3e2ba59
JD
1418 }
1419
2f8f53af 1420 /* Send stream and ignore the sent flag. */
c06fdd95 1421 ret = send_viewer_streams(conn->sock, session_id, 1);
2f8f53af 1422 if (ret < 0) {
7591bab1 1423 goto end_put_session;
d3e2ba59 1424 }
d3e2ba59 1425
7591bab1
MD
1426end_put_session:
1427 if (session) {
c06fdd95 1428 pthread_mutex_unlock(&session->lock);
7591bab1
MD
1429 session_put(session);
1430 }
4a9daf17
JD
1431error:
1432 return ret;
1433}
1434
878c34cf
DG
1435/*
1436 * Open the index file if needed for the given vstream.
1437 *
f8f3885c
MD
1438 * If an index file is successfully opened, the vstream will set it as its
1439 * current index file.
878c34cf
DG
1440 *
1441 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
7591bab1
MD
1442 *
1443 * Called with rstream lock held.
878c34cf 1444 */
28ab034a 1445static int try_open_index(struct relay_viewer_stream *vstream, struct relay_stream *rstream)
878c34cf
DG
1446{
1447 int ret = 0;
ebb29c10
JG
1448 const uint32_t connection_major = rstream->trace->session->major;
1449 const uint32_t connection_minor = rstream->trace->session->minor;
3ff5c5db 1450 enum lttng_trace_chunk_status chunk_status;
878c34cf 1451
f8f3885c 1452 if (vstream->index_file) {
878c34cf
DG
1453 goto end;
1454 }
1455
1456 /*
7591bab1 1457 * First time, we open the index file and at least one index is ready.
878c34cf 1458 */
28ab034a 1459 if (rstream->index_received_seqcount == 0 || !vstream->stream_file.trace_chunk) {
878c34cf
DG
1460 ret = -ENOENT;
1461 goto end;
1462 }
80516611 1463
3ff5c5db 1464 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
28ab034a
JG
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);
3ff5c5db
MD
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 }
878c34cf
DG
1480 }
1481
1482end:
1483 return ret;
1484}
1485
1486/*
7591bab1
MD
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.
878c34cf 1494 *
7591bab1 1495 * Called with rstream lock held.
878c34cf
DG
1496 */
1497static int check_index_status(struct relay_viewer_stream *vstream,
28ab034a
JG
1498 struct relay_stream *rstream,
1499 struct ctf_trace *trace,
1500 struct lttng_viewer_index *index)
878c34cf
DG
1501{
1502 int ret;
1503
68c40154 1504 DBG("Check index status: index_received_seqcount %" PRIu64 " "
28ab034a
JG
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) {
797bc362
MD
1512 /*
1513 * Last index sent and session connection or relay
1514 * stream are closed.
1515 */
7e4fb89b
FD
1516 index->status = LTTNG_VIEWER_INDEX_HUP;
1517 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
28ab034a
JG
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));
7591bab1
MD
1524 goto hup;
1525 } else if (rstream->beacon_ts_end != -1ULL &&
28ab034a
JG
1526 (rstream->index_received_seqcount == 0 ||
1527 (vstream->index_sent_seqcount != 0 &&
1528 rstream->index_received_seqcount <= vstream->index_sent_seqcount))) {
7591bab1
MD
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).
b0d240a2
MD
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.
7591bab1 1543 */
7e4fb89b 1544 index->status = LTTNG_VIEWER_INDEX_INACTIVE;
7591bab1
MD
1545 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1546 index->stream_id = htobe64(rstream->ctf_stream_id);
7e4fb89b 1547 DBG("Check index status: inactive with beacon, for stream %" PRIu64
28ab034a
JG
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));
7591bab1 1552 goto index_ready;
b0d240a2 1553 } else if (rstream->index_received_seqcount == 0 ||
28ab034a
JG
1554 (vstream->index_sent_seqcount != 0 &&
1555 rstream->index_received_seqcount <= vstream->index_sent_seqcount)) {
7591bab1 1556 /*
b0d240a2 1557 * This checks whether received <= sent seqcount. In
a44ca2ca
MD
1558 * this case, we have not received a beacon. Therefore,
1559 * we can only ask the client to retry later.
b0d240a2
MD
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.
7591bab1 1566 */
7e4fb89b
FD
1567 index->status = LTTNG_VIEWER_INDEX_RETRY;
1568 DBG("Check index status:"
28ab034a
JG
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));
7591bab1 1573 goto index_ready;
a44ca2ca 1574 } else if (!tracefile_array_seq_in_file(rstream->tfa,
28ab034a
JG
1575 vstream->current_tracefile_id,
1576 vstream->index_sent_seqcount)) {
7591bab1 1577 /*
a44ca2ca
MD
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.
7591bab1 1581 */
28ab034a 1582 DBG("Viewer stream %" PRIu64 " rotation", vstream->stream->stream_handle);
7591bab1 1583 ret = viewer_stream_rotate(vstream);
b0d240a2 1584 if (ret == 1) {
7591bab1 1585 /* EOF across entire stream. */
7e4fb89b
FD
1586 index->status = LTTNG_VIEWER_INDEX_HUP;
1587 DBG("Check index status:"
28ab034a
JG
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));
7591bab1
MD
1592 goto hup;
1593 }
7591bab1 1594 /*
a44ca2ca
MD
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.
7591bab1 1607 */
a44ca2ca 1608 if (rstream->tracefile_count == 1 &&
28ab034a
JG
1609 !tracefile_array_seq_in_file(rstream->tfa,
1610 vstream->current_tracefile_id,
1611 vstream->index_sent_seqcount)) {
7e4fb89b
FD
1612 index->status = LTTNG_VIEWER_INDEX_RETRY;
1613 DBG("Check index status:"
28ab034a
JG
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));
a44ca2ca 1620 goto index_ready;
878c34cf 1621 }
28ab034a
JG
1622 LTTNG_ASSERT(tracefile_array_seq_in_file(
1623 rstream->tfa, vstream->current_tracefile_id, vstream->index_sent_seqcount));
878c34cf 1624 }
a44ca2ca
MD
1625 /* ret == 0 means successful so we continue. */
1626 ret = 0;
878c34cf
DG
1627 return ret;
1628
1629hup:
7591bab1 1630 viewer_stream_put(vstream);
878c34cf
DG
1631index_ready:
1632 return 1;
1633}
1634
28ab034a
JG
1635static void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream *vstream,
1636 struct lttng_trace_chunk *new_trace_chunk)
80516611
JG
1637{
1638 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
1639
1640 if (new_trace_chunk) {
28ab034a 1641 const bool acquired_reference = lttng_trace_chunk_get(new_trace_chunk);
80516611 1642
a0377dfe 1643 LTTNG_ASSERT(acquired_reference);
80516611
JG
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
d3e2ba59
JD
1651/*
1652 * Send the next index for a stream.
1653 *
1654 * Return 0 on success or else a negative value.
1655 */
28ab034a 1656static int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1657{
1658 int ret;
1659 struct lttng_viewer_get_next_index request_index;
1660 struct lttng_viewer_index viewer_index;
50adc264 1661 struct ctf_packet_index packet_index;
cd9adb8b
JG
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;
bb1dcf01
MD
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;
283a96e4 1669 bool attached_sessions_have_new_streams = false;
d3e2ba59 1670
a0377dfe 1671 LTTNG_ASSERT(conn);
d3e2ba59 1672
7591bab1 1673 memset(&viewer_index, 0, sizeof(viewer_index));
eea7556c 1674 health_code_update();
2f8f53af 1675
58eb9381 1676 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1677 if (ret < 0) {
d3e2ba59
JD
1678 goto end;
1679 }
eea7556c 1680 health_code_update();
d3e2ba59 1681
7591bab1 1682 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
6763619c 1683 if (!vstream) {
7e4fb89b 1684 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
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));
f1883937 1689 goto send_reply;
2a174661
DG
1690 }
1691
7591bab1
MD
1692 /* Use back. ref. Protected by refcounts. */
1693 rstream = vstream->stream;
1694 ctf_trace = rstream->trace;
d3e2ba59 1695
7591bab1 1696 /* metadata_viewer_stream may be NULL. */
28ab034a 1697 metadata_viewer_stream = ctf_trace_get_viewer_metadata_stream(ctf_trace);
2a174661 1698
b6ae2a95
MD
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);
7591bab1 1706 pthread_mutex_lock(&rstream->lock);
d3e2ba59
JD
1707
1708 /*
1709 * The viewer should not ask for index on metadata stream.
1710 */
7591bab1 1711 if (rstream->is_metadata) {
7e4fb89b 1712 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
28ab034a
JG
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));
d3e2ba59
JD
1718 goto send_reply;
1719 }
1720
283a96e4
JG
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
b0d240a2
MD
1732 if (rstream->ongoing_rotation.is_set) {
1733 /* Rotation is ongoing, try again later. */
7e4fb89b 1734 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
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));
b0d240a2
MD
1740 goto send_reply;
1741 }
1742
fe88e517 1743 if (session_has_ongoing_rotation(rstream->trace->session)) {
b0d240a2 1744 /* Rotation is ongoing, try again later. */
7e4fb89b 1745 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
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));
b0d240a2
MD
1751 goto send_reply;
1752 }
1753
80516611
JG
1754 /*
1755 * Transition the viewer session into the newest trace chunk available.
1756 */
28ab034a
JG
1757 if (!lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
1758 rstream->trace_chunk)) {
9a9c8637 1759 DBG("Relay stream and viewer chunk ids differ");
b0d240a2 1760
28ab034a
JG
1761 ret = viewer_session_set_trace_chunk_copy(conn->viewer_session,
1762 rstream->trace_chunk);
ad8bec24 1763 if (ret) {
7e4fb89b
FD
1764 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1765 ERR("Error copying trace chunk for stream id %" PRIu64
28ab034a
JG
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));
b0d240a2
MD
1770 goto send_reply;
1771 }
b0d240a2 1772 }
b0d240a2 1773
80516611
JG
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 */
bb1dcf01 1785 if (vstream->stream_file.trace_chunk) {
28ab034a
JG
1786 status = lttng_trace_chunk_get_id(vstream->stream_file.trace_chunk,
1787 &stream_file_chunk_id);
bb1dcf01
MD
1788 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1789 }
1790 if (conn->viewer_session->current_trace_chunk) {
28ab034a
JG
1791 status = lttng_trace_chunk_get_id(conn->viewer_session->current_trace_chunk,
1792 &viewer_session_chunk_id);
bb1dcf01
MD
1793 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1794 }
1795
1796 viewer_stream_and_session_in_same_chunk = lttng_trace_chunk_ids_equal(
28ab034a 1797 conn->viewer_session->current_trace_chunk, vstream->stream_file.trace_chunk);
bb1dcf01 1798 viewer_stream_one_rotation_behind = rstream->completed_rotation_count ==
28ab034a 1799 vstream->last_seen_rotation_count + 1;
bb1dcf01
MD
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",
28ab034a
JG
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");
bb1dcf01
MD
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",
28ab034a
JG
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");
bb1dcf01
MD
1817 } else {
1818 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
28ab034a
JG
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");
bb1dcf01 1825
80516611 1826 viewer_stream_rotate_to_trace_chunk(vstream,
28ab034a
JG
1827 conn->viewer_session->current_trace_chunk);
1828 vstream->last_seen_rotation_count = rstream->completed_rotation_count;
d3e2ba59
JD
1829 }
1830
878c34cf 1831 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
878c34cf 1832 if (ret < 0) {
7591bab1 1833 goto error_put;
878c34cf
DG
1834 } else if (ret == 1) {
1835 /*
7591bab1
MD
1836 * We have no index to send and check_index_status has populated
1837 * viewer_index's status.
878c34cf 1838 */
d3e2ba59
JD
1839 goto send_reply;
1840 }
283a96e4 1841
7591bab1 1842 /* At this point, ret is 0 thus we will be able to read the index. */
a0377dfe 1843 LTTNG_ASSERT(!ret);
d3e2ba59 1844
b0d240a2
MD
1845 /* Try to open an index if one is needed for that stream. */
1846 ret = try_open_index(vstream, rstream);
1847 if (ret == -ENOENT) {
28ab034a 1848 if (rstream->closed) {
7e4fb89b
FD
1849 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1850 DBG("Cannot open index for stream id %" PRIu64
28ab034a
JG
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));
b0d240a2 1855 goto send_reply;
28ab034a 1856 } else {
7e4fb89b 1857 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
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));
b0d240a2 1862 goto send_reply;
28ab034a 1863 }
b0d240a2
MD
1864 }
1865 if (ret < 0) {
7e4fb89b 1866 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
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));
b0d240a2
MD
1871 goto send_reply;
1872 }
1873
7591bab1
MD
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 */
8bb66c3c 1881 if (!vstream->stream_file.handle) {
ebb29c10 1882 char file_path[LTTNG_PATH_MAX];
8bb66c3c 1883 struct fs_handle *fs_handle;
ebb29c10
JG
1884
1885 ret = utils_stream_file_path(rstream->path_name,
28ab034a
JG
1886 rstream->channel_name,
1887 rstream->tracefile_size,
1888 vstream->current_tracefile_id,
cd9adb8b 1889 nullptr,
28ab034a
JG
1890 file_path,
1891 sizeof(file_path));
7591bab1
MD
1892 if (ret < 0) {
1893 goto error_put;
1894 }
ebb29c10 1895
3ff5c5db
MD
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 */
8bb66c3c 1901 status = lttng_trace_chunk_open_fs_handle(
28ab034a 1902 vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true);
ebb29c10 1903 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
28ab034a 1904 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE && rstream->closed) {
7e4fb89b
FD
1905 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1906 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
28ab034a
JG
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));
b0d240a2
MD
1912 goto send_reply;
1913 }
ebb29c10 1914 PERROR("Failed to open trace file for viewer stream");
7591bab1
MD
1915 goto error_put;
1916 }
8bb66c3c 1917 vstream->stream_file.handle = fs_handle;
d3e2ba59
JD
1918 }
1919
f8f3885c
MD
1920 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1921 if (ret) {
7e4fb89b 1922 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
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));
6b6b9a5a 1927 goto send_reply;
d3e2ba59 1928 } else {
7e4fb89b 1929 viewer_index.status = LTTNG_VIEWER_INDEX_OK;
28ab034a
JG
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));
a44ca2ca 1934 vstream->index_sent_seqcount++;
d3e2ba59
JD
1935 }
1936
1937 /*
1938 * Indexes are stored in big endian, no need to switch before sending.
1939 */
7591bab1 1940 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
28ab034a
JG
1941 rstream->stream_handle,
1942 (uint64_t) be64toh(packet_index.offset));
d3e2ba59
JD
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
1951send_reply:
f1883937
MD
1952 if (rstream) {
1953 pthread_mutex_unlock(&rstream->lock);
b6ae2a95 1954 pthread_mutex_unlock(&rstream->trace->session->lock);
f1883937 1955 }
7591bab1
MD
1956
1957 if (metadata_viewer_stream) {
1958 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
28ab034a
JG
1959 DBG("get next index metadata check: recv %" PRIu64 " sent %" PRIu64,
1960 metadata_viewer_stream->stream->metadata_received,
1961 metadata_viewer_stream->metadata_sent);
7591bab1 1962 if (!metadata_viewer_stream->stream->metadata_received ||
28ab034a
JG
1963 metadata_viewer_stream->stream->metadata_received >
1964 metadata_viewer_stream->metadata_sent) {
7591bab1
MD
1965 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1966 }
1967 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1968 }
1969
283a96e4
JG
1970 if (attached_sessions_have_new_streams) {
1971 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1972 }
1973
d3e2ba59 1974 viewer_index.flags = htobe32(viewer_index.flags);
7e4fb89b 1975 viewer_index.status = htobe32(viewer_index.status);
eea7556c 1976 health_code_update();
2f8f53af 1977
58eb9381 1978 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1979 if (ret < 0) {
7591bab1 1980 goto end;
d3e2ba59 1981 }
eea7556c 1982 health_code_update();
d3e2ba59 1983
9237e6a1
MD
1984 if (vstream) {
1985 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
28ab034a
JG
1986 vstream->index_sent_seqcount,
1987 vstream->stream->stream_handle);
9237e6a1 1988 }
d3e2ba59 1989end:
7591bab1
MD
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
1998error_put:
1999 pthread_mutex_unlock(&rstream->lock);
b6ae2a95 2000 pthread_mutex_unlock(&rstream->trace->session->lock);
7591bab1
MD
2001 if (metadata_viewer_stream) {
2002 viewer_stream_put(metadata_viewer_stream);
2003 }
2004 viewer_stream_put(vstream);
d3e2ba59
JD
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 */
28ab034a 2013static int viewer_get_packet(struct relay_connection *conn)
d3e2ba59 2014{
b6025e94 2015 int ret;
a5df8828 2016 off_t lseek_ret;
cd9adb8b 2017 char *reply = nullptr;
d3e2ba59 2018 struct lttng_viewer_get_packet get_packet_info;
553b2adb 2019 struct lttng_viewer_trace_packet reply_header;
cd9adb8b 2020 struct relay_viewer_stream *vstream = nullptr;
553b2adb 2021 uint32_t reply_size = sizeof(reply_header);
b6025e94
JR
2022 uint32_t packet_data_len = 0;
2023 ssize_t read_len;
8bb66c3c 2024 uint64_t stream_id;
556d5fae 2025 enum lttng_viewer_get_packet_return_code get_packet_status;
d3e2ba59 2026
eea7556c 2027 health_code_update();
2f8f53af 2028
28ab034a 2029 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
2f8f53af 2030 if (ret < 0) {
d3e2ba59
JD
2031 goto end;
2032 }
eea7556c 2033 health_code_update();
d3e2ba59 2034
0233a6a5 2035 /* From this point on, the error label can be reached. */
553b2adb 2036 memset(&reply_header, 0, sizeof(reply_header));
8bb66c3c 2037 stream_id = (uint64_t) be64toh(get_packet_info.stream_id);
0233a6a5 2038
8bb66c3c 2039 vstream = viewer_stream_get_by_id(stream_id);
7591bab1 2040 if (!vstream) {
556d5fae 2041 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
28ab034a
JG
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));
553b2adb 2045 goto send_reply_nolock;
b6025e94
JR
2046 } else {
2047 packet_data_len = be32toh(get_packet_info.len);
553b2adb 2048 reply_size += packet_data_len;
d3e2ba59 2049 }
2a174661 2050
64803277 2051 reply = zmalloc<char>(reply_size);
553b2adb 2052 if (!reply) {
556d5fae
FD
2053 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2054 PERROR("Falled to allocate reply, returning status=%s",
28ab034a 2055 lttng_viewer_get_packet_return_code_str(get_packet_status));
d3e2ba59
JD
2056 goto error;
2057 }
2058
b6025e94 2059 pthread_mutex_lock(&vstream->stream->lock);
28ab034a
JG
2060 lseek_ret = fs_handle_seek(
2061 vstream->stream_file.handle, be64toh(get_packet_info.offset), SEEK_SET);
a5df8828 2062 if (lseek_ret < 0) {
556d5fae 2063 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
8bb66c3c 2064 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
28ab034a
JG
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));
7591bab1 2069 goto error;
d3e2ba59 2070 }
28ab034a
JG
2071 read_len = fs_handle_read(
2072 vstream->stream_file.handle, reply + sizeof(reply_header), packet_data_len);
b6025e94 2073 if (read_len < packet_data_len) {
556d5fae 2074 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
8bb66c3c 2075 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
28ab034a
JG
2076 ", offset: %" PRIu64 ", returning status=%s",
2077 stream_id,
556d5fae 2078 (uint64_t) be64toh(get_packet_info.offset),
28ab034a 2079 lttng_viewer_get_packet_return_code_str(get_packet_status));
7591bab1 2080 goto error;
d3e2ba59 2081 }
556d5fae
FD
2082
2083 get_packet_status = LTTNG_VIEWER_GET_PACKET_OK;
553b2adb 2084 reply_header.len = htobe32(packet_data_len);
d3e2ba59
JD
2085 goto send_reply;
2086
2087error:
f28ecb10
JG
2088 /* No payload to send on error. */
2089 reply_size = sizeof(reply_header);
d3e2ba59
JD
2090
2091send_reply:
7591bab1
MD
2092 if (vstream) {
2093 pthread_mutex_unlock(&vstream->stream->lock);
2094 }
2095send_reply_nolock:
eea7556c
MD
2096
2097 health_code_update();
2f8f53af 2098
556d5fae 2099 reply_header.status = htobe32(get_packet_status);
553b2adb
JG
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. */
28ab034a 2105 ret = send_response(conn->sock, &reply_header, reply_size);
553b2adb
JG
2106 }
2107
b6025e94 2108 health_code_update();
d3e2ba59 2109 if (ret < 0) {
b6025e94 2110 PERROR("sendmsg of packet data failed");
7591bab1 2111 goto end_free;
d3e2ba59 2112 }
d3e2ba59 2113
8bb66c3c 2114 DBG("Sent %u bytes for stream %" PRIu64, reply_size, stream_id);
d3e2ba59 2115
7591bab1 2116end_free:
553b2adb 2117 free(reply);
d3e2ba59 2118end:
7591bab1
MD
2119 if (vstream) {
2120 viewer_stream_put(vstream);
2121 }
d3e2ba59
JD
2122 return ret;
2123}
2124
2125/*
2126 * Send the session's metadata
2127 *
2128 * Return 0 on success else a negative value.
2129 */
28ab034a 2130static int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
2131{
2132 int ret = 0;
8bb66c3c 2133 int fd = -1;
d3e2ba59
JD
2134 ssize_t read_len;
2135 uint64_t len = 0;
cd9adb8b 2136 char *data = nullptr;
d3e2ba59
JD
2137 struct lttng_viewer_get_metadata request;
2138 struct lttng_viewer_metadata_packet reply;
cd9adb8b 2139 struct relay_viewer_stream *vstream = nullptr;
55d3f5e5 2140 bool dispose_of_stream = false;
d3e2ba59 2141
a0377dfe 2142 LTTNG_ASSERT(conn);
d3e2ba59 2143
eea7556c 2144 health_code_update();
2f8f53af 2145
58eb9381 2146 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 2147 if (ret < 0) {
d3e2ba59
JD
2148 goto end;
2149 }
eea7556c 2150 health_code_update();
d3e2ba59 2151
53efb85a
MD
2152 memset(&reply, 0, sizeof(reply));
2153
7591bab1
MD
2154 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
2155 if (!vstream) {
3b463131
MD
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 */
a44ca2ca 2164 DBG("Client requested metadata of unknown stream id %" PRIu64,
28ab034a 2165 (uint64_t) be64toh(request.stream_id));
3b463131 2166 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
7591bab1 2167 goto send_reply;
d3e2ba59 2168 }
55d3f5e5
JG
2169
2170 pthread_mutex_lock(&vstream->stream->trace->session->lock);
2171 pthread_mutex_lock(&vstream->stream->trace->lock);
7591bab1
MD
2172 pthread_mutex_lock(&vstream->stream->lock);
2173 if (!vstream->stream->is_metadata) {
2174 ERR("Invalid metadata stream");
6763619c
JD
2175 goto error;
2176 }
2177
b0d240a2 2178 if (vstream->metadata_sent >= vstream->stream->metadata_received) {
94f73d08 2179 /*
0359d56c 2180 * Clear feature resets the metadata_received to 0 until the
b0d240a2 2181 * same metadata is received again.
94f73d08 2182 */
c4e361a4 2183 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
94f73d08
MD
2184 /*
2185 * The live viewer considers a closed 0 byte metadata stream as
2186 * an error.
2187 */
55d3f5e5 2188 dispose_of_stream = vstream->metadata_sent > 0 && vstream->stream->closed;
d3e2ba59
JD
2189 goto send_reply;
2190 }
2191
ad8bec24 2192 if (vstream->stream->trace_chunk &&
28ab034a
JG
2193 !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
2194 vstream->stream->trace_chunk)) {
ad8bec24
JG
2195 /* A rotation has occurred on the relay stream. */
2196 DBG("Metadata relay stream and viewer chunk ids differ");
2197
28ab034a
JG
2198 ret = viewer_session_set_trace_chunk_copy(conn->viewer_session,
2199 vstream->stream->trace_chunk);
ad8bec24
JG
2200 if (ret) {
2201 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2202 goto send_reply;
2203 }
2204 }
2205
87250ba1 2206 if (conn->viewer_session->current_trace_chunk &&
28ab034a
JG
2207 !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
2208 vstream->stream_file.trace_chunk)) {
ad8bec24
JG
2209 bool acquired_reference;
2210
2211 DBG("Viewer session and viewer stream chunk differ: "
28ab034a
JG
2212 "vsession chunk %p vstream chunk %p",
2213 conn->viewer_session->current_trace_chunk,
2214 vstream->stream_file.trace_chunk);
ad8bec24 2215 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
28ab034a
JG
2216 acquired_reference =
2217 lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk);
a0377dfe 2218 LTTNG_ASSERT(acquired_reference);
28ab034a 2219 vstream->stream_file.trace_chunk = conn->viewer_session->current_trace_chunk;
ad8bec24
JG
2220 viewer_stream_close_files(vstream);
2221 }
2222
b0d240a2
MD
2223 len = vstream->stream->metadata_received - vstream->metadata_sent;
2224
87250ba1 2225 if (!vstream->stream_file.trace_chunk) {
55d3f5e5
JG
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
87250ba1
JG
2239 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2240 len = 0;
2241 goto send_reply;
28ab034a 2242 } else if (vstream->stream_file.trace_chunk && !vstream->stream_file.handle && len > 0) {
87250ba1
JG
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 */
8bb66c3c 2247 struct fs_handle *fs_handle;
ebb29c10
JG
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,
28ab034a
JG
2253 rstream->channel_name,
2254 rstream->tracefile_size,
2255 vstream->current_tracefile_id,
cd9adb8b 2256 nullptr,
28ab034a
JG
2257 file_path,
2258 sizeof(file_path));
d3e2ba59
JD
2259 if (ret < 0) {
2260 goto error;
2261 }
ebb29c10 2262
3ff5c5db
MD
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 */
8bb66c3c 2268 status = lttng_trace_chunk_open_fs_handle(
28ab034a 2269 vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true);
ebb29c10 2270 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
b0d240a2
MD
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 }
ebb29c10 2279 PERROR("Failed to open metadata file for viewer stream");
d3e2ba59
JD
2280 goto error;
2281 }
8bb66c3c 2282 vstream->stream_file.handle = fs_handle;
ad8bec24
JG
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 */
28ab034a
JG
2300 const off_t seek_ret =
2301 fs_handle_seek(fs_handle, vstream->metadata_sent, SEEK_SET);
ad8bec24
JG
2302
2303 if (seek_ret < 0) {
2304 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64,
28ab034a 2305 vstream->metadata_sent);
ad8bec24
JG
2306 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2307 goto send_reply;
2308 }
2309 }
d3e2ba59
JD
2310 }
2311
2312 reply.len = htobe64(len);
64803277 2313 data = zmalloc<char>(len);
d3e2ba59
JD
2314 if (!data) {
2315 PERROR("viewer metadata zmalloc");
2316 goto error;
2317 }
2318
8bb66c3c
JG
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;
6cd525e8 2327 if (read_len < len) {
ad8bec24
JG
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 */
28ab034a
JG
2342 const off_t seek_ret =
2343 fs_handle_seek(vstream->stream_file.handle, -read_len, SEEK_CUR);
ad8bec24 2344
cfa1e2c2 2345 DBG("Failed to read metadata: requested = %" PRIu64 ", got = %zd",
28ab034a
JG
2346 len,
2347 read_len);
ad8bec24
JG
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 }
d3e2ba59 2356 }
7591bab1 2357 vstream->metadata_sent += read_len;
c4e361a4 2358 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
7591bab1 2359
d3e2ba59
JD
2360 goto send_reply;
2361
2362error:
c4e361a4 2363 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
2364
2365send_reply:
eea7556c 2366 health_code_update();
7591bab1
MD
2367 if (vstream) {
2368 pthread_mutex_unlock(&vstream->stream->lock);
55d3f5e5
JG
2369 pthread_mutex_unlock(&vstream->stream->trace->lock);
2370 pthread_mutex_unlock(&vstream->stream->trace->session->lock);
7591bab1 2371 }
58eb9381 2372 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 2373 if (ret < 0) {
7591bab1 2374 goto end_free;
d3e2ba59 2375 }
eea7556c 2376 health_code_update();
d3e2ba59
JD
2377
2378 if (len > 0) {
58eb9381 2379 ret = send_response(conn->sock, data, len);
d3e2ba59 2380 if (ret < 0) {
7591bab1 2381 goto end_free;
d3e2ba59
JD
2382 }
2383 }
2384
28ab034a
JG
2385 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64,
2386 len,
2387 (uint64_t) be64toh(request.stream_id));
d3e2ba59
JD
2388
2389 DBG("Metadata sent");
2390
7591bab1 2391end_free:
d3e2ba59 2392 free(data);
d3e2ba59 2393end:
7591bab1
MD
2394 if (vstream) {
2395 viewer_stream_put(vstream);
55d3f5e5
JG
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 }
7591bab1 2410 }
55d3f5e5 2411
d3e2ba59
JD
2412 return ret;
2413}
2414
c3b7390b
JD
2415/*
2416 * Create a viewer session.
2417 *
2418 * Return 0 on success or else a negative value.
2419 */
28ab034a 2420static int viewer_create_session(struct relay_connection *conn)
c3b7390b
JD
2421{
2422 int ret;
2423 struct lttng_viewer_create_session_response resp;
2424
53efb85a 2425 memset(&resp, 0, sizeof(resp));
c3b7390b 2426 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
7591bab1 2427 conn->viewer_session = viewer_session_create();
c3b7390b
JD
2428 if (!conn->viewer_session) {
2429 ERR("Allocation viewer session");
2430 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
2431 goto send_reply;
2432 }
c3b7390b
JD
2433
2434send_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
2443end:
2444 return ret;
2445}
2446
d62023be
JD
2447/*
2448 * Detach a viewer session.
2449 *
2450 * Return 0 on success or else a negative value.
2451 */
28ab034a 2452static int viewer_detach_session(struct relay_connection *conn)
d62023be
JD
2453{
2454 int ret;
2455 struct lttng_viewer_detach_session_response response;
2456 struct lttng_viewer_detach_session_request request;
cd9adb8b 2457 struct relay_session *session = nullptr;
d62023be
JD
2458 uint64_t viewer_session_to_close;
2459
a0377dfe 2460 LTTNG_ASSERT(conn);
d62023be
JD
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) {
28ab034a 2484 DBG("Relay session %" PRIu64 " not found", (uint64_t) be64toh(request.session_id));
d62023be
JD
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
2500send_reply_put:
2501 session_put(session);
2502
2503send_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
2512end:
2513 return ret;
2514}
c3b7390b 2515
d3e2ba59
JD
2516/*
2517 * live_relay_unknown_command: send -1 if received unknown command
2518 */
28ab034a 2519static void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
2520{
2521 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 2522
53efb85a 2523 memset(&reply, 0, sizeof(reply));
d3e2ba59 2524 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 2525 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
2526}
2527
2528/*
2529 * Process the commands received on the control socket
2530 */
28ab034a 2531static int process_control(struct lttng_viewer_cmd *recv_hdr, struct relay_connection *conn)
d3e2ba59
JD
2532{
2533 int ret = 0;
28ab034a 2534 lttng_viewer_command cmd = (lttng_viewer_command) be32toh(recv_hdr->cmd);
2f8f53af
DG
2535
2536 /*
7201079f
FD
2537 * Make sure we've done the version check before any command other then
2538 * a new client connection.
2f8f53af 2539 */
7201079f
FD
2540 if (cmd != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
2541 ERR("Viewer on connection %d requested %s command before version check",
28ab034a
JG
2542 conn->sock->fd,
2543 lttng_viewer_command_str(cmd));
2f8f53af
DG
2544 ret = -1;
2545 goto end;
2546 }
d3e2ba59 2547
7201079f 2548 DBG("Processing %s viewer command from connection %d",
28ab034a
JG
2549 lttng_viewer_command_str(cmd),
2550 conn->sock->fd);
7201079f
FD
2551
2552 switch (cmd) {
c4e361a4 2553 case LTTNG_VIEWER_CONNECT:
58eb9381 2554 ret = viewer_connect(conn);
d3e2ba59 2555 break;
c4e361a4 2556 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 2557 ret = viewer_list_sessions(conn);
d3e2ba59 2558 break;
c4e361a4 2559 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 2560 ret = viewer_attach_session(conn);
d3e2ba59 2561 break;
c4e361a4 2562 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 2563 ret = viewer_get_next_index(conn);
d3e2ba59 2564 break;
c4e361a4 2565 case LTTNG_VIEWER_GET_PACKET:
58eb9381 2566 ret = viewer_get_packet(conn);
d3e2ba59 2567 break;
c4e361a4 2568 case LTTNG_VIEWER_GET_METADATA:
58eb9381 2569 ret = viewer_get_metadata(conn);
d3e2ba59 2570 break;
c4e361a4 2571 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 2572 ret = viewer_get_new_streams(conn);
80e8027a 2573 break;
c3b7390b
JD
2574 case LTTNG_VIEWER_CREATE_SESSION:
2575 ret = viewer_create_session(conn);
2576 break;
d62023be
JD
2577 case LTTNG_VIEWER_DETACH_SESSION:
2578 ret = viewer_detach_session(conn);
2579 break;
d3e2ba59 2580 default:
28ab034a 2581 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
58eb9381 2582 live_relay_unknown_command(conn);
d3e2ba59
JD
2583 ret = -1;
2584 goto end;
2585 }
2586
2587end:
2588 return ret;
2589}
2590
28ab034a 2591static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
2592{
2593 int ret;
2594
58eb9381 2595 (void) lttng_poll_del(events, pollfd);
d3e2ba59 2596
28ab034a 2597 ret = fd_tracker_close_unsuspendable_fd(
cd9adb8b 2598 the_fd_tracker, &pollfd, 1, fd_tracker_util_close_fd, nullptr);
d3e2ba59
JD
2599 if (ret < 0) {
2600 ERR("Closing pollfd %d", pollfd);
2601 }
2602}
2603
d3e2ba59
JD
2604/*
2605 * This thread does the actual work
2606 */
28ab034a 2607static void *thread_worker(void *data __attribute__((unused)))
d3e2ba59
JD
2608{
2609 int ret, err = -1;
2610 uint32_t nb_fd;
d3e2ba59 2611 struct lttng_poll_event events;
7591bab1 2612 struct lttng_ht *viewer_connections_ht;
d3e2ba59
JD
2613 struct lttng_ht_iter iter;
2614 struct lttng_viewer_cmd recv_hdr;
302d8906 2615 struct relay_connection *destroy_conn;
d3e2ba59
JD
2616
2617 DBG("[thread] Live viewer relay worker started");
2618
2619 rcu_register_thread();
2620
eea7556c
MD
2621 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
2622
9b5e0863
MD
2623 if (testpoint(relayd_thread_live_worker)) {
2624 goto error_testpoint;
2625 }
2626
d3e2ba59 2627 /* table of connections indexed on socket */
7591bab1
MD
2628 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2629 if (!viewer_connections_ht) {
2630 goto viewer_connections_ht_error;
d3e2ba59
JD
2631 }
2632
28ab034a 2633 ret = create_named_thread_poll_set(&events, 2, "Live viewer worker thread epoll");
d3e2ba59
JD
2634 if (ret < 0) {
2635 goto error_poll_create;
2636 }
2637
58eb9381 2638 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
2639 if (ret < 0) {
2640 goto error;
2641 }
2642
2643restart:
cd9adb8b 2644 while (true) {
d3e2ba59
JD
2645 int i;
2646
eea7556c
MD
2647 health_code_update();
2648
d3e2ba59
JD
2649 /* Infinite blocking call, waiting for transmission */
2650 DBG3("Relayd live viewer worker thread polling...");
eea7556c 2651 health_poll_entry();
d3e2ba59 2652 ret = lttng_poll_wait(&events, -1);
eea7556c 2653 health_poll_exit();
d3e2ba59
JD
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 */
8a00688e
MJ
2673 const auto revents = LTTNG_POLL_GETEV(&events, i);
2674 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
d3e2ba59 2675
eea7556c
MD
2676 health_code_update();
2677
8a00688e
MJ
2678 /* Activity on thread quit pipe, exiting. */
2679 if (relayd_is_thread_quit_pipe(pollfd)) {
2680 DBG("Activity on thread quit pipe");
d3e2ba59
JD
2681 err = 0;
2682 goto exit;
2683 }
2684
7591bab1 2685 /* Inspect the relay conn pipe for new connection. */
58eb9381 2686 if (pollfd == live_conn_pipe[0]) {
03e43155 2687 if (revents & LPOLLIN) {
302d8906
JG
2688 struct relay_connection *conn;
2689
5c7248cd
JG
2690 ret = lttng_read(live_conn_pipe[0],
2691 &conn,
2692 sizeof(conn)); /* NOLINT sizeof used on a
2693 pointer. */
d3e2ba59
JD
2694 if (ret < 0) {
2695 goto error;
2696 }
28ab034a
JG
2697 ret = lttng_poll_add(
2698 &events, conn->sock->fd, LPOLLIN | LPOLLRDHUP);
73039936
FD
2699 if (ret) {
2700 ERR("Failed to add new live connection file descriptor to poll set");
2701 goto error;
2702 }
7591bab1
MD
2703 connection_ht_add(viewer_connections_ht, conn);
2704 DBG("Connection socket %d added to poll", conn->sock->fd);
03e43155
MD
2705 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2706 ERR("Relay live pipe error");
2707 goto error;
2708 } else {
28ab034a
JG
2709 ERR("Unexpected poll events %u for sock %d",
2710 revents,
2711 pollfd);
03e43155 2712 goto error;
d3e2ba59 2713 }
58eb9381 2714 } else {
7591bab1 2715 /* Connection activity. */
302d8906
JG
2716 struct relay_connection *conn;
2717
7591bab1
MD
2718 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2719 if (!conn) {
2720 continue;
2721 }
58eb9381 2722
03e43155 2723 if (revents & LPOLLIN) {
28ab034a
JG
2724 ret = conn->sock->ops->recvmsg(
2725 conn->sock, &recv_hdr, sizeof(recv_hdr), 0);
d3e2ba59 2726 if (ret <= 0) {
7591bab1 2727 /* Connection closed. */
58eb9381 2728 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2729 /* Put "create" ownership reference. */
2730 connection_put(conn);
58eb9381 2731 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 2732 } else {
58eb9381 2733 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
2734 if (ret < 0) {
2735 /* Clear the session on error. */
58eb9381 2736 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2737 /* Put "create" ownership reference. */
2738 connection_put(conn);
28ab034a
JG
2739 DBG("Viewer connection closed with %d",
2740 pollfd);
d3e2ba59
JD
2741 }
2742 }
03e43155
MD
2743 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2744 cleanup_connection_pollfd(&events, pollfd);
2745 /* Put "create" ownership reference. */
2746 connection_put(conn);
2747 } else {
28ab034a
JG
2748 ERR("Unexpected poll events %u for sock %d",
2749 revents,
2750 pollfd);
03e43155
MD
2751 connection_put(conn);
2752 goto error;
d3e2ba59 2753 }
7591bab1
MD
2754 /* Put local "get_by_sock" reference. */
2755 connection_put(conn);
d3e2ba59
JD
2756 }
2757 }
2758 }
2759
2760exit:
2761error:
35bc1f58 2762 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
d3e2ba59 2763
71efa8ef 2764 /* Cleanup remaining connection object. */
56047f5a
JG
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 }
d3e2ba59 2773 }
d3e2ba59 2774error_poll_create:
7591bab1
MD
2775 lttng_ht_destroy(viewer_connections_ht);
2776viewer_connections_ht_error:
58eb9381 2777 /* Close relay conn pipes */
87bcbe91 2778 (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe);
d3e2ba59
JD
2779 if (err) {
2780 DBG("Viewer worker thread exited with error");
2781 }
2782 DBG("Viewer worker thread cleanup complete");
9b5e0863 2783error_testpoint:
eea7556c
MD
2784 if (err) {
2785 health_error();
2786 ERR("Health error occurred in %s", __func__);
2787 }
2788 health_unregister(health_relayd);
b4aacfdc
MD
2789 if (lttng_relay_stop_threads()) {
2790 ERR("Error stopping threads");
178a0557 2791 }
d3e2ba59 2792 rcu_unregister_thread();
cd9adb8b 2793 return nullptr;
d3e2ba59
JD
2794}
2795
2796/*
2797 * Create the relay command pipe to wake thread_manage_apps.
2798 * Closed in cleanup().
2799 */
cd9adb8b 2800static int create_conn_pipe()
d3e2ba59 2801{
28ab034a
JG
2802 return fd_tracker_util_pipe_open_cloexec(
2803 the_fd_tracker, "Live connection pipe", live_conn_pipe);
178a0557 2804}
d3e2ba59 2805
cd9adb8b 2806int relayd_live_join()
d3e2ba59 2807{
178a0557 2808 int ret, retval = 0;
d3e2ba59
JD
2809 void *status;
2810
d3e2ba59 2811 ret = pthread_join(live_listener_thread, &status);
178a0557
MD
2812 if (ret) {
2813 errno = ret;
d3e2ba59 2814 PERROR("pthread_join live listener");
178a0557 2815 retval = -1;
d3e2ba59
JD
2816 }
2817
2818 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2819 if (ret) {
2820 errno = ret;
d3e2ba59 2821 PERROR("pthread_join live worker");
178a0557 2822 retval = -1;
d3e2ba59
JD
2823 }
2824
2825 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2826 if (ret) {
2827 errno = ret;
d3e2ba59 2828 PERROR("pthread_join live dispatcher");
178a0557 2829 retval = -1;
d3e2ba59
JD
2830 }
2831
178a0557 2832 cleanup_relayd_live();
d3e2ba59 2833
178a0557 2834 return retval;
d3e2ba59
JD
2835}
2836
2837/*
2838 * main
2839 */
7591bab1 2840int relayd_live_create(struct lttng_uri *uri)
d3e2ba59 2841{
178a0557 2842 int ret = 0, retval = 0;
d3e2ba59
JD
2843 void *status;
2844 int is_root;
2845
178a0557
MD
2846 if (!uri) {
2847 retval = -1;
2848 goto exit_init_data;
2849 }
d3e2ba59
JD
2850 live_uri = uri;
2851
d3e2ba59
JD
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");
178a0557
MD
2858 retval = -1;
2859 goto exit_init_data;
d3e2ba59
JD
2860 }
2861 }
2862
2863 /* Setup the thread apps communication pipe. */
178a0557
MD
2864 if (create_conn_pipe()) {
2865 retval = -1;
2866 goto exit_init_data;
d3e2ba59
JD
2867 }
2868
2869 /* Init relay command queue. */
8bdee6e2 2870 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
d3e2ba59
JD
2871
2872 /* Set up max poll set size */
25b397f9
MD
2873 if (lttng_poll_set_max_size()) {
2874 retval = -1;
2875 goto exit_init_data;
2876 }
d3e2ba59
JD
2877
2878 /* Setup the dispatcher thread */
cd9adb8b
JG
2879 ret = pthread_create(&live_dispatcher_thread,
2880 default_pthread_attr(),
2881 thread_dispatcher,
2882 (void *) nullptr);
178a0557
MD
2883 if (ret) {
2884 errno = ret;
d3e2ba59 2885 PERROR("pthread_create viewer dispatcher");
178a0557
MD
2886 retval = -1;
2887 goto exit_dispatcher_thread;
d3e2ba59
JD
2888 }
2889
2890 /* Setup the worker thread */
cd9adb8b 2891 ret = pthread_create(&live_worker_thread, default_pthread_attr(), thread_worker, nullptr);
178a0557
MD
2892 if (ret) {
2893 errno = ret;
d3e2ba59 2894 PERROR("pthread_create viewer worker");
178a0557
MD
2895 retval = -1;
2896 goto exit_worker_thread;
d3e2ba59
JD
2897 }
2898
2899 /* Setup the listener thread */
28ab034a 2900 ret = pthread_create(
cd9adb8b 2901 &live_listener_thread, default_pthread_attr(), thread_listener, (void *) nullptr);
178a0557
MD
2902 if (ret) {
2903 errno = ret;
d3e2ba59 2904 PERROR("pthread_create viewer listener");
178a0557
MD
2905 retval = -1;
2906 goto exit_listener_thread;
d3e2ba59
JD
2907 }
2908
178a0557
MD
2909 /*
2910 * All OK, started all threads.
2911 */
2912 return retval;
2913
9911d21b
JG
2914 /*
2915 * Join on the live_listener_thread should anything be added after
2916 * the live_listener thread's creation.
2917 */
d3e2ba59 2918
178a0557 2919exit_listener_thread:
d3e2ba59 2920
d3e2ba59 2921 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2922 if (ret) {
2923 errno = ret;
d3e2ba59 2924 PERROR("pthread_join live worker");
178a0557 2925 retval = -1;
d3e2ba59 2926 }
178a0557 2927exit_worker_thread:
d3e2ba59 2928
d3e2ba59 2929 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2930 if (ret) {
2931 errno = ret;
d3e2ba59 2932 PERROR("pthread_join live dispatcher");
178a0557 2933 retval = -1;
d3e2ba59 2934 }
178a0557 2935exit_dispatcher_thread:
d3e2ba59 2936
178a0557
MD
2937exit_init_data:
2938 cleanup_relayd_live();
d3e2ba59 2939
178a0557 2940 return retval;
d3e2ba59 2941}
This page took 0.264608 seconds and 4 git commands to generate.