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
;
97 void cleanup_relayd_live(void)
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 int 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");
360 /* Dispatch thread */
361 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
362 futex_nto1_wake(&viewer_conn_queue
.futex
);
367 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
370 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
374 if (events
== NULL
|| size
== 0) {
379 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
385 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
397 * Check if the thread quit pipe was triggered.
399 * Return 1 if it was triggered else 0;
402 int check_thread_quit_pipe(int fd
, uint32_t events
)
404 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
412 * Create and init socket from uri.
415 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
418 struct lttcomm_sock
*sock
= NULL
;
420 sock
= lttcomm_alloc_sock_from_uri(uri
);
422 ERR("Allocating socket");
426 ret
= lttcomm_create_sock(sock
);
430 DBG("Listening on sock %d for live", sock
->fd
);
432 ret
= sock
->ops
->bind(sock
);
437 ret
= sock
->ops
->listen(sock
, -1);
447 lttcomm_destroy_sock(sock
);
453 * This thread manages the listening for new connections on the network
456 void *thread_listener(void *data
)
458 int i
, ret
, pollfd
, err
= -1;
459 uint32_t revents
, nb_fd
;
460 struct lttng_poll_event events
;
461 struct lttcomm_sock
*live_control_sock
;
463 DBG("[thread] Relay live listener started");
465 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
467 health_code_update();
469 live_control_sock
= init_socket(live_uri
);
470 if (!live_control_sock
) {
471 goto error_sock_control
;
474 /* Pass 2 as size here for the thread quit pipe and control sockets. */
475 ret
= create_thread_poll_set(&events
, 2);
477 goto error_create_poll
;
480 /* Add the control socket */
481 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
486 lttng_relay_notify_ready();
488 if (testpoint(relayd_thread_live_listener
)) {
489 goto error_testpoint
;
493 health_code_update();
495 DBG("Listener accepting live viewers connections");
499 ret
= lttng_poll_wait(&events
, -1);
503 * Restart interrupted system call.
505 if (errno
== EINTR
) {
512 DBG("Relay new viewer connection received");
513 for (i
= 0; i
< nb_fd
; i
++) {
514 health_code_update();
516 /* Fetch once the poll data */
517 revents
= LTTNG_POLL_GETEV(&events
, i
);
518 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
521 /* No activity for this FD (poll implementation). */
525 /* Thread quit pipe has been closed. Killing thread. */
526 ret
= check_thread_quit_pipe(pollfd
, revents
);
532 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
533 ERR("socket poll error");
535 } else if (revents
& LPOLLIN
) {
537 * Get allocated in this thread, enqueued to a global queue,
538 * dequeued and freed in the worker thread.
541 struct relay_connection
*new_conn
;
542 struct lttcomm_sock
*newsock
;
544 new_conn
= connection_create();
549 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
551 PERROR("accepting control sock");
552 connection_free(new_conn
);
555 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
557 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
560 PERROR("setsockopt inet");
561 lttcomm_destroy_sock(newsock
);
562 connection_free(new_conn
);
565 new_conn
->sock
= newsock
;
567 /* Enqueue request for the dispatcher thread. */
568 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
572 * Wake the dispatch queue futex. Implicit memory barrier with
573 * the exchange in cds_wfcq_enqueue.
575 futex_nto1_wake(&viewer_conn_queue
.futex
);
584 lttng_poll_clean(&events
);
586 if (live_control_sock
->fd
>= 0) {
587 ret
= live_control_sock
->ops
->close(live_control_sock
);
592 lttcomm_destroy_sock(live_control_sock
);
596 DBG("Live viewer listener thread exited with error");
598 health_unregister(health_relayd
);
599 DBG("Live viewer listener thread cleanup complete");
600 if (stop_threads()) {
601 ERR("Error stopping live threads");
607 * This thread manages the dispatching of the requests to worker threads
610 void *thread_dispatcher(void *data
)
614 struct cds_wfcq_node
*node
;
615 struct relay_connection
*conn
= NULL
;
617 DBG("[thread] Live viewer relay dispatcher started");
619 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
621 if (testpoint(relayd_thread_live_dispatcher
)) {
622 goto error_testpoint
;
625 health_code_update();
627 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
628 health_code_update();
630 /* Atomically prepare the queue futex */
631 futex_nto1_prepare(&viewer_conn_queue
.futex
);
634 health_code_update();
636 /* Dequeue commands */
637 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
638 &viewer_conn_queue
.tail
);
640 DBG("Woken up but nothing in the live-viewer "
641 "relay command queue");
642 /* Continue thread execution */
645 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
646 DBG("Dispatching viewer request waiting on sock %d",
650 * Inform worker thread of the new request. This call is blocking
651 * so we can be assured that the data will be read at some point in
652 * time or wait to the end of the world :)
654 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
656 PERROR("write conn pipe");
657 connection_destroy(conn
);
660 } while (node
!= NULL
);
662 /* Futex wait on queue. Blocking call on futex() */
664 futex_nto1_wait(&viewer_conn_queue
.futex
);
668 /* Normal exit, no error */
675 ERR("Health error occurred in %s", __func__
);
677 health_unregister(health_relayd
);
678 DBG("Live viewer dispatch thread dying");
679 if (stop_threads()) {
680 ERR("Error stopping live threads");
686 * Establish connection with the viewer and check the versions.
688 * Return 0 on success or else negative value.
691 int viewer_connect(struct relay_connection
*conn
)
694 struct lttng_viewer_connect reply
, msg
;
698 conn
->version_check_done
= 1;
700 health_code_update();
702 DBG("Viewer is establishing a connection to the relayd.");
704 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
709 health_code_update();
711 memset(&reply
, 0, sizeof(reply
));
712 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
713 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
715 /* Major versions must be the same */
716 if (reply
.major
!= be32toh(msg
.major
)) {
717 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
718 reply
.major
, be32toh(msg
.major
));
723 conn
->major
= reply
.major
;
724 /* We adapt to the lowest compatible version */
725 if (reply
.minor
<= be32toh(msg
.minor
)) {
726 conn
->minor
= reply
.minor
;
728 conn
->minor
= be32toh(msg
.minor
);
731 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
732 conn
->type
= RELAY_VIEWER_COMMAND
;
733 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
734 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
736 ERR("Unknown connection type : %u", be32toh(msg
.type
));
741 reply
.major
= htobe32(reply
.major
);
742 reply
.minor
= htobe32(reply
.minor
);
743 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
745 * Increment outside of htobe64 macro, because can be used more than once
746 * within the macro, and thus the operation may be undefined.
748 last_relay_viewer_session_id
++;
749 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
752 health_code_update();
754 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
759 health_code_update();
761 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
769 * Send the viewer the list of current sessions.
771 * Return 0 on success or else a negative value.
774 int viewer_list_sessions(struct relay_connection
*conn
)
777 struct lttng_viewer_list_sessions session_list
;
779 long approx_before
, approx_after
;
780 struct lttng_ht_iter iter
;
781 struct lttng_viewer_session send_session
;
782 struct relay_session
*session
;
784 DBG("List sessions received");
787 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
789 session_list
.sessions_count
= htobe32(count
);
791 health_code_update();
793 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
798 health_code_update();
800 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
802 health_code_update();
804 strncpy(send_session
.session_name
, session
->session_name
,
805 sizeof(send_session
.session_name
));
806 strncpy(send_session
.hostname
, session
->hostname
,
807 sizeof(send_session
.hostname
));
808 send_session
.id
= htobe64(session
->id
);
809 send_session
.live_timer
= htobe32(session
->live_timer
);
810 send_session
.clients
= htobe32(session
->viewer_refcount
);
811 send_session
.streams
= htobe32(session
->stream_count
);
813 health_code_update();
815 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
820 health_code_update();
829 * Check if a connection is attached to a session.
830 * Return 1 if attached, 0 if not attached, a negative value on error.
833 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
835 struct relay_session
*session
;
838 if (!conn
->viewer_session
) {
841 cds_list_for_each_entry(session
,
842 &conn
->viewer_session
->sessions_head
,
843 viewer_session_list
) {
844 if (session
->id
== session_id
) {
855 * Delete all streams for a specific session ID.
857 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
859 struct relay_viewer_stream
*stream
;
860 struct lttng_ht_iter iter
;
865 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
867 struct ctf_trace
*ctf_trace
;
869 health_code_update();
870 if (stream
->session_id
!= session
->id
) {
874 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
878 viewer_stream_delete(stream
);
880 if (stream
->metadata_flag
) {
881 ctf_trace
->metadata_sent
= 0;
882 ctf_trace
->viewer_metadata_stream
= NULL
;
885 viewer_stream_destroy(ctf_trace
, stream
);
890 static void try_destroy_streams(struct relay_session
*session
)
892 struct ctf_trace
*ctf_trace
;
893 struct lttng_ht_iter iter
;
897 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
899 /* Attempt to destroy the ctf trace of that session. */
900 ctf_trace_try_destroy(session
, ctf_trace
);
907 static void cleanup_session(struct relay_connection
*conn
,
908 struct relay_session
*session
)
911 * Very important that this is done before destroying the session so we
912 * can put back every viewer stream reference from the ctf_trace.
914 destroy_viewer_streams_by_session(session
);
915 try_destroy_streams(session
);
916 cds_list_del(&session
->viewer_session_list
);
917 session_viewer_try_destroy(conn
->sessions_ht
, session
);
921 * Send the viewer the list of current sessions.
924 int viewer_get_new_streams(struct relay_connection
*conn
)
926 int ret
, send_streams
= 0;
927 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
928 struct lttng_viewer_new_streams_request request
;
929 struct lttng_viewer_new_streams_response response
;
930 struct relay_session
*session
;
935 DBG("Get new streams received");
937 health_code_update();
939 /* Receive the request from the connected client. */
940 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
944 session_id
= be64toh(request
.session_id
);
946 health_code_update();
948 memset(&response
, 0, sizeof(response
));
951 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
953 DBG("Relay session %" PRIu64
" not found", session_id
);
954 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
958 if (!session_attached(conn
, session_id
)) {
960 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
965 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
967 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
972 /* Only send back the newly created streams with the unsent ones. */
973 nb_streams
= nb_created
+ nb_unsent
;
974 response
.streams_count
= htobe32(nb_streams
);
977 * If the session is closed and we have no new streams to send,
978 * it means that the viewer has already received the whole trace
979 * for this session and should now close it.
981 if (nb_streams
== 0 && session
->close_flag
) {
983 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
985 * Remove the session from the attached list of the connection
986 * and try to destroy it.
988 cds_list_del(&session
->viewer_session_list
);
989 cleanup_session(conn
, session
);
994 health_code_update();
995 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
999 health_code_update();
1002 * Unknown or empty session, just return gracefully, the viewer knows what
1005 if (!send_streams
|| !nb_streams
) {
1011 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1012 * that were not sent from that point will be sent to the viewer.
1014 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1026 * Send the viewer the list of current sessions.
1029 int viewer_attach_session(struct relay_connection
*conn
)
1031 int send_streams
= 0;
1033 uint32_t nb_streams
= 0;
1034 enum lttng_viewer_seek seek_type
;
1035 struct lttng_viewer_attach_session_request request
;
1036 struct lttng_viewer_attach_session_response response
;
1037 struct relay_session
*session
;
1041 health_code_update();
1043 /* Receive the request from the connected client. */
1044 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1049 health_code_update();
1051 memset(&response
, 0, sizeof(response
));
1053 if (!conn
->viewer_session
) {
1054 DBG("Client trying to attach before creating a live viewer session");
1055 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1060 session
= session_find_by_id(conn
->sessions_ht
,
1061 be64toh(request
.session_id
));
1063 DBG("Relay session %" PRIu64
" not found",
1064 be64toh(request
.session_id
));
1065 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1068 session_viewer_attach(session
);
1069 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1071 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1072 DBG("Already a viewer attached");
1073 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1074 session_viewer_detach(session
);
1076 } else if (session
->live_timer
== 0) {
1077 DBG("Not live session");
1078 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1082 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1083 cds_list_add(&session
->viewer_session_list
,
1084 &conn
->viewer_session
->sessions_head
);
1087 switch (be32toh(request
.seek
)) {
1088 case LTTNG_VIEWER_SEEK_BEGINNING
:
1089 case LTTNG_VIEWER_SEEK_LAST
:
1090 seek_type
= be32toh(request
.seek
);
1093 ERR("Wrong seek parameter");
1094 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1099 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1103 response
.streams_count
= htobe32(nb_streams
);
1106 health_code_update();
1107 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1111 health_code_update();
1114 * Unknown or empty session, just return gracefully, the viewer knows what
1117 if (!send_streams
|| !nb_streams
) {
1122 /* Send stream and ignore the sent flag. */
1123 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1135 * Open the index file if needed for the given vstream.
1137 * If an index file is successfully opened, the index_read_fd of the stream is
1140 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1142 static int try_open_index(struct relay_viewer_stream
*vstream
,
1143 struct relay_stream
*rstream
)
1150 if (vstream
->index_read_fd
>= 0) {
1155 * First time, we open the index file and at least one index is ready. The
1156 * race between the read and write of the total_index_received is
1157 * acceptable here since the client will be notified to simply come back
1158 * and get the next index.
1160 if (rstream
->total_index_received
<= 0) {
1164 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1165 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1167 vstream
->index_read_fd
= ret
;
1177 * Check the status of the index for the given stream. This function updates
1178 * the index structure if needed and can destroy the vstream also for the HUP
1181 * Return 0 means that we can proceed with the index. A value of 1 means that
1182 * the index has been updated and is ready to be send to the client. A negative
1183 * value indicates an error that can't be handled.
1185 static int check_index_status(struct relay_viewer_stream
*vstream
,
1186 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1187 struct lttng_viewer_index
*index
)
1196 if (!rstream
->close_flag
) {
1197 /* Rotate on abort (overwrite). */
1198 if (vstream
->abort_flag
) {
1199 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1200 vstream
->stream_handle
);
1201 ret
= viewer_stream_rotate(vstream
, rstream
);
1204 } else if (ret
== 1) {
1206 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1209 /* ret == 0 means successful so we continue. */
1212 /* Check if we are in the same trace file at this point. */
1213 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1214 if (rstream
->beacon_ts_end
!= -1ULL &&
1215 vstream
->last_sent_index
== rstream
->total_index_received
) {
1217 * We've received a synchronization beacon and the last index
1218 * available has been sent, the index for now is inactive.
1220 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1221 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1222 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1224 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1225 && !vstream
->close_write_flag
) {
1227 * Reader and writer are working in the same tracefile, so we care
1228 * about the number of index received and sent. Otherwise, we read
1231 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1235 /* Nothing to do with the index, continue with it. */
1237 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1238 vstream
->total_index_received
== vstream
->last_sent_index
) {
1239 /* Last index sent and current tracefile closed in write */
1240 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1243 vstream
->close_write_flag
= 1;
1251 viewer_stream_delete(vstream
);
1252 viewer_stream_destroy(trace
, vstream
);
1258 * Send the next index for a stream.
1260 * Return 0 on success or else a negative value.
1263 int viewer_get_next_index(struct relay_connection
*conn
)
1267 struct lttng_viewer_get_next_index request_index
;
1268 struct lttng_viewer_index viewer_index
;
1269 struct ctf_packet_index packet_index
;
1270 struct relay_viewer_stream
*vstream
;
1271 struct relay_stream
*rstream
;
1272 struct ctf_trace
*ctf_trace
;
1273 struct relay_session
*session
;
1277 DBG("Viewer get next index");
1279 health_code_update();
1281 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1285 health_code_update();
1288 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1294 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1300 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1303 memset(&viewer_index
, 0, sizeof(viewer_index
));
1306 * The viewer should not ask for index on metadata stream.
1308 if (vstream
->metadata_flag
) {
1309 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1313 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1316 /* Try to open an index if one is needed for that stream. */
1317 ret
= try_open_index(vstream
, rstream
);
1319 if (ret
== -ENOENT
) {
1321 * The index is created only when the first data packet arrives, it
1322 * might not be ready at the beginning of the session
1324 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1326 /* Unhandled error. */
1327 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1332 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1333 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1334 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1337 } else if (ret
== 1) {
1339 * This means the viewer index data structure has been populated by the
1340 * check call thus we now send back the reply to the client.
1344 /* At this point, ret MUST be 0 thus we continue with the get. */
1347 if (!ctf_trace
->metadata_received
||
1348 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1349 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1352 ret
= check_new_streams(conn
);
1355 } else if (ret
== 1) {
1356 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1359 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1360 pthread_mutex_lock(&vstream
->overwrite_lock
);
1361 if (vstream
->abort_flag
) {
1362 /* The file is being overwritten by the writer, we cannot use it. */
1363 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1364 ret
= viewer_stream_rotate(vstream
, rstream
);
1365 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1368 } else if (ret
== 1) {
1369 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1370 viewer_stream_delete(vstream
);
1371 viewer_stream_destroy(ctf_trace
, vstream
);
1373 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1378 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1379 sizeof(packet_index
));
1380 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1381 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1383 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1384 viewer_stream_delete(vstream
);
1385 viewer_stream_destroy(ctf_trace
, vstream
);
1387 } else if (read_ret
< sizeof(packet_index
)) {
1388 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1389 if (vstream
->close_write_flag
) {
1390 ret
= viewer_stream_rotate(vstream
, rstream
);
1392 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1394 } else if (ret
== 1) {
1395 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1396 viewer_stream_delete(vstream
);
1397 viewer_stream_destroy(ctf_trace
, vstream
);
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1402 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1403 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1405 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1408 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1409 vstream
->last_sent_index
++;
1413 * Indexes are stored in big endian, no need to switch before sending.
1415 viewer_index
.offset
= packet_index
.offset
;
1416 viewer_index
.packet_size
= packet_index
.packet_size
;
1417 viewer_index
.content_size
= packet_index
.content_size
;
1418 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1419 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1420 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1421 viewer_index
.stream_id
= packet_index
.stream_id
;
1424 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1425 health_code_update();
1427 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1431 health_code_update();
1433 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1434 vstream
->last_sent_index
, vstream
->stream_handle
);
1444 * Send the next index for a stream
1446 * Return 0 on success or else a negative value.
1449 int viewer_get_packet(struct relay_connection
*conn
)
1451 int ret
, send_data
= 0;
1455 struct lttng_viewer_get_packet get_packet_info
;
1456 struct lttng_viewer_trace_packet reply
;
1457 struct relay_viewer_stream
*stream
;
1458 struct relay_session
*session
;
1459 struct ctf_trace
*ctf_trace
;
1463 DBG2("Relay get data packet");
1465 health_code_update();
1467 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1471 health_code_update();
1473 /* From this point on, the error label can be reached. */
1474 memset(&reply
, 0, sizeof(reply
));
1477 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1482 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1488 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1493 * First time we read this stream, we need open the tracefile, we should
1494 * only arrive here if an index has already been sent to the viewer, so the
1495 * tracefile must exist, if it does not it is a fatal error.
1497 if (stream
->read_fd
< 0) {
1498 char fullpath
[PATH_MAX
];
1500 if (stream
->tracefile_count
> 0) {
1501 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1502 stream
->channel_name
,
1503 stream
->tracefile_count_current
);
1505 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1506 stream
->channel_name
);
1511 ret
= open(fullpath
, O_RDONLY
);
1513 PERROR("Relay opening trace file");
1516 stream
->read_fd
= ret
;
1519 if (!ctf_trace
->metadata_received
||
1520 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1521 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1522 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1526 ret
= check_new_streams(conn
);
1529 } else if (ret
== 1) {
1530 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1531 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1535 len
= be32toh(get_packet_info
.len
);
1536 data
= zmalloc(len
);
1538 PERROR("relay data zmalloc");
1542 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1545 * If the read fd was closed by the streaming side, the
1546 * abort_flag will be set to 1, otherwise it is an error.
1548 if (stream
->abort_flag
== 0) {
1552 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1555 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1556 if (read_len
< len
) {
1558 * If the read fd was closed by the streaming side, the
1559 * abort_flag will be set to 1, otherwise it is an error.
1561 if (stream
->abort_flag
== 0) {
1562 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1564 be64toh(get_packet_info
.offset
));
1567 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1571 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1572 reply
.len
= htobe32(len
);
1577 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1580 reply
.flags
= htobe32(reply
.flags
);
1582 health_code_update();
1584 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1588 health_code_update();
1591 health_code_update();
1592 ret
= send_response(conn
->sock
, data
, len
);
1596 health_code_update();
1599 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1600 be64toh(get_packet_info
.stream_id
));
1611 * Send the session's metadata
1613 * Return 0 on success else a negative value.
1616 int viewer_get_metadata(struct relay_connection
*conn
)
1622 struct lttng_viewer_get_metadata request
;
1623 struct lttng_viewer_metadata_packet reply
;
1624 struct relay_viewer_stream
*stream
;
1625 struct ctf_trace
*ctf_trace
;
1626 struct relay_session
*session
;
1630 DBG("Relay get metadata");
1632 health_code_update();
1634 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1638 health_code_update();
1640 memset(&reply
, 0, sizeof(reply
));
1643 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1644 if (!stream
|| !stream
->metadata_flag
) {
1645 ERR("Invalid metadata stream");
1649 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1655 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1658 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1660 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1662 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1666 /* first time, we open the metadata file */
1667 if (stream
->read_fd
< 0) {
1668 char fullpath
[PATH_MAX
];
1670 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1671 stream
->channel_name
);
1675 ret
= open(fullpath
, O_RDONLY
);
1677 PERROR("Relay opening metadata file");
1680 stream
->read_fd
= ret
;
1683 reply
.len
= htobe64(len
);
1684 data
= zmalloc(len
);
1686 PERROR("viewer metadata zmalloc");
1690 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1691 if (read_len
< len
) {
1692 PERROR("Relay reading metadata file");
1695 ctf_trace
->metadata_sent
+= read_len
;
1696 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1700 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1703 health_code_update();
1704 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1708 health_code_update();
1711 ret
= send_response(conn
->sock
, data
, len
);
1717 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1718 be64toh(request
.stream_id
));
1720 DBG("Metadata sent");
1730 * Create a viewer session.
1732 * Return 0 on success or else a negative value.
1735 int viewer_create_session(struct relay_connection
*conn
)
1738 struct lttng_viewer_create_session_response resp
;
1740 DBG("Viewer create session received");
1742 memset(&resp
, 0, sizeof(resp
));
1743 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1744 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1745 if (!conn
->viewer_session
) {
1746 ERR("Allocation viewer session");
1747 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1750 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1753 health_code_update();
1754 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1758 health_code_update();
1767 * live_relay_unknown_command: send -1 if received unknown command
1770 void live_relay_unknown_command(struct relay_connection
*conn
)
1772 struct lttcomm_relayd_generic_reply reply
;
1774 memset(&reply
, 0, sizeof(reply
));
1775 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1776 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1780 * Process the commands received on the control socket
1783 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1784 struct relay_connection
*conn
)
1792 msg_value
= be32toh(recv_hdr
->cmd
);
1795 * Make sure we've done the version check before any command other then a
1796 * new client connection.
1798 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1799 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1804 switch (msg_value
) {
1805 case LTTNG_VIEWER_CONNECT
:
1806 ret
= viewer_connect(conn
);
1808 case LTTNG_VIEWER_LIST_SESSIONS
:
1809 ret
= viewer_list_sessions(conn
);
1811 case LTTNG_VIEWER_ATTACH_SESSION
:
1812 ret
= viewer_attach_session(conn
);
1814 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1815 ret
= viewer_get_next_index(conn
);
1817 case LTTNG_VIEWER_GET_PACKET
:
1818 ret
= viewer_get_packet(conn
);
1820 case LTTNG_VIEWER_GET_METADATA
:
1821 ret
= viewer_get_metadata(conn
);
1823 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1824 ret
= viewer_get_new_streams(conn
);
1826 case LTTNG_VIEWER_CREATE_SESSION
:
1827 ret
= viewer_create_session(conn
);
1830 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1831 live_relay_unknown_command(conn
);
1841 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1847 (void) lttng_poll_del(events
, pollfd
);
1849 ret
= close(pollfd
);
1851 ERR("Closing pollfd %d", pollfd
);
1856 * Delete and destroy a connection.
1858 * RCU read side lock MUST be acquired.
1860 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1861 struct relay_connection
*conn
)
1863 struct relay_session
*session
, *tmp_session
;
1865 assert(relay_connections_ht
);
1868 connection_delete(relay_connections_ht
, conn
);
1870 if (!conn
->viewer_session
) {
1875 cds_list_for_each_entry_safe(session
, tmp_session
,
1876 &conn
->viewer_session
->sessions_head
,
1877 viewer_session_list
) {
1878 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1879 cleanup_session(conn
, session
);
1884 connection_destroy(conn
);
1888 * This thread does the actual work
1891 void *thread_worker(void *data
)
1895 struct relay_connection
*conn
;
1896 struct lttng_poll_event events
;
1897 struct lttng_ht
*relay_connections_ht
;
1898 struct lttng_ht_iter iter
;
1899 struct lttng_viewer_cmd recv_hdr
;
1900 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1901 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1903 DBG("[thread] Live viewer relay worker started");
1905 rcu_register_thread();
1907 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1909 if (testpoint(relayd_thread_live_worker
)) {
1910 goto error_testpoint
;
1913 /* table of connections indexed on socket */
1914 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1915 if (!relay_connections_ht
) {
1916 goto relay_connections_ht_error
;
1919 ret
= create_thread_poll_set(&events
, 2);
1921 goto error_poll_create
;
1924 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1933 health_code_update();
1935 /* Infinite blocking call, waiting for transmission */
1936 DBG3("Relayd live viewer worker thread polling...");
1937 health_poll_entry();
1938 ret
= lttng_poll_wait(&events
, -1);
1942 * Restart interrupted system call.
1944 if (errno
== EINTR
) {
1953 * Process control. The control connection is prioritised so we don't
1954 * starve it with high throughput tracing data on the data
1957 for (i
= 0; i
< nb_fd
; i
++) {
1958 /* Fetch once the poll data */
1959 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1960 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1962 health_code_update();
1965 /* No activity for this FD (poll implementation). */
1969 /* Thread quit pipe has been closed. Killing thread. */
1970 ret
= check_thread_quit_pipe(pollfd
, revents
);
1976 /* Inspect the relay conn pipe for new connection */
1977 if (pollfd
== live_conn_pipe
[0]) {
1978 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1979 ERR("Relay live pipe error");
1981 } else if (revents
& LPOLLIN
) {
1982 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1986 conn
->sessions_ht
= sessions_ht
;
1987 connection_init(conn
);
1988 lttng_poll_add(&events
, conn
->sock
->fd
,
1989 LPOLLIN
| LPOLLRDHUP
);
1991 lttng_ht_add_unique_ulong(relay_connections_ht
,
1994 DBG("Connection socket %d added", conn
->sock
->fd
);
1998 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1999 /* If not found, there is a synchronization issue. */
2002 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2003 cleanup_connection_pollfd(&events
, pollfd
);
2004 destroy_connection(relay_connections_ht
, conn
);
2005 } else if (revents
& LPOLLIN
) {
2006 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2007 sizeof(recv_hdr
), 0);
2009 /* Connection closed */
2010 cleanup_connection_pollfd(&events
, pollfd
);
2011 destroy_connection(relay_connections_ht
, conn
);
2012 DBG("Viewer control conn closed with %d", pollfd
);
2014 ret
= process_control(&recv_hdr
, conn
);
2016 /* Clear the session on error. */
2017 cleanup_connection_pollfd(&events
, pollfd
);
2018 destroy_connection(relay_connections_ht
, conn
);
2019 DBG("Viewer connection closed with %d", pollfd
);
2030 lttng_poll_clean(&events
);
2032 /* Cleanup reamaining connection object. */
2034 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2036 health_code_update();
2037 destroy_connection(relay_connections_ht
, conn
);
2041 lttng_ht_destroy(relay_connections_ht
);
2042 relay_connections_ht_error
:
2043 /* Close relay conn pipes */
2044 utils_close_pipe(live_conn_pipe
);
2046 DBG("Viewer worker thread exited with error");
2048 DBG("Viewer worker thread cleanup complete");
2052 ERR("Health error occurred in %s", __func__
);
2054 health_unregister(health_relayd
);
2055 if (stop_threads()) {
2056 ERR("Error stopping live threads");
2058 rcu_unregister_thread();
2063 * Create the relay command pipe to wake thread_manage_apps.
2064 * Closed in cleanup().
2066 static int create_conn_pipe(void)
2068 return utils_create_pipe_cloexec(live_conn_pipe
);
2071 int relayd_live_stop(void)
2073 return stop_threads();
2076 int relayd_live_join(void)
2078 int ret
, retval
= 0;
2081 ret
= pthread_join(live_listener_thread
, &status
);
2084 PERROR("pthread_join live listener");
2088 ret
= pthread_join(live_worker_thread
, &status
);
2091 PERROR("pthread_join live worker");
2095 ret
= pthread_join(live_dispatcher_thread
, &status
);
2098 PERROR("pthread_join live dispatcher");
2102 cleanup_relayd_live();
2110 int relayd_live_create(struct lttng_uri
*uri
,
2111 struct relay_local_data
*relay_ctx
)
2113 int ret
= 0, retval
= 0;
2119 goto exit_init_data
;
2123 /* Check if daemon is UID = 0 */
2124 is_root
= !getuid();
2127 if (live_uri
->port
< 1024) {
2128 ERR("Need to be root to use ports < 1024");
2130 goto exit_init_data
;
2134 /* Setup the thread apps communication pipe. */
2135 if (create_conn_pipe()) {
2137 goto exit_init_data
;
2140 /* Init relay command queue. */
2141 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2143 /* Set up max poll set size */
2144 lttng_poll_set_max_size();
2146 /* Setup the dispatcher thread */
2147 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2148 thread_dispatcher
, (void *) NULL
);
2151 PERROR("pthread_create viewer dispatcher");
2153 goto exit_dispatcher_thread
;
2156 /* Setup the worker thread */
2157 ret
= pthread_create(&live_worker_thread
, NULL
,
2158 thread_worker
, relay_ctx
);
2161 PERROR("pthread_create viewer worker");
2163 goto exit_worker_thread
;
2166 /* Setup the listener thread */
2167 ret
= pthread_create(&live_listener_thread
, NULL
,
2168 thread_listener
, (void *) NULL
);
2171 PERROR("pthread_create viewer listener");
2173 goto exit_listener_thread
;
2177 * All OK, started all threads.
2182 ret
= pthread_join(live_listener_thread
, &status
);
2185 PERROR("pthread_join live listener");
2188 exit_listener_thread
:
2190 ret
= pthread_join(live_worker_thread
, &status
);
2193 PERROR("pthread_join live worker");
2198 ret
= pthread_join(live_dispatcher_thread
, &status
);
2201 PERROR("pthread_join live dispatcher");
2204 exit_dispatcher_thread
:
2207 cleanup_relayd_live();