2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
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.
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
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.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
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>
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "ctf-trace.h"
67 #include "connection.h"
69 static struct lttng_uri
*live_uri
;
72 * This pipe is used to inform the worker thread that a command is queued and
73 * ready to be processed.
75 static int live_conn_pipe
[2] = { -1, -1 };
77 /* Shared between threads */
78 static int live_dispatch_thread_exit
;
80 static pthread_t live_listener_thread
;
81 static pthread_t live_dispatcher_thread
;
82 static pthread_t live_worker_thread
;
85 * Relay command queue.
87 * The live_thread_listener and live_thread_dispatcher communicate with this
90 static struct relay_conn_queue viewer_conn_queue
;
92 static uint64_t last_relay_viewer_session_id
;
98 void cleanup_relayd_live(void)
106 * Receive a request buffer using a given socket, destination allocated buffer
109 * Return the size of the received message or else a negative value on error
110 * with errno being set by recvmsg() syscall.
113 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
120 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
121 if (ret
< 0 || ret
!= size
) {
123 /* Orderly shutdown. Not necessary to print an error. */
124 DBG("Socket %d did an orderly shutdown", sock
->fd
);
126 ERR("Relay failed to receive request.");
135 * Send a response buffer using a given socket, source allocated buffer of
138 * Return the size of the sent message or else a negative value on error with
139 * errno being set by sendmsg() syscall.
142 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
149 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
151 ERR("Relayd failed to send response.");
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
165 int check_new_streams(struct relay_connection
*conn
)
167 struct relay_session
*session
;
168 unsigned long current_val
;
171 if (!conn
->viewer_session
) {
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);
189 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
190 * this function should ignore the sent flag or not.
192 * Return 0 on success or else a negative value.
195 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
196 struct relay_session
*session
, unsigned int ignore_sent_flag
)
199 struct lttng_viewer_stream send_stream
;
200 struct lttng_ht_iter iter
;
201 struct relay_viewer_stream
*vstream
;
207 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
209 struct ctf_trace
*ctf_trace
;
211 health_code_update();
213 /* Ignore if not the same session. */
214 if (vstream
->session_id
!= session
->id
||
215 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
219 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
223 send_stream
.id
= htobe64(vstream
->stream_handle
);
224 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
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
));
231 DBG("Sending stream %" PRIu64
" to viewer", vstream
->stream_handle
);
232 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
236 vstream
->sent_flag
= 1;
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.
252 * Return 0 on success or else a negative value.
255 int 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
)
260 struct lttng_ht_iter iter
;
261 struct ctf_trace
*ctf_trace
;
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
272 pthread_mutex_lock(&session
->viewer_ready_lock
);
275 * Create viewer streams for relay streams that are ready to be used for a
276 * the given session id only.
279 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
281 struct relay_stream
*stream
;
283 health_code_update();
285 if (ctf_trace
->invalid_flag
) {
289 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
290 struct relay_viewer_stream
*vstream
;
292 if (!stream
->viewer_ready
) {
296 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
298 vstream
= viewer_stream_create(stream
, seek_t
, ctf_trace
);
303 /* Acquire reference to ctf_trace. */
304 ctf_trace_get_ref(ctf_trace
);
307 /* Update number of created stream counter. */
310 } else if (!vstream
->sent_flag
&& nb_unsent
) {
311 /* Update number of unsent stream counter. */
314 /* Update number of total stream counter. */
325 pthread_mutex_unlock(&session
->viewer_ready_lock
);
330 * Write to writable pipe used to notify a thread.
333 int notify_thread_pipe(int wpipe
)
337 ret
= lttng_write(wpipe
, "!", 1);
339 PERROR("write poll pipe");
346 * Stop all threads by closing the thread quit pipe.
349 int stop_threads(void)
353 /* Stopping all threads */
354 DBG("Terminating all live threads");
355 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
357 ERR("write error on thread quit pipe");
361 /* Dispatch thread */
362 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
363 futex_nto1_wake(&viewer_conn_queue
.futex
);
368 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
371 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
375 if (events
== NULL
|| size
== 0) {
380 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
386 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
398 * Check if the thread quit pipe was triggered.
400 * Return 1 if it was triggered else 0;
403 int check_thread_quit_pipe(int fd
, uint32_t events
)
405 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
413 * Create and init socket from uri.
416 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
419 struct lttcomm_sock
*sock
= NULL
;
421 sock
= lttcomm_alloc_sock_from_uri(uri
);
423 ERR("Allocating socket");
427 ret
= lttcomm_create_sock(sock
);
431 DBG("Listening on sock %d for live", sock
->fd
);
433 ret
= sock
->ops
->bind(sock
);
438 ret
= sock
->ops
->listen(sock
, -1);
448 lttcomm_destroy_sock(sock
);
454 * This thread manages the listening for new connections on the network
457 void *thread_listener(void *data
)
459 int i
, ret
, pollfd
, err
= -1;
460 uint32_t revents
, nb_fd
;
461 struct lttng_poll_event events
;
462 struct lttcomm_sock
*live_control_sock
;
464 DBG("[thread] Relay live listener started");
466 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
468 health_code_update();
470 live_control_sock
= init_socket(live_uri
);
471 if (!live_control_sock
) {
472 goto error_sock_control
;
475 /* Pass 2 as size here for the thread quit pipe and control sockets. */
476 ret
= create_thread_poll_set(&events
, 2);
478 goto error_create_poll
;
481 /* Add the control socket */
482 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
487 lttng_relay_notify_ready();
489 if (testpoint(relayd_thread_live_listener
)) {
490 goto error_testpoint
;
494 health_code_update();
496 DBG("Listener accepting live viewers connections");
500 ret
= lttng_poll_wait(&events
, -1);
504 * Restart interrupted system call.
506 if (errno
== EINTR
) {
513 DBG("Relay new viewer connection received");
514 for (i
= 0; i
< nb_fd
; i
++) {
515 health_code_update();
517 /* Fetch once the poll data */
518 revents
= LTTNG_POLL_GETEV(&events
, i
);
519 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
521 /* Thread quit pipe has been closed. Killing thread. */
522 ret
= check_thread_quit_pipe(pollfd
, revents
);
528 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
529 ERR("socket poll error");
531 } else if (revents
& LPOLLIN
) {
533 * Get allocated in this thread, enqueued to a global queue,
534 * dequeued and freed in the worker thread.
537 struct relay_connection
*new_conn
;
538 struct lttcomm_sock
*newsock
;
540 new_conn
= connection_create();
545 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
547 PERROR("accepting control sock");
548 connection_free(new_conn
);
551 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
553 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
556 PERROR("setsockopt inet");
557 lttcomm_destroy_sock(newsock
);
558 connection_free(new_conn
);
561 new_conn
->sock
= newsock
;
563 /* Enqueue request for the dispatcher thread. */
564 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
568 * Wake the dispatch queue futex. Implicit memory barrier with
569 * the exchange in cds_wfcq_enqueue.
571 futex_nto1_wake(&viewer_conn_queue
.futex
);
580 lttng_poll_clean(&events
);
582 if (live_control_sock
->fd
>= 0) {
583 ret
= live_control_sock
->ops
->close(live_control_sock
);
588 lttcomm_destroy_sock(live_control_sock
);
592 DBG("Live viewer listener thread exited with error");
594 health_unregister(health_relayd
);
595 DBG("Live viewer listener thread cleanup complete");
596 if (stop_threads()) {
597 ERR("Error stopping live threads");
603 * This thread manages the dispatching of the requests to worker threads
606 void *thread_dispatcher(void *data
)
610 struct cds_wfcq_node
*node
;
611 struct relay_connection
*conn
= NULL
;
613 DBG("[thread] Live viewer relay dispatcher started");
615 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
617 if (testpoint(relayd_thread_live_dispatcher
)) {
618 goto error_testpoint
;
621 health_code_update();
623 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
624 health_code_update();
626 /* Atomically prepare the queue futex */
627 futex_nto1_prepare(&viewer_conn_queue
.futex
);
630 health_code_update();
632 /* Dequeue commands */
633 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
634 &viewer_conn_queue
.tail
);
636 DBG("Woken up but nothing in the live-viewer "
637 "relay command queue");
638 /* Continue thread execution */
641 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
642 DBG("Dispatching viewer request waiting on sock %d",
646 * Inform worker thread of the new request. This call is blocking
647 * so we can be assured that the data will be read at some point in
648 * time or wait to the end of the world :)
650 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
652 PERROR("write conn pipe");
653 connection_destroy(conn
);
656 } while (node
!= NULL
);
658 /* Futex wait on queue. Blocking call on futex() */
660 futex_nto1_wait(&viewer_conn_queue
.futex
);
664 /* Normal exit, no error */
671 ERR("Health error occurred in %s", __func__
);
673 health_unregister(health_relayd
);
674 DBG("Live viewer dispatch thread dying");
675 if (stop_threads()) {
676 ERR("Error stopping live threads");
682 * Establish connection with the viewer and check the versions.
684 * Return 0 on success or else negative value.
687 int viewer_connect(struct relay_connection
*conn
)
690 struct lttng_viewer_connect reply
, msg
;
694 conn
->version_check_done
= 1;
696 health_code_update();
698 DBG("Viewer is establishing a connection to the relayd.");
700 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
705 health_code_update();
707 memset(&reply
, 0, sizeof(reply
));
708 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
709 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
711 /* Major versions must be the same */
712 if (reply
.major
!= be32toh(msg
.major
)) {
713 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
714 reply
.major
, be32toh(msg
.major
));
719 conn
->major
= reply
.major
;
720 /* We adapt to the lowest compatible version */
721 if (reply
.minor
<= be32toh(msg
.minor
)) {
722 conn
->minor
= reply
.minor
;
724 conn
->minor
= be32toh(msg
.minor
);
727 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
728 conn
->type
= RELAY_VIEWER_COMMAND
;
729 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
730 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
732 ERR("Unknown connection type : %u", be32toh(msg
.type
));
737 reply
.major
= htobe32(reply
.major
);
738 reply
.minor
= htobe32(reply
.minor
);
739 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
740 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
743 health_code_update();
745 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
750 health_code_update();
752 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
760 * Send the viewer the list of current sessions.
762 * Return 0 on success or else a negative value.
765 int viewer_list_sessions(struct relay_connection
*conn
)
768 struct lttng_viewer_list_sessions session_list
;
770 long approx_before
, approx_after
;
771 struct lttng_ht_iter iter
;
772 struct lttng_viewer_session send_session
;
773 struct relay_session
*session
;
775 DBG("List sessions received");
778 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
780 session_list
.sessions_count
= htobe32(count
);
782 health_code_update();
784 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
789 health_code_update();
791 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
793 health_code_update();
795 strncpy(send_session
.session_name
, session
->session_name
,
796 sizeof(send_session
.session_name
));
797 strncpy(send_session
.hostname
, session
->hostname
,
798 sizeof(send_session
.hostname
));
799 send_session
.id
= htobe64(session
->id
);
800 send_session
.live_timer
= htobe32(session
->live_timer
);
801 send_session
.clients
= htobe32(session
->viewer_refcount
);
802 send_session
.streams
= htobe32(session
->stream_count
);
804 health_code_update();
806 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
811 health_code_update();
820 * Check if a connection is attached to a session.
821 * Return 1 if attached, 0 if not attached, a negative value on error.
824 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
826 struct relay_session
*session
;
829 if (!conn
->viewer_session
) {
832 cds_list_for_each_entry(session
,
833 &conn
->viewer_session
->sessions_head
,
834 viewer_session_list
) {
835 if (session
->id
== session_id
) {
846 * Delete all streams for a specific session ID.
848 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
850 struct relay_viewer_stream
*stream
;
851 struct lttng_ht_iter iter
;
856 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
858 struct ctf_trace
*ctf_trace
;
860 health_code_update();
861 if (stream
->session_id
!= session
->id
) {
865 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
869 viewer_stream_delete(stream
);
871 if (stream
->metadata_flag
) {
872 ctf_trace
->metadata_sent
= 0;
873 ctf_trace
->viewer_metadata_stream
= NULL
;
876 viewer_stream_destroy(ctf_trace
, stream
);
881 static void try_destroy_streams(struct relay_session
*session
)
883 struct ctf_trace
*ctf_trace
;
884 struct lttng_ht_iter iter
;
888 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
890 /* Attempt to destroy the ctf trace of that session. */
891 ctf_trace_try_destroy(session
, ctf_trace
);
898 static void cleanup_session(struct relay_connection
*conn
,
899 struct relay_session
*session
)
902 * Very important that this is done before destroying the session so we
903 * can put back every viewer stream reference from the ctf_trace.
905 destroy_viewer_streams_by_session(session
);
906 try_destroy_streams(session
);
907 cds_list_del(&session
->viewer_session_list
);
908 session_viewer_try_destroy(conn
->sessions_ht
, session
);
912 * Send the viewer the list of current sessions.
915 int viewer_get_new_streams(struct relay_connection
*conn
)
917 int ret
, send_streams
= 0;
918 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
919 struct lttng_viewer_new_streams_request request
;
920 struct lttng_viewer_new_streams_response response
;
921 struct relay_session
*session
;
926 DBG("Get new streams received");
928 health_code_update();
930 /* Receive the request from the connected client. */
931 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
935 session_id
= be64toh(request
.session_id
);
937 health_code_update();
939 memset(&response
, 0, sizeof(response
));
942 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
944 DBG("Relay session %" PRIu64
" not found", session_id
);
945 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
949 if (!session_attached(conn
, session_id
)) {
951 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
956 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
958 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
963 /* Only send back the newly created streams with the unsent ones. */
964 nb_streams
= nb_created
+ nb_unsent
;
965 response
.streams_count
= htobe32(nb_streams
);
968 * If the session is closed and we have no new streams to send,
969 * it means that the viewer has already received the whole trace
970 * for this session and should now close it.
972 if (nb_streams
== 0 && session
->close_flag
) {
974 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
976 * Remove the session from the attached list of the connection
977 * and try to destroy it.
979 cds_list_del(&session
->viewer_session_list
);
980 cleanup_session(conn
, session
);
985 health_code_update();
986 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
990 health_code_update();
993 * Unknown or empty session, just return gracefully, the viewer knows what
996 if (!send_streams
|| !nb_streams
) {
1002 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1003 * that were not sent from that point will be sent to the viewer.
1005 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1017 * Send the viewer the list of current sessions.
1020 int viewer_attach_session(struct relay_connection
*conn
)
1022 int send_streams
= 0;
1024 uint32_t nb_streams
= 0;
1025 enum lttng_viewer_seek seek_type
;
1026 struct lttng_viewer_attach_session_request request
;
1027 struct lttng_viewer_attach_session_response response
;
1028 struct relay_session
*session
;
1032 health_code_update();
1034 /* Receive the request from the connected client. */
1035 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1040 health_code_update();
1042 memset(&response
, 0, sizeof(response
));
1044 if (!conn
->viewer_session
) {
1045 DBG("Client trying to attach before creating a live viewer session");
1046 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1051 session
= session_find_by_id(conn
->sessions_ht
,
1052 be64toh(request
.session_id
));
1054 DBG("Relay session %" PRIu64
" not found",
1055 be64toh(request
.session_id
));
1056 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1059 session_viewer_attach(session
);
1060 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1062 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1063 DBG("Already a viewer attached");
1064 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1065 session_viewer_detach(session
);
1067 } else if (session
->live_timer
== 0) {
1068 DBG("Not live session");
1069 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1073 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1074 cds_list_add(&session
->viewer_session_list
,
1075 &conn
->viewer_session
->sessions_head
);
1078 switch (be32toh(request
.seek
)) {
1079 case LTTNG_VIEWER_SEEK_BEGINNING
:
1080 case LTTNG_VIEWER_SEEK_LAST
:
1081 seek_type
= be32toh(request
.seek
);
1084 ERR("Wrong seek parameter");
1085 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1090 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1094 response
.streams_count
= htobe32(nb_streams
);
1097 health_code_update();
1098 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1102 health_code_update();
1105 * Unknown or empty session, just return gracefully, the viewer knows what
1108 if (!send_streams
|| !nb_streams
) {
1113 /* Send stream and ignore the sent flag. */
1114 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1126 * Open the index file if needed for the given vstream.
1128 * If an index file is successfully opened, the index_read_fd of the stream is
1131 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1133 static int try_open_index(struct relay_viewer_stream
*vstream
,
1134 struct relay_stream
*rstream
)
1141 if (vstream
->index_read_fd
>= 0) {
1146 * First time, we open the index file and at least one index is ready. The
1147 * race between the read and write of the total_index_received is
1148 * acceptable here since the client will be notified to simply come back
1149 * and get the next index.
1151 if (rstream
->total_index_received
<= 0) {
1155 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1156 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1158 vstream
->index_read_fd
= ret
;
1168 * Check the status of the index for the given stream. This function updates
1169 * the index structure if needed and can destroy the vstream also for the HUP
1172 * Return 0 means that we can proceed with the index. A value of 1 means that
1173 * the index has been updated and is ready to be send to the client. A negative
1174 * value indicates an error that can't be handled.
1176 static int check_index_status(struct relay_viewer_stream
*vstream
,
1177 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1178 struct lttng_viewer_index
*index
)
1187 if (!rstream
->close_flag
) {
1188 /* Rotate on abort (overwrite). */
1189 if (vstream
->abort_flag
) {
1190 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1191 vstream
->stream_handle
);
1192 ret
= viewer_stream_rotate(vstream
, rstream
);
1195 } else if (ret
== 1) {
1197 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1200 /* ret == 0 means successful so we continue. */
1203 /* Check if we are in the same trace file at this point. */
1204 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1205 if (rstream
->beacon_ts_end
!= -1ULL &&
1206 vstream
->last_sent_index
== rstream
->total_index_received
) {
1208 * We've received a synchronization beacon and the last index
1209 * available has been sent, the index for now is inactive.
1211 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1212 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1213 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1215 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1216 && !vstream
->close_write_flag
) {
1218 * Reader and writer are working in the same tracefile, so we care
1219 * about the number of index received and sent. Otherwise, we read
1222 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1226 /* Nothing to do with the index, continue with it. */
1228 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1229 vstream
->total_index_received
== vstream
->last_sent_index
) {
1230 /* Last index sent and current tracefile closed in write */
1231 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1234 vstream
->close_write_flag
= 1;
1242 viewer_stream_delete(vstream
);
1243 viewer_stream_destroy(trace
, vstream
);
1249 * Send the next index for a stream.
1251 * Return 0 on success or else a negative value.
1254 int viewer_get_next_index(struct relay_connection
*conn
)
1258 struct lttng_viewer_get_next_index request_index
;
1259 struct lttng_viewer_index viewer_index
;
1260 struct ctf_packet_index packet_index
;
1261 struct relay_viewer_stream
*vstream
;
1262 struct relay_stream
*rstream
;
1263 struct ctf_trace
*ctf_trace
;
1264 struct relay_session
*session
;
1268 DBG("Viewer get next index");
1270 health_code_update();
1272 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1276 health_code_update();
1279 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1285 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1291 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1294 memset(&viewer_index
, 0, sizeof(viewer_index
));
1297 * The viewer should not ask for index on metadata stream.
1299 if (vstream
->metadata_flag
) {
1300 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1304 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1307 /* Try to open an index if one is needed for that stream. */
1308 ret
= try_open_index(vstream
, rstream
);
1310 if (ret
== -ENOENT
) {
1312 * The index is created only when the first data packet arrives, it
1313 * might not be ready at the beginning of the session
1315 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1317 /* Unhandled error. */
1318 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1323 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1324 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1325 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1328 } else if (ret
== 1) {
1330 * This means the viewer index data structure has been populated by the
1331 * check call thus we now send back the reply to the client.
1335 /* At this point, ret MUST be 0 thus we continue with the get. */
1338 if (!ctf_trace
->metadata_received
||
1339 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1340 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1343 ret
= check_new_streams(conn
);
1346 } else if (ret
== 1) {
1347 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1350 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1351 pthread_mutex_lock(&vstream
->overwrite_lock
);
1352 if (vstream
->abort_flag
) {
1353 /* The file is being overwritten by the writer, we cannot use it. */
1354 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1355 ret
= viewer_stream_rotate(vstream
, rstream
);
1356 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1359 } else if (ret
== 1) {
1360 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1361 viewer_stream_delete(vstream
);
1362 viewer_stream_destroy(ctf_trace
, vstream
);
1364 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1369 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1370 sizeof(packet_index
));
1371 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1372 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1374 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1375 viewer_stream_delete(vstream
);
1376 viewer_stream_destroy(ctf_trace
, vstream
);
1378 } else if (read_ret
< sizeof(packet_index
)) {
1379 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1380 if (vstream
->close_write_flag
) {
1381 ret
= viewer_stream_rotate(vstream
, rstream
);
1383 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1385 } else if (ret
== 1) {
1386 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1387 viewer_stream_delete(vstream
);
1388 viewer_stream_destroy(ctf_trace
, vstream
);
1390 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1393 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1394 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1396 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1400 vstream
->last_sent_index
++;
1404 * Indexes are stored in big endian, no need to switch before sending.
1406 viewer_index
.offset
= packet_index
.offset
;
1407 viewer_index
.packet_size
= packet_index
.packet_size
;
1408 viewer_index
.content_size
= packet_index
.content_size
;
1409 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1410 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1411 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1412 viewer_index
.stream_id
= packet_index
.stream_id
;
1415 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1416 health_code_update();
1418 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1422 health_code_update();
1424 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1425 vstream
->last_sent_index
, vstream
->stream_handle
);
1435 * Send the next index for a stream
1437 * Return 0 on success or else a negative value.
1440 int viewer_get_packet(struct relay_connection
*conn
)
1442 int ret
, send_data
= 0;
1446 struct lttng_viewer_get_packet get_packet_info
;
1447 struct lttng_viewer_trace_packet reply
;
1448 struct relay_viewer_stream
*stream
;
1449 struct relay_session
*session
;
1450 struct ctf_trace
*ctf_trace
;
1454 DBG2("Relay get data packet");
1456 health_code_update();
1458 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1462 health_code_update();
1464 /* From this point on, the error label can be reached. */
1465 memset(&reply
, 0, sizeof(reply
));
1468 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1473 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1479 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1484 * First time we read this stream, we need open the tracefile, we should
1485 * only arrive here if an index has already been sent to the viewer, so the
1486 * tracefile must exist, if it does not it is a fatal error.
1488 if (stream
->read_fd
< 0) {
1489 char fullpath
[PATH_MAX
];
1491 if (stream
->tracefile_count
> 0) {
1492 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1493 stream
->channel_name
,
1494 stream
->tracefile_count_current
);
1496 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1497 stream
->channel_name
);
1502 ret
= open(fullpath
, O_RDONLY
);
1504 PERROR("Relay opening trace file");
1507 stream
->read_fd
= ret
;
1510 if (!ctf_trace
->metadata_received
||
1511 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1512 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1513 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1517 ret
= check_new_streams(conn
);
1520 } else if (ret
== 1) {
1521 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1522 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1526 len
= be32toh(get_packet_info
.len
);
1527 data
= zmalloc(len
);
1529 PERROR("relay data zmalloc");
1533 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1536 * If the read fd was closed by the streaming side, the
1537 * abort_flag will be set to 1, otherwise it is an error.
1539 if (stream
->abort_flag
== 0) {
1543 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1546 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1547 if (read_len
< len
) {
1549 * If the read fd was closed by the streaming side, the
1550 * abort_flag will be set to 1, otherwise it is an error.
1552 if (stream
->abort_flag
== 0) {
1553 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1555 be64toh(get_packet_info
.offset
));
1558 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1562 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1563 reply
.len
= htobe32(len
);
1568 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1571 reply
.flags
= htobe32(reply
.flags
);
1573 health_code_update();
1575 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1579 health_code_update();
1582 health_code_update();
1583 ret
= send_response(conn
->sock
, data
, len
);
1587 health_code_update();
1590 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1591 be64toh(get_packet_info
.stream_id
));
1602 * Send the session's metadata
1604 * Return 0 on success else a negative value.
1607 int viewer_get_metadata(struct relay_connection
*conn
)
1613 struct lttng_viewer_get_metadata request
;
1614 struct lttng_viewer_metadata_packet reply
;
1615 struct relay_viewer_stream
*stream
;
1616 struct ctf_trace
*ctf_trace
;
1617 struct relay_session
*session
;
1621 DBG("Relay get metadata");
1623 health_code_update();
1625 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1629 health_code_update();
1631 memset(&reply
, 0, sizeof(reply
));
1634 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1635 if (!stream
|| !stream
->metadata_flag
) {
1636 ERR("Invalid metadata stream");
1640 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1646 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1649 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1651 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1653 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1657 /* first time, we open the metadata file */
1658 if (stream
->read_fd
< 0) {
1659 char fullpath
[PATH_MAX
];
1661 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1662 stream
->channel_name
);
1666 ret
= open(fullpath
, O_RDONLY
);
1668 PERROR("Relay opening metadata file");
1671 stream
->read_fd
= ret
;
1674 reply
.len
= htobe64(len
);
1675 data
= zmalloc(len
);
1677 PERROR("viewer metadata zmalloc");
1681 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1682 if (read_len
< len
) {
1683 PERROR("Relay reading metadata file");
1686 ctf_trace
->metadata_sent
+= read_len
;
1687 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1691 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1694 health_code_update();
1695 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1699 health_code_update();
1702 ret
= send_response(conn
->sock
, data
, len
);
1708 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1709 be64toh(request
.stream_id
));
1711 DBG("Metadata sent");
1721 * Create a viewer session.
1723 * Return 0 on success or else a negative value.
1726 int viewer_create_session(struct relay_connection
*conn
)
1729 struct lttng_viewer_create_session_response resp
;
1731 DBG("Viewer create session received");
1733 memset(&resp
, 0, sizeof(resp
));
1734 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1735 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1736 if (!conn
->viewer_session
) {
1737 ERR("Allocation viewer session");
1738 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1741 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1744 health_code_update();
1745 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1749 health_code_update();
1758 * live_relay_unknown_command: send -1 if received unknown command
1761 void live_relay_unknown_command(struct relay_connection
*conn
)
1763 struct lttcomm_relayd_generic_reply reply
;
1765 memset(&reply
, 0, sizeof(reply
));
1766 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1767 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1771 * Process the commands received on the control socket
1774 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1775 struct relay_connection
*conn
)
1783 msg_value
= be32toh(recv_hdr
->cmd
);
1786 * Make sure we've done the version check before any command other then a
1787 * new client connection.
1789 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1790 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1795 switch (msg_value
) {
1796 case LTTNG_VIEWER_CONNECT
:
1797 ret
= viewer_connect(conn
);
1799 case LTTNG_VIEWER_LIST_SESSIONS
:
1800 ret
= viewer_list_sessions(conn
);
1802 case LTTNG_VIEWER_ATTACH_SESSION
:
1803 ret
= viewer_attach_session(conn
);
1805 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1806 ret
= viewer_get_next_index(conn
);
1808 case LTTNG_VIEWER_GET_PACKET
:
1809 ret
= viewer_get_packet(conn
);
1811 case LTTNG_VIEWER_GET_METADATA
:
1812 ret
= viewer_get_metadata(conn
);
1814 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1815 ret
= viewer_get_new_streams(conn
);
1817 case LTTNG_VIEWER_CREATE_SESSION
:
1818 ret
= viewer_create_session(conn
);
1821 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1822 live_relay_unknown_command(conn
);
1832 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1838 (void) lttng_poll_del(events
, pollfd
);
1840 ret
= close(pollfd
);
1842 ERR("Closing pollfd %d", pollfd
);
1847 * Delete and destroy a connection.
1849 * RCU read side lock MUST be acquired.
1851 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1852 struct relay_connection
*conn
)
1854 struct relay_session
*session
, *tmp_session
;
1856 assert(relay_connections_ht
);
1859 connection_delete(relay_connections_ht
, conn
);
1861 if (!conn
->viewer_session
) {
1866 cds_list_for_each_entry_safe(session
, tmp_session
,
1867 &conn
->viewer_session
->sessions_head
,
1868 viewer_session_list
) {
1869 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1870 cleanup_session(conn
, session
);
1875 connection_destroy(conn
);
1879 * This thread does the actual work
1882 void *thread_worker(void *data
)
1886 struct relay_connection
*conn
;
1887 struct lttng_poll_event events
;
1888 struct lttng_ht
*relay_connections_ht
;
1889 struct lttng_ht_iter iter
;
1890 struct lttng_viewer_cmd recv_hdr
;
1891 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1892 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1894 DBG("[thread] Live viewer relay worker started");
1896 rcu_register_thread();
1898 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1900 if (testpoint(relayd_thread_live_worker
)) {
1901 goto error_testpoint
;
1904 /* table of connections indexed on socket */
1905 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1906 if (!relay_connections_ht
) {
1907 goto relay_connections_ht_error
;
1910 ret
= create_thread_poll_set(&events
, 2);
1912 goto error_poll_create
;
1915 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1924 health_code_update();
1926 /* Infinite blocking call, waiting for transmission */
1927 DBG3("Relayd live viewer worker thread polling...");
1928 health_poll_entry();
1929 ret
= lttng_poll_wait(&events
, -1);
1933 * Restart interrupted system call.
1935 if (errno
== EINTR
) {
1944 * Process control. The control connection is prioritised so we don't
1945 * starve it with high throughput tracing data on the data
1948 for (i
= 0; i
< nb_fd
; i
++) {
1949 /* Fetch once the poll data */
1950 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1951 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1953 health_code_update();
1955 /* Thread quit pipe has been closed. Killing thread. */
1956 ret
= check_thread_quit_pipe(pollfd
, revents
);
1962 /* Inspect the relay conn pipe for new connection */
1963 if (pollfd
== live_conn_pipe
[0]) {
1964 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1965 ERR("Relay live pipe error");
1967 } else if (revents
& LPOLLIN
) {
1968 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1972 conn
->sessions_ht
= sessions_ht
;
1973 connection_init(conn
);
1974 lttng_poll_add(&events
, conn
->sock
->fd
,
1975 LPOLLIN
| LPOLLRDHUP
);
1977 lttng_ht_add_unique_ulong(relay_connections_ht
,
1980 DBG("Connection socket %d added", conn
->sock
->fd
);
1984 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1985 /* If not found, there is a synchronization issue. */
1988 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1989 cleanup_connection_pollfd(&events
, pollfd
);
1990 destroy_connection(relay_connections_ht
, conn
);
1991 } else if (revents
& LPOLLIN
) {
1992 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1993 sizeof(recv_hdr
), 0);
1995 /* Connection closed */
1996 cleanup_connection_pollfd(&events
, pollfd
);
1997 destroy_connection(relay_connections_ht
, conn
);
1998 DBG("Viewer control conn closed with %d", pollfd
);
2000 ret
= process_control(&recv_hdr
, conn
);
2002 /* Clear the session on error. */
2003 cleanup_connection_pollfd(&events
, pollfd
);
2004 destroy_connection(relay_connections_ht
, conn
);
2005 DBG("Viewer connection closed with %d", pollfd
);
2016 lttng_poll_clean(&events
);
2018 /* Cleanup reamaining connection object. */
2020 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2022 health_code_update();
2023 destroy_connection(relay_connections_ht
, conn
);
2027 lttng_ht_destroy(relay_connections_ht
);
2028 relay_connections_ht_error
:
2029 /* Close relay conn pipes */
2030 utils_close_pipe(live_conn_pipe
);
2032 DBG("Viewer worker thread exited with error");
2034 DBG("Viewer worker thread cleanup complete");
2038 ERR("Health error occurred in %s", __func__
);
2040 health_unregister(health_relayd
);
2041 if (stop_threads()) {
2042 ERR("Error stopping live threads");
2044 rcu_unregister_thread();
2049 * Create the relay command pipe to wake thread_manage_apps.
2050 * Closed in cleanup().
2052 static int create_conn_pipe(void)
2054 return utils_create_pipe_cloexec(live_conn_pipe
);
2057 int relayd_live_stop(void)
2059 return stop_threads();
2062 int relayd_live_join(void)
2064 int ret
, retval
= 0;
2067 ret
= pthread_join(live_listener_thread
, &status
);
2070 PERROR("pthread_join live listener");
2074 ret
= pthread_join(live_worker_thread
, &status
);
2077 PERROR("pthread_join live worker");
2081 ret
= pthread_join(live_dispatcher_thread
, &status
);
2084 PERROR("pthread_join live dispatcher");
2088 cleanup_relayd_live();
2096 int relayd_live_create(struct lttng_uri
*uri
,
2097 struct relay_local_data
*relay_ctx
)
2099 int ret
= 0, retval
= 0;
2105 goto exit_init_data
;
2109 /* Check if daemon is UID = 0 */
2110 is_root
= !getuid();
2113 if (live_uri
->port
< 1024) {
2114 ERR("Need to be root to use ports < 1024");
2116 goto exit_init_data
;
2120 /* Setup the thread apps communication pipe. */
2121 if (create_conn_pipe()) {
2123 goto exit_init_data
;
2126 /* Init relay command queue. */
2127 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2129 /* Set up max poll set size */
2130 lttng_poll_set_max_size();
2132 /* Setup the dispatcher thread */
2133 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2134 thread_dispatcher
, (void *) NULL
);
2137 PERROR("pthread_create viewer dispatcher");
2139 goto exit_dispatcher_thread
;
2142 /* Setup the worker thread */
2143 ret
= pthread_create(&live_worker_thread
, NULL
,
2144 thread_worker
, relay_ctx
);
2147 PERROR("pthread_create viewer worker");
2149 goto exit_worker_thread
;
2152 /* Setup the listener thread */
2153 ret
= pthread_create(&live_listener_thread
, NULL
,
2154 thread_listener
, (void *) NULL
);
2157 PERROR("pthread_create viewer listener");
2159 goto exit_listener_thread
;
2163 * All OK, started all threads.
2168 ret
= pthread_join(live_listener_thread
, &status
);
2171 PERROR("pthread_join live listener");
2174 exit_listener_thread
:
2176 ret
= pthread_join(live_worker_thread
, &status
);
2179 PERROR("pthread_join live worker");
2184 ret
= pthread_join(live_dispatcher_thread
, &status
);
2187 PERROR("pthread_join live dispatcher");
2190 exit_dispatcher_thread
:
2193 cleanup_relayd_live();