Commit | Line | Data |
---|---|---|
7591bab1 | 1 | /* |
ab5be9fa MJ |
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> | |
7591bab1 | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
7591bab1 | 7 | * |
7591bab1 MD |
8 | */ |
9 | ||
7591bab1 | 10 | #define _LGPL_SOURCE |
c9e313bc | 11 | #include "ctf-trace.hpp" |
28ab034a | 12 | #include "lttng-relayd.hpp" |
c9e313bc | 13 | #include "session.hpp" |
28ab034a | 14 | #include "stream.hpp" |
c9e313bc SM |
15 | #include "viewer-session.hpp" |
16 | #include "viewer-stream.hpp" | |
28ab034a JG |
17 | |
18 | #include <common/common.hpp> | |
56047f5a | 19 | #include <common/urcu.hpp> |
28ab034a JG |
20 | |
21 | #include <urcu/rculist.h> | |
7591bab1 | 22 | |
cd9adb8b | 23 | struct relay_viewer_session *viewer_session_create() |
7591bab1 MD |
24 | { |
25 | struct relay_viewer_session *vsession; | |
26 | ||
64803277 | 27 | vsession = zmalloc<relay_viewer_session>(); |
7591bab1 MD |
28 | if (!vsession) { |
29 | goto end; | |
30 | } | |
31 | CDS_INIT_LIST_HEAD(&vsession->session_list); | |
32 | end: | |
33 | return vsession; | |
34 | } | |
35 | ||
b3ab5004 | 36 | int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession, |
28ab034a | 37 | struct lttng_trace_chunk *relay_session_trace_chunk) |
664eef54 JG |
38 | { |
39 | int ret = 0; | |
40 | struct lttng_trace_chunk *viewer_chunk; | |
41 | ||
ad8bec24 | 42 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
cd9adb8b | 43 | vsession->current_trace_chunk = nullptr; |
664eef54 JG |
44 | |
45 | DBG("Copying relay session's current trace chunk to the viewer session"); | |
80516611 JG |
46 | if (!relay_session_trace_chunk) { |
47 | goto end; | |
48 | } | |
49 | ||
664eef54 JG |
50 | viewer_chunk = lttng_trace_chunk_copy(relay_session_trace_chunk); |
51 | if (!viewer_chunk) { | |
52 | ERR("Failed to create a viewer trace chunk from the relay session's current chunk"); | |
53 | ret = -1; | |
54 | goto end; | |
55 | } | |
56 | ||
57 | vsession->current_trace_chunk = viewer_chunk; | |
58 | end: | |
59 | return ret; | |
60 | } | |
61 | ||
7591bab1 | 62 | /* The existence of session must be guaranteed by the caller. */ |
28ab034a JG |
63 | enum lttng_viewer_attach_return_code viewer_session_attach(struct relay_viewer_session *vsession, |
64 | struct relay_session *session) | |
7591bab1 | 65 | { |
28ab034a | 66 | enum lttng_viewer_attach_return_code viewer_attach_status = LTTNG_VIEWER_ATTACH_OK; |
7591bab1 | 67 | |
c06fdd95 JG |
68 | ASSERT_LOCKED(session->lock); |
69 | ||
7591bab1 MD |
70 | /* Will not fail, as per the ownership guarantee. */ |
71 | if (!session_get(session)) { | |
dbd6665b | 72 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
7591bab1 MD |
73 | goto end; |
74 | } | |
7591bab1 | 75 | if (session->viewer_attached) { |
dbd6665b | 76 | viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY; |
7591bab1 | 77 | } else { |
dbd6665b JG |
78 | int ret; |
79 | ||
7591bab1 | 80 | session->viewer_attached = true; |
dbd6665b | 81 | |
28ab034a | 82 | ret = viewer_session_set_trace_chunk_copy(vsession, session->current_trace_chunk); |
dbd6665b JG |
83 | if (ret) { |
84 | /* | |
85 | * The live protocol does not define a generic error | |
86 | * value for the "attach" command. The "unknown" | |
87 | * status is used so that the viewer may handle this | |
88 | * failure as if the session didn't exist anymore. | |
89 | */ | |
90 | DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK", | |
28ab034a | 91 | session->session_name); |
dbd6665b JG |
92 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
93 | } | |
7591bab1 MD |
94 | } |
95 | ||
dbd6665b | 96 | if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) { |
7591bab1 MD |
97 | pthread_mutex_lock(&vsession->session_list_lock); |
98 | /* Ownership is transfered to the list. */ | |
28ab034a | 99 | cds_list_add_rcu(&session->viewer_session_node, &vsession->session_list); |
7591bab1 MD |
100 | pthread_mutex_unlock(&vsession->session_list_lock); |
101 | } else { | |
102 | /* Put our local ref. */ | |
103 | session_put(session); | |
104 | } | |
7591bab1 | 105 | end: |
dbd6665b | 106 | return viewer_attach_status; |
7591bab1 MD |
107 | } |
108 | ||
109 | /* The existence of session must be guaranteed by the caller. */ | |
110 | static int viewer_session_detach(struct relay_viewer_session *vsession, | |
28ab034a | 111 | struct relay_session *session) |
7591bab1 MD |
112 | { |
113 | int ret = 0; | |
114 | ||
115 | pthread_mutex_lock(&session->lock); | |
116 | if (!session->viewer_attached) { | |
117 | ret = -1; | |
118 | } else { | |
119 | session->viewer_attached = false; | |
120 | } | |
121 | ||
122 | if (!ret) { | |
123 | pthread_mutex_lock(&vsession->session_list_lock); | |
124 | cds_list_del_rcu(&session->viewer_session_node); | |
125 | pthread_mutex_unlock(&vsession->session_list_lock); | |
126 | /* Release reference held by the list. */ | |
127 | session_put(session); | |
128 | } | |
129 | /* Safe since we know the session exists. */ | |
130 | pthread_mutex_unlock(&session->lock); | |
131 | return ret; | |
132 | } | |
133 | ||
134 | void viewer_session_destroy(struct relay_viewer_session *vsession) | |
135 | { | |
b66a15d1 | 136 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
7591bab1 MD |
137 | free(vsession); |
138 | } | |
139 | ||
d62023be JD |
140 | /* |
141 | * Release ownership of all the streams of one session and detach the viewer. | |
142 | */ | |
143 | void viewer_session_close_one_session(struct relay_viewer_session *vsession, | |
28ab034a | 144 | struct relay_session *session) |
d62023be JD |
145 | { |
146 | struct lttng_ht_iter iter; | |
147 | struct relay_viewer_stream *vstream; | |
148 | ||
149 | /* | |
150 | * TODO: improvement: create more efficient list of | |
151 | * vstream per session. | |
152 | */ | |
56047f5a JG |
153 | { |
154 | lttng::urcu::read_lock_guard read_guard; | |
155 | ||
156 | cds_lfht_for_each_entry ( | |
157 | viewer_streams_ht->ht, &iter.iter, vstream, stream_n.node) { | |
158 | if (!viewer_stream_get(vstream)) { | |
159 | continue; | |
160 | } | |
161 | if (vstream->stream->trace->session != session) { | |
162 | viewer_stream_put(vstream); | |
163 | continue; | |
164 | } | |
165 | /* Put local reference. */ | |
166 | viewer_stream_put(vstream); | |
167 | /* | |
168 | * We have reached one of the viewer stream's lifetime | |
169 | * end condition. This "put" will cause the proper | |
170 | * teardown of the viewer stream. | |
171 | */ | |
d62023be | 172 | viewer_stream_put(vstream); |
d62023be | 173 | } |
d62023be | 174 | } |
56047f5a | 175 | |
b3feb91b | 176 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
cd9adb8b | 177 | vsession->current_trace_chunk = nullptr; |
d62023be JD |
178 | viewer_session_detach(vsession, session); |
179 | } | |
180 | ||
7591bab1 MD |
181 | void viewer_session_close(struct relay_viewer_session *vsession) |
182 | { | |
183 | struct relay_session *session; | |
184 | ||
28ab034a | 185 | { |
56047f5a JG |
186 | lttng::urcu::read_lock_guard read_lock; |
187 | ||
188 | cds_list_for_each_entry_rcu(session, &vsession->session_list, viewer_session_node) | |
189 | { | |
190 | viewer_session_close_one_session(vsession, session); | |
191 | } | |
7591bab1 | 192 | } |
7591bab1 MD |
193 | } |
194 | ||
195 | /* | |
196 | * Check if a connection is attached to a session. | |
197 | * Return 1 if attached, 0 if not attached, a negative value on error. | |
198 | */ | |
28ab034a | 199 | int viewer_session_is_attached(struct relay_viewer_session *vsession, struct relay_session *session) |
7591bab1 MD |
200 | { |
201 | struct relay_session *iter; | |
202 | int found = 0; | |
203 | ||
204 | pthread_mutex_lock(&session->lock); | |
205 | if (!vsession) { | |
206 | goto end; | |
207 | } | |
208 | if (!session->viewer_attached) { | |
209 | goto end; | |
210 | } | |
56047f5a | 211 | |
28ab034a | 212 | { |
56047f5a JG |
213 | lttng::urcu::read_lock_guard read_lock; |
214 | cds_list_for_each_entry_rcu(iter, &vsession->session_list, viewer_session_node) | |
215 | { | |
216 | if (session == iter) { | |
217 | found = 1; | |
218 | break; | |
219 | } | |
7591bab1 MD |
220 | } |
221 | } | |
56047f5a | 222 | |
7591bab1 MD |
223 | end: |
224 | pthread_mutex_unlock(&session->lock); | |
225 | return found; | |
226 | } |