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, |
6fa5fe7c | 281 | const char *hostname, const char *base_path, |
db1da059 JG |
282 | uint32_t live_timer, |
283 | bool snapshot, | |
284 | const lttng_uuid sessiond_uuid, | |
285 | const uint64_t *id_sessiond, | |
286 | const uint64_t *current_chunk_id, | |
287 | const time_t *creation_time, | |
288 | uint32_t major, | |
46ef2188 MD |
289 | uint32_t minor, |
290 | bool session_name_contains_creation_time) | |
2a174661 | 291 | { |
23c8ff50 | 292 | int ret; |
590f0324 JG |
293 | struct relay_session *session = NULL; |
294 | ||
a0377dfe FD |
295 | LTTNG_ASSERT(session_name); |
296 | LTTNG_ASSERT(hostname); | |
297 | LTTNG_ASSERT(base_path); | |
5c956ba3 | 298 | |
6ec9dc48 JG |
299 | if (!is_name_path_safe(session_name)) { |
300 | ERR("Refusing to create session as the provided session name is not path-safe"); | |
301 | goto error; | |
302 | } | |
303 | if (!is_name_path_safe(hostname)) { | |
304 | ERR("Refusing to create session as the provided hostname is not path-safe"); | |
590f0324 JG |
305 | goto error; |
306 | } | |
5c956ba3 | 307 | if (strstr(base_path, "../")) { |
590f0324 JG |
308 | ERR("Invalid session base path walks up the path hierarchy: \"%s\"", |
309 | base_path); | |
310 | goto error; | |
311 | } | |
2a174661 | 312 | |
64803277 | 313 | session = zmalloc<relay_session>(); |
2a174661 | 314 | if (!session) { |
1e791a74 | 315 | PERROR("Failed to allocate session"); |
2a174661 DG |
316 | goto error; |
317 | } | |
19efdf65 JG |
318 | |
319 | pthread_mutex_lock(&last_relay_session_id_lock); | |
320 | session->id = ++last_relay_session_id; | |
321 | pthread_mutex_unlock(&last_relay_session_id_lock); | |
322 | ||
323 | lttng_ht_node_init_u64(&session->session_n, session->id); | |
324 | urcu_ref_init(&session->ref); | |
325 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
326 | pthread_mutex_init(&session->lock, NULL); | |
327 | pthread_mutex_init(&session->recv_list_lock, NULL); | |
328 | ||
bb5d54e7 MD |
329 | if (lttng_strncpy(session->session_name, session_name, |
330 | sizeof(session->session_name))) { | |
eec856bf | 331 | WARN("Session name exceeds maximal allowed length"); |
bb5d54e7 MD |
332 | goto error; |
333 | } | |
334 | if (lttng_strncpy(session->hostname, hostname, | |
335 | sizeof(session->hostname))) { | |
1e791a74 | 336 | WARN("Hostname exceeds maximal allowed length"); |
bb5d54e7 MD |
337 | goto error; |
338 | } | |
6fa5fe7c MD |
339 | if (lttng_strncpy(session->base_path, base_path, |
340 | sizeof(session->base_path))) { | |
341 | WARN("Base path exceeds maximal allowed length"); | |
342 | goto error; | |
343 | } | |
46ef2188 MD |
344 | if (creation_time) { |
345 | LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); | |
d2cb4a90 JG |
346 | } else { |
347 | LTTNG_OPTIONAL_SET(&session->creation_time, time(NULL)); | |
348 | if (session->creation_time.value == (time_t) -1) { | |
349 | PERROR("Failed to sample session creation time"); | |
350 | goto error; | |
351 | } | |
46ef2188 MD |
352 | } |
353 | session->session_name_contains_creation_time = | |
354 | session_name_contains_creation_time; | |
6fa5fe7c | 355 | |
2a174661 DG |
356 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
357 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
358 | goto error; |
359 | } | |
360 | ||
7591bab1 MD |
361 | session->major = major; |
362 | session->minor = minor; | |
7591bab1 | 363 | |
7591bab1 MD |
364 | session->live_timer = live_timer; |
365 | session->snapshot = snapshot; | |
23c8ff50 JG |
366 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
367 | ||
1e791a74 JG |
368 | if (id_sessiond) { |
369 | LTTNG_OPTIONAL_SET(&session->id_sessiond, *id_sessiond); | |
370 | } | |
371 | ||
d519f442 JR |
372 | if (major == 2 && minor >= 11) { |
373 | /* Only applies for 2.11+ peers using trace chunks. */ | |
374 | ret = init_session_output_path(session); | |
375 | if (ret) { | |
376 | goto error; | |
377 | } | |
ecd1a12f | 378 | } |
d519f442 | 379 | |
23c8ff50 JG |
380 | ret = sessiond_trace_chunk_registry_session_created( |
381 | sessiond_trace_chunk_registry, sessiond_uuid); | |
382 | if (ret) { | |
383 | goto error; | |
384 | } | |
7591bab1 | 385 | |
1e791a74 | 386 | if (id_sessiond && current_chunk_id) { |
7ceefac4 JG |
387 | enum lttng_trace_chunk_status chunk_status; |
388 | struct lttng_directory_handle *session_output_directory; | |
389 | ||
1e791a74 JG |
390 | session->current_trace_chunk = |
391 | sessiond_trace_chunk_registry_get_chunk( | |
392 | sessiond_trace_chunk_registry, | |
393 | session->sessiond_uuid, | |
394 | session->id_sessiond.value, | |
395 | *current_chunk_id); | |
396 | if (!session->current_trace_chunk) { | |
eec856bf | 397 | char uuid_str[LTTNG_UUID_STR_LEN]; |
1e791a74 JG |
398 | |
399 | lttng_uuid_to_str(sessiond_uuid, uuid_str); | |
400 | ERR("Could not find trace chunk: sessiond = {%s}, sessiond session id = %" PRIu64 ", trace chunk id = %" PRIu64, | |
401 | uuid_str, *id_sessiond, | |
402 | *current_chunk_id); | |
53eb691c | 403 | goto error; |
eec856bf | 404 | } |
7ceefac4 JG |
405 | |
406 | chunk_status = lttng_trace_chunk_get_session_output_directory_handle( | |
407 | session->current_trace_chunk, | |
408 | &session_output_directory); | |
409 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
410 | goto error; | |
411 | } | |
412 | ||
a0377dfe | 413 | LTTNG_ASSERT(session_output_directory); |
7ceefac4 | 414 | session->output_directory = session_output_directory; |
1e791a74 JG |
415 | } else if (!id_sessiond) { |
416 | /* | |
417 | * Pre-2.11 peers will not announce trace chunks. An | |
418 | * anonymous trace chunk which will remain set for the | |
419 | * duration of the session is created. | |
420 | */ | |
421 | ret = session_set_anonymous_chunk(session); | |
422 | if (ret) { | |
423 | goto error; | |
424 | } | |
7ceefac4 JG |
425 | } else { |
426 | session->output_directory = | |
427 | session_create_output_directory_handle(session); | |
428 | if (!session->output_directory) { | |
429 | goto error; | |
430 | } | |
1e791a74 JG |
431 | } |
432 | ||
7591bab1 | 433 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); |
bb5d54e7 | 434 | return session; |
2a174661 DG |
435 | |
436 | error: | |
1e791a74 | 437 | session_put(session); |
bb5d54e7 | 438 | return NULL; |
2a174661 | 439 | } |
2f8f53af | 440 | |
7591bab1 MD |
441 | /* Should be called with RCU read-side lock held. */ |
442 | bool session_get(struct relay_session *session) | |
443 | { | |
ce4d4083 | 444 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
445 | } |
446 | ||
2f8f53af | 447 | /* |
7591bab1 MD |
448 | * Lookup a session within the session hash table using the session id |
449 | * as key. A session reference is taken when a session is returned. | |
450 | * session_put() must be called on that session. | |
2f8f53af DG |
451 | * |
452 | * Return session or NULL if not found. | |
453 | */ | |
7591bab1 | 454 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
455 | { |
456 | struct relay_session *session = NULL; | |
2a174661 | 457 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
458 | struct lttng_ht_iter iter; |
459 | ||
7591bab1 MD |
460 | rcu_read_lock(); |
461 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 462 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 463 | if (!node) { |
2a174661 | 464 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
465 | goto end; |
466 | } | |
467 | session = caa_container_of(node, struct relay_session, session_n); | |
2a174661 | 468 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
469 | if (!session_get(session)) { |
470 | session = NULL; | |
471 | } | |
2f8f53af | 472 | end: |
7591bab1 | 473 | rcu_read_unlock(); |
2f8f53af DG |
474 | return session; |
475 | } | |
2a174661 | 476 | |
fe88e517 JG |
477 | /* |
478 | * Check if any of the relay sessions originating from the same | |
479 | * session daemon session have the 'ongoing_rotation' state set. | |
480 | * | |
481 | * The caller must hold the lock of session. | |
482 | */ | |
483 | bool session_has_ongoing_rotation(const struct relay_session *session) | |
484 | { | |
485 | bool ongoing_rotation = false; | |
486 | struct lttng_ht_iter iter; | |
487 | struct relay_session *iterated_session; | |
488 | ||
489 | ASSERT_LOCKED(session->lock); | |
490 | ||
491 | if (!session->id_sessiond.is_set) { | |
492 | /* | |
493 | * The peer that created this session is too old to | |
494 | * support rotations; we can assume no rotations are ongoing. | |
495 | */ | |
496 | goto end; | |
497 | } | |
498 | ||
499 | if (session->ongoing_rotation) { | |
500 | ongoing_rotation = true; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | rcu_read_lock(); | |
505 | /* | |
506 | * Sample the 'ongoing_rotation' status of all relay sessions that | |
507 | * originate from the same session daemon session. | |
508 | */ | |
509 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, iterated_session, | |
510 | session_n.node) { | |
511 | if (!session_get(iterated_session)) { | |
512 | continue; | |
513 | } | |
514 | ||
515 | if (session == iterated_session) { | |
516 | /* Skip this session. */ | |
517 | goto next_session_no_unlock; | |
518 | } | |
519 | ||
520 | pthread_mutex_lock(&iterated_session->lock); | |
521 | ||
522 | if (!iterated_session->id_sessiond.is_set) { | |
523 | /* | |
524 | * Session belongs to a peer that doesn't support | |
525 | * rotations. | |
526 | */ | |
527 | goto next_session; | |
528 | } | |
529 | ||
530 | if (!lttng_uuid_is_equal(session->sessiond_uuid, | |
531 | iterated_session->sessiond_uuid)) { | |
532 | /* Sessions do not originate from the same sessiond. */ | |
533 | goto next_session; | |
534 | } | |
535 | ||
536 | if (LTTNG_OPTIONAL_GET(session->id_sessiond) != | |
537 | LTTNG_OPTIONAL_GET(iterated_session->id_sessiond)) { | |
538 | /* | |
539 | * Sessions do not originate from the same sessiond | |
540 | * session. | |
541 | */ | |
542 | goto next_session; | |
543 | } | |
544 | ||
545 | ongoing_rotation = iterated_session->ongoing_rotation; | |
546 | ||
547 | next_session: | |
548 | pthread_mutex_unlock(&iterated_session->lock); | |
549 | next_session_no_unlock: | |
550 | session_put(iterated_session); | |
551 | ||
552 | if (ongoing_rotation) { | |
553 | break; | |
554 | } | |
555 | } | |
556 | rcu_read_unlock(); | |
557 | ||
558 | end: | |
559 | return ongoing_rotation; | |
560 | } | |
561 | ||
7591bab1 MD |
562 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
563 | { | |
564 | struct relay_session *session = | |
565 | caa_container_of(rcu_head, struct relay_session, | |
566 | rcu_node); | |
49e614cb MD |
567 | /* |
568 | * Since each trace has a reference on the session, it means | |
569 | * that if we are at the point where we teardown the session, no | |
570 | * trace belonging to that session exist at this point. | |
571 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
572 | * don't hold the RCU read-side lock while calling it. | |
573 | */ | |
574 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
575 | free(session); |
576 | } | |
577 | ||
2a174661 DG |
578 | /* |
579 | * Delete session from the given hash table. | |
580 | * | |
581 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
582 | */ | |
7591bab1 | 583 | static int session_delete(struct relay_session *session) |
2a174661 DG |
584 | { |
585 | struct lttng_ht_iter iter; | |
586 | ||
2a174661 | 587 | iter.iter.node = &session->session_n.node; |
7591bab1 | 588 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
589 | } |
590 | ||
7591bab1 MD |
591 | |
592 | static void destroy_session(struct relay_session *session) | |
593 | { | |
594 | int ret; | |
595 | ||
596 | ret = session_delete(session); | |
a0377dfe | 597 | LTTNG_ASSERT(!ret); |
639ddf68 | 598 | lttng_trace_chunk_put(session->current_trace_chunk); |
c35f9726 | 599 | session->current_trace_chunk = NULL; |
62bad3bf JG |
600 | lttng_trace_chunk_put(session->pending_closure_trace_chunk); |
601 | session->pending_closure_trace_chunk = NULL; | |
23c8ff50 JG |
602 | ret = sessiond_trace_chunk_registry_session_destroyed( |
603 | sessiond_trace_chunk_registry, session->sessiond_uuid); | |
a0377dfe | 604 | LTTNG_ASSERT(!ret); |
7ceefac4 JG |
605 | lttng_directory_handle_put(session->output_directory); |
606 | session->output_directory = NULL; | |
7591bab1 MD |
607 | call_rcu(&session->rcu_node, rcu_destroy_session); |
608 | } | |
609 | ||
feb33caa | 610 | static void session_release(struct urcu_ref *ref) |
2a174661 | 611 | { |
7591bab1 MD |
612 | struct relay_session *session = |
613 | caa_container_of(ref, struct relay_session, ref); | |
2a174661 | 614 | |
7591bab1 MD |
615 | destroy_session(session); |
616 | } | |
2a174661 | 617 | |
7591bab1 MD |
618 | void session_put(struct relay_session *session) |
619 | { | |
874ec45e JG |
620 | if (!session) { |
621 | return; | |
622 | } | |
7591bab1 | 623 | rcu_read_lock(); |
7591bab1 | 624 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 625 | rcu_read_unlock(); |
2a174661 DG |
626 | } |
627 | ||
7591bab1 | 628 | int session_close(struct relay_session *session) |
2a174661 DG |
629 | { |
630 | int ret = 0; | |
7591bab1 MD |
631 | struct ctf_trace *trace; |
632 | struct lttng_ht_iter iter; | |
633 | struct relay_stream *stream; | |
634 | ||
635 | pthread_mutex_lock(&session->lock); | |
636 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
637 | session->id, session->connection_closed); | |
7591bab1 | 638 | session->connection_closed = true; |
7591bab1 | 639 | pthread_mutex_unlock(&session->lock); |
2a174661 | 640 | |
7591bab1 MD |
641 | rcu_read_lock(); |
642 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, | |
643 | &iter.iter, trace, node.node) { | |
644 | ret = ctf_trace_close(trace); | |
645 | if (ret) { | |
646 | goto rcu_unlock; | |
2a174661 DG |
647 | } |
648 | } | |
7591bab1 MD |
649 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
650 | recv_node) { | |
bda7c7b9 JG |
651 | /* Close streams which have not been published yet. */ |
652 | try_stream_close(stream); | |
7591bab1 MD |
653 | } |
654 | rcu_unlock: | |
655 | rcu_read_unlock(); | |
656 | if (ret) { | |
657 | return ret; | |
658 | } | |
659 | /* Put self-reference from create. */ | |
660 | session_put(session); | |
661 | return ret; | |
2a174661 DG |
662 | } |
663 | ||
98ba050e JR |
664 | int session_abort(struct relay_session *session) |
665 | { | |
666 | int ret = 0; | |
667 | ||
668 | if (!session) { | |
669 | return 0; | |
670 | } | |
671 | ||
672 | pthread_mutex_lock(&session->lock); | |
673 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 674 | session->aborted = true; |
98ba050e JR |
675 | pthread_mutex_unlock(&session->lock); |
676 | return ret; | |
677 | } | |
678 | ||
7591bab1 | 679 | void print_sessions(void) |
2a174661 | 680 | { |
2a174661 | 681 | struct lttng_ht_iter iter; |
7591bab1 | 682 | struct relay_session *session; |
2a174661 | 683 | |
ce3f3ba3 JG |
684 | if (!sessions_ht) { |
685 | return; | |
686 | } | |
687 | ||
2a174661 | 688 | rcu_read_lock(); |
7591bab1 MD |
689 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
690 | session_n.node) { | |
691 | if (!session_get(session)) { | |
692 | continue; | |
693 | } | |
694 | DBG("session %p refcount %ld session %" PRIu64, | |
695 | session, | |
696 | session->ref.refcount, | |
697 | session->id); | |
698 | session_put(session); | |
2a174661 | 699 | } |
2a174661 | 700 | rcu_read_unlock(); |
2a174661 | 701 | } |