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.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
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>
48 #include <common/index/index.h>
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>
57 #include "lttng-relayd.h"
59 #include "health-relayd.h"
60 #include "testpoint.h"
61 #include "viewer-stream.h"
64 #include "ctf-trace.h"
65 #include "connection.h"
67 static struct lttng_uri
*live_uri
;
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
73 static int live_conn_pipe
[2] = { -1, -1 };
75 /* Shared between threads */
76 static int live_dispatch_thread_exit
;
78 static pthread_t live_listener_thread
;
79 static pthread_t live_dispatcher_thread
;
80 static pthread_t live_worker_thread
;
83 * Relay command queue.
85 * The live_thread_listener and live_thread_dispatcher communicate with this
88 static struct relay_conn_queue viewer_conn_queue
;
90 static uint64_t last_relay_viewer_session_id
;
104 * Receive a request buffer using a given socket, destination allocated buffer
107 * Return the size of the received message or else a negative value on error
108 * with errno being set by recvmsg() syscall.
111 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
118 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
119 if (ret
< 0 || ret
!= size
) {
121 /* Orderly shutdown. Not necessary to print an error. */
122 DBG("Socket %d did an orderly shutdown", sock
->fd
);
124 ERR("Relay failed to receive request.");
133 * Send a response buffer using a given socket, source allocated buffer of
136 * Return the size of the sent message or else a negative value on error with
137 * errno being set by sendmsg() syscall.
140 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
147 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
149 ERR("Relayd failed to send response.");
156 * Atomically check if new streams got added in one of the sessions attached
157 * and reset the flag to 0.
159 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
163 int check_new_streams(struct relay_connection
*conn
)
165 struct relay_session
*session
;
166 unsigned long current_val
;
169 if (!conn
->viewer_session
) {
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);
187 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
188 * this function should ignore the sent flag or not.
190 * Return 0 on success or else a negative value.
193 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
194 struct relay_session
*session
, unsigned int ignore_sent_flag
)
197 struct lttng_viewer_stream send_stream
;
198 struct lttng_ht_iter iter
;
199 struct relay_viewer_stream
*vstream
;
205 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
207 struct ctf_trace
*ctf_trace
;
209 health_code_update();
211 /* Ignore if not the same session. */
212 if (vstream
->session_id
!= session
->id
||
213 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
217 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
221 send_stream
.id
= htobe64(vstream
->stream_handle
);
222 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
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
));
229 DBG("Sending stream %" PRIu64
" to viewer", vstream
->stream_handle
);
230 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
234 vstream
->sent_flag
= 1;
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.
250 * Return 0 on success or else a negative value.
253 int 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
)
258 struct lttng_ht_iter iter
;
259 struct ctf_trace
*ctf_trace
;
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
270 pthread_mutex_lock(&session
->viewer_ready_lock
);
273 * Create viewer streams for relay streams that are ready to be used for a
274 * the given session id only.
277 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
279 struct relay_stream
*stream
;
281 health_code_update();
283 if (ctf_trace
->invalid_flag
) {
287 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
288 struct relay_viewer_stream
*vstream
;
290 if (!stream
->viewer_ready
) {
294 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
296 vstream
= viewer_stream_create(stream
, seek_t
, ctf_trace
);
301 /* Acquire reference to ctf_trace. */
302 ctf_trace_get_ref(ctf_trace
);
305 /* Update number of created stream counter. */
308 } else if (!vstream
->sent_flag
&& nb_unsent
) {
309 /* Update number of unsent stream counter. */
312 /* Update number of total stream counter. */
323 pthread_mutex_unlock(&session
->viewer_ready_lock
);
328 * Write to writable pipe used to notify a thread.
331 int notify_thread_pipe(int wpipe
)
335 ret
= lttng_write(wpipe
, "!", 1);
337 PERROR("write poll pipe");
344 * Stop all threads by closing the thread quit pipe.
347 void stop_threads(void)
351 /* Stopping all threads */
352 DBG("Terminating all live threads");
353 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
355 ERR("write error on thread quit pipe");
358 /* Dispatch thread */
359 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
360 futex_nto1_wake(&viewer_conn_queue
.futex
);
364 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
367 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
371 if (events
== NULL
|| size
== 0) {
376 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
382 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
394 * Check if the thread quit pipe was triggered.
396 * Return 1 if it was triggered else 0;
399 int check_thread_quit_pipe(int fd
, uint32_t events
)
401 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
409 * Create and init socket from uri.
412 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
415 struct lttcomm_sock
*sock
= NULL
;
417 sock
= lttcomm_alloc_sock_from_uri(uri
);
419 ERR("Allocating socket");
423 ret
= lttcomm_create_sock(sock
);
427 DBG("Listening on sock %d for live", sock
->fd
);
429 ret
= sock
->ops
->bind(sock
);
434 ret
= sock
->ops
->listen(sock
, -1);
444 lttcomm_destroy_sock(sock
);
450 * This thread manages the listening for new connections on the network
453 void *thread_listener(void *data
)
455 int i
, ret
, pollfd
, err
= -1;
456 uint32_t revents
, nb_fd
;
457 struct lttng_poll_event events
;
458 struct lttcomm_sock
*live_control_sock
;
460 DBG("[thread] Relay live listener started");
462 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
464 health_code_update();
466 live_control_sock
= init_socket(live_uri
);
467 if (!live_control_sock
) {
468 goto error_sock_control
;
471 /* Pass 2 as size here for the thread quit pipe and control sockets. */
472 ret
= create_thread_poll_set(&events
, 2);
474 goto error_create_poll
;
477 /* Add the control socket */
478 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
483 lttng_relay_notify_ready();
485 if (testpoint(relayd_thread_live_listener
)) {
486 goto error_testpoint
;
490 health_code_update();
492 DBG("Listener accepting live viewers connections");
496 ret
= lttng_poll_wait(&events
, -1);
500 * Restart interrupted system call.
502 if (errno
== EINTR
) {
509 DBG("Relay new viewer connection received");
510 for (i
= 0; i
< nb_fd
; i
++) {
511 health_code_update();
513 /* Fetch once the poll data */
514 revents
= LTTNG_POLL_GETEV(&events
, i
);
515 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
518 /* No activity for this FD (poll implementation). */
522 /* Thread quit pipe has been closed. Killing thread. */
523 ret
= check_thread_quit_pipe(pollfd
, revents
);
529 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
530 ERR("socket poll error");
532 } else if (revents
& LPOLLIN
) {
534 * Get allocated in this thread, enqueued to a global queue,
535 * dequeued and freed in the worker thread.
538 struct relay_connection
*new_conn
;
539 struct lttcomm_sock
*newsock
;
541 new_conn
= connection_create();
546 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
548 PERROR("accepting control sock");
549 connection_free(new_conn
);
552 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
554 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
557 PERROR("setsockopt inet");
558 lttcomm_destroy_sock(newsock
);
559 connection_free(new_conn
);
562 new_conn
->sock
= newsock
;
564 /* Enqueue request for the dispatcher thread. */
565 cds_wfq_enqueue(&viewer_conn_queue
.queue
, &new_conn
->qnode
);
568 * Wake the dispatch queue futex. Implicit memory barrier with
569 * the exchange in cds_wfq_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");
601 * This thread manages the dispatching of the requests to worker threads
604 void *thread_dispatcher(void *data
)
608 struct cds_wfq_node
*node
;
609 struct relay_connection
*conn
= NULL
;
611 DBG("[thread] Live viewer relay dispatcher started");
613 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
615 if (testpoint(relayd_thread_live_dispatcher
)) {
616 goto error_testpoint
;
619 health_code_update();
621 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
622 health_code_update();
624 /* Atomically prepare the queue futex */
625 futex_nto1_prepare(&viewer_conn_queue
.futex
);
628 health_code_update();
630 /* Dequeue commands */
631 node
= cds_wfq_dequeue_blocking(&viewer_conn_queue
.queue
);
633 DBG("Woken up but nothing in the live-viewer "
634 "relay command queue");
635 /* Continue thread execution */
638 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
639 DBG("Dispatching viewer request waiting on sock %d",
643 * Inform worker thread of the new request. This call is blocking
644 * so we can be assured that the data will be read at some point in
645 * time or wait to the end of the world :)
647 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
649 PERROR("write conn pipe");
650 connection_destroy(conn
);
653 } while (node
!= NULL
);
655 /* Futex wait on queue. Blocking call on futex() */
657 futex_nto1_wait(&viewer_conn_queue
.futex
);
661 /* Normal exit, no error */
668 ERR("Health error occurred in %s", __func__
);
670 health_unregister(health_relayd
);
671 DBG("Live viewer dispatch thread dying");
677 * Establish connection with the viewer and check the versions.
679 * Return 0 on success or else negative value.
682 int viewer_connect(struct relay_connection
*conn
)
685 struct lttng_viewer_connect reply
, msg
;
689 conn
->version_check_done
= 1;
691 health_code_update();
693 DBG("Viewer is establishing a connection to the relayd.");
695 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
700 health_code_update();
702 memset(&reply
, 0, sizeof(reply
));
703 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
704 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
706 /* Major versions must be the same */
707 if (reply
.major
!= be32toh(msg
.major
)) {
708 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
709 reply
.major
, be32toh(msg
.major
));
714 conn
->major
= reply
.major
;
715 /* We adapt to the lowest compatible version */
716 if (reply
.minor
<= be32toh(msg
.minor
)) {
717 conn
->minor
= reply
.minor
;
719 conn
->minor
= be32toh(msg
.minor
);
722 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
723 conn
->type
= RELAY_VIEWER_COMMAND
;
724 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
725 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
727 ERR("Unknown connection type : %u", be32toh(msg
.type
));
732 reply
.major
= htobe32(reply
.major
);
733 reply
.minor
= htobe32(reply
.minor
);
734 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
736 * Increment outside of htobe64 macro, because can be used more than once
737 * within the macro, and thus the operation may be undefined.
739 last_relay_viewer_session_id
++;
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();
825 * Check if a connection is attached to a session.
826 * Return 1 if attached, 0 if not attached, a negative value on error.
829 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
831 struct relay_session
*session
;
834 if (!conn
->viewer_session
) {
837 cds_list_for_each_entry(session
,
838 &conn
->viewer_session
->sessions_head
,
839 viewer_session_list
) {
840 if (session
->id
== session_id
) {
851 * Delete all streams for a specific session ID.
853 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
855 struct relay_viewer_stream
*stream
;
856 struct lttng_ht_iter iter
;
861 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
863 struct ctf_trace
*ctf_trace
;
865 health_code_update();
866 if (stream
->session_id
!= session
->id
) {
870 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
874 viewer_stream_delete(stream
);
876 if (stream
->metadata_flag
) {
877 ctf_trace
->metadata_sent
= 0;
878 ctf_trace
->viewer_metadata_stream
= NULL
;
881 viewer_stream_destroy(ctf_trace
, stream
);
886 static void try_destroy_streams(struct relay_session
*session
)
888 struct ctf_trace
*ctf_trace
;
889 struct lttng_ht_iter iter
;
893 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
895 /* Attempt to destroy the ctf trace of that session. */
896 ctf_trace_try_destroy(session
, ctf_trace
);
903 static void cleanup_session(struct relay_connection
*conn
,
904 struct relay_session
*session
)
907 * Very important that this is done before destroying the session so we
908 * can put back every viewer stream reference from the ctf_trace.
910 destroy_viewer_streams_by_session(session
);
911 try_destroy_streams(session
);
912 cds_list_del(&session
->viewer_session_list
);
913 session_viewer_try_destroy(conn
->sessions_ht
, session
);
917 * Send the viewer the list of current sessions.
920 int viewer_get_new_streams(struct relay_connection
*conn
)
922 int ret
, send_streams
= 0;
923 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
924 struct lttng_viewer_new_streams_request request
;
925 struct lttng_viewer_new_streams_response response
;
926 struct relay_session
*session
;
931 DBG("Get new streams received");
933 health_code_update();
935 /* Receive the request from the connected client. */
936 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
940 session_id
= be64toh(request
.session_id
);
942 health_code_update();
944 memset(&response
, 0, sizeof(response
));
947 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
949 DBG("Relay session %" PRIu64
" not found", session_id
);
950 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
954 if (!session_attached(conn
, session_id
)) {
956 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
961 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
963 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
968 /* Only send back the newly created streams with the unsent ones. */
969 nb_streams
= nb_created
+ nb_unsent
;
970 response
.streams_count
= htobe32(nb_streams
);
973 * If the session is closed and we have no new streams to send,
974 * it means that the viewer has already received the whole trace
975 * for this session and should now close it.
977 if (nb_streams
== 0 && session
->close_flag
) {
979 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
981 * Remove the session from the attached list of the connection
982 * and try to destroy it.
984 cds_list_del(&session
->viewer_session_list
);
985 cleanup_session(conn
, session
);
990 health_code_update();
991 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
995 health_code_update();
998 * Unknown or empty session, just return gracefully, the viewer knows what
1001 if (!send_streams
|| !nb_streams
) {
1007 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1008 * that were not sent from that point will be sent to the viewer.
1010 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1022 * Send the viewer the list of current sessions.
1025 int viewer_attach_session(struct relay_connection
*conn
)
1027 int send_streams
= 0;
1029 uint32_t nb_streams
= 0;
1030 enum lttng_viewer_seek seek_type
;
1031 struct lttng_viewer_attach_session_request request
;
1032 struct lttng_viewer_attach_session_response response
;
1033 struct relay_session
*session
;
1037 health_code_update();
1039 /* Receive the request from the connected client. */
1040 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1045 health_code_update();
1047 memset(&response
, 0, sizeof(response
));
1049 if (!conn
->viewer_session
) {
1050 DBG("Client trying to attach before creating a live viewer session");
1051 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1056 session
= session_find_by_id(conn
->sessions_ht
,
1057 be64toh(request
.session_id
));
1059 DBG("Relay session %" PRIu64
" not found",
1060 be64toh(request
.session_id
));
1061 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1064 session_viewer_attach(session
);
1065 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1067 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1068 DBG("Already a viewer attached");
1069 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1070 session_viewer_detach(session
);
1072 } else if (session
->live_timer
== 0) {
1073 DBG("Not live session");
1074 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1078 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1079 cds_list_add(&session
->viewer_session_list
,
1080 &conn
->viewer_session
->sessions_head
);
1083 switch (be32toh(request
.seek
)) {
1084 case LTTNG_VIEWER_SEEK_BEGINNING
:
1085 case LTTNG_VIEWER_SEEK_LAST
:
1086 seek_type
= be32toh(request
.seek
);
1089 ERR("Wrong seek parameter");
1090 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1095 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1099 response
.streams_count
= htobe32(nb_streams
);
1102 health_code_update();
1103 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1107 health_code_update();
1110 * Unknown or empty session, just return gracefully, the viewer knows what
1113 if (!send_streams
|| !nb_streams
) {
1118 /* Send stream and ignore the sent flag. */
1119 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1131 * Open the index file if needed for the given vstream.
1133 * If an index file is successfully opened, the index_read_fd of the stream is
1136 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1138 static int try_open_index(struct relay_viewer_stream
*vstream
,
1139 struct relay_stream
*rstream
)
1146 if (vstream
->index_read_fd
>= 0) {
1151 * First time, we open the index file and at least one index is ready. The
1152 * race between the read and write of the total_index_received is
1153 * acceptable here since the client will be notified to simply come back
1154 * and get the next index.
1156 if (rstream
->total_index_received
<= 0) {
1160 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1161 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1163 vstream
->index_read_fd
= ret
;
1173 * Check the status of the index for the given stream. This function updates
1174 * the index structure if needed and can destroy the vstream also for the HUP
1177 * Return 0 means that we can proceed with the index. A value of 1 means that
1178 * the index has been updated and is ready to be send to the client. A negative
1179 * value indicates an error that can't be handled.
1181 static int check_index_status(struct relay_viewer_stream
*vstream
,
1182 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1183 struct lttng_viewer_index
*index
)
1192 if (!rstream
->close_flag
) {
1193 /* Rotate on abort (overwrite). */
1194 if (vstream
->abort_flag
) {
1195 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1196 vstream
->stream_handle
);
1197 ret
= viewer_stream_rotate(vstream
, rstream
);
1200 } else if (ret
== 1) {
1202 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1205 /* ret == 0 means successful so we continue. */
1208 /* Check if we are in the same trace file at this point. */
1209 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1210 if (rstream
->beacon_ts_end
!= -1ULL &&
1211 vstream
->last_sent_index
== rstream
->total_index_received
) {
1213 * We've received a synchronization beacon and the last index
1214 * available has been sent, the index for now is inactive.
1216 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1217 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1218 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1220 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1221 && !vstream
->close_write_flag
) {
1223 * Reader and writer are working in the same tracefile, so we care
1224 * about the number of index received and sent. Otherwise, we read
1227 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1231 /* Nothing to do with the index, continue with it. */
1233 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1234 vstream
->total_index_received
== vstream
->last_sent_index
) {
1235 /* Last index sent and current tracefile closed in write */
1236 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1239 vstream
->close_write_flag
= 1;
1247 viewer_stream_delete(vstream
);
1248 viewer_stream_destroy(trace
, vstream
);
1254 * Send the next index for a stream.
1256 * Return 0 on success or else a negative value.
1259 int viewer_get_next_index(struct relay_connection
*conn
)
1263 struct lttng_viewer_get_next_index request_index
;
1264 struct lttng_viewer_index viewer_index
;
1265 struct ctf_packet_index packet_index
;
1266 struct relay_viewer_stream
*vstream
;
1267 struct relay_stream
*rstream
;
1268 struct ctf_trace
*ctf_trace
;
1269 struct relay_session
*session
;
1273 DBG("Viewer get next index");
1275 health_code_update();
1277 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1281 health_code_update();
1284 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1290 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1296 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1299 memset(&viewer_index
, 0, sizeof(viewer_index
));
1302 * The viewer should not ask for index on metadata stream.
1304 if (vstream
->metadata_flag
) {
1305 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1309 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1312 /* Try to open an index if one is needed for that stream. */
1313 ret
= try_open_index(vstream
, rstream
);
1315 if (ret
== -ENOENT
) {
1317 * The index is created only when the first data packet arrives, it
1318 * might not be ready at the beginning of the session
1320 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1322 /* Unhandled error. */
1323 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1328 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1329 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1330 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1333 } else if (ret
== 1) {
1335 * This means the viewer index data structure has been populated by the
1336 * check call thus we now send back the reply to the client.
1340 /* At this point, ret MUST be 0 thus we continue with the get. */
1343 if (!ctf_trace
->metadata_received
||
1344 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1345 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1348 ret
= check_new_streams(conn
);
1351 } else if (ret
== 1) {
1352 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1355 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1356 pthread_mutex_lock(&vstream
->overwrite_lock
);
1357 if (vstream
->abort_flag
) {
1358 /* The file is being overwritten by the writer, we cannot use it. */
1359 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1360 ret
= viewer_stream_rotate(vstream
, rstream
);
1361 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1364 } else if (ret
== 1) {
1365 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1366 viewer_stream_delete(vstream
);
1367 viewer_stream_destroy(ctf_trace
, vstream
);
1369 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1374 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1375 sizeof(packet_index
));
1376 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1377 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1379 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1380 viewer_stream_delete(vstream
);
1381 viewer_stream_destroy(ctf_trace
, vstream
);
1383 } else if (read_ret
< sizeof(packet_index
)) {
1384 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1385 if (vstream
->close_write_flag
) {
1386 ret
= viewer_stream_rotate(vstream
, rstream
);
1388 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1390 } else if (ret
== 1) {
1391 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1392 viewer_stream_delete(vstream
);
1393 viewer_stream_destroy(ctf_trace
, vstream
);
1395 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1398 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1401 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1404 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1405 vstream
->last_sent_index
++;
1409 * Indexes are stored in big endian, no need to switch before sending.
1411 viewer_index
.offset
= packet_index
.offset
;
1412 viewer_index
.packet_size
= packet_index
.packet_size
;
1413 viewer_index
.content_size
= packet_index
.content_size
;
1414 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1415 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1416 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1417 viewer_index
.stream_id
= packet_index
.stream_id
;
1420 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1421 health_code_update();
1423 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1427 health_code_update();
1429 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1430 vstream
->last_sent_index
, vstream
->stream_handle
);
1440 * Send the next index for a stream
1442 * Return 0 on success or else a negative value.
1445 int viewer_get_packet(struct relay_connection
*conn
)
1447 int ret
, send_data
= 0;
1451 struct lttng_viewer_get_packet get_packet_info
;
1452 struct lttng_viewer_trace_packet reply
;
1453 struct relay_viewer_stream
*stream
;
1454 struct relay_session
*session
;
1455 struct ctf_trace
*ctf_trace
;
1459 DBG2("Relay get data packet");
1461 health_code_update();
1463 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1467 health_code_update();
1469 /* From this point on, the error label can be reached. */
1470 memset(&reply
, 0, sizeof(reply
));
1473 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1478 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1484 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1489 * First time we read this stream, we need open the tracefile, we should
1490 * only arrive here if an index has already been sent to the viewer, so the
1491 * tracefile must exist, if it does not it is a fatal error.
1493 if (stream
->read_fd
< 0) {
1494 char fullpath
[PATH_MAX
];
1496 if (stream
->tracefile_count
> 0) {
1497 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1498 stream
->channel_name
,
1499 stream
->tracefile_count_current
);
1501 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1502 stream
->channel_name
);
1507 ret
= open(fullpath
, O_RDONLY
);
1509 PERROR("Relay opening trace file");
1512 stream
->read_fd
= ret
;
1515 if (!ctf_trace
->metadata_received
||
1516 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1517 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1518 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1522 ret
= check_new_streams(conn
);
1525 } else if (ret
== 1) {
1526 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1527 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1531 len
= be32toh(get_packet_info
.len
);
1532 data
= zmalloc(len
);
1534 PERROR("relay data zmalloc");
1538 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1541 * If the read fd was closed by the streaming side, the
1542 * abort_flag will be set to 1, otherwise it is an error.
1544 if (stream
->abort_flag
== 0) {
1548 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1551 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1552 if (read_len
< len
) {
1554 * If the read fd was closed by the streaming side, the
1555 * abort_flag will be set to 1, otherwise it is an error.
1557 if (stream
->abort_flag
== 0) {
1558 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1560 be64toh(get_packet_info
.offset
));
1563 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1567 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1568 reply
.len
= htobe32(len
);
1573 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1576 reply
.flags
= htobe32(reply
.flags
);
1578 health_code_update();
1580 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1584 health_code_update();
1587 health_code_update();
1588 ret
= send_response(conn
->sock
, data
, len
);
1592 health_code_update();
1595 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1596 be64toh(get_packet_info
.stream_id
));
1607 * Send the session's metadata
1609 * Return 0 on success else a negative value.
1612 int viewer_get_metadata(struct relay_connection
*conn
)
1618 struct lttng_viewer_get_metadata request
;
1619 struct lttng_viewer_metadata_packet reply
;
1620 struct relay_viewer_stream
*stream
;
1621 struct ctf_trace
*ctf_trace
;
1622 struct relay_session
*session
;
1626 DBG("Relay get metadata");
1628 health_code_update();
1630 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1634 health_code_update();
1636 memset(&reply
, 0, sizeof(reply
));
1639 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1640 if (!stream
|| !stream
->metadata_flag
) {
1641 ERR("Invalid metadata stream");
1645 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1651 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1654 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1656 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1658 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1662 /* first time, we open the metadata file */
1663 if (stream
->read_fd
< 0) {
1664 char fullpath
[PATH_MAX
];
1666 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1667 stream
->channel_name
);
1671 ret
= open(fullpath
, O_RDONLY
);
1673 PERROR("Relay opening metadata file");
1676 stream
->read_fd
= ret
;
1679 reply
.len
= htobe64(len
);
1680 data
= zmalloc(len
);
1682 PERROR("viewer metadata zmalloc");
1686 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1687 if (read_len
< len
) {
1688 PERROR("Relay reading metadata file");
1691 ctf_trace
->metadata_sent
+= read_len
;
1692 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1696 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1699 health_code_update();
1700 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1704 health_code_update();
1707 ret
= send_response(conn
->sock
, data
, len
);
1713 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1714 be64toh(request
.stream_id
));
1716 DBG("Metadata sent");
1726 * Create a viewer session.
1728 * Return 0 on success or else a negative value.
1731 int viewer_create_session(struct relay_connection
*conn
)
1734 struct lttng_viewer_create_session_response resp
;
1736 DBG("Viewer create session received");
1738 memset(&resp
, 0, sizeof(resp
));
1739 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1740 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1741 if (!conn
->viewer_session
) {
1742 ERR("Allocation viewer session");
1743 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1746 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1749 health_code_update();
1750 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1754 health_code_update();
1763 * live_relay_unknown_command: send -1 if received unknown command
1766 void live_relay_unknown_command(struct relay_connection
*conn
)
1768 struct lttcomm_relayd_generic_reply reply
;
1770 memset(&reply
, 0, sizeof(reply
));
1771 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1772 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1776 * Process the commands received on the control socket
1779 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1780 struct relay_connection
*conn
)
1788 msg_value
= be32toh(recv_hdr
->cmd
);
1791 * Make sure we've done the version check before any command other then a
1792 * new client connection.
1794 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1795 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1800 switch (msg_value
) {
1801 case LTTNG_VIEWER_CONNECT
:
1802 ret
= viewer_connect(conn
);
1804 case LTTNG_VIEWER_LIST_SESSIONS
:
1805 ret
= viewer_list_sessions(conn
);
1807 case LTTNG_VIEWER_ATTACH_SESSION
:
1808 ret
= viewer_attach_session(conn
);
1810 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1811 ret
= viewer_get_next_index(conn
);
1813 case LTTNG_VIEWER_GET_PACKET
:
1814 ret
= viewer_get_packet(conn
);
1816 case LTTNG_VIEWER_GET_METADATA
:
1817 ret
= viewer_get_metadata(conn
);
1819 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1820 ret
= viewer_get_new_streams(conn
);
1822 case LTTNG_VIEWER_CREATE_SESSION
:
1823 ret
= viewer_create_session(conn
);
1826 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1827 live_relay_unknown_command(conn
);
1837 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1843 (void) lttng_poll_del(events
, pollfd
);
1845 ret
= close(pollfd
);
1847 ERR("Closing pollfd %d", pollfd
);
1852 * Delete and destroy a connection.
1854 * RCU read side lock MUST be acquired.
1856 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1857 struct relay_connection
*conn
)
1859 struct relay_session
*session
, *tmp_session
;
1861 assert(relay_connections_ht
);
1864 connection_delete(relay_connections_ht
, conn
);
1866 if (!conn
->viewer_session
) {
1871 cds_list_for_each_entry_safe(session
, tmp_session
,
1872 &conn
->viewer_session
->sessions_head
,
1873 viewer_session_list
) {
1874 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1875 cleanup_session(conn
, session
);
1880 connection_destroy(conn
);
1884 * This thread does the actual work
1887 void *thread_worker(void *data
)
1891 struct relay_connection
*conn
;
1892 struct lttng_poll_event events
;
1893 struct lttng_ht
*relay_connections_ht
;
1894 struct lttng_ht_iter iter
;
1895 struct lttng_viewer_cmd recv_hdr
;
1896 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1897 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1899 DBG("[thread] Live viewer relay worker started");
1901 rcu_register_thread();
1903 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1905 if (testpoint(relayd_thread_live_worker
)) {
1906 goto error_testpoint
;
1909 /* table of connections indexed on socket */
1910 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1911 if (!relay_connections_ht
) {
1912 goto relay_connections_ht_error
;
1915 ret
= create_thread_poll_set(&events
, 2);
1917 goto error_poll_create
;
1920 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1929 health_code_update();
1931 /* Infinite blocking call, waiting for transmission */
1932 DBG3("Relayd live viewer worker thread polling...");
1933 health_poll_entry();
1934 ret
= lttng_poll_wait(&events
, -1);
1938 * Restart interrupted system call.
1940 if (errno
== EINTR
) {
1949 * Process control. The control connection is prioritised so we don't
1950 * starve it with high throughput tracing data on the data
1953 for (i
= 0; i
< nb_fd
; i
++) {
1954 /* Fetch once the poll data */
1955 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1956 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1958 health_code_update();
1961 /* No activity for this FD (poll implementation). */
1965 /* Thread quit pipe has been closed. Killing thread. */
1966 ret
= check_thread_quit_pipe(pollfd
, revents
);
1972 /* Inspect the relay conn pipe for new connection */
1973 if (pollfd
== live_conn_pipe
[0]) {
1974 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1975 ERR("Relay live pipe error");
1977 } else if (revents
& LPOLLIN
) {
1978 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1982 conn
->sessions_ht
= sessions_ht
;
1983 connection_init(conn
);
1984 lttng_poll_add(&events
, conn
->sock
->fd
,
1985 LPOLLIN
| LPOLLRDHUP
);
1987 lttng_ht_add_unique_ulong(relay_connections_ht
,
1990 DBG("Connection socket %d added", conn
->sock
->fd
);
1994 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1995 /* If not found, there is a synchronization issue. */
1998 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1999 cleanup_connection_pollfd(&events
, pollfd
);
2000 destroy_connection(relay_connections_ht
, conn
);
2001 } else if (revents
& LPOLLIN
) {
2002 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2003 sizeof(recv_hdr
), 0);
2005 /* Connection closed */
2006 cleanup_connection_pollfd(&events
, pollfd
);
2007 destroy_connection(relay_connections_ht
, conn
);
2008 DBG("Viewer control conn closed with %d", pollfd
);
2010 ret
= process_control(&recv_hdr
, conn
);
2012 /* Clear the session on error. */
2013 cleanup_connection_pollfd(&events
, pollfd
);
2014 destroy_connection(relay_connections_ht
, conn
);
2015 DBG("Viewer connection closed with %d", pollfd
);
2026 lttng_poll_clean(&events
);
2028 /* Cleanup reamaining connection object. */
2030 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2032 health_code_update();
2033 destroy_connection(relay_connections_ht
, conn
);
2037 lttng_ht_destroy(relay_connections_ht
);
2038 relay_connections_ht_error
:
2039 /* Close relay conn pipes */
2040 utils_close_pipe(live_conn_pipe
);
2042 DBG("Viewer worker thread exited with error");
2044 DBG("Viewer worker thread cleanup complete");
2048 ERR("Health error occurred in %s", __func__
);
2050 health_unregister(health_relayd
);
2052 rcu_unregister_thread();
2057 * Create the relay command pipe to wake thread_manage_apps.
2058 * Closed in cleanup().
2060 static int create_conn_pipe(void)
2064 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2069 void live_stop_threads(void)
2076 ret
= pthread_join(live_listener_thread
, &status
);
2078 PERROR("pthread_join live listener");
2079 goto error
; /* join error, exit without cleanup */
2082 ret
= pthread_join(live_worker_thread
, &status
);
2084 PERROR("pthread_join live worker");
2085 goto error
; /* join error, exit without cleanup */
2088 ret
= pthread_join(live_dispatcher_thread
, &status
);
2090 PERROR("pthread_join live dispatcher");
2091 goto error
; /* join error, exit without cleanup */
2103 int live_start_threads(struct lttng_uri
*uri
,
2104 struct relay_local_data
*relay_ctx
)
2113 /* Check if daemon is UID = 0 */
2114 is_root
= !getuid();
2117 if (live_uri
->port
< 1024) {
2118 ERR("Need to be root to use ports < 1024");
2124 /* Setup the thread apps communication pipe. */
2125 if ((ret
= create_conn_pipe()) < 0) {
2129 /* Init relay command queue. */
2130 cds_wfq_init(&viewer_conn_queue
.queue
);
2132 /* Set up max poll set size */
2133 lttng_poll_set_max_size();
2135 /* Setup the dispatcher thread */
2136 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2137 thread_dispatcher
, (void *) NULL
);
2139 PERROR("pthread_create viewer dispatcher");
2140 goto exit_dispatcher
;
2143 /* Setup the worker thread */
2144 ret
= pthread_create(&live_worker_thread
, NULL
,
2145 thread_worker
, relay_ctx
);
2147 PERROR("pthread_create viewer worker");
2151 /* Setup the listener thread */
2152 ret
= pthread_create(&live_listener_thread
, NULL
,
2153 thread_listener
, (void *) NULL
);
2155 PERROR("pthread_create viewer listener");
2163 ret
= pthread_join(live_listener_thread
, &status
);
2165 PERROR("pthread_join live listener");
2166 goto error
; /* join error, exit without cleanup */
2170 ret
= pthread_join(live_worker_thread
, &status
);
2172 PERROR("pthread_join live worker");
2173 goto error
; /* join error, exit without cleanup */
2177 ret
= pthread_join(live_dispatcher_thread
, &status
);
2179 PERROR("pthread_join live dispatcher");
2180 goto error
; /* join error, exit without cleanup */