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