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.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/rculist.h>
44 #include <lttng/lttng.h>
45 #include <common/common.h>
46 #include <common/compat/poll.h>
47 #include <common/compat/socket.h>
48 #include <common/compat/endian.h>
49 #include <common/defaults.h>
50 #include <common/futex.h>
51 #include <common/index/index.h>
52 #include <common/sessiond-comm/sessiond-comm.h>
53 #include <common/sessiond-comm/inet.h>
54 #include <common/sessiond-comm/relayd.h>
55 #include <common/uri.h>
56 #include <common/utils.h>
60 #include "lttng-relayd.h"
62 #include "health-relayd.h"
63 #include "testpoint.h"
64 #include "viewer-stream.h"
67 #include "ctf-trace.h"
68 #include "connection.h"
69 #include "viewer-session.h"
71 #define SESSION_BUF_DEFAULT_COUNT 16
73 static struct lttng_uri
*live_uri
;
76 * This pipe is used to inform the worker thread that a command is queued and
77 * ready to be processed.
79 static int live_conn_pipe
[2] = { -1, -1 };
81 /* Shared between threads */
82 static int live_dispatch_thread_exit
;
84 static pthread_t live_listener_thread
;
85 static pthread_t live_dispatcher_thread
;
86 static pthread_t live_worker_thread
;
89 * Relay command queue.
91 * The live_thread_listener and live_thread_dispatcher communicate with this
94 static struct relay_conn_queue viewer_conn_queue
;
96 static uint64_t last_relay_viewer_session_id
;
97 static pthread_mutex_t last_relay_viewer_session_id_lock
=
98 PTHREAD_MUTEX_INITIALIZER
;
104 void cleanup_relayd_live(void)
112 * Receive a request buffer using a given socket, destination allocated buffer
115 * Return the size of the received message or else a negative value on error
116 * with errno being set by recvmsg() syscall.
119 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
123 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
124 if (ret
< 0 || ret
!= size
) {
126 /* Orderly shutdown. Not necessary to print an error. */
127 DBG("Socket %d did an orderly shutdown", sock
->fd
);
129 ERR("Relay failed to receive request.");
138 * Send a response buffer using a given socket, source allocated buffer of
141 * Return the size of the sent message or else a negative value on error with
142 * errno being set by sendmsg() syscall.
145 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
149 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
151 ERR("Relayd failed to send response.");
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
165 int check_new_streams(struct relay_connection
*conn
)
167 struct relay_session
*session
;
168 unsigned long current_val
;
171 if (!conn
->viewer_session
) {
175 cds_list_for_each_entry_rcu(session
,
176 &conn
->viewer_session
->session_list
,
177 viewer_session_node
) {
178 if (!session_get(session
)) {
181 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
183 session_put(session
);
194 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
195 * this function should ignore the sent flag or not.
197 * Return 0 on success or else a negative value.
200 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
201 struct relay_session
*session
, unsigned int ignore_sent_flag
)
204 struct lttng_viewer_stream send_stream
;
205 struct lttng_ht_iter iter
;
206 struct relay_viewer_stream
*vstream
;
210 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
212 struct ctf_trace
*ctf_trace
;
214 health_code_update();
216 if (!viewer_stream_get(vstream
)) {
220 pthread_mutex_lock(&vstream
->stream
->lock
);
221 /* Ignore if not the same session. */
222 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
223 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
224 pthread_mutex_unlock(&vstream
->stream
->lock
);
225 viewer_stream_put(vstream
);
229 ctf_trace
= vstream
->stream
->trace
;
230 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
231 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
232 send_stream
.metadata_flag
= htobe32(
233 vstream
->stream
->is_metadata
);
234 strncpy(send_stream
.path_name
, vstream
->path_name
,
235 sizeof(send_stream
.path_name
));
236 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
237 sizeof(send_stream
.channel_name
));
239 DBG("Sending stream %" PRIu64
" to viewer",
240 vstream
->stream
->stream_handle
);
241 vstream
->sent_flag
= 1;
242 pthread_mutex_unlock(&vstream
->stream
->lock
);
244 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
245 viewer_stream_put(vstream
);
259 * Create every viewer stream possible for the given session with the seek
260 * type. Three counters *can* be return which are in order the total amount of
261 * viewer stream of the session, the number of unsent stream and the number of
262 * stream created. Those counters can be NULL and thus will be ignored.
264 * Return 0 on success or else a negative value.
267 int make_viewer_streams(struct relay_session
*session
,
268 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
269 uint32_t *nb_created
, bool *closed
)
272 struct lttng_ht_iter iter
;
273 struct ctf_trace
*ctf_trace
;
278 * Hold the session lock to ensure that we see either none or
279 * all initial streams for a session, but no intermediate state.
281 pthread_mutex_lock(&session
->lock
);
283 if (session
->connection_closed
) {
288 * Create viewer streams for relay streams that are ready to be
289 * used for a the given session id only.
292 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
294 struct relay_stream
*stream
;
296 health_code_update();
298 if (!ctf_trace_get(ctf_trace
)) {
302 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
303 struct relay_viewer_stream
*vstream
;
305 if (!stream_get(stream
)) {
309 * stream published is protected by the session
312 if (!stream
->published
) {
316 * Stream has no data, don't consider it yet.
318 if (stream
->prev_seq
== -1ULL) {
321 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
323 vstream
= viewer_stream_create(stream
, seek_t
);
326 ctf_trace_put(ctf_trace
);
332 /* Update number of created stream counter. */
336 * Ensure a self-reference is preserved even
337 * after we have put our local reference.
339 viewer_stream_get(vstream
);
341 if (!vstream
->sent_flag
&& nb_unsent
) {
342 /* Update number of unsent stream counter. */
346 /* Update number of total stream counter. */
348 if (stream
->is_metadata
) {
349 if (!stream
->closed
||
350 stream
->metadata_received
> vstream
->metadata_sent
) {
354 if (!stream
->closed
||
355 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
361 /* Put local reference. */
362 viewer_stream_put(vstream
);
366 ctf_trace_put(ctf_trace
);
373 pthread_mutex_unlock(&session
->lock
);
377 int relayd_live_stop(void)
379 /* Stop dispatch thread */
380 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
381 futex_nto1_wake(&viewer_conn_queue
.futex
);
386 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
389 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
393 if (events
== NULL
|| size
== 0) {
398 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
404 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
416 * Check if the thread quit pipe was triggered.
418 * Return 1 if it was triggered else 0;
421 int check_thread_quit_pipe(int fd
, uint32_t events
)
423 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
431 * Create and init socket from uri.
434 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
437 struct lttcomm_sock
*sock
= NULL
;
439 sock
= lttcomm_alloc_sock_from_uri(uri
);
441 ERR("Allocating socket");
445 ret
= lttcomm_create_sock(sock
);
449 DBG("Listening on sock %d for live", sock
->fd
);
451 ret
= sock
->ops
->bind(sock
);
456 ret
= sock
->ops
->listen(sock
, -1);
466 lttcomm_destroy_sock(sock
);
472 * This thread manages the listening for new connections on the network
475 void *thread_listener(void *data
)
477 int i
, ret
, pollfd
, err
= -1;
478 uint32_t revents
, nb_fd
;
479 struct lttng_poll_event events
;
480 struct lttcomm_sock
*live_control_sock
;
482 DBG("[thread] Relay live listener started");
484 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
486 health_code_update();
488 live_control_sock
= init_socket(live_uri
);
489 if (!live_control_sock
) {
490 goto error_sock_control
;
493 /* Pass 2 as size here for the thread quit pipe and control sockets. */
494 ret
= create_thread_poll_set(&events
, 2);
496 goto error_create_poll
;
499 /* Add the control socket */
500 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
505 lttng_relay_notify_ready();
507 if (testpoint(relayd_thread_live_listener
)) {
508 goto error_testpoint
;
512 health_code_update();
514 DBG("Listener accepting live viewers connections");
518 ret
= lttng_poll_wait(&events
, -1);
522 * Restart interrupted system call.
524 if (errno
== EINTR
) {
531 DBG("Relay new viewer connection received");
532 for (i
= 0; i
< nb_fd
; i
++) {
533 health_code_update();
535 /* Fetch once the poll data */
536 revents
= LTTNG_POLL_GETEV(&events
, i
);
537 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
540 /* No activity for this FD (poll implementation). */
544 /* Thread quit pipe has been closed. Killing thread. */
545 ret
= check_thread_quit_pipe(pollfd
, revents
);
551 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
552 ERR("socket poll error");
554 } else if (revents
& LPOLLIN
) {
556 * A new connection is requested, therefore a
557 * viewer connection is allocated in this
558 * thread, enqueued to a global queue and
559 * dequeued (and freed) in the worker thread.
562 struct relay_connection
*new_conn
;
563 struct lttcomm_sock
*newsock
;
565 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
567 PERROR("accepting control sock");
570 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
572 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
575 PERROR("setsockopt inet");
576 lttcomm_destroy_sock(newsock
);
579 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
581 lttcomm_destroy_sock(newsock
);
584 /* Ownership assumed by the connection. */
587 /* Enqueue request for the dispatcher thread. */
588 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
592 * Wake the dispatch queue futex.
593 * Implicit memory barrier with the
594 * exchange in cds_wfcq_enqueue.
596 futex_nto1_wake(&viewer_conn_queue
.futex
);
605 lttng_poll_clean(&events
);
607 if (live_control_sock
->fd
>= 0) {
608 ret
= live_control_sock
->ops
->close(live_control_sock
);
613 lttcomm_destroy_sock(live_control_sock
);
617 DBG("Live viewer listener thread exited with error");
619 health_unregister(health_relayd
);
620 DBG("Live viewer listener thread cleanup complete");
621 if (lttng_relay_stop_threads()) {
622 ERR("Error stopping threads");
628 * This thread manages the dispatching of the requests to worker threads
631 void *thread_dispatcher(void *data
)
635 struct cds_wfcq_node
*node
;
636 struct relay_connection
*conn
= NULL
;
638 DBG("[thread] Live viewer relay dispatcher started");
640 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
642 if (testpoint(relayd_thread_live_dispatcher
)) {
643 goto error_testpoint
;
646 health_code_update();
648 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
649 health_code_update();
651 /* Atomically prepare the queue futex */
652 futex_nto1_prepare(&viewer_conn_queue
.futex
);
655 health_code_update();
657 /* Dequeue commands */
658 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
659 &viewer_conn_queue
.tail
);
661 DBG("Woken up but nothing in the live-viewer "
662 "relay command queue");
663 /* Continue thread execution */
666 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
667 DBG("Dispatching viewer request waiting on sock %d",
671 * Inform worker thread of the new request. This
672 * call is blocking so we can be assured that
673 * the data will be read at some point in time
674 * or wait to the end of the world :)
676 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
678 PERROR("write conn pipe");
679 connection_put(conn
);
682 } while (node
!= NULL
);
684 /* Futex wait on queue. Blocking call on futex() */
686 futex_nto1_wait(&viewer_conn_queue
.futex
);
690 /* Normal exit, no error */
697 ERR("Health error occurred in %s", __func__
);
699 health_unregister(health_relayd
);
700 DBG("Live viewer dispatch thread dying");
701 if (lttng_relay_stop_threads()) {
702 ERR("Error stopping threads");
708 * Establish connection with the viewer and check the versions.
710 * Return 0 on success or else negative value.
713 int viewer_connect(struct relay_connection
*conn
)
716 struct lttng_viewer_connect reply
, msg
;
718 conn
->version_check_done
= 1;
720 health_code_update();
722 DBG("Viewer is establishing a connection to the relayd.");
724 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
729 health_code_update();
731 memset(&reply
, 0, sizeof(reply
));
732 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
733 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
735 /* Major versions must be the same */
736 if (reply
.major
!= be32toh(msg
.major
)) {
737 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
738 reply
.major
, be32toh(msg
.major
));
743 conn
->major
= reply
.major
;
744 /* We adapt to the lowest compatible version */
745 if (reply
.minor
<= be32toh(msg
.minor
)) {
746 conn
->minor
= reply
.minor
;
748 conn
->minor
= be32toh(msg
.minor
);
751 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
752 conn
->type
= RELAY_VIEWER_COMMAND
;
753 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
754 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
756 ERR("Unknown connection type : %u", be32toh(msg
.type
));
761 reply
.major
= htobe32(reply
.major
);
762 reply
.minor
= htobe32(reply
.minor
);
763 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
765 * Increment outside of htobe64 macro, because the argument can
766 * be used more than once within the macro, and thus the
767 * operation may be undefined.
769 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
770 last_relay_viewer_session_id
++;
771 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
772 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
775 health_code_update();
777 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
782 health_code_update();
784 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
792 * Send the viewer the list of current sessions.
793 * We need to create a copy of the hash table content because otherwise
794 * we cannot assume the number of entries stays the same between getting
795 * the number of HT elements and iteration over the HT.
797 * Return 0 on success or else a negative value.
800 int viewer_list_sessions(struct relay_connection
*conn
)
803 struct lttng_viewer_list_sessions session_list
;
804 struct lttng_ht_iter iter
;
805 struct relay_session
*session
;
806 struct lttng_viewer_session
*send_session_buf
= NULL
;
807 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
810 DBG("List sessions received");
812 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
813 if (!send_session_buf
) {
818 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
820 struct lttng_viewer_session
*send_session
;
822 health_code_update();
824 if (count
>= buf_count
) {
825 struct lttng_viewer_session
*newbuf
;
826 uint32_t new_buf_count
= buf_count
<< 1;
828 newbuf
= realloc(send_session_buf
,
829 new_buf_count
* sizeof(*send_session_buf
));
835 send_session_buf
= newbuf
;
836 buf_count
= new_buf_count
;
838 send_session
= &send_session_buf
[count
];
839 strncpy(send_session
->session_name
, session
->session_name
,
840 sizeof(send_session
->session_name
));
841 strncpy(send_session
->hostname
, session
->hostname
,
842 sizeof(send_session
->hostname
));
843 send_session
->id
= htobe64(session
->id
);
844 send_session
->live_timer
= htobe32(session
->live_timer
);
845 if (session
->viewer_attached
) {
846 send_session
->clients
= htobe32(1);
848 send_session
->clients
= htobe32(0);
850 send_session
->streams
= htobe32(session
->stream_count
);
855 session_list
.sessions_count
= htobe32(count
);
857 health_code_update();
859 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
864 health_code_update();
866 ret
= send_response(conn
->sock
, send_session_buf
,
867 count
* sizeof(*send_session_buf
));
871 health_code_update();
875 free(send_session_buf
);
880 * Send the viewer the list of current streams.
883 int viewer_get_new_streams(struct relay_connection
*conn
)
885 int ret
, send_streams
= 0;
886 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
887 struct lttng_viewer_new_streams_request request
;
888 struct lttng_viewer_new_streams_response response
;
889 struct relay_session
*session
;
895 DBG("Get new streams received");
897 health_code_update();
899 /* Receive the request from the connected client. */
900 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
904 session_id
= be64toh(request
.session_id
);
906 health_code_update();
908 memset(&response
, 0, sizeof(response
));
910 session
= session_get_by_id(session_id
);
912 DBG("Relay session %" PRIu64
" not found", session_id
);
913 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
917 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
919 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
924 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
926 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
927 &nb_created
, &closed
);
929 goto end_put_session
;
931 /* Only send back the newly created streams with the unsent ones. */
932 nb_streams
= nb_created
+ nb_unsent
;
933 response
.streams_count
= htobe32(nb_streams
);
936 * If the session is closed, HUP when there are no more streams
939 if (closed
&& nb_total
== 0) {
941 response
.streams_count
= 0;
942 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
947 health_code_update();
948 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
950 goto end_put_session
;
952 health_code_update();
955 * Unknown or empty session, just return gracefully, the viewer
956 * knows what is happening.
958 if (!send_streams
|| !nb_streams
) {
960 goto end_put_session
;
964 * Send stream and *DON'T* ignore the sent flag so every viewer
965 * streams that were not sent from that point will be sent to
968 ret
= send_viewer_streams(conn
->sock
, session
, 0);
970 goto end_put_session
;
975 session_put(session
);
982 * Send the viewer the list of current sessions.
985 int viewer_attach_session(struct relay_connection
*conn
)
987 int send_streams
= 0;
989 uint32_t nb_streams
= 0;
990 enum lttng_viewer_seek seek_type
;
991 struct lttng_viewer_attach_session_request request
;
992 struct lttng_viewer_attach_session_response response
;
993 struct relay_session
*session
= NULL
;
998 health_code_update();
1000 /* Receive the request from the connected client. */
1001 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1006 health_code_update();
1008 memset(&response
, 0, sizeof(response
));
1010 if (!conn
->viewer_session
) {
1011 DBG("Client trying to attach before creating a live viewer session");
1012 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1016 session
= session_get_by_id(be64toh(request
.session_id
));
1018 DBG("Relay session %" PRIu64
" not found",
1019 be64toh(request
.session_id
));
1020 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1023 DBG("Attach session ID %" PRIu64
" received",
1024 be64toh(request
.session_id
));
1026 if (session
->live_timer
== 0) {
1027 DBG("Not live session");
1028 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1033 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1035 DBG("Already a viewer attached");
1036 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1040 switch (be32toh(request
.seek
)) {
1041 case LTTNG_VIEWER_SEEK_BEGINNING
:
1042 case LTTNG_VIEWER_SEEK_LAST
:
1043 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1044 seek_type
= be32toh(request
.seek
);
1047 ERR("Wrong seek parameter");
1048 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1053 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1056 goto end_put_session
;
1058 response
.streams_count
= htobe32(nb_streams
);
1061 * If the session is closed when the viewer is attaching, it
1062 * means some of the streams may have been concurrently removed,
1063 * so we don't allow the viewer to attach, even if there are
1064 * streams available.
1068 response
.streams_count
= 0;
1069 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1074 health_code_update();
1075 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1077 goto end_put_session
;
1079 health_code_update();
1082 * Unknown or empty session, just return gracefully, the viewer
1083 * knows what is happening.
1085 if (!send_streams
|| !nb_streams
) {
1087 goto end_put_session
;
1090 /* Send stream and ignore the sent flag. */
1091 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1093 goto end_put_session
;
1098 session_put(session
);
1105 * Open the index file if needed for the given vstream.
1107 * If an index file is successfully opened, the vstream index_fd set with
1110 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1112 * Called with rstream lock held.
1114 static int try_open_index(struct relay_viewer_stream
*vstream
,
1115 struct relay_stream
*rstream
)
1119 if (vstream
->index_fd
) {
1124 * First time, we open the index file and at least one index is ready.
1126 if (rstream
->total_index_received
== 0) {
1130 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1131 vstream
->stream
->tracefile_count
,
1132 vstream
->current_tracefile_id
);
1134 vstream
->index_fd
= stream_fd_create(ret
);
1135 if (!vstream
->index_fd
) {
1151 * Check the status of the index for the given stream. This function
1152 * updates the index structure if needed and can put (close) the vstream
1153 * in the HUP situation.
1155 * Return 0 means that we can proceed with the index. A value of 1 means
1156 * that the index has been updated and is ready to be sent to the
1157 * client. A negative value indicates an error that can't be handled.
1159 * Called with rstream lock held.
1161 static int check_index_status(struct relay_viewer_stream
*vstream
,
1162 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1163 struct lttng_viewer_index
*index
)
1167 if (trace
->session
->connection_closed
1168 && rstream
->total_index_received
1169 == vstream
->last_sent_index
) {
1170 /* Last index sent and session connection is closed. */
1171 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1173 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1174 rstream
->total_index_received
1175 == vstream
->last_sent_index
) {
1177 * We've received a synchronization beacon and the last index
1178 * available has been sent, the index for now is inactive.
1180 * In this case, we have received a beacon which allows us to
1181 * inform the client of a time interval during which we can
1182 * guarantee that there are no events to read (and never will
1185 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1186 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1187 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1189 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
1191 * This actually checks the case where recv == last_sent.
1192 * In this case, we have not received a beacon. Therefore, we
1193 * can only ask the client to retry later.
1195 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1197 } else if (!viewer_stream_is_tracefile_seq_readable(vstream
,
1198 vstream
->current_tracefile_seq
)) {
1200 * The producer has overwritten our current file. We
1203 DBG("Viewer stream %" PRIu64
" rotation due to overwrite",
1204 vstream
->stream
->stream_handle
);
1205 ret
= viewer_stream_rotate(vstream
);
1208 } else if (ret
== 1) {
1209 /* EOF across entire stream. */
1210 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1213 assert(viewer_stream_is_tracefile_seq_readable(vstream
,
1214 vstream
->current_tracefile_seq
));
1215 /* ret == 0 means successful so we continue. */
1222 * Use EOF on current index file to find out when we
1225 read_ret
= lttng_read(vstream
->index_fd
->fd
, tmp
, 1);
1226 if (read_ret
== 1) {
1229 /* There is still data to read. Rewind position. */
1230 seek_ret
= lseek(vstream
->index_fd
->fd
, -1, SEEK_CUR
);
1236 } else if (read_ret
== 0) {
1237 /* EOF. We need to rotate. */
1238 DBG("Viewer stream %" PRIu64
" rotation due to EOF",
1239 vstream
->stream
->stream_handle
);
1240 ret
= viewer_stream_rotate(vstream
);
1243 } else if (ret
== 1) {
1244 /* EOF across entire stream. */
1245 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1248 assert(viewer_stream_is_tracefile_seq_readable(vstream
,
1249 vstream
->current_tracefile_seq
));
1250 /* ret == 0 means successful so we continue. */
1253 /* Error reading index. */
1261 viewer_stream_put(vstream
);
1267 * Send the next index for a stream.
1269 * Return 0 on success or else a negative value.
1272 int viewer_get_next_index(struct relay_connection
*conn
)
1276 struct lttng_viewer_get_next_index request_index
;
1277 struct lttng_viewer_index viewer_index
;
1278 struct ctf_packet_index packet_index
;
1279 struct relay_viewer_stream
*vstream
= NULL
;
1280 struct relay_stream
*rstream
= NULL
;
1281 struct ctf_trace
*ctf_trace
= NULL
;
1282 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1286 DBG("Viewer get next index");
1288 memset(&viewer_index
, 0, sizeof(viewer_index
));
1289 health_code_update();
1291 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1295 health_code_update();
1297 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1299 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1303 /* Use back. ref. Protected by refcounts. */
1304 rstream
= vstream
->stream
;
1305 ctf_trace
= rstream
->trace
;
1307 /* metadata_viewer_stream may be NULL. */
1308 metadata_viewer_stream
=
1309 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1311 pthread_mutex_lock(&rstream
->lock
);
1314 * The viewer should not ask for index on metadata stream.
1316 if (rstream
->is_metadata
) {
1317 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1321 /* Try to open an index if one is needed for that stream. */
1322 ret
= try_open_index(vstream
, rstream
);
1324 if (ret
== -ENOENT
) {
1326 * The index is created only when the first data
1327 * packet arrives, it might not be ready at the
1328 * beginning of the session
1330 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1332 /* Unhandled error. */
1333 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1338 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1341 } else if (ret
== 1) {
1343 * We have no index to send and check_index_status has populated
1344 * viewer_index's status.
1348 /* At this point, ret is 0 thus we will be able to read the index. */
1352 * vstream->stream_fd may be NULL if it has been closed by
1353 * tracefile rotation, or if we are at the beginning of the
1354 * stream. We open the data stream file here to protect against
1355 * overwrite caused by tracefile rotation (in association with
1356 * unlink performed before overwrite).
1358 if (!vstream
->stream_fd
) {
1359 char fullpath
[PATH_MAX
];
1361 if (vstream
->stream
->tracefile_count
> 0) {
1362 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1364 vstream
->channel_name
,
1365 vstream
->current_tracefile_id
);
1367 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1369 vstream
->channel_name
);
1374 ret
= open(fullpath
, O_RDONLY
);
1376 PERROR("Relay opening trace file");
1379 vstream
->stream_fd
= stream_fd_create(ret
);
1380 if (!vstream
->stream_fd
) {
1388 ret
= check_new_streams(conn
);
1390 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1392 } else if (ret
== 1) {
1393 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1396 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1397 sizeof(packet_index
));
1398 if (read_ret
< sizeof(packet_index
)) {
1399 ERR("Relay reading index file %d returned %zd",
1400 vstream
->index_fd
->fd
, read_ret
);
1401 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1404 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1405 vstream
->last_sent_index
++;
1409 * Indexes are stored in big endian, no need to switch before sending.
1411 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1412 rstream
->stream_handle
,
1413 be64toh(packet_index
.offset
));
1414 viewer_index
.offset
= packet_index
.offset
;
1415 viewer_index
.packet_size
= packet_index
.packet_size
;
1416 viewer_index
.content_size
= packet_index
.content_size
;
1417 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1418 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1419 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1420 viewer_index
.stream_id
= packet_index
.stream_id
;
1424 pthread_mutex_unlock(&rstream
->lock
);
1427 if (metadata_viewer_stream
) {
1428 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1429 DBG("get next index metadata check: recv %" PRIu64
1431 metadata_viewer_stream
->stream
->metadata_received
,
1432 metadata_viewer_stream
->metadata_sent
);
1433 if (!metadata_viewer_stream
->stream
->metadata_received
||
1434 metadata_viewer_stream
->stream
->metadata_received
>
1435 metadata_viewer_stream
->metadata_sent
) {
1436 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1438 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1441 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1442 health_code_update();
1444 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1448 health_code_update();
1450 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1451 vstream
->last_sent_index
,
1452 vstream
->stream
->stream_handle
);
1454 if (metadata_viewer_stream
) {
1455 viewer_stream_put(metadata_viewer_stream
);
1458 viewer_stream_put(vstream
);
1463 pthread_mutex_unlock(&rstream
->lock
);
1464 if (metadata_viewer_stream
) {
1465 viewer_stream_put(metadata_viewer_stream
);
1467 viewer_stream_put(vstream
);
1472 * Send the next index for a stream
1474 * Return 0 on success or else a negative value.
1477 int viewer_get_packet(struct relay_connection
*conn
)
1479 int ret
, send_data
= 0;
1483 struct lttng_viewer_get_packet get_packet_info
;
1484 struct lttng_viewer_trace_packet reply
;
1485 struct relay_viewer_stream
*vstream
= NULL
;
1487 DBG2("Relay get data packet");
1489 health_code_update();
1491 ret
= recv_request(conn
->sock
, &get_packet_info
,
1492 sizeof(get_packet_info
));
1496 health_code_update();
1498 /* From this point on, the error label can be reached. */
1499 memset(&reply
, 0, sizeof(reply
));
1501 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1503 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1504 goto send_reply_nolock
;
1507 pthread_mutex_lock(&vstream
->stream
->lock
);
1509 len
= be32toh(get_packet_info
.len
);
1510 data
= zmalloc(len
);
1512 PERROR("relay data zmalloc");
1516 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1519 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1520 be64toh(get_packet_info
.offset
));
1523 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1524 if (read_len
< len
) {
1525 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1526 vstream
->stream_fd
->fd
,
1527 be64toh(get_packet_info
.offset
));
1530 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1531 reply
.len
= htobe32(len
);
1536 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1540 pthread_mutex_unlock(&vstream
->stream
->lock
);
1543 reply
.flags
= htobe32(reply
.flags
);
1545 health_code_update();
1547 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1551 health_code_update();
1554 health_code_update();
1555 ret
= send_response(conn
->sock
, data
, len
);
1559 health_code_update();
1562 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1563 be64toh(get_packet_info
.stream_id
));
1569 viewer_stream_put(vstream
);
1575 * Send the session's metadata
1577 * Return 0 on success else a negative value.
1580 int viewer_get_metadata(struct relay_connection
*conn
)
1586 struct lttng_viewer_get_metadata request
;
1587 struct lttng_viewer_metadata_packet reply
;
1588 struct relay_viewer_stream
*vstream
= NULL
;
1592 DBG("Relay get metadata");
1594 health_code_update();
1596 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1600 health_code_update();
1602 memset(&reply
, 0, sizeof(reply
));
1604 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1607 * The metadata stream can be closed by a CLOSE command
1608 * just before we attach. It can also be closed by
1609 * per-pid tracing during tracing. Therefore, it is
1610 * possible that we cannot find this viewer stream.
1611 * Reply back to the client with an error if we cannot
1614 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1617 pthread_mutex_lock(&vstream
->stream
->lock
);
1618 if (!vstream
->stream
->is_metadata
) {
1619 ERR("Invalid metadata stream");
1623 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1625 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1627 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1631 /* first time, we open the metadata file */
1632 if (!vstream
->stream_fd
) {
1633 char fullpath
[PATH_MAX
];
1635 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1636 vstream
->channel_name
);
1640 ret
= open(fullpath
, O_RDONLY
);
1642 PERROR("Relay opening metadata file");
1645 vstream
->stream_fd
= stream_fd_create(ret
);
1646 if (!vstream
->stream_fd
) {
1654 reply
.len
= htobe64(len
);
1655 data
= zmalloc(len
);
1657 PERROR("viewer metadata zmalloc");
1661 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1662 if (read_len
< len
) {
1663 PERROR("Relay reading metadata file");
1666 vstream
->metadata_sent
+= read_len
;
1667 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1668 && vstream
->stream
->closed
) {
1669 /* Release ownership for the viewer metadata stream. */
1670 viewer_stream_put(vstream
);
1673 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1678 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1681 health_code_update();
1683 pthread_mutex_unlock(&vstream
->stream
->lock
);
1685 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1689 health_code_update();
1692 ret
= send_response(conn
->sock
, data
, len
);
1698 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1699 be64toh(request
.stream_id
));
1701 DBG("Metadata sent");
1707 viewer_stream_put(vstream
);
1713 * Create a viewer session.
1715 * Return 0 on success or else a negative value.
1718 int viewer_create_session(struct relay_connection
*conn
)
1721 struct lttng_viewer_create_session_response resp
;
1723 DBG("Viewer create session received");
1725 memset(&resp
, 0, sizeof(resp
));
1726 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1727 conn
->viewer_session
= viewer_session_create();
1728 if (!conn
->viewer_session
) {
1729 ERR("Allocation viewer session");
1730 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1735 health_code_update();
1736 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1740 health_code_update();
1749 * live_relay_unknown_command: send -1 if received unknown command
1752 void live_relay_unknown_command(struct relay_connection
*conn
)
1754 struct lttcomm_relayd_generic_reply reply
;
1756 memset(&reply
, 0, sizeof(reply
));
1757 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1758 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1762 * Process the commands received on the control socket
1765 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1766 struct relay_connection
*conn
)
1771 msg_value
= be32toh(recv_hdr
->cmd
);
1774 * Make sure we've done the version check before any command other then a
1775 * new client connection.
1777 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1778 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1783 switch (msg_value
) {
1784 case LTTNG_VIEWER_CONNECT
:
1785 ret
= viewer_connect(conn
);
1787 case LTTNG_VIEWER_LIST_SESSIONS
:
1788 ret
= viewer_list_sessions(conn
);
1790 case LTTNG_VIEWER_ATTACH_SESSION
:
1791 ret
= viewer_attach_session(conn
);
1793 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1794 ret
= viewer_get_next_index(conn
);
1796 case LTTNG_VIEWER_GET_PACKET
:
1797 ret
= viewer_get_packet(conn
);
1799 case LTTNG_VIEWER_GET_METADATA
:
1800 ret
= viewer_get_metadata(conn
);
1802 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1803 ret
= viewer_get_new_streams(conn
);
1805 case LTTNG_VIEWER_CREATE_SESSION
:
1806 ret
= viewer_create_session(conn
);
1809 ERR("Received unknown viewer command (%u)",
1810 be32toh(recv_hdr
->cmd
));
1811 live_relay_unknown_command(conn
);
1821 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1825 (void) lttng_poll_del(events
, pollfd
);
1827 ret
= close(pollfd
);
1829 ERR("Closing pollfd %d", pollfd
);
1834 * This thread does the actual work
1837 void *thread_worker(void *data
)
1841 struct lttng_poll_event events
;
1842 struct lttng_ht
*viewer_connections_ht
;
1843 struct lttng_ht_iter iter
;
1844 struct lttng_viewer_cmd recv_hdr
;
1845 struct relay_connection
*destroy_conn
;
1847 DBG("[thread] Live viewer relay worker started");
1849 rcu_register_thread();
1851 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1853 if (testpoint(relayd_thread_live_worker
)) {
1854 goto error_testpoint
;
1857 /* table of connections indexed on socket */
1858 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1859 if (!viewer_connections_ht
) {
1860 goto viewer_connections_ht_error
;
1863 ret
= create_thread_poll_set(&events
, 2);
1865 goto error_poll_create
;
1868 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1877 health_code_update();
1879 /* Infinite blocking call, waiting for transmission */
1880 DBG3("Relayd live viewer worker thread polling...");
1881 health_poll_entry();
1882 ret
= lttng_poll_wait(&events
, -1);
1886 * Restart interrupted system call.
1888 if (errno
== EINTR
) {
1897 * Process control. The control connection is prioritised so we don't
1898 * starve it with high throughput tracing data on the data
1901 for (i
= 0; i
< nb_fd
; i
++) {
1902 /* Fetch once the poll data */
1903 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1904 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1906 health_code_update();
1909 /* No activity for this FD (poll implementation). */
1913 /* Thread quit pipe has been closed. Killing thread. */
1914 ret
= check_thread_quit_pipe(pollfd
, revents
);
1920 /* Inspect the relay conn pipe for new connection. */
1921 if (pollfd
== live_conn_pipe
[0]) {
1922 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1923 ERR("Relay live pipe error");
1925 } else if (revents
& LPOLLIN
) {
1926 struct relay_connection
*conn
;
1928 ret
= lttng_read(live_conn_pipe
[0],
1929 &conn
, sizeof(conn
));
1933 lttng_poll_add(&events
, conn
->sock
->fd
,
1934 LPOLLIN
| LPOLLRDHUP
);
1935 connection_ht_add(viewer_connections_ht
, conn
);
1936 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
1939 /* Connection activity. */
1940 struct relay_connection
*conn
;
1942 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
1947 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1948 cleanup_connection_pollfd(&events
, pollfd
);
1949 /* Put "create" ownership reference. */
1950 connection_put(conn
);
1951 } else if (revents
& LPOLLIN
) {
1952 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1953 sizeof(recv_hdr
), 0);
1955 /* Connection closed. */
1956 cleanup_connection_pollfd(&events
, pollfd
);
1957 /* Put "create" ownership reference. */
1958 connection_put(conn
);
1959 DBG("Viewer control conn closed with %d", pollfd
);
1961 ret
= process_control(&recv_hdr
, conn
);
1963 /* Clear the session on error. */
1964 cleanup_connection_pollfd(&events
, pollfd
);
1965 /* Put "create" ownership reference. */
1966 connection_put(conn
);
1967 DBG("Viewer connection closed with %d", pollfd
);
1971 /* Put local "get_by_sock" reference. */
1972 connection_put(conn
);
1979 lttng_poll_clean(&events
);
1981 /* Cleanup reamaining connection object. */
1983 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
1986 health_code_update();
1987 connection_put(destroy_conn
);
1991 lttng_ht_destroy(viewer_connections_ht
);
1992 viewer_connections_ht_error
:
1993 /* Close relay conn pipes */
1994 utils_close_pipe(live_conn_pipe
);
1996 DBG("Viewer worker thread exited with error");
1998 DBG("Viewer worker thread cleanup complete");
2002 ERR("Health error occurred in %s", __func__
);
2004 health_unregister(health_relayd
);
2005 if (lttng_relay_stop_threads()) {
2006 ERR("Error stopping threads");
2008 rcu_unregister_thread();
2013 * Create the relay command pipe to wake thread_manage_apps.
2014 * Closed in cleanup().
2016 static int create_conn_pipe(void)
2018 return utils_create_pipe_cloexec(live_conn_pipe
);
2021 int relayd_live_join(void)
2023 int ret
, retval
= 0;
2026 ret
= pthread_join(live_listener_thread
, &status
);
2029 PERROR("pthread_join live listener");
2033 ret
= pthread_join(live_worker_thread
, &status
);
2036 PERROR("pthread_join live worker");
2040 ret
= pthread_join(live_dispatcher_thread
, &status
);
2043 PERROR("pthread_join live dispatcher");
2047 cleanup_relayd_live();
2055 int relayd_live_create(struct lttng_uri
*uri
)
2057 int ret
= 0, retval
= 0;
2063 goto exit_init_data
;
2067 /* Check if daemon is UID = 0 */
2068 is_root
= !getuid();
2071 if (live_uri
->port
< 1024) {
2072 ERR("Need to be root to use ports < 1024");
2074 goto exit_init_data
;
2078 /* Setup the thread apps communication pipe. */
2079 if (create_conn_pipe()) {
2081 goto exit_init_data
;
2084 /* Init relay command queue. */
2085 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2087 /* Set up max poll set size */
2088 lttng_poll_set_max_size();
2090 /* Setup the dispatcher thread */
2091 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2092 thread_dispatcher
, (void *) NULL
);
2095 PERROR("pthread_create viewer dispatcher");
2097 goto exit_dispatcher_thread
;
2100 /* Setup the worker thread */
2101 ret
= pthread_create(&live_worker_thread
, NULL
,
2102 thread_worker
, NULL
);
2105 PERROR("pthread_create viewer worker");
2107 goto exit_worker_thread
;
2110 /* Setup the listener thread */
2111 ret
= pthread_create(&live_listener_thread
, NULL
,
2112 thread_listener
, (void *) NULL
);
2115 PERROR("pthread_create viewer listener");
2117 goto exit_listener_thread
;
2121 * All OK, started all threads.
2126 * Join on the live_listener_thread should anything be added after
2127 * the live_listener thread's creation.
2130 exit_listener_thread
:
2132 ret
= pthread_join(live_worker_thread
, &status
);
2135 PERROR("pthread_join live worker");
2140 ret
= pthread_join(live_dispatcher_thread
, &status
);
2143 PERROR("pthread_join live dispatcher");
2146 exit_dispatcher_thread
:
2149 cleanup_relayd_live();