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