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