Commit | Line | Data |
---|---|---|
2f8f53af | 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> | |
2f8f53af | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
2f8f53af | 7 | * |
2f8f53af DG |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
28ab034a JG |
11 | #include "ctf-trace.hpp" |
12 | #include "lttng-relayd.hpp" | |
13 | #include "session.hpp" | |
14 | #include "sessiond-trace-chunks.hpp" | |
15 | #include "stream.hpp" | |
16 | #include "utils.hpp" | |
17 | ||
c9e313bc SM |
18 | #include <common/common.hpp> |
19 | #include <common/compat/path.hpp> | |
28ab034a | 20 | #include <common/defaults.hpp> |
c9e313bc SM |
21 | #include <common/fd-tracker/utils.hpp> |
22 | #include <common/time.hpp> | |
23 | #include <common/utils.hpp> | |
24 | #include <common/uuid.hpp> | |
2a174661 | 25 | |
d37856b8 | 26 | #include <sys/stat.h> |
28ab034a | 27 | #include <urcu/rculist.h> |
2a174661 DG |
28 | |
29 | /* Global session id used in the session creation. */ | |
30 | static uint64_t last_relay_session_id; | |
7591bab1 | 31 | static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
2a174661 | 32 | |
a8b66566 | 33 | static int init_session_output_path_group_by_host(struct relay_session *session) |
ecd1a12f MD |
34 | { |
35 | /* | |
36 | * session_directory: | |
37 | * | |
38 | * if base_path is \0' | |
39 | * hostname/session_name | |
40 | * else | |
41 | * hostname/base_path | |
42 | */ | |
43 | char *session_directory = NULL; | |
44 | int ret = 0; | |
45 | ||
46 | if (session->output_path[0] != '\0') { | |
47 | goto end; | |
48 | } | |
49 | /* | |
50 | * If base path is set, it overrides the session name for the | |
51 | * session relative base path. No timestamp is appended if the | |
52 | * base path is overridden. | |
53 | * | |
54 | * If the session name already contains the creation time (e.g. | |
55 | * auto-<timestamp>, don't append yet another timestamp after | |
56 | * the session name in the generated path. | |
57 | * | |
58 | * Otherwise, generate the path with session_name-<timestamp>. | |
59 | */ | |
60 | if (session->base_path[0] != '\0') { | |
28ab034a | 61 | ret = asprintf(&session_directory, "%s/%s", session->hostname, session->base_path); |
ecd1a12f | 62 | } else if (session->session_name_contains_creation_time) { |
28ab034a JG |
63 | ret = asprintf( |
64 | &session_directory, "%s/%s", session->hostname, session->session_name); | |
ecd1a12f | 65 | } else { |
a8b66566 | 66 | char session_creation_datetime[DATETIME_STR_LEN]; |
ecd1a12f | 67 | |
28ab034a JG |
68 | ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time), |
69 | session_creation_datetime, | |
70 | sizeof(session_creation_datetime)); | |
a8b66566 | 71 | if (ret) { |
ecd1a12f MD |
72 | ERR("Failed to format session creation timestamp while initializing session output directory handle"); |
73 | ret = -1; | |
74 | goto end; | |
75 | } | |
a8b66566 | 76 | |
28ab034a JG |
77 | ret = asprintf(&session_directory, |
78 | "%s/%s-%s", | |
79 | session->hostname, | |
80 | session->session_name, | |
81 | session_creation_datetime); | |
ecd1a12f MD |
82 | } |
83 | if (ret < 0) { | |
84 | PERROR("Failed to format session directory name"); | |
85 | goto end; | |
86 | } | |
87 | ||
88 | if (strlen(session_directory) >= LTTNG_PATH_MAX) { | |
89 | ERR("Session output directory exceeds maximal length"); | |
90 | ret = -1; | |
91 | goto end; | |
92 | } | |
93 | strcpy(session->output_path, session_directory); | |
94 | ret = 0; | |
95 | ||
96 | end: | |
97 | free(session_directory); | |
98 | return ret; | |
99 | } | |
100 | ||
28ab034a | 101 | static int init_session_output_path_group_by_session(struct relay_session *session) |
a8b66566 JR |
102 | { |
103 | /* | |
104 | * session_directory: | |
105 | * | |
106 | * session_name/hostname-creation_time/base_path | |
107 | * | |
108 | * For session name including the datetime, use it as the complete name | |
109 | * since. Do not perform modification on it since the datetime is an | |
110 | * integral part of the name and how a user identify a session. | |
111 | */ | |
112 | int ret = 0; | |
113 | char *session_directory = NULL; | |
114 | char creation_datetime[DATETIME_STR_LEN]; | |
115 | ||
116 | if (session->output_path[0] != '\0') { | |
117 | /* output_path as been generated already */ | |
118 | goto end; | |
119 | } | |
120 | ||
121 | ret = time_to_datetime_str(LTTNG_OPTIONAL_GET(session->creation_time), | |
28ab034a JG |
122 | creation_datetime, |
123 | sizeof(creation_datetime)); | |
a8b66566 JR |
124 | if (ret) { |
125 | ERR("Failed to format session creation timestamp while initializing session output directory handle"); | |
126 | ret = -1; | |
127 | goto end; | |
128 | } | |
129 | ||
28ab034a JG |
130 | ret = asprintf(&session_directory, |
131 | "%s/%s-%s%s%s", | |
132 | session->session_name, | |
133 | session->hostname, | |
134 | creation_datetime, | |
135 | session->base_path[0] != '\0' ? "/" : "", | |
136 | session->base_path); | |
a8b66566 JR |
137 | if (ret < 0) { |
138 | PERROR("Failed to format session directory name"); | |
139 | goto end; | |
140 | } | |
141 | ||
142 | if (strlen(session_directory) >= LTTNG_PATH_MAX) { | |
143 | ERR("Session output directory exceeds maximal length"); | |
144 | ret = -1; | |
145 | goto end; | |
146 | } | |
147 | ||
148 | strcpy(session->output_path, session_directory); | |
149 | ret = 0; | |
150 | ||
151 | end: | |
152 | free(session_directory); | |
153 | return ret; | |
154 | } | |
155 | ||
156 | static int init_session_output_path(struct relay_session *session) | |
157 | { | |
158 | int ret; | |
159 | ||
160 | switch (opt_group_output_by) { | |
161 | case RELAYD_GROUP_OUTPUT_BY_HOST: | |
162 | ret = init_session_output_path_group_by_host(session); | |
163 | break; | |
164 | case RELAYD_GROUP_OUTPUT_BY_SESSION: | |
165 | ret = init_session_output_path_group_by_session(session); | |
166 | break; | |
167 | case RELAYD_GROUP_OUTPUT_BY_UNKNOWN: | |
168 | default: | |
169 | abort(); | |
170 | break; | |
171 | } | |
172 | ||
173 | return ret; | |
174 | } | |
175 | ||
28ab034a JG |
176 | static struct lttng_directory_handle * |
177 | session_create_output_directory_handle(struct relay_session *session) | |
7ceefac4 JG |
178 | { |
179 | int ret; | |
180 | /* | |
181 | * relayd_output_path/session_directory | |
182 | * e.g. /home/user/lttng-traces/hostname/session_name | |
183 | */ | |
184 | char *full_session_path = NULL; | |
185 | struct lttng_directory_handle *handle = NULL; | |
186 | ||
187 | pthread_mutex_lock(&session->lock); | |
188 | full_session_path = create_output_path(session->output_path); | |
189 | if (!full_session_path) { | |
190 | goto end; | |
191 | } | |
192 | ||
28ab034a | 193 | ret = utils_mkdir_recursive(full_session_path, S_IRWXU | S_IRWXG, -1, -1); |
7ceefac4 | 194 | if (ret) { |
28ab034a | 195 | ERR("Failed to create session output path \"%s\"", full_session_path); |
7ceefac4 JG |
196 | goto end; |
197 | } | |
198 | ||
dd95933f | 199 | handle = fd_tracker_create_directory_handle(the_fd_tracker, full_session_path); |
7ceefac4 JG |
200 | end: |
201 | pthread_mutex_unlock(&session->lock); | |
202 | free(full_session_path); | |
203 | return handle; | |
204 | } | |
205 | ||
1e791a74 JG |
206 | static int session_set_anonymous_chunk(struct relay_session *session) |
207 | { | |
208 | int ret = 0; | |
209 | struct lttng_trace_chunk *chunk = NULL; | |
210 | enum lttng_trace_chunk_status status; | |
cbf53d23 | 211 | struct lttng_directory_handle *output_directory; |
1e791a74 | 212 | |
cbf53d23 JG |
213 | output_directory = session_create_output_directory_handle(session); |
214 | if (!output_directory) { | |
1e791a74 JG |
215 | goto end; |
216 | } | |
217 | ||
218 | chunk = lttng_trace_chunk_create_anonymous(); | |
219 | if (!chunk) { | |
220 | goto end; | |
221 | } | |
222 | ||
9642d9bf | 223 | lttng_trace_chunk_set_fd_tracker(chunk, the_fd_tracker); |
1e791a74 JG |
224 | status = lttng_trace_chunk_set_credentials_current_user(chunk); |
225 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
226 | ret = -1; | |
227 | goto end; | |
228 | } | |
229 | ||
cbf53d23 | 230 | status = lttng_trace_chunk_set_as_owner(chunk, output_directory); |
1e791a74 JG |
231 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
232 | ret = -1; | |
233 | goto end; | |
234 | } | |
7145f5e9 | 235 | |
1e791a74 JG |
236 | session->current_trace_chunk = chunk; |
237 | chunk = NULL; | |
238 | end: | |
239 | lttng_trace_chunk_put(chunk); | |
cbf53d23 | 240 | lttng_directory_handle_put(output_directory); |
1e791a74 JG |
241 | return ret; |
242 | } | |
243 | ||
6ec9dc48 JG |
244 | /* |
245 | * Check if a name is safe to use in a path. | |
246 | * | |
247 | * A name that is deemed "path-safe": | |
248 | * - Does not contains a path separator (/ or \, platform dependant), | |
249 | * - Does not start with a '.' (hidden file/folder), | |
250 | * - Is not empty. | |
251 | */ | |
252 | static bool is_name_path_safe(const char *name) | |
253 | { | |
254 | const size_t name_len = strlen(name); | |
255 | ||
256 | /* Not empty. */ | |
257 | if (name_len == 0) { | |
258 | WARN("An empty name is not allowed to be used in a path"); | |
259 | return false; | |
260 | } | |
261 | /* Does not start with '.'. */ | |
262 | if (name[0] == '.') { | |
28ab034a JG |
263 | WARN("Name \"%s\" is not allowed to be used in a path since it starts with '.'", |
264 | name); | |
6ec9dc48 JG |
265 | return false; |
266 | } | |
267 | /* Does not contain a path-separator. */ | |
268 | if (strchr(name, LTTNG_PATH_SEPARATOR)) { | |
28ab034a JG |
269 | WARN("Name \"%s\" is not allowed to be used in a path since it contains a path separator", |
270 | name); | |
6ec9dc48 JG |
271 | return false; |
272 | } | |
273 | ||
274 | return true; | |
275 | } | |
276 | ||
2a174661 DG |
277 | /* |
278 | * Create a new session by assigning a new session ID. | |
279 | * | |
280 | * Return allocated session or else NULL. | |
281 | */ | |
7591bab1 | 282 | struct relay_session *session_create(const char *session_name, |
28ab034a JG |
283 | const char *hostname, |
284 | const char *base_path, | |
285 | uint32_t live_timer, | |
286 | bool snapshot, | |
287 | const lttng_uuid& sessiond_uuid, | |
288 | const uint64_t *id_sessiond, | |
289 | const uint64_t *current_chunk_id, | |
290 | const time_t *creation_time, | |
291 | uint32_t major, | |
292 | uint32_t minor, | |
293 | bool session_name_contains_creation_time) | |
2a174661 | 294 | { |
23c8ff50 | 295 | int ret; |
590f0324 JG |
296 | struct relay_session *session = NULL; |
297 | ||
a0377dfe FD |
298 | LTTNG_ASSERT(session_name); |
299 | LTTNG_ASSERT(hostname); | |
300 | LTTNG_ASSERT(base_path); | |
5c956ba3 | 301 | |
6ec9dc48 JG |
302 | if (!is_name_path_safe(session_name)) { |
303 | ERR("Refusing to create session as the provided session name is not path-safe"); | |
304 | goto error; | |
305 | } | |
306 | if (!is_name_path_safe(hostname)) { | |
307 | ERR("Refusing to create session as the provided hostname is not path-safe"); | |
590f0324 JG |
308 | goto error; |
309 | } | |
5c956ba3 | 310 | if (strstr(base_path, "../")) { |
28ab034a | 311 | ERR("Invalid session base path walks up the path hierarchy: \"%s\"", base_path); |
590f0324 JG |
312 | goto error; |
313 | } | |
2a174661 | 314 | |
64803277 | 315 | session = zmalloc<relay_session>(); |
2a174661 | 316 | if (!session) { |
1e791a74 | 317 | PERROR("Failed to allocate session"); |
2a174661 DG |
318 | goto error; |
319 | } | |
19efdf65 JG |
320 | |
321 | pthread_mutex_lock(&last_relay_session_id_lock); | |
322 | session->id = ++last_relay_session_id; | |
323 | pthread_mutex_unlock(&last_relay_session_id_lock); | |
324 | ||
325 | lttng_ht_node_init_u64(&session->session_n, session->id); | |
326 | urcu_ref_init(&session->ref); | |
327 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
328 | pthread_mutex_init(&session->lock, NULL); | |
329 | pthread_mutex_init(&session->recv_list_lock, NULL); | |
330 | ||
28ab034a | 331 | if (lttng_strncpy(session->session_name, session_name, sizeof(session->session_name))) { |
eec856bf | 332 | WARN("Session name exceeds maximal allowed length"); |
bb5d54e7 MD |
333 | goto error; |
334 | } | |
28ab034a | 335 | if (lttng_strncpy(session->hostname, hostname, sizeof(session->hostname))) { |
1e791a74 | 336 | WARN("Hostname exceeds maximal allowed length"); |
bb5d54e7 MD |
337 | goto error; |
338 | } | |
28ab034a | 339 | if (lttng_strncpy(session->base_path, base_path, sizeof(session->base_path))) { |
6fa5fe7c MD |
340 | WARN("Base path exceeds maximal allowed length"); |
341 | goto error; | |
342 | } | |
46ef2188 MD |
343 | if (creation_time) { |
344 | LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); | |
d2cb4a90 JG |
345 | } else { |
346 | LTTNG_OPTIONAL_SET(&session->creation_time, time(NULL)); | |
347 | if (session->creation_time.value == (time_t) -1) { | |
348 | PERROR("Failed to sample session creation time"); | |
349 | goto error; | |
350 | } | |
46ef2188 | 351 | } |
28ab034a | 352 | session->session_name_contains_creation_time = session_name_contains_creation_time; |
6fa5fe7c | 353 | |
2a174661 DG |
354 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
355 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
356 | goto error; |
357 | } | |
358 | ||
7591bab1 MD |
359 | session->major = major; |
360 | session->minor = minor; | |
7591bab1 | 361 | |
7591bab1 MD |
362 | session->live_timer = live_timer; |
363 | session->snapshot = snapshot; | |
328c2fe7 | 364 | session->sessiond_uuid = sessiond_uuid; |
23c8ff50 | 365 | |
1e791a74 JG |
366 | if (id_sessiond) { |
367 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); | |
368 | } | |
369 | ||
d519f442 JR |
370 | if (major == 2 && minor >= 11) { |
371 | /* Only applies for 2.11+ peers using trace chunks. */ | |
372 | ret = init_session_output_path(session); | |
373 | if (ret) { | |
374 | goto error; | |
375 | } | |
ecd1a12f | 376 | } |
d519f442 | 377 | |
28ab034a JG |
378 | ret = sessiond_trace_chunk_registry_session_created(sessiond_trace_chunk_registry, |
379 | sessiond_uuid); | |
23c8ff50 JG |
380 | if (ret) { |
381 | goto error; | |
382 | } | |
7591bab1 | 383 | |
1e791a74 | 384 | if (id_sessiond && current_chunk_id) { |
7ceefac4 JG |
385 | enum lttng_trace_chunk_status chunk_status; |
386 | struct lttng_directory_handle *session_output_directory; | |
387 | ||
1e791a74 | 388 | session->current_trace_chunk = |
28ab034a JG |
389 | sessiond_trace_chunk_registry_get_chunk(sessiond_trace_chunk_registry, |
390 | session->sessiond_uuid, | |
391 | session->id_sessiond.value, | |
392 | *current_chunk_id); | |
1e791a74 | 393 | if (!session->current_trace_chunk) { |
eec856bf | 394 | char uuid_str[LTTNG_UUID_STR_LEN]; |
1e791a74 JG |
395 | |
396 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
28ab034a JG |
397 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 |
398 | ", trace chunk id = %" PRIu64, | |
399 | uuid_str, | |
400 | *id_sessiond, | |
401 | *current_chunk_id); | |
53eb691c | 402 | goto error; |
eec856bf | 403 | } |
7ceefac4 JG |
404 | |
405 | chunk_status = lttng_trace_chunk_get_session_output_directory_handle( | |
28ab034a | 406 | session->current_trace_chunk, &session_output_directory); |
7ceefac4 JG |
407 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
408 | goto error; | |
409 | } | |
410 | ||
a0377dfe | 411 | LTTNG_ASSERT(session_output_directory); |
7ceefac4 | 412 | session->output_directory = session_output_directory; |
1e791a74 JG |
413 | } else if (!id_sessiond) { |
414 | /* | |
415 | * Pre-2.11 peers will not announce trace chunks. An | |
416 | * anonymous trace chunk which will remain set for the | |
417 | * duration of the session is created. | |
418 | */ | |
419 | ret = session_set_anonymous_chunk(session); | |
420 | if (ret) { | |
421 | goto error; | |
422 | } | |
7ceefac4 | 423 | } else { |
28ab034a | 424 | session->output_directory = session_create_output_directory_handle(session); |
7ceefac4 JG |
425 | if (!session->output_directory) { |
426 | goto error; | |
427 | } | |
1e791a74 JG |
428 | } |
429 | ||
7591bab1 | 430 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
bb5d54e7 | 431 | return session; |
2a174661 DG |
432 | |
433 | error: | |
1e791a74 | 434 | session_put(session); |
bb5d54e7 | 435 | return NULL; |
2a174661 | 436 | } |
2f8f53af | 437 | |
7591bab1 MD |
438 | /* Should be called with RCU read-side lock held. */ |
439 | bool session_get(struct relay_session *session) | |
440 | { | |
ce4d4083 | 441 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
442 | } |
443 | ||
2f8f53af | 444 | /* |
7591bab1 MD |
445 | * Lookup a session within the session hash table using the session id |
446 | * as key. A session reference is taken when a session is returned. | |
447 | * session_put() must be called on that session. | |
2f8f53af DG |
448 | * |
449 | * Return session or NULL if not found. | |
450 | */ | |
7591bab1 | 451 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
452 | { |
453 | struct relay_session *session = NULL; | |
2a174661 | 454 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
455 | struct lttng_ht_iter iter; |
456 | ||
7591bab1 MD |
457 | rcu_read_lock(); |
458 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 459 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 460 | if (!node) { |
2a174661 | 461 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
462 | goto end; |
463 | } | |
0114db0e | 464 | session = lttng::utils::container_of(node, &relay_session::session_n); |
2a174661 | 465 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
466 | if (!session_get(session)) { |
467 | session = NULL; | |
468 | } | |
2f8f53af | 469 | end: |
7591bab1 | 470 | rcu_read_unlock(); |
2f8f53af DG |
471 | return session; |
472 | } | |
2a174661 | 473 | |
fe88e517 JG |
474 | /* |
475 | * Check if any of the relay sessions originating from the same | |
476 | * session daemon session have the 'ongoing_rotation' state set. | |
477 | * | |
478 | * The caller must hold the lock of session. | |
479 | */ | |
480 | bool session_has_ongoing_rotation(const struct relay_session *session) | |
481 | { | |
482 | bool ongoing_rotation = false; | |
483 | struct lttng_ht_iter iter; | |
484 | struct relay_session *iterated_session; | |
485 | ||
486 | ASSERT_LOCKED(session->lock); | |
487 | ||
488 | if (!session->id_sessiond.is_set) { | |
489 | /* | |
490 | * The peer that created this session is too old to | |
491 | * support rotations; we can assume no rotations are ongoing. | |
492 | */ | |
493 | goto end; | |
494 | } | |
495 | ||
496 | if (session->ongoing_rotation) { | |
497 | ongoing_rotation = true; | |
498 | goto end; | |
499 | } | |
500 | ||
501 | rcu_read_lock(); | |
502 | /* | |
503 | * Sample the 'ongoing_rotation' status of all relay sessions that | |
504 | * originate from the same session daemon session. | |
505 | */ | |
28ab034a | 506 | cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, iterated_session, session_n.node) { |
fe88e517 JG |
507 | if (!session_get(iterated_session)) { |
508 | continue; | |
509 | } | |
510 | ||
511 | if (session == iterated_session) { | |
512 | /* Skip this session. */ | |
513 | goto next_session_no_unlock; | |
514 | } | |
515 | ||
516 | pthread_mutex_lock(&iterated_session->lock); | |
517 | ||
518 | if (!iterated_session->id_sessiond.is_set) { | |
519 | /* | |
520 | * Session belongs to a peer that doesn't support | |
521 | * rotations. | |
522 | */ | |
523 | goto next_session; | |
524 | } | |
525 | ||
328c2fe7 | 526 | if (session->sessiond_uuid != iterated_session->sessiond_uuid) { |
fe88e517 JG |
527 | /* Sessions do not originate from the same sessiond. */ |
528 | goto next_session; | |
529 | } | |
530 | ||
531 | if (LTTNG_OPTIONAL_GET(session->id_sessiond) != | |
28ab034a | 532 | LTTNG_OPTIONAL_GET(iterated_session->id_sessiond)) { |
fe88e517 JG |
533 | /* |
534 | * Sessions do not originate from the same sessiond | |
535 | * session. | |
536 | */ | |
537 | goto next_session; | |
538 | } | |
539 | ||
540 | ongoing_rotation = iterated_session->ongoing_rotation; | |
541 | ||
28ab034a | 542 | next_session: |
fe88e517 | 543 | pthread_mutex_unlock(&iterated_session->lock); |
28ab034a | 544 | next_session_no_unlock: |
fe88e517 JG |
545 | session_put(iterated_session); |
546 | ||
547 | if (ongoing_rotation) { | |
548 | break; | |
549 | } | |
550 | } | |
551 | rcu_read_unlock(); | |
552 | ||
553 | end: | |
554 | return ongoing_rotation; | |
555 | } | |
556 | ||
7591bab1 MD |
557 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
558 | { | |
28ab034a | 559 | struct relay_session *session = caa_container_of(rcu_head, struct relay_session, rcu_node); |
49e614cb MD |
560 | /* |
561 | * Since each trace has a reference on the session, it means | |
562 | * that if we are at the point where we teardown the session, no | |
563 | * trace belonging to that session exist at this point. | |
564 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
565 | * don't hold the RCU read-side lock while calling it. | |
566 | */ | |
567 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
568 | free(session); |
569 | } | |
570 | ||
2a174661 DG |
571 | /* |
572 | * Delete session from the given hash table. | |
573 | * | |
574 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
575 | */ | |
7591bab1 | 576 | static int session_delete(struct relay_session *session) |
2a174661 DG |
577 | { |
578 | struct lttng_ht_iter iter; | |
579 | ||
2a174661 | 580 | iter.iter.node = &session->session_n.node; |
7591bab1 | 581 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
582 | } |
583 | ||
7591bab1 MD |
584 | static void destroy_session(struct relay_session *session) |
585 | { | |
586 | int ret; | |
587 | ||
588 | ret = session_delete(session); | |
a0377dfe | 589 | LTTNG_ASSERT(!ret); |
639ddf68 | 590 | lttng_trace_chunk_put(session->current_trace_chunk); |
c35f9726 | 591 | session->current_trace_chunk = NULL; |
62bad3bf JG |
592 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
593 | session->pending_closure_trace_chunk = NULL; | |
28ab034a JG |
594 | ret = sessiond_trace_chunk_registry_session_destroyed(sessiond_trace_chunk_registry, |
595 | session->sessiond_uuid); | |
a0377dfe | 596 | LTTNG_ASSERT(!ret); |
7ceefac4 JG |
597 | lttng_directory_handle_put(session->output_directory); |
598 | session->output_directory = NULL; | |
7591bab1 MD |
599 | call_rcu(&session->rcu_node, rcu_destroy_session); |
600 | } | |
601 | ||
feb33caa | 602 | static void session_release(struct urcu_ref *ref) |
2a174661 | 603 | { |
28ab034a | 604 | struct relay_session *session = lttng::utils::container_of(ref, &relay_session::ref); |
2a174661 | 605 | |
7591bab1 MD |
606 | destroy_session(session); |
607 | } | |
2a174661 | 608 | |
7591bab1 MD |
609 | void session_put(struct relay_session *session) |
610 | { | |
874ec45e JG |
611 | if (!session) { |
612 | return; | |
613 | } | |
7591bab1 | 614 | rcu_read_lock(); |
7591bab1 | 615 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 616 | rcu_read_unlock(); |
2a174661 DG |
617 | } |
618 | ||
7591bab1 | 619 | int session_close(struct relay_session *session) |
2a174661 DG |
620 | { |
621 | int ret = 0; | |
7591bab1 MD |
622 | struct ctf_trace *trace; |
623 | struct lttng_ht_iter iter; | |
624 | struct relay_stream *stream; | |
625 | ||
626 | pthread_mutex_lock(&session->lock); | |
627 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
28ab034a JG |
628 | session->id, |
629 | session->connection_closed); | |
7591bab1 | 630 | session->connection_closed = true; |
7591bab1 | 631 | pthread_mutex_unlock(&session->lock); |
2a174661 | 632 | |
7591bab1 | 633 | rcu_read_lock(); |
28ab034a | 634 | cds_lfht_for_each_entry (session->ctf_traces_ht->ht, &iter.iter, trace, node.node) { |
7591bab1 MD |
635 | ret = ctf_trace_close(trace); |
636 | if (ret) { | |
637 | goto rcu_unlock; | |
2a174661 DG |
638 | } |
639 | } | |
28ab034a JG |
640 | cds_list_for_each_entry_rcu(stream, &session->recv_list, recv_node) |
641 | { | |
bda7c7b9 JG |
642 | /* Close streams which have not been published yet. */ |
643 | try_stream_close(stream); | |
7591bab1 MD |
644 | } |
645 | rcu_unlock: | |
646 | rcu_read_unlock(); | |
647 | if (ret) { | |
648 | return ret; | |
649 | } | |
650 | /* Put self-reference from create. */ | |
651 | session_put(session); | |
652 | return ret; | |
2a174661 DG |
653 | } |
654 | ||
98ba050e JR |
655 | int session_abort(struct relay_session *session) |
656 | { | |
657 | int ret = 0; | |
658 | ||
659 | if (!session) { | |
660 | return 0; | |
661 | } | |
662 | ||
663 | pthread_mutex_lock(&session->lock); | |
664 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 665 | session->aborted = true; |
98ba050e JR |
666 | pthread_mutex_unlock(&session->lock); |
667 | return ret; | |
668 | } | |
669 | ||
7591bab1 | 670 | void print_sessions(void) |
2a174661 | 671 | { |
2a174661 | 672 | struct lttng_ht_iter iter; |
7591bab1 | 673 | struct relay_session *session; |
2a174661 | 674 | |
ce3f3ba3 JG |
675 | if (!sessions_ht) { |
676 | return; | |
677 | } | |
678 | ||
2a174661 | 679 | rcu_read_lock(); |
28ab034a | 680 | cds_lfht_for_each_entry (sessions_ht->ht, &iter.iter, session, session_n.node) { |
7591bab1 MD |
681 | if (!session_get(session)) { |
682 | continue; | |
683 | } | |
684 | DBG("session %p refcount %ld session %" PRIu64, | |
28ab034a JG |
685 | session, |
686 | session->ref.refcount, | |
687 | session->id); | |
7591bab1 | 688 | session_put(session); |
2a174661 | 689 | } |
2a174661 | 690 | rcu_read_unlock(); |
2a174661 | 691 | } |