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.h>
35 #include <common/compat/endian.h>
36 #include <common/compat/poll.h>
37 #include <common/compat/socket.h>
38 #include <common/defaults.h>
39 #include <common/fd-tracker/utils.h>
40 #include <common/fs-handle.h>
41 #include <common/futex.h>
42 #include <common/index/index.h>
43 #include <common/sessiond-comm/inet.h>
44 #include <common/sessiond-comm/relayd.h>
45 #include <common/sessiond-comm/sessiond-comm.h>
46 #include <common/uri.h>
47 #include <common/utils.h>
48 #include <lttng/lttng.h>
51 #include "connection.h"
52 #include "ctf-trace.h"
53 #include "health-relayd.h"
55 #include "lttng-relayd.h"
58 #include "testpoint.h"
60 #include "viewer-session.h"
61 #include "viewer-stream.h"
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
:
145 void cleanup_relayd_live(void)
153 * Receive a request buffer using a given socket, destination allocated buffer
156 * Return the size of the received message or else a negative value on error
157 * with errno being set by recvmsg() syscall.
160 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
164 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
165 if (ret
< 0 || ret
!= size
) {
167 /* Orderly shutdown. Not necessary to print an error. */
168 DBG("Socket %d did an orderly shutdown", sock
->fd
);
170 ERR("Relay failed to receive request.");
179 * Send a response buffer using a given socket, source allocated buffer of
182 * Return the size of the sent message or else a negative value on error with
183 * errno being set by sendmsg() syscall.
186 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
190 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
192 ERR("Relayd failed to send response.");
199 * Atomically check if new streams got added in one of the sessions attached
200 * and reset the flag to 0.
202 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
206 int check_new_streams(struct relay_connection
*conn
)
208 struct relay_session
*session
;
209 unsigned long current_val
;
212 if (!conn
->viewer_session
) {
216 cds_list_for_each_entry_rcu(session
,
217 &conn
->viewer_session
->session_list
,
218 viewer_session_node
) {
219 if (!session_get(session
)) {
222 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
224 session_put(session
);
235 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
236 * this function should ignore the sent flag or not.
238 * Return 0 on success or else a negative value.
241 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
242 uint64_t session_id
, unsigned int ignore_sent_flag
)
245 struct lttng_ht_iter iter
;
246 struct relay_viewer_stream
*vstream
;
250 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
252 struct ctf_trace
*ctf_trace
;
253 struct lttng_viewer_stream send_stream
= {};
255 health_code_update();
257 if (!viewer_stream_get(vstream
)) {
261 pthread_mutex_lock(&vstream
->stream
->lock
);
262 /* Ignore if not the same session. */
263 if (vstream
->stream
->trace
->session
->id
!= session_id
||
264 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
265 pthread_mutex_unlock(&vstream
->stream
->lock
);
266 viewer_stream_put(vstream
);
270 ctf_trace
= vstream
->stream
->trace
;
271 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
272 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
273 send_stream
.metadata_flag
= htobe32(
274 vstream
->stream
->is_metadata
);
275 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
276 sizeof(send_stream
.path_name
))) {
277 pthread_mutex_unlock(&vstream
->stream
->lock
);
278 viewer_stream_put(vstream
);
279 ret
= -1; /* Error. */
282 if (lttng_strncpy(send_stream
.channel_name
,
283 vstream
->channel_name
,
284 sizeof(send_stream
.channel_name
))) {
285 pthread_mutex_unlock(&vstream
->stream
->lock
);
286 viewer_stream_put(vstream
);
287 ret
= -1; /* Error. */
291 DBG("Sending stream %" PRIu64
" to viewer",
292 vstream
->stream
->stream_handle
);
293 vstream
->sent_flag
= 1;
294 pthread_mutex_unlock(&vstream
->stream
->lock
);
296 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
297 viewer_stream_put(vstream
);
311 * Create every viewer stream possible for the given session with the seek
312 * type. Three counters *can* be return which are in order the total amount of
313 * viewer stream of the session, the number of unsent stream and the number of
314 * stream created. Those counters can be NULL and thus will be ignored.
316 * session must be locked to ensure that we see either none or all initial
317 * streams for a session, but no intermediate state..
319 * Return 0 on success or else a negative value.
321 static int make_viewer_streams(struct relay_session
*relay_session
,
322 struct relay_viewer_session
*viewer_session
,
323 enum lttng_viewer_seek seek_t
,
326 uint32_t *nb_created
,
330 struct lttng_ht_iter iter
;
331 struct ctf_trace
*ctf_trace
;
332 struct relay_stream
*relay_stream
= NULL
;
334 LTTNG_ASSERT(relay_session
);
335 ASSERT_LOCKED(relay_session
->lock
);
337 if (relay_session
->connection_closed
) {
342 * Create viewer streams for relay streams that are ready to be
343 * used for a the given session id only.
346 cds_lfht_for_each_entry (relay_session
->ctf_traces_ht
->ht
, &iter
.iter
,
347 ctf_trace
, node
.node
) {
348 bool trace_has_metadata_stream
= false;
350 health_code_update();
352 if (!ctf_trace_get(ctf_trace
)) {
357 * Iterate over all the streams of the trace to see if we have a
360 cds_list_for_each_entry_rcu(relay_stream
,
361 &ctf_trace
->stream_list
, stream_node
)
363 bool is_metadata_stream
;
365 pthread_mutex_lock(&relay_stream
->lock
);
366 is_metadata_stream
= relay_stream
->is_metadata
;
367 pthread_mutex_unlock(&relay_stream
->lock
);
369 if (is_metadata_stream
) {
370 trace_has_metadata_stream
= true;
378 * If there is no metadata stream in this trace at the moment
379 * and we never sent one to the viewer, skip the trace. We
380 * accept that the viewer will not see this trace at all.
382 if (!trace_has_metadata_stream
&&
383 !ctf_trace
->metadata_stream_sent_to_viewer
) {
384 ctf_trace_put(ctf_trace
);
388 cds_list_for_each_entry_rcu(relay_stream
,
389 &ctf_trace
->stream_list
, stream_node
)
391 struct relay_viewer_stream
*viewer_stream
;
393 if (!stream_get(relay_stream
)) {
397 pthread_mutex_lock(&relay_stream
->lock
);
399 * stream published is protected by the session lock.
401 if (!relay_stream
->published
) {
404 viewer_stream
= viewer_stream_get_by_id(
405 relay_stream
->stream_handle
);
406 if (!viewer_stream
) {
407 struct lttng_trace_chunk
*viewer_stream_trace_chunk
= NULL
;
410 * Save that we sent the metadata stream to the
411 * viewer. So that we know what trace the viewer
414 if (relay_stream
->is_metadata
) {
415 ctf_trace
->metadata_stream_sent_to_viewer
= true;
419 * If a rotation is ongoing, use a copy of the
420 * relay stream's chunk to ensure the stream
423 * Otherwise, the viewer session's current trace
424 * chunk can be used safely.
426 if ((relay_stream
->ongoing_rotation
.is_set
||
427 relay_session
->ongoing_rotation
) &&
428 relay_stream
->trace_chunk
) {
429 viewer_stream_trace_chunk
= lttng_trace_chunk_copy(
430 relay_stream
->trace_chunk
);
431 if (!viewer_stream_trace_chunk
) {
433 ctf_trace_put(ctf_trace
);
438 * Transition the viewer session into the newest trace chunk available.
440 if (!lttng_trace_chunk_ids_equal(viewer_session
->current_trace_chunk
,
441 relay_stream
->trace_chunk
)) {
443 ret
= viewer_session_set_trace_chunk_copy(
445 relay_stream
->trace_chunk
);
448 ctf_trace_put(ctf_trace
);
453 if (relay_stream
->trace_chunk
) {
455 * If the corresponding relay
456 * stream's trace chunk is set,
457 * the viewer stream will be
460 * Note that a relay stream can
461 * have a NULL output trace
462 * chunk (for instance, after a
463 * clear against a stopped
466 const bool reference_acquired
= lttng_trace_chunk_get(
467 viewer_session
->current_trace_chunk
);
469 LTTNG_ASSERT(reference_acquired
);
470 viewer_stream_trace_chunk
=
471 viewer_session
->current_trace_chunk
;
475 viewer_stream
= viewer_stream_create(
477 viewer_stream_trace_chunk
,
479 lttng_trace_chunk_put(viewer_stream_trace_chunk
);
480 viewer_stream_trace_chunk
= NULL
;
481 if (!viewer_stream
) {
483 ctf_trace_put(ctf_trace
);
488 /* Update number of created stream counter. */
492 * Ensure a self-reference is preserved even
493 * after we have put our local reference.
495 if (!viewer_stream_get(viewer_stream
)) {
496 ERR("Unable to get self-reference on viewer stream, logic error.");
500 if (!viewer_stream
->sent_flag
&& nb_unsent
) {
501 /* Update number of unsent stream counter. */
505 /* Update number of total stream counter. */
507 if (relay_stream
->is_metadata
) {
508 if (!relay_stream
->closed
||
509 relay_stream
->metadata_received
>
510 viewer_stream
->metadata_sent
) {
514 if (!relay_stream
->closed
||
515 !(((int64_t)(relay_stream
->prev_data_seq
-
516 relay_stream
->last_net_seq_num
)) >=
522 /* Put local reference. */
523 viewer_stream_put(viewer_stream
);
525 pthread_mutex_unlock(&relay_stream
->lock
);
526 stream_put(relay_stream
);
529 ctf_trace_put(ctf_trace
);
538 pthread_mutex_unlock(&relay_stream
->lock
);
539 stream_put(relay_stream
);
545 int relayd_live_stop(void)
547 /* Stop dispatch thread */
548 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
549 futex_nto1_wake(&viewer_conn_queue
.futex
);
554 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
557 int create_named_thread_poll_set(struct lttng_poll_event
*events
,
558 int size
, const char *name
)
562 if (events
== NULL
|| size
== 0) {
567 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
568 name
, events
, 1, LTTNG_CLOEXEC
);
570 PERROR("Failed to create \"%s\" poll file descriptor", name
);
575 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
587 * Check if the thread quit pipe was triggered.
589 * Return 1 if it was triggered else 0;
592 int check_thread_quit_pipe(int fd
, uint32_t events
)
594 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
602 int create_sock(void *data
, int *out_fd
)
605 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
607 ret
= lttcomm_create_sock(sock
);
618 int close_sock(void *data
, int *in_fd
)
620 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
622 return sock
->ops
->close(sock
);
625 static int accept_sock(void *data
, int *out_fd
)
628 /* Socks is an array of in_sock, out_sock. */
629 struct lttcomm_sock
**socks
= (lttcomm_sock
**) data
;
630 struct lttcomm_sock
*in_sock
= socks
[0];
632 socks
[1] = in_sock
->ops
->accept(in_sock
);
637 *out_fd
= socks
[1]->fd
;
643 struct lttcomm_sock
*accept_live_sock(struct lttcomm_sock
*listening_sock
,
647 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
648 struct lttcomm_sock
*new_sock
= NULL
;
650 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &out_fd
,
651 (const char **) &name
, 1, accept_sock
, &socks
);
656 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
662 * Create and init socket from uri.
665 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
, const char *name
)
668 struct lttcomm_sock
*sock
= NULL
;
669 char uri_str
[LTTNG_PATH_MAX
];
670 char *formated_name
= NULL
;
672 sock
= lttcomm_alloc_sock_from_uri(uri
);
674 ERR("Allocating socket");
679 * Don't fail to create the socket if the name can't be built as it is
680 * only used for debugging purposes.
682 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
683 uri_str
[sizeof(uri_str
) - 1] = '\0';
685 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
688 formated_name
= NULL
;
692 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
693 (const char **) (formated_name
? &formated_name
: NULL
),
694 1, create_sock
, sock
);
696 PERROR("Failed to create \"%s\" socket",
697 formated_name
?: "Unknown");
700 DBG("Listening on %s socket %d", name
, sock
->fd
);
702 ret
= sock
->ops
->bind(sock
);
704 PERROR("Failed to bind lttng-live socket");
708 ret
= sock
->ops
->listen(sock
, -1);
719 lttcomm_destroy_sock(sock
);
726 * This thread manages the listening for new connections on the network
729 void *thread_listener(void *data
)
731 int i
, ret
, pollfd
, err
= -1;
732 uint32_t revents
, nb_fd
;
733 struct lttng_poll_event events
;
734 struct lttcomm_sock
*live_control_sock
;
736 DBG("[thread] Relay live listener started");
738 rcu_register_thread();
739 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
741 health_code_update();
743 live_control_sock
= init_socket(live_uri
, "Live listener");
744 if (!live_control_sock
) {
745 goto error_sock_control
;
748 /* Pass 2 as size here for the thread quit pipe and control sockets. */
749 ret
= create_named_thread_poll_set(&events
, 2,
750 "Live listener thread epoll");
752 goto error_create_poll
;
755 /* Add the control socket */
756 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
761 lttng_relay_notify_ready();
763 if (testpoint(relayd_thread_live_listener
)) {
764 goto error_testpoint
;
768 health_code_update();
770 DBG("Listener accepting live viewers connections");
774 ret
= lttng_poll_wait(&events
, -1);
778 * Restart interrupted system call.
780 if (errno
== EINTR
) {
787 DBG("Relay new viewer connection received");
788 for (i
= 0; i
< nb_fd
; i
++) {
789 health_code_update();
791 /* Fetch once the poll data */
792 revents
= LTTNG_POLL_GETEV(&events
, i
);
793 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
795 /* Thread quit pipe has been closed. Killing thread. */
796 ret
= check_thread_quit_pipe(pollfd
, revents
);
802 if (revents
& LPOLLIN
) {
804 * A new connection is requested, therefore a
805 * viewer connection is allocated in this
806 * thread, enqueued to a global queue and
807 * dequeued (and freed) in the worker thread.
810 struct relay_connection
*new_conn
;
811 struct lttcomm_sock
*newsock
;
813 newsock
= accept_live_sock(live_control_sock
,
814 "Live socket to client");
816 PERROR("accepting control sock");
819 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
821 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
824 PERROR("setsockopt inet");
825 lttcomm_destroy_sock(newsock
);
828 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
830 lttcomm_destroy_sock(newsock
);
833 /* Ownership assumed by the connection. */
836 /* Enqueue request for the dispatcher thread. */
837 cds_wfcq_head_ptr_t head
;
838 head
.h
= &viewer_conn_queue
.head
;
839 cds_wfcq_enqueue(head
, &viewer_conn_queue
.tail
,
843 * Wake the dispatch queue futex.
844 * Implicit memory barrier with the
845 * exchange in cds_wfcq_enqueue.
847 futex_nto1_wake(&viewer_conn_queue
.futex
);
848 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
849 ERR("socket poll error");
852 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
862 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
864 if (live_control_sock
->fd
>= 0) {
865 int sock_fd
= live_control_sock
->fd
;
867 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
868 &sock_fd
, 1, close_sock
,
873 live_control_sock
->fd
= -1;
875 lttcomm_destroy_sock(live_control_sock
);
879 DBG("Live viewer listener thread exited with error");
881 health_unregister(health_relayd
);
882 rcu_unregister_thread();
883 DBG("Live viewer listener thread cleanup complete");
884 if (lttng_relay_stop_threads()) {
885 ERR("Error stopping threads");
891 * This thread manages the dispatching of the requests to worker threads
894 void *thread_dispatcher(void *data
)
898 struct cds_wfcq_node
*node
;
899 struct relay_connection
*conn
= NULL
;
901 DBG("[thread] Live viewer relay dispatcher started");
903 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
905 if (testpoint(relayd_thread_live_dispatcher
)) {
906 goto error_testpoint
;
909 health_code_update();
912 health_code_update();
914 /* Atomically prepare the queue futex */
915 futex_nto1_prepare(&viewer_conn_queue
.futex
);
917 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
922 health_code_update();
924 /* Dequeue commands */
925 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
926 &viewer_conn_queue
.tail
);
928 DBG("Woken up but nothing in the live-viewer "
929 "relay command queue");
930 /* Continue thread execution */
933 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
934 DBG("Dispatching viewer request waiting on sock %d",
938 * Inform worker thread of the new request. This
939 * call is blocking so we can be assured that
940 * the data will be read at some point in time
941 * or wait to the end of the world :)
943 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
945 PERROR("write conn pipe");
946 connection_put(conn
);
949 } while (node
!= NULL
);
951 /* Futex wait on queue. Blocking call on futex() */
953 futex_nto1_wait(&viewer_conn_queue
.futex
);
957 /* Normal exit, no error */
964 ERR("Health error occurred in %s", __func__
);
966 health_unregister(health_relayd
);
967 DBG("Live viewer dispatch thread dying");
968 if (lttng_relay_stop_threads()) {
969 ERR("Error stopping threads");
975 * Establish connection with the viewer and check the versions.
977 * Return 0 on success or else negative value.
980 int viewer_connect(struct relay_connection
*conn
)
983 struct lttng_viewer_connect reply
, msg
;
985 conn
->version_check_done
= 1;
987 health_code_update();
989 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
994 health_code_update();
996 memset(&reply
, 0, sizeof(reply
));
997 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
998 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1000 /* Major versions must be the same */
1001 if (reply
.major
!= be32toh(msg
.major
)) {
1002 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
1003 reply
.major
, be32toh(msg
.major
));
1008 conn
->major
= reply
.major
;
1009 /* We adapt to the lowest compatible version */
1010 if (reply
.minor
<= be32toh(msg
.minor
)) {
1011 conn
->minor
= reply
.minor
;
1013 conn
->minor
= be32toh(msg
.minor
);
1016 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
1017 conn
->type
= RELAY_VIEWER_COMMAND
;
1018 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
1019 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
1021 ERR("Unknown connection type : %u", be32toh(msg
.type
));
1026 reply
.major
= htobe32(reply
.major
);
1027 reply
.minor
= htobe32(reply
.minor
);
1028 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
1030 * Increment outside of htobe64 macro, because the argument can
1031 * be used more than once within the macro, and thus the
1032 * operation may be undefined.
1034 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
1035 last_relay_viewer_session_id
++;
1036 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
1037 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
1040 health_code_update();
1042 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1047 health_code_update();
1049 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
1057 * Send the viewer the list of current sessions.
1058 * We need to create a copy of the hash table content because otherwise
1059 * we cannot assume the number of entries stays the same between getting
1060 * the number of HT elements and iteration over the HT.
1062 * Return 0 on success or else a negative value.
1065 int viewer_list_sessions(struct relay_connection
*conn
)
1068 struct lttng_viewer_list_sessions session_list
;
1069 struct lttng_ht_iter iter
;
1070 struct relay_session
*session
;
1071 struct lttng_viewer_session
*send_session_buf
= NULL
;
1072 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
1075 send_session_buf
= (lttng_viewer_session
*) zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
1076 if (!send_session_buf
) {
1081 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
1083 struct lttng_viewer_session
*send_session
;
1085 health_code_update();
1087 pthread_mutex_lock(&session
->lock
);
1088 if (session
->connection_closed
) {
1089 /* Skip closed session */
1093 if (count
>= buf_count
) {
1094 struct lttng_viewer_session
*newbuf
;
1095 uint32_t new_buf_count
= buf_count
<< 1;
1097 newbuf
= (lttng_viewer_session
*) realloc(send_session_buf
,
1098 new_buf_count
* sizeof(*send_session_buf
));
1103 send_session_buf
= newbuf
;
1104 buf_count
= new_buf_count
;
1106 send_session
= &send_session_buf
[count
];
1107 if (lttng_strncpy(send_session
->session_name
,
1108 session
->session_name
,
1109 sizeof(send_session
->session_name
))) {
1113 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
1114 sizeof(send_session
->hostname
))) {
1118 send_session
->id
= htobe64(session
->id
);
1119 send_session
->live_timer
= htobe32(session
->live_timer
);
1120 if (session
->viewer_attached
) {
1121 send_session
->clients
= htobe32(1);
1123 send_session
->clients
= htobe32(0);
1125 send_session
->streams
= htobe32(session
->stream_count
);
1128 pthread_mutex_unlock(&session
->lock
);
1131 pthread_mutex_unlock(&session
->lock
);
1139 session_list
.sessions_count
= htobe32(count
);
1141 health_code_update();
1143 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
1148 health_code_update();
1150 ret
= send_response(conn
->sock
, send_session_buf
,
1151 count
* sizeof(*send_session_buf
));
1155 health_code_update();
1159 free(send_session_buf
);
1164 * Send the viewer the list of current streams.
1167 int viewer_get_new_streams(struct relay_connection
*conn
)
1169 int ret
, send_streams
= 0;
1170 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
1171 struct lttng_viewer_new_streams_request request
;
1172 struct lttng_viewer_new_streams_response response
;
1173 struct relay_session
*session
= NULL
;
1174 uint64_t session_id
;
1175 bool closed
= false;
1179 health_code_update();
1181 /* Receive the request from the connected client. */
1182 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1186 session_id
= be64toh(request
.session_id
);
1188 health_code_update();
1190 memset(&response
, 0, sizeof(response
));
1192 session
= session_get_by_id(session_id
);
1194 DBG("Relay session %" PRIu64
" not found", session_id
);
1195 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1199 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1200 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1205 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1206 * that at this point the client is already attached to the session.Aany
1207 * initial stream will have been created with the seek type at attach
1208 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1209 * Otherwise any event happening in a new stream between the attach and
1210 * a call to viewer_get_new_streams will be "lost" (never received) from
1211 * the viewer's point of view.
1213 pthread_mutex_lock(&session
->lock
);
1215 * If a session rotation is ongoing, do not attempt to open any
1216 * stream, because the chunk can be in an intermediate state
1217 * due to directory renaming.
1219 if (session
->ongoing_rotation
) {
1220 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1221 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW
);
1222 goto send_reply_unlock
;
1224 ret
= make_viewer_streams(session
,
1225 conn
->viewer_session
,
1226 LTTNG_VIEWER_SEEK_BEGINNING
, &nb_total
, &nb_unsent
,
1227 &nb_created
, &closed
);
1229 goto error_unlock_session
;
1232 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1234 /* Only send back the newly created streams with the unsent ones. */
1235 nb_streams
= nb_created
+ nb_unsent
;
1236 response
.streams_count
= htobe32(nb_streams
);
1239 * If the session is closed, HUP when there are no more streams
1242 if (closed
&& nb_total
== 0) {
1244 response
.streams_count
= 0;
1245 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1246 goto send_reply_unlock
;
1249 pthread_mutex_unlock(&session
->lock
);
1252 health_code_update();
1253 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1255 goto end_put_session
;
1257 health_code_update();
1260 * Unknown or empty session, just return gracefully, the viewer
1261 * knows what is happening.
1263 if (!send_streams
|| !nb_streams
) {
1265 goto end_put_session
;
1269 * Send stream and *DON'T* ignore the sent flag so every viewer
1270 * streams that were not sent from that point will be sent to
1273 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1275 goto end_put_session
;
1280 session_put(session
);
1284 error_unlock_session
:
1285 pthread_mutex_unlock(&session
->lock
);
1286 session_put(session
);
1291 * Send the viewer the list of current sessions.
1294 int viewer_attach_session(struct relay_connection
*conn
)
1296 int send_streams
= 0;
1298 uint32_t nb_streams
= 0;
1299 enum lttng_viewer_seek seek_type
;
1300 struct lttng_viewer_attach_session_request request
;
1301 struct lttng_viewer_attach_session_response response
;
1302 struct relay_session
*session
= NULL
;
1303 enum lttng_viewer_attach_return_code viewer_attach_status
;
1304 bool closed
= false;
1305 uint64_t session_id
;
1309 health_code_update();
1311 /* Receive the request from the connected client. */
1312 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1317 session_id
= be64toh(request
.session_id
);
1318 health_code_update();
1320 memset(&response
, 0, sizeof(response
));
1322 if (!conn
->viewer_session
) {
1323 DBG("Client trying to attach before creating a live viewer session");
1324 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1328 session
= session_get_by_id(session_id
);
1330 DBG("Relay session %" PRIu64
" not found", session_id
);
1331 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1334 DBG("Attach session ID %" PRIu64
" received", session_id
);
1336 pthread_mutex_lock(&session
->lock
);
1337 if (session
->live_timer
== 0) {
1338 DBG("Not live session");
1339 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1344 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1346 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1347 response
.status
= htobe32(viewer_attach_status
);
1351 switch (be32toh(request
.seek
)) {
1352 case LTTNG_VIEWER_SEEK_BEGINNING
:
1353 case LTTNG_VIEWER_SEEK_LAST
:
1354 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1355 seek_type
= (lttng_viewer_seek
) be32toh(request
.seek
);
1358 ERR("Wrong seek parameter");
1359 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1365 * If a session rotation is ongoing, do not attempt to open any
1366 * stream, because the chunk can be in an intermediate state
1367 * due to directory renaming.
1369 if (session
->ongoing_rotation
) {
1370 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1375 ret
= make_viewer_streams(session
,
1376 conn
->viewer_session
, seek_type
,
1377 &nb_streams
, NULL
, NULL
, &closed
);
1379 goto end_put_session
;
1381 pthread_mutex_unlock(&session
->lock
);
1382 session_put(session
);
1385 response
.streams_count
= htobe32(nb_streams
);
1387 * If the session is closed when the viewer is attaching, it
1388 * means some of the streams may have been concurrently removed,
1389 * so we don't allow the viewer to attach, even if there are
1390 * streams available.
1394 response
.streams_count
= 0;
1395 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1400 health_code_update();
1401 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1403 goto end_put_session
;
1405 health_code_update();
1408 * Unknown or empty session, just return gracefully, the viewer
1409 * knows what is happening.
1411 if (!send_streams
|| !nb_streams
) {
1413 goto end_put_session
;
1416 /* Send stream and ignore the sent flag. */
1417 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1419 goto end_put_session
;
1424 pthread_mutex_unlock(&session
->lock
);
1425 session_put(session
);
1432 * Open the index file if needed for the given vstream.
1434 * If an index file is successfully opened, the vstream will set it as its
1435 * current index file.
1437 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1439 * Called with rstream lock held.
1441 static int try_open_index(struct relay_viewer_stream
*vstream
,
1442 struct relay_stream
*rstream
)
1445 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1446 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1447 enum lttng_trace_chunk_status chunk_status
;
1449 if (vstream
->index_file
) {
1454 * First time, we open the index file and at least one index is ready.
1456 if (rstream
->index_received_seqcount
== 0 ||
1457 !vstream
->stream_file
.trace_chunk
) {
1462 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1463 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1464 rstream
->channel_name
, rstream
->tracefile_size
,
1465 vstream
->current_tracefile_id
,
1466 lttng_to_index_major(connection_major
, connection_minor
),
1467 lttng_to_index_minor(connection_major
, connection_minor
),
1468 true, &vstream
->index_file
);
1469 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1470 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1482 * Check the status of the index for the given stream. This function
1483 * updates the index structure if needed and can put (close) the vstream
1484 * in the HUP situation.
1486 * Return 0 means that we can proceed with the index. A value of 1 means
1487 * that the index has been updated and is ready to be sent to the
1488 * client. A negative value indicates an error that can't be handled.
1490 * Called with rstream lock held.
1492 static int check_index_status(struct relay_viewer_stream
*vstream
,
1493 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1494 struct lttng_viewer_index
*index
)
1498 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1499 "index_sent_seqcount %" PRIu64
" "
1500 "for stream %" PRIu64
,
1501 rstream
->index_received_seqcount
,
1502 vstream
->index_sent_seqcount
,
1503 vstream
->stream
->stream_handle
);
1504 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1505 && rstream
->index_received_seqcount
1506 == vstream
->index_sent_seqcount
) {
1508 * Last index sent and session connection or relay
1509 * stream are closed.
1511 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1512 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
1513 ",connection-closed=%d, relay-stream-closed=%d, returning status=%s",
1514 vstream
->stream
->stream_handle
,
1515 trace
->session
->connection_closed
, rstream
->closed
,
1516 lttng_viewer_next_index_return_code_str(
1517 (enum lttng_viewer_next_index_return_code
) index
->status
));
1519 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1520 (rstream
->index_received_seqcount
== 0 ||
1521 (vstream
->index_sent_seqcount
!= 0 &&
1522 rstream
->index_received_seqcount
1523 <= vstream
->index_sent_seqcount
))) {
1525 * We've received a synchronization beacon and the last index
1526 * available has been sent, the index for now is inactive.
1528 * In this case, we have received a beacon which allows us to
1529 * inform the client of a time interval during which we can
1530 * guarantee that there are no events to read (and never will
1533 * The sent seqcount can grow higher than receive seqcount on
1534 * clear because the rotation performed by clear will push
1535 * the index_sent_seqcount ahead (see
1536 * viewer_stream_sync_tracefile_array_tail) and skip over
1537 * packet sequence numbers.
1539 index
->status
= LTTNG_VIEWER_INDEX_INACTIVE
;
1540 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1541 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1542 DBG("Check index status: inactive with beacon, for stream %" PRIu64
1543 ", returning status=%s",
1544 vstream
->stream
->stream_handle
,
1545 lttng_viewer_next_index_return_code_str(
1546 (enum lttng_viewer_next_index_return_code
) index
->status
));
1548 } else if (rstream
->index_received_seqcount
== 0 ||
1549 (vstream
->index_sent_seqcount
!= 0 &&
1550 rstream
->index_received_seqcount
1551 <= vstream
->index_sent_seqcount
)) {
1553 * This checks whether received <= sent seqcount. In
1554 * this case, we have not received a beacon. Therefore,
1555 * we can only ask the client to retry later.
1557 * The sent seqcount can grow higher than receive seqcount on
1558 * clear because the rotation performed by clear will push
1559 * the index_sent_seqcount ahead (see
1560 * viewer_stream_sync_tracefile_array_tail) and skip over
1561 * packet sequence numbers.
1563 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1564 DBG("Check index status:"
1565 "did not received beacon for stream %" PRIu64
1566 ", returning status=%s",
1567 vstream
->stream
->stream_handle
,
1568 lttng_viewer_next_index_return_code_str(
1569 (enum lttng_viewer_next_index_return_code
) index
->status
));
1571 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1572 vstream
->current_tracefile_id
,
1573 vstream
->index_sent_seqcount
)) {
1575 * The next index we want to send cannot be read either
1576 * because we need to perform a rotation, or due to
1577 * the producer having overwritten its trace file.
1579 DBG("Viewer stream %" PRIu64
" rotation",
1580 vstream
->stream
->stream_handle
);
1581 ret
= viewer_stream_rotate(vstream
);
1583 /* EOF across entire stream. */
1584 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1585 DBG("Check index status:"
1586 "reached end of file for stream %" PRIu64
1587 ", returning status=%s",
1588 vstream
->stream
->stream_handle
,
1589 lttng_viewer_next_index_return_code_str(
1590 (enum lttng_viewer_next_index_return_code
) index
->status
));
1594 * If we have been pushed due to overwrite, it
1595 * necessarily means there is data that can be read in
1596 * the stream. If we rotated because we reached the end
1597 * of a tracefile, it means the following tracefile
1598 * needs to contain at least one index, else we would
1599 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1600 * viewer. The updated index_sent_seqcount needs to
1601 * point to a readable index entry now.
1603 * In the case where we "rotate" on a single file, we
1604 * can end up in a case where the requested index is
1605 * still unavailable.
1607 if (rstream
->tracefile_count
== 1 &&
1608 !tracefile_array_seq_in_file(
1610 vstream
->current_tracefile_id
,
1611 vstream
->index_sent_seqcount
)) {
1612 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1613 DBG("Check index status:"
1614 "tracefile array sequence number %" PRIu64
1615 " not in file for stream %" PRIu64
1616 ", returning status=%s",
1617 vstream
->index_sent_seqcount
,
1618 vstream
->stream
->stream_handle
,
1619 lttng_viewer_next_index_return_code_str(
1620 (enum lttng_viewer_next_index_return_code
) index
->status
));
1623 LTTNG_ASSERT(tracefile_array_seq_in_file(rstream
->tfa
,
1624 vstream
->current_tracefile_id
,
1625 vstream
->index_sent_seqcount
));
1627 /* ret == 0 means successful so we continue. */
1632 viewer_stream_put(vstream
);
1638 void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream
*vstream
,
1639 struct lttng_trace_chunk
*new_trace_chunk
)
1641 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1643 if (new_trace_chunk
) {
1644 const bool acquired_reference
= lttng_trace_chunk_get(
1647 LTTNG_ASSERT(acquired_reference
);
1650 vstream
->stream_file
.trace_chunk
= new_trace_chunk
;
1651 viewer_stream_sync_tracefile_array_tail(vstream
);
1652 viewer_stream_close_files(vstream
);
1656 * Send the next index for a stream.
1658 * Return 0 on success or else a negative value.
1661 int viewer_get_next_index(struct relay_connection
*conn
)
1664 struct lttng_viewer_get_next_index request_index
;
1665 struct lttng_viewer_index viewer_index
;
1666 struct ctf_packet_index packet_index
;
1667 struct relay_viewer_stream
*vstream
= NULL
;
1668 struct relay_stream
*rstream
= NULL
;
1669 struct ctf_trace
*ctf_trace
= NULL
;
1670 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1671 bool viewer_stream_and_session_in_same_chunk
, viewer_stream_one_rotation_behind
;
1672 uint64_t stream_file_chunk_id
= -1ULL, viewer_session_chunk_id
= -1ULL;
1673 enum lttng_trace_chunk_status status
;
1677 memset(&viewer_index
, 0, sizeof(viewer_index
));
1678 health_code_update();
1680 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1684 health_code_update();
1686 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1688 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1689 DBG("Client requested index of unknown stream id %" PRIu64
", returning status=%s",
1690 (uint64_t) be64toh(request_index
.stream_id
),
1691 lttng_viewer_next_index_return_code_str(
1692 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1696 /* Use back. ref. Protected by refcounts. */
1697 rstream
= vstream
->stream
;
1698 ctf_trace
= rstream
->trace
;
1700 /* metadata_viewer_stream may be NULL. */
1701 metadata_viewer_stream
=
1702 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1705 * Hold the session lock to protect against concurrent changes
1706 * to the chunk files (e.g. rename done by clear), which are
1707 * protected by the session ongoing rotation state. Those are
1708 * synchronized with the session lock.
1710 pthread_mutex_lock(&rstream
->trace
->session
->lock
);
1711 pthread_mutex_lock(&rstream
->lock
);
1714 * The viewer should not ask for index on metadata stream.
1716 if (rstream
->is_metadata
) {
1717 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1718 DBG("Client requested index of a metadata stream id %" PRIu64
", returning status=%s",
1719 (uint64_t) be64toh(request_index
.stream_id
),
1720 lttng_viewer_next_index_return_code_str(
1721 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1725 if (rstream
->ongoing_rotation
.is_set
) {
1726 /* Rotation is ongoing, try again later. */
1727 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1728 DBG("Client requested index for stream id %" PRIu64
" while a stream rotation is ongoing, returning status=%s",
1729 (uint64_t) be64toh(request_index
.stream_id
),
1730 lttng_viewer_next_index_return_code_str(
1731 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1735 if (rstream
->trace
->session
->ongoing_rotation
) {
1736 /* Rotation is ongoing, try again later. */
1737 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1738 DBG("Client requested index for stream id %" PRIu64
" while a session rotation is ongoing, returning status=%s",
1739 (uint64_t) be64toh(request_index
.stream_id
),
1740 lttng_viewer_next_index_return_code_str(
1741 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1746 * Transition the viewer session into the newest trace chunk available.
1748 if (!lttng_trace_chunk_ids_equal(
1749 conn
->viewer_session
->current_trace_chunk
,
1750 rstream
->trace_chunk
)) {
1751 DBG("Relay stream and viewer chunk ids differ");
1753 ret
= viewer_session_set_trace_chunk_copy(
1754 conn
->viewer_session
,
1755 rstream
->trace_chunk
);
1757 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1758 ERR("Error copying trace chunk for stream id %" PRIu64
1759 ", returning status=%s",
1760 (uint64_t) be64toh(request_index
.stream_id
),
1761 lttng_viewer_next_index_return_code_str(
1762 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1768 * Transition the viewer stream into the latest trace chunk available.
1770 * Note that the stream must _not_ rotate in one precise condition:
1771 * the relay stream has rotated to a NULL trace chunk and the viewer
1772 * stream is consuming the trace chunk that was active just before
1773 * that rotation to NULL.
1775 * This allows clients to consume all the packets of a trace chunk
1776 * after a session's destruction.
1778 if (vstream
->stream_file
.trace_chunk
) {
1779 status
= lttng_trace_chunk_get_id(
1780 vstream
->stream_file
.trace_chunk
,
1781 &stream_file_chunk_id
);
1782 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1784 if (conn
->viewer_session
->current_trace_chunk
) {
1785 status
= lttng_trace_chunk_get_id(
1786 conn
->viewer_session
->current_trace_chunk
,
1787 &viewer_session_chunk_id
);
1788 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1791 viewer_stream_and_session_in_same_chunk
= lttng_trace_chunk_ids_equal(
1792 conn
->viewer_session
->current_trace_chunk
,
1793 vstream
->stream_file
.trace_chunk
);
1794 viewer_stream_one_rotation_behind
= rstream
->completed_rotation_count
==
1795 vstream
->last_seen_rotation_count
+ 1;
1797 if (viewer_stream_and_session_in_same_chunk
) {
1798 DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate",
1799 vstream
->stream_file
.trace_chunk
?
1800 std::to_string(stream_file_chunk_id
).c_str() :
1802 conn
->viewer_session
->current_trace_chunk
?
1803 std::to_string(viewer_session_chunk_id
).c_str() :
1805 } else if (viewer_stream_one_rotation_behind
&& !rstream
->trace_chunk
) {
1806 DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate",
1807 vstream
->stream_file
.trace_chunk
?
1808 std::to_string(stream_file_chunk_id
).c_str() :
1810 conn
->viewer_session
->current_trace_chunk
?
1811 std::to_string(viewer_session_chunk_id
).c_str() :
1814 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
1815 vstream
->stream_file
.trace_chunk
?
1816 std::to_string(stream_file_chunk_id
).c_str() :
1818 conn
->viewer_session
->current_trace_chunk
?
1819 std::to_string(viewer_session_chunk_id
).c_str() :
1822 viewer_stream_rotate_to_trace_chunk(vstream
,
1823 conn
->viewer_session
->current_trace_chunk
);
1824 vstream
->last_seen_rotation_count
=
1825 rstream
->completed_rotation_count
;
1828 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1831 } else if (ret
== 1) {
1833 * We have no index to send and check_index_status has populated
1834 * viewer_index's status.
1838 /* At this point, ret is 0 thus we will be able to read the index. */
1841 /* Try to open an index if one is needed for that stream. */
1842 ret
= try_open_index(vstream
, rstream
);
1843 if (ret
== -ENOENT
) {
1844 if (rstream
->closed
) {
1845 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1846 DBG("Cannot open index for stream id %" PRIu64
1847 "stream is closed, returning status=%s",
1848 (uint64_t) be64toh(request_index
.stream_id
),
1849 lttng_viewer_next_index_return_code_str(
1850 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1853 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1854 DBG("Cannot open index for stream id %" PRIu64
1855 ", returning status=%s",
1856 (uint64_t) be64toh(request_index
.stream_id
),
1857 lttng_viewer_next_index_return_code_str(
1858 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1863 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1864 ERR("Error opening index for stream id %" PRIu64
1865 ", returning status=%s",
1866 (uint64_t) be64toh(request_index
.stream_id
),
1867 lttng_viewer_next_index_return_code_str(
1868 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1873 * vstream->stream_fd may be NULL if it has been closed by
1874 * tracefile rotation, or if we are at the beginning of the
1875 * stream. We open the data stream file here to protect against
1876 * overwrite caused by tracefile rotation (in association with
1877 * unlink performed before overwrite).
1879 if (!vstream
->stream_file
.handle
) {
1880 char file_path
[LTTNG_PATH_MAX
];
1881 struct fs_handle
*fs_handle
;
1883 ret
= utils_stream_file_path(rstream
->path_name
,
1884 rstream
->channel_name
, rstream
->tracefile_size
,
1885 vstream
->current_tracefile_id
, NULL
, file_path
,
1892 * It is possible the the file we are trying to open is
1893 * missing if the stream has been closed (application exits with
1894 * per-pid buffers) and a clear command has been performed.
1896 status
= lttng_trace_chunk_open_fs_handle(
1897 vstream
->stream_file
.trace_chunk
,
1898 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1899 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1900 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1902 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1903 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
1904 ", 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 PERROR("Failed to open trace file for viewer stream");
1913 vstream
->stream_file
.handle
= fs_handle
;
1916 ret
= check_new_streams(conn
);
1918 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1919 ERR("Error checking for new streams before sending new index to stream id %" PRIu64
1920 ", returning status=%s",
1921 (uint64_t) be64toh(request_index
.stream_id
),
1922 lttng_viewer_next_index_return_code_str(
1923 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1925 } else if (ret
== 1) {
1926 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1929 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1931 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1932 ERR("Relay error reading index file for stream id %" PRIu64
1933 ", returning status=%s",
1934 (uint64_t) be64toh(request_index
.stream_id
),
1935 lttng_viewer_next_index_return_code_str(
1936 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1939 viewer_index
.status
= LTTNG_VIEWER_INDEX_OK
;
1940 DBG("Read index file for stream id %" PRIu64
1941 ", returning status=%s",
1942 (uint64_t) be64toh(request_index
.stream_id
),
1943 lttng_viewer_next_index_return_code_str(
1944 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1945 vstream
->index_sent_seqcount
++;
1949 * Indexes are stored in big endian, no need to switch before sending.
1951 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1952 rstream
->stream_handle
,
1953 (uint64_t) be64toh(packet_index
.offset
));
1954 viewer_index
.offset
= packet_index
.offset
;
1955 viewer_index
.packet_size
= packet_index
.packet_size
;
1956 viewer_index
.content_size
= packet_index
.content_size
;
1957 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1958 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1959 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1960 viewer_index
.stream_id
= packet_index
.stream_id
;
1964 pthread_mutex_unlock(&rstream
->lock
);
1965 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
1968 if (metadata_viewer_stream
) {
1969 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1970 DBG("get next index metadata check: recv %" PRIu64
1972 metadata_viewer_stream
->stream
->metadata_received
,
1973 metadata_viewer_stream
->metadata_sent
);
1974 if (!metadata_viewer_stream
->stream
->metadata_received
||
1975 metadata_viewer_stream
->stream
->metadata_received
>
1976 metadata_viewer_stream
->metadata_sent
) {
1977 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1979 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1982 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1983 viewer_index
.status
= htobe32(viewer_index
.status
);
1984 health_code_update();
1986 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1990 health_code_update();
1993 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1994 vstream
->index_sent_seqcount
,
1995 vstream
->stream
->stream_handle
);
1998 if (metadata_viewer_stream
) {
1999 viewer_stream_put(metadata_viewer_stream
);
2002 viewer_stream_put(vstream
);
2007 pthread_mutex_unlock(&rstream
->lock
);
2008 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
2009 if (metadata_viewer_stream
) {
2010 viewer_stream_put(metadata_viewer_stream
);
2012 viewer_stream_put(vstream
);
2017 * Send the next index for a stream
2019 * Return 0 on success or else a negative value.
2022 int viewer_get_packet(struct relay_connection
*conn
)
2027 struct lttng_viewer_get_packet get_packet_info
;
2028 struct lttng_viewer_trace_packet reply_header
;
2029 struct relay_viewer_stream
*vstream
= NULL
;
2030 uint32_t reply_size
= sizeof(reply_header
);
2031 uint32_t packet_data_len
= 0;
2035 health_code_update();
2037 ret
= recv_request(conn
->sock
, &get_packet_info
,
2038 sizeof(get_packet_info
));
2042 health_code_update();
2044 /* From this point on, the error label can be reached. */
2045 memset(&reply_header
, 0, sizeof(reply_header
));
2046 stream_id
= (uint64_t) be64toh(get_packet_info
.stream_id
);
2048 vstream
= viewer_stream_get_by_id(stream_id
);
2050 DBG("Client requested packet of unknown stream id %" PRIu64
,
2052 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
2053 goto send_reply_nolock
;
2055 packet_data_len
= be32toh(get_packet_info
.len
);
2056 reply_size
+= packet_data_len
;
2059 reply
= (char *) zmalloc(reply_size
);
2061 PERROR("packet reply zmalloc");
2062 reply_size
= sizeof(reply_header
);
2066 pthread_mutex_lock(&vstream
->stream
->lock
);
2067 lseek_ret
= fs_handle_seek(vstream
->stream_file
.handle
,
2068 be64toh(get_packet_info
.offset
), SEEK_SET
);
2069 if (lseek_ret
< 0) {
2070 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
2071 " to offset %" PRIu64
,
2073 (uint64_t) be64toh(get_packet_info
.offset
));
2076 read_len
= fs_handle_read(vstream
->stream_file
.handle
,
2077 reply
+ sizeof(reply_header
), packet_data_len
);
2078 if (read_len
< packet_data_len
) {
2079 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
2080 ", offset: %" PRIu64
,
2082 (uint64_t) be64toh(get_packet_info
.offset
));
2085 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
2086 reply_header
.len
= htobe32(packet_data_len
);
2090 /* No payload to send on error. */
2091 reply_size
= sizeof(reply_header
);
2092 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
2096 pthread_mutex_unlock(&vstream
->stream
->lock
);
2100 health_code_update();
2103 memcpy(reply
, &reply_header
, sizeof(reply_header
));
2104 ret
= send_response(conn
->sock
, reply
, reply_size
);
2106 /* No reply to send. */
2107 ret
= send_response(conn
->sock
, &reply_header
,
2111 health_code_update();
2113 PERROR("sendmsg of packet data failed");
2117 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
, stream_id
);
2123 viewer_stream_put(vstream
);
2129 * Send the session's metadata
2131 * Return 0 on success else a negative value.
2134 int viewer_get_metadata(struct relay_connection
*conn
)
2141 struct lttng_viewer_get_metadata request
;
2142 struct lttng_viewer_metadata_packet reply
;
2143 struct relay_viewer_stream
*vstream
= NULL
;
2147 health_code_update();
2149 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2153 health_code_update();
2155 memset(&reply
, 0, sizeof(reply
));
2157 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
2160 * The metadata stream can be closed by a CLOSE command
2161 * just before we attach. It can also be closed by
2162 * per-pid tracing during tracing. Therefore, it is
2163 * possible that we cannot find this viewer stream.
2164 * Reply back to the client with an error if we cannot
2167 DBG("Client requested metadata of unknown stream id %" PRIu64
,
2168 (uint64_t) be64toh(request
.stream_id
));
2169 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2172 pthread_mutex_lock(&vstream
->stream
->lock
);
2173 if (!vstream
->stream
->is_metadata
) {
2174 ERR("Invalid metadata stream");
2178 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
2180 * The live viewers expect to receive a NO_NEW_METADATA
2181 * status before a stream disappears, otherwise they abort the
2182 * entire live connection when receiving an error status.
2184 * Clear feature resets the metadata_sent to 0 until the
2185 * same metadata is received again.
2187 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2189 * The live viewer considers a closed 0 byte metadata stream as
2192 if (vstream
->metadata_sent
> 0) {
2193 if (vstream
->stream
->closed
&& vstream
->stream
->no_new_metadata_notified
) {
2195 * Release ownership for the viewer metadata
2196 * stream. Note that this reference is the
2197 * viewer's reference. The vstream still exists
2198 * until the end of the function as
2199 * viewer_stream_get_by_id() took a reference.
2201 viewer_stream_put(vstream
);
2204 vstream
->stream
->no_new_metadata_notified
= true;
2209 if (vstream
->stream
->trace_chunk
&&
2210 !lttng_trace_chunk_ids_equal(
2211 conn
->viewer_session
->current_trace_chunk
,
2212 vstream
->stream
->trace_chunk
)) {
2213 /* A rotation has occurred on the relay stream. */
2214 DBG("Metadata relay stream and viewer chunk ids differ");
2216 ret
= viewer_session_set_trace_chunk_copy(
2217 conn
->viewer_session
,
2218 vstream
->stream
->trace_chunk
);
2220 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2225 if (conn
->viewer_session
->current_trace_chunk
&&
2226 !lttng_trace_chunk_ids_equal(conn
->viewer_session
->current_trace_chunk
,
2227 vstream
->stream_file
.trace_chunk
)) {
2228 bool acquired_reference
;
2230 DBG("Viewer session and viewer stream chunk differ: "
2231 "vsession chunk %p vstream chunk %p",
2232 conn
->viewer_session
->current_trace_chunk
,
2233 vstream
->stream_file
.trace_chunk
);
2234 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
2235 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
2236 LTTNG_ASSERT(acquired_reference
);
2237 vstream
->stream_file
.trace_chunk
=
2238 conn
->viewer_session
->current_trace_chunk
;
2239 viewer_stream_close_files(vstream
);
2242 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
2244 if (!vstream
->stream_file
.trace_chunk
) {
2245 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2248 } else if (vstream
->stream_file
.trace_chunk
&&
2249 !vstream
->stream_file
.handle
&& len
> 0) {
2251 * Either this is the first time the metadata file is read, or a
2252 * rotation of the corresponding relay stream has occurred.
2254 struct fs_handle
*fs_handle
;
2255 char file_path
[LTTNG_PATH_MAX
];
2256 enum lttng_trace_chunk_status status
;
2257 struct relay_stream
*rstream
= vstream
->stream
;
2259 ret
= utils_stream_file_path(rstream
->path_name
,
2260 rstream
->channel_name
, rstream
->tracefile_size
,
2261 vstream
->current_tracefile_id
, NULL
, file_path
,
2268 * It is possible the the metadata file we are trying to open is
2269 * missing if the stream has been closed (application exits with
2270 * per-pid buffers) and a clear command has been performed.
2272 status
= lttng_trace_chunk_open_fs_handle(
2273 vstream
->stream_file
.trace_chunk
,
2274 file_path
, O_RDONLY
, 0, &fs_handle
, true);
2275 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2276 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
2277 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2279 if (vstream
->stream
->closed
) {
2280 viewer_stream_put(vstream
);
2284 PERROR("Failed to open metadata file for viewer stream");
2287 vstream
->stream_file
.handle
= fs_handle
;
2289 if (vstream
->metadata_sent
!= 0) {
2291 * The client does not expect to receive any metadata
2292 * it has received and metadata files in successive
2293 * chunks must be a strict superset of one another.
2295 * Skip the first `metadata_sent` bytes to ensure
2296 * they are not sent a second time to the client.
2298 * Baring a block layer error or an internal error,
2299 * this seek should not fail as
2300 * `vstream->stream->metadata_received` is reset when
2301 * a relay stream is rotated. If this is reached, it is
2302 * safe to assume that
2303 * `metadata_received` > `metadata_sent`.
2305 const off_t seek_ret
= fs_handle_seek(fs_handle
,
2306 vstream
->metadata_sent
, SEEK_SET
);
2309 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64
,
2310 vstream
->metadata_sent
);
2311 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2317 reply
.len
= htobe64(len
);
2318 data
= (char *) zmalloc(len
);
2320 PERROR("viewer metadata zmalloc");
2324 fd
= fs_handle_get_fd(vstream
->stream_file
.handle
);
2326 ERR("Failed to restore viewer stream file system handle");
2329 read_len
= lttng_read(fd
, data
, len
);
2330 fs_handle_put_fd(vstream
->stream_file
.handle
);
2332 if (read_len
< len
) {
2334 PERROR("Failed to read metadata file");
2338 * A clear has been performed which prevents the relay
2339 * from sending `len` bytes of metadata.
2341 * It is important not to send any metadata if we
2342 * couldn't read all the available metadata in one shot:
2343 * sending partial metadata can cause the client to
2344 * attempt to parse an incomplete (incoherent) metadata
2345 * stream, which would result in an error.
2347 const off_t seek_ret
= fs_handle_seek(
2348 vstream
->stream_file
.handle
, -read_len
,
2351 DBG("Failed to read metadata: requested = %" PRIu64
", got = %zd",
2356 PERROR("Failed to restore metadata file position after partial read");
2362 vstream
->metadata_sent
+= read_len
;
2363 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
2368 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2371 health_code_update();
2373 pthread_mutex_unlock(&vstream
->stream
->lock
);
2375 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
2379 health_code_update();
2382 ret
= send_response(conn
->sock
, data
, len
);
2388 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
2389 (uint64_t) be64toh(request
.stream_id
));
2391 DBG("Metadata sent");
2397 viewer_stream_put(vstream
);
2403 * Create a viewer session.
2405 * Return 0 on success or else a negative value.
2408 int viewer_create_session(struct relay_connection
*conn
)
2411 struct lttng_viewer_create_session_response resp
;
2413 memset(&resp
, 0, sizeof(resp
));
2414 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
2415 conn
->viewer_session
= viewer_session_create();
2416 if (!conn
->viewer_session
) {
2417 ERR("Allocation viewer session");
2418 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
2423 health_code_update();
2424 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
2428 health_code_update();
2436 * Detach a viewer session.
2438 * Return 0 on success or else a negative value.
2441 int viewer_detach_session(struct relay_connection
*conn
)
2444 struct lttng_viewer_detach_session_response response
;
2445 struct lttng_viewer_detach_session_request request
;
2446 struct relay_session
*session
= NULL
;
2447 uint64_t viewer_session_to_close
;
2451 health_code_update();
2453 /* Receive the request from the connected client. */
2454 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2458 viewer_session_to_close
= be64toh(request
.session_id
);
2460 if (!conn
->viewer_session
) {
2461 DBG("Client trying to detach before creating a live viewer session");
2462 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2466 health_code_update();
2468 memset(&response
, 0, sizeof(response
));
2469 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2471 session
= session_get_by_id(be64toh(request
.session_id
));
2473 DBG("Relay session %" PRIu64
" not found",
2474 (uint64_t) be64toh(request
.session_id
));
2475 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2479 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2481 DBG("Not attached to this session");
2482 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2483 goto send_reply_put
;
2486 viewer_session_close_one_session(conn
->viewer_session
, session
);
2487 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2488 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2491 session_put(session
);
2494 health_code_update();
2495 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2499 health_code_update();
2507 * live_relay_unknown_command: send -1 if received unknown command
2510 void live_relay_unknown_command(struct relay_connection
*conn
)
2512 struct lttcomm_relayd_generic_reply reply
;
2514 memset(&reply
, 0, sizeof(reply
));
2515 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2516 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2520 * Process the commands received on the control socket
2523 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2524 struct relay_connection
*conn
)
2527 lttng_viewer_command cmd
=
2528 (lttng_viewer_command
) be32toh(recv_hdr
->cmd
);
2531 * Make sure we've done the version check before any command other then
2532 * a new client connection.
2534 if (cmd
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2535 ERR("Viewer on connection %d requested %s command before version check",
2536 conn
->sock
->fd
, lttng_viewer_command_str(cmd
));
2541 DBG("Processing %s viewer command from connection %d",
2542 lttng_viewer_command_str(cmd
), conn
->sock
->fd
);
2545 case LTTNG_VIEWER_CONNECT
:
2546 ret
= viewer_connect(conn
);
2548 case LTTNG_VIEWER_LIST_SESSIONS
:
2549 ret
= viewer_list_sessions(conn
);
2551 case LTTNG_VIEWER_ATTACH_SESSION
:
2552 ret
= viewer_attach_session(conn
);
2554 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2555 ret
= viewer_get_next_index(conn
);
2557 case LTTNG_VIEWER_GET_PACKET
:
2558 ret
= viewer_get_packet(conn
);
2560 case LTTNG_VIEWER_GET_METADATA
:
2561 ret
= viewer_get_metadata(conn
);
2563 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2564 ret
= viewer_get_new_streams(conn
);
2566 case LTTNG_VIEWER_CREATE_SESSION
:
2567 ret
= viewer_create_session(conn
);
2569 case LTTNG_VIEWER_DETACH_SESSION
:
2570 ret
= viewer_detach_session(conn
);
2573 ERR("Received unknown viewer command (%u)",
2574 be32toh(recv_hdr
->cmd
));
2575 live_relay_unknown_command(conn
);
2585 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2589 (void) lttng_poll_del(events
, pollfd
);
2591 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &pollfd
, 1,
2592 fd_tracker_util_close_fd
, NULL
);
2594 ERR("Closing pollfd %d", pollfd
);
2599 * This thread does the actual work
2602 void *thread_worker(void *data
)
2606 struct lttng_poll_event events
;
2607 struct lttng_ht
*viewer_connections_ht
;
2608 struct lttng_ht_iter iter
;
2609 struct lttng_viewer_cmd recv_hdr
;
2610 struct relay_connection
*destroy_conn
;
2612 DBG("[thread] Live viewer relay worker started");
2614 rcu_register_thread();
2616 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2618 if (testpoint(relayd_thread_live_worker
)) {
2619 goto error_testpoint
;
2622 /* table of connections indexed on socket */
2623 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2624 if (!viewer_connections_ht
) {
2625 goto viewer_connections_ht_error
;
2628 ret
= create_named_thread_poll_set(&events
, 2,
2629 "Live viewer worker thread epoll");
2631 goto error_poll_create
;
2634 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2643 health_code_update();
2645 /* Infinite blocking call, waiting for transmission */
2646 DBG3("Relayd live viewer worker thread polling...");
2647 health_poll_entry();
2648 ret
= lttng_poll_wait(&events
, -1);
2652 * Restart interrupted system call.
2654 if (errno
== EINTR
) {
2663 * Process control. The control connection is prioritised so we don't
2664 * starve it with high throughput tracing data on the data
2667 for (i
= 0; i
< nb_fd
; i
++) {
2668 /* Fetch once the poll data */
2669 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2670 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2672 health_code_update();
2674 /* Thread quit pipe has been closed. Killing thread. */
2675 ret
= check_thread_quit_pipe(pollfd
, revents
);
2681 /* Inspect the relay conn pipe for new connection. */
2682 if (pollfd
== live_conn_pipe
[0]) {
2683 if (revents
& LPOLLIN
) {
2684 struct relay_connection
*conn
;
2686 ret
= lttng_read(live_conn_pipe
[0],
2687 &conn
, sizeof(conn
));
2691 ret
= lttng_poll_add(&events
,
2693 LPOLLIN
| LPOLLRDHUP
);
2695 ERR("Failed to add new live connection file descriptor to poll set");
2698 connection_ht_add(viewer_connections_ht
, conn
);
2699 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2700 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2701 ERR("Relay live pipe error");
2704 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2708 /* Connection activity. */
2709 struct relay_connection
*conn
;
2711 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2716 if (revents
& LPOLLIN
) {
2717 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2718 sizeof(recv_hdr
), 0);
2720 /* Connection closed. */
2721 cleanup_connection_pollfd(&events
, pollfd
);
2722 /* Put "create" ownership reference. */
2723 connection_put(conn
);
2724 DBG("Viewer control conn closed with %d", pollfd
);
2726 ret
= process_control(&recv_hdr
, conn
);
2728 /* Clear the session on error. */
2729 cleanup_connection_pollfd(&events
, pollfd
);
2730 /* Put "create" ownership reference. */
2731 connection_put(conn
);
2732 DBG("Viewer connection closed with %d", pollfd
);
2735 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2736 cleanup_connection_pollfd(&events
, pollfd
);
2737 /* Put "create" ownership reference. */
2738 connection_put(conn
);
2740 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2741 connection_put(conn
);
2744 /* Put local "get_by_sock" reference. */
2745 connection_put(conn
);
2752 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
2754 /* Cleanup remaining connection object. */
2756 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2759 health_code_update();
2760 connection_put(destroy_conn
);
2764 lttng_ht_destroy(viewer_connections_ht
);
2765 viewer_connections_ht_error
:
2766 /* Close relay conn pipes */
2767 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2769 DBG("Viewer worker thread exited with error");
2771 DBG("Viewer worker thread cleanup complete");
2775 ERR("Health error occurred in %s", __func__
);
2777 health_unregister(health_relayd
);
2778 if (lttng_relay_stop_threads()) {
2779 ERR("Error stopping threads");
2781 rcu_unregister_thread();
2786 * Create the relay command pipe to wake thread_manage_apps.
2787 * Closed in cleanup().
2789 static int create_conn_pipe(void)
2791 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2792 "Live connection pipe", live_conn_pipe
);
2795 int relayd_live_join(void)
2797 int ret
, retval
= 0;
2800 ret
= pthread_join(live_listener_thread
, &status
);
2803 PERROR("pthread_join live listener");
2807 ret
= pthread_join(live_worker_thread
, &status
);
2810 PERROR("pthread_join live worker");
2814 ret
= pthread_join(live_dispatcher_thread
, &status
);
2817 PERROR("pthread_join live dispatcher");
2821 cleanup_relayd_live();
2829 int relayd_live_create(struct lttng_uri
*uri
)
2831 int ret
= 0, retval
= 0;
2837 goto exit_init_data
;
2841 /* Check if daemon is UID = 0 */
2842 is_root
= !getuid();
2845 if (live_uri
->port
< 1024) {
2846 ERR("Need to be root to use ports < 1024");
2848 goto exit_init_data
;
2852 /* Setup the thread apps communication pipe. */
2853 if (create_conn_pipe()) {
2855 goto exit_init_data
;
2858 /* Init relay command queue. */
2859 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2861 /* Set up max poll set size */
2862 if (lttng_poll_set_max_size()) {
2864 goto exit_init_data
;
2867 /* Setup the dispatcher thread */
2868 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2869 thread_dispatcher
, (void *) NULL
);
2872 PERROR("pthread_create viewer dispatcher");
2874 goto exit_dispatcher_thread
;
2877 /* Setup the worker thread */
2878 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2879 thread_worker
, NULL
);
2882 PERROR("pthread_create viewer worker");
2884 goto exit_worker_thread
;
2887 /* Setup the listener thread */
2888 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2889 thread_listener
, (void *) NULL
);
2892 PERROR("pthread_create viewer listener");
2894 goto exit_listener_thread
;
2898 * All OK, started all threads.
2903 * Join on the live_listener_thread should anything be added after
2904 * the live_listener thread's creation.
2907 exit_listener_thread
:
2909 ret
= pthread_join(live_worker_thread
, &status
);
2912 PERROR("pthread_join live worker");
2917 ret
= pthread_join(live_dispatcher_thread
, &status
);
2920 PERROR("pthread_join live dispatcher");
2923 exit_dispatcher_thread
:
2926 cleanup_relayd_live();