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