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