2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/rculist.h>
43 #include <lttng/lttng.h>
44 #include <common/common.h>
45 #include <common/compat/poll.h>
46 #include <common/compat/socket.h>
47 #include <common/compat/endian.h>
48 #include <common/defaults.h>
49 #include <common/futex.h>
50 #include <common/index/index.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/sessiond-comm/inet.h>
53 #include <common/sessiond-comm/relayd.h>
54 #include <common/uri.h>
55 #include <common/utils.h>
59 #include "lttng-relayd.h"
61 #include "health-relayd.h"
62 #include "testpoint.h"
63 #include "viewer-stream.h"
66 #include "ctf-trace.h"
67 #include "connection.h"
68 #include "viewer-session.h"
70 #define SESSION_BUF_DEFAULT_COUNT 16
72 static struct lttng_uri
*live_uri
;
75 * This pipe is used to inform the worker thread that a command is queued and
76 * ready to be processed.
78 static int live_conn_pipe
[2] = { -1, -1 };
80 /* Shared between threads */
81 static int live_dispatch_thread_exit
;
83 static pthread_t live_listener_thread
;
84 static pthread_t live_dispatcher_thread
;
85 static pthread_t live_worker_thread
;
88 * Relay command queue.
90 * The live_thread_listener and live_thread_dispatcher communicate with this
93 static struct relay_conn_queue viewer_conn_queue
;
95 static uint64_t last_relay_viewer_session_id
;
96 static pthread_mutex_t last_relay_viewer_session_id_lock
=
97 PTHREAD_MUTEX_INITIALIZER
;
103 void cleanup_relayd_live(void)
111 * Receive a request buffer using a given socket, destination allocated buffer
114 * Return the size of the received message or else a negative value on error
115 * with errno being set by recvmsg() syscall.
118 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
122 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
123 if (ret
< 0 || ret
!= size
) {
125 /* Orderly shutdown. Not necessary to print an error. */
126 DBG("Socket %d did an orderly shutdown", sock
->fd
);
128 ERR("Relay failed to receive request.");
137 * Send a response buffer using a given socket, source allocated buffer of
140 * Return the size of the sent message or else a negative value on error with
141 * errno being set by sendmsg() syscall.
144 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
148 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
150 ERR("Relayd failed to send response.");
157 * Atomically check if new streams got added in one of the sessions attached
158 * and reset the flag to 0.
160 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
164 int check_new_streams(struct relay_connection
*conn
)
166 struct relay_session
*session
;
167 unsigned long current_val
;
170 if (!conn
->viewer_session
) {
174 cds_list_for_each_entry_rcu(session
,
175 &conn
->viewer_session
->session_list
,
176 viewer_session_node
) {
177 if (!session_get(session
)) {
180 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
182 session_put(session
);
193 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
194 * this function should ignore the sent flag or not.
196 * Return 0 on success or else a negative value.
199 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
200 uint64_t session_id
, unsigned int ignore_sent_flag
)
203 struct lttng_viewer_stream send_stream
;
204 struct lttng_ht_iter iter
;
205 struct relay_viewer_stream
*vstream
;
209 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
211 struct ctf_trace
*ctf_trace
;
213 health_code_update();
215 if (!viewer_stream_get(vstream
)) {
219 pthread_mutex_lock(&vstream
->stream
->lock
);
220 /* Ignore if not the same session. */
221 if (vstream
->stream
->trace
->session
->id
!= session_id
||
222 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
223 pthread_mutex_unlock(&vstream
->stream
->lock
);
224 viewer_stream_put(vstream
);
228 ctf_trace
= vstream
->stream
->trace
;
229 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
230 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
231 send_stream
.metadata_flag
= htobe32(
232 vstream
->stream
->is_metadata
);
233 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
234 sizeof(send_stream
.path_name
))) {
235 pthread_mutex_unlock(&vstream
->stream
->lock
);
236 viewer_stream_put(vstream
);
237 ret
= -1; /* Error. */
240 if (lttng_strncpy(send_stream
.channel_name
,
241 vstream
->channel_name
,
242 sizeof(send_stream
.channel_name
))) {
243 pthread_mutex_unlock(&vstream
->stream
->lock
);
244 viewer_stream_put(vstream
);
245 ret
= -1; /* Error. */
249 DBG("Sending stream %" PRIu64
" to viewer",
250 vstream
->stream
->stream_handle
);
251 vstream
->sent_flag
= 1;
252 pthread_mutex_unlock(&vstream
->stream
->lock
);
254 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
255 viewer_stream_put(vstream
);
269 * Create every viewer stream possible for the given session with the seek
270 * type. Three counters *can* be return which are in order the total amount of
271 * viewer stream of the session, the number of unsent stream and the number of
272 * stream created. Those counters can be NULL and thus will be ignored.
274 * session must be locked to ensure that we see either none or all initial
275 * streams for a session, but no intermediate state..
277 * Return 0 on success or else a negative value.
280 int make_viewer_streams(struct relay_session
*session
,
281 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
282 uint32_t *nb_created
, bool *closed
)
285 struct lttng_ht_iter iter
;
286 struct ctf_trace
*ctf_trace
;
289 ASSERT_LOCKED(session
->lock
);
291 if (session
->connection_closed
) {
296 * Create viewer streams for relay streams that are ready to be
297 * used for a the given session id only.
300 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
302 struct relay_stream
*stream
;
304 health_code_update();
306 if (!ctf_trace_get(ctf_trace
)) {
310 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
311 struct relay_viewer_stream
*vstream
;
313 if (!stream_get(stream
)) {
317 * stream published is protected by the session lock.
319 if (!stream
->published
) {
322 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
324 vstream
= viewer_stream_create(stream
, seek_t
);
327 ctf_trace_put(ctf_trace
);
333 /* Update number of created stream counter. */
337 * Ensure a self-reference is preserved even
338 * after we have put our local reference.
340 if (!viewer_stream_get(vstream
)) {
341 ERR("Unable to get self-reference on viewer stream, logic error.");
345 if (!vstream
->sent_flag
&& nb_unsent
) {
346 /* Update number of unsent stream counter. */
350 /* Update number of total stream counter. */
352 if (stream
->is_metadata
) {
353 if (!stream
->closed
||
354 stream
->metadata_received
> vstream
->metadata_sent
) {
358 if (!stream
->closed
||
359 !(((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) >= 0)) {
365 /* Put local reference. */
366 viewer_stream_put(vstream
);
370 ctf_trace_put(ctf_trace
);
380 int relayd_live_stop(void)
382 /* Stop dispatch thread */
383 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
384 futex_nto1_wake(&viewer_conn_queue
.futex
);
389 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
392 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
396 if (events
== NULL
|| size
== 0) {
401 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
407 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
419 * Check if the thread quit pipe was triggered.
421 * Return 1 if it was triggered else 0;
424 int check_thread_quit_pipe(int fd
, uint32_t events
)
426 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
434 * Create and init socket from uri.
437 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
440 struct lttcomm_sock
*sock
= NULL
;
442 sock
= lttcomm_alloc_sock_from_uri(uri
);
444 ERR("Allocating socket");
448 ret
= lttcomm_create_sock(sock
);
452 DBG("Listening on sock %d for lttng-live", sock
->fd
);
454 ret
= sock
->ops
->bind(sock
);
456 PERROR("Failed to bind lttng-live socket");
460 ret
= sock
->ops
->listen(sock
, -1);
470 lttcomm_destroy_sock(sock
);
476 * This thread manages the listening for new connections on the network
479 void *thread_listener(void *data
)
481 int i
, ret
, pollfd
, err
= -1;
482 uint32_t revents
, nb_fd
;
483 struct lttng_poll_event events
;
484 struct lttcomm_sock
*live_control_sock
;
486 DBG("[thread] Relay live listener started");
488 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
490 health_code_update();
492 live_control_sock
= init_socket(live_uri
);
493 if (!live_control_sock
) {
494 goto error_sock_control
;
497 /* Pass 2 as size here for the thread quit pipe and control sockets. */
498 ret
= create_thread_poll_set(&events
, 2);
500 goto error_create_poll
;
503 /* Add the control socket */
504 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
509 lttng_relay_notify_ready();
511 if (testpoint(relayd_thread_live_listener
)) {
512 goto error_testpoint
;
516 health_code_update();
518 DBG("Listener accepting live viewers connections");
522 ret
= lttng_poll_wait(&events
, -1);
526 * Restart interrupted system call.
528 if (errno
== EINTR
) {
535 DBG("Relay new viewer connection received");
536 for (i
= 0; i
< nb_fd
; i
++) {
537 health_code_update();
539 /* Fetch once the poll data */
540 revents
= LTTNG_POLL_GETEV(&events
, i
);
541 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
543 /* Thread quit pipe has been closed. Killing thread. */
544 ret
= check_thread_quit_pipe(pollfd
, revents
);
550 if (revents
& LPOLLIN
) {
552 * A new connection is requested, therefore a
553 * viewer connection is allocated in this
554 * thread, enqueued to a global queue and
555 * dequeued (and freed) in the worker thread.
558 struct relay_connection
*new_conn
;
559 struct lttcomm_sock
*newsock
;
561 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
563 PERROR("accepting control sock");
566 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
568 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
571 PERROR("setsockopt inet");
572 lttcomm_destroy_sock(newsock
);
575 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
577 lttcomm_destroy_sock(newsock
);
580 /* Ownership assumed by the connection. */
583 /* Enqueue request for the dispatcher thread. */
584 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
588 * Wake the dispatch queue futex.
589 * Implicit memory barrier with the
590 * exchange in cds_wfcq_enqueue.
592 futex_nto1_wake(&viewer_conn_queue
.futex
);
593 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
594 ERR("socket poll error");
597 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
607 lttng_poll_clean(&events
);
609 if (live_control_sock
->fd
>= 0) {
610 ret
= live_control_sock
->ops
->close(live_control_sock
);
615 lttcomm_destroy_sock(live_control_sock
);
619 DBG("Live viewer listener thread exited with error");
621 health_unregister(health_relayd
);
622 DBG("Live viewer listener thread cleanup complete");
623 if (lttng_relay_stop_threads()) {
624 ERR("Error stopping threads");
630 * This thread manages the dispatching of the requests to worker threads
633 void *thread_dispatcher(void *data
)
637 struct cds_wfcq_node
*node
;
638 struct relay_connection
*conn
= NULL
;
640 DBG("[thread] Live viewer relay dispatcher started");
642 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
644 if (testpoint(relayd_thread_live_dispatcher
)) {
645 goto error_testpoint
;
648 health_code_update();
651 health_code_update();
653 /* Atomically prepare the queue futex */
654 futex_nto1_prepare(&viewer_conn_queue
.futex
);
656 if (CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
661 health_code_update();
663 /* Dequeue commands */
664 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
665 &viewer_conn_queue
.tail
);
667 DBG("Woken up but nothing in the live-viewer "
668 "relay command queue");
669 /* Continue thread execution */
672 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
673 DBG("Dispatching viewer request waiting on sock %d",
677 * Inform worker thread of the new request. This
678 * call is blocking so we can be assured that
679 * the data will be read at some point in time
680 * or wait to the end of the world :)
682 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
684 PERROR("write conn pipe");
685 connection_put(conn
);
688 } while (node
!= NULL
);
690 /* Futex wait on queue. Blocking call on futex() */
692 futex_nto1_wait(&viewer_conn_queue
.futex
);
696 /* Normal exit, no error */
703 ERR("Health error occurred in %s", __func__
);
705 health_unregister(health_relayd
);
706 DBG("Live viewer dispatch thread dying");
707 if (lttng_relay_stop_threads()) {
708 ERR("Error stopping threads");
714 * Establish connection with the viewer and check the versions.
716 * Return 0 on success or else negative value.
719 int viewer_connect(struct relay_connection
*conn
)
722 struct lttng_viewer_connect reply
, msg
;
724 conn
->version_check_done
= 1;
726 health_code_update();
728 DBG("Viewer is establishing a connection to the relayd.");
730 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
735 health_code_update();
737 memset(&reply
, 0, sizeof(reply
));
738 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
739 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
741 /* Major versions must be the same */
742 if (reply
.major
!= be32toh(msg
.major
)) {
743 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
744 reply
.major
, be32toh(msg
.major
));
749 conn
->major
= reply
.major
;
750 /* We adapt to the lowest compatible version */
751 if (reply
.minor
<= be32toh(msg
.minor
)) {
752 conn
->minor
= reply
.minor
;
754 conn
->minor
= be32toh(msg
.minor
);
757 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
758 conn
->type
= RELAY_VIEWER_COMMAND
;
759 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
760 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
762 ERR("Unknown connection type : %u", be32toh(msg
.type
));
767 reply
.major
= htobe32(reply
.major
);
768 reply
.minor
= htobe32(reply
.minor
);
769 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
771 * Increment outside of htobe64 macro, because the argument can
772 * be used more than once within the macro, and thus the
773 * operation may be undefined.
775 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
776 last_relay_viewer_session_id
++;
777 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
778 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
781 health_code_update();
783 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
788 health_code_update();
790 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
798 * Send the viewer the list of current sessions.
799 * We need to create a copy of the hash table content because otherwise
800 * we cannot assume the number of entries stays the same between getting
801 * the number of HT elements and iteration over the HT.
803 * Return 0 on success or else a negative value.
806 int viewer_list_sessions(struct relay_connection
*conn
)
809 struct lttng_viewer_list_sessions session_list
;
810 struct lttng_ht_iter iter
;
811 struct relay_session
*session
;
812 struct lttng_viewer_session
*send_session_buf
= NULL
;
813 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
816 DBG("List sessions received");
818 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
819 if (!send_session_buf
) {
824 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
826 struct lttng_viewer_session
*send_session
;
828 health_code_update();
830 if (session
->connection_closed
) {
831 /* Skip closed session */
835 if (count
>= buf_count
) {
836 struct lttng_viewer_session
*newbuf
;
837 uint32_t new_buf_count
= buf_count
<< 1;
839 newbuf
= realloc(send_session_buf
,
840 new_buf_count
* sizeof(*send_session_buf
));
845 send_session_buf
= newbuf
;
846 buf_count
= new_buf_count
;
848 send_session
= &send_session_buf
[count
];
849 if (lttng_strncpy(send_session
->session_name
,
850 session
->session_name
,
851 sizeof(send_session
->session_name
))) {
855 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
856 sizeof(send_session
->hostname
))) {
860 send_session
->id
= htobe64(session
->id
);
861 send_session
->live_timer
= htobe32(session
->live_timer
);
862 if (session
->viewer_attached
) {
863 send_session
->clients
= htobe32(1);
865 send_session
->clients
= htobe32(0);
867 send_session
->streams
= htobe32(session
->stream_count
);
875 session_list
.sessions_count
= htobe32(count
);
877 health_code_update();
879 ret
= send_response(conn
->sock
, &session_list
, sizeof(session_list
));
884 health_code_update();
886 ret
= send_response(conn
->sock
, send_session_buf
,
887 count
* sizeof(*send_session_buf
));
891 health_code_update();
895 free(send_session_buf
);
900 * Send the viewer the list of current streams.
903 int viewer_get_new_streams(struct relay_connection
*conn
)
905 int ret
, send_streams
= 0;
906 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0, nb_total
= 0;
907 struct lttng_viewer_new_streams_request request
;
908 struct lttng_viewer_new_streams_response response
;
909 struct relay_session
*session
;
915 DBG("Get new streams received");
917 health_code_update();
919 /* Receive the request from the connected client. */
920 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
924 session_id
= be64toh(request
.session_id
);
926 health_code_update();
928 memset(&response
, 0, sizeof(response
));
930 session
= session_get_by_id(session_id
);
932 DBG("Relay session %" PRIu64
" not found", session_id
);
933 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
937 if (!viewer_session_is_attached(conn
->viewer_session
, session
)) {
939 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_ERR
);
944 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_OK
);
946 pthread_mutex_lock(&session
->lock
);
947 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
948 &nb_created
, &closed
);
949 pthread_mutex_unlock(&session
->lock
);
951 goto end_put_session
;
953 /* Only send back the newly created streams with the unsent ones. */
954 nb_streams
= nb_created
+ nb_unsent
;
955 response
.streams_count
= htobe32(nb_streams
);
958 * If the session is closed, HUP when there are no more streams
961 if (closed
&& nb_total
== 0) {
963 response
.streams_count
= 0;
964 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
969 health_code_update();
970 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
972 goto end_put_session
;
974 health_code_update();
977 * Unknown or empty session, just return gracefully, the viewer
978 * knows what is happening.
980 if (!send_streams
|| !nb_streams
) {
982 goto end_put_session
;
986 * Send stream and *DON'T* ignore the sent flag so every viewer
987 * streams that were not sent from that point will be sent to
990 ret
= send_viewer_streams(conn
->sock
, session_id
, 0);
992 goto end_put_session
;
997 session_put(session
);
1004 * Send the viewer the list of current sessions.
1007 int viewer_attach_session(struct relay_connection
*conn
)
1009 int send_streams
= 0;
1011 uint32_t nb_streams
= 0;
1012 enum lttng_viewer_seek seek_type
;
1013 struct lttng_viewer_attach_session_request request
;
1014 struct lttng_viewer_attach_session_response response
;
1015 struct relay_session
*session
= NULL
;
1016 bool closed
= false;
1017 uint64_t session_id
;
1021 health_code_update();
1023 /* Receive the request from the connected client. */
1024 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1029 session_id
= be64toh(request
.session_id
);
1030 health_code_update();
1032 memset(&response
, 0, sizeof(response
));
1034 if (!conn
->viewer_session
) {
1035 DBG("Client trying to attach before creating a live viewer session");
1036 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1040 session
= session_get_by_id(session_id
);
1042 DBG("Relay session %" PRIu64
" not found", session_id
);
1043 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1046 DBG("Attach session ID %" PRIu64
" received", session_id
);
1048 pthread_mutex_lock(&session
->lock
);
1049 if (session
->live_timer
== 0) {
1050 DBG("Not live session");
1051 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1056 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1058 DBG("Already a viewer attached");
1059 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1063 switch (be32toh(request
.seek
)) {
1064 case LTTNG_VIEWER_SEEK_BEGINNING
:
1065 case LTTNG_VIEWER_SEEK_LAST
:
1066 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1067 seek_type
= be32toh(request
.seek
);
1070 ERR("Wrong seek parameter");
1071 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1076 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1079 goto end_put_session
;
1082 pthread_mutex_unlock(&session
->lock
);
1083 session_put(session
);
1086 response
.streams_count
= htobe32(nb_streams
);
1088 * If the session is closed when the viewer is attaching, it
1089 * means some of the streams may have been concurrently removed,
1090 * so we don't allow the viewer to attach, even if there are
1091 * streams available.
1095 response
.streams_count
= 0;
1096 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1101 health_code_update();
1102 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1104 goto end_put_session
;
1106 health_code_update();
1109 * Unknown or empty session, just return gracefully, the viewer
1110 * knows what is happening.
1112 if (!send_streams
|| !nb_streams
) {
1114 goto end_put_session
;
1117 /* Send stream and ignore the sent flag. */
1118 ret
= send_viewer_streams(conn
->sock
, session_id
, 1);
1120 goto end_put_session
;
1125 pthread_mutex_unlock(&session
->lock
);
1126 session_put(session
);
1133 * Open the index file if needed for the given vstream.
1135 * If an index file is successfully opened, the vstream will set it as its
1136 * current index file.
1138 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1140 * Called with rstream lock held.
1142 static int try_open_index(struct relay_viewer_stream
*vstream
,
1143 struct relay_stream
*rstream
)
1146 const uint32_t connection_major
= rstream
->trace
->session
->major
;
1147 const uint32_t connection_minor
= rstream
->trace
->session
->minor
;
1149 if (vstream
->index_file
) {
1154 * First time, we open the index file and at least one index is ready.
1156 if (rstream
->index_received_seqcount
== 0) {
1160 vstream
->index_file
= lttng_index_file_create_from_trace_chunk_read_only(
1161 rstream
->trace_chunk
, rstream
->path_name
,
1162 rstream
->channel_name
, rstream
->tracefile_size
,
1163 vstream
->current_tracefile_id
,
1164 lttng_to_index_major(connection_major
, connection_minor
),
1165 lttng_to_index_minor(connection_major
, connection_minor
));
1166 if (!vstream
->index_file
) {
1175 * Check the status of the index for the given stream. This function
1176 * updates the index structure if needed and can put (close) the vstream
1177 * in the HUP situation.
1179 * Return 0 means that we can proceed with the index. A value of 1 means
1180 * that the index has been updated and is ready to be sent to the
1181 * client. A negative value indicates an error that can't be handled.
1183 * Called with rstream lock held.
1185 static int check_index_status(struct relay_viewer_stream
*vstream
,
1186 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1187 struct lttng_viewer_index
*index
)
1191 if ((trace
->session
->connection_closed
|| rstream
->closed
)
1192 && rstream
->index_received_seqcount
1193 == vstream
->index_sent_seqcount
) {
1195 * Last index sent and session connection or relay
1196 * stream are closed.
1198 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1200 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1201 rstream
->index_received_seqcount
1202 == vstream
->index_sent_seqcount
) {
1204 * We've received a synchronization beacon and the last index
1205 * available has been sent, the index for now is inactive.
1207 * In this case, we have received a beacon which allows us to
1208 * inform the client of a time interval during which we can
1209 * guarantee that there are no events to read (and never will
1212 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1213 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1214 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1216 } else if (rstream
->index_received_seqcount
1217 == vstream
->index_sent_seqcount
) {
1219 * This checks whether received == sent seqcount. In
1220 * this case, we have not received a beacon. Therefore,
1221 * we can only ask the client to retry later.
1223 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1225 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1226 vstream
->current_tracefile_id
,
1227 vstream
->index_sent_seqcount
)) {
1229 * The next index we want to send cannot be read either
1230 * because we need to perform a rotation, or due to
1231 * the producer having overwritten its trace file.
1233 DBG("Viewer stream %" PRIu64
" rotation",
1234 vstream
->stream
->stream_handle
);
1235 ret
= viewer_stream_rotate(vstream
);
1238 } else if (ret
== 1) {
1239 /* EOF across entire stream. */
1240 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1244 * If we have been pushed due to overwrite, it
1245 * necessarily means there is data that can be read in
1246 * the stream. If we rotated because we reached the end
1247 * of a tracefile, it means the following tracefile
1248 * needs to contain at least one index, else we would
1249 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1250 * viewer. The updated index_sent_seqcount needs to
1251 * point to a readable index entry now.
1253 * In the case where we "rotate" on a single file, we
1254 * can end up in a case where the requested index is
1255 * still unavailable.
1257 if (rstream
->tracefile_count
== 1 &&
1258 !tracefile_array_seq_in_file(
1260 vstream
->current_tracefile_id
,
1261 vstream
->index_sent_seqcount
)) {
1262 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1265 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1266 vstream
->current_tracefile_id
,
1267 vstream
->index_sent_seqcount
));
1269 /* ret == 0 means successful so we continue. */
1275 viewer_stream_put(vstream
);
1281 * Send the next index for a stream.
1283 * Return 0 on success or else a negative value.
1286 int viewer_get_next_index(struct relay_connection
*conn
)
1289 struct lttng_viewer_get_next_index request_index
;
1290 struct lttng_viewer_index viewer_index
;
1291 struct ctf_packet_index packet_index
;
1292 struct relay_viewer_stream
*vstream
= NULL
;
1293 struct relay_stream
*rstream
= NULL
;
1294 struct ctf_trace
*ctf_trace
= NULL
;
1295 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1299 DBG("Viewer get next index");
1301 memset(&viewer_index
, 0, sizeof(viewer_index
));
1302 health_code_update();
1304 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1308 health_code_update();
1310 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1312 DBG("Client requested index of unknown stream id %" PRIu64
,
1313 (uint64_t) be64toh(request_index
.stream_id
));
1314 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1318 /* Use back. ref. Protected by refcounts. */
1319 rstream
= vstream
->stream
;
1320 ctf_trace
= rstream
->trace
;
1322 /* metadata_viewer_stream may be NULL. */
1323 metadata_viewer_stream
=
1324 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1326 pthread_mutex_lock(&rstream
->lock
);
1329 * The viewer should not ask for index on metadata stream.
1331 if (rstream
->is_metadata
) {
1332 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1336 /* Try to open an index if one is needed for that stream. */
1337 ret
= try_open_index(vstream
, rstream
);
1339 if (ret
== -ENOENT
) {
1341 * The index is created only when the first data
1342 * packet arrives, it might not be ready at the
1343 * beginning of the session
1345 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1347 /* Unhandled error. */
1348 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1353 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1356 } else if (ret
== 1) {
1358 * We have no index to send and check_index_status has populated
1359 * viewer_index's status.
1363 /* At this point, ret is 0 thus we will be able to read the index. */
1367 * vstream->stream_fd may be NULL if it has been closed by
1368 * tracefile rotation, or if we are at the beginning of the
1369 * stream. We open the data stream file here to protect against
1370 * overwrite caused by tracefile rotation (in association with
1371 * unlink performed before overwrite).
1373 if (!vstream
->stream_file
.fd
) {
1375 char file_path
[LTTNG_PATH_MAX
];
1376 enum lttng_trace_chunk_status status
;
1378 ret
= utils_stream_file_path(rstream
->path_name
,
1379 rstream
->channel_name
, rstream
->tracefile_size
,
1380 vstream
->current_tracefile_id
, NULL
, file_path
,
1386 status
= lttng_trace_chunk_open_file(
1387 vstream
->stream_file
.trace_chunk
,
1388 file_path
, O_RDONLY
, 0, &fd
);
1389 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1390 PERROR("Failed to open trace file for viewer stream");
1393 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1394 if (!vstream
->stream_file
.fd
) {
1396 PERROR("Failed to close viewer stream file");
1402 ret
= check_new_streams(conn
);
1404 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1406 } else if (ret
== 1) {
1407 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1410 ret
= lttng_index_file_read(vstream
->index_file
, &packet_index
);
1412 ERR("Relay error reading index file %d",
1413 vstream
->index_file
->fd
);
1414 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1417 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1418 vstream
->index_sent_seqcount
++;
1422 * Indexes are stored in big endian, no need to switch before sending.
1424 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1425 rstream
->stream_handle
,
1426 (uint64_t) be64toh(packet_index
.offset
));
1427 viewer_index
.offset
= packet_index
.offset
;
1428 viewer_index
.packet_size
= packet_index
.packet_size
;
1429 viewer_index
.content_size
= packet_index
.content_size
;
1430 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1431 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1432 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1433 viewer_index
.stream_id
= packet_index
.stream_id
;
1437 pthread_mutex_unlock(&rstream
->lock
);
1440 if (metadata_viewer_stream
) {
1441 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1442 DBG("get next index metadata check: recv %" PRIu64
1444 metadata_viewer_stream
->stream
->metadata_received
,
1445 metadata_viewer_stream
->metadata_sent
);
1446 if (!metadata_viewer_stream
->stream
->metadata_received
||
1447 metadata_viewer_stream
->stream
->metadata_received
>
1448 metadata_viewer_stream
->metadata_sent
) {
1449 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1451 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1454 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1455 health_code_update();
1457 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1461 health_code_update();
1464 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1465 vstream
->index_sent_seqcount
,
1466 vstream
->stream
->stream_handle
);
1469 if (metadata_viewer_stream
) {
1470 viewer_stream_put(metadata_viewer_stream
);
1473 viewer_stream_put(vstream
);
1478 pthread_mutex_unlock(&rstream
->lock
);
1479 if (metadata_viewer_stream
) {
1480 viewer_stream_put(metadata_viewer_stream
);
1482 viewer_stream_put(vstream
);
1487 * Send the next index for a stream
1489 * Return 0 on success or else a negative value.
1492 int viewer_get_packet(struct relay_connection
*conn
)
1497 struct lttng_viewer_get_packet get_packet_info
;
1498 struct lttng_viewer_trace_packet reply_header
;
1499 struct relay_viewer_stream
*vstream
= NULL
;
1500 uint32_t reply_size
= sizeof(reply_header
);
1501 uint32_t packet_data_len
= 0;
1504 DBG2("Relay get data packet");
1506 health_code_update();
1508 ret
= recv_request(conn
->sock
, &get_packet_info
,
1509 sizeof(get_packet_info
));
1513 health_code_update();
1515 /* From this point on, the error label can be reached. */
1516 memset(&reply_header
, 0, sizeof(reply_header
));
1518 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1520 DBG("Client requested packet of unknown stream id %" PRIu64
,
1521 (uint64_t) be64toh(get_packet_info
.stream_id
));
1522 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1523 goto send_reply_nolock
;
1525 packet_data_len
= be32toh(get_packet_info
.len
);
1526 reply_size
+= packet_data_len
;
1529 reply
= zmalloc(reply_size
);
1531 PERROR("packet reply zmalloc");
1532 reply_size
= sizeof(reply_header
);
1536 pthread_mutex_lock(&vstream
->stream
->lock
);
1537 lseek_ret
= lseek(vstream
->stream_file
.fd
->fd
,
1538 be64toh(get_packet_info
.offset
), SEEK_SET
);
1539 if (lseek_ret
< 0) {
1540 PERROR("lseek fd %d to offset %" PRIu64
,
1541 vstream
->stream_file
.fd
->fd
,
1542 (uint64_t) be64toh(get_packet_info
.offset
));
1545 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
,
1546 reply
+ sizeof(reply_header
), packet_data_len
);
1547 if (read_len
< packet_data_len
) {
1548 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1549 vstream
->stream_file
.fd
->fd
,
1550 (uint64_t) be64toh(get_packet_info
.offset
));
1553 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1554 reply_header
.len
= htobe32(packet_data_len
);
1558 reply_header
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1562 pthread_mutex_unlock(&vstream
->stream
->lock
);
1566 health_code_update();
1569 memcpy(reply
, &reply_header
, sizeof(reply_header
));
1570 ret
= send_response(conn
->sock
, reply
, reply_size
);
1572 /* No reply to send. */
1573 ret
= send_response(conn
->sock
, &reply_header
,
1577 health_code_update();
1579 PERROR("sendmsg of packet data failed");
1583 DBG("Sent %u bytes for stream %" PRIu64
, reply_size
,
1584 (uint64_t) be64toh(get_packet_info
.stream_id
));
1590 viewer_stream_put(vstream
);
1596 * Send the session's metadata
1598 * Return 0 on success else a negative value.
1601 int viewer_get_metadata(struct relay_connection
*conn
)
1607 struct lttng_viewer_get_metadata request
;
1608 struct lttng_viewer_metadata_packet reply
;
1609 struct relay_viewer_stream
*vstream
= NULL
;
1613 DBG("Relay get metadata");
1615 health_code_update();
1617 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1621 health_code_update();
1623 memset(&reply
, 0, sizeof(reply
));
1625 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1628 * The metadata stream can be closed by a CLOSE command
1629 * just before we attach. It can also be closed by
1630 * per-pid tracing during tracing. Therefore, it is
1631 * possible that we cannot find this viewer stream.
1632 * Reply back to the client with an error if we cannot
1635 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1636 (uint64_t) be64toh(request
.stream_id
));
1637 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1640 pthread_mutex_lock(&vstream
->stream
->lock
);
1641 if (!vstream
->stream
->is_metadata
) {
1642 ERR("Invalid metadata stream");
1646 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1648 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1650 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1654 /* first time, we open the metadata file */
1655 if (!vstream
->stream_file
.fd
) {
1657 char file_path
[LTTNG_PATH_MAX
];
1658 enum lttng_trace_chunk_status status
;
1659 struct relay_stream
*rstream
= vstream
->stream
;
1661 ret
= utils_stream_file_path(rstream
->path_name
,
1662 rstream
->channel_name
, rstream
->tracefile_size
,
1663 vstream
->current_tracefile_id
, NULL
, file_path
,
1669 status
= lttng_trace_chunk_open_file(
1670 vstream
->stream_file
.trace_chunk
,
1671 file_path
, O_RDONLY
, 0, &fd
);
1672 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1673 PERROR("Failed to open metadata file for viewer stream");
1676 vstream
->stream_file
.fd
= stream_fd_create(fd
);
1677 if (!vstream
->stream_file
.fd
) {
1679 PERROR("Failed to close viewer metadata file");
1685 reply
.len
= htobe64(len
);
1686 data
= zmalloc(len
);
1688 PERROR("viewer metadata zmalloc");
1692 read_len
= lttng_read(vstream
->stream_file
.fd
->fd
, data
, len
);
1693 if (read_len
< len
) {
1694 PERROR("Relay reading metadata file");
1697 vstream
->metadata_sent
+= read_len
;
1698 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1699 && vstream
->stream
->closed
) {
1700 /* Release ownership for the viewer metadata stream. */
1701 viewer_stream_put(vstream
);
1704 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1709 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1712 health_code_update();
1714 pthread_mutex_unlock(&vstream
->stream
->lock
);
1716 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1720 health_code_update();
1723 ret
= send_response(conn
->sock
, data
, len
);
1729 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1730 (uint64_t) be64toh(request
.stream_id
));
1732 DBG("Metadata sent");
1738 viewer_stream_put(vstream
);
1744 * Create a viewer session.
1746 * Return 0 on success or else a negative value.
1749 int viewer_create_session(struct relay_connection
*conn
)
1752 struct lttng_viewer_create_session_response resp
;
1754 DBG("Viewer create session received");
1756 memset(&resp
, 0, sizeof(resp
));
1757 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1758 conn
->viewer_session
= viewer_session_create();
1759 if (!conn
->viewer_session
) {
1760 ERR("Allocation viewer session");
1761 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1766 health_code_update();
1767 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1771 health_code_update();
1779 * Detach a viewer session.
1781 * Return 0 on success or else a negative value.
1784 int viewer_detach_session(struct relay_connection
*conn
)
1787 struct lttng_viewer_detach_session_response response
;
1788 struct lttng_viewer_detach_session_request request
;
1789 struct relay_session
*session
= NULL
;
1790 uint64_t viewer_session_to_close
;
1792 DBG("Viewer detach session received");
1796 health_code_update();
1798 /* Receive the request from the connected client. */
1799 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1803 viewer_session_to_close
= be64toh(request
.session_id
);
1805 if (!conn
->viewer_session
) {
1806 DBG("Client trying to detach before creating a live viewer session");
1807 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1811 health_code_update();
1813 memset(&response
, 0, sizeof(response
));
1814 DBG("Detaching from session ID %" PRIu64
, viewer_session_to_close
);
1816 session
= session_get_by_id(be64toh(request
.session_id
));
1818 DBG("Relay session %" PRIu64
" not found",
1819 (uint64_t) be64toh(request
.session_id
));
1820 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_UNK
);
1824 ret
= viewer_session_is_attached(conn
->viewer_session
, session
);
1826 DBG("Not attached to this session");
1827 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_ERR
);
1828 goto send_reply_put
;
1831 viewer_session_close_one_session(conn
->viewer_session
, session
);
1832 response
.status
= htobe32(LTTNG_VIEWER_DETACH_SESSION_OK
);
1833 DBG("Session %" PRIu64
" detached.", viewer_session_to_close
);
1836 session_put(session
);
1839 health_code_update();
1840 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1844 health_code_update();
1852 * live_relay_unknown_command: send -1 if received unknown command
1855 void live_relay_unknown_command(struct relay_connection
*conn
)
1857 struct lttcomm_relayd_generic_reply reply
;
1859 memset(&reply
, 0, sizeof(reply
));
1860 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1861 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1865 * Process the commands received on the control socket
1868 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1869 struct relay_connection
*conn
)
1874 msg_value
= be32toh(recv_hdr
->cmd
);
1877 * Make sure we've done the version check before any command other then a
1878 * new client connection.
1880 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1881 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1886 switch (msg_value
) {
1887 case LTTNG_VIEWER_CONNECT
:
1888 ret
= viewer_connect(conn
);
1890 case LTTNG_VIEWER_LIST_SESSIONS
:
1891 ret
= viewer_list_sessions(conn
);
1893 case LTTNG_VIEWER_ATTACH_SESSION
:
1894 ret
= viewer_attach_session(conn
);
1896 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1897 ret
= viewer_get_next_index(conn
);
1899 case LTTNG_VIEWER_GET_PACKET
:
1900 ret
= viewer_get_packet(conn
);
1902 case LTTNG_VIEWER_GET_METADATA
:
1903 ret
= viewer_get_metadata(conn
);
1905 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1906 ret
= viewer_get_new_streams(conn
);
1908 case LTTNG_VIEWER_CREATE_SESSION
:
1909 ret
= viewer_create_session(conn
);
1911 case LTTNG_VIEWER_DETACH_SESSION
:
1912 ret
= viewer_detach_session(conn
);
1915 ERR("Received unknown viewer command (%u)",
1916 be32toh(recv_hdr
->cmd
));
1917 live_relay_unknown_command(conn
);
1927 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1931 (void) lttng_poll_del(events
, pollfd
);
1933 ret
= close(pollfd
);
1935 ERR("Closing pollfd %d", pollfd
);
1940 * This thread does the actual work
1943 void *thread_worker(void *data
)
1947 struct lttng_poll_event events
;
1948 struct lttng_ht
*viewer_connections_ht
;
1949 struct lttng_ht_iter iter
;
1950 struct lttng_viewer_cmd recv_hdr
;
1951 struct relay_connection
*destroy_conn
;
1953 DBG("[thread] Live viewer relay worker started");
1955 rcu_register_thread();
1957 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1959 if (testpoint(relayd_thread_live_worker
)) {
1960 goto error_testpoint
;
1963 /* table of connections indexed on socket */
1964 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1965 if (!viewer_connections_ht
) {
1966 goto viewer_connections_ht_error
;
1969 ret
= create_thread_poll_set(&events
, 2);
1971 goto error_poll_create
;
1974 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1983 health_code_update();
1985 /* Infinite blocking call, waiting for transmission */
1986 DBG3("Relayd live viewer worker thread polling...");
1987 health_poll_entry();
1988 ret
= lttng_poll_wait(&events
, -1);
1992 * Restart interrupted system call.
1994 if (errno
== EINTR
) {
2003 * Process control. The control connection is prioritised so we don't
2004 * starve it with high throughput tracing data on the data
2007 for (i
= 0; i
< nb_fd
; i
++) {
2008 /* Fetch once the poll data */
2009 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
2010 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2012 health_code_update();
2014 /* Thread quit pipe has been closed. Killing thread. */
2015 ret
= check_thread_quit_pipe(pollfd
, revents
);
2021 /* Inspect the relay conn pipe for new connection. */
2022 if (pollfd
== live_conn_pipe
[0]) {
2023 if (revents
& LPOLLIN
) {
2024 struct relay_connection
*conn
;
2026 ret
= lttng_read(live_conn_pipe
[0],
2027 &conn
, sizeof(conn
));
2031 lttng_poll_add(&events
, conn
->sock
->fd
,
2032 LPOLLIN
| LPOLLRDHUP
);
2033 connection_ht_add(viewer_connections_ht
, conn
);
2034 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
2035 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2036 ERR("Relay live pipe error");
2039 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2043 /* Connection activity. */
2044 struct relay_connection
*conn
;
2046 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
2051 if (revents
& LPOLLIN
) {
2052 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
2053 sizeof(recv_hdr
), 0);
2055 /* Connection closed. */
2056 cleanup_connection_pollfd(&events
, pollfd
);
2057 /* Put "create" ownership reference. */
2058 connection_put(conn
);
2059 DBG("Viewer control conn closed with %d", pollfd
);
2061 ret
= process_control(&recv_hdr
, conn
);
2063 /* Clear the session on error. */
2064 cleanup_connection_pollfd(&events
, pollfd
);
2065 /* Put "create" ownership reference. */
2066 connection_put(conn
);
2067 DBG("Viewer connection closed with %d", pollfd
);
2070 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2071 cleanup_connection_pollfd(&events
, pollfd
);
2072 /* Put "create" ownership reference. */
2073 connection_put(conn
);
2075 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2076 connection_put(conn
);
2079 /* Put local "get_by_sock" reference. */
2080 connection_put(conn
);
2087 lttng_poll_clean(&events
);
2089 /* Cleanup remaining connection object. */
2091 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2094 health_code_update();
2095 connection_put(destroy_conn
);
2099 lttng_ht_destroy(viewer_connections_ht
);
2100 viewer_connections_ht_error
:
2101 /* Close relay conn pipes */
2102 utils_close_pipe(live_conn_pipe
);
2104 DBG("Viewer worker thread exited with error");
2106 DBG("Viewer worker thread cleanup complete");
2110 ERR("Health error occurred in %s", __func__
);
2112 health_unregister(health_relayd
);
2113 if (lttng_relay_stop_threads()) {
2114 ERR("Error stopping threads");
2116 rcu_unregister_thread();
2121 * Create the relay command pipe to wake thread_manage_apps.
2122 * Closed in cleanup().
2124 static int create_conn_pipe(void)
2126 return utils_create_pipe_cloexec(live_conn_pipe
);
2129 int relayd_live_join(void)
2131 int ret
, retval
= 0;
2134 ret
= pthread_join(live_listener_thread
, &status
);
2137 PERROR("pthread_join live listener");
2141 ret
= pthread_join(live_worker_thread
, &status
);
2144 PERROR("pthread_join live worker");
2148 ret
= pthread_join(live_dispatcher_thread
, &status
);
2151 PERROR("pthread_join live dispatcher");
2155 cleanup_relayd_live();
2163 int relayd_live_create(struct lttng_uri
*uri
)
2165 int ret
= 0, retval
= 0;
2171 goto exit_init_data
;
2175 /* Check if daemon is UID = 0 */
2176 is_root
= !getuid();
2179 if (live_uri
->port
< 1024) {
2180 ERR("Need to be root to use ports < 1024");
2182 goto exit_init_data
;
2186 /* Setup the thread apps communication pipe. */
2187 if (create_conn_pipe()) {
2189 goto exit_init_data
;
2192 /* Init relay command queue. */
2193 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2195 /* Set up max poll set size */
2196 if (lttng_poll_set_max_size()) {
2198 goto exit_init_data
;
2201 /* Setup the dispatcher thread */
2202 ret
= pthread_create(&live_dispatcher_thread
, default_pthread_attr(),
2203 thread_dispatcher
, (void *) NULL
);
2206 PERROR("pthread_create viewer dispatcher");
2208 goto exit_dispatcher_thread
;
2211 /* Setup the worker thread */
2212 ret
= pthread_create(&live_worker_thread
, default_pthread_attr(),
2213 thread_worker
, NULL
);
2216 PERROR("pthread_create viewer worker");
2218 goto exit_worker_thread
;
2221 /* Setup the listener thread */
2222 ret
= pthread_create(&live_listener_thread
, default_pthread_attr(),
2223 thread_listener
, (void *) NULL
);
2226 PERROR("pthread_create viewer listener");
2228 goto exit_listener_thread
;
2232 * All OK, started all threads.
2237 * Join on the live_listener_thread should anything be added after
2238 * the live_listener thread's creation.
2241 exit_listener_thread
:
2243 ret
= pthread_join(live_worker_thread
, &status
);
2246 PERROR("pthread_join live worker");
2251 ret
= pthread_join(live_dispatcher_thread
, &status
);
2254 PERROR("pthread_join live dispatcher");
2257 exit_dispatcher_thread
:
2260 cleanup_relayd_live();