2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
40 #include <urcu/rculist.h>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/defaults.h>
51 #include <common/futex.h>
52 #include <common/index/index.h>
53 #include <common/sessiond-comm/sessiond-comm.h>
54 #include <common/sessiond-comm/inet.h>
55 #include <common/sessiond-comm/relayd.h>
56 #include <common/uri.h>
57 #include <common/utils.h>
61 #include "lttng-relayd.h"
63 #include "health-relayd.h"
64 #include "testpoint.h"
65 #include "viewer-stream.h"
68 #include "ctf-trace.h"
69 #include "connection.h"
70 #include "viewer-session.h"
72 #define SESSION_BUF_DEFAULT_COUNT 16
74 static struct lttng_uri
*live_uri
;
77 * This pipe is used to inform the worker thread that a command is queued and
78 * ready to be processed.
80 static int live_conn_pipe
[2] = { -1, -1 };
82 /* Shared between threads */
83 static int live_dispatch_thread_exit
;
85 static pthread_t live_listener_thread
;
86 static pthread_t live_dispatcher_thread
;
87 static pthread_t live_worker_thread
;
90 * Relay command queue.
92 * The live_thread_listener and live_thread_dispatcher communicate with this
95 static struct relay_conn_queue viewer_conn_queue
;
97 static uint64_t last_relay_viewer_session_id
;
98 static pthread_mutex_t last_relay_viewer_session_id_lock
=
99 PTHREAD_MUTEX_INITIALIZER
;
105 void cleanup_relayd_live(void)
113 * Receive a request buffer using a given socket, destination allocated buffer
116 * Return the size of the received message or else a negative value on error
117 * with errno being set by recvmsg() syscall.
120 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
124 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
125 if (ret
< 0 || ret
!= size
) {
127 /* Orderly shutdown. Not necessary to print an error. */
128 DBG("Socket %d did an orderly shutdown", sock
->fd
);
130 ERR("Relay failed to receive request.");
139 * Send a response buffer using a given socket, source allocated buffer of
142 * Return the size of the sent message or else a negative value on error with
143 * errno being set by sendmsg() syscall.
146 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
150 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
152 ERR("Relayd failed to send response.");
159 * Atomically check if new streams got added in one of the sessions attached
160 * and reset the flag to 0.
162 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
166 int check_new_streams(struct relay_connection
*conn
)
168 struct relay_session
*session
;
169 unsigned long current_val
;
172 if (!conn
->viewer_session
) {
176 cds_list_for_each_entry_rcu(session
,
177 &conn
->viewer_session
->session_list
,
178 viewer_session_node
) {
179 if (!session_get(session
)) {
182 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
184 session_put(session
);
195 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
196 * this function should ignore the sent flag or not.
198 * Return 0 on success or else a negative value.
201 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
202 struct relay_session
*session
, unsigned int ignore_sent_flag
)
205 struct lttng_viewer_stream send_stream
;
206 struct lttng_ht_iter iter
;
207 struct relay_viewer_stream
*vstream
;
211 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
213 struct ctf_trace
*ctf_trace
;
215 health_code_update();
217 if (!viewer_stream_get(vstream
)) {
221 pthread_mutex_lock(&vstream
->stream
->lock
);
222 /* Ignore if not the same session. */
223 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
224 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
225 pthread_mutex_unlock(&vstream
->stream
->lock
);
226 viewer_stream_put(vstream
);
230 ctf_trace
= vstream
->stream
->trace
;
231 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
232 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
233 send_stream
.metadata_flag
= htobe32(
234 vstream
->stream
->is_metadata
);
235 strncpy(send_stream
.path_name
, vstream
->path_name
,
236 sizeof(send_stream
.path_name
));
237 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
238 sizeof(send_stream
.channel_name
));
240 DBG("Sending stream %" PRIu64
" to viewer",
241 vstream
->stream
->stream_handle
);
242 vstream
->sent_flag
= 1;
243 pthread_mutex_unlock(&vstream
->stream
->lock
);
245 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
246 viewer_stream_put(vstream
);
260 * Create every viewer stream possible for the given session with the seek
261 * type. Three counters *can* be return which are in order the total amount of
262 * viewer stream of the session, the number of unsent stream and the number of
263 * stream created. Those counters can be NULL and thus will be ignored.
265 * Return 0 on success or else a negative value.
268 int make_viewer_streams(struct relay_session
*session
,
269 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
270 uint32_t *nb_created
, bool *closed
)
273 struct lttng_ht_iter iter
;
274 struct ctf_trace
*ctf_trace
;
279 * Hold the session lock to ensure that we see either none or
280 * all initial streams for a session, but no intermediate state.
282 pthread_mutex_lock(&session
->lock
);
284 if (session
->connection_closed
) {
289 * Create viewer streams for relay streams that are ready to be
290 * used for a the given session id only.
293 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
295 struct relay_stream
*stream
;
297 health_code_update();
299 if (!ctf_trace_get(ctf_trace
)) {
303 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
304 struct relay_viewer_stream
*vstream
;
306 if (!stream_get(stream
)) {
310 * stream published is protected by the session
313 if (!stream
->published
) {
317 * Stream has no data, don't consider it yet.
319 if (stream
->prev_seq
== -1ULL) {
322 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
324 vstream
= viewer_stream_create(stream
, seek_t
);
327 ctf_trace_put(ctf_trace
);
333 /* Update number of created stream counter. */
337 * Ensure a self-reference is preserved even
338 * after we have put our local reference.
340 viewer_stream_get(vstream
);
342 if (!vstream
->sent_flag
&& nb_unsent
) {
343 /* Update number of unsent stream counter. */
347 /* Update number of total stream counter. */
349 if (stream
->is_metadata
) {
350 if (!stream
->closed
||
351 stream
->metadata_received
> vstream
->metadata_sent
) {
355 if (!stream
->closed
||
356 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
362 /* Put local reference. */
363 viewer_stream_put(vstream
);
367 ctf_trace_put(ctf_trace
);
374 pthread_mutex_unlock(&session
->lock
);
378 int relayd_live_stop(void)
380 /* Stop dispatch thread */
381 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
382 futex_nto1_wake(&viewer_conn_queue
.futex
);
387 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
390 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
394 if (events
== NULL
|| size
== 0) {
399 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
405 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
417 * Check if the thread quit pipe was triggered.
419 * Return 1 if it was triggered else 0;
422 int check_thread_quit_pipe(int fd
, uint32_t events
)
424 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
432 * Create and init socket from uri.
435 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
438 struct lttcomm_sock
*sock
= NULL
;
440 sock
= lttcomm_alloc_sock_from_uri(uri
);
442 ERR("Allocating socket");
446 ret
= lttcomm_create_sock(sock
);
450 DBG("Listening on sock %d for live", sock
->fd
);
452 ret
= sock
->ops
->bind(sock
);
457 ret
= sock
->ops
->listen(sock
, -1);
467 lttcomm_destroy_sock(sock
);
473 * This thread manages the listening for new connections on the network
476 void *thread_listener(void *data
)
478 int i
, ret
, pollfd
, err
= -1;
479 uint32_t revents
, nb_fd
;
480 struct lttng_poll_event events
;
481 struct lttcomm_sock
*live_control_sock
;
483 DBG("[thread] Relay live listener started");
485 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
487 health_code_update();
489 live_control_sock
= init_socket(live_uri
);
490 if (!live_control_sock
) {
491 goto error_sock_control
;
494 /* Pass 2 as size here for the thread quit pipe and control sockets. */
495 ret
= create_thread_poll_set(&events
, 2);
497 goto error_create_poll
;
500 /* Add the control socket */
501 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
506 lttng_relay_notify_ready();
508 if (testpoint(relayd_thread_live_listener
)) {
509 goto error_testpoint
;
513 health_code_update();
515 DBG("Listener accepting live viewers connections");
519 ret
= lttng_poll_wait(&events
, -1);
523 * Restart interrupted system call.
525 if (errno
== EINTR
) {
532 DBG("Relay new viewer connection received");
533 for (i
= 0; i
< nb_fd
; i
++) {
534 health_code_update();
536 /* Fetch once the poll data */
537 revents
= LTTNG_POLL_GETEV(&events
, i
);
538 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
541 /* No activity for this FD (poll implementation). */
545 /* Thread quit pipe has been closed. Killing thread. */
546 ret
= check_thread_quit_pipe(pollfd
, revents
);
552 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
553 ERR("socket poll error");
555 } else if (revents
& LPOLLIN
) {
557 * A new connection is requested, therefore a
558 * viewer connection is allocated in this
559 * thread, enqueued to a global queue and
560 * dequeued (and freed) in the worker thread.
563 struct relay_connection
*new_conn
;
564 struct lttcomm_sock
*newsock
;
566 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
568 PERROR("accepting control sock");
571 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
573 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
576 PERROR("setsockopt inet");
577 lttcomm_destroy_sock(newsock
);
580 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
582 lttcomm_destroy_sock(newsock
);
585 /* Ownership assumed by the connection. */
588 /* Enqueue request for the dispatcher thread. */
589 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
593 * Wake the dispatch queue futex.
594 * Implicit memory barrier with the
595 * exchange in cds_wfcq_enqueue.
597 futex_nto1_wake(&viewer_conn_queue
.futex
);
606 lttng_poll_clean(&events
);
608 if (live_control_sock
->fd
>= 0) {
609 ret
= live_control_sock
->ops
->close(live_control_sock
);
614 lttcomm_destroy_sock(live_control_sock
);
618 DBG("Live viewer listener thread exited with error");
620 health_unregister(health_relayd
);
621 DBG("Live viewer listener thread cleanup complete");
622 if (lttng_relay_stop_threads()) {
623 ERR("Error stopping threads");
629 * This thread manages the dispatching of the requests to worker threads
632 void *thread_dispatcher(void *data
)
636 struct cds_wfcq_node
*node
;
637 struct relay_connection
*conn
= NULL
;
639 DBG("[thread] Live viewer relay dispatcher started");
641 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
643 if (testpoint(relayd_thread_live_dispatcher
)) {
644 goto error_testpoint
;
647 health_code_update();
649 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
650 health_code_update();
652 /* Atomically prepare the queue futex */
653 futex_nto1_prepare(&viewer_conn_queue
.futex
);
656 health_code_update();
658 /* Dequeue commands */
659 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
660 &viewer_conn_queue
.tail
);
662 DBG("Woken up but nothing in the live-viewer "
663 "relay command queue");
664 /* Continue thread execution */
667 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
668 DBG("Dispatching viewer request waiting on sock %d",
672 * Inform worker thread of the new request. This
673 * call is blocking so we can be assured that
674 * the data will be read at some point in time
675 * or wait to the end of the world :)
677 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
679 PERROR("write conn pipe");
680 connection_put(conn
);
683 } while (node
!= NULL
);
685 /* Futex wait on queue. Blocking call on futex() */
687 futex_nto1_wait(&viewer_conn_queue
.futex
);
691 /* Normal exit, no error */
698 ERR("Health error occurred in %s", __func__
);
700 health_unregister(health_relayd
);
701 DBG("Live viewer dispatch thread dying");
702 if (lttng_relay_stop_threads()) {
703 ERR("Error stopping threads");
709 * Establish connection with the viewer and check the versions.
711 * Return 0 on success or else negative value.
714 int viewer_connect(struct relay_connection
*conn
)
717 struct lttng_viewer_connect reply
, msg
;
719 conn
->version_check_done
= 1;
721 health_code_update();
723 DBG("Viewer is establishing a connection to the relayd.");
725 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
730 health_code_update();
732 memset(&reply
, 0, sizeof(reply
));
733 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
734 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
736 /* Major versions must be the same */
737 if (reply
.major
!= be32toh(msg
.major
)) {
738 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
739 reply
.major
, be32toh(msg
.major
));
744 conn
->major
= reply
.major
;
745 /* We adapt to the lowest compatible version */
746 if (reply
.minor
<= be32toh(msg
.minor
)) {
747 conn
->minor
= reply
.minor
;
749 conn
->minor
= be32toh(msg
.minor
);
752 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
753 conn
->type
= RELAY_VIEWER_COMMAND
;
754 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
755 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
757 ERR("Unknown connection type : %u", be32toh(msg
.type
));
762 reply
.major
= htobe32(reply
.major
);
763 reply
.minor
= htobe32(reply
.minor
);
764 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
766 * Increment outside of htobe64 macro, because the argument can
767 * be used more than once within the macro, and thus the
768 * operation may be undefined.
770 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
771 last_relay_viewer_session_id
++;
772 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
773 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
776 health_code_update();
778 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
783 health_code_update();
785 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
793 * Send the viewer the list of current sessions.
794 * We need to create a copy of the hash table content because otherwise
795 * we cannot assume the number of entries stays the same between getting
796 * the number of HT elements and iteration over the HT.
798 * Return 0 on success or else a negative value.
801 int viewer_list_sessions(struct relay_connection
*conn
)
804 struct lttng_viewer_list_sessions session_list
;
805 struct lttng_ht_iter iter
;
806 struct relay_session
*session
;
807 struct lttng_viewer_session
*send_session_buf
= NULL
;
808 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
811 DBG("List sessions received");
813 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
814 if (!send_session_buf
) {
819 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
821 struct lttng_viewer_session
*send_session
;
823 health_code_update();
825 if (count
>= buf_count
) {
826 struct lttng_viewer_session
*newbuf
;
827 uint32_t new_buf_count
= buf_count
<< 1;
829 newbuf
= realloc(send_session_buf
,
830 new_buf_count
* sizeof(*send_session_buf
));
836 send_session_buf
= newbuf
;
837 buf_count
= new_buf_count
;
839 send_session
= &send_session_buf
[count
];
840 strncpy(send_session
->session_name
, session
->session_name
,
841 sizeof(send_session
->session_name
));
842 strncpy(send_session
->hostname
, session
->hostname
,
843 sizeof(send_session
->hostname
));
844 send_session
->id
= htobe64(session
->id
);
845 send_session
->live_timer
= htobe32(session
->live_timer
);
846 if (session
->viewer_attached
) {
847 send_session
->clients
= htobe32(1);
849 send_session
->clients
= htobe32(0);
851 send_session
->streams
= htobe32(session
->stream_count
);
856 session_list
.sessions_count
= htobe32(count
);
858 health_code_update();
860 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
865 health_code_update();
867 ret
= send_response(conn
->sock
, send_session_buf
,
868 count
* sizeof(*send_session_buf
));
872 health_code_update();
876 free(send_session_buf
);
881 * Send the viewer the list of current streams.
884 int viewer_get_new_streams(struct relay_connection
*conn
)
886 int ret
, send_streams
= 0;
887 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
888 struct lttng_viewer_new_streams_request request
;
889 struct lttng_viewer_new_streams_response response
;
890 struct relay_session
*session
;
896 DBG("Get new streams received");
898 health_code_update();
900 /* Receive the request from the connected client. */
901 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
905 session_id
= be64toh(request
.session_id
);
907 health_code_update();
909 memset(&response
, 0, sizeof(response
));
911 session
= session_get_by_id(session_id
);
913 DBG("Relay session %" PRIu64
" not found", session_id
);
914 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
918 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
920 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
925 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
927 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
928 &nb_created
, &closed
);
930 goto end_put_session
;
932 /* Only send back the newly created streams with the unsent ones. */
933 nb_streams
= nb_created
+ nb_unsent
;
934 response
.streams_count
= htobe32(nb_streams
);
937 * If the session is closed, HUP when there are no more streams
940 if (closed
&& nb_total
== 0) {
942 response
.streams_count
= 0;
943 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
948 health_code_update();
949 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
951 goto end_put_session
;
953 health_code_update();
956 * Unknown or empty session, just return gracefully, the viewer
957 * knows what is happening.
959 if (!send_streams
|| !nb_streams
) {
961 goto end_put_session
;
965 * Send stream and *DON'T* ignore the sent flag so every viewer
966 * streams that were not sent from that point will be sent to
969 ret
= send_viewer_streams(conn
->sock
, session
, 0);
971 goto end_put_session
;
976 session_put(session
);
983 * Send the viewer the list of current sessions.
986 int viewer_attach_session(struct relay_connection
*conn
)
988 int send_streams
= 0;
990 uint32_t nb_streams
= 0;
991 enum lttng_viewer_seek seek_type
;
992 struct lttng_viewer_attach_session_request request
;
993 struct lttng_viewer_attach_session_response response
;
994 struct relay_session
*session
= NULL
;
999 health_code_update();
1001 /* Receive the request from the connected client. */
1002 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1007 health_code_update();
1009 memset(&response
, 0, sizeof(response
));
1011 if (!conn
->viewer_session
) {
1012 DBG("Client trying to attach before creating a live viewer session");
1013 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1017 session
= session_get_by_id(be64toh(request
.session_id
));
1019 DBG("Relay session %" PRIu64
" not found",
1020 be64toh(request
.session_id
));
1021 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1024 DBG("Attach session ID %" PRIu64
" received",
1025 be64toh(request
.session_id
));
1027 if (session
->live_timer
== 0) {
1028 DBG("Not live session");
1029 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1034 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1036 DBG("Already a viewer attached");
1037 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1041 switch (be32toh(request
.seek
)) {
1042 case LTTNG_VIEWER_SEEK_BEGINNING
:
1043 case LTTNG_VIEWER_SEEK_LAST
:
1044 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1045 seek_type
= be32toh(request
.seek
);
1048 ERR("Wrong seek parameter");
1049 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1054 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1057 goto end_put_session
;
1059 response
.streams_count
= htobe32(nb_streams
);
1062 * If the session is closed when the viewer is attaching, it
1063 * means some of the streams may have been concurrently removed,
1064 * so we don't allow the viewer to attach, even if there are
1065 * streams available.
1069 response
.streams_count
= 0;
1070 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1075 health_code_update();
1076 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1078 goto end_put_session
;
1080 health_code_update();
1083 * Unknown or empty session, just return gracefully, the viewer
1084 * knows what is happening.
1086 if (!send_streams
|| !nb_streams
) {
1088 goto end_put_session
;
1091 /* Send stream and ignore the sent flag. */
1092 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1094 goto end_put_session
;
1099 session_put(session
);
1106 * Open the index file if needed for the given vstream.
1108 * If an index file is successfully opened, the vstream index_fd set with
1111 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1113 * Called with rstream lock held.
1115 static int try_open_index(struct relay_viewer_stream
*vstream
,
1116 struct relay_stream
*rstream
)
1120 if (vstream
->index_fd
) {
1125 * First time, we open the index file and at least one index is ready.
1127 if (rstream
->total_index_received
== 0) {
1131 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1132 vstream
->stream
->tracefile_count
,
1133 vstream
->current_tracefile_id
);
1135 vstream
->index_fd
= stream_fd_create(ret
);
1136 if (!vstream
->index_fd
) {
1152 * Check the status of the index for the given stream. This function
1153 * updates the index structure if needed and can put (close) the vstream
1154 * in the HUP situation.
1156 * Return 0 means that we can proceed with the index. A value of 1 means
1157 * that the index has been updated and is ready to be sent to the
1158 * client. A negative value indicates an error that can't be handled.
1160 * Called with rstream lock held.
1162 static int check_index_status(struct relay_viewer_stream
*vstream
,
1163 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1164 struct lttng_viewer_index
*index
)
1168 if (trace
->session
->connection_closed
1169 && rstream
->total_index_received
1170 == vstream
->last_sent_index
) {
1171 /* Last index sent and session connection is closed. */
1172 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1174 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1175 rstream
->total_index_received
1176 == vstream
->last_sent_index
) {
1178 * We've received a synchronization beacon and the last index
1179 * available has been sent, the index for now is inactive.
1181 * In this case, we have received a beacon which allows us to
1182 * inform the client of a time interval during which we can
1183 * guarantee that there are no events to read (and never will
1186 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1187 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1188 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1190 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
1192 * This actually checks the case where recv == last_sent.
1193 * In this case, we have not received a beacon. Therefore, we
1194 * can only ask the client to retry later.
1196 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1198 } else if (!viewer_stream_is_tracefile_seq_readable(vstream
,
1199 vstream
->current_tracefile_seq
)) {
1201 * The producer has overwritten our current file. We
1204 DBG("Viewer stream %" PRIu64
" rotation due to overwrite",
1205 vstream
->stream
->stream_handle
);
1206 ret
= viewer_stream_rotate(vstream
);
1209 } else if (ret
== 1) {
1210 /* EOF across entire stream. */
1211 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1214 assert(viewer_stream_is_tracefile_seq_readable(vstream
,
1215 vstream
->current_tracefile_seq
));
1216 /* ret == 0 means successful so we continue. */
1223 * Use EOF on current index file to find out when we
1226 read_ret
= lttng_read(vstream
->index_fd
->fd
, tmp
, 1);
1227 if (read_ret
== 1) {
1230 /* There is still data to read. Rewind position. */
1231 seek_ret
= lseek(vstream
->index_fd
->fd
, -1, SEEK_CUR
);
1237 } else if (read_ret
== 0) {
1238 /* EOF. We need to rotate. */
1239 DBG("Viewer stream %" PRIu64
" rotation due to EOF",
1240 vstream
->stream
->stream_handle
);
1241 ret
= viewer_stream_rotate(vstream
);
1244 } else if (ret
== 1) {
1245 /* EOF across entire stream. */
1246 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1249 assert(viewer_stream_is_tracefile_seq_readable(vstream
,
1250 vstream
->current_tracefile_seq
));
1251 /* ret == 0 means successful so we continue. */
1254 /* Error reading index. */
1262 viewer_stream_put(vstream
);
1268 * Send the next index for a stream.
1270 * Return 0 on success or else a negative value.
1273 int viewer_get_next_index(struct relay_connection
*conn
)
1277 struct lttng_viewer_get_next_index request_index
;
1278 struct lttng_viewer_index viewer_index
;
1279 struct ctf_packet_index packet_index
;
1280 struct relay_viewer_stream
*vstream
= NULL
;
1281 struct relay_stream
*rstream
= NULL
;
1282 struct ctf_trace
*ctf_trace
= NULL
;
1283 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1287 DBG("Viewer get next index");
1289 memset(&viewer_index
, 0, sizeof(viewer_index
));
1290 health_code_update();
1292 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1296 health_code_update();
1298 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1300 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1304 /* Use back. ref. Protected by refcounts. */
1305 rstream
= vstream
->stream
;
1306 ctf_trace
= rstream
->trace
;
1308 /* metadata_viewer_stream may be NULL. */
1309 metadata_viewer_stream
=
1310 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1312 pthread_mutex_lock(&rstream
->lock
);
1315 * The viewer should not ask for index on metadata stream.
1317 if (rstream
->is_metadata
) {
1318 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1322 /* Try to open an index if one is needed for that stream. */
1323 ret
= try_open_index(vstream
, rstream
);
1325 if (ret
== -ENOENT
) {
1327 * The index is created only when the first data
1328 * packet arrives, it might not be ready at the
1329 * beginning of the session
1331 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1333 /* Unhandled error. */
1334 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1339 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1342 } else if (ret
== 1) {
1344 * We have no index to send and check_index_status has populated
1345 * viewer_index's status.
1349 /* At this point, ret is 0 thus we will be able to read the index. */
1353 * vstream->stream_fd may be NULL if it has been closed by
1354 * tracefile rotation, or if we are at the beginning of the
1355 * stream. We open the data stream file here to protect against
1356 * overwrite caused by tracefile rotation (in association with
1357 * unlink performed before overwrite).
1359 if (!vstream
->stream_fd
) {
1360 char fullpath
[PATH_MAX
];
1362 if (vstream
->stream
->tracefile_count
> 0) {
1363 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1365 vstream
->channel_name
,
1366 vstream
->current_tracefile_id
);
1368 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1370 vstream
->channel_name
);
1375 ret
= open(fullpath
, O_RDONLY
);
1377 PERROR("Relay opening trace file");
1380 vstream
->stream_fd
= stream_fd_create(ret
);
1381 if (!vstream
->stream_fd
) {
1389 ret
= check_new_streams(conn
);
1391 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1393 } else if (ret
== 1) {
1394 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1397 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1398 sizeof(packet_index
));
1399 if (read_ret
< sizeof(packet_index
)) {
1400 ERR("Relay reading index file %d returned %zd",
1401 vstream
->index_fd
->fd
, read_ret
);
1402 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
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 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1413 rstream
->stream_handle
,
1414 be64toh(packet_index
.offset
));
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
;
1425 pthread_mutex_unlock(&rstream
->lock
);
1428 if (metadata_viewer_stream
) {
1429 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1430 DBG("get next index metadata check: recv %" PRIu64
1432 metadata_viewer_stream
->stream
->metadata_received
,
1433 metadata_viewer_stream
->metadata_sent
);
1434 if (!metadata_viewer_stream
->stream
->metadata_received
||
1435 metadata_viewer_stream
->stream
->metadata_received
>
1436 metadata_viewer_stream
->metadata_sent
) {
1437 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1439 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1442 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1443 health_code_update();
1445 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1449 health_code_update();
1451 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1452 vstream
->last_sent_index
,
1453 vstream
->stream
->stream_handle
);
1455 if (metadata_viewer_stream
) {
1456 viewer_stream_put(metadata_viewer_stream
);
1459 viewer_stream_put(vstream
);
1464 pthread_mutex_unlock(&rstream
->lock
);
1465 if (metadata_viewer_stream
) {
1466 viewer_stream_put(metadata_viewer_stream
);
1468 viewer_stream_put(vstream
);
1473 * Send the next index for a stream
1475 * Return 0 on success or else a negative value.
1478 int viewer_get_packet(struct relay_connection
*conn
)
1480 int ret
, send_data
= 0;
1484 struct lttng_viewer_get_packet get_packet_info
;
1485 struct lttng_viewer_trace_packet reply
;
1486 struct relay_viewer_stream
*vstream
= NULL
;
1488 DBG2("Relay get data packet");
1490 health_code_update();
1492 ret
= recv_request(conn
->sock
, &get_packet_info
,
1493 sizeof(get_packet_info
));
1497 health_code_update();
1499 /* From this point on, the error label can be reached. */
1500 memset(&reply
, 0, sizeof(reply
));
1502 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1504 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1505 goto send_reply_nolock
;
1508 pthread_mutex_lock(&vstream
->stream
->lock
);
1510 len
= be32toh(get_packet_info
.len
);
1511 data
= zmalloc(len
);
1513 PERROR("relay data zmalloc");
1517 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1520 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1521 be64toh(get_packet_info
.offset
));
1524 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1525 if (read_len
< len
) {
1526 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1527 vstream
->stream_fd
->fd
,
1528 be64toh(get_packet_info
.offset
));
1531 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1532 reply
.len
= htobe32(len
);
1537 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1541 pthread_mutex_unlock(&vstream
->stream
->lock
);
1544 reply
.flags
= htobe32(reply
.flags
);
1546 health_code_update();
1548 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1552 health_code_update();
1555 health_code_update();
1556 ret
= send_response(conn
->sock
, data
, len
);
1560 health_code_update();
1563 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1564 be64toh(get_packet_info
.stream_id
));
1570 viewer_stream_put(vstream
);
1576 * Send the session's metadata
1578 * Return 0 on success else a negative value.
1581 int viewer_get_metadata(struct relay_connection
*conn
)
1587 struct lttng_viewer_get_metadata request
;
1588 struct lttng_viewer_metadata_packet reply
;
1589 struct relay_viewer_stream
*vstream
= NULL
;
1593 DBG("Relay get metadata");
1595 health_code_update();
1597 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1601 health_code_update();
1603 memset(&reply
, 0, sizeof(reply
));
1605 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1608 * The metadata stream can be closed by a CLOSE command
1609 * just before we attach. It can also be closed by
1610 * per-pid tracing during tracing. Therefore, it is
1611 * possible that we cannot find this viewer stream.
1612 * Reply back to the client with an error if we cannot
1615 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1618 pthread_mutex_lock(&vstream
->stream
->lock
);
1619 if (!vstream
->stream
->is_metadata
) {
1620 ERR("Invalid metadata stream");
1624 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1626 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1628 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1632 /* first time, we open the metadata file */
1633 if (!vstream
->stream_fd
) {
1634 char fullpath
[PATH_MAX
];
1636 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1637 vstream
->channel_name
);
1641 ret
= open(fullpath
, O_RDONLY
);
1643 PERROR("Relay opening metadata file");
1646 vstream
->stream_fd
= stream_fd_create(ret
);
1647 if (!vstream
->stream_fd
) {
1655 reply
.len
= htobe64(len
);
1656 data
= zmalloc(len
);
1658 PERROR("viewer metadata zmalloc");
1662 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1663 if (read_len
< len
) {
1664 PERROR("Relay reading metadata file");
1667 vstream
->metadata_sent
+= read_len
;
1668 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1669 && vstream
->stream
->closed
) {
1670 /* Release ownership for the viewer metadata stream. */
1671 viewer_stream_put(vstream
);
1674 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1679 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1682 health_code_update();
1684 pthread_mutex_unlock(&vstream
->stream
->lock
);
1686 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1690 health_code_update();
1693 ret
= send_response(conn
->sock
, data
, len
);
1699 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1700 be64toh(request
.stream_id
));
1702 DBG("Metadata sent");
1708 viewer_stream_put(vstream
);
1714 * Create a viewer session.
1716 * Return 0 on success or else a negative value.
1719 int viewer_create_session(struct relay_connection
*conn
)
1722 struct lttng_viewer_create_session_response resp
;
1724 DBG("Viewer create session received");
1726 memset(&resp
, 0, sizeof(resp
));
1727 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1728 conn
->viewer_session
= viewer_session_create();
1729 if (!conn
->viewer_session
) {
1730 ERR("Allocation viewer session");
1731 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1736 health_code_update();
1737 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1741 health_code_update();
1750 * live_relay_unknown_command: send -1 if received unknown command
1753 void live_relay_unknown_command(struct relay_connection
*conn
)
1755 struct lttcomm_relayd_generic_reply reply
;
1757 memset(&reply
, 0, sizeof(reply
));
1758 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1759 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1763 * Process the commands received on the control socket
1766 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1767 struct relay_connection
*conn
)
1772 msg_value
= be32toh(recv_hdr
->cmd
);
1775 * Make sure we've done the version check before any command other then a
1776 * new client connection.
1778 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1779 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1784 switch (msg_value
) {
1785 case LTTNG_VIEWER_CONNECT
:
1786 ret
= viewer_connect(conn
);
1788 case LTTNG_VIEWER_LIST_SESSIONS
:
1789 ret
= viewer_list_sessions(conn
);
1791 case LTTNG_VIEWER_ATTACH_SESSION
:
1792 ret
= viewer_attach_session(conn
);
1794 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1795 ret
= viewer_get_next_index(conn
);
1797 case LTTNG_VIEWER_GET_PACKET
:
1798 ret
= viewer_get_packet(conn
);
1800 case LTTNG_VIEWER_GET_METADATA
:
1801 ret
= viewer_get_metadata(conn
);
1803 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1804 ret
= viewer_get_new_streams(conn
);
1806 case LTTNG_VIEWER_CREATE_SESSION
:
1807 ret
= viewer_create_session(conn
);
1810 ERR("Received unknown viewer command (%u)",
1811 be32toh(recv_hdr
->cmd
));
1812 live_relay_unknown_command(conn
);
1822 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1826 (void) lttng_poll_del(events
, pollfd
);
1828 ret
= close(pollfd
);
1830 ERR("Closing pollfd %d", pollfd
);
1835 * This thread does the actual work
1838 void *thread_worker(void *data
)
1842 struct lttng_poll_event events
;
1843 struct lttng_ht
*viewer_connections_ht
;
1844 struct lttng_ht_iter iter
;
1845 struct lttng_viewer_cmd recv_hdr
;
1846 struct relay_connection
*destroy_conn
;
1848 DBG("[thread] Live viewer relay worker started");
1850 rcu_register_thread();
1852 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1854 if (testpoint(relayd_thread_live_worker
)) {
1855 goto error_testpoint
;
1858 /* table of connections indexed on socket */
1859 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1860 if (!viewer_connections_ht
) {
1861 goto viewer_connections_ht_error
;
1864 ret
= create_thread_poll_set(&events
, 2);
1866 goto error_poll_create
;
1869 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1878 health_code_update();
1880 /* Infinite blocking call, waiting for transmission */
1881 DBG3("Relayd live viewer worker thread polling...");
1882 health_poll_entry();
1883 ret
= lttng_poll_wait(&events
, -1);
1887 * Restart interrupted system call.
1889 if (errno
== EINTR
) {
1898 * Process control. The control connection is prioritised so we don't
1899 * starve it with high throughput tracing data on the data
1902 for (i
= 0; i
< nb_fd
; i
++) {
1903 /* Fetch once the poll data */
1904 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1905 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1907 health_code_update();
1910 /* No activity for this FD (poll implementation). */
1914 /* Thread quit pipe has been closed. Killing thread. */
1915 ret
= check_thread_quit_pipe(pollfd
, revents
);
1921 /* Inspect the relay conn pipe for new connection. */
1922 if (pollfd
== live_conn_pipe
[0]) {
1923 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1924 ERR("Relay live pipe error");
1926 } else if (revents
& LPOLLIN
) {
1927 struct relay_connection
*conn
;
1929 ret
= lttng_read(live_conn_pipe
[0],
1930 &conn
, sizeof(conn
));
1934 lttng_poll_add(&events
, conn
->sock
->fd
,
1935 LPOLLIN
| LPOLLRDHUP
);
1936 connection_ht_add(viewer_connections_ht
, conn
);
1937 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
1940 /* Connection activity. */
1941 struct relay_connection
*conn
;
1943 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
1948 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1949 cleanup_connection_pollfd(&events
, pollfd
);
1950 /* Put "create" ownership reference. */
1951 connection_put(conn
);
1952 } else if (revents
& LPOLLIN
) {
1953 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1954 sizeof(recv_hdr
), 0);
1956 /* Connection closed. */
1957 cleanup_connection_pollfd(&events
, pollfd
);
1958 /* Put "create" ownership reference. */
1959 connection_put(conn
);
1960 DBG("Viewer control conn closed with %d", pollfd
);
1962 ret
= process_control(&recv_hdr
, conn
);
1964 /* Clear the session on error. */
1965 cleanup_connection_pollfd(&events
, pollfd
);
1966 /* Put "create" ownership reference. */
1967 connection_put(conn
);
1968 DBG("Viewer connection closed with %d", pollfd
);
1972 /* Put local "get_by_sock" reference. */
1973 connection_put(conn
);
1980 lttng_poll_clean(&events
);
1982 /* Cleanup reamaining connection object. */
1984 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
1987 health_code_update();
1988 connection_put(destroy_conn
);
1992 lttng_ht_destroy(viewer_connections_ht
);
1993 viewer_connections_ht_error
:
1994 /* Close relay conn pipes */
1995 utils_close_pipe(live_conn_pipe
);
1997 DBG("Viewer worker thread exited with error");
1999 DBG("Viewer worker thread cleanup complete");
2003 ERR("Health error occurred in %s", __func__
);
2005 health_unregister(health_relayd
);
2006 if (lttng_relay_stop_threads()) {
2007 ERR("Error stopping threads");
2009 rcu_unregister_thread();
2014 * Create the relay command pipe to wake thread_manage_apps.
2015 * Closed in cleanup().
2017 static int create_conn_pipe(void)
2019 return utils_create_pipe_cloexec(live_conn_pipe
);
2022 int relayd_live_join(void)
2024 int ret
, retval
= 0;
2027 ret
= pthread_join(live_listener_thread
, &status
);
2030 PERROR("pthread_join live listener");
2034 ret
= pthread_join(live_worker_thread
, &status
);
2037 PERROR("pthread_join live worker");
2041 ret
= pthread_join(live_dispatcher_thread
, &status
);
2044 PERROR("pthread_join live dispatcher");
2048 cleanup_relayd_live();
2056 int relayd_live_create(struct lttng_uri
*uri
)
2058 int ret
= 0, retval
= 0;
2064 goto exit_init_data
;
2068 /* Check if daemon is UID = 0 */
2069 is_root
= !getuid();
2072 if (live_uri
->port
< 1024) {
2073 ERR("Need to be root to use ports < 1024");
2075 goto exit_init_data
;
2079 /* Setup the thread apps communication pipe. */
2080 if (create_conn_pipe()) {
2082 goto exit_init_data
;
2085 /* Init relay command queue. */
2086 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2088 /* Set up max poll set size */
2089 if (lttng_poll_set_max_size()) {
2091 goto exit_init_data
;
2094 /* Setup the dispatcher thread */
2095 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2096 thread_dispatcher
, (void *) NULL
);
2099 PERROR("pthread_create viewer dispatcher");
2101 goto exit_dispatcher_thread
;
2104 /* Setup the worker thread */
2105 ret
= pthread_create(&live_worker_thread
, NULL
,
2106 thread_worker
, NULL
);
2109 PERROR("pthread_create viewer worker");
2111 goto exit_worker_thread
;
2114 /* Setup the listener thread */
2115 ret
= pthread_create(&live_listener_thread
, NULL
,
2116 thread_listener
, (void *) NULL
);
2119 PERROR("pthread_create viewer listener");
2121 goto exit_listener_thread
;
2125 * All OK, started all threads.
2130 * Join on the live_listener_thread should anything be added after
2131 * the live_listener thread's creation.
2134 exit_listener_thread
:
2136 ret
= pthread_join(live_worker_thread
, &status
);
2139 PERROR("pthread_join live worker");
2144 ret
= pthread_join(live_dispatcher_thread
, &status
);
2147 PERROR("pthread_join live dispatcher");
2150 exit_dispatcher_thread
:
2153 cleanup_relayd_live();