2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "ctf-trace.h"
67 #include "connection.h"
69 static struct lttng_uri
*live_uri
;
72 * This pipe is used to inform the worker thread that a command is queued and
73 * ready to be processed.
75 static int live_conn_pipe
[2] = { -1, -1 };
77 /* Shared between threads */
78 static int live_dispatch_thread_exit
;
80 static pthread_t live_listener_thread
;
81 static pthread_t live_dispatcher_thread
;
82 static pthread_t live_worker_thread
;
85 * Relay command queue.
87 * The live_thread_listener and live_thread_dispatcher communicate with this
90 static struct relay_conn_queue viewer_conn_queue
;
92 static uint64_t last_relay_viewer_session_id
;
106 * Receive a request buffer using a given socket, destination allocated buffer
109 * Return the size of the received message or else a negative value on error
110 * with errno being set by recvmsg() syscall.
113 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
120 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
121 if (ret
< 0 || ret
!= size
) {
123 /* Orderly shutdown. Not necessary to print an error. */
124 DBG("Socket %d did an orderly shutdown", sock
->fd
);
126 ERR("Relay failed to receive request.");
135 * Send a response buffer using a given socket, source allocated buffer of
138 * Return the size of the sent message or else a negative value on error with
139 * errno being set by sendmsg() syscall.
142 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
149 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
151 ERR("Relayd failed to send response.");
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
165 int check_new_streams(struct relay_connection
*conn
)
167 struct relay_session
*session
;
168 unsigned long current_val
;
171 if (!conn
->viewer_session
) {
174 cds_list_for_each_entry(session
,
175 &conn
->viewer_session
->sessions_head
,
176 viewer_session_list
) {
177 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
189 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
190 * this function should ignore the sent flag or not.
192 * Return 0 on success or else a negative value.
195 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
196 struct relay_session
*session
, unsigned int ignore_sent_flag
)
199 struct lttng_viewer_stream send_stream
;
200 struct lttng_ht_iter iter
;
201 struct relay_viewer_stream
*vstream
;
207 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
209 struct ctf_trace
*ctf_trace
;
211 health_code_update();
213 /* Ignore if not the same session. */
214 if (vstream
->session_id
!= session
->id
||
215 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
219 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
223 send_stream
.id
= htobe64(vstream
->stream_handle
);
224 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
225 send_stream
.metadata_flag
= htobe32(vstream
->metadata_flag
);
226 strncpy(send_stream
.path_name
, vstream
->path_name
,
227 sizeof(send_stream
.path_name
));
228 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
229 sizeof(send_stream
.channel_name
));
231 DBG("Sending stream %" PRIu64
" to viewer", vstream
->stream_handle
);
232 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
236 vstream
->sent_flag
= 1;
247 * Create every viewer stream possible for the given session with the seek
248 * type. Three counters *can* be return which are in order the total amount of
249 * viewer stream of the session, the number of unsent stream and the number of
250 * stream created. Those counters can be NULL and thus will be ignored.
252 * Return 0 on success or else a negative value.
255 int make_viewer_streams(struct relay_session
*session
,
256 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
257 uint32_t *nb_created
)
260 struct lttng_ht_iter iter
;
261 struct ctf_trace
*ctf_trace
;
266 * This is to make sure we create viewer streams for a full received
267 * channel. For instance, if we have 8 streams for a channel that are
268 * concurrently being flagged ready, we can end up creating just a subset
269 * of the 8 streams (the ones that are flagged). This lock avoids this
272 pthread_mutex_lock(&session
->viewer_ready_lock
);
275 * Create viewer streams for relay streams that are ready to be used for a
276 * the given session id only.
279 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
281 struct relay_stream
*stream
;
283 health_code_update();
285 if (ctf_trace
->invalid_flag
) {
289 cds_list_for_each_entry(stream
, &ctf_trace
->stream_list
, trace_list
) {
290 struct relay_viewer_stream
*vstream
;
292 if (!stream
->viewer_ready
) {
296 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
298 vstream
= viewer_stream_create(stream
, seek_t
, ctf_trace
);
303 /* Acquire reference to ctf_trace. */
304 ctf_trace_get_ref(ctf_trace
);
307 /* Update number of created stream counter. */
310 } else if (!vstream
->sent_flag
&& nb_unsent
) {
311 /* Update number of unsent stream counter. */
314 /* Update number of total stream counter. */
325 pthread_mutex_unlock(&session
->viewer_ready_lock
);
330 * Write to writable pipe used to notify a thread.
333 int notify_thread_pipe(int wpipe
)
337 ret
= lttng_write(wpipe
, "!", 1);
339 PERROR("write poll pipe");
346 * Stop all threads by closing the thread quit pipe.
349 void stop_threads(void)
353 /* Stopping all threads */
354 DBG("Terminating all live threads");
355 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
357 ERR("write error on thread quit pipe");
360 /* Dispatch thread */
361 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
362 futex_nto1_wake(&viewer_conn_queue
.futex
);
366 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
369 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
373 if (events
== NULL
|| size
== 0) {
378 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
384 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
396 * Check if the thread quit pipe was triggered.
398 * Return 1 if it was triggered else 0;
401 int check_thread_quit_pipe(int fd
, uint32_t events
)
403 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
411 * Create and init socket from uri.
414 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
417 struct lttcomm_sock
*sock
= NULL
;
419 sock
= lttcomm_alloc_sock_from_uri(uri
);
421 ERR("Allocating socket");
425 ret
= lttcomm_create_sock(sock
);
429 DBG("Listening on sock %d for live", sock
->fd
);
431 ret
= sock
->ops
->bind(sock
);
436 ret
= sock
->ops
->listen(sock
, -1);
446 lttcomm_destroy_sock(sock
);
452 * This thread manages the listening for new connections on the network
455 void *thread_listener(void *data
)
457 int i
, ret
, pollfd
, err
= -1;
458 uint32_t revents
, nb_fd
;
459 struct lttng_poll_event events
;
460 struct lttcomm_sock
*live_control_sock
;
462 DBG("[thread] Relay live listener started");
464 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
466 health_code_update();
468 live_control_sock
= init_socket(live_uri
);
469 if (!live_control_sock
) {
470 goto error_sock_control
;
473 /* Pass 2 as size here for the thread quit pipe and control sockets. */
474 ret
= create_thread_poll_set(&events
, 2);
476 goto error_create_poll
;
479 /* Add the control socket */
480 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
485 lttng_relay_notify_ready();
487 if (testpoint(relayd_thread_live_listener
)) {
488 goto error_testpoint
;
492 health_code_update();
494 DBG("Listener accepting live viewers connections");
498 ret
= lttng_poll_wait(&events
, -1);
502 * Restart interrupted system call.
504 if (errno
== EINTR
) {
511 DBG("Relay new viewer connection received");
512 for (i
= 0; i
< nb_fd
; i
++) {
513 health_code_update();
515 /* Fetch once the poll data */
516 revents
= LTTNG_POLL_GETEV(&events
, i
);
517 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
519 /* Thread quit pipe has been closed. Killing thread. */
520 ret
= check_thread_quit_pipe(pollfd
, revents
);
526 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
527 ERR("socket poll error");
529 } else if (revents
& LPOLLIN
) {
531 * Get allocated in this thread, enqueued to a global queue,
532 * dequeued and freed in the worker thread.
535 struct relay_connection
*new_conn
;
536 struct lttcomm_sock
*newsock
;
538 new_conn
= connection_create();
543 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
545 PERROR("accepting control sock");
546 connection_free(new_conn
);
549 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
551 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
554 PERROR("setsockopt inet");
555 lttcomm_destroy_sock(newsock
);
556 connection_free(new_conn
);
559 new_conn
->sock
= newsock
;
561 /* Enqueue request for the dispatcher thread. */
562 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
566 * Wake the dispatch queue futex. Implicit memory barrier with
567 * the exchange in cds_wfcq_enqueue.
569 futex_nto1_wake(&viewer_conn_queue
.futex
);
578 lttng_poll_clean(&events
);
580 if (live_control_sock
->fd
>= 0) {
581 ret
= live_control_sock
->ops
->close(live_control_sock
);
586 lttcomm_destroy_sock(live_control_sock
);
590 DBG("Live viewer listener thread exited with error");
592 health_unregister(health_relayd
);
593 DBG("Live viewer listener thread cleanup complete");
599 * This thread manages the dispatching of the requests to worker threads
602 void *thread_dispatcher(void *data
)
606 struct cds_wfcq_node
*node
;
607 struct relay_connection
*conn
= NULL
;
609 DBG("[thread] Live viewer relay dispatcher started");
611 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
613 if (testpoint(relayd_thread_live_dispatcher
)) {
614 goto error_testpoint
;
617 health_code_update();
619 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
620 health_code_update();
622 /* Atomically prepare the queue futex */
623 futex_nto1_prepare(&viewer_conn_queue
.futex
);
626 health_code_update();
628 /* Dequeue commands */
629 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
630 &viewer_conn_queue
.tail
);
632 DBG("Woken up but nothing in the live-viewer "
633 "relay command queue");
634 /* Continue thread execution */
637 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
638 DBG("Dispatching viewer request waiting on sock %d",
642 * Inform worker thread of the new request. This call is blocking
643 * so we can be assured that the data will be read at some point in
644 * time or wait to the end of the world :)
646 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
648 PERROR("write conn pipe");
649 connection_destroy(conn
);
652 } while (node
!= NULL
);
654 /* Futex wait on queue. Blocking call on futex() */
656 futex_nto1_wait(&viewer_conn_queue
.futex
);
660 /* Normal exit, no error */
667 ERR("Health error occurred in %s", __func__
);
669 health_unregister(health_relayd
);
670 DBG("Live viewer dispatch thread dying");
676 * Establish connection with the viewer and check the versions.
678 * Return 0 on success or else negative value.
681 int viewer_connect(struct relay_connection
*conn
)
684 struct lttng_viewer_connect reply
, msg
;
688 conn
->version_check_done
= 1;
690 health_code_update();
692 DBG("Viewer is establishing a connection to the relayd.");
694 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
699 health_code_update();
701 memset(&reply
, 0, sizeof(reply
));
702 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
703 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
705 /* Major versions must be the same */
706 if (reply
.major
!= be32toh(msg
.major
)) {
707 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
708 reply
.major
, be32toh(msg
.major
));
713 conn
->major
= reply
.major
;
714 /* We adapt to the lowest compatible version */
715 if (reply
.minor
<= be32toh(msg
.minor
)) {
716 conn
->minor
= reply
.minor
;
718 conn
->minor
= be32toh(msg
.minor
);
721 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
722 conn
->type
= RELAY_VIEWER_COMMAND
;
723 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
724 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
726 ERR("Unknown connection type : %u", be32toh(msg
.type
));
731 reply
.major
= htobe32(reply
.major
);
732 reply
.minor
= htobe32(reply
.minor
);
733 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
734 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
737 health_code_update();
739 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
744 health_code_update();
746 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
754 * Send the viewer the list of current sessions.
756 * Return 0 on success or else a negative value.
759 int viewer_list_sessions(struct relay_connection
*conn
)
762 struct lttng_viewer_list_sessions session_list
;
764 long approx_before
, approx_after
;
765 struct lttng_ht_iter iter
;
766 struct lttng_viewer_session send_session
;
767 struct relay_session
*session
;
769 DBG("List sessions received");
772 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
774 session_list
.sessions_count
= htobe32(count
);
776 health_code_update();
778 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
783 health_code_update();
785 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
787 health_code_update();
789 strncpy(send_session
.session_name
, session
->session_name
,
790 sizeof(send_session
.session_name
));
791 strncpy(send_session
.hostname
, session
->hostname
,
792 sizeof(send_session
.hostname
));
793 send_session
.id
= htobe64(session
->id
);
794 send_session
.live_timer
= htobe32(session
->live_timer
);
795 send_session
.clients
= htobe32(session
->viewer_refcount
);
796 send_session
.streams
= htobe32(session
->stream_count
);
798 health_code_update();
800 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
805 health_code_update();
819 * Check if a connection is attached to a session.
820 * Return 1 if attached, 0 if not attached, a negative value on error.
823 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
825 struct relay_session
*session
;
828 if (!conn
->viewer_session
) {
831 cds_list_for_each_entry(session
,
832 &conn
->viewer_session
->sessions_head
,
833 viewer_session_list
) {
834 if (session
->id
== session_id
) {
845 * Delete all streams for a specific session ID.
847 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
849 struct relay_viewer_stream
*stream
;
850 struct lttng_ht_iter iter
;
855 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
857 struct ctf_trace
*ctf_trace
;
859 health_code_update();
860 if (stream
->session_id
!= session
->id
) {
864 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
868 viewer_stream_delete(stream
);
870 if (stream
->metadata_flag
) {
871 ctf_trace
->metadata_sent
= 0;
872 ctf_trace
->viewer_metadata_stream
= NULL
;
875 viewer_stream_destroy(ctf_trace
, stream
);
880 static void try_destroy_streams(struct relay_session
*session
)
882 struct ctf_trace
*ctf_trace
;
883 struct lttng_ht_iter iter
;
887 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
889 /* Attempt to destroy the ctf trace of that session. */
890 ctf_trace_try_destroy(session
, ctf_trace
);
897 static void cleanup_session(struct relay_connection
*conn
,
898 struct relay_session
*session
)
901 * Very important that this is done before destroying the session so we
902 * can put back every viewer stream reference from the ctf_trace.
904 destroy_viewer_streams_by_session(session
);
905 try_destroy_streams(session
);
906 cds_list_del(&session
->viewer_session_list
);
907 session_viewer_try_destroy(conn
->sessions_ht
, session
);
911 * Send the viewer the list of current sessions.
914 int viewer_get_new_streams(struct relay_connection
*conn
)
916 int ret
, send_streams
= 0;
917 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
918 struct lttng_viewer_new_streams_request request
;
919 struct lttng_viewer_new_streams_response response
;
920 struct relay_session
*session
;
925 DBG("Get new streams received");
927 health_code_update();
929 /* Receive the request from the connected client. */
930 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
934 session_id
= be64toh(request
.session_id
);
936 health_code_update();
938 memset(&response
, 0, sizeof(response
));
941 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
943 DBG("Relay session %" PRIu64
" not found", session_id
);
944 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
948 if (!session_attached(conn
, session_id
)) {
950 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
955 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
957 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
962 /* Only send back the newly created streams with the unsent ones. */
963 nb_streams
= nb_created
+ nb_unsent
;
964 response
.streams_count
= htobe32(nb_streams
);
967 * If the session is closed and we have no new streams to send,
968 * it means that the viewer has already received the whole trace
969 * for this session and should now close it.
971 if (nb_streams
== 0 && session
->close_flag
) {
973 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
975 * Remove the session from the attached list of the connection
976 * and try to destroy it.
978 cds_list_del(&session
->viewer_session_list
);
979 cleanup_session(conn
, session
);
984 health_code_update();
985 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
989 health_code_update();
992 * Unknown or empty session, just return gracefully, the viewer knows what
995 if (!send_streams
|| !nb_streams
) {
1001 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1002 * that were not sent from that point will be sent to the viewer.
1004 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1016 * Send the viewer the list of current sessions.
1019 int viewer_attach_session(struct relay_connection
*conn
)
1021 int send_streams
= 0;
1023 uint32_t nb_streams
= 0;
1024 enum lttng_viewer_seek seek_type
;
1025 struct lttng_viewer_attach_session_request request
;
1026 struct lttng_viewer_attach_session_response response
;
1027 struct relay_session
*session
;
1031 health_code_update();
1033 /* Receive the request from the connected client. */
1034 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1039 health_code_update();
1041 memset(&response
, 0, sizeof(response
));
1043 if (!conn
->viewer_session
) {
1044 DBG("Client trying to attach before creating a live viewer session");
1045 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1050 session
= session_find_by_id(conn
->sessions_ht
,
1051 be64toh(request
.session_id
));
1053 DBG("Relay session %" PRIu64
" not found",
1054 be64toh(request
.session_id
));
1055 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1058 session_viewer_attach(session
);
1059 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1061 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1062 DBG("Already a viewer attached");
1063 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1064 session_viewer_detach(session
);
1066 } else if (session
->live_timer
== 0) {
1067 DBG("Not live session");
1068 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1072 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1073 cds_list_add(&session
->viewer_session_list
,
1074 &conn
->viewer_session
->sessions_head
);
1077 switch (be32toh(request
.seek
)) {
1078 case LTTNG_VIEWER_SEEK_BEGINNING
:
1079 case LTTNG_VIEWER_SEEK_LAST
:
1080 seek_type
= be32toh(request
.seek
);
1083 ERR("Wrong seek parameter");
1084 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1089 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1093 response
.streams_count
= htobe32(nb_streams
);
1096 health_code_update();
1097 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1101 health_code_update();
1104 * Unknown or empty session, just return gracefully, the viewer knows what
1107 if (!send_streams
|| !nb_streams
) {
1112 /* Send stream and ignore the sent flag. */
1113 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1125 * Open the index file if needed for the given vstream.
1127 * If an index file is successfully opened, the index_read_fd of the stream is
1130 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1132 static int try_open_index(struct relay_viewer_stream
*vstream
,
1133 struct relay_stream
*rstream
)
1140 if (vstream
->index_read_fd
>= 0) {
1145 * First time, we open the index file and at least one index is ready. The
1146 * race between the read and write of the total_index_received is
1147 * acceptable here since the client will be notified to simply come back
1148 * and get the next index.
1150 if (rstream
->total_index_received
<= 0) {
1154 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1155 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1157 vstream
->index_read_fd
= ret
;
1167 * Check the status of the index for the given stream. This function updates
1168 * the index structure if needed and can destroy the vstream also for the HUP
1171 * Return 0 means that we can proceed with the index. A value of 1 means that
1172 * the index has been updated and is ready to be send to the client. A negative
1173 * value indicates an error that can't be handled.
1175 static int check_index_status(struct relay_viewer_stream
*vstream
,
1176 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1177 struct lttng_viewer_index
*index
)
1186 if (!rstream
->close_flag
) {
1187 /* Rotate on abort (overwrite). */
1188 if (vstream
->abort_flag
) {
1189 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1190 vstream
->stream_handle
);
1191 ret
= viewer_stream_rotate(vstream
, rstream
);
1194 } else if (ret
== 1) {
1196 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1199 /* ret == 0 means successful so we continue. */
1202 /* Check if we are in the same trace file at this point. */
1203 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1204 if (rstream
->beacon_ts_end
!= -1ULL &&
1205 vstream
->last_sent_index
== rstream
->total_index_received
) {
1207 * We've received a synchronization beacon and the last index
1208 * available has been sent, the index for now is inactive.
1210 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1211 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1212 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1214 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1215 && !vstream
->close_write_flag
) {
1217 * Reader and writer are working in the same tracefile, so we care
1218 * about the number of index received and sent. Otherwise, we read
1221 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1225 /* Nothing to do with the index, continue with it. */
1227 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1228 vstream
->total_index_received
== vstream
->last_sent_index
) {
1229 /* Last index sent and current tracefile closed in write */
1230 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1233 vstream
->close_write_flag
= 1;
1241 viewer_stream_delete(vstream
);
1242 viewer_stream_destroy(trace
, vstream
);
1248 * Send the next index for a stream.
1250 * Return 0 on success or else a negative value.
1253 int viewer_get_next_index(struct relay_connection
*conn
)
1257 struct lttng_viewer_get_next_index request_index
;
1258 struct lttng_viewer_index viewer_index
;
1259 struct ctf_packet_index packet_index
;
1260 struct relay_viewer_stream
*vstream
;
1261 struct relay_stream
*rstream
;
1262 struct ctf_trace
*ctf_trace
;
1263 struct relay_session
*session
;
1267 DBG("Viewer get next index");
1269 health_code_update();
1271 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1275 health_code_update();
1278 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1284 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1290 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1293 memset(&viewer_index
, 0, sizeof(viewer_index
));
1296 * The viewer should not ask for index on metadata stream.
1298 if (vstream
->metadata_flag
) {
1299 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1303 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1306 /* Try to open an index if one is needed for that stream. */
1307 ret
= try_open_index(vstream
, rstream
);
1309 if (ret
== -ENOENT
) {
1311 * The index is created only when the first data packet arrives, it
1312 * might not be ready at the beginning of the session
1314 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1316 /* Unhandled error. */
1317 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1322 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1323 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1324 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1327 } else if (ret
== 1) {
1329 * This means the viewer index data structure has been populated by the
1330 * check call thus we now send back the reply to the client.
1334 /* At this point, ret MUST be 0 thus we continue with the get. */
1337 if (!ctf_trace
->metadata_received
||
1338 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1339 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1342 ret
= check_new_streams(conn
);
1345 } else if (ret
== 1) {
1346 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1349 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1350 pthread_mutex_lock(&vstream
->overwrite_lock
);
1351 if (vstream
->abort_flag
) {
1352 /* The file is being overwritten by the writer, we cannot use it. */
1353 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1354 ret
= viewer_stream_rotate(vstream
, rstream
);
1355 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1358 } else if (ret
== 1) {
1359 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1360 viewer_stream_delete(vstream
);
1361 viewer_stream_destroy(ctf_trace
, vstream
);
1363 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1368 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1369 sizeof(packet_index
));
1370 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1371 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1373 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1374 viewer_stream_delete(vstream
);
1375 viewer_stream_destroy(ctf_trace
, vstream
);
1377 } else if (read_ret
< sizeof(packet_index
)) {
1378 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1379 if (vstream
->close_write_flag
) {
1380 ret
= viewer_stream_rotate(vstream
, rstream
);
1382 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1384 } else if (ret
== 1) {
1385 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1386 viewer_stream_delete(vstream
);
1387 viewer_stream_destroy(ctf_trace
, vstream
);
1389 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1392 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1393 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1395 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1398 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1399 vstream
->last_sent_index
++;
1403 * Indexes are stored in big endian, no need to switch before sending.
1405 viewer_index
.offset
= packet_index
.offset
;
1406 viewer_index
.packet_size
= packet_index
.packet_size
;
1407 viewer_index
.content_size
= packet_index
.content_size
;
1408 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1409 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1410 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1411 viewer_index
.stream_id
= packet_index
.stream_id
;
1414 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1415 health_code_update();
1417 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1421 health_code_update();
1423 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1424 vstream
->last_sent_index
, vstream
->stream_handle
);
1434 * Send the next index for a stream
1436 * Return 0 on success or else a negative value.
1439 int viewer_get_packet(struct relay_connection
*conn
)
1441 int ret
, send_data
= 0;
1445 struct lttng_viewer_get_packet get_packet_info
;
1446 struct lttng_viewer_trace_packet reply
;
1447 struct relay_viewer_stream
*stream
;
1448 struct relay_session
*session
;
1449 struct ctf_trace
*ctf_trace
;
1453 DBG2("Relay get data packet");
1455 health_code_update();
1457 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1461 health_code_update();
1463 /* From this point on, the error label can be reached. */
1464 memset(&reply
, 0, sizeof(reply
));
1467 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1472 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1478 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1483 * First time we read this stream, we need open the tracefile, we should
1484 * only arrive here if an index has already been sent to the viewer, so the
1485 * tracefile must exist, if it does not it is a fatal error.
1487 if (stream
->read_fd
< 0) {
1488 char fullpath
[PATH_MAX
];
1490 if (stream
->tracefile_count
> 0) {
1491 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1492 stream
->channel_name
,
1493 stream
->tracefile_count_current
);
1495 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1496 stream
->channel_name
);
1501 ret
= open(fullpath
, O_RDONLY
);
1503 PERROR("Relay opening trace file");
1506 stream
->read_fd
= ret
;
1509 if (!ctf_trace
->metadata_received
||
1510 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1511 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1512 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1516 ret
= check_new_streams(conn
);
1519 } else if (ret
== 1) {
1520 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1521 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1525 len
= be32toh(get_packet_info
.len
);
1526 data
= zmalloc(len
);
1528 PERROR("relay data zmalloc");
1532 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1535 * If the read fd was closed by the streaming side, the
1536 * abort_flag will be set to 1, otherwise it is an error.
1538 if (stream
->abort_flag
== 0) {
1542 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1545 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1546 if (read_len
< len
) {
1548 * If the read fd was closed by the streaming side, the
1549 * abort_flag will be set to 1, otherwise it is an error.
1551 if (stream
->abort_flag
== 0) {
1552 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1554 be64toh(get_packet_info
.offset
));
1557 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1561 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1562 reply
.len
= htobe32(len
);
1567 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1570 reply
.flags
= htobe32(reply
.flags
);
1572 health_code_update();
1574 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1578 health_code_update();
1581 health_code_update();
1582 ret
= send_response(conn
->sock
, data
, len
);
1586 health_code_update();
1589 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1590 be64toh(get_packet_info
.stream_id
));
1601 * Send the session's metadata
1603 * Return 0 on success else a negative value.
1606 int viewer_get_metadata(struct relay_connection
*conn
)
1612 struct lttng_viewer_get_metadata request
;
1613 struct lttng_viewer_metadata_packet reply
;
1614 struct relay_viewer_stream
*stream
;
1615 struct ctf_trace
*ctf_trace
;
1616 struct relay_session
*session
;
1620 DBG("Relay get metadata");
1622 health_code_update();
1624 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1628 health_code_update();
1630 memset(&reply
, 0, sizeof(reply
));
1633 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1634 if (!stream
|| !stream
->metadata_flag
) {
1635 ERR("Invalid metadata stream");
1639 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1645 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1648 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1650 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1652 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1656 /* first time, we open the metadata file */
1657 if (stream
->read_fd
< 0) {
1658 char fullpath
[PATH_MAX
];
1660 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1661 stream
->channel_name
);
1665 ret
= open(fullpath
, O_RDONLY
);
1667 PERROR("Relay opening metadata file");
1670 stream
->read_fd
= ret
;
1673 reply
.len
= htobe64(len
);
1674 data
= zmalloc(len
);
1676 PERROR("viewer metadata zmalloc");
1680 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1681 if (read_len
< len
) {
1682 PERROR("Relay reading metadata file");
1685 ctf_trace
->metadata_sent
+= read_len
;
1686 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1690 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1693 health_code_update();
1694 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1698 health_code_update();
1701 ret
= send_response(conn
->sock
, data
, len
);
1707 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1708 be64toh(request
.stream_id
));
1710 DBG("Metadata sent");
1720 * Create a viewer session.
1722 * Return 0 on success or else a negative value.
1725 int viewer_create_session(struct relay_connection
*conn
)
1728 struct lttng_viewer_create_session_response resp
;
1730 DBG("Viewer create session received");
1732 memset(&resp
, 0, sizeof(resp
));
1733 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1734 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1735 if (!conn
->viewer_session
) {
1736 ERR("Allocation viewer session");
1737 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1740 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1743 health_code_update();
1744 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1748 health_code_update();
1757 * live_relay_unknown_command: send -1 if received unknown command
1760 void live_relay_unknown_command(struct relay_connection
*conn
)
1762 struct lttcomm_relayd_generic_reply reply
;
1764 memset(&reply
, 0, sizeof(reply
));
1765 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1766 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1770 * Process the commands received on the control socket
1773 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1774 struct relay_connection
*conn
)
1782 msg_value
= be32toh(recv_hdr
->cmd
);
1785 * Make sure we've done the version check before any command other then a
1786 * new client connection.
1788 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1789 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1794 switch (msg_value
) {
1795 case LTTNG_VIEWER_CONNECT
:
1796 ret
= viewer_connect(conn
);
1798 case LTTNG_VIEWER_LIST_SESSIONS
:
1799 ret
= viewer_list_sessions(conn
);
1801 case LTTNG_VIEWER_ATTACH_SESSION
:
1802 ret
= viewer_attach_session(conn
);
1804 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1805 ret
= viewer_get_next_index(conn
);
1807 case LTTNG_VIEWER_GET_PACKET
:
1808 ret
= viewer_get_packet(conn
);
1810 case LTTNG_VIEWER_GET_METADATA
:
1811 ret
= viewer_get_metadata(conn
);
1813 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1814 ret
= viewer_get_new_streams(conn
);
1816 case LTTNG_VIEWER_CREATE_SESSION
:
1817 ret
= viewer_create_session(conn
);
1820 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1821 live_relay_unknown_command(conn
);
1831 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1837 (void) lttng_poll_del(events
, pollfd
);
1839 ret
= close(pollfd
);
1841 ERR("Closing pollfd %d", pollfd
);
1846 * Delete and destroy a connection.
1848 * RCU read side lock MUST be acquired.
1850 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1851 struct relay_connection
*conn
)
1853 struct relay_session
*session
, *tmp_session
;
1855 assert(relay_connections_ht
);
1858 connection_delete(relay_connections_ht
, conn
);
1860 if (!conn
->viewer_session
) {
1865 cds_list_for_each_entry_safe(session
, tmp_session
,
1866 &conn
->viewer_session
->sessions_head
,
1867 viewer_session_list
) {
1868 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1869 cleanup_session(conn
, session
);
1874 connection_destroy(conn
);
1878 * This thread does the actual work
1881 void *thread_worker(void *data
)
1885 struct relay_connection
*conn
;
1886 struct lttng_poll_event events
;
1887 struct lttng_ht
*relay_connections_ht
;
1888 struct lttng_ht_iter iter
;
1889 struct lttng_viewer_cmd recv_hdr
;
1890 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1891 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1893 DBG("[thread] Live viewer relay worker started");
1895 rcu_register_thread();
1897 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1899 if (testpoint(relayd_thread_live_worker
)) {
1900 goto error_testpoint
;
1903 /* table of connections indexed on socket */
1904 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1905 if (!relay_connections_ht
) {
1906 goto relay_connections_ht_error
;
1909 ret
= create_thread_poll_set(&events
, 2);
1911 goto error_poll_create
;
1914 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1923 health_code_update();
1925 /* Infinite blocking call, waiting for transmission */
1926 DBG3("Relayd live viewer worker thread polling...");
1927 health_poll_entry();
1928 ret
= lttng_poll_wait(&events
, -1);
1932 * Restart interrupted system call.
1934 if (errno
== EINTR
) {
1943 * Process control. The control connection is prioritised so we don't
1944 * starve it with high throughput tracing data on the data
1947 for (i
= 0; i
< nb_fd
; i
++) {
1948 /* Fetch once the poll data */
1949 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1950 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1952 health_code_update();
1954 /* Thread quit pipe has been closed. Killing thread. */
1955 ret
= check_thread_quit_pipe(pollfd
, revents
);
1961 /* Inspect the relay conn pipe for new connection */
1962 if (pollfd
== live_conn_pipe
[0]) {
1963 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1964 ERR("Relay live pipe error");
1966 } else if (revents
& LPOLLIN
) {
1967 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1971 conn
->sessions_ht
= sessions_ht
;
1972 connection_init(conn
);
1973 lttng_poll_add(&events
, conn
->sock
->fd
,
1974 LPOLLIN
| LPOLLRDHUP
);
1976 lttng_ht_add_unique_ulong(relay_connections_ht
,
1979 DBG("Connection socket %d added", conn
->sock
->fd
);
1983 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1984 /* If not found, there is a synchronization issue. */
1987 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1988 cleanup_connection_pollfd(&events
, pollfd
);
1989 destroy_connection(relay_connections_ht
, conn
);
1990 } else if (revents
& LPOLLIN
) {
1991 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1992 sizeof(recv_hdr
), 0);
1994 /* Connection closed */
1995 cleanup_connection_pollfd(&events
, pollfd
);
1996 destroy_connection(relay_connections_ht
, conn
);
1997 DBG("Viewer control conn closed with %d", pollfd
);
1999 ret
= process_control(&recv_hdr
, conn
);
2001 /* Clear the session on error. */
2002 cleanup_connection_pollfd(&events
, pollfd
);
2003 destroy_connection(relay_connections_ht
, conn
);
2004 DBG("Viewer connection closed with %d", pollfd
);
2015 lttng_poll_clean(&events
);
2017 /* Cleanup reamaining connection object. */
2019 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2021 health_code_update();
2022 destroy_connection(relay_connections_ht
, conn
);
2026 lttng_ht_destroy(relay_connections_ht
);
2027 relay_connections_ht_error
:
2028 /* Close relay conn pipes */
2029 utils_close_pipe(live_conn_pipe
);
2031 DBG("Viewer worker thread exited with error");
2033 DBG("Viewer worker thread cleanup complete");
2037 ERR("Health error occurred in %s", __func__
);
2039 health_unregister(health_relayd
);
2041 rcu_unregister_thread();
2046 * Create the relay command pipe to wake thread_manage_apps.
2047 * Closed in cleanup().
2049 static int create_conn_pipe(void)
2053 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2058 void live_stop_threads(void)
2065 ret
= pthread_join(live_listener_thread
, &status
);
2067 PERROR("pthread_join live listener");
2068 goto error
; /* join error, exit without cleanup */
2071 ret
= pthread_join(live_worker_thread
, &status
);
2073 PERROR("pthread_join live worker");
2074 goto error
; /* join error, exit without cleanup */
2077 ret
= pthread_join(live_dispatcher_thread
, &status
);
2079 PERROR("pthread_join live dispatcher");
2080 goto error
; /* join error, exit without cleanup */
2092 int live_start_threads(struct lttng_uri
*uri
,
2093 struct relay_local_data
*relay_ctx
)
2102 /* Check if daemon is UID = 0 */
2103 is_root
= !getuid();
2106 if (live_uri
->port
< 1024) {
2107 ERR("Need to be root to use ports < 1024");
2113 /* Setup the thread apps communication pipe. */
2114 if ((ret
= create_conn_pipe()) < 0) {
2118 /* Init relay command queue. */
2119 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2121 /* Set up max poll set size */
2122 lttng_poll_set_max_size();
2124 /* Setup the dispatcher thread */
2125 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2126 thread_dispatcher
, (void *) NULL
);
2128 PERROR("pthread_create viewer dispatcher");
2129 goto exit_dispatcher
;
2132 /* Setup the worker thread */
2133 ret
= pthread_create(&live_worker_thread
, NULL
,
2134 thread_worker
, relay_ctx
);
2136 PERROR("pthread_create viewer worker");
2140 /* Setup the listener thread */
2141 ret
= pthread_create(&live_listener_thread
, NULL
,
2142 thread_listener
, (void *) NULL
);
2144 PERROR("pthread_create viewer listener");
2152 ret
= pthread_join(live_listener_thread
, &status
);
2154 PERROR("pthread_join live listener");
2155 goto error
; /* join error, exit without cleanup */
2159 ret
= pthread_join(live_worker_thread
, &status
);
2161 PERROR("pthread_join live worker");
2162 goto error
; /* join error, exit without cleanup */
2166 ret
= pthread_join(live_dispatcher_thread
, &status
);
2168 PERROR("pthread_join live dispatcher");
2169 goto error
; /* join error, exit without cleanup */