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
) {
737 * Increment outside of htobe64 macro, because can be used more than once
738 * within the macro, and thus the operation may be undefined.
740 last_relay_viewer_session_id
++;
741 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
744 health_code_update();
746 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
751 health_code_update();
753 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
761 * Send the viewer the list of current sessions.
763 * Return 0 on success or else a negative value.
766 int viewer_list_sessions(struct relay_connection
*conn
)
769 struct lttng_viewer_list_sessions session_list
;
771 long approx_before
, approx_after
;
772 struct lttng_ht_iter iter
;
773 struct lttng_viewer_session send_session
;
774 struct relay_session
*session
;
776 DBG("List sessions received");
779 cds_lfht_count_nodes(conn
->sessions_ht
->ht
, &approx_before
, &count
,
781 session_list
.sessions_count
= htobe32(count
);
783 health_code_update();
785 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
790 health_code_update();
792 cds_lfht_for_each_entry(conn
->sessions_ht
->ht
, &iter
.iter
, session
,
794 health_code_update();
796 strncpy(send_session
.session_name
, session
->session_name
,
797 sizeof(send_session
.session_name
));
798 strncpy(send_session
.hostname
, session
->hostname
,
799 sizeof(send_session
.hostname
));
800 send_session
.id
= htobe64(session
->id
);
801 send_session
.live_timer
= htobe32(session
->live_timer
);
802 send_session
.clients
= htobe32(session
->viewer_refcount
);
803 send_session
.streams
= htobe32(session
->stream_count
);
805 health_code_update();
807 ret
= send_response(conn
->sock
, &send_session
, sizeof(send_session
));
812 health_code_update();
826 * Check if a connection is attached to a session.
827 * Return 1 if attached, 0 if not attached, a negative value on error.
830 int session_attached(struct relay_connection
*conn
, uint64_t session_id
)
832 struct relay_session
*session
;
835 if (!conn
->viewer_session
) {
838 cds_list_for_each_entry(session
,
839 &conn
->viewer_session
->sessions_head
,
840 viewer_session_list
) {
841 if (session
->id
== session_id
) {
852 * Delete all streams for a specific session ID.
854 static void destroy_viewer_streams_by_session(struct relay_session
*session
)
856 struct relay_viewer_stream
*stream
;
857 struct lttng_ht_iter iter
;
862 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
864 struct ctf_trace
*ctf_trace
;
866 health_code_update();
867 if (stream
->session_id
!= session
->id
) {
871 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
875 viewer_stream_delete(stream
);
877 if (stream
->metadata_flag
) {
878 ctf_trace
->metadata_sent
= 0;
879 ctf_trace
->viewer_metadata_stream
= NULL
;
882 viewer_stream_destroy(ctf_trace
, stream
);
887 static void try_destroy_streams(struct relay_session
*session
)
889 struct ctf_trace
*ctf_trace
;
890 struct lttng_ht_iter iter
;
894 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
896 /* Attempt to destroy the ctf trace of that session. */
897 ctf_trace_try_destroy(session
, ctf_trace
);
904 static void cleanup_session(struct relay_connection
*conn
,
905 struct relay_session
*session
)
908 * Very important that this is done before destroying the session so we
909 * can put back every viewer stream reference from the ctf_trace.
911 destroy_viewer_streams_by_session(session
);
912 try_destroy_streams(session
);
913 cds_list_del(&session
->viewer_session_list
);
914 session_viewer_try_destroy(conn
->sessions_ht
, session
);
918 * Send the viewer the list of current sessions.
921 int viewer_get_new_streams(struct relay_connection
*conn
)
923 int ret
, send_streams
= 0;
924 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
925 struct lttng_viewer_new_streams_request request
;
926 struct lttng_viewer_new_streams_response response
;
927 struct relay_session
*session
;
932 DBG("Get new streams received");
934 health_code_update();
936 /* Receive the request from the connected client. */
937 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
941 session_id
= be64toh(request
.session_id
);
943 health_code_update();
945 memset(&response
, 0, sizeof(response
));
948 session
= session_find_by_id(conn
->sessions_ht
, session_id
);
950 DBG("Relay session %" PRIu64
" not found", session_id
);
951 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
955 if (!session_attached(conn
, session_id
)) {
957 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
962 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
964 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
969 /* Only send back the newly created streams with the unsent ones. */
970 nb_streams
= nb_created
+ nb_unsent
;
971 response
.streams_count
= htobe32(nb_streams
);
974 * If the session is closed and we have no new streams to send,
975 * it means that the viewer has already received the whole trace
976 * for this session and should now close it.
978 if (nb_streams
== 0 && session
->close_flag
) {
980 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
982 * Remove the session from the attached list of the connection
983 * and try to destroy it.
985 cds_list_del(&session
->viewer_session_list
);
986 cleanup_session(conn
, session
);
991 health_code_update();
992 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
996 health_code_update();
999 * Unknown or empty session, just return gracefully, the viewer knows what
1002 if (!send_streams
|| !nb_streams
) {
1008 * Send stream and *DON'T* ignore the sent flag so every viewer streams
1009 * that were not sent from that point will be sent to the viewer.
1011 ret
= send_viewer_streams(conn
->sock
, session
, 0);
1023 * Send the viewer the list of current sessions.
1026 int viewer_attach_session(struct relay_connection
*conn
)
1028 int send_streams
= 0;
1030 uint32_t nb_streams
= 0;
1031 enum lttng_viewer_seek seek_type
;
1032 struct lttng_viewer_attach_session_request request
;
1033 struct lttng_viewer_attach_session_response response
;
1034 struct relay_session
*session
;
1038 health_code_update();
1040 /* Receive the request from the connected client. */
1041 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1046 health_code_update();
1048 memset(&response
, 0, sizeof(response
));
1050 if (!conn
->viewer_session
) {
1051 DBG("Client trying to attach before creating a live viewer session");
1052 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1057 session
= session_find_by_id(conn
->sessions_ht
,
1058 be64toh(request
.session_id
));
1060 DBG("Relay session %" PRIu64
" not found",
1061 be64toh(request
.session_id
));
1062 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1065 session_viewer_attach(session
);
1066 DBG("Attach session ID %" PRIu64
" received", be64toh(request
.session_id
));
1068 if (uatomic_read(&session
->viewer_refcount
) > 1) {
1069 DBG("Already a viewer attached");
1070 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1071 session_viewer_detach(session
);
1073 } else if (session
->live_timer
== 0) {
1074 DBG("Not live session");
1075 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1079 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1080 cds_list_add(&session
->viewer_session_list
,
1081 &conn
->viewer_session
->sessions_head
);
1084 switch (be32toh(request
.seek
)) {
1085 case LTTNG_VIEWER_SEEK_BEGINNING
:
1086 case LTTNG_VIEWER_SEEK_LAST
:
1087 seek_type
= be32toh(request
.seek
);
1090 ERR("Wrong seek parameter");
1091 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1096 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
1100 response
.streams_count
= htobe32(nb_streams
);
1103 health_code_update();
1104 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1108 health_code_update();
1111 * Unknown or empty session, just return gracefully, the viewer knows what
1114 if (!send_streams
|| !nb_streams
) {
1119 /* Send stream and ignore the sent flag. */
1120 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1132 * Open the index file if needed for the given vstream.
1134 * If an index file is successfully opened, the index_read_fd of the stream is
1137 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1139 static int try_open_index(struct relay_viewer_stream
*vstream
,
1140 struct relay_stream
*rstream
)
1147 if (vstream
->index_read_fd
>= 0) {
1152 * First time, we open the index file and at least one index is ready. The
1153 * race between the read and write of the total_index_received is
1154 * acceptable here since the client will be notified to simply come back
1155 * and get the next index.
1157 if (rstream
->total_index_received
<= 0) {
1161 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1162 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1164 vstream
->index_read_fd
= ret
;
1174 * Check the status of the index for the given stream. This function updates
1175 * the index structure if needed and can destroy the vstream also for the HUP
1178 * Return 0 means that we can proceed with the index. A value of 1 means that
1179 * the index has been updated and is ready to be send to the client. A negative
1180 * value indicates an error that can't be handled.
1182 static int check_index_status(struct relay_viewer_stream
*vstream
,
1183 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1184 struct lttng_viewer_index
*index
)
1193 if (!rstream
->close_flag
) {
1194 /* Rotate on abort (overwrite). */
1195 if (vstream
->abort_flag
) {
1196 DBG("Viewer stream %" PRIu64
" rotate because of overwrite",
1197 vstream
->stream_handle
);
1198 ret
= viewer_stream_rotate(vstream
, rstream
);
1201 } else if (ret
== 1) {
1203 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1206 /* ret == 0 means successful so we continue. */
1209 /* Check if we are in the same trace file at this point. */
1210 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1211 if (rstream
->beacon_ts_end
!= -1ULL &&
1212 vstream
->last_sent_index
== rstream
->total_index_received
) {
1214 * We've received a synchronization beacon and the last index
1215 * available has been sent, the index for now is inactive.
1217 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1218 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1219 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1221 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1222 && !vstream
->close_write_flag
) {
1224 * Reader and writer are working in the same tracefile, so we care
1225 * about the number of index received and sent. Otherwise, we read
1228 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1232 /* Nothing to do with the index, continue with it. */
1234 } else if (rstream
->close_flag
&& vstream
->close_write_flag
&&
1235 vstream
->total_index_received
== vstream
->last_sent_index
) {
1236 /* Last index sent and current tracefile closed in write */
1237 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1240 vstream
->close_write_flag
= 1;
1248 viewer_stream_delete(vstream
);
1249 viewer_stream_destroy(trace
, vstream
);
1255 * Send the next index for a stream.
1257 * Return 0 on success or else a negative value.
1260 int viewer_get_next_index(struct relay_connection
*conn
)
1264 struct lttng_viewer_get_next_index request_index
;
1265 struct lttng_viewer_index viewer_index
;
1266 struct ctf_packet_index packet_index
;
1267 struct relay_viewer_stream
*vstream
;
1268 struct relay_stream
*rstream
;
1269 struct ctf_trace
*ctf_trace
;
1270 struct relay_session
*session
;
1274 DBG("Viewer get next index");
1276 health_code_update();
1278 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1282 health_code_update();
1285 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1291 session
= session_find_by_id(conn
->sessions_ht
, vstream
->session_id
);
1297 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
, vstream
->path_name
);
1300 memset(&viewer_index
, 0, sizeof(viewer_index
));
1303 * The viewer should not ask for index on metadata stream.
1305 if (vstream
->metadata_flag
) {
1306 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1310 rstream
= stream_find_by_id(relay_streams_ht
, vstream
->stream_handle
);
1313 /* Try to open an index if one is needed for that stream. */
1314 ret
= try_open_index(vstream
, rstream
);
1316 if (ret
== -ENOENT
) {
1318 * The index is created only when the first data packet arrives, it
1319 * might not be ready at the beginning of the session
1321 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1323 /* Unhandled error. */
1324 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1329 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1330 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1331 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1334 } else if (ret
== 1) {
1336 * This means the viewer index data structure has been populated by the
1337 * check call thus we now send back the reply to the client.
1341 /* At this point, ret MUST be 0 thus we continue with the get. */
1344 if (!ctf_trace
->metadata_received
||
1345 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1346 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1349 ret
= check_new_streams(conn
);
1352 } else if (ret
== 1) {
1353 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1356 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1357 pthread_mutex_lock(&vstream
->overwrite_lock
);
1358 if (vstream
->abort_flag
) {
1359 /* The file is being overwritten by the writer, we cannot use it. */
1360 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1361 ret
= viewer_stream_rotate(vstream
, rstream
);
1362 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1365 } else if (ret
== 1) {
1366 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1367 viewer_stream_delete(vstream
);
1368 viewer_stream_destroy(ctf_trace
, vstream
);
1370 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1375 read_ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1376 sizeof(packet_index
));
1377 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1378 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1380 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1381 viewer_stream_delete(vstream
);
1382 viewer_stream_destroy(ctf_trace
, vstream
);
1384 } else if (read_ret
< sizeof(packet_index
)) {
1385 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1386 if (vstream
->close_write_flag
) {
1387 ret
= viewer_stream_rotate(vstream
, rstream
);
1389 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1391 } else if (ret
== 1) {
1392 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1393 viewer_stream_delete(vstream
);
1394 viewer_stream_destroy(ctf_trace
, vstream
);
1396 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1399 ERR("Relay reading index file %d", vstream
->index_read_fd
);
1400 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1402 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1405 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1406 vstream
->last_sent_index
++;
1410 * Indexes are stored in big endian, no need to switch before sending.
1412 viewer_index
.offset
= packet_index
.offset
;
1413 viewer_index
.packet_size
= packet_index
.packet_size
;
1414 viewer_index
.content_size
= packet_index
.content_size
;
1415 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1416 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1417 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1418 viewer_index
.stream_id
= packet_index
.stream_id
;
1421 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1422 health_code_update();
1424 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1428 health_code_update();
1430 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1431 vstream
->last_sent_index
, vstream
->stream_handle
);
1441 * Send the next index for a stream
1443 * Return 0 on success or else a negative value.
1446 int viewer_get_packet(struct relay_connection
*conn
)
1448 int ret
, send_data
= 0;
1452 struct lttng_viewer_get_packet get_packet_info
;
1453 struct lttng_viewer_trace_packet reply
;
1454 struct relay_viewer_stream
*stream
;
1455 struct relay_session
*session
;
1456 struct ctf_trace
*ctf_trace
;
1460 DBG2("Relay get data packet");
1462 health_code_update();
1464 ret
= recv_request(conn
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1468 health_code_update();
1470 /* From this point on, the error label can be reached. */
1471 memset(&reply
, 0, sizeof(reply
));
1474 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1479 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1485 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1490 * First time we read this stream, we need open the tracefile, we should
1491 * only arrive here if an index has already been sent to the viewer, so the
1492 * tracefile must exist, if it does not it is a fatal error.
1494 if (stream
->read_fd
< 0) {
1495 char fullpath
[PATH_MAX
];
1497 if (stream
->tracefile_count
> 0) {
1498 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1499 stream
->channel_name
,
1500 stream
->tracefile_count_current
);
1502 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1503 stream
->channel_name
);
1508 ret
= open(fullpath
, O_RDONLY
);
1510 PERROR("Relay opening trace file");
1513 stream
->read_fd
= ret
;
1516 if (!ctf_trace
->metadata_received
||
1517 ctf_trace
->metadata_received
> ctf_trace
->metadata_sent
) {
1518 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1519 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1523 ret
= check_new_streams(conn
);
1526 } else if (ret
== 1) {
1527 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1528 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1532 len
= be32toh(get_packet_info
.len
);
1533 data
= zmalloc(len
);
1535 PERROR("relay data zmalloc");
1539 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1542 * If the read fd was closed by the streaming side, the
1543 * abort_flag will be set to 1, otherwise it is an error.
1545 if (stream
->abort_flag
== 0) {
1549 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1552 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1553 if (read_len
< len
) {
1555 * If the read fd was closed by the streaming side, the
1556 * abort_flag will be set to 1, otherwise it is an error.
1558 if (stream
->abort_flag
== 0) {
1559 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1561 be64toh(get_packet_info
.offset
));
1564 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_EOF
);
1568 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1569 reply
.len
= htobe32(len
);
1574 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1577 reply
.flags
= htobe32(reply
.flags
);
1579 health_code_update();
1581 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1585 health_code_update();
1588 health_code_update();
1589 ret
= send_response(conn
->sock
, data
, len
);
1593 health_code_update();
1596 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1597 be64toh(get_packet_info
.stream_id
));
1608 * Send the session's metadata
1610 * Return 0 on success else a negative value.
1613 int viewer_get_metadata(struct relay_connection
*conn
)
1619 struct lttng_viewer_get_metadata request
;
1620 struct lttng_viewer_metadata_packet reply
;
1621 struct relay_viewer_stream
*stream
;
1622 struct ctf_trace
*ctf_trace
;
1623 struct relay_session
*session
;
1627 DBG("Relay get metadata");
1629 health_code_update();
1631 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1635 health_code_update();
1637 memset(&reply
, 0, sizeof(reply
));
1640 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1641 if (!stream
|| !stream
->metadata_flag
) {
1642 ERR("Invalid metadata stream");
1646 session
= session_find_by_id(conn
->sessions_ht
, stream
->session_id
);
1652 ctf_trace
= ctf_trace_find_by_path(session
->ctf_traces_ht
,
1655 assert(ctf_trace
->metadata_sent
<= ctf_trace
->metadata_received
);
1657 len
= ctf_trace
->metadata_received
- ctf_trace
->metadata_sent
;
1659 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1663 /* first time, we open the metadata file */
1664 if (stream
->read_fd
< 0) {
1665 char fullpath
[PATH_MAX
];
1667 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1668 stream
->channel_name
);
1672 ret
= open(fullpath
, O_RDONLY
);
1674 PERROR("Relay opening metadata file");
1677 stream
->read_fd
= ret
;
1680 reply
.len
= htobe64(len
);
1681 data
= zmalloc(len
);
1683 PERROR("viewer metadata zmalloc");
1687 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1688 if (read_len
< len
) {
1689 PERROR("Relay reading metadata file");
1692 ctf_trace
->metadata_sent
+= read_len
;
1693 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1697 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1700 health_code_update();
1701 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1705 health_code_update();
1708 ret
= send_response(conn
->sock
, data
, len
);
1714 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1715 be64toh(request
.stream_id
));
1717 DBG("Metadata sent");
1727 * Create a viewer session.
1729 * Return 0 on success or else a negative value.
1732 int viewer_create_session(struct relay_connection
*conn
)
1735 struct lttng_viewer_create_session_response resp
;
1737 DBG("Viewer create session received");
1739 memset(&resp
, 0, sizeof(resp
));
1740 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1741 conn
->viewer_session
= zmalloc(sizeof(*conn
->viewer_session
));
1742 if (!conn
->viewer_session
) {
1743 ERR("Allocation viewer session");
1744 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1747 CDS_INIT_LIST_HEAD(&conn
->viewer_session
->sessions_head
);
1750 health_code_update();
1751 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1755 health_code_update();
1764 * live_relay_unknown_command: send -1 if received unknown command
1767 void live_relay_unknown_command(struct relay_connection
*conn
)
1769 struct lttcomm_relayd_generic_reply reply
;
1771 memset(&reply
, 0, sizeof(reply
));
1772 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1773 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1777 * Process the commands received on the control socket
1780 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1781 struct relay_connection
*conn
)
1789 msg_value
= be32toh(recv_hdr
->cmd
);
1792 * Make sure we've done the version check before any command other then a
1793 * new client connection.
1795 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1796 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1801 switch (msg_value
) {
1802 case LTTNG_VIEWER_CONNECT
:
1803 ret
= viewer_connect(conn
);
1805 case LTTNG_VIEWER_LIST_SESSIONS
:
1806 ret
= viewer_list_sessions(conn
);
1808 case LTTNG_VIEWER_ATTACH_SESSION
:
1809 ret
= viewer_attach_session(conn
);
1811 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1812 ret
= viewer_get_next_index(conn
);
1814 case LTTNG_VIEWER_GET_PACKET
:
1815 ret
= viewer_get_packet(conn
);
1817 case LTTNG_VIEWER_GET_METADATA
:
1818 ret
= viewer_get_metadata(conn
);
1820 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1821 ret
= viewer_get_new_streams(conn
);
1823 case LTTNG_VIEWER_CREATE_SESSION
:
1824 ret
= viewer_create_session(conn
);
1827 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1828 live_relay_unknown_command(conn
);
1838 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1844 (void) lttng_poll_del(events
, pollfd
);
1846 ret
= close(pollfd
);
1848 ERR("Closing pollfd %d", pollfd
);
1853 * Delete and destroy a connection.
1855 * RCU read side lock MUST be acquired.
1857 static void destroy_connection(struct lttng_ht
*relay_connections_ht
,
1858 struct relay_connection
*conn
)
1860 struct relay_session
*session
, *tmp_session
;
1862 assert(relay_connections_ht
);
1865 connection_delete(relay_connections_ht
, conn
);
1867 if (!conn
->viewer_session
) {
1872 cds_list_for_each_entry_safe(session
, tmp_session
,
1873 &conn
->viewer_session
->sessions_head
,
1874 viewer_session_list
) {
1875 DBG("Cleaning connection of session ID %" PRIu64
, session
->id
);
1876 cleanup_session(conn
, session
);
1881 connection_destroy(conn
);
1885 * This thread does the actual work
1888 void *thread_worker(void *data
)
1892 struct relay_connection
*conn
;
1893 struct lttng_poll_event events
;
1894 struct lttng_ht
*relay_connections_ht
;
1895 struct lttng_ht_iter iter
;
1896 struct lttng_viewer_cmd recv_hdr
;
1897 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1898 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1900 DBG("[thread] Live viewer relay worker started");
1902 rcu_register_thread();
1904 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1906 if (testpoint(relayd_thread_live_worker
)) {
1907 goto error_testpoint
;
1910 /* table of connections indexed on socket */
1911 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1912 if (!relay_connections_ht
) {
1913 goto relay_connections_ht_error
;
1916 ret
= create_thread_poll_set(&events
, 2);
1918 goto error_poll_create
;
1921 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1930 health_code_update();
1932 /* Infinite blocking call, waiting for transmission */
1933 DBG3("Relayd live viewer worker thread polling...");
1934 health_poll_entry();
1935 ret
= lttng_poll_wait(&events
, -1);
1939 * Restart interrupted system call.
1941 if (errno
== EINTR
) {
1950 * Process control. The control connection is prioritised so we don't
1951 * starve it with high throughput tracing data on the data
1954 for (i
= 0; i
< nb_fd
; i
++) {
1955 /* Fetch once the poll data */
1956 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1957 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1959 health_code_update();
1962 /* No activity for this FD (poll implementation). */
1966 /* Thread quit pipe has been closed. Killing thread. */
1967 ret
= check_thread_quit_pipe(pollfd
, revents
);
1973 /* Inspect the relay conn pipe for new connection */
1974 if (pollfd
== live_conn_pipe
[0]) {
1975 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1976 ERR("Relay live pipe error");
1978 } else if (revents
& LPOLLIN
) {
1979 ret
= lttng_read(live_conn_pipe
[0], &conn
, sizeof(conn
));
1983 conn
->sessions_ht
= sessions_ht
;
1984 connection_init(conn
);
1985 lttng_poll_add(&events
, conn
->sock
->fd
,
1986 LPOLLIN
| LPOLLRDHUP
);
1988 lttng_ht_add_unique_ulong(relay_connections_ht
,
1991 DBG("Connection socket %d added", conn
->sock
->fd
);
1995 conn
= connection_find_by_sock(relay_connections_ht
, pollfd
);
1996 /* If not found, there is a synchronization issue. */
1999 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2000 cleanup_connection_pollfd(&events
, pollfd
);
2001 destroy_connection(relay_connections_ht
, conn
);
2002 } else if (revents
& LPOLLIN
) {
2003 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2004 sizeof(recv_hdr
), 0);
2006 /* Connection closed */
2007 cleanup_connection_pollfd(&events
, pollfd
);
2008 destroy_connection(relay_connections_ht
, conn
);
2009 DBG("Viewer control conn closed with %d", pollfd
);
2011 ret
= process_control(&recv_hdr
, conn
);
2013 /* Clear the session on error. */
2014 cleanup_connection_pollfd(&events
, pollfd
);
2015 destroy_connection(relay_connections_ht
, conn
);
2016 DBG("Viewer connection closed with %d", pollfd
);
2027 lttng_poll_clean(&events
);
2029 /* Cleanup reamaining connection object. */
2031 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, conn
,
2033 health_code_update();
2034 destroy_connection(relay_connections_ht
, conn
);
2038 lttng_ht_destroy(relay_connections_ht
);
2039 relay_connections_ht_error
:
2040 /* Close relay conn pipes */
2041 utils_close_pipe(live_conn_pipe
);
2043 DBG("Viewer worker thread exited with error");
2045 DBG("Viewer worker thread cleanup complete");
2049 ERR("Health error occurred in %s", __func__
);
2051 health_unregister(health_relayd
);
2053 rcu_unregister_thread();
2058 * Create the relay command pipe to wake thread_manage_apps.
2059 * Closed in cleanup().
2061 static int create_conn_pipe(void)
2065 ret
= utils_create_pipe_cloexec(live_conn_pipe
);
2070 void live_stop_threads(void)
2077 ret
= pthread_join(live_listener_thread
, &status
);
2079 PERROR("pthread_join live listener");
2080 goto error
; /* join error, exit without cleanup */
2083 ret
= pthread_join(live_worker_thread
, &status
);
2085 PERROR("pthread_join live worker");
2086 goto error
; /* join error, exit without cleanup */
2089 ret
= pthread_join(live_dispatcher_thread
, &status
);
2091 PERROR("pthread_join live dispatcher");
2092 goto error
; /* join error, exit without cleanup */
2104 int live_start_threads(struct lttng_uri
*uri
,
2105 struct relay_local_data
*relay_ctx
)
2114 /* Check if daemon is UID = 0 */
2115 is_root
= !getuid();
2118 if (live_uri
->port
< 1024) {
2119 ERR("Need to be root to use ports < 1024");
2125 /* Setup the thread apps communication pipe. */
2126 if ((ret
= create_conn_pipe()) < 0) {
2130 /* Init relay command queue. */
2131 cds_wfq_init(&viewer_conn_queue
.queue
);
2133 /* Set up max poll set size */
2134 lttng_poll_set_max_size();
2136 /* Setup the dispatcher thread */
2137 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2138 thread_dispatcher
, (void *) NULL
);
2140 PERROR("pthread_create viewer dispatcher");
2141 goto exit_dispatcher
;
2144 /* Setup the worker thread */
2145 ret
= pthread_create(&live_worker_thread
, NULL
,
2146 thread_worker
, relay_ctx
);
2148 PERROR("pthread_create viewer worker");
2152 /* Setup the listener thread */
2153 ret
= pthread_create(&live_listener_thread
, NULL
,
2154 thread_listener
, (void *) NULL
);
2156 PERROR("pthread_create viewer listener");
2164 ret
= pthread_join(live_listener_thread
, &status
);
2166 PERROR("pthread_join live listener");
2167 goto error
; /* join error, exit without cleanup */
2171 ret
= pthread_join(live_worker_thread
, &status
);
2173 PERROR("pthread_join live worker");
2174 goto error
; /* join error, exit without cleanup */
2178 ret
= pthread_join(live_dispatcher_thread
, &status
);
2180 PERROR("pthread_join live dispatcher");
2181 goto error
; /* join error, exit without cleanup */