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
:
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";
167 void cleanup_relayd_live(void)
175 * Receive a request buffer using a given socket, destination allocated buffer
178 * Return the size of the received message or else a negative value on error
179 * with errno being set by recvmsg() syscall.
182 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
186 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
187 if (ret
< 0 || ret
!= size
) {
189 /* Orderly shutdown. Not necessary to print an error. */
190 DBG("Socket %d did an orderly shutdown", sock
->fd
);
192 ERR("Relay failed to receive request.");
201 * Send a response buffer using a given socket, source allocated buffer of
204 * Return the size of the sent message or else a negative value on error with
205 * errno being set by sendmsg() syscall.
208 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
212 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
214 ERR("Relayd failed to send response.");
221 * Atomically check if new streams got added in one of the sessions attached
222 * and reset the flag to 0.
224 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
228 int check_new_streams(struct relay_connection
*conn
)
230 struct relay_session
*session
;
231 unsigned long current_val
;
234 if (!conn
->viewer_session
) {
238 cds_list_for_each_entry_rcu(session
,
239 &conn
->viewer_session
->session_list
,
240 viewer_session_node
) {
241 if (!session_get(session
)) {
244 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
246 session_put(session
);
257 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
258 * this function should ignore the sent flag or not.
260 * Return 0 on success or else a negative value.
263 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
264 uint64_t session_id
, unsigned int ignore_sent_flag
)
267 struct lttng_ht_iter iter
;
268 struct relay_viewer_stream
*vstream
;
272 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
274 struct ctf_trace
*ctf_trace
;
275 struct lttng_viewer_stream send_stream
= {};
277 health_code_update();
279 if (!viewer_stream_get(vstream
)) {
283 pthread_mutex_lock(&vstream
->stream
->lock
);
284 /* Ignore if not the same session. */
285 if (vstream
->stream
->trace
->session
->id
!= session_id
||
286 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
287 pthread_mutex_unlock(&vstream
->stream
->lock
);
288 viewer_stream_put(vstream
);
292 ctf_trace
= vstream
->stream
->trace
;
293 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
294 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
295 send_stream
.metadata_flag
= htobe32(
296 vstream
->stream
->is_metadata
);
297 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
298 sizeof(send_stream
.path_name
))) {
299 pthread_mutex_unlock(&vstream
->stream
->lock
);
300 viewer_stream_put(vstream
);
301 ret
= -1; /* Error. */
304 if (lttng_strncpy(send_stream
.channel_name
,
305 vstream
->channel_name
,
306 sizeof(send_stream
.channel_name
))) {
307 pthread_mutex_unlock(&vstream
->stream
->lock
);
308 viewer_stream_put(vstream
);
309 ret
= -1; /* Error. */
313 DBG("Sending stream %" PRIu64
" to viewer",
314 vstream
->stream
->stream_handle
);
315 vstream
->sent_flag
= 1;
316 pthread_mutex_unlock(&vstream
->stream
->lock
);
318 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
319 viewer_stream_put(vstream
);
333 * Create every viewer stream possible for the given session with the seek
334 * type. Three counters *can* be return which are in order the total amount of
335 * viewer stream of the session, the number of unsent stream and the number of
336 * stream created. Those counters can be NULL and thus will be ignored.
338 * session must be locked to ensure that we see either none or all initial
339 * streams for a session, but no intermediate state..
341 * Return 0 on success or else a negative value.
343 static int make_viewer_streams(struct relay_session
*relay_session
,
344 struct relay_viewer_session
*viewer_session
,
345 enum lttng_viewer_seek seek_t
,
348 uint32_t *nb_created
,
352 struct lttng_ht_iter iter
;
353 struct ctf_trace
*ctf_trace
;
354 struct relay_stream
*relay_stream
= NULL
;
356 LTTNG_ASSERT(relay_session
);
357 ASSERT_LOCKED(relay_session
->lock
);
359 if (relay_session
->connection_closed
) {
364 * Create viewer streams for relay streams that are ready to be
365 * used for a the given session id only.
368 cds_lfht_for_each_entry (relay_session
->ctf_traces_ht
->ht
, &iter
.iter
,
369 ctf_trace
, node
.node
) {
370 bool trace_has_metadata_stream
= false;
372 health_code_update();
374 if (!ctf_trace_get(ctf_trace
)) {
379 * Iterate over all the streams of the trace to see if we have a
382 cds_list_for_each_entry_rcu(relay_stream
,
383 &ctf_trace
->stream_list
, stream_node
)
385 bool is_metadata_stream
;
387 pthread_mutex_lock(&relay_stream
->lock
);
388 is_metadata_stream
= relay_stream
->is_metadata
;
389 pthread_mutex_unlock(&relay_stream
->lock
);
391 if (is_metadata_stream
) {
392 trace_has_metadata_stream
= true;
400 * If there is no metadata stream in this trace at the moment
401 * and we never sent one to the viewer, skip the trace. We
402 * accept that the viewer will not see this trace at all.
404 if (!trace_has_metadata_stream
&&
405 !ctf_trace
->metadata_stream_sent_to_viewer
) {
406 ctf_trace_put(ctf_trace
);
410 cds_list_for_each_entry_rcu(relay_stream
,
411 &ctf_trace
->stream_list
, stream_node
)
413 struct relay_viewer_stream
*viewer_stream
;
415 if (!stream_get(relay_stream
)) {
419 pthread_mutex_lock(&relay_stream
->lock
);
421 * stream published is protected by the session lock.
423 if (!relay_stream
->published
) {
426 viewer_stream
= viewer_stream_get_by_id(
427 relay_stream
->stream_handle
);
428 if (!viewer_stream
) {
429 struct lttng_trace_chunk
*viewer_stream_trace_chunk
= NULL
;
432 * Save that we sent the metadata stream to the
433 * viewer. So that we know what trace the viewer
436 if (relay_stream
->is_metadata
) {
437 ctf_trace
->metadata_stream_sent_to_viewer
= true;
441 * If a rotation is ongoing, use a copy of the
442 * relay stream's chunk to ensure the stream
445 * Otherwise, the viewer session's current trace
446 * chunk can be used safely.
448 if ((relay_stream
->ongoing_rotation
.is_set
||
449 relay_session
->ongoing_rotation
) &&
450 relay_stream
->trace_chunk
) {
451 viewer_stream_trace_chunk
= lttng_trace_chunk_copy(
452 relay_stream
->trace_chunk
);
453 if (!viewer_stream_trace_chunk
) {
455 ctf_trace_put(ctf_trace
);
460 * Transition the viewer session into the newest trace chunk available.
462 if (!lttng_trace_chunk_ids_equal(viewer_session
->current_trace_chunk
,
463 relay_stream
->trace_chunk
)) {
465 ret
= viewer_session_set_trace_chunk_copy(
467 relay_stream
->trace_chunk
);
470 ctf_trace_put(ctf_trace
);
475 if (relay_stream
->trace_chunk
) {
477 * If the corresponding relay
478 * stream's trace chunk is set,
479 * the viewer stream will be
482 * Note that a relay stream can
483 * have a NULL output trace
484 * chunk (for instance, after a
485 * clear against a stopped
488 const bool reference_acquired
= lttng_trace_chunk_get(
489 viewer_session
->current_trace_chunk
);
491 LTTNG_ASSERT(reference_acquired
);
492 viewer_stream_trace_chunk
=
493 viewer_session
->current_trace_chunk
;
497 viewer_stream
= viewer_stream_create(
499 viewer_stream_trace_chunk
,
501 lttng_trace_chunk_put(viewer_stream_trace_chunk
);
502 viewer_stream_trace_chunk
= NULL
;
503 if (!viewer_stream
) {
505 ctf_trace_put(ctf_trace
);
510 /* Update number of created stream counter. */
514 * Ensure a self-reference is preserved even
515 * after we have put our local reference.
517 if (!viewer_stream_get(viewer_stream
)) {
518 ERR("Unable to get self-reference on viewer stream, logic error.");
522 if (!viewer_stream
->sent_flag
&& nb_unsent
) {
523 /* Update number of unsent stream counter. */
527 /* Update number of total stream counter. */
529 if (relay_stream
->is_metadata
) {
530 if (!relay_stream
->closed
||
531 relay_stream
->metadata_received
>
532 viewer_stream
->metadata_sent
) {
536 if (!relay_stream
->closed
||
537 !(((int64_t)(relay_stream
->prev_data_seq
-
538 relay_stream
->last_net_seq_num
)) >=
544 /* Put local reference. */
545 viewer_stream_put(viewer_stream
);
547 pthread_mutex_unlock(&relay_stream
->lock
);
548 stream_put(relay_stream
);
551 ctf_trace_put(ctf_trace
);
560 pthread_mutex_unlock(&relay_stream
->lock
);
561 stream_put(relay_stream
);
567 int relayd_live_stop(void)
569 /* Stop dispatch thread */
570 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
571 futex_nto1_wake(&viewer_conn_queue
.futex
);
576 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
579 int create_named_thread_poll_set(struct lttng_poll_event
*events
,
580 int size
, const char *name
)
584 if (events
== NULL
|| size
== 0) {
589 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
590 name
, events
, 1, LTTNG_CLOEXEC
);
592 PERROR("Failed to create \"%s\" poll file descriptor", name
);
597 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
609 * Check if the thread quit pipe was triggered.
611 * Return 1 if it was triggered else 0;
614 int check_thread_quit_pipe(int fd
, uint32_t events
)
616 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
624 int create_sock(void *data
, int *out_fd
)
627 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
629 ret
= lttcomm_create_sock(sock
);
640 int close_sock(void *data
, int *in_fd
)
642 struct lttcomm_sock
*sock
= (lttcomm_sock
*) data
;
644 return sock
->ops
->close(sock
);
647 static int accept_sock(void *data
, int *out_fd
)
650 /* Socks is an array of in_sock, out_sock. */
651 struct lttcomm_sock
**socks
= (lttcomm_sock
**) data
;
652 struct lttcomm_sock
*in_sock
= socks
[0];
654 socks
[1] = in_sock
->ops
->accept(in_sock
);
659 *out_fd
= socks
[1]->fd
;
665 struct lttcomm_sock
*accept_live_sock(struct lttcomm_sock
*listening_sock
,
669 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
670 struct lttcomm_sock
*new_sock
= NULL
;
672 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &out_fd
,
673 (const char **) &name
, 1, accept_sock
, &socks
);
678 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
684 * Create and init socket from uri.
687 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
, const char *name
)
690 struct lttcomm_sock
*sock
= NULL
;
691 char uri_str
[LTTNG_PATH_MAX
];
692 char *formated_name
= NULL
;
694 sock
= lttcomm_alloc_sock_from_uri(uri
);
696 ERR("Allocating socket");
701 * Don't fail to create the socket if the name can't be built as it is
702 * only used for debugging purposes.
704 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
705 uri_str
[sizeof(uri_str
) - 1] = '\0';
707 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
710 formated_name
= NULL
;
714 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
715 (const char **) (formated_name
? &formated_name
: NULL
),
716 1, create_sock
, sock
);
718 PERROR("Failed to create \"%s\" socket",
719 formated_name
?: "Unknown");
722 DBG("Listening on %s socket %d", name
, sock
->fd
);
724 ret
= sock
->ops
->bind(sock
);
726 PERROR("Failed to bind lttng-live socket");
730 ret
= sock
->ops
->listen(sock
, -1);
741 lttcomm_destroy_sock(sock
);
748 * This thread manages the listening for new connections on the network
751 void *thread_listener(void *data
)
753 int i
, ret
, pollfd
, err
= -1;
754 uint32_t revents
, nb_fd
;
755 struct lttng_poll_event events
;
756 struct lttcomm_sock
*live_control_sock
;
758 DBG("[thread] Relay live listener started");
760 rcu_register_thread();
761 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
763 health_code_update();
765 live_control_sock
= init_socket(live_uri
, "Live listener");
766 if (!live_control_sock
) {
767 goto error_sock_control
;
770 /* Pass 2 as size here for the thread quit pipe and control sockets. */
771 ret
= create_named_thread_poll_set(&events
, 2,
772 "Live listener thread epoll");
774 goto error_create_poll
;
777 /* Add the control socket */
778 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
783 lttng_relay_notify_ready();
785 if (testpoint(relayd_thread_live_listener
)) {
786 goto error_testpoint
;
790 health_code_update();
792 DBG("Listener accepting live viewers connections");
796 ret
= lttng_poll_wait(&events
, -1);
800 * Restart interrupted system call.
802 if (errno
== EINTR
) {
809 DBG("Relay new viewer connection received");
810 for (i
= 0; i
< nb_fd
; i
++) {
811 health_code_update();
813 /* Fetch once the poll data */
814 revents
= LTTNG_POLL_GETEV(&events
, i
);
815 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
817 /* Thread quit pipe has been closed. Killing thread. */
818 ret
= check_thread_quit_pipe(pollfd
, revents
);
824 if (revents
& LPOLLIN
) {
826 * A new connection is requested, therefore a
827 * viewer connection is allocated in this
828 * thread, enqueued to a global queue and
829 * dequeued (and freed) in the worker thread.
832 struct relay_connection
*new_conn
;
833 struct lttcomm_sock
*newsock
;
835 newsock
= accept_live_sock(live_control_sock
,
836 "Live socket to client");
838 PERROR("accepting control sock");
841 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
843 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
846 PERROR("setsockopt inet");
847 lttcomm_destroy_sock(newsock
);
850 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
852 lttcomm_destroy_sock(newsock
);
855 /* Ownership assumed by the connection. */
858 /* Enqueue request for the dispatcher thread. */
859 cds_wfcq_head_ptr_t head
;
860 head
.h
= &viewer_conn_queue
.head
;
861 cds_wfcq_enqueue(head
, &viewer_conn_queue
.tail
,
865 * Wake the dispatch queue futex.
866 * Implicit memory barrier with the
867 * exchange in cds_wfcq_enqueue.
869 futex_nto1_wake(&viewer_conn_queue
.futex
);
870 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
871 ERR("socket poll error");
874 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
884 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
886 if (live_control_sock
->fd
>= 0) {
887 int sock_fd
= live_control_sock
->fd
;
889 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
890 &sock_fd
, 1, close_sock
,
895 live_control_sock
->fd
= -1;
897 lttcomm_destroy_sock(live_control_sock
);
901 DBG("Live viewer listener thread exited with error");
903 health_unregister(health_relayd
);
904 rcu_unregister_thread();
905 DBG("Live viewer listener thread cleanup complete");
906 if (lttng_relay_stop_threads()) {
907 ERR("Error stopping threads");
913 * This thread manages the dispatching of the requests to worker threads
916 void *thread_dispatcher(void *data
)
920 struct cds_wfcq_node
*node
;
921 struct relay_connection
*conn
= NULL
;
923 DBG("[thread] Live viewer relay dispatcher started");
925 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
927 if (testpoint(relayd_thread_live_dispatcher
)) {
928 goto error_testpoint
;
931 health_code_update();
934 health_code_update();
936 /* Atomically prepare the queue futex */
937 futex_nto1_prepare(&viewer_conn_queue
.futex
);
939 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
944 health_code_update();
946 /* Dequeue commands */
947 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
948 &viewer_conn_queue
.tail
);
950 DBG("Woken up but nothing in the live-viewer "
951 "relay command queue");
952 /* Continue thread execution */
955 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
956 DBG("Dispatching viewer request waiting on sock %d",
960 * Inform worker thread of the new request. This
961 * call is blocking so we can be assured that
962 * the data will be read at some point in time
963 * or wait to the end of the world :)
965 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
967 PERROR("write conn pipe");
968 connection_put(conn
);
971 } while (node
!= NULL
);
973 /* Futex wait on queue. Blocking call on futex() */
975 futex_nto1_wait(&viewer_conn_queue
.futex
);
979 /* Normal exit, no error */
986 ERR("Health error occurred in %s", __func__
);
988 health_unregister(health_relayd
);
989 DBG("Live viewer dispatch thread dying");
990 if (lttng_relay_stop_threads()) {
991 ERR("Error stopping threads");
997 * Establish connection with the viewer and check the versions.
999 * Return 0 on success or else negative value.
1002 int viewer_connect(struct relay_connection
*conn
)
1005 struct lttng_viewer_connect reply
, msg
;
1007 conn
->version_check_done
= 1;
1009 health_code_update();
1011 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
1016 health_code_update();
1018 memset(&reply
, 0, sizeof(reply
));
1019 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
1020 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
1022 /* Major versions must be the same */
1023 if (reply
.major
!= be32toh(msg
.major
)) {
1024 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
1025 reply
.major
, be32toh(msg
.major
));
1030 conn
->major
= reply
.major
;
1031 /* We adapt to the lowest compatible version */
1032 if (reply
.minor
<= be32toh(msg
.minor
)) {
1033 conn
->minor
= reply
.minor
;
1035 conn
->minor
= be32toh(msg
.minor
);
1038 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
1039 conn
->type
= RELAY_VIEWER_COMMAND
;
1040 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
1041 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
1043 ERR("Unknown connection type : %u", be32toh(msg
.type
));
1048 reply
.major
= htobe32(reply
.major
);
1049 reply
.minor
= htobe32(reply
.minor
);
1050 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
1052 * Increment outside of htobe64 macro, because the argument can
1053 * be used more than once within the macro, and thus the
1054 * operation may be undefined.
1056 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
1057 last_relay_viewer_session_id
++;
1058 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
1059 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
1062 health_code_update();
1064 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1069 health_code_update();
1071 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
1079 * Send the viewer the list of current sessions.
1080 * We need to create a copy of the hash table content because otherwise
1081 * we cannot assume the number of entries stays the same between getting
1082 * the number of HT elements and iteration over the HT.
1084 * Return 0 on success or else a negative value.
1087 int viewer_list_sessions(struct relay_connection
*conn
)
1090 struct lttng_viewer_list_sessions session_list
;
1091 struct lttng_ht_iter iter
;
1092 struct relay_session
*session
;
1093 struct lttng_viewer_session
*send_session_buf
= NULL
;
1094 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
1097 send_session_buf
= (lttng_viewer_session
*) zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
1098 if (!send_session_buf
) {
1103 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
1105 struct lttng_viewer_session
*send_session
;
1107 health_code_update();
1109 pthread_mutex_lock(&session
->lock
);
1110 if (session
->connection_closed
) {
1111 /* Skip closed session */
1115 if (count
>= buf_count
) {
1116 struct lttng_viewer_session
*newbuf
;
1117 uint32_t new_buf_count
= buf_count
<< 1;
1119 newbuf
= (lttng_viewer_session
*) realloc(send_session_buf
,
1120 new_buf_count
* sizeof(*send_session_buf
));
1125 send_session_buf
= newbuf
;
1126 buf_count
= new_buf_count
;
1128 send_session
= &send_session_buf
[count
];
1129 if (lttng_strncpy(send_session
->session_name
,
1130 session
->session_name
,
1131 sizeof(send_session
->session_name
))) {
1135 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
1136 sizeof(send_session
->hostname
))) {
1140 send_session
->id
= htobe64(session
->id
);
1141 send_session
->live_timer
= htobe32(session
->live_timer
);
1142 if (session
->viewer_attached
) {
1143 send_session
->clients
= htobe32(1);
1145 send_session
->clients
= htobe32(0);
1147 send_session
->streams
= htobe32(session
->stream_count
);
1150 pthread_mutex_unlock(&session
->lock
);
1153 pthread_mutex_unlock(&session
->lock
);
1161 session_list
.sessions_count
= htobe32(count
);
1163 health_code_update();
1165 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
1170 health_code_update();
1172 ret
= send_response(conn
->sock
, send_session_buf
,
1173 count
* sizeof(*send_session_buf
));
1177 health_code_update();
1181 free(send_session_buf
);
1186 * Send the viewer the list of current streams.
1189 int viewer_get_new_streams(struct relay_connection
*conn
)
1191 int ret
, send_streams
= 0;
1192 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
1193 struct lttng_viewer_new_streams_request request
;
1194 struct lttng_viewer_new_streams_response response
;
1195 struct relay_session
*session
= NULL
;
1196 uint64_t session_id
;
1197 bool closed
= false;
1201 health_code_update();
1203 /* Receive the request from the connected client. */
1204 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1208 session_id
= be64toh(request
.session_id
);
1210 health_code_update();
1212 memset(&response
, 0, sizeof(response
));
1214 session
= session_get_by_id(session_id
);
1216 DBG("Relay session %" PRIu64
" not found", session_id
);
1217 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1221 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1222 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1227 * For any new stream, create it with LTTNG_VIEWER_SEEK_BEGINNING since
1228 * that at this point the client is already attached to the session.Aany
1229 * initial stream will have been created with the seek type at attach
1230 * time (for now most readers use the LTTNG_VIEWER_SEEK_LAST on attach).
1231 * Otherwise any event happening in a new stream between the attach and
1232 * a call to viewer_get_new_streams will be "lost" (never received) from
1233 * the viewer's point of view.
1235 pthread_mutex_lock(&session
->lock
);
1237 * If a session rotation is ongoing, do not attempt to open any
1238 * stream, because the chunk can be in an intermediate state
1239 * due to directory renaming.
1241 if (session
->ongoing_rotation
) {
1242 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1243 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_NO_NEW
);
1244 goto send_reply_unlock
;
1246 ret
= make_viewer_streams(session
,
1247 conn
->viewer_session
,
1248 LTTNG_VIEWER_SEEK_BEGINNING
, &nb_total
, &nb_unsent
,
1249 &nb_created
, &closed
);
1251 goto error_unlock_session
;
1254 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1256 /* Only send back the newly created streams with the unsent ones. */
1257 nb_streams
= nb_created
+ nb_unsent
;
1258 response
.streams_count
= htobe32(nb_streams
);
1261 * If the session is closed, HUP when there are no more streams
1264 if (closed
&& nb_total
== 0) {
1266 response
.streams_count
= 0;
1267 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1268 goto send_reply_unlock
;
1271 pthread_mutex_unlock(&session
->lock
);
1274 health_code_update();
1275 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1277 goto end_put_session
;
1279 health_code_update();
1282 * Unknown or empty session, just return gracefully, the viewer
1283 * knows what is happening.
1285 if (!send_streams
|| !nb_streams
) {
1287 goto end_put_session
;
1291 * Send stream and *DON'T* ignore the sent flag so every viewer
1292 * streams that were not sent from that point will be sent to
1295 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1297 goto end_put_session
;
1302 session_put(session
);
1306 error_unlock_session
:
1307 pthread_mutex_unlock(&session
->lock
);
1308 session_put(session
);
1313 * Send the viewer the list of current sessions.
1316 int viewer_attach_session(struct relay_connection
*conn
)
1318 int send_streams
= 0;
1320 uint32_t nb_streams
= 0;
1321 enum lttng_viewer_seek seek_type
;
1322 struct lttng_viewer_attach_session_request request
;
1323 struct lttng_viewer_attach_session_response response
;
1324 struct relay_session
*session
= NULL
;
1325 enum lttng_viewer_attach_return_code viewer_attach_status
;
1326 bool closed
= false;
1327 uint64_t session_id
;
1331 health_code_update();
1333 /* Receive the request from the connected client. */
1334 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1339 session_id
= be64toh(request
.session_id
);
1341 health_code_update();
1343 memset(&response
, 0, sizeof(response
));
1345 if (!conn
->viewer_session
) {
1346 viewer_attach_status
= LTTNG_VIEWER_ATTACH_NO_SESSION
;
1347 DBG("Client trying to attach before creating a live viewer session, returning status=%s",
1348 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1352 session
= session_get_by_id(session_id
);
1354 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
1355 DBG("Relay session %" PRIu64
" not found, returning status=%s",
1357 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1360 DBG("Attach relay session ID %" PRIu64
" received", session_id
);
1362 pthread_mutex_lock(&session
->lock
);
1363 if (session
->live_timer
== 0) {
1364 viewer_attach_status
= LTTNG_VIEWER_ATTACH_NOT_LIVE
;
1365 DBG("Relay session ID %" PRIu64
" is not a live session, returning status=%s",
1367 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1372 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1374 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1375 DBG("Error attaching to relay session %" PRIu64
", returning status=%s",
1377 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1381 switch (be32toh(request
.seek
)) {
1382 case LTTNG_VIEWER_SEEK_BEGINNING
:
1383 case LTTNG_VIEWER_SEEK_LAST
:
1384 viewer_attach_status
= LTTNG_VIEWER_ATTACH_OK
;
1385 seek_type
= (lttng_viewer_seek
) be32toh(request
.seek
);
1388 ERR("Wrong seek parameter for relay session %" PRIu64
1389 ", returning status=%s", session_id
,
1390 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1391 viewer_attach_status
= LTTNG_VIEWER_ATTACH_SEEK_ERR
;
1397 * If a session rotation is ongoing, do not attempt to open any
1398 * stream, because the chunk can be in an intermediate state
1399 * due to directory renaming.
1401 if (session
->ongoing_rotation
) {
1402 DBG("Relay session %" PRIu64
" rotation ongoing", session_id
);
1407 ret
= make_viewer_streams(session
,
1408 conn
->viewer_session
, seek_type
,
1409 &nb_streams
, NULL
, NULL
, &closed
);
1411 goto end_put_session
;
1413 pthread_mutex_unlock(&session
->lock
);
1414 session_put(session
);
1417 response
.streams_count
= htobe32(nb_streams
);
1419 * If the session is closed when the viewer is attaching, it
1420 * means some of the streams may have been concurrently removed,
1421 * so we don't allow the viewer to attach, even if there are
1422 * streams available.
1426 response
.streams_count
= 0;
1427 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
1428 ERR("Session %" PRIu64
" is closed, returning status=%s",
1430 lttng_viewer_attach_return_code_str(viewer_attach_status
));
1435 health_code_update();
1437 response
.status
= htobe32((uint32_t) viewer_attach_status
);
1439 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1441 goto end_put_session
;
1443 health_code_update();
1446 * Unknown or empty session, just return gracefully, the viewer
1447 * knows what is happening.
1449 if (!send_streams
|| !nb_streams
) {
1451 goto end_put_session
;
1454 /* Send stream and ignore the sent flag. */
1455 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1457 goto end_put_session
;
1462 pthread_mutex_unlock(&session
->lock
);
1463 session_put(session
);
1470 * Open the index file if needed for the given vstream.
1472 * If an index file is successfully opened, the vstream will set it as its
1473 * current index file.
1475 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1477 * Called with rstream lock held.
1479 static int try_open_index(struct relay_viewer_stream
*vstream
,
1480 struct relay_stream
*rstream
)
1483 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1484 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1485 enum lttng_trace_chunk_status chunk_status
;
1487 if (vstream
->index_file
) {
1492 * First time, we open the index file and at least one index is ready.
1494 if (rstream
->index_received_seqcount
== 0 ||
1495 !vstream
->stream_file
.trace_chunk
) {
1500 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1501 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1502 rstream
->channel_name
, rstream
->tracefile_size
,
1503 vstream
->current_tracefile_id
,
1504 lttng_to_index_major(connection_major
, connection_minor
),
1505 lttng_to_index_minor(connection_major
, connection_minor
),
1506 true, &vstream
->index_file
);
1507 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1508 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1520 * Check the status of the index for the given stream. This function
1521 * updates the index structure if needed and can put (close) the vstream
1522 * in the HUP situation.
1524 * Return 0 means that we can proceed with the index. A value of 1 means
1525 * that the index has been updated and is ready to be sent to the
1526 * client. A negative value indicates an error that can't be handled.
1528 * Called with rstream lock held.
1530 static int check_index_status(struct relay_viewer_stream
*vstream
,
1531 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1532 struct lttng_viewer_index
*index
)
1536 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1537 "index_sent_seqcount %" PRIu64
" "
1538 "for stream %" PRIu64
,
1539 rstream
->index_received_seqcount
,
1540 vstream
->index_sent_seqcount
,
1541 vstream
->stream
->stream_handle
);
1542 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1543 && rstream
->index_received_seqcount
1544 == vstream
->index_sent_seqcount
) {
1546 * Last index sent and session connection or relay
1547 * stream are closed.
1549 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1550 DBG("Check index status: Connection or stream are closed, stream %" PRIu64
1551 ",connection-closed=%d, relay-stream-closed=%d, returning status=%s",
1552 vstream
->stream
->stream_handle
,
1553 trace
->session
->connection_closed
, rstream
->closed
,
1554 lttng_viewer_next_index_return_code_str(
1555 (enum lttng_viewer_next_index_return_code
) index
->status
));
1557 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1558 (rstream
->index_received_seqcount
== 0 ||
1559 (vstream
->index_sent_seqcount
!= 0 &&
1560 rstream
->index_received_seqcount
1561 <= vstream
->index_sent_seqcount
))) {
1563 * We've received a synchronization beacon and the last index
1564 * available has been sent, the index for now is inactive.
1566 * In this case, we have received a beacon which allows us to
1567 * inform the client of a time interval during which we can
1568 * guarantee that there are no events to read (and never will
1571 * The sent seqcount can grow higher than receive seqcount on
1572 * clear because the rotation performed by clear will push
1573 * the index_sent_seqcount ahead (see
1574 * viewer_stream_sync_tracefile_array_tail) and skip over
1575 * packet sequence numbers.
1577 index
->status
= LTTNG_VIEWER_INDEX_INACTIVE
;
1578 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1579 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1580 DBG("Check index status: inactive with beacon, for stream %" PRIu64
1581 ", returning status=%s",
1582 vstream
->stream
->stream_handle
,
1583 lttng_viewer_next_index_return_code_str(
1584 (enum lttng_viewer_next_index_return_code
) index
->status
));
1586 } else if (rstream
->index_received_seqcount
== 0 ||
1587 (vstream
->index_sent_seqcount
!= 0 &&
1588 rstream
->index_received_seqcount
1589 <= vstream
->index_sent_seqcount
)) {
1591 * This checks whether received <= sent seqcount. In
1592 * this case, we have not received a beacon. Therefore,
1593 * we can only ask the client to retry later.
1595 * The sent seqcount can grow higher than receive seqcount on
1596 * clear because the rotation performed by clear will push
1597 * the index_sent_seqcount ahead (see
1598 * viewer_stream_sync_tracefile_array_tail) and skip over
1599 * packet sequence numbers.
1601 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1602 DBG("Check index status:"
1603 "did not received beacon for stream %" PRIu64
1604 ", returning status=%s",
1605 vstream
->stream
->stream_handle
,
1606 lttng_viewer_next_index_return_code_str(
1607 (enum lttng_viewer_next_index_return_code
) index
->status
));
1609 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1610 vstream
->current_tracefile_id
,
1611 vstream
->index_sent_seqcount
)) {
1613 * The next index we want to send cannot be read either
1614 * because we need to perform a rotation, or due to
1615 * the producer having overwritten its trace file.
1617 DBG("Viewer stream %" PRIu64
" rotation",
1618 vstream
->stream
->stream_handle
);
1619 ret
= viewer_stream_rotate(vstream
);
1621 /* EOF across entire stream. */
1622 index
->status
= LTTNG_VIEWER_INDEX_HUP
;
1623 DBG("Check index status:"
1624 "reached end of file for stream %" PRIu64
1625 ", returning status=%s",
1626 vstream
->stream
->stream_handle
,
1627 lttng_viewer_next_index_return_code_str(
1628 (enum lttng_viewer_next_index_return_code
) index
->status
));
1632 * If we have been pushed due to overwrite, it
1633 * necessarily means there is data that can be read in
1634 * the stream. If we rotated because we reached the end
1635 * of a tracefile, it means the following tracefile
1636 * needs to contain at least one index, else we would
1637 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1638 * viewer. The updated index_sent_seqcount needs to
1639 * point to a readable index entry now.
1641 * In the case where we "rotate" on a single file, we
1642 * can end up in a case where the requested index is
1643 * still unavailable.
1645 if (rstream
->tracefile_count
== 1 &&
1646 !tracefile_array_seq_in_file(
1648 vstream
->current_tracefile_id
,
1649 vstream
->index_sent_seqcount
)) {
1650 index
->status
= LTTNG_VIEWER_INDEX_RETRY
;
1651 DBG("Check index status:"
1652 "tracefile array sequence number %" PRIu64
1653 " not in file for stream %" PRIu64
1654 ", returning status=%s",
1655 vstream
->index_sent_seqcount
,
1656 vstream
->stream
->stream_handle
,
1657 lttng_viewer_next_index_return_code_str(
1658 (enum lttng_viewer_next_index_return_code
) index
->status
));
1661 LTTNG_ASSERT(tracefile_array_seq_in_file(rstream
->tfa
,
1662 vstream
->current_tracefile_id
,
1663 vstream
->index_sent_seqcount
));
1665 /* ret == 0 means successful so we continue. */
1670 viewer_stream_put(vstream
);
1676 void viewer_stream_rotate_to_trace_chunk(struct relay_viewer_stream
*vstream
,
1677 struct lttng_trace_chunk
*new_trace_chunk
)
1679 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1681 if (new_trace_chunk
) {
1682 const bool acquired_reference
= lttng_trace_chunk_get(
1685 LTTNG_ASSERT(acquired_reference
);
1688 vstream
->stream_file
.trace_chunk
= new_trace_chunk
;
1689 viewer_stream_sync_tracefile_array_tail(vstream
);
1690 viewer_stream_close_files(vstream
);
1694 * Send the next index for a stream.
1696 * Return 0 on success or else a negative value.
1699 int viewer_get_next_index(struct relay_connection
*conn
)
1702 struct lttng_viewer_get_next_index request_index
;
1703 struct lttng_viewer_index viewer_index
;
1704 struct ctf_packet_index packet_index
;
1705 struct relay_viewer_stream
*vstream
= NULL
;
1706 struct relay_stream
*rstream
= NULL
;
1707 struct ctf_trace
*ctf_trace
= NULL
;
1708 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1709 bool viewer_stream_and_session_in_same_chunk
, viewer_stream_one_rotation_behind
;
1710 uint64_t stream_file_chunk_id
= -1ULL, viewer_session_chunk_id
= -1ULL;
1711 enum lttng_trace_chunk_status status
;
1715 memset(&viewer_index
, 0, sizeof(viewer_index
));
1716 health_code_update();
1718 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1722 health_code_update();
1724 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1726 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1727 DBG("Client requested index of unknown stream id %" PRIu64
", returning status=%s",
1728 (uint64_t) be64toh(request_index
.stream_id
),
1729 lttng_viewer_next_index_return_code_str(
1730 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1734 /* Use back. ref. Protected by refcounts. */
1735 rstream
= vstream
->stream
;
1736 ctf_trace
= rstream
->trace
;
1738 /* metadata_viewer_stream may be NULL. */
1739 metadata_viewer_stream
=
1740 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1743 * Hold the session lock to protect against concurrent changes
1744 * to the chunk files (e.g. rename done by clear), which are
1745 * protected by the session ongoing rotation state. Those are
1746 * synchronized with the session lock.
1748 pthread_mutex_lock(&rstream
->trace
->session
->lock
);
1749 pthread_mutex_lock(&rstream
->lock
);
1752 * The viewer should not ask for index on metadata stream.
1754 if (rstream
->is_metadata
) {
1755 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1756 DBG("Client requested index of a metadata stream id %" PRIu64
", returning status=%s",
1757 (uint64_t) be64toh(request_index
.stream_id
),
1758 lttng_viewer_next_index_return_code_str(
1759 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1763 if (rstream
->ongoing_rotation
.is_set
) {
1764 /* Rotation is ongoing, try again later. */
1765 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1766 DBG("Client requested index for stream id %" PRIu64
" while a stream rotation is ongoing, returning status=%s",
1767 (uint64_t) be64toh(request_index
.stream_id
),
1768 lttng_viewer_next_index_return_code_str(
1769 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1773 if (rstream
->trace
->session
->ongoing_rotation
) {
1774 /* Rotation is ongoing, try again later. */
1775 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1776 DBG("Client requested index for stream id %" PRIu64
" while a session rotation is ongoing, returning status=%s",
1777 (uint64_t) be64toh(request_index
.stream_id
),
1778 lttng_viewer_next_index_return_code_str(
1779 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1784 * Transition the viewer session into the newest trace chunk available.
1786 if (!lttng_trace_chunk_ids_equal(
1787 conn
->viewer_session
->current_trace_chunk
,
1788 rstream
->trace_chunk
)) {
1789 DBG("Relay stream and viewer chunk ids differ");
1791 ret
= viewer_session_set_trace_chunk_copy(
1792 conn
->viewer_session
,
1793 rstream
->trace_chunk
);
1795 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1796 ERR("Error copying trace chunk for stream id %" PRIu64
1797 ", returning status=%s",
1798 (uint64_t) be64toh(request_index
.stream_id
),
1799 lttng_viewer_next_index_return_code_str(
1800 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1806 * Transition the viewer stream into the latest trace chunk available.
1808 * Note that the stream must _not_ rotate in one precise condition:
1809 * the relay stream has rotated to a NULL trace chunk and the viewer
1810 * stream is consuming the trace chunk that was active just before
1811 * that rotation to NULL.
1813 * This allows clients to consume all the packets of a trace chunk
1814 * after a session's destruction.
1816 if (vstream
->stream_file
.trace_chunk
) {
1817 status
= lttng_trace_chunk_get_id(
1818 vstream
->stream_file
.trace_chunk
,
1819 &stream_file_chunk_id
);
1820 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1822 if (conn
->viewer_session
->current_trace_chunk
) {
1823 status
= lttng_trace_chunk_get_id(
1824 conn
->viewer_session
->current_trace_chunk
,
1825 &viewer_session_chunk_id
);
1826 LTTNG_ASSERT(status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
1829 viewer_stream_and_session_in_same_chunk
= lttng_trace_chunk_ids_equal(
1830 conn
->viewer_session
->current_trace_chunk
,
1831 vstream
->stream_file
.trace_chunk
);
1832 viewer_stream_one_rotation_behind
= rstream
->completed_rotation_count
==
1833 vstream
->last_seen_rotation_count
+ 1;
1835 if (viewer_stream_and_session_in_same_chunk
) {
1836 DBG("Transition to latest chunk check (%s -> %s): Same chunk, no need to rotate",
1837 vstream
->stream_file
.trace_chunk
?
1838 std::to_string(stream_file_chunk_id
).c_str() :
1840 conn
->viewer_session
->current_trace_chunk
?
1841 std::to_string(viewer_session_chunk_id
).c_str() :
1843 } else if (viewer_stream_one_rotation_behind
&& !rstream
->trace_chunk
) {
1844 DBG("Transition to latest chunk check (%s -> %s): One chunk behind relay stream which is being destroyed, no need to rotate",
1845 vstream
->stream_file
.trace_chunk
?
1846 std::to_string(stream_file_chunk_id
).c_str() :
1848 conn
->viewer_session
->current_trace_chunk
?
1849 std::to_string(viewer_session_chunk_id
).c_str() :
1852 DBG("Transition to latest chunk check (%s -> %s): Viewer stream chunk ID and viewer session chunk ID differ, rotating viewer stream",
1853 vstream
->stream_file
.trace_chunk
?
1854 std::to_string(stream_file_chunk_id
).c_str() :
1856 conn
->viewer_session
->current_trace_chunk
?
1857 std::to_string(viewer_session_chunk_id
).c_str() :
1860 viewer_stream_rotate_to_trace_chunk(vstream
,
1861 conn
->viewer_session
->current_trace_chunk
);
1862 vstream
->last_seen_rotation_count
=
1863 rstream
->completed_rotation_count
;
1866 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1869 } else if (ret
== 1) {
1871 * We have no index to send and check_index_status has populated
1872 * viewer_index's status.
1876 /* At this point, ret is 0 thus we will be able to read the index. */
1879 /* Try to open an index if one is needed for that stream. */
1880 ret
= try_open_index(vstream
, rstream
);
1881 if (ret
== -ENOENT
) {
1882 if (rstream
->closed
) {
1883 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1884 DBG("Cannot open index for stream id %" PRIu64
1885 "stream is closed, returning status=%s",
1886 (uint64_t) be64toh(request_index
.stream_id
),
1887 lttng_viewer_next_index_return_code_str(
1888 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1891 viewer_index
.status
= LTTNG_VIEWER_INDEX_RETRY
;
1892 DBG("Cannot open index for stream id %" PRIu64
1893 ", returning status=%s",
1894 (uint64_t) be64toh(request_index
.stream_id
),
1895 lttng_viewer_next_index_return_code_str(
1896 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1901 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1902 ERR("Error opening index for stream id %" PRIu64
1903 ", returning status=%s",
1904 (uint64_t) be64toh(request_index
.stream_id
),
1905 lttng_viewer_next_index_return_code_str(
1906 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1911 * vstream->stream_fd may be NULL if it has been closed by
1912 * tracefile rotation, or if we are at the beginning of the
1913 * stream. We open the data stream file here to protect against
1914 * overwrite caused by tracefile rotation (in association with
1915 * unlink performed before overwrite).
1917 if (!vstream
->stream_file
.handle
) {
1918 char file_path
[LTTNG_PATH_MAX
];
1919 struct fs_handle
*fs_handle
;
1921 ret
= utils_stream_file_path(rstream
->path_name
,
1922 rstream
->channel_name
, rstream
->tracefile_size
,
1923 vstream
->current_tracefile_id
, NULL
, file_path
,
1930 * It is possible the the file we are trying to open is
1931 * missing if the stream has been closed (application exits with
1932 * per-pid buffers) and a clear command has been performed.
1934 status
= lttng_trace_chunk_open_fs_handle(
1935 vstream
->stream_file
.trace_chunk
,
1936 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1937 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1938 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1940 viewer_index
.status
= LTTNG_VIEWER_INDEX_HUP
;
1941 DBG("Cannot find trace chunk file and stream is closed for stream id %" PRIu64
1942 ", returning status=%s",
1943 (uint64_t) be64toh(request_index
.stream_id
),
1944 lttng_viewer_next_index_return_code_str(
1945 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1948 PERROR("Failed to open trace file for viewer stream");
1951 vstream
->stream_file
.handle
= fs_handle
;
1954 ret
= check_new_streams(conn
);
1956 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1957 ERR("Error checking for new streams before sending new index to stream id %" PRIu64
1958 ", returning status=%s",
1959 (uint64_t) be64toh(request_index
.stream_id
),
1960 lttng_viewer_next_index_return_code_str(
1961 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1963 } else if (ret
== 1) {
1964 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1967 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1969 viewer_index
.status
= LTTNG_VIEWER_INDEX_ERR
;
1970 ERR("Relay error reading index file for stream id %" PRIu64
1971 ", returning status=%s",
1972 (uint64_t) be64toh(request_index
.stream_id
),
1973 lttng_viewer_next_index_return_code_str(
1974 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1977 viewer_index
.status
= LTTNG_VIEWER_INDEX_OK
;
1978 DBG("Read index file for stream id %" PRIu64
1979 ", returning status=%s",
1980 (uint64_t) be64toh(request_index
.stream_id
),
1981 lttng_viewer_next_index_return_code_str(
1982 (enum lttng_viewer_next_index_return_code
) viewer_index
.status
));
1983 vstream
->index_sent_seqcount
++;
1987 * Indexes are stored in big endian, no need to switch before sending.
1989 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1990 rstream
->stream_handle
,
1991 (uint64_t) be64toh(packet_index
.offset
));
1992 viewer_index
.offset
= packet_index
.offset
;
1993 viewer_index
.packet_size
= packet_index
.packet_size
;
1994 viewer_index
.content_size
= packet_index
.content_size
;
1995 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1996 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1997 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1998 viewer_index
.stream_id
= packet_index
.stream_id
;
2002 pthread_mutex_unlock(&rstream
->lock
);
2003 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
2006 if (metadata_viewer_stream
) {
2007 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
2008 DBG("get next index metadata check: recv %" PRIu64
2010 metadata_viewer_stream
->stream
->metadata_received
,
2011 metadata_viewer_stream
->metadata_sent
);
2012 if (!metadata_viewer_stream
->stream
->metadata_received
||
2013 metadata_viewer_stream
->stream
->metadata_received
>
2014 metadata_viewer_stream
->metadata_sent
) {
2015 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
2017 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
2020 viewer_index
.flags
= htobe32(viewer_index
.flags
);
2021 viewer_index
.status
= htobe32(viewer_index
.status
);
2022 health_code_update();
2024 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
2028 health_code_update();
2031 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
2032 vstream
->index_sent_seqcount
,
2033 vstream
->stream
->stream_handle
);
2036 if (metadata_viewer_stream
) {
2037 viewer_stream_put(metadata_viewer_stream
);
2040 viewer_stream_put(vstream
);
2045 pthread_mutex_unlock(&rstream
->lock
);
2046 pthread_mutex_unlock(&rstream
->trace
->session
->lock
);
2047 if (metadata_viewer_stream
) {
2048 viewer_stream_put(metadata_viewer_stream
);
2050 viewer_stream_put(vstream
);
2055 * Send the next index for a stream
2057 * Return 0 on success or else a negative value.
2060 int viewer_get_packet(struct relay_connection
*conn
)
2065 struct lttng_viewer_get_packet get_packet_info
;
2066 struct lttng_viewer_trace_packet reply_header
;
2067 struct relay_viewer_stream
*vstream
= NULL
;
2068 uint32_t reply_size
= sizeof(reply_header
);
2069 uint32_t packet_data_len
= 0;
2073 health_code_update();
2075 ret
= recv_request(conn
->sock
, &get_packet_info
,
2076 sizeof(get_packet_info
));
2080 health_code_update();
2082 /* From this point on, the error label can be reached. */
2083 memset(&reply_header
, 0, sizeof(reply_header
));
2084 stream_id
= (uint64_t) be64toh(get_packet_info
.stream_id
);
2086 vstream
= viewer_stream_get_by_id(stream_id
);
2088 DBG("Client requested packet of unknown stream id %" PRIu64
,
2090 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
2091 goto send_reply_nolock
;
2093 packet_data_len
= be32toh(get_packet_info
.len
);
2094 reply_size
+= packet_data_len
;
2097 reply
= (char *) zmalloc(reply_size
);
2099 PERROR("packet reply zmalloc");
2100 reply_size
= sizeof(reply_header
);
2104 pthread_mutex_lock(&vstream
->stream
->lock
);
2105 lseek_ret
= fs_handle_seek(vstream
->stream_file
.handle
,
2106 be64toh(get_packet_info
.offset
), SEEK_SET
);
2107 if (lseek_ret
< 0) {
2108 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
2109 " to offset %" PRIu64
,
2111 (uint64_t) be64toh(get_packet_info
.offset
));
2114 read_len
= fs_handle_read(vstream
->stream_file
.handle
,
2115 reply
+ sizeof(reply_header
), packet_data_len
);
2116 if (read_len
< packet_data_len
) {
2117 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
2118 ", offset: %" PRIu64
,
2120 (uint64_t) be64toh(get_packet_info
.offset
));
2123 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
2124 reply_header
.len
= htobe32(packet_data_len
);
2128 /* No payload to send on error. */
2129 reply_size
= sizeof(reply_header
);
2130 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
2134 pthread_mutex_unlock(&vstream
->stream
->lock
);
2138 health_code_update();
2141 memcpy(reply
, &reply_header
, sizeof(reply_header
));
2142 ret
= send_response(conn
->sock
, reply
, reply_size
);
2144 /* No reply to send. */
2145 ret
= send_response(conn
->sock
, &reply_header
,
2149 health_code_update();
2151 PERROR("sendmsg of packet data failed");
2155 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
, stream_id
);
2161 viewer_stream_put(vstream
);
2167 * Send the session's metadata
2169 * Return 0 on success else a negative value.
2172 int viewer_get_metadata(struct relay_connection
*conn
)
2179 struct lttng_viewer_get_metadata request
;
2180 struct lttng_viewer_metadata_packet reply
;
2181 struct relay_viewer_stream
*vstream
= NULL
;
2185 health_code_update();
2187 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2191 health_code_update();
2193 memset(&reply
, 0, sizeof(reply
));
2195 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
2198 * The metadata stream can be closed by a CLOSE command
2199 * just before we attach. It can also be closed by
2200 * per-pid tracing during tracing. Therefore, it is
2201 * possible that we cannot find this viewer stream.
2202 * Reply back to the client with an error if we cannot
2205 DBG("Client requested metadata of unknown stream id %" PRIu64
,
2206 (uint64_t) be64toh(request
.stream_id
));
2207 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2210 pthread_mutex_lock(&vstream
->stream
->lock
);
2211 if (!vstream
->stream
->is_metadata
) {
2212 ERR("Invalid metadata stream");
2216 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
2218 * The live viewers expect to receive a NO_NEW_METADATA
2219 * status before a stream disappears, otherwise they abort the
2220 * entire live connection when receiving an error status.
2222 * Clear feature resets the metadata_sent to 0 until the
2223 * same metadata is received again.
2225 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2227 * The live viewer considers a closed 0 byte metadata stream as
2230 if (vstream
->metadata_sent
> 0) {
2231 if (vstream
->stream
->closed
&& vstream
->stream
->no_new_metadata_notified
) {
2233 * Release ownership for the viewer metadata
2234 * stream. Note that this reference is the
2235 * viewer's reference. The vstream still exists
2236 * until the end of the function as
2237 * viewer_stream_get_by_id() took a reference.
2239 viewer_stream_put(vstream
);
2242 vstream
->stream
->no_new_metadata_notified
= true;
2247 if (vstream
->stream
->trace_chunk
&&
2248 !lttng_trace_chunk_ids_equal(
2249 conn
->viewer_session
->current_trace_chunk
,
2250 vstream
->stream
->trace_chunk
)) {
2251 /* A rotation has occurred on the relay stream. */
2252 DBG("Metadata relay stream and viewer chunk ids differ");
2254 ret
= viewer_session_set_trace_chunk_copy(
2255 conn
->viewer_session
,
2256 vstream
->stream
->trace_chunk
);
2258 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2263 if (conn
->viewer_session
->current_trace_chunk
&&
2264 !lttng_trace_chunk_ids_equal(conn
->viewer_session
->current_trace_chunk
,
2265 vstream
->stream_file
.trace_chunk
)) {
2266 bool acquired_reference
;
2268 DBG("Viewer session and viewer stream chunk differ: "
2269 "vsession chunk %p vstream chunk %p",
2270 conn
->viewer_session
->current_trace_chunk
,
2271 vstream
->stream_file
.trace_chunk
);
2272 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
2273 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
2274 LTTNG_ASSERT(acquired_reference
);
2275 vstream
->stream_file
.trace_chunk
=
2276 conn
->viewer_session
->current_trace_chunk
;
2277 viewer_stream_close_files(vstream
);
2280 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
2282 if (!vstream
->stream_file
.trace_chunk
) {
2283 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2286 } else if (vstream
->stream_file
.trace_chunk
&&
2287 !vstream
->stream_file
.handle
&& len
> 0) {
2289 * Either this is the first time the metadata file is read, or a
2290 * rotation of the corresponding relay stream has occurred.
2292 struct fs_handle
*fs_handle
;
2293 char file_path
[LTTNG_PATH_MAX
];
2294 enum lttng_trace_chunk_status status
;
2295 struct relay_stream
*rstream
= vstream
->stream
;
2297 ret
= utils_stream_file_path(rstream
->path_name
,
2298 rstream
->channel_name
, rstream
->tracefile_size
,
2299 vstream
->current_tracefile_id
, NULL
, file_path
,
2306 * It is possible the the metadata file we are trying to open is
2307 * missing if the stream has been closed (application exits with
2308 * per-pid buffers) and a clear command has been performed.
2310 status
= lttng_trace_chunk_open_fs_handle(
2311 vstream
->stream_file
.trace_chunk
,
2312 file_path
, O_RDONLY
, 0, &fs_handle
, true);
2313 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
2314 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
2315 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
2317 if (vstream
->stream
->closed
) {
2318 viewer_stream_put(vstream
);
2322 PERROR("Failed to open metadata file for viewer stream");
2325 vstream
->stream_file
.handle
= fs_handle
;
2327 if (vstream
->metadata_sent
!= 0) {
2329 * The client does not expect to receive any metadata
2330 * it has received and metadata files in successive
2331 * chunks must be a strict superset of one another.
2333 * Skip the first `metadata_sent` bytes to ensure
2334 * they are not sent a second time to the client.
2336 * Baring a block layer error or an internal error,
2337 * this seek should not fail as
2338 * `vstream->stream->metadata_received` is reset when
2339 * a relay stream is rotated. If this is reached, it is
2340 * safe to assume that
2341 * `metadata_received` > `metadata_sent`.
2343 const off_t seek_ret
= fs_handle_seek(fs_handle
,
2344 vstream
->metadata_sent
, SEEK_SET
);
2347 PERROR("Failed to seek metadata viewer stream file to `sent` position: pos = %" PRId64
,
2348 vstream
->metadata_sent
);
2349 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2355 reply
.len
= htobe64(len
);
2356 data
= (char *) zmalloc(len
);
2358 PERROR("viewer metadata zmalloc");
2362 fd
= fs_handle_get_fd(vstream
->stream_file
.handle
);
2364 ERR("Failed to restore viewer stream file system handle");
2367 read_len
= lttng_read(fd
, data
, len
);
2368 fs_handle_put_fd(vstream
->stream_file
.handle
);
2370 if (read_len
< len
) {
2372 PERROR("Failed to read metadata file");
2376 * A clear has been performed which prevents the relay
2377 * from sending `len` bytes of metadata.
2379 * It is important not to send any metadata if we
2380 * couldn't read all the available metadata in one shot:
2381 * sending partial metadata can cause the client to
2382 * attempt to parse an incomplete (incoherent) metadata
2383 * stream, which would result in an error.
2385 const off_t seek_ret
= fs_handle_seek(
2386 vstream
->stream_file
.handle
, -read_len
,
2389 DBG("Failed to read metadata: requested = %" PRIu64
", got = %zd",
2394 PERROR("Failed to restore metadata file position after partial read");
2400 vstream
->metadata_sent
+= read_len
;
2401 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
2406 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2409 health_code_update();
2411 pthread_mutex_unlock(&vstream
->stream
->lock
);
2413 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
2417 health_code_update();
2420 ret
= send_response(conn
->sock
, data
, len
);
2426 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
2427 (uint64_t) be64toh(request
.stream_id
));
2429 DBG("Metadata sent");
2435 viewer_stream_put(vstream
);
2441 * Create a viewer session.
2443 * Return 0 on success or else a negative value.
2446 int viewer_create_session(struct relay_connection
*conn
)
2449 struct lttng_viewer_create_session_response resp
;
2451 memset(&resp
, 0, sizeof(resp
));
2452 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
2453 conn
->viewer_session
= viewer_session_create();
2454 if (!conn
->viewer_session
) {
2455 ERR("Allocation viewer session");
2456 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
2461 health_code_update();
2462 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
2466 health_code_update();
2474 * Detach a viewer session.
2476 * Return 0 on success or else a negative value.
2479 int viewer_detach_session(struct relay_connection
*conn
)
2482 struct lttng_viewer_detach_session_response response
;
2483 struct lttng_viewer_detach_session_request request
;
2484 struct relay_session
*session
= NULL
;
2485 uint64_t viewer_session_to_close
;
2489 health_code_update();
2491 /* Receive the request from the connected client. */
2492 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2496 viewer_session_to_close
= be64toh(request
.session_id
);
2498 if (!conn
->viewer_session
) {
2499 DBG("Client trying to detach before creating a live viewer session");
2500 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2504 health_code_update();
2506 memset(&response
, 0, sizeof(response
));
2507 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2509 session
= session_get_by_id(be64toh(request
.session_id
));
2511 DBG("Relay session %" PRIu64
" not found",
2512 (uint64_t) be64toh(request
.session_id
));
2513 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2517 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2519 DBG("Not attached to this session");
2520 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2521 goto send_reply_put
;
2524 viewer_session_close_one_session(conn
->viewer_session
, session
);
2525 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2526 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2529 session_put(session
);
2532 health_code_update();
2533 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2537 health_code_update();
2545 * live_relay_unknown_command: send -1 if received unknown command
2548 void live_relay_unknown_command(struct relay_connection
*conn
)
2550 struct lttcomm_relayd_generic_reply reply
;
2552 memset(&reply
, 0, sizeof(reply
));
2553 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2554 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2558 * Process the commands received on the control socket
2561 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2562 struct relay_connection
*conn
)
2565 lttng_viewer_command cmd
=
2566 (lttng_viewer_command
) be32toh(recv_hdr
->cmd
);
2569 * Make sure we've done the version check before any command other then
2570 * a new client connection.
2572 if (cmd
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2573 ERR("Viewer on connection %d requested %s command before version check",
2574 conn
->sock
->fd
, lttng_viewer_command_str(cmd
));
2579 DBG("Processing %s viewer command from connection %d",
2580 lttng_viewer_command_str(cmd
), conn
->sock
->fd
);
2583 case LTTNG_VIEWER_CONNECT
:
2584 ret
= viewer_connect(conn
);
2586 case LTTNG_VIEWER_LIST_SESSIONS
:
2587 ret
= viewer_list_sessions(conn
);
2589 case LTTNG_VIEWER_ATTACH_SESSION
:
2590 ret
= viewer_attach_session(conn
);
2592 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2593 ret
= viewer_get_next_index(conn
);
2595 case LTTNG_VIEWER_GET_PACKET
:
2596 ret
= viewer_get_packet(conn
);
2598 case LTTNG_VIEWER_GET_METADATA
:
2599 ret
= viewer_get_metadata(conn
);
2601 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2602 ret
= viewer_get_new_streams(conn
);
2604 case LTTNG_VIEWER_CREATE_SESSION
:
2605 ret
= viewer_create_session(conn
);
2607 case LTTNG_VIEWER_DETACH_SESSION
:
2608 ret
= viewer_detach_session(conn
);
2611 ERR("Received unknown viewer command (%u)",
2612 be32toh(recv_hdr
->cmd
));
2613 live_relay_unknown_command(conn
);
2623 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2627 (void) lttng_poll_del(events
, pollfd
);
2629 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &pollfd
, 1,
2630 fd_tracker_util_close_fd
, NULL
);
2632 ERR("Closing pollfd %d", pollfd
);
2637 * This thread does the actual work
2640 void *thread_worker(void *data
)
2644 struct lttng_poll_event events
;
2645 struct lttng_ht
*viewer_connections_ht
;
2646 struct lttng_ht_iter iter
;
2647 struct lttng_viewer_cmd recv_hdr
;
2648 struct relay_connection
*destroy_conn
;
2650 DBG("[thread] Live viewer relay worker started");
2652 rcu_register_thread();
2654 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2656 if (testpoint(relayd_thread_live_worker
)) {
2657 goto error_testpoint
;
2660 /* table of connections indexed on socket */
2661 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2662 if (!viewer_connections_ht
) {
2663 goto viewer_connections_ht_error
;
2666 ret
= create_named_thread_poll_set(&events
, 2,
2667 "Live viewer worker thread epoll");
2669 goto error_poll_create
;
2672 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2681 health_code_update();
2683 /* Infinite blocking call, waiting for transmission */
2684 DBG3("Relayd live viewer worker thread polling...");
2685 health_poll_entry();
2686 ret
= lttng_poll_wait(&events
, -1);
2690 * Restart interrupted system call.
2692 if (errno
== EINTR
) {
2701 * Process control. The control connection is prioritised so we don't
2702 * starve it with high throughput tracing data on the data
2705 for (i
= 0; i
< nb_fd
; i
++) {
2706 /* Fetch once the poll data */
2707 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2708 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2710 health_code_update();
2712 /* Thread quit pipe has been closed. Killing thread. */
2713 ret
= check_thread_quit_pipe(pollfd
, revents
);
2719 /* Inspect the relay conn pipe for new connection. */
2720 if (pollfd
== live_conn_pipe
[0]) {
2721 if (revents
& LPOLLIN
) {
2722 struct relay_connection
*conn
;
2724 ret
= lttng_read(live_conn_pipe
[0],
2725 &conn
, sizeof(conn
));
2729 ret
= lttng_poll_add(&events
,
2731 LPOLLIN
| LPOLLRDHUP
);
2733 ERR("Failed to add new live connection file descriptor to poll set");
2736 connection_ht_add(viewer_connections_ht
, conn
);
2737 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2738 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2739 ERR("Relay live pipe error");
2742 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2746 /* Connection activity. */
2747 struct relay_connection
*conn
;
2749 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2754 if (revents
& LPOLLIN
) {
2755 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2756 sizeof(recv_hdr
), 0);
2758 /* Connection closed. */
2759 cleanup_connection_pollfd(&events
, pollfd
);
2760 /* Put "create" ownership reference. */
2761 connection_put(conn
);
2762 DBG("Viewer control conn closed with %d", pollfd
);
2764 ret
= process_control(&recv_hdr
, conn
);
2766 /* Clear the session on error. */
2767 cleanup_connection_pollfd(&events
, pollfd
);
2768 /* Put "create" ownership reference. */
2769 connection_put(conn
);
2770 DBG("Viewer connection closed with %d", pollfd
);
2773 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2774 cleanup_connection_pollfd(&events
, pollfd
);
2775 /* Put "create" ownership reference. */
2776 connection_put(conn
);
2778 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2779 connection_put(conn
);
2782 /* Put local "get_by_sock" reference. */
2783 connection_put(conn
);
2790 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
2792 /* Cleanup remaining connection object. */
2794 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2797 health_code_update();
2798 connection_put(destroy_conn
);
2802 lttng_ht_destroy(viewer_connections_ht
);
2803 viewer_connections_ht_error
:
2804 /* Close relay conn pipes */
2805 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2807 DBG("Viewer worker thread exited with error");
2809 DBG("Viewer worker thread cleanup complete");
2813 ERR("Health error occurred in %s", __func__
);
2815 health_unregister(health_relayd
);
2816 if (lttng_relay_stop_threads()) {
2817 ERR("Error stopping threads");
2819 rcu_unregister_thread();
2824 * Create the relay command pipe to wake thread_manage_apps.
2825 * Closed in cleanup().
2827 static int create_conn_pipe(void)
2829 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2830 "Live connection pipe", live_conn_pipe
);
2833 int relayd_live_join(void)
2835 int ret
, retval
= 0;
2838 ret
= pthread_join(live_listener_thread
, &status
);
2841 PERROR("pthread_join live listener");
2845 ret
= pthread_join(live_worker_thread
, &status
);
2848 PERROR("pthread_join live worker");
2852 ret
= pthread_join(live_dispatcher_thread
, &status
);
2855 PERROR("pthread_join live dispatcher");
2859 cleanup_relayd_live();
2867 int relayd_live_create(struct lttng_uri
*uri
)
2869 int ret
= 0, retval
= 0;
2875 goto exit_init_data
;
2879 /* Check if daemon is UID = 0 */
2880 is_root
= !getuid();
2883 if (live_uri
->port
< 1024) {
2884 ERR("Need to be root to use ports < 1024");
2886 goto exit_init_data
;
2890 /* Setup the thread apps communication pipe. */
2891 if (create_conn_pipe()) {
2893 goto exit_init_data
;
2896 /* Init relay command queue. */
2897 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2899 /* Set up max poll set size */
2900 if (lttng_poll_set_max_size()) {
2902 goto exit_init_data
;
2905 /* Setup the dispatcher thread */
2906 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2907 thread_dispatcher
, (void *) NULL
);
2910 PERROR("pthread_create viewer dispatcher");
2912 goto exit_dispatcher_thread
;
2915 /* Setup the worker thread */
2916 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2917 thread_worker
, NULL
);
2920 PERROR("pthread_create viewer worker");
2922 goto exit_worker_thread
;
2925 /* Setup the listener thread */
2926 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2927 thread_listener
, (void *) NULL
);
2930 PERROR("pthread_create viewer listener");
2932 goto exit_listener_thread
;
2936 * All OK, started all threads.
2941 * Join on the live_listener_thread should anything be added after
2942 * the live_listener thread's creation.
2945 exit_listener_thread
:
2947 ret
= pthread_join(live_worker_thread
, &status
);
2950 PERROR("pthread_join live worker");
2955 ret
= pthread_join(live_dispatcher_thread
, &status
);
2958 PERROR("pthread_join live dispatcher");
2961 exit_dispatcher_thread
:
2964 cleanup_relayd_live();