2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include "ctf-trace.hpp"
12 #include "lttng-relayd.hpp"
13 #include "session.hpp"
15 #include "viewer-session.hpp"
16 #include "viewer-stream.hpp"
18 #include <common/common.hpp>
20 #include <urcu/rculist.h>
22 struct relay_viewer_session
*viewer_session_create()
24 struct relay_viewer_session
*vsession
;
26 vsession
= zmalloc
<relay_viewer_session
>();
30 CDS_INIT_LIST_HEAD(&vsession
->session_list
);
35 int viewer_session_set_trace_chunk_copy(struct relay_viewer_session
*vsession
,
36 struct lttng_trace_chunk
*relay_session_trace_chunk
)
39 struct lttng_trace_chunk
*viewer_chunk
;
41 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
42 vsession
->current_trace_chunk
= nullptr;
44 DBG("Copying relay session's current trace chunk to the viewer session");
45 if (!relay_session_trace_chunk
) {
49 viewer_chunk
= lttng_trace_chunk_copy(relay_session_trace_chunk
);
51 ERR("Failed to create a viewer trace chunk from the relay session's current chunk");
56 vsession
->current_trace_chunk
= viewer_chunk
;
61 /* The existence of session must be guaranteed by the caller. */
62 enum lttng_viewer_attach_return_code
viewer_session_attach(struct relay_viewer_session
*vsession
,
63 struct relay_session
*session
)
65 enum lttng_viewer_attach_return_code viewer_attach_status
= LTTNG_VIEWER_ATTACH_OK
;
67 ASSERT_LOCKED(session
->lock
);
69 /* Will not fail, as per the ownership guarantee. */
70 if (!session_get(session
)) {
71 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
74 if (session
->viewer_attached
) {
75 viewer_attach_status
= LTTNG_VIEWER_ATTACH_ALREADY
;
79 session
->viewer_attached
= true;
81 ret
= viewer_session_set_trace_chunk_copy(vsession
, session
->current_trace_chunk
);
84 * The live protocol does not define a generic error
85 * value for the "attach" command. The "unknown"
86 * status is used so that the viewer may handle this
87 * failure as if the session didn't exist anymore.
89 DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK",
90 session
->session_name
);
91 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
95 if (viewer_attach_status
== LTTNG_VIEWER_ATTACH_OK
) {
96 pthread_mutex_lock(&vsession
->session_list_lock
);
97 /* Ownership is transfered to the list. */
98 cds_list_add_rcu(&session
->viewer_session_node
, &vsession
->session_list
);
99 pthread_mutex_unlock(&vsession
->session_list_lock
);
101 /* Put our local ref. */
102 session_put(session
);
105 return viewer_attach_status
;
108 /* The existence of session must be guaranteed by the caller. */
109 static int viewer_session_detach(struct relay_viewer_session
*vsession
,
110 struct relay_session
*session
)
114 pthread_mutex_lock(&session
->lock
);
115 if (!session
->viewer_attached
) {
118 session
->viewer_attached
= false;
122 pthread_mutex_lock(&vsession
->session_list_lock
);
123 cds_list_del_rcu(&session
->viewer_session_node
);
124 pthread_mutex_unlock(&vsession
->session_list_lock
);
125 /* Release reference held by the list. */
126 session_put(session
);
128 /* Safe since we know the session exists. */
129 pthread_mutex_unlock(&session
->lock
);
133 void viewer_session_destroy(struct relay_viewer_session
*vsession
)
135 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
140 * Release ownership of all the streams of one session and detach the viewer.
142 void viewer_session_close_one_session(struct relay_viewer_session
*vsession
,
143 struct relay_session
*session
)
145 struct lttng_ht_iter iter
;
146 struct relay_viewer_stream
*vstream
;
149 * TODO: improvement: create more efficient list of
150 * vstream per session.
152 cds_lfht_for_each_entry (viewer_streams_ht
->ht
, &iter
.iter
, vstream
, stream_n
.node
) {
153 if (!viewer_stream_get(vstream
)) {
156 if (vstream
->stream
->trace
->session
!= session
) {
157 viewer_stream_put(vstream
);
160 /* Put local reference. */
161 viewer_stream_put(vstream
);
163 * We have reached one of the viewer stream's lifetime
164 * end condition. This "put" will cause the proper
165 * teardown of the viewer stream.
167 viewer_stream_put(vstream
);
169 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
170 vsession
->current_trace_chunk
= nullptr;
171 viewer_session_detach(vsession
, session
);
174 void viewer_session_close(struct relay_viewer_session
*vsession
)
176 struct relay_session
*session
;
179 cds_list_for_each_entry_rcu(session
, &vsession
->session_list
, viewer_session_node
)
181 viewer_session_close_one_session(vsession
, session
);
187 * Check if a connection is attached to a session.
188 * Return 1 if attached, 0 if not attached, a negative value on error.
190 int viewer_session_is_attached(struct relay_viewer_session
*vsession
, struct relay_session
*session
)
192 struct relay_session
*iter
;
195 pthread_mutex_lock(&session
->lock
);
199 if (!session
->viewer_attached
) {
203 cds_list_for_each_entry_rcu(iter
, &vsession
->session_list
, viewer_session_node
)
205 if (session
== iter
) {
213 pthread_mutex_unlock(&session
->lock
);