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"
60 #include "testpoint.h"
62 static struct lttng_uri
*live_uri
;
65 * This pipe is used to inform the worker thread that a command is queued and
66 * ready to be processed.
68 static int live_relay_cmd_pipe
[2] = { -1, -1 };
70 /* Shared between threads */
71 static int live_dispatch_thread_exit
;
73 static pthread_t live_listener_thread
;
74 static pthread_t live_dispatcher_thread
;
75 static pthread_t live_worker_thread
;
78 * Relay command queue.
80 * The live_thread_listener and live_thread_dispatcher communicate with this
83 static struct relay_cmd_queue viewer_cmd_queue
;
85 static uint64_t last_relay_viewer_session_id
;
99 * Write to writable pipe used to notify a thread.
102 int notify_thread_pipe(int wpipe
)
106 ret
= lttng_write(wpipe
, "!", 1);
108 PERROR("write poll pipe");
115 * Stop all threads by closing the thread quit pipe.
118 void stop_threads(void)
122 /* Stopping all threads */
123 DBG("Terminating all live threads");
124 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
126 ERR("write error on thread quit pipe");
129 /* Dispatch thread */
130 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
131 futex_nto1_wake(&viewer_cmd_queue
.futex
);
135 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
138 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
142 if (events
== NULL
|| size
== 0) {
147 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
153 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
165 * Check if the thread quit pipe was triggered.
167 * Return 1 if it was triggered else 0;
170 int check_thread_quit_pipe(int fd
, uint32_t events
)
172 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
180 * Create and init socket from uri.
183 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
186 struct lttcomm_sock
*sock
= NULL
;
188 sock
= lttcomm_alloc_sock_from_uri(uri
);
190 ERR("Allocating socket");
194 ret
= lttcomm_create_sock(sock
);
198 DBG("Listening on sock %d for live", sock
->fd
);
200 ret
= sock
->ops
->bind(sock
);
205 ret
= sock
->ops
->listen(sock
, -1);
215 lttcomm_destroy_sock(sock
);
221 * This thread manages the listening for new connections on the network
224 void *thread_listener(void *data
)
226 int i
, ret
, pollfd
, err
= -1;
228 uint32_t revents
, nb_fd
;
229 struct lttng_poll_event events
;
230 struct lttcomm_sock
*live_control_sock
;
232 DBG("[thread] Relay live listener started");
234 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
236 health_code_update();
238 live_control_sock
= init_socket(live_uri
);
239 if (!live_control_sock
) {
240 goto error_sock_control
;
243 /* Pass 2 as size here for the thread quit pipe and control sockets. */
244 ret
= create_thread_poll_set(&events
, 2);
246 goto error_create_poll
;
249 /* Add the control socket */
250 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
255 lttng_relay_notify_ready();
257 if (testpoint(relayd_thread_live_listener
)) {
258 goto error_testpoint
;
262 health_code_update();
264 DBG("Listener accepting live viewers connections");
268 ret
= lttng_poll_wait(&events
, -1);
272 * Restart interrupted system call.
274 if (errno
== EINTR
) {
281 DBG("Relay new viewer connection received");
282 for (i
= 0; i
< nb_fd
; i
++) {
283 health_code_update();
285 /* Fetch once the poll data */
286 revents
= LTTNG_POLL_GETEV(&events
, i
);
287 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
289 /* Thread quit pipe has been closed. Killing thread. */
290 ret
= check_thread_quit_pipe(pollfd
, revents
);
296 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
297 ERR("socket poll error");
299 } else if (revents
& LPOLLIN
) {
301 * Get allocated in this thread, enqueued to a global queue,
302 * dequeued and freed in the worker thread.
304 struct relay_command
*relay_cmd
;
305 struct lttcomm_sock
*newsock
;
307 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
309 PERROR("relay command zmalloc");
313 assert(pollfd
== live_control_sock
->fd
);
314 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
316 PERROR("accepting control sock");
320 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
321 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
324 PERROR("setsockopt inet");
325 lttcomm_destroy_sock(newsock
);
329 relay_cmd
->sock
= newsock
;
332 * Lock free enqueue the request.
334 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
337 * Wake the dispatch queue futex. Implicit memory
338 * barrier with the exchange in cds_wfq_enqueue.
340 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 if (testpoint(relayd_thread_live_dispatcher
)) {
385 goto error_testpoint
;
388 health_code_update();
390 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
391 health_code_update();
393 /* Atomically prepare the queue futex */
394 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
397 health_code_update();
399 /* Dequeue commands */
400 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
402 DBG("Woken up but nothing in the live-viewer "
403 "relay command queue");
404 /* Continue thread execution */
408 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
409 DBG("Dispatching viewer request waiting on sock %d",
410 relay_cmd
->sock
->fd
);
413 * Inform worker thread of the new request. This call is blocking
414 * so we can be assured that the data will be read at some point in
415 * time or wait to the end of the world :)
417 ret
= lttng_write(live_relay_cmd_pipe
[1], relay_cmd
,
420 if (ret
< sizeof(struct relay_command
)) {
421 PERROR("write cmd pipe");
424 } while (node
!= NULL
);
426 /* Futex wait on queue. Blocking call on futex() */
428 futex_nto1_wait(&viewer_cmd_queue
.futex
);
432 /* Normal exit, no error */
439 ERR("Health error occurred in %s", __func__
);
441 health_unregister(health_relayd
);
442 DBG("Live viewer dispatch thread dying");
448 * Establish connection with the viewer and check the versions.
450 * Return 0 on success or else negative value.
453 int viewer_connect(struct relay_command
*cmd
)
456 struct lttng_viewer_connect reply
, msg
;
460 cmd
->version_check_done
= 1;
462 health_code_update();
464 /* Get version from the other side. */
465 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
466 if (ret
< 0 || ret
!= sizeof(msg
)) {
468 /* Orderly shutdown. Not necessary to print an error. */
469 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
471 ERR("Relay failed to receive the version values.");
477 health_code_update();
479 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
480 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
482 /* Major versions must be the same */
483 if (reply
.major
!= be32toh(msg
.major
)) {
484 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
490 cmd
->major
= reply
.major
;
491 /* We adapt to the lowest compatible version */
492 if (reply
.minor
<= be32toh(msg
.minor
)) {
493 cmd
->minor
= reply
.minor
;
495 cmd
->minor
= be32toh(msg
.minor
);
498 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
499 cmd
->type
= RELAY_VIEWER_COMMAND
;
500 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
501 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
503 ERR("Unknown connection type : %u", be32toh(msg
.type
));
508 reply
.major
= htobe32(reply
.major
);
509 reply
.minor
= htobe32(reply
.minor
);
510 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
511 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
514 health_code_update();
516 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
517 sizeof(struct lttng_viewer_connect
), 0);
519 ERR("Relay sending version");
522 health_code_update();
524 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
532 * Send the viewer the list of current sessions.
534 * Return 0 on success or else a negative value.
537 int viewer_list_sessions(struct relay_command
*cmd
,
538 struct lttng_ht
*sessions_ht
)
541 struct lttng_viewer_list_sessions session_list
;
543 long approx_before
, approx_after
;
544 struct lttng_ht_node_ulong
*node
;
545 struct lttng_ht_iter iter
;
546 struct lttng_viewer_session send_session
;
547 struct relay_session
*session
;
549 DBG("List sessions received");
551 if (cmd
->version_check_done
== 0) {
552 ERR("Trying to list sessions before version check");
558 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
559 session_list
.sessions_count
= htobe32(count
);
561 health_code_update();
563 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
564 sizeof(session_list
), 0);
566 ERR("Relay sending sessions list");
570 health_code_update();
572 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
573 health_code_update();
575 node
= lttng_ht_iter_get_node_ulong(&iter
);
579 session
= caa_container_of(node
, struct relay_session
, session_n
);
581 strncpy(send_session
.session_name
, session
->session_name
,
582 sizeof(send_session
.session_name
));
583 strncpy(send_session
.hostname
, session
->hostname
,
584 sizeof(send_session
.hostname
));
585 send_session
.id
= htobe64(session
->id
);
586 send_session
.live_timer
= htobe32(session
->live_timer
);
587 send_session
.clients
= htobe32(session
->viewer_attached
);
588 send_session
.streams
= htobe32(session
->stream_count
);
590 health_code_update();
592 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
593 sizeof(send_session
), 0);
595 ERR("Relay sending session info");
599 health_code_update();
614 * Open index file using a given viewer stream.
616 * Return 0 on success or else a negative value.
618 static int open_index(struct relay_viewer_stream
*stream
)
621 char fullpath
[PATH_MAX
];
622 struct ctf_packet_index_file_hdr hdr
;
624 if (stream
->tracefile_count
> 0) {
625 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s_%"
626 PRIu64 DEFAULT_INDEX_FILE_SUFFIX
, stream
->path_name
,
627 stream
->channel_name
, stream
->tracefile_count_current
);
629 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
"/%s"
630 DEFAULT_INDEX_FILE_SUFFIX
, stream
->path_name
,
631 stream
->channel_name
);
634 PERROR("snprintf index path");
638 DBG("Opening index file %s in read only", fullpath
);
639 ret
= open(fullpath
, O_RDONLY
);
641 if (errno
== ENOENT
) {
645 PERROR("opening index in read-only");
649 stream
->index_read_fd
= ret
;
650 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
652 ret
= lttng_read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
653 if (ret
< sizeof(hdr
)) {
654 PERROR("Reading index header");
657 if (be32toh(hdr
.magic
) != CTF_INDEX_MAGIC
) {
658 ERR("Invalid header magic");
662 if (be32toh(hdr
.index_major
) != CTF_INDEX_MAJOR
||
663 be32toh(hdr
.index_minor
) != CTF_INDEX_MINOR
) {
664 ERR("Invalid header version");
675 * Allocate and init a new viewer_stream.
677 * Copies the values from the stream passed in parameter and insert the new
678 * stream in the viewer_streams_ht.
680 * MUST be called with rcu_read_lock held.
682 * Returns 0 on success or a negative value on error.
685 int init_viewer_stream(struct relay_stream
*stream
, int seek_last
)
688 struct relay_viewer_stream
*viewer_stream
;
692 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
693 if (!viewer_stream
) {
694 PERROR("relay viewer stream zmalloc");
698 viewer_stream
->session_id
= stream
->session
->id
;
699 viewer_stream
->stream_handle
= stream
->stream_handle
;
700 viewer_stream
->path_name
= strndup(stream
->path_name
,
701 LTTNG_VIEWER_PATH_MAX
);
702 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
703 LTTNG_VIEWER_NAME_MAX
);
704 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
705 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
706 viewer_stream
->tracefile_count_last
= -1ULL;
708 viewer_stream
->tracefile_count_current
=
709 stream
->tracefile_count_current
;
711 viewer_stream
->tracefile_count_current
=
712 stream
->oldest_tracefile_id
;
715 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
716 if (viewer_stream
->metadata_flag
) {
717 viewer_stream
->ctf_trace
->viewer_metadata_stream
=
720 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
722 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
723 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
725 viewer_stream
->index_read_fd
= -1;
726 viewer_stream
->read_fd
= -1;
729 * This is to avoid a race between the initialization of this object and
730 * the close of the given stream. If the stream is unable to find this
731 * viewer stream when closing, this copy will at least take the latest
733 * We also need that for the seek_last.
735 viewer_stream
->total_index_received
= stream
->total_index_received
;
738 * If we never received an index for the current stream, delay
739 * the opening of the index, otherwise open it right now.
741 if (viewer_stream
->tracefile_count_current
==
742 stream
->tracefile_count_current
&&
743 viewer_stream
->total_index_received
== 0) {
744 viewer_stream
->index_read_fd
= -1;
746 ret
= open_index(viewer_stream
);
752 if (seek_last
&& viewer_stream
->index_read_fd
> 0) {
753 ret
= lseek(viewer_stream
->index_read_fd
,
754 viewer_stream
->total_index_received
*
755 sizeof(struct ctf_packet_index
),
760 viewer_stream
->last_sent_index
=
761 viewer_stream
->total_index_received
;
771 * Rotate a stream to the next tracefile.
773 * Returns 0 on success, 1 on EOF, a negative value on error.
776 int rotate_viewer_stream(struct relay_viewer_stream
*viewer_stream
,
777 struct relay_stream
*stream
)
780 uint64_t tracefile_id
;
782 assert(viewer_stream
);
784 tracefile_id
= (viewer_stream
->tracefile_count_current
+ 1) %
785 viewer_stream
->tracefile_count
;
787 * Detect the last tracefile to open.
789 if (viewer_stream
->tracefile_count_last
!= -1ULL &&
790 viewer_stream
->tracefile_count_last
==
791 viewer_stream
->tracefile_count_current
) {
797 pthread_mutex_lock(&stream
->viewer_stream_rotation_lock
);
800 * The writer and the reader are not working in the same
801 * tracefile, we can read up to EOF, we don't care about the
802 * total_index_received.
804 if (!stream
|| (stream
->tracefile_count_current
!= tracefile_id
)) {
805 viewer_stream
->close_write_flag
= 1;
808 * We are opening a file that is still open in write, make
809 * sure we limit our reading to the number of indexes
812 viewer_stream
->close_write_flag
= 0;
814 viewer_stream
->total_index_received
=
815 stream
->total_index_received
;
818 viewer_stream
->tracefile_count_current
= tracefile_id
;
820 ret
= close(viewer_stream
->index_read_fd
);
822 PERROR("close index file %d",
823 viewer_stream
->index_read_fd
);
825 viewer_stream
->index_read_fd
= -1;
826 ret
= close(viewer_stream
->read_fd
);
828 PERROR("close tracefile %d",
829 viewer_stream
->read_fd
);
831 viewer_stream
->read_fd
= -1;
833 pthread_mutex_lock(&viewer_stream
->overwrite_lock
);
834 viewer_stream
->abort_flag
= 0;
835 pthread_mutex_unlock(&viewer_stream
->overwrite_lock
);
837 viewer_stream
->index_read_fd
= -1;
838 viewer_stream
->read_fd
= -1;
841 pthread_mutex_unlock(&stream
->viewer_stream_rotation_lock
);
843 ret
= open_index(viewer_stream
);
856 * Send the viewer the list of current sessions.
859 int viewer_attach_session(struct relay_command
*cmd
,
860 struct lttng_ht
*sessions_ht
)
862 int ret
, send_streams
= 0;
863 uint32_t nb_streams
= 0, nb_streams_ready
= 0;
864 struct lttng_viewer_attach_session_request request
;
865 struct lttng_viewer_attach_session_response response
;
866 struct lttng_viewer_stream send_stream
;
867 struct relay_stream
*stream
;
868 struct relay_viewer_stream
*viewer_stream
;
869 struct lttng_ht_node_ulong
*node
;
870 struct lttng_ht_node_u64
*node64
;
871 struct lttng_ht_iter iter
;
872 struct relay_session
*session
;
878 DBG("Attach session received");
880 if (cmd
->version_check_done
== 0) {
881 ERR("Trying to attach session before version check");
886 health_code_update();
888 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
889 if (ret
< 0 || ret
!= sizeof(request
)) {
891 /* Orderly shutdown. Not necessary to print an error. */
892 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
894 ERR("Relay failed to receive the attach parameters.");
900 health_code_update();
903 lttng_ht_lookup(sessions_ht
,
904 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
905 node
= lttng_ht_iter_get_node_ulong(&iter
);
907 DBG("Relay session %" PRIu64
" not found",
908 be64toh(request
.session_id
));
909 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
913 session
= caa_container_of(node
, struct relay_session
, session_n
);
914 if (cmd
->session_id
== session
->id
) {
915 /* Same viewer already attached, just send the stream list. */
917 response
.status
= htobe32(VIEWER_ATTACH_OK
);
918 } else if (session
->viewer_attached
!= 0) {
919 DBG("Already a viewer attached");
920 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
922 } else if (session
->live_timer
== 0) {
923 DBG("Not live session");
924 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
927 session
->viewer_attached
++;
929 response
.status
= htobe32(VIEWER_ATTACH_OK
);
930 cmd
->session_id
= session
->id
;
931 cmd
->session
= session
;
934 switch (be32toh(request
.seek
)) {
935 case VIEWER_SEEK_BEGINNING
:
936 /* Default behaviour. */
938 case VIEWER_SEEK_LAST
:
942 ERR("Wrong seek parameter");
943 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
949 /* We should only be there if we have a session to attach to. */
953 * Fill the viewer_streams_ht to count the number of streams
954 * ready to be sent and avoid concurrency issues on the
955 * relay_streams_ht and don't rely on a total session stream count.
957 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
958 struct relay_viewer_stream
*vstream
;
960 health_code_update();
962 node
= lttng_ht_iter_get_node_ulong(&iter
);
966 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
967 if (stream
->session
!= cmd
->session
) {
973 * Don't send streams with no ctf_trace, they are not
976 if (!stream
->ctf_trace
|| !stream
->viewer_ready
) {
981 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
983 ret
= init_viewer_stream(stream
, seek_last
);
990 /* We must have the same amount of existing stream and ready stream. */
991 if (nb_streams
!= nb_streams_ready
) {
994 response
.streams_count
= htobe32(nb_streams
);
998 health_code_update();
999 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
1001 ERR("Relay sending viewer attach response");
1004 health_code_update();
1007 * Unknown or empty session, just return gracefully, the viewer knows what
1010 if (!send_streams
|| !nb_streams
) {
1015 /* We should only be there if we have a session to attach to. */
1017 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1018 health_code_update();
1020 node64
= lttng_ht_iter_get_node_u64(&iter
);
1024 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
1026 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
1030 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
1031 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
1032 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
1033 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
1034 sizeof(send_stream
.path_name
));
1035 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
1036 sizeof(send_stream
.channel_name
));
1038 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
1039 sizeof(send_stream
), 0);
1041 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
1044 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
1056 * Get viewer stream from stream id.
1058 * RCU read side lock MUST be acquired.
1060 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
1062 struct lttng_ht_node_u64
*node
;
1063 struct lttng_ht_iter iter
;
1064 struct relay_viewer_stream
*stream
= NULL
;
1066 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
1067 node
= lttng_ht_iter_get_node_u64(&iter
);
1069 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
1072 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1079 void deferred_free_viewer_stream(struct rcu_head
*head
)
1081 struct relay_viewer_stream
*stream
=
1082 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1084 free(stream
->path_name
);
1085 free(stream
->channel_name
);
1090 void delete_viewer_stream(struct relay_viewer_stream
*vstream
)
1093 struct lttng_ht_iter iter
;
1095 iter
.iter
.node
= &vstream
->stream_n
.node
;
1096 delret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1101 void destroy_viewer_stream(struct relay_viewer_stream
*vstream
)
1103 unsigned long ret_ref
;
1107 ret_ref
= uatomic_add_return(&vstream
->ctf_trace
->refcount
, -1);
1108 assert(ret_ref
>= 0);
1110 if (vstream
->read_fd
>= 0) {
1111 ret
= close(vstream
->read_fd
);
1113 PERROR("close read_fd");
1116 if (vstream
->index_read_fd
>= 0) {
1117 ret
= close(vstream
->index_read_fd
);
1119 PERROR("close index_read_fd");
1124 * If the only stream left in the HT is the metadata stream,
1125 * we need to remove it because we won't detect a EOF for this
1128 if (ret_ref
== 1 && vstream
->ctf_trace
->viewer_metadata_stream
) {
1129 delete_viewer_stream(vstream
->ctf_trace
->viewer_metadata_stream
);
1130 destroy_viewer_stream(vstream
->ctf_trace
->viewer_metadata_stream
);
1131 vstream
->ctf_trace
->metadata_stream
= NULL
;
1132 DBG("Freeing ctf_trace %" PRIu64
, vstream
->ctf_trace
->id
);
1134 * The streaming-side is already closed and we can't receive a new
1135 * stream concurrently at this point (since the session is being
1136 * destroyed), so when we detect the refcount equals 0, we are the
1137 * only owners of the ctf_trace and we can free it ourself.
1139 free(vstream
->ctf_trace
);
1142 call_rcu(&vstream
->rcu_node
, deferred_free_viewer_stream
);
1146 * Send the next index for a stream.
1148 * Return 0 on success or else a negative value.
1151 int viewer_get_next_index(struct relay_command
*cmd
,
1152 struct lttng_ht
*sessions_ht
)
1155 struct lttng_viewer_get_next_index request_index
;
1156 struct lttng_viewer_index viewer_index
;
1157 struct ctf_packet_index packet_index
;
1158 struct relay_viewer_stream
*vstream
;
1159 struct relay_stream
*rstream
;
1162 assert(sessions_ht
);
1164 DBG("Viewer get next index");
1166 if (cmd
->version_check_done
== 0) {
1167 ERR("Trying to request index before version check");
1169 goto end_no_session
;
1172 health_code_update();
1173 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
1174 sizeof(request_index
), 0);
1175 if (ret
< 0 || ret
!= sizeof(request_index
)) {
1177 ERR("Relay didn't receive the whole packet");
1180 health_code_update();
1183 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
1189 memset(&viewer_index
, 0, sizeof(viewer_index
));
1192 * The viewer should not ask for index on metadata stream.
1194 if (vstream
->metadata_flag
) {
1195 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1199 /* First time, we open the index file */
1200 if (vstream
->index_read_fd
< 0) {
1201 ret
= open_index(vstream
);
1202 if (ret
== -ENOENT
) {
1204 * The index is created only when the first data packet arrives, it
1205 * might not be ready at the beginning of the session
1207 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1209 } else if (ret
< 0) {
1210 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1215 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
1217 if (vstream
->abort_flag
) {
1218 /* Rotate on abort (overwrite). */
1219 DBG("Viewer rotate because of overwrite");
1220 ret
= rotate_viewer_stream(vstream
, rstream
);
1223 } else if (ret
== 1) {
1224 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1225 delete_viewer_stream(vstream
);
1226 destroy_viewer_stream(vstream
);
1230 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1231 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1232 if (rstream
->beacon_ts_end
!= -1ULL &&
1233 vstream
->last_sent_index
== rstream
->total_index_received
) {
1234 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
1235 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1236 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1239 * Reader and writer are working in the same tracefile, so we care
1240 * about the number of index received and sent. Otherwise, we read
1243 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1244 && !vstream
->close_write_flag
) {
1245 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1246 /* No new index to send, retry later. */
1247 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1251 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1252 } else if (!rstream
&& vstream
->close_write_flag
&&
1253 vstream
->total_index_received
== vstream
->last_sent_index
) {
1254 /* Last index sent and current tracefile closed in write */
1255 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1256 delete_viewer_stream(vstream
);
1257 destroy_viewer_stream(vstream
);
1260 vstream
->close_write_flag
= 1;
1263 if (!vstream
->ctf_trace
->metadata_received
||
1264 vstream
->ctf_trace
->metadata_received
>
1265 vstream
->ctf_trace
->metadata_sent
) {
1266 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1269 pthread_mutex_lock(&vstream
->overwrite_lock
);
1270 if (vstream
->abort_flag
) {
1272 * The file is being overwritten by the writer, we cannot
1275 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1276 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1277 ret
= rotate_viewer_stream(vstream
, rstream
);
1280 } else if (ret
== 1) {
1281 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1282 delete_viewer_stream(vstream
);
1283 destroy_viewer_stream(vstream
);
1288 ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1289 sizeof(packet_index
));
1290 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1291 if (ret
< sizeof(packet_index
)) {
1293 * The tracefile is closed in write, so we read up to EOF.
1295 if (vstream
->close_write_flag
== 1) {
1296 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1297 /* Rotate on normal EOF */
1298 ret
= rotate_viewer_stream(vstream
, rstream
);
1301 } else if (ret
== 1) {
1302 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1303 delete_viewer_stream(vstream
);
1304 destroy_viewer_stream(vstream
);
1308 PERROR("Relay reading index file %d",
1309 vstream
->index_read_fd
);
1310 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1314 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1315 vstream
->last_sent_index
++;
1319 * Indexes are stored in big endian, no need to switch before sending.
1321 viewer_index
.offset
= packet_index
.offset
;
1322 viewer_index
.packet_size
= packet_index
.packet_size
;
1323 viewer_index
.content_size
= packet_index
.content_size
;
1324 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1325 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1326 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1327 viewer_index
.stream_id
= packet_index
.stream_id
;
1330 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1331 health_code_update();
1332 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1333 sizeof(viewer_index
), 0);
1335 ERR("Relay index to viewer");
1338 health_code_update();
1340 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1341 vstream
->last_sent_index
, vstream
->stream_handle
);
1352 * Send the next index for a stream
1354 * Return 0 on success or else a negative value.
1357 int viewer_get_packet(struct relay_command
*cmd
)
1359 int ret
, send_data
= 0;
1363 struct lttng_viewer_get_packet get_packet_info
;
1364 struct lttng_viewer_trace_packet reply
;
1365 struct relay_viewer_stream
*stream
;
1369 DBG2("Relay get data packet");
1371 if (cmd
->version_check_done
== 0) {
1372 ERR("Trying to get packet before version check");
1377 health_code_update();
1378 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1379 sizeof(get_packet_info
), 0);
1380 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1382 ERR("Relay didn't receive the whole packet");
1385 health_code_update();
1387 /* From this point on, the error label can be reached. */
1388 memset(&reply
, 0, sizeof(reply
));
1391 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1395 assert(stream
->ctf_trace
);
1398 * First time we read this stream, we need open the tracefile, we should
1399 * only arrive here if an index has already been sent to the viewer, so the
1400 * tracefile must exist, if it does not it is a fatal error.
1402 if (stream
->read_fd
< 0) {
1403 char fullpath
[PATH_MAX
];
1405 if (stream
->tracefile_count
> 0) {
1406 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1407 stream
->channel_name
,
1408 stream
->tracefile_count_current
);
1410 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1411 stream
->channel_name
);
1416 ret
= open(fullpath
, O_RDONLY
);
1418 PERROR("Relay opening trace file");
1421 stream
->read_fd
= ret
;
1424 if (!stream
->ctf_trace
->metadata_received
||
1425 stream
->ctf_trace
->metadata_received
>
1426 stream
->ctf_trace
->metadata_sent
) {
1427 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1428 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1432 len
= be32toh(get_packet_info
.len
);
1433 data
= zmalloc(len
);
1435 PERROR("relay data zmalloc");
1439 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1442 * If the read fd was closed by the streaming side, the
1443 * abort_flag will be set to 1, otherwise it is an error.
1445 if (stream
->abort_flag
== 0) {
1449 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1452 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1453 if (read_len
< len
) {
1455 * If the read fd was closed by the streaming side, the
1456 * abort_flag will be set to 1, otherwise it is an error.
1458 if (stream
->abort_flag
== 0) {
1459 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1461 be64toh(get_packet_info
.offset
));
1464 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1468 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1469 reply
.len
= htobe32(len
);
1474 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1477 reply
.flags
= htobe32(reply
.flags
);
1479 health_code_update();
1480 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1482 ERR("Relay data header to viewer");
1485 health_code_update();
1488 health_code_update();
1489 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1491 ERR("Relay send data to viewer");
1494 health_code_update();
1497 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1498 be64toh(get_packet_info
.stream_id
));
1509 * Send the session's metadata
1511 * Return 0 on success else a negative value.
1514 int viewer_get_metadata(struct relay_command
*cmd
)
1520 struct lttng_viewer_get_metadata request
;
1521 struct lttng_viewer_metadata_packet reply
;
1522 struct relay_viewer_stream
*stream
;
1526 DBG("Relay get metadata");
1528 if (cmd
->version_check_done
== 0) {
1529 ERR("Trying to get metadata before version check");
1534 health_code_update();
1535 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1536 sizeof(request
), 0);
1537 if (ret
< 0 || ret
!= sizeof(request
)) {
1539 ERR("Relay didn't receive the whole packet");
1542 health_code_update();
1545 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1546 if (!stream
|| !stream
->metadata_flag
) {
1547 ERR("Invalid metadata stream");
1550 assert(stream
->ctf_trace
);
1551 assert(stream
->ctf_trace
->metadata_sent
<=
1552 stream
->ctf_trace
->metadata_received
);
1554 len
= stream
->ctf_trace
->metadata_received
-
1555 stream
->ctf_trace
->metadata_sent
;
1557 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1561 /* first time, we open the metadata file */
1562 if (stream
->read_fd
< 0) {
1563 char fullpath
[PATH_MAX
];
1565 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1566 stream
->channel_name
);
1570 ret
= open(fullpath
, O_RDONLY
);
1572 PERROR("Relay opening metadata file");
1575 stream
->read_fd
= ret
;
1578 reply
.len
= htobe64(len
);
1579 data
= zmalloc(len
);
1581 PERROR("viewer metadata zmalloc");
1585 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1586 if (read_len
< len
) {
1587 PERROR("Relay reading metadata file");
1590 stream
->ctf_trace
->metadata_sent
+= read_len
;
1591 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1595 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1598 health_code_update();
1599 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1601 ERR("Relay data header to viewer");
1604 health_code_update();
1607 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1609 ERR("Relay send data to viewer");
1614 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1615 be64toh(request
.stream_id
));
1617 DBG("Metadata sent");
1627 * live_relay_unknown_command: send -1 if received unknown command
1630 void live_relay_unknown_command(struct relay_command
*cmd
)
1632 struct lttcomm_relayd_generic_reply reply
;
1635 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1636 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1637 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1639 ERR("Relay sending unknown command");
1644 * Process the commands received on the control socket
1647 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1648 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1652 switch (be32toh(recv_hdr
->cmd
)) {
1653 case VIEWER_CONNECT
:
1654 ret
= viewer_connect(cmd
);
1656 case VIEWER_LIST_SESSIONS
:
1657 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1659 case VIEWER_ATTACH_SESSION
:
1660 ret
= viewer_attach_session(cmd
, sessions_ht
);
1662 case VIEWER_GET_NEXT_INDEX
:
1663 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1665 case VIEWER_GET_PACKET
:
1666 ret
= viewer_get_packet(cmd
);
1668 case VIEWER_GET_METADATA
:
1669 ret
= viewer_get_metadata(cmd
);
1672 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1673 live_relay_unknown_command(cmd
);
1683 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1689 lttng_poll_del(events
, pollfd
);
1691 ret
= close(pollfd
);
1693 ERR("Closing pollfd %d", pollfd
);
1698 * Create and add connection to the given hash table.
1700 * Return poll add value or else -1 on error.
1703 int add_connection(int fd
, struct lttng_poll_event
*events
,
1704 struct lttng_ht
*relay_connections_ht
)
1707 struct relay_command
*relay_connection
;
1710 assert(relay_connections_ht
);
1712 relay_connection
= zmalloc(sizeof(struct relay_command
));
1713 if (relay_connection
== NULL
) {
1714 PERROR("Relay command zmalloc");
1718 ret
= lttng_read(fd
, relay_connection
, sizeof(*relay_connection
));
1719 if (ret
< sizeof(*relay_connection
)) {
1720 PERROR("read relay cmd pipe");
1724 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1725 (unsigned long) relay_connection
->sock
->fd
);
1727 lttng_ht_add_unique_ulong(relay_connections_ht
,
1728 &relay_connection
->sock_n
);
1731 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1732 LPOLLIN
| LPOLLRDHUP
);
1735 free(relay_connection
);
1741 void deferred_free_connection(struct rcu_head
*head
)
1743 struct relay_command
*relay_connection
=
1744 caa_container_of(head
, struct relay_command
, rcu_node
);
1746 if (relay_connection
->session
&&
1747 relay_connection
->session
->viewer_attached
> 0) {
1748 relay_connection
->session
->viewer_attached
--;
1750 lttcomm_destroy_sock(relay_connection
->sock
);
1751 free(relay_connection
);
1755 * Delete all streams for a specific session ID.
1758 void viewer_del_streams(uint64_t session_id
)
1760 struct relay_viewer_stream
*stream
;
1761 struct lttng_ht_iter iter
;
1764 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
1766 health_code_update();
1768 if (stream
->session_id
!= session_id
) {
1772 delete_viewer_stream(stream
);
1773 assert(stream
->ctf_trace
);
1775 if (stream
->metadata_flag
) {
1777 * The metadata viewer stream is destroyed once the refcount on the
1778 * ctf trace goes to 0 in the destroy stream function thus there is
1779 * no explicit call to that function here.
1781 stream
->ctf_trace
->metadata_sent
= 0;
1782 stream
->ctf_trace
->viewer_metadata_stream
= NULL
;
1784 destroy_viewer_stream(stream
);
1791 * Delete and free a connection.
1793 * RCU read side lock MUST be acquired.
1796 void del_connection(struct lttng_ht
*relay_connections_ht
,
1797 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1801 assert(relay_connections_ht
);
1803 assert(relay_connection
);
1805 DBG("Cleaning connection of session ID %" PRIu64
,
1806 relay_connection
->session_id
);
1808 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1811 viewer_del_streams(relay_connection
->session_id
);
1813 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1817 * This thread does the actual work
1820 void *thread_worker(void *data
)
1824 struct relay_command
*relay_connection
;
1825 struct lttng_poll_event events
;
1826 struct lttng_ht
*relay_connections_ht
;
1827 struct lttng_ht_node_ulong
*node
;
1828 struct lttng_ht_iter iter
;
1829 struct lttng_viewer_cmd recv_hdr
;
1830 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1831 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1833 DBG("[thread] Live viewer relay worker started");
1835 rcu_register_thread();
1837 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1839 if (testpoint(relayd_thread_live_worker
)) {
1840 goto error_testpoint
;
1843 /* table of connections indexed on socket */
1844 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1845 if (!relay_connections_ht
) {
1846 goto relay_connections_ht_error
;
1849 ret
= create_thread_poll_set(&events
, 2);
1851 goto error_poll_create
;
1854 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1863 health_code_update();
1865 /* Infinite blocking call, waiting for transmission */
1866 DBG3("Relayd live viewer worker thread polling...");
1867 health_poll_entry();
1868 ret
= lttng_poll_wait(&events
, -1);
1872 * Restart interrupted system call.
1874 if (errno
== EINTR
) {
1883 * Process control. The control connection is prioritised so we don't
1884 * starve it with high throughput tracing data on the data
1887 for (i
= 0; i
< nb_fd
; i
++) {
1888 /* Fetch once the poll data */
1889 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1890 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1892 health_code_update();
1894 /* Thread quit pipe has been closed. Killing thread. */
1895 ret
= check_thread_quit_pipe(pollfd
, revents
);
1901 /* Inspect the relay cmd pipe for new connection */
1902 if (pollfd
== live_relay_cmd_pipe
[0]) {
1903 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1904 ERR("Relay live pipe error");
1906 } else if (revents
& LPOLLIN
) {
1907 DBG("Relay live viewer command received");
1908 ret
= add_connection(live_relay_cmd_pipe
[0],
1909 &events
, relay_connections_ht
);
1914 } else if (revents
) {
1916 lttng_ht_lookup(relay_connections_ht
,
1917 (void *)((unsigned long) pollfd
), &iter
);
1918 node
= lttng_ht_iter_get_node_ulong(&iter
);
1920 DBG2("Relay viewer sock %d not found", pollfd
);
1924 relay_connection
= caa_container_of(node
, struct relay_command
,
1927 if (revents
& (LPOLLERR
)) {
1928 cleanup_poll_connection(&events
, pollfd
);
1929 del_connection(relay_connections_ht
, &iter
,
1931 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1932 DBG("Viewer socket %d hung up", pollfd
);
1933 cleanup_poll_connection(&events
, pollfd
);
1934 del_connection(relay_connections_ht
, &iter
,
1936 } else if (revents
& LPOLLIN
) {
1937 ret
= relay_connection
->sock
->ops
->recvmsg(
1938 relay_connection
->sock
, &recv_hdr
,
1939 sizeof(struct lttng_viewer_cmd
),
1941 /* connection closed */
1943 cleanup_poll_connection(&events
, pollfd
);
1944 del_connection(relay_connections_ht
, &iter
,
1946 DBG("Viewer control connection closed with %d",
1949 if (relay_connection
->session
) {
1950 DBG2("Relay viewer worker receiving data for "
1951 "session: %" PRIu64
,
1952 relay_connection
->session
->id
);
1954 ret
= process_control(&recv_hdr
, relay_connection
,
1957 /* Clear the session on error. */
1958 cleanup_poll_connection(&events
, pollfd
);
1959 del_connection(relay_connections_ht
, &iter
,
1961 DBG("Viewer connection closed with %d", pollfd
);
1972 lttng_poll_clean(&events
);
1974 /* empty the hash table and free the memory */
1976 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1977 health_code_update();
1979 node
= lttng_ht_iter_get_node_ulong(&iter
);
1984 relay_connection
= caa_container_of(node
, struct relay_command
,
1986 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1990 lttng_ht_destroy(relay_connections_ht
);
1991 relay_connections_ht_error
:
1992 /* Close relay cmd pipes */
1993 utils_close_pipe(live_relay_cmd_pipe
);
1995 DBG("Viewer worker thread exited with error");
1997 DBG("Viewer worker thread cleanup complete");
2001 ERR("Health error occurred in %s", __func__
);
2003 health_unregister(health_relayd
);
2005 rcu_unregister_thread();
2010 * Create the relay command pipe to wake thread_manage_apps.
2011 * Closed in cleanup().
2013 static int create_relay_cmd_pipe(void)
2017 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
2022 void live_stop_threads(void)
2029 ret
= pthread_join(live_listener_thread
, &status
);
2031 PERROR("pthread_join live listener");
2032 goto error
; /* join error, exit without cleanup */
2035 ret
= pthread_join(live_worker_thread
, &status
);
2037 PERROR("pthread_join live worker");
2038 goto error
; /* join error, exit without cleanup */
2041 ret
= pthread_join(live_dispatcher_thread
, &status
);
2043 PERROR("pthread_join live dispatcher");
2044 goto error
; /* join error, exit without cleanup */
2056 int live_start_threads(struct lttng_uri
*uri
,
2057 struct relay_local_data
*relay_ctx
)
2066 /* Check if daemon is UID = 0 */
2067 is_root
= !getuid();
2070 if (live_uri
->port
< 1024) {
2071 ERR("Need to be root to use ports < 1024");
2077 /* Setup the thread apps communication pipe. */
2078 if ((ret
= create_relay_cmd_pipe()) < 0) {
2082 /* Init relay command queue. */
2083 cds_wfq_init(&viewer_cmd_queue
.queue
);
2085 /* Set up max poll set size */
2086 lttng_poll_set_max_size();
2088 /* Setup the dispatcher thread */
2089 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2090 thread_dispatcher
, (void *) NULL
);
2092 PERROR("pthread_create viewer dispatcher");
2093 goto exit_dispatcher
;
2096 /* Setup the worker thread */
2097 ret
= pthread_create(&live_worker_thread
, NULL
,
2098 thread_worker
, relay_ctx
);
2100 PERROR("pthread_create viewer worker");
2104 /* Setup the listener thread */
2105 ret
= pthread_create(&live_listener_thread
, NULL
,
2106 thread_listener
, (void *) NULL
);
2108 PERROR("pthread_create viewer listener");
2116 ret
= pthread_join(live_listener_thread
, &status
);
2118 PERROR("pthread_join live listener");
2119 goto error
; /* join error, exit without cleanup */
2123 ret
= pthread_join(live_worker_thread
, &status
);
2125 PERROR("pthread_join live worker");
2126 goto error
; /* join error, exit without cleanup */
2130 ret
= pthread_join(live_dispatcher_thread
, &status
);
2132 PERROR("pthread_join live dispatcher");
2133 goto error
; /* join error, exit without cleanup */