Commit | Line | Data |
---|---|---|
2f8f53af DG |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2f8f53af DG |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License, version 2 only, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
2a174661 | 21 | #include <common/common.h> |
1e791a74 | 22 | #include <common/compat/uuid.h> |
7591bab1 | 23 | #include <urcu/rculist.h> |
2a174661 | 24 | |
7591bab1 | 25 | #include "lttng-relayd.h" |
2a174661 | 26 | #include "ctf-trace.h" |
2f8f53af | 27 | #include "session.h" |
2a174661 | 28 | #include "stream.h" |
23c8ff50 | 29 | #include "sessiond-trace-chunks.h" |
2a174661 DG |
30 | |
31 | /* Global session id used in the session creation. */ | |
32 | static uint64_t last_relay_session_id; | |
7591bab1 | 33 | static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
2a174661 | 34 | |
1e791a74 JG |
35 | static int session_set_anonymous_chunk(struct relay_session *session) |
36 | { | |
37 | int ret = 0; | |
38 | struct lttng_trace_chunk *chunk = NULL; | |
39 | enum lttng_trace_chunk_status status; | |
40 | struct lttng_directory_handle output_directory; | |
41 | ||
42 | ret = lttng_directory_handle_init(&output_directory, opt_output_path); | |
43 | if (ret) { | |
44 | goto end; | |
45 | } | |
46 | ||
47 | chunk = lttng_trace_chunk_create_anonymous(); | |
48 | if (!chunk) { | |
49 | goto end; | |
50 | } | |
51 | ||
52 | status = lttng_trace_chunk_set_credentials_current_user(chunk); | |
53 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
54 | ret = -1; | |
55 | goto end; | |
56 | } | |
57 | ||
58 | status = lttng_trace_chunk_set_as_owner(chunk, &output_directory); | |
59 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
60 | ret = -1; | |
61 | goto end; | |
62 | } | |
63 | session->current_trace_chunk = chunk; | |
64 | chunk = NULL; | |
65 | end: | |
66 | lttng_trace_chunk_put(chunk); | |
67 | lttng_directory_handle_fini(&output_directory); | |
68 | return ret; | |
69 | } | |
70 | ||
2a174661 DG |
71 | /* |
72 | * Create a new session by assigning a new session ID. | |
73 | * | |
74 | * Return allocated session or else NULL. | |
75 | */ | |
7591bab1 MD |
76 | struct relay_session *session_create(const char *session_name, |
77 | const char *hostname, uint32_t live_timer, | |
23c8ff50 | 78 | bool snapshot, const lttng_uuid sessiond_uuid, |
1e791a74 | 79 | uint64_t *id_sessiond, uint64_t *current_chunk_id, |
23c8ff50 | 80 | uint32_t major, uint32_t minor) |
2a174661 | 81 | { |
23c8ff50 | 82 | int ret; |
2a174661 DG |
83 | struct relay_session *session; |
84 | ||
85 | session = zmalloc(sizeof(*session)); | |
86 | if (!session) { | |
1e791a74 | 87 | PERROR("Failed to allocate session"); |
2a174661 DG |
88 | goto error; |
89 | } | |
bb5d54e7 MD |
90 | if (lttng_strncpy(session->session_name, session_name, |
91 | sizeof(session->session_name))) { | |
1e791a74 | 92 | WARN("Session name exceeds maximal allowed length"); |
bb5d54e7 MD |
93 | goto error; |
94 | } | |
95 | if (lttng_strncpy(session->hostname, hostname, | |
96 | sizeof(session->hostname))) { | |
1e791a74 | 97 | WARN("Hostname exceeds maximal allowed length"); |
bb5d54e7 MD |
98 | goto error; |
99 | } | |
2a174661 DG |
100 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
101 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
102 | goto error; |
103 | } | |
104 | ||
7591bab1 | 105 | pthread_mutex_lock(&last_relay_session_id_lock); |
2a174661 | 106 | session->id = ++last_relay_session_id; |
7591bab1 MD |
107 | pthread_mutex_unlock(&last_relay_session_id_lock); |
108 | ||
109 | session->major = major; | |
110 | session->minor = minor; | |
2a174661 | 111 | lttng_ht_node_init_u64(&session->session_n, session->id); |
7591bab1 MD |
112 | urcu_ref_init(&session->ref); |
113 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
114 | pthread_mutex_init(&session->lock, NULL); | |
7591bab1 MD |
115 | pthread_mutex_init(&session->recv_list_lock, NULL); |
116 | ||
7591bab1 MD |
117 | session->live_timer = live_timer; |
118 | session->snapshot = snapshot; | |
23c8ff50 JG |
119 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
120 | ||
1e791a74 JG |
121 | if (id_sessiond) { |
122 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); | |
123 | } | |
124 | ||
23c8ff50 JG |
125 | ret = sessiond_trace_chunk_registry_session_created( |
126 | sessiond_trace_chunk_registry, sessiond_uuid); | |
127 | if (ret) { | |
128 | goto error; | |
129 | } | |
7591bab1 | 130 | |
1e791a74 JG |
131 | if (id_sessiond && current_chunk_id) { |
132 | session->current_trace_chunk = | |
133 | sessiond_trace_chunk_registry_get_chunk( | |
134 | sessiond_trace_chunk_registry, | |
135 | session->sessiond_uuid, | |
136 | session->id_sessiond.value, | |
137 | *current_chunk_id); | |
138 | if (!session->current_trace_chunk) { | |
139 | char uuid_str[UUID_STR_LEN]; | |
140 | ||
141 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
142 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64, | |
143 | uuid_str, *id_sessiond, | |
144 | *current_chunk_id); | |
145 | } | |
146 | } else if (!id_sessiond) { | |
147 | /* | |
148 | * Pre-2.11 peers will not announce trace chunks. An | |
149 | * anonymous trace chunk which will remain set for the | |
150 | * duration of the session is created. | |
151 | */ | |
152 | ret = session_set_anonymous_chunk(session); | |
153 | if (ret) { | |
154 | goto error; | |
155 | } | |
156 | } | |
157 | ||
7591bab1 | 158 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
bb5d54e7 | 159 | return session; |
2a174661 DG |
160 | |
161 | error: | |
1e791a74 | 162 | session_put(session); |
bb5d54e7 | 163 | return NULL; |
2a174661 | 164 | } |
2f8f53af | 165 | |
7591bab1 MD |
166 | /* Should be called with RCU read-side lock held. */ |
167 | bool session_get(struct relay_session *session) | |
168 | { | |
ce4d4083 | 169 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
170 | } |
171 | ||
2f8f53af | 172 | /* |
7591bab1 MD |
173 | * Lookup a session within the session hash table using the session id |
174 | * as key. A session reference is taken when a session is returned. | |
175 | * session_put() must be called on that session. | |
2f8f53af DG |
176 | * |
177 | * Return session or NULL if not found. | |
178 | */ | |
7591bab1 | 179 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
180 | { |
181 | struct relay_session *session = NULL; | |
2a174661 | 182 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
183 | struct lttng_ht_iter iter; |
184 | ||
7591bab1 MD |
185 | rcu_read_lock(); |
186 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 187 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 188 | if (!node) { |
2a174661 | 189 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
190 | goto end; |
191 | } | |
192 | session = caa_container_of(node, struct relay_session, session_n); | |
2a174661 | 193 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
194 | if (!session_get(session)) { |
195 | session = NULL; | |
196 | } | |
2f8f53af | 197 | end: |
7591bab1 | 198 | rcu_read_unlock(); |
2f8f53af DG |
199 | return session; |
200 | } | |
2a174661 | 201 | |
7591bab1 MD |
202 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
203 | { | |
204 | struct relay_session *session = | |
205 | caa_container_of(rcu_head, struct relay_session, | |
206 | rcu_node); | |
49e614cb MD |
207 | /* |
208 | * Since each trace has a reference on the session, it means | |
209 | * that if we are at the point where we teardown the session, no | |
210 | * trace belonging to that session exist at this point. | |
211 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
212 | * don't hold the RCU read-side lock while calling it. | |
213 | */ | |
214 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
215 | free(session); |
216 | } | |
217 | ||
2a174661 DG |
218 | /* |
219 | * Delete session from the given hash table. | |
220 | * | |
221 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
222 | */ | |
7591bab1 | 223 | static int session_delete(struct relay_session *session) |
2a174661 DG |
224 | { |
225 | struct lttng_ht_iter iter; | |
226 | ||
2a174661 | 227 | iter.iter.node = &session->session_n.node; |
7591bab1 | 228 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
229 | } |
230 | ||
7591bab1 MD |
231 | |
232 | static void destroy_session(struct relay_session *session) | |
233 | { | |
234 | int ret; | |
235 | ||
236 | ret = session_delete(session); | |
237 | assert(!ret); | |
639ddf68 | 238 | lttng_trace_chunk_put(session->current_trace_chunk); |
23c8ff50 JG |
239 | ret = sessiond_trace_chunk_registry_session_destroyed( |
240 | sessiond_trace_chunk_registry, session->sessiond_uuid); | |
241 | assert(!ret); | |
639ddf68 JG |
242 | lttng_trace_chunk_put(session->current_trace_chunk); |
243 | session->current_trace_chunk = NULL; | |
7591bab1 MD |
244 | call_rcu(&session->rcu_node, rcu_destroy_session); |
245 | } | |
246 | ||
247 | void session_release(struct urcu_ref *ref) | |
2a174661 | 248 | { |
7591bab1 MD |
249 | struct relay_session *session = |
250 | caa_container_of(ref, struct relay_session, ref); | |
2a174661 | 251 | |
7591bab1 MD |
252 | destroy_session(session); |
253 | } | |
2a174661 | 254 | |
7591bab1 MD |
255 | void session_put(struct relay_session *session) |
256 | { | |
257 | rcu_read_lock(); | |
7591bab1 | 258 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 259 | rcu_read_unlock(); |
2a174661 DG |
260 | } |
261 | ||
7591bab1 | 262 | int session_close(struct relay_session *session) |
2a174661 DG |
263 | { |
264 | int ret = 0; | |
7591bab1 MD |
265 | struct ctf_trace *trace; |
266 | struct lttng_ht_iter iter; | |
267 | struct relay_stream *stream; | |
268 | ||
269 | pthread_mutex_lock(&session->lock); | |
270 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
271 | session->id, session->connection_closed); | |
7591bab1 | 272 | session->connection_closed = true; |
7591bab1 | 273 | pthread_mutex_unlock(&session->lock); |
2a174661 | 274 | |
7591bab1 MD |
275 | rcu_read_lock(); |
276 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, | |
277 | &iter.iter, trace, node.node) { | |
278 | ret = ctf_trace_close(trace); | |
279 | if (ret) { | |
280 | goto rcu_unlock; | |
2a174661 DG |
281 | } |
282 | } | |
7591bab1 MD |
283 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
284 | recv_node) { | |
bda7c7b9 JG |
285 | /* Close streams which have not been published yet. */ |
286 | try_stream_close(stream); | |
7591bab1 MD |
287 | } |
288 | rcu_unlock: | |
289 | rcu_read_unlock(); | |
290 | if (ret) { | |
291 | return ret; | |
292 | } | |
293 | /* Put self-reference from create. */ | |
294 | session_put(session); | |
295 | return ret; | |
2a174661 DG |
296 | } |
297 | ||
98ba050e JR |
298 | int session_abort(struct relay_session *session) |
299 | { | |
300 | int ret = 0; | |
301 | ||
302 | if (!session) { | |
303 | return 0; | |
304 | } | |
305 | ||
306 | pthread_mutex_lock(&session->lock); | |
307 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 308 | session->aborted = true; |
98ba050e JR |
309 | pthread_mutex_unlock(&session->lock); |
310 | return ret; | |
311 | } | |
312 | ||
7591bab1 | 313 | void print_sessions(void) |
2a174661 | 314 | { |
2a174661 | 315 | struct lttng_ht_iter iter; |
7591bab1 | 316 | struct relay_session *session; |
2a174661 | 317 | |
ce3f3ba3 JG |
318 | if (!sessions_ht) { |
319 | return; | |
320 | } | |
321 | ||
2a174661 | 322 | rcu_read_lock(); |
7591bab1 MD |
323 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
324 | session_n.node) { | |
325 | if (!session_get(session)) { | |
326 | continue; | |
327 | } | |
328 | DBG("session %p refcount %ld session %" PRIu64, | |
329 | session, | |
330 | session->ref.refcount, | |
331 | session->id); | |
332 | session_put(session); | |
2a174661 | 333 | } |
2a174661 | 334 | rcu_read_unlock(); |
2a174661 | 335 | } |