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