2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
29 #include <urcu/futex.h>
30 #include <urcu/rculist.h>
31 #include <urcu/uatomic.h>
33 #include <common/common.h>
34 #include <common/compat/endian.h>
35 #include <common/compat/poll.h>
36 #include <common/compat/socket.h>
37 #include <common/defaults.h>
38 #include <common/fd-tracker/utils.h>
39 #include <common/fs-handle.h>
40 #include <common/futex.h>
41 #include <common/index/index.h>
42 #include <common/sessiond-comm/inet.h>
43 #include <common/sessiond-comm/relayd.h>
44 #include <common/sessiond-comm/sessiond-comm.h>
45 #include <common/uri.h>
46 #include <common/utils.h>
47 #include <lttng/lttng.h>
50 #include "connection.h"
51 #include "ctf-trace.h"
52 #include "health-relayd.h"
54 #include "lttng-relayd.h"
57 #include "testpoint.h"
59 #include "viewer-session.h"
60 #include "viewer-stream.h"
62 #define SESSION_BUF_DEFAULT_COUNT 16
64 static struct lttng_uri
*live_uri
;
67 * This pipe is used to inform the worker thread that a command is queued and
68 * ready to be processed.
70 static int live_conn_pipe
[2] = { -1, -1 };
72 /* Shared between threads */
73 static int live_dispatch_thread_exit
;
75 static pthread_t live_listener_thread
;
76 static pthread_t live_dispatcher_thread
;
77 static pthread_t live_worker_thread
;
80 * Relay command queue.
82 * The live_thread_listener and live_thread_dispatcher communicate with this
85 static struct relay_conn_queue viewer_conn_queue
;
87 static uint64_t last_relay_viewer_session_id
;
88 static pthread_mutex_t last_relay_viewer_session_id_lock
=
89 PTHREAD_MUTEX_INITIALIZER
;
95 void cleanup_relayd_live(void)
103 * Receive a request buffer using a given socket, destination allocated buffer
106 * Return the size of the received message or else a negative value on error
107 * with errno being set by recvmsg() syscall.
110 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
114 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
115 if (ret
< 0 || ret
!= size
) {
117 /* Orderly shutdown. Not necessary to print an error. */
118 DBG("Socket %d did an orderly shutdown", sock
->fd
);
120 ERR("Relay failed to receive request.");
129 * Send a response buffer using a given socket, source allocated buffer of
132 * Return the size of the sent message or else a negative value on error with
133 * errno being set by sendmsg() syscall.
136 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
140 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
142 ERR("Relayd failed to send response.");
149 * Atomically check if new streams got added in one of the sessions attached
150 * and reset the flag to 0.
152 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
156 int check_new_streams(struct relay_connection
*conn
)
158 struct relay_session
*session
;
159 unsigned long current_val
;
162 if (!conn
->viewer_session
) {
166 cds_list_for_each_entry_rcu(session
,
167 &conn
->viewer_session
->session_list
,
168 viewer_session_node
) {
169 if (!session_get(session
)) {
172 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
174 session_put(session
);
185 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
186 * this function should ignore the sent flag or not.
188 * Return 0 on success or else a negative value.
191 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
192 uint64_t session_id
, unsigned int ignore_sent_flag
)
195 struct lttng_ht_iter iter
;
196 struct relay_viewer_stream
*vstream
;
200 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
202 struct ctf_trace
*ctf_trace
;
203 struct lttng_viewer_stream send_stream
= {};
205 health_code_update();
207 if (!viewer_stream_get(vstream
)) {
211 pthread_mutex_lock(&vstream
->stream
->lock
);
212 /* Ignore if not the same session. */
213 if (vstream
->stream
->trace
->session
->id
!= session_id
||
214 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
215 pthread_mutex_unlock(&vstream
->stream
->lock
);
216 viewer_stream_put(vstream
);
220 ctf_trace
= vstream
->stream
->trace
;
221 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
222 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
223 send_stream
.metadata_flag
= htobe32(
224 vstream
->stream
->is_metadata
);
225 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
226 sizeof(send_stream
.path_name
))) {
227 pthread_mutex_unlock(&vstream
->stream
->lock
);
228 viewer_stream_put(vstream
);
229 ret
= -1; /* Error. */
232 if (lttng_strncpy(send_stream
.channel_name
,
233 vstream
->channel_name
,
234 sizeof(send_stream
.channel_name
))) {
235 pthread_mutex_unlock(&vstream
->stream
->lock
);
236 viewer_stream_put(vstream
);
237 ret
= -1; /* Error. */
241 DBG("Sending stream %" PRIu64
" to viewer",
242 vstream
->stream
->stream_handle
);
243 vstream
->sent_flag
= 1;
244 pthread_mutex_unlock(&vstream
->stream
->lock
);
246 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
247 viewer_stream_put(vstream
);
261 * Create every viewer stream possible for the given session with the seek
262 * type. Three counters *can* be return which are in order the total amount of
263 * viewer stream of the session, the number of unsent stream and the number of
264 * stream created. Those counters can be NULL and thus will be ignored.
266 * session must be locked to ensure that we see either none or all initial
267 * streams for a session, but no intermediate state..
269 * Return 0 on success or else a negative value.
271 static int make_viewer_streams(struct relay_session
*relay_session
,
272 struct relay_viewer_session
*viewer_session
,
273 enum lttng_viewer_seek seek_t
,
276 uint32_t *nb_created
,
280 struct lttng_ht_iter iter
;
281 struct ctf_trace
*ctf_trace
;
282 struct relay_stream
*relay_stream
= NULL
;
284 assert(relay_session
);
285 ASSERT_LOCKED(relay_session
->lock
);
287 if (relay_session
->connection_closed
) {
292 * Create viewer streams for relay streams that are ready to be
293 * used for a the given session id only.
296 cds_lfht_for_each_entry (relay_session
->ctf_traces_ht
->ht
, &iter
.iter
,
297 ctf_trace
, node
.node
) {
298 bool trace_has_metadata_stream
= false;
300 health_code_update();
302 if (!ctf_trace_get(ctf_trace
)) {
307 * Iterate over all the streams of the trace to see if we have a
310 cds_list_for_each_entry_rcu(relay_stream
,
311 &ctf_trace
->stream_list
, stream_node
)
313 bool is_metadata_stream
;
315 pthread_mutex_lock(&relay_stream
->lock
);
316 is_metadata_stream
= relay_stream
->is_metadata
;
317 pthread_mutex_unlock(&relay_stream
->lock
);
319 if (is_metadata_stream
) {
320 trace_has_metadata_stream
= true;
328 * If there is no metadata stream in this trace at the moment
329 * and we never sent one to the viewer, skip the trace. We
330 * accept that the viewer will not see this trace at all.
332 if (!trace_has_metadata_stream
&&
333 !ctf_trace
->metadata_stream_sent_to_viewer
) {
334 ctf_trace_put(ctf_trace
);
338 cds_list_for_each_entry_rcu(relay_stream
,
339 &ctf_trace
->stream_list
, stream_node
)
341 struct relay_viewer_stream
*viewer_stream
;
343 if (!stream_get(relay_stream
)) {
347 pthread_mutex_lock(&relay_stream
->lock
);
349 * stream published is protected by the session lock.
351 if (!relay_stream
->published
) {
354 viewer_stream
= viewer_stream_get_by_id(
355 relay_stream
->stream_handle
);
356 if (!viewer_stream
) {
357 struct lttng_trace_chunk
*viewer_stream_trace_chunk
= NULL
;
360 * Save that we sent the metadata stream to the
361 * viewer. So that we know what trace the viewer
364 if (relay_stream
->is_metadata
) {
365 ctf_trace
->metadata_stream_sent_to_viewer
= true;
369 * If a rotation is ongoing, use a copy of the
370 * relay stream's chunk to ensure the stream
373 * Otherwise, the viewer session's current trace
374 * chunk can be used safely.
376 if ((relay_stream
->ongoing_rotation
.is_set
||
377 relay_session
->ongoing_rotation
) &&
378 relay_stream
->trace_chunk
) {
379 viewer_stream_trace_chunk
= lttng_trace_chunk_copy(
380 relay_stream
->trace_chunk
);
381 if (!viewer_stream_trace_chunk
) {
383 ctf_trace_put(ctf_trace
);
388 * Transition the viewer session into the newest trace chunk available.
390 if (!lttng_trace_chunk_ids_equal(viewer_session
->current_trace_chunk
,
391 relay_stream
->trace_chunk
)) {
393 ret
= viewer_session_set_trace_chunk_copy(
395 relay_stream
->trace_chunk
);
398 ctf_trace_put(ctf_trace
);
403 if (relay_stream
->trace_chunk
) {
405 * If the corresponding relay
406 * stream's trace chunk is set,
407 * the viewer stream will be
410 * Note that a relay stream can
411 * have a NULL output trace
412 * chunk (for instance, after a
413 * clear against a stopped
416 const bool reference_acquired
= lttng_trace_chunk_get(
417 viewer_session
->current_trace_chunk
);
419 assert(reference_acquired
);
420 viewer_stream_trace_chunk
=
421 viewer_session
->current_trace_chunk
;
425 viewer_stream
= viewer_stream_create(
427 viewer_stream_trace_chunk
,
429 lttng_trace_chunk_put(viewer_stream_trace_chunk
);
430 viewer_stream_trace_chunk
= NULL
;
431 if (!viewer_stream
) {
433 ctf_trace_put(ctf_trace
);
438 /* Update number of created stream counter. */
442 * Ensure a self-reference is preserved even
443 * after we have put our local reference.
445 if (!viewer_stream_get(viewer_stream
)) {
446 ERR("Unable to get self-reference on viewer stream, logic error.");
450 if (!viewer_stream
->sent_flag
&& nb_unsent
) {
451 /* Update number of unsent stream counter. */
455 /* Update number of total stream counter. */
457 if (relay_stream
->is_metadata
) {
458 if (!relay_stream
->closed
||
459 relay_stream
->metadata_received
>
460 viewer_stream
->metadata_sent
) {
464 if (!relay_stream
->closed
||
465 !(((int64_t)(relay_stream
->prev_data_seq
-
466 relay_stream
->last_net_seq_num
)) >=
472 /* Put local reference. */
473 viewer_stream_put(viewer_stream
);
475 pthread_mutex_unlock(&relay_stream
->lock
);
476 stream_put(relay_stream
);
479 ctf_trace_put(ctf_trace
);
488 pthread_mutex_unlock(&relay_stream
->lock
);
489 stream_put(relay_stream
);
495 int relayd_live_stop(void)
497 /* Stop dispatch thread */
498 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
499 futex_nto1_wake(&viewer_conn_queue
.futex
);
504 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
507 int create_named_thread_poll_set(struct lttng_poll_event
*events
,
508 int size
, const char *name
)
512 if (events
== NULL
|| size
== 0) {
517 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
518 name
, events
, 1, LTTNG_CLOEXEC
);
520 PERROR("Failed to create \"%s\" poll file descriptor", name
);
525 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
537 * Check if the thread quit pipe was triggered.
539 * Return 1 if it was triggered else 0;
542 int check_thread_quit_pipe(int fd
, uint32_t events
)
544 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
552 int create_sock(void *data
, int *out_fd
)
555 struct lttcomm_sock
*sock
= data
;
557 ret
= lttcomm_create_sock(sock
);
568 int close_sock(void *data
, int *in_fd
)
570 struct lttcomm_sock
*sock
= data
;
572 return sock
->ops
->close(sock
);
575 static int accept_sock(void *data
, int *out_fd
)
578 /* Socks is an array of in_sock, out_sock. */
579 struct lttcomm_sock
**socks
= data
;
580 struct lttcomm_sock
*in_sock
= socks
[0];
582 socks
[1] = in_sock
->ops
->accept(in_sock
);
587 *out_fd
= socks
[1]->fd
;
593 struct lttcomm_sock
*accept_live_sock(struct lttcomm_sock
*listening_sock
,
597 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
598 struct lttcomm_sock
*new_sock
= NULL
;
600 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &out_fd
,
601 (const char **) &name
, 1, accept_sock
, &socks
);
606 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
612 * Create and init socket from uri.
615 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
, const char *name
)
618 struct lttcomm_sock
*sock
= NULL
;
619 char uri_str
[LTTNG_PATH_MAX
];
620 char *formated_name
= NULL
;
622 sock
= lttcomm_alloc_sock_from_uri(uri
);
624 ERR("Allocating socket");
629 * Don't fail to create the socket if the name can't be built as it is
630 * only used for debugging purposes.
632 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
633 uri_str
[sizeof(uri_str
) - 1] = '\0';
635 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
638 formated_name
= NULL
;
642 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
643 (const char **) (formated_name
? &formated_name
: NULL
),
644 1, create_sock
, sock
);
646 PERROR("Failed to create \"%s\" socket",
647 formated_name
?: "Unknown");
650 DBG("Listening on %s socket %d", name
, sock
->fd
);
652 ret
= sock
->ops
->bind(sock
);
654 PERROR("Failed to bind lttng-live socket");
658 ret
= sock
->ops
->listen(sock
, -1);
669 lttcomm_destroy_sock(sock
);
676 * This thread manages the listening for new connections on the network
679 void *thread_listener(void *data
)
681 int i
, ret
, pollfd
, err
= -1;
682 uint32_t revents
, nb_fd
;
683 struct lttng_poll_event events
;
684 struct lttcomm_sock
*live_control_sock
;
686 DBG("[thread] Relay live listener started");
688 rcu_register_thread();
689 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
691 health_code_update();
693 live_control_sock
= init_socket(live_uri
, "Live listener");
694 if (!live_control_sock
) {
695 goto error_sock_control
;
698 /* Pass 2 as size here for the thread quit pipe and control sockets. */
699 ret
= create_named_thread_poll_set(&events
, 2,
700 "Live listener thread epoll");
702 goto error_create_poll
;
705 /* Add the control socket */
706 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
711 lttng_relay_notify_ready();
713 if (testpoint(relayd_thread_live_listener
)) {
714 goto error_testpoint
;
718 health_code_update();
720 DBG("Listener accepting live viewers connections");
724 ret
= lttng_poll_wait(&events
, -1);
728 * Restart interrupted system call.
730 if (errno
== EINTR
) {
737 DBG("Relay new viewer connection received");
738 for (i
= 0; i
< nb_fd
; i
++) {
739 health_code_update();
741 /* Fetch once the poll data */
742 revents
= LTTNG_POLL_GETEV(&events
, i
);
743 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
745 /* Thread quit pipe has been closed. Killing thread. */
746 ret
= check_thread_quit_pipe(pollfd
, revents
);
752 if (revents
& LPOLLIN
) {
754 * A new connection is requested, therefore a
755 * viewer connection is allocated in this
756 * thread, enqueued to a global queue and
757 * dequeued (and freed) in the worker thread.
760 struct relay_connection
*new_conn
;
761 struct lttcomm_sock
*newsock
;
763 newsock
= accept_live_sock(live_control_sock
,
764 "Live socket to client");
766 PERROR("accepting control sock");
769 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
771 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
774 PERROR("setsockopt inet");
775 lttcomm_destroy_sock(newsock
);
778 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
780 lttcomm_destroy_sock(newsock
);
783 /* Ownership assumed by the connection. */
786 /* Enqueue request for the dispatcher thread. */
787 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
791 * Wake the dispatch queue futex.
792 * Implicit memory barrier with the
793 * exchange in cds_wfcq_enqueue.
795 futex_nto1_wake(&viewer_conn_queue
.futex
);
796 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
797 ERR("socket poll error");
800 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
810 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
812 if (live_control_sock
->fd
>= 0) {
813 int sock_fd
= live_control_sock
->fd
;
815 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
816 &sock_fd
, 1, close_sock
,
821 live_control_sock
->fd
= -1;
823 lttcomm_destroy_sock(live_control_sock
);
827 DBG("Live viewer listener thread exited with error");
829 health_unregister(health_relayd
);
830 rcu_unregister_thread();
831 DBG("Live viewer listener thread cleanup complete");
832 if (lttng_relay_stop_threads()) {
833 ERR("Error stopping threads");
839 * This thread manages the dispatching of the requests to worker threads
842 void *thread_dispatcher(void *data
)
846 struct cds_wfcq_node
*node
;
847 struct relay_connection
*conn
= NULL
;
849 DBG("[thread] Live viewer relay dispatcher started");
851 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
853 if (testpoint(relayd_thread_live_dispatcher
)) {
854 goto error_testpoint
;
857 health_code_update();
860 health_code_update();
862 /* Atomically prepare the queue futex */
863 futex_nto1_prepare(&viewer_conn_queue
.futex
);
865 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
870 health_code_update();
872 /* Dequeue commands */
873 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
874 &viewer_conn_queue
.tail
);
876 DBG("Woken up but nothing in the live-viewer "
877 "relay command queue");
878 /* Continue thread execution */
881 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
882 DBG("Dispatching viewer request waiting on sock %d",
886 * Inform worker thread of the new request. This
887 * call is blocking so we can be assured that
888 * the data will be read at some point in time
889 * or wait to the end of the world :)
891 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
893 PERROR("write conn pipe");
894 connection_put(conn
);
897 } while (node
!= NULL
);
899 /* Futex wait on queue. Blocking call on futex() */
901 futex_nto1_wait(&viewer_conn_queue
.futex
);
905 /* Normal exit, no error */
912 ERR("Health error occurred in %s", __func__
);
914 health_unregister(health_relayd
);
915 DBG("Live viewer dispatch thread dying");
916 if (lttng_relay_stop_threads()) {
917 ERR("Error stopping threads");
923 * Establish connection with the viewer and check the versions.
925 * Return 0 on success or else negative value.
928 int viewer_connect(struct relay_connection
*conn
)
931 struct lttng_viewer_connect reply
, msg
;
933 conn
->version_check_done
= 1;
935 health_code_update();
937 DBG("Viewer is establishing a connection to the relayd.");
939 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
944 health_code_update();
946 memset(&reply
, 0, sizeof(reply
));
947 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
948 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
950 /* Major versions must be the same */
951 if (reply
.major
!= be32toh(msg
.major
)) {
952 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
953 reply
.major
, be32toh(msg
.major
));
958 conn
->major
= reply
.major
;
959 /* We adapt to the lowest compatible version */
960 if (reply
.minor
<= be32toh(msg
.minor
)) {
961 conn
->minor
= reply
.minor
;
963 conn
->minor
= be32toh(msg
.minor
);
966 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
967 conn
->type
= RELAY_VIEWER_COMMAND
;
968 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
969 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
971 ERR("Unknown connection type : %u", be32toh(msg
.type
));
976 reply
.major
= htobe32(reply
.major
);
977 reply
.minor
= htobe32(reply
.minor
);
978 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
980 * Increment outside of htobe64 macro, because the argument can
981 * be used more than once within the macro, and thus the
982 * operation may be undefined.
984 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
985 last_relay_viewer_session_id
++;
986 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
987 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
990 health_code_update();
992 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
997 health_code_update();
999 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
1007 * Send the viewer the list of current sessions.
1008 * We need to create a copy of the hash table content because otherwise
1009 * we cannot assume the number of entries stays the same between getting
1010 * the number of HT elements and iteration over the HT.
1012 * Return 0 on success or else a negative value.
1015 int viewer_list_sessions(struct relay_connection
*conn
)
1018 struct lttng_viewer_list_sessions session_list
;
1019 struct lttng_ht_iter iter
;
1020 struct relay_session
*session
;
1021 struct lttng_viewer_session
*send_session_buf
= NULL
;
1022 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
1025 DBG("List sessions received");
1027 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
1028 if (!send_session_buf
) {
1033 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
1035 struct lttng_viewer_session
*send_session
;
1037 health_code_update();
1039 pthread_mutex_lock(&session
->lock
);
1040 if (session
->connection_closed
) {
1041 /* Skip closed session */
1045 if (count
>= buf_count
) {
1046 struct lttng_viewer_session
*newbuf
;
1047 uint32_t new_buf_count
= buf_count
<< 1;
1049 newbuf
= realloc(send_session_buf
,
1050 new_buf_count
* sizeof(*send_session_buf
));
1055 send_session_buf
= newbuf
;
1056 buf_count
= new_buf_count
;
1058 send_session
= &send_session_buf
[count
];
1059 if (lttng_strncpy(send_session
->session_name
,
1060 session
->session_name
,
1061 sizeof(send_session
->session_name
))) {
1065 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
1066 sizeof(send_session
->hostname
))) {
1070 send_session
->id
= htobe64(session
->id
);
1071 send_session
->live_timer
= htobe32(session
->live_timer
);
1072 if (session
->viewer_attached
) {
1073 send_session
->clients
= htobe32(1);
1075 send_session
->clients
= htobe32(0);
1077 send_session
->streams
= htobe32(session
->stream_count
);
1080 pthread_mutex_unlock(&session
->lock
);
1083 pthread_mutex_unlock(&session
->lock
);
1091 session_list
.sessions_count
= htobe32(count
);
1093 health_code_update();
1095 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
1100 health_code_update();
1102 ret
= send_response(conn
->sock
, send_session_buf
,
1103 count
* sizeof(*send_session_buf
));
1107 health_code_update();
1111 free(send_session_buf
);
1116 * Send the viewer the list of current streams.
1119 int viewer_get_new_streams(struct relay_connection
*conn
)
1121 int ret
, send_streams
= 0;
1122 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
1123 struct lttng_viewer_new_streams_request request
;
1124 struct lttng_viewer_new_streams_response response
;
1125 struct relay_session
*session
= NULL
;
1126 uint64_t session_id
;
1127 bool closed
= false;
1131 DBG("Get new streams received");
1133 health_code_update();
1135 /* Receive the request from the connected client. */
1136 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1140 session_id
= be64toh(request
.session_id
);
1142 health_code_update();
1144 memset(&response
, 0, sizeof(response
));
1146 session
= session_get_by_id(session_id
);
1148 DBG("Relay session %" PRIu64
" not found", session_id
);
1149 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1153 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1154 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1159 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1160 * that at this point the client is already attached to the session.Aany
1161 * initial stream will have been created with the seek type at attach
1162 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1163 * Otherwise any event happening in a new stream between the attach and
1164 * a call to viewer_get_new_streams will be "lost" (never received) from
1165 * the viewer's point of view.
1167 pthread_mutex_lock(&session
->lock
);
1169 * If a session rotation is ongoing, do not attempt to open any
1170 * stream, because the chunk can be in an intermediate state
1171 * due to directory renaming.
1173 if (session
->ongoing_rotation
) {
1174 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1175 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW
);
1176 goto send_reply_unlock
;
1178 ret
= make_viewer_streams(session
,
1179 conn
->viewer_session
,
1180 LTTNG_VIEWER_SEEK_BEGINNING
, &nb_total
, &nb_unsent
,
1181 &nb_created
, &closed
);
1183 goto error_unlock_session
;
1186 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1188 /* Only send back the newly created streams with the unsent ones. */
1189 nb_streams
= nb_created
+ nb_unsent
;
1190 response
.streams_count
= htobe32(nb_streams
);
1193 * If the session is closed, HUP when there are no more streams
1196 if (closed
&& nb_total
== 0) {
1198 response
.streams_count
= 0;
1199 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1200 goto send_reply_unlock
;
1203 pthread_mutex_unlock(&session
->lock
);
1206 health_code_update();
1207 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1209 goto end_put_session
;
1211 health_code_update();
1214 * Unknown or empty session, just return gracefully, the viewer
1215 * knows what is happening.
1217 if (!send_streams
|| !nb_streams
) {
1219 goto end_put_session
;
1223 * Send stream and *DON'T* ignore the sent flag so every viewer
1224 * streams that were not sent from that point will be sent to
1227 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1229 goto end_put_session
;
1234 session_put(session
);
1238 error_unlock_session
:
1239 pthread_mutex_unlock(&session
->lock
);
1240 session_put(session
);
1245 * Send the viewer the list of current sessions.
1248 int viewer_attach_session(struct relay_connection
*conn
)
1250 int send_streams
= 0;
1252 uint32_t nb_streams
= 0;
1253 enum lttng_viewer_seek seek_type
;
1254 struct lttng_viewer_attach_session_request request
;
1255 struct lttng_viewer_attach_session_response response
;
1256 struct relay_session
*session
= NULL
;
1257 enum lttng_viewer_attach_return_code viewer_attach_status
;
1258 bool closed
= false;
1259 uint64_t session_id
;
1263 health_code_update();
1265 /* Receive the request from the connected client. */
1266 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1271 session_id
= be64toh(request
.session_id
);
1272 health_code_update();
1274 memset(&response
, 0, sizeof(response
));
1276 if (!conn
->viewer_session
) {
1277 DBG("Client trying to attach before creating a live viewer session");
1278 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1282 session
= session_get_by_id(session_id
);
1284 DBG("Relay session %" PRIu64
" not found", session_id
);
1285 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1288 DBG("Attach session ID %" PRIu64
" received", session_id
);
1290 pthread_mutex_lock(&session
->lock
);
1291 if (session
->live_timer
== 0) {
1292 DBG("Not live session");
1293 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1298 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1300 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1301 response
.status
= htobe32(viewer_attach_status
);
1305 switch (be32toh(request
.seek
)) {
1306 case LTTNG_VIEWER_SEEK_BEGINNING
:
1307 case LTTNG_VIEWER_SEEK_LAST
:
1308 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1309 seek_type
= be32toh(request
.seek
);
1312 ERR("Wrong seek parameter");
1313 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1319 * If a session rotation is ongoing, do not attempt to open any
1320 * stream, because the chunk can be in an intermediate state
1321 * due to directory renaming.
1323 if (session
->ongoing_rotation
) {
1324 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1329 ret
= make_viewer_streams(session
,
1330 conn
->viewer_session
, seek_type
,
1331 &nb_streams
, NULL
, NULL
, &closed
);
1333 goto end_put_session
;
1335 pthread_mutex_unlock(&session
->lock
);
1336 session_put(session
);
1339 response
.streams_count
= htobe32(nb_streams
);
1341 * If the session is closed when the viewer is attaching, it
1342 * means some of the streams may have been concurrently removed,
1343 * so we don't allow the viewer to attach, even if there are
1344 * streams available.
1348 response
.streams_count
= 0;
1349 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1354 health_code_update();
1355 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1357 goto end_put_session
;
1359 health_code_update();
1362 * Unknown or empty session, just return gracefully, the viewer
1363 * knows what is happening.
1365 if (!send_streams
|| !nb_streams
) {
1367 goto end_put_session
;
1370 /* Send stream and ignore the sent flag. */
1371 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1373 goto end_put_session
;
1378 pthread_mutex_unlock(&session
->lock
);
1379 session_put(session
);
1386 * Open the index file if needed for the given vstream.
1388 * If an index file is successfully opened, the vstream will set it as its
1389 * current index file.
1391 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1393 * Called with rstream lock held.
1395 static int try_open_index(struct relay_viewer_stream
*vstream
,
1396 struct relay_stream
*rstream
)
1399 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1400 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1401 enum lttng_trace_chunk_status chunk_status
;
1403 if (vstream
->index_file
) {
1408 * First time, we open the index file and at least one index is ready.
1410 if (rstream
->index_received_seqcount
== 0 ||
1411 !vstream
->stream_file
.trace_chunk
) {
1416 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1417 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1418 rstream
->channel_name
, rstream
->tracefile_size
,
1419 vstream
->current_tracefile_id
,
1420 lttng_to_index_major(connection_major
, connection_minor
),
1421 lttng_to_index_minor(connection_major
, connection_minor
),
1422 true, &vstream
->index_file
);
1423 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1424 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1436 * Check the status of the index for the given stream. This function
1437 * updates the index structure if needed and can put (close) the vstream
1438 * in the HUP situation.
1440 * Return 0 means that we can proceed with the index. A value of 1 means
1441 * that the index has been updated and is ready to be sent to the
1442 * client. A negative value indicates an error that can't be handled.
1444 * Called with rstream lock held.
1446 static int check_index_status(struct relay_viewer_stream
*vstream
,
1447 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1448 struct lttng_viewer_index
*index
)
1452 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1453 "index_sent_seqcount %" PRIu64
" "
1454 "for stream %" PRIu64
,
1455 rstream
->index_received_seqcount
,
1456 vstream
->index_sent_seqcount
,
1457 vstream
->stream
->stream_handle
);
1458 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1459 && rstream
->index_received_seqcount
1460 == vstream
->index_sent_seqcount
) {
1462 * Last index sent and session connection or relay
1463 * stream are closed.
1465 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1467 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1468 (rstream
->index_received_seqcount
== 0 ||
1469 (vstream
->index_sent_seqcount
!= 0 &&
1470 rstream
->index_received_seqcount
1471 <= vstream
->index_sent_seqcount
))) {
1473 * We've received a synchronization beacon and the last index
1474 * available has been sent, the index for now is inactive.
1476 * In this case, we have received a beacon which allows us to
1477 * inform the client of a time interval during which we can
1478 * guarantee that there are no events to read (and never will
1481 * The sent seqcount can grow higher than receive seqcount on
1482 * clear because the rotation performed by clear will push
1483 * the index_sent_seqcount ahead (see
1484 * viewer_stream_sync_tracefile_array_tail) and skip over
1485 * packet sequence numbers.
1487 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1488 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1489 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1490 DBG("Check index status: inactive with beacon, for stream %" PRIu64
,
1491 vstream
->stream
->stream_handle
);
1493 } else if (rstream
->index_received_seqcount
== 0 ||
1494 (vstream
->index_sent_seqcount
!= 0 &&
1495 rstream
->index_received_seqcount
1496 <= vstream
->index_sent_seqcount
)) {
1498 * This checks whether received <= sent seqcount. In
1499 * this case, we have not received a beacon. Therefore,
1500 * we can only ask the client to retry later.
1502 * The sent seqcount can grow higher than receive seqcount on
1503 * clear because the rotation performed by clear will push
1504 * the index_sent_seqcount ahead (see
1505 * viewer_stream_sync_tracefile_array_tail) and skip over
1506 * packet sequence numbers.
1508 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1509 DBG("Check index status: retry for stream %" PRIu64
,
1510 vstream
->stream
->stream_handle
);
1512 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1513 vstream
->current_tracefile_id
,
1514 vstream
->index_sent_seqcount
)) {
1516 * The next index we want to send cannot be read either
1517 * because we need to perform a rotation, or due to
1518 * the producer having overwritten its trace file.
1520 DBG("Viewer stream %" PRIu64
" rotation",
1521 vstream
->stream
->stream_handle
);
1522 ret
= viewer_stream_rotate(vstream
);
1524 /* EOF across entire stream. */
1525 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1529 * If we have been pushed due to overwrite, it
1530 * necessarily means there is data that can be read in
1531 * the stream. If we rotated because we reached the end
1532 * of a tracefile, it means the following tracefile
1533 * needs to contain at least one index, else we would
1534 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1535 * viewer. The updated index_sent_seqcount needs to
1536 * point to a readable index entry now.
1538 * In the case where we "rotate" on a single file, we
1539 * can end up in a case where the requested index is
1540 * still unavailable.
1542 if (rstream
->tracefile_count
== 1 &&
1543 !tracefile_array_seq_in_file(
1545 vstream
->current_tracefile_id
,
1546 vstream
->index_sent_seqcount
)) {
1547 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1548 DBG("Check index status: retry: "
1549 "tracefile array sequence number %" PRIu64
1550 " not in file for stream %" PRIu64
,
1551 vstream
->index_sent_seqcount
,
1552 vstream
->stream
->stream_handle
);
1555 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1556 vstream
->current_tracefile_id
,
1557 vstream
->index_sent_seqcount
));
1559 /* ret == 0 means successful so we continue. */
1564 viewer_stream_put(vstream
);
1570 void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream
*vstream
,
1571 struct lttng_trace_chunk
*new_trace_chunk
)
1573 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1575 if (new_trace_chunk
) {
1576 const bool acquired_reference
= lttng_trace_chunk_get(
1579 assert(acquired_reference
);
1582 vstream
->stream_file
.trace_chunk
= new_trace_chunk
;
1583 viewer_stream_sync_tracefile_array_tail(vstream
);
1584 viewer_stream_close_files(vstream
);
1588 * Send the next index for a stream.
1590 * Return 0 on success or else a negative value.
1593 int viewer_get_next_index(struct relay_connection
*conn
)
1596 struct lttng_viewer_get_next_index request_index
;
1597 struct lttng_viewer_index viewer_index
;
1598 struct ctf_packet_index packet_index
;
1599 struct relay_viewer_stream
*vstream
= NULL
;
1600 struct relay_stream
*rstream
= NULL
;
1601 struct ctf_trace
*ctf_trace
= NULL
;
1602 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1606 DBG("Viewer get next index");
1608 memset(&viewer_index
, 0, sizeof(viewer_index
));
1609 health_code_update();
1611 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1615 health_code_update();
1617 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1619 DBG("Client requested index of unknown stream id %" PRIu64
,
1620 (uint64_t) be64toh(request_index
.stream_id
));
1621 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1625 /* Use back. ref. Protected by refcounts. */
1626 rstream
= vstream
->stream
;
1627 ctf_trace
= rstream
->trace
;
1629 /* metadata_viewer_stream may be NULL. */
1630 metadata_viewer_stream
=
1631 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1633 pthread_mutex_lock(&rstream
->lock
);
1636 * The viewer should not ask for index on metadata stream.
1638 if (rstream
->is_metadata
) {
1639 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1643 if (rstream
->ongoing_rotation
.is_set
) {
1644 /* Rotation is ongoing, try again later. */
1645 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1649 if (rstream
->trace
->session
->ongoing_rotation
) {
1650 /* Rotation is ongoing, try again later. */
1651 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1656 * Transition the viewer session into the newest trace chunk available.
1658 if (!lttng_trace_chunk_ids_equal(
1659 conn
->viewer_session
->current_trace_chunk
,
1660 rstream
->trace_chunk
)) {
1661 DBG("Relay stream and viewer chunk ids differ");
1663 ret
= viewer_session_set_trace_chunk_copy(
1664 conn
->viewer_session
,
1665 rstream
->trace_chunk
);
1667 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1673 * Transition the viewer stream into the latest trace chunk available.
1675 * Note that the stream must _not_ rotate in one precise condition:
1676 * the relay stream has rotated to a NULL trace chunk and the viewer
1677 * stream is consuming the trace chunk that was active just before
1678 * that rotation to NULL.
1680 * This allows clients to consume all the packets of a trace chunk
1681 * after a session's destruction.
1683 if (!lttng_trace_chunk_ids_equal(conn
->viewer_session
->current_trace_chunk
, vstream
->stream_file
.trace_chunk
) &&
1684 !(rstream
->completed_rotation_count
== vstream
->last_seen_rotation_count
+ 1 && !rstream
->trace_chunk
)) {
1685 DBG("Viewer session and viewer stream chunk IDs differ: "
1686 "vsession chunk %p vstream chunk %p",
1687 conn
->viewer_session
->current_trace_chunk
,
1688 vstream
->stream_file
.trace_chunk
);
1689 viewer_stream_rotate_to_trace_chunk(vstream
,
1690 conn
->viewer_session
->current_trace_chunk
);
1691 vstream
->last_seen_rotation_count
=
1692 rstream
->completed_rotation_count
;
1695 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1698 } else if (ret
== 1) {
1700 * We have no index to send and check_index_status has populated
1701 * viewer_index's status.
1705 /* At this point, ret is 0 thus we will be able to read the index. */
1708 /* Try to open an index if one is needed for that stream. */
1709 ret
= try_open_index(vstream
, rstream
);
1710 if (ret
== -ENOENT
) {
1711 if (rstream
->closed
) {
1712 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1715 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1720 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1725 * vstream->stream_fd may be NULL if it has been closed by
1726 * tracefile rotation, or if we are at the beginning of the
1727 * stream. We open the data stream file here to protect against
1728 * overwrite caused by tracefile rotation (in association with
1729 * unlink performed before overwrite).
1731 if (!vstream
->stream_file
.handle
) {
1732 char file_path
[LTTNG_PATH_MAX
];
1733 enum lttng_trace_chunk_status status
;
1734 struct fs_handle
*fs_handle
;
1736 ret
= utils_stream_file_path(rstream
->path_name
,
1737 rstream
->channel_name
, rstream
->tracefile_size
,
1738 vstream
->current_tracefile_id
, NULL
, file_path
,
1745 * It is possible the the file we are trying to open is
1746 * missing if the stream has been closed (application exits with
1747 * per-pid buffers) and a clear command has been performed.
1749 status
= lttng_trace_chunk_open_fs_handle(
1750 vstream
->stream_file
.trace_chunk
,
1751 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1752 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1753 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1755 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1758 PERROR("Failed to open trace file for viewer stream");
1761 vstream
->stream_file
.handle
= fs_handle
;
1764 ret
= check_new_streams(conn
);
1766 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1768 } else if (ret
== 1) {
1769 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1772 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1774 ERR("Relay error reading index file");
1775 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1778 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1779 vstream
->index_sent_seqcount
++;
1783 * Indexes are stored in big endian, no need to switch before sending.
1785 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1786 rstream
->stream_handle
,
1787 (uint64_t) be64toh(packet_index
.offset
));
1788 viewer_index
.offset
= packet_index
.offset
;
1789 viewer_index
.packet_size
= packet_index
.packet_size
;
1790 viewer_index
.content_size
= packet_index
.content_size
;
1791 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1792 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1793 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1794 viewer_index
.stream_id
= packet_index
.stream_id
;
1798 pthread_mutex_unlock(&rstream
->lock
);
1801 if (metadata_viewer_stream
) {
1802 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1803 DBG("get next index metadata check: recv %" PRIu64
1805 metadata_viewer_stream
->stream
->metadata_received
,
1806 metadata_viewer_stream
->metadata_sent
);
1807 if (!metadata_viewer_stream
->stream
->metadata_received
||
1808 metadata_viewer_stream
->stream
->metadata_received
>
1809 metadata_viewer_stream
->metadata_sent
) {
1810 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1812 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1815 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1816 health_code_update();
1818 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1822 health_code_update();
1825 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1826 vstream
->index_sent_seqcount
,
1827 vstream
->stream
->stream_handle
);
1830 if (metadata_viewer_stream
) {
1831 viewer_stream_put(metadata_viewer_stream
);
1834 viewer_stream_put(vstream
);
1839 pthread_mutex_unlock(&rstream
->lock
);
1840 if (metadata_viewer_stream
) {
1841 viewer_stream_put(metadata_viewer_stream
);
1843 viewer_stream_put(vstream
);
1848 * Send the next index for a stream
1850 * Return 0 on success or else a negative value.
1853 int viewer_get_packet(struct relay_connection
*conn
)
1858 struct lttng_viewer_get_packet get_packet_info
;
1859 struct lttng_viewer_trace_packet reply_header
;
1860 struct relay_viewer_stream
*vstream
= NULL
;
1861 uint32_t reply_size
= sizeof(reply_header
);
1862 uint32_t packet_data_len
= 0;
1866 DBG2("Relay get data packet");
1868 health_code_update();
1870 ret
= recv_request(conn
->sock
, &get_packet_info
,
1871 sizeof(get_packet_info
));
1875 health_code_update();
1877 /* From this point on, the error label can be reached. */
1878 memset(&reply_header
, 0, sizeof(reply_header
));
1879 stream_id
= (uint64_t) be64toh(get_packet_info
.stream_id
);
1881 vstream
= viewer_stream_get_by_id(stream_id
);
1883 DBG("Client requested packet of unknown stream id %" PRIu64
,
1885 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1886 goto send_reply_nolock
;
1888 packet_data_len
= be32toh(get_packet_info
.len
);
1889 reply_size
+= packet_data_len
;
1892 reply
= zmalloc(reply_size
);
1894 PERROR("packet reply zmalloc");
1895 reply_size
= sizeof(reply_header
);
1899 pthread_mutex_lock(&vstream
->stream
->lock
);
1900 lseek_ret
= fs_handle_seek(vstream
->stream_file
.handle
,
1901 be64toh(get_packet_info
.offset
), SEEK_SET
);
1902 if (lseek_ret
< 0) {
1903 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
1904 " to offset %" PRIu64
,
1906 (uint64_t) be64toh(get_packet_info
.offset
));
1909 read_len
= fs_handle_read(vstream
->stream_file
.handle
,
1910 reply
+ sizeof(reply_header
), packet_data_len
);
1911 if (read_len
< packet_data_len
) {
1912 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
1913 ", offset: %" PRIu64
,
1915 (uint64_t) be64toh(get_packet_info
.offset
));
1918 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1919 reply_header
.len
= htobe32(packet_data_len
);
1923 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1927 pthread_mutex_unlock(&vstream
->stream
->lock
);
1931 health_code_update();
1934 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1935 ret
= send_response(conn
->sock
, reply
, reply_size
);
1937 /* No reply to send. */
1938 ret
= send_response(conn
->sock
, &reply_header
,
1942 health_code_update();
1944 PERROR("sendmsg of packet data failed");
1948 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
, stream_id
);
1954 viewer_stream_put(vstream
);
1960 * Send the session's metadata
1962 * Return 0 on success else a negative value.
1965 int viewer_get_metadata(struct relay_connection
*conn
)
1972 struct lttng_viewer_get_metadata request
;
1973 struct lttng_viewer_metadata_packet reply
;
1974 struct relay_viewer_stream
*vstream
= NULL
;
1978 DBG("Relay get metadata");
1980 health_code_update();
1982 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1986 health_code_update();
1988 memset(&reply
, 0, sizeof(reply
));
1990 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1993 * The metadata stream can be closed by a CLOSE command
1994 * just before we attach. It can also be closed by
1995 * per-pid tracing during tracing. Therefore, it is
1996 * possible that we cannot find this viewer stream.
1997 * Reply back to the client with an error if we cannot
2000 DBG("Client requested metadata of unknown stream id %" PRIu64
,
2001 (uint64_t) be64toh(request
.stream_id
));
2002 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2005 pthread_mutex_lock(&vstream
->stream
->lock
);
2006 if (!vstream
->stream
->is_metadata
) {
2007 ERR("Invalid metadata stream");
2011 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
2013 * The live viewers expect to receive a NO_NEW_METADATA
2014 * status before a stream disappears, otherwise they abort the
2015 * entire live connection when receiving an error status.
2017 * Clear feature resets the metadata_sent to 0 until the
2018 * same metadata is received again.
2020 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2022 * The live viewer considers a closed 0 byte metadata stream as
2025 if (vstream
->metadata_sent
> 0) {
2026 if (vstream
->stream
->closed
&& vstream
->stream
->no_new_metadata_notified
) {
2027 /* Release ownership for the viewer metadata stream. */
2028 viewer_stream_put(vstream
);
2030 vstream
->stream
->no_new_metadata_notified
= true;
2035 if (vstream
->stream
->trace_chunk
&&
2036 !lttng_trace_chunk_ids_equal(
2037 conn
->viewer_session
->current_trace_chunk
,
2038 vstream
->stream
->trace_chunk
)) {
2039 /* A rotation has occurred on the relay stream. */
2040 DBG("Metadata relay stream and viewer chunk ids differ");
2042 ret
= viewer_session_set_trace_chunk_copy(
2043 conn
->viewer_session
,
2044 vstream
->stream
->trace_chunk
);
2046 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2051 if (conn
->viewer_session
->current_trace_chunk
&&
2052 !lttng_trace_chunk_ids_equal(conn
->viewer_session
->current_trace_chunk
,
2053 vstream
->stream_file
.trace_chunk
)) {
2054 bool acquired_reference
;
2056 DBG("Viewer session and viewer stream chunk differ: "
2057 "vsession chunk %p vstream chunk %p",
2058 conn
->viewer_session
->current_trace_chunk
,
2059 vstream
->stream_file
.trace_chunk
);
2060 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
2061 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
2062 assert(acquired_reference
);
2063 vstream
->stream_file
.trace_chunk
=
2064 conn
->viewer_session
->current_trace_chunk
;
2065 viewer_stream_close_files(vstream
);
2068 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
2070 if (!vstream
->stream_file
.trace_chunk
) {
2071 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2074 } else if (vstream
->stream_file
.trace_chunk
&&
2075 !vstream
->stream_file
.handle
&& len
> 0) {
2077 * Either this is the first time the metadata file is read, or a
2078 * rotation of the corresponding relay stream has occurred.
2080 struct fs_handle
*fs_handle
;
2081 char file_path
[LTTNG_PATH_MAX
];
2082 enum lttng_trace_chunk_status status
;
2083 struct relay_stream
*rstream
= vstream
->stream
;
2085 ret
= utils_stream_file_path(rstream
->path_name
,
2086 rstream
->channel_name
, rstream
->tracefile_size
,
2087 vstream
->current_tracefile_id
, NULL
, file_path
,
2094 * It is possible the the metadata file we are trying to open is
2095 * missing if the stream has been closed (application exits with
2096 * per-pid buffers) and a clear command has been performed.
2098 status
= lttng_trace_chunk_open_fs_handle(
2099 vstream
->stream_file
.trace_chunk
,
2100 file_path
, O_RDONLY
, 0, &fs_handle
, true);
2101 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2102 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
2103 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2105 if (vstream
->stream
->closed
) {
2106 viewer_stream_put(vstream
);
2110 PERROR("Failed to open metadata file for viewer stream");
2113 vstream
->stream_file
.handle
= fs_handle
;
2115 if (vstream
->metadata_sent
!= 0) {
2117 * The client does not expect to receive any metadata
2118 * it has received and metadata files in successive
2119 * chunks must be a strict superset of one another.
2121 * Skip the first `metadata_sent` bytes to ensure
2122 * they are not sent a second time to the client.
2124 * Baring a block layer error or an internal error,
2125 * this seek should not fail as
2126 * `vstream->stream->metadata_received` is reset when
2127 * a relay stream is rotated. If this is reached, it is
2128 * safe to assume that
2129 * `metadata_received` > `metadata_sent`.
2131 const off_t seek_ret
= fs_handle_seek(fs_handle
,
2132 vstream
->metadata_sent
, SEEK_SET
);
2135 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64
,
2136 vstream
->metadata_sent
);
2137 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2143 reply
.len
= htobe64(len
);
2144 data
= zmalloc(len
);
2146 PERROR("viewer metadata zmalloc");
2150 fd
= fs_handle_get_fd(vstream
->stream_file
.handle
);
2152 ERR("Failed to restore viewer stream file system handle");
2155 read_len
= lttng_read(fd
, data
, len
);
2156 fs_handle_put_fd(vstream
->stream_file
.handle
);
2158 if (read_len
< len
) {
2160 PERROR("Failed to read metadata file");
2164 * A clear has been performed which prevents the relay
2165 * from sending `len` bytes of metadata.
2167 * It is important not to send any metadata if we
2168 * couldn't read all the available metadata in one shot:
2169 * sending partial metadata can cause the client to
2170 * attempt to parse an incomplete (incoherent) metadata
2171 * stream, which would result in an error.
2173 const off_t seek_ret
= fs_handle_seek(
2174 vstream
->stream_file
.handle
, -read_len
,
2177 DBG("Failed to read metadata: requested = %" PRIu64
", got = %zd",
2182 PERROR("Failed to restore metadata file position after partial read");
2188 vstream
->metadata_sent
+= read_len
;
2189 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
2194 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2197 health_code_update();
2199 pthread_mutex_unlock(&vstream
->stream
->lock
);
2201 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
2205 health_code_update();
2208 ret
= send_response(conn
->sock
, data
, len
);
2214 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
2215 (uint64_t) be64toh(request
.stream_id
));
2217 DBG("Metadata sent");
2223 viewer_stream_put(vstream
);
2229 * Create a viewer session.
2231 * Return 0 on success or else a negative value.
2234 int viewer_create_session(struct relay_connection
*conn
)
2237 struct lttng_viewer_create_session_response resp
;
2239 DBG("Viewer create session received");
2241 memset(&resp
, 0, sizeof(resp
));
2242 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
2243 conn
->viewer_session
= viewer_session_create();
2244 if (!conn
->viewer_session
) {
2245 ERR("Allocation viewer session");
2246 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
2251 health_code_update();
2252 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
2256 health_code_update();
2264 * Detach a viewer session.
2266 * Return 0 on success or else a negative value.
2269 int viewer_detach_session(struct relay_connection
*conn
)
2272 struct lttng_viewer_detach_session_response response
;
2273 struct lttng_viewer_detach_session_request request
;
2274 struct relay_session
*session
= NULL
;
2275 uint64_t viewer_session_to_close
;
2277 DBG("Viewer detach session received");
2281 health_code_update();
2283 /* Receive the request from the connected client. */
2284 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2288 viewer_session_to_close
= be64toh(request
.session_id
);
2290 if (!conn
->viewer_session
) {
2291 DBG("Client trying to detach before creating a live viewer session");
2292 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2296 health_code_update();
2298 memset(&response
, 0, sizeof(response
));
2299 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2301 session
= session_get_by_id(be64toh(request
.session_id
));
2303 DBG("Relay session %" PRIu64
" not found",
2304 (uint64_t) be64toh(request
.session_id
));
2305 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2309 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2311 DBG("Not attached to this session");
2312 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2313 goto send_reply_put
;
2316 viewer_session_close_one_session(conn
->viewer_session
, session
);
2317 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2318 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2321 session_put(session
);
2324 health_code_update();
2325 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2329 health_code_update();
2337 * live_relay_unknown_command: send -1 if received unknown command
2340 void live_relay_unknown_command(struct relay_connection
*conn
)
2342 struct lttcomm_relayd_generic_reply reply
;
2344 memset(&reply
, 0, sizeof(reply
));
2345 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2346 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2350 * Process the commands received on the control socket
2353 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2354 struct relay_connection
*conn
)
2359 msg_value
= be32toh(recv_hdr
->cmd
);
2362 * Make sure we've done the version check before any command other then a
2363 * new client connection.
2365 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2366 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
2371 switch (msg_value
) {
2372 case LTTNG_VIEWER_CONNECT
:
2373 ret
= viewer_connect(conn
);
2375 case LTTNG_VIEWER_LIST_SESSIONS
:
2376 ret
= viewer_list_sessions(conn
);
2378 case LTTNG_VIEWER_ATTACH_SESSION
:
2379 ret
= viewer_attach_session(conn
);
2381 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2382 ret
= viewer_get_next_index(conn
);
2384 case LTTNG_VIEWER_GET_PACKET
:
2385 ret
= viewer_get_packet(conn
);
2387 case LTTNG_VIEWER_GET_METADATA
:
2388 ret
= viewer_get_metadata(conn
);
2390 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2391 ret
= viewer_get_new_streams(conn
);
2393 case LTTNG_VIEWER_CREATE_SESSION
:
2394 ret
= viewer_create_session(conn
);
2396 case LTTNG_VIEWER_DETACH_SESSION
:
2397 ret
= viewer_detach_session(conn
);
2400 ERR("Received unknown viewer command (%u)",
2401 be32toh(recv_hdr
->cmd
));
2402 live_relay_unknown_command(conn
);
2412 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2416 (void) lttng_poll_del(events
, pollfd
);
2418 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &pollfd
, 1,
2419 fd_tracker_util_close_fd
, NULL
);
2421 ERR("Closing pollfd %d", pollfd
);
2426 * This thread does the actual work
2429 void *thread_worker(void *data
)
2433 struct lttng_poll_event events
;
2434 struct lttng_ht
*viewer_connections_ht
;
2435 struct lttng_ht_iter iter
;
2436 struct lttng_viewer_cmd recv_hdr
;
2437 struct relay_connection
*destroy_conn
;
2439 DBG("[thread] Live viewer relay worker started");
2441 rcu_register_thread();
2443 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2445 if (testpoint(relayd_thread_live_worker
)) {
2446 goto error_testpoint
;
2449 /* table of connections indexed on socket */
2450 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2451 if (!viewer_connections_ht
) {
2452 goto viewer_connections_ht_error
;
2455 ret
= create_named_thread_poll_set(&events
, 2,
2456 "Live viewer worker thread epoll");
2458 goto error_poll_create
;
2461 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2470 health_code_update();
2472 /* Infinite blocking call, waiting for transmission */
2473 DBG3("Relayd live viewer worker thread polling...");
2474 health_poll_entry();
2475 ret
= lttng_poll_wait(&events
, -1);
2479 * Restart interrupted system call.
2481 if (errno
== EINTR
) {
2490 * Process control. The control connection is prioritised so we don't
2491 * starve it with high throughput tracing data on the data
2494 for (i
= 0; i
< nb_fd
; i
++) {
2495 /* Fetch once the poll data */
2496 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2497 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2499 health_code_update();
2501 /* Thread quit pipe has been closed. Killing thread. */
2502 ret
= check_thread_quit_pipe(pollfd
, revents
);
2508 /* Inspect the relay conn pipe for new connection. */
2509 if (pollfd
== live_conn_pipe
[0]) {
2510 if (revents
& LPOLLIN
) {
2511 struct relay_connection
*conn
;
2513 ret
= lttng_read(live_conn_pipe
[0],
2514 &conn
, sizeof(conn
));
2518 ret
= lttng_poll_add(&events
,
2520 LPOLLIN
| LPOLLRDHUP
);
2522 ERR("Failed to add new live connection file descriptor to poll set");
2525 connection_ht_add(viewer_connections_ht
, conn
);
2526 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2527 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2528 ERR("Relay live pipe error");
2531 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2535 /* Connection activity. */
2536 struct relay_connection
*conn
;
2538 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2543 if (revents
& LPOLLIN
) {
2544 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2545 sizeof(recv_hdr
), 0);
2547 /* Connection closed. */
2548 cleanup_connection_pollfd(&events
, pollfd
);
2549 /* Put "create" ownership reference. */
2550 connection_put(conn
);
2551 DBG("Viewer control conn closed with %d", pollfd
);
2553 ret
= process_control(&recv_hdr
, conn
);
2555 /* Clear the session on error. */
2556 cleanup_connection_pollfd(&events
, pollfd
);
2557 /* Put "create" ownership reference. */
2558 connection_put(conn
);
2559 DBG("Viewer connection closed with %d", pollfd
);
2562 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2563 cleanup_connection_pollfd(&events
, pollfd
);
2564 /* Put "create" ownership reference. */
2565 connection_put(conn
);
2567 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2568 connection_put(conn
);
2571 /* Put local "get_by_sock" reference. */
2572 connection_put(conn
);
2579 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
2581 /* Cleanup remaining connection object. */
2583 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2586 health_code_update();
2587 connection_put(destroy_conn
);
2591 lttng_ht_destroy(viewer_connections_ht
);
2592 viewer_connections_ht_error
:
2593 /* Close relay conn pipes */
2594 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2596 DBG("Viewer worker thread exited with error");
2598 DBG("Viewer worker thread cleanup complete");
2602 ERR("Health error occurred in %s", __func__
);
2604 health_unregister(health_relayd
);
2605 if (lttng_relay_stop_threads()) {
2606 ERR("Error stopping threads");
2608 rcu_unregister_thread();
2613 * Create the relay command pipe to wake thread_manage_apps.
2614 * Closed in cleanup().
2616 static int create_conn_pipe(void)
2618 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2619 "Live connection pipe", live_conn_pipe
);
2622 int relayd_live_join(void)
2624 int ret
, retval
= 0;
2627 ret
= pthread_join(live_listener_thread
, &status
);
2630 PERROR("pthread_join live listener");
2634 ret
= pthread_join(live_worker_thread
, &status
);
2637 PERROR("pthread_join live worker");
2641 ret
= pthread_join(live_dispatcher_thread
, &status
);
2644 PERROR("pthread_join live dispatcher");
2648 cleanup_relayd_live();
2656 int relayd_live_create(struct lttng_uri
*uri
)
2658 int ret
= 0, retval
= 0;
2664 goto exit_init_data
;
2668 /* Check if daemon is UID = 0 */
2669 is_root
= !getuid();
2672 if (live_uri
->port
< 1024) {
2673 ERR("Need to be root to use ports < 1024");
2675 goto exit_init_data
;
2679 /* Setup the thread apps communication pipe. */
2680 if (create_conn_pipe()) {
2682 goto exit_init_data
;
2685 /* Init relay command queue. */
2686 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2688 /* Set up max poll set size */
2689 if (lttng_poll_set_max_size()) {
2691 goto exit_init_data
;
2694 /* Setup the dispatcher thread */
2695 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2696 thread_dispatcher
, (void *) NULL
);
2699 PERROR("pthread_create viewer dispatcher");
2701 goto exit_dispatcher_thread
;
2704 /* Setup the worker thread */
2705 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2706 thread_worker
, NULL
);
2709 PERROR("pthread_create viewer worker");
2711 goto exit_worker_thread
;
2714 /* Setup the listener thread */
2715 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2716 thread_listener
, (void *) NULL
);
2719 PERROR("pthread_create viewer listener");
2721 goto exit_listener_thread
;
2725 * All OK, started all threads.
2730 * Join on the live_listener_thread should anything be added after
2731 * the live_listener thread's creation.
2734 exit_listener_thread
:
2736 ret
= pthread_join(live_worker_thread
, &status
);
2739 PERROR("pthread_join live worker");
2744 ret
= pthread_join(live_dispatcher_thread
, &status
);
2747 PERROR("pthread_join live dispatcher");
2750 exit_dispatcher_thread
:
2753 cleanup_relayd_live();