2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/rculist.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
56 #include <common/fd-tracker/utils.h>
60 #include "lttng-relayd.h"
62 #include "health-relayd.h"
63 #include "testpoint.h"
64 #include "viewer-stream.h"
67 #include "ctf-trace.h"
68 #include "connection.h"
69 #include "viewer-session.h"
71 #define SESSION_BUF_DEFAULT_COUNT 16
73 static struct lttng_uri
*live_uri
;
76 * This pipe is used to inform the worker thread that a command is queued and
77 * ready to be processed.
79 static int live_conn_pipe
[2] = { -1, -1 };
81 /* Shared between threads */
82 static int live_dispatch_thread_exit
;
84 static pthread_t live_listener_thread
;
85 static pthread_t live_dispatcher_thread
;
86 static pthread_t live_worker_thread
;
89 * Relay command queue.
91 * The live_thread_listener and live_thread_dispatcher communicate with this
94 static struct relay_conn_queue viewer_conn_queue
;
96 static uint64_t last_relay_viewer_session_id
;
97 static pthread_mutex_t last_relay_viewer_session_id_lock
=
98 PTHREAD_MUTEX_INITIALIZER
;
104 void cleanup_relayd_live(void)
112 * Receive a request buffer using a given socket, destination allocated buffer
115 * Return the size of the received message or else a negative value on error
116 * with errno being set by recvmsg() syscall.
119 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
123 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
124 if (ret
< 0 || ret
!= size
) {
126 /* Orderly shutdown. Not necessary to print an error. */
127 DBG("Socket %d did an orderly shutdown", sock
->fd
);
129 ERR("Relay failed to receive request.");
138 * Send a response buffer using a given socket, source allocated buffer of
141 * Return the size of the sent message or else a negative value on error with
142 * errno being set by sendmsg() syscall.
145 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
149 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
151 ERR("Relayd failed to send response.");
158 * Atomically check if new streams got added in one of the sessions attached
159 * and reset the flag to 0.
161 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
165 int check_new_streams(struct relay_connection
*conn
)
167 struct relay_session
*session
;
168 unsigned long current_val
;
171 if (!conn
->viewer_session
) {
175 cds_list_for_each_entry_rcu(session
,
176 &conn
->viewer_session
->session_list
,
177 viewer_session_node
) {
178 if (!session_get(session
)) {
181 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
183 session_put(session
);
194 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
195 * this function should ignore the sent flag or not.
197 * Return 0 on success or else a negative value.
200 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
201 uint64_t session_id
, unsigned int ignore_sent_flag
)
204 struct lttng_viewer_stream send_stream
;
205 struct lttng_ht_iter iter
;
206 struct relay_viewer_stream
*vstream
;
210 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
212 struct ctf_trace
*ctf_trace
;
214 health_code_update();
216 if (!viewer_stream_get(vstream
)) {
220 pthread_mutex_lock(&vstream
->stream
->lock
);
221 /* Ignore if not the same session. */
222 if (vstream
->stream
->trace
->session
->id
!= session_id
||
223 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
224 pthread_mutex_unlock(&vstream
->stream
->lock
);
225 viewer_stream_put(vstream
);
229 ctf_trace
= vstream
->stream
->trace
;
230 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
231 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
232 send_stream
.metadata_flag
= htobe32(
233 vstream
->stream
->is_metadata
);
234 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
235 sizeof(send_stream
.path_name
))) {
236 pthread_mutex_unlock(&vstream
->stream
->lock
);
237 viewer_stream_put(vstream
);
238 ret
= -1; /* Error. */
241 if (lttng_strncpy(send_stream
.channel_name
,
242 vstream
->channel_name
,
243 sizeof(send_stream
.channel_name
))) {
244 pthread_mutex_unlock(&vstream
->stream
->lock
);
245 viewer_stream_put(vstream
);
246 ret
= -1; /* Error. */
250 DBG("Sending stream %" PRIu64
" to viewer",
251 vstream
->stream
->stream_handle
);
252 vstream
->sent_flag
= 1;
253 pthread_mutex_unlock(&vstream
->stream
->lock
);
255 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
256 viewer_stream_put(vstream
);
270 * Create every viewer stream possible for the given session with the seek
271 * type. Three counters *can* be return which are in order the total amount of
272 * viewer stream of the session, the number of unsent stream and the number of
273 * stream created. Those counters can be NULL and thus will be ignored.
275 * session must be locked to ensure that we see either none or all initial
276 * streams for a session, but no intermediate state..
278 * Return 0 on success or else a negative value.
280 static int make_viewer_streams(struct relay_session
*session
,
281 struct lttng_trace_chunk
*viewer_trace_chunk
,
282 enum lttng_viewer_seek seek_t
,
285 uint32_t *nb_created
,
289 struct lttng_ht_iter iter
;
290 struct ctf_trace
*ctf_trace
;
293 ASSERT_LOCKED(session
->lock
);
295 if (!viewer_trace_chunk
) {
296 ERR("Internal error: viewer session associated with session \"%s\" has a NULL trace chunk",
297 session
->session_name
);
302 if (session
->connection_closed
) {
307 * Create viewer streams for relay streams that are ready to be
308 * used for a the given session id only.
311 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
313 bool trace_has_metadata_stream
= false;
314 struct relay_stream
*stream
;
316 health_code_update();
318 if (!ctf_trace_get(ctf_trace
)) {
323 * Iterate over all the streams of the trace to see if we have a
326 cds_list_for_each_entry_rcu(
327 stream
, &ctf_trace
->stream_list
, stream_node
)
329 if (stream
->is_metadata
) {
330 trace_has_metadata_stream
= true;
336 * If there is no metadata stream in this trace at the moment
337 * and we never sent one to the viewer, skip the trace. We
338 * accept that the viewer will not see this trace at all.
340 if (!trace_has_metadata_stream
&&
341 !ctf_trace
->metadata_stream_sent_to_viewer
) {
342 ctf_trace_put(ctf_trace
);
346 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
347 struct relay_viewer_stream
*vstream
;
349 if (!stream_get(stream
)) {
353 * stream published is protected by the session lock.
355 if (!stream
->published
) {
358 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
361 * Save that we sent the metadata stream to the
362 * viewer. So that we know what trace the viewer
365 if (stream
->is_metadata
) {
366 ctf_trace
->metadata_stream_sent_to_viewer
=
369 vstream
= viewer_stream_create(stream
,
370 viewer_trace_chunk
, seek_t
);
373 ctf_trace_put(ctf_trace
);
379 /* Update number of created stream counter. */
383 * Ensure a self-reference is preserved even
384 * after we have put our local reference.
386 if (!viewer_stream_get(vstream
)) {
387 ERR("Unable to get self-reference on viewer stream, logic error.");
391 if (!vstream
->sent_flag
&& nb_unsent
) {
392 /* Update number of unsent stream counter. */
396 /* Update number of total stream counter. */
398 if (stream
->is_metadata
) {
399 if (!stream
->closed
||
400 stream
->metadata_received
> vstream
->metadata_sent
) {
404 if (!stream
->closed
||
405 !(((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) >= 0)) {
411 /* Put local reference. */
412 viewer_stream_put(vstream
);
416 ctf_trace_put(ctf_trace
);
427 int relayd_live_stop(void)
429 /* Stop dispatch thread */
430 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
431 futex_nto1_wake(&viewer_conn_queue
.futex
);
436 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
439 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
443 if (events
== NULL
|| size
== 0) {
448 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
454 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
466 * Check if the thread quit pipe was triggered.
468 * Return 1 if it was triggered else 0;
471 int check_thread_quit_pipe(int fd
, uint32_t events
)
473 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
481 * Create and init socket from uri.
484 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
487 struct lttcomm_sock
*sock
= NULL
;
489 sock
= lttcomm_alloc_sock_from_uri(uri
);
491 ERR("Allocating socket");
495 ret
= lttcomm_create_sock(sock
);
499 DBG("Listening on sock %d for lttng-live", sock
->fd
);
501 ret
= sock
->ops
->bind(sock
);
503 PERROR("Failed to bind lttng-live socket");
507 ret
= sock
->ops
->listen(sock
, -1);
517 lttcomm_destroy_sock(sock
);
523 * This thread manages the listening for new connections on the network
526 void *thread_listener(void *data
)
528 int i
, ret
, pollfd
, err
= -1;
529 uint32_t revents
, nb_fd
;
530 struct lttng_poll_event events
;
531 struct lttcomm_sock
*live_control_sock
;
533 DBG("[thread] Relay live listener started");
535 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
537 health_code_update();
539 live_control_sock
= init_socket(live_uri
);
540 if (!live_control_sock
) {
541 goto error_sock_control
;
544 /* Pass 2 as size here for the thread quit pipe and control sockets. */
545 ret
= create_thread_poll_set(&events
, 2);
547 goto error_create_poll
;
550 /* Add the control socket */
551 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
556 lttng_relay_notify_ready();
558 if (testpoint(relayd_thread_live_listener
)) {
559 goto error_testpoint
;
563 health_code_update();
565 DBG("Listener accepting live viewers connections");
569 ret
= lttng_poll_wait(&events
, -1);
573 * Restart interrupted system call.
575 if (errno
== EINTR
) {
582 DBG("Relay new viewer connection received");
583 for (i
= 0; i
< nb_fd
; i
++) {
584 health_code_update();
586 /* Fetch once the poll data */
587 revents
= LTTNG_POLL_GETEV(&events
, i
);
588 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
590 /* Thread quit pipe has been closed. Killing thread. */
591 ret
= check_thread_quit_pipe(pollfd
, revents
);
597 if (revents
& LPOLLIN
) {
599 * A new connection is requested, therefore a
600 * viewer connection is allocated in this
601 * thread, enqueued to a global queue and
602 * dequeued (and freed) in the worker thread.
605 struct relay_connection
*new_conn
;
606 struct lttcomm_sock
*newsock
;
608 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
610 PERROR("accepting control sock");
613 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
615 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
618 PERROR("setsockopt inet");
619 lttcomm_destroy_sock(newsock
);
622 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
624 lttcomm_destroy_sock(newsock
);
627 /* Ownership assumed by the connection. */
630 /* Enqueue request for the dispatcher thread. */
631 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
635 * Wake the dispatch queue futex.
636 * Implicit memory barrier with the
637 * exchange in cds_wfcq_enqueue.
639 futex_nto1_wake(&viewer_conn_queue
.futex
);
640 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
641 ERR("socket poll error");
644 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
654 lttng_poll_clean(&events
);
656 if (live_control_sock
->fd
>= 0) {
657 ret
= live_control_sock
->ops
->close(live_control_sock
);
662 lttcomm_destroy_sock(live_control_sock
);
666 DBG("Live viewer listener thread exited with error");
668 health_unregister(health_relayd
);
669 DBG("Live viewer listener thread cleanup complete");
670 if (lttng_relay_stop_threads()) {
671 ERR("Error stopping threads");
677 * This thread manages the dispatching of the requests to worker threads
680 void *thread_dispatcher(void *data
)
684 struct cds_wfcq_node
*node
;
685 struct relay_connection
*conn
= NULL
;
687 DBG("[thread] Live viewer relay dispatcher started");
689 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
691 if (testpoint(relayd_thread_live_dispatcher
)) {
692 goto error_testpoint
;
695 health_code_update();
698 health_code_update();
700 /* Atomically prepare the queue futex */
701 futex_nto1_prepare(&viewer_conn_queue
.futex
);
703 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
708 health_code_update();
710 /* Dequeue commands */
711 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
712 &viewer_conn_queue
.tail
);
714 DBG("Woken up but nothing in the live-viewer "
715 "relay command queue");
716 /* Continue thread execution */
719 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
720 DBG("Dispatching viewer request waiting on sock %d",
724 * Inform worker thread of the new request. This
725 * call is blocking so we can be assured that
726 * the data will be read at some point in time
727 * or wait to the end of the world :)
729 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
731 PERROR("write conn pipe");
732 connection_put(conn
);
735 } while (node
!= NULL
);
737 /* Futex wait on queue. Blocking call on futex() */
739 futex_nto1_wait(&viewer_conn_queue
.futex
);
743 /* Normal exit, no error */
750 ERR("Health error occurred in %s", __func__
);
752 health_unregister(health_relayd
);
753 DBG("Live viewer dispatch thread dying");
754 if (lttng_relay_stop_threads()) {
755 ERR("Error stopping threads");
761 * Establish connection with the viewer and check the versions.
763 * Return 0 on success or else negative value.
766 int viewer_connect(struct relay_connection
*conn
)
769 struct lttng_viewer_connect reply
, msg
;
771 conn
->version_check_done
= 1;
773 health_code_update();
775 DBG("Viewer is establishing a connection to the relayd.");
777 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
782 health_code_update();
784 memset(&reply
, 0, sizeof(reply
));
785 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
786 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
788 /* Major versions must be the same */
789 if (reply
.major
!= be32toh(msg
.major
)) {
790 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
791 reply
.major
, be32toh(msg
.major
));
796 conn
->major
= reply
.major
;
797 /* We adapt to the lowest compatible version */
798 if (reply
.minor
<= be32toh(msg
.minor
)) {
799 conn
->minor
= reply
.minor
;
801 conn
->minor
= be32toh(msg
.minor
);
804 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
805 conn
->type
= RELAY_VIEWER_COMMAND
;
806 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
807 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
809 ERR("Unknown connection type : %u", be32toh(msg
.type
));
814 reply
.major
= htobe32(reply
.major
);
815 reply
.minor
= htobe32(reply
.minor
);
816 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
818 * Increment outside of htobe64 macro, because the argument can
819 * be used more than once within the macro, and thus the
820 * operation may be undefined.
822 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
823 last_relay_viewer_session_id
++;
824 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
825 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
828 health_code_update();
830 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
835 health_code_update();
837 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
845 * Send the viewer the list of current sessions.
846 * We need to create a copy of the hash table content because otherwise
847 * we cannot assume the number of entries stays the same between getting
848 * the number of HT elements and iteration over the HT.
850 * Return 0 on success or else a negative value.
853 int viewer_list_sessions(struct relay_connection
*conn
)
856 struct lttng_viewer_list_sessions session_list
;
857 struct lttng_ht_iter iter
;
858 struct relay_session
*session
;
859 struct lttng_viewer_session
*send_session_buf
= NULL
;
860 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
863 DBG("List sessions received");
865 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
866 if (!send_session_buf
) {
871 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
873 struct lttng_viewer_session
*send_session
;
875 health_code_update();
877 pthread_mutex_lock(&session
->lock
);
878 if (session
->connection_closed
) {
879 /* Skip closed session */
882 if (!session
->current_trace_chunk
) {
884 * Skip un-attachable session. It is either
885 * being destroyed or has not had a trace
886 * chunk created against it yet.
891 if (count
>= buf_count
) {
892 struct lttng_viewer_session
*newbuf
;
893 uint32_t new_buf_count
= buf_count
<< 1;
895 newbuf
= realloc(send_session_buf
,
896 new_buf_count
* sizeof(*send_session_buf
));
901 send_session_buf
= newbuf
;
902 buf_count
= new_buf_count
;
904 send_session
= &send_session_buf
[count
];
905 if (lttng_strncpy(send_session
->session_name
,
906 session
->session_name
,
907 sizeof(send_session
->session_name
))) {
911 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
912 sizeof(send_session
->hostname
))) {
916 send_session
->id
= htobe64(session
->id
);
917 send_session
->live_timer
= htobe32(session
->live_timer
);
918 if (session
->viewer_attached
) {
919 send_session
->clients
= htobe32(1);
921 send_session
->clients
= htobe32(0);
923 send_session
->streams
= htobe32(session
->stream_count
);
926 pthread_mutex_unlock(&session
->lock
);
929 pthread_mutex_unlock(&session
->lock
);
937 session_list
.sessions_count
= htobe32(count
);
939 health_code_update();
941 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
946 health_code_update();
948 ret
= send_response(conn
->sock
, send_session_buf
,
949 count
* sizeof(*send_session_buf
));
953 health_code_update();
957 free(send_session_buf
);
962 * Send the viewer the list of current streams.
965 int viewer_get_new_streams(struct relay_connection
*conn
)
967 int ret
, send_streams
= 0;
968 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
969 struct lttng_viewer_new_streams_request request
;
970 struct lttng_viewer_new_streams_response response
;
971 struct relay_session
*session
= NULL
;
977 DBG("Get new streams received");
979 health_code_update();
981 /* Receive the request from the connected client. */
982 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
986 session_id
= be64toh(request
.session_id
);
988 health_code_update();
990 memset(&response
, 0, sizeof(response
));
992 session
= session_get_by_id(session_id
);
994 DBG("Relay session %" PRIu64
" not found", session_id
);
995 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
999 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
1000 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
1004 pthread_mutex_lock(&session
->lock
);
1005 ret
= make_viewer_streams(session
,
1006 conn
->viewer_session
->current_trace_chunk
,
1007 LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
1008 &nb_created
, &closed
);
1010 goto error_unlock_session
;
1013 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
1015 /* Only send back the newly created streams with the unsent ones. */
1016 nb_streams
= nb_created
+ nb_unsent
;
1017 response
.streams_count
= htobe32(nb_streams
);
1020 * If the session is closed, HUP when there are no more streams
1023 if (closed
&& nb_total
== 0) {
1025 response
.streams_count
= 0;
1026 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1027 goto send_reply_unlock
;
1030 pthread_mutex_unlock(&session
->lock
);
1033 health_code_update();
1034 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1036 goto end_put_session
;
1038 health_code_update();
1041 * Unknown or empty session, just return gracefully, the viewer
1042 * knows what is happening.
1044 if (!send_streams
|| !nb_streams
) {
1046 goto end_put_session
;
1050 * Send stream and *DON'T* ignore the sent flag so every viewer
1051 * streams that were not sent from that point will be sent to
1054 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
1056 goto end_put_session
;
1061 session_put(session
);
1065 error_unlock_session
:
1066 pthread_mutex_unlock(&session
->lock
);
1067 session_put(session
);
1072 * Send the viewer the list of current sessions.
1075 int viewer_attach_session(struct relay_connection
*conn
)
1077 int send_streams
= 0;
1079 uint32_t nb_streams
= 0;
1080 enum lttng_viewer_seek seek_type
;
1081 struct lttng_viewer_attach_session_request request
;
1082 struct lttng_viewer_attach_session_response response
;
1083 struct relay_session
*session
= NULL
;
1084 enum lttng_viewer_attach_return_code viewer_attach_status
;
1085 bool closed
= false;
1086 uint64_t session_id
;
1090 health_code_update();
1092 /* Receive the request from the connected client. */
1093 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1098 session_id
= be64toh(request
.session_id
);
1099 health_code_update();
1101 memset(&response
, 0, sizeof(response
));
1103 if (!conn
->viewer_session
) {
1104 DBG("Client trying to attach before creating a live viewer session");
1105 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1109 session
= session_get_by_id(session_id
);
1111 DBG("Relay session %" PRIu64
" not found", session_id
);
1112 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1115 DBG("Attach session ID %" PRIu64
" received", session_id
);
1117 pthread_mutex_lock(&session
->lock
);
1118 if (!session
->current_trace_chunk
) {
1120 * Session is either being destroyed or it never had a trace
1121 * chunk created against it.
1123 DBG("Session requested by live client has no current trace chunk, returning unknown session");
1124 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1127 if (session
->live_timer
== 0) {
1128 DBG("Not live session");
1129 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1134 viewer_attach_status
= viewer_session_attach(conn
->viewer_session
,
1136 if (viewer_attach_status
!= LTTNG_VIEWER_ATTACH_OK
) {
1137 response
.status
= htobe32(viewer_attach_status
);
1141 switch (be32toh(request
.seek
)) {
1142 case LTTNG_VIEWER_SEEK_BEGINNING
:
1143 case LTTNG_VIEWER_SEEK_LAST
:
1144 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1145 seek_type
= be32toh(request
.seek
);
1148 ERR("Wrong seek parameter");
1149 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1154 ret
= make_viewer_streams(session
,
1155 conn
->viewer_session
->current_trace_chunk
, seek_type
,
1156 &nb_streams
, NULL
, NULL
, &closed
);
1158 goto end_put_session
;
1160 pthread_mutex_unlock(&session
->lock
);
1161 session_put(session
);
1164 response
.streams_count
= htobe32(nb_streams
);
1166 * If the session is closed when the viewer is attaching, it
1167 * means some of the streams may have been concurrently removed,
1168 * so we don't allow the viewer to attach, even if there are
1169 * streams available.
1173 response
.streams_count
= 0;
1174 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1179 health_code_update();
1180 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1182 goto end_put_session
;
1184 health_code_update();
1187 * Unknown or empty session, just return gracefully, the viewer
1188 * knows what is happening.
1190 if (!send_streams
|| !nb_streams
) {
1192 goto end_put_session
;
1195 /* Send stream and ignore the sent flag. */
1196 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1198 goto end_put_session
;
1203 pthread_mutex_unlock(&session
->lock
);
1204 session_put(session
);
1211 * Open the index file if needed for the given vstream.
1213 * If an index file is successfully opened, the vstream will set it as its
1214 * current index file.
1216 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1218 * Called with rstream lock held.
1220 static int try_open_index(struct relay_viewer_stream
*vstream
,
1221 struct relay_stream
*rstream
)
1224 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1225 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1226 enum lttng_trace_chunk_status chunk_status
;
1228 if (vstream
->index_file
) {
1233 * First time, we open the index file and at least one index is ready.
1235 if (rstream
->index_received_seqcount
== 0) {
1239 chunk_status
= lttng_index_file_create_from_trace_chunk_read_only(
1240 vstream
->stream_file
.trace_chunk
, rstream
->path_name
,
1241 rstream
->channel_name
, rstream
->tracefile_size
,
1242 vstream
->current_tracefile_id
,
1243 lttng_to_index_major(connection_major
, connection_minor
),
1244 lttng_to_index_minor(connection_major
, connection_minor
),
1245 true, &vstream
->index_file
);
1246 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1247 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1259 * Check the status of the index for the given stream. This function
1260 * updates the index structure if needed and can put (close) the vstream
1261 * in the HUP situation.
1263 * Return 0 means that we can proceed with the index. A value of 1 means
1264 * that the index has been updated and is ready to be sent to the
1265 * client. A negative value indicates an error that can't be handled.
1267 * Called with rstream lock held.
1269 static int check_index_status(struct relay_viewer_stream
*vstream
,
1270 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1271 struct lttng_viewer_index
*index
)
1275 DBG("Check index status: index_received_seqcount %" PRIu64
" "
1276 "index_sent_seqcount %" PRIu64
" "
1277 "for stream %" PRIu64
,
1278 rstream
->index_received_seqcount
,
1279 vstream
->index_sent_seqcount
,
1280 vstream
->stream
->stream_handle
);
1281 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1282 && rstream
->index_received_seqcount
1283 == vstream
->index_sent_seqcount
) {
1285 * Last index sent and session connection or relay
1286 * stream are closed.
1288 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1290 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1291 (rstream
->index_received_seqcount
== 0 ||
1292 (vstream
->index_sent_seqcount
!= 0 &&
1293 rstream
->index_received_seqcount
1294 <= vstream
->index_sent_seqcount
))) {
1296 * We've received a synchronization beacon and the last index
1297 * available has been sent, the index for now is inactive.
1299 * In this case, we have received a beacon which allows us to
1300 * inform the client of a time interval during which we can
1301 * guarantee that there are no events to read (and never will
1304 * The sent seqcount can grow higher than receive seqcount on
1305 * clear because the rotation performed by clear will push
1306 * the index_sent_seqcount ahead (see
1307 * viewer_stream_sync_tracefile_array_tail) and skip over
1308 * packet sequence numbers.
1310 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1311 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1312 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1313 DBG("Check index status: inactive with beacon, for stream %" PRIu64
,
1314 vstream
->stream
->stream_handle
);
1316 } else if (rstream
->index_received_seqcount
== 0 ||
1317 (vstream
->index_sent_seqcount
!= 0 &&
1318 rstream
->index_received_seqcount
1319 <= vstream
->index_sent_seqcount
)) {
1321 * This checks whether received <= sent seqcount. In
1322 * this case, we have not received a beacon. Therefore,
1323 * we can only ask the client to retry later.
1325 * The sent seqcount can grow higher than receive seqcount on
1326 * clear because the rotation performed by clear will push
1327 * the index_sent_seqcount ahead (see
1328 * viewer_stream_sync_tracefile_array_tail) and skip over
1329 * packet sequence numbers.
1331 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1332 DBG("Check index status: retry for stream %" PRIu64
,
1333 vstream
->stream
->stream_handle
);
1335 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1336 vstream
->current_tracefile_id
,
1337 vstream
->index_sent_seqcount
)) {
1339 * The next index we want to send cannot be read either
1340 * because we need to perform a rotation, or due to
1341 * the producer having overwritten its trace file.
1343 DBG("Viewer stream %" PRIu64
" rotation",
1344 vstream
->stream
->stream_handle
);
1345 ret
= viewer_stream_rotate(vstream
);
1347 /* EOF across entire stream. */
1348 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1352 * If we have been pushed due to overwrite, it
1353 * necessarily means there is data that can be read in
1354 * the stream. If we rotated because we reached the end
1355 * of a tracefile, it means the following tracefile
1356 * needs to contain at least one index, else we would
1357 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1358 * viewer. The updated index_sent_seqcount needs to
1359 * point to a readable index entry now.
1361 * In the case where we "rotate" on a single file, we
1362 * can end up in a case where the requested index is
1363 * still unavailable.
1365 if (rstream
->tracefile_count
== 1 &&
1366 !tracefile_array_seq_in_file(
1368 vstream
->current_tracefile_id
,
1369 vstream
->index_sent_seqcount
)) {
1370 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1371 DBG("Check index status: retry: "
1372 "tracefile array sequence number %" PRIu64
1373 " not in file for stream %" PRIu64
,
1374 vstream
->index_sent_seqcount
,
1375 vstream
->stream
->stream_handle
);
1378 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1379 vstream
->current_tracefile_id
,
1380 vstream
->index_sent_seqcount
));
1382 /* ret == 0 means successful so we continue. */
1387 viewer_stream_put(vstream
);
1393 * Send the next index for a stream.
1395 * Return 0 on success or else a negative value.
1398 int viewer_get_next_index(struct relay_connection
*conn
)
1401 struct lttng_viewer_get_next_index request_index
;
1402 struct lttng_viewer_index viewer_index
;
1403 struct ctf_packet_index packet_index
;
1404 struct relay_viewer_stream
*vstream
= NULL
;
1405 struct relay_stream
*rstream
= NULL
;
1406 struct ctf_trace
*ctf_trace
= NULL
;
1407 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1411 DBG("Viewer get next index");
1413 memset(&viewer_index
, 0, sizeof(viewer_index
));
1414 health_code_update();
1416 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1420 health_code_update();
1422 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1424 DBG("Client requested index of unknown stream id %" PRIu64
,
1425 (uint64_t) be64toh(request_index
.stream_id
));
1426 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1430 /* Use back. ref. Protected by refcounts. */
1431 rstream
= vstream
->stream
;
1432 ctf_trace
= rstream
->trace
;
1434 /* metadata_viewer_stream may be NULL. */
1435 metadata_viewer_stream
=
1436 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1438 pthread_mutex_lock(&rstream
->lock
);
1441 * The viewer should not ask for index on metadata stream.
1443 if (rstream
->is_metadata
) {
1444 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1448 if (rstream
->ongoing_rotation
.is_set
) {
1449 /* Rotation is ongoing, try again later. */
1450 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1454 if (rstream
->trace
->session
->ongoing_rotation
) {
1455 /* Rotation is ongoing, try again later. */
1456 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1460 if (rstream
->trace_chunk
) {
1461 uint64_t rchunk_id
, vchunk_id
;
1464 * If the relay stream is not yet closed, ensure the viewer
1465 * chunk matches the relay chunk after clear.
1467 if (lttng_trace_chunk_get_id(rstream
->trace_chunk
,
1468 &rchunk_id
) != LTTNG_TRACE_CHUNK_STATUS_OK
) {
1469 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1472 if (lttng_trace_chunk_get_id(
1473 conn
->viewer_session
->current_trace_chunk
,
1474 &vchunk_id
) != LTTNG_TRACE_CHUNK_STATUS_OK
) {
1475 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1479 if (rchunk_id
!= vchunk_id
) {
1480 DBG("Relay and viewer chunk ids differ: "
1481 "rchunk_id %" PRIu64
" vchunk_id %" PRIu64
,
1482 rchunk_id
, vchunk_id
);
1484 lttng_trace_chunk_put(
1485 conn
->viewer_session
->current_trace_chunk
);
1486 conn
->viewer_session
->current_trace_chunk
= NULL
;
1487 ret
= viewer_session_set_trace_chunk_copy(
1488 conn
->viewer_session
,
1489 rstream
->trace_chunk
);
1491 viewer_index
.status
=
1492 htobe32(LTTNG_VIEWER_INDEX_ERR
);
1497 if (conn
->viewer_session
->current_trace_chunk
!=
1498 vstream
->stream_file
.trace_chunk
) {
1499 bool acquired_reference
;
1501 DBG("Viewer session and viewer stream chunk differ: "
1502 "vsession chunk %p vstream chunk %p",
1503 conn
->viewer_session
->current_trace_chunk
,
1504 vstream
->stream_file
.trace_chunk
);
1505 lttng_trace_chunk_put(vstream
->stream_file
.trace_chunk
);
1506 acquired_reference
= lttng_trace_chunk_get(conn
->viewer_session
->current_trace_chunk
);
1507 assert(acquired_reference
);
1508 vstream
->stream_file
.trace_chunk
=
1509 conn
->viewer_session
->current_trace_chunk
;
1510 viewer_stream_sync_tracefile_array_tail(vstream
);
1511 viewer_stream_close_files(vstream
);
1514 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1517 } else if (ret
== 1) {
1519 * We have no index to send and check_index_status has populated
1520 * viewer_index's status.
1524 /* At this point, ret is 0 thus we will be able to read the index. */
1527 /* Try to open an index if one is needed for that stream. */
1528 ret
= try_open_index(vstream
, rstream
);
1529 if (ret
== -ENOENT
) {
1530 if (rstream
->closed
) {
1531 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1534 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1539 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1544 * vstream->stream_fd may be NULL if it has been closed by
1545 * tracefile rotation, or if we are at the beginning of the
1546 * stream. We open the data stream file here to protect against
1547 * overwrite caused by tracefile rotation (in association with
1548 * unlink performed before overwrite).
1550 if (!vstream
->stream_file
.fd
) {
1552 char file_path
[LTTNG_PATH_MAX
];
1553 enum lttng_trace_chunk_status status
;
1555 ret
= utils_stream_file_path(rstream
->path_name
,
1556 rstream
->channel_name
, rstream
->tracefile_size
,
1557 vstream
->current_tracefile_id
, NULL
, file_path
,
1564 * It is possible the the file we are trying to open is
1565 * missing if the stream has been closed (application exits with
1566 * per-pid buffers) and a clear command has been performed.
1568 status
= lttng_trace_chunk_open_file(
1569 vstream
->stream_file
.trace_chunk
,
1570 file_path
, O_RDONLY
, 0, &fd
, true);
1571 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1572 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
&&
1574 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1577 PERROR("Failed to open trace file for viewer stream");
1580 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1581 if (!vstream
->stream_file
.fd
) {
1583 PERROR("Failed to close viewer stream file");
1589 ret
= check_new_streams(conn
);
1591 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1593 } else if (ret
== 1) {
1594 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1597 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1599 ERR("Relay error reading index file %d",
1600 vstream
->index_file
->fd
);
1601 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1604 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1605 vstream
->index_sent_seqcount
++;
1609 * Indexes are stored in big endian, no need to switch before sending.
1611 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1612 rstream
->stream_handle
,
1613 (uint64_t) be64toh(packet_index
.offset
));
1614 viewer_index
.offset
= packet_index
.offset
;
1615 viewer_index
.packet_size
= packet_index
.packet_size
;
1616 viewer_index
.content_size
= packet_index
.content_size
;
1617 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1618 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1619 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1620 viewer_index
.stream_id
= packet_index
.stream_id
;
1624 pthread_mutex_unlock(&rstream
->lock
);
1627 if (metadata_viewer_stream
) {
1628 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1629 DBG("get next index metadata check: recv %" PRIu64
1631 metadata_viewer_stream
->stream
->metadata_received
,
1632 metadata_viewer_stream
->metadata_sent
);
1633 if (!metadata_viewer_stream
->stream
->metadata_received
||
1634 metadata_viewer_stream
->stream
->metadata_received
>
1635 metadata_viewer_stream
->metadata_sent
) {
1636 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1638 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1641 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1642 health_code_update();
1644 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1648 health_code_update();
1651 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1652 vstream
->index_sent_seqcount
,
1653 vstream
->stream
->stream_handle
);
1656 if (metadata_viewer_stream
) {
1657 viewer_stream_put(metadata_viewer_stream
);
1660 viewer_stream_put(vstream
);
1665 pthread_mutex_unlock(&rstream
->lock
);
1666 if (metadata_viewer_stream
) {
1667 viewer_stream_put(metadata_viewer_stream
);
1669 viewer_stream_put(vstream
);
1674 * Send the next index for a stream
1676 * Return 0 on success or else a negative value.
1679 int viewer_get_packet(struct relay_connection
*conn
)
1684 struct lttng_viewer_get_packet get_packet_info
;
1685 struct lttng_viewer_trace_packet reply_header
;
1686 struct relay_viewer_stream
*vstream
= NULL
;
1687 uint32_t reply_size
= sizeof(reply_header
);
1688 uint32_t packet_data_len
= 0;
1691 DBG2("Relay get data packet");
1693 health_code_update();
1695 ret
= recv_request(conn
->sock
, &get_packet_info
,
1696 sizeof(get_packet_info
));
1700 health_code_update();
1702 /* From this point on, the error label can be reached. */
1703 memset(&reply_header
, 0, sizeof(reply_header
));
1705 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1707 DBG("Client requested packet of unknown stream id %" PRIu64
,
1708 (uint64_t) be64toh(get_packet_info
.stream_id
));
1709 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1710 goto send_reply_nolock
;
1712 packet_data_len
= be32toh(get_packet_info
.len
);
1713 reply_size
+= packet_data_len
;
1716 reply
= zmalloc(reply_size
);
1718 PERROR("packet reply zmalloc");
1719 reply_size
= sizeof(reply_header
);
1723 pthread_mutex_lock(&vstream
->stream
->lock
);
1724 lseek_ret
= lseek(vstream
->stream_file
.fd
->fd
,
1725 be64toh(get_packet_info
.offset
), SEEK_SET
);
1726 if (lseek_ret
< 0) {
1727 PERROR("lseek fd %d to offset %" PRIu64
,
1728 vstream
->stream_file
.fd
->fd
,
1729 (uint64_t) be64toh(get_packet_info
.offset
));
1732 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
,
1733 reply
+ sizeof(reply_header
), packet_data_len
);
1734 if (read_len
< packet_data_len
) {
1735 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1736 vstream
->stream_file
.fd
->fd
,
1737 (uint64_t) be64toh(get_packet_info
.offset
));
1740 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1741 reply_header
.len
= htobe32(packet_data_len
);
1745 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1749 pthread_mutex_unlock(&vstream
->stream
->lock
);
1753 health_code_update();
1756 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1757 ret
= send_response(conn
->sock
, reply
, reply_size
);
1759 /* No reply to send. */
1760 ret
= send_response(conn
->sock
, &reply_header
,
1764 health_code_update();
1766 PERROR("sendmsg of packet data failed");
1770 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
,
1771 (uint64_t) be64toh(get_packet_info
.stream_id
));
1777 viewer_stream_put(vstream
);
1783 * Send the session's metadata
1785 * Return 0 on success else a negative value.
1788 int viewer_get_metadata(struct relay_connection
*conn
)
1794 struct lttng_viewer_get_metadata request
;
1795 struct lttng_viewer_metadata_packet reply
;
1796 struct relay_viewer_stream
*vstream
= NULL
;
1800 DBG("Relay get metadata");
1802 health_code_update();
1804 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1808 health_code_update();
1810 memset(&reply
, 0, sizeof(reply
));
1812 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1815 * The metadata stream can be closed by a CLOSE command
1816 * just before we attach. It can also be closed by
1817 * per-pid tracing during tracing. Therefore, it is
1818 * possible that we cannot find this viewer stream.
1819 * Reply back to the client with an error if we cannot
1822 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1823 (uint64_t) be64toh(request
.stream_id
));
1824 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1827 pthread_mutex_lock(&vstream
->stream
->lock
);
1828 if (!vstream
->stream
->is_metadata
) {
1829 ERR("Invalid metadata stream");
1833 if (vstream
->metadata_sent
>= vstream
->stream
->metadata_received
) {
1835 * The live viewers expect to receive a NO_NEW_METADATA
1836 * status before a stream disappears, otherwise they abort the
1837 * entire live connection when receiving an error status.
1839 * Clear feature resets the metadata_sent to 0 until the
1840 * same metadata is received again.
1842 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1844 * The live viewer considers a closed 0 byte metadata stream as
1847 if (vstream
->metadata_sent
> 0) {
1848 vstream
->stream
->no_new_metadata_notified
= true;
1849 if (vstream
->stream
->closed
) {
1850 /* Release ownership for the viewer metadata stream. */
1851 viewer_stream_put(vstream
);
1857 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1859 /* first time, we open the metadata file */
1860 if (!vstream
->stream_file
.fd
) {
1862 char file_path
[LTTNG_PATH_MAX
];
1863 enum lttng_trace_chunk_status status
;
1864 struct relay_stream
*rstream
= vstream
->stream
;
1866 ret
= utils_stream_file_path(rstream
->path_name
,
1867 rstream
->channel_name
, rstream
->tracefile_size
,
1868 vstream
->current_tracefile_id
, NULL
, file_path
,
1875 * It is possible the the metadata file we are trying to open is
1876 * missing if the stream has been closed (application exits with
1877 * per-pid buffers) and a clear command has been performed.
1879 status
= lttng_trace_chunk_open_file(
1880 vstream
->stream_file
.trace_chunk
,
1881 file_path
, O_RDONLY
, 0, &fd
, true);
1882 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1883 if (status
== LTTNG_TRACE_CHUNK_STATUS_NO_FILE
) {
1884 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1886 if (vstream
->stream
->closed
) {
1887 viewer_stream_put(vstream
);
1891 PERROR("Failed to open metadata file for viewer stream");
1894 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1895 if (!vstream
->stream_file
.fd
) {
1897 PERROR("Failed to close viewer metadata file");
1903 reply
.len
= htobe64(len
);
1904 data
= zmalloc(len
);
1906 PERROR("viewer metadata zmalloc");
1910 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
, data
, len
);
1911 if (read_len
< len
) {
1912 PERROR("Relay reading metadata file");
1915 vstream
->metadata_sent
+= read_len
;
1916 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1921 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1924 health_code_update();
1926 pthread_mutex_unlock(&vstream
->stream
->lock
);
1928 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1932 health_code_update();
1935 ret
= send_response(conn
->sock
, data
, len
);
1941 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1942 (uint64_t) be64toh(request
.stream_id
));
1944 DBG("Metadata sent");
1950 viewer_stream_put(vstream
);
1956 * Create a viewer session.
1958 * Return 0 on success or else a negative value.
1961 int viewer_create_session(struct relay_connection
*conn
)
1964 struct lttng_viewer_create_session_response resp
;
1966 DBG("Viewer create session received");
1968 memset(&resp
, 0, sizeof(resp
));
1969 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1970 conn
->viewer_session
= viewer_session_create();
1971 if (!conn
->viewer_session
) {
1972 ERR("Allocation viewer session");
1973 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1978 health_code_update();
1979 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1983 health_code_update();
1991 * Detach a viewer session.
1993 * Return 0 on success or else a negative value.
1996 int viewer_detach_session(struct relay_connection
*conn
)
1999 struct lttng_viewer_detach_session_response response
;
2000 struct lttng_viewer_detach_session_request request
;
2001 struct relay_session
*session
= NULL
;
2002 uint64_t viewer_session_to_close
;
2004 DBG("Viewer detach session received");
2008 health_code_update();
2010 /* Receive the request from the connected client. */
2011 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
2015 viewer_session_to_close
= be64toh(request
.session_id
);
2017 if (!conn
->viewer_session
) {
2018 DBG("Client trying to detach before creating a live viewer session");
2019 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2023 health_code_update();
2025 memset(&response
, 0, sizeof(response
));
2026 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
2028 session
= session_get_by_id(be64toh(request
.session_id
));
2030 DBG("Relay session %" PRIu64
" not found",
2031 (uint64_t) be64toh(request
.session_id
));
2032 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
2036 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
2038 DBG("Not attached to this session");
2039 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
2040 goto send_reply_put
;
2043 viewer_session_close_one_session(conn
->viewer_session
, session
);
2044 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
2045 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
2048 session_put(session
);
2051 health_code_update();
2052 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
2056 health_code_update();
2064 * live_relay_unknown_command: send -1 if received unknown command
2067 void live_relay_unknown_command(struct relay_connection
*conn
)
2069 struct lttcomm_relayd_generic_reply reply
;
2071 memset(&reply
, 0, sizeof(reply
));
2072 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
2073 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
2077 * Process the commands received on the control socket
2080 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
2081 struct relay_connection
*conn
)
2086 msg_value
= be32toh(recv_hdr
->cmd
);
2089 * Make sure we've done the version check before any command other then a
2090 * new client connection.
2092 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
2093 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
2098 switch (msg_value
) {
2099 case LTTNG_VIEWER_CONNECT
:
2100 ret
= viewer_connect(conn
);
2102 case LTTNG_VIEWER_LIST_SESSIONS
:
2103 ret
= viewer_list_sessions(conn
);
2105 case LTTNG_VIEWER_ATTACH_SESSION
:
2106 ret
= viewer_attach_session(conn
);
2108 case LTTNG_VIEWER_GET_NEXT_INDEX
:
2109 ret
= viewer_get_next_index(conn
);
2111 case LTTNG_VIEWER_GET_PACKET
:
2112 ret
= viewer_get_packet(conn
);
2114 case LTTNG_VIEWER_GET_METADATA
:
2115 ret
= viewer_get_metadata(conn
);
2117 case LTTNG_VIEWER_GET_NEW_STREAMS
:
2118 ret
= viewer_get_new_streams(conn
);
2120 case LTTNG_VIEWER_CREATE_SESSION
:
2121 ret
= viewer_create_session(conn
);
2123 case LTTNG_VIEWER_DETACH_SESSION
:
2124 ret
= viewer_detach_session(conn
);
2127 ERR("Received unknown viewer command (%u)",
2128 be32toh(recv_hdr
->cmd
));
2129 live_relay_unknown_command(conn
);
2139 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
2143 (void) lttng_poll_del(events
, pollfd
);
2145 ret
= close(pollfd
);
2147 ERR("Closing pollfd %d", pollfd
);
2152 * This thread does the actual work
2155 void *thread_worker(void *data
)
2159 struct lttng_poll_event events
;
2160 struct lttng_ht
*viewer_connections_ht
;
2161 struct lttng_ht_iter iter
;
2162 struct lttng_viewer_cmd recv_hdr
;
2163 struct relay_connection
*destroy_conn
;
2165 DBG("[thread] Live viewer relay worker started");
2167 rcu_register_thread();
2169 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
2171 if (testpoint(relayd_thread_live_worker
)) {
2172 goto error_testpoint
;
2175 /* table of connections indexed on socket */
2176 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2177 if (!viewer_connections_ht
) {
2178 goto viewer_connections_ht_error
;
2181 ret
= create_thread_poll_set(&events
, 2);
2183 goto error_poll_create
;
2186 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
2195 health_code_update();
2197 /* Infinite blocking call, waiting for transmission */
2198 DBG3("Relayd live viewer worker thread polling...");
2199 health_poll_entry();
2200 ret
= lttng_poll_wait(&events
, -1);
2204 * Restart interrupted system call.
2206 if (errno
== EINTR
) {
2215 * Process control. The control connection is prioritised so we don't
2216 * starve it with high throughput tracing data on the data
2219 for (i
= 0; i
< nb_fd
; i
++) {
2220 /* Fetch once the poll data */
2221 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2222 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2224 health_code_update();
2226 /* Thread quit pipe has been closed. Killing thread. */
2227 ret
= check_thread_quit_pipe(pollfd
, revents
);
2233 /* Inspect the relay conn pipe for new connection. */
2234 if (pollfd
== live_conn_pipe
[0]) {
2235 if (revents
& LPOLLIN
) {
2236 struct relay_connection
*conn
;
2238 ret
= lttng_read(live_conn_pipe
[0],
2239 &conn
, sizeof(conn
));
2243 ret
= lttng_poll_add(&events
,
2245 LPOLLIN
| LPOLLRDHUP
);
2247 ERR("Failed to add new live connection file descriptor to poll set");
2250 connection_ht_add(viewer_connections_ht
, conn
);
2251 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2252 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2253 ERR("Relay live pipe error");
2256 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2260 /* Connection activity. */
2261 struct relay_connection
*conn
;
2263 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2268 if (revents
& LPOLLIN
) {
2269 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2270 sizeof(recv_hdr
), 0);
2272 /* Connection closed. */
2273 cleanup_connection_pollfd(&events
, pollfd
);
2274 /* Put "create" ownership reference. */
2275 connection_put(conn
);
2276 DBG("Viewer control conn closed with %d", pollfd
);
2278 ret
= process_control(&recv_hdr
, conn
);
2280 /* Clear the session on error. */
2281 cleanup_connection_pollfd(&events
, pollfd
);
2282 /* Put "create" ownership reference. */
2283 connection_put(conn
);
2284 DBG("Viewer connection closed with %d", pollfd
);
2287 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2288 cleanup_connection_pollfd(&events
, pollfd
);
2289 /* Put "create" ownership reference. */
2290 connection_put(conn
);
2292 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2293 connection_put(conn
);
2296 /* Put local "get_by_sock" reference. */
2297 connection_put(conn
);
2304 lttng_poll_clean(&events
);
2306 /* Cleanup remaining connection object. */
2308 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2311 health_code_update();
2312 connection_put(destroy_conn
);
2316 lttng_ht_destroy(viewer_connections_ht
);
2317 viewer_connections_ht_error
:
2318 /* Close relay conn pipes */
2319 (void) fd_tracker_util_pipe_close(the_fd_tracker
, live_conn_pipe
);
2321 DBG("Viewer worker thread exited with error");
2323 DBG("Viewer worker thread cleanup complete");
2327 ERR("Health error occurred in %s", __func__
);
2329 health_unregister(health_relayd
);
2330 if (lttng_relay_stop_threads()) {
2331 ERR("Error stopping threads");
2333 rcu_unregister_thread();
2338 * Create the relay command pipe to wake thread_manage_apps.
2339 * Closed in cleanup().
2341 static int create_conn_pipe(void)
2343 return fd_tracker_util_pipe_open_cloexec(the_fd_tracker
,
2344 "Live connection pipe", live_conn_pipe
);
2347 int relayd_live_join(void)
2349 int ret
, retval
= 0;
2352 ret
= pthread_join(live_listener_thread
, &status
);
2355 PERROR("pthread_join live listener");
2359 ret
= pthread_join(live_worker_thread
, &status
);
2362 PERROR("pthread_join live worker");
2366 ret
= pthread_join(live_dispatcher_thread
, &status
);
2369 PERROR("pthread_join live dispatcher");
2373 cleanup_relayd_live();
2381 int relayd_live_create(struct lttng_uri
*uri
)
2383 int ret
= 0, retval
= 0;
2389 goto exit_init_data
;
2393 /* Check if daemon is UID = 0 */
2394 is_root
= !getuid();
2397 if (live_uri
->port
< 1024) {
2398 ERR("Need to be root to use ports < 1024");
2400 goto exit_init_data
;
2404 /* Setup the thread apps communication pipe. */
2405 if (create_conn_pipe()) {
2407 goto exit_init_data
;
2410 /* Init relay command queue. */
2411 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2413 /* Set up max poll set size */
2414 if (lttng_poll_set_max_size()) {
2416 goto exit_init_data
;
2419 /* Setup the dispatcher thread */
2420 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2421 thread_dispatcher
, (void *) NULL
);
2424 PERROR("pthread_create viewer dispatcher");
2426 goto exit_dispatcher_thread
;
2429 /* Setup the worker thread */
2430 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2431 thread_worker
, NULL
);
2434 PERROR("pthread_create viewer worker");
2436 goto exit_worker_thread
;
2439 /* Setup the listener thread */
2440 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2441 thread_listener
, (void *) NULL
);
2444 PERROR("pthread_create viewer listener");
2446 goto exit_listener_thread
;
2450 * All OK, started all threads.
2455 * Join on the live_listener_thread should anything be added after
2456 * the live_listener thread's creation.
2459 exit_listener_thread
:
2461 ret
= pthread_join(live_worker_thread
, &status
);
2464 PERROR("pthread_join live worker");
2469 ret
= pthread_join(live_dispatcher_thread
, &status
);
2472 PERROR("pthread_join live dispatcher");
2475 exit_dispatcher_thread
:
2478 cleanup_relayd_live();