2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/futex.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/sessiond-comm/inet.h>
50 #include <common/sessiond-comm/relayd.h>
51 #include <common/uri.h>
52 #include <common/utils.h>
56 #include "lttng-relayd.h"
57 #include "lttng-viewer.h"
60 static struct lttng_uri
*live_uri
;
63 * Quit pipe for all threads. This permits a single cancellation point
64 * for all threads when receiving an event on the pipe.
66 static int live_thread_quit_pipe
[2] = { -1, -1 };
69 * This pipe is used to inform the worker thread that a command is queued and
70 * ready to be processed.
72 static int live_relay_cmd_pipe
[2] = { -1, -1 };
74 /* Shared between threads */
75 static int live_dispatch_thread_exit
;
77 static pthread_t live_listener_thread
;
78 static pthread_t live_dispatcher_thread
;
79 static pthread_t live_worker_thread
;
82 * Relay command queue.
84 * The live_thread_listener and live_thread_dispatcher communicate with this
87 static struct relay_cmd_queue viewer_cmd_queue
;
89 static uint64_t last_relay_viewer_session_id
;
99 /* Close thread quit pipes */
100 utils_close_pipe(live_thread_quit_pipe
);
105 * Write to writable pipe used to notify a thread.
108 int notify_thread_pipe(int wpipe
)
113 ret
= write(wpipe
, "!", 1);
114 } while (ret
< 0 && errno
== EINTR
);
115 if (ret
< 0 || ret
!= 1) {
116 PERROR("write poll pipe");
123 * Stop all threads by closing the thread quit pipe.
126 void stop_threads(void)
130 /* Stopping all threads */
131 DBG("Terminating all live threads");
132 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
134 ERR("write error on thread quit pipe");
137 /* Dispatch thread */
138 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
139 futex_nto1_wake(&viewer_cmd_queue
.futex
);
143 * Init thread quit pipe.
145 * Return -1 on error or 0 if all pipes are created.
148 int init_thread_quit_pipe(void)
152 ret
= utils_create_pipe_cloexec(live_thread_quit_pipe
);
158 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
161 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
165 if (events
== NULL
|| size
== 0) {
170 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
176 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
188 * Check if the thread quit pipe was triggered.
190 * Return 1 if it was triggered else 0;
193 int check_thread_quit_pipe(int fd
, uint32_t events
)
195 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
203 * Create and init socket from uri.
206 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
209 struct lttcomm_sock
*sock
= NULL
;
211 sock
= lttcomm_alloc_sock_from_uri(uri
);
213 ERR("Allocating socket");
217 ret
= lttcomm_create_sock(sock
);
221 DBG("Listening on sock %d for live", sock
->fd
);
223 ret
= sock
->ops
->bind(sock
);
228 ret
= sock
->ops
->listen(sock
, -1);
238 lttcomm_destroy_sock(sock
);
244 * This thread manages the listening for new connections on the network
247 void *thread_listener(void *data
)
249 int i
, ret
, pollfd
, err
= -1;
251 uint32_t revents
, nb_fd
;
252 struct lttng_poll_event events
;
253 struct lttcomm_sock
*live_control_sock
;
255 DBG("[thread] Relay live listener started");
257 live_control_sock
= init_socket(live_uri
);
258 if (!live_control_sock
) {
259 goto error_sock_control
;
263 * Pass 3 as size here for the thread quit pipe, control and data socket.
265 ret
= create_thread_poll_set(&events
, 2);
267 goto error_create_poll
;
270 /* Add the control socket */
271 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
277 DBG("Listener accepting live viewers connections");
280 ret
= lttng_poll_wait(&events
, -1);
283 * Restart interrupted system call.
285 if (errno
== EINTR
) {
292 DBG("Relay new viewer connection received");
293 for (i
= 0; i
< nb_fd
; i
++) {
294 /* Fetch once the poll data */
295 revents
= LTTNG_POLL_GETEV(&events
, i
);
296 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
298 /* Thread quit pipe has been closed. Killing thread. */
299 ret
= check_thread_quit_pipe(pollfd
, revents
);
305 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
306 ERR("socket poll error");
308 } else if (revents
& LPOLLIN
) {
310 * Get allocated in this thread, enqueued to a global queue,
311 * dequeued and freed in the worker thread.
313 struct relay_command
*relay_cmd
;
314 struct lttcomm_sock
*newsock
;
316 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
318 PERROR("relay command zmalloc");
322 assert(pollfd
== live_control_sock
->fd
);
323 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
325 PERROR("accepting control sock");
329 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
330 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
333 PERROR("setsockopt inet");
334 lttcomm_destroy_sock(newsock
);
338 relay_cmd
->sock
= newsock
;
341 * Lock free enqueue the request.
343 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
346 * Wake the dispatch queue futex. Implicit memory
347 * barrier with the exchange in cds_wfq_enqueue.
349 futex_nto1_wake(&viewer_cmd_queue
.futex
);
357 lttng_poll_clean(&events
);
359 if (live_control_sock
->fd
>= 0) {
360 ret
= live_control_sock
->ops
->close(live_control_sock
);
365 lttcomm_destroy_sock(live_control_sock
);
368 DBG("Live viewer listener thread exited with error");
370 DBG("Live viewer listener thread cleanup complete");
376 * This thread manages the dispatching of the requests to worker threads
379 void *thread_dispatcher(void *data
)
382 struct cds_wfq_node
*node
;
383 struct relay_command
*relay_cmd
= NULL
;
385 DBG("[thread] Live viewer relay dispatcher started");
387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
388 /* Atomically prepare the queue futex */
389 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
392 /* Dequeue commands */
393 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
395 DBG("Woken up but nothing in the live-viewer "
396 "relay command queue");
397 /* Continue thread execution */
401 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
402 DBG("Dispatching viewer request waiting on sock %d",
403 relay_cmd
->sock
->fd
);
406 * Inform worker thread of the new request. This call is blocking
407 * so we can be assured that the data will be read at some point in
408 * time or wait to the end of the world :)
411 ret
= write(live_relay_cmd_pipe
[1], relay_cmd
,
413 } while (ret
< 0 && errno
== EINTR
);
415 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
416 PERROR("write cmd pipe");
419 } while (node
!= NULL
);
421 /* Futex wait on queue. Blocking call on futex() */
422 futex_nto1_wait(&viewer_cmd_queue
.futex
);
426 DBG("Live viewer dispatch thread dying");
432 * Establish connection with the viewer and check the versions.
434 * Return 0 on success or else negative value.
437 int viewer_connect(struct relay_command
*cmd
)
440 struct lttng_viewer_connect reply
, msg
;
444 cmd
->version_check_done
= 1;
446 /* Get version from the other side. */
447 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
448 if (ret
< 0 || ret
!= sizeof(msg
)) {
450 /* Orderly shutdown. Not necessary to print an error. */
451 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
453 ERR("Relay failed to receive the version values.");
459 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
460 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
462 /* Major versions must be the same */
463 if (reply
.major
!= be32toh(msg
.major
)) {
464 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
470 cmd
->major
= reply
.major
;
471 /* We adapt to the lowest compatible version */
472 if (reply
.minor
<= be32toh(msg
.minor
)) {
473 cmd
->minor
= reply
.minor
;
475 cmd
->minor
= be32toh(msg
.minor
);
478 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
479 cmd
->type
= RELAY_VIEWER_COMMAND
;
480 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
481 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
483 ERR("Unknown connection type : %u", be32toh(msg
.type
));
488 reply
.major
= htobe32(reply
.major
);
489 reply
.minor
= htobe32(reply
.minor
);
490 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
491 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
493 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
494 sizeof(struct lttng_viewer_connect
), 0);
496 ERR("Relay sending version");
499 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
507 * Send the viewer the list of current sessions.
509 * Return 0 on success or else a negative value.
512 int viewer_list_sessions(struct relay_command
*cmd
,
513 struct lttng_ht
*sessions_ht
)
516 struct lttng_viewer_list_sessions session_list
;
518 long approx_before
, approx_after
;
519 struct lttng_ht_node_ulong
*node
;
520 struct lttng_ht_iter iter
;
521 struct lttng_viewer_session send_session
;
522 struct relay_session
*session
;
524 DBG("List sessions received");
526 if (cmd
->version_check_done
== 0) {
527 ERR("Trying to list sessions before version check");
533 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
534 session_list
.sessions_count
= htobe32(count
);
536 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
537 sizeof(session_list
), 0);
539 ERR("Relay sending sessions list");
543 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
544 node
= lttng_ht_iter_get_node_ulong(&iter
);
548 session
= caa_container_of(node
, struct relay_session
, session_n
);
550 strncpy(send_session
.session_name
, session
->session_name
,
551 sizeof(send_session
.session_name
));
552 strncpy(send_session
.hostname
, session
->hostname
,
553 sizeof(send_session
.hostname
));
554 send_session
.id
= htobe64(session
->id
);
555 send_session
.live_timer
= htobe32(session
->live_timer
);
556 send_session
.clients
= htobe32(session
->viewer_attached
);
558 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
559 sizeof(send_session
), 0);
561 ERR("Relay sending session info");
578 * Allocate and init a new viewer_stream.
580 * Copies the values from the stream passed in parameter and insert the new
581 * stream in the viewer_streams_ht.
583 * MUST be called with rcu_read_lock held.
585 * Returns 0 on success or a negative value on error.
588 int init_viewer_stream(struct relay_stream
*stream
,
589 struct lttng_ht
*viewer_streams_ht
)
592 struct relay_viewer_stream
*viewer_stream
;
595 assert(viewer_streams_ht
);
597 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
598 if (!viewer_stream
) {
599 PERROR("relay viewer stream zmalloc");
604 viewer_stream
->read_fd
= -1;
605 viewer_stream
->index_read_fd
= -1;
606 viewer_stream
->session_id
= stream
->session
->id
;
607 viewer_stream
->stream_handle
= stream
->stream_handle
;
608 viewer_stream
->path_name
= strndup(stream
->path_name
,
609 LTTNG_VIEWER_PATH_MAX
);
610 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
611 LTTNG_VIEWER_NAME_MAX
);
612 viewer_stream
->total_index_received
= stream
->total_index_received
;
613 viewer_stream
->tracefile_size
= stream
->tracefile_size
;
614 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
615 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
618 * This is to avoid a race between the initialization of this object and
619 * the close of the given stream. If the stream is unable to find this
620 * viewer stream when closing, this copy will at least take the latest
623 viewer_stream
->total_index_received
= stream
->total_index_received
;
626 * The deletion of this ctf_trace object is only done in a call RCU of the
627 * relay stream making it valid as long as we have the read side lock.
629 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
630 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
632 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
633 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
642 * Send the viewer the list of current sessions.
645 int viewer_attach_session(struct relay_command
*cmd
,
646 struct lttng_ht
*sessions_ht
,
647 struct lttng_ht
*viewer_streams_ht
)
649 int ret
, send_streams
= 0, nb_streams
= 0;
650 struct lttng_viewer_attach_session_request request
;
651 struct lttng_viewer_attach_session_response response
;
652 struct lttng_viewer_stream send_stream
;
653 struct relay_stream
*stream
;
654 struct relay_viewer_stream
*viewer_stream
;
655 struct lttng_ht_node_ulong
*node
;
656 struct lttng_ht_node_u64
*node64
;
657 struct lttng_ht_iter iter
;
658 struct relay_session
*session
;
662 assert(viewer_streams_ht
);
664 DBG("Attach session received");
666 if (cmd
->version_check_done
== 0) {
667 ERR("Trying to attach session before version check");
672 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
673 if (ret
< 0 || ret
!= sizeof(request
)) {
675 /* Orderly shutdown. Not necessary to print an error. */
676 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
678 ERR("Relay failed to receive the attach parameters.");
685 lttng_ht_lookup(sessions_ht
,
686 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
687 node
= lttng_ht_iter_get_node_ulong(&iter
);
689 DBG("Relay session %" PRIu64
" not found",
690 be64toh(request
.session_id
));
691 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
695 session
= caa_container_of(node
, struct relay_session
, session_n
);
696 if (cmd
->session
== session
) {
697 /* Same viewer already attached, just send the stream list. */
699 response
.status
= htobe32(VIEWER_ATTACH_OK
);
700 } else if (session
->viewer_attached
!= 0) {
701 DBG("Already a viewer attached");
702 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
704 } else if (session
->live_timer
== 0) {
705 DBG("Not live session");
706 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
709 session
->viewer_attached
++;
711 response
.status
= htobe32(VIEWER_ATTACH_OK
);
712 cmd
->session
= session
;
715 switch (be32toh(request
.seek
)) {
716 case VIEWER_SEEK_BEGINNING
:
717 /* Default behaviour. */
719 case VIEWER_SEEK_LAST
:
723 ERR("Wrong seek parameter");
724 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
730 /* We should only be there if we have a session to attach to. */
734 * Fill the viewer_streams_ht to count the number of streams
735 * ready to be sent and avoid concurrency issues on the
736 * relay_streams_ht and don't rely on a total session stream count.
738 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
739 struct relay_viewer_stream
*vstream
;
741 node
= lttng_ht_iter_get_node_ulong(&iter
);
745 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
746 if (stream
->session
!= cmd
->session
) {
751 * Don't send streams with no ctf_trace, they are not ready to be
754 if (!stream
->ctf_trace
) {
758 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
,
761 ret
= init_viewer_stream(stream
, viewer_streams_ht
);
768 response
.streams_count
= htobe32(nb_streams
);
772 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
774 ERR("Relay sending viewer attach response");
779 * Unknown or busy session, just return gracefully, the viewer knows what
787 /* We should only be there if we have a session to attach to. */
789 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
790 node64
= lttng_ht_iter_get_node_u64(&iter
);
794 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
796 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
800 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
801 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
802 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
803 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
804 sizeof(send_stream
.path_name
));
805 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
806 sizeof(send_stream
.channel_name
));
808 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
809 sizeof(send_stream
), 0);
811 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
814 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
826 * Open index file using a given viewer stream.
828 * Return 0 on success or else a negative value.
830 static int open_index(struct relay_viewer_stream
*stream
)
833 char fullpath
[PATH_MAX
];
834 struct lttng_packet_index_file_hdr hdr
;
836 if (stream
->tracefile_size
> 0) {
837 /* For now we don't support on-disk ring buffer. */
841 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
842 "/%s" DEFAULT_INDEX_FILE_SUFFIX
,
843 stream
->path_name
, stream
->channel_name
);
845 PERROR("snprintf index path");
850 DBG("Opening index file %s in read only", fullpath
);
851 ret
= open(fullpath
, O_RDONLY
);
853 if (errno
== ENOENT
) {
857 PERROR("opening index in read-only");
861 stream
->index_read_fd
= ret
;
862 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
865 ret
= read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
866 } while (ret
< 0 && errno
== EINTR
);
868 PERROR("Reading index header");
871 if (strncmp(hdr
.magic
, INDEX_MAGIC
, sizeof(hdr
.magic
)) != 0) {
872 ERR("Invalid header magic");
876 if (be32toh(hdr
.index_major
) != INDEX_MAJOR
||
877 be32toh(hdr
.index_minor
) != INDEX_MINOR
) {
878 ERR("Invalid header version");
890 * Get viewer stream from stream id.
892 * RCU read side lock MUST be acquired.
894 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
,
895 struct lttng_ht
*viewer_streams_ht
)
897 struct lttng_ht_node_u64
*node
;
898 struct lttng_ht_iter iter
;
899 struct relay_viewer_stream
*stream
= NULL
;
901 assert(viewer_streams_ht
);
903 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
904 node
= lttng_ht_iter_get_node_u64(&iter
);
906 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
909 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
916 * Send the next index for a stream.
918 * Return 0 on success or else a negative value.
921 int viewer_get_next_index(struct relay_command
*cmd
,
922 struct lttng_ht
*viewer_streams_ht
, struct lttng_ht
*sessions_ht
)
925 struct lttng_viewer_get_next_index request_index
;
926 struct lttng_viewer_index viewer_index
;
927 struct lttng_packet_index packet_index
;
928 struct relay_viewer_stream
*vstream
;
929 struct relay_stream
*rstream
;
932 assert(viewer_streams_ht
);
935 DBG("Viewer get next index");
937 if (cmd
->version_check_done
== 0) {
938 ERR("Trying to request index before version check");
943 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
944 sizeof(request_index
), 0);
945 if (ret
< 0 || ret
!= sizeof(request_index
)) {
947 ERR("Relay didn't receive the whole packet");
952 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
),
959 memset(&viewer_index
, 0, sizeof(viewer_index
));
962 * The viewer should not ask for index on metadata stream.
964 if (vstream
->metadata_flag
) {
965 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
969 /* First time, we open the index file */
970 if (vstream
->index_read_fd
< 0) {
971 ret
= open_index(vstream
);
974 * The index is created only when the first data packet arrives, it
975 * might not be ready at the beginning of the session
977 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
979 } else if (ret
< 0) {
980 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
985 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
987 if (rstream
->beacon_ts_end
!= -1ULL &&
988 vstream
->last_sent_index
== rstream
->total_index_received
) {
989 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
990 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
994 if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
995 /* No new index to send, retry later. */
996 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
999 } else if (!rstream
&&
1000 vstream
->total_index_received
== vstream
->last_sent_index
) {
1001 /* Last index sent and stream closed */
1002 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1006 if (!vstream
->ctf_trace
->metadata_received
||
1007 vstream
->ctf_trace
->metadata_received
>
1008 vstream
->ctf_trace
->metadata_sent
) {
1009 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1013 ret
= read(vstream
->index_read_fd
, &packet_index
,
1014 sizeof(packet_index
));
1015 } while (ret
< 0 && errno
== EINTR
);
1016 if (ret
< sizeof(packet_index
)) {
1017 PERROR("Relay reading index file");
1018 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1020 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1021 vstream
->last_sent_index
++;
1025 * Indexes are stored in big endian, no need to switch before sending.
1027 viewer_index
.offset
= packet_index
.offset
;
1028 viewer_index
.packet_size
= packet_index
.packet_size
;
1029 viewer_index
.content_size
= packet_index
.content_size
;
1030 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1031 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1032 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1033 viewer_index
.stream_id
= packet_index
.stream_id
;
1036 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1037 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1038 sizeof(viewer_index
), 0);
1040 ERR("Relay index to viewer");
1044 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1045 vstream
->last_sent_index
, vstream
->stream_handle
);
1056 * Send the next index for a stream
1058 * Return 0 on success or else a negative value.
1061 int viewer_get_packet(struct relay_command
*cmd
,
1062 struct lttng_ht
*viewer_streams_ht
)
1064 int ret
, send_data
= 0;
1068 struct lttng_viewer_get_packet get_packet_info
;
1069 struct lttng_viewer_trace_packet reply
;
1070 struct relay_viewer_stream
*stream
;
1073 assert(viewer_streams_ht
);
1075 DBG2("Relay get data packet");
1077 if (cmd
->version_check_done
== 0) {
1078 ERR("Trying to get packet before version check");
1083 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1084 sizeof(get_packet_info
), 0);
1085 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1087 ERR("Relay didn't receive the whole packet");
1092 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
),
1097 assert(stream
->ctf_trace
);
1100 * First time we read this stream, we need open the tracefile, we should
1101 * only arrive here if an index has already been sent to the viewer, so the
1102 * tracefile must exist, if it does not it is a fatal error.
1104 if (stream
->read_fd
< 0) {
1105 char fullpath
[PATH_MAX
];
1107 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1108 stream
->channel_name
);
1112 ret
= open(fullpath
, O_RDONLY
);
1114 PERROR("Relay opening trace file");
1117 stream
->read_fd
= ret
;
1120 memset(&reply
, 0, sizeof(reply
));
1122 if (!stream
->ctf_trace
->metadata_received
||
1123 stream
->ctf_trace
->metadata_received
>
1124 stream
->ctf_trace
->metadata_sent
) {
1125 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1126 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1131 len
= be32toh(get_packet_info
.len
);
1132 data
= zmalloc(len
);
1134 PERROR("relay data zmalloc");
1138 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1143 read_len
= read(stream
->read_fd
, data
, len
);
1144 if (read_len
< (ssize_t
) len
) {
1145 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1146 stream
->read_fd
, be64toh(get_packet_info
.offset
));
1149 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1150 reply
.len
= htobe32(len
);
1155 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1158 reply
.flags
= htobe32(reply
.flags
);
1159 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1161 ERR("Relay data header to viewer");
1166 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1168 ERR("Relay send data to viewer");
1173 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1174 be64toh(get_packet_info
.stream_id
));
1185 * Send the session's metadata
1187 * Return 0 on success else a negative value.
1190 int viewer_get_metadata(struct relay_command
*cmd
,
1191 struct lttng_ht
*viewer_streams_ht
)
1197 struct lttng_viewer_get_metadata request
;
1198 struct lttng_viewer_metadata_packet reply
;
1199 struct relay_viewer_stream
*stream
;
1202 assert(viewer_streams_ht
);
1204 DBG("Relay get metadata");
1206 if (cmd
->version_check_done
== 0) {
1207 ERR("Trying to get metadata before version check");
1212 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1213 sizeof(request
), 0);
1214 if (ret
< 0 || ret
!= sizeof(request
)) {
1216 ERR("Relay didn't receive the whole packet");
1221 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
),
1223 if (!stream
|| !stream
->metadata_flag
) {
1224 ERR("Invalid metadata stream");
1227 assert(stream
->ctf_trace
);
1228 assert(stream
->ctf_trace
->metadata_sent
<=
1229 stream
->ctf_trace
->metadata_received
);
1231 len
= stream
->ctf_trace
->metadata_received
-
1232 stream
->ctf_trace
->metadata_sent
;
1234 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1238 /* first time, we open the metadata file */
1239 if (stream
->read_fd
< 0) {
1240 char fullpath
[PATH_MAX
];
1242 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1243 stream
->channel_name
);
1247 ret
= open(fullpath
, O_RDONLY
);
1249 PERROR("Relay opening metadata file");
1252 stream
->read_fd
= ret
;
1255 reply
.len
= htobe64(len
);
1256 data
= zmalloc(len
);
1258 PERROR("viewer metadata zmalloc");
1262 read_len
= read(stream
->read_fd
, data
, len
);
1263 if (read_len
< (ssize_t
) len
) {
1264 PERROR("Relay reading metadata file");
1267 stream
->ctf_trace
->metadata_sent
+= read_len
;
1268 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1272 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1275 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1277 ERR("Relay data header to viewer");
1282 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1284 ERR("Relay send data to viewer");
1289 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1290 be64toh(request
.stream_id
));
1292 DBG("Metadata sent");
1302 * live_relay_unknown_command: send -1 if received unknown command
1305 void live_relay_unknown_command(struct relay_command
*cmd
)
1307 struct lttcomm_relayd_generic_reply reply
;
1310 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1311 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1312 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1314 ERR("Relay sending unknown command");
1319 * Process the commands received on the control socket
1322 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1323 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
,
1324 struct lttng_ht
*viewer_streams_ht
)
1328 switch (be32toh(recv_hdr
->cmd
)) {
1329 case VIEWER_CONNECT
:
1330 ret
= viewer_connect(cmd
);
1332 case VIEWER_LIST_SESSIONS
:
1333 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1335 case VIEWER_ATTACH_SESSION
:
1336 ret
= viewer_attach_session(cmd
, sessions_ht
,
1339 case VIEWER_GET_NEXT_INDEX
:
1340 ret
= viewer_get_next_index(cmd
, viewer_streams_ht
, sessions_ht
);
1342 case VIEWER_GET_PACKET
:
1343 ret
= viewer_get_packet(cmd
, viewer_streams_ht
);
1345 case VIEWER_GET_METADATA
:
1346 ret
= viewer_get_metadata(cmd
, viewer_streams_ht
);
1349 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1350 live_relay_unknown_command(cmd
);
1360 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1366 lttng_poll_del(events
, pollfd
);
1368 ret
= close(pollfd
);
1370 ERR("Closing pollfd %d", pollfd
);
1375 * Create and add connection to the given hash table.
1377 * Return poll add value or else -1 on error.
1380 int add_connection(int fd
, struct lttng_poll_event
*events
,
1381 struct lttng_ht
*relay_connections_ht
)
1384 struct relay_command
*relay_connection
;
1387 assert(relay_connections_ht
);
1389 relay_connection
= zmalloc(sizeof(struct relay_command
));
1390 if (relay_connection
== NULL
) {
1391 PERROR("Relay command zmalloc");
1396 ret
= read(fd
, relay_connection
, sizeof(*relay_connection
));
1397 } while (ret
< 0 && errno
== EINTR
);
1398 if (ret
< 0 || ret
< sizeof(*relay_connection
)) {
1399 PERROR("read relay cmd pipe");
1403 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1404 (unsigned long) relay_connection
->sock
->fd
);
1406 lttng_ht_add_unique_ulong(relay_connections_ht
,
1407 &relay_connection
->sock_n
);
1410 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1411 LPOLLIN
| LPOLLRDHUP
);
1414 free(relay_connection
);
1420 void deferred_free_connection(struct rcu_head
*head
)
1422 struct relay_command
*relay_connection
=
1423 caa_container_of(head
, struct relay_command
, rcu_node
);
1425 if (relay_connection
->session
&&
1426 relay_connection
->session
->viewer_attached
> 0) {
1427 relay_connection
->session
->viewer_attached
--;
1429 lttcomm_destroy_sock(relay_connection
->sock
);
1430 free(relay_connection
);
1434 void deferred_free_viewer_stream(struct rcu_head
*head
)
1436 struct relay_viewer_stream
*stream
=
1437 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1439 if (stream
->ctf_trace
) {
1440 uatomic_dec(&stream
->ctf_trace
->refcount
);
1441 assert(uatomic_read(&stream
->ctf_trace
->refcount
) >= 0);
1442 if (uatomic_read(&stream
->ctf_trace
->refcount
) == 0) {
1443 DBG("Freeing ctf_trace %" PRIu64
, stream
->ctf_trace
->id
);
1444 free(stream
->ctf_trace
);
1448 free(stream
->path_name
);
1449 free(stream
->channel_name
);
1454 void viewer_del_streams(struct lttng_ht
*viewer_streams_ht
,
1455 struct relay_session
*session
)
1458 struct relay_viewer_stream
*stream
;
1459 struct lttng_ht_node_u64
*node
;
1460 struct lttng_ht_iter iter
;
1462 assert(viewer_streams_ht
);
1466 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1467 node
= lttng_ht_iter_get_node_u64(&iter
);
1472 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1473 if (stream
->session_id
!= session
->id
) {
1477 if (stream
->read_fd
> 0) {
1478 ret
= close(stream
->read_fd
);
1480 PERROR("close read_fd");
1483 if (stream
->index_read_fd
> 0) {
1484 ret
= close(stream
->index_read_fd
);
1486 PERROR("close index_read_fd");
1489 if (stream
->metadata_flag
&& stream
->ctf_trace
) {
1490 stream
->ctf_trace
->metadata_sent
= 0;
1492 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1494 call_rcu(&stream
->rcu_node
, deferred_free_viewer_stream
);
1500 * Delete and free a connection.
1502 * RCU read side lock MUST be acquired.
1505 void del_connection(struct lttng_ht
*relay_connections_ht
,
1506 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
,
1507 struct lttng_ht
*viewer_streams_ht
)
1511 assert(relay_connections_ht
);
1513 assert(relay_connection
);
1514 assert(viewer_streams_ht
);
1516 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1519 if (relay_connection
->session
) {
1520 viewer_del_streams(viewer_streams_ht
, relay_connection
->session
);
1523 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1527 * This thread does the actual work
1530 void *thread_worker(void *data
)
1534 struct relay_command
*relay_connection
;
1535 struct lttng_poll_event events
;
1536 struct lttng_ht
*relay_connections_ht
;
1537 struct lttng_ht_node_ulong
*node
;
1538 struct lttng_ht_iter iter
;
1539 struct lttng_viewer_cmd recv_hdr
;
1540 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1541 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1542 struct lttng_ht
*viewer_streams_ht
= relay_ctx
->viewer_streams_ht
;
1544 DBG("[thread] Live viewer relay worker started");
1546 rcu_register_thread();
1548 /* table of connections indexed on socket */
1549 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1550 if (!relay_connections_ht
) {
1551 goto relay_connections_ht_error
;
1554 ret
= create_thread_poll_set(&events
, 2);
1556 goto error_poll_create
;
1559 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1568 /* Infinite blocking call, waiting for transmission */
1569 DBG3("Relayd live viewer worker thread polling...");
1570 ret
= lttng_poll_wait(&events
, -1);
1573 * Restart interrupted system call.
1575 if (errno
== EINTR
) {
1584 * Process control. The control connection is prioritised so we don't
1585 * starve it with high throughput tracing data on the data
1588 for (i
= 0; i
< nb_fd
; i
++) {
1589 /* Fetch once the poll data */
1590 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1591 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1593 /* Thread quit pipe has been closed. Killing thread. */
1594 ret
= check_thread_quit_pipe(pollfd
, revents
);
1600 /* Inspect the relay cmd pipe for new connection */
1601 if (pollfd
== live_relay_cmd_pipe
[0]) {
1602 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1603 ERR("Relay live pipe error");
1605 } else if (revents
& LPOLLIN
) {
1606 DBG("Relay live viewer command received");
1607 ret
= add_connection(live_relay_cmd_pipe
[0],
1608 &events
, relay_connections_ht
);
1613 } else if (revents
) {
1615 lttng_ht_lookup(relay_connections_ht
,
1616 (void *)((unsigned long) pollfd
), &iter
);
1617 node
= lttng_ht_iter_get_node_ulong(&iter
);
1619 DBG2("Relay viewer sock %d not found", pollfd
);
1623 relay_connection
= caa_container_of(node
, struct relay_command
,
1626 if (revents
& (LPOLLERR
)) {
1627 ERR("VIEWER POLL ERROR");
1628 cleanup_poll_connection(&events
, pollfd
);
1629 del_connection(relay_connections_ht
, &iter
,
1630 relay_connection
, viewer_streams_ht
);
1631 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1632 DBG("Viewer socket %d hung up", pollfd
);
1633 cleanup_poll_connection(&events
, pollfd
);
1634 del_connection(relay_connections_ht
, &iter
,
1635 relay_connection
, viewer_streams_ht
);
1636 } else if (revents
& LPOLLIN
) {
1637 ret
= relay_connection
->sock
->ops
->recvmsg(
1638 relay_connection
->sock
, &recv_hdr
,
1639 sizeof(struct lttng_viewer_cmd
),
1641 /* connection closed */
1643 cleanup_poll_connection(&events
, pollfd
);
1644 del_connection( relay_connections_ht
, &iter
,
1645 relay_connection
, viewer_streams_ht
);
1646 DBG("Viewer control connection closed with %d",
1649 if (relay_connection
->session
) {
1650 DBG2("Relay viewer worker receiving data for "
1651 "session: %" PRIu64
,
1652 relay_connection
->session
->id
);
1654 ret
= process_control(&recv_hdr
, relay_connection
,
1655 sessions_ht
, viewer_streams_ht
);
1657 /* Clear the session on error. */
1658 cleanup_poll_connection(&events
, pollfd
);
1659 del_connection(relay_connections_ht
, &iter
,
1660 relay_connection
, viewer_streams_ht
);
1661 DBG("Viewer connection closed with %d", pollfd
);
1672 lttng_poll_clean(&events
);
1674 /* empty the hash table and free the memory */
1676 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1677 node
= lttng_ht_iter_get_node_ulong(&iter
);
1682 relay_connection
= caa_container_of(node
, struct relay_command
,
1684 del_connection(relay_connections_ht
, &iter
, relay_connection
,
1689 lttng_ht_destroy(relay_connections_ht
);
1690 relay_connections_ht_error
:
1691 /* Close relay cmd pipes */
1692 utils_close_pipe(live_relay_cmd_pipe
);
1694 DBG("Viewer worker thread exited with error");
1696 DBG("Viewer worker thread cleanup complete");
1698 rcu_unregister_thread();
1703 * Create the relay command pipe to wake thread_manage_apps.
1704 * Closed in cleanup().
1706 static int create_relay_cmd_pipe(void)
1710 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1715 void live_stop_threads()
1722 ret
= pthread_join(live_listener_thread
, &status
);
1724 PERROR("pthread_join live listener");
1725 goto error
; /* join error, exit without cleanup */
1728 ret
= pthread_join(live_worker_thread
, &status
);
1730 PERROR("pthread_join live worker");
1731 goto error
; /* join error, exit without cleanup */
1734 ret
= pthread_join(live_dispatcher_thread
, &status
);
1736 PERROR("pthread_join live dispatcher");
1737 goto error
; /* join error, exit without cleanup */
1749 int live_start_threads(struct lttng_uri
*uri
,
1750 struct relay_local_data
*relay_ctx
)
1759 /* Create thread quit pipe */
1760 if ((ret
= init_thread_quit_pipe()) < 0) {
1764 /* Check if daemon is UID = 0 */
1765 is_root
= !getuid();
1768 if (live_uri
->port
< 1024) {
1769 ERR("Need to be root to use ports < 1024");
1775 /* Setup the thread apps communication pipe. */
1776 if ((ret
= create_relay_cmd_pipe()) < 0) {
1780 /* Init relay command queue. */
1781 cds_wfq_init(&viewer_cmd_queue
.queue
);
1783 /* Set up max poll set size */
1784 lttng_poll_set_max_size();
1786 /* Setup the dispatcher thread */
1787 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1788 thread_dispatcher
, (void *) NULL
);
1790 PERROR("pthread_create viewer dispatcher");
1791 goto exit_dispatcher
;
1794 /* Setup the worker thread */
1795 ret
= pthread_create(&live_worker_thread
, NULL
,
1796 thread_worker
, relay_ctx
);
1798 PERROR("pthread_create viewer worker");
1802 /* Setup the listener thread */
1803 ret
= pthread_create(&live_listener_thread
, NULL
,
1804 thread_listener
, (void *) NULL
);
1806 PERROR("pthread_create viewer listener");
1814 ret
= pthread_join(live_listener_thread
, &status
);
1816 PERROR("pthread_join live listener");
1817 goto error
; /* join error, exit without cleanup */
1821 ret
= pthread_join(live_worker_thread
, &status
);
1823 PERROR("pthread_join live worker");
1824 goto error
; /* join error, exit without cleanup */
1828 ret
= pthread_join(live_dispatcher_thread
, &status
);
1830 PERROR("pthread_join live dispatcher");
1831 goto error
; /* join error, exit without cleanup */