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
);
517 /* Thread quit pipe has been closed. Killing thread. */
518 ret
= check_thread_quit_pipe(pollfd
, revents
);
524 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
525 ERR("socket poll error");
527 } else if (revents
& LPOLLIN
) {
529 * Get allocated in this thread, enqueued to a global queue,
530 * dequeued and freed in the worker thread.
533 struct relay_connection
*new_conn
;
534 struct lttcomm_sock
*newsock
;
536 new_conn
= connection_create();
541 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
543 PERROR("accepting control sock");
544 connection_free(new_conn
);
547 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
549 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
552 PERROR("setsockopt inet");
553 lttcomm_destroy_sock(newsock
);
554 connection_free(new_conn
);
557 new_conn
->sock
= newsock
;
559 /* Enqueue request for the dispatcher thread. */
560 cds_wfq_enqueue(&viewer_conn_queue
.queue
, &new_conn
->qnode
);
563 * Wake the dispatch queue futex. Implicit memory barrier with
564 * the exchange in cds_wfq_enqueue.
566 futex_nto1_wake(&viewer_conn_queue
.futex
);
575 lttng_poll_clean(&events
);
577 if (live_control_sock
->fd
>= 0) {
578 ret
= live_control_sock
->ops
->close(live_control_sock
);
583 lttcomm_destroy_sock(live_control_sock
);
587 DBG("Live viewer listener thread exited with error");
589 health_unregister(health_relayd
);
590 DBG("Live viewer listener thread cleanup complete");
596 * This thread manages the dispatching of the requests to worker threads
599 void *thread_dispatcher(void *data
)
603 struct cds_wfq_node
*node
;
604 struct relay_connection
*conn
= NULL
;
606 DBG("[thread] Live viewer relay dispatcher started");
608 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
610 if (testpoint(relayd_thread_live_dispatcher
)) {
611 goto error_testpoint
;
614 health_code_update();
616 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
617 health_code_update();
619 /* Atomically prepare the queue futex */
620 futex_nto1_prepare(&viewer_conn_queue
.futex
);
623 health_code_update();
625 /* Dequeue commands */
626 node
= cds_wfq_dequeue_blocking(&viewer_conn_queue
.queue
);
628 DBG("Woken up but nothing in the live-viewer "
629 "relay command queue");
630 /* Continue thread execution */
633 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
634 DBG("Dispatching viewer request waiting on sock %d",
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 :)
642 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
644 PERROR("write conn pipe");
645 connection_destroy(conn
);
648 } while (node
!= NULL
);
650 /* Futex wait on queue. Blocking call on futex() */
652 futex_nto1_wait(&viewer_conn_queue
.futex
);
656 /* Normal exit, no error */
663 ERR("Health error occurred in %s", __func__
);
665 health_unregister(health_relayd
);
666 DBG("Live viewer dispatch thread dying");
672 * Establish connection with the viewer and check the versions.
674 * Return 0 on success or else negative value.
677 int viewer_connect(struct relay_connection
*conn
)
680 struct lttng_viewer_connect reply
, msg
;
684 conn
->version_check_done
= 1;
686 health_code_update();
688 DBG("Viewer is establishing a connection to the relayd.");
690 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
695 health_code_update();
697 memset(&reply
, 0, sizeof(reply
));
698 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
699 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
701 /* Major versions must be the same */
702 if (reply
.major
!= be32toh(msg
.major
)) {
703 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
704 reply
.major
, be32toh(msg
.major
));
709 conn
->major
= reply
.major
;
710 /* We adapt to the lowest compatible version */
711 if (reply
.minor
<= be32toh(msg
.minor
)) {
712 conn
->minor
= reply
.minor
;
714 conn
->minor
= be32toh(msg
.minor
);
717 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
718 conn
->type
= RELAY_VIEWER_COMMAND
;
719 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
720 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
722 ERR("Unknown connection type : %u", be32toh(msg
.type
));
727 reply
.major
= htobe32(reply
.major
);
728 reply
.minor
= htobe32(reply
.minor
);
729 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
730 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
733 health_code_update();
735 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
740 health_code_update();
742 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
750 * Send the viewer the list of current sessions.
752 * Return 0 on success or else a negative value.
755 int viewer_list_sessions(struct relay_connection
*conn
)
758 struct lttng_viewer_list_sessions session_list
;
760 long approx_before
, approx_after
;
761 struct lttng_ht_iter iter
;
762 struct lttng_viewer_session send_session
;
763 struct relay_session
*session
;
765 DBG("List sessions received");
768 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
770 session_list
.sessions_count
= htobe32(count
);
772 health_code_update();
774 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
779 health_code_update();
781 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
783 health_code_update();
785 strncpy(send_session
.session_name
, session
->session_name
,
786 sizeof(send_session
.session_name
));
787 strncpy(send_session
.hostname
, session
->hostname
,
788 sizeof(send_session
.hostname
));
789 send_session
.id
= htobe64(session
->id
);
790 send_session
.live_timer
= htobe32(session
->live_timer
);
791 send_session
.clients
= htobe32(session
->viewer_refcount
);
792 send_session
.streams
= htobe32(session
->stream_count
);
794 health_code_update();
796 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
801 health_code_update();
815 * Check if a connection is attached to a session.
816 * Return 1 if attached, 0 if not attached, a negative value on error.
819 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
821 struct relay_session
*session
;
824 if (!conn
->viewer_session
) {
827 cds_list_for_each_entry(session
,
828 &conn
->viewer_session
->sessions_head
,
829 viewer_session_list
) {
830 if (session
->id
== session_id
) {
841 * Delete all streams for a specific session ID.
843 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
845 struct relay_viewer_stream
*stream
;
846 struct lttng_ht_iter iter
;
851 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
853 struct ctf_trace
*ctf_trace
;
855 health_code_update();
856 if (stream
->session_id
!= session
->id
) {
860 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
864 viewer_stream_delete(stream
);
866 if (stream
->metadata_flag
) {
867 ctf_trace
->metadata_sent
= 0;
868 ctf_trace
->viewer_metadata_stream
= NULL
;
871 viewer_stream_destroy(ctf_trace
, stream
);
876 static void try_destroy_streams(struct relay_session
*session
)
878 struct ctf_trace
*ctf_trace
;
879 struct lttng_ht_iter iter
;
883 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
885 /* Attempt to destroy the ctf trace of that session. */
886 ctf_trace_try_destroy(session
, ctf_trace
);
893 static void cleanup_session(struct relay_connection
*conn
,
894 struct relay_session
*session
)
897 * Very important that this is done before destroying the session so we
898 * can put back every viewer stream reference from the ctf_trace.
900 destroy_viewer_streams_by_session(session
);
901 try_destroy_streams(session
);
902 cds_list_del(&session
->viewer_session_list
);
903 session_viewer_try_destroy(conn
->sessions_ht
, session
);
907 * Send the viewer the list of current sessions.
910 int viewer_get_new_streams(struct relay_connection
*conn
)
912 int ret
, send_streams
= 0;
913 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
914 struct lttng_viewer_new_streams_request request
;
915 struct lttng_viewer_new_streams_response response
;
916 struct relay_session
*session
;
921 DBG("Get new streams received");
923 health_code_update();
925 /* Receive the request from the connected client. */
926 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
930 session_id
= be64toh(request
.session_id
);
932 health_code_update();
934 memset(&response
, 0, sizeof(response
));
937 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
939 DBG("Relay session %" PRIu64
" not found", session_id
);
940 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
944 if (!session_attached(conn
, session_id
)) {
946 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
951 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
953 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
958 /* Only send back the newly created streams with the unsent ones. */
959 nb_streams
= nb_created
+ nb_unsent
;
960 response
.streams_count
= htobe32(nb_streams
);
963 * If the session is closed and we have no new streams to send,
964 * it means that the viewer has already received the whole trace
965 * for this session and should now close it.
967 if (nb_streams
== 0 && session
->close_flag
) {
969 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
971 * Remove the session from the attached list of the connection
972 * and try to destroy it.
974 cds_list_del(&session
->viewer_session_list
);
975 cleanup_session(conn
, session
);
980 health_code_update();
981 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
985 health_code_update();
988 * Unknown or empty session, just return gracefully, the viewer knows what
991 if (!send_streams
|| !nb_streams
) {
997 * Send stream and *DON'T* ignore the sent flag so every viewer streams
998 * that were not sent from that point will be sent to the viewer.
1000 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1012 * Send the viewer the list of current sessions.
1015 int viewer_attach_session(struct relay_connection
*conn
)
1017 int send_streams
= 0;
1019 uint32_t nb_streams
= 0;
1020 enum lttng_viewer_seek seek_type
;
1021 struct lttng_viewer_attach_session_request request
;
1022 struct lttng_viewer_attach_session_response response
;
1023 struct relay_session
*session
;
1027 health_code_update();
1029 /* Receive the request from the connected client. */
1030 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1035 health_code_update();
1037 memset(&response
, 0, sizeof(response
));
1039 if (!conn
->viewer_session
) {
1040 DBG("Client trying to attach before creating a live viewer session");
1041 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1046 session
= session_find_by_id(conn
->sessions_ht
,
1047 be64toh(request
.session_id
));
1049 DBG("Relay session %" PRIu64
" not found",
1050 be64toh(request
.session_id
));
1051 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1054 session_viewer_attach(session
);
1055 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1057 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1058 DBG("Already a viewer attached");
1059 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1060 session_viewer_detach(session
);
1062 } else if (session
->live_timer
== 0) {
1063 DBG("Not live session");
1064 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1068 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1069 cds_list_add(&session
->viewer_session_list
,
1070 &conn
->viewer_session
->sessions_head
);
1073 switch (be32toh(request
.seek
)) {
1074 case LTTNG_VIEWER_SEEK_BEGINNING
:
1075 case LTTNG_VIEWER_SEEK_LAST
:
1076 seek_type
= be32toh(request
.seek
);
1079 ERR("Wrong seek parameter");
1080 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1085 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1089 response
.streams_count
= htobe32(nb_streams
);
1092 health_code_update();
1093 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1097 health_code_update();
1100 * Unknown or empty session, just return gracefully, the viewer knows what
1103 if (!send_streams
|| !nb_streams
) {
1108 /* Send stream and ignore the sent flag. */
1109 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1121 * Open the index file if needed for the given vstream.
1123 * If an index file is successfully opened, the index_read_fd of the stream is
1126 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1128 static int try_open_index(struct relay_viewer_stream
*vstream
,
1129 struct relay_stream
*rstream
)
1136 if (vstream
->index_read_fd
>= 0) {
1141 * First time, we open the index file and at least one index is ready. The
1142 * race between the read and write of the total_index_received is
1143 * acceptable here since the client will be notified to simply come back
1144 * and get the next index.
1146 if (rstream
->total_index_received
<= 0) {
1150 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1151 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1153 vstream
->index_read_fd
= ret
;
1163 * Check the status of the index for the given stream. This function updates
1164 * the index structure if needed and can destroy the vstream also for the HUP
1167 * Return 0 means that we can proceed with the index. A value of 1 means that
1168 * the index has been updated and is ready to be send to the client. A negative
1169 * value indicates an error that can't be handled.
1171 static int check_index_status(struct relay_viewer_stream
*vstream
,
1172 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1173 struct lttng_viewer_index
*index
)
1182 if (!rstream
->close_flag
) {
1183 /* Rotate on abort (overwrite). */
1184 if (vstream
->abort_flag
) {
1185 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1186 vstream
->stream_handle
);
1187 ret
= viewer_stream_rotate(vstream
, rstream
);
1190 } else if (ret
== 1) {
1192 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1195 /* ret == 0 means successful so we continue. */
1198 /* Check if we are in the same trace file at this point. */
1199 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1200 if (rstream
->beacon_ts_end
!= -1ULL &&
1201 vstream
->last_sent_index
== rstream
->total_index_received
) {
1203 * We've received a synchronization beacon and the last index
1204 * available has been sent, the index for now is inactive.
1206 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1207 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1209 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1210 && !vstream
->close_write_flag
) {
1212 * Reader and writer are working in the same tracefile, so we care
1213 * about the number of index received and sent. Otherwise, we read
1216 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1220 /* Nothing to do with the index, continue with it. */
1222 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1223 vstream
->total_index_received
== vstream
->last_sent_index
) {
1224 /* Last index sent and current tracefile closed in write */
1225 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1228 vstream
->close_write_flag
= 1;
1236 viewer_stream_delete(vstream
);
1237 viewer_stream_destroy(trace
, vstream
);
1243 * Send the next index for a stream.
1245 * Return 0 on success or else a negative value.
1248 int viewer_get_next_index(struct relay_connection
*conn
)
1252 struct lttng_viewer_get_next_index request_index
;
1253 struct lttng_viewer_index viewer_index
;
1254 struct ctf_packet_index packet_index
;
1255 struct relay_viewer_stream
*vstream
;
1256 struct relay_stream
*rstream
;
1257 struct ctf_trace
*ctf_trace
;
1258 struct relay_session
*session
;
1262 DBG("Viewer get next index");
1264 health_code_update();
1266 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1270 health_code_update();
1273 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1279 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1285 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1288 memset(&viewer_index
, 0, sizeof(viewer_index
));
1291 * The viewer should not ask for index on metadata stream.
1293 if (vstream
->metadata_flag
) {
1294 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1298 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1301 /* Try to open an index if one is needed for that stream. */
1302 ret
= try_open_index(vstream
, rstream
);
1304 if (ret
== -ENOENT
) {
1306 * The index is created only when the first data packet arrives, it
1307 * might not be ready at the beginning of the session
1309 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1311 /* Unhandled error. */
1312 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1317 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1318 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1319 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1322 } else if (ret
== 1) {
1324 * This means the viewer index data structure has been populated by the
1325 * check call thus we now send back the reply to the client.
1329 /* At this point, ret MUST be 0 thus we continue with the get. */
1332 if (!ctf_trace
->metadata_received
||
1333 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1334 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1337 ret
= check_new_streams(conn
);
1340 } else if (ret
== 1) {
1341 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1344 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1345 pthread_mutex_lock(&vstream
->overwrite_lock
);
1346 if (vstream
->abort_flag
) {
1347 /* The file is being overwritten by the writer, we cannot use it. */
1348 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1349 ret
= viewer_stream_rotate(vstream
, rstream
);
1350 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1353 } else if (ret
== 1) {
1354 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1355 viewer_stream_delete(vstream
);
1356 viewer_stream_destroy(ctf_trace
, vstream
);
1358 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1363 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1364 sizeof(packet_index
));
1365 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1366 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1368 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1369 viewer_stream_delete(vstream
);
1370 viewer_stream_destroy(ctf_trace
, vstream
);
1372 } else if (read_ret
< sizeof(packet_index
)) {
1373 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1374 if (vstream
->close_write_flag
) {
1375 ret
= viewer_stream_rotate(vstream
, rstream
);
1377 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1379 } else if (ret
== 1) {
1380 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1381 viewer_stream_delete(vstream
);
1382 viewer_stream_destroy(ctf_trace
, vstream
);
1384 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1387 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1388 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1390 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1393 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1394 vstream
->last_sent_index
++;
1398 * Indexes are stored in big endian, no need to switch before sending.
1400 viewer_index
.offset
= packet_index
.offset
;
1401 viewer_index
.packet_size
= packet_index
.packet_size
;
1402 viewer_index
.content_size
= packet_index
.content_size
;
1403 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1404 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1405 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1406 viewer_index
.stream_id
= packet_index
.stream_id
;
1409 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1410 health_code_update();
1412 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1416 health_code_update();
1418 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1419 vstream
->last_sent_index
, vstream
->stream_handle
);
1429 * Send the next index for a stream
1431 * Return 0 on success or else a negative value.
1434 int viewer_get_packet(struct relay_connection
*conn
)
1436 int ret
, send_data
= 0;
1440 struct lttng_viewer_get_packet get_packet_info
;
1441 struct lttng_viewer_trace_packet reply
;
1442 struct relay_viewer_stream
*stream
;
1443 struct relay_session
*session
;
1444 struct ctf_trace
*ctf_trace
;
1448 DBG2("Relay get data packet");
1450 health_code_update();
1452 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1456 health_code_update();
1458 /* From this point on, the error label can be reached. */
1459 memset(&reply
, 0, sizeof(reply
));
1462 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1467 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1473 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1478 * First time we read this stream, we need open the tracefile, we should
1479 * only arrive here if an index has already been sent to the viewer, so the
1480 * tracefile must exist, if it does not it is a fatal error.
1482 if (stream
->read_fd
< 0) {
1483 char fullpath
[PATH_MAX
];
1485 if (stream
->tracefile_count
> 0) {
1486 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1487 stream
->channel_name
,
1488 stream
->tracefile_count_current
);
1490 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1491 stream
->channel_name
);
1496 ret
= open(fullpath
, O_RDONLY
);
1498 PERROR("Relay opening trace file");
1501 stream
->read_fd
= ret
;
1504 if (!ctf_trace
->metadata_received
||
1505 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1506 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1507 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1511 ret
= check_new_streams(conn
);
1514 } else if (ret
== 1) {
1515 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1516 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1520 len
= be32toh(get_packet_info
.len
);
1521 data
= zmalloc(len
);
1523 PERROR("relay data zmalloc");
1527 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1530 * If the read fd was closed by the streaming side, the
1531 * abort_flag will be set to 1, otherwise it is an error.
1533 if (stream
->abort_flag
== 0) {
1537 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1540 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1541 if (read_len
< len
) {
1543 * If the read fd was closed by the streaming side, the
1544 * abort_flag will be set to 1, otherwise it is an error.
1546 if (stream
->abort_flag
== 0) {
1547 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1549 be64toh(get_packet_info
.offset
));
1552 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1556 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1557 reply
.len
= htobe32(len
);
1562 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1565 reply
.flags
= htobe32(reply
.flags
);
1567 health_code_update();
1569 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1573 health_code_update();
1576 health_code_update();
1577 ret
= send_response(conn
->sock
, data
, len
);
1581 health_code_update();
1584 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1585 be64toh(get_packet_info
.stream_id
));
1596 * Send the session's metadata
1598 * Return 0 on success else a negative value.
1601 int viewer_get_metadata(struct relay_connection
*conn
)
1607 struct lttng_viewer_get_metadata request
;
1608 struct lttng_viewer_metadata_packet reply
;
1609 struct relay_viewer_stream
*stream
;
1610 struct ctf_trace
*ctf_trace
;
1611 struct relay_session
*session
;
1615 DBG("Relay get metadata");
1617 health_code_update();
1619 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1623 health_code_update();
1625 memset(&reply
, 0, sizeof(reply
));
1628 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1629 if (!stream
|| !stream
->metadata_flag
) {
1630 ERR("Invalid metadata stream");
1634 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1640 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1643 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1645 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1647 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1651 /* first time, we open the metadata file */
1652 if (stream
->read_fd
< 0) {
1653 char fullpath
[PATH_MAX
];
1655 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1656 stream
->channel_name
);
1660 ret
= open(fullpath
, O_RDONLY
);
1662 PERROR("Relay opening metadata file");
1665 stream
->read_fd
= ret
;
1668 reply
.len
= htobe64(len
);
1669 data
= zmalloc(len
);
1671 PERROR("viewer metadata zmalloc");
1675 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1676 if (read_len
< len
) {
1677 PERROR("Relay reading metadata file");
1680 ctf_trace
->metadata_sent
+= read_len
;
1681 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1685 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1688 health_code_update();
1689 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1693 health_code_update();
1696 ret
= send_response(conn
->sock
, data
, len
);
1702 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1703 be64toh(request
.stream_id
));
1705 DBG("Metadata sent");
1715 * Create a viewer session.
1717 * Return 0 on success or else a negative value.
1720 int viewer_create_session(struct relay_connection
*conn
)
1723 struct lttng_viewer_create_session_response resp
;
1725 DBG("Viewer create session received");
1727 memset(&resp
, 0, sizeof(resp
));
1728 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1729 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1730 if (!conn
->viewer_session
) {
1731 ERR("Allocation viewer session");
1732 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1735 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1738 health_code_update();
1739 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1743 health_code_update();
1752 * live_relay_unknown_command: send -1 if received unknown command
1755 void live_relay_unknown_command(struct relay_connection
*conn
)
1757 struct lttcomm_relayd_generic_reply reply
;
1759 memset(&reply
, 0, sizeof(reply
));
1760 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1761 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1765 * Process the commands received on the control socket
1768 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1769 struct relay_connection
*conn
)
1777 msg_value
= be32toh(recv_hdr
->cmd
);
1780 * Make sure we've done the version check before any command other then a
1781 * new client connection.
1783 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1784 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1789 switch (msg_value
) {
1790 case LTTNG_VIEWER_CONNECT
:
1791 ret
= viewer_connect(conn
);
1793 case LTTNG_VIEWER_LIST_SESSIONS
:
1794 ret
= viewer_list_sessions(conn
);
1796 case LTTNG_VIEWER_ATTACH_SESSION
:
1797 ret
= viewer_attach_session(conn
);
1799 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1800 ret
= viewer_get_next_index(conn
);
1802 case LTTNG_VIEWER_GET_PACKET
:
1803 ret
= viewer_get_packet(conn
);
1805 case LTTNG_VIEWER_GET_METADATA
:
1806 ret
= viewer_get_metadata(conn
);
1808 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1809 ret
= viewer_get_new_streams(conn
);
1811 case LTTNG_VIEWER_CREATE_SESSION
:
1812 ret
= viewer_create_session(conn
);
1815 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1816 live_relay_unknown_command(conn
);
1826 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1832 (void) lttng_poll_del(events
, pollfd
);
1834 ret
= close(pollfd
);
1836 ERR("Closing pollfd %d", pollfd
);
1841 * Delete and destroy a connection.
1843 * RCU read side lock MUST be acquired.
1845 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1846 struct relay_connection
*conn
)
1848 struct relay_session
*session
, *tmp_session
;
1850 assert(relay_connections_ht
);
1853 connection_delete(relay_connections_ht
, conn
);
1855 if (!conn
->viewer_session
) {
1860 cds_list_for_each_entry_safe(session
, tmp_session
,
1861 &conn
->viewer_session
->sessions_head
,
1862 viewer_session_list
) {
1863 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1864 cleanup_session(conn
, session
);
1869 connection_destroy(conn
);
1873 * This thread does the actual work
1876 void *thread_worker(void *data
)
1880 struct relay_connection
*conn
;
1881 struct lttng_poll_event events
;
1882 struct lttng_ht
*relay_connections_ht
;
1883 struct lttng_ht_iter iter
;
1884 struct lttng_viewer_cmd recv_hdr
;
1885 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1886 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1888 DBG("[thread] Live viewer relay worker started");
1890 rcu_register_thread();
1892 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1894 if (testpoint(relayd_thread_live_worker
)) {
1895 goto error_testpoint
;
1898 /* table of connections indexed on socket */
1899 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1900 if (!relay_connections_ht
) {
1901 goto relay_connections_ht_error
;
1904 ret
= create_thread_poll_set(&events
, 2);
1906 goto error_poll_create
;
1909 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1918 health_code_update();
1920 /* Infinite blocking call, waiting for transmission */
1921 DBG3("Relayd live viewer worker thread polling...");
1922 health_poll_entry();
1923 ret
= lttng_poll_wait(&events
, -1);
1927 * Restart interrupted system call.
1929 if (errno
== EINTR
) {
1938 * Process control. The control connection is prioritised so we don't
1939 * starve it with high throughput tracing data on the data
1942 for (i
= 0; i
< nb_fd
; i
++) {
1943 /* Fetch once the poll data */
1944 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1945 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1947 health_code_update();
1949 /* Thread quit pipe has been closed. Killing thread. */
1950 ret
= check_thread_quit_pipe(pollfd
, revents
);
1956 /* Inspect the relay conn pipe for new connection */
1957 if (pollfd
== live_conn_pipe
[0]) {
1958 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1959 ERR("Relay live pipe error");
1961 } else if (revents
& LPOLLIN
) {
1962 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1966 conn
->sessions_ht
= sessions_ht
;
1967 connection_init(conn
);
1968 lttng_poll_add(&events
, conn
->sock
->fd
,
1969 LPOLLIN
| LPOLLRDHUP
);
1971 lttng_ht_add_unique_ulong(relay_connections_ht
,
1974 DBG("Connection socket %d added", conn
->sock
->fd
);
1978 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1979 /* If not found, there is a synchronization issue. */
1982 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1983 cleanup_connection_pollfd(&events
, pollfd
);
1984 destroy_connection(relay_connections_ht
, conn
);
1985 } else if (revents
& LPOLLIN
) {
1986 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1987 sizeof(recv_hdr
), 0);
1989 /* Connection closed */
1990 cleanup_connection_pollfd(&events
, pollfd
);
1991 destroy_connection(relay_connections_ht
, conn
);
1992 DBG("Viewer control conn closed with %d", pollfd
);
1994 ret
= process_control(&recv_hdr
, conn
);
1996 /* Clear the session on error. */
1997 cleanup_connection_pollfd(&events
, pollfd
);
1998 destroy_connection(relay_connections_ht
, conn
);
1999 DBG("Viewer connection closed with %d", pollfd
);
2010 lttng_poll_clean(&events
);
2012 /* Cleanup reamaining connection object. */
2014 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2016 health_code_update();
2017 destroy_connection(relay_connections_ht
, conn
);
2021 lttng_ht_destroy(relay_connections_ht
);
2022 relay_connections_ht_error
:
2023 /* Close relay conn pipes */
2024 utils_close_pipe(live_conn_pipe
);
2026 DBG("Viewer worker thread exited with error");
2028 DBG("Viewer worker thread cleanup complete");
2032 ERR("Health error occurred in %s", __func__
);
2034 health_unregister(health_relayd
);
2036 rcu_unregister_thread();
2041 * Create the relay command pipe to wake thread_manage_apps.
2042 * Closed in cleanup().
2044 static int create_conn_pipe(void)
2048 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2053 void live_stop_threads(void)
2060 ret
= pthread_join(live_listener_thread
, &status
);
2062 PERROR("pthread_join live listener");
2063 goto error
; /* join error, exit without cleanup */
2066 ret
= pthread_join(live_worker_thread
, &status
);
2068 PERROR("pthread_join live worker");
2069 goto error
; /* join error, exit without cleanup */
2072 ret
= pthread_join(live_dispatcher_thread
, &status
);
2074 PERROR("pthread_join live dispatcher");
2075 goto error
; /* join error, exit without cleanup */
2087 int live_start_threads(struct lttng_uri
*uri
,
2088 struct relay_local_data
*relay_ctx
)
2097 /* Check if daemon is UID = 0 */
2098 is_root
= !getuid();
2101 if (live_uri
->port
< 1024) {
2102 ERR("Need to be root to use ports < 1024");
2108 /* Setup the thread apps communication pipe. */
2109 if ((ret
= create_conn_pipe()) < 0) {
2113 /* Init relay command queue. */
2114 cds_wfq_init(&viewer_conn_queue
.queue
);
2116 /* Set up max poll set size */
2117 lttng_poll_set_max_size();
2119 /* Setup the dispatcher thread */
2120 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2121 thread_dispatcher
, (void *) NULL
);
2123 PERROR("pthread_create viewer dispatcher");
2124 goto exit_dispatcher
;
2127 /* Setup the worker thread */
2128 ret
= pthread_create(&live_worker_thread
, NULL
,
2129 thread_worker
, relay_ctx
);
2131 PERROR("pthread_create viewer worker");
2135 /* Setup the listener thread */
2136 ret
= pthread_create(&live_listener_thread
, NULL
,
2137 thread_listener
, (void *) NULL
);
2139 PERROR("pthread_create viewer listener");
2147 ret
= pthread_join(live_listener_thread
, &status
);
2149 PERROR("pthread_join live listener");
2150 goto error
; /* join error, exit without cleanup */
2154 ret
= pthread_join(live_worker_thread
, &status
);
2156 PERROR("pthread_join live worker");
2157 goto error
; /* join error, exit without cleanup */
2161 ret
= pthread_join(live_dispatcher_thread
, &status
);
2163 PERROR("pthread_join live dispatcher");
2164 goto error
; /* join error, exit without cleanup */