Refactor consumerd main/cleanup
[lttng-tools.git] / src / bin / lttng-relayd / live.c
CommitLineData
d3e2ba59
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
6c1c0768 20#define _LGPL_SOURCE
d3e2ba59
JD
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
30#include <sys/mount.h>
31#include <sys/resource.h>
32#include <sys/socket.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/wait.h>
36#include <inttypes.h>
37#include <urcu/futex.h>
38#include <urcu/uatomic.h>
39#include <unistd.h>
40#include <fcntl.h>
41#include <config.h>
42
43#include <lttng/lttng.h>
44#include <common/common.h>
45#include <common/compat/poll.h>
46#include <common/compat/socket.h>
f263b7fd 47#include <common/compat/endian.h>
d3e2ba59
JD
48#include <common/defaults.h>
49#include <common/futex.h>
2f8f53af 50#include <common/index/index.h>
d3e2ba59
JD
51#include <common/sessiond-comm/sessiond-comm.h>
52#include <common/sessiond-comm/inet.h>
53#include <common/sessiond-comm/relayd.h>
54#include <common/uri.h>
55#include <common/utils.h>
56
57#include "cmd.h"
58#include "live.h"
59#include "lttng-relayd.h"
d3e2ba59 60#include "utils.h"
eea7556c 61#include "health-relayd.h"
9b5e0863 62#include "testpoint.h"
2f8f53af 63#include "viewer-stream.h"
2a174661
DG
64#include "stream.h"
65#include "session.h"
66#include "ctf-trace.h"
58eb9381 67#include "connection.h"
d3e2ba59
JD
68
69static struct lttng_uri *live_uri;
70
d3e2ba59
JD
71/*
72 * This pipe is used to inform the worker thread that a command is queued and
73 * ready to be processed.
74 */
58eb9381 75static int live_conn_pipe[2] = { -1, -1 };
d3e2ba59
JD
76
77/* Shared between threads */
78static int live_dispatch_thread_exit;
79
80static pthread_t live_listener_thread;
81static pthread_t live_dispatcher_thread;
82static pthread_t live_worker_thread;
83
84/*
85 * Relay command queue.
86 *
87 * The live_thread_listener and live_thread_dispatcher communicate with this
88 * queue.
89 */
58eb9381 90static struct relay_conn_queue viewer_conn_queue;
d3e2ba59
JD
91
92static uint64_t last_relay_viewer_session_id;
93
94/*
95 * Cleanup the daemon
96 */
97static
98void cleanup(void)
99{
100 DBG("Cleaning up");
101
d3e2ba59
JD
102 free(live_uri);
103}
104
2f8f53af
DG
105/*
106 * Receive a request buffer using a given socket, destination allocated buffer
107 * of length size.
108 *
109 * Return the size of the received message or else a negative value on error
110 * with errno being set by recvmsg() syscall.
111 */
112static
113ssize_t recv_request(struct lttcomm_sock *sock, void *buf, size_t size)
114{
115 ssize_t ret;
116
117 assert(sock);
118 assert(buf);
119
120 ret = sock->ops->recvmsg(sock, buf, size, 0);
121 if (ret < 0 || ret != size) {
122 if (ret == 0) {
123 /* Orderly shutdown. Not necessary to print an error. */
124 DBG("Socket %d did an orderly shutdown", sock->fd);
125 } else {
126 ERR("Relay failed to receive request.");
127 }
128 ret = -1;
129 }
130
131 return ret;
132}
133
134/*
135 * Send a response buffer using a given socket, source allocated buffer of
136 * length size.
137 *
138 * Return the size of the sent message or else a negative value on error with
139 * errno being set by sendmsg() syscall.
140 */
141static
142ssize_t send_response(struct lttcomm_sock *sock, void *buf, size_t size)
143{
144 ssize_t ret;
145
146 assert(sock);
147 assert(buf);
148
149 ret = sock->ops->sendmsg(sock, buf, size, 0);
150 if (ret < 0) {
151 ERR("Relayd failed to send response.");
152 }
153
154 return ret;
155}
156
157/*
f04a971b
JD
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
2f8f53af
DG
160 *
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
162 * on error.
163 */
164static
f04a971b 165int check_new_streams(struct relay_connection *conn)
2f8f53af 166{
2f8f53af 167 struct relay_session *session;
f04a971b
JD
168 unsigned long current_val;
169 int ret = 0;
2f8f53af 170
f04a971b
JD
171 if (!conn->viewer_session) {
172 goto end;
173 }
174 cds_list_for_each_entry(session,
175 &conn->viewer_session->sessions_head,
176 viewer_session_list) {
177 current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
178 ret = current_val;
179 if (ret == 1) {
180 goto end;
181 }
2f8f53af
DG
182 }
183
f04a971b 184end:
2f8f53af
DG
185 return ret;
186}
187
188/*
189 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
190 * this function should ignore the sent flag or not.
191 *
192 * Return 0 on success or else a negative value.
193 */
194static
195ssize_t send_viewer_streams(struct lttcomm_sock *sock,
196 struct relay_session *session, unsigned int ignore_sent_flag)
197{
198 ssize_t ret;
199 struct lttng_viewer_stream send_stream;
200 struct lttng_ht_iter iter;
201 struct relay_viewer_stream *vstream;
202
203 assert(session);
204
205 rcu_read_lock();
206
207 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
208 stream_n.node) {
2a174661
DG
209 struct ctf_trace *ctf_trace;
210
2f8f53af
DG
211 health_code_update();
212
213 /* Ignore if not the same session. */
214 if (vstream->session_id != session->id ||
215 (!ignore_sent_flag && vstream->sent_flag)) {
216 continue;
217 }
218
2a174661
DG
219 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
220 vstream->path_name);
221 assert(ctf_trace);
222
2f8f53af 223 send_stream.id = htobe64(vstream->stream_handle);
2a174661 224 send_stream.ctf_trace_id = htobe64(ctf_trace->id);
2f8f53af
DG
225 send_stream.metadata_flag = htobe32(vstream->metadata_flag);
226 strncpy(send_stream.path_name, vstream->path_name,
227 sizeof(send_stream.path_name));
228 strncpy(send_stream.channel_name, vstream->channel_name,
229 sizeof(send_stream.channel_name));
230
231 DBG("Sending stream %" PRIu64 " to viewer", vstream->stream_handle);
232 ret = send_response(sock, &send_stream, sizeof(send_stream));
233 if (ret < 0) {
234 goto end_unlock;
235 }
236 vstream->sent_flag = 1;
237 }
238
239 ret = 0;
240
241end_unlock:
242 rcu_read_unlock();
243 return ret;
244}
245
246/*
247 * Create every viewer stream possible for the given session with the seek
248 * type. Three counters *can* be return which are in order the total amount of
249 * viewer stream of the session, the number of unsent stream and the number of
250 * stream created. Those counters can be NULL and thus will be ignored.
251 *
252 * Return 0 on success or else a negative value.
253 */
254static
255int make_viewer_streams(struct relay_session *session,
256 enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
257 uint32_t *nb_created)
258{
259 int ret;
2f8f53af 260 struct lttng_ht_iter iter;
2a174661 261 struct ctf_trace *ctf_trace;
2f8f53af
DG
262
263 assert(session);
264
265 /*
266 * This is to make sure we create viewer streams for a full received
267 * channel. For instance, if we have 8 streams for a channel that are
268 * concurrently being flagged ready, we can end up creating just a subset
269 * of the 8 streams (the ones that are flagged). This lock avoids this
270 * limbo state.
271 */
272 pthread_mutex_lock(&session->viewer_ready_lock);
273
274 /*
275 * Create viewer streams for relay streams that are ready to be used for a
276 * the given session id only.
277 */
2a174661
DG
278 rcu_read_lock();
279 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
280 node.node) {
281 struct relay_stream *stream;
2f8f53af
DG
282
283 health_code_update();
284
2a174661 285 if (ctf_trace->invalid_flag) {
2f8f53af
DG
286 continue;
287 }
288
2a174661
DG
289 cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) {
290 struct relay_viewer_stream *vstream;
291
292 if (!stream->viewer_ready) {
293 continue;
294 }
295
296 vstream = viewer_stream_find_by_id(stream->stream_handle);
2f8f53af 297 if (!vstream) {
2a174661
DG
298 vstream = viewer_stream_create(stream, seek_t, ctf_trace);
299 if (!vstream) {
300 ret = -1;
301 goto error_unlock;
302 }
303 /* Acquire reference to ctf_trace. */
304 ctf_trace_get_ref(ctf_trace);
305
306 if (nb_created) {
307 /* Update number of created stream counter. */
308 (*nb_created)++;
309 }
310 } else if (!vstream->sent_flag && nb_unsent) {
311 /* Update number of unsent stream counter. */
312 (*nb_unsent)++;
2f8f53af 313 }
2a174661
DG
314 /* Update number of total stream counter. */
315 if (nb_total) {
316 (*nb_total)++;
2f8f53af 317 }
2f8f53af
DG
318 }
319 }
320
321 ret = 0;
322
323error_unlock:
2a174661 324 rcu_read_unlock();
2f8f53af
DG
325 pthread_mutex_unlock(&session->viewer_ready_lock);
326 return ret;
327}
328
d3e2ba59
JD
329/*
330 * Write to writable pipe used to notify a thread.
331 */
332static
333int notify_thread_pipe(int wpipe)
334{
6cd525e8 335 ssize_t ret;
d3e2ba59 336
6cd525e8
MD
337 ret = lttng_write(wpipe, "!", 1);
338 if (ret < 1) {
d3e2ba59
JD
339 PERROR("write poll pipe");
340 }
341
6cd525e8 342 return (int) ret;
d3e2ba59
JD
343}
344
345/*
346 * Stop all threads by closing the thread quit pipe.
347 */
348static
349void stop_threads(void)
350{
351 int ret;
352
353 /* Stopping all threads */
354 DBG("Terminating all live threads");
bcf4a440 355 ret = notify_thread_pipe(thread_quit_pipe[1]);
d3e2ba59
JD
356 if (ret < 0) {
357 ERR("write error on thread quit pipe");
358 }
359
360 /* Dispatch thread */
361 CMM_STORE_SHARED(live_dispatch_thread_exit, 1);
58eb9381 362 futex_nto1_wake(&viewer_conn_queue.futex);
d3e2ba59
JD
363}
364
d3e2ba59
JD
365/*
366 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
367 */
368static
369int create_thread_poll_set(struct lttng_poll_event *events, int size)
370{
371 int ret;
372
373 if (events == NULL || size == 0) {
374 ret = -1;
375 goto error;
376 }
377
378 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
379 if (ret < 0) {
380 goto error;
381 }
382
383 /* Add quit pipe */
bcf4a440 384 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
d3e2ba59
JD
385 if (ret < 0) {
386 goto error;
387 }
388
389 return 0;
390
391error:
392 return ret;
393}
394
395/*
396 * Check if the thread quit pipe was triggered.
397 *
398 * Return 1 if it was triggered else 0;
399 */
400static
bcf4a440 401int check_thread_quit_pipe(int fd, uint32_t events)
d3e2ba59 402{
bcf4a440 403 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
d3e2ba59
JD
404 return 1;
405 }
406
407 return 0;
408}
409
410/*
411 * Create and init socket from uri.
412 */
413static
414struct lttcomm_sock *init_socket(struct lttng_uri *uri)
415{
416 int ret;
417 struct lttcomm_sock *sock = NULL;
418
419 sock = lttcomm_alloc_sock_from_uri(uri);
420 if (sock == NULL) {
421 ERR("Allocating socket");
422 goto error;
423 }
424
425 ret = lttcomm_create_sock(sock);
426 if (ret < 0) {
427 goto error;
428 }
429 DBG("Listening on sock %d for live", sock->fd);
430
431 ret = sock->ops->bind(sock);
432 if (ret < 0) {
433 goto error;
434 }
435
436 ret = sock->ops->listen(sock, -1);
437 if (ret < 0) {
438 goto error;
439
440 }
441
442 return sock;
443
444error:
445 if (sock) {
446 lttcomm_destroy_sock(sock);
447 }
448 return NULL;
449}
450
451/*
452 * This thread manages the listening for new connections on the network
453 */
454static
455void *thread_listener(void *data)
456{
457 int i, ret, pollfd, err = -1;
d3e2ba59
JD
458 uint32_t revents, nb_fd;
459 struct lttng_poll_event events;
460 struct lttcomm_sock *live_control_sock;
461
462 DBG("[thread] Relay live listener started");
463
eea7556c
MD
464 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_LISTENER);
465
466 health_code_update();
467
d3e2ba59
JD
468 live_control_sock = init_socket(live_uri);
469 if (!live_control_sock) {
470 goto error_sock_control;
471 }
472
fb4d42ab 473 /* Pass 2 as size here for the thread quit pipe and control sockets. */
d3e2ba59
JD
474 ret = create_thread_poll_set(&events, 2);
475 if (ret < 0) {
476 goto error_create_poll;
477 }
478
479 /* Add the control socket */
480 ret = lttng_poll_add(&events, live_control_sock->fd, LPOLLIN | LPOLLRDHUP);
481 if (ret < 0) {
482 goto error_poll_add;
483 }
484
3fd27398
MD
485 lttng_relay_notify_ready();
486
9b5e0863
MD
487 if (testpoint(relayd_thread_live_listener)) {
488 goto error_testpoint;
489 }
490
d3e2ba59 491 while (1) {
eea7556c
MD
492 health_code_update();
493
d3e2ba59
JD
494 DBG("Listener accepting live viewers connections");
495
496restart:
eea7556c 497 health_poll_entry();
d3e2ba59 498 ret = lttng_poll_wait(&events, -1);
eea7556c 499 health_poll_exit();
d3e2ba59
JD
500 if (ret < 0) {
501 /*
502 * Restart interrupted system call.
503 */
504 if (errno == EINTR) {
505 goto restart;
506 }
507 goto error;
508 }
509 nb_fd = ret;
510
511 DBG("Relay new viewer connection received");
512 for (i = 0; i < nb_fd; i++) {
eea7556c
MD
513 health_code_update();
514
d3e2ba59
JD
515 /* Fetch once the poll data */
516 revents = LTTNG_POLL_GETEV(&events, i);
517 pollfd = LTTNG_POLL_GETFD(&events, i);
518
519 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 520 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
521 if (ret) {
522 err = 0;
523 goto exit;
524 }
525
526 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
527 ERR("socket poll error");
528 goto error;
529 } else if (revents & LPOLLIN) {
530 /*
531 * Get allocated in this thread, enqueued to a global queue,
532 * dequeued and freed in the worker thread.
533 */
58eb9381
DG
534 int val = 1;
535 struct relay_connection *new_conn;
d3e2ba59
JD
536 struct lttcomm_sock *newsock;
537
58eb9381
DG
538 new_conn = connection_create();
539 if (!new_conn) {
d3e2ba59
JD
540 goto error;
541 }
542
d3e2ba59
JD
543 newsock = live_control_sock->ops->accept(live_control_sock);
544 if (!newsock) {
545 PERROR("accepting control sock");
58eb9381 546 connection_free(new_conn);
d3e2ba59
JD
547 goto error;
548 }
549 DBG("Relay viewer connection accepted socket %d", newsock->fd);
58eb9381 550
d3e2ba59 551 ret = setsockopt(newsock->fd, SOL_SOCKET, SO_REUSEADDR, &val,
58eb9381 552 sizeof(val));
d3e2ba59
JD
553 if (ret < 0) {
554 PERROR("setsockopt inet");
555 lttcomm_destroy_sock(newsock);
58eb9381 556 connection_free(new_conn);
d3e2ba59
JD
557 goto error;
558 }
58eb9381 559 new_conn->sock = newsock;
d3e2ba59 560
58eb9381 561 /* Enqueue request for the dispatcher thread. */
8bdee6e2
SM
562 cds_wfcq_enqueue(&viewer_conn_queue.head, &viewer_conn_queue.tail,
563 &new_conn->qnode);
d3e2ba59
JD
564
565 /*
58eb9381 566 * Wake the dispatch queue futex. Implicit memory barrier with
8bdee6e2 567 * the exchange in cds_wfcq_enqueue.
d3e2ba59 568 */
58eb9381 569 futex_nto1_wake(&viewer_conn_queue.futex);
d3e2ba59
JD
570 }
571 }
572 }
573
574exit:
575error:
576error_poll_add:
9b5e0863 577error_testpoint:
d3e2ba59
JD
578 lttng_poll_clean(&events);
579error_create_poll:
580 if (live_control_sock->fd >= 0) {
581 ret = live_control_sock->ops->close(live_control_sock);
582 if (ret) {
583 PERROR("close");
584 }
585 }
586 lttcomm_destroy_sock(live_control_sock);
587error_sock_control:
588 if (err) {
eea7556c 589 health_error();
d3e2ba59
JD
590 DBG("Live viewer listener thread exited with error");
591 }
eea7556c 592 health_unregister(health_relayd);
d3e2ba59
JD
593 DBG("Live viewer listener thread cleanup complete");
594 stop_threads();
595 return NULL;
596}
597
598/*
599 * This thread manages the dispatching of the requests to worker threads
600 */
601static
602void *thread_dispatcher(void *data)
603{
6cd525e8
MD
604 int err = -1;
605 ssize_t ret;
8bdee6e2 606 struct cds_wfcq_node *node;
58eb9381 607 struct relay_connection *conn = NULL;
d3e2ba59
JD
608
609 DBG("[thread] Live viewer relay dispatcher started");
610
eea7556c
MD
611 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER);
612
9b5e0863
MD
613 if (testpoint(relayd_thread_live_dispatcher)) {
614 goto error_testpoint;
615 }
616
eea7556c
MD
617 health_code_update();
618
d3e2ba59 619 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit)) {
eea7556c
MD
620 health_code_update();
621
d3e2ba59 622 /* Atomically prepare the queue futex */
58eb9381 623 futex_nto1_prepare(&viewer_conn_queue.futex);
d3e2ba59
JD
624
625 do {
eea7556c
MD
626 health_code_update();
627
d3e2ba59 628 /* Dequeue commands */
8bdee6e2
SM
629 node = cds_wfcq_dequeue_blocking(&viewer_conn_queue.head,
630 &viewer_conn_queue.tail);
d3e2ba59
JD
631 if (node == NULL) {
632 DBG("Woken up but nothing in the live-viewer "
633 "relay command queue");
634 /* Continue thread execution */
635 break;
636 }
58eb9381 637 conn = caa_container_of(node, struct relay_connection, qnode);
d3e2ba59 638 DBG("Dispatching viewer request waiting on sock %d",
58eb9381 639 conn->sock->fd);
d3e2ba59
JD
640
641 /*
642 * Inform worker thread of the new request. This call is blocking
643 * so we can be assured that the data will be read at some point in
644 * time or wait to the end of the world :)
645 */
58eb9381
DG
646 ret = lttng_write(live_conn_pipe[1], &conn, sizeof(conn));
647 if (ret < 0) {
648 PERROR("write conn pipe");
649 connection_destroy(conn);
d3e2ba59
JD
650 goto error;
651 }
652 } while (node != NULL);
653
654 /* Futex wait on queue. Blocking call on futex() */
eea7556c 655 health_poll_entry();
58eb9381 656 futex_nto1_wait(&viewer_conn_queue.futex);
eea7556c 657 health_poll_exit();
d3e2ba59
JD
658 }
659
eea7556c
MD
660 /* Normal exit, no error */
661 err = 0;
662
d3e2ba59 663error:
9b5e0863 664error_testpoint:
eea7556c
MD
665 if (err) {
666 health_error();
667 ERR("Health error occurred in %s", __func__);
668 }
669 health_unregister(health_relayd);
d3e2ba59
JD
670 DBG("Live viewer dispatch thread dying");
671 stop_threads();
672 return NULL;
673}
674
675/*
676 * Establish connection with the viewer and check the versions.
677 *
678 * Return 0 on success or else negative value.
679 */
680static
58eb9381 681int viewer_connect(struct relay_connection *conn)
d3e2ba59
JD
682{
683 int ret;
684 struct lttng_viewer_connect reply, msg;
685
58eb9381 686 assert(conn);
d3e2ba59 687
58eb9381 688 conn->version_check_done = 1;
d3e2ba59 689
eea7556c
MD
690 health_code_update();
691
2f8f53af
DG
692 DBG("Viewer is establishing a connection to the relayd.");
693
58eb9381 694 ret = recv_request(conn->sock, &msg, sizeof(msg));
2f8f53af 695 if (ret < 0) {
d3e2ba59
JD
696 goto end;
697 }
698
eea7556c
MD
699 health_code_update();
700
f46b2ce6 701 memset(&reply, 0, sizeof(reply));
d3e2ba59
JD
702 reply.major = RELAYD_VERSION_COMM_MAJOR;
703 reply.minor = RELAYD_VERSION_COMM_MINOR;
704
705 /* Major versions must be the same */
706 if (reply.major != be32toh(msg.major)) {
2f8f53af
DG
707 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
708 reply.major, be32toh(msg.major));
72180669 709 ret = -1;
d3e2ba59
JD
710 goto end;
711 }
712
58eb9381 713 conn->major = reply.major;
d3e2ba59
JD
714 /* We adapt to the lowest compatible version */
715 if (reply.minor <= be32toh(msg.minor)) {
58eb9381 716 conn->minor = reply.minor;
d3e2ba59 717 } else {
58eb9381 718 conn->minor = be32toh(msg.minor);
d3e2ba59
JD
719 }
720
c4e361a4 721 if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_COMMAND) {
58eb9381 722 conn->type = RELAY_VIEWER_COMMAND;
c4e361a4 723 } else if (be32toh(msg.type) == LTTNG_VIEWER_CLIENT_NOTIFICATION) {
58eb9381 724 conn->type = RELAY_VIEWER_NOTIFICATION;
d3e2ba59
JD
725 } else {
726 ERR("Unknown connection type : %u", be32toh(msg.type));
727 ret = -1;
728 goto end;
729 }
730
731 reply.major = htobe32(reply.major);
732 reply.minor = htobe32(reply.minor);
58eb9381 733 if (conn->type == RELAY_VIEWER_COMMAND) {
d3e2ba59
JD
734 reply.viewer_session_id = htobe64(++last_relay_viewer_session_id);
735 }
eea7556c
MD
736
737 health_code_update();
738
58eb9381 739 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 740 if (ret < 0) {
2f8f53af 741 goto end;
d3e2ba59
JD
742 }
743
eea7556c
MD
744 health_code_update();
745
58eb9381 746 DBG("Version check done using protocol %u.%u", conn->major, conn->minor);
d3e2ba59
JD
747 ret = 0;
748
749end:
750 return ret;
751}
752
753/*
754 * Send the viewer the list of current sessions.
755 *
756 * Return 0 on success or else a negative value.
757 */
758static
58eb9381 759int viewer_list_sessions(struct relay_connection *conn)
d3e2ba59
JD
760{
761 int ret;
762 struct lttng_viewer_list_sessions session_list;
763 unsigned long count;
764 long approx_before, approx_after;
d3e2ba59
JD
765 struct lttng_ht_iter iter;
766 struct lttng_viewer_session send_session;
767 struct relay_session *session;
768
769 DBG("List sessions received");
770
d3e2ba59 771 rcu_read_lock();
58eb9381
DG
772 cds_lfht_count_nodes(conn->sessions_ht->ht, &approx_before, &count,
773 &approx_after);
d3e2ba59
JD
774 session_list.sessions_count = htobe32(count);
775
eea7556c
MD
776 health_code_update();
777
58eb9381 778 ret = send_response(conn->sock, &session_list, sizeof(session_list));
d3e2ba59 779 if (ret < 0) {
d3e2ba59
JD
780 goto end_unlock;
781 }
782
eea7556c
MD
783 health_code_update();
784
58eb9381 785 cds_lfht_for_each_entry(conn->sessions_ht->ht, &iter.iter, session,
2f8f53af 786 session_n.node) {
eea7556c
MD
787 health_code_update();
788
d3e2ba59
JD
789 strncpy(send_session.session_name, session->session_name,
790 sizeof(send_session.session_name));
791 strncpy(send_session.hostname, session->hostname,
792 sizeof(send_session.hostname));
793 send_session.id = htobe64(session->id);
794 send_session.live_timer = htobe32(session->live_timer);
2a174661 795 send_session.clients = htobe32(session->viewer_refcount);
87b576ec 796 send_session.streams = htobe32(session->stream_count);
d3e2ba59 797
eea7556c
MD
798 health_code_update();
799
58eb9381 800 ret = send_response(conn->sock, &send_session, sizeof(send_session));
d3e2ba59 801 if (ret < 0) {
d3e2ba59
JD
802 goto end_unlock;
803 }
804 }
eea7556c
MD
805 health_code_update();
806
d3e2ba59 807 ret = 0;
d3e2ba59
JD
808end_unlock:
809 rcu_read_unlock();
d3e2ba59
JD
810 return ret;
811}
812
6763619c
JD
813/*
814 * Check if a connection is attached to a session.
815 * Return 1 if attached, 0 if not attached, a negative value on error.
816 */
817static
818int session_attached(struct relay_connection *conn, uint64_t session_id)
819{
820 struct relay_session *session;
821 int found = 0;
822
823 if (!conn->viewer_session) {
824 goto end;
825 }
826 cds_list_for_each_entry(session,
827 &conn->viewer_session->sessions_head,
828 viewer_session_list) {
829 if (session->id == session_id) {
830 found = 1;
831 goto end;
832 }
833 }
834
835end:
836 return found;
837}
838
d227d5bd
JD
839/*
840 * Delete all streams for a specific session ID.
841 */
842static void destroy_viewer_streams_by_session(struct relay_session *session)
843{
844 struct relay_viewer_stream *stream;
845 struct lttng_ht_iter iter;
846
847 assert(session);
848
849 rcu_read_lock();
850 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, stream,
851 stream_n.node) {
852 struct ctf_trace *ctf_trace;
853
854 health_code_update();
855 if (stream->session_id != session->id) {
856 continue;
857 }
858
859 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
860 stream->path_name);
861 assert(ctf_trace);
862
863 viewer_stream_delete(stream);
864
865 if (stream->metadata_flag) {
866 ctf_trace->metadata_sent = 0;
867 ctf_trace->viewer_metadata_stream = NULL;
868 }
869
870 viewer_stream_destroy(ctf_trace, stream);
871 }
872 rcu_read_unlock();
873}
874
875static void try_destroy_streams(struct relay_session *session)
876{
877 struct ctf_trace *ctf_trace;
878 struct lttng_ht_iter iter;
879
880 assert(session);
881
882 cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
883 node.node) {
884 /* Attempt to destroy the ctf trace of that session. */
885 ctf_trace_try_destroy(session, ctf_trace);
886 }
887}
888
889/*
890 * Cleanup a session.
891 */
892static void cleanup_session(struct relay_connection *conn,
893 struct relay_session *session)
894{
895 /*
896 * Very important that this is done before destroying the session so we
897 * can put back every viewer stream reference from the ctf_trace.
898 */
899 destroy_viewer_streams_by_session(session);
900 try_destroy_streams(session);
901 cds_list_del(&session->viewer_session_list);
902 session_viewer_try_destroy(conn->sessions_ht, session);
903}
904
80e8027a
JD
905/*
906 * Send the viewer the list of current sessions.
907 */
908static
58eb9381 909int viewer_get_new_streams(struct relay_connection *conn)
80e8027a
JD
910{
911 int ret, send_streams = 0;
2f8f53af 912 uint32_t nb_created = 0, nb_unsent = 0, nb_streams = 0;
80e8027a
JD
913 struct lttng_viewer_new_streams_request request;
914 struct lttng_viewer_new_streams_response response;
80e8027a 915 struct relay_session *session;
6763619c 916 uint64_t session_id;
80e8027a 917
58eb9381 918 assert(conn);
80e8027a
JD
919
920 DBG("Get new streams received");
921
80e8027a
JD
922 health_code_update();
923
2f8f53af 924 /* Receive the request from the connected client. */
58eb9381 925 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 926 if (ret < 0) {
80e8027a
JD
927 goto error;
928 }
6763619c 929 session_id = be64toh(request.session_id);
80e8027a
JD
930
931 health_code_update();
932
53efb85a
MD
933 memset(&response, 0, sizeof(response));
934
80e8027a 935 rcu_read_lock();
6763619c 936 session = session_find_by_id(conn->sessions_ht, session_id);
2f8f53af 937 if (!session) {
6763619c 938 DBG("Relay session %" PRIu64 " not found", session_id);
c4e361a4 939 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
940 goto send_reply;
941 }
942
6763619c 943 if (!session_attached(conn, session_id)) {
80e8027a 944 send_streams = 0;
c4e361a4 945 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR);
80e8027a
JD
946 goto send_reply;
947 }
948
6763619c
JD
949 send_streams = 1;
950 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_OK);
951
c4e361a4 952 ret = make_viewer_streams(session, LTTNG_VIEWER_SEEK_LAST, NULL, &nb_unsent,
2f8f53af
DG
953 &nb_created);
954 if (ret < 0) {
955 goto end_unlock;
956 }
957 /* Only send back the newly created streams with the unsent ones. */
958 nb_streams = nb_created + nb_unsent;
80e8027a
JD
959 response.streams_count = htobe32(nb_streams);
960
4479f682
JD
961 /*
962 * If the session is closed and we have no new streams to send,
963 * it means that the viewer has already received the whole trace
964 * for this session and should now close it.
965 */
966 if (nb_streams == 0 && session->close_flag) {
967 send_streams = 0;
968 response.status = htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP);
6763619c
JD
969 /*
970 * Remove the session from the attached list of the connection
971 * and try to destroy it.
972 */
973 cds_list_del(&session->viewer_session_list);
d227d5bd 974 cleanup_session(conn, session);
4479f682
JD
975 goto send_reply;
976 }
977
80e8027a
JD
978send_reply:
979 health_code_update();
58eb9381 980 ret = send_response(conn->sock, &response, sizeof(response));
80e8027a 981 if (ret < 0) {
80e8027a
JD
982 goto end_unlock;
983 }
984 health_code_update();
985
986 /*
987 * Unknown or empty session, just return gracefully, the viewer knows what
988 * is happening.
989 */
990 if (!send_streams || !nb_streams) {
991 ret = 0;
992 goto end_unlock;
993 }
994
2f8f53af
DG
995 /*
996 * Send stream and *DON'T* ignore the sent flag so every viewer streams
997 * that were not sent from that point will be sent to the viewer.
998 */
58eb9381 999 ret = send_viewer_streams(conn->sock, session, 0);
2f8f53af
DG
1000 if (ret < 0) {
1001 goto end_unlock;
80e8027a
JD
1002 }
1003
80e8027a
JD
1004end_unlock:
1005 rcu_read_unlock();
80e8027a
JD
1006error:
1007 return ret;
1008}
1009
d3e2ba59
JD
1010/*
1011 * Send the viewer the list of current sessions.
1012 */
1013static
58eb9381 1014int viewer_attach_session(struct relay_connection *conn)
d3e2ba59 1015{
2f8f53af
DG
1016 int send_streams = 0;
1017 ssize_t ret;
80e8027a 1018 uint32_t nb_streams = 0;
2f8f53af 1019 enum lttng_viewer_seek seek_type;
d3e2ba59
JD
1020 struct lttng_viewer_attach_session_request request;
1021 struct lttng_viewer_attach_session_response response;
d3e2ba59
JD
1022 struct relay_session *session;
1023
58eb9381 1024 assert(conn);
d3e2ba59 1025
eea7556c
MD
1026 health_code_update();
1027
2f8f53af 1028 /* Receive the request from the connected client. */
58eb9381 1029 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1030 if (ret < 0) {
d3e2ba59
JD
1031 goto error;
1032 }
1033
eea7556c
MD
1034 health_code_update();
1035
53efb85a
MD
1036 memset(&response, 0, sizeof(response));
1037
c3b7390b
JD
1038 if (!conn->viewer_session) {
1039 DBG("Client trying to attach before creating a live viewer session");
1040 response.status = htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION);
1041 goto send_reply;
1042 }
1043
d3e2ba59 1044 rcu_read_lock();
58eb9381
DG
1045 session = session_find_by_id(conn->sessions_ht,
1046 be64toh(request.session_id));
2f8f53af 1047 if (!session) {
d3e2ba59
JD
1048 DBG("Relay session %" PRIu64 " not found",
1049 be64toh(request.session_id));
c4e361a4 1050 response.status = htobe32(LTTNG_VIEWER_ATTACH_UNK);
d3e2ba59
JD
1051 goto send_reply;
1052 }
2a174661
DG
1053 session_viewer_attach(session);
1054 DBG("Attach session ID %" PRIu64 " received", be64toh(request.session_id));
d3e2ba59 1055
2a174661 1056 if (uatomic_read(&session->viewer_refcount) > 1) {
d3e2ba59 1057 DBG("Already a viewer attached");
c4e361a4 1058 response.status = htobe32(LTTNG_VIEWER_ATTACH_ALREADY);
2a174661 1059 session_viewer_detach(session);
d3e2ba59
JD
1060 goto send_reply;
1061 } else if (session->live_timer == 0) {
1062 DBG("Not live session");
c4e361a4 1063 response.status = htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE);
d3e2ba59
JD
1064 goto send_reply;
1065 } else {
d3e2ba59 1066 send_streams = 1;
c4e361a4 1067 response.status = htobe32(LTTNG_VIEWER_ATTACH_OK);
6763619c
JD
1068 cds_list_add(&session->viewer_session_list,
1069 &conn->viewer_session->sessions_head);
d3e2ba59
JD
1070 }
1071
1072 switch (be32toh(request.seek)) {
c4e361a4
JD
1073 case LTTNG_VIEWER_SEEK_BEGINNING:
1074 case LTTNG_VIEWER_SEEK_LAST:
2f8f53af 1075 seek_type = be32toh(request.seek);
d3e2ba59
JD
1076 break;
1077 default:
1078 ERR("Wrong seek parameter");
c4e361a4 1079 response.status = htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR);
d3e2ba59
JD
1080 send_streams = 0;
1081 goto send_reply;
1082 }
1083
2f8f53af
DG
1084 ret = make_viewer_streams(session, seek_type, &nb_streams, NULL, NULL);
1085 if (ret < 0) {
1086 goto end_unlock;
d3e2ba59 1087 }
2f8f53af 1088 response.streams_count = htobe32(nb_streams);
d3e2ba59
JD
1089
1090send_reply:
eea7556c 1091 health_code_update();
58eb9381 1092 ret = send_response(conn->sock, &response, sizeof(response));
d3e2ba59 1093 if (ret < 0) {
d3e2ba59
JD
1094 goto end_unlock;
1095 }
eea7556c 1096 health_code_update();
d3e2ba59
JD
1097
1098 /*
157df586 1099 * Unknown or empty session, just return gracefully, the viewer knows what
d3e2ba59
JD
1100 * is happening.
1101 */
157df586 1102 if (!send_streams || !nb_streams) {
d3e2ba59
JD
1103 ret = 0;
1104 goto end_unlock;
1105 }
1106
2f8f53af 1107 /* Send stream and ignore the sent flag. */
58eb9381 1108 ret = send_viewer_streams(conn->sock, session, 1);
2f8f53af
DG
1109 if (ret < 0) {
1110 goto end_unlock;
d3e2ba59 1111 }
d3e2ba59
JD
1112
1113end_unlock:
1114 rcu_read_unlock();
4a9daf17
JD
1115error:
1116 return ret;
1117}
1118
878c34cf
DG
1119/*
1120 * Open the index file if needed for the given vstream.
1121 *
1122 * If an index file is successfully opened, the index_read_fd of the stream is
1123 * set with it.
1124 *
1125 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1126 */
1127static int try_open_index(struct relay_viewer_stream *vstream,
1128 struct relay_stream *rstream)
1129{
1130 int ret = 0;
1131
1132 assert(vstream);
1133 assert(rstream);
1134
1135 if (vstream->index_read_fd >= 0) {
1136 goto end;
1137 }
1138
1139 /*
1140 * First time, we open the index file and at least one index is ready. The
1141 * race between the read and write of the total_index_received is
1142 * acceptable here since the client will be notified to simply come back
1143 * and get the next index.
1144 */
1145 if (rstream->total_index_received <= 0) {
1146 ret = -ENOENT;
1147 goto end;
1148 }
1149 ret = index_open(vstream->path_name, vstream->channel_name,
1150 vstream->tracefile_count, vstream->tracefile_count_current);
1151 if (ret >= 0) {
1152 vstream->index_read_fd = ret;
1153 ret = 0;
1154 goto end;
1155 }
1156
1157end:
1158 return ret;
1159}
1160
1161/*
1162 * Check the status of the index for the given stream. This function updates
1163 * the index structure if needed and can destroy the vstream also for the HUP
1164 * situation.
1165 *
1166 * Return 0 means that we can proceed with the index. A value of 1 means that
1167 * the index has been updated and is ready to be send to the client. A negative
1168 * value indicates an error that can't be handled.
1169 */
1170static int check_index_status(struct relay_viewer_stream *vstream,
1171 struct relay_stream *rstream, struct ctf_trace *trace,
1172 struct lttng_viewer_index *index)
1173{
1174 int ret;
1175
1176 assert(vstream);
1177 assert(rstream);
1178 assert(index);
1179 assert(trace);
1180
1181 if (!rstream->close_flag) {
1182 /* Rotate on abort (overwrite). */
1183 if (vstream->abort_flag) {
1184 DBG("Viewer stream %" PRIu64 " rotate because of overwrite",
1185 vstream->stream_handle);
1186 ret = viewer_stream_rotate(vstream, rstream);
1187 if (ret < 0) {
1188 goto error;
1189 } else if (ret == 1) {
1190 /* EOF */
1191 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1192 goto hup;
1193 }
1194 /* ret == 0 means successful so we continue. */
1195 }
1196
1197 /* Check if we are in the same trace file at this point. */
1198 if (rstream->tracefile_count_current == vstream->tracefile_count_current) {
1199 if (rstream->beacon_ts_end != -1ULL &&
1200 vstream->last_sent_index == rstream->total_index_received) {
1201 /*
1202 * We've received a synchronization beacon and the last index
1203 * available has been sent, the index for now is inactive.
1204 */
1205 index->status = htobe32(LTTNG_VIEWER_INDEX_INACTIVE);
1206 index->timestamp_end = htobe64(rstream->beacon_ts_end);
528f2ffa 1207 index->stream_id = htobe64(rstream->ctf_stream_id);
878c34cf 1208 goto index_ready;
ca3aecdf 1209 } else if (rstream->total_index_received <= vstream->last_sent_index
878c34cf
DG
1210 && !vstream->close_write_flag) {
1211 /*
1212 * Reader and writer are working in the same tracefile, so we care
1213 * about the number of index received and sent. Otherwise, we read
1214 * up to EOF.
1215 */
1216 index->status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
1217 goto index_ready;
1218 }
1219 }
1220 /* Nothing to do with the index, continue with it. */
1221 ret = 0;
1222 } else if (rstream->close_flag && vstream->close_write_flag &&
1223 vstream->total_index_received == vstream->last_sent_index) {
1224 /* Last index sent and current tracefile closed in write */
1225 index->status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1226 goto hup;
1227 } else {
1228 vstream->close_write_flag = 1;
1229 ret = 0;
1230 }
1231
1232error:
1233 return ret;
1234
1235hup:
1236 viewer_stream_delete(vstream);
1237 viewer_stream_destroy(trace, vstream);
1238index_ready:
1239 return 1;
1240}
1241
d3e2ba59
JD
1242/*
1243 * Send the next index for a stream.
1244 *
1245 * Return 0 on success or else a negative value.
1246 */
1247static
58eb9381 1248int viewer_get_next_index(struct relay_connection *conn)
d3e2ba59
JD
1249{
1250 int ret;
878c34cf 1251 ssize_t read_ret;
d3e2ba59
JD
1252 struct lttng_viewer_get_next_index request_index;
1253 struct lttng_viewer_index viewer_index;
50adc264 1254 struct ctf_packet_index packet_index;
d3e2ba59
JD
1255 struct relay_viewer_stream *vstream;
1256 struct relay_stream *rstream;
2a174661
DG
1257 struct ctf_trace *ctf_trace;
1258 struct relay_session *session;
d3e2ba59 1259
58eb9381 1260 assert(conn);
d3e2ba59
JD
1261
1262 DBG("Viewer get next index");
1263
eea7556c 1264 health_code_update();
2f8f53af 1265
58eb9381 1266 ret = recv_request(conn->sock, &request_index, sizeof(request_index));
2f8f53af 1267 if (ret < 0) {
d3e2ba59
JD
1268 goto end;
1269 }
eea7556c 1270 health_code_update();
d3e2ba59
JD
1271
1272 rcu_read_lock();
6763619c
JD
1273 vstream = viewer_stream_find_by_id(be64toh(request_index.stream_id));
1274 if (!vstream) {
2a174661
DG
1275 ret = -1;
1276 goto end_unlock;
1277 }
1278
6763619c
JD
1279 session = session_find_by_id(conn->sessions_ht, vstream->session_id);
1280 if (!session) {
d3e2ba59
JD
1281 ret = -1;
1282 goto end_unlock;
1283 }
1284
2a174661
DG
1285 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht, vstream->path_name);
1286 assert(ctf_trace);
1287
d3e2ba59
JD
1288 memset(&viewer_index, 0, sizeof(viewer_index));
1289
1290 /*
1291 * The viewer should not ask for index on metadata stream.
1292 */
1293 if (vstream->metadata_flag) {
c4e361a4 1294 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
d3e2ba59
JD
1295 goto send_reply;
1296 }
1297
878c34cf
DG
1298 rstream = stream_find_by_id(relay_streams_ht, vstream->stream_handle);
1299 assert(rstream);
1300
1301 /* Try to open an index if one is needed for that stream. */
1302 ret = try_open_index(vstream, rstream);
1303 if (ret < 0) {
0e6830aa 1304 if (ret == -ENOENT) {
d3e2ba59
JD
1305 /*
1306 * The index is created only when the first data packet arrives, it
1307 * might not be ready at the beginning of the session
1308 */
c4e361a4 1309 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
878c34cf
DG
1310 } else {
1311 /* Unhandled error. */
c4e361a4 1312 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
d3e2ba59 1313 }
878c34cf 1314 goto send_reply;
d3e2ba59
JD
1315 }
1316
10b0cd2e 1317 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
878c34cf
DG
1318 ret = check_index_status(vstream, rstream, ctf_trace, &viewer_index);
1319 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
1320 if (ret < 0) {
43247259 1321 goto end_unlock;
878c34cf
DG
1322 } else if (ret == 1) {
1323 /*
1324 * This means the viewer index data structure has been populated by the
1325 * check call thus we now send back the reply to the client.
1326 */
d3e2ba59
JD
1327 goto send_reply;
1328 }
878c34cf
DG
1329 /* At this point, ret MUST be 0 thus we continue with the get. */
1330 assert(!ret);
d3e2ba59 1331
2a174661
DG
1332 if (!ctf_trace->metadata_received ||
1333 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
d3e2ba59
JD
1334 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
1335 }
1336
f04a971b 1337 ret = check_new_streams(conn);
4a9daf17
JD
1338 if (ret < 0) {
1339 goto end_unlock;
1340 } else if (ret == 1) {
1341 viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1342 }
1343
56611b06 1344 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1345 pthread_mutex_lock(&vstream->overwrite_lock);
1346 if (vstream->abort_flag) {
878c34cf 1347 /* The file is being overwritten by the writer, we cannot use it. */
cef0f7d5 1348 pthread_mutex_unlock(&vstream->overwrite_lock);
2f8f53af 1349 ret = viewer_stream_rotate(vstream, rstream);
56611b06 1350 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
cef0f7d5
JD
1351 if (ret < 0) {
1352 goto end_unlock;
a020f610 1353 } else if (ret == 1) {
c4e361a4 1354 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1355 viewer_stream_delete(vstream);
2a174661 1356 viewer_stream_destroy(ctf_trace, vstream);
878c34cf
DG
1357 } else {
1358 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
cef0f7d5
JD
1359 }
1360 goto send_reply;
1361 }
2f8f53af 1362
878c34cf 1363 read_ret = lttng_read(vstream->index_read_fd, &packet_index,
6cd525e8 1364 sizeof(packet_index));
cef0f7d5 1365 pthread_mutex_unlock(&vstream->overwrite_lock);
56611b06 1366 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
878c34cf
DG
1367 if (read_ret < 0) {
1368 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
1369 viewer_stream_delete(vstream);
1370 viewer_stream_destroy(ctf_trace, vstream);
1371 goto send_reply;
1372 } else if (read_ret < sizeof(packet_index)) {
10b0cd2e 1373 pthread_mutex_lock(&rstream->viewer_stream_rotation_lock);
878c34cf 1374 if (vstream->close_write_flag) {
2f8f53af 1375 ret = viewer_stream_rotate(vstream, rstream);
6b6b9a5a 1376 if (ret < 0) {
878c34cf 1377 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
6b6b9a5a 1378 goto end_unlock;
a020f610 1379 } else if (ret == 1) {
c4e361a4 1380 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_HUP);
2f8f53af 1381 viewer_stream_delete(vstream);
2a174661 1382 viewer_stream_destroy(ctf_trace, vstream);
878c34cf
DG
1383 } else {
1384 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_RETRY);
6b6b9a5a
JD
1385 }
1386 } else {
878c34cf 1387 ERR("Relay reading index file %d", vstream->index_read_fd);
c4e361a4 1388 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_ERR);
6b6b9a5a 1389 }
878c34cf 1390 pthread_mutex_unlock(&rstream->viewer_stream_rotation_lock);
6b6b9a5a 1391 goto send_reply;
d3e2ba59 1392 } else {
c4e361a4 1393 viewer_index.status = htobe32(LTTNG_VIEWER_INDEX_OK);
d3e2ba59
JD
1394 vstream->last_sent_index++;
1395 }
1396
1397 /*
1398 * Indexes are stored in big endian, no need to switch before sending.
1399 */
1400 viewer_index.offset = packet_index.offset;
1401 viewer_index.packet_size = packet_index.packet_size;
1402 viewer_index.content_size = packet_index.content_size;
1403 viewer_index.timestamp_begin = packet_index.timestamp_begin;
1404 viewer_index.timestamp_end = packet_index.timestamp_end;
1405 viewer_index.events_discarded = packet_index.events_discarded;
1406 viewer_index.stream_id = packet_index.stream_id;
1407
1408send_reply:
1409 viewer_index.flags = htobe32(viewer_index.flags);
eea7556c 1410 health_code_update();
2f8f53af 1411
58eb9381 1412 ret = send_response(conn->sock, &viewer_index, sizeof(viewer_index));
d3e2ba59 1413 if (ret < 0) {
d3e2ba59
JD
1414 goto end_unlock;
1415 }
eea7556c 1416 health_code_update();
d3e2ba59 1417
2f8f53af 1418 DBG("Index %" PRIu64 " for stream %" PRIu64 " sent",
d3e2ba59
JD
1419 vstream->last_sent_index, vstream->stream_handle);
1420
1421end_unlock:
1422 rcu_read_unlock();
1423
d3e2ba59
JD
1424end:
1425 return ret;
1426}
1427
1428/*
1429 * Send the next index for a stream
1430 *
1431 * Return 0 on success or else a negative value.
1432 */
1433static
58eb9381 1434int viewer_get_packet(struct relay_connection *conn)
d3e2ba59
JD
1435{
1436 int ret, send_data = 0;
1437 char *data = NULL;
1438 uint32_t len = 0;
1439 ssize_t read_len;
1440 struct lttng_viewer_get_packet get_packet_info;
1441 struct lttng_viewer_trace_packet reply;
1442 struct relay_viewer_stream *stream;
6763619c 1443 struct relay_session *session;
2a174661 1444 struct ctf_trace *ctf_trace;
d3e2ba59 1445
58eb9381 1446 assert(conn);
d3e2ba59
JD
1447
1448 DBG2("Relay get data packet");
1449
eea7556c 1450 health_code_update();
2f8f53af 1451
58eb9381 1452 ret = recv_request(conn->sock, &get_packet_info, sizeof(get_packet_info));
2f8f53af 1453 if (ret < 0) {
d3e2ba59
JD
1454 goto end;
1455 }
eea7556c 1456 health_code_update();
d3e2ba59 1457
0233a6a5
DG
1458 /* From this point on, the error label can be reached. */
1459 memset(&reply, 0, sizeof(reply));
1460
d3e2ba59 1461 rcu_read_lock();
2f8f53af 1462 stream = viewer_stream_find_by_id(be64toh(get_packet_info.stream_id));
d3e2ba59
JD
1463 if (!stream) {
1464 goto error;
1465 }
2a174661 1466
6763619c
JD
1467 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1468 if (!session) {
1469 ret = -1;
1470 goto error;
1471 }
1472
1473 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1474 stream->path_name);
1475 assert(ctf_trace);
d3e2ba59
JD
1476
1477 /*
1478 * First time we read this stream, we need open the tracefile, we should
1479 * only arrive here if an index has already been sent to the viewer, so the
1480 * tracefile must exist, if it does not it is a fatal error.
1481 */
1482 if (stream->read_fd < 0) {
1483 char fullpath[PATH_MAX];
1484
6b6b9a5a
JD
1485 if (stream->tracefile_count > 0) {
1486 ret = snprintf(fullpath, PATH_MAX, "%s/%s_%" PRIu64, stream->path_name,
1487 stream->channel_name,
1488 stream->tracefile_count_current);
1489 } else {
1490 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1491 stream->channel_name);
1492 }
d3e2ba59
JD
1493 if (ret < 0) {
1494 goto error;
1495 }
1496 ret = open(fullpath, O_RDONLY);
1497 if (ret < 0) {
1498 PERROR("Relay opening trace file");
1499 goto error;
1500 }
1501 stream->read_fd = ret;
1502 }
1503
2a174661
DG
1504 if (!ctf_trace->metadata_received ||
1505 ctf_trace->metadata_received > ctf_trace->metadata_sent) {
c4e361a4 1506 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59 1507 reply.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
d3e2ba59
JD
1508 goto send_reply;
1509 }
1510
f04a971b 1511 ret = check_new_streams(conn);
4a9daf17
JD
1512 if (ret < 0) {
1513 goto end_unlock;
1514 } else if (ret == 1) {
c4e361a4 1515 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
4a9daf17
JD
1516 reply.flags |= LTTNG_VIEWER_FLAG_NEW_STREAM;
1517 goto send_reply;
1518 }
1519
d3e2ba59
JD
1520 len = be32toh(get_packet_info.len);
1521 data = zmalloc(len);
1522 if (!data) {
1523 PERROR("relay data zmalloc");
1524 goto error;
1525 }
1526
1527 ret = lseek(stream->read_fd, be64toh(get_packet_info.offset), SEEK_SET);
1528 if (ret < 0) {
6b6b9a5a
JD
1529 /*
1530 * If the read fd was closed by the streaming side, the
1531 * abort_flag will be set to 1, otherwise it is an error.
1532 */
1533 if (stream->abort_flag == 0) {
1534 PERROR("lseek");
1535 goto error;
1536 }
c4e361a4 1537 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a 1538 goto send_reply;
d3e2ba59 1539 }
6cd525e8
MD
1540 read_len = lttng_read(stream->read_fd, data, len);
1541 if (read_len < len) {
6b6b9a5a
JD
1542 /*
1543 * If the read fd was closed by the streaming side, the
1544 * abort_flag will be set to 1, otherwise it is an error.
1545 */
1546 if (stream->abort_flag == 0) {
1547 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
1548 stream->read_fd,
1549 be64toh(get_packet_info.offset));
1550 goto error;
1551 } else {
c4e361a4 1552 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_EOF);
6b6b9a5a
JD
1553 goto send_reply;
1554 }
d3e2ba59 1555 }
c4e361a4 1556 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_OK);
d3e2ba59
JD
1557 reply.len = htobe32(len);
1558 send_data = 1;
1559 goto send_reply;
1560
1561error:
c4e361a4 1562 reply.status = htobe32(LTTNG_VIEWER_GET_PACKET_ERR);
d3e2ba59
JD
1563
1564send_reply:
1565 reply.flags = htobe32(reply.flags);
eea7556c
MD
1566
1567 health_code_update();
2f8f53af 1568
58eb9381 1569 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1570 if (ret < 0) {
d3e2ba59
JD
1571 goto end_unlock;
1572 }
eea7556c 1573 health_code_update();
d3e2ba59
JD
1574
1575 if (send_data) {
eea7556c 1576 health_code_update();
58eb9381 1577 ret = send_response(conn->sock, data, len);
d3e2ba59 1578 if (ret < 0) {
d3e2ba59
JD
1579 goto end_unlock;
1580 }
eea7556c 1581 health_code_update();
d3e2ba59
JD
1582 }
1583
1584 DBG("Sent %u bytes for stream %" PRIu64, len,
1585 be64toh(get_packet_info.stream_id));
1586
1587end_unlock:
1588 free(data);
1589 rcu_read_unlock();
1590
1591end:
1592 return ret;
1593}
1594
1595/*
1596 * Send the session's metadata
1597 *
1598 * Return 0 on success else a negative value.
1599 */
1600static
58eb9381 1601int viewer_get_metadata(struct relay_connection *conn)
d3e2ba59
JD
1602{
1603 int ret = 0;
1604 ssize_t read_len;
1605 uint64_t len = 0;
1606 char *data = NULL;
1607 struct lttng_viewer_get_metadata request;
1608 struct lttng_viewer_metadata_packet reply;
1609 struct relay_viewer_stream *stream;
2a174661 1610 struct ctf_trace *ctf_trace;
6763619c 1611 struct relay_session *session;
d3e2ba59 1612
58eb9381 1613 assert(conn);
d3e2ba59
JD
1614
1615 DBG("Relay get metadata");
1616
eea7556c 1617 health_code_update();
2f8f53af 1618
58eb9381 1619 ret = recv_request(conn->sock, &request, sizeof(request));
2f8f53af 1620 if (ret < 0) {
d3e2ba59
JD
1621 goto end;
1622 }
eea7556c 1623 health_code_update();
d3e2ba59 1624
53efb85a
MD
1625 memset(&reply, 0, sizeof(reply));
1626
d3e2ba59 1627 rcu_read_lock();
2f8f53af 1628 stream = viewer_stream_find_by_id(be64toh(request.stream_id));
d3e2ba59
JD
1629 if (!stream || !stream->metadata_flag) {
1630 ERR("Invalid metadata stream");
1631 goto error;
1632 }
d3e2ba59 1633
6763619c
JD
1634 session = session_find_by_id(conn->sessions_ht, stream->session_id);
1635 if (!session) {
1636 ret = -1;
1637 goto error;
1638 }
1639
1640 ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
2a174661
DG
1641 stream->path_name);
1642 assert(ctf_trace);
1643 assert(ctf_trace->metadata_sent <= ctf_trace->metadata_received);
1644
1645 len = ctf_trace->metadata_received - ctf_trace->metadata_sent;
d3e2ba59 1646 if (len == 0) {
c4e361a4 1647 reply.status = htobe32(LTTNG_VIEWER_NO_NEW_METADATA);
d3e2ba59
JD
1648 goto send_reply;
1649 }
1650
1651 /* first time, we open the metadata file */
1652 if (stream->read_fd < 0) {
1653 char fullpath[PATH_MAX];
1654
1655 ret = snprintf(fullpath, PATH_MAX, "%s/%s", stream->path_name,
1656 stream->channel_name);
1657 if (ret < 0) {
1658 goto error;
1659 }
1660 ret = open(fullpath, O_RDONLY);
1661 if (ret < 0) {
1662 PERROR("Relay opening metadata file");
1663 goto error;
1664 }
1665 stream->read_fd = ret;
1666 }
1667
1668 reply.len = htobe64(len);
1669 data = zmalloc(len);
1670 if (!data) {
1671 PERROR("viewer metadata zmalloc");
1672 goto error;
1673 }
1674
6cd525e8
MD
1675 read_len = lttng_read(stream->read_fd, data, len);
1676 if (read_len < len) {
d3e2ba59
JD
1677 PERROR("Relay reading metadata file");
1678 goto error;
1679 }
2a174661 1680 ctf_trace->metadata_sent += read_len;
c4e361a4 1681 reply.status = htobe32(LTTNG_VIEWER_METADATA_OK);
d3e2ba59
JD
1682 goto send_reply;
1683
1684error:
c4e361a4 1685 reply.status = htobe32(LTTNG_VIEWER_METADATA_ERR);
d3e2ba59
JD
1686
1687send_reply:
eea7556c 1688 health_code_update();
58eb9381 1689 ret = send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59 1690 if (ret < 0) {
d3e2ba59
JD
1691 goto end_unlock;
1692 }
eea7556c 1693 health_code_update();
d3e2ba59
JD
1694
1695 if (len > 0) {
58eb9381 1696 ret = send_response(conn->sock, data, len);
d3e2ba59 1697 if (ret < 0) {
d3e2ba59
JD
1698 goto end_unlock;
1699 }
1700 }
1701
1702 DBG("Sent %" PRIu64 " bytes of metadata for stream %" PRIu64, len,
1703 be64toh(request.stream_id));
1704
1705 DBG("Metadata sent");
1706
1707end_unlock:
1708 free(data);
1709 rcu_read_unlock();
1710end:
1711 return ret;
1712}
1713
c3b7390b
JD
1714/*
1715 * Create a viewer session.
1716 *
1717 * Return 0 on success or else a negative value.
1718 */
1719static
1720int viewer_create_session(struct relay_connection *conn)
1721{
1722 int ret;
1723 struct lttng_viewer_create_session_response resp;
1724
1725 DBG("Viewer create session received");
1726
53efb85a 1727 memset(&resp, 0, sizeof(resp));
c3b7390b 1728 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_OK);
32816a93 1729 conn->viewer_session = zmalloc(sizeof(*conn->viewer_session));
c3b7390b
JD
1730 if (!conn->viewer_session) {
1731 ERR("Allocation viewer session");
1732 resp.status = htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR);
1733 goto send_reply;
1734 }
1735 CDS_INIT_LIST_HEAD(&conn->viewer_session->sessions_head);
1736
1737send_reply:
1738 health_code_update();
1739 ret = send_response(conn->sock, &resp, sizeof(resp));
1740 if (ret < 0) {
1741 goto end;
1742 }
1743 health_code_update();
1744 ret = 0;
1745
1746end:
1747 return ret;
1748}
1749
1750
d3e2ba59
JD
1751/*
1752 * live_relay_unknown_command: send -1 if received unknown command
1753 */
1754static
58eb9381 1755void live_relay_unknown_command(struct relay_connection *conn)
d3e2ba59
JD
1756{
1757 struct lttcomm_relayd_generic_reply reply;
d3e2ba59 1758
53efb85a 1759 memset(&reply, 0, sizeof(reply));
d3e2ba59 1760 reply.ret_code = htobe32(LTTNG_ERR_UNK);
58eb9381 1761 (void) send_response(conn->sock, &reply, sizeof(reply));
d3e2ba59
JD
1762}
1763
1764/*
1765 * Process the commands received on the control socket
1766 */
1767static
1768int process_control(struct lttng_viewer_cmd *recv_hdr,
58eb9381 1769 struct relay_connection *conn)
d3e2ba59
JD
1770{
1771 int ret = 0;
2f8f53af
DG
1772 uint32_t msg_value;
1773
1774 assert(recv_hdr);
58eb9381 1775 assert(conn);
2f8f53af
DG
1776
1777 msg_value = be32toh(recv_hdr->cmd);
1778
1779 /*
1780 * Make sure we've done the version check before any command other then a
1781 * new client connection.
1782 */
c4e361a4 1783 if (msg_value != LTTNG_VIEWER_CONNECT && !conn->version_check_done) {
58eb9381 1784 ERR("Viewer conn value %" PRIu32 " before version check", msg_value);
2f8f53af
DG
1785 ret = -1;
1786 goto end;
1787 }
d3e2ba59 1788
2f8f53af 1789 switch (msg_value) {
c4e361a4 1790 case LTTNG_VIEWER_CONNECT:
58eb9381 1791 ret = viewer_connect(conn);
d3e2ba59 1792 break;
c4e361a4 1793 case LTTNG_VIEWER_LIST_SESSIONS:
58eb9381 1794 ret = viewer_list_sessions(conn);
d3e2ba59 1795 break;
c4e361a4 1796 case LTTNG_VIEWER_ATTACH_SESSION:
58eb9381 1797 ret = viewer_attach_session(conn);
d3e2ba59 1798 break;
c4e361a4 1799 case LTTNG_VIEWER_GET_NEXT_INDEX:
58eb9381 1800 ret = viewer_get_next_index(conn);
d3e2ba59 1801 break;
c4e361a4 1802 case LTTNG_VIEWER_GET_PACKET:
58eb9381 1803 ret = viewer_get_packet(conn);
d3e2ba59 1804 break;
c4e361a4 1805 case LTTNG_VIEWER_GET_METADATA:
58eb9381 1806 ret = viewer_get_metadata(conn);
d3e2ba59 1807 break;
c4e361a4 1808 case LTTNG_VIEWER_GET_NEW_STREAMS:
58eb9381 1809 ret = viewer_get_new_streams(conn);
80e8027a 1810 break;
c3b7390b
JD
1811 case LTTNG_VIEWER_CREATE_SESSION:
1812 ret = viewer_create_session(conn);
1813 break;
d3e2ba59
JD
1814 default:
1815 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr->cmd));
58eb9381 1816 live_relay_unknown_command(conn);
d3e2ba59
JD
1817 ret = -1;
1818 goto end;
1819 }
1820
1821end:
1822 return ret;
1823}
1824
1825static
58eb9381 1826void cleanup_connection_pollfd(struct lttng_poll_event *events, int pollfd)
d3e2ba59
JD
1827{
1828 int ret;
1829
1830 assert(events);
1831
58eb9381 1832 (void) lttng_poll_del(events, pollfd);
d3e2ba59
JD
1833
1834 ret = close(pollfd);
1835 if (ret < 0) {
1836 ERR("Closing pollfd %d", pollfd);
1837 }
1838}
1839
d3e2ba59 1840/*
58eb9381 1841 * Delete and destroy a connection.
d3e2ba59
JD
1842 *
1843 * RCU read side lock MUST be acquired.
1844 */
58eb9381
DG
1845static void destroy_connection(struct lttng_ht *relay_connections_ht,
1846 struct relay_connection *conn)
d3e2ba59 1847{
6763619c 1848 struct relay_session *session, *tmp_session;
d3e2ba59
JD
1849
1850 assert(relay_connections_ht);
58eb9381 1851 assert(conn);
d3e2ba59 1852
58eb9381 1853 connection_delete(relay_connections_ht, conn);
d3e2ba59 1854
6763619c
JD
1855 if (!conn->viewer_session) {
1856 goto end;
1857 }
1858
58eb9381 1859 rcu_read_lock();
6763619c
JD
1860 cds_list_for_each_entry_safe(session, tmp_session,
1861 &conn->viewer_session->sessions_head,
1862 viewer_session_list) {
1863 DBG("Cleaning connection of session ID %" PRIu64, session->id);
d227d5bd 1864 cleanup_session(conn, session);
2a174661
DG
1865 }
1866 rcu_read_unlock();
d3e2ba59 1867
6763619c 1868end:
58eb9381 1869 connection_destroy(conn);
d3e2ba59
JD
1870}
1871
1872/*
1873 * This thread does the actual work
1874 */
1875static
1876void *thread_worker(void *data)
1877{
1878 int ret, err = -1;
1879 uint32_t nb_fd;
58eb9381 1880 struct relay_connection *conn;
d3e2ba59
JD
1881 struct lttng_poll_event events;
1882 struct lttng_ht *relay_connections_ht;
d3e2ba59
JD
1883 struct lttng_ht_iter iter;
1884 struct lttng_viewer_cmd recv_hdr;
1885 struct relay_local_data *relay_ctx = (struct relay_local_data *) data;
1886 struct lttng_ht *sessions_ht = relay_ctx->sessions_ht;
d3e2ba59
JD
1887
1888 DBG("[thread] Live viewer relay worker started");
1889
1890 rcu_register_thread();
1891
eea7556c
MD
1892 health_register(health_relayd, HEALTH_RELAYD_TYPE_LIVE_WORKER);
1893
9b5e0863
MD
1894 if (testpoint(relayd_thread_live_worker)) {
1895 goto error_testpoint;
1896 }
1897
d3e2ba59
JD
1898 /* table of connections indexed on socket */
1899 relay_connections_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1900 if (!relay_connections_ht) {
1901 goto relay_connections_ht_error;
1902 }
1903
1904 ret = create_thread_poll_set(&events, 2);
1905 if (ret < 0) {
1906 goto error_poll_create;
1907 }
1908
58eb9381 1909 ret = lttng_poll_add(&events, live_conn_pipe[0], LPOLLIN | LPOLLRDHUP);
d3e2ba59
JD
1910 if (ret < 0) {
1911 goto error;
1912 }
1913
1914restart:
1915 while (1) {
1916 int i;
1917
eea7556c
MD
1918 health_code_update();
1919
d3e2ba59
JD
1920 /* Infinite blocking call, waiting for transmission */
1921 DBG3("Relayd live viewer worker thread polling...");
eea7556c 1922 health_poll_entry();
d3e2ba59 1923 ret = lttng_poll_wait(&events, -1);
eea7556c 1924 health_poll_exit();
d3e2ba59
JD
1925 if (ret < 0) {
1926 /*
1927 * Restart interrupted system call.
1928 */
1929 if (errno == EINTR) {
1930 goto restart;
1931 }
1932 goto error;
1933 }
1934
1935 nb_fd = ret;
1936
1937 /*
1938 * Process control. The control connection is prioritised so we don't
1939 * starve it with high throughput tracing data on the data
1940 * connection.
1941 */
1942 for (i = 0; i < nb_fd; i++) {
1943 /* Fetch once the poll data */
1944 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1945 int pollfd = LTTNG_POLL_GETFD(&events, i);
1946
eea7556c
MD
1947 health_code_update();
1948
d3e2ba59 1949 /* Thread quit pipe has been closed. Killing thread. */
bcf4a440 1950 ret = check_thread_quit_pipe(pollfd, revents);
d3e2ba59
JD
1951 if (ret) {
1952 err = 0;
1953 goto exit;
1954 }
1955
58eb9381
DG
1956 /* Inspect the relay conn pipe for new connection */
1957 if (pollfd == live_conn_pipe[0]) {
d3e2ba59
JD
1958 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1959 ERR("Relay live pipe error");
1960 goto error;
1961 } else if (revents & LPOLLIN) {
58eb9381 1962 ret = lttng_read(live_conn_pipe[0], &conn, sizeof(conn));
d3e2ba59
JD
1963 if (ret < 0) {
1964 goto error;
1965 }
58eb9381
DG
1966 conn->sessions_ht = sessions_ht;
1967 connection_init(conn);
1968 lttng_poll_add(&events, conn->sock->fd,
1969 LPOLLIN | LPOLLRDHUP);
1970 rcu_read_lock();
1971 lttng_ht_add_unique_ulong(relay_connections_ht,
1972 &conn->sock_n);
d3e2ba59 1973 rcu_read_unlock();
58eb9381 1974 DBG("Connection socket %d added", conn->sock->fd);
d3e2ba59 1975 }
58eb9381
DG
1976 } else {
1977 rcu_read_lock();
1978 conn = connection_find_by_sock(relay_connections_ht, pollfd);
1979 /* If not found, there is a synchronization issue. */
1980 assert(conn);
1981
1982 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1983 cleanup_connection_pollfd(&events, pollfd);
1984 destroy_connection(relay_connections_ht, conn);
d3e2ba59 1985 } else if (revents & LPOLLIN) {
58eb9381
DG
1986 ret = conn->sock->ops->recvmsg(conn->sock, &recv_hdr,
1987 sizeof(recv_hdr), 0);
d3e2ba59 1988 if (ret <= 0) {
58eb9381
DG
1989 /* Connection closed */
1990 cleanup_connection_pollfd(&events, pollfd);
1991 destroy_connection(relay_connections_ht, conn);
1992 DBG("Viewer control conn closed with %d", pollfd);
d3e2ba59 1993 } else {
58eb9381 1994 ret = process_control(&recv_hdr, conn);
d3e2ba59
JD
1995 if (ret < 0) {
1996 /* Clear the session on error. */
58eb9381
DG
1997 cleanup_connection_pollfd(&events, pollfd);
1998 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
1999 DBG("Viewer connection closed with %d", pollfd);
2000 }
2001 }
2002 }
2003 rcu_read_unlock();
2004 }
2005 }
2006 }
2007
2008exit:
2009error:
2010 lttng_poll_clean(&events);
2011
58eb9381 2012 /* Cleanup reamaining connection object. */
d3e2ba59 2013 rcu_read_lock();
58eb9381
DG
2014 cds_lfht_for_each_entry(relay_connections_ht->ht, &iter.iter, conn,
2015 sock_n.node) {
eea7556c 2016 health_code_update();
58eb9381 2017 destroy_connection(relay_connections_ht, conn);
d3e2ba59
JD
2018 }
2019 rcu_read_unlock();
2020error_poll_create:
2021 lttng_ht_destroy(relay_connections_ht);
2022relay_connections_ht_error:
58eb9381
DG
2023 /* Close relay conn pipes */
2024 utils_close_pipe(live_conn_pipe);
d3e2ba59
JD
2025 if (err) {
2026 DBG("Viewer worker thread exited with error");
2027 }
2028 DBG("Viewer worker thread cleanup complete");
9b5e0863 2029error_testpoint:
eea7556c
MD
2030 if (err) {
2031 health_error();
2032 ERR("Health error occurred in %s", __func__);
2033 }
2034 health_unregister(health_relayd);
d3e2ba59
JD
2035 stop_threads();
2036 rcu_unregister_thread();
2037 return NULL;
2038}
2039
2040/*
2041 * Create the relay command pipe to wake thread_manage_apps.
2042 * Closed in cleanup().
2043 */
58eb9381 2044static int create_conn_pipe(void)
d3e2ba59
JD
2045{
2046 int ret;
2047
58eb9381 2048 ret = utils_create_pipe_cloexec(live_conn_pipe);
d3e2ba59
JD
2049
2050 return ret;
2051}
2052
aaec7998 2053void live_stop_threads(void)
d3e2ba59
JD
2054{
2055 int ret;
2056 void *status;
2057
2058 stop_threads();
2059
2060 ret = pthread_join(live_listener_thread, &status);
2061 if (ret != 0) {
2062 PERROR("pthread_join live listener");
2063 goto error; /* join error, exit without cleanup */
2064 }
2065
2066 ret = pthread_join(live_worker_thread, &status);
2067 if (ret != 0) {
2068 PERROR("pthread_join live worker");
2069 goto error; /* join error, exit without cleanup */
2070 }
2071
2072 ret = pthread_join(live_dispatcher_thread, &status);
2073 if (ret != 0) {
2074 PERROR("pthread_join live dispatcher");
2075 goto error; /* join error, exit without cleanup */
2076 }
2077
2078 cleanup();
2079
2080error:
2081 return;
2082}
2083
2084/*
2085 * main
2086 */
2087int live_start_threads(struct lttng_uri *uri,
0b242f62 2088 struct relay_local_data *relay_ctx)
d3e2ba59
JD
2089{
2090 int ret = 0;
2091 void *status;
2092 int is_root;
2093
2094 assert(uri);
2095 live_uri = uri;
2096
d3e2ba59
JD
2097 /* Check if daemon is UID = 0 */
2098 is_root = !getuid();
2099
2100 if (!is_root) {
2101 if (live_uri->port < 1024) {
2102 ERR("Need to be root to use ports < 1024");
2103 ret = -1;
2104 goto exit;
2105 }
2106 }
2107
2108 /* Setup the thread apps communication pipe. */
58eb9381 2109 if ((ret = create_conn_pipe()) < 0) {
d3e2ba59
JD
2110 goto exit;
2111 }
2112
2113 /* Init relay command queue. */
8bdee6e2 2114 cds_wfcq_init(&viewer_conn_queue.head, &viewer_conn_queue.tail);
d3e2ba59
JD
2115
2116 /* Set up max poll set size */
2117 lttng_poll_set_max_size();
2118
2119 /* Setup the dispatcher thread */
2120 ret = pthread_create(&live_dispatcher_thread, NULL,
2121 thread_dispatcher, (void *) NULL);
2122 if (ret != 0) {
2123 PERROR("pthread_create viewer dispatcher");
2124 goto exit_dispatcher;
2125 }
2126
2127 /* Setup the worker thread */
2128 ret = pthread_create(&live_worker_thread, NULL,
2129 thread_worker, relay_ctx);
2130 if (ret != 0) {
2131 PERROR("pthread_create viewer worker");
2132 goto exit_worker;
2133 }
2134
2135 /* Setup the listener thread */
2136 ret = pthread_create(&live_listener_thread, NULL,
2137 thread_listener, (void *) NULL);
2138 if (ret != 0) {
2139 PERROR("pthread_create viewer listener");
2140 goto exit_listener;
2141 }
2142
2143 ret = 0;
2144 goto end;
2145
2146exit_listener:
2147 ret = pthread_join(live_listener_thread, &status);
2148 if (ret != 0) {
2149 PERROR("pthread_join live listener");
2150 goto error; /* join error, exit without cleanup */
2151 }
2152
2153exit_worker:
2154 ret = pthread_join(live_worker_thread, &status);
2155 if (ret != 0) {
2156 PERROR("pthread_join live worker");
2157 goto error; /* join error, exit without cleanup */
2158 }
2159
2160exit_dispatcher:
2161 ret = pthread_join(live_dispatcher_thread, &status);
2162 if (ret != 0) {
2163 PERROR("pthread_join live dispatcher");
2164 goto error; /* join error, exit without cleanup */
2165 }
2166
2167exit:
2168 cleanup();
2169
2170end:
2171error:
2172 return ret;
2173}
This page took 0.237712 seconds and 4 git commands to generate.