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