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.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
38 #include <urcu/futex.h>
39 #include <urcu/uatomic.h>
40 #include <urcu/rculist.h>
45 #include <lttng/lttng.h>
46 #include <common/common.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/socket.h>
49 #include <common/compat/endian.h>
50 #include <common/defaults.h>
51 #include <common/futex.h>
52 #include <common/index/index.h>
53 #include <common/sessiond-comm/sessiond-comm.h>
54 #include <common/sessiond-comm/inet.h>
55 #include <common/sessiond-comm/relayd.h>
56 #include <common/uri.h>
57 #include <common/utils.h>
61 #include "lttng-relayd.h"
63 #include "health-relayd.h"
64 #include "testpoint.h"
65 #include "viewer-stream.h"
68 #include "ctf-trace.h"
69 #include "connection.h"
70 #include "viewer-session.h"
72 #define SESSION_BUF_DEFAULT_COUNT 16
74 static struct lttng_uri
*live_uri
;
77 * This pipe is used to inform the worker thread that a command is queued and
78 * ready to be processed.
80 static int live_conn_pipe
[2] = { -1, -1 };
82 /* Shared between threads */
83 static int live_dispatch_thread_exit
;
85 static pthread_t live_listener_thread
;
86 static pthread_t live_dispatcher_thread
;
87 static pthread_t live_worker_thread
;
90 * Relay command queue.
92 * The live_thread_listener and live_thread_dispatcher communicate with this
95 static struct relay_conn_queue viewer_conn_queue
;
97 static uint64_t last_relay_viewer_session_id
;
98 static pthread_mutex_t last_relay_viewer_session_id_lock
=
99 PTHREAD_MUTEX_INITIALIZER
;
105 void cleanup_relayd_live(void)
113 * Receive a request buffer using a given socket, destination allocated buffer
116 * Return the size of the received message or else a negative value on error
117 * with errno being set by recvmsg() syscall.
120 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
124 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
125 if (ret
< 0 || ret
!= size
) {
127 /* Orderly shutdown. Not necessary to print an error. */
128 DBG("Socket %d did an orderly shutdown", sock
->fd
);
130 ERR("Relay failed to receive request.");
139 * Send a response buffer using a given socket, source allocated buffer of
142 * Return the size of the sent message or else a negative value on error with
143 * errno being set by sendmsg() syscall.
146 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
150 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
152 ERR("Relayd failed to send response.");
159 * Atomically check if new streams got added in one of the sessions attached
160 * and reset the flag to 0.
162 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
166 int check_new_streams(struct relay_connection
*conn
)
168 struct relay_session
*session
;
169 unsigned long current_val
;
172 if (!conn
->viewer_session
) {
176 cds_list_for_each_entry_rcu(session
,
177 &conn
->viewer_session
->session_list
,
178 viewer_session_node
) {
179 if (!session_get(session
)) {
182 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
184 session_put(session
);
195 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
196 * this function should ignore the sent flag or not.
198 * Return 0 on success or else a negative value.
201 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
202 struct relay_session
*session
, unsigned int ignore_sent_flag
)
205 struct lttng_viewer_stream send_stream
;
206 struct lttng_ht_iter iter
;
207 struct relay_viewer_stream
*vstream
;
211 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
213 struct ctf_trace
*ctf_trace
;
215 health_code_update();
217 if (!viewer_stream_get(vstream
)) {
221 pthread_mutex_lock(&vstream
->stream
->lock
);
222 /* Ignore if not the same session. */
223 if (vstream
->stream
->trace
->session
->id
!= session
->id
||
224 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
225 pthread_mutex_unlock(&vstream
->stream
->lock
);
226 viewer_stream_put(vstream
);
230 ctf_trace
= vstream
->stream
->trace
;
231 send_stream
.id
= htobe64(vstream
->stream
->stream_handle
);
232 send_stream
.ctf_trace_id
= htobe64(ctf_trace
->id
);
233 send_stream
.metadata_flag
= htobe32(
234 vstream
->stream
->is_metadata
);
235 if (lttng_strncpy(send_stream
.path_name
, vstream
->path_name
,
236 sizeof(send_stream
.path_name
))) {
237 pthread_mutex_unlock(&vstream
->stream
->lock
);
238 viewer_stream_put(vstream
);
239 ret
= -1; /* Error. */
242 if (lttng_strncpy(send_stream
.channel_name
,
243 vstream
->channel_name
,
244 sizeof(send_stream
.channel_name
))) {
245 pthread_mutex_unlock(&vstream
->stream
->lock
);
246 viewer_stream_put(vstream
);
247 ret
= -1; /* Error. */
251 DBG("Sending stream %" PRIu64
" to viewer",
252 vstream
->stream
->stream_handle
);
253 vstream
->sent_flag
= 1;
254 pthread_mutex_unlock(&vstream
->stream
->lock
);
256 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
257 viewer_stream_put(vstream
);
271 * Create every viewer stream possible for the given session with the seek
272 * type. Three counters *can* be return which are in order the total amount of
273 * viewer stream of the session, the number of unsent stream and the number of
274 * stream created. Those counters can be NULL and thus will be ignored.
276 * Return 0 on success or else a negative value.
279 int make_viewer_streams(struct relay_session
*session
,
280 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
281 uint32_t *nb_created
, bool *closed
)
284 struct lttng_ht_iter iter
;
285 struct ctf_trace
*ctf_trace
;
290 * Hold the session lock to ensure that we see either none or
291 * all initial streams for a session, but no intermediate state.
293 pthread_mutex_lock(&session
->lock
);
295 if (session
->connection_closed
) {
300 * Create viewer streams for relay streams that are ready to be
301 * used for a the given session id only.
304 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
, &iter
.iter
, ctf_trace
,
306 struct relay_stream
*stream
;
308 health_code_update();
310 if (!ctf_trace_get(ctf_trace
)) {
314 cds_list_for_each_entry_rcu(stream
, &ctf_trace
->stream_list
, stream_node
) {
315 struct relay_viewer_stream
*vstream
;
317 if (!stream_get(stream
)) {
321 * stream published is protected by the session lock.
323 if (!stream
->published
) {
326 vstream
= viewer_stream_get_by_id(stream
->stream_handle
);
328 vstream
= viewer_stream_create(stream
, seek_t
);
331 ctf_trace_put(ctf_trace
);
337 /* Update number of created stream counter. */
341 * Ensure a self-reference is preserved even
342 * after we have put our local reference.
344 if (!viewer_stream_get(vstream
)) {
345 ERR("Unable to get self-reference on viewer stream, logic error.");
349 if (!vstream
->sent_flag
&& nb_unsent
) {
350 /* Update number of unsent stream counter. */
354 /* Update number of total stream counter. */
356 if (stream
->is_metadata
) {
357 if (!stream
->closed
||
358 stream
->metadata_received
> vstream
->metadata_sent
) {
362 if (!stream
->closed
||
363 !(((int64_t) (stream
->prev_seq
- stream
->last_net_seq_num
)) >= 0)) {
369 /* Put local reference. */
370 viewer_stream_put(vstream
);
374 ctf_trace_put(ctf_trace
);
381 pthread_mutex_unlock(&session
->lock
);
385 int relayd_live_stop(void)
387 /* Stop dispatch thread */
388 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
389 futex_nto1_wake(&viewer_conn_queue
.futex
);
394 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
397 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
401 if (events
== NULL
|| size
== 0) {
406 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
412 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
424 * Check if the thread quit pipe was triggered.
426 * Return 1 if it was triggered else 0;
429 int check_thread_quit_pipe(int fd
, uint32_t events
)
431 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
439 * Create and init socket from uri.
442 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
445 struct lttcomm_sock
*sock
= NULL
;
447 sock
= lttcomm_alloc_sock_from_uri(uri
);
449 ERR("Allocating socket");
453 ret
= lttcomm_create_sock(sock
);
457 DBG("Listening on sock %d for live", sock
->fd
);
459 ret
= sock
->ops
->bind(sock
);
464 ret
= sock
->ops
->listen(sock
, -1);
474 lttcomm_destroy_sock(sock
);
480 * This thread manages the listening for new connections on the network
483 void *thread_listener(void *data
)
485 int i
, ret
, pollfd
, err
= -1;
486 uint32_t revents
, nb_fd
;
487 struct lttng_poll_event events
;
488 struct lttcomm_sock
*live_control_sock
;
490 DBG("[thread] Relay live listener started");
492 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
494 health_code_update();
496 live_control_sock
= init_socket(live_uri
);
497 if (!live_control_sock
) {
498 goto error_sock_control
;
501 /* Pass 2 as size here for the thread quit pipe and control sockets. */
502 ret
= create_thread_poll_set(&events
, 2);
504 goto error_create_poll
;
507 /* Add the control socket */
508 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
513 lttng_relay_notify_ready();
515 if (testpoint(relayd_thread_live_listener
)) {
516 goto error_testpoint
;
520 health_code_update();
522 DBG("Listener accepting live viewers connections");
526 ret
= lttng_poll_wait(&events
, -1);
530 * Restart interrupted system call.
532 if (errno
== EINTR
) {
539 DBG("Relay new viewer connection received");
540 for (i
= 0; i
< nb_fd
; i
++) {
541 health_code_update();
543 /* Fetch once the poll data */
544 revents
= LTTNG_POLL_GETEV(&events
, i
);
545 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
548 /* No activity for this FD (poll implementation). */
552 /* Thread quit pipe has been closed. Killing thread. */
553 ret
= check_thread_quit_pipe(pollfd
, revents
);
559 if (revents
& LPOLLIN
) {
561 * A new connection is requested, therefore a
562 * viewer connection is allocated in this
563 * thread, enqueued to a global queue and
564 * dequeued (and freed) in the worker thread.
567 struct relay_connection
*new_conn
;
568 struct lttcomm_sock
*newsock
;
570 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
572 PERROR("accepting control sock");
575 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
577 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
580 PERROR("setsockopt inet");
581 lttcomm_destroy_sock(newsock
);
584 new_conn
= connection_create(newsock
, RELAY_CONNECTION_UNKNOWN
);
586 lttcomm_destroy_sock(newsock
);
589 /* Ownership assumed by the connection. */
592 /* Enqueue request for the dispatcher thread. */
593 cds_wfcq_enqueue(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
,
597 * Wake the dispatch queue futex.
598 * Implicit memory barrier with the
599 * exchange in cds_wfcq_enqueue.
601 futex_nto1_wake(&viewer_conn_queue
.futex
);
602 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
603 ERR("socket poll error");
606 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
616 lttng_poll_clean(&events
);
618 if (live_control_sock
->fd
>= 0) {
619 ret
= live_control_sock
->ops
->close(live_control_sock
);
624 lttcomm_destroy_sock(live_control_sock
);
628 DBG("Live viewer listener thread exited with error");
630 health_unregister(health_relayd
);
631 DBG("Live viewer listener thread cleanup complete");
632 if (lttng_relay_stop_threads()) {
633 ERR("Error stopping threads");
639 * This thread manages the dispatching of the requests to worker threads
642 void *thread_dispatcher(void *data
)
646 struct cds_wfcq_node
*node
;
647 struct relay_connection
*conn
= NULL
;
649 DBG("[thread] Live viewer relay dispatcher started");
651 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
653 if (testpoint(relayd_thread_live_dispatcher
)) {
654 goto error_testpoint
;
657 health_code_update();
659 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
660 health_code_update();
662 /* Atomically prepare the queue futex */
663 futex_nto1_prepare(&viewer_conn_queue
.futex
);
666 health_code_update();
668 /* Dequeue commands */
669 node
= cds_wfcq_dequeue_blocking(&viewer_conn_queue
.head
,
670 &viewer_conn_queue
.tail
);
672 DBG("Woken up but nothing in the live-viewer "
673 "relay command queue");
674 /* Continue thread execution */
677 conn
= caa_container_of(node
, struct relay_connection
, qnode
);
678 DBG("Dispatching viewer request waiting on sock %d",
682 * Inform worker thread of the new request. This
683 * call is blocking so we can be assured that
684 * the data will be read at some point in time
685 * or wait to the end of the world :)
687 ret
= lttng_write(live_conn_pipe
[1], &conn
, sizeof(conn
));
689 PERROR("write conn pipe");
690 connection_put(conn
);
693 } while (node
!= NULL
);
695 /* Futex wait on queue. Blocking call on futex() */
697 futex_nto1_wait(&viewer_conn_queue
.futex
);
701 /* Normal exit, no error */
708 ERR("Health error occurred in %s", __func__
);
710 health_unregister(health_relayd
);
711 DBG("Live viewer dispatch thread dying");
712 if (lttng_relay_stop_threads()) {
713 ERR("Error stopping threads");
719 * Establish connection with the viewer and check the versions.
721 * Return 0 on success or else negative value.
724 int viewer_connect(struct relay_connection
*conn
)
727 struct lttng_viewer_connect reply
, msg
;
729 conn
->version_check_done
= 1;
731 health_code_update();
733 DBG("Viewer is establishing a connection to the relayd.");
735 ret
= recv_request(conn
->sock
, &msg
, sizeof(msg
));
740 health_code_update();
742 memset(&reply
, 0, sizeof(reply
));
743 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
744 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
746 /* Major versions must be the same */
747 if (reply
.major
!= be32toh(msg
.major
)) {
748 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
749 reply
.major
, be32toh(msg
.major
));
754 conn
->major
= reply
.major
;
755 /* We adapt to the lowest compatible version */
756 if (reply
.minor
<= be32toh(msg
.minor
)) {
757 conn
->minor
= reply
.minor
;
759 conn
->minor
= be32toh(msg
.minor
);
762 if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_COMMAND
) {
763 conn
->type
= RELAY_VIEWER_COMMAND
;
764 } else if (be32toh(msg
.type
) == LTTNG_VIEWER_CLIENT_NOTIFICATION
) {
765 conn
->type
= RELAY_VIEWER_NOTIFICATION
;
767 ERR("Unknown connection type : %u", be32toh(msg
.type
));
772 reply
.major
= htobe32(reply
.major
);
773 reply
.minor
= htobe32(reply
.minor
);
774 if (conn
->type
== RELAY_VIEWER_COMMAND
) {
776 * Increment outside of htobe64 macro, because the argument can
777 * be used more than once within the macro, and thus the
778 * operation may be undefined.
780 pthread_mutex_lock(&last_relay_viewer_session_id_lock
);
781 last_relay_viewer_session_id
++;
782 pthread_mutex_unlock(&last_relay_viewer_session_id_lock
);
783 reply
.viewer_session_id
= htobe64(last_relay_viewer_session_id
);
786 health_code_update();
788 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
793 health_code_update();
795 DBG("Version check done using protocol %u.%u", conn
->major
, conn
->minor
);
803 * Send the viewer the list of current sessions.
804 * We need to create a copy of the hash table content because otherwise
805 * we cannot assume the number of entries stays the same between getting
806 * the number of HT elements and iteration over the HT.
808 * Return 0 on success or else a negative value.
811 int viewer_list_sessions(struct relay_connection
*conn
)
814 struct lttng_viewer_list_sessions session_list
;
815 struct lttng_ht_iter iter
;
816 struct relay_session
*session
;
817 struct lttng_viewer_session
*send_session_buf
= NULL
;
818 uint32_t buf_count
= SESSION_BUF_DEFAULT_COUNT
;
821 DBG("List sessions received");
823 send_session_buf
= zmalloc(SESSION_BUF_DEFAULT_COUNT
* sizeof(*send_session_buf
));
824 if (!send_session_buf
) {
829 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
831 struct lttng_viewer_session
*send_session
;
833 health_code_update();
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
));
846 send_session_buf
= newbuf
;
847 buf_count
= new_buf_count
;
849 send_session
= &send_session_buf
[count
];
850 if (lttng_strncpy(send_session
->session_name
,
851 session
->session_name
,
852 sizeof(send_session
->session_name
))) {
857 if (lttng_strncpy(send_session
->hostname
, session
->hostname
,
858 sizeof(send_session
->hostname
))) {
863 send_session
->id
= htobe64(session
->id
);
864 send_session
->live_timer
= htobe32(session
->live_timer
);
865 if (session
->viewer_attached
) {
866 send_session
->clients
= htobe32(1);
868 send_session
->clients
= htobe32(0);
870 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 ret
= make_viewer_streams(session
, LTTNG_VIEWER_SEEK_LAST
, &nb_total
, &nb_unsent
,
947 &nb_created
, &closed
);
949 goto end_put_session
;
951 /* Only send back the newly created streams with the unsent ones. */
952 nb_streams
= nb_created
+ nb_unsent
;
953 response
.streams_count
= htobe32(nb_streams
);
956 * If the session is closed, HUP when there are no more streams
959 if (closed
&& nb_total
== 0) {
961 response
.streams_count
= 0;
962 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
967 health_code_update();
968 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
970 goto end_put_session
;
972 health_code_update();
975 * Unknown or empty session, just return gracefully, the viewer
976 * knows what is happening.
978 if (!send_streams
|| !nb_streams
) {
980 goto end_put_session
;
984 * Send stream and *DON'T* ignore the sent flag so every viewer
985 * streams that were not sent from that point will be sent to
988 ret
= send_viewer_streams(conn
->sock
, session
, 0);
990 goto end_put_session
;
995 session_put(session
);
1002 * Send the viewer the list of current sessions.
1005 int viewer_attach_session(struct relay_connection
*conn
)
1007 int send_streams
= 0;
1009 uint32_t nb_streams
= 0;
1010 enum lttng_viewer_seek seek_type
;
1011 struct lttng_viewer_attach_session_request request
;
1012 struct lttng_viewer_attach_session_response response
;
1013 struct relay_session
*session
= NULL
;
1014 bool closed
= false;
1018 health_code_update();
1020 /* Receive the request from the connected client. */
1021 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1026 health_code_update();
1028 memset(&response
, 0, sizeof(response
));
1030 if (!conn
->viewer_session
) {
1031 DBG("Client trying to attach before creating a live viewer session");
1032 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NO_SESSION
);
1036 session
= session_get_by_id(be64toh(request
.session_id
));
1038 DBG("Relay session %" PRIu64
" not found",
1039 be64toh(request
.session_id
));
1040 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_UNK
);
1043 DBG("Attach session ID %" PRIu64
" received",
1044 be64toh(request
.session_id
));
1046 if (session
->live_timer
== 0) {
1047 DBG("Not live session");
1048 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_NOT_LIVE
);
1053 ret
= viewer_session_attach(conn
->viewer_session
, session
);
1055 DBG("Already a viewer attached");
1056 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_ALREADY
);
1060 switch (be32toh(request
.seek
)) {
1061 case LTTNG_VIEWER_SEEK_BEGINNING
:
1062 case LTTNG_VIEWER_SEEK_LAST
:
1063 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_OK
);
1064 seek_type
= be32toh(request
.seek
);
1067 ERR("Wrong seek parameter");
1068 response
.status
= htobe32(LTTNG_VIEWER_ATTACH_SEEK_ERR
);
1073 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
,
1076 goto end_put_session
;
1078 response
.streams_count
= htobe32(nb_streams
);
1081 * If the session is closed when the viewer is attaching, it
1082 * means some of the streams may have been concurrently removed,
1083 * so we don't allow the viewer to attach, even if there are
1084 * streams available.
1088 response
.streams_count
= 0;
1089 response
.status
= htobe32(LTTNG_VIEWER_NEW_STREAMS_HUP
);
1094 health_code_update();
1095 ret
= send_response(conn
->sock
, &response
, sizeof(response
));
1097 goto end_put_session
;
1099 health_code_update();
1102 * Unknown or empty session, just return gracefully, the viewer
1103 * knows what is happening.
1105 if (!send_streams
|| !nb_streams
) {
1107 goto end_put_session
;
1110 /* Send stream and ignore the sent flag. */
1111 ret
= send_viewer_streams(conn
->sock
, session
, 1);
1113 goto end_put_session
;
1118 session_put(session
);
1125 * Open the index file if needed for the given vstream.
1127 * If an index file is successfully opened, the vstream index_fd set with
1130 * Return 0 on success, a negative value on error (-ENOENT if not ready yet).
1132 * Called with rstream lock held.
1134 static int try_open_index(struct relay_viewer_stream
*vstream
,
1135 struct relay_stream
*rstream
)
1139 if (vstream
->index_fd
) {
1144 * First time, we open the index file and at least one index is ready.
1146 if (rstream
->index_received_seqcount
== 0) {
1150 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1151 vstream
->stream
->tracefile_count
,
1152 vstream
->current_tracefile_id
);
1154 vstream
->index_fd
= stream_fd_create(ret
);
1155 if (!vstream
->index_fd
) {
1171 * Check the status of the index for the given stream. This function
1172 * updates the index structure if needed and can put (close) the vstream
1173 * in the HUP situation.
1175 * Return 0 means that we can proceed with the index. A value of 1 means
1176 * that the index has been updated and is ready to be sent to the
1177 * client. A negative value indicates an error that can't be handled.
1179 * Called with rstream lock held.
1181 static int check_index_status(struct relay_viewer_stream
*vstream
,
1182 struct relay_stream
*rstream
, struct ctf_trace
*trace
,
1183 struct lttng_viewer_index
*index
)
1187 if (trace
->session
->connection_closed
1188 && rstream
->index_received_seqcount
1189 == vstream
->index_sent_seqcount
) {
1190 /* Last index sent and session connection is closed. */
1191 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1193 } else if (rstream
->beacon_ts_end
!= -1ULL &&
1194 rstream
->index_received_seqcount
1195 == vstream
->index_sent_seqcount
) {
1197 * We've received a synchronization beacon and the last index
1198 * available has been sent, the index for now is inactive.
1200 * In this case, we have received a beacon which allows us to
1201 * inform the client of a time interval during which we can
1202 * guarantee that there are no events to read (and never will
1205 index
->status
= htobe32(LTTNG_VIEWER_INDEX_INACTIVE
);
1206 index
->timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1207 index
->stream_id
= htobe64(rstream
->ctf_stream_id
);
1209 } else if (rstream
->index_received_seqcount
1210 == vstream
->index_sent_seqcount
) {
1212 * This checks whether received == sent seqcount. In
1213 * this case, we have not received a beacon. Therefore,
1214 * we can only ask the client to retry later.
1216 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1218 } else if (!tracefile_array_seq_in_file(rstream
->tfa
,
1219 vstream
->current_tracefile_id
,
1220 vstream
->index_sent_seqcount
)) {
1222 * The next index we want to send cannot be read either
1223 * because we need to perform a rotation, or due to
1224 * the producer having overwritten its trace file.
1226 DBG("Viewer stream %" PRIu64
" rotation",
1227 vstream
->stream
->stream_handle
);
1228 ret
= viewer_stream_rotate(vstream
);
1231 } else if (ret
== 1) {
1232 /* EOF across entire stream. */
1233 index
->status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1237 * If we have been pushed due to overwrite, it
1238 * necessarily means there is data that can be read in
1239 * the stream. If we rotated because we reached the end
1240 * of a tracefile, it means the following tracefile
1241 * needs to contain at least one index, else we would
1242 * have already returned LTTNG_VIEWER_INDEX_RETRY to the
1243 * viewer. The updated index_sent_seqcount needs to
1244 * point to a readable index entry now.
1246 * In the case where we "rotate" on a single file, we
1247 * can end up in a case where the requested index is
1248 * still unavailable.
1250 if (rstream
->tracefile_count
== 1 &&
1251 !tracefile_array_seq_in_file(
1253 vstream
->current_tracefile_id
,
1254 vstream
->index_sent_seqcount
)) {
1255 index
->status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1258 assert(tracefile_array_seq_in_file(rstream
->tfa
,
1259 vstream
->current_tracefile_id
,
1260 vstream
->index_sent_seqcount
));
1262 /* ret == 0 means successful so we continue. */
1268 viewer_stream_put(vstream
);
1274 * Send the next index for a stream.
1276 * Return 0 on success or else a negative value.
1279 int viewer_get_next_index(struct relay_connection
*conn
)
1283 struct lttng_viewer_get_next_index request_index
;
1284 struct lttng_viewer_index viewer_index
;
1285 struct ctf_packet_index packet_index
;
1286 struct relay_viewer_stream
*vstream
= NULL
;
1287 struct relay_stream
*rstream
= NULL
;
1288 struct ctf_trace
*ctf_trace
= NULL
;
1289 struct relay_viewer_stream
*metadata_viewer_stream
= NULL
;
1293 DBG("Viewer get next index");
1295 memset(&viewer_index
, 0, sizeof(viewer_index
));
1296 health_code_update();
1298 ret
= recv_request(conn
->sock
, &request_index
, sizeof(request_index
));
1302 health_code_update();
1304 vstream
= viewer_stream_get_by_id(be64toh(request_index
.stream_id
));
1306 DBG("Client requested index of unknown stream id %" PRIu64
,
1307 be64toh(request_index
.stream_id
));
1308 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1312 /* Use back. ref. Protected by refcounts. */
1313 rstream
= vstream
->stream
;
1314 ctf_trace
= rstream
->trace
;
1316 /* metadata_viewer_stream may be NULL. */
1317 metadata_viewer_stream
=
1318 ctf_trace_get_viewer_metadata_stream(ctf_trace
);
1320 pthread_mutex_lock(&rstream
->lock
);
1323 * The viewer should not ask for index on metadata stream.
1325 if (rstream
->is_metadata
) {
1326 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_HUP
);
1330 /* Try to open an index if one is needed for that stream. */
1331 ret
= try_open_index(vstream
, rstream
);
1333 if (ret
== -ENOENT
) {
1335 * The index is created only when the first data
1336 * packet arrives, it might not be ready at the
1337 * beginning of the session
1339 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_RETRY
);
1341 /* Unhandled error. */
1342 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1347 ret
= check_index_status(vstream
, rstream
, ctf_trace
, &viewer_index
);
1350 } else if (ret
== 1) {
1352 * We have no index to send and check_index_status has populated
1353 * viewer_index's status.
1357 /* At this point, ret is 0 thus we will be able to read the index. */
1361 * vstream->stream_fd may be NULL if it has been closed by
1362 * tracefile rotation, or if we are at the beginning of the
1363 * stream. We open the data stream file here to protect against
1364 * overwrite caused by tracefile rotation (in association with
1365 * unlink performed before overwrite).
1367 if (!vstream
->stream_fd
) {
1368 char fullpath
[PATH_MAX
];
1370 if (vstream
->stream
->tracefile_count
> 0) {
1371 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
,
1373 vstream
->channel_name
,
1374 vstream
->current_tracefile_id
);
1376 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s",
1378 vstream
->channel_name
);
1383 ret
= open(fullpath
, O_RDONLY
);
1385 PERROR("Relay opening trace file");
1388 vstream
->stream_fd
= stream_fd_create(ret
);
1389 if (!vstream
->stream_fd
) {
1397 ret
= check_new_streams(conn
);
1399 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1401 } else if (ret
== 1) {
1402 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1405 read_ret
= lttng_read(vstream
->index_fd
->fd
, &packet_index
,
1406 sizeof(packet_index
));
1407 if (read_ret
< sizeof(packet_index
)) {
1408 ERR("Relay reading index file %d returned %zd",
1409 vstream
->index_fd
->fd
, read_ret
);
1410 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_ERR
);
1413 viewer_index
.status
= htobe32(LTTNG_VIEWER_INDEX_OK
);
1414 vstream
->index_sent_seqcount
++;
1418 * Indexes are stored in big endian, no need to switch before sending.
1420 DBG("Sending viewer index for stream %" PRIu64
" offset %" PRIu64
,
1421 rstream
->stream_handle
,
1422 be64toh(packet_index
.offset
));
1423 viewer_index
.offset
= packet_index
.offset
;
1424 viewer_index
.packet_size
= packet_index
.packet_size
;
1425 viewer_index
.content_size
= packet_index
.content_size
;
1426 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1427 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1428 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1429 viewer_index
.stream_id
= packet_index
.stream_id
;
1433 pthread_mutex_unlock(&rstream
->lock
);
1436 if (metadata_viewer_stream
) {
1437 pthread_mutex_lock(&metadata_viewer_stream
->stream
->lock
);
1438 DBG("get next index metadata check: recv %" PRIu64
1440 metadata_viewer_stream
->stream
->metadata_received
,
1441 metadata_viewer_stream
->metadata_sent
);
1442 if (!metadata_viewer_stream
->stream
->metadata_received
||
1443 metadata_viewer_stream
->stream
->metadata_received
>
1444 metadata_viewer_stream
->metadata_sent
) {
1445 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1447 pthread_mutex_unlock(&metadata_viewer_stream
->stream
->lock
);
1450 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1451 health_code_update();
1453 ret
= send_response(conn
->sock
, &viewer_index
, sizeof(viewer_index
));
1457 health_code_update();
1460 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1461 vstream
->index_sent_seqcount
,
1462 vstream
->stream
->stream_handle
);
1465 if (metadata_viewer_stream
) {
1466 viewer_stream_put(metadata_viewer_stream
);
1469 viewer_stream_put(vstream
);
1474 pthread_mutex_unlock(&rstream
->lock
);
1475 if (metadata_viewer_stream
) {
1476 viewer_stream_put(metadata_viewer_stream
);
1478 viewer_stream_put(vstream
);
1483 * Send the next index for a stream
1485 * Return 0 on success or else a negative value.
1488 int viewer_get_packet(struct relay_connection
*conn
)
1490 int ret
, send_data
= 0;
1494 struct lttng_viewer_get_packet get_packet_info
;
1495 struct lttng_viewer_trace_packet reply
;
1496 struct relay_viewer_stream
*vstream
= NULL
;
1498 DBG2("Relay get data packet");
1500 health_code_update();
1502 ret
= recv_request(conn
->sock
, &get_packet_info
,
1503 sizeof(get_packet_info
));
1507 health_code_update();
1509 /* From this point on, the error label can be reached. */
1510 memset(&reply
, 0, sizeof(reply
));
1512 vstream
= viewer_stream_get_by_id(be64toh(get_packet_info
.stream_id
));
1514 DBG("Client requested packet of unknown stream id %" PRIu64
,
1515 be64toh(get_packet_info
.stream_id
));
1516 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1517 goto send_reply_nolock
;
1520 pthread_mutex_lock(&vstream
->stream
->lock
);
1522 len
= be32toh(get_packet_info
.len
);
1523 data
= zmalloc(len
);
1525 PERROR("relay data zmalloc");
1529 ret
= lseek(vstream
->stream_fd
->fd
, be64toh(get_packet_info
.offset
),
1532 PERROR("lseek fd %d to offset %" PRIu64
, vstream
->stream_fd
->fd
,
1533 be64toh(get_packet_info
.offset
));
1536 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1537 if (read_len
< len
) {
1538 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1539 vstream
->stream_fd
->fd
,
1540 be64toh(get_packet_info
.offset
));
1543 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_OK
);
1544 reply
.len
= htobe32(len
);
1549 reply
.status
= htobe32(LTTNG_VIEWER_GET_PACKET_ERR
);
1553 pthread_mutex_unlock(&vstream
->stream
->lock
);
1556 reply
.flags
= htobe32(reply
.flags
);
1558 health_code_update();
1560 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1564 health_code_update();
1567 health_code_update();
1568 ret
= send_response(conn
->sock
, data
, len
);
1572 health_code_update();
1575 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1576 be64toh(get_packet_info
.stream_id
));
1582 viewer_stream_put(vstream
);
1588 * Send the session's metadata
1590 * Return 0 on success else a negative value.
1593 int viewer_get_metadata(struct relay_connection
*conn
)
1599 struct lttng_viewer_get_metadata request
;
1600 struct lttng_viewer_metadata_packet reply
;
1601 struct relay_viewer_stream
*vstream
= NULL
;
1605 DBG("Relay get metadata");
1607 health_code_update();
1609 ret
= recv_request(conn
->sock
, &request
, sizeof(request
));
1613 health_code_update();
1615 memset(&reply
, 0, sizeof(reply
));
1617 vstream
= viewer_stream_get_by_id(be64toh(request
.stream_id
));
1620 * The metadata stream can be closed by a CLOSE command
1621 * just before we attach. It can also be closed by
1622 * per-pid tracing during tracing. Therefore, it is
1623 * possible that we cannot find this viewer stream.
1624 * Reply back to the client with an error if we cannot
1627 DBG("Client requested metadata of unknown stream id %" PRIu64
,
1628 be64toh(request
.stream_id
));
1629 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1632 pthread_mutex_lock(&vstream
->stream
->lock
);
1633 if (!vstream
->stream
->is_metadata
) {
1634 ERR("Invalid metadata stream");
1638 assert(vstream
->metadata_sent
<= vstream
->stream
->metadata_received
);
1640 len
= vstream
->stream
->metadata_received
- vstream
->metadata_sent
;
1642 reply
.status
= htobe32(LTTNG_VIEWER_NO_NEW_METADATA
);
1646 /* first time, we open the metadata file */
1647 if (!vstream
->stream_fd
) {
1648 char fullpath
[PATH_MAX
];
1650 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", vstream
->path_name
,
1651 vstream
->channel_name
);
1655 ret
= open(fullpath
, O_RDONLY
);
1657 PERROR("Relay opening metadata file");
1660 vstream
->stream_fd
= stream_fd_create(ret
);
1661 if (!vstream
->stream_fd
) {
1669 reply
.len
= htobe64(len
);
1670 data
= zmalloc(len
);
1672 PERROR("viewer metadata zmalloc");
1676 read_len
= lttng_read(vstream
->stream_fd
->fd
, data
, len
);
1677 if (read_len
< len
) {
1678 PERROR("Relay reading metadata file");
1681 vstream
->metadata_sent
+= read_len
;
1682 if (vstream
->metadata_sent
== vstream
->stream
->metadata_received
1683 && vstream
->stream
->closed
) {
1684 /* Release ownership for the viewer metadata stream. */
1685 viewer_stream_put(vstream
);
1688 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_OK
);
1693 reply
.status
= htobe32(LTTNG_VIEWER_METADATA_ERR
);
1696 health_code_update();
1698 pthread_mutex_unlock(&vstream
->stream
->lock
);
1700 ret
= send_response(conn
->sock
, &reply
, sizeof(reply
));
1704 health_code_update();
1707 ret
= send_response(conn
->sock
, data
, len
);
1713 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1714 be64toh(request
.stream_id
));
1716 DBG("Metadata sent");
1722 viewer_stream_put(vstream
);
1728 * Create a viewer session.
1730 * Return 0 on success or else a negative value.
1733 int viewer_create_session(struct relay_connection
*conn
)
1736 struct lttng_viewer_create_session_response resp
;
1738 DBG("Viewer create session received");
1740 memset(&resp
, 0, sizeof(resp
));
1741 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_OK
);
1742 conn
->viewer_session
= viewer_session_create();
1743 if (!conn
->viewer_session
) {
1744 ERR("Allocation viewer session");
1745 resp
.status
= htobe32(LTTNG_VIEWER_CREATE_SESSION_ERR
);
1750 health_code_update();
1751 ret
= send_response(conn
->sock
, &resp
, sizeof(resp
));
1755 health_code_update();
1764 * live_relay_unknown_command: send -1 if received unknown command
1767 void live_relay_unknown_command(struct relay_connection
*conn
)
1769 struct lttcomm_relayd_generic_reply reply
;
1771 memset(&reply
, 0, sizeof(reply
));
1772 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1773 (void) send_response(conn
->sock
, &reply
, sizeof(reply
));
1777 * Process the commands received on the control socket
1780 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1781 struct relay_connection
*conn
)
1786 msg_value
= be32toh(recv_hdr
->cmd
);
1789 * Make sure we've done the version check before any command other then a
1790 * new client connection.
1792 if (msg_value
!= LTTNG_VIEWER_CONNECT
&& !conn
->version_check_done
) {
1793 ERR("Viewer conn value %" PRIu32
" before version check", msg_value
);
1798 switch (msg_value
) {
1799 case LTTNG_VIEWER_CONNECT
:
1800 ret
= viewer_connect(conn
);
1802 case LTTNG_VIEWER_LIST_SESSIONS
:
1803 ret
= viewer_list_sessions(conn
);
1805 case LTTNG_VIEWER_ATTACH_SESSION
:
1806 ret
= viewer_attach_session(conn
);
1808 case LTTNG_VIEWER_GET_NEXT_INDEX
:
1809 ret
= viewer_get_next_index(conn
);
1811 case LTTNG_VIEWER_GET_PACKET
:
1812 ret
= viewer_get_packet(conn
);
1814 case LTTNG_VIEWER_GET_METADATA
:
1815 ret
= viewer_get_metadata(conn
);
1817 case LTTNG_VIEWER_GET_NEW_STREAMS
:
1818 ret
= viewer_get_new_streams(conn
);
1820 case LTTNG_VIEWER_CREATE_SESSION
:
1821 ret
= viewer_create_session(conn
);
1824 ERR("Received unknown viewer command (%u)",
1825 be32toh(recv_hdr
->cmd
));
1826 live_relay_unknown_command(conn
);
1836 void cleanup_connection_pollfd(struct lttng_poll_event
*events
, int pollfd
)
1840 (void) lttng_poll_del(events
, pollfd
);
1842 ret
= close(pollfd
);
1844 ERR("Closing pollfd %d", pollfd
);
1849 * This thread does the actual work
1852 void *thread_worker(void *data
)
1856 struct lttng_poll_event events
;
1857 struct lttng_ht
*viewer_connections_ht
;
1858 struct lttng_ht_iter iter
;
1859 struct lttng_viewer_cmd recv_hdr
;
1860 struct relay_connection
*destroy_conn
;
1862 DBG("[thread] Live viewer relay worker started");
1864 rcu_register_thread();
1866 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1868 if (testpoint(relayd_thread_live_worker
)) {
1869 goto error_testpoint
;
1872 /* table of connections indexed on socket */
1873 viewer_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1874 if (!viewer_connections_ht
) {
1875 goto viewer_connections_ht_error
;
1878 ret
= create_thread_poll_set(&events
, 2);
1880 goto error_poll_create
;
1883 ret
= lttng_poll_add(&events
, live_conn_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1892 health_code_update();
1894 /* Infinite blocking call, waiting for transmission */
1895 DBG3("Relayd live viewer worker thread polling...");
1896 health_poll_entry();
1897 ret
= lttng_poll_wait(&events
, -1);
1901 * Restart interrupted system call.
1903 if (errno
== EINTR
) {
1912 * Process control. The control connection is prioritised so we don't
1913 * starve it with high throughput tracing data on the data
1916 for (i
= 0; i
< nb_fd
; i
++) {
1917 /* Fetch once the poll data */
1918 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1919 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1921 health_code_update();
1924 /* No activity for this FD (poll implementation). */
1928 /* Thread quit pipe has been closed. Killing thread. */
1929 ret
= check_thread_quit_pipe(pollfd
, revents
);
1935 /* Inspect the relay conn pipe for new connection. */
1936 if (pollfd
== live_conn_pipe
[0]) {
1937 if (revents
& LPOLLIN
) {
1938 struct relay_connection
*conn
;
1940 ret
= lttng_read(live_conn_pipe
[0],
1941 &conn
, sizeof(conn
));
1945 lttng_poll_add(&events
, conn
->sock
->fd
,
1946 LPOLLIN
| LPOLLRDHUP
);
1947 connection_ht_add(viewer_connections_ht
, conn
);
1948 DBG("Connection socket %d added to poll", conn
->sock
->fd
);
1949 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1950 ERR("Relay live pipe error");
1953 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1957 /* Connection activity. */
1958 struct relay_connection
*conn
;
1960 conn
= connection_get_by_sock(viewer_connections_ht
, pollfd
);
1965 if (revents
& LPOLLIN
) {
1966 ret
= conn
->sock
->ops
->recvmsg(conn
->sock
, &recv_hdr
,
1967 sizeof(recv_hdr
), 0);
1969 /* Connection closed. */
1970 cleanup_connection_pollfd(&events
, pollfd
);
1971 /* Put "create" ownership reference. */
1972 connection_put(conn
);
1973 DBG("Viewer control conn closed with %d", pollfd
);
1975 ret
= process_control(&recv_hdr
, conn
);
1977 /* Clear the session on error. */
1978 cleanup_connection_pollfd(&events
, pollfd
);
1979 /* Put "create" ownership reference. */
1980 connection_put(conn
);
1981 DBG("Viewer connection closed with %d", pollfd
);
1984 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1985 cleanup_connection_pollfd(&events
, pollfd
);
1986 /* Put "create" ownership reference. */
1987 connection_put(conn
);
1989 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1990 connection_put(conn
);
1993 /* Put local "get_by_sock" reference. */
1994 connection_put(conn
);
2001 lttng_poll_clean(&events
);
2003 /* Cleanup reamaining connection object. */
2005 cds_lfht_for_each_entry(viewer_connections_ht
->ht
, &iter
.iter
,
2008 health_code_update();
2009 connection_put(destroy_conn
);
2013 lttng_ht_destroy(viewer_connections_ht
);
2014 viewer_connections_ht_error
:
2015 /* Close relay conn pipes */
2016 utils_close_pipe(live_conn_pipe
);
2018 DBG("Viewer worker thread exited with error");
2020 DBG("Viewer worker thread cleanup complete");
2024 ERR("Health error occurred in %s", __func__
);
2026 health_unregister(health_relayd
);
2027 if (lttng_relay_stop_threads()) {
2028 ERR("Error stopping threads");
2030 rcu_unregister_thread();
2035 * Create the relay command pipe to wake thread_manage_apps.
2036 * Closed in cleanup().
2038 static int create_conn_pipe(void)
2040 return utils_create_pipe_cloexec(live_conn_pipe
);
2043 int relayd_live_join(void)
2045 int ret
, retval
= 0;
2048 ret
= pthread_join(live_listener_thread
, &status
);
2051 PERROR("pthread_join live listener");
2055 ret
= pthread_join(live_worker_thread
, &status
);
2058 PERROR("pthread_join live worker");
2062 ret
= pthread_join(live_dispatcher_thread
, &status
);
2065 PERROR("pthread_join live dispatcher");
2069 cleanup_relayd_live();
2077 int relayd_live_create(struct lttng_uri
*uri
)
2079 int ret
= 0, retval
= 0;
2085 goto exit_init_data
;
2089 /* Check if daemon is UID = 0 */
2090 is_root
= !getuid();
2093 if (live_uri
->port
< 1024) {
2094 ERR("Need to be root to use ports < 1024");
2096 goto exit_init_data
;
2100 /* Setup the thread apps communication pipe. */
2101 if (create_conn_pipe()) {
2103 goto exit_init_data
;
2106 /* Init relay command queue. */
2107 cds_wfcq_init(&viewer_conn_queue
.head
, &viewer_conn_queue
.tail
);
2109 /* Set up max poll set size */
2110 if (lttng_poll_set_max_size()) {
2112 goto exit_init_data
;
2115 /* Setup the dispatcher thread */
2116 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
2117 thread_dispatcher
, (void *) NULL
);
2120 PERROR("pthread_create viewer dispatcher");
2122 goto exit_dispatcher_thread
;
2125 /* Setup the worker thread */
2126 ret
= pthread_create(&live_worker_thread
, NULL
,
2127 thread_worker
, NULL
);
2130 PERROR("pthread_create viewer worker");
2132 goto exit_worker_thread
;
2135 /* Setup the listener thread */
2136 ret
= pthread_create(&live_listener_thread
, NULL
,
2137 thread_listener
, (void *) NULL
);
2140 PERROR("pthread_create viewer listener");
2142 goto exit_listener_thread
;
2146 * All OK, started all threads.
2151 * Join on the live_listener_thread should anything be added after
2152 * the live_listener thread's creation.
2155 exit_listener_thread
:
2157 ret
= pthread_join(live_worker_thread
, &status
);
2160 PERROR("pthread_join live worker");
2165 ret
= pthread_join(live_dispatcher_thread
, &status
);
2168 PERROR("pthread_join live dispatcher");
2171 exit_dispatcher_thread
:
2174 cleanup_relayd_live();