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>
34 #include <common/common.hpp>
35 #include <common/compat/endian.hpp>
36 #include <common/compat/poll.hpp>
37 #include <common/compat/socket.hpp>
38 #include <common/defaults.hpp>
39 #include <common/fd-tracker/utils.hpp>
40 #include <common/fs-handle.hpp>
41 #include <common/futex.hpp>
42 #include <common/index/index.hpp>
43 #include <common/sessiond-comm/inet.hpp>
44 #include <common/sessiond-comm/relayd.hpp>
45 #include <common/sessiond-comm/sessiond-comm.hpp>
46 #include <common/uri.hpp>
47 #include <common/utils.hpp>
48 #include <lttng/lttng.h>
51 #include "connection.hpp"
52 #include "ctf-trace.hpp"
53 #include "health-relayd.hpp"
55 #include "lttng-relayd.hpp"
56 #include "session.hpp"
58 #include "testpoint.hpp"
60 #include "viewer-session.hpp"
61 #include "viewer-stream.hpp"
63 #define SESSION_BUF_DEFAULT_COUNT 16
65 static struct lttng_uri
*live_uri
;
68 * This pipe is used to inform the worker thread that a command is queued and
69 * ready to be processed.
71 static int live_conn_pipe
[2] = { -1, -1 };
73 /* Shared between threads */
74 static int live_dispatch_thread_exit
;
76 static pthread_t live_listener_thread
;
77 static pthread_t live_dispatcher_thread
;
78 static pthread_t live_worker_thread
;
81 * Relay command queue.
83 * The live_thread_listener and live_thread_dispatcher communicate with this
86 static struct relay_conn_queue viewer_conn_queue
;
88 static uint64_t last_relay_viewer_session_id
;
89 static pthread_mutex_t last_relay_viewer_session_id_lock
=
90 PTHREAD_MUTEX_INITIALIZER
;
93 const char *lttng_viewer_command_str(lttng_viewer_command cmd
)
96 case LTTNG_VIEWER_CONNECT
:
98 case LTTNG_VIEWER_LIST_SESSIONS
:
99 return "LIST_SESSIONS";
100 case LTTNG_VIEWER_ATTACH_SESSION
:
101 return "ATTACH_SESSION";
102 case LTTNG_VIEWER_GET_NEXT_INDEX
:
103 return "GET_NEXT_INDEX";
104 case LTTNG_VIEWER_GET_PACKET
:
106 case LTTNG_VIEWER_GET_METADATA
:
107 return "GET_METADATA";
108 case LTTNG_VIEWER_GET_NEW_STREAMS
:
109 return "GET_NEW_STREAMS";
110 case LTTNG_VIEWER_CREATE_SESSION
:
111 return "CREATE_SESSION";
112 case LTTNG_VIEWER_DETACH_SESSION
:
113 return "DETACH_SESSION";
120 const char *lttng_viewer_next_index_return_code_str(
121 enum lttng_viewer_next_index_return_code code
)
124 case LTTNG_VIEWER_INDEX_OK
:
126 case LTTNG_VIEWER_INDEX_RETRY
:
127 return "INDEX_RETRY";
128 case LTTNG_VIEWER_INDEX_HUP
:
130 case LTTNG_VIEWER_INDEX_ERR
:
132 case LTTNG_VIEWER_INDEX_INACTIVE
:
133 return "INDEX_INACTIVE";
134 case LTTNG_VIEWER_INDEX_EOF
:
142 const char *lttng_viewer_attach_return_code_str(
143 enum lttng_viewer_attach_return_code code
)
146 case LTTNG_VIEWER_ATTACH_OK
:
148 case LTTNG_VIEWER_ATTACH_ALREADY
:
149 return "ATTACH_ALREADY";
150 case LTTNG_VIEWER_ATTACH_UNK
:
152 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
153 return "ATTACH_NOT_LIVE";
154 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
155 return "ATTACH_SEEK_ERR";
156 case LTTNG_VIEWER_ATTACH_NO_SESSION
:
157 return "ATTACH_NO_SESSION";
164 const char *lttng_viewer_get_packet_return_code_str(
165 enum lttng_viewer_get_packet_return_code code
)
168 case LTTNG_VIEWER_GET_PACKET_OK
:
169 return "GET_PACKET_OK";
170 case LTTNG_VIEWER_GET_PACKET_RETRY
:
171 return "GET_PACKET_RETRY";
172 case LTTNG_VIEWER_GET_PACKET_ERR
:
173 return "GET_PACKET_ERR";
174 case LTTNG_VIEWER_GET_PACKET_EOF
:
175 return "GET_PACKET_EOF";
185 void cleanup_relayd_live(void)
193 * Receive a request buffer using a given socket, destination allocated buffer
196 * Return the size of the received message or else a negative value on error
197 * with errno being set by recvmsg() syscall.
200 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
204 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
205 if (ret
< 0 || ret
!= size
) {
207 /* Orderly shutdown. Not necessary to print an error. */
208 DBG("Socket %d did an orderly shutdown", sock
->fd
);
210 ERR("Relay failed to receive request.");
219 * Send a response buffer using a given socket, source allocated buffer of
222 * Return the size of the sent message or else a negative value on error with
223 * errno being set by sendmsg() syscall.
226 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
230 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
232 ERR("Relayd failed to send response.");
239 * Atomically check if new streams got added in one of the sessions attached
240 * and reset the flag to 0.
242 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
246 int check_new_streams(struct relay_connection
*conn
)
248 struct relay_session
*session
;
249 unsigned long current_val
;
252 if (!conn
->viewer_session
) {
256 cds_list_for_each_entry_rcu(session
,
257 &conn
->viewer_session
->session_list
,
258 viewer_session_node
) {
259 if (!session_get(session
)) {
262 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
264 session_put(session
);
275 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
276 * this function should ignore the sent flag or not.
278 * Return 0 on success or else a negative value.
281 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
282 uint64_t session_id
, unsigned int ignore_sent_flag
)
285 struct lttng_ht_iter iter
;
286 struct relay_viewer_stream
*vstream
;
290 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
292 struct ctf_trace
*ctf_trace
;
293 struct lttng_viewer_stream send_stream
= {};
295 health_code_update();
297 if (!viewer_stream_get(vstream
)) {
301 pthread_mutex_lock(&vstream
->stream
->lock
);
302 /* Ignore if not the same session. */
303 if (vstream
->stream
->trace
->session
->id
!= session_id
||
304 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
305 pthread_mutex_unlock(&vstream
->stream
->lock
);
306 viewer_stream_put(vstream
);
310 ctf_trace
= vstream
->stream
->trace
;
311 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
312 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
313 send_stream
.metadata_flag
= htobe32(
314 vstream
->stream
->is_metadata
);
315 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
316 sizeof(send_stream
.path_name
))) {
317 pthread_mutex_unlock(&vstream
->stream
->lock
);
318 viewer_stream_put(vstream
);
319 ret
= -1; /* Error. */
322 if (lttng_strncpy(send_stream
.channel_name
,
323 vstream
->channel_name
,
324 sizeof(send_stream
.channel_name
))) {
325 pthread_mutex_unlock(&vstream
->stream
->lock
);
326 viewer_stream_put(vstream
);
327 ret
= -1; /* Error. */
331 DBG("Sending stream %" PRIu64
" to viewer",
332 vstream
->stream
->stream_handle
);
333 vstream
->sent_flag
= 1;
334 pthread_mutex_unlock(&vstream
->stream
->lock
);
336 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
337 viewer_stream_put(vstream
);
351 * Create every viewer stream possible for the given session with the seek
352 * type. Three counters *can* be return which are in order the total amount of
353 * viewer stream of the session, the number of unsent stream and the number of
354 * stream created. Those counters can be NULL and thus will be ignored.
356 * session must be locked to ensure that we see either none or all initial
357 * streams for a session, but no intermediate state..
359 * Return 0 on success or else a negative value.
361 static int make_viewer_streams(struct relay_session
*relay_session
,
362 struct relay_viewer_session
*viewer_session
,
363 enum lttng_viewer_seek seek_t
,
366 uint32_t *nb_created
,
370 struct lttng_ht_iter iter
;
371 struct ctf_trace
*ctf_trace
;
372 struct relay_stream
*relay_stream
= NULL
;
374 LTTNG_ASSERT(relay_session
);
375 ASSERT_LOCKED(relay_session
->lock
);
377 if (relay_session
->connection_closed
) {
382 * Create viewer streams for relay streams that are ready to be
383 * used for a the given session id only.
386 cds_lfht_for_each_entry (relay_session
->ctf_traces_ht
->ht
, &iter
.iter
,
387 ctf_trace
, node
.node
) {
388 bool trace_has_metadata_stream
= false;
390 health_code_update();
392 if (!ctf_trace_get(ctf_trace
)) {
397 * Iterate over all the streams of the trace to see if we have a
400 cds_list_for_each_entry_rcu(relay_stream
,
401 &ctf_trace
->stream_list
, stream_node
)
403 bool is_metadata_stream
;
405 pthread_mutex_lock(&relay_stream
->lock
);
406 is_metadata_stream
= relay_stream
->is_metadata
;
407 pthread_mutex_unlock(&relay_stream
->lock
);
409 if (is_metadata_stream
) {
410 trace_has_metadata_stream
= true;
418 * If there is no metadata stream in this trace at the moment
419 * and we never sent one to the viewer, skip the trace. We
420 * accept that the viewer will not see this trace at all.
422 if (!trace_has_metadata_stream
&&
423 !ctf_trace
->metadata_stream_sent_to_viewer
) {
424 ctf_trace_put(ctf_trace
);
428 cds_list_for_each_entry_rcu(relay_stream
,
429 &ctf_trace
->stream_list
, stream_node
)
431 struct relay_viewer_stream
*viewer_stream
;
433 if (!stream_get(relay_stream
)) {
437 pthread_mutex_lock(&relay_stream
->lock
);
439 * stream published is protected by the session lock.
441 if (!relay_stream
->published
) {
444 viewer_stream
= viewer_stream_get_by_id(
445 relay_stream
->stream_handle
);
446 if (!viewer_stream
) {
447 struct lttng_trace_chunk
*viewer_stream_trace_chunk
= NULL
;
450 * Save that we sent the metadata stream to the
451 * viewer. So that we know what trace the viewer
454 if (relay_stream
->is_metadata
) {
455 ctf_trace
->metadata_stream_sent_to_viewer
= true;
459 * If a rotation is ongoing, use a copy of the
460 * relay stream's chunk to ensure the stream
463 * Otherwise, the viewer session's current trace
464 * chunk can be used safely.
466 if ((relay_stream
->ongoing_rotation
.is_set
||
467 session_has_ongoing_rotation(relay_session
)) &&
468 relay_stream
->trace_chunk
) {
469 viewer_stream_trace_chunk
= lttng_trace_chunk_copy(
470 relay_stream
->trace_chunk
);
471 if (!viewer_stream_trace_chunk
) {
473 ctf_trace_put(ctf_trace
);
478 * Transition the viewer session into the newest trace chunk available.
480 if (!lttng_trace_chunk_ids_equal(viewer_session
->current_trace_chunk
,
481 relay_stream
->trace_chunk
)) {
483 ret
= viewer_session_set_trace_chunk_copy(
485 relay_stream
->trace_chunk
);
488 ctf_trace_put(ctf_trace
);
493 if (relay_stream
->trace_chunk
) {
495 * If the corresponding relay
496 * stream's trace chunk is set,
497 * the viewer stream will be
500 * Note that a relay stream can
501 * have a NULL output trace
502 * chunk (for instance, after a
503 * clear against a stopped
506 const bool reference_acquired
= lttng_trace_chunk_get(
507 viewer_session
->current_trace_chunk
);
509 LTTNG_ASSERT(reference_acquired
);
510 viewer_stream_trace_chunk
=
511 viewer_session
->current_trace_chunk
;
515 viewer_stream
= viewer_stream_create(
517 viewer_stream_trace_chunk
,
519 lttng_trace_chunk_put(viewer_stream_trace_chunk
);
520 viewer_stream_trace_chunk
= NULL
;
521 if (!viewer_stream
) {
523 ctf_trace_put(ctf_trace
);
528 /* Update number of created stream counter. */
532 * Ensure a self-reference is preserved even
533 * after we have put our local reference.
535 if (!viewer_stream_get(viewer_stream
)) {
536 ERR("Unable to get self-reference on viewer stream, logic error.");
540 if (!viewer_stream
->sent_flag
&& nb_unsent
) {
541 /* Update number of unsent stream counter. */
545 /* Update number of total stream counter. */
547 if (relay_stream
->is_metadata
) {
548 if (!relay_stream
->closed
||
549 relay_stream
->metadata_received
>
550 viewer_stream
->metadata_sent
) {
554 if (!relay_stream
->closed
||
555 !(((int64_t)(relay_stream
->prev_data_seq
-
556 relay_stream
->last_net_seq_num
)) >=
562 /* Put local reference. */
563 viewer_stream_put(viewer_stream
);
565 pthread_mutex_unlock(&relay_stream
->lock
);
566 stream_put(relay_stream
);
569 ctf_trace_put(ctf_trace
);
578 pthread_mutex_unlock(&relay_stream
->lock
);
579 stream_put(relay_stream
);
585 int relayd_live_stop(void)
587 /* Stop dispatch thread */
588 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
589 futex_nto1_wake(&viewer_conn_queue
.futex
);
594 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
597 int create_named_thread_poll_set(struct lttng_poll_event
*events
,
598 int size
, const char *name
)
602 if (events
== NULL
|| size
== 0) {
607 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
608 name
, events
, 1, LTTNG_CLOEXEC
);
610 PERROR("Failed to create \"%s\" poll file descriptor", name
);
615 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
627 * Check if the thread quit pipe was triggered.
629 * Return 1 if it was triggered else 0;
632 int check_thread_quit_pipe(int fd
, uint32_t events
)
634 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
642 int create_sock(void *data
, int *out_fd
)
645 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
647 ret
= lttcomm_create_sock(sock
);
658 int close_sock(void *data
, int *in_fd
__attribute__((unused
)))
660 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
662 return sock
->ops
->close(sock
);
665 static int accept_sock(void *data
, int *out_fd
)
668 /* Socks is an array of in_sock, out_sock. */
669 struct lttcomm_sock
**socks
= (lttcomm_sock
**) data
;
670 struct lttcomm_sock
*in_sock
= socks
[0];
672 socks
[1] = in_sock
->ops
->accept(in_sock
);
677 *out_fd
= socks
[1]->fd
;
683 struct lttcomm_sock
*accept_live_sock(struct lttcomm_sock
*listening_sock
,
687 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
688 struct lttcomm_sock
*new_sock
= NULL
;
690 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &out_fd
,
691 (const char **) &name
, 1, accept_sock
, &socks
);
696 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
702 * Create and init socket from uri.
705 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
, const char *name
)
708 struct lttcomm_sock
*sock
= NULL
;
709 char uri_str
[LTTNG_PATH_MAX
];
710 char *formated_name
= NULL
;
712 sock
= lttcomm_alloc_sock_from_uri(uri
);
714 ERR("Allocating socket");
719 * Don't fail to create the socket if the name can't be built as it is
720 * only used for debugging purposes.
722 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
723 uri_str
[sizeof(uri_str
) - 1] = '\0';
725 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
728 formated_name
= NULL
;
732 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
733 (const char **) (formated_name
? &formated_name
: NULL
),
734 1, create_sock
, sock
);
736 PERROR("Failed to create \"%s\" socket",
737 formated_name
?: "Unknown");
740 DBG("Listening on %s socket %d", name
, sock
->fd
);
742 ret
= sock
->ops
->bind(sock
);
744 PERROR("Failed to bind lttng-live socket");
748 ret
= sock
->ops
->listen(sock
, -1);
759 lttcomm_destroy_sock(sock
);
766 * This thread manages the listening for new connections on the network
769 void *thread_listener(void *data
__attribute__((unused
)))
771 int i
, ret
, pollfd
, err
= -1;
772 uint32_t revents
, nb_fd
;
773 struct lttng_poll_event events
;
774 struct lttcomm_sock
*live_control_sock
;
776 DBG("[thread] Relay live listener started");
778 rcu_register_thread();
779 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
781 health_code_update();
783 live_control_sock
= init_socket(live_uri
, "Live listener");
784 if (!live_control_sock
) {
785 goto error_sock_control
;
788 /* Pass 2 as size here for the thread quit pipe and control sockets. */
789 ret
= create_named_thread_poll_set(&events
, 2,
790 "Live listener thread epoll");
792 goto error_create_poll
;
795 /* Add the control socket */
796 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
801 lttng_relay_notify_ready();
803 if (testpoint(relayd_thread_live_listener
)) {
804 goto error_testpoint
;
808 health_code_update();
810 DBG("Listener accepting live viewers connections");
814 ret
= lttng_poll_wait(&events
, -1);
818 * Restart interrupted system call.
820 if (errno
== EINTR
) {
827 DBG("Relay new viewer connection received");
828 for (i
= 0; i
< nb_fd
; i
++) {
829 health_code_update();
831 /* Fetch once the poll data */
832 revents
= LTTNG_POLL_GETEV(&events
, i
);
833 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
835 /* Thread quit pipe has been closed. Killing thread. */
836 ret
= check_thread_quit_pipe(pollfd
, revents
);
842 if (revents
& LPOLLIN
) {
844 * A new connection is requested, therefore a
845 * viewer connection is allocated in this
846 * thread, enqueued to a global queue and
847 * dequeued (and freed) in the worker thread.
850 struct relay_connection
*new_conn
;
851 struct lttcomm_sock
*newsock
;
853 newsock
= accept_live_sock(live_control_sock
,
854 "Live socket to client");
856 PERROR("accepting control sock");
859 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
861 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
864 PERROR("setsockopt inet");
865 lttcomm_destroy_sock(newsock
);
868 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
870 lttcomm_destroy_sock(newsock
);
873 /* Ownership assumed by the connection. */
876 /* Enqueue request for the dispatcher thread. */
877 cds_wfcq_head_ptr_t head
;
878 head
.h
= &viewer_conn_queue
.head
;
879 cds_wfcq_enqueue(head
, &viewer_conn_queue
.tail
,
883 * Wake the dispatch queue futex.
884 * Implicit memory barrier with the
885 * exchange in cds_wfcq_enqueue.
887 futex_nto1_wake(&viewer_conn_queue
.futex
);
888 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
889 ERR("socket poll error");
892 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
902 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
904 if (live_control_sock
->fd
>= 0) {
905 int sock_fd
= live_control_sock
->fd
;
907 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
908 &sock_fd
, 1, close_sock
,
913 live_control_sock
->fd
= -1;
915 lttcomm_destroy_sock(live_control_sock
);
919 DBG("Live viewer listener thread exited with error");
921 health_unregister(health_relayd
);
922 rcu_unregister_thread();
923 DBG("Live viewer listener thread cleanup complete");
924 if (lttng_relay_stop_threads()) {
925 ERR("Error stopping threads");
931 * This thread manages the dispatching of the requests to worker threads
934 void *thread_dispatcher(void *data
__attribute__((unused
)))
938 struct cds_wfcq_node
*node
;
939 struct relay_connection
*conn
= NULL
;
941 DBG("[thread] Live viewer relay dispatcher started");
943 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
945 if (testpoint(relayd_thread_live_dispatcher
)) {
946 goto error_testpoint
;
949 health_code_update();
952 health_code_update();
954 /* Atomically prepare the queue futex */
955 futex_nto1_prepare(&viewer_conn_queue
.futex
);
957 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
962 health_code_update();
964 /* Dequeue commands */
965 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
966 &viewer_conn_queue
.tail
);
968 DBG("Woken up but nothing in the live-viewer "
969 "relay command queue");
970 /* Continue thread execution */
973 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
974 DBG("Dispatching viewer request waiting on sock %d",
978 * Inform worker thread of the new request. This
979 * call is blocking so we can be assured that
980 * the data will be read at some point in time
981 * or wait to the end of the world :)
983 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
985 PERROR("write conn pipe");
986 connection_put(conn
);
989 } while (node
!= NULL
);
991 /* Futex wait on queue. Blocking call on futex() */
993 futex_nto1_wait(&viewer_conn_queue
.futex
);
997 /* Normal exit, no error */
1004 ERR("Health error occurred in %s", __func__
);
1006 health_unregister(health_relayd
);
1007 DBG("Live viewer dispatch thread dying");
1008 if (lttng_relay_stop_threads()) {
1009 ERR("Error stopping threads");
1015 * Establish connection with the viewer and check the versions.
1017 * Return 0 on success or else negative value.
1020 int viewer_connect(struct relay_connection
*conn
)
1023 struct lttng_viewer_connect reply
, msg
;
1025 conn
->version_check_done
= 1;
1027 health_code_update();
1029 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
1034 health_code_update();
1036 memset(&reply
, 0, sizeof(reply
));
1037 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1038 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1040 /* Major versions must be the same */
1041 if (reply
.major
!= be32toh(msg
.major
)) {
1042 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
1043 reply
.major
, be32toh(msg
.major
));
1048 conn
->major
= reply
.major
;
1049 /* We adapt to the lowest compatible version */
1050 if (reply
.minor
<= be32toh(msg
.minor
)) {
1051 conn
->minor
= reply
.minor
;
1053 conn
->minor
= be32toh(msg
.minor
);
1056 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
1057 conn
->type
= RELAY_VIEWER_COMMAND
;
1058 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
1059 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
1061 ERR("Unknown connection type : %u", be32toh(msg
.type
));
1066 reply
.major
= htobe32(reply
.major
);
1067 reply
.minor
= htobe32(reply
.minor
);
1068 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
1070 * Increment outside of htobe64 macro, because the argument can
1071 * be used more than once within the macro, and thus the
1072 * operation may be undefined.
1074 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
1075 last_relay_viewer_session_id
++;
1076 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
1077 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
1080 health_code_update();
1082 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1087 health_code_update();
1089 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
1097 * Send the viewer the list of current sessions.
1098 * We need to create a copy of the hash table content because otherwise
1099 * we cannot assume the number of entries stays the same between getting
1100 * the number of HT elements and iteration over the HT.
1102 * Return 0 on success or else a negative value.
1105 int viewer_list_sessions(struct relay_connection
*conn
)
1108 struct lttng_viewer_list_sessions session_list
;
1109 struct lttng_ht_iter iter
;
1110 struct relay_session
*session
;
1111 struct lttng_viewer_session
*send_session_buf
= NULL
;
1112 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
1115 send_session_buf
= calloc
<lttng_viewer_session
>(SESSION_BUF_DEFAULT_COUNT
);
1116 if (!send_session_buf
) {
1121 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
1123 struct lttng_viewer_session
*send_session
;
1125 health_code_update();
1127 pthread_mutex_lock(&session
->lock
);
1128 if (session
->connection_closed
) {
1129 /* Skip closed session */
1133 if (count
>= buf_count
) {
1134 struct lttng_viewer_session
*newbuf
;
1135 uint32_t new_buf_count
= buf_count
<< 1;
1137 newbuf
= (lttng_viewer_session
*) realloc(send_session_buf
,
1138 new_buf_count
* sizeof(*send_session_buf
));
1143 send_session_buf
= newbuf
;
1144 buf_count
= new_buf_count
;
1146 send_session
= &send_session_buf
[count
];
1147 if (lttng_strncpy(send_session
->session_name
,
1148 session
->session_name
,
1149 sizeof(send_session
->session_name
))) {
1153 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
1154 sizeof(send_session
->hostname
))) {
1158 send_session
->id
= htobe64(session
->id
);
1159 send_session
->live_timer
= htobe32(session
->live_timer
);
1160 if (session
->viewer_attached
) {
1161 send_session
->clients
= htobe32(1);
1163 send_session
->clients
= htobe32(0);
1165 send_session
->streams
= htobe32(session
->stream_count
);
1168 pthread_mutex_unlock(&session
->lock
);
1171 pthread_mutex_unlock(&session
->lock
);
1179 session_list
.sessions_count
= htobe32(count
);
1181 health_code_update();
1183 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
1188 health_code_update();
1190 ret
= send_response(conn
->sock
, send_session_buf
,
1191 count
* sizeof(*send_session_buf
));
1195 health_code_update();
1199 free(send_session_buf
);
1204 * Send the viewer the list of current streams.
1207 int viewer_get_new_streams(struct relay_connection
*conn
)
1209 int ret
, send_streams
= 0;
1210 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
1211 struct lttng_viewer_new_streams_request request
;
1212 struct lttng_viewer_new_streams_response response
;
1213 struct relay_session
*session
= NULL
;
1214 uint64_t session_id
;
1215 bool closed
= false;
1219 health_code_update();
1221 /* Receive the request from the connected client. */
1222 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1226 session_id
= be64toh(request
.session_id
);
1228 health_code_update();
1230 memset(&response
, 0, sizeof(response
));
1232 session
= session_get_by_id(session_id
);
1234 DBG("Relay session %" PRIu64
" not found", session_id
);
1235 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1239 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1240 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1245 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1246 * that at this point the client is already attached to the session.Aany
1247 * initial stream will have been created with the seek type at attach
1248 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1249 * Otherwise any event happening in a new stream between the attach and
1250 * a call to viewer_get_new_streams will be "lost" (never received) from
1251 * the viewer's point of view.
1253 pthread_mutex_lock(&session
->lock
);
1255 * If a session rotation is ongoing, do not attempt to open any
1256 * stream, because the chunk can be in an intermediate state
1257 * due to directory renaming.
1259 if (session_has_ongoing_rotation(session
)) {
1260 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1261 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW
);
1262 goto send_reply_unlock
;
1264 ret
= make_viewer_streams(session
,
1265 conn
->viewer_session
,
1266 LTTNG_VIEWER_SEEK_BEGINNING
, &nb_total
, &nb_unsent
,
1267 &nb_created
, &closed
);
1270 * This is caused by an internal error; propagate the negative
1271 * 'ret' to close the connection.
1273 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1274 goto send_reply_unlock
;
1277 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1279 /* Only send back the newly created streams with the unsent ones. */
1280 nb_streams
= nb_created
+ nb_unsent
;
1281 response
.streams_count
= htobe32(nb_streams
);
1284 * If the session is closed, HUP when there are no more streams
1287 if (closed
&& nb_total
== 0) {
1289 response
.streams_count
= 0;
1290 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1291 goto send_reply_unlock
;
1294 pthread_mutex_unlock(&session
->lock
);
1297 health_code_update();
1298 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1300 goto end_put_session
;
1302 health_code_update();
1305 * Unknown or empty session, just return gracefully, the viewer
1306 * knows what is happening.
1308 if (!send_streams
|| !nb_streams
) {
1310 goto end_put_session
;
1314 * Send stream and *DON'T* ignore the sent flag so every viewer
1315 * streams that were not sent from that point will be sent to
1318 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1320 goto end_put_session
;
1325 session_put(session
);
1332 * Send the viewer the list of current sessions.
1335 int viewer_attach_session(struct relay_connection
*conn
)
1337 int send_streams
= 0;
1339 uint32_t nb_streams
= 0;
1340 enum lttng_viewer_seek seek_type
;
1341 struct lttng_viewer_attach_session_request request
;
1342 struct lttng_viewer_attach_session_response response
;
1343 struct relay_session
*session
= NULL
;
1344 enum lttng_viewer_attach_return_code viewer_attach_status
;
1345 bool closed
= false;
1346 uint64_t session_id
;
1350 health_code_update();
1352 /* Receive the request from the connected client. */
1353 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1358 session_id
= be64toh(request
.session_id
);
1360 health_code_update();
1362 memset(&response
, 0, sizeof(response
));
1364 if (!conn
->viewer_session
) {
1365 viewer_attach_status
= LTTNG_VIEWER_ATTACH_NO_SESSION
;
1366 DBG("Client trying to attach before creating a live viewer session, returning status=%s",
1367 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1371 session
= session_get_by_id(session_id
);
1373 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
1374 DBG("Relay session %" PRIu64
" not found, returning status=%s",
1376 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1379 DBG("Attach relay session ID %" PRIu64
" received", session_id
);
1381 pthread_mutex_lock(&session
->lock
);
1382 if (session
->live_timer
== 0) {
1383 viewer_attach_status
= LTTNG_VIEWER_ATTACH_NOT_LIVE
;
1384 DBG("Relay session ID %" PRIu64
" is not a live session, returning status=%s",
1386 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1391 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1393 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1394 DBG("Error attaching to relay session %" PRIu64
", returning status=%s",
1396 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1400 switch (be32toh(request
.seek
)) {
1401 case LTTNG_VIEWER_SEEK_BEGINNING
:
1402 case LTTNG_VIEWER_SEEK_LAST
:
1403 viewer_attach_status
= LTTNG_VIEWER_ATTACH_OK
;
1404 seek_type
= (lttng_viewer_seek
) be32toh(request
.seek
);
1407 ERR("Wrong seek parameter for relay session %" PRIu64
1408 ", returning status=%s", session_id
,
1409 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1410 viewer_attach_status
= LTTNG_VIEWER_ATTACH_SEEK_ERR
;
1416 * If a session rotation is ongoing, do not attempt to open any
1417 * stream, because the chunk can be in an intermediate state
1418 * due to directory renaming.
1420 if (session_has_ongoing_rotation(session
)) {
1421 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1426 ret
= make_viewer_streams(session
,
1427 conn
->viewer_session
, seek_type
,
1428 &nb_streams
, NULL
, NULL
, &closed
);
1430 goto end_put_session
;
1432 pthread_mutex_unlock(&session
->lock
);
1433 session_put(session
);
1436 response
.streams_count
= htobe32(nb_streams
);
1438 * If the session is closed when the viewer is attaching, it
1439 * means some of the streams may have been concurrently removed,
1440 * so we don't allow the viewer to attach, even if there are
1441 * streams available.
1445 response
.streams_count
= 0;
1446 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
1447 ERR("Session %" PRIu64
" is closed, returning status=%s",
1449 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1454 health_code_update();
1456 response
.status
= htobe32((uint32_t) viewer_attach_status
);
1458 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1460 goto end_put_session
;
1462 health_code_update();
1465 * Unknown or empty session, just return gracefully, the viewer
1466 * knows what is happening.
1468 if (!send_streams
|| !nb_streams
) {
1470 goto end_put_session
;
1473 /* Send stream and ignore the sent flag. */
1474 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1476 goto end_put_session
;
1481 pthread_mutex_unlock(&session
->lock
);
1482 session_put(session
);
1489 * Open the index file if needed for the given vstream.
1491 * If an index file is successfully opened, the vstream will set it as its
1492 * current index file.
1494 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1496 * Called with rstream lock held.
1498 static int try_open_index(struct relay_viewer_stream
*vstream
,
1499 struct relay_stream
*rstream
)
1502 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1503 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1504 enum lttng_trace_chunk_status chunk_status
;
1506 if (vstream
->index_file
) {
1511 * First time, we open the index file and at least one index is ready.
1513 if (rstream
->index_received_seqcount
== 0 ||
1514 !vstream
->stream_file
.trace_chunk
) {
1519 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1520 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1521 rstream
->channel_name
, rstream
->tracefile_size
,
1522 vstream
->current_tracefile_id
,
1523 lttng_to_index_major(connection_major
, connection_minor
),
1524 lttng_to_index_minor(connection_major
, connection_minor
),
1525 true, &vstream
->index_file
);
1526 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1527 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1539 * Check the status of the index for the given stream. This function
1540 * updates the index structure if needed and can put (close) the vstream
1541 * in the HUP situation.
1543 * Return 0 means that we can proceed with the index. A value of 1 means
1544 * that the index has been updated and is ready to be sent to the
1545 * client. A negative value indicates an error that can't be handled.
1547 * Called with rstream lock held.
1549 static int check_index_status(struct relay_viewer_stream
*vstream
,
1550 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1551 struct lttng_viewer_index
*index
)
1555 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1556 "index_sent_seqcount %" PRIu64
" "
1557 "for stream %" PRIu64
,
1558 rstream
->index_received_seqcount
,
1559 vstream
->index_sent_seqcount
,
1560 vstream
->stream
->stream_handle
);
1561 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1562 && rstream
->index_received_seqcount
1563 == vstream
->index_sent_seqcount
) {
1565 * Last index sent and session connection or relay
1566 * stream are closed.
1568 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1569 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
1570 ",connection-closed=%d, relay-stream-closed=%d, returning status=%s",
1571 vstream
->stream
->stream_handle
,
1572 trace
->session
->connection_closed
, rstream
->closed
,
1573 lttng_viewer_next_index_return_code_str(
1574 (enum lttng_viewer_next_index_return_code
) index
->status
));
1576 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1577 (rstream
->index_received_seqcount
== 0 ||
1578 (vstream
->index_sent_seqcount
!= 0 &&
1579 rstream
->index_received_seqcount
1580 <= vstream
->index_sent_seqcount
))) {
1582 * We've received a synchronization beacon and the last index
1583 * available has been sent, the index for now is inactive.
1585 * In this case, we have received a beacon which allows us to
1586 * inform the client of a time interval during which we can
1587 * guarantee that there are no events to read (and never will
1590 * The sent seqcount can grow higher than receive seqcount on
1591 * clear because the rotation performed by clear will push
1592 * the index_sent_seqcount ahead (see
1593 * viewer_stream_sync_tracefile_array_tail) and skip over
1594 * packet sequence numbers.
1596 index
->status
= LTTNG_VIEWER_INDEX_INACTIVE
;
1597 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1598 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1599 DBG("Check index status: inactive with beacon, for stream %" PRIu64
1600 ", returning status=%s",
1601 vstream
->stream
->stream_handle
,
1602 lttng_viewer_next_index_return_code_str(
1603 (enum lttng_viewer_next_index_return_code
) index
->status
));
1605 } else if (rstream
->index_received_seqcount
== 0 ||
1606 (vstream
->index_sent_seqcount
!= 0 &&
1607 rstream
->index_received_seqcount
1608 <= vstream
->index_sent_seqcount
)) {
1610 * This checks whether received <= sent seqcount. In
1611 * this case, we have not received a beacon. Therefore,
1612 * we can only ask the client to retry later.
1614 * The sent seqcount can grow higher than receive seqcount on
1615 * clear because the rotation performed by clear will push
1616 * the index_sent_seqcount ahead (see
1617 * viewer_stream_sync_tracefile_array_tail) and skip over
1618 * packet sequence numbers.
1620 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1621 DBG("Check index status:"
1622 "did not received beacon for stream %" PRIu64
1623 ", returning status=%s",
1624 vstream
->stream
->stream_handle
,
1625 lttng_viewer_next_index_return_code_str(
1626 (enum lttng_viewer_next_index_return_code
) index
->status
));
1628 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1629 vstream
->current_tracefile_id
,
1630 vstream
->index_sent_seqcount
)) {
1632 * The next index we want to send cannot be read either
1633 * because we need to perform a rotation, or due to
1634 * the producer having overwritten its trace file.
1636 DBG("Viewer stream %" PRIu64
" rotation",
1637 vstream
->stream
->stream_handle
);
1638 ret
= viewer_stream_rotate(vstream
);
1640 /* EOF across entire stream. */
1641 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1642 DBG("Check index status:"
1643 "reached end of file for stream %" PRIu64
1644 ", returning status=%s",
1645 vstream
->stream
->stream_handle
,
1646 lttng_viewer_next_index_return_code_str(
1647 (enum lttng_viewer_next_index_return_code
) index
->status
));
1651 * If we have been pushed due to overwrite, it
1652 * necessarily means there is data that can be read in
1653 * the stream. If we rotated because we reached the end
1654 * of a tracefile, it means the following tracefile
1655 * needs to contain at least one index, else we would
1656 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1657 * viewer. The updated index_sent_seqcount needs to
1658 * point to a readable index entry now.
1660 * In the case where we "rotate" on a single file, we
1661 * can end up in a case where the requested index is
1662 * still unavailable.
1664 if (rstream
->tracefile_count
== 1 &&
1665 !tracefile_array_seq_in_file(
1667 vstream
->current_tracefile_id
,
1668 vstream
->index_sent_seqcount
)) {
1669 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1670 DBG("Check index status:"
1671 "tracefile array sequence number %" PRIu64
1672 " not in file for stream %" PRIu64
1673 ", returning status=%s",
1674 vstream
->index_sent_seqcount
,
1675 vstream
->stream
->stream_handle
,
1676 lttng_viewer_next_index_return_code_str(
1677 (enum lttng_viewer_next_index_return_code
) index
->status
));
1680 LTTNG_ASSERT(tracefile_array_seq_in_file(rstream
->tfa
,
1681 vstream
->current_tracefile_id
,
1682 vstream
->index_sent_seqcount
));
1684 /* ret == 0 means successful so we continue. */
1689 viewer_stream_put(vstream
);
1695 void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream
*vstream
,
1696 struct lttng_trace_chunk
*new_trace_chunk
)
1698 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1700 if (new_trace_chunk
) {
1701 const bool acquired_reference
= lttng_trace_chunk_get(
1704 LTTNG_ASSERT(acquired_reference
);
1707 vstream
->stream_file
.trace_chunk
= new_trace_chunk
;
1708 viewer_stream_sync_tracefile_array_tail(vstream
);
1709 viewer_stream_close_files(vstream
);
1713 * Send the next index for a stream.
1715 * Return 0 on success or else a negative value.
1718 int viewer_get_next_index(struct relay_connection
*conn
)
1721 struct lttng_viewer_get_next_index request_index
;
1722 struct lttng_viewer_index viewer_index
;
1723 struct ctf_packet_index packet_index
;
1724 struct relay_viewer_stream
*vstream
= NULL
;
1725 struct relay_stream
*rstream
= NULL
;
1726 struct ctf_trace
*ctf_trace
= NULL
;
1727 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1728 bool viewer_stream_and_session_in_same_chunk
, viewer_stream_one_rotation_behind
;
1729 uint64_t stream_file_chunk_id
= -1ULL, viewer_session_chunk_id
= -1ULL;
1730 enum lttng_trace_chunk_status status
;
1734 memset(&viewer_index
, 0, sizeof(viewer_index
));
1735 health_code_update();
1737 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1741 health_code_update();
1743 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1745 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1746 DBG("Client requested index of unknown stream id %" PRIu64
", returning status=%s",
1747 (uint64_t) be64toh(request_index
.stream_id
),
1748 lttng_viewer_next_index_return_code_str(
1749 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1753 /* Use back. ref. Protected by refcounts. */
1754 rstream
= vstream
->stream
;
1755 ctf_trace
= rstream
->trace
;
1757 /* metadata_viewer_stream may be NULL. */
1758 metadata_viewer_stream
=
1759 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1762 * Hold the session lock to protect against concurrent changes
1763 * to the chunk files (e.g. rename done by clear), which are
1764 * protected by the session ongoing rotation state. Those are
1765 * synchronized with the session lock.
1767 pthread_mutex_lock(&rstream
->trace
->session
->lock
);
1768 pthread_mutex_lock(&rstream
->lock
);
1771 * The viewer should not ask for index on metadata stream.
1773 if (rstream
->is_metadata
) {
1774 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1775 DBG("Client requested index of a metadata stream id %" PRIu64
", returning status=%s",
1776 (uint64_t) be64toh(request_index
.stream_id
),
1777 lttng_viewer_next_index_return_code_str(
1778 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1782 if (rstream
->ongoing_rotation
.is_set
) {
1783 /* Rotation is ongoing, try again later. */
1784 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1785 DBG("Client requested index for stream id %" PRIu64
" while a stream rotation is ongoing, returning status=%s",
1786 (uint64_t) be64toh(request_index
.stream_id
),
1787 lttng_viewer_next_index_return_code_str(
1788 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1792 if (session_has_ongoing_rotation(rstream
->trace
->session
)) {
1793 /* Rotation is ongoing, try again later. */
1794 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1795 DBG("Client requested index for stream id %" PRIu64
" while a session rotation is ongoing, returning status=%s",
1796 (uint64_t) be64toh(request_index
.stream_id
),
1797 lttng_viewer_next_index_return_code_str(
1798 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1803 * Transition the viewer session into the newest trace chunk available.
1805 if (!lttng_trace_chunk_ids_equal(
1806 conn
->viewer_session
->current_trace_chunk
,
1807 rstream
->trace_chunk
)) {
1808 DBG("Relay stream and viewer chunk ids differ");
1810 ret
= viewer_session_set_trace_chunk_copy(
1811 conn
->viewer_session
,
1812 rstream
->trace_chunk
);
1814 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1815 ERR("Error copying trace chunk for stream id %" PRIu64
1816 ", returning status=%s",
1817 (uint64_t) be64toh(request_index
.stream_id
),
1818 lttng_viewer_next_index_return_code_str(
1819 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1825 * Transition the viewer stream into the latest trace chunk available.
1827 * Note that the stream must _not_ rotate in one precise condition:
1828 * the relay stream has rotated to a NULL trace chunk and the viewer
1829 * stream is consuming the trace chunk that was active just before
1830 * that rotation to NULL.
1832 * This allows clients to consume all the packets of a trace chunk
1833 * after a session's destruction.
1835 if (vstream
->stream_file
.trace_chunk
) {
1836 status
= lttng_trace_chunk_get_id(
1837 vstream
->stream_file
.trace_chunk
,
1838 &stream_file_chunk_id
);
1839 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1841 if (conn
->viewer_session
->current_trace_chunk
) {
1842 status
= lttng_trace_chunk_get_id(
1843 conn
->viewer_session
->current_trace_chunk
,
1844 &viewer_session_chunk_id
);
1845 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1848 viewer_stream_and_session_in_same_chunk
= lttng_trace_chunk_ids_equal(
1849 conn
->viewer_session
->current_trace_chunk
,
1850 vstream
->stream_file
.trace_chunk
);
1851 viewer_stream_one_rotation_behind
= rstream
->completed_rotation_count
==
1852 vstream
->last_seen_rotation_count
+ 1;
1854 if (viewer_stream_and_session_in_same_chunk
) {
1855 DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate",
1856 vstream
->stream_file
.trace_chunk
?
1857 std::to_string(stream_file_chunk_id
).c_str() :
1859 conn
->viewer_session
->current_trace_chunk
?
1860 std::to_string(viewer_session_chunk_id
).c_str() :
1862 } else if (viewer_stream_one_rotation_behind
&& !rstream
->trace_chunk
) {
1863 DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate",
1864 vstream
->stream_file
.trace_chunk
?
1865 std::to_string(stream_file_chunk_id
).c_str() :
1867 conn
->viewer_session
->current_trace_chunk
?
1868 std::to_string(viewer_session_chunk_id
).c_str() :
1871 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
1872 vstream
->stream_file
.trace_chunk
?
1873 std::to_string(stream_file_chunk_id
).c_str() :
1875 conn
->viewer_session
->current_trace_chunk
?
1876 std::to_string(viewer_session_chunk_id
).c_str() :
1879 viewer_stream_rotate_to_trace_chunk(vstream
,
1880 conn
->viewer_session
->current_trace_chunk
);
1881 vstream
->last_seen_rotation_count
=
1882 rstream
->completed_rotation_count
;
1885 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1888 } else if (ret
== 1) {
1890 * We have no index to send and check_index_status has populated
1891 * viewer_index's status.
1895 /* At this point, ret is 0 thus we will be able to read the index. */
1898 /* Try to open an index if one is needed for that stream. */
1899 ret
= try_open_index(vstream
, rstream
);
1900 if (ret
== -ENOENT
) {
1901 if (rstream
->closed
) {
1902 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1903 DBG("Cannot open index for stream id %" PRIu64
1904 "stream is closed, returning status=%s",
1905 (uint64_t) be64toh(request_index
.stream_id
),
1906 lttng_viewer_next_index_return_code_str(
1907 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1910 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1911 DBG("Cannot open index for stream id %" PRIu64
1912 ", returning status=%s",
1913 (uint64_t) be64toh(request_index
.stream_id
),
1914 lttng_viewer_next_index_return_code_str(
1915 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1920 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1921 ERR("Error opening index for stream id %" PRIu64
1922 ", returning status=%s",
1923 (uint64_t) be64toh(request_index
.stream_id
),
1924 lttng_viewer_next_index_return_code_str(
1925 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1930 * vstream->stream_fd may be NULL if it has been closed by
1931 * tracefile rotation, or if we are at the beginning of the
1932 * stream. We open the data stream file here to protect against
1933 * overwrite caused by tracefile rotation (in association with
1934 * unlink performed before overwrite).
1936 if (!vstream
->stream_file
.handle
) {
1937 char file_path
[LTTNG_PATH_MAX
];
1938 struct fs_handle
*fs_handle
;
1940 ret
= utils_stream_file_path(rstream
->path_name
,
1941 rstream
->channel_name
, rstream
->tracefile_size
,
1942 vstream
->current_tracefile_id
, NULL
, file_path
,
1949 * It is possible the the file we are trying to open is
1950 * missing if the stream has been closed (application exits with
1951 * per-pid buffers) and a clear command has been performed.
1953 status
= lttng_trace_chunk_open_fs_handle(
1954 vstream
->stream_file
.trace_chunk
,
1955 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1956 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1957 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1959 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1960 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
1961 ", returning status=%s",
1962 (uint64_t) be64toh(request_index
.stream_id
),
1963 lttng_viewer_next_index_return_code_str(
1964 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1967 PERROR("Failed to open trace file for viewer stream");
1970 vstream
->stream_file
.handle
= fs_handle
;
1973 ret
= check_new_streams(conn
);
1975 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1976 ERR("Error checking for new streams before sending new index to stream id %" PRIu64
1977 ", returning status=%s",
1978 (uint64_t) be64toh(request_index
.stream_id
),
1979 lttng_viewer_next_index_return_code_str(
1980 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1982 } else if (ret
== 1) {
1983 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1986 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1988 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1989 ERR("Relay error reading index file for stream id %" PRIu64
1990 ", returning status=%s",
1991 (uint64_t) be64toh(request_index
.stream_id
),
1992 lttng_viewer_next_index_return_code_str(
1993 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1996 viewer_index
.status
= LTTNG_VIEWER_INDEX_OK
;
1997 DBG("Read index file for stream id %" PRIu64
1998 ", returning status=%s",
1999 (uint64_t) be64toh(request_index
.stream_id
),
2000 lttng_viewer_next_index_return_code_str(
2001 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
2002 vstream
->index_sent_seqcount
++;
2006 * Indexes are stored in big endian, no need to switch before sending.
2008 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
2009 rstream
->stream_handle
,
2010 (uint64_t) be64toh(packet_index
.offset
));
2011 viewer_index
.offset
= packet_index
.offset
;
2012 viewer_index
.packet_size
= packet_index
.packet_size
;
2013 viewer_index
.content_size
= packet_index
.content_size
;
2014 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
2015 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
2016 viewer_index
.events_discarded
= packet_index
.events_discarded
;
2017 viewer_index
.stream_id
= packet_index
.stream_id
;
2021 pthread_mutex_unlock(&rstream
->lock
);
2022 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
2025 if (metadata_viewer_stream
) {
2026 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
2027 DBG("get next index metadata check: recv %" PRIu64
2029 metadata_viewer_stream
->stream
->metadata_received
,
2030 metadata_viewer_stream
->metadata_sent
);
2031 if (!metadata_viewer_stream
->stream
->metadata_received
||
2032 metadata_viewer_stream
->stream
->metadata_received
>
2033 metadata_viewer_stream
->metadata_sent
) {
2034 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
2036 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
2039 viewer_index
.flags
= htobe32(viewer_index
.flags
);
2040 viewer_index
.status
= htobe32(viewer_index
.status
);
2041 health_code_update();
2043 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
2047 health_code_update();
2050 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
2051 vstream
->index_sent_seqcount
,
2052 vstream
->stream
->stream_handle
);
2055 if (metadata_viewer_stream
) {
2056 viewer_stream_put(metadata_viewer_stream
);
2059 viewer_stream_put(vstream
);
2064 pthread_mutex_unlock(&rstream
->lock
);
2065 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
2066 if (metadata_viewer_stream
) {
2067 viewer_stream_put(metadata_viewer_stream
);
2069 viewer_stream_put(vstream
);
2074 * Send the next index for a stream
2076 * Return 0 on success or else a negative value.
2079 int viewer_get_packet(struct relay_connection
*conn
)
2084 struct lttng_viewer_get_packet get_packet_info
;
2085 struct lttng_viewer_trace_packet reply_header
;
2086 struct relay_viewer_stream
*vstream
= NULL
;
2087 uint32_t reply_size
= sizeof(reply_header
);
2088 uint32_t packet_data_len
= 0;
2091 enum lttng_viewer_get_packet_return_code get_packet_status
;
2093 health_code_update();
2095 ret
= recv_request(conn
->sock
, &get_packet_info
,
2096 sizeof(get_packet_info
));
2100 health_code_update();
2102 /* From this point on, the error label can be reached. */
2103 memset(&reply_header
, 0, sizeof(reply_header
));
2104 stream_id
= (uint64_t) be64toh(get_packet_info
.stream_id
);
2106 vstream
= viewer_stream_get_by_id(stream_id
);
2108 get_packet_status
= LTTNG_VIEWER_GET_PACKET_ERR
;
2109 DBG("Client requested packet of unknown stream id %" PRIu64
2110 ", returning status=%s", stream_id
,
2111 lttng_viewer_get_packet_return_code_str(get_packet_status
));
2112 goto send_reply_nolock
;
2114 packet_data_len
= be32toh(get_packet_info
.len
);
2115 reply_size
+= packet_data_len
;
2118 reply
= zmalloc
<char>(reply_size
);
2120 get_packet_status
= LTTNG_VIEWER_GET_PACKET_ERR
;
2121 PERROR("Falled to allocate reply, returning status=%s",
2122 lttng_viewer_get_packet_return_code_str(get_packet_status
));
2126 pthread_mutex_lock(&vstream
->stream
->lock
);
2127 lseek_ret
= fs_handle_seek(vstream
->stream_file
.handle
,
2128 be64toh(get_packet_info
.offset
), SEEK_SET
);
2129 if (lseek_ret
< 0) {
2130 get_packet_status
= LTTNG_VIEWER_GET_PACKET_ERR
;
2131 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
2132 " to offset %" PRIu64
", returning status=%s", stream_id
,
2133 (uint64_t) be64toh(get_packet_info
.offset
),
2134 lttng_viewer_get_packet_return_code_str(get_packet_status
));
2137 read_len
= fs_handle_read(vstream
->stream_file
.handle
,
2138 reply
+ sizeof(reply_header
), packet_data_len
);
2139 if (read_len
< packet_data_len
) {
2140 get_packet_status
= LTTNG_VIEWER_GET_PACKET_ERR
;
2141 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
2142 ", offset: %" PRIu64
", returning status=%s", stream_id
,
2143 (uint64_t) be64toh(get_packet_info
.offset
),
2144 lttng_viewer_get_packet_return_code_str(get_packet_status
));
2148 get_packet_status
= LTTNG_VIEWER_GET_PACKET_OK
;
2149 reply_header
.len
= htobe32(packet_data_len
);
2153 /* No payload to send on error. */
2154 reply_size
= sizeof(reply_header
);
2158 pthread_mutex_unlock(&vstream
->stream
->lock
);
2162 health_code_update();
2164 reply_header
.status
= htobe32(get_packet_status
);
2166 memcpy(reply
, &reply_header
, sizeof(reply_header
));
2167 ret
= send_response(conn
->sock
, reply
, reply_size
);
2169 /* No reply to send. */
2170 ret
= send_response(conn
->sock
, &reply_header
,
2174 health_code_update();
2176 PERROR("sendmsg of packet data failed");
2180 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
, stream_id
);
2186 viewer_stream_put(vstream
);
2192 * Send the session's metadata
2194 * Return 0 on success else a negative value.
2197 int viewer_get_metadata(struct relay_connection
*conn
)
2204 struct lttng_viewer_get_metadata request
;
2205 struct lttng_viewer_metadata_packet reply
;
2206 struct relay_viewer_stream
*vstream
= NULL
;
2210 health_code_update();
2212 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2216 health_code_update();
2218 memset(&reply
, 0, sizeof(reply
));
2220 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
2223 * The metadata stream can be closed by a CLOSE command
2224 * just before we attach. It can also be closed by
2225 * per-pid tracing during tracing. Therefore, it is
2226 * possible that we cannot find this viewer stream.
2227 * Reply back to the client with an error if we cannot
2230 DBG("Client requested metadata of unknown stream id %" PRIu64
,
2231 (uint64_t) be64toh(request
.stream_id
));
2232 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2235 pthread_mutex_lock(&vstream
->stream
->lock
);
2236 if (!vstream
->stream
->is_metadata
) {
2237 ERR("Invalid metadata stream");
2241 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
2243 * The live viewers expect to receive a NO_NEW_METADATA
2244 * status before a stream disappears, otherwise they abort the
2245 * entire live connection when receiving an error status.
2247 * Clear feature resets the metadata_sent to 0 until the
2248 * same metadata is received again.
2250 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2252 * The live viewer considers a closed 0 byte metadata stream as
2255 if (vstream
->metadata_sent
> 0) {
2256 if (vstream
->stream
->closed
&& vstream
->stream
->no_new_metadata_notified
) {
2258 * Release ownership for the viewer metadata
2259 * stream. Note that this reference is the
2260 * viewer's reference. The vstream still exists
2261 * until the end of the function as
2262 * viewer_stream_get_by_id() took a reference.
2264 viewer_stream_put(vstream
);
2267 vstream
->stream
->no_new_metadata_notified
= true;
2272 if (vstream
->stream
->trace_chunk
&&
2273 !lttng_trace_chunk_ids_equal(
2274 conn
->viewer_session
->current_trace_chunk
,
2275 vstream
->stream
->trace_chunk
)) {
2276 /* A rotation has occurred on the relay stream. */
2277 DBG("Metadata relay stream and viewer chunk ids differ");
2279 ret
= viewer_session_set_trace_chunk_copy(
2280 conn
->viewer_session
,
2281 vstream
->stream
->trace_chunk
);
2283 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2288 if (conn
->viewer_session
->current_trace_chunk
&&
2289 !lttng_trace_chunk_ids_equal(conn
->viewer_session
->current_trace_chunk
,
2290 vstream
->stream_file
.trace_chunk
)) {
2291 bool acquired_reference
;
2293 DBG("Viewer session and viewer stream chunk differ: "
2294 "vsession chunk %p vstream chunk %p",
2295 conn
->viewer_session
->current_trace_chunk
,
2296 vstream
->stream_file
.trace_chunk
);
2297 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
2298 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
2299 LTTNG_ASSERT(acquired_reference
);
2300 vstream
->stream_file
.trace_chunk
=
2301 conn
->viewer_session
->current_trace_chunk
;
2302 viewer_stream_close_files(vstream
);
2305 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
2307 if (!vstream
->stream_file
.trace_chunk
) {
2308 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2311 } else if (vstream
->stream_file
.trace_chunk
&&
2312 !vstream
->stream_file
.handle
&& len
> 0) {
2314 * Either this is the first time the metadata file is read, or a
2315 * rotation of the corresponding relay stream has occurred.
2317 struct fs_handle
*fs_handle
;
2318 char file_path
[LTTNG_PATH_MAX
];
2319 enum lttng_trace_chunk_status status
;
2320 struct relay_stream
*rstream
= vstream
->stream
;
2322 ret
= utils_stream_file_path(rstream
->path_name
,
2323 rstream
->channel_name
, rstream
->tracefile_size
,
2324 vstream
->current_tracefile_id
, NULL
, file_path
,
2331 * It is possible the the metadata file we are trying to open is
2332 * missing if the stream has been closed (application exits with
2333 * per-pid buffers) and a clear command has been performed.
2335 status
= lttng_trace_chunk_open_fs_handle(
2336 vstream
->stream_file
.trace_chunk
,
2337 file_path
, O_RDONLY
, 0, &fs_handle
, true);
2338 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2339 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
2340 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2342 if (vstream
->stream
->closed
) {
2343 viewer_stream_put(vstream
);
2347 PERROR("Failed to open metadata file for viewer stream");
2350 vstream
->stream_file
.handle
= fs_handle
;
2352 if (vstream
->metadata_sent
!= 0) {
2354 * The client does not expect to receive any metadata
2355 * it has received and metadata files in successive
2356 * chunks must be a strict superset of one another.
2358 * Skip the first `metadata_sent` bytes to ensure
2359 * they are not sent a second time to the client.
2361 * Baring a block layer error or an internal error,
2362 * this seek should not fail as
2363 * `vstream->stream->metadata_received` is reset when
2364 * a relay stream is rotated. If this is reached, it is
2365 * safe to assume that
2366 * `metadata_received` > `metadata_sent`.
2368 const off_t seek_ret
= fs_handle_seek(fs_handle
,
2369 vstream
->metadata_sent
, SEEK_SET
);
2372 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64
,
2373 vstream
->metadata_sent
);
2374 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2380 reply
.len
= htobe64(len
);
2381 data
= zmalloc
<char>(len
);
2383 PERROR("viewer metadata zmalloc");
2387 fd
= fs_handle_get_fd(vstream
->stream_file
.handle
);
2389 ERR("Failed to restore viewer stream file system handle");
2392 read_len
= lttng_read(fd
, data
, len
);
2393 fs_handle_put_fd(vstream
->stream_file
.handle
);
2395 if (read_len
< len
) {
2397 PERROR("Failed to read metadata file");
2401 * A clear has been performed which prevents the relay
2402 * from sending `len` bytes of metadata.
2404 * It is important not to send any metadata if we
2405 * couldn't read all the available metadata in one shot:
2406 * sending partial metadata can cause the client to
2407 * attempt to parse an incomplete (incoherent) metadata
2408 * stream, which would result in an error.
2410 const off_t seek_ret
= fs_handle_seek(
2411 vstream
->stream_file
.handle
, -read_len
,
2414 DBG("Failed to read metadata: requested = %" PRIu64
", got = %zd",
2419 PERROR("Failed to restore metadata file position after partial read");
2425 vstream
->metadata_sent
+= read_len
;
2426 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
2431 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2434 health_code_update();
2436 pthread_mutex_unlock(&vstream
->stream
->lock
);
2438 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
2442 health_code_update();
2445 ret
= send_response(conn
->sock
, data
, len
);
2451 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
2452 (uint64_t) be64toh(request
.stream_id
));
2454 DBG("Metadata sent");
2460 viewer_stream_put(vstream
);
2466 * Create a viewer session.
2468 * Return 0 on success or else a negative value.
2471 int viewer_create_session(struct relay_connection
*conn
)
2474 struct lttng_viewer_create_session_response resp
;
2476 memset(&resp
, 0, sizeof(resp
));
2477 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
2478 conn
->viewer_session
= viewer_session_create();
2479 if (!conn
->viewer_session
) {
2480 ERR("Allocation viewer session");
2481 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
2486 health_code_update();
2487 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
2491 health_code_update();
2499 * Detach a viewer session.
2501 * Return 0 on success or else a negative value.
2504 int viewer_detach_session(struct relay_connection
*conn
)
2507 struct lttng_viewer_detach_session_response response
;
2508 struct lttng_viewer_detach_session_request request
;
2509 struct relay_session
*session
= NULL
;
2510 uint64_t viewer_session_to_close
;
2514 health_code_update();
2516 /* Receive the request from the connected client. */
2517 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2521 viewer_session_to_close
= be64toh(request
.session_id
);
2523 if (!conn
->viewer_session
) {
2524 DBG("Client trying to detach before creating a live viewer session");
2525 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2529 health_code_update();
2531 memset(&response
, 0, sizeof(response
));
2532 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2534 session
= session_get_by_id(be64toh(request
.session_id
));
2536 DBG("Relay session %" PRIu64
" not found",
2537 (uint64_t) be64toh(request
.session_id
));
2538 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2542 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2544 DBG("Not attached to this session");
2545 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2546 goto send_reply_put
;
2549 viewer_session_close_one_session(conn
->viewer_session
, session
);
2550 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2551 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2554 session_put(session
);
2557 health_code_update();
2558 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2562 health_code_update();
2570 * live_relay_unknown_command: send -1 if received unknown command
2573 void live_relay_unknown_command(struct relay_connection
*conn
)
2575 struct lttcomm_relayd_generic_reply reply
;
2577 memset(&reply
, 0, sizeof(reply
));
2578 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2579 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2583 * Process the commands received on the control socket
2586 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2587 struct relay_connection
*conn
)
2590 lttng_viewer_command cmd
=
2591 (lttng_viewer_command
) be32toh(recv_hdr
->cmd
);
2594 * Make sure we've done the version check before any command other then
2595 * a new client connection.
2597 if (cmd
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2598 ERR("Viewer on connection %d requested %s command before version check",
2599 conn
->sock
->fd
, lttng_viewer_command_str(cmd
));
2604 DBG("Processing %s viewer command from connection %d",
2605 lttng_viewer_command_str(cmd
), conn
->sock
->fd
);
2608 case LTTNG_VIEWER_CONNECT
:
2609 ret
= viewer_connect(conn
);
2611 case LTTNG_VIEWER_LIST_SESSIONS
:
2612 ret
= viewer_list_sessions(conn
);
2614 case LTTNG_VIEWER_ATTACH_SESSION
:
2615 ret
= viewer_attach_session(conn
);
2617 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2618 ret
= viewer_get_next_index(conn
);
2620 case LTTNG_VIEWER_GET_PACKET
:
2621 ret
= viewer_get_packet(conn
);
2623 case LTTNG_VIEWER_GET_METADATA
:
2624 ret
= viewer_get_metadata(conn
);
2626 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2627 ret
= viewer_get_new_streams(conn
);
2629 case LTTNG_VIEWER_CREATE_SESSION
:
2630 ret
= viewer_create_session(conn
);
2632 case LTTNG_VIEWER_DETACH_SESSION
:
2633 ret
= viewer_detach_session(conn
);
2636 ERR("Received unknown viewer command (%u)",
2637 be32toh(recv_hdr
->cmd
));
2638 live_relay_unknown_command(conn
);
2648 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2652 (void) lttng_poll_del(events
, pollfd
);
2654 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &pollfd
, 1,
2655 fd_tracker_util_close_fd
, NULL
);
2657 ERR("Closing pollfd %d", pollfd
);
2662 * This thread does the actual work
2665 void *thread_worker(void *data
__attribute__((unused
)))
2669 struct lttng_poll_event events
;
2670 struct lttng_ht
*viewer_connections_ht
;
2671 struct lttng_ht_iter iter
;
2672 struct lttng_viewer_cmd recv_hdr
;
2673 struct relay_connection
*destroy_conn
;
2675 DBG("[thread] Live viewer relay worker started");
2677 rcu_register_thread();
2679 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2681 if (testpoint(relayd_thread_live_worker
)) {
2682 goto error_testpoint
;
2685 /* table of connections indexed on socket */
2686 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2687 if (!viewer_connections_ht
) {
2688 goto viewer_connections_ht_error
;
2691 ret
= create_named_thread_poll_set(&events
, 2,
2692 "Live viewer worker thread epoll");
2694 goto error_poll_create
;
2697 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2706 health_code_update();
2708 /* Infinite blocking call, waiting for transmission */
2709 DBG3("Relayd live viewer worker thread polling...");
2710 health_poll_entry();
2711 ret
= lttng_poll_wait(&events
, -1);
2715 * Restart interrupted system call.
2717 if (errno
== EINTR
) {
2726 * Process control. The control connection is prioritised so we don't
2727 * starve it with high throughput tracing data on the data
2730 for (i
= 0; i
< nb_fd
; i
++) {
2731 /* Fetch once the poll data */
2732 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2733 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2735 health_code_update();
2737 /* Thread quit pipe has been closed. Killing thread. */
2738 ret
= check_thread_quit_pipe(pollfd
, revents
);
2744 /* Inspect the relay conn pipe for new connection. */
2745 if (pollfd
== live_conn_pipe
[0]) {
2746 if (revents
& LPOLLIN
) {
2747 struct relay_connection
*conn
;
2749 ret
= lttng_read(live_conn_pipe
[0],
2750 &conn
, sizeof(conn
));
2754 ret
= lttng_poll_add(&events
,
2756 LPOLLIN
| LPOLLRDHUP
);
2758 ERR("Failed to add new live connection file descriptor to poll set");
2761 connection_ht_add(viewer_connections_ht
, conn
);
2762 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2763 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2764 ERR("Relay live pipe error");
2767 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2771 /* Connection activity. */
2772 struct relay_connection
*conn
;
2774 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2779 if (revents
& LPOLLIN
) {
2780 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2781 sizeof(recv_hdr
), 0);
2783 /* Connection closed. */
2784 cleanup_connection_pollfd(&events
, pollfd
);
2785 /* Put "create" ownership reference. */
2786 connection_put(conn
);
2787 DBG("Viewer control conn closed with %d", pollfd
);
2789 ret
= process_control(&recv_hdr
, conn
);
2791 /* Clear the session on error. */
2792 cleanup_connection_pollfd(&events
, pollfd
);
2793 /* Put "create" ownership reference. */
2794 connection_put(conn
);
2795 DBG("Viewer connection closed with %d", pollfd
);
2798 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2799 cleanup_connection_pollfd(&events
, pollfd
);
2800 /* Put "create" ownership reference. */
2801 connection_put(conn
);
2803 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2804 connection_put(conn
);
2807 /* Put local "get_by_sock" reference. */
2808 connection_put(conn
);
2815 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
2817 /* Cleanup remaining connection object. */
2819 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2822 health_code_update();
2823 connection_put(destroy_conn
);
2827 lttng_ht_destroy(viewer_connections_ht
);
2828 viewer_connections_ht_error
:
2829 /* Close relay conn pipes */
2830 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2832 DBG("Viewer worker thread exited with error");
2834 DBG("Viewer worker thread cleanup complete");
2838 ERR("Health error occurred in %s", __func__
);
2840 health_unregister(health_relayd
);
2841 if (lttng_relay_stop_threads()) {
2842 ERR("Error stopping threads");
2844 rcu_unregister_thread();
2849 * Create the relay command pipe to wake thread_manage_apps.
2850 * Closed in cleanup().
2852 static int create_conn_pipe(void)
2854 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2855 "Live connection pipe", live_conn_pipe
);
2858 int relayd_live_join(void)
2860 int ret
, retval
= 0;
2863 ret
= pthread_join(live_listener_thread
, &status
);
2866 PERROR("pthread_join live listener");
2870 ret
= pthread_join(live_worker_thread
, &status
);
2873 PERROR("pthread_join live worker");
2877 ret
= pthread_join(live_dispatcher_thread
, &status
);
2880 PERROR("pthread_join live dispatcher");
2884 cleanup_relayd_live();
2892 int relayd_live_create(struct lttng_uri
*uri
)
2894 int ret
= 0, retval
= 0;
2900 goto exit_init_data
;
2904 /* Check if daemon is UID = 0 */
2905 is_root
= !getuid();
2908 if (live_uri
->port
< 1024) {
2909 ERR("Need to be root to use ports < 1024");
2911 goto exit_init_data
;
2915 /* Setup the thread apps communication pipe. */
2916 if (create_conn_pipe()) {
2918 goto exit_init_data
;
2921 /* Init relay command queue. */
2922 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2924 /* Set up max poll set size */
2925 if (lttng_poll_set_max_size()) {
2927 goto exit_init_data
;
2930 /* Setup the dispatcher thread */
2931 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2932 thread_dispatcher
, (void *) NULL
);
2935 PERROR("pthread_create viewer dispatcher");
2937 goto exit_dispatcher_thread
;
2940 /* Setup the worker thread */
2941 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2942 thread_worker
, NULL
);
2945 PERROR("pthread_create viewer worker");
2947 goto exit_worker_thread
;
2950 /* Setup the listener thread */
2951 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2952 thread_listener
, (void *) NULL
);
2955 PERROR("pthread_create viewer listener");
2957 goto exit_listener_thread
;
2961 * All OK, started all threads.
2966 * Join on the live_listener_thread should anything be added after
2967 * the live_listener thread's creation.
2970 exit_listener_thread
:
2972 ret
= pthread_join(live_worker_thread
, &status
);
2975 PERROR("pthread_join live worker");
2980 ret
= pthread_join(live_dispatcher_thread
, &status
);
2983 PERROR("pthread_join live dispatcher");
2986 exit_dispatcher_thread
:
2989 cleanup_relayd_live();