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 lock.
312 if (!stream
->published
) {
315 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
317 vstream
= viewer_stream_create(stream
, seek_t
);
320 ctf_trace_put(ctf_trace
);
326 /* Update number of created stream counter. */
330 * Ensure a self-reference is preserved even
331 * after we have put our local reference.
333 viewer_stream_get(vstream
);
335 if (!vstream
->sent_flag
&& nb_unsent
) {
336 /* Update number of unsent stream counter. */
340 /* Update number of total stream counter. */
342 if (stream
->is_metadata
) {
343 if (!stream
->closed
||
344 stream
->metadata_received
> vstream
->metadata_sent
) {
348 if (!stream
->closed
||
349 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
355 /* Put local reference. */
356 viewer_stream_put(vstream
);
360 ctf_trace_put(ctf_trace
);
367 pthread_mutex_unlock(&session
->lock
);
371 int relayd_live_stop(void)
373 /* Stop dispatch thread */
374 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
375 futex_nto1_wake(&viewer_conn_queue
.futex
);
380 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
383 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
387 if (events
== NULL
|| size
== 0) {
392 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
398 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
410 * Check if the thread quit pipe was triggered.
412 * Return 1 if it was triggered else 0;
415 int check_thread_quit_pipe(int fd
, uint32_t events
)
417 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
425 * Create and init socket from uri.
428 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
431 struct lttcomm_sock
*sock
= NULL
;
433 sock
= lttcomm_alloc_sock_from_uri(uri
);
435 ERR("Allocating socket");
439 ret
= lttcomm_create_sock(sock
);
443 DBG("Listening on sock %d for live", sock
->fd
);
445 ret
= sock
->ops
->bind(sock
);
450 ret
= sock
->ops
->listen(sock
, -1);
460 lttcomm_destroy_sock(sock
);
466 * This thread manages the listening for new connections on the network
469 void *thread_listener(void *data
)
471 int i
, ret
, pollfd
, err
= -1;
472 uint32_t revents
, nb_fd
;
473 struct lttng_poll_event events
;
474 struct lttcomm_sock
*live_control_sock
;
476 DBG("[thread] Relay live listener started");
478 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
480 health_code_update();
482 live_control_sock
= init_socket(live_uri
);
483 if (!live_control_sock
) {
484 goto error_sock_control
;
487 /* Pass 2 as size here for the thread quit pipe and control sockets. */
488 ret
= create_thread_poll_set(&events
, 2);
490 goto error_create_poll
;
493 /* Add the control socket */
494 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
499 lttng_relay_notify_ready();
501 if (testpoint(relayd_thread_live_listener
)) {
502 goto error_testpoint
;
506 health_code_update();
508 DBG("Listener accepting live viewers connections");
512 ret
= lttng_poll_wait(&events
, -1);
516 * Restart interrupted system call.
518 if (errno
== EINTR
) {
525 DBG("Relay new viewer connection received");
526 for (i
= 0; i
< nb_fd
; i
++) {
527 health_code_update();
529 /* Fetch once the poll data */
530 revents
= LTTNG_POLL_GETEV(&events
, i
);
531 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
534 /* No activity for this FD (poll implementation). */
538 /* Thread quit pipe has been closed. Killing thread. */
539 ret
= check_thread_quit_pipe(pollfd
, revents
);
545 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
546 ERR("socket poll error");
548 } else if (revents
& LPOLLIN
) {
550 * A new connection is requested, therefore a
551 * viewer connection is allocated in this
552 * thread, enqueued to a global queue and
553 * dequeued (and freed) in the worker thread.
556 struct relay_connection
*new_conn
;
557 struct lttcomm_sock
*newsock
;
559 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
561 PERROR("accepting control sock");
564 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
566 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
569 PERROR("setsockopt inet");
570 lttcomm_destroy_sock(newsock
);
573 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
575 lttcomm_destroy_sock(newsock
);
578 /* Ownership assumed by the connection. */
581 /* Enqueue request for the dispatcher thread. */
582 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
586 * Wake the dispatch queue futex.
587 * Implicit memory barrier with the
588 * exchange in cds_wfcq_enqueue.
590 futex_nto1_wake(&viewer_conn_queue
.futex
);
599 lttng_poll_clean(&events
);
601 if (live_control_sock
->fd
>= 0) {
602 ret
= live_control_sock
->ops
->close(live_control_sock
);
607 lttcomm_destroy_sock(live_control_sock
);
611 DBG("Live viewer listener thread exited with error");
613 health_unregister(health_relayd
);
614 DBG("Live viewer listener thread cleanup complete");
615 if (lttng_relay_stop_threads()) {
616 ERR("Error stopping threads");
622 * This thread manages the dispatching of the requests to worker threads
625 void *thread_dispatcher(void *data
)
629 struct cds_wfcq_node
*node
;
630 struct relay_connection
*conn
= NULL
;
632 DBG("[thread] Live viewer relay dispatcher started");
634 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
636 if (testpoint(relayd_thread_live_dispatcher
)) {
637 goto error_testpoint
;
640 health_code_update();
642 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
643 health_code_update();
645 /* Atomically prepare the queue futex */
646 futex_nto1_prepare(&viewer_conn_queue
.futex
);
649 health_code_update();
651 /* Dequeue commands */
652 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
653 &viewer_conn_queue
.tail
);
655 DBG("Woken up but nothing in the live-viewer "
656 "relay command queue");
657 /* Continue thread execution */
660 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
661 DBG("Dispatching viewer request waiting on sock %d",
665 * Inform worker thread of the new request. This
666 * call is blocking so we can be assured that
667 * the data will be read at some point in time
668 * or wait to the end of the world :)
670 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
672 PERROR("write conn pipe");
673 connection_put(conn
);
676 } while (node
!= NULL
);
678 /* Futex wait on queue. Blocking call on futex() */
680 futex_nto1_wait(&viewer_conn_queue
.futex
);
684 /* Normal exit, no error */
691 ERR("Health error occurred in %s", __func__
);
693 health_unregister(health_relayd
);
694 DBG("Live viewer dispatch thread dying");
695 if (lttng_relay_stop_threads()) {
696 ERR("Error stopping threads");
702 * Establish connection with the viewer and check the versions.
704 * Return 0 on success or else negative value.
707 int viewer_connect(struct relay_connection
*conn
)
710 struct lttng_viewer_connect reply
, msg
;
712 conn
->version_check_done
= 1;
714 health_code_update();
716 DBG("Viewer is establishing a connection to the relayd.");
718 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
723 health_code_update();
725 memset(&reply
, 0, sizeof(reply
));
726 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
727 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
729 /* Major versions must be the same */
730 if (reply
.major
!= be32toh(msg
.major
)) {
731 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
732 reply
.major
, be32toh(msg
.major
));
737 conn
->major
= reply
.major
;
738 /* We adapt to the lowest compatible version */
739 if (reply
.minor
<= be32toh(msg
.minor
)) {
740 conn
->minor
= reply
.minor
;
742 conn
->minor
= be32toh(msg
.minor
);
745 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
746 conn
->type
= RELAY_VIEWER_COMMAND
;
747 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
748 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
750 ERR("Unknown connection type : %u", be32toh(msg
.type
));
755 reply
.major
= htobe32(reply
.major
);
756 reply
.minor
= htobe32(reply
.minor
);
757 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
759 * Increment outside of htobe64 macro, because the argument can
760 * be used more than once within the macro, and thus the
761 * operation may be undefined.
763 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
764 last_relay_viewer_session_id
++;
765 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
766 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
769 health_code_update();
771 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
776 health_code_update();
778 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
786 * Send the viewer the list of current sessions.
787 * We need to create a copy of the hash table content because otherwise
788 * we cannot assume the number of entries stays the same between getting
789 * the number of HT elements and iteration over the HT.
791 * Return 0 on success or else a negative value.
794 int viewer_list_sessions(struct relay_connection
*conn
)
797 struct lttng_viewer_list_sessions session_list
;
798 struct lttng_ht_iter iter
;
799 struct relay_session
*session
;
800 struct lttng_viewer_session
*send_session_buf
= NULL
;
801 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
804 DBG("List sessions received");
806 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
807 if (!send_session_buf
) {
812 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
814 struct lttng_viewer_session
*send_session
;
816 health_code_update();
818 if (count
>= buf_count
) {
819 struct lttng_viewer_session
*newbuf
;
820 uint32_t new_buf_count
= buf_count
<< 1;
822 newbuf
= realloc(send_session_buf
,
823 new_buf_count
* sizeof(*send_session_buf
));
829 send_session_buf
= newbuf
;
830 buf_count
= new_buf_count
;
832 send_session
= &send_session_buf
[count
];
833 strncpy(send_session
->session_name
, session
->session_name
,
834 sizeof(send_session
->session_name
));
835 strncpy(send_session
->hostname
, session
->hostname
,
836 sizeof(send_session
->hostname
));
837 send_session
->id
= htobe64(session
->id
);
838 send_session
->live_timer
= htobe32(session
->live_timer
);
839 if (session
->viewer_attached
) {
840 send_session
->clients
= htobe32(1);
842 send_session
->clients
= htobe32(0);
844 send_session
->streams
= htobe32(session
->stream_count
);
849 session_list
.sessions_count
= htobe32(count
);
851 health_code_update();
853 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
858 health_code_update();
860 ret
= send_response(conn
->sock
, send_session_buf
,
861 count
* sizeof(*send_session_buf
));
865 health_code_update();
869 free(send_session_buf
);
874 * Send the viewer the list of current streams.
877 int viewer_get_new_streams(struct relay_connection
*conn
)
879 int ret
, send_streams
= 0;
880 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
881 struct lttng_viewer_new_streams_request request
;
882 struct lttng_viewer_new_streams_response response
;
883 struct relay_session
*session
;
889 DBG("Get new streams received");
891 health_code_update();
893 /* Receive the request from the connected client. */
894 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
898 session_id
= be64toh(request
.session_id
);
900 health_code_update();
902 memset(&response
, 0, sizeof(response
));
904 session
= session_get_by_id(session_id
);
906 DBG("Relay session %" PRIu64
" not found", session_id
);
907 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
911 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
913 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
918 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
920 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
921 &nb_created
, &closed
);
923 goto end_put_session
;
925 /* Only send back the newly created streams with the unsent ones. */
926 nb_streams
= nb_created
+ nb_unsent
;
927 response
.streams_count
= htobe32(nb_streams
);
930 * If the session is closed, HUP when there are no more streams
933 if (closed
&& nb_total
== 0) {
935 response
.streams_count
= 0;
936 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
941 health_code_update();
942 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
944 goto end_put_session
;
946 health_code_update();
949 * Unknown or empty session, just return gracefully, the viewer
950 * knows what is happening.
952 if (!send_streams
|| !nb_streams
) {
954 goto end_put_session
;
958 * Send stream and *DON'T* ignore the sent flag so every viewer
959 * streams that were not sent from that point will be sent to
962 ret
= send_viewer_streams(conn
->sock
, session
, 0);
964 goto end_put_session
;
969 session_put(session
);
976 * Send the viewer the list of current sessions.
979 int viewer_attach_session(struct relay_connection
*conn
)
981 int send_streams
= 0;
983 uint32_t nb_streams
= 0;
984 enum lttng_viewer_seek seek_type
;
985 struct lttng_viewer_attach_session_request request
;
986 struct lttng_viewer_attach_session_response response
;
987 struct relay_session
*session
= NULL
;
992 health_code_update();
994 /* Receive the request from the connected client. */
995 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1000 health_code_update();
1002 memset(&response
, 0, sizeof(response
));
1004 if (!conn
->viewer_session
) {
1005 DBG("Client trying to attach before creating a live viewer session");
1006 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1010 session
= session_get_by_id(be64toh(request
.session_id
));
1012 DBG("Relay session %" PRIu64
" not found",
1013 be64toh(request
.session_id
));
1014 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1017 DBG("Attach session ID %" PRIu64
" received",
1018 be64toh(request
.session_id
));
1020 if (session
->live_timer
== 0) {
1021 DBG("Not live session");
1022 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1027 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1029 DBG("Already a viewer attached");
1030 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1034 switch (be32toh(request
.seek
)) {
1035 case LTTNG_VIEWER_SEEK_BEGINNING
:
1036 case LTTNG_VIEWER_SEEK_LAST
:
1037 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1038 seek_type
= be32toh(request
.seek
);
1041 ERR("Wrong seek parameter");
1042 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1047 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1050 goto end_put_session
;
1052 response
.streams_count
= htobe32(nb_streams
);
1055 * If the session is closed when the viewer is attaching, it
1056 * means some of the streams may have been concurrently removed,
1057 * so we don't allow the viewer to attach, even if there are
1058 * streams available.
1062 response
.streams_count
= 0;
1063 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1068 health_code_update();
1069 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1071 goto end_put_session
;
1073 health_code_update();
1076 * Unknown or empty session, just return gracefully, the viewer
1077 * knows what is happening.
1079 if (!send_streams
|| !nb_streams
) {
1081 goto end_put_session
;
1084 /* Send stream and ignore the sent flag. */
1085 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1087 goto end_put_session
;
1092 session_put(session
);
1099 * Open the index file if needed for the given vstream.
1101 * If an index file is successfully opened, the vstream index_fd set with
1104 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1106 * Called with rstream lock held.
1108 static int try_open_index(struct relay_viewer_stream
*vstream
,
1109 struct relay_stream
*rstream
)
1113 if (vstream
->index_fd
) {
1118 * First time, we open the index file and at least one index is ready.
1120 if (rstream
->index_received_seqcount
== 0) {
1124 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1125 vstream
->stream
->tracefile_count
,
1126 vstream
->current_tracefile_id
);
1128 vstream
->index_fd
= stream_fd_create(ret
);
1129 if (!vstream
->index_fd
) {
1145 * Check the status of the index for the given stream. This function
1146 * updates the index structure if needed and can put (close) the vstream
1147 * in the HUP situation.
1149 * Return 0 means that we can proceed with the index. A value of 1 means
1150 * that the index has been updated and is ready to be sent to the
1151 * client. A negative value indicates an error that can't be handled.
1153 * Called with rstream lock held.
1155 static int check_index_status(struct relay_viewer_stream
*vstream
,
1156 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1157 struct lttng_viewer_index
*index
)
1161 if (trace
->session
->connection_closed
1162 && rstream
->index_received_seqcount
1163 == vstream
->index_sent_seqcount
) {
1164 /* Last index sent and session connection is closed. */
1165 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1167 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1168 rstream
->index_received_seqcount
1169 == vstream
->index_sent_seqcount
) {
1171 * We've received a synchronization beacon and the last index
1172 * available has been sent, the index for now is inactive.
1174 * In this case, we have received a beacon which allows us to
1175 * inform the client of a time interval during which we can
1176 * guarantee that there are no events to read (and never will
1179 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1180 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1181 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1183 } else if (rstream
->index_received_seqcount
1184 == vstream
->index_sent_seqcount
) {
1186 * This checks whether received == sent seqcount. In
1187 * this case, we have not received a beacon. Therefore,
1188 * we can only ask the client to retry later.
1190 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1192 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1193 vstream
->current_tracefile_id
,
1194 vstream
->index_sent_seqcount
)) {
1196 * The next index we want to send cannot be read either
1197 * because we need to perform a rotation, or due to
1198 * the producer having overwritten its trace file.
1200 DBG("Viewer stream %" PRIu64
" rotation",
1201 vstream
->stream
->stream_handle
);
1202 ret
= viewer_stream_rotate(vstream
);
1205 } else if (ret
== 1) {
1206 /* EOF across entire stream. */
1207 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1211 * If we have been pushed due to overwrite, it
1212 * necessarily means there is data that can be read in
1213 * the stream. If we rotated because we reached the end
1214 * of a tracefile, it means the following tracefile
1215 * needs to contain at least one index, else we would
1216 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1217 * viewer. The updated index_sent_seqcount needs to
1218 * point to a readable index entry now.
1220 * In the case where we "rotate" on a single file, we
1221 * can end up in a case where the requested index is
1222 * still unavailable.
1224 if (rstream
->tracefile_count
== 1 &&
1225 !tracefile_array_seq_in_file(
1227 vstream
->current_tracefile_id
,
1228 vstream
->index_sent_seqcount
)) {
1229 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1232 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1233 vstream
->current_tracefile_id
,
1234 vstream
->index_sent_seqcount
));
1236 /* ret == 0 means successful so we continue. */
1242 viewer_stream_put(vstream
);
1248 * Send the next index for a stream.
1250 * Return 0 on success or else a negative value.
1253 int viewer_get_next_index(struct relay_connection
*conn
)
1257 struct lttng_viewer_get_next_index request_index
;
1258 struct lttng_viewer_index viewer_index
;
1259 struct ctf_packet_index packet_index
;
1260 struct relay_viewer_stream
*vstream
= NULL
;
1261 struct relay_stream
*rstream
= NULL
;
1262 struct ctf_trace
*ctf_trace
= NULL
;
1263 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1267 DBG("Viewer get next index");
1269 memset(&viewer_index
, 0, sizeof(viewer_index
));
1270 health_code_update();
1272 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1276 health_code_update();
1278 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1280 DBG("Client requested index of unknown stream id %" PRIu64
,
1281 be64toh(request_index
.stream_id
));
1282 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1286 /* Use back. ref. Protected by refcounts. */
1287 rstream
= vstream
->stream
;
1288 ctf_trace
= rstream
->trace
;
1290 /* metadata_viewer_stream may be NULL. */
1291 metadata_viewer_stream
=
1292 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1294 pthread_mutex_lock(&rstream
->lock
);
1297 * The viewer should not ask for index on metadata stream.
1299 if (rstream
->is_metadata
) {
1300 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1304 /* Try to open an index if one is needed for that stream. */
1305 ret
= try_open_index(vstream
, rstream
);
1307 if (ret
== -ENOENT
) {
1309 * The index is created only when the first data
1310 * packet arrives, it might not be ready at the
1311 * beginning of the session
1313 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1315 /* Unhandled error. */
1316 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1321 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1324 } else if (ret
== 1) {
1326 * We have no index to send and check_index_status has populated
1327 * viewer_index's status.
1331 /* At this point, ret is 0 thus we will be able to read the index. */
1335 * vstream->stream_fd may be NULL if it has been closed by
1336 * tracefile rotation, or if we are at the beginning of the
1337 * stream. We open the data stream file here to protect against
1338 * overwrite caused by tracefile rotation (in association with
1339 * unlink performed before overwrite).
1341 if (!vstream
->stream_fd
) {
1342 char fullpath
[PATH_MAX
];
1344 if (vstream
->stream
->tracefile_count
> 0) {
1345 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1347 vstream
->channel_name
,
1348 vstream
->current_tracefile_id
);
1350 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1352 vstream
->channel_name
);
1357 ret
= open(fullpath
, O_RDONLY
);
1359 PERROR("Relay opening trace file");
1362 vstream
->stream_fd
= stream_fd_create(ret
);
1363 if (!vstream
->stream_fd
) {
1371 ret
= check_new_streams(conn
);
1373 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1375 } else if (ret
== 1) {
1376 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1379 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1380 sizeof(packet_index
));
1381 if (read_ret
< sizeof(packet_index
)) {
1382 ERR("Relay reading index file %d returned %zd",
1383 vstream
->index_fd
->fd
, read_ret
);
1384 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1387 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1388 vstream
->index_sent_seqcount
++;
1392 * Indexes are stored in big endian, no need to switch before sending.
1394 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1395 rstream
->stream_handle
,
1396 be64toh(packet_index
.offset
));
1397 viewer_index
.offset
= packet_index
.offset
;
1398 viewer_index
.packet_size
= packet_index
.packet_size
;
1399 viewer_index
.content_size
= packet_index
.content_size
;
1400 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1401 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1402 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1403 viewer_index
.stream_id
= packet_index
.stream_id
;
1407 pthread_mutex_unlock(&rstream
->lock
);
1410 if (metadata_viewer_stream
) {
1411 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1412 DBG("get next index metadata check: recv %" PRIu64
1414 metadata_viewer_stream
->stream
->metadata_received
,
1415 metadata_viewer_stream
->metadata_sent
);
1416 if (!metadata_viewer_stream
->stream
->metadata_received
||
1417 metadata_viewer_stream
->stream
->metadata_received
>
1418 metadata_viewer_stream
->metadata_sent
) {
1419 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1421 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
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();
1434 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1435 vstream
->index_sent_seqcount
,
1436 vstream
->stream
->stream_handle
);
1439 if (metadata_viewer_stream
) {
1440 viewer_stream_put(metadata_viewer_stream
);
1443 viewer_stream_put(vstream
);
1448 pthread_mutex_unlock(&rstream
->lock
);
1449 if (metadata_viewer_stream
) {
1450 viewer_stream_put(metadata_viewer_stream
);
1452 viewer_stream_put(vstream
);
1457 * Send the next index for a stream
1459 * Return 0 on success or else a negative value.
1462 int viewer_get_packet(struct relay_connection
*conn
)
1464 int ret
, send_data
= 0;
1468 struct lttng_viewer_get_packet get_packet_info
;
1469 struct lttng_viewer_trace_packet reply
;
1470 struct relay_viewer_stream
*vstream
= NULL
;
1472 DBG2("Relay get data packet");
1474 health_code_update();
1476 ret
= recv_request(conn
->sock
, &get_packet_info
,
1477 sizeof(get_packet_info
));
1481 health_code_update();
1483 /* From this point on, the error label can be reached. */
1484 memset(&reply
, 0, sizeof(reply
));
1486 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1488 DBG("Client requested packet of unknown stream id %" PRIu64
,
1489 be64toh(get_packet_info
.stream_id
));
1490 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1491 goto send_reply_nolock
;
1494 pthread_mutex_lock(&vstream
->stream
->lock
);
1496 len
= be32toh(get_packet_info
.len
);
1497 data
= zmalloc(len
);
1499 PERROR("relay data zmalloc");
1503 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1506 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1507 be64toh(get_packet_info
.offset
));
1510 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1511 if (read_len
< len
) {
1512 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1513 vstream
->stream_fd
->fd
,
1514 be64toh(get_packet_info
.offset
));
1517 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1518 reply
.len
= htobe32(len
);
1523 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1527 pthread_mutex_unlock(&vstream
->stream
->lock
);
1530 reply
.flags
= htobe32(reply
.flags
);
1532 health_code_update();
1534 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1538 health_code_update();
1541 health_code_update();
1542 ret
= send_response(conn
->sock
, data
, len
);
1546 health_code_update();
1549 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1550 be64toh(get_packet_info
.stream_id
));
1556 viewer_stream_put(vstream
);
1562 * Send the session's metadata
1564 * Return 0 on success else a negative value.
1567 int viewer_get_metadata(struct relay_connection
*conn
)
1573 struct lttng_viewer_get_metadata request
;
1574 struct lttng_viewer_metadata_packet reply
;
1575 struct relay_viewer_stream
*vstream
= NULL
;
1579 DBG("Relay get metadata");
1581 health_code_update();
1583 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1587 health_code_update();
1589 memset(&reply
, 0, sizeof(reply
));
1591 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1594 * The metadata stream can be closed by a CLOSE command
1595 * just before we attach. It can also be closed by
1596 * per-pid tracing during tracing. Therefore, it is
1597 * possible that we cannot find this viewer stream.
1598 * Reply back to the client with an error if we cannot
1601 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1602 be64toh(request
.stream_id
));
1603 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1606 pthread_mutex_lock(&vstream
->stream
->lock
);
1607 if (!vstream
->stream
->is_metadata
) {
1608 ERR("Invalid metadata stream");
1612 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1614 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1616 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1620 /* first time, we open the metadata file */
1621 if (!vstream
->stream_fd
) {
1622 char fullpath
[PATH_MAX
];
1624 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1625 vstream
->channel_name
);
1629 ret
= open(fullpath
, O_RDONLY
);
1631 PERROR("Relay opening metadata file");
1634 vstream
->stream_fd
= stream_fd_create(ret
);
1635 if (!vstream
->stream_fd
) {
1643 reply
.len
= htobe64(len
);
1644 data
= zmalloc(len
);
1646 PERROR("viewer metadata zmalloc");
1650 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1651 if (read_len
< len
) {
1652 PERROR("Relay reading metadata file");
1655 vstream
->metadata_sent
+= read_len
;
1656 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1657 && vstream
->stream
->closed
) {
1658 /* Release ownership for the viewer metadata stream. */
1659 viewer_stream_put(vstream
);
1662 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1667 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1670 health_code_update();
1672 pthread_mutex_unlock(&vstream
->stream
->lock
);
1674 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1678 health_code_update();
1681 ret
= send_response(conn
->sock
, data
, len
);
1687 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1688 be64toh(request
.stream_id
));
1690 DBG("Metadata sent");
1696 viewer_stream_put(vstream
);
1702 * Create a viewer session.
1704 * Return 0 on success or else a negative value.
1707 int viewer_create_session(struct relay_connection
*conn
)
1710 struct lttng_viewer_create_session_response resp
;
1712 DBG("Viewer create session received");
1714 memset(&resp
, 0, sizeof(resp
));
1715 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1716 conn
->viewer_session
= viewer_session_create();
1717 if (!conn
->viewer_session
) {
1718 ERR("Allocation viewer session");
1719 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1724 health_code_update();
1725 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1729 health_code_update();
1738 * live_relay_unknown_command: send -1 if received unknown command
1741 void live_relay_unknown_command(struct relay_connection
*conn
)
1743 struct lttcomm_relayd_generic_reply reply
;
1745 memset(&reply
, 0, sizeof(reply
));
1746 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1747 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1751 * Process the commands received on the control socket
1754 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1755 struct relay_connection
*conn
)
1760 msg_value
= be32toh(recv_hdr
->cmd
);
1763 * Make sure we've done the version check before any command other then a
1764 * new client connection.
1766 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1767 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1772 switch (msg_value
) {
1773 case LTTNG_VIEWER_CONNECT
:
1774 ret
= viewer_connect(conn
);
1776 case LTTNG_VIEWER_LIST_SESSIONS
:
1777 ret
= viewer_list_sessions(conn
);
1779 case LTTNG_VIEWER_ATTACH_SESSION
:
1780 ret
= viewer_attach_session(conn
);
1782 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1783 ret
= viewer_get_next_index(conn
);
1785 case LTTNG_VIEWER_GET_PACKET
:
1786 ret
= viewer_get_packet(conn
);
1788 case LTTNG_VIEWER_GET_METADATA
:
1789 ret
= viewer_get_metadata(conn
);
1791 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1792 ret
= viewer_get_new_streams(conn
);
1794 case LTTNG_VIEWER_CREATE_SESSION
:
1795 ret
= viewer_create_session(conn
);
1798 ERR("Received unknown viewer command (%u)",
1799 be32toh(recv_hdr
->cmd
));
1800 live_relay_unknown_command(conn
);
1810 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1814 (void) lttng_poll_del(events
, pollfd
);
1816 ret
= close(pollfd
);
1818 ERR("Closing pollfd %d", pollfd
);
1823 * This thread does the actual work
1826 void *thread_worker(void *data
)
1830 struct lttng_poll_event events
;
1831 struct lttng_ht
*viewer_connections_ht
;
1832 struct lttng_ht_iter iter
;
1833 struct lttng_viewer_cmd recv_hdr
;
1834 struct relay_connection
*destroy_conn
;
1836 DBG("[thread] Live viewer relay worker started");
1838 rcu_register_thread();
1840 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1842 if (testpoint(relayd_thread_live_worker
)) {
1843 goto error_testpoint
;
1846 /* table of connections indexed on socket */
1847 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1848 if (!viewer_connections_ht
) {
1849 goto viewer_connections_ht_error
;
1852 ret
= create_thread_poll_set(&events
, 2);
1854 goto error_poll_create
;
1857 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1866 health_code_update();
1868 /* Infinite blocking call, waiting for transmission */
1869 DBG3("Relayd live viewer worker thread polling...");
1870 health_poll_entry();
1871 ret
= lttng_poll_wait(&events
, -1);
1875 * Restart interrupted system call.
1877 if (errno
== EINTR
) {
1886 * Process control. The control connection is prioritised so we don't
1887 * starve it with high throughput tracing data on the data
1890 for (i
= 0; i
< nb_fd
; i
++) {
1891 /* Fetch once the poll data */
1892 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1893 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1895 health_code_update();
1898 /* No activity for this FD (poll implementation). */
1902 /* Thread quit pipe has been closed. Killing thread. */
1903 ret
= check_thread_quit_pipe(pollfd
, revents
);
1909 /* Inspect the relay conn pipe for new connection. */
1910 if (pollfd
== live_conn_pipe
[0]) {
1911 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1912 ERR("Relay live pipe error");
1914 } else if (revents
& LPOLLIN
) {
1915 struct relay_connection
*conn
;
1917 ret
= lttng_read(live_conn_pipe
[0],
1918 &conn
, sizeof(conn
));
1922 lttng_poll_add(&events
, conn
->sock
->fd
,
1923 LPOLLIN
| LPOLLRDHUP
);
1924 connection_ht_add(viewer_connections_ht
, conn
);
1925 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
1928 /* Connection activity. */
1929 struct relay_connection
*conn
;
1931 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
1936 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1937 cleanup_connection_pollfd(&events
, pollfd
);
1938 /* Put "create" ownership reference. */
1939 connection_put(conn
);
1940 } else if (revents
& LPOLLIN
) {
1941 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1942 sizeof(recv_hdr
), 0);
1944 /* Connection closed. */
1945 cleanup_connection_pollfd(&events
, pollfd
);
1946 /* Put "create" ownership reference. */
1947 connection_put(conn
);
1948 DBG("Viewer control conn closed with %d", pollfd
);
1950 ret
= process_control(&recv_hdr
, conn
);
1952 /* Clear the session on error. */
1953 cleanup_connection_pollfd(&events
, pollfd
);
1954 /* Put "create" ownership reference. */
1955 connection_put(conn
);
1956 DBG("Viewer connection closed with %d", pollfd
);
1960 /* Put local "get_by_sock" reference. */
1961 connection_put(conn
);
1968 lttng_poll_clean(&events
);
1970 /* Cleanup reamaining connection object. */
1972 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
1975 health_code_update();
1976 connection_put(destroy_conn
);
1980 lttng_ht_destroy(viewer_connections_ht
);
1981 viewer_connections_ht_error
:
1982 /* Close relay conn pipes */
1983 utils_close_pipe(live_conn_pipe
);
1985 DBG("Viewer worker thread exited with error");
1987 DBG("Viewer worker thread cleanup complete");
1991 ERR("Health error occurred in %s", __func__
);
1993 health_unregister(health_relayd
);
1994 if (lttng_relay_stop_threads()) {
1995 ERR("Error stopping threads");
1997 rcu_unregister_thread();
2002 * Create the relay command pipe to wake thread_manage_apps.
2003 * Closed in cleanup().
2005 static int create_conn_pipe(void)
2007 return utils_create_pipe_cloexec(live_conn_pipe
);
2010 int relayd_live_join(void)
2012 int ret
, retval
= 0;
2015 ret
= pthread_join(live_listener_thread
, &status
);
2018 PERROR("pthread_join live listener");
2022 ret
= pthread_join(live_worker_thread
, &status
);
2025 PERROR("pthread_join live worker");
2029 ret
= pthread_join(live_dispatcher_thread
, &status
);
2032 PERROR("pthread_join live dispatcher");
2036 cleanup_relayd_live();
2044 int relayd_live_create(struct lttng_uri
*uri
)
2046 int ret
= 0, retval
= 0;
2052 goto exit_init_data
;
2056 /* Check if daemon is UID = 0 */
2057 is_root
= !getuid();
2060 if (live_uri
->port
< 1024) {
2061 ERR("Need to be root to use ports < 1024");
2063 goto exit_init_data
;
2067 /* Setup the thread apps communication pipe. */
2068 if (create_conn_pipe()) {
2070 goto exit_init_data
;
2073 /* Init relay command queue. */
2074 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2076 /* Set up max poll set size */
2077 if (lttng_poll_set_max_size()) {
2079 goto exit_init_data
;
2082 /* Setup the dispatcher thread */
2083 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2084 thread_dispatcher
, (void *) NULL
);
2087 PERROR("pthread_create viewer dispatcher");
2089 goto exit_dispatcher_thread
;
2092 /* Setup the worker thread */
2093 ret
= pthread_create(&live_worker_thread
, NULL
,
2094 thread_worker
, NULL
);
2097 PERROR("pthread_create viewer worker");
2099 goto exit_worker_thread
;
2102 /* Setup the listener thread */
2103 ret
= pthread_create(&live_listener_thread
, NULL
,
2104 thread_listener
, (void *) NULL
);
2107 PERROR("pthread_create viewer listener");
2109 goto exit_listener_thread
;
2113 * All OK, started all threads.
2118 * Join on the live_listener_thread should anything be added after
2119 * the live_listener thread's creation.
2122 exit_listener_thread
:
2124 ret
= pthread_join(live_worker_thread
, &status
);
2127 PERROR("pthread_join live worker");
2132 ret
= pthread_join(live_dispatcher_thread
, &status
);
2135 PERROR("pthread_join live dispatcher");
2138 exit_dispatcher_thread
:
2141 cleanup_relayd_live();