2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
29 #include <urcu/futex.h>
30 #include <urcu/rculist.h>
31 #include <urcu/uatomic.h>
33 #include <common/common.h>
34 #include <common/compat/endian.h>
35 #include <common/compat/poll.h>
36 #include <common/compat/socket.h>
37 #include <common/defaults.h>
38 #include <common/fd-tracker/utils.h>
39 #include <common/fs-handle.h>
40 #include <common/futex.h>
41 #include <common/index/index.h>
42 #include <common/sessiond-comm/inet.h>
43 #include <common/sessiond-comm/relayd.h>
44 #include <common/sessiond-comm/sessiond-comm.h>
45 #include <common/uri.h>
46 #include <common/utils.h>
47 #include <lttng/lttng.h>
50 #include "connection.h"
51 #include "ctf-trace.h"
52 #include "health-relayd.h"
54 #include "lttng-relayd.h"
57 #include "testpoint.h"
59 #include "viewer-session.h"
60 #include "viewer-stream.h"
62 #define SESSION_BUF_DEFAULT_COUNT 16
64 static struct lttng_uri
*live_uri
;
67 * This pipe is used to inform the worker thread that a command is queued and
68 * ready to be processed.
70 static int live_conn_pipe
[2] = { -1, -1 };
72 /* Shared between threads */
73 static int live_dispatch_thread_exit
;
75 static pthread_t live_listener_thread
;
76 static pthread_t live_dispatcher_thread
;
77 static pthread_t live_worker_thread
;
80 * Relay command queue.
82 * The live_thread_listener and live_thread_dispatcher communicate with this
85 static struct relay_conn_queue viewer_conn_queue
;
87 static uint64_t last_relay_viewer_session_id
;
88 static pthread_mutex_t last_relay_viewer_session_id_lock
=
89 PTHREAD_MUTEX_INITIALIZER
;
95 void cleanup_relayd_live(void)
103 * Receive a request buffer using a given socket, destination allocated buffer
106 * Return the size of the received message or else a negative value on error
107 * with errno being set by recvmsg() syscall.
110 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
114 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
115 if (ret
< 0 || ret
!= size
) {
117 /* Orderly shutdown. Not necessary to print an error. */
118 DBG("Socket %d did an orderly shutdown", sock
->fd
);
120 ERR("Relay failed to receive request.");
129 * Send a response buffer using a given socket, source allocated buffer of
132 * Return the size of the sent message or else a negative value on error with
133 * errno being set by sendmsg() syscall.
136 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
140 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
142 ERR("Relayd failed to send response.");
149 * Atomically check if new streams got added in one of the sessions attached
150 * and reset the flag to 0.
152 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
156 int check_new_streams(struct relay_connection
*conn
)
158 struct relay_session
*session
;
159 unsigned long current_val
;
162 if (!conn
->viewer_session
) {
166 cds_list_for_each_entry_rcu(session
,
167 &conn
->viewer_session
->session_list
,
168 viewer_session_node
) {
169 if (!session_get(session
)) {
172 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
174 session_put(session
);
185 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
186 * this function should ignore the sent flag or not.
188 * Return 0 on success or else a negative value.
191 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
192 uint64_t session_id
, unsigned int ignore_sent_flag
)
195 struct lttng_viewer_stream send_stream
;
196 struct lttng_ht_iter iter
;
197 struct relay_viewer_stream
*vstream
;
201 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
203 struct ctf_trace
*ctf_trace
;
205 health_code_update();
207 if (!viewer_stream_get(vstream
)) {
211 pthread_mutex_lock(&vstream
->stream
->lock
);
212 /* Ignore if not the same session. */
213 if (vstream
->stream
->trace
->session
->id
!= session_id
||
214 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
215 pthread_mutex_unlock(&vstream
->stream
->lock
);
216 viewer_stream_put(vstream
);
220 ctf_trace
= vstream
->stream
->trace
;
221 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
222 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
223 send_stream
.metadata_flag
= htobe32(
224 vstream
->stream
->is_metadata
);
225 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
226 sizeof(send_stream
.path_name
))) {
227 pthread_mutex_unlock(&vstream
->stream
->lock
);
228 viewer_stream_put(vstream
);
229 ret
= -1; /* Error. */
232 if (lttng_strncpy(send_stream
.channel_name
,
233 vstream
->channel_name
,
234 sizeof(send_stream
.channel_name
))) {
235 pthread_mutex_unlock(&vstream
->stream
->lock
);
236 viewer_stream_put(vstream
);
237 ret
= -1; /* Error. */
241 DBG("Sending stream %" PRIu64
" to viewer",
242 vstream
->stream
->stream_handle
);
243 vstream
->sent_flag
= 1;
244 pthread_mutex_unlock(&vstream
->stream
->lock
);
246 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
247 viewer_stream_put(vstream
);
261 * Create every viewer stream possible for the given session with the seek
262 * type. Three counters *can* be return which are in order the total amount of
263 * viewer stream of the session, the number of unsent stream and the number of
264 * stream created. Those counters can be NULL and thus will be ignored.
266 * session must be locked to ensure that we see either none or all initial
267 * streams for a session, but no intermediate state..
269 * Return 0 on success or else a negative value.
271 static int make_viewer_streams(struct relay_session
*session
,
272 struct lttng_trace_chunk
*viewer_trace_chunk
,
273 enum lttng_viewer_seek seek_t
,
276 uint32_t *nb_created
,
280 struct lttng_ht_iter iter
;
281 struct ctf_trace
*ctf_trace
;
284 ASSERT_LOCKED(session
->lock
);
286 if (!viewer_trace_chunk
) {
287 ERR("Internal error: viewer session associated with session \"%s\" has a NULL trace chunk",
288 session
->session_name
);
293 if (session
->connection_closed
) {
298 * Create viewer streams for relay streams that are ready to be
299 * used for a the given session id only.
302 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
304 bool trace_has_metadata_stream
= false;
305 struct relay_stream
*stream
;
307 health_code_update();
309 if (!ctf_trace_get(ctf_trace
)) {
314 * Iterate over all the streams of the trace to see if we have a
317 cds_list_for_each_entry_rcu(
318 stream
, &ctf_trace
->stream_list
, stream_node
)
320 if (stream
->is_metadata
) {
321 trace_has_metadata_stream
= true;
327 * If there is no metadata stream in this trace at the moment
328 * and we never sent one to the viewer, skip the trace. We
329 * accept that the viewer will not see this trace at all.
331 if (!trace_has_metadata_stream
&&
332 !ctf_trace
->metadata_stream_sent_to_viewer
) {
333 ctf_trace_put(ctf_trace
);
337 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
338 struct relay_viewer_stream
*vstream
;
340 if (!stream_get(stream
)) {
344 * stream published is protected by the session lock.
346 if (!stream
->published
) {
349 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
352 * Save that we sent the metadata stream to the
353 * viewer. So that we know what trace the viewer
356 if (stream
->is_metadata
) {
357 ctf_trace
->metadata_stream_sent_to_viewer
=
360 vstream
= viewer_stream_create(stream
,
361 viewer_trace_chunk
, seek_t
);
364 ctf_trace_put(ctf_trace
);
370 /* Update number of created stream counter. */
374 * Ensure a self-reference is preserved even
375 * after we have put our local reference.
377 if (!viewer_stream_get(vstream
)) {
378 ERR("Unable to get self-reference on viewer stream, logic error.");
382 if (!vstream
->sent_flag
&& nb_unsent
) {
383 /* Update number of unsent stream counter. */
387 /* Update number of total stream counter. */
389 if (stream
->is_metadata
) {
390 if (!stream
->closed
||
391 stream
->metadata_received
> vstream
->metadata_sent
) {
395 if (!stream
->closed
||
396 !(((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) >= 0)) {
402 /* Put local reference. */
403 viewer_stream_put(vstream
);
407 ctf_trace_put(ctf_trace
);
418 int relayd_live_stop(void)
420 /* Stop dispatch thread */
421 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
422 futex_nto1_wake(&viewer_conn_queue
.futex
);
427 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
430 int create_named_thread_poll_set(struct lttng_poll_event
*events
,
431 int size
, const char *name
)
435 if (events
== NULL
|| size
== 0) {
440 ret
= fd_tracker_util_poll_create(the_fd_tracker
,
441 name
, events
, 1, LTTNG_CLOEXEC
);
444 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
456 * Check if the thread quit pipe was triggered.
458 * Return 1 if it was triggered else 0;
461 int check_thread_quit_pipe(int fd
, uint32_t events
)
463 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
471 int create_sock(void *data
, int *out_fd
)
474 struct lttcomm_sock
*sock
= data
;
476 ret
= lttcomm_create_sock(sock
);
487 int close_sock(void *data
, int *in_fd
)
489 struct lttcomm_sock
*sock
= data
;
491 return sock
->ops
->close(sock
);
494 static int accept_sock(void *data
, int *out_fd
)
497 /* Socks is an array of in_sock, out_sock. */
498 struct lttcomm_sock
**socks
= data
;
499 struct lttcomm_sock
*in_sock
= socks
[0];
501 socks
[1] = in_sock
->ops
->accept(in_sock
);
506 *out_fd
= socks
[1]->fd
;
512 struct lttcomm_sock
*accept_live_sock(struct lttcomm_sock
*listening_sock
,
516 struct lttcomm_sock
*socks
[2] = { listening_sock
, NULL
};
517 struct lttcomm_sock
*new_sock
= NULL
;
519 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &out_fd
,
520 (const char **) &name
, 1, accept_sock
, &socks
);
525 DBG("%s accepted, socket %d", name
, new_sock
->fd
);
531 * Create and init socket from uri.
534 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
, const char *name
)
537 struct lttcomm_sock
*sock
= NULL
;
538 char uri_str
[LTTNG_PATH_MAX
];
539 char *formated_name
= NULL
;
541 sock
= lttcomm_alloc_sock_from_uri(uri
);
543 ERR("Allocating socket");
548 * Don't fail to create the socket if the name can't be built as it is
549 * only used for debugging purposes.
551 ret
= uri_to_str_url(uri
, uri_str
, sizeof(uri_str
));
552 uri_str
[sizeof(uri_str
) - 1] = '\0';
554 ret
= asprintf(&formated_name
, "%s socket @ %s", name
,
557 formated_name
= NULL
;
561 ret
= fd_tracker_open_unsuspendable_fd(the_fd_tracker
, &sock_fd
,
562 (const char **) (formated_name
? &formated_name
: NULL
),
563 1, create_sock
, sock
);
565 PERROR("Failed to create \"%s\" socket",
566 formated_name
?: "Unknown");
569 DBG("Listening on %s socket %d", name
, sock
->fd
);
571 ret
= sock
->ops
->bind(sock
);
573 PERROR("Failed to bind lttng-live socket");
577 ret
= sock
->ops
->listen(sock
, -1);
588 lttcomm_destroy_sock(sock
);
595 * This thread manages the listening for new connections on the network
598 void *thread_listener(void *data
)
600 int i
, ret
, pollfd
, err
= -1;
601 uint32_t revents
, nb_fd
;
602 struct lttng_poll_event events
;
603 struct lttcomm_sock
*live_control_sock
;
605 DBG("[thread] Relay live listener started");
607 rcu_register_thread();
608 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
610 health_code_update();
612 live_control_sock
= init_socket(live_uri
, "Live listener");
613 if (!live_control_sock
) {
614 goto error_sock_control
;
617 /* Pass 2 as size here for the thread quit pipe and control sockets. */
618 ret
= create_named_thread_poll_set(&events
, 2,
619 "Live listener thread epoll");
621 goto error_create_poll
;
624 /* Add the control socket */
625 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
630 lttng_relay_notify_ready();
632 if (testpoint(relayd_thread_live_listener
)) {
633 goto error_testpoint
;
637 health_code_update();
639 DBG("Listener accepting live viewers connections");
643 ret
= lttng_poll_wait(&events
, -1);
647 * Restart interrupted system call.
649 if (errno
== EINTR
) {
656 DBG("Relay new viewer connection received");
657 for (i
= 0; i
< nb_fd
; i
++) {
658 health_code_update();
660 /* Fetch once the poll data */
661 revents
= LTTNG_POLL_GETEV(&events
, i
);
662 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
664 /* Thread quit pipe has been closed. Killing thread. */
665 ret
= check_thread_quit_pipe(pollfd
, revents
);
671 if (revents
& LPOLLIN
) {
673 * A new connection is requested, therefore a
674 * viewer connection is allocated in this
675 * thread, enqueued to a global queue and
676 * dequeued (and freed) in the worker thread.
679 struct relay_connection
*new_conn
;
680 struct lttcomm_sock
*newsock
;
682 newsock
= accept_live_sock(live_control_sock
,
683 "Live socket to client");
685 PERROR("accepting control sock");
688 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
690 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
693 PERROR("setsockopt inet");
694 lttcomm_destroy_sock(newsock
);
697 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
699 lttcomm_destroy_sock(newsock
);
702 /* Ownership assumed by the connection. */
705 /* Enqueue request for the dispatcher thread. */
706 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
710 * Wake the dispatch queue futex.
711 * Implicit memory barrier with the
712 * exchange in cds_wfcq_enqueue.
714 futex_nto1_wake(&viewer_conn_queue
.futex
);
715 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
716 ERR("socket poll error");
719 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
729 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
731 if (live_control_sock
->fd
>= 0) {
732 int sock_fd
= live_control_sock
->fd
;
734 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
,
735 &sock_fd
, 1, close_sock
,
740 live_control_sock
->fd
= -1;
742 lttcomm_destroy_sock(live_control_sock
);
746 DBG("Live viewer listener thread exited with error");
748 health_unregister(health_relayd
);
749 rcu_unregister_thread();
750 DBG("Live viewer listener thread cleanup complete");
751 if (lttng_relay_stop_threads()) {
752 ERR("Error stopping threads");
758 * This thread manages the dispatching of the requests to worker threads
761 void *thread_dispatcher(void *data
)
765 struct cds_wfcq_node
*node
;
766 struct relay_connection
*conn
= NULL
;
768 DBG("[thread] Live viewer relay dispatcher started");
770 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
772 if (testpoint(relayd_thread_live_dispatcher
)) {
773 goto error_testpoint
;
776 health_code_update();
779 health_code_update();
781 /* Atomically prepare the queue futex */
782 futex_nto1_prepare(&viewer_conn_queue
.futex
);
784 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
789 health_code_update();
791 /* Dequeue commands */
792 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
793 &viewer_conn_queue
.tail
);
795 DBG("Woken up but nothing in the live-viewer "
796 "relay command queue");
797 /* Continue thread execution */
800 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
801 DBG("Dispatching viewer request waiting on sock %d",
805 * Inform worker thread of the new request. This
806 * call is blocking so we can be assured that
807 * the data will be read at some point in time
808 * or wait to the end of the world :)
810 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
812 PERROR("write conn pipe");
813 connection_put(conn
);
816 } while (node
!= NULL
);
818 /* Futex wait on queue. Blocking call on futex() */
820 futex_nto1_wait(&viewer_conn_queue
.futex
);
824 /* Normal exit, no error */
831 ERR("Health error occurred in %s", __func__
);
833 health_unregister(health_relayd
);
834 DBG("Live viewer dispatch thread dying");
835 if (lttng_relay_stop_threads()) {
836 ERR("Error stopping threads");
842 * Establish connection with the viewer and check the versions.
844 * Return 0 on success or else negative value.
847 int viewer_connect(struct relay_connection
*conn
)
850 struct lttng_viewer_connect reply
, msg
;
852 conn
->version_check_done
= 1;
854 health_code_update();
856 DBG("Viewer is establishing a connection to the relayd.");
858 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
863 health_code_update();
865 memset(&reply
, 0, sizeof(reply
));
866 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
867 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
869 /* Major versions must be the same */
870 if (reply
.major
!= be32toh(msg
.major
)) {
871 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
872 reply
.major
, be32toh(msg
.major
));
877 conn
->major
= reply
.major
;
878 /* We adapt to the lowest compatible version */
879 if (reply
.minor
<= be32toh(msg
.minor
)) {
880 conn
->minor
= reply
.minor
;
882 conn
->minor
= be32toh(msg
.minor
);
885 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
886 conn
->type
= RELAY_VIEWER_COMMAND
;
887 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
888 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
890 ERR("Unknown connection type : %u", be32toh(msg
.type
));
895 reply
.major
= htobe32(reply
.major
);
896 reply
.minor
= htobe32(reply
.minor
);
897 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
899 * Increment outside of htobe64 macro, because the argument can
900 * be used more than once within the macro, and thus the
901 * operation may be undefined.
903 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
904 last_relay_viewer_session_id
++;
905 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
906 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
909 health_code_update();
911 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
916 health_code_update();
918 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
926 * Send the viewer the list of current sessions.
927 * We need to create a copy of the hash table content because otherwise
928 * we cannot assume the number of entries stays the same between getting
929 * the number of HT elements and iteration over the HT.
931 * Return 0 on success or else a negative value.
934 int viewer_list_sessions(struct relay_connection
*conn
)
937 struct lttng_viewer_list_sessions session_list
;
938 struct lttng_ht_iter iter
;
939 struct relay_session
*session
;
940 struct lttng_viewer_session
*send_session_buf
= NULL
;
941 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
944 DBG("List sessions received");
946 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
947 if (!send_session_buf
) {
952 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
954 struct lttng_viewer_session
*send_session
;
956 health_code_update();
958 pthread_mutex_lock(&session
->lock
);
959 if (session
->connection_closed
) {
960 /* Skip closed session */
963 if (!session
->current_trace_chunk
) {
965 * Skip un-attachable session. It is either
966 * being destroyed or has not had a trace
967 * chunk created against it yet.
972 if (count
>= buf_count
) {
973 struct lttng_viewer_session
*newbuf
;
974 uint32_t new_buf_count
= buf_count
<< 1;
976 newbuf
= realloc(send_session_buf
,
977 new_buf_count
* sizeof(*send_session_buf
));
982 send_session_buf
= newbuf
;
983 buf_count
= new_buf_count
;
985 send_session
= &send_session_buf
[count
];
986 if (lttng_strncpy(send_session
->session_name
,
987 session
->session_name
,
988 sizeof(send_session
->session_name
))) {
992 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
993 sizeof(send_session
->hostname
))) {
997 send_session
->id
= htobe64(session
->id
);
998 send_session
->live_timer
= htobe32(session
->live_timer
);
999 if (session
->viewer_attached
) {
1000 send_session
->clients
= htobe32(1);
1002 send_session
->clients
= htobe32(0);
1004 send_session
->streams
= htobe32(session
->stream_count
);
1007 pthread_mutex_unlock(&session
->lock
);
1010 pthread_mutex_unlock(&session
->lock
);
1018 session_list
.sessions_count
= htobe32(count
);
1020 health_code_update();
1022 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
1027 health_code_update();
1029 ret
= send_response(conn
->sock
, send_session_buf
,
1030 count
* sizeof(*send_session_buf
));
1034 health_code_update();
1038 free(send_session_buf
);
1043 * Send the viewer the list of current streams.
1046 int viewer_get_new_streams(struct relay_connection
*conn
)
1048 int ret
, send_streams
= 0;
1049 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
1050 struct lttng_viewer_new_streams_request request
;
1051 struct lttng_viewer_new_streams_response response
;
1052 struct relay_session
*session
= NULL
;
1053 uint64_t session_id
;
1054 bool closed
= false;
1058 DBG("Get new streams received");
1060 health_code_update();
1062 /* Receive the request from the connected client. */
1063 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1067 session_id
= be64toh(request
.session_id
);
1069 health_code_update();
1071 memset(&response
, 0, sizeof(response
));
1073 session
= session_get_by_id(session_id
);
1075 DBG("Relay session %" PRIu64
" not found", session_id
);
1076 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1080 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1081 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1085 pthread_mutex_lock(&session
->lock
);
1086 ret
= make_viewer_streams(session
,
1087 conn
->viewer_session
->current_trace_chunk
,
1088 LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
1089 &nb_created
, &closed
);
1091 goto error_unlock_session
;
1094 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1096 /* Only send back the newly created streams with the unsent ones. */
1097 nb_streams
= nb_created
+ nb_unsent
;
1098 response
.streams_count
= htobe32(nb_streams
);
1101 * If the session is closed, HUP when there are no more streams
1104 if (closed
&& nb_total
== 0) {
1106 response
.streams_count
= 0;
1107 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1108 goto send_reply_unlock
;
1111 pthread_mutex_unlock(&session
->lock
);
1114 health_code_update();
1115 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1117 goto end_put_session
;
1119 health_code_update();
1122 * Unknown or empty session, just return gracefully, the viewer
1123 * knows what is happening.
1125 if (!send_streams
|| !nb_streams
) {
1127 goto end_put_session
;
1131 * Send stream and *DON'T* ignore the sent flag so every viewer
1132 * streams that were not sent from that point will be sent to
1135 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1137 goto end_put_session
;
1142 session_put(session
);
1146 error_unlock_session
:
1147 pthread_mutex_unlock(&session
->lock
);
1148 session_put(session
);
1153 * Send the viewer the list of current sessions.
1156 int viewer_attach_session(struct relay_connection
*conn
)
1158 int send_streams
= 0;
1160 uint32_t nb_streams
= 0;
1161 enum lttng_viewer_seek seek_type
;
1162 struct lttng_viewer_attach_session_request request
;
1163 struct lttng_viewer_attach_session_response response
;
1164 struct relay_session
*session
= NULL
;
1165 enum lttng_viewer_attach_return_code viewer_attach_status
;
1166 bool closed
= false;
1167 uint64_t session_id
;
1171 health_code_update();
1173 /* Receive the request from the connected client. */
1174 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1179 session_id
= be64toh(request
.session_id
);
1180 health_code_update();
1182 memset(&response
, 0, sizeof(response
));
1184 if (!conn
->viewer_session
) {
1185 DBG("Client trying to attach before creating a live viewer session");
1186 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1190 session
= session_get_by_id(session_id
);
1192 DBG("Relay session %" PRIu64
" not found", session_id
);
1193 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1196 DBG("Attach session ID %" PRIu64
" received", session_id
);
1198 pthread_mutex_lock(&session
->lock
);
1199 if (!session
->current_trace_chunk
) {
1201 * Session is either being destroyed or it never had a trace
1202 * chunk created against it.
1204 DBG("Session requested by live client has no current trace chunk, returning unknown session");
1205 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1208 if (session
->live_timer
== 0) {
1209 DBG("Not live session");
1210 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1215 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1217 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1218 response
.status
= htobe32(viewer_attach_status
);
1222 switch (be32toh(request
.seek
)) {
1223 case LTTNG_VIEWER_SEEK_BEGINNING
:
1224 case LTTNG_VIEWER_SEEK_LAST
:
1225 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1226 seek_type
= be32toh(request
.seek
);
1229 ERR("Wrong seek parameter");
1230 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1235 ret
= make_viewer_streams(session
,
1236 conn
->viewer_session
->current_trace_chunk
, seek_type
,
1237 &nb_streams
, NULL
, NULL
, &closed
);
1239 goto end_put_session
;
1241 pthread_mutex_unlock(&session
->lock
);
1242 session_put(session
);
1245 response
.streams_count
= htobe32(nb_streams
);
1247 * If the session is closed when the viewer is attaching, it
1248 * means some of the streams may have been concurrently removed,
1249 * so we don't allow the viewer to attach, even if there are
1250 * streams available.
1254 response
.streams_count
= 0;
1255 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1260 health_code_update();
1261 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1263 goto end_put_session
;
1265 health_code_update();
1268 * Unknown or empty session, just return gracefully, the viewer
1269 * knows what is happening.
1271 if (!send_streams
|| !nb_streams
) {
1273 goto end_put_session
;
1276 /* Send stream and ignore the sent flag. */
1277 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1279 goto end_put_session
;
1284 pthread_mutex_unlock(&session
->lock
);
1285 session_put(session
);
1292 * Open the index file if needed for the given vstream.
1294 * If an index file is successfully opened, the vstream will set it as its
1295 * current index file.
1297 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1299 * Called with rstream lock held.
1301 static int try_open_index(struct relay_viewer_stream
*vstream
,
1302 struct relay_stream
*rstream
)
1305 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1306 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1307 enum lttng_trace_chunk_status chunk_status
;
1309 if (vstream
->index_file
) {
1314 * First time, we open the index file and at least one index is ready.
1316 if (rstream
->index_received_seqcount
== 0) {
1320 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1321 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1322 rstream
->channel_name
, rstream
->tracefile_size
,
1323 vstream
->current_tracefile_id
,
1324 lttng_to_index_major(connection_major
, connection_minor
),
1325 lttng_to_index_minor(connection_major
, connection_minor
),
1326 true, &vstream
->index_file
);
1327 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1328 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1340 * Check the status of the index for the given stream. This function
1341 * updates the index structure if needed and can put (close) the vstream
1342 * in the HUP situation.
1344 * Return 0 means that we can proceed with the index. A value of 1 means
1345 * that the index has been updated and is ready to be sent to the
1346 * client. A negative value indicates an error that can't be handled.
1348 * Called with rstream lock held.
1350 static int check_index_status(struct relay_viewer_stream
*vstream
,
1351 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1352 struct lttng_viewer_index
*index
)
1356 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1357 "index_sent_seqcount %" PRIu64
" "
1358 "for stream %" PRIu64
,
1359 rstream
->index_received_seqcount
,
1360 vstream
->index_sent_seqcount
,
1361 vstream
->stream
->stream_handle
);
1362 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1363 && rstream
->index_received_seqcount
1364 == vstream
->index_sent_seqcount
) {
1366 * Last index sent and session connection or relay
1367 * stream are closed.
1369 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1371 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1372 (rstream
->index_received_seqcount
== 0 ||
1373 (vstream
->index_sent_seqcount
!= 0 &&
1374 rstream
->index_received_seqcount
1375 <= vstream
->index_sent_seqcount
))) {
1377 * We've received a synchronization beacon and the last index
1378 * available has been sent, the index for now is inactive.
1380 * In this case, we have received a beacon which allows us to
1381 * inform the client of a time interval during which we can
1382 * guarantee that there are no events to read (and never will
1385 * The sent seqcount can grow higher than receive seqcount on
1386 * clear because the rotation performed by clear will push
1387 * the index_sent_seqcount ahead (see
1388 * viewer_stream_sync_tracefile_array_tail) and skip over
1389 * packet sequence numbers.
1391 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1392 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1393 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1394 DBG("Check index status: inactive with beacon, for stream %" PRIu64
,
1395 vstream
->stream
->stream_handle
);
1397 } else if (rstream
->index_received_seqcount
== 0 ||
1398 (vstream
->index_sent_seqcount
!= 0 &&
1399 rstream
->index_received_seqcount
1400 <= vstream
->index_sent_seqcount
)) {
1402 * This checks whether received <= sent seqcount. In
1403 * this case, we have not received a beacon. Therefore,
1404 * we can only ask the client to retry later.
1406 * The sent seqcount can grow higher than receive seqcount on
1407 * clear because the rotation performed by clear will push
1408 * the index_sent_seqcount ahead (see
1409 * viewer_stream_sync_tracefile_array_tail) and skip over
1410 * packet sequence numbers.
1412 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1413 DBG("Check index status: retry for stream %" PRIu64
,
1414 vstream
->stream
->stream_handle
);
1416 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1417 vstream
->current_tracefile_id
,
1418 vstream
->index_sent_seqcount
)) {
1420 * The next index we want to send cannot be read either
1421 * because we need to perform a rotation, or due to
1422 * the producer having overwritten its trace file.
1424 DBG("Viewer stream %" PRIu64
" rotation",
1425 vstream
->stream
->stream_handle
);
1426 ret
= viewer_stream_rotate(vstream
);
1428 /* EOF across entire stream. */
1429 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1433 * If we have been pushed due to overwrite, it
1434 * necessarily means there is data that can be read in
1435 * the stream. If we rotated because we reached the end
1436 * of a tracefile, it means the following tracefile
1437 * needs to contain at least one index, else we would
1438 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1439 * viewer. The updated index_sent_seqcount needs to
1440 * point to a readable index entry now.
1442 * In the case where we "rotate" on a single file, we
1443 * can end up in a case where the requested index is
1444 * still unavailable.
1446 if (rstream
->tracefile_count
== 1 &&
1447 !tracefile_array_seq_in_file(
1449 vstream
->current_tracefile_id
,
1450 vstream
->index_sent_seqcount
)) {
1451 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1452 DBG("Check index status: retry: "
1453 "tracefile array sequence number %" PRIu64
1454 " not in file for stream %" PRIu64
,
1455 vstream
->index_sent_seqcount
,
1456 vstream
->stream
->stream_handle
);
1459 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1460 vstream
->current_tracefile_id
,
1461 vstream
->index_sent_seqcount
));
1463 /* ret == 0 means successful so we continue. */
1468 viewer_stream_put(vstream
);
1474 * Send the next index for a stream.
1476 * Return 0 on success or else a negative value.
1479 int viewer_get_next_index(struct relay_connection
*conn
)
1482 struct lttng_viewer_get_next_index request_index
;
1483 struct lttng_viewer_index viewer_index
;
1484 struct ctf_packet_index packet_index
;
1485 struct relay_viewer_stream
*vstream
= NULL
;
1486 struct relay_stream
*rstream
= NULL
;
1487 struct ctf_trace
*ctf_trace
= NULL
;
1488 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1492 DBG("Viewer get next index");
1494 memset(&viewer_index
, 0, sizeof(viewer_index
));
1495 health_code_update();
1497 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1501 health_code_update();
1503 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1505 DBG("Client requested index of unknown stream id %" PRIu64
,
1506 (uint64_t) be64toh(request_index
.stream_id
));
1507 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1511 /* Use back. ref. Protected by refcounts. */
1512 rstream
= vstream
->stream
;
1513 ctf_trace
= rstream
->trace
;
1515 /* metadata_viewer_stream may be NULL. */
1516 metadata_viewer_stream
=
1517 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1519 pthread_mutex_lock(&rstream
->lock
);
1522 * The viewer should not ask for index on metadata stream.
1524 if (rstream
->is_metadata
) {
1525 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1529 if (rstream
->ongoing_rotation
.is_set
) {
1530 /* Rotation is ongoing, try again later. */
1531 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1535 if (rstream
->trace
->session
->ongoing_rotation
) {
1536 /* Rotation is ongoing, try again later. */
1537 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1541 if (rstream
->trace_chunk
) {
1542 uint64_t rchunk_id
, vchunk_id
;
1545 * If the relay stream is not yet closed, ensure the viewer
1546 * chunk matches the relay chunk after clear.
1548 if (lttng_trace_chunk_get_id(rstream
->trace_chunk
,
1549 &rchunk_id
) != LTTNG_TRACE_CHUNK_STATUS_OK
) {
1550 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1553 if (lttng_trace_chunk_get_id(
1554 conn
->viewer_session
->current_trace_chunk
,
1555 &vchunk_id
) != LTTNG_TRACE_CHUNK_STATUS_OK
) {
1556 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1560 if (rchunk_id
!= vchunk_id
) {
1561 DBG("Relay and viewer chunk ids differ: "
1562 "rchunk_id %" PRIu64
" vchunk_id %" PRIu64
,
1563 rchunk_id
, vchunk_id
);
1565 lttng_trace_chunk_put(
1566 conn
->viewer_session
->current_trace_chunk
);
1567 conn
->viewer_session
->current_trace_chunk
= NULL
;
1568 ret
= viewer_session_set_trace_chunk_copy(
1569 conn
->viewer_session
,
1570 rstream
->trace_chunk
);
1572 viewer_index
.status
=
1573 htobe32(LTTNG_VIEWER_INDEX_ERR
);
1578 if (conn
->viewer_session
->current_trace_chunk
!=
1579 vstream
->stream_file
.trace_chunk
) {
1580 bool acquired_reference
;
1582 DBG("Viewer session and viewer stream chunk differ: "
1583 "vsession chunk %p vstream chunk %p",
1584 conn
->viewer_session
->current_trace_chunk
,
1585 vstream
->stream_file
.trace_chunk
);
1586 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1587 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
1588 assert(acquired_reference
);
1589 vstream
->stream_file
.trace_chunk
=
1590 conn
->viewer_session
->current_trace_chunk
;
1591 viewer_stream_sync_tracefile_array_tail(vstream
);
1592 viewer_stream_close_files(vstream
);
1595 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1598 } else if (ret
== 1) {
1600 * We have no index to send and check_index_status has populated
1601 * viewer_index's status.
1605 /* At this point, ret is 0 thus we will be able to read the index. */
1608 /* Try to open an index if one is needed for that stream. */
1609 ret
= try_open_index(vstream
, rstream
);
1610 if (ret
== -ENOENT
) {
1611 if (rstream
->closed
) {
1612 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1615 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1620 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1625 * vstream->stream_fd may be NULL if it has been closed by
1626 * tracefile rotation, or if we are at the beginning of the
1627 * stream. We open the data stream file here to protect against
1628 * overwrite caused by tracefile rotation (in association with
1629 * unlink performed before overwrite).
1631 if (!vstream
->stream_file
.handle
) {
1632 char file_path
[LTTNG_PATH_MAX
];
1633 enum lttng_trace_chunk_status status
;
1634 struct fs_handle
*fs_handle
;
1636 ret
= utils_stream_file_path(rstream
->path_name
,
1637 rstream
->channel_name
, rstream
->tracefile_size
,
1638 vstream
->current_tracefile_id
, NULL
, file_path
,
1645 * It is possible the the file we are trying to open is
1646 * missing if the stream has been closed (application exits with
1647 * per-pid buffers) and a clear command has been performed.
1649 status
= lttng_trace_chunk_open_fs_handle(
1650 vstream
->stream_file
.trace_chunk
,
1651 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1652 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1653 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1655 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1658 PERROR("Failed to open trace file for viewer stream");
1661 vstream
->stream_file
.handle
= fs_handle
;
1664 ret
= check_new_streams(conn
);
1666 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1668 } else if (ret
== 1) {
1669 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1672 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1674 ERR("Relay error reading index file");
1675 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1678 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1679 vstream
->index_sent_seqcount
++;
1683 * Indexes are stored in big endian, no need to switch before sending.
1685 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1686 rstream
->stream_handle
,
1687 (uint64_t) be64toh(packet_index
.offset
));
1688 viewer_index
.offset
= packet_index
.offset
;
1689 viewer_index
.packet_size
= packet_index
.packet_size
;
1690 viewer_index
.content_size
= packet_index
.content_size
;
1691 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1692 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1693 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1694 viewer_index
.stream_id
= packet_index
.stream_id
;
1698 pthread_mutex_unlock(&rstream
->lock
);
1701 if (metadata_viewer_stream
) {
1702 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1703 DBG("get next index metadata check: recv %" PRIu64
1705 metadata_viewer_stream
->stream
->metadata_received
,
1706 metadata_viewer_stream
->metadata_sent
);
1707 if (!metadata_viewer_stream
->stream
->metadata_received
||
1708 metadata_viewer_stream
->stream
->metadata_received
>
1709 metadata_viewer_stream
->metadata_sent
) {
1710 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1712 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1715 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1716 health_code_update();
1718 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1722 health_code_update();
1725 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1726 vstream
->index_sent_seqcount
,
1727 vstream
->stream
->stream_handle
);
1730 if (metadata_viewer_stream
) {
1731 viewer_stream_put(metadata_viewer_stream
);
1734 viewer_stream_put(vstream
);
1739 pthread_mutex_unlock(&rstream
->lock
);
1740 if (metadata_viewer_stream
) {
1741 viewer_stream_put(metadata_viewer_stream
);
1743 viewer_stream_put(vstream
);
1748 * Send the next index for a stream
1750 * Return 0 on success or else a negative value.
1753 int viewer_get_packet(struct relay_connection
*conn
)
1758 struct lttng_viewer_get_packet get_packet_info
;
1759 struct lttng_viewer_trace_packet reply_header
;
1760 struct relay_viewer_stream
*vstream
= NULL
;
1761 uint32_t reply_size
= sizeof(reply_header
);
1762 uint32_t packet_data_len
= 0;
1766 DBG2("Relay get data packet");
1768 health_code_update();
1770 ret
= recv_request(conn
->sock
, &get_packet_info
,
1771 sizeof(get_packet_info
));
1775 health_code_update();
1777 /* From this point on, the error label can be reached. */
1778 memset(&reply_header
, 0, sizeof(reply_header
));
1779 stream_id
= (uint64_t) be64toh(get_packet_info
.stream_id
);
1781 vstream
= viewer_stream_get_by_id(stream_id
);
1783 DBG("Client requested packet of unknown stream id %" PRIu64
,
1785 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1786 goto send_reply_nolock
;
1788 packet_data_len
= be32toh(get_packet_info
.len
);
1789 reply_size
+= packet_data_len
;
1792 reply
= zmalloc(reply_size
);
1794 PERROR("packet reply zmalloc");
1795 reply_size
= sizeof(reply_header
);
1799 pthread_mutex_lock(&vstream
->stream
->lock
);
1800 lseek_ret
= fs_handle_seek(vstream
->stream_file
.handle
,
1801 be64toh(get_packet_info
.offset
), SEEK_SET
);
1802 if (lseek_ret
< 0) {
1803 PERROR("Failed to seek file system handle of viewer stream %" PRIu64
1804 " to offset %" PRIu64
,
1806 (uint64_t) be64toh(get_packet_info
.offset
));
1809 read_len
= fs_handle_read(vstream
->stream_file
.handle
,
1810 reply
+ sizeof(reply_header
), packet_data_len
);
1811 if (read_len
< packet_data_len
) {
1812 PERROR("Failed to read from file system handle of viewer stream id %" PRIu64
1813 ", offset: %" PRIu64
,
1815 (uint64_t) be64toh(get_packet_info
.offset
));
1818 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1819 reply_header
.len
= htobe32(packet_data_len
);
1823 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1827 pthread_mutex_unlock(&vstream
->stream
->lock
);
1831 health_code_update();
1834 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1835 ret
= send_response(conn
->sock
, reply
, reply_size
);
1837 /* No reply to send. */
1838 ret
= send_response(conn
->sock
, &reply_header
,
1842 health_code_update();
1844 PERROR("sendmsg of packet data failed");
1848 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
, stream_id
);
1854 viewer_stream_put(vstream
);
1860 * Send the session's metadata
1862 * Return 0 on success else a negative value.
1865 int viewer_get_metadata(struct relay_connection
*conn
)
1872 struct lttng_viewer_get_metadata request
;
1873 struct lttng_viewer_metadata_packet reply
;
1874 struct relay_viewer_stream
*vstream
= NULL
;
1878 DBG("Relay get metadata");
1880 health_code_update();
1882 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1886 health_code_update();
1888 memset(&reply
, 0, sizeof(reply
));
1890 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1893 * The metadata stream can be closed by a CLOSE command
1894 * just before we attach. It can also be closed by
1895 * per-pid tracing during tracing. Therefore, it is
1896 * possible that we cannot find this viewer stream.
1897 * Reply back to the client with an error if we cannot
1900 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1901 (uint64_t) be64toh(request
.stream_id
));
1902 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1905 pthread_mutex_lock(&vstream
->stream
->lock
);
1906 if (!vstream
->stream
->is_metadata
) {
1907 ERR("Invalid metadata stream");
1911 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
1913 * The live viewers expect to receive a NO_NEW_METADATA
1914 * status before a stream disappears, otherwise they abort the
1915 * entire live connection when receiving an error status.
1917 * Clear feature resets the metadata_sent to 0 until the
1918 * same metadata is received again.
1920 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1922 * The live viewer considers a closed 0 byte metadata stream as
1925 if (vstream
->metadata_sent
> 0) {
1926 vstream
->stream
->no_new_metadata_notified
= true;
1927 if (vstream
->stream
->closed
) {
1928 /* Release ownership for the viewer metadata stream. */
1929 viewer_stream_put(vstream
);
1935 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1937 /* first time, we open the metadata file */
1938 if (!vstream
->stream_file
.handle
) {
1939 struct fs_handle
*fs_handle
;
1940 char file_path
[LTTNG_PATH_MAX
];
1941 enum lttng_trace_chunk_status status
;
1942 struct relay_stream
*rstream
= vstream
->stream
;
1944 ret
= utils_stream_file_path(rstream
->path_name
,
1945 rstream
->channel_name
, rstream
->tracefile_size
,
1946 vstream
->current_tracefile_id
, NULL
, file_path
,
1953 * It is possible the the metadata file we are trying to open is
1954 * missing if the stream has been closed (application exits with
1955 * per-pid buffers) and a clear command has been performed.
1957 status
= lttng_trace_chunk_open_fs_handle(
1958 vstream
->stream_file
.trace_chunk
,
1959 file_path
, O_RDONLY
, 0, &fs_handle
, true);
1960 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1961 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1962 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1964 if (vstream
->stream
->closed
) {
1965 viewer_stream_put(vstream
);
1969 PERROR("Failed to open metadata file for viewer stream");
1972 vstream
->stream_file
.handle
= fs_handle
;
1975 reply
.len
= htobe64(len
);
1976 data
= zmalloc(len
);
1978 PERROR("viewer metadata zmalloc");
1982 fd
= fs_handle_get_fd(vstream
->stream_file
.handle
);
1984 ERR("Failed to restore viewer stream file system handle");
1987 read_len
= lttng_read(fd
, data
, len
);
1988 fs_handle_put_fd(vstream
->stream_file
.handle
);
1990 if (read_len
< len
) {
1991 PERROR("Relay reading metadata file");
1994 vstream
->metadata_sent
+= read_len
;
1995 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
2000 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
2003 health_code_update();
2005 pthread_mutex_unlock(&vstream
->stream
->lock
);
2007 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
2011 health_code_update();
2014 ret
= send_response(conn
->sock
, data
, len
);
2020 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
2021 (uint64_t) be64toh(request
.stream_id
));
2023 DBG("Metadata sent");
2029 viewer_stream_put(vstream
);
2035 * Create a viewer session.
2037 * Return 0 on success or else a negative value.
2040 int viewer_create_session(struct relay_connection
*conn
)
2043 struct lttng_viewer_create_session_response resp
;
2045 DBG("Viewer create session received");
2047 memset(&resp
, 0, sizeof(resp
));
2048 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
2049 conn
->viewer_session
= viewer_session_create();
2050 if (!conn
->viewer_session
) {
2051 ERR("Allocation viewer session");
2052 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
2057 health_code_update();
2058 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
2062 health_code_update();
2070 * Detach a viewer session.
2072 * Return 0 on success or else a negative value.
2075 int viewer_detach_session(struct relay_connection
*conn
)
2078 struct lttng_viewer_detach_session_response response
;
2079 struct lttng_viewer_detach_session_request request
;
2080 struct relay_session
*session
= NULL
;
2081 uint64_t viewer_session_to_close
;
2083 DBG("Viewer detach session received");
2087 health_code_update();
2089 /* Receive the request from the connected client. */
2090 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2094 viewer_session_to_close
= be64toh(request
.session_id
);
2096 if (!conn
->viewer_session
) {
2097 DBG("Client trying to detach before creating a live viewer session");
2098 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2102 health_code_update();
2104 memset(&response
, 0, sizeof(response
));
2105 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2107 session
= session_get_by_id(be64toh(request
.session_id
));
2109 DBG("Relay session %" PRIu64
" not found",
2110 (uint64_t) be64toh(request
.session_id
));
2111 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2115 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2117 DBG("Not attached to this session");
2118 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2119 goto send_reply_put
;
2122 viewer_session_close_one_session(conn
->viewer_session
, session
);
2123 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2124 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2127 session_put(session
);
2130 health_code_update();
2131 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2135 health_code_update();
2143 * live_relay_unknown_command: send -1 if received unknown command
2146 void live_relay_unknown_command(struct relay_connection
*conn
)
2148 struct lttcomm_relayd_generic_reply reply
;
2150 memset(&reply
, 0, sizeof(reply
));
2151 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2152 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2156 * Process the commands received on the control socket
2159 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2160 struct relay_connection
*conn
)
2165 msg_value
= be32toh(recv_hdr
->cmd
);
2168 * Make sure we've done the version check before any command other then a
2169 * new client connection.
2171 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2172 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
2177 switch (msg_value
) {
2178 case LTTNG_VIEWER_CONNECT
:
2179 ret
= viewer_connect(conn
);
2181 case LTTNG_VIEWER_LIST_SESSIONS
:
2182 ret
= viewer_list_sessions(conn
);
2184 case LTTNG_VIEWER_ATTACH_SESSION
:
2185 ret
= viewer_attach_session(conn
);
2187 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2188 ret
= viewer_get_next_index(conn
);
2190 case LTTNG_VIEWER_GET_PACKET
:
2191 ret
= viewer_get_packet(conn
);
2193 case LTTNG_VIEWER_GET_METADATA
:
2194 ret
= viewer_get_metadata(conn
);
2196 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2197 ret
= viewer_get_new_streams(conn
);
2199 case LTTNG_VIEWER_CREATE_SESSION
:
2200 ret
= viewer_create_session(conn
);
2202 case LTTNG_VIEWER_DETACH_SESSION
:
2203 ret
= viewer_detach_session(conn
);
2206 ERR("Received unknown viewer command (%u)",
2207 be32toh(recv_hdr
->cmd
));
2208 live_relay_unknown_command(conn
);
2218 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2222 (void) lttng_poll_del(events
, pollfd
);
2224 ret
= fd_tracker_close_unsuspendable_fd(the_fd_tracker
, &pollfd
, 1,
2225 fd_tracker_util_close_fd
, NULL
);
2227 ERR("Closing pollfd %d", pollfd
);
2232 * This thread does the actual work
2235 void *thread_worker(void *data
)
2239 struct lttng_poll_event events
;
2240 struct lttng_ht
*viewer_connections_ht
;
2241 struct lttng_ht_iter iter
;
2242 struct lttng_viewer_cmd recv_hdr
;
2243 struct relay_connection
*destroy_conn
;
2245 DBG("[thread] Live viewer relay worker started");
2247 rcu_register_thread();
2249 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2251 if (testpoint(relayd_thread_live_worker
)) {
2252 goto error_testpoint
;
2255 /* table of connections indexed on socket */
2256 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2257 if (!viewer_connections_ht
) {
2258 goto viewer_connections_ht_error
;
2261 ret
= create_named_thread_poll_set(&events
, 2,
2262 "Live viewer worker thread epoll");
2264 goto error_poll_create
;
2267 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2276 health_code_update();
2278 /* Infinite blocking call, waiting for transmission */
2279 DBG3("Relayd live viewer worker thread polling...");
2280 health_poll_entry();
2281 ret
= lttng_poll_wait(&events
, -1);
2285 * Restart interrupted system call.
2287 if (errno
== EINTR
) {
2296 * Process control. The control connection is prioritised so we don't
2297 * starve it with high throughput tracing data on the data
2300 for (i
= 0; i
< nb_fd
; i
++) {
2301 /* Fetch once the poll data */
2302 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2303 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2305 health_code_update();
2307 /* Thread quit pipe has been closed. Killing thread. */
2308 ret
= check_thread_quit_pipe(pollfd
, revents
);
2314 /* Inspect the relay conn pipe for new connection. */
2315 if (pollfd
== live_conn_pipe
[0]) {
2316 if (revents
& LPOLLIN
) {
2317 struct relay_connection
*conn
;
2319 ret
= lttng_read(live_conn_pipe
[0],
2320 &conn
, sizeof(conn
));
2324 ret
= lttng_poll_add(&events
,
2326 LPOLLIN
| LPOLLRDHUP
);
2328 ERR("Failed to add new live connection file descriptor to poll set");
2331 connection_ht_add(viewer_connections_ht
, conn
);
2332 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2333 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2334 ERR("Relay live pipe error");
2337 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2341 /* Connection activity. */
2342 struct relay_connection
*conn
;
2344 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2349 if (revents
& LPOLLIN
) {
2350 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2351 sizeof(recv_hdr
), 0);
2353 /* Connection closed. */
2354 cleanup_connection_pollfd(&events
, pollfd
);
2355 /* Put "create" ownership reference. */
2356 connection_put(conn
);
2357 DBG("Viewer control conn closed with %d", pollfd
);
2359 ret
= process_control(&recv_hdr
, conn
);
2361 /* Clear the session on error. */
2362 cleanup_connection_pollfd(&events
, pollfd
);
2363 /* Put "create" ownership reference. */
2364 connection_put(conn
);
2365 DBG("Viewer connection closed with %d", pollfd
);
2368 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2369 cleanup_connection_pollfd(&events
, pollfd
);
2370 /* Put "create" ownership reference. */
2371 connection_put(conn
);
2373 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2374 connection_put(conn
);
2377 /* Put local "get_by_sock" reference. */
2378 connection_put(conn
);
2385 (void) fd_tracker_util_poll_clean(the_fd_tracker
, &events
);
2387 /* Cleanup remaining connection object. */
2389 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2392 health_code_update();
2393 connection_put(destroy_conn
);
2397 lttng_ht_destroy(viewer_connections_ht
);
2398 viewer_connections_ht_error
:
2399 /* Close relay conn pipes */
2400 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2402 DBG("Viewer worker thread exited with error");
2404 DBG("Viewer worker thread cleanup complete");
2408 ERR("Health error occurred in %s", __func__
);
2410 health_unregister(health_relayd
);
2411 if (lttng_relay_stop_threads()) {
2412 ERR("Error stopping threads");
2414 rcu_unregister_thread();
2419 * Create the relay command pipe to wake thread_manage_apps.
2420 * Closed in cleanup().
2422 static int create_conn_pipe(void)
2424 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2425 "Live connection pipe", live_conn_pipe
);
2428 int relayd_live_join(void)
2430 int ret
, retval
= 0;
2433 ret
= pthread_join(live_listener_thread
, &status
);
2436 PERROR("pthread_join live listener");
2440 ret
= pthread_join(live_worker_thread
, &status
);
2443 PERROR("pthread_join live worker");
2447 ret
= pthread_join(live_dispatcher_thread
, &status
);
2450 PERROR("pthread_join live dispatcher");
2454 cleanup_relayd_live();
2462 int relayd_live_create(struct lttng_uri
*uri
)
2464 int ret
= 0, retval
= 0;
2470 goto exit_init_data
;
2474 /* Check if daemon is UID = 0 */
2475 is_root
= !getuid();
2478 if (live_uri
->port
< 1024) {
2479 ERR("Need to be root to use ports < 1024");
2481 goto exit_init_data
;
2485 /* Setup the thread apps communication pipe. */
2486 if (create_conn_pipe()) {
2488 goto exit_init_data
;
2491 /* Init relay command queue. */
2492 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2494 /* Set up max poll set size */
2495 if (lttng_poll_set_max_size()) {
2497 goto exit_init_data
;
2500 /* Setup the dispatcher thread */
2501 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2502 thread_dispatcher
, (void *) NULL
);
2505 PERROR("pthread_create viewer dispatcher");
2507 goto exit_dispatcher_thread
;
2510 /* Setup the worker thread */
2511 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2512 thread_worker
, NULL
);
2515 PERROR("pthread_create viewer worker");
2517 goto exit_worker_thread
;
2520 /* Setup the listener thread */
2521 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2522 thread_listener
, (void *) NULL
);
2525 PERROR("pthread_create viewer listener");
2527 goto exit_listener_thread
;
2531 * All OK, started all threads.
2536 * Join on the live_listener_thread should anything be added after
2537 * the live_listener thread's creation.
2540 exit_listener_thread
:
2542 ret
= pthread_join(live_worker_thread
, &status
);
2545 PERROR("pthread_join live worker");
2550 ret
= pthread_join(live_dispatcher_thread
, &status
);
2553 PERROR("pthread_join live dispatcher");
2556 exit_dispatcher_thread
:
2559 cleanup_relayd_live();