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>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "ctf-trace.h"
67 #include "connection.h"
68 #include "viewer-session.h"
70 #define SESSION_BUF_DEFAULT_COUNT 16
72 static struct lttng_uri
*live_uri
;
75 * This pipe is used to inform the worker thread that a command is queued and
76 * ready to be processed.
78 static int live_conn_pipe
[2] = { -1, -1 };
80 /* Shared between threads */
81 static int live_dispatch_thread_exit
;
83 static pthread_t live_listener_thread
;
84 static pthread_t live_dispatcher_thread
;
85 static pthread_t live_worker_thread
;
88 * Relay command queue.
90 * The live_thread_listener and live_thread_dispatcher communicate with this
93 static struct relay_conn_queue viewer_conn_queue
;
95 static uint64_t last_relay_viewer_session_id
;
96 static pthread_mutex_t last_relay_viewer_session_id_lock
=
97 PTHREAD_MUTEX_INITIALIZER
;
103 void cleanup_relayd_live(void)
111 * Receive a request buffer using a given socket, destination allocated buffer
114 * Return the size of the received message or else a negative value on error
115 * with errno being set by recvmsg() syscall.
118 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
122 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
123 if (ret
< 0 || ret
!= size
) {
125 /* Orderly shutdown. Not necessary to print an error. */
126 DBG("Socket %d did an orderly shutdown", sock
->fd
);
128 ERR("Relay failed to receive request.");
137 * Send a response buffer using a given socket, source allocated buffer of
140 * Return the size of the sent message or else a negative value on error with
141 * errno being set by sendmsg() syscall.
144 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
148 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
150 ERR("Relayd failed to send response.");
157 * Atomically check if new streams got added in one of the sessions attached
158 * and reset the flag to 0.
160 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
164 int check_new_streams(struct relay_connection
*conn
)
166 struct relay_session
*session
;
167 unsigned long current_val
;
170 if (!conn
->viewer_session
) {
174 cds_list_for_each_entry_rcu(session
,
175 &conn
->viewer_session
->session_list
,
176 viewer_session_node
) {
177 if (!session_get(session
)) {
180 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
182 session_put(session
);
193 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
194 * this function should ignore the sent flag or not.
196 * Return 0 on success or else a negative value.
199 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
200 struct relay_session
*session
, unsigned int ignore_sent_flag
)
203 struct lttng_viewer_stream send_stream
;
204 struct lttng_ht_iter iter
;
205 struct relay_viewer_stream
*vstream
;
209 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
211 struct ctf_trace
*ctf_trace
;
213 health_code_update();
215 if (!viewer_stream_get(vstream
)) {
219 pthread_mutex_lock(&vstream
->stream
->lock
);
220 /* Ignore if not the same session. */
221 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
222 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
223 pthread_mutex_unlock(&vstream
->stream
->lock
);
224 viewer_stream_put(vstream
);
228 ctf_trace
= vstream
->stream
->trace
;
229 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
230 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
231 send_stream
.metadata_flag
= htobe32(
232 vstream
->stream
->is_metadata
);
233 strncpy(send_stream
.path_name
, vstream
->path_name
,
234 sizeof(send_stream
.path_name
));
235 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
236 sizeof(send_stream
.channel_name
));
238 DBG("Sending stream %" PRIu64
" to viewer",
239 vstream
->stream
->stream_handle
);
240 vstream
->sent_flag
= 1;
241 pthread_mutex_unlock(&vstream
->stream
->lock
);
243 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
244 viewer_stream_put(vstream
);
258 * Create every viewer stream possible for the given session with the seek
259 * type. Three counters *can* be return which are in order the total amount of
260 * viewer stream of the session, the number of unsent stream and the number of
261 * stream created. Those counters can be NULL and thus will be ignored.
263 * Return 0 on success or else a negative value.
266 int make_viewer_streams(struct relay_session
*session
,
267 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
268 uint32_t *nb_created
, bool *closed
)
271 struct lttng_ht_iter iter
;
272 struct ctf_trace
*ctf_trace
;
277 * Hold the session lock to ensure that we see either none or
278 * all initial streams for a session, but no intermediate state.
280 pthread_mutex_lock(&session
->lock
);
282 if (session
->connection_closed
) {
287 * Create viewer streams for relay streams that are ready to be
288 * used for a the given session id only.
291 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
293 struct relay_stream
*stream
;
295 health_code_update();
297 if (!ctf_trace_get(ctf_trace
)) {
301 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
302 struct relay_viewer_stream
*vstream
;
304 if (!stream_get(stream
)) {
308 * stream published is protected by the session lock.
310 if (!stream
->published
) {
313 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
315 vstream
= viewer_stream_create(stream
, seek_t
);
318 ctf_trace_put(ctf_trace
);
324 /* Update number of created stream counter. */
328 * Ensure a self-reference is preserved even
329 * after we have put our local reference.
331 viewer_stream_get(vstream
);
333 if (!vstream
->sent_flag
&& nb_unsent
) {
334 /* Update number of unsent stream counter. */
338 /* Update number of total stream counter. */
340 if (stream
->is_metadata
) {
341 if (!stream
->closed
||
342 stream
->metadata_received
> vstream
->metadata_sent
) {
346 if (!stream
->closed
||
347 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
353 /* Put local reference. */
354 viewer_stream_put(vstream
);
358 ctf_trace_put(ctf_trace
);
365 pthread_mutex_unlock(&session
->lock
);
369 int relayd_live_stop(void)
371 /* Stop dispatch thread */
372 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
373 futex_nto1_wake(&viewer_conn_queue
.futex
);
378 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
381 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
385 if (events
== NULL
|| size
== 0) {
390 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
396 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
408 * Check if the thread quit pipe was triggered.
410 * Return 1 if it was triggered else 0;
413 int check_thread_quit_pipe(int fd
, uint32_t events
)
415 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
423 * Create and init socket from uri.
426 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
429 struct lttcomm_sock
*sock
= NULL
;
431 sock
= lttcomm_alloc_sock_from_uri(uri
);
433 ERR("Allocating socket");
437 ret
= lttcomm_create_sock(sock
);
441 DBG("Listening on sock %d for live", sock
->fd
);
443 ret
= sock
->ops
->bind(sock
);
448 ret
= sock
->ops
->listen(sock
, -1);
458 lttcomm_destroy_sock(sock
);
464 * This thread manages the listening for new connections on the network
467 void *thread_listener(void *data
)
469 int i
, ret
, pollfd
, err
= -1;
470 uint32_t revents
, nb_fd
;
471 struct lttng_poll_event events
;
472 struct lttcomm_sock
*live_control_sock
;
474 DBG("[thread] Relay live listener started");
476 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
478 health_code_update();
480 live_control_sock
= init_socket(live_uri
);
481 if (!live_control_sock
) {
482 goto error_sock_control
;
485 /* Pass 2 as size here for the thread quit pipe and control sockets. */
486 ret
= create_thread_poll_set(&events
, 2);
488 goto error_create_poll
;
491 /* Add the control socket */
492 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
497 lttng_relay_notify_ready();
499 if (testpoint(relayd_thread_live_listener
)) {
500 goto error_testpoint
;
504 health_code_update();
506 DBG("Listener accepting live viewers connections");
510 ret
= lttng_poll_wait(&events
, -1);
514 * Restart interrupted system call.
516 if (errno
== EINTR
) {
523 DBG("Relay new viewer connection received");
524 for (i
= 0; i
< nb_fd
; i
++) {
525 health_code_update();
527 /* Fetch once the poll data */
528 revents
= LTTNG_POLL_GETEV(&events
, i
);
529 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
532 /* No activity for this FD (poll implementation). */
536 /* Thread quit pipe has been closed. Killing thread. */
537 ret
= check_thread_quit_pipe(pollfd
, revents
);
543 if (revents
& LPOLLIN
) {
545 * A new connection is requested, therefore a
546 * viewer connection is allocated in this
547 * thread, enqueued to a global queue and
548 * dequeued (and freed) in the worker thread.
551 struct relay_connection
*new_conn
;
552 struct lttcomm_sock
*newsock
;
554 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
556 PERROR("accepting control sock");
559 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
561 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
564 PERROR("setsockopt inet");
565 lttcomm_destroy_sock(newsock
);
568 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
570 lttcomm_destroy_sock(newsock
);
573 /* Ownership assumed by the connection. */
576 /* Enqueue request for the dispatcher thread. */
577 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
581 * Wake the dispatch queue futex.
582 * Implicit memory barrier with the
583 * exchange in cds_wfcq_enqueue.
585 futex_nto1_wake(&viewer_conn_queue
.futex
);
586 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
587 ERR("socket poll error");
590 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
600 lttng_poll_clean(&events
);
602 if (live_control_sock
->fd
>= 0) {
603 ret
= live_control_sock
->ops
->close(live_control_sock
);
608 lttcomm_destroy_sock(live_control_sock
);
612 DBG("Live viewer listener thread exited with error");
614 health_unregister(health_relayd
);
615 DBG("Live viewer listener thread cleanup complete");
616 if (lttng_relay_stop_threads()) {
617 ERR("Error stopping threads");
623 * This thread manages the dispatching of the requests to worker threads
626 void *thread_dispatcher(void *data
)
630 struct cds_wfcq_node
*node
;
631 struct relay_connection
*conn
= NULL
;
633 DBG("[thread] Live viewer relay dispatcher started");
635 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
637 if (testpoint(relayd_thread_live_dispatcher
)) {
638 goto error_testpoint
;
641 health_code_update();
643 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
644 health_code_update();
646 /* Atomically prepare the queue futex */
647 futex_nto1_prepare(&viewer_conn_queue
.futex
);
650 health_code_update();
652 /* Dequeue commands */
653 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
654 &viewer_conn_queue
.tail
);
656 DBG("Woken up but nothing in the live-viewer "
657 "relay command queue");
658 /* Continue thread execution */
661 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
662 DBG("Dispatching viewer request waiting on sock %d",
666 * Inform worker thread of the new request. This
667 * call is blocking so we can be assured that
668 * the data will be read at some point in time
669 * or wait to the end of the world :)
671 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
673 PERROR("write conn pipe");
674 connection_put(conn
);
677 } while (node
!= NULL
);
679 /* Futex wait on queue. Blocking call on futex() */
681 futex_nto1_wait(&viewer_conn_queue
.futex
);
685 /* Normal exit, no error */
692 ERR("Health error occurred in %s", __func__
);
694 health_unregister(health_relayd
);
695 DBG("Live viewer dispatch thread dying");
696 if (lttng_relay_stop_threads()) {
697 ERR("Error stopping threads");
703 * Establish connection with the viewer and check the versions.
705 * Return 0 on success or else negative value.
708 int viewer_connect(struct relay_connection
*conn
)
711 struct lttng_viewer_connect reply
, msg
;
713 conn
->version_check_done
= 1;
715 health_code_update();
717 DBG("Viewer is establishing a connection to the relayd.");
719 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
724 health_code_update();
726 memset(&reply
, 0, sizeof(reply
));
727 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
728 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
730 /* Major versions must be the same */
731 if (reply
.major
!= be32toh(msg
.major
)) {
732 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
733 reply
.major
, be32toh(msg
.major
));
738 conn
->major
= reply
.major
;
739 /* We adapt to the lowest compatible version */
740 if (reply
.minor
<= be32toh(msg
.minor
)) {
741 conn
->minor
= reply
.minor
;
743 conn
->minor
= be32toh(msg
.minor
);
746 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
747 conn
->type
= RELAY_VIEWER_COMMAND
;
748 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
749 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
751 ERR("Unknown connection type : %u", be32toh(msg
.type
));
756 reply
.major
= htobe32(reply
.major
);
757 reply
.minor
= htobe32(reply
.minor
);
758 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
760 * Increment outside of htobe64 macro, because the argument can
761 * be used more than once within the macro, and thus the
762 * operation may be undefined.
764 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
765 last_relay_viewer_session_id
++;
766 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
767 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
770 health_code_update();
772 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
777 health_code_update();
779 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
787 * Send the viewer the list of current sessions.
788 * We need to create a copy of the hash table content because otherwise
789 * we cannot assume the number of entries stays the same between getting
790 * the number of HT elements and iteration over the HT.
792 * Return 0 on success or else a negative value.
795 int viewer_list_sessions(struct relay_connection
*conn
)
798 struct lttng_viewer_list_sessions session_list
;
799 struct lttng_ht_iter iter
;
800 struct relay_session
*session
;
801 struct lttng_viewer_session
*send_session_buf
= NULL
;
802 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
805 DBG("List sessions received");
807 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
808 if (!send_session_buf
) {
813 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
815 struct lttng_viewer_session
*send_session
;
817 health_code_update();
819 if (count
>= buf_count
) {
820 struct lttng_viewer_session
*newbuf
;
821 uint32_t new_buf_count
= buf_count
<< 1;
823 newbuf
= realloc(send_session_buf
,
824 new_buf_count
* sizeof(*send_session_buf
));
830 send_session_buf
= newbuf
;
831 buf_count
= new_buf_count
;
833 send_session
= &send_session_buf
[count
];
834 if (lttng_strncpy(send_session
->session_name
,
835 session
->session_name
,
836 sizeof(send_session
->session_name
))) {
841 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
842 sizeof(send_session
->hostname
))) {
847 send_session
->id
= htobe64(session
->id
);
848 send_session
->live_timer
= htobe32(session
->live_timer
);
849 if (session
->viewer_attached
) {
850 send_session
->clients
= htobe32(1);
852 send_session
->clients
= htobe32(0);
854 send_session
->streams
= htobe32(session
->stream_count
);
859 session_list
.sessions_count
= htobe32(count
);
861 health_code_update();
863 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
868 health_code_update();
870 ret
= send_response(conn
->sock
, send_session_buf
,
871 count
* sizeof(*send_session_buf
));
875 health_code_update();
879 free(send_session_buf
);
884 * Send the viewer the list of current streams.
887 int viewer_get_new_streams(struct relay_connection
*conn
)
889 int ret
, send_streams
= 0;
890 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
891 struct lttng_viewer_new_streams_request request
;
892 struct lttng_viewer_new_streams_response response
;
893 struct relay_session
*session
;
899 DBG("Get new streams received");
901 health_code_update();
903 /* Receive the request from the connected client. */
904 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
908 session_id
= be64toh(request
.session_id
);
910 health_code_update();
912 memset(&response
, 0, sizeof(response
));
914 session
= session_get_by_id(session_id
);
916 DBG("Relay session %" PRIu64
" not found", session_id
);
917 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
921 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
923 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
928 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
930 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
931 &nb_created
, &closed
);
933 goto end_put_session
;
935 /* Only send back the newly created streams with the unsent ones. */
936 nb_streams
= nb_created
+ nb_unsent
;
937 response
.streams_count
= htobe32(nb_streams
);
940 * If the session is closed, HUP when there are no more streams
943 if (closed
&& nb_total
== 0) {
945 response
.streams_count
= 0;
946 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
951 health_code_update();
952 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
954 goto end_put_session
;
956 health_code_update();
959 * Unknown or empty session, just return gracefully, the viewer
960 * knows what is happening.
962 if (!send_streams
|| !nb_streams
) {
964 goto end_put_session
;
968 * Send stream and *DON'T* ignore the sent flag so every viewer
969 * streams that were not sent from that point will be sent to
972 ret
= send_viewer_streams(conn
->sock
, session
, 0);
974 goto end_put_session
;
979 session_put(session
);
986 * Send the viewer the list of current sessions.
989 int viewer_attach_session(struct relay_connection
*conn
)
991 int send_streams
= 0;
993 uint32_t nb_streams
= 0;
994 enum lttng_viewer_seek seek_type
;
995 struct lttng_viewer_attach_session_request request
;
996 struct lttng_viewer_attach_session_response response
;
997 struct relay_session
*session
= NULL
;
1002 health_code_update();
1004 /* Receive the request from the connected client. */
1005 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1010 health_code_update();
1012 memset(&response
, 0, sizeof(response
));
1014 if (!conn
->viewer_session
) {
1015 DBG("Client trying to attach before creating a live viewer session");
1016 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1020 session
= session_get_by_id(be64toh(request
.session_id
));
1022 DBG("Relay session %" PRIu64
" not found",
1023 be64toh(request
.session_id
));
1024 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1027 DBG("Attach session ID %" PRIu64
" received",
1028 be64toh(request
.session_id
));
1030 if (session
->live_timer
== 0) {
1031 DBG("Not live session");
1032 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1037 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1039 DBG("Already a viewer attached");
1040 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1044 switch (be32toh(request
.seek
)) {
1045 case LTTNG_VIEWER_SEEK_BEGINNING
:
1046 case LTTNG_VIEWER_SEEK_LAST
:
1047 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1048 seek_type
= be32toh(request
.seek
);
1051 ERR("Wrong seek parameter");
1052 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1057 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1060 goto end_put_session
;
1062 response
.streams_count
= htobe32(nb_streams
);
1065 * If the session is closed when the viewer is attaching, it
1066 * means some of the streams may have been concurrently removed,
1067 * so we don't allow the viewer to attach, even if there are
1068 * streams available.
1072 response
.streams_count
= 0;
1073 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1078 health_code_update();
1079 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1081 goto end_put_session
;
1083 health_code_update();
1086 * Unknown or empty session, just return gracefully, the viewer
1087 * knows what is happening.
1089 if (!send_streams
|| !nb_streams
) {
1091 goto end_put_session
;
1094 /* Send stream and ignore the sent flag. */
1095 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1097 goto end_put_session
;
1102 session_put(session
);
1109 * Open the index file if needed for the given vstream.
1111 * If an index file is successfully opened, the vstream index_fd set with
1114 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1116 * Called with rstream lock held.
1118 static int try_open_index(struct relay_viewer_stream
*vstream
,
1119 struct relay_stream
*rstream
)
1123 if (vstream
->index_fd
) {
1128 * First time, we open the index file and at least one index is ready.
1130 if (rstream
->index_received_seqcount
== 0) {
1134 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1135 vstream
->stream
->tracefile_count
,
1136 vstream
->current_tracefile_id
);
1138 vstream
->index_fd
= stream_fd_create(ret
);
1139 if (!vstream
->index_fd
) {
1155 * Check the status of the index for the given stream. This function
1156 * updates the index structure if needed and can put (close) the vstream
1157 * in the HUP situation.
1159 * Return 0 means that we can proceed with the index. A value of 1 means
1160 * that the index has been updated and is ready to be sent to the
1161 * client. A negative value indicates an error that can't be handled.
1163 * Called with rstream lock held.
1165 static int check_index_status(struct relay_viewer_stream
*vstream
,
1166 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1167 struct lttng_viewer_index
*index
)
1171 if (trace
->session
->connection_closed
1172 && rstream
->index_received_seqcount
1173 == vstream
->index_sent_seqcount
) {
1174 /* Last index sent and session connection is closed. */
1175 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1177 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1178 rstream
->index_received_seqcount
1179 == vstream
->index_sent_seqcount
) {
1181 * We've received a synchronization beacon and the last index
1182 * available has been sent, the index for now is inactive.
1184 * In this case, we have received a beacon which allows us to
1185 * inform the client of a time interval during which we can
1186 * guarantee that there are no events to read (and never will
1189 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1190 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1191 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1193 } else if (rstream
->index_received_seqcount
1194 == vstream
->index_sent_seqcount
) {
1196 * This checks whether received == sent seqcount. In
1197 * this case, we have not received a beacon. Therefore,
1198 * we can only ask the client to retry later.
1200 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1202 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1203 vstream
->current_tracefile_id
,
1204 vstream
->index_sent_seqcount
)) {
1206 * The next index we want to send cannot be read either
1207 * because we need to perform a rotation, or due to
1208 * the producer having overwritten its trace file.
1210 DBG("Viewer stream %" PRIu64
" rotation",
1211 vstream
->stream
->stream_handle
);
1212 ret
= viewer_stream_rotate(vstream
);
1215 } else if (ret
== 1) {
1216 /* EOF across entire stream. */
1217 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1221 * If we have been pushed due to overwrite, it
1222 * necessarily means there is data that can be read in
1223 * the stream. If we rotated because we reached the end
1224 * of a tracefile, it means the following tracefile
1225 * needs to contain at least one index, else we would
1226 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1227 * viewer. The updated index_sent_seqcount needs to
1228 * point to a readable index entry now.
1230 * In the case where we "rotate" on a single file, we
1231 * can end up in a case where the requested index is
1232 * still unavailable.
1234 if (rstream
->tracefile_count
== 1 &&
1235 !tracefile_array_seq_in_file(
1237 vstream
->current_tracefile_id
,
1238 vstream
->index_sent_seqcount
)) {
1239 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1242 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1243 vstream
->current_tracefile_id
,
1244 vstream
->index_sent_seqcount
));
1246 /* ret == 0 means successful so we continue. */
1252 viewer_stream_put(vstream
);
1258 * Send the next index for a stream.
1260 * Return 0 on success or else a negative value.
1263 int viewer_get_next_index(struct relay_connection
*conn
)
1267 struct lttng_viewer_get_next_index request_index
;
1268 struct lttng_viewer_index viewer_index
;
1269 struct ctf_packet_index packet_index
;
1270 struct relay_viewer_stream
*vstream
= NULL
;
1271 struct relay_stream
*rstream
= NULL
;
1272 struct ctf_trace
*ctf_trace
= NULL
;
1273 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1277 DBG("Viewer get next index");
1279 memset(&viewer_index
, 0, sizeof(viewer_index
));
1280 health_code_update();
1282 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1286 health_code_update();
1288 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1290 DBG("Client requested index of unknown stream id %" PRIu64
,
1291 be64toh(request_index
.stream_id
));
1292 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1296 /* Use back. ref. Protected by refcounts. */
1297 rstream
= vstream
->stream
;
1298 ctf_trace
= rstream
->trace
;
1300 /* metadata_viewer_stream may be NULL. */
1301 metadata_viewer_stream
=
1302 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1304 pthread_mutex_lock(&rstream
->lock
);
1307 * The viewer should not ask for index on metadata stream.
1309 if (rstream
->is_metadata
) {
1310 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1314 /* Try to open an index if one is needed for that stream. */
1315 ret
= try_open_index(vstream
, rstream
);
1317 if (ret
== -ENOENT
) {
1319 * The index is created only when the first data
1320 * packet arrives, it might not be ready at the
1321 * beginning of the session
1323 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1325 /* Unhandled error. */
1326 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1331 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1334 } else if (ret
== 1) {
1336 * We have no index to send and check_index_status has populated
1337 * viewer_index's status.
1341 /* At this point, ret is 0 thus we will be able to read the index. */
1345 * vstream->stream_fd may be NULL if it has been closed by
1346 * tracefile rotation, or if we are at the beginning of the
1347 * stream. We open the data stream file here to protect against
1348 * overwrite caused by tracefile rotation (in association with
1349 * unlink performed before overwrite).
1351 if (!vstream
->stream_fd
) {
1352 char fullpath
[PATH_MAX
];
1354 if (vstream
->stream
->tracefile_count
> 0) {
1355 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1357 vstream
->channel_name
,
1358 vstream
->current_tracefile_id
);
1360 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1362 vstream
->channel_name
);
1367 ret
= open(fullpath
, O_RDONLY
);
1369 PERROR("Relay opening trace file");
1372 vstream
->stream_fd
= stream_fd_create(ret
);
1373 if (!vstream
->stream_fd
) {
1381 ret
= check_new_streams(conn
);
1383 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1385 } else if (ret
== 1) {
1386 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1389 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1390 sizeof(packet_index
));
1391 if (read_ret
< sizeof(packet_index
)) {
1392 ERR("Relay reading index file %d returned %zd",
1393 vstream
->index_fd
->fd
, read_ret
);
1394 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1397 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1398 vstream
->index_sent_seqcount
++;
1402 * Indexes are stored in big endian, no need to switch before sending.
1404 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1405 rstream
->stream_handle
,
1406 be64toh(packet_index
.offset
));
1407 viewer_index
.offset
= packet_index
.offset
;
1408 viewer_index
.packet_size
= packet_index
.packet_size
;
1409 viewer_index
.content_size
= packet_index
.content_size
;
1410 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1411 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1412 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1413 viewer_index
.stream_id
= packet_index
.stream_id
;
1417 pthread_mutex_unlock(&rstream
->lock
);
1420 if (metadata_viewer_stream
) {
1421 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1422 DBG("get next index metadata check: recv %" PRIu64
1424 metadata_viewer_stream
->stream
->metadata_received
,
1425 metadata_viewer_stream
->metadata_sent
);
1426 if (!metadata_viewer_stream
->stream
->metadata_received
||
1427 metadata_viewer_stream
->stream
->metadata_received
>
1428 metadata_viewer_stream
->metadata_sent
) {
1429 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1431 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1434 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1435 health_code_update();
1437 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1441 health_code_update();
1444 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1445 vstream
->index_sent_seqcount
,
1446 vstream
->stream
->stream_handle
);
1449 if (metadata_viewer_stream
) {
1450 viewer_stream_put(metadata_viewer_stream
);
1453 viewer_stream_put(vstream
);
1458 pthread_mutex_unlock(&rstream
->lock
);
1459 if (metadata_viewer_stream
) {
1460 viewer_stream_put(metadata_viewer_stream
);
1462 viewer_stream_put(vstream
);
1467 * Send the next index for a stream
1469 * Return 0 on success or else a negative value.
1472 int viewer_get_packet(struct relay_connection
*conn
)
1474 int ret
, send_data
= 0;
1478 struct lttng_viewer_get_packet get_packet_info
;
1479 struct lttng_viewer_trace_packet reply
;
1480 struct relay_viewer_stream
*vstream
= NULL
;
1482 DBG2("Relay get data packet");
1484 health_code_update();
1486 ret
= recv_request(conn
->sock
, &get_packet_info
,
1487 sizeof(get_packet_info
));
1491 health_code_update();
1493 /* From this point on, the error label can be reached. */
1494 memset(&reply
, 0, sizeof(reply
));
1496 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1498 DBG("Client requested packet of unknown stream id %" PRIu64
,
1499 be64toh(get_packet_info
.stream_id
));
1500 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1501 goto send_reply_nolock
;
1504 pthread_mutex_lock(&vstream
->stream
->lock
);
1506 len
= be32toh(get_packet_info
.len
);
1507 data
= zmalloc(len
);
1509 PERROR("relay data zmalloc");
1513 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1516 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1517 be64toh(get_packet_info
.offset
));
1520 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1521 if (read_len
< len
) {
1522 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1523 vstream
->stream_fd
->fd
,
1524 be64toh(get_packet_info
.offset
));
1527 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1528 reply
.len
= htobe32(len
);
1533 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1537 pthread_mutex_unlock(&vstream
->stream
->lock
);
1540 reply
.flags
= htobe32(reply
.flags
);
1542 health_code_update();
1544 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1548 health_code_update();
1551 health_code_update();
1552 ret
= send_response(conn
->sock
, data
, len
);
1556 health_code_update();
1559 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1560 be64toh(get_packet_info
.stream_id
));
1566 viewer_stream_put(vstream
);
1572 * Send the session's metadata
1574 * Return 0 on success else a negative value.
1577 int viewer_get_metadata(struct relay_connection
*conn
)
1583 struct lttng_viewer_get_metadata request
;
1584 struct lttng_viewer_metadata_packet reply
;
1585 struct relay_viewer_stream
*vstream
= NULL
;
1589 DBG("Relay get metadata");
1591 health_code_update();
1593 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1597 health_code_update();
1599 memset(&reply
, 0, sizeof(reply
));
1601 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1604 * The metadata stream can be closed by a CLOSE command
1605 * just before we attach. It can also be closed by
1606 * per-pid tracing during tracing. Therefore, it is
1607 * possible that we cannot find this viewer stream.
1608 * Reply back to the client with an error if we cannot
1611 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1612 be64toh(request
.stream_id
));
1613 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1616 pthread_mutex_lock(&vstream
->stream
->lock
);
1617 if (!vstream
->stream
->is_metadata
) {
1618 ERR("Invalid metadata stream");
1622 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1624 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1626 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1630 /* first time, we open the metadata file */
1631 if (!vstream
->stream_fd
) {
1632 char fullpath
[PATH_MAX
];
1634 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1635 vstream
->channel_name
);
1639 ret
= open(fullpath
, O_RDONLY
);
1641 PERROR("Relay opening metadata file");
1644 vstream
->stream_fd
= stream_fd_create(ret
);
1645 if (!vstream
->stream_fd
) {
1653 reply
.len
= htobe64(len
);
1654 data
= zmalloc(len
);
1656 PERROR("viewer metadata zmalloc");
1660 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1661 if (read_len
< len
) {
1662 PERROR("Relay reading metadata file");
1665 vstream
->metadata_sent
+= read_len
;
1666 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1667 && vstream
->stream
->closed
) {
1668 /* Release ownership for the viewer metadata stream. */
1669 viewer_stream_put(vstream
);
1672 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1677 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1680 health_code_update();
1682 pthread_mutex_unlock(&vstream
->stream
->lock
);
1684 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1688 health_code_update();
1691 ret
= send_response(conn
->sock
, data
, len
);
1697 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1698 be64toh(request
.stream_id
));
1700 DBG("Metadata sent");
1706 viewer_stream_put(vstream
);
1712 * Create a viewer session.
1714 * Return 0 on success or else a negative value.
1717 int viewer_create_session(struct relay_connection
*conn
)
1720 struct lttng_viewer_create_session_response resp
;
1722 DBG("Viewer create session received");
1724 memset(&resp
, 0, sizeof(resp
));
1725 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1726 conn
->viewer_session
= viewer_session_create();
1727 if (!conn
->viewer_session
) {
1728 ERR("Allocation viewer session");
1729 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1734 health_code_update();
1735 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1739 health_code_update();
1747 * Detach a viewer session.
1749 * Return 0 on success or else a negative value.
1752 int viewer_detach_session(struct relay_connection
*conn
)
1755 struct lttng_viewer_detach_session_response response
;
1756 struct lttng_viewer_detach_session_request request
;
1757 struct relay_session
*session
= NULL
;
1758 uint64_t viewer_session_to_close
;
1760 DBG("Viewer detach session received");
1764 health_code_update();
1766 /* Receive the request from the connected client. */
1767 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1771 viewer_session_to_close
= be64toh(request
.session_id
);
1773 if (!conn
->viewer_session
) {
1774 DBG("Client trying to detach before creating a live viewer session");
1775 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1779 health_code_update();
1781 memset(&response
, 0, sizeof(response
));
1782 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
1784 session
= session_get_by_id(be64toh(request
.session_id
));
1786 DBG("Relay session %" PRIu64
" not found",
1787 be64toh(request
.session_id
));
1788 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
1792 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
1794 DBG("Not attached to this session");
1795 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1796 goto send_reply_put
;
1799 viewer_session_close_one_session(conn
->viewer_session
, session
);
1800 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
1801 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
1804 session_put(session
);
1807 health_code_update();
1808 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1812 health_code_update();
1820 * live_relay_unknown_command: send -1 if received unknown command
1823 void live_relay_unknown_command(struct relay_connection
*conn
)
1825 struct lttcomm_relayd_generic_reply reply
;
1827 memset(&reply
, 0, sizeof(reply
));
1828 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1829 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1833 * Process the commands received on the control socket
1836 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1837 struct relay_connection
*conn
)
1842 msg_value
= be32toh(recv_hdr
->cmd
);
1845 * Make sure we've done the version check before any command other then a
1846 * new client connection.
1848 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1849 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1854 switch (msg_value
) {
1855 case LTTNG_VIEWER_CONNECT
:
1856 ret
= viewer_connect(conn
);
1858 case LTTNG_VIEWER_LIST_SESSIONS
:
1859 ret
= viewer_list_sessions(conn
);
1861 case LTTNG_VIEWER_ATTACH_SESSION
:
1862 ret
= viewer_attach_session(conn
);
1864 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1865 ret
= viewer_get_next_index(conn
);
1867 case LTTNG_VIEWER_GET_PACKET
:
1868 ret
= viewer_get_packet(conn
);
1870 case LTTNG_VIEWER_GET_METADATA
:
1871 ret
= viewer_get_metadata(conn
);
1873 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1874 ret
= viewer_get_new_streams(conn
);
1876 case LTTNG_VIEWER_CREATE_SESSION
:
1877 ret
= viewer_create_session(conn
);
1879 case LTTNG_VIEWER_DETACH_SESSION
:
1880 ret
= viewer_detach_session(conn
);
1883 ERR("Received unknown viewer command (%u)",
1884 be32toh(recv_hdr
->cmd
));
1885 live_relay_unknown_command(conn
);
1895 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1899 (void) lttng_poll_del(events
, pollfd
);
1901 ret
= close(pollfd
);
1903 ERR("Closing pollfd %d", pollfd
);
1908 * This thread does the actual work
1911 void *thread_worker(void *data
)
1915 struct lttng_poll_event events
;
1916 struct lttng_ht
*viewer_connections_ht
;
1917 struct lttng_ht_iter iter
;
1918 struct lttng_viewer_cmd recv_hdr
;
1919 struct relay_connection
*destroy_conn
;
1921 DBG("[thread] Live viewer relay worker started");
1923 rcu_register_thread();
1925 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1927 if (testpoint(relayd_thread_live_worker
)) {
1928 goto error_testpoint
;
1931 /* table of connections indexed on socket */
1932 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1933 if (!viewer_connections_ht
) {
1934 goto viewer_connections_ht_error
;
1937 ret
= create_thread_poll_set(&events
, 2);
1939 goto error_poll_create
;
1942 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1951 health_code_update();
1953 /* Infinite blocking call, waiting for transmission */
1954 DBG3("Relayd live viewer worker thread polling...");
1955 health_poll_entry();
1956 ret
= lttng_poll_wait(&events
, -1);
1960 * Restart interrupted system call.
1962 if (errno
== EINTR
) {
1971 * Process control. The control connection is prioritised so we don't
1972 * starve it with high throughput tracing data on the data
1975 for (i
= 0; i
< nb_fd
; i
++) {
1976 /* Fetch once the poll data */
1977 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1978 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1980 health_code_update();
1983 /* No activity for this FD (poll implementation). */
1987 /* Thread quit pipe has been closed. Killing thread. */
1988 ret
= check_thread_quit_pipe(pollfd
, revents
);
1994 /* Inspect the relay conn pipe for new connection. */
1995 if (pollfd
== live_conn_pipe
[0]) {
1996 if (revents
& LPOLLIN
) {
1997 struct relay_connection
*conn
;
1999 ret
= lttng_read(live_conn_pipe
[0],
2000 &conn
, sizeof(conn
));
2004 lttng_poll_add(&events
, conn
->sock
->fd
,
2005 LPOLLIN
| LPOLLRDHUP
);
2006 connection_ht_add(viewer_connections_ht
, conn
);
2007 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2008 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2009 ERR("Relay live pipe error");
2012 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2016 /* Connection activity. */
2017 struct relay_connection
*conn
;
2019 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2024 if (revents
& LPOLLIN
) {
2025 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2026 sizeof(recv_hdr
), 0);
2028 /* Connection closed. */
2029 cleanup_connection_pollfd(&events
, pollfd
);
2030 /* Put "create" ownership reference. */
2031 connection_put(conn
);
2032 DBG("Viewer control conn closed with %d", pollfd
);
2034 ret
= process_control(&recv_hdr
, conn
);
2036 /* Clear the session on error. */
2037 cleanup_connection_pollfd(&events
, pollfd
);
2038 /* Put "create" ownership reference. */
2039 connection_put(conn
);
2040 DBG("Viewer connection closed with %d", pollfd
);
2043 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2044 cleanup_connection_pollfd(&events
, pollfd
);
2045 /* Put "create" ownership reference. */
2046 connection_put(conn
);
2048 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2049 connection_put(conn
);
2052 /* Put local "get_by_sock" reference. */
2053 connection_put(conn
);
2060 lttng_poll_clean(&events
);
2062 /* Cleanup reamaining connection object. */
2064 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2067 health_code_update();
2068 connection_put(destroy_conn
);
2072 lttng_ht_destroy(viewer_connections_ht
);
2073 viewer_connections_ht_error
:
2074 /* Close relay conn pipes */
2075 utils_close_pipe(live_conn_pipe
);
2077 DBG("Viewer worker thread exited with error");
2079 DBG("Viewer worker thread cleanup complete");
2083 ERR("Health error occurred in %s", __func__
);
2085 health_unregister(health_relayd
);
2086 if (lttng_relay_stop_threads()) {
2087 ERR("Error stopping threads");
2089 rcu_unregister_thread();
2094 * Create the relay command pipe to wake thread_manage_apps.
2095 * Closed in cleanup().
2097 static int create_conn_pipe(void)
2099 return utils_create_pipe_cloexec(live_conn_pipe
);
2102 int relayd_live_join(void)
2104 int ret
, retval
= 0;
2107 ret
= pthread_join(live_listener_thread
, &status
);
2110 PERROR("pthread_join live listener");
2114 ret
= pthread_join(live_worker_thread
, &status
);
2117 PERROR("pthread_join live worker");
2121 ret
= pthread_join(live_dispatcher_thread
, &status
);
2124 PERROR("pthread_join live dispatcher");
2128 cleanup_relayd_live();
2136 int relayd_live_create(struct lttng_uri
*uri
)
2138 int ret
= 0, retval
= 0;
2144 goto exit_init_data
;
2148 /* Check if daemon is UID = 0 */
2149 is_root
= !getuid();
2152 if (live_uri
->port
< 1024) {
2153 ERR("Need to be root to use ports < 1024");
2155 goto exit_init_data
;
2159 /* Setup the thread apps communication pipe. */
2160 if (create_conn_pipe()) {
2162 goto exit_init_data
;
2165 /* Init relay command queue. */
2166 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2168 /* Set up max poll set size */
2169 if (lttng_poll_set_max_size()) {
2171 goto exit_init_data
;
2174 /* Setup the dispatcher thread */
2175 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2176 thread_dispatcher
, (void *) NULL
);
2179 PERROR("pthread_create viewer dispatcher");
2181 goto exit_dispatcher_thread
;
2184 /* Setup the worker thread */
2185 ret
= pthread_create(&live_worker_thread
, NULL
,
2186 thread_worker
, NULL
);
2189 PERROR("pthread_create viewer worker");
2191 goto exit_worker_thread
;
2194 /* Setup the listener thread */
2195 ret
= pthread_create(&live_listener_thread
, NULL
,
2196 thread_listener
, (void *) NULL
);
2199 PERROR("pthread_create viewer listener");
2201 goto exit_listener_thread
;
2205 * All OK, started all threads.
2210 * Join on the live_listener_thread should anything be added after
2211 * the live_listener thread's creation.
2214 exit_listener_thread
:
2216 ret
= pthread_join(live_worker_thread
, &status
);
2219 PERROR("pthread_join live worker");
2224 ret
= pthread_join(live_dispatcher_thread
, &status
);
2227 PERROR("pthread_join live dispatcher");
2230 exit_dispatcher_thread
:
2233 cleanup_relayd_live();