Run clang-format on the whole tree
[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 */
28ab034a 179static void cleanup_relayd_live(void)
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);
7591bab1
MD
321 vstream->sent_flag = 1;
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;
9edaf114 360 struct relay_stream *relay_stream = NULL;
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
9edaf114
JG
402 relay_stream = NULL;
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) {
80516611 431 struct lttng_trace_chunk *viewer_stream_trace_chunk = NULL;
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
JG
502 lttng_trace_chunk_put(viewer_stream_trace_chunk);
503 viewer_stream_trace_chunk = NULL;
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 }
9edaf114 550 relay_stream = NULL;
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
b4aacfdc 567int relayd_live_stop(void)
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;
617 struct lttcomm_sock *socks[2] = { listening_sock, NULL };
618 struct lttcomm_sock *new_sock = NULL;
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;
d3e2ba59 637 struct lttcomm_sock *sock = NULL;
8855795d
JG
638 char uri_str[LTTNG_PATH_MAX];
639 char *formated_name = NULL;
d3e2ba59
JD
640
641 sock = lttcomm_alloc_sock_from_uri(uri);
642 if (sock == NULL) {
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
JG
655 if (ret < 0) {
656 formated_name = NULL;
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 :
663 NULL),
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);
d3e2ba59
JD
692 return NULL;
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
d3e2ba59 735 while (1) {
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. */
802 newsock = NULL;
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 }
d3e2ba59
JD
853 return NULL;
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;
58eb9381 864 struct relay_connection *conn = NULL;
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);
d3e2ba59
JD
892 if (node == NULL) {
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 */
58eb9381
DG
907 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
908 if (ret < 0) {
909 PERROR("write conn pipe");
7591bab1 910 connection_put(conn);
d3e2ba59
JD
911 goto error;
912 }
913 } while (node != NULL);
914
915 /* Futex wait on queue. Blocking call on futex() */
eea7556c 916 health_poll_entry();
58eb9381 917 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 918 health_poll_exit();
d3e2ba59
JD
919 }
920
eea7556c
MD
921 /* Normal exit, no error */
922 err = 0;
923
d3e2ba59 924error:
9b5e0863 925error_testpoint:
eea7556c
MD
926 if (err) {
927 health_error();
928 ERR("Health error occurred in %s", __func__);
929 }
930 health_unregister(health_relayd);
d3e2ba59 931 DBG("Live viewer dispatch thread dying");
b4aacfdc
MD
932 if (lttng_relay_stop_threads()) {
933 ERR("Error stopping threads");
178a0557 934 }
d3e2ba59
JD
935 return NULL;
936}
937
938/*
939 * Establish connection with the viewer and check the versions.
940 *
941 * Return 0 on success or else negative value.
942 */
28ab034a 943static int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
944{
945 int ret;
946 struct lttng_viewer_connect reply, msg;
947
58eb9381 948 conn->version_check_done = 1;
d3e2ba59 949
eea7556c
MD
950 health_code_update();
951
58eb9381 952 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 953 if (ret < 0) {
d3e2ba59
JD
954 goto end;
955 }
956
eea7556c
MD
957 health_code_update();
958
f46b2ce6 959 memset(&reply, 0, sizeof(reply));
d3e2ba59
JD
960 reply.major = RELAYD_VERSION_COMM_MAJOR;
961 reply.minor = RELAYD_VERSION_COMM_MINOR;
962
963 /* Major versions must be the same */
964 if (reply.major != be32toh(msg.major)) {
2f8f53af 965 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
28ab034a
JG
966 reply.major,
967 be32toh(msg.major));
72180669 968 ret = -1;
d3e2ba59
JD
969 goto end;
970 }
971
58eb9381 972 conn->major = reply.major;
d3e2ba59
JD
973 /* We adapt to the lowest compatible version */
974 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 975 conn->minor = reply.minor;
d3e2ba59 976 } else {
58eb9381 977 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
978 }
979
c4e361a4 980 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 981 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 982 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 983 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
984 } else {
985 ERR("Unknown connection type : %u", be32toh(msg.type));
986 ret = -1;
987 goto end;
988 }
989
990 reply.major = htobe32(reply.major);
991 reply.minor = htobe32(reply.minor);
58eb9381 992 if (conn->type == RELAY_VIEWER_COMMAND) {
93b4787b 993 /*
7591bab1
MD
994 * Increment outside of htobe64 macro, because the argument can
995 * be used more than once within the macro, and thus the
996 * operation may be undefined.
93b4787b 997 */
7591bab1 998 pthread_mutex_lock(&last_relay_viewer_session_id_lock);
93b4787b 999 last_relay_viewer_session_id++;
7591bab1 1000 pthread_mutex_unlock(&last_relay_viewer_session_id_lock);
93b4787b 1001 reply.viewer_session_id = htobe64(last_relay_viewer_session_id);
d3e2ba59 1002 }
eea7556c
MD
1003
1004 health_code_update();
1005
58eb9381 1006 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1007 if (ret < 0) {
2f8f53af 1008 goto end;
d3e2ba59
JD
1009 }
1010
eea7556c
MD
1011 health_code_update();
1012
58eb9381 1013 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
1014 ret = 0;
1015
1016end:
1017 return ret;
1018}
1019
1020/*
1021 * Send the viewer the list of current sessions.
7591bab1
MD
1022 * We need to create a copy of the hash table content because otherwise
1023 * we cannot assume the number of entries stays the same between getting
1024 * the number of HT elements and iteration over the HT.
d3e2ba59
JD
1025 *
1026 * Return 0 on success or else a negative value.
1027 */
28ab034a 1028static int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59 1029{
5f10c6b1 1030 int ret = 0;
d3e2ba59 1031 struct lttng_viewer_list_sessions session_list;
d3e2ba59 1032 struct lttng_ht_iter iter;
d3e2ba59 1033 struct relay_session *session;
7591bab1
MD
1034 struct lttng_viewer_session *send_session_buf = NULL;
1035 uint32_t buf_count = SESSION_BUF_DEFAULT_COUNT;
1036 uint32_t count = 0;
d3e2ba59 1037
64803277 1038 send_session_buf = calloc<lttng_viewer_session>(SESSION_BUF_DEFAULT_COUNT);
7591bab1
MD
1039 if (!send_session_buf) {
1040 return -1;
d3e2ba59
JD
1041 }
1042
7591bab1 1043 rcu_read_lock();
28ab034a 1044 cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) {
7591bab1 1045 struct lttng_viewer_session *send_session;
d3e2ba59 1046
eea7556c
MD
1047 health_code_update();
1048
d995f382 1049 pthread_mutex_lock(&session->lock);
7e0a4379
JR
1050 if (session->connection_closed) {
1051 /* Skip closed session */
d995f382
JG
1052 goto next_session;
1053 }
7e0a4379 1054
7591bab1
MD
1055 if (count >= buf_count) {
1056 struct lttng_viewer_session *newbuf;
1057 uint32_t new_buf_count = buf_count << 1;
eea7556c 1058
28ab034a
JG
1059 newbuf = (lttng_viewer_session *) realloc(
1060 send_session_buf, new_buf_count * sizeof(*send_session_buf));
7591bab1
MD
1061 if (!newbuf) {
1062 ret = -1;
d995f382 1063 goto break_loop;
7591bab1
MD
1064 }
1065 send_session_buf = newbuf;
1066 buf_count = new_buf_count;
6763619c 1067 }
7591bab1 1068 send_session = &send_session_buf[count];
bfc3fb17 1069 if (lttng_strncpy(send_session->session_name,
28ab034a
JG
1070 session->session_name,
1071 sizeof(send_session->session_name))) {
bfc3fb17 1072 ret = -1;
d995f382 1073 goto break_loop;
bfc3fb17 1074 }
28ab034a
JG
1075 if (lttng_strncpy(send_session->hostname,
1076 session->hostname,
1077 sizeof(send_session->hostname))) {
bfc3fb17 1078 ret = -1;
d995f382 1079 goto break_loop;
bfc3fb17 1080 }
7591bab1
MD
1081 send_session->id = htobe64(session->id);
1082 send_session->live_timer = htobe32(session->live_timer);
1083 if (session->viewer_attached) {
1084 send_session->clients = htobe32(1);
1085 } else {
1086 send_session->clients = htobe32(0);
d227d5bd 1087 }
7591bab1
MD
1088 send_session->streams = htobe32(session->stream_count);
1089 count++;
d995f382
JG
1090 next_session:
1091 pthread_mutex_unlock(&session->lock);
1092 continue;
1093 break_loop:
1094 pthread_mutex_unlock(&session->lock);
1095 break;
7591bab1
MD
1096 }
1097 rcu_read_unlock();
5f10c6b1
JG
1098 if (ret < 0) {
1099 goto end_free;
1100 }
d227d5bd 1101
7591bab1 1102 session_list.sessions_count = htobe32(count);
d227d5bd 1103
7591bab1 1104 health_code_update();
d227d5bd 1105
7591bab1
MD
1106 ret = send_response(conn->sock, &session_list, sizeof(session_list));
1107 if (ret < 0) {
1108 goto end_free;
d227d5bd 1109 }
d227d5bd 1110
7591bab1 1111 health_code_update();
d227d5bd 1112
28ab034a 1113 ret = send_response(conn->sock, send_session_buf, count * sizeof(*send_session_buf));
7591bab1
MD
1114 if (ret < 0) {
1115 goto end_free;
d227d5bd 1116 }
7591bab1 1117 health_code_update();
d227d5bd 1118
7591bab1
MD
1119 ret = 0;
1120end_free:
1121 free(send_session_buf);
1122 return ret;
d227d5bd
JD
1123}
1124
80e8027a 1125/*
7591bab1 1126 * Send the viewer the list of current streams.
80e8027a 1127 */
28ab034a 1128static int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
1129{
1130 int ret, send_streams = 0;
7591bab1 1131 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0, nb_total = 0;
80e8027a
JD
1132 struct lttng_viewer_new_streams_request request;
1133 struct lttng_viewer_new_streams_response response;
b66a15d1 1134 struct relay_session *session = NULL;
6763619c 1135 uint64_t session_id;
bddf80e4 1136 bool closed = false;
80e8027a 1137
a0377dfe 1138 LTTNG_ASSERT(conn);
80e8027a 1139
80e8027a
JD
1140 health_code_update();
1141
2f8f53af 1142 /* Receive the request from the connected client. */
58eb9381 1143 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1144 if (ret < 0) {
80e8027a
JD
1145 goto error;
1146 }
6763619c 1147 session_id = be64toh(request.session_id);
80e8027a
JD
1148
1149 health_code_update();
1150
53efb85a
MD
1151 memset(&response, 0, sizeof(response));
1152
7591bab1 1153 session = session_get_by_id(session_id);
2f8f53af 1154 if (!session) {
6763619c 1155 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 1156 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
1157 goto send_reply;
1158 }
1159
7591bab1 1160 if (!viewer_session_is_attached(conn->viewer_session, session)) {
c4e361a4 1161 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
1162 goto send_reply;
1163 }
1164
c876d657
JR
1165 /*
1166 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1167 * that at this point the client is already attached to the session.Aany
1168 * initial stream will have been created with the seek type at attach
1169 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1170 * Otherwise any event happening in a new stream between the attach and
1171 * a call to viewer_get_new_streams will be "lost" (never received) from
1172 * the viewer's point of view.
1173 */
c06fdd95 1174 pthread_mutex_lock(&session->lock);
8cd15f6a
MD
1175 /*
1176 * If a session rotation is ongoing, do not attempt to open any
1177 * stream, because the chunk can be in an intermediate state
1178 * due to directory renaming.
1179 */
fe88e517 1180 if (session_has_ongoing_rotation(session)) {
8cd15f6a
MD
1181 DBG("Relay session %" PRIu64 " rotation ongoing", session_id);
1182 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW);
1183 goto send_reply_unlock;
1184 }
b66a15d1 1185 ret = make_viewer_streams(session,
28ab034a
JG
1186 conn->viewer_session,
1187 LTTNG_VIEWER_SEEK_BEGINNING,
1188 &nb_total,
1189 &nb_unsent,
1190 &nb_created,
1191 &closed);
2f8f53af 1192 if (ret < 0) {
14e34907
JG
1193 /*
1194 * This is caused by an internal error; propagate the negative
1195 * 'ret' to close the connection.
1196 */
1197 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
0283afa9 1198 goto send_reply_unlock;
2f8f53af 1199 }
3d0bbd40
JG
1200 send_streams = 1;
1201 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
b66a15d1 1202
2f8f53af
DG
1203 /* Only send back the newly created streams with the unsent ones. */
1204 nb_streams = nb_created + nb_unsent;
80e8027a
JD
1205 response.streams_count = htobe32(nb_streams);
1206
4479f682 1207 /*
2229a09c
MD
1208 * If the session is closed, HUP when there are no more streams
1209 * with data.
4479f682 1210 */
bddf80e4 1211 if (closed && nb_total == 0) {
4479f682 1212 send_streams = 0;
bddf80e4 1213 response.streams_count = 0;
4479f682 1214 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
3d0bbd40 1215 goto send_reply_unlock;
4479f682 1216 }
3d0bbd40
JG
1217send_reply_unlock:
1218 pthread_mutex_unlock(&session->lock);
4479f682 1219
80e8027a
JD
1220send_reply:
1221 health_code_update();
58eb9381 1222 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 1223 if (ret < 0) {
7591bab1 1224 goto end_put_session;
80e8027a
JD
1225 }
1226 health_code_update();
1227
1228 /*
7591bab1
MD
1229 * Unknown or empty session, just return gracefully, the viewer
1230 * knows what is happening.
80e8027a
JD
1231 */
1232 if (!send_streams || !nb_streams) {
1233 ret = 0;
7591bab1 1234 goto end_put_session;
80e8027a
JD
1235 }
1236
2f8f53af 1237 /*
7591bab1
MD
1238 * Send stream and *DON'T* ignore the sent flag so every viewer
1239 * streams that were not sent from that point will be sent to
1240 * the viewer.
2f8f53af 1241 */
c06fdd95 1242 ret = send_viewer_streams(conn->sock, session_id, 0);
2f8f53af 1243 if (ret < 0) {
7591bab1 1244 goto end_put_session;
80e8027a
JD
1245 }
1246
7591bab1
MD
1247end_put_session:
1248 if (session) {
1249 session_put(session);
1250 }
80e8027a
JD
1251error:
1252 return ret;
1253}
1254
d3e2ba59
JD
1255/*
1256 * Send the viewer the list of current sessions.
1257 */
28ab034a 1258static int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1259{
2f8f53af
DG
1260 int send_streams = 0;
1261 ssize_t ret;
80e8027a 1262 uint32_t nb_streams = 0;
2f8f53af 1263 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1264 struct lttng_viewer_attach_session_request request;
1265 struct lttng_viewer_attach_session_response response;
7591bab1 1266 struct relay_session *session = NULL;
dbd6665b 1267 enum lttng_viewer_attach_return_code viewer_attach_status;
bddf80e4 1268 bool closed = false;
c06fdd95 1269 uint64_t session_id;
d3e2ba59 1270
a0377dfe 1271 LTTNG_ASSERT(conn);
d3e2ba59 1272
eea7556c
MD
1273 health_code_update();
1274
2f8f53af 1275 /* Receive the request from the connected client. */
58eb9381 1276 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1277 if (ret < 0) {
d3e2ba59
JD
1278 goto error;
1279 }
1280
c06fdd95 1281 session_id = be64toh(request.session_id);
07d9fffa 1282
eea7556c
MD
1283 health_code_update();
1284
53efb85a
MD
1285 memset(&response, 0, sizeof(response));
1286
c3b7390b 1287 if (!conn->viewer_session) {
07d9fffa
FD
1288 viewer_attach_status = LTTNG_VIEWER_ATTACH_NO_SESSION;
1289 DBG("Client trying to attach before creating a live viewer session, returning status=%s",
28ab034a 1290 lttng_viewer_attach_return_code_str(viewer_attach_status));
c3b7390b
JD
1291 goto send_reply;
1292 }
1293
c06fdd95 1294 session = session_get_by_id(session_id);
2f8f53af 1295 if (!session) {
07d9fffa
FD
1296 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1297 DBG("Relay session %" PRIu64 " not found, returning status=%s",
28ab034a
JG
1298 session_id,
1299 lttng_viewer_attach_return_code_str(viewer_attach_status));
d3e2ba59
JD
1300 goto send_reply;
1301 }
07d9fffa 1302 DBG("Attach relay session ID %" PRIu64 " received", session_id);
d3e2ba59 1303
c06fdd95 1304 pthread_mutex_lock(&session->lock);
7591bab1 1305 if (session->live_timer == 0) {
07d9fffa
FD
1306 viewer_attach_status = LTTNG_VIEWER_ATTACH_NOT_LIVE;
1307 DBG("Relay session ID %" PRIu64 " is not a live session, returning status=%s",
28ab034a
JG
1308 session_id,
1309 lttng_viewer_attach_return_code_str(viewer_attach_status));
d3e2ba59 1310 goto send_reply;
7591bab1
MD
1311 }
1312
1313 send_streams = 1;
28ab034a 1314 viewer_attach_status = viewer_session_attach(conn->viewer_session, session);
dbd6665b 1315 if (viewer_attach_status != LTTNG_VIEWER_ATTACH_OK) {
07d9fffa 1316 DBG("Error attaching to relay session %" PRIu64 ", returning status=%s",
28ab034a
JG
1317 session_id,
1318 lttng_viewer_attach_return_code_str(viewer_attach_status));
7591bab1 1319 goto send_reply;
d3e2ba59
JD
1320 }
1321
1322 switch (be32toh(request.seek)) {
c4e361a4
JD
1323 case LTTNG_VIEWER_SEEK_BEGINNING:
1324 case LTTNG_VIEWER_SEEK_LAST:
07d9fffa 1325 viewer_attach_status = LTTNG_VIEWER_ATTACH_OK;
ac497a37 1326 seek_type = (lttng_viewer_seek) be32toh(request.seek);
d3e2ba59
JD
1327 break;
1328 default:
28ab034a
JG
1329 ERR("Wrong seek parameter for relay session %" PRIu64 ", returning status=%s",
1330 session_id,
1331 lttng_viewer_attach_return_code_str(viewer_attach_status));
07d9fffa 1332 viewer_attach_status = LTTNG_VIEWER_ATTACH_SEEK_ERR;
d3e2ba59
JD
1333 send_streams = 0;
1334 goto send_reply;
1335 }
1336
8cd15f6a
MD
1337 /*
1338 * If a session rotation is ongoing, do not attempt to open any
1339 * stream, because the chunk can be in an intermediate state
1340 * due to directory renaming.
1341 */
fe88e517 1342 if (session_has_ongoing_rotation(session)) {
8cd15f6a
MD
1343 DBG("Relay session %" PRIu64 " rotation ongoing", session_id);
1344 send_streams = 0;
1345 goto send_reply;
1346 }
1347
28ab034a
JG
1348 ret = make_viewer_streams(
1349 session, conn->viewer_session, seek_type, &nb_streams, NULL, NULL, &closed);
2f8f53af 1350 if (ret < 0) {
7591bab1 1351 goto end_put_session;
d3e2ba59 1352 }
c06fdd95
JG
1353 pthread_mutex_unlock(&session->lock);
1354 session_put(session);
1355 session = NULL;
1356
1357 response.streams_count = htobe32(nb_streams);
bddf80e4
MD
1358 /*
1359 * If the session is closed when the viewer is attaching, it
1360 * means some of the streams may have been concurrently removed,
1361 * so we don't allow the viewer to attach, even if there are
1362 * streams available.
1363 */
1364 if (closed) {
1365 send_streams = 0;
1366 response.streams_count = 0;
07d9fffa
FD
1367 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
1368 ERR("Session %" PRIu64 " is closed, returning status=%s",
28ab034a
JG
1369 session_id,
1370 lttng_viewer_attach_return_code_str(viewer_attach_status));
bddf80e4
MD
1371 goto send_reply;
1372 }
1373
d3e2ba59 1374send_reply:
eea7556c 1375 health_code_update();
07d9fffa
FD
1376
1377 response.status = htobe32((uint32_t) viewer_attach_status);
1378
58eb9381 1379 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1380 if (ret < 0) {
7591bab1 1381 goto end_put_session;
d3e2ba59 1382 }
eea7556c 1383 health_code_update();
d3e2ba59
JD
1384
1385 /*
7591bab1
MD
1386 * Unknown or empty session, just return gracefully, the viewer
1387 * knows what is happening.
d3e2ba59 1388 */
157df586 1389 if (!send_streams || !nb_streams) {
d3e2ba59 1390 ret = 0;
7591bab1 1391 goto end_put_session;
d3e2ba59
JD
1392 }
1393
2f8f53af 1394 /* Send stream and ignore the sent flag. */
c06fdd95 1395 ret = send_viewer_streams(conn->sock, session_id, 1);
2f8f53af 1396 if (ret < 0) {
7591bab1 1397 goto end_put_session;
d3e2ba59 1398 }
d3e2ba59 1399
7591bab1
MD
1400end_put_session:
1401 if (session) {
c06fdd95 1402 pthread_mutex_unlock(&session->lock);
7591bab1
MD
1403 session_put(session);
1404 }
4a9daf17
JD
1405error:
1406 return ret;
1407}
1408
878c34cf
DG
1409/*
1410 * Open the index file if needed for the given vstream.
1411 *
f8f3885c
MD
1412 * If an index file is successfully opened, the vstream will set it as its
1413 * current index file.
878c34cf
DG
1414 *
1415 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
7591bab1
MD
1416 *
1417 * Called with rstream lock held.
878c34cf 1418 */
28ab034a 1419static int try_open_index(struct relay_viewer_stream *vstream, struct relay_stream *rstream)
878c34cf
DG
1420{
1421 int ret = 0;
ebb29c10
JG
1422 const uint32_t connection_major = rstream->trace->session->major;
1423 const uint32_t connection_minor = rstream->trace->session->minor;
3ff5c5db 1424 enum lttng_trace_chunk_status chunk_status;
878c34cf 1425
f8f3885c 1426 if (vstream->index_file) {
878c34cf
DG
1427 goto end;
1428 }
1429
1430 /*
7591bab1 1431 * First time, we open the index file and at least one index is ready.
878c34cf 1432 */
28ab034a 1433 if (rstream->index_received_seqcount == 0 || !vstream->stream_file.trace_chunk) {
878c34cf
DG
1434 ret = -ENOENT;
1435 goto end;
1436 }
80516611 1437
3ff5c5db 1438 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
28ab034a
JG
1439 vstream->stream_file.trace_chunk,
1440 rstream->path_name,
1441 rstream->channel_name,
1442 rstream->tracefile_size,
1443 vstream->current_tracefile_id,
1444 lttng_to_index_major(connection_major, connection_minor),
1445 lttng_to_index_minor(connection_major, connection_minor),
1446 true,
1447 &vstream->index_file);
3ff5c5db
MD
1448 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1449 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
1450 ret = -ENOENT;
1451 } else {
1452 ret = -1;
1453 }
878c34cf
DG
1454 }
1455
1456end:
1457 return ret;
1458}
1459
1460/*
7591bab1
MD
1461 * Check the status of the index for the given stream. This function
1462 * updates the index structure if needed and can put (close) the vstream
1463 * in the HUP situation.
1464 *
1465 * Return 0 means that we can proceed with the index. A value of 1 means
1466 * that the index has been updated and is ready to be sent to the
1467 * client. A negative value indicates an error that can't be handled.
878c34cf 1468 *
7591bab1 1469 * Called with rstream lock held.
878c34cf
DG
1470 */
1471static int check_index_status(struct relay_viewer_stream *vstream,
28ab034a
JG
1472 struct relay_stream *rstream,
1473 struct ctf_trace *trace,
1474 struct lttng_viewer_index *index)
878c34cf
DG
1475{
1476 int ret;
1477
68c40154 1478 DBG("Check index status: index_received_seqcount %" PRIu64 " "
28ab034a
JG
1479 "index_sent_seqcount %" PRIu64 " "
1480 "for stream %" PRIu64,
1481 rstream->index_received_seqcount,
1482 vstream->index_sent_seqcount,
1483 vstream->stream->stream_handle);
1484 if ((trace->session->connection_closed || rstream->closed) &&
1485 rstream->index_received_seqcount == vstream->index_sent_seqcount) {
797bc362
MD
1486 /*
1487 * Last index sent and session connection or relay
1488 * stream are closed.
1489 */
7e4fb89b
FD
1490 index->status = LTTNG_VIEWER_INDEX_HUP;
1491 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
28ab034a
JG
1492 ",connection-closed=%d, relay-stream-closed=%d, returning status=%s",
1493 vstream->stream->stream_handle,
1494 trace->session->connection_closed,
1495 rstream->closed,
1496 lttng_viewer_next_index_return_code_str(
1497 (enum lttng_viewer_next_index_return_code) index->status));
7591bab1
MD
1498 goto hup;
1499 } else if (rstream->beacon_ts_end != -1ULL &&
28ab034a
JG
1500 (rstream->index_received_seqcount == 0 ||
1501 (vstream->index_sent_seqcount != 0 &&
1502 rstream->index_received_seqcount <= vstream->index_sent_seqcount))) {
7591bab1
MD
1503 /*
1504 * We've received a synchronization beacon and the last index
1505 * available has been sent, the index for now is inactive.
1506 *
1507 * In this case, we have received a beacon which allows us to
1508 * inform the client of a time interval during which we can
1509 * guarantee that there are no events to read (and never will
1510 * be).
b0d240a2
MD
1511 *
1512 * The sent seqcount can grow higher than receive seqcount on
1513 * clear because the rotation performed by clear will push
1514 * the index_sent_seqcount ahead (see
1515 * viewer_stream_sync_tracefile_array_tail) and skip over
1516 * packet sequence numbers.
7591bab1 1517 */
7e4fb89b 1518 index->status = LTTNG_VIEWER_INDEX_INACTIVE;
7591bab1
MD
1519 index->timestamp_end = htobe64(rstream->beacon_ts_end);
1520 index->stream_id = htobe64(rstream->ctf_stream_id);
7e4fb89b 1521 DBG("Check index status: inactive with beacon, for stream %" PRIu64
28ab034a
JG
1522 ", returning status=%s",
1523 vstream->stream->stream_handle,
1524 lttng_viewer_next_index_return_code_str(
1525 (enum lttng_viewer_next_index_return_code) index->status));
7591bab1 1526 goto index_ready;
b0d240a2 1527 } else if (rstream->index_received_seqcount == 0 ||
28ab034a
JG
1528 (vstream->index_sent_seqcount != 0 &&
1529 rstream->index_received_seqcount <= vstream->index_sent_seqcount)) {
7591bab1 1530 /*
b0d240a2 1531 * This checks whether received <= sent seqcount. In
a44ca2ca
MD
1532 * this case, we have not received a beacon. Therefore,
1533 * we can only ask the client to retry later.
b0d240a2
MD
1534 *
1535 * The sent seqcount can grow higher than receive seqcount on
1536 * clear because the rotation performed by clear will push
1537 * the index_sent_seqcount ahead (see
1538 * viewer_stream_sync_tracefile_array_tail) and skip over
1539 * packet sequence numbers.
7591bab1 1540 */
7e4fb89b
FD
1541 index->status = LTTNG_VIEWER_INDEX_RETRY;
1542 DBG("Check index status:"
28ab034a
JG
1543 "did not received beacon for stream %" PRIu64 ", returning status=%s",
1544 vstream->stream->stream_handle,
1545 lttng_viewer_next_index_return_code_str(
1546 (enum lttng_viewer_next_index_return_code) index->status));
7591bab1 1547 goto index_ready;
a44ca2ca 1548 } else if (!tracefile_array_seq_in_file(rstream->tfa,
28ab034a
JG
1549 vstream->current_tracefile_id,
1550 vstream->index_sent_seqcount)) {
7591bab1 1551 /*
a44ca2ca
MD
1552 * The next index we want to send cannot be read either
1553 * because we need to perform a rotation, or due to
1554 * the producer having overwritten its trace file.
7591bab1 1555 */
28ab034a 1556 DBG("Viewer stream %" PRIu64 " rotation", vstream->stream->stream_handle);
7591bab1 1557 ret = viewer_stream_rotate(vstream);
b0d240a2 1558 if (ret == 1) {
7591bab1 1559 /* EOF across entire stream. */
7e4fb89b
FD
1560 index->status = LTTNG_VIEWER_INDEX_HUP;
1561 DBG("Check index status:"
28ab034a
JG
1562 "reached end of file for stream %" PRIu64 ", returning status=%s",
1563 vstream->stream->stream_handle,
1564 lttng_viewer_next_index_return_code_str(
1565 (enum lttng_viewer_next_index_return_code) index->status));
7591bab1
MD
1566 goto hup;
1567 }
7591bab1 1568 /*
a44ca2ca
MD
1569 * If we have been pushed due to overwrite, it
1570 * necessarily means there is data that can be read in
1571 * the stream. If we rotated because we reached the end
1572 * of a tracefile, it means the following tracefile
1573 * needs to contain at least one index, else we would
1574 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1575 * viewer. The updated index_sent_seqcount needs to
1576 * point to a readable index entry now.
1577 *
1578 * In the case where we "rotate" on a single file, we
1579 * can end up in a case where the requested index is
1580 * still unavailable.
7591bab1 1581 */
a44ca2ca 1582 if (rstream->tracefile_count == 1 &&
28ab034a
JG
1583 !tracefile_array_seq_in_file(rstream->tfa,
1584 vstream->current_tracefile_id,
1585 vstream->index_sent_seqcount)) {
7e4fb89b
FD
1586 index->status = LTTNG_VIEWER_INDEX_RETRY;
1587 DBG("Check index status:"
28ab034a
JG
1588 "tracefile array sequence number %" PRIu64
1589 " not in file for stream %" PRIu64 ", returning status=%s",
1590 vstream->index_sent_seqcount,
1591 vstream->stream->stream_handle,
1592 lttng_viewer_next_index_return_code_str(
1593 (enum lttng_viewer_next_index_return_code) index->status));
a44ca2ca 1594 goto index_ready;
878c34cf 1595 }
28ab034a
JG
1596 LTTNG_ASSERT(tracefile_array_seq_in_file(
1597 rstream->tfa, vstream->current_tracefile_id, vstream->index_sent_seqcount));
878c34cf 1598 }
a44ca2ca
MD
1599 /* ret == 0 means successful so we continue. */
1600 ret = 0;
878c34cf
DG
1601 return ret;
1602
1603hup:
7591bab1 1604 viewer_stream_put(vstream);
878c34cf
DG
1605index_ready:
1606 return 1;
1607}
1608
28ab034a
JG
1609static void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream *vstream,
1610 struct lttng_trace_chunk *new_trace_chunk)
80516611
JG
1611{
1612 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
1613
1614 if (new_trace_chunk) {
28ab034a 1615 const bool acquired_reference = lttng_trace_chunk_get(new_trace_chunk);
80516611 1616
a0377dfe 1617 LTTNG_ASSERT(acquired_reference);
80516611
JG
1618 }
1619
1620 vstream->stream_file.trace_chunk = new_trace_chunk;
1621 viewer_stream_sync_tracefile_array_tail(vstream);
1622 viewer_stream_close_files(vstream);
1623}
1624
d3e2ba59
JD
1625/*
1626 * Send the next index for a stream.
1627 *
1628 * Return 0 on success or else a negative value.
1629 */
28ab034a 1630static int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1631{
1632 int ret;
1633 struct lttng_viewer_get_next_index request_index;
1634 struct lttng_viewer_index viewer_index;
50adc264 1635 struct ctf_packet_index packet_index;
7591bab1
MD
1636 struct relay_viewer_stream *vstream = NULL;
1637 struct relay_stream *rstream = NULL;
1638 struct ctf_trace *ctf_trace = NULL;
1639 struct relay_viewer_stream *metadata_viewer_stream = NULL;
bb1dcf01
MD
1640 bool viewer_stream_and_session_in_same_chunk, viewer_stream_one_rotation_behind;
1641 uint64_t stream_file_chunk_id = -1ULL, viewer_session_chunk_id = -1ULL;
1642 enum lttng_trace_chunk_status status;
d3e2ba59 1643
a0377dfe 1644 LTTNG_ASSERT(conn);
d3e2ba59 1645
7591bab1 1646 memset(&viewer_index, 0, sizeof(viewer_index));
eea7556c 1647 health_code_update();
2f8f53af 1648
58eb9381 1649 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1650 if (ret < 0) {
d3e2ba59
JD
1651 goto end;
1652 }
eea7556c 1653 health_code_update();
d3e2ba59 1654
7591bab1 1655 vstream = viewer_stream_get_by_id(be64toh(request_index.stream_id));
6763619c 1656 if (!vstream) {
7e4fb89b 1657 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
1658 DBG("Client requested index of unknown stream id %" PRIu64 ", returning status=%s",
1659 (uint64_t) be64toh(request_index.stream_id),
1660 lttng_viewer_next_index_return_code_str(
1661 (enum lttng_viewer_next_index_return_code) viewer_index.status));
f1883937 1662 goto send_reply;
2a174661
DG
1663 }
1664
7591bab1
MD
1665 /* Use back. ref. Protected by refcounts. */
1666 rstream = vstream->stream;
1667 ctf_trace = rstream->trace;
d3e2ba59 1668
7591bab1 1669 /* metadata_viewer_stream may be NULL. */
28ab034a 1670 metadata_viewer_stream = ctf_trace_get_viewer_metadata_stream(ctf_trace);
2a174661 1671
b6ae2a95
MD
1672 /*
1673 * Hold the session lock to protect against concurrent changes
1674 * to the chunk files (e.g. rename done by clear), which are
1675 * protected by the session ongoing rotation state. Those are
1676 * synchronized with the session lock.
1677 */
1678 pthread_mutex_lock(&rstream->trace->session->lock);
7591bab1 1679 pthread_mutex_lock(&rstream->lock);
d3e2ba59
JD
1680
1681 /*
1682 * The viewer should not ask for index on metadata stream.
1683 */
7591bab1 1684 if (rstream->is_metadata) {
7e4fb89b 1685 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
28ab034a
JG
1686 DBG("Client requested index of a metadata stream id %" PRIu64
1687 ", returning status=%s",
1688 (uint64_t) be64toh(request_index.stream_id),
1689 lttng_viewer_next_index_return_code_str(
1690 (enum lttng_viewer_next_index_return_code) viewer_index.status));
d3e2ba59
JD
1691 goto send_reply;
1692 }
1693
b0d240a2
MD
1694 if (rstream->ongoing_rotation.is_set) {
1695 /* Rotation is ongoing, try again later. */
7e4fb89b 1696 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
1697 DBG("Client requested index for stream id %" PRIu64
1698 " while a stream rotation is ongoing, returning status=%s",
1699 (uint64_t) be64toh(request_index.stream_id),
1700 lttng_viewer_next_index_return_code_str(
1701 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2
MD
1702 goto send_reply;
1703 }
1704
fe88e517 1705 if (session_has_ongoing_rotation(rstream->trace->session)) {
b0d240a2 1706 /* Rotation is ongoing, try again later. */
7e4fb89b 1707 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
1708 DBG("Client requested index for stream id %" PRIu64
1709 " while a session rotation is ongoing, returning status=%s",
1710 (uint64_t) be64toh(request_index.stream_id),
1711 lttng_viewer_next_index_return_code_str(
1712 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2
MD
1713 goto send_reply;
1714 }
1715
80516611
JG
1716 /*
1717 * Transition the viewer session into the newest trace chunk available.
1718 */
28ab034a
JG
1719 if (!lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
1720 rstream->trace_chunk)) {
9a9c8637 1721 DBG("Relay stream and viewer chunk ids differ");
b0d240a2 1722
28ab034a
JG
1723 ret = viewer_session_set_trace_chunk_copy(conn->viewer_session,
1724 rstream->trace_chunk);
ad8bec24 1725 if (ret) {
7e4fb89b
FD
1726 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1727 ERR("Error copying trace chunk for stream id %" PRIu64
28ab034a
JG
1728 ", returning status=%s",
1729 (uint64_t) be64toh(request_index.stream_id),
1730 lttng_viewer_next_index_return_code_str(
1731 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2
MD
1732 goto send_reply;
1733 }
b0d240a2 1734 }
b0d240a2 1735
80516611
JG
1736 /*
1737 * Transition the viewer stream into the latest trace chunk available.
1738 *
1739 * Note that the stream must _not_ rotate in one precise condition:
1740 * the relay stream has rotated to a NULL trace chunk and the viewer
1741 * stream is consuming the trace chunk that was active just before
1742 * that rotation to NULL.
1743 *
1744 * This allows clients to consume all the packets of a trace chunk
1745 * after a session's destruction.
1746 */
bb1dcf01 1747 if (vstream->stream_file.trace_chunk) {
28ab034a
JG
1748 status = lttng_trace_chunk_get_id(vstream->stream_file.trace_chunk,
1749 &stream_file_chunk_id);
bb1dcf01
MD
1750 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1751 }
1752 if (conn->viewer_session->current_trace_chunk) {
28ab034a
JG
1753 status = lttng_trace_chunk_get_id(conn->viewer_session->current_trace_chunk,
1754 &viewer_session_chunk_id);
bb1dcf01
MD
1755 LTTNG_ASSERT(status == LTTNG_TRACE_CHUNK_STATUS_OK);
1756 }
1757
1758 viewer_stream_and_session_in_same_chunk = lttng_trace_chunk_ids_equal(
28ab034a 1759 conn->viewer_session->current_trace_chunk, vstream->stream_file.trace_chunk);
bb1dcf01 1760 viewer_stream_one_rotation_behind = rstream->completed_rotation_count ==
28ab034a 1761 vstream->last_seen_rotation_count + 1;
bb1dcf01
MD
1762
1763 if (viewer_stream_and_session_in_same_chunk) {
1764 DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate",
28ab034a
JG
1765 vstream->stream_file.trace_chunk ?
1766 std::to_string(stream_file_chunk_id).c_str() :
1767 "None",
1768 conn->viewer_session->current_trace_chunk ?
1769 std::to_string(viewer_session_chunk_id).c_str() :
1770 "None");
bb1dcf01
MD
1771 } else if (viewer_stream_one_rotation_behind && !rstream->trace_chunk) {
1772 DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate",
28ab034a
JG
1773 vstream->stream_file.trace_chunk ?
1774 std::to_string(stream_file_chunk_id).c_str() :
1775 "None",
1776 conn->viewer_session->current_trace_chunk ?
1777 std::to_string(viewer_session_chunk_id).c_str() :
1778 "None");
bb1dcf01
MD
1779 } else {
1780 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
28ab034a
JG
1781 vstream->stream_file.trace_chunk ?
1782 std::to_string(stream_file_chunk_id).c_str() :
1783 "None",
1784 conn->viewer_session->current_trace_chunk ?
1785 std::to_string(viewer_session_chunk_id).c_str() :
1786 "None");
bb1dcf01 1787
80516611 1788 viewer_stream_rotate_to_trace_chunk(vstream,
28ab034a
JG
1789 conn->viewer_session->current_trace_chunk);
1790 vstream->last_seen_rotation_count = rstream->completed_rotation_count;
d3e2ba59
JD
1791 }
1792
878c34cf 1793 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
878c34cf 1794 if (ret < 0) {
7591bab1 1795 goto error_put;
878c34cf
DG
1796 } else if (ret == 1) {
1797 /*
7591bab1
MD
1798 * We have no index to send and check_index_status has populated
1799 * viewer_index's status.
878c34cf 1800 */
d3e2ba59
JD
1801 goto send_reply;
1802 }
7591bab1 1803 /* At this point, ret is 0 thus we will be able to read the index. */
a0377dfe 1804 LTTNG_ASSERT(!ret);
d3e2ba59 1805
b0d240a2
MD
1806 /* Try to open an index if one is needed for that stream. */
1807 ret = try_open_index(vstream, rstream);
1808 if (ret == -ENOENT) {
28ab034a 1809 if (rstream->closed) {
7e4fb89b
FD
1810 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1811 DBG("Cannot open index for stream id %" PRIu64
28ab034a
JG
1812 "stream is closed, returning status=%s",
1813 (uint64_t) be64toh(request_index.stream_id),
1814 lttng_viewer_next_index_return_code_str(
1815 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2 1816 goto send_reply;
28ab034a 1817 } else {
7e4fb89b 1818 viewer_index.status = LTTNG_VIEWER_INDEX_RETRY;
28ab034a
JG
1819 DBG("Cannot open index for stream id %" PRIu64 ", returning status=%s",
1820 (uint64_t) be64toh(request_index.stream_id),
1821 lttng_viewer_next_index_return_code_str(
1822 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2 1823 goto send_reply;
28ab034a 1824 }
b0d240a2
MD
1825 }
1826 if (ret < 0) {
7e4fb89b 1827 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
1828 ERR("Error opening index for stream id %" PRIu64 ", returning status=%s",
1829 (uint64_t) be64toh(request_index.stream_id),
1830 lttng_viewer_next_index_return_code_str(
1831 (enum lttng_viewer_next_index_return_code) viewer_index.status));
b0d240a2
MD
1832 goto send_reply;
1833 }
1834
7591bab1
MD
1835 /*
1836 * vstream->stream_fd may be NULL if it has been closed by
1837 * tracefile rotation, or if we are at the beginning of the
1838 * stream. We open the data stream file here to protect against
1839 * overwrite caused by tracefile rotation (in association with
1840 * unlink performed before overwrite).
1841 */
8bb66c3c 1842 if (!vstream->stream_file.handle) {
ebb29c10 1843 char file_path[LTTNG_PATH_MAX];
8bb66c3c 1844 struct fs_handle *fs_handle;
ebb29c10
JG
1845
1846 ret = utils_stream_file_path(rstream->path_name,
28ab034a
JG
1847 rstream->channel_name,
1848 rstream->tracefile_size,
1849 vstream->current_tracefile_id,
1850 NULL,
1851 file_path,
1852 sizeof(file_path));
7591bab1
MD
1853 if (ret < 0) {
1854 goto error_put;
1855 }
ebb29c10 1856
3ff5c5db
MD
1857 /*
1858 * It is possible the the file we are trying to open is
1859 * missing if the stream has been closed (application exits with
1860 * per-pid buffers) and a clear command has been performed.
1861 */
8bb66c3c 1862 status = lttng_trace_chunk_open_fs_handle(
28ab034a 1863 vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true);
ebb29c10 1864 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
28ab034a 1865 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE && rstream->closed) {
7e4fb89b
FD
1866 viewer_index.status = LTTNG_VIEWER_INDEX_HUP;
1867 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
28ab034a
JG
1868 ", returning status=%s",
1869 (uint64_t) be64toh(request_index.stream_id),
1870 lttng_viewer_next_index_return_code_str(
1871 (enum lttng_viewer_next_index_return_code)
1872 viewer_index.status));
b0d240a2
MD
1873 goto send_reply;
1874 }
ebb29c10 1875 PERROR("Failed to open trace file for viewer stream");
7591bab1
MD
1876 goto error_put;
1877 }
8bb66c3c 1878 vstream->stream_file.handle = fs_handle;
d3e2ba59
JD
1879 }
1880
f04a971b 1881 ret = check_new_streams(conn);
4a9daf17 1882 if (ret < 0) {
7e4fb89b
FD
1883 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
1884 ERR("Error checking for new streams before sending new index to stream id %" PRIu64
28ab034a
JG
1885 ", returning status=%s",
1886 (uint64_t) be64toh(request_index.stream_id),
1887 lttng_viewer_next_index_return_code_str(
1888 (enum lttng_viewer_next_index_return_code) viewer_index.status));
7591bab1 1889 goto send_reply;
4a9daf17
JD
1890 } else if (ret == 1) {
1891 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1892 }
1893
f8f3885c
MD
1894 ret = lttng_index_file_read(vstream->index_file, &packet_index);
1895 if (ret) {
7e4fb89b 1896 viewer_index.status = LTTNG_VIEWER_INDEX_ERR;
28ab034a
JG
1897 ERR("Relay error reading index file for stream id %" PRIu64 ", returning status=%s",
1898 (uint64_t) be64toh(request_index.stream_id),
1899 lttng_viewer_next_index_return_code_str(
1900 (enum lttng_viewer_next_index_return_code) viewer_index.status));
6b6b9a5a 1901 goto send_reply;
d3e2ba59 1902 } else {
7e4fb89b 1903 viewer_index.status = LTTNG_VIEWER_INDEX_OK;
28ab034a
JG
1904 DBG("Read index file for stream id %" PRIu64 ", returning status=%s",
1905 (uint64_t) be64toh(request_index.stream_id),
1906 lttng_viewer_next_index_return_code_str(
1907 (enum lttng_viewer_next_index_return_code) viewer_index.status));
a44ca2ca 1908 vstream->index_sent_seqcount++;
d3e2ba59
JD
1909 }
1910
1911 /*
1912 * Indexes are stored in big endian, no need to switch before sending.
1913 */
7591bab1 1914 DBG("Sending viewer index for stream %" PRIu64 " offset %" PRIu64,
28ab034a
JG
1915 rstream->stream_handle,
1916 (uint64_t) be64toh(packet_index.offset));
d3e2ba59
JD
1917 viewer_index.offset = packet_index.offset;
1918 viewer_index.packet_size = packet_index.packet_size;
1919 viewer_index.content_size = packet_index.content_size;
1920 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1921 viewer_index.timestamp_end = packet_index.timestamp_end;
1922 viewer_index.events_discarded = packet_index.events_discarded;
1923 viewer_index.stream_id = packet_index.stream_id;
1924
1925send_reply:
f1883937
MD
1926 if (rstream) {
1927 pthread_mutex_unlock(&rstream->lock);
b6ae2a95 1928 pthread_mutex_unlock(&rstream->trace->session->lock);
f1883937 1929 }
7591bab1
MD
1930
1931 if (metadata_viewer_stream) {
1932 pthread_mutex_lock(&metadata_viewer_stream->stream->lock);
28ab034a
JG
1933 DBG("get next index metadata check: recv %" PRIu64 " sent %" PRIu64,
1934 metadata_viewer_stream->stream->metadata_received,
1935 metadata_viewer_stream->metadata_sent);
7591bab1 1936 if (!metadata_viewer_stream->stream->metadata_received ||
28ab034a
JG
1937 metadata_viewer_stream->stream->metadata_received >
1938 metadata_viewer_stream->metadata_sent) {
7591bab1
MD
1939 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1940 }
1941 pthread_mutex_unlock(&metadata_viewer_stream->stream->lock);
1942 }
1943
d3e2ba59 1944 viewer_index.flags = htobe32(viewer_index.flags);
7e4fb89b 1945 viewer_index.status = htobe32(viewer_index.status);
eea7556c 1946 health_code_update();
2f8f53af 1947
58eb9381 1948 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1949 if (ret < 0) {
7591bab1 1950 goto end;
d3e2ba59 1951 }
eea7556c 1952 health_code_update();
d3e2ba59 1953
9237e6a1
MD
1954 if (vstream) {
1955 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
28ab034a
JG
1956 vstream->index_sent_seqcount,
1957 vstream->stream->stream_handle);
9237e6a1 1958 }
d3e2ba59 1959end:
7591bab1
MD
1960 if (metadata_viewer_stream) {
1961 viewer_stream_put(metadata_viewer_stream);
1962 }
1963 if (vstream) {
1964 viewer_stream_put(vstream);
1965 }
1966 return ret;
1967
1968error_put:
1969 pthread_mutex_unlock(&rstream->lock);
b6ae2a95 1970 pthread_mutex_unlock(&rstream->trace->session->lock);
7591bab1
MD
1971 if (metadata_viewer_stream) {
1972 viewer_stream_put(metadata_viewer_stream);
1973 }
1974 viewer_stream_put(vstream);
d3e2ba59
JD
1975 return ret;
1976}
1977
1978/*
1979 * Send the next index for a stream
1980 *
1981 * Return 0 on success or else a negative value.
1982 */
28ab034a 1983static int viewer_get_packet(struct relay_connection *conn)
d3e2ba59 1984{
b6025e94 1985 int ret;
a5df8828 1986 off_t lseek_ret;
553b2adb 1987 char *reply = NULL;
d3e2ba59 1988 struct lttng_viewer_get_packet get_packet_info;
553b2adb 1989 struct lttng_viewer_trace_packet reply_header;
7591bab1 1990 struct relay_viewer_stream *vstream = NULL;
553b2adb 1991 uint32_t reply_size = sizeof(reply_header);
b6025e94
JR
1992 uint32_t packet_data_len = 0;
1993 ssize_t read_len;
8bb66c3c 1994 uint64_t stream_id;
556d5fae 1995 enum lttng_viewer_get_packet_return_code get_packet_status;
d3e2ba59 1996
eea7556c 1997 health_code_update();
2f8f53af 1998
28ab034a 1999 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
2f8f53af 2000 if (ret < 0) {
d3e2ba59
JD
2001 goto end;
2002 }
eea7556c 2003 health_code_update();
d3e2ba59 2004
0233a6a5 2005 /* From this point on, the error label can be reached. */
553b2adb 2006 memset(&reply_header, 0, sizeof(reply_header));
8bb66c3c 2007 stream_id = (uint64_t) be64toh(get_packet_info.stream_id);
0233a6a5 2008
8bb66c3c 2009 vstream = viewer_stream_get_by_id(stream_id);
7591bab1 2010 if (!vstream) {
556d5fae 2011 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
28ab034a
JG
2012 DBG("Client requested packet of unknown stream id %" PRIu64 ", returning status=%s",
2013 stream_id,
2014 lttng_viewer_get_packet_return_code_str(get_packet_status));
553b2adb 2015 goto send_reply_nolock;
b6025e94
JR
2016 } else {
2017 packet_data_len = be32toh(get_packet_info.len);
553b2adb 2018 reply_size += packet_data_len;
d3e2ba59 2019 }
2a174661 2020
64803277 2021 reply = zmalloc<char>(reply_size);
553b2adb 2022 if (!reply) {
556d5fae
FD
2023 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
2024 PERROR("Falled to allocate reply, returning status=%s",
28ab034a 2025 lttng_viewer_get_packet_return_code_str(get_packet_status));
d3e2ba59
JD
2026 goto error;
2027 }
2028
b6025e94 2029 pthread_mutex_lock(&vstream->stream->lock);
28ab034a
JG
2030 lseek_ret = fs_handle_seek(
2031 vstream->stream_file.handle, be64toh(get_packet_info.offset), SEEK_SET);
a5df8828 2032 if (lseek_ret < 0) {
556d5fae 2033 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
8bb66c3c 2034 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
28ab034a
JG
2035 " to offset %" PRIu64 ", returning status=%s",
2036 stream_id,
2037 (uint64_t) be64toh(get_packet_info.offset),
2038 lttng_viewer_get_packet_return_code_str(get_packet_status));
7591bab1 2039 goto error;
d3e2ba59 2040 }
28ab034a
JG
2041 read_len = fs_handle_read(
2042 vstream->stream_file.handle, reply + sizeof(reply_header), packet_data_len);
b6025e94 2043 if (read_len < packet_data_len) {
556d5fae 2044 get_packet_status = LTTNG_VIEWER_GET_PACKET_ERR;
8bb66c3c 2045 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
28ab034a
JG
2046 ", offset: %" PRIu64 ", returning status=%s",
2047 stream_id,
556d5fae 2048 (uint64_t) be64toh(get_packet_info.offset),
28ab034a 2049 lttng_viewer_get_packet_return_code_str(get_packet_status));
7591bab1 2050 goto error;
d3e2ba59 2051 }
556d5fae
FD
2052
2053 get_packet_status = LTTNG_VIEWER_GET_PACKET_OK;
553b2adb 2054 reply_header.len = htobe32(packet_data_len);
d3e2ba59
JD
2055 goto send_reply;
2056
2057error:
f28ecb10
JG
2058 /* No payload to send on error. */
2059 reply_size = sizeof(reply_header);
d3e2ba59
JD
2060
2061send_reply:
7591bab1
MD
2062 if (vstream) {
2063 pthread_mutex_unlock(&vstream->stream->lock);
2064 }
2065send_reply_nolock:
eea7556c
MD
2066
2067 health_code_update();
2f8f53af 2068
556d5fae 2069 reply_header.status = htobe32(get_packet_status);
553b2adb
JG
2070 if (reply) {
2071 memcpy(reply, &reply_header, sizeof(reply_header));
2072 ret = send_response(conn->sock, reply, reply_size);
2073 } else {
2074 /* No reply to send. */
28ab034a 2075 ret = send_response(conn->sock, &reply_header, reply_size);
553b2adb
JG
2076 }
2077
b6025e94 2078 health_code_update();
d3e2ba59 2079 if (ret < 0) {
b6025e94 2080 PERROR("sendmsg of packet data failed");
7591bab1 2081 goto end_free;
d3e2ba59 2082 }
d3e2ba59 2083
8bb66c3c 2084 DBG("Sent %u bytes for stream %" PRIu64, reply_size, stream_id);
d3e2ba59 2085
7591bab1 2086end_free:
553b2adb 2087 free(reply);
d3e2ba59 2088end:
7591bab1
MD
2089 if (vstream) {
2090 viewer_stream_put(vstream);
2091 }
d3e2ba59
JD
2092 return ret;
2093}
2094
2095/*
2096 * Send the session's metadata
2097 *
2098 * Return 0 on success else a negative value.
2099 */
28ab034a 2100static int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
2101{
2102 int ret = 0;
8bb66c3c 2103 int fd = -1;
d3e2ba59
JD
2104 ssize_t read_len;
2105 uint64_t len = 0;
2106 char *data = NULL;
2107 struct lttng_viewer_get_metadata request;
2108 struct lttng_viewer_metadata_packet reply;
7591bab1 2109 struct relay_viewer_stream *vstream = NULL;
d3e2ba59 2110
a0377dfe 2111 LTTNG_ASSERT(conn);
d3e2ba59 2112
eea7556c 2113 health_code_update();
2f8f53af 2114
58eb9381 2115 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 2116 if (ret < 0) {
d3e2ba59
JD
2117 goto end;
2118 }
eea7556c 2119 health_code_update();
d3e2ba59 2120
53efb85a
MD
2121 memset(&reply, 0, sizeof(reply));
2122
7591bab1
MD
2123 vstream = viewer_stream_get_by_id(be64toh(request.stream_id));
2124 if (!vstream) {
3b463131
MD
2125 /*
2126 * The metadata stream can be closed by a CLOSE command
2127 * just before we attach. It can also be closed by
2128 * per-pid tracing during tracing. Therefore, it is
2129 * possible that we cannot find this viewer stream.
2130 * Reply back to the client with an error if we cannot
2131 * find it.
2132 */
a44ca2ca 2133 DBG("Client requested metadata of unknown stream id %" PRIu64,
28ab034a 2134 (uint64_t) be64toh(request.stream_id));
3b463131 2135 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
7591bab1 2136 goto send_reply;
d3e2ba59 2137 }
7591bab1
MD
2138 pthread_mutex_lock(&vstream->stream->lock);
2139 if (!vstream->stream->is_metadata) {
2140 ERR("Invalid metadata stream");
6763619c
JD
2141 goto error;
2142 }
2143
b0d240a2 2144 if (vstream->metadata_sent >= vstream->stream->metadata_received) {
94f73d08
MD
2145 /*
2146 * The live viewers expect to receive a NO_NEW_METADATA
2147 * status before a stream disappears, otherwise they abort the
2148 * entire live connection when receiving an error status.
b0d240a2
MD
2149 *
2150 * Clear feature resets the metadata_sent to 0 until the
2151 * same metadata is received again.
94f73d08 2152 */
c4e361a4 2153 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
94f73d08
MD
2154 /*
2155 * The live viewer considers a closed 0 byte metadata stream as
2156 * an error.
2157 */
2158 if (vstream->metadata_sent > 0) {
e8b269fa 2159 if (vstream->stream->closed && vstream->stream->no_new_metadata_notified) {
8f141dbd
JG
2160 /*
2161 * Release ownership for the viewer metadata
2162 * stream. Note that this reference is the
2163 * viewer's reference. The vstream still exists
2164 * until the end of the function as
2165 * viewer_stream_get_by_id() took a reference.
2166 */
94f73d08
MD
2167 viewer_stream_put(vstream);
2168 }
8f141dbd 2169
e8b269fa 2170 vstream->stream->no_new_metadata_notified = true;
94f73d08 2171 }
d3e2ba59
JD
2172 goto send_reply;
2173 }
2174
ad8bec24 2175 if (vstream->stream->trace_chunk &&
28ab034a
JG
2176 !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
2177 vstream->stream->trace_chunk)) {
ad8bec24
JG
2178 /* A rotation has occurred on the relay stream. */
2179 DBG("Metadata relay stream and viewer chunk ids differ");
2180
28ab034a
JG
2181 ret = viewer_session_set_trace_chunk_copy(conn->viewer_session,
2182 vstream->stream->trace_chunk);
ad8bec24
JG
2183 if (ret) {
2184 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2185 goto send_reply;
2186 }
2187 }
2188
87250ba1 2189 if (conn->viewer_session->current_trace_chunk &&
28ab034a
JG
2190 !lttng_trace_chunk_ids_equal(conn->viewer_session->current_trace_chunk,
2191 vstream->stream_file.trace_chunk)) {
ad8bec24
JG
2192 bool acquired_reference;
2193
2194 DBG("Viewer session and viewer stream chunk differ: "
28ab034a
JG
2195 "vsession chunk %p vstream chunk %p",
2196 conn->viewer_session->current_trace_chunk,
2197 vstream->stream_file.trace_chunk);
ad8bec24 2198 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
28ab034a
JG
2199 acquired_reference =
2200 lttng_trace_chunk_get(conn->viewer_session->current_trace_chunk);
a0377dfe 2201 LTTNG_ASSERT(acquired_reference);
28ab034a 2202 vstream->stream_file.trace_chunk = conn->viewer_session->current_trace_chunk;
ad8bec24
JG
2203 viewer_stream_close_files(vstream);
2204 }
2205
b0d240a2
MD
2206 len = vstream->stream->metadata_received - vstream->metadata_sent;
2207
87250ba1
JG
2208 if (!vstream->stream_file.trace_chunk) {
2209 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2210 len = 0;
2211 goto send_reply;
28ab034a 2212 } else if (vstream->stream_file.trace_chunk && !vstream->stream_file.handle && len > 0) {
87250ba1
JG
2213 /*
2214 * Either this is the first time the metadata file is read, or a
2215 * rotation of the corresponding relay stream has occurred.
2216 */
8bb66c3c 2217 struct fs_handle *fs_handle;
ebb29c10
JG
2218 char file_path[LTTNG_PATH_MAX];
2219 enum lttng_trace_chunk_status status;
2220 struct relay_stream *rstream = vstream->stream;
2221
2222 ret = utils_stream_file_path(rstream->path_name,
28ab034a
JG
2223 rstream->channel_name,
2224 rstream->tracefile_size,
2225 vstream->current_tracefile_id,
2226 NULL,
2227 file_path,
2228 sizeof(file_path));
d3e2ba59
JD
2229 if (ret < 0) {
2230 goto error;
2231 }
ebb29c10 2232
3ff5c5db
MD
2233 /*
2234 * It is possible the the metadata file we are trying to open is
2235 * missing if the stream has been closed (application exits with
2236 * per-pid buffers) and a clear command has been performed.
2237 */
8bb66c3c 2238 status = lttng_trace_chunk_open_fs_handle(
28ab034a 2239 vstream->stream_file.trace_chunk, file_path, O_RDONLY, 0, &fs_handle, true);
ebb29c10 2240 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
b0d240a2
MD
2241 if (status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
2242 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
2243 len = 0;
2244 if (vstream->stream->closed) {
2245 viewer_stream_put(vstream);
2246 }
2247 goto send_reply;
2248 }
ebb29c10 2249 PERROR("Failed to open metadata file for viewer stream");
d3e2ba59
JD
2250 goto error;
2251 }
8bb66c3c 2252 vstream->stream_file.handle = fs_handle;
ad8bec24
JG
2253
2254 if (vstream->metadata_sent != 0) {
2255 /*
2256 * The client does not expect to receive any metadata
2257 * it has received and metadata files in successive
2258 * chunks must be a strict superset of one another.
2259 *
2260 * Skip the first `metadata_sent` bytes to ensure
2261 * they are not sent a second time to the client.
2262 *
2263 * Baring a block layer error or an internal error,
2264 * this seek should not fail as
2265 * `vstream->stream->metadata_received` is reset when
2266 * a relay stream is rotated. If this is reached, it is
2267 * safe to assume that
2268 * `metadata_received` > `metadata_sent`.
2269 */
28ab034a
JG
2270 const off_t seek_ret =
2271 fs_handle_seek(fs_handle, vstream->metadata_sent, SEEK_SET);
ad8bec24
JG
2272
2273 if (seek_ret < 0) {
2274 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64,
28ab034a 2275 vstream->metadata_sent);
ad8bec24
JG
2276 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
2277 goto send_reply;
2278 }
2279 }
d3e2ba59
JD
2280 }
2281
2282 reply.len = htobe64(len);
64803277 2283 data = zmalloc<char>(len);
d3e2ba59
JD
2284 if (!data) {
2285 PERROR("viewer metadata zmalloc");
2286 goto error;
2287 }
2288
8bb66c3c
JG
2289 fd = fs_handle_get_fd(vstream->stream_file.handle);
2290 if (fd < 0) {
2291 ERR("Failed to restore viewer stream file system handle");
2292 goto error;
2293 }
2294 read_len = lttng_read(fd, data, len);
2295 fs_handle_put_fd(vstream->stream_file.handle);
2296 fd = -1;
6cd525e8 2297 if (read_len < len) {
ad8bec24
JG
2298 if (read_len < 0) {
2299 PERROR("Failed to read metadata file");
2300 goto error;
2301 } else {
2302 /*
2303 * A clear has been performed which prevents the relay
2304 * from sending `len` bytes of metadata.
2305 *
2306 * It is important not to send any metadata if we
2307 * couldn't read all the available metadata in one shot:
2308 * sending partial metadata can cause the client to
2309 * attempt to parse an incomplete (incoherent) metadata
2310 * stream, which would result in an error.
2311 */
28ab034a
JG
2312 const off_t seek_ret =
2313 fs_handle_seek(vstream->stream_file.handle, -read_len, SEEK_CUR);
ad8bec24 2314
cfa1e2c2 2315 DBG("Failed to read metadata: requested = %" PRIu64 ", got = %zd",
28ab034a
JG
2316 len,
2317 read_len);
ad8bec24
JG
2318 read_len = 0;
2319 len = 0;
2320 if (seek_ret < 0) {
2321 PERROR("Failed to restore metadata file position after partial read");
2322 ret = -1;
2323 goto error;
2324 }
2325 }
d3e2ba59 2326 }
7591bab1 2327 vstream->metadata_sent += read_len;
c4e361a4 2328 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
7591bab1 2329
d3e2ba59
JD
2330 goto send_reply;
2331
2332error:
c4e361a4 2333 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
2334
2335send_reply:
eea7556c 2336 health_code_update();
7591bab1
MD
2337 if (vstream) {
2338 pthread_mutex_unlock(&vstream->stream->lock);
2339 }
58eb9381 2340 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 2341 if (ret < 0) {
7591bab1 2342 goto end_free;
d3e2ba59 2343 }
eea7556c 2344 health_code_update();
d3e2ba59
JD
2345
2346 if (len > 0) {
58eb9381 2347 ret = send_response(conn->sock, data, len);
d3e2ba59 2348 if (ret < 0) {
7591bab1 2349 goto end_free;
d3e2ba59
JD
2350 }
2351 }
2352
28ab034a
JG
2353 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64,
2354 len,
2355 (uint64_t) be64toh(request.stream_id));
d3e2ba59
JD
2356
2357 DBG("Metadata sent");
2358
7591bab1 2359end_free:
d3e2ba59 2360 free(data);
d3e2ba59 2361end:
7591bab1
MD
2362 if (vstream) {
2363 viewer_stream_put(vstream);
2364 }
d3e2ba59
JD
2365 return ret;
2366}
2367
c3b7390b
JD
2368/*
2369 * Create a viewer session.
2370 *
2371 * Return 0 on success or else a negative value.
2372 */
28ab034a 2373static int viewer_create_session(struct relay_connection *conn)
c3b7390b
JD
2374{
2375 int ret;
2376 struct lttng_viewer_create_session_response resp;
2377
53efb85a 2378 memset(&resp, 0, sizeof(resp));
c3b7390b 2379 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
7591bab1 2380 conn->viewer_session = viewer_session_create();
c3b7390b
JD
2381 if (!conn->viewer_session) {
2382 ERR("Allocation viewer session");
2383 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
2384 goto send_reply;
2385 }
c3b7390b
JD
2386
2387send_reply:
2388 health_code_update();
2389 ret = send_response(conn->sock, &resp, sizeof(resp));
2390 if (ret < 0) {
2391 goto end;
2392 }
2393 health_code_update();
2394 ret = 0;
2395
2396end:
2397 return ret;
2398}
2399
d62023be
JD
2400/*
2401 * Detach a viewer session.
2402 *
2403 * Return 0 on success or else a negative value.
2404 */
28ab034a 2405static int viewer_detach_session(struct relay_connection *conn)
d62023be
JD
2406{
2407 int ret;
2408 struct lttng_viewer_detach_session_response response;
2409 struct lttng_viewer_detach_session_request request;
2410 struct relay_session *session = NULL;
2411 uint64_t viewer_session_to_close;
2412
a0377dfe 2413 LTTNG_ASSERT(conn);
d62023be
JD
2414
2415 health_code_update();
2416
2417 /* Receive the request from the connected client. */
2418 ret = recv_request(conn->sock, &request, sizeof(request));
2419 if (ret < 0) {
2420 goto end;
2421 }
2422 viewer_session_to_close = be64toh(request.session_id);
2423
2424 if (!conn->viewer_session) {
2425 DBG("Client trying to detach before creating a live viewer session");
2426 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2427 goto send_reply;
2428 }
2429
2430 health_code_update();
2431
2432 memset(&response, 0, sizeof(response));
2433 DBG("Detaching from session ID %" PRIu64, viewer_session_to_close);
2434
2435 session = session_get_by_id(be64toh(request.session_id));
2436 if (!session) {
28ab034a 2437 DBG("Relay session %" PRIu64 " not found", (uint64_t) be64toh(request.session_id));
d62023be
JD
2438 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK);
2439 goto send_reply;
2440 }
2441
2442 ret = viewer_session_is_attached(conn->viewer_session, session);
2443 if (ret != 1) {
2444 DBG("Not attached to this session");
2445 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR);
2446 goto send_reply_put;
2447 }
2448
2449 viewer_session_close_one_session(conn->viewer_session, session);
2450 response.status = htobe32(LTTNG_VIEWER_DETACH_SESSION_OK);
2451 DBG("Session %" PRIu64 " detached.", viewer_session_to_close);
2452
2453send_reply_put:
2454 session_put(session);
2455
2456send_reply:
2457 health_code_update();
2458 ret = send_response(conn->sock, &response, sizeof(response));
2459 if (ret < 0) {
2460 goto end;
2461 }
2462 health_code_update();
2463 ret = 0;
2464
2465end:
2466 return ret;
2467}
c3b7390b 2468
d3e2ba59
JD
2469/*
2470 * live_relay_unknown_command: send -1 if received unknown command
2471 */
28ab034a 2472static void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
2473{
2474 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 2475
53efb85a 2476 memset(&reply, 0, sizeof(reply));
d3e2ba59 2477 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 2478 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
2479}
2480
2481/*
2482 * Process the commands received on the control socket
2483 */
28ab034a 2484static int process_control(struct lttng_viewer_cmd *recv_hdr, struct relay_connection *conn)
d3e2ba59
JD
2485{
2486 int ret = 0;
28ab034a 2487 lttng_viewer_command cmd = (lttng_viewer_command) be32toh(recv_hdr->cmd);
2f8f53af
DG
2488
2489 /*
7201079f
FD
2490 * Make sure we've done the version check before any command other then
2491 * a new client connection.
2f8f53af 2492 */
7201079f
FD
2493 if (cmd != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
2494 ERR("Viewer on connection %d requested %s command before version check",
28ab034a
JG
2495 conn->sock->fd,
2496 lttng_viewer_command_str(cmd));
2f8f53af
DG
2497 ret = -1;
2498 goto end;
2499 }
d3e2ba59 2500
7201079f 2501 DBG("Processing %s viewer command from connection %d",
28ab034a
JG
2502 lttng_viewer_command_str(cmd),
2503 conn->sock->fd);
7201079f
FD
2504
2505 switch (cmd) {
c4e361a4 2506 case LTTNG_VIEWER_CONNECT:
58eb9381 2507 ret = viewer_connect(conn);
d3e2ba59 2508 break;
c4e361a4 2509 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 2510 ret = viewer_list_sessions(conn);
d3e2ba59 2511 break;
c4e361a4 2512 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 2513 ret = viewer_attach_session(conn);
d3e2ba59 2514 break;
c4e361a4 2515 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 2516 ret = viewer_get_next_index(conn);
d3e2ba59 2517 break;
c4e361a4 2518 case LTTNG_VIEWER_GET_PACKET:
58eb9381 2519 ret = viewer_get_packet(conn);
d3e2ba59 2520 break;
c4e361a4 2521 case LTTNG_VIEWER_GET_METADATA:
58eb9381 2522 ret = viewer_get_metadata(conn);
d3e2ba59 2523 break;
c4e361a4 2524 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 2525 ret = viewer_get_new_streams(conn);
80e8027a 2526 break;
c3b7390b
JD
2527 case LTTNG_VIEWER_CREATE_SESSION:
2528 ret = viewer_create_session(conn);
2529 break;
d62023be
JD
2530 case LTTNG_VIEWER_DETACH_SESSION:
2531 ret = viewer_detach_session(conn);
2532 break;
d3e2ba59 2533 default:
28ab034a 2534 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
58eb9381 2535 live_relay_unknown_command(conn);
d3e2ba59
JD
2536 ret = -1;
2537 goto end;
2538 }
2539
2540end:
2541 return ret;
2542}
2543
28ab034a 2544static void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
2545{
2546 int ret;
2547
58eb9381 2548 (void) lttng_poll_del(events, pollfd);
d3e2ba59 2549
28ab034a
JG
2550 ret = fd_tracker_close_unsuspendable_fd(
2551 the_fd_tracker, &pollfd, 1, fd_tracker_util_close_fd, NULL);
d3e2ba59
JD
2552 if (ret < 0) {
2553 ERR("Closing pollfd %d", pollfd);
2554 }
2555}
2556
d3e2ba59
JD
2557/*
2558 * This thread does the actual work
2559 */
28ab034a 2560static void *thread_worker(void *data __attribute__((unused)))
d3e2ba59
JD
2561{
2562 int ret, err = -1;
2563 uint32_t nb_fd;
d3e2ba59 2564 struct lttng_poll_event events;
7591bab1 2565 struct lttng_ht *viewer_connections_ht;
d3e2ba59
JD
2566 struct lttng_ht_iter iter;
2567 struct lttng_viewer_cmd recv_hdr;
302d8906 2568 struct relay_connection *destroy_conn;
d3e2ba59
JD
2569
2570 DBG("[thread] Live viewer relay worker started");
2571
2572 rcu_register_thread();
2573
eea7556c
MD
2574 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
2575
9b5e0863
MD
2576 if (testpoint(relayd_thread_live_worker)) {
2577 goto error_testpoint;
2578 }
2579
d3e2ba59 2580 /* table of connections indexed on socket */
7591bab1
MD
2581 viewer_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2582 if (!viewer_connections_ht) {
2583 goto viewer_connections_ht_error;
d3e2ba59
JD
2584 }
2585
28ab034a 2586 ret = create_named_thread_poll_set(&events, 2, "Live viewer worker thread epoll");
d3e2ba59
JD
2587 if (ret < 0) {
2588 goto error_poll_create;
2589 }
2590
58eb9381 2591 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
2592 if (ret < 0) {
2593 goto error;
2594 }
2595
2596restart:
2597 while (1) {
2598 int i;
2599
eea7556c
MD
2600 health_code_update();
2601
d3e2ba59
JD
2602 /* Infinite blocking call, waiting for transmission */
2603 DBG3("Relayd live viewer worker thread polling...");
eea7556c 2604 health_poll_entry();
d3e2ba59 2605 ret = lttng_poll_wait(&events, -1);
eea7556c 2606 health_poll_exit();
d3e2ba59
JD
2607 if (ret < 0) {
2608 /*
2609 * Restart interrupted system call.
2610 */
2611 if (errno == EINTR) {
2612 goto restart;
2613 }
2614 goto error;
2615 }
2616
2617 nb_fd = ret;
2618
2619 /*
2620 * Process control. The control connection is prioritised so we don't
2621 * starve it with high throughput tracing data on the data
2622 * connection.
2623 */
2624 for (i = 0; i < nb_fd; i++) {
2625 /* Fetch once the poll data */
8a00688e
MJ
2626 const auto revents = LTTNG_POLL_GETEV(&events, i);
2627 const auto pollfd = LTTNG_POLL_GETFD(&events, i);
d3e2ba59 2628
eea7556c
MD
2629 health_code_update();
2630
8a00688e
MJ
2631 /* Activity on thread quit pipe, exiting. */
2632 if (relayd_is_thread_quit_pipe(pollfd)) {
2633 DBG("Activity on thread quit pipe");
d3e2ba59
JD
2634 err = 0;
2635 goto exit;
2636 }
2637
7591bab1 2638 /* Inspect the relay conn pipe for new connection. */
58eb9381 2639 if (pollfd == live_conn_pipe[0]) {
03e43155 2640 if (revents & LPOLLIN) {
302d8906
JG
2641 struct relay_connection *conn;
2642
28ab034a 2643 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
d3e2ba59
JD
2644 if (ret < 0) {
2645 goto error;
2646 }
28ab034a
JG
2647 ret = lttng_poll_add(
2648 &events, conn->sock->fd, LPOLLIN | LPOLLRDHUP);
73039936
FD
2649 if (ret) {
2650 ERR("Failed to add new live connection file descriptor to poll set");
2651 goto error;
2652 }
7591bab1
MD
2653 connection_ht_add(viewer_connections_ht, conn);
2654 DBG("Connection socket %d added to poll", conn->sock->fd);
03e43155
MD
2655 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2656 ERR("Relay live pipe error");
2657 goto error;
2658 } else {
28ab034a
JG
2659 ERR("Unexpected poll events %u for sock %d",
2660 revents,
2661 pollfd);
03e43155 2662 goto error;
d3e2ba59 2663 }
58eb9381 2664 } else {
7591bab1 2665 /* Connection activity. */
302d8906
JG
2666 struct relay_connection *conn;
2667
7591bab1
MD
2668 conn = connection_get_by_sock(viewer_connections_ht, pollfd);
2669 if (!conn) {
2670 continue;
2671 }
58eb9381 2672
03e43155 2673 if (revents & LPOLLIN) {
28ab034a
JG
2674 ret = conn->sock->ops->recvmsg(
2675 conn->sock, &recv_hdr, sizeof(recv_hdr), 0);
d3e2ba59 2676 if (ret <= 0) {
7591bab1 2677 /* Connection closed. */
58eb9381 2678 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2679 /* Put "create" ownership reference. */
2680 connection_put(conn);
58eb9381 2681 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 2682 } else {
58eb9381 2683 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
2684 if (ret < 0) {
2685 /* Clear the session on error. */
58eb9381 2686 cleanup_connection_pollfd(&events, pollfd);
7591bab1
MD
2687 /* Put "create" ownership reference. */
2688 connection_put(conn);
28ab034a
JG
2689 DBG("Viewer connection closed with %d",
2690 pollfd);
d3e2ba59
JD
2691 }
2692 }
03e43155
MD
2693 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2694 cleanup_connection_pollfd(&events, pollfd);
2695 /* Put "create" ownership reference. */
2696 connection_put(conn);
2697 } else {
28ab034a
JG
2698 ERR("Unexpected poll events %u for sock %d",
2699 revents,
2700 pollfd);
03e43155
MD
2701 connection_put(conn);
2702 goto error;
d3e2ba59 2703 }
7591bab1
MD
2704 /* Put local "get_by_sock" reference. */
2705 connection_put(conn);
d3e2ba59
JD
2706 }
2707 }
2708 }
2709
2710exit:
2711error:
35bc1f58 2712 (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
d3e2ba59 2713
71efa8ef 2714 /* Cleanup remaining connection object. */
d3e2ba59 2715 rcu_read_lock();
28ab034a 2716 cds_lfht_for_each_entry (viewer_connections_ht->ht, &iter.iter, destroy_conn, sock_n.node) {
eea7556c 2717 health_code_update();
7591bab1 2718 connection_put(destroy_conn);
d3e2ba59
JD
2719 }
2720 rcu_read_unlock();
2721error_poll_create:
7591bab1
MD
2722 lttng_ht_destroy(viewer_connections_ht);
2723viewer_connections_ht_error:
58eb9381 2724 /* Close relay conn pipes */
87bcbe91 2725 (void) fd_tracker_util_pipe_close(the_fd_tracker, live_conn_pipe);
d3e2ba59
JD
2726 if (err) {
2727 DBG("Viewer worker thread exited with error");
2728 }
2729 DBG("Viewer worker thread cleanup complete");
9b5e0863 2730error_testpoint:
eea7556c
MD
2731 if (err) {
2732 health_error();
2733 ERR("Health error occurred in %s", __func__);
2734 }
2735 health_unregister(health_relayd);
b4aacfdc
MD
2736 if (lttng_relay_stop_threads()) {
2737 ERR("Error stopping threads");
178a0557 2738 }
d3e2ba59
JD
2739 rcu_unregister_thread();
2740 return NULL;
2741}
2742
2743/*
2744 * Create the relay command pipe to wake thread_manage_apps.
2745 * Closed in cleanup().
2746 */
58eb9381 2747static int create_conn_pipe(void)
d3e2ba59 2748{
28ab034a
JG
2749 return fd_tracker_util_pipe_open_cloexec(
2750 the_fd_tracker, "Live connection pipe", live_conn_pipe);
178a0557 2751}
d3e2ba59 2752
178a0557 2753int relayd_live_join(void)
d3e2ba59 2754{
178a0557 2755 int ret, retval = 0;
d3e2ba59
JD
2756 void *status;
2757
d3e2ba59 2758 ret = pthread_join(live_listener_thread, &status);
178a0557
MD
2759 if (ret) {
2760 errno = ret;
d3e2ba59 2761 PERROR("pthread_join live listener");
178a0557 2762 retval = -1;
d3e2ba59
JD
2763 }
2764
2765 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2766 if (ret) {
2767 errno = ret;
d3e2ba59 2768 PERROR("pthread_join live worker");
178a0557 2769 retval = -1;
d3e2ba59
JD
2770 }
2771
2772 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2773 if (ret) {
2774 errno = ret;
d3e2ba59 2775 PERROR("pthread_join live dispatcher");
178a0557 2776 retval = -1;
d3e2ba59
JD
2777 }
2778
178a0557 2779 cleanup_relayd_live();
d3e2ba59 2780
178a0557 2781 return retval;
d3e2ba59
JD
2782}
2783
2784/*
2785 * main
2786 */
7591bab1 2787int relayd_live_create(struct lttng_uri *uri)
d3e2ba59 2788{
178a0557 2789 int ret = 0, retval = 0;
d3e2ba59
JD
2790 void *status;
2791 int is_root;
2792
178a0557
MD
2793 if (!uri) {
2794 retval = -1;
2795 goto exit_init_data;
2796 }
d3e2ba59
JD
2797 live_uri = uri;
2798
d3e2ba59
JD
2799 /* Check if daemon is UID = 0 */
2800 is_root = !getuid();
2801
2802 if (!is_root) {
2803 if (live_uri->port < 1024) {
2804 ERR("Need to be root to use ports < 1024");
178a0557
MD
2805 retval = -1;
2806 goto exit_init_data;
d3e2ba59
JD
2807 }
2808 }
2809
2810 /* Setup the thread apps communication pipe. */
178a0557
MD
2811 if (create_conn_pipe()) {
2812 retval = -1;
2813 goto exit_init_data;
d3e2ba59
JD
2814 }
2815
2816 /* Init relay command queue. */
8bdee6e2 2817 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
d3e2ba59
JD
2818
2819 /* Set up max poll set size */
25b397f9
MD
2820 if (lttng_poll_set_max_size()) {
2821 retval = -1;
2822 goto exit_init_data;
2823 }
d3e2ba59
JD
2824
2825 /* Setup the dispatcher thread */
28ab034a
JG
2826 ret = pthread_create(
2827 &live_dispatcher_thread, default_pthread_attr(), thread_dispatcher, (void *) NULL);
178a0557
MD
2828 if (ret) {
2829 errno = ret;
d3e2ba59 2830 PERROR("pthread_create viewer dispatcher");
178a0557
MD
2831 retval = -1;
2832 goto exit_dispatcher_thread;
d3e2ba59
JD
2833 }
2834
2835 /* Setup the worker thread */
28ab034a 2836 ret = pthread_create(&live_worker_thread, default_pthread_attr(), thread_worker, NULL);
178a0557
MD
2837 if (ret) {
2838 errno = ret;
d3e2ba59 2839 PERROR("pthread_create viewer worker");
178a0557
MD
2840 retval = -1;
2841 goto exit_worker_thread;
d3e2ba59
JD
2842 }
2843
2844 /* Setup the listener thread */
28ab034a
JG
2845 ret = pthread_create(
2846 &live_listener_thread, default_pthread_attr(), thread_listener, (void *) NULL);
178a0557
MD
2847 if (ret) {
2848 errno = ret;
d3e2ba59 2849 PERROR("pthread_create viewer listener");
178a0557
MD
2850 retval = -1;
2851 goto exit_listener_thread;
d3e2ba59
JD
2852 }
2853
178a0557
MD
2854 /*
2855 * All OK, started all threads.
2856 */
2857 return retval;
2858
9911d21b
JG
2859 /*
2860 * Join on the live_listener_thread should anything be added after
2861 * the live_listener thread's creation.
2862 */
d3e2ba59 2863
178a0557 2864exit_listener_thread:
d3e2ba59 2865
d3e2ba59 2866 ret = pthread_join(live_worker_thread, &status);
178a0557
MD
2867 if (ret) {
2868 errno = ret;
d3e2ba59 2869 PERROR("pthread_join live worker");
178a0557 2870 retval = -1;
d3e2ba59 2871 }
178a0557 2872exit_worker_thread:
d3e2ba59 2873
d3e2ba59 2874 ret = pthread_join(live_dispatcher_thread, &status);
178a0557
MD
2875 if (ret) {
2876 errno = ret;
d3e2ba59 2877 PERROR("pthread_join live dispatcher");
178a0557 2878 retval = -1;
d3e2ba59 2879 }
178a0557 2880exit_dispatcher_thread:
d3e2ba59 2881
178a0557
MD
2882exit_init_data:
2883 cleanup_relayd_live();
d3e2ba59 2884
178a0557 2885 return retval;
d3e2ba59 2886}
This page took 0.244561 seconds and 4 git commands to generate.