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
);
519 /* No activity for this FD (poll implementation). */
523 /* Thread quit pipe has been closed. Killing thread. */
524 ret
= check_thread_quit_pipe(pollfd
, revents
);
530 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
531 ERR("socket poll error");
533 } else if (revents
& LPOLLIN
) {
535 * Get allocated in this thread, enqueued to a global queue,
536 * dequeued and freed in the worker thread.
539 struct relay_connection
*new_conn
;
540 struct lttcomm_sock
*newsock
;
542 new_conn
= connection_create();
547 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
549 PERROR("accepting control sock");
550 connection_free(new_conn
);
553 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
555 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
558 PERROR("setsockopt inet");
559 lttcomm_destroy_sock(newsock
);
560 connection_free(new_conn
);
563 new_conn
->sock
= newsock
;
565 /* Enqueue request for the dispatcher thread. */
566 cds_wfq_enqueue(&viewer_conn_queue
.queue
, &new_conn
->qnode
);
569 * Wake the dispatch queue futex. Implicit memory barrier with
570 * the exchange in cds_wfq_enqueue.
572 futex_nto1_wake(&viewer_conn_queue
.futex
);
581 lttng_poll_clean(&events
);
583 if (live_control_sock
->fd
>= 0) {
584 ret
= live_control_sock
->ops
->close(live_control_sock
);
589 lttcomm_destroy_sock(live_control_sock
);
593 DBG("Live viewer listener thread exited with error");
595 health_unregister(health_relayd
);
596 DBG("Live viewer listener thread cleanup complete");
602 * This thread manages the dispatching of the requests to worker threads
605 void *thread_dispatcher(void *data
)
609 struct cds_wfq_node
*node
;
610 struct relay_connection
*conn
= NULL
;
612 DBG("[thread] Live viewer relay dispatcher started");
614 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
616 if (testpoint(relayd_thread_live_dispatcher
)) {
617 goto error_testpoint
;
620 health_code_update();
622 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
623 health_code_update();
625 /* Atomically prepare the queue futex */
626 futex_nto1_prepare(&viewer_conn_queue
.futex
);
629 health_code_update();
631 /* Dequeue commands */
632 node
= cds_wfq_dequeue_blocking(&viewer_conn_queue
.queue
);
634 DBG("Woken up but nothing in the live-viewer "
635 "relay command queue");
636 /* Continue thread execution */
639 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
640 DBG("Dispatching viewer request waiting on sock %d",
644 * Inform worker thread of the new request. This call is blocking
645 * so we can be assured that the data will be read at some point in
646 * time or wait to the end of the world :)
648 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
650 PERROR("write conn pipe");
651 connection_destroy(conn
);
654 } while (node
!= NULL
);
656 /* Futex wait on queue. Blocking call on futex() */
658 futex_nto1_wait(&viewer_conn_queue
.futex
);
662 /* Normal exit, no error */
669 ERR("Health error occurred in %s", __func__
);
671 health_unregister(health_relayd
);
672 DBG("Live viewer dispatch thread dying");
678 * Establish connection with the viewer and check the versions.
680 * Return 0 on success or else negative value.
683 int viewer_connect(struct relay_connection
*conn
)
686 struct lttng_viewer_connect reply
, msg
;
690 conn
->version_check_done
= 1;
692 health_code_update();
694 DBG("Viewer is establishing a connection to the relayd.");
696 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
701 health_code_update();
703 memset(&reply
, 0, sizeof(reply
));
704 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
705 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
707 /* Major versions must be the same */
708 if (reply
.major
!= be32toh(msg
.major
)) {
709 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
710 reply
.major
, be32toh(msg
.major
));
715 conn
->major
= reply
.major
;
716 /* We adapt to the lowest compatible version */
717 if (reply
.minor
<= be32toh(msg
.minor
)) {
718 conn
->minor
= reply
.minor
;
720 conn
->minor
= be32toh(msg
.minor
);
723 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
724 conn
->type
= RELAY_VIEWER_COMMAND
;
725 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
726 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
728 ERR("Unknown connection type : %u", be32toh(msg
.type
));
733 reply
.major
= htobe32(reply
.major
);
734 reply
.minor
= htobe32(reply
.minor
);
735 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
736 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
739 health_code_update();
741 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
746 health_code_update();
748 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
756 * Send the viewer the list of current sessions.
758 * Return 0 on success or else a negative value.
761 int viewer_list_sessions(struct relay_connection
*conn
)
764 struct lttng_viewer_list_sessions session_list
;
766 long approx_before
, approx_after
;
767 struct lttng_ht_iter iter
;
768 struct lttng_viewer_session send_session
;
769 struct relay_session
*session
;
771 DBG("List sessions received");
774 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
776 session_list
.sessions_count
= htobe32(count
);
778 health_code_update();
780 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
785 health_code_update();
787 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
789 health_code_update();
791 strncpy(send_session
.session_name
, session
->session_name
,
792 sizeof(send_session
.session_name
));
793 strncpy(send_session
.hostname
, session
->hostname
,
794 sizeof(send_session
.hostname
));
795 send_session
.id
= htobe64(session
->id
);
796 send_session
.live_timer
= htobe32(session
->live_timer
);
797 send_session
.clients
= htobe32(session
->viewer_refcount
);
798 send_session
.streams
= htobe32(session
->stream_count
);
800 health_code_update();
802 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
807 health_code_update();
821 * Check if a connection is attached to a session.
822 * Return 1 if attached, 0 if not attached, a negative value on error.
825 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
827 struct relay_session
*session
;
830 if (!conn
->viewer_session
) {
833 cds_list_for_each_entry(session
,
834 &conn
->viewer_session
->sessions_head
,
835 viewer_session_list
) {
836 if (session
->id
== session_id
) {
847 * Delete all streams for a specific session ID.
849 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
851 struct relay_viewer_stream
*stream
;
852 struct lttng_ht_iter iter
;
857 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
859 struct ctf_trace
*ctf_trace
;
861 health_code_update();
862 if (stream
->session_id
!= session
->id
) {
866 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
870 viewer_stream_delete(stream
);
872 if (stream
->metadata_flag
) {
873 ctf_trace
->metadata_sent
= 0;
874 ctf_trace
->viewer_metadata_stream
= NULL
;
877 viewer_stream_destroy(ctf_trace
, stream
);
882 static void try_destroy_streams(struct relay_session
*session
)
884 struct ctf_trace
*ctf_trace
;
885 struct lttng_ht_iter iter
;
889 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
891 /* Attempt to destroy the ctf trace of that session. */
892 ctf_trace_try_destroy(session
, ctf_trace
);
899 static void cleanup_session(struct relay_connection
*conn
,
900 struct relay_session
*session
)
903 * Very important that this is done before destroying the session so we
904 * can put back every viewer stream reference from the ctf_trace.
906 destroy_viewer_streams_by_session(session
);
907 try_destroy_streams(session
);
908 cds_list_del(&session
->viewer_session_list
);
909 session_viewer_try_destroy(conn
->sessions_ht
, session
);
913 * Send the viewer the list of current sessions.
916 int viewer_get_new_streams(struct relay_connection
*conn
)
918 int ret
, send_streams
= 0;
919 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
920 struct lttng_viewer_new_streams_request request
;
921 struct lttng_viewer_new_streams_response response
;
922 struct relay_session
*session
;
927 DBG("Get new streams received");
929 health_code_update();
931 /* Receive the request from the connected client. */
932 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
936 session_id
= be64toh(request
.session_id
);
938 health_code_update();
940 memset(&response
, 0, sizeof(response
));
943 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
945 DBG("Relay session %" PRIu64
" not found", session_id
);
946 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
950 if (!session_attached(conn
, session_id
)) {
952 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
957 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
959 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
964 /* Only send back the newly created streams with the unsent ones. */
965 nb_streams
= nb_created
+ nb_unsent
;
966 response
.streams_count
= htobe32(nb_streams
);
969 * If the session is closed and we have no new streams to send,
970 * it means that the viewer has already received the whole trace
971 * for this session and should now close it.
973 if (nb_streams
== 0 && session
->close_flag
) {
975 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
977 * Remove the session from the attached list of the connection
978 * and try to destroy it.
980 cds_list_del(&session
->viewer_session_list
);
981 cleanup_session(conn
, session
);
986 health_code_update();
987 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
991 health_code_update();
994 * Unknown or empty session, just return gracefully, the viewer knows what
997 if (!send_streams
|| !nb_streams
) {
1003 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1004 * that were not sent from that point will be sent to the viewer.
1006 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1018 * Send the viewer the list of current sessions.
1021 int viewer_attach_session(struct relay_connection
*conn
)
1023 int send_streams
= 0;
1025 uint32_t nb_streams
= 0;
1026 enum lttng_viewer_seek seek_type
;
1027 struct lttng_viewer_attach_session_request request
;
1028 struct lttng_viewer_attach_session_response response
;
1029 struct relay_session
*session
;
1033 health_code_update();
1035 /* Receive the request from the connected client. */
1036 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1041 health_code_update();
1043 memset(&response
, 0, sizeof(response
));
1045 if (!conn
->viewer_session
) {
1046 DBG("Client trying to attach before creating a live viewer session");
1047 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1052 session
= session_find_by_id(conn
->sessions_ht
,
1053 be64toh(request
.session_id
));
1055 DBG("Relay session %" PRIu64
" not found",
1056 be64toh(request
.session_id
));
1057 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1060 session_viewer_attach(session
);
1061 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1063 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1064 DBG("Already a viewer attached");
1065 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1066 session_viewer_detach(session
);
1068 } else if (session
->live_timer
== 0) {
1069 DBG("Not live session");
1070 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1074 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1075 cds_list_add(&session
->viewer_session_list
,
1076 &conn
->viewer_session
->sessions_head
);
1079 switch (be32toh(request
.seek
)) {
1080 case LTTNG_VIEWER_SEEK_BEGINNING
:
1081 case LTTNG_VIEWER_SEEK_LAST
:
1082 seek_type
= be32toh(request
.seek
);
1085 ERR("Wrong seek parameter");
1086 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1091 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1095 response
.streams_count
= htobe32(nb_streams
);
1098 health_code_update();
1099 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1103 health_code_update();
1106 * Unknown or empty session, just return gracefully, the viewer knows what
1109 if (!send_streams
|| !nb_streams
) {
1114 /* Send stream and ignore the sent flag. */
1115 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1127 * Open the index file if needed for the given vstream.
1129 * If an index file is successfully opened, the index_read_fd of the stream is
1132 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1134 static int try_open_index(struct relay_viewer_stream
*vstream
,
1135 struct relay_stream
*rstream
)
1142 if (vstream
->index_read_fd
>= 0) {
1147 * First time, we open the index file and at least one index is ready. The
1148 * race between the read and write of the total_index_received is
1149 * acceptable here since the client will be notified to simply come back
1150 * and get the next index.
1152 if (rstream
->total_index_received
<= 0) {
1156 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1157 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1159 vstream
->index_read_fd
= ret
;
1169 * Check the status of the index for the given stream. This function updates
1170 * the index structure if needed and can destroy the vstream also for the HUP
1173 * Return 0 means that we can proceed with the index. A value of 1 means that
1174 * the index has been updated and is ready to be send to the client. A negative
1175 * value indicates an error that can't be handled.
1177 static int check_index_status(struct relay_viewer_stream
*vstream
,
1178 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1179 struct lttng_viewer_index
*index
)
1188 if (!rstream
->close_flag
) {
1189 /* Rotate on abort (overwrite). */
1190 if (vstream
->abort_flag
) {
1191 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1192 vstream
->stream_handle
);
1193 ret
= viewer_stream_rotate(vstream
, rstream
);
1196 } else if (ret
== 1) {
1198 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1201 /* ret == 0 means successful so we continue. */
1204 /* Check if we are in the same trace file at this point. */
1205 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1206 if (rstream
->beacon_ts_end
!= -1ULL &&
1207 vstream
->last_sent_index
== rstream
->total_index_received
) {
1209 * We've received a synchronization beacon and the last index
1210 * available has been sent, the index for now is inactive.
1212 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1213 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1214 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1216 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1217 && !vstream
->close_write_flag
) {
1219 * Reader and writer are working in the same tracefile, so we care
1220 * about the number of index received and sent. Otherwise, we read
1223 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1227 /* Nothing to do with the index, continue with it. */
1229 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1230 vstream
->total_index_received
== vstream
->last_sent_index
) {
1231 /* Last index sent and current tracefile closed in write */
1232 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1235 vstream
->close_write_flag
= 1;
1243 viewer_stream_delete(vstream
);
1244 viewer_stream_destroy(trace
, vstream
);
1250 * Send the next index for a stream.
1252 * Return 0 on success or else a negative value.
1255 int viewer_get_next_index(struct relay_connection
*conn
)
1259 struct lttng_viewer_get_next_index request_index
;
1260 struct lttng_viewer_index viewer_index
;
1261 struct ctf_packet_index packet_index
;
1262 struct relay_viewer_stream
*vstream
;
1263 struct relay_stream
*rstream
;
1264 struct ctf_trace
*ctf_trace
;
1265 struct relay_session
*session
;
1269 DBG("Viewer get next index");
1271 health_code_update();
1273 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1277 health_code_update();
1280 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1286 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1292 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1295 memset(&viewer_index
, 0, sizeof(viewer_index
));
1298 * The viewer should not ask for index on metadata stream.
1300 if (vstream
->metadata_flag
) {
1301 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1305 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1308 /* Try to open an index if one is needed for that stream. */
1309 ret
= try_open_index(vstream
, rstream
);
1311 if (ret
== -ENOENT
) {
1313 * The index is created only when the first data packet arrives, it
1314 * might not be ready at the beginning of the session
1316 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1318 /* Unhandled error. */
1319 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1324 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1325 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1326 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1329 } else if (ret
== 1) {
1331 * This means the viewer index data structure has been populated by the
1332 * check call thus we now send back the reply to the client.
1336 /* At this point, ret MUST be 0 thus we continue with the get. */
1339 if (!ctf_trace
->metadata_received
||
1340 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1341 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1344 ret
= check_new_streams(conn
);
1347 } else if (ret
== 1) {
1348 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1351 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1352 pthread_mutex_lock(&vstream
->overwrite_lock
);
1353 if (vstream
->abort_flag
) {
1354 /* The file is being overwritten by the writer, we cannot use it. */
1355 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1356 ret
= viewer_stream_rotate(vstream
, rstream
);
1357 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1360 } else if (ret
== 1) {
1361 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1362 viewer_stream_delete(vstream
);
1363 viewer_stream_destroy(ctf_trace
, vstream
);
1365 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1370 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1371 sizeof(packet_index
));
1372 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1373 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1375 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1376 viewer_stream_delete(vstream
);
1377 viewer_stream_destroy(ctf_trace
, vstream
);
1379 } else if (read_ret
< sizeof(packet_index
)) {
1380 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1381 if (vstream
->close_write_flag
) {
1382 ret
= viewer_stream_rotate(vstream
, rstream
);
1384 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1386 } else if (ret
== 1) {
1387 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1388 viewer_stream_delete(vstream
);
1389 viewer_stream_destroy(ctf_trace
, vstream
);
1391 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1394 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1395 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1397 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1400 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1401 vstream
->last_sent_index
++;
1405 * Indexes are stored in big endian, no need to switch before sending.
1407 viewer_index
.offset
= packet_index
.offset
;
1408 viewer_index
.packet_size
= packet_index
.packet_size
;
1409 viewer_index
.content_size
= packet_index
.content_size
;
1410 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1411 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1412 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1413 viewer_index
.stream_id
= packet_index
.stream_id
;
1416 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1417 health_code_update();
1419 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1423 health_code_update();
1425 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1426 vstream
->last_sent_index
, vstream
->stream_handle
);
1436 * Send the next index for a stream
1438 * Return 0 on success or else a negative value.
1441 int viewer_get_packet(struct relay_connection
*conn
)
1443 int ret
, send_data
= 0;
1447 struct lttng_viewer_get_packet get_packet_info
;
1448 struct lttng_viewer_trace_packet reply
;
1449 struct relay_viewer_stream
*stream
;
1450 struct relay_session
*session
;
1451 struct ctf_trace
*ctf_trace
;
1455 DBG2("Relay get data packet");
1457 health_code_update();
1459 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1463 health_code_update();
1465 /* From this point on, the error label can be reached. */
1466 memset(&reply
, 0, sizeof(reply
));
1469 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1474 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1480 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1485 * First time we read this stream, we need open the tracefile, we should
1486 * only arrive here if an index has already been sent to the viewer, so the
1487 * tracefile must exist, if it does not it is a fatal error.
1489 if (stream
->read_fd
< 0) {
1490 char fullpath
[PATH_MAX
];
1492 if (stream
->tracefile_count
> 0) {
1493 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1494 stream
->channel_name
,
1495 stream
->tracefile_count_current
);
1497 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1498 stream
->channel_name
);
1503 ret
= open(fullpath
, O_RDONLY
);
1505 PERROR("Relay opening trace file");
1508 stream
->read_fd
= ret
;
1511 if (!ctf_trace
->metadata_received
||
1512 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1513 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1514 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1518 ret
= check_new_streams(conn
);
1521 } else if (ret
== 1) {
1522 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1523 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1527 len
= be32toh(get_packet_info
.len
);
1528 data
= zmalloc(len
);
1530 PERROR("relay data zmalloc");
1534 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1537 * If the read fd was closed by the streaming side, the
1538 * abort_flag will be set to 1, otherwise it is an error.
1540 if (stream
->abort_flag
== 0) {
1544 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1547 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1548 if (read_len
< len
) {
1550 * If the read fd was closed by the streaming side, the
1551 * abort_flag will be set to 1, otherwise it is an error.
1553 if (stream
->abort_flag
== 0) {
1554 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1556 be64toh(get_packet_info
.offset
));
1559 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1563 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1564 reply
.len
= htobe32(len
);
1569 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1572 reply
.flags
= htobe32(reply
.flags
);
1574 health_code_update();
1576 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1580 health_code_update();
1583 health_code_update();
1584 ret
= send_response(conn
->sock
, data
, len
);
1588 health_code_update();
1591 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1592 be64toh(get_packet_info
.stream_id
));
1603 * Send the session's metadata
1605 * Return 0 on success else a negative value.
1608 int viewer_get_metadata(struct relay_connection
*conn
)
1614 struct lttng_viewer_get_metadata request
;
1615 struct lttng_viewer_metadata_packet reply
;
1616 struct relay_viewer_stream
*stream
;
1617 struct ctf_trace
*ctf_trace
;
1618 struct relay_session
*session
;
1622 DBG("Relay get metadata");
1624 health_code_update();
1626 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1630 health_code_update();
1632 memset(&reply
, 0, sizeof(reply
));
1635 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1636 if (!stream
|| !stream
->metadata_flag
) {
1637 ERR("Invalid metadata stream");
1641 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1647 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1650 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1652 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1654 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1658 /* first time, we open the metadata file */
1659 if (stream
->read_fd
< 0) {
1660 char fullpath
[PATH_MAX
];
1662 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1663 stream
->channel_name
);
1667 ret
= open(fullpath
, O_RDONLY
);
1669 PERROR("Relay opening metadata file");
1672 stream
->read_fd
= ret
;
1675 reply
.len
= htobe64(len
);
1676 data
= zmalloc(len
);
1678 PERROR("viewer metadata zmalloc");
1682 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1683 if (read_len
< len
) {
1684 PERROR("Relay reading metadata file");
1687 ctf_trace
->metadata_sent
+= read_len
;
1688 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1692 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1695 health_code_update();
1696 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1700 health_code_update();
1703 ret
= send_response(conn
->sock
, data
, len
);
1709 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1710 be64toh(request
.stream_id
));
1712 DBG("Metadata sent");
1722 * Create a viewer session.
1724 * Return 0 on success or else a negative value.
1727 int viewer_create_session(struct relay_connection
*conn
)
1730 struct lttng_viewer_create_session_response resp
;
1732 DBG("Viewer create session received");
1734 memset(&resp
, 0, sizeof(resp
));
1735 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1736 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1737 if (!conn
->viewer_session
) {
1738 ERR("Allocation viewer session");
1739 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1742 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1745 health_code_update();
1746 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1750 health_code_update();
1759 * live_relay_unknown_command: send -1 if received unknown command
1762 void live_relay_unknown_command(struct relay_connection
*conn
)
1764 struct lttcomm_relayd_generic_reply reply
;
1766 memset(&reply
, 0, sizeof(reply
));
1767 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1768 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1772 * Process the commands received on the control socket
1775 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1776 struct relay_connection
*conn
)
1784 msg_value
= be32toh(recv_hdr
->cmd
);
1787 * Make sure we've done the version check before any command other then a
1788 * new client connection.
1790 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1791 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1796 switch (msg_value
) {
1797 case LTTNG_VIEWER_CONNECT
:
1798 ret
= viewer_connect(conn
);
1800 case LTTNG_VIEWER_LIST_SESSIONS
:
1801 ret
= viewer_list_sessions(conn
);
1803 case LTTNG_VIEWER_ATTACH_SESSION
:
1804 ret
= viewer_attach_session(conn
);
1806 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1807 ret
= viewer_get_next_index(conn
);
1809 case LTTNG_VIEWER_GET_PACKET
:
1810 ret
= viewer_get_packet(conn
);
1812 case LTTNG_VIEWER_GET_METADATA
:
1813 ret
= viewer_get_metadata(conn
);
1815 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1816 ret
= viewer_get_new_streams(conn
);
1818 case LTTNG_VIEWER_CREATE_SESSION
:
1819 ret
= viewer_create_session(conn
);
1822 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1823 live_relay_unknown_command(conn
);
1833 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1839 (void) lttng_poll_del(events
, pollfd
);
1841 ret
= close(pollfd
);
1843 ERR("Closing pollfd %d", pollfd
);
1848 * Delete and destroy a connection.
1850 * RCU read side lock MUST be acquired.
1852 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1853 struct relay_connection
*conn
)
1855 struct relay_session
*session
, *tmp_session
;
1857 assert(relay_connections_ht
);
1860 connection_delete(relay_connections_ht
, conn
);
1862 if (!conn
->viewer_session
) {
1867 cds_list_for_each_entry_safe(session
, tmp_session
,
1868 &conn
->viewer_session
->sessions_head
,
1869 viewer_session_list
) {
1870 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1871 cleanup_session(conn
, session
);
1876 connection_destroy(conn
);
1880 * This thread does the actual work
1883 void *thread_worker(void *data
)
1887 struct relay_connection
*conn
;
1888 struct lttng_poll_event events
;
1889 struct lttng_ht
*relay_connections_ht
;
1890 struct lttng_ht_iter iter
;
1891 struct lttng_viewer_cmd recv_hdr
;
1892 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1893 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1895 DBG("[thread] Live viewer relay worker started");
1897 rcu_register_thread();
1899 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1901 if (testpoint(relayd_thread_live_worker
)) {
1902 goto error_testpoint
;
1905 /* table of connections indexed on socket */
1906 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1907 if (!relay_connections_ht
) {
1908 goto relay_connections_ht_error
;
1911 ret
= create_thread_poll_set(&events
, 2);
1913 goto error_poll_create
;
1916 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1925 health_code_update();
1927 /* Infinite blocking call, waiting for transmission */
1928 DBG3("Relayd live viewer worker thread polling...");
1929 health_poll_entry();
1930 ret
= lttng_poll_wait(&events
, -1);
1934 * Restart interrupted system call.
1936 if (errno
== EINTR
) {
1945 * Process control. The control connection is prioritised so we don't
1946 * starve it with high throughput tracing data on the data
1949 for (i
= 0; i
< nb_fd
; i
++) {
1950 /* Fetch once the poll data */
1951 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1952 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1954 health_code_update();
1957 /* No activity for this FD (poll implementation). */
1961 /* Thread quit pipe has been closed. Killing thread. */
1962 ret
= check_thread_quit_pipe(pollfd
, revents
);
1968 /* Inspect the relay conn pipe for new connection */
1969 if (pollfd
== live_conn_pipe
[0]) {
1970 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1971 ERR("Relay live pipe error");
1973 } else if (revents
& LPOLLIN
) {
1974 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1978 conn
->sessions_ht
= sessions_ht
;
1979 connection_init(conn
);
1980 lttng_poll_add(&events
, conn
->sock
->fd
,
1981 LPOLLIN
| LPOLLRDHUP
);
1983 lttng_ht_add_unique_ulong(relay_connections_ht
,
1986 DBG("Connection socket %d added", conn
->sock
->fd
);
1990 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1991 /* If not found, there is a synchronization issue. */
1994 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1995 cleanup_connection_pollfd(&events
, pollfd
);
1996 destroy_connection(relay_connections_ht
, conn
);
1997 } else if (revents
& LPOLLIN
) {
1998 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1999 sizeof(recv_hdr
), 0);
2001 /* Connection closed */
2002 cleanup_connection_pollfd(&events
, pollfd
);
2003 destroy_connection(relay_connections_ht
, conn
);
2004 DBG("Viewer control conn closed with %d", pollfd
);
2006 ret
= process_control(&recv_hdr
, conn
);
2008 /* Clear the session on error. */
2009 cleanup_connection_pollfd(&events
, pollfd
);
2010 destroy_connection(relay_connections_ht
, conn
);
2011 DBG("Viewer connection closed with %d", pollfd
);
2022 lttng_poll_clean(&events
);
2024 /* Cleanup reamaining connection object. */
2026 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2028 health_code_update();
2029 destroy_connection(relay_connections_ht
, conn
);
2033 lttng_ht_destroy(relay_connections_ht
);
2034 relay_connections_ht_error
:
2035 /* Close relay conn pipes */
2036 utils_close_pipe(live_conn_pipe
);
2038 DBG("Viewer worker thread exited with error");
2040 DBG("Viewer worker thread cleanup complete");
2044 ERR("Health error occurred in %s", __func__
);
2046 health_unregister(health_relayd
);
2048 rcu_unregister_thread();
2053 * Create the relay command pipe to wake thread_manage_apps.
2054 * Closed in cleanup().
2056 static int create_conn_pipe(void)
2060 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2065 void live_stop_threads(void)
2072 ret
= pthread_join(live_listener_thread
, &status
);
2074 PERROR("pthread_join live listener");
2075 goto error
; /* join error, exit without cleanup */
2078 ret
= pthread_join(live_worker_thread
, &status
);
2080 PERROR("pthread_join live worker");
2081 goto error
; /* join error, exit without cleanup */
2084 ret
= pthread_join(live_dispatcher_thread
, &status
);
2086 PERROR("pthread_join live dispatcher");
2087 goto error
; /* join error, exit without cleanup */
2099 int live_start_threads(struct lttng_uri
*uri
,
2100 struct relay_local_data
*relay_ctx
)
2109 /* Check if daemon is UID = 0 */
2110 is_root
= !getuid();
2113 if (live_uri
->port
< 1024) {
2114 ERR("Need to be root to use ports < 1024");
2120 /* Setup the thread apps communication pipe. */
2121 if ((ret
= create_conn_pipe()) < 0) {
2125 /* Init relay command queue. */
2126 cds_wfq_init(&viewer_conn_queue
.queue
);
2128 /* Set up max poll set size */
2129 lttng_poll_set_max_size();
2131 /* Setup the dispatcher thread */
2132 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2133 thread_dispatcher
, (void *) NULL
);
2135 PERROR("pthread_create viewer dispatcher");
2136 goto exit_dispatcher
;
2139 /* Setup the worker thread */
2140 ret
= pthread_create(&live_worker_thread
, NULL
,
2141 thread_worker
, relay_ctx
);
2143 PERROR("pthread_create viewer worker");
2147 /* Setup the listener thread */
2148 ret
= pthread_create(&live_listener_thread
, NULL
,
2149 thread_listener
, (void *) NULL
);
2151 PERROR("pthread_create viewer listener");
2159 ret
= pthread_join(live_listener_thread
, &status
);
2161 PERROR("pthread_join live listener");
2162 goto error
; /* join error, exit without cleanup */
2166 ret
= pthread_join(live_worker_thread
, &status
);
2168 PERROR("pthread_join live worker");
2169 goto error
; /* join error, exit without cleanup */
2173 ret
= pthread_join(live_dispatcher_thread
, &status
);
2175 PERROR("pthread_join live dispatcher");
2176 goto error
; /* join error, exit without cleanup */