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/compat/endian.h>
47 #include <common/defaults.h>
48 #include <common/futex.h>
49 #include <common/index/index.h>
50 #include <common/sessiond-comm/sessiond-comm.h>
51 #include <common/sessiond-comm/inet.h>
52 #include <common/sessiond-comm/relayd.h>
53 #include <common/uri.h>
54 #include <common/utils.h>
58 #include "lttng-relayd.h"
60 #include "health-relayd.h"
61 #include "testpoint.h"
62 #include "viewer-stream.h"
65 #include "ctf-trace.h"
66 #include "connection.h"
68 static struct lttng_uri
*live_uri
;
71 * This pipe is used to inform the worker thread that a command is queued and
72 * ready to be processed.
74 static int live_conn_pipe
[2] = { -1, -1 };
76 /* Shared between threads */
77 static int live_dispatch_thread_exit
;
79 static pthread_t live_listener_thread
;
80 static pthread_t live_dispatcher_thread
;
81 static pthread_t live_worker_thread
;
84 * Relay command queue.
86 * The live_thread_listener and live_thread_dispatcher communicate with this
89 static struct relay_conn_queue viewer_conn_queue
;
91 static uint64_t last_relay_viewer_session_id
;
105 * Receive a request buffer using a given socket, destination allocated buffer
108 * Return the size of the received message or else a negative value on error
109 * with errno being set by recvmsg() syscall.
112 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
119 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
120 if (ret
< 0 || ret
!= size
) {
122 /* Orderly shutdown. Not necessary to print an error. */
123 DBG("Socket %d did an orderly shutdown", sock
->fd
);
125 ERR("Relay failed to receive request.");
134 * Send a response buffer using a given socket, source allocated buffer of
137 * Return the size of the sent message or else a negative value on error with
138 * errno being set by sendmsg() syscall.
141 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
148 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
150 ERR("Relayd failed to send response.");
157 * Atomically check if new streams got added in one of the sessions attached
158 * and reset the flag to 0.
160 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
164 int check_new_streams(struct relay_connection
*conn
)
166 struct relay_session
*session
;
167 unsigned long current_val
;
170 if (!conn
->viewer_session
) {
173 cds_list_for_each_entry(session
,
174 &conn
->viewer_session
->sessions_head
,
175 viewer_session_list
) {
176 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
188 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
189 * this function should ignore the sent flag or not.
191 * Return 0 on success or else a negative value.
194 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
195 struct relay_session
*session
, unsigned int ignore_sent_flag
)
198 struct lttng_viewer_stream send_stream
;
199 struct lttng_ht_iter iter
;
200 struct relay_viewer_stream
*vstream
;
206 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
208 struct ctf_trace
*ctf_trace
;
210 health_code_update();
212 /* Ignore if not the same session. */
213 if (vstream
->session_id
!= session
->id
||
214 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
218 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
222 send_stream
.id
= htobe64(vstream
->stream_handle
);
223 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
224 send_stream
.metadata_flag
= htobe32(vstream
->metadata_flag
);
225 strncpy(send_stream
.path_name
, vstream
->path_name
,
226 sizeof(send_stream
.path_name
));
227 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
228 sizeof(send_stream
.channel_name
));
230 DBG("Sending stream %" PRIu64
" to viewer", vstream
->stream_handle
);
231 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
235 vstream
->sent_flag
= 1;
246 * Create every viewer stream possible for the given session with the seek
247 * type. Three counters *can* be return which are in order the total amount of
248 * viewer stream of the session, the number of unsent stream and the number of
249 * stream created. Those counters can be NULL and thus will be ignored.
251 * Return 0 on success or else a negative value.
254 int make_viewer_streams(struct relay_session
*session
,
255 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
256 uint32_t *nb_created
)
259 struct lttng_ht_iter iter
;
260 struct ctf_trace
*ctf_trace
;
265 * This is to make sure we create viewer streams for a full received
266 * channel. For instance, if we have 8 streams for a channel that are
267 * concurrently being flagged ready, we can end up creating just a subset
268 * of the 8 streams (the ones that are flagged). This lock avoids this
271 pthread_mutex_lock(&session
->viewer_ready_lock
);
274 * Create viewer streams for relay streams that are ready to be used for a
275 * the given session id only.
278 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
280 struct relay_stream
*stream
;
282 health_code_update();
284 if (ctf_trace
->invalid_flag
) {
288 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
289 struct relay_viewer_stream
*vstream
;
291 if (!stream
->viewer_ready
) {
295 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
297 vstream
= viewer_stream_create(stream
, seek_t
, ctf_trace
);
302 /* Acquire reference to ctf_trace. */
303 ctf_trace_get_ref(ctf_trace
);
306 /* Update number of created stream counter. */
309 } else if (!vstream
->sent_flag
&& nb_unsent
) {
310 /* Update number of unsent stream counter. */
313 /* Update number of total stream counter. */
324 pthread_mutex_unlock(&session
->viewer_ready_lock
);
329 * Write to writable pipe used to notify a thread.
332 int notify_thread_pipe(int wpipe
)
336 ret
= lttng_write(wpipe
, "!", 1);
338 PERROR("write poll pipe");
345 * Stop all threads by closing the thread quit pipe.
348 void stop_threads(void)
352 /* Stopping all threads */
353 DBG("Terminating all live threads");
354 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
356 ERR("write error on thread quit pipe");
359 /* Dispatch thread */
360 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
361 futex_nto1_wake(&viewer_conn_queue
.futex
);
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
368 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
372 if (events
== NULL
|| size
== 0) {
377 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
383 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
395 * Check if the thread quit pipe was triggered.
397 * Return 1 if it was triggered else 0;
400 int check_thread_quit_pipe(int fd
, uint32_t events
)
402 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
410 * Create and init socket from uri.
413 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
416 struct lttcomm_sock
*sock
= NULL
;
418 sock
= lttcomm_alloc_sock_from_uri(uri
);
420 ERR("Allocating socket");
424 ret
= lttcomm_create_sock(sock
);
428 DBG("Listening on sock %d for live", sock
->fd
);
430 ret
= sock
->ops
->bind(sock
);
435 ret
= sock
->ops
->listen(sock
, -1);
445 lttcomm_destroy_sock(sock
);
451 * This thread manages the listening for new connections on the network
454 void *thread_listener(void *data
)
456 int i
, ret
, pollfd
, err
= -1;
457 uint32_t revents
, nb_fd
;
458 struct lttng_poll_event events
;
459 struct lttcomm_sock
*live_control_sock
;
461 DBG("[thread] Relay live listener started");
463 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
465 health_code_update();
467 live_control_sock
= init_socket(live_uri
);
468 if (!live_control_sock
) {
469 goto error_sock_control
;
472 /* Pass 2 as size here for the thread quit pipe and control sockets. */
473 ret
= create_thread_poll_set(&events
, 2);
475 goto error_create_poll
;
478 /* Add the control socket */
479 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
484 lttng_relay_notify_ready();
486 if (testpoint(relayd_thread_live_listener
)) {
487 goto error_testpoint
;
491 health_code_update();
493 DBG("Listener accepting live viewers connections");
497 ret
= lttng_poll_wait(&events
, -1);
501 * Restart interrupted system call.
503 if (errno
== EINTR
) {
510 DBG("Relay new viewer connection received");
511 for (i
= 0; i
< nb_fd
; i
++) {
512 health_code_update();
514 /* Fetch once the poll data */
515 revents
= LTTNG_POLL_GETEV(&events
, i
);
516 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
518 /* Thread quit pipe has been closed. Killing thread. */
519 ret
= check_thread_quit_pipe(pollfd
, revents
);
525 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
526 ERR("socket poll error");
528 } else if (revents
& LPOLLIN
) {
530 * Get allocated in this thread, enqueued to a global queue,
531 * dequeued and freed in the worker thread.
534 struct relay_connection
*new_conn
;
535 struct lttcomm_sock
*newsock
;
537 new_conn
= connection_create();
542 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
544 PERROR("accepting control sock");
545 connection_free(new_conn
);
548 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
550 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
553 PERROR("setsockopt inet");
554 lttcomm_destroy_sock(newsock
);
555 connection_free(new_conn
);
558 new_conn
->sock
= newsock
;
560 /* Enqueue request for the dispatcher thread. */
561 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
565 * Wake the dispatch queue futex. Implicit memory barrier with
566 * the exchange in cds_wfcq_enqueue.
568 futex_nto1_wake(&viewer_conn_queue
.futex
);
577 lttng_poll_clean(&events
);
579 if (live_control_sock
->fd
>= 0) {
580 ret
= live_control_sock
->ops
->close(live_control_sock
);
585 lttcomm_destroy_sock(live_control_sock
);
589 DBG("Live viewer listener thread exited with error");
591 health_unregister(health_relayd
);
592 DBG("Live viewer listener thread cleanup complete");
598 * This thread manages the dispatching of the requests to worker threads
601 void *thread_dispatcher(void *data
)
605 struct cds_wfcq_node
*node
;
606 struct relay_connection
*conn
= NULL
;
608 DBG("[thread] Live viewer relay dispatcher started");
610 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
612 if (testpoint(relayd_thread_live_dispatcher
)) {
613 goto error_testpoint
;
616 health_code_update();
618 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
619 health_code_update();
621 /* Atomically prepare the queue futex */
622 futex_nto1_prepare(&viewer_conn_queue
.futex
);
625 health_code_update();
627 /* Dequeue commands */
628 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
629 &viewer_conn_queue
.tail
);
631 DBG("Woken up but nothing in the live-viewer "
632 "relay command queue");
633 /* Continue thread execution */
636 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
637 DBG("Dispatching viewer request waiting on sock %d",
641 * Inform worker thread of the new request. This call is blocking
642 * so we can be assured that the data will be read at some point in
643 * time or wait to the end of the world :)
645 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
647 PERROR("write conn pipe");
648 connection_destroy(conn
);
651 } while (node
!= NULL
);
653 /* Futex wait on queue. Blocking call on futex() */
655 futex_nto1_wait(&viewer_conn_queue
.futex
);
659 /* Normal exit, no error */
666 ERR("Health error occurred in %s", __func__
);
668 health_unregister(health_relayd
);
669 DBG("Live viewer dispatch thread dying");
675 * Establish connection with the viewer and check the versions.
677 * Return 0 on success or else negative value.
680 int viewer_connect(struct relay_connection
*conn
)
683 struct lttng_viewer_connect reply
, msg
;
687 conn
->version_check_done
= 1;
689 health_code_update();
691 DBG("Viewer is establishing a connection to the relayd.");
693 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
698 health_code_update();
700 memset(&reply
, 0, sizeof(reply
));
701 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
702 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
704 /* Major versions must be the same */
705 if (reply
.major
!= be32toh(msg
.major
)) {
706 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
707 reply
.major
, be32toh(msg
.major
));
712 conn
->major
= reply
.major
;
713 /* We adapt to the lowest compatible version */
714 if (reply
.minor
<= be32toh(msg
.minor
)) {
715 conn
->minor
= reply
.minor
;
717 conn
->minor
= be32toh(msg
.minor
);
720 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
721 conn
->type
= RELAY_VIEWER_COMMAND
;
722 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
723 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
725 ERR("Unknown connection type : %u", be32toh(msg
.type
));
730 reply
.major
= htobe32(reply
.major
);
731 reply
.minor
= htobe32(reply
.minor
);
732 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
733 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
736 health_code_update();
738 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
743 health_code_update();
745 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
753 * Send the viewer the list of current sessions.
755 * Return 0 on success or else a negative value.
758 int viewer_list_sessions(struct relay_connection
*conn
)
761 struct lttng_viewer_list_sessions session_list
;
763 long approx_before
, approx_after
;
764 struct lttng_ht_iter iter
;
765 struct lttng_viewer_session send_session
;
766 struct relay_session
*session
;
768 DBG("List sessions received");
771 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
773 session_list
.sessions_count
= htobe32(count
);
775 health_code_update();
777 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
782 health_code_update();
784 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
786 health_code_update();
788 strncpy(send_session
.session_name
, session
->session_name
,
789 sizeof(send_session
.session_name
));
790 strncpy(send_session
.hostname
, session
->hostname
,
791 sizeof(send_session
.hostname
));
792 send_session
.id
= htobe64(session
->id
);
793 send_session
.live_timer
= htobe32(session
->live_timer
);
794 send_session
.clients
= htobe32(session
->viewer_refcount
);
795 send_session
.streams
= htobe32(session
->stream_count
);
797 health_code_update();
799 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
804 health_code_update();
818 * Check if a connection is attached to a session.
819 * Return 1 if attached, 0 if not attached, a negative value on error.
822 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
824 struct relay_session
*session
;
827 if (!conn
->viewer_session
) {
830 cds_list_for_each_entry(session
,
831 &conn
->viewer_session
->sessions_head
,
832 viewer_session_list
) {
833 if (session
->id
== session_id
) {
844 * Delete all streams for a specific session ID.
846 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
848 struct relay_viewer_stream
*stream
;
849 struct lttng_ht_iter iter
;
854 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
856 struct ctf_trace
*ctf_trace
;
858 health_code_update();
859 if (stream
->session_id
!= session
->id
) {
863 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
867 viewer_stream_delete(stream
);
869 if (stream
->metadata_flag
) {
870 ctf_trace
->metadata_sent
= 0;
871 ctf_trace
->viewer_metadata_stream
= NULL
;
874 viewer_stream_destroy(ctf_trace
, stream
);
879 static void try_destroy_streams(struct relay_session
*session
)
881 struct ctf_trace
*ctf_trace
;
882 struct lttng_ht_iter iter
;
886 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
888 /* Attempt to destroy the ctf trace of that session. */
889 ctf_trace_try_destroy(session
, ctf_trace
);
896 static void cleanup_session(struct relay_connection
*conn
,
897 struct relay_session
*session
)
900 * Very important that this is done before destroying the session so we
901 * can put back every viewer stream reference from the ctf_trace.
903 destroy_viewer_streams_by_session(session
);
904 try_destroy_streams(session
);
905 cds_list_del(&session
->viewer_session_list
);
906 session_viewer_try_destroy(conn
->sessions_ht
, session
);
910 * Send the viewer the list of current sessions.
913 int viewer_get_new_streams(struct relay_connection
*conn
)
915 int ret
, send_streams
= 0;
916 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
917 struct lttng_viewer_new_streams_request request
;
918 struct lttng_viewer_new_streams_response response
;
919 struct relay_session
*session
;
924 DBG("Get new streams received");
926 health_code_update();
928 /* Receive the request from the connected client. */
929 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
933 session_id
= be64toh(request
.session_id
);
935 health_code_update();
937 memset(&response
, 0, sizeof(response
));
940 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
942 DBG("Relay session %" PRIu64
" not found", session_id
);
943 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
947 if (!session_attached(conn
, session_id
)) {
949 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
954 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
956 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
961 /* Only send back the newly created streams with the unsent ones. */
962 nb_streams
= nb_created
+ nb_unsent
;
963 response
.streams_count
= htobe32(nb_streams
);
966 * If the session is closed and we have no new streams to send,
967 * it means that the viewer has already received the whole trace
968 * for this session and should now close it.
970 if (nb_streams
== 0 && session
->close_flag
) {
972 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
974 * Remove the session from the attached list of the connection
975 * and try to destroy it.
977 cds_list_del(&session
->viewer_session_list
);
978 cleanup_session(conn
, session
);
983 health_code_update();
984 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
988 health_code_update();
991 * Unknown or empty session, just return gracefully, the viewer knows what
994 if (!send_streams
|| !nb_streams
) {
1000 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1001 * that were not sent from that point will be sent to the viewer.
1003 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1015 * Send the viewer the list of current sessions.
1018 int viewer_attach_session(struct relay_connection
*conn
)
1020 int send_streams
= 0;
1022 uint32_t nb_streams
= 0;
1023 enum lttng_viewer_seek seek_type
;
1024 struct lttng_viewer_attach_session_request request
;
1025 struct lttng_viewer_attach_session_response response
;
1026 struct relay_session
*session
;
1030 health_code_update();
1032 /* Receive the request from the connected client. */
1033 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1038 health_code_update();
1040 memset(&response
, 0, sizeof(response
));
1042 if (!conn
->viewer_session
) {
1043 DBG("Client trying to attach before creating a live viewer session");
1044 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1049 session
= session_find_by_id(conn
->sessions_ht
,
1050 be64toh(request
.session_id
));
1052 DBG("Relay session %" PRIu64
" not found",
1053 be64toh(request
.session_id
));
1054 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1057 session_viewer_attach(session
);
1058 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1060 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1061 DBG("Already a viewer attached");
1062 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1063 session_viewer_detach(session
);
1065 } else if (session
->live_timer
== 0) {
1066 DBG("Not live session");
1067 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1071 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1072 cds_list_add(&session
->viewer_session_list
,
1073 &conn
->viewer_session
->sessions_head
);
1076 switch (be32toh(request
.seek
)) {
1077 case LTTNG_VIEWER_SEEK_BEGINNING
:
1078 case LTTNG_VIEWER_SEEK_LAST
:
1079 seek_type
= be32toh(request
.seek
);
1082 ERR("Wrong seek parameter");
1083 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1088 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1092 response
.streams_count
= htobe32(nb_streams
);
1095 health_code_update();
1096 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1100 health_code_update();
1103 * Unknown or empty session, just return gracefully, the viewer knows what
1106 if (!send_streams
|| !nb_streams
) {
1111 /* Send stream and ignore the sent flag. */
1112 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1124 * Open the index file if needed for the given vstream.
1126 * If an index file is successfully opened, the index_read_fd of the stream is
1129 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1131 static int try_open_index(struct relay_viewer_stream
*vstream
,
1132 struct relay_stream
*rstream
)
1139 if (vstream
->index_read_fd
>= 0) {
1144 * First time, we open the index file and at least one index is ready. The
1145 * race between the read and write of the total_index_received is
1146 * acceptable here since the client will be notified to simply come back
1147 * and get the next index.
1149 if (rstream
->total_index_received
<= 0) {
1153 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1154 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1156 vstream
->index_read_fd
= ret
;
1166 * Check the status of the index for the given stream. This function updates
1167 * the index structure if needed and can destroy the vstream also for the HUP
1170 * Return 0 means that we can proceed with the index. A value of 1 means that
1171 * the index has been updated and is ready to be send to the client. A negative
1172 * value indicates an error that can't be handled.
1174 static int check_index_status(struct relay_viewer_stream
*vstream
,
1175 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1176 struct lttng_viewer_index
*index
)
1185 if (!rstream
->close_flag
) {
1186 /* Rotate on abort (overwrite). */
1187 if (vstream
->abort_flag
) {
1188 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1189 vstream
->stream_handle
);
1190 ret
= viewer_stream_rotate(vstream
, rstream
);
1193 } else if (ret
== 1) {
1195 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1198 /* ret == 0 means successful so we continue. */
1201 /* Check if we are in the same trace file at this point. */
1202 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1203 if (rstream
->beacon_ts_end
!= -1ULL &&
1204 vstream
->last_sent_index
== rstream
->total_index_received
) {
1206 * We've received a synchronization beacon and the last index
1207 * available has been sent, the index for now is inactive.
1209 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1210 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1211 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1213 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1214 && !vstream
->close_write_flag
) {
1216 * Reader and writer are working in the same tracefile, so we care
1217 * about the number of index received and sent. Otherwise, we read
1220 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1224 /* Nothing to do with the index, continue with it. */
1226 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1227 vstream
->total_index_received
== vstream
->last_sent_index
) {
1228 /* Last index sent and current tracefile closed in write */
1229 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1232 vstream
->close_write_flag
= 1;
1240 viewer_stream_delete(vstream
);
1241 viewer_stream_destroy(trace
, vstream
);
1247 * Send the next index for a stream.
1249 * Return 0 on success or else a negative value.
1252 int viewer_get_next_index(struct relay_connection
*conn
)
1256 struct lttng_viewer_get_next_index request_index
;
1257 struct lttng_viewer_index viewer_index
;
1258 struct ctf_packet_index packet_index
;
1259 struct relay_viewer_stream
*vstream
;
1260 struct relay_stream
*rstream
;
1261 struct ctf_trace
*ctf_trace
;
1262 struct relay_session
*session
;
1266 DBG("Viewer get next index");
1268 health_code_update();
1270 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1274 health_code_update();
1277 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1283 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1289 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1292 memset(&viewer_index
, 0, sizeof(viewer_index
));
1295 * The viewer should not ask for index on metadata stream.
1297 if (vstream
->metadata_flag
) {
1298 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1302 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1305 /* Try to open an index if one is needed for that stream. */
1306 ret
= try_open_index(vstream
, rstream
);
1308 if (ret
== -ENOENT
) {
1310 * The index is created only when the first data packet arrives, it
1311 * might not be ready at the beginning of the session
1313 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1315 /* Unhandled error. */
1316 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1321 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1322 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1323 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1326 } else if (ret
== 1) {
1328 * This means the viewer index data structure has been populated by the
1329 * check call thus we now send back the reply to the client.
1333 /* At this point, ret MUST be 0 thus we continue with the get. */
1336 if (!ctf_trace
->metadata_received
||
1337 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1338 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1341 ret
= check_new_streams(conn
);
1344 } else if (ret
== 1) {
1345 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1348 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1349 pthread_mutex_lock(&vstream
->overwrite_lock
);
1350 if (vstream
->abort_flag
) {
1351 /* The file is being overwritten by the writer, we cannot use it. */
1352 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1353 ret
= viewer_stream_rotate(vstream
, rstream
);
1354 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1357 } else if (ret
== 1) {
1358 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1359 viewer_stream_delete(vstream
);
1360 viewer_stream_destroy(ctf_trace
, vstream
);
1362 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1367 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1368 sizeof(packet_index
));
1369 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1370 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1372 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1373 viewer_stream_delete(vstream
);
1374 viewer_stream_destroy(ctf_trace
, vstream
);
1376 } else if (read_ret
< sizeof(packet_index
)) {
1377 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1378 if (vstream
->close_write_flag
) {
1379 ret
= viewer_stream_rotate(vstream
, rstream
);
1381 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1383 } else if (ret
== 1) {
1384 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1385 viewer_stream_delete(vstream
);
1386 viewer_stream_destroy(ctf_trace
, vstream
);
1388 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1391 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1392 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1394 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1397 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1398 vstream
->last_sent_index
++;
1402 * Indexes are stored in big endian, no need to switch before sending.
1404 viewer_index
.offset
= packet_index
.offset
;
1405 viewer_index
.packet_size
= packet_index
.packet_size
;
1406 viewer_index
.content_size
= packet_index
.content_size
;
1407 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1408 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1409 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1410 viewer_index
.stream_id
= packet_index
.stream_id
;
1413 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1414 health_code_update();
1416 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1420 health_code_update();
1422 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1423 vstream
->last_sent_index
, vstream
->stream_handle
);
1433 * Send the next index for a stream
1435 * Return 0 on success or else a negative value.
1438 int viewer_get_packet(struct relay_connection
*conn
)
1440 int ret
, send_data
= 0;
1444 struct lttng_viewer_get_packet get_packet_info
;
1445 struct lttng_viewer_trace_packet reply
;
1446 struct relay_viewer_stream
*stream
;
1447 struct relay_session
*session
;
1448 struct ctf_trace
*ctf_trace
;
1452 DBG2("Relay get data packet");
1454 health_code_update();
1456 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1460 health_code_update();
1462 /* From this point on, the error label can be reached. */
1463 memset(&reply
, 0, sizeof(reply
));
1466 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1471 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1477 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1482 * First time we read this stream, we need open the tracefile, we should
1483 * only arrive here if an index has already been sent to the viewer, so the
1484 * tracefile must exist, if it does not it is a fatal error.
1486 if (stream
->read_fd
< 0) {
1487 char fullpath
[PATH_MAX
];
1489 if (stream
->tracefile_count
> 0) {
1490 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1491 stream
->channel_name
,
1492 stream
->tracefile_count_current
);
1494 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1495 stream
->channel_name
);
1500 ret
= open(fullpath
, O_RDONLY
);
1502 PERROR("Relay opening trace file");
1505 stream
->read_fd
= ret
;
1508 if (!ctf_trace
->metadata_received
||
1509 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1510 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1511 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1515 ret
= check_new_streams(conn
);
1518 } else if (ret
== 1) {
1519 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1520 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1524 len
= be32toh(get_packet_info
.len
);
1525 data
= zmalloc(len
);
1527 PERROR("relay data zmalloc");
1531 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1534 * If the read fd was closed by the streaming side, the
1535 * abort_flag will be set to 1, otherwise it is an error.
1537 if (stream
->abort_flag
== 0) {
1541 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1544 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1545 if (read_len
< len
) {
1547 * If the read fd was closed by the streaming side, the
1548 * abort_flag will be set to 1, otherwise it is an error.
1550 if (stream
->abort_flag
== 0) {
1551 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1553 be64toh(get_packet_info
.offset
));
1556 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1560 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1561 reply
.len
= htobe32(len
);
1566 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1569 reply
.flags
= htobe32(reply
.flags
);
1571 health_code_update();
1573 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1577 health_code_update();
1580 health_code_update();
1581 ret
= send_response(conn
->sock
, data
, len
);
1585 health_code_update();
1588 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1589 be64toh(get_packet_info
.stream_id
));
1600 * Send the session's metadata
1602 * Return 0 on success else a negative value.
1605 int viewer_get_metadata(struct relay_connection
*conn
)
1611 struct lttng_viewer_get_metadata request
;
1612 struct lttng_viewer_metadata_packet reply
;
1613 struct relay_viewer_stream
*stream
;
1614 struct ctf_trace
*ctf_trace
;
1615 struct relay_session
*session
;
1619 DBG("Relay get metadata");
1621 health_code_update();
1623 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1627 health_code_update();
1629 memset(&reply
, 0, sizeof(reply
));
1632 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1633 if (!stream
|| !stream
->metadata_flag
) {
1634 ERR("Invalid metadata stream");
1638 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1644 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1647 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1649 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1651 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1655 /* first time, we open the metadata file */
1656 if (stream
->read_fd
< 0) {
1657 char fullpath
[PATH_MAX
];
1659 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1660 stream
->channel_name
);
1664 ret
= open(fullpath
, O_RDONLY
);
1666 PERROR("Relay opening metadata file");
1669 stream
->read_fd
= ret
;
1672 reply
.len
= htobe64(len
);
1673 data
= zmalloc(len
);
1675 PERROR("viewer metadata zmalloc");
1679 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1680 if (read_len
< len
) {
1681 PERROR("Relay reading metadata file");
1684 ctf_trace
->metadata_sent
+= read_len
;
1685 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1689 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1692 health_code_update();
1693 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1697 health_code_update();
1700 ret
= send_response(conn
->sock
, data
, len
);
1706 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1707 be64toh(request
.stream_id
));
1709 DBG("Metadata sent");
1719 * Create a viewer session.
1721 * Return 0 on success or else a negative value.
1724 int viewer_create_session(struct relay_connection
*conn
)
1727 struct lttng_viewer_create_session_response resp
;
1729 DBG("Viewer create session received");
1731 memset(&resp
, 0, sizeof(resp
));
1732 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1733 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1734 if (!conn
->viewer_session
) {
1735 ERR("Allocation viewer session");
1736 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1739 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1742 health_code_update();
1743 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1747 health_code_update();
1756 * live_relay_unknown_command: send -1 if received unknown command
1759 void live_relay_unknown_command(struct relay_connection
*conn
)
1761 struct lttcomm_relayd_generic_reply reply
;
1763 memset(&reply
, 0, sizeof(reply
));
1764 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1765 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1769 * Process the commands received on the control socket
1772 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1773 struct relay_connection
*conn
)
1781 msg_value
= be32toh(recv_hdr
->cmd
);
1784 * Make sure we've done the version check before any command other then a
1785 * new client connection.
1787 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1788 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1793 switch (msg_value
) {
1794 case LTTNG_VIEWER_CONNECT
:
1795 ret
= viewer_connect(conn
);
1797 case LTTNG_VIEWER_LIST_SESSIONS
:
1798 ret
= viewer_list_sessions(conn
);
1800 case LTTNG_VIEWER_ATTACH_SESSION
:
1801 ret
= viewer_attach_session(conn
);
1803 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1804 ret
= viewer_get_next_index(conn
);
1806 case LTTNG_VIEWER_GET_PACKET
:
1807 ret
= viewer_get_packet(conn
);
1809 case LTTNG_VIEWER_GET_METADATA
:
1810 ret
= viewer_get_metadata(conn
);
1812 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1813 ret
= viewer_get_new_streams(conn
);
1815 case LTTNG_VIEWER_CREATE_SESSION
:
1816 ret
= viewer_create_session(conn
);
1819 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1820 live_relay_unknown_command(conn
);
1830 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1836 (void) lttng_poll_del(events
, pollfd
);
1838 ret
= close(pollfd
);
1840 ERR("Closing pollfd %d", pollfd
);
1845 * Delete and destroy a connection.
1847 * RCU read side lock MUST be acquired.
1849 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1850 struct relay_connection
*conn
)
1852 struct relay_session
*session
, *tmp_session
;
1854 assert(relay_connections_ht
);
1857 connection_delete(relay_connections_ht
, conn
);
1859 if (!conn
->viewer_session
) {
1864 cds_list_for_each_entry_safe(session
, tmp_session
,
1865 &conn
->viewer_session
->sessions_head
,
1866 viewer_session_list
) {
1867 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1868 cleanup_session(conn
, session
);
1873 connection_destroy(conn
);
1877 * This thread does the actual work
1880 void *thread_worker(void *data
)
1884 struct relay_connection
*conn
;
1885 struct lttng_poll_event events
;
1886 struct lttng_ht
*relay_connections_ht
;
1887 struct lttng_ht_iter iter
;
1888 struct lttng_viewer_cmd recv_hdr
;
1889 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1890 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1892 DBG("[thread] Live viewer relay worker started");
1894 rcu_register_thread();
1896 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1898 if (testpoint(relayd_thread_live_worker
)) {
1899 goto error_testpoint
;
1902 /* table of connections indexed on socket */
1903 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1904 if (!relay_connections_ht
) {
1905 goto relay_connections_ht_error
;
1908 ret
= create_thread_poll_set(&events
, 2);
1910 goto error_poll_create
;
1913 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1922 health_code_update();
1924 /* Infinite blocking call, waiting for transmission */
1925 DBG3("Relayd live viewer worker thread polling...");
1926 health_poll_entry();
1927 ret
= lttng_poll_wait(&events
, -1);
1931 * Restart interrupted system call.
1933 if (errno
== EINTR
) {
1942 * Process control. The control connection is prioritised so we don't
1943 * starve it with high throughput tracing data on the data
1946 for (i
= 0; i
< nb_fd
; i
++) {
1947 /* Fetch once the poll data */
1948 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1949 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1951 health_code_update();
1953 /* Thread quit pipe has been closed. Killing thread. */
1954 ret
= check_thread_quit_pipe(pollfd
, revents
);
1960 /* Inspect the relay conn pipe for new connection */
1961 if (pollfd
== live_conn_pipe
[0]) {
1962 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1963 ERR("Relay live pipe error");
1965 } else if (revents
& LPOLLIN
) {
1966 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1970 conn
->sessions_ht
= sessions_ht
;
1971 connection_init(conn
);
1972 lttng_poll_add(&events
, conn
->sock
->fd
,
1973 LPOLLIN
| LPOLLRDHUP
);
1975 lttng_ht_add_unique_ulong(relay_connections_ht
,
1978 DBG("Connection socket %d added", conn
->sock
->fd
);
1982 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1983 /* If not found, there is a synchronization issue. */
1986 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1987 cleanup_connection_pollfd(&events
, pollfd
);
1988 destroy_connection(relay_connections_ht
, conn
);
1989 } else if (revents
& LPOLLIN
) {
1990 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1991 sizeof(recv_hdr
), 0);
1993 /* Connection closed */
1994 cleanup_connection_pollfd(&events
, pollfd
);
1995 destroy_connection(relay_connections_ht
, conn
);
1996 DBG("Viewer control conn closed with %d", pollfd
);
1998 ret
= process_control(&recv_hdr
, conn
);
2000 /* Clear the session on error. */
2001 cleanup_connection_pollfd(&events
, pollfd
);
2002 destroy_connection(relay_connections_ht
, conn
);
2003 DBG("Viewer connection closed with %d", pollfd
);
2014 lttng_poll_clean(&events
);
2016 /* Cleanup reamaining connection object. */
2018 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2020 health_code_update();
2021 destroy_connection(relay_connections_ht
, conn
);
2025 lttng_ht_destroy(relay_connections_ht
);
2026 relay_connections_ht_error
:
2027 /* Close relay conn pipes */
2028 utils_close_pipe(live_conn_pipe
);
2030 DBG("Viewer worker thread exited with error");
2032 DBG("Viewer worker thread cleanup complete");
2036 ERR("Health error occurred in %s", __func__
);
2038 health_unregister(health_relayd
);
2040 rcu_unregister_thread();
2045 * Create the relay command pipe to wake thread_manage_apps.
2046 * Closed in cleanup().
2048 static int create_conn_pipe(void)
2052 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2057 void live_stop_threads(void)
2064 ret
= pthread_join(live_listener_thread
, &status
);
2066 PERROR("pthread_join live listener");
2067 goto error
; /* join error, exit without cleanup */
2070 ret
= pthread_join(live_worker_thread
, &status
);
2072 PERROR("pthread_join live worker");
2073 goto error
; /* join error, exit without cleanup */
2076 ret
= pthread_join(live_dispatcher_thread
, &status
);
2078 PERROR("pthread_join live dispatcher");
2079 goto error
; /* join error, exit without cleanup */
2091 int live_start_threads(struct lttng_uri
*uri
,
2092 struct relay_local_data
*relay_ctx
)
2101 /* Check if daemon is UID = 0 */
2102 is_root
= !getuid();
2105 if (live_uri
->port
< 1024) {
2106 ERR("Need to be root to use ports < 1024");
2112 /* Setup the thread apps communication pipe. */
2113 if ((ret
= create_conn_pipe()) < 0) {
2117 /* Init relay command queue. */
2118 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2120 /* Set up max poll set size */
2121 lttng_poll_set_max_size();
2123 /* Setup the dispatcher thread */
2124 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2125 thread_dispatcher
, (void *) NULL
);
2127 PERROR("pthread_create viewer dispatcher");
2128 goto exit_dispatcher
;
2131 /* Setup the worker thread */
2132 ret
= pthread_create(&live_worker_thread
, NULL
,
2133 thread_worker
, relay_ctx
);
2135 PERROR("pthread_create viewer worker");
2139 /* Setup the listener thread */
2140 ret
= pthread_create(&live_listener_thread
, NULL
,
2141 thread_listener
, (void *) NULL
);
2143 PERROR("pthread_create viewer listener");
2151 ret
= pthread_join(live_listener_thread
, &status
);
2153 PERROR("pthread_join live listener");
2154 goto error
; /* join error, exit without cleanup */
2158 ret
= pthread_join(live_worker_thread
, &status
);
2160 PERROR("pthread_join live worker");
2161 goto error
; /* join error, exit without cleanup */
2165 ret
= pthread_join(live_dispatcher_thread
, &status
);
2167 PERROR("pthread_join live dispatcher");
2168 goto error
; /* join error, exit without cleanup */