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"
59 #include "health-relayd.h"
61 static struct lttng_uri
*live_uri
;
64 * Quit pipe for all threads. This permits a single cancellation point
65 * for all threads when receiving an event on the pipe.
67 static int live_thread_quit_pipe
[2] = { -1, -1 };
70 * This pipe is used to inform the worker thread that a command is queued and
71 * ready to be processed.
73 static int live_relay_cmd_pipe
[2] = { -1, -1 };
75 /* Shared between threads */
76 static int live_dispatch_thread_exit
;
78 static pthread_t live_listener_thread
;
79 static pthread_t live_dispatcher_thread
;
80 static pthread_t live_worker_thread
;
83 * Relay command queue.
85 * The live_thread_listener and live_thread_dispatcher communicate with this
88 static struct relay_cmd_queue viewer_cmd_queue
;
90 static uint64_t last_relay_viewer_session_id
;
104 * Write to writable pipe used to notify a thread.
107 int notify_thread_pipe(int wpipe
)
111 ret
= lttng_write(wpipe
, "!", 1);
113 PERROR("write poll pipe");
120 * Stop all threads by closing the thread quit pipe.
123 void stop_threads(void)
127 /* Stopping all threads */
128 DBG("Terminating all live threads");
129 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
131 ERR("write error on thread quit pipe");
134 /* Dispatch thread */
135 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
136 futex_nto1_wake(&viewer_cmd_queue
.futex
);
140 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
143 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
147 if (events
== NULL
|| size
== 0) {
152 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
158 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
170 * Check if the thread quit pipe was triggered.
172 * Return 1 if it was triggered else 0;
175 int check_thread_quit_pipe(int fd
, uint32_t events
)
177 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
185 * Create and init socket from uri.
188 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
191 struct lttcomm_sock
*sock
= NULL
;
193 sock
= lttcomm_alloc_sock_from_uri(uri
);
195 ERR("Allocating socket");
199 ret
= lttcomm_create_sock(sock
);
203 DBG("Listening on sock %d for live", sock
->fd
);
205 ret
= sock
->ops
->bind(sock
);
210 ret
= sock
->ops
->listen(sock
, -1);
220 lttcomm_destroy_sock(sock
);
226 * This thread manages the listening for new connections on the network
229 void *thread_listener(void *data
)
231 int i
, ret
, pollfd
, err
= -1;
233 uint32_t revents
, nb_fd
;
234 struct lttng_poll_event events
;
235 struct lttcomm_sock
*live_control_sock
;
237 DBG("[thread] Relay live listener started");
239 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
241 health_code_update();
243 live_control_sock
= init_socket(live_uri
);
244 if (!live_control_sock
) {
245 goto error_sock_control
;
249 * Pass 3 as size here for the thread quit pipe, control and data socket.
251 ret
= create_thread_poll_set(&events
, 2);
253 goto error_create_poll
;
256 /* Add the control socket */
257 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
263 health_code_update();
265 DBG("Listener accepting live viewers connections");
269 ret
= lttng_poll_wait(&events
, -1);
273 * Restart interrupted system call.
275 if (errno
== EINTR
) {
282 DBG("Relay new viewer connection received");
283 for (i
= 0; i
< nb_fd
; i
++) {
284 health_code_update();
286 /* Fetch once the poll data */
287 revents
= LTTNG_POLL_GETEV(&events
, i
);
288 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
290 /* Thread quit pipe has been closed. Killing thread. */
291 ret
= check_thread_quit_pipe(pollfd
, revents
);
297 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
298 ERR("socket poll error");
300 } else if (revents
& LPOLLIN
) {
302 * Get allocated in this thread, enqueued to a global queue,
303 * dequeued and freed in the worker thread.
305 struct relay_command
*relay_cmd
;
306 struct lttcomm_sock
*newsock
;
308 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
310 PERROR("relay command zmalloc");
314 assert(pollfd
== live_control_sock
->fd
);
315 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
317 PERROR("accepting control sock");
321 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
322 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
325 PERROR("setsockopt inet");
326 lttcomm_destroy_sock(newsock
);
330 relay_cmd
->sock
= newsock
;
333 * Lock free enqueue the request.
335 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
338 * Wake the dispatch queue futex. Implicit memory
339 * barrier with the exchange in cds_wfq_enqueue.
341 futex_nto1_wake(&viewer_cmd_queue
.futex
);
349 lttng_poll_clean(&events
);
351 if (live_control_sock
->fd
>= 0) {
352 ret
= live_control_sock
->ops
->close(live_control_sock
);
357 lttcomm_destroy_sock(live_control_sock
);
361 DBG("Live viewer listener thread exited with error");
363 health_unregister(health_relayd
);
364 DBG("Live viewer listener thread cleanup complete");
370 * This thread manages the dispatching of the requests to worker threads
373 void *thread_dispatcher(void *data
)
377 struct cds_wfq_node
*node
;
378 struct relay_command
*relay_cmd
= NULL
;
380 DBG("[thread] Live viewer relay dispatcher started");
382 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
384 health_code_update();
386 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
387 health_code_update();
389 /* Atomically prepare the queue futex */
390 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
393 health_code_update();
395 /* Dequeue commands */
396 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
398 DBG("Woken up but nothing in the live-viewer "
399 "relay command queue");
400 /* Continue thread execution */
404 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
405 DBG("Dispatching viewer request waiting on sock %d",
406 relay_cmd
->sock
->fd
);
409 * Inform worker thread of the new request. This call is blocking
410 * so we can be assured that the data will be read at some point in
411 * time or wait to the end of the world :)
413 ret
= lttng_write(live_relay_cmd_pipe
[1], relay_cmd
,
416 if (ret
< sizeof(struct relay_command
)) {
417 PERROR("write cmd pipe");
420 } while (node
!= NULL
);
422 /* Futex wait on queue. Blocking call on futex() */
424 futex_nto1_wait(&viewer_cmd_queue
.futex
);
428 /* Normal exit, no error */
434 ERR("Health error occurred in %s", __func__
);
436 health_unregister(health_relayd
);
437 DBG("Live viewer dispatch thread dying");
443 * Establish connection with the viewer and check the versions.
445 * Return 0 on success or else negative value.
448 int viewer_connect(struct relay_command
*cmd
)
451 struct lttng_viewer_connect reply
, msg
;
455 cmd
->version_check_done
= 1;
457 health_code_update();
459 /* Get version from the other side. */
460 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
461 if (ret
< 0 || ret
!= sizeof(msg
)) {
463 /* Orderly shutdown. Not necessary to print an error. */
464 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
466 ERR("Relay failed to receive the version values.");
472 health_code_update();
474 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
475 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
477 /* Major versions must be the same */
478 if (reply
.major
!= be32toh(msg
.major
)) {
479 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
485 cmd
->major
= reply
.major
;
486 /* We adapt to the lowest compatible version */
487 if (reply
.minor
<= be32toh(msg
.minor
)) {
488 cmd
->minor
= reply
.minor
;
490 cmd
->minor
= be32toh(msg
.minor
);
493 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
494 cmd
->type
= RELAY_VIEWER_COMMAND
;
495 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
496 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
498 ERR("Unknown connection type : %u", be32toh(msg
.type
));
503 reply
.major
= htobe32(reply
.major
);
504 reply
.minor
= htobe32(reply
.minor
);
505 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
506 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
509 health_code_update();
511 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
512 sizeof(struct lttng_viewer_connect
), 0);
514 ERR("Relay sending version");
517 health_code_update();
519 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
527 * Send the viewer the list of current sessions.
529 * Return 0 on success or else a negative value.
532 int viewer_list_sessions(struct relay_command
*cmd
,
533 struct lttng_ht
*sessions_ht
)
536 struct lttng_viewer_list_sessions session_list
;
538 long approx_before
, approx_after
;
539 struct lttng_ht_node_ulong
*node
;
540 struct lttng_ht_iter iter
;
541 struct lttng_viewer_session send_session
;
542 struct relay_session
*session
;
544 DBG("List sessions received");
546 if (cmd
->version_check_done
== 0) {
547 ERR("Trying to list sessions before version check");
553 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
554 session_list
.sessions_count
= htobe32(count
);
556 health_code_update();
558 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
559 sizeof(session_list
), 0);
561 ERR("Relay sending sessions list");
565 health_code_update();
567 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
568 health_code_update();
570 node
= lttng_ht_iter_get_node_ulong(&iter
);
574 session
= caa_container_of(node
, struct relay_session
, session_n
);
576 strncpy(send_session
.session_name
, session
->session_name
,
577 sizeof(send_session
.session_name
));
578 strncpy(send_session
.hostname
, session
->hostname
,
579 sizeof(send_session
.hostname
));
580 send_session
.id
= htobe64(session
->id
);
581 send_session
.live_timer
= htobe32(session
->live_timer
);
582 send_session
.clients
= htobe32(session
->viewer_attached
);
583 send_session
.streams
= htobe32(session
->stream_count
);
585 health_code_update();
587 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
588 sizeof(send_session
), 0);
590 ERR("Relay sending session info");
594 health_code_update();
609 * Open index file using a given viewer stream.
611 * Return 0 on success or else a negative value.
613 static int open_index(struct relay_viewer_stream
*stream
)
616 char fullpath
[PATH_MAX
];
617 struct ctf_packet_index_file_hdr hdr
;
619 if (stream
->tracefile_count
> 0) {
620 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
621 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, stream
->path_name
,
622 stream
->channel_name
, stream
->tracefile_count_current
);
624 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
625 DEFAULT_INDEX_FILE_SUFFIX
, stream
->path_name
,
626 stream
->channel_name
);
629 PERROR("snprintf index path");
633 DBG("Opening index file %s in read only", fullpath
);
634 ret
= open(fullpath
, O_RDONLY
);
636 if (errno
== ENOENT
) {
640 PERROR("opening index in read-only");
644 stream
->index_read_fd
= ret
;
645 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
647 ret
= lttng_read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
648 if (ret
< sizeof(hdr
)) {
649 PERROR("Reading index header");
652 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
653 ERR("Invalid header magic");
657 if (be32toh(hdr
.index_major
) != CTF_INDEX_MAJOR
||
658 be32toh(hdr
.index_minor
) != CTF_INDEX_MINOR
) {
659 ERR("Invalid header version");
670 * Allocate and init a new viewer_stream.
672 * Copies the values from the stream passed in parameter and insert the new
673 * stream in the viewer_streams_ht.
675 * MUST be called with rcu_read_lock held.
677 * Returns 0 on success or a negative value on error.
680 int init_viewer_stream(struct relay_stream
*stream
, int seek_last
)
683 struct relay_viewer_stream
*viewer_stream
;
687 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
688 if (!viewer_stream
) {
689 PERROR("relay viewer stream zmalloc");
693 viewer_stream
->session_id
= stream
->session
->id
;
694 viewer_stream
->stream_handle
= stream
->stream_handle
;
695 viewer_stream
->path_name
= strndup(stream
->path_name
,
696 LTTNG_VIEWER_PATH_MAX
);
697 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
698 LTTNG_VIEWER_NAME_MAX
);
699 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
700 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
701 viewer_stream
->tracefile_count_last
= -1ULL;
703 viewer_stream
->tracefile_count_current
=
704 stream
->tracefile_count_current
;
706 viewer_stream
->tracefile_count_current
=
707 stream
->oldest_tracefile_id
;
710 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
711 if (viewer_stream
->metadata_flag
) {
712 viewer_stream
->ctf_trace
->viewer_metadata_stream
=
715 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
717 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
718 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
720 viewer_stream
->index_read_fd
= -1;
721 viewer_stream
->read_fd
= -1;
724 * This is to avoid a race between the initialization of this object and
725 * the close of the given stream. If the stream is unable to find this
726 * viewer stream when closing, this copy will at least take the latest
728 * We also need that for the seek_last.
730 viewer_stream
->total_index_received
= stream
->total_index_received
;
733 * If we never received an index for the current stream, delay
734 * the opening of the index, otherwise open it right now.
736 if (viewer_stream
->tracefile_count_current
==
737 stream
->tracefile_count_current
&&
738 viewer_stream
->total_index_received
== 0) {
739 viewer_stream
->index_read_fd
= -1;
741 ret
= open_index(viewer_stream
);
747 if (seek_last
&& viewer_stream
->index_read_fd
> 0) {
748 ret
= lseek(viewer_stream
->index_read_fd
,
749 viewer_stream
->total_index_received
*
750 sizeof(struct ctf_packet_index
),
755 viewer_stream
->last_sent_index
=
756 viewer_stream
->total_index_received
;
766 * Rotate a stream to the next tracefile.
768 * Returns 0 on success, 1 on EOF, a negative value on error.
771 int rotate_viewer_stream(struct relay_viewer_stream
*viewer_stream
,
772 struct relay_stream
*stream
)
775 uint64_t tracefile_id
;
777 assert(viewer_stream
);
779 tracefile_id
= (viewer_stream
->tracefile_count_current
+ 1) %
780 viewer_stream
->tracefile_count
;
782 * Detect the last tracefile to open.
784 if (viewer_stream
->tracefile_count_last
!= -1ULL &&
785 viewer_stream
->tracefile_count_last
==
786 viewer_stream
->tracefile_count_current
) {
792 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
795 * The writer and the reader are not working in the same
796 * tracefile, we can read up to EOF, we don't care about the
797 * total_index_received.
799 if (!stream
|| (stream
->tracefile_count_current
!= tracefile_id
)) {
800 viewer_stream
->close_write_flag
= 1;
803 * We are opening a file that is still open in write, make
804 * sure we limit our reading to the number of indexes
807 viewer_stream
->close_write_flag
= 0;
809 viewer_stream
->total_index_received
=
810 stream
->total_index_received
;
813 viewer_stream
->tracefile_count_current
= tracefile_id
;
815 ret
= close(viewer_stream
->index_read_fd
);
817 PERROR("close index file %d",
818 viewer_stream
->index_read_fd
);
820 viewer_stream
->index_read_fd
= -1;
821 ret
= close(viewer_stream
->read_fd
);
823 PERROR("close tracefile %d",
824 viewer_stream
->read_fd
);
826 viewer_stream
->read_fd
= -1;
828 pthread_mutex_lock(&viewer_stream
->overwrite_lock
);
829 viewer_stream
->abort_flag
= 0;
830 pthread_mutex_unlock(&viewer_stream
->overwrite_lock
);
832 viewer_stream
->index_read_fd
= -1;
833 viewer_stream
->read_fd
= -1;
836 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
838 ret
= open_index(viewer_stream
);
851 * Send the viewer the list of current sessions.
854 int viewer_attach_session(struct relay_command
*cmd
,
855 struct lttng_ht
*sessions_ht
)
857 int ret
, send_streams
= 0;
858 uint32_t nb_streams
= 0, nb_streams_ready
= 0;
859 struct lttng_viewer_attach_session_request request
;
860 struct lttng_viewer_attach_session_response response
;
861 struct lttng_viewer_stream send_stream
;
862 struct relay_stream
*stream
;
863 struct relay_viewer_stream
*viewer_stream
;
864 struct lttng_ht_node_ulong
*node
;
865 struct lttng_ht_node_u64
*node64
;
866 struct lttng_ht_iter iter
;
867 struct relay_session
*session
;
873 DBG("Attach session received");
875 if (cmd
->version_check_done
== 0) {
876 ERR("Trying to attach session before version check");
881 health_code_update();
883 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
884 if (ret
< 0 || ret
!= sizeof(request
)) {
886 /* Orderly shutdown. Not necessary to print an error. */
887 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
889 ERR("Relay failed to receive the attach parameters.");
895 health_code_update();
898 lttng_ht_lookup(sessions_ht
,
899 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
900 node
= lttng_ht_iter_get_node_ulong(&iter
);
902 DBG("Relay session %" PRIu64
" not found",
903 be64toh(request
.session_id
));
904 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
908 session
= caa_container_of(node
, struct relay_session
, session_n
);
909 if (cmd
->session_id
== session
->id
) {
910 /* Same viewer already attached, just send the stream list. */
912 response
.status
= htobe32(VIEWER_ATTACH_OK
);
913 } else if (session
->viewer_attached
!= 0) {
914 DBG("Already a viewer attached");
915 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
917 } else if (session
->live_timer
== 0) {
918 DBG("Not live session");
919 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
922 session
->viewer_attached
++;
924 response
.status
= htobe32(VIEWER_ATTACH_OK
);
925 cmd
->session_id
= session
->id
;
926 cmd
->session
= session
;
929 switch (be32toh(request
.seek
)) {
930 case VIEWER_SEEK_BEGINNING
:
931 /* Default behaviour. */
933 case VIEWER_SEEK_LAST
:
937 ERR("Wrong seek parameter");
938 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
944 /* We should only be there if we have a session to attach to. */
948 * Fill the viewer_streams_ht to count the number of streams
949 * ready to be sent and avoid concurrency issues on the
950 * relay_streams_ht and don't rely on a total session stream count.
952 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
953 struct relay_viewer_stream
*vstream
;
955 health_code_update();
957 node
= lttng_ht_iter_get_node_ulong(&iter
);
961 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
962 if (stream
->session
!= cmd
->session
) {
968 * Don't send streams with no ctf_trace, they are not
971 if (!stream
->ctf_trace
|| !stream
->viewer_ready
) {
976 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
978 ret
= init_viewer_stream(stream
, seek_last
);
985 /* We must have the same amount of existing stream and ready stream. */
986 if (nb_streams
!= nb_streams_ready
) {
989 response
.streams_count
= htobe32(nb_streams
);
993 health_code_update();
994 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
996 ERR("Relay sending viewer attach response");
999 health_code_update();
1002 * Unknown or empty session, just return gracefully, the viewer knows what
1005 if (!send_streams
|| !nb_streams
) {
1010 /* We should only be there if we have a session to attach to. */
1012 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1013 health_code_update();
1015 node64
= lttng_ht_iter_get_node_u64(&iter
);
1019 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
1021 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
1025 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
1026 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
1027 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
1028 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
1029 sizeof(send_stream
.path_name
));
1030 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
1031 sizeof(send_stream
.channel_name
));
1033 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
1034 sizeof(send_stream
), 0);
1036 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
1039 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
1051 * Get viewer stream from stream id.
1053 * RCU read side lock MUST be acquired.
1055 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
1057 struct lttng_ht_node_u64
*node
;
1058 struct lttng_ht_iter iter
;
1059 struct relay_viewer_stream
*stream
= NULL
;
1061 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
1062 node
= lttng_ht_iter_get_node_u64(&iter
);
1064 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
1067 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1074 void deferred_free_viewer_stream(struct rcu_head
*head
)
1076 struct relay_viewer_stream
*stream
=
1077 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1079 free(stream
->path_name
);
1080 free(stream
->channel_name
);
1085 void delete_viewer_stream(struct relay_viewer_stream
*vstream
)
1088 struct lttng_ht_iter iter
;
1090 iter
.iter
.node
= &vstream
->stream_n
.node
;
1091 delret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1096 void destroy_viewer_stream(struct relay_viewer_stream
*vstream
)
1098 unsigned long ret_ref
;
1102 ret_ref
= uatomic_add_return(&vstream
->ctf_trace
->refcount
, -1);
1103 assert(ret_ref
>= 0);
1105 if (vstream
->read_fd
>= 0) {
1106 ret
= close(vstream
->read_fd
);
1108 PERROR("close read_fd");
1111 if (vstream
->index_read_fd
>= 0) {
1112 ret
= close(vstream
->index_read_fd
);
1114 PERROR("close index_read_fd");
1119 * If the only stream left in the HT is the metadata stream,
1120 * we need to remove it because we won't detect a EOF for this
1123 if (ret_ref
== 1 && vstream
->ctf_trace
->viewer_metadata_stream
) {
1124 destroy_viewer_stream(vstream
->ctf_trace
->viewer_metadata_stream
);
1125 vstream
->ctf_trace
->metadata_stream
= NULL
;
1126 DBG("Freeing ctf_trace %" PRIu64
, vstream
->ctf_trace
->id
);
1128 * The streaming-side is already closed and we can't receive a new
1129 * stream concurrently at this point (since the session is being
1130 * destroyed), so when we detect the refcount equals 0, we are the
1131 * only owners of the ctf_trace and we can free it ourself.
1133 free(vstream
->ctf_trace
);
1136 call_rcu(&vstream
->rcu_node
, deferred_free_viewer_stream
);
1140 * Send the next index for a stream.
1142 * Return 0 on success or else a negative value.
1145 int viewer_get_next_index(struct relay_command
*cmd
,
1146 struct lttng_ht
*sessions_ht
)
1149 struct lttng_viewer_get_next_index request_index
;
1150 struct lttng_viewer_index viewer_index
;
1151 struct ctf_packet_index packet_index
;
1152 struct relay_viewer_stream
*vstream
;
1153 struct relay_stream
*rstream
;
1156 assert(sessions_ht
);
1158 DBG("Viewer get next index");
1160 if (cmd
->version_check_done
== 0) {
1161 ERR("Trying to request index before version check");
1163 goto end_no_session
;
1166 health_code_update();
1167 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
1168 sizeof(request_index
), 0);
1169 if (ret
< 0 || ret
!= sizeof(request_index
)) {
1171 ERR("Relay didn't receive the whole packet");
1174 health_code_update();
1177 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
1183 memset(&viewer_index
, 0, sizeof(viewer_index
));
1186 * The viewer should not ask for index on metadata stream.
1188 if (vstream
->metadata_flag
) {
1189 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1193 /* First time, we open the index file */
1194 if (vstream
->index_read_fd
< 0) {
1195 ret
= open_index(vstream
);
1196 if (ret
== -ENOENT
) {
1198 * The index is created only when the first data packet arrives, it
1199 * might not be ready at the beginning of the session
1201 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1203 } else if (ret
< 0) {
1204 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1209 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
1211 if (vstream
->abort_flag
) {
1212 /* Rotate on abort (overwrite). */
1213 DBG("Viewer rotate because of overwrite");
1214 ret
= rotate_viewer_stream(vstream
, rstream
);
1217 } else if (ret
== 1) {
1218 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1219 delete_viewer_stream(vstream
);
1220 destroy_viewer_stream(vstream
);
1224 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1225 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1226 if (rstream
->beacon_ts_end
!= -1ULL &&
1227 vstream
->last_sent_index
== rstream
->total_index_received
) {
1228 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
1229 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1230 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1233 * Reader and writer are working in the same tracefile, so we care
1234 * about the number of index received and sent. Otherwise, we read
1237 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1238 && !vstream
->close_write_flag
) {
1239 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1240 /* No new index to send, retry later. */
1241 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1245 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1246 } else if (!rstream
&& vstream
->close_write_flag
&&
1247 vstream
->total_index_received
== vstream
->last_sent_index
) {
1248 /* Last index sent and current tracefile closed in write */
1249 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1250 delete_viewer_stream(vstream
);
1251 destroy_viewer_stream(vstream
);
1254 vstream
->close_write_flag
= 1;
1257 if (!vstream
->ctf_trace
->metadata_received
||
1258 vstream
->ctf_trace
->metadata_received
>
1259 vstream
->ctf_trace
->metadata_sent
) {
1260 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1263 pthread_mutex_lock(&vstream
->overwrite_lock
);
1264 if (vstream
->abort_flag
) {
1266 * The file is being overwritten by the writer, we cannot
1269 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1270 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1271 ret
= rotate_viewer_stream(vstream
, rstream
);
1274 } else if (ret
== 1) {
1275 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1276 delete_viewer_stream(vstream
);
1277 destroy_viewer_stream(vstream
);
1282 ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1283 sizeof(packet_index
));
1284 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1285 if (ret
< sizeof(packet_index
)) {
1287 * The tracefile is closed in write, so we read up to EOF.
1289 if (vstream
->close_write_flag
== 1) {
1290 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1291 /* Rotate on normal EOF */
1292 ret
= rotate_viewer_stream(vstream
, rstream
);
1295 } else if (ret
== 1) {
1296 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1297 delete_viewer_stream(vstream
);
1298 destroy_viewer_stream(vstream
);
1302 PERROR("Relay reading index file %d",
1303 vstream
->index_read_fd
);
1304 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1308 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1309 vstream
->last_sent_index
++;
1313 * Indexes are stored in big endian, no need to switch before sending.
1315 viewer_index
.offset
= packet_index
.offset
;
1316 viewer_index
.packet_size
= packet_index
.packet_size
;
1317 viewer_index
.content_size
= packet_index
.content_size
;
1318 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1319 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1320 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1321 viewer_index
.stream_id
= packet_index
.stream_id
;
1324 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1325 health_code_update();
1326 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1327 sizeof(viewer_index
), 0);
1329 ERR("Relay index to viewer");
1332 health_code_update();
1334 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1335 vstream
->last_sent_index
, vstream
->stream_handle
);
1346 * Send the next index for a stream
1348 * Return 0 on success or else a negative value.
1351 int viewer_get_packet(struct relay_command
*cmd
)
1353 int ret
, send_data
= 0;
1357 struct lttng_viewer_get_packet get_packet_info
;
1358 struct lttng_viewer_trace_packet reply
;
1359 struct relay_viewer_stream
*stream
;
1363 DBG2("Relay get data packet");
1365 if (cmd
->version_check_done
== 0) {
1366 ERR("Trying to get packet before version check");
1371 health_code_update();
1372 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1373 sizeof(get_packet_info
), 0);
1374 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1376 ERR("Relay didn't receive the whole packet");
1379 health_code_update();
1381 /* From this point on, the error label can be reached. */
1382 memset(&reply
, 0, sizeof(reply
));
1385 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1389 assert(stream
->ctf_trace
);
1392 * First time we read this stream, we need open the tracefile, we should
1393 * only arrive here if an index has already been sent to the viewer, so the
1394 * tracefile must exist, if it does not it is a fatal error.
1396 if (stream
->read_fd
< 0) {
1397 char fullpath
[PATH_MAX
];
1399 if (stream
->tracefile_count
> 0) {
1400 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1401 stream
->channel_name
,
1402 stream
->tracefile_count_current
);
1404 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1405 stream
->channel_name
);
1410 ret
= open(fullpath
, O_RDONLY
);
1412 PERROR("Relay opening trace file");
1415 stream
->read_fd
= ret
;
1418 if (!stream
->ctf_trace
->metadata_received
||
1419 stream
->ctf_trace
->metadata_received
>
1420 stream
->ctf_trace
->metadata_sent
) {
1421 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1422 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1426 len
= be32toh(get_packet_info
.len
);
1427 data
= zmalloc(len
);
1429 PERROR("relay data zmalloc");
1433 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1436 * If the read fd was closed by the streaming side, the
1437 * abort_flag will be set to 1, otherwise it is an error.
1439 if (stream
->abort_flag
== 0) {
1443 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1446 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1447 if (read_len
< len
) {
1449 * If the read fd was closed by the streaming side, the
1450 * abort_flag will be set to 1, otherwise it is an error.
1452 if (stream
->abort_flag
== 0) {
1453 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1455 be64toh(get_packet_info
.offset
));
1458 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1462 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1463 reply
.len
= htobe32(len
);
1468 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1471 reply
.flags
= htobe32(reply
.flags
);
1473 health_code_update();
1474 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1476 ERR("Relay data header to viewer");
1479 health_code_update();
1482 health_code_update();
1483 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1485 ERR("Relay send data to viewer");
1488 health_code_update();
1491 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1492 be64toh(get_packet_info
.stream_id
));
1503 * Send the session's metadata
1505 * Return 0 on success else a negative value.
1508 int viewer_get_metadata(struct relay_command
*cmd
)
1514 struct lttng_viewer_get_metadata request
;
1515 struct lttng_viewer_metadata_packet reply
;
1516 struct relay_viewer_stream
*stream
;
1520 DBG("Relay get metadata");
1522 if (cmd
->version_check_done
== 0) {
1523 ERR("Trying to get metadata before version check");
1528 health_code_update();
1529 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1530 sizeof(request
), 0);
1531 if (ret
< 0 || ret
!= sizeof(request
)) {
1533 ERR("Relay didn't receive the whole packet");
1536 health_code_update();
1539 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1540 if (!stream
|| !stream
->metadata_flag
) {
1541 ERR("Invalid metadata stream");
1544 assert(stream
->ctf_trace
);
1545 assert(stream
->ctf_trace
->metadata_sent
<=
1546 stream
->ctf_trace
->metadata_received
);
1548 len
= stream
->ctf_trace
->metadata_received
-
1549 stream
->ctf_trace
->metadata_sent
;
1551 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1555 /* first time, we open the metadata file */
1556 if (stream
->read_fd
< 0) {
1557 char fullpath
[PATH_MAX
];
1559 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1560 stream
->channel_name
);
1564 ret
= open(fullpath
, O_RDONLY
);
1566 PERROR("Relay opening metadata file");
1569 stream
->read_fd
= ret
;
1572 reply
.len
= htobe64(len
);
1573 data
= zmalloc(len
);
1575 PERROR("viewer metadata zmalloc");
1579 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1580 if (read_len
< len
) {
1581 PERROR("Relay reading metadata file");
1584 stream
->ctf_trace
->metadata_sent
+= read_len
;
1585 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1589 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1592 health_code_update();
1593 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1595 ERR("Relay data header to viewer");
1598 health_code_update();
1601 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1603 ERR("Relay send data to viewer");
1608 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1609 be64toh(request
.stream_id
));
1611 DBG("Metadata sent");
1621 * live_relay_unknown_command: send -1 if received unknown command
1624 void live_relay_unknown_command(struct relay_command
*cmd
)
1626 struct lttcomm_relayd_generic_reply reply
;
1629 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1630 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1631 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1633 ERR("Relay sending unknown command");
1638 * Process the commands received on the control socket
1641 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1642 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1646 switch (be32toh(recv_hdr
->cmd
)) {
1647 case VIEWER_CONNECT
:
1648 ret
= viewer_connect(cmd
);
1650 case VIEWER_LIST_SESSIONS
:
1651 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1653 case VIEWER_ATTACH_SESSION
:
1654 ret
= viewer_attach_session(cmd
, sessions_ht
);
1656 case VIEWER_GET_NEXT_INDEX
:
1657 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1659 case VIEWER_GET_PACKET
:
1660 ret
= viewer_get_packet(cmd
);
1662 case VIEWER_GET_METADATA
:
1663 ret
= viewer_get_metadata(cmd
);
1666 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1667 live_relay_unknown_command(cmd
);
1677 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1683 lttng_poll_del(events
, pollfd
);
1685 ret
= close(pollfd
);
1687 ERR("Closing pollfd %d", pollfd
);
1692 * Create and add connection to the given hash table.
1694 * Return poll add value or else -1 on error.
1697 int add_connection(int fd
, struct lttng_poll_event
*events
,
1698 struct lttng_ht
*relay_connections_ht
)
1701 struct relay_command
*relay_connection
;
1704 assert(relay_connections_ht
);
1706 relay_connection
= zmalloc(sizeof(struct relay_command
));
1707 if (relay_connection
== NULL
) {
1708 PERROR("Relay command zmalloc");
1712 ret
= lttng_read(fd
, relay_connection
, sizeof(*relay_connection
));
1713 if (ret
< sizeof(*relay_connection
)) {
1714 PERROR("read relay cmd pipe");
1718 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1719 (unsigned long) relay_connection
->sock
->fd
);
1721 lttng_ht_add_unique_ulong(relay_connections_ht
,
1722 &relay_connection
->sock_n
);
1725 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1726 LPOLLIN
| LPOLLRDHUP
);
1729 free(relay_connection
);
1735 void deferred_free_connection(struct rcu_head
*head
)
1737 struct relay_command
*relay_connection
=
1738 caa_container_of(head
, struct relay_command
, rcu_node
);
1740 if (relay_connection
->session
&&
1741 relay_connection
->session
->viewer_attached
> 0) {
1742 relay_connection
->session
->viewer_attached
--;
1744 lttcomm_destroy_sock(relay_connection
->sock
);
1745 free(relay_connection
);
1749 * Delete all streams for a specific session ID.
1752 void viewer_del_streams(uint64_t session_id
)
1754 struct relay_viewer_stream
*stream
;
1755 struct lttng_ht_iter iter
;
1758 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
1760 health_code_update();
1762 if (stream
->session_id
!= session_id
) {
1766 delete_viewer_stream(stream
);
1767 assert(stream
->ctf_trace
);
1769 if (stream
->metadata_flag
) {
1771 * The metadata viewer stream is destroyed once the refcount on the
1772 * ctf trace goes to 0 in the destroy stream function thus there is
1773 * no explicit call to that function here.
1775 stream
->ctf_trace
->metadata_sent
= 0;
1776 stream
->ctf_trace
->viewer_metadata_stream
= NULL
;
1778 destroy_viewer_stream(stream
);
1785 * Delete and free a connection.
1787 * RCU read side lock MUST be acquired.
1790 void del_connection(struct lttng_ht
*relay_connections_ht
,
1791 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1795 assert(relay_connections_ht
);
1797 assert(relay_connection
);
1799 DBG("Cleaning connection of session ID %" PRIu64
,
1800 relay_connection
->session_id
);
1802 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1805 viewer_del_streams(relay_connection
->session_id
);
1807 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1811 * This thread does the actual work
1814 void *thread_worker(void *data
)
1818 struct relay_command
*relay_connection
;
1819 struct lttng_poll_event events
;
1820 struct lttng_ht
*relay_connections_ht
;
1821 struct lttng_ht_node_ulong
*node
;
1822 struct lttng_ht_iter iter
;
1823 struct lttng_viewer_cmd recv_hdr
;
1824 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1825 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1827 DBG("[thread] Live viewer relay worker started");
1829 rcu_register_thread();
1831 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1833 /* table of connections indexed on socket */
1834 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1835 if (!relay_connections_ht
) {
1836 goto relay_connections_ht_error
;
1839 ret
= create_thread_poll_set(&events
, 2);
1841 goto error_poll_create
;
1844 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1853 health_code_update();
1855 /* Infinite blocking call, waiting for transmission */
1856 DBG3("Relayd live viewer worker thread polling...");
1857 health_poll_entry();
1858 ret
= lttng_poll_wait(&events
, -1);
1862 * Restart interrupted system call.
1864 if (errno
== EINTR
) {
1873 * Process control. The control connection is prioritised so we don't
1874 * starve it with high throughput tracing data on the data
1877 for (i
= 0; i
< nb_fd
; i
++) {
1878 /* Fetch once the poll data */
1879 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1880 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1882 health_code_update();
1884 /* Thread quit pipe has been closed. Killing thread. */
1885 ret
= check_thread_quit_pipe(pollfd
, revents
);
1891 /* Inspect the relay cmd pipe for new connection */
1892 if (pollfd
== live_relay_cmd_pipe
[0]) {
1893 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1894 ERR("Relay live pipe error");
1896 } else if (revents
& LPOLLIN
) {
1897 DBG("Relay live viewer command received");
1898 ret
= add_connection(live_relay_cmd_pipe
[0],
1899 &events
, relay_connections_ht
);
1904 } else if (revents
) {
1906 lttng_ht_lookup(relay_connections_ht
,
1907 (void *)((unsigned long) pollfd
), &iter
);
1908 node
= lttng_ht_iter_get_node_ulong(&iter
);
1910 DBG2("Relay viewer sock %d not found", pollfd
);
1914 relay_connection
= caa_container_of(node
, struct relay_command
,
1917 if (revents
& (LPOLLERR
)) {
1918 cleanup_poll_connection(&events
, pollfd
);
1919 del_connection(relay_connections_ht
, &iter
,
1921 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1922 DBG("Viewer socket %d hung up", pollfd
);
1923 cleanup_poll_connection(&events
, pollfd
);
1924 del_connection(relay_connections_ht
, &iter
,
1926 } else if (revents
& LPOLLIN
) {
1927 ret
= relay_connection
->sock
->ops
->recvmsg(
1928 relay_connection
->sock
, &recv_hdr
,
1929 sizeof(struct lttng_viewer_cmd
),
1931 /* connection closed */
1933 cleanup_poll_connection(&events
, pollfd
);
1934 del_connection(relay_connections_ht
, &iter
,
1936 DBG("Viewer control connection closed with %d",
1939 if (relay_connection
->session
) {
1940 DBG2("Relay viewer worker receiving data for "
1941 "session: %" PRIu64
,
1942 relay_connection
->session
->id
);
1944 ret
= process_control(&recv_hdr
, relay_connection
,
1947 /* Clear the session on error. */
1948 cleanup_poll_connection(&events
, pollfd
);
1949 del_connection(relay_connections_ht
, &iter
,
1951 DBG("Viewer connection closed with %d", pollfd
);
1962 lttng_poll_clean(&events
);
1964 /* empty the hash table and free the memory */
1966 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1967 health_code_update();
1969 node
= lttng_ht_iter_get_node_ulong(&iter
);
1974 relay_connection
= caa_container_of(node
, struct relay_command
,
1976 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1980 lttng_ht_destroy(relay_connections_ht
);
1981 relay_connections_ht_error
:
1982 /* Close relay cmd pipes */
1983 utils_close_pipe(live_relay_cmd_pipe
);
1985 DBG("Viewer worker thread exited with error");
1987 DBG("Viewer worker thread cleanup complete");
1990 ERR("Health error occurred in %s", __func__
);
1992 health_unregister(health_relayd
);
1994 rcu_unregister_thread();
1999 * Create the relay command pipe to wake thread_manage_apps.
2000 * Closed in cleanup().
2002 static int create_relay_cmd_pipe(void)
2006 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
2011 void live_stop_threads(void)
2018 ret
= pthread_join(live_listener_thread
, &status
);
2020 PERROR("pthread_join live listener");
2021 goto error
; /* join error, exit without cleanup */
2024 ret
= pthread_join(live_worker_thread
, &status
);
2026 PERROR("pthread_join live worker");
2027 goto error
; /* join error, exit without cleanup */
2030 ret
= pthread_join(live_dispatcher_thread
, &status
);
2032 PERROR("pthread_join live dispatcher");
2033 goto error
; /* join error, exit without cleanup */
2045 int live_start_threads(struct lttng_uri
*uri
,
2046 struct relay_local_data
*relay_ctx
, int quit_pipe
[2])
2055 live_thread_quit_pipe
[0] = quit_pipe
[0];
2056 live_thread_quit_pipe
[1] = quit_pipe
[1];
2058 /* Check if daemon is UID = 0 */
2059 is_root
= !getuid();
2062 if (live_uri
->port
< 1024) {
2063 ERR("Need to be root to use ports < 1024");
2069 /* Setup the thread apps communication pipe. */
2070 if ((ret
= create_relay_cmd_pipe()) < 0) {
2074 /* Init relay command queue. */
2075 cds_wfq_init(&viewer_cmd_queue
.queue
);
2077 /* Set up max poll set size */
2078 lttng_poll_set_max_size();
2080 /* Setup the dispatcher thread */
2081 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2082 thread_dispatcher
, (void *) NULL
);
2084 PERROR("pthread_create viewer dispatcher");
2085 goto exit_dispatcher
;
2088 /* Setup the worker thread */
2089 ret
= pthread_create(&live_worker_thread
, NULL
,
2090 thread_worker
, relay_ctx
);
2092 PERROR("pthread_create viewer worker");
2096 /* Setup the listener thread */
2097 ret
= pthread_create(&live_listener_thread
, NULL
,
2098 thread_listener
, (void *) NULL
);
2100 PERROR("pthread_create viewer listener");
2108 ret
= pthread_join(live_listener_thread
, &status
);
2110 PERROR("pthread_join live listener");
2111 goto error
; /* join error, exit without cleanup */
2115 ret
= pthread_join(live_worker_thread
, &status
);
2117 PERROR("pthread_join live worker");
2118 goto error
; /* join error, exit without cleanup */
2122 ret
= pthread_join(live_dispatcher_thread
, &status
);
2124 PERROR("pthread_join live dispatcher");
2125 goto error
; /* join error, exit without cleanup */