b588e605510e48b9118c391d4e3dc3c1ee3f5552
[lttng-tools.git] / src / bin / lttng-sessiond / session.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "cmd.hpp"
10 #include "kernel.hpp"
11 #include "lttng-sessiond.hpp"
12 #include "session.hpp"
13 #include "timer.hpp"
14 #include "trace-ust.hpp"
15 #include "utils.hpp"
16
17 #include <common/common.hpp>
18 #include <common/sessiond-comm/sessiond-comm.hpp>
19 #include <common/trace-chunk.hpp>
20 #include <common/urcu.hpp>
21 #include <common/utils.hpp>
22
23 #include <lttng/location-internal.hpp>
24
25 #include <dirent.h>
26 #include <inttypes.h>
27 #include <limits.h>
28 #include <pthread.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <urcu.h>
35
36 namespace {
37 struct ltt_session_destroy_notifier_element {
38 ltt_session_destroy_notifier notifier;
39 void *user_data;
40 };
41
42 struct ltt_session_clear_notifier_element {
43 ltt_session_clear_notifier notifier;
44 void *user_data;
45 };
46
47 namespace ls = lttng::sessiond;
48
49 /*
50 * NOTES:
51 *
52 * No ltt_session.lock is taken here because those data structure are widely
53 * spread across the lttng-tools code base so before calling functions below
54 * that can read/write a session, the caller MUST acquire the session lock
55 * using session_lock() and session_unlock().
56 */
57
58 /* These characters are forbidden in a session name. Used by validate_name. */
59 const char *forbidden_name_chars = "/";
60
61 /* Global hash table to keep the sessions, indexed by id. */
62 struct lttng_ht *ltt_sessions_ht_by_id = nullptr;
63 /* Global hash table to keep the sessions, indexed by name. */
64 struct lttng_ht *ltt_sessions_ht_by_name = nullptr;
65
66 /*
67 * Init tracing session list.
68 *
69 * Please see session.h for more explanation and correct usage of the list.
70 */
71 struct ltt_session_list the_session_list;
72
73 /*
74 * Return a ltt_session structure ptr that matches name. If no session found,
75 * NULL is returned. This must be called with the session list lock held using
76 * session_lock_list and session_unlock_list.
77 * A reference to the session is implicitly acquired by this function.
78 */
79 struct ltt_session *session_find_by_name(const char *name)
80 {
81 struct ltt_session *iter;
82
83 LTTNG_ASSERT(name);
84 ASSERT_SESSION_LIST_LOCKED();
85
86 DBG2("Trying to find session by name %s", name);
87
88 cds_list_for_each_entry (iter, &the_session_list.head, list) {
89 if (!strncmp(iter->name, name, NAME_MAX) && !iter->destroyed) {
90 goto found;
91 }
92 }
93
94 return nullptr;
95 found:
96 return session_get(iter) ? iter : nullptr;
97 }
98
99 /*
100 * Return an ltt_session that matches the id. If no session is found,
101 * NULL is returned. This must be called with rcu_read_lock and
102 * session list lock held (to guarantee the lifetime of the session).
103 */
104 struct ltt_session *session_find_by_id(uint64_t id)
105 {
106 struct lttng_ht_node_u64 *node;
107 struct lttng_ht_iter iter;
108 struct ltt_session *ls;
109
110 ASSERT_RCU_READ_LOCKED();
111 ASSERT_SESSION_LIST_LOCKED();
112
113 if (!ltt_sessions_ht_by_id) {
114 goto end;
115 }
116
117 lttng_ht_lookup(ltt_sessions_ht_by_id, &id, &iter);
118 node = lttng_ht_iter_get_node_u64(&iter);
119 if (node == nullptr) {
120 goto end;
121 }
122 ls = lttng::utils::container_of(node, &ltt_session::node);
123
124 DBG3("Session %" PRIu64 " found by id.", id);
125 return session_get(ls) ? ls : nullptr;
126
127 end:
128 DBG3("Session %" PRIu64 " NOT found by id", id);
129 return nullptr;
130 }
131 } /* namespace */
132
133 /*
134 * Validate the session name for forbidden characters.
135 *
136 * Return 0 on success else -1 meaning a forbidden char. has been found.
137 */
138 static int validate_name(const char *name)
139 {
140 int ret;
141 char *tok, *tmp_name;
142
143 LTTNG_ASSERT(name);
144
145 tmp_name = strdup(name);
146 if (!tmp_name) {
147 /* ENOMEM here. */
148 ret = -1;
149 goto error;
150 }
151
152 tok = strpbrk(tmp_name, forbidden_name_chars);
153 if (tok) {
154 DBG("Session name %s contains a forbidden character", name);
155 /* Forbidden character has been found. */
156 ret = -1;
157 goto error;
158 }
159 ret = 0;
160
161 error:
162 free(tmp_name);
163 return ret;
164 }
165
166 /*
167 * Add a ltt_session structure to the global list.
168 *
169 * The caller MUST acquire the session list lock before.
170 * Returns the unique identifier for the session.
171 */
172 static uint64_t add_session_list(struct ltt_session *ls)
173 {
174 LTTNG_ASSERT(ls);
175
176 cds_list_add(&ls->list, &the_session_list.head);
177 return the_session_list.next_uuid++;
178 }
179
180 /*
181 * Delete a ltt_session structure to the global list.
182 *
183 * The caller MUST acquire the session list lock before.
184 */
185 static void del_session_list(struct ltt_session *ls)
186 {
187 LTTNG_ASSERT(ls);
188
189 cds_list_del(&ls->list);
190 }
191
192 /*
193 * Return a pointer to the session list.
194 */
195 struct ltt_session_list *session_get_list()
196 {
197 return &the_session_list;
198 }
199
200 /*
201 * Returns once the session list is empty.
202 */
203 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock)
204 {
205 /* Keep waiting until the session list is empty. */
206 the_session_list.removal_cond.wait(list_lock,
207 [] { return cds_list_empty(&the_session_list.head); });
208 }
209
210 /*
211 * Try to acquire session list lock
212 */
213 int session_trylock_list() noexcept
214 {
215 /* Return 0 if successfully acquired. */
216 return the_session_list.lock.try_lock() ? 0 : 1;
217 }
218
219 /*
220 * Get the session's consumer destination type.
221 *
222 * The caller must hold the session lock.
223 */
224 enum consumer_dst_type session_get_consumer_destination_type(const struct ltt_session *session)
225 {
226 /*
227 * The output information is duplicated in both of those session types.
228 * Hence, it doesn't matter from which it is retrieved. However, it is
229 * possible for only one of them to be set.
230 */
231 return session->kernel_session ? session->kernel_session->consumer->type :
232 session->ust_session->consumer->type;
233 }
234
235 /*
236 * Get the session's consumer network hostname.
237 * The caller must ensure that the destination is of type "net".
238 *
239 * The caller must hold the session lock.
240 */
241 const char *session_get_net_consumer_hostname(const struct ltt_session *session)
242 {
243 const char *hostname = nullptr;
244 const struct consumer_output *output;
245
246 output = session->kernel_session ? session->kernel_session->consumer :
247 session->ust_session->consumer;
248
249 /*
250 * hostname is assumed to be the same for both control and data
251 * connections.
252 */
253 switch (output->dst.net.control.dtype) {
254 case LTTNG_DST_IPV4:
255 hostname = output->dst.net.control.dst.ipv4;
256 break;
257 case LTTNG_DST_IPV6:
258 hostname = output->dst.net.control.dst.ipv6;
259 break;
260 default:
261 abort();
262 }
263 return hostname;
264 }
265
266 /*
267 * Get the session's consumer network control and data ports.
268 * The caller must ensure that the destination is of type "net".
269 *
270 * The caller must hold the session lock.
271 */
272 void session_get_net_consumer_ports(const struct ltt_session *session,
273 uint16_t *control_port,
274 uint16_t *data_port)
275 {
276 const struct consumer_output *output;
277
278 output = session->kernel_session ? session->kernel_session->consumer :
279 session->ust_session->consumer;
280 *control_port = output->dst.net.control.port;
281 *data_port = output->dst.net.data.port;
282 }
283
284 /*
285 * Get the location of the latest trace archive produced by a rotation.
286 *
287 * The caller must hold the session lock.
288 */
289 struct lttng_trace_archive_location *
290 session_get_trace_archive_location(const struct ltt_session *session)
291 {
292 int ret;
293 struct lttng_trace_archive_location *location = nullptr;
294 char *chunk_path = nullptr;
295
296 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED ||
297 !session->last_archived_chunk_name) {
298 goto end;
299 }
300
301 switch (session_get_consumer_destination_type(session)) {
302 case CONSUMER_DST_LOCAL:
303 ret = asprintf(&chunk_path,
304 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
305 session_get_base_path(session),
306 session->last_archived_chunk_name);
307 if (ret == -1) {
308 goto end;
309 }
310 location = lttng_trace_archive_location_local_create(chunk_path);
311 break;
312 case CONSUMER_DST_NET:
313 {
314 const char *hostname;
315 uint16_t control_port, data_port;
316
317 hostname = session_get_net_consumer_hostname(session);
318 session_get_net_consumer_ports(session, &control_port, &data_port);
319 location = lttng_trace_archive_location_relay_create(
320 hostname,
321 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP,
322 control_port,
323 data_port,
324 session->last_chunk_path);
325 break;
326 }
327 default:
328 abort();
329 }
330 end:
331 free(chunk_path);
332 return location;
333 }
334
335 /*
336 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
337 *
338 * The session list lock must be held.
339 */
340 static int ltt_sessions_ht_alloc()
341 {
342 int ret = 0;
343
344 DBG("Allocating ltt_sessions_ht_by_id");
345 ltt_sessions_ht_by_id = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
346 if (!ltt_sessions_ht_by_id) {
347 ret = -1;
348 ERR("Failed to allocate ltt_sessions_ht_by_id");
349 goto end;
350 }
351
352 DBG("Allocating ltt_sessions_ht_by_name");
353 ltt_sessions_ht_by_name = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
354 if (!ltt_sessions_ht_by_name) {
355 ret = -1;
356 ERR("Failed to allocate ltt_sessions_ht_by_name");
357 goto end;
358 }
359
360 end:
361 return ret;
362 }
363
364 /*
365 * Destroy the ltt_sessions_ht_by_id HT.
366 *
367 * The session list lock must be held.
368 */
369 static void ltt_sessions_ht_destroy()
370 {
371 if (ltt_sessions_ht_by_id) {
372 lttng_ht_destroy(ltt_sessions_ht_by_id);
373 ltt_sessions_ht_by_id = nullptr;
374 }
375
376 if (ltt_sessions_ht_by_name) {
377 lttng_ht_destroy(ltt_sessions_ht_by_name);
378 ltt_sessions_ht_by_name = nullptr;
379 }
380
381 return;
382 }
383
384 /*
385 * Add a ltt_session to the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name.
386 * If unallocated, the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name. HTs
387 * are allocated. The session list lock must be held.
388 */
389 static void add_session_ht(struct ltt_session *ls)
390 {
391 int ret;
392
393 LTTNG_ASSERT(ls);
394
395 if (!ltt_sessions_ht_by_id) {
396 ret = ltt_sessions_ht_alloc();
397 if (ret) {
398 ERR("Error allocating the sessions HT");
399 goto end;
400 }
401 }
402
403 /* Should always be present with ltt_sessions_ht_by_id. */
404 LTTNG_ASSERT(ltt_sessions_ht_by_name);
405
406 lttng_ht_node_init_u64(&ls->node, ls->id);
407 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id, &ls->node);
408
409 lttng_ht_node_init_str(&ls->node_by_name, ls->name);
410 lttng_ht_add_unique_str(ltt_sessions_ht_by_name, &ls->node_by_name);
411
412 end:
413 return;
414 }
415
416 /*
417 * Test if ltt_sessions_ht_by_id/name are empty.
418 * Return `false` if hash table objects are null.
419 * The session list lock must be held.
420 */
421 static bool ltt_sessions_ht_empty()
422 {
423 bool empty = false;
424
425 if (!ltt_sessions_ht_by_id) {
426 /* The hash tables do not exist yet. */
427 goto end;
428 }
429
430 LTTNG_ASSERT(ltt_sessions_ht_by_name);
431
432 if (lttng_ht_get_count(ltt_sessions_ht_by_id) != 0) {
433 /* Not empty.*/
434 goto end;
435 }
436
437 /*
438 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
439 * empty.
440 *
441 * The removal from both hash tables is done in two different
442 * places:
443 * - removal from `ltt_sessions_ht_by_name` is done during
444 * `session_destroy()`
445 * - removal from `ltt_sessions_ht_by_id` is done later
446 * in `session_release()` on the last reference put.
447 *
448 * This means that it is possible for `ltt_sessions_ht_by_name` to be
449 * empty but for `ltt_sessions_ht_by_id` to still contain objects when
450 * multiple sessions exists. The reverse is false, hence this sanity
451 * check.
452 */
453 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name) == 0);
454 empty = true;
455 end:
456 return empty;
457 }
458
459 /*
460 * Remove a ltt_session from the ltt_sessions_ht_by_id.
461 * If empty, the ltt_sessions_ht_by_id/name HTs are freed.
462 * The session list lock must be held.
463 */
464 static void del_session_ht(struct ltt_session *ls)
465 {
466 struct lttng_ht_iter iter;
467 int ret;
468
469 LTTNG_ASSERT(ls);
470 LTTNG_ASSERT(ltt_sessions_ht_by_id);
471 LTTNG_ASSERT(ltt_sessions_ht_by_name);
472
473 iter.iter.node = &ls->node.node;
474 ret = lttng_ht_del(ltt_sessions_ht_by_id, &iter);
475 LTTNG_ASSERT(!ret);
476
477 if (ltt_sessions_ht_empty()) {
478 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
479 ltt_sessions_ht_destroy();
480 }
481 }
482
483 /*
484 * Acquire session lock
485 */
486 void session_lock(struct ltt_session *session)
487 {
488 LTTNG_ASSERT(session);
489 session->lock();
490 }
491
492 void ltt_session::lock() const noexcept
493 {
494 pthread_mutex_lock(&_lock);
495 }
496
497 void ltt_session::unlock() const noexcept
498 {
499 ltt_session::_const_session_unlock(*this);
500 }
501
502 /*
503 * Release session lock
504 */
505 void session_unlock(struct ltt_session *session)
506 {
507 LTTNG_ASSERT(session);
508 session->unlock();
509 }
510
511 void ltt_session::_const_session_unlock(const ltt_session &session)
512 {
513 pthread_mutex_unlock(&session._lock);
514 }
515
516 static int _session_set_trace_chunk_no_lock_check(struct ltt_session *session,
517 struct lttng_trace_chunk *new_trace_chunk,
518 struct lttng_trace_chunk **_current_trace_chunk)
519 {
520 int ret = 0;
521 unsigned int i, refs_to_acquire = 0, refs_acquired = 0, refs_to_release = 0;
522 struct cds_lfht_iter iter;
523 struct consumer_socket *socket;
524 struct lttng_trace_chunk *current_trace_chunk;
525 uint64_t chunk_id;
526 enum lttng_trace_chunk_status chunk_status;
527
528 lttng::urcu::read_lock_guard read_lock;
529 /*
530 * Ownership of current trace chunk is transferred to
531 * `current_trace_chunk`.
532 */
533 current_trace_chunk = session->current_trace_chunk;
534 session->current_trace_chunk = nullptr;
535 if (session->ust_session) {
536 lttng_trace_chunk_put(session->ust_session->current_trace_chunk);
537 session->ust_session->current_trace_chunk = nullptr;
538 }
539 if (session->kernel_session) {
540 lttng_trace_chunk_put(session->kernel_session->current_trace_chunk);
541 session->kernel_session->current_trace_chunk = nullptr;
542 }
543 if (!new_trace_chunk) {
544 ret = 0;
545 goto end;
546 }
547 chunk_status = lttng_trace_chunk_get_id(new_trace_chunk, &chunk_id);
548 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
549
550 refs_to_acquire = 1;
551 refs_to_acquire += !!session->ust_session;
552 refs_to_acquire += !!session->kernel_session;
553
554 for (refs_acquired = 0; refs_acquired < refs_to_acquire; refs_acquired++) {
555 if (!lttng_trace_chunk_get(new_trace_chunk)) {
556 ERR("Failed to acquire reference to new trace chunk of session \"%s\"",
557 session->name);
558 goto error;
559 }
560 }
561
562 if (session->ust_session) {
563 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
564 const bool is_local_trace = session->ust_session->consumer->type ==
565 CONSUMER_DST_LOCAL;
566
567 session->ust_session->current_trace_chunk = new_trace_chunk;
568 if (is_local_trace) {
569 enum lttng_error_code ret_error_code;
570
571 ret_error_code =
572 ust_app_create_channel_subdirectories(session->ust_session);
573 if (ret_error_code != LTTNG_OK) {
574 goto error;
575 }
576 }
577 cds_lfht_for_each_entry (
578 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
579 pthread_mutex_lock(socket->lock);
580 ret = consumer_create_trace_chunk(socket,
581 relayd_id,
582 session->id,
583 new_trace_chunk,
584 DEFAULT_UST_TRACE_DIR);
585 pthread_mutex_unlock(socket->lock);
586 if (ret) {
587 goto error;
588 }
589 }
590 }
591 if (session->kernel_session) {
592 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
593 const bool is_local_trace = session->kernel_session->consumer->type ==
594 CONSUMER_DST_LOCAL;
595
596 session->kernel_session->current_trace_chunk = new_trace_chunk;
597 if (is_local_trace) {
598 enum lttng_error_code ret_error_code;
599
600 ret_error_code =
601 kernel_create_channel_subdirectories(session->kernel_session);
602 if (ret_error_code != LTTNG_OK) {
603 goto error;
604 }
605 }
606 cds_lfht_for_each_entry (
607 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
608 pthread_mutex_lock(socket->lock);
609 ret = consumer_create_trace_chunk(socket,
610 relayd_id,
611 session->id,
612 new_trace_chunk,
613 DEFAULT_KERNEL_TRACE_DIR);
614 pthread_mutex_unlock(socket->lock);
615 if (ret) {
616 goto error;
617 }
618 }
619 }
620
621 /*
622 * Update local current trace chunk state last, only if all remote
623 * creations succeeded.
624 */
625 session->current_trace_chunk = new_trace_chunk;
626 LTTNG_OPTIONAL_SET(&session->most_recent_chunk_id, chunk_id);
627 end:
628 if (_current_trace_chunk) {
629 *_current_trace_chunk = current_trace_chunk;
630 current_trace_chunk = nullptr;
631 }
632 end_no_move:
633 lttng_trace_chunk_put(current_trace_chunk);
634 return ret;
635 error:
636 if (session->ust_session) {
637 session->ust_session->current_trace_chunk = nullptr;
638 }
639 if (session->kernel_session) {
640 session->kernel_session->current_trace_chunk = nullptr;
641 }
642 /*
643 * Release references taken in the case where all references could not
644 * be acquired.
645 */
646 refs_to_release = refs_to_acquire - refs_acquired;
647 for (i = 0; i < refs_to_release; i++) {
648 lttng_trace_chunk_put(new_trace_chunk);
649 }
650 ret = -1;
651 goto end_no_move;
652 }
653
654 struct lttng_trace_chunk *
655 session_create_new_trace_chunk(const struct ltt_session *session,
656 const struct consumer_output *consumer_output_override,
657 const char *session_base_path_override,
658 const char *chunk_name_override)
659 {
660 int ret;
661 struct lttng_trace_chunk *trace_chunk = nullptr;
662 enum lttng_trace_chunk_status chunk_status;
663 const time_t chunk_creation_ts = time(nullptr);
664 bool is_local_trace;
665 const char *base_path;
666 struct lttng_directory_handle *session_output_directory = nullptr;
667 const struct lttng_credentials session_credentials = {
668 .uid = LTTNG_OPTIONAL_INIT_VALUE(session->uid),
669 .gid = LTTNG_OPTIONAL_INIT_VALUE(session->gid),
670 };
671 uint64_t next_chunk_id;
672 const struct consumer_output *output;
673 const char *new_path;
674
675 if (consumer_output_override) {
676 output = consumer_output_override;
677 } else {
678 LTTNG_ASSERT(session->ust_session || session->kernel_session);
679 output = session->ust_session ? session->ust_session->consumer :
680 session->kernel_session->consumer;
681 }
682
683 is_local_trace = output->type == CONSUMER_DST_LOCAL;
684 base_path = session_base_path_override ?: consumer_output_get_base_path(output);
685
686 if (chunk_creation_ts == (time_t) -1) {
687 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
688 session->name);
689 goto error;
690 }
691
692 next_chunk_id =
693 session->most_recent_chunk_id.is_set ? session->most_recent_chunk_id.value + 1 : 0;
694
695 if (session->current_trace_chunk &&
696 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
697 chunk_status = lttng_trace_chunk_rename_path(session->current_trace_chunk,
698 DEFAULT_CHUNK_TMP_OLD_DIRECTORY);
699 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
700 goto error;
701 }
702 }
703 if (!session->current_trace_chunk) {
704 if (!session->rotated) {
705 new_path = "";
706 } else {
707 new_path = nullptr;
708 }
709 } else {
710 new_path = DEFAULT_CHUNK_TMP_NEW_DIRECTORY;
711 }
712
713 trace_chunk = lttng_trace_chunk_create(next_chunk_id, chunk_creation_ts, new_path);
714 if (!trace_chunk) {
715 goto error;
716 }
717
718 if (chunk_name_override) {
719 chunk_status = lttng_trace_chunk_override_name(trace_chunk, chunk_name_override);
720 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
721 goto error;
722 }
723 }
724
725 if (!is_local_trace) {
726 /*
727 * No need to set crendentials and output directory
728 * for remote trace chunks.
729 */
730 goto end;
731 }
732
733 chunk_status = lttng_trace_chunk_set_credentials(trace_chunk, &session_credentials);
734 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
735 goto error;
736 }
737
738 DBG("Creating base output directory of session \"%s\" at %s", session->name, base_path);
739 ret = utils_mkdir_recursive(base_path, S_IRWXU | S_IRWXG, session->uid, session->gid);
740 if (ret) {
741 goto error;
742 }
743 session_output_directory = lttng_directory_handle_create(base_path);
744 if (!session_output_directory) {
745 goto error;
746 }
747 chunk_status = lttng_trace_chunk_set_as_owner(trace_chunk, session_output_directory);
748 lttng_directory_handle_put(session_output_directory);
749 session_output_directory = nullptr;
750 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
751 goto error;
752 }
753 end:
754 return trace_chunk;
755 error:
756 lttng_directory_handle_put(session_output_directory);
757 lttng_trace_chunk_put(trace_chunk);
758 trace_chunk = nullptr;
759 goto end;
760 }
761
762 int session_close_trace_chunk(struct ltt_session *session,
763 struct lttng_trace_chunk *trace_chunk,
764 enum lttng_trace_chunk_command_type close_command,
765 char *closed_trace_chunk_path)
766 {
767 int ret = 0;
768 bool error_occurred = false;
769 struct cds_lfht_iter iter;
770 struct consumer_socket *socket;
771 enum lttng_trace_chunk_status chunk_status;
772 const time_t chunk_close_timestamp = time(nullptr);
773 const char *new_path;
774
775 chunk_status = lttng_trace_chunk_set_close_command(trace_chunk, close_command);
776 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
777 ret = -1;
778 goto end;
779 }
780
781 if (chunk_close_timestamp == (time_t) -1) {
782 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
783 session->name);
784 ret = -1;
785 goto end;
786 }
787
788 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE && !session->rotated) {
789 /* New chunk stays in session output directory. */
790 new_path = "";
791 } else {
792 /* Use chunk name for new chunk. */
793 new_path = nullptr;
794 }
795 if (session->current_trace_chunk &&
796 !lttng_trace_chunk_get_name_overridden(session->current_trace_chunk)) {
797 /* Rename new chunk path. */
798 chunk_status =
799 lttng_trace_chunk_rename_path(session->current_trace_chunk, new_path);
800 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
801 ret = -1;
802 goto end;
803 }
804 }
805 if (!lttng_trace_chunk_get_name_overridden(trace_chunk) &&
806 close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION) {
807 const char *old_path;
808
809 if (!session->rotated) {
810 old_path = "";
811 } else {
812 old_path = nullptr;
813 }
814 /* We need to move back the .tmp_old_chunk to its rightful place. */
815 chunk_status = lttng_trace_chunk_rename_path(trace_chunk, old_path);
816 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
817 ret = -1;
818 goto end;
819 }
820 }
821 if (close_command == LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED) {
822 session->rotated = true;
823 }
824 chunk_status = lttng_trace_chunk_set_close_timestamp(trace_chunk, chunk_close_timestamp);
825 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
826 ERR("Failed to set the close timestamp of the current trace chunk of session \"%s\"",
827 session->name);
828 ret = -1;
829 goto end;
830 }
831
832 if (session->ust_session) {
833 const uint64_t relayd_id = session->ust_session->consumer->net_seq_index;
834
835 cds_lfht_for_each_entry (
836 session->ust_session->consumer->socks->ht, &iter, socket, node.node) {
837 pthread_mutex_lock(socket->lock);
838 ret = consumer_close_trace_chunk(socket,
839 relayd_id,
840 session->id,
841 trace_chunk,
842 closed_trace_chunk_path);
843 pthread_mutex_unlock(socket->lock);
844 if (ret) {
845 ERR("Failed to close trace chunk on user space consumer");
846 error_occurred = true;
847 }
848 }
849 }
850 if (session->kernel_session) {
851 const uint64_t relayd_id = session->kernel_session->consumer->net_seq_index;
852
853 cds_lfht_for_each_entry (
854 session->kernel_session->consumer->socks->ht, &iter, socket, node.node) {
855 pthread_mutex_lock(socket->lock);
856 ret = consumer_close_trace_chunk(socket,
857 relayd_id,
858 session->id,
859 trace_chunk,
860 closed_trace_chunk_path);
861 pthread_mutex_unlock(socket->lock);
862 if (ret) {
863 ERR("Failed to close trace chunk on kernel consumer");
864 error_occurred = true;
865 }
866 }
867 }
868 ret = error_occurred ? -1 : 0;
869 end:
870 return ret;
871 }
872
873 /*
874 * This function skips the metadata channel as the begin/end timestamps of a
875 * metadata packet are useless.
876 *
877 * Moreover, opening a packet after a "clear" will cause problems for live
878 * sessions as it will introduce padding that was not part of the first trace
879 * chunk. The relay daemon expects the content of the metadata stream of
880 * successive metadata trace chunks to be strict supersets of one another.
881 *
882 * For example, flushing a packet at the beginning of the metadata stream of
883 * a trace chunk resulting from a "clear" session command will cause the
884 * size of the metadata stream of the new trace chunk to not match the size of
885 * the metadata stream of the original chunk. This will confuse the relay
886 * daemon as the same "offset" in a metadata stream will no longer point
887 * to the same content.
888 */
889 static enum lttng_error_code session_kernel_open_packets(struct ltt_session *session)
890 {
891 enum lttng_error_code ret = LTTNG_OK;
892 struct consumer_socket *socket;
893 struct lttng_ht_iter iter;
894 struct cds_lfht_node *node;
895 struct ltt_kernel_channel *chan;
896
897 lttng::urcu::read_lock_guard read_lock;
898
899 cds_lfht_first(session->kernel_session->consumer->socks->ht, &iter.iter);
900 node = cds_lfht_iter_get_node(&iter.iter);
901 socket = caa_container_of(node, typeof(*socket), node.node);
902
903 cds_list_for_each_entry (chan, &session->kernel_session->channel_list.head, list) {
904 int open_ret;
905
906 DBG("Open packet of kernel channel: channel key = %" PRIu64
907 ", session name = %s, session_id = %" PRIu64,
908 chan->key,
909 session->name,
910 session->id);
911
912 open_ret = consumer_open_channel_packets(socket, chan->key);
913 if (open_ret < 0) {
914 /* General error (no known error expected). */
915 ret = LTTNG_ERR_UNK;
916 goto end;
917 }
918 }
919
920 end:
921 return ret;
922 }
923
924 enum lttng_error_code session_open_packets(struct ltt_session *session)
925 {
926 enum lttng_error_code ret = LTTNG_OK;
927
928 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64,
929 session->name,
930 session->id);
931
932 if (session->ust_session) {
933 ret = ust_app_open_packets(session);
934 if (ret != LTTNG_OK) {
935 goto end;
936 }
937 }
938
939 if (session->kernel_session) {
940 ret = session_kernel_open_packets(session);
941 if (ret != LTTNG_OK) {
942 goto end;
943 }
944 }
945
946 end:
947 return ret;
948 }
949
950 /*
951 * Set a session's current trace chunk.
952 *
953 * Must be called with the session lock held.
954 */
955 int session_set_trace_chunk(struct ltt_session *session,
956 struct lttng_trace_chunk *new_trace_chunk,
957 struct lttng_trace_chunk **current_trace_chunk)
958 {
959 ASSERT_LOCKED(session->_lock);
960 return _session_set_trace_chunk_no_lock_check(
961 session, new_trace_chunk, current_trace_chunk);
962 }
963
964 static void session_notify_destruction(const struct ltt_session *session)
965 {
966 size_t i;
967 const size_t count = lttng_dynamic_array_get_count(&session->destroy_notifiers);
968
969 for (i = 0; i < count; i++) {
970 const struct ltt_session_destroy_notifier_element *element =
971 (ltt_session_destroy_notifier_element *) lttng_dynamic_array_get_element(
972 &session->destroy_notifiers, i);
973
974 element->notifier(session, element->user_data);
975 }
976 }
977
978 /*
979 * Fire each clear notifier once, and remove them from the array.
980 */
981 void session_notify_clear(ltt_session& session)
982 {
983 size_t i;
984 const size_t count = lttng_dynamic_array_get_count(&session.clear_notifiers);
985
986 for (i = 0; i < count; i++) {
987 const struct ltt_session_clear_notifier_element *element =
988 (ltt_session_clear_notifier_element *) lttng_dynamic_array_get_element(
989 &session.clear_notifiers, i);
990
991 element->notifier(&session, element->user_data);
992 }
993 lttng_dynamic_array_clear(&session.clear_notifiers);
994 }
995
996 static void session_release(struct urcu_ref *ref)
997 {
998 int ret;
999 struct ltt_ust_session *usess;
1000 struct ltt_kernel_session *ksess;
1001 struct ltt_session *session = lttng::utils::container_of(ref, &ltt_session::ref_count);
1002 const bool session_published = session->published;
1003
1004 LTTNG_ASSERT(!session->chunk_being_archived);
1005
1006 usess = session->ust_session;
1007 ksess = session->kernel_session;
1008
1009 /* Clean kernel session teardown, keeping data for destroy notifier. */
1010 kernel_destroy_session(ksess);
1011
1012 /* UST session teardown, keeping data for destroy notifier. */
1013 if (usess) {
1014 /* Close any relayd session */
1015 consumer_output_send_destroy_relayd(usess->consumer);
1016
1017 /* Destroy every UST application related to this session. */
1018 ret = ust_app_destroy_trace_all(usess);
1019 if (ret) {
1020 ERR("Error in ust_app_destroy_trace_all");
1021 }
1022
1023 /* Clean up the rest, keeping destroy notifier data. */
1024 trace_ust_destroy_session(usess);
1025 }
1026
1027 /*
1028 * Must notify the kernel thread here to update it's poll set in order to
1029 * remove the channel(s)' fd just destroyed.
1030 */
1031 ret = notify_thread_pipe(the_kernel_poll_pipe[1]);
1032 if (ret < 0) {
1033 PERROR("write kernel poll pipe");
1034 }
1035
1036 DBG("Destroying session %s (id %" PRIu64 ")", session->name, session->id);
1037
1038 snapshot_destroy(&session->snapshot);
1039
1040 pthread_mutex_destroy(&session->_lock);
1041
1042 if (session_published) {
1043 ASSERT_SESSION_LIST_LOCKED();
1044 del_session_list(session);
1045 del_session_ht(session);
1046 }
1047 session_notify_destruction(session);
1048
1049 consumer_output_put(session->consumer);
1050 kernel_free_session(ksess);
1051 session->kernel_session = nullptr;
1052 if (usess) {
1053 trace_ust_free_session(usess);
1054 session->ust_session = nullptr;
1055 }
1056 lttng_dynamic_array_reset(&session->destroy_notifiers);
1057 lttng_dynamic_array_reset(&session->clear_notifiers);
1058 free(session->last_archived_chunk_name);
1059 free(session->base_path);
1060 lttng_trigger_put(session->rotate_trigger);
1061 free(session);
1062 if (session_published) {
1063 /*
1064 * Notify after free-ing to ensure the memory is
1065 * reclaimed before the main thread exits (and prevent memory leak
1066 * reports).
1067 */
1068 ASSERT_SESSION_LIST_LOCKED();
1069 the_session_list.removal_cond.notify_all();
1070 }
1071 }
1072
1073 /*
1074 * Acquire a reference to a session.
1075 * This function may fail (return false); its return value must be checked.
1076 */
1077 bool session_get(struct ltt_session *session)
1078 {
1079 return urcu_ref_get_unless_zero(&session->ref_count);
1080 }
1081
1082 /*
1083 * Release a reference to a session.
1084 */
1085 void session_put(struct ltt_session *session)
1086 {
1087 if (!session) {
1088 return;
1089 }
1090 /*
1091 * The session list lock must be held as any session_put()
1092 * may cause the removal of the session from the session_list.
1093 */
1094 ASSERT_SESSION_LIST_LOCKED();
1095 LTTNG_ASSERT(session->ref_count.refcount);
1096 urcu_ref_put(&session->ref_count, session_release);
1097 }
1098
1099 /*
1100 * Destroy a session.
1101 *
1102 * This method does not immediately release/free the session as other
1103 * components may still hold a reference to the session. However,
1104 * the session should no longer be presented to the user.
1105 *
1106 * Releases the session list's reference to the session
1107 * and marks it as destroyed. Iterations on the session list should be
1108 * mindful of the "destroyed" flag.
1109 */
1110 void session_destroy(struct ltt_session *session)
1111 {
1112 int ret;
1113 struct lttng_ht_iter iter;
1114
1115 LTTNG_ASSERT(!session->destroyed);
1116 session->destroyed = true;
1117
1118 /*
1119 * Remove immediately from the "session by name" hash table. Only one
1120 * session is expected to exist with a given name for at any given time.
1121 *
1122 * Even if a session still technically exists for a little while longer,
1123 * there is no point in performing action on a "destroyed" session.
1124 */
1125 iter.iter.node = &session->node_by_name.node;
1126 ret = lttng_ht_del(ltt_sessions_ht_by_name, &iter);
1127 LTTNG_ASSERT(!ret);
1128
1129 session_put(session);
1130 }
1131
1132 int session_add_destroy_notifier(struct ltt_session *session,
1133 ltt_session_destroy_notifier notifier,
1134 void *user_data)
1135 {
1136 const struct ltt_session_destroy_notifier_element element = { .notifier = notifier,
1137 .user_data = user_data };
1138
1139 return lttng_dynamic_array_add_element(&session->destroy_notifiers, &element);
1140 }
1141
1142 int session_add_clear_notifier(struct ltt_session *session,
1143 ltt_session_clear_notifier notifier,
1144 void *user_data)
1145 {
1146 const struct ltt_session_clear_notifier_element element = { .notifier = notifier,
1147 .user_data = user_data };
1148
1149 return lttng_dynamic_array_add_element(&session->clear_notifiers, &element);
1150 }
1151
1152 /*
1153 * Create a new session and add it to the session list.
1154 * Session list lock must be held by the caller.
1155 */
1156 enum lttng_error_code
1157 session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session)
1158 {
1159 int ret;
1160 enum lttng_error_code ret_code;
1161 struct ltt_session *new_session = nullptr;
1162
1163 ASSERT_SESSION_LIST_LOCKED();
1164 if (name) {
1165 struct ltt_session *clashing_session;
1166
1167 clashing_session = session_find_by_name(name);
1168 if (clashing_session) {
1169 session_put(clashing_session);
1170 ret_code = LTTNG_ERR_EXIST_SESS;
1171 goto error;
1172 }
1173 }
1174 new_session = zmalloc<ltt_session>();
1175 if (!new_session) {
1176 PERROR("Failed to allocate an ltt_session structure");
1177 ret_code = LTTNG_ERR_NOMEM;
1178 goto error;
1179 }
1180
1181 lttng_dynamic_array_init(&new_session->destroy_notifiers,
1182 sizeof(struct ltt_session_destroy_notifier_element),
1183 nullptr);
1184 lttng_dynamic_array_init(&new_session->clear_notifiers,
1185 sizeof(struct ltt_session_clear_notifier_element),
1186 nullptr);
1187 urcu_ref_init(&new_session->ref_count);
1188 pthread_mutex_init(&new_session->_lock, nullptr);
1189
1190 new_session->creation_time = time(nullptr);
1191 if (new_session->creation_time == (time_t) -1) {
1192 PERROR("Failed to sample session creation time");
1193 ret_code = LTTNG_ERR_SESSION_FAIL;
1194 goto error;
1195 }
1196
1197 /* Create default consumer output. */
1198 new_session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1199 if (new_session->consumer == nullptr) {
1200 ret_code = LTTNG_ERR_NOMEM;
1201 goto error;
1202 }
1203
1204 if (name) {
1205 ret = lttng_strncpy(new_session->name, name, sizeof(new_session->name));
1206 if (ret) {
1207 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1208 goto error;
1209 }
1210 ret = validate_name(name);
1211 if (ret < 0) {
1212 ret_code = LTTNG_ERR_SESSION_INVALID_CHAR;
1213 goto error;
1214 }
1215 } else {
1216 int i = 0;
1217 bool found_name = false;
1218 char datetime[16];
1219 struct tm *timeinfo;
1220
1221 timeinfo = localtime(&new_session->creation_time);
1222 if (!timeinfo) {
1223 ret_code = LTTNG_ERR_SESSION_FAIL;
1224 goto error;
1225 }
1226 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1227 for (i = 0; i < INT_MAX; i++) {
1228 struct ltt_session *clashing_session;
1229
1230 if (i == 0) {
1231 ret = snprintf(new_session->name,
1232 sizeof(new_session->name),
1233 "%s-%s",
1234 DEFAULT_SESSION_NAME,
1235 datetime);
1236 } else {
1237 ret = snprintf(new_session->name,
1238 sizeof(new_session->name),
1239 "%s%d-%s",
1240 DEFAULT_SESSION_NAME,
1241 i,
1242 datetime);
1243 }
1244 new_session->name_contains_creation_time = true;
1245 if (ret == -1 || ret >= sizeof(new_session->name)) {
1246 /*
1247 * Null-terminate in case the name is used
1248 * in logging statements.
1249 */
1250 new_session->name[sizeof(new_session->name) - 1] = '\0';
1251 ret_code = LTTNG_ERR_SESSION_FAIL;
1252 goto error;
1253 }
1254
1255 clashing_session = session_find_by_name(new_session->name);
1256 session_put(clashing_session);
1257 if (!clashing_session) {
1258 found_name = true;
1259 break;
1260 }
1261 }
1262 if (found_name) {
1263 DBG("Generated session name \"%s\"", new_session->name);
1264 new_session->has_auto_generated_name = true;
1265 } else {
1266 ERR("Failed to auto-generate a session name");
1267 ret_code = LTTNG_ERR_SESSION_FAIL;
1268 goto error;
1269 }
1270 }
1271
1272 ret = gethostname(new_session->hostname, sizeof(new_session->hostname));
1273 if (ret < 0) {
1274 if (errno == ENAMETOOLONG) {
1275 new_session->hostname[sizeof(new_session->hostname) - 1] = '\0';
1276 ERR("Hostname exceeds the maximal permitted length and has been truncated to %s",
1277 new_session->hostname);
1278 } else {
1279 ret_code = LTTNG_ERR_SESSION_FAIL;
1280 goto error;
1281 }
1282 }
1283
1284 new_session->uid = uid;
1285 new_session->gid = gid;
1286
1287 ret = snapshot_init(&new_session->snapshot);
1288 if (ret < 0) {
1289 ret_code = LTTNG_ERR_NOMEM;
1290 goto error;
1291 }
1292
1293 new_session->rotation_state = LTTNG_ROTATION_STATE_NO_ROTATION;
1294
1295 /* Add new session to the session list. */
1296 new_session->id = add_session_list(new_session);
1297
1298 /*
1299 * Add the new session to the ltt_sessions_ht_by_id.
1300 * No ownership is taken by the hash table; it is merely
1301 * a wrapper around the session list used for faster access
1302 * by session id.
1303 */
1304 add_session_ht(new_session);
1305 new_session->published = true;
1306
1307 /*
1308 * Consumer is left to NULL since the create_session_uri command will
1309 * set it up and, if valid, assign it to the session.
1310 */
1311 DBG("Tracing session %s created with ID %" PRIu64 " by uid = %d, gid = %d",
1312 new_session->name,
1313 new_session->id,
1314 new_session->uid,
1315 new_session->gid);
1316 ret_code = LTTNG_OK;
1317 end:
1318 if (new_session) {
1319 (void) session_get(new_session);
1320 *out_session = new_session;
1321 }
1322 return ret_code;
1323 error:
1324 session_put(new_session);
1325 new_session = nullptr;
1326 goto end;
1327 }
1328
1329 /*
1330 * Check if the UID matches the session. Root user has access to all
1331 * sessions.
1332 */
1333 bool session_access_ok(struct ltt_session *session, uid_t uid)
1334 {
1335 LTTNG_ASSERT(session);
1336 return (uid == session->uid) || uid == 0;
1337 }
1338
1339 /*
1340 * Set a session's rotation state and reset all associated state.
1341 *
1342 * This function resets the rotation state (check timers, pending
1343 * flags, etc.) and sets the result of the last rotation. The result
1344 * can be queries by a liblttng-ctl client.
1345 *
1346 * Be careful of the result passed to this function. For instance,
1347 * on failure to launch a rotation, a client will expect the rotation
1348 * state to be set to "NO_ROTATION". If an error occurred while the
1349 * rotation was "ONGOING", result should be set to "ERROR", which will
1350 * allow a client to report it.
1351 *
1352 * Must be called with the session and session_list locks held.
1353 */
1354 int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result)
1355 {
1356 int ret = 0;
1357
1358 ASSERT_SESSION_LIST_LOCKED();
1359 ASSERT_LOCKED(session._lock);
1360
1361 session.rotation_state = result;
1362 if (session.rotation_pending_check_timer_enabled) {
1363 ret = timer_session_rotation_pending_check_stop(session);
1364 }
1365 if (session.chunk_being_archived) {
1366 uint64_t chunk_id;
1367 enum lttng_trace_chunk_status chunk_status;
1368
1369 chunk_status = lttng_trace_chunk_get_id(session.chunk_being_archived, &chunk_id);
1370 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
1371 LTTNG_OPTIONAL_SET(&session.last_archived_chunk_id, chunk_id);
1372 lttng_trace_chunk_put(session.chunk_being_archived);
1373 session.chunk_being_archived = nullptr;
1374 /*
1375 * Fire the clear reply notifiers if we are completing a clear
1376 * rotation.
1377 */
1378 session_notify_clear(session);
1379 }
1380 return ret;
1381 }
1382
1383 /*
1384 * Sample the id of a session looked up via its name.
1385 * Here the term "sampling" hint the caller that this return the id at a given
1386 * point in time with no guarantee that the session for which the id was
1387 * sampled still exist at that point.
1388 *
1389 * Return 0 when the session is not found,
1390 * Return 1 when the session is found and set `id`.
1391 */
1392 bool sample_session_id_by_name(const char *name, uint64_t *id)
1393 {
1394 bool found = false;
1395 struct lttng_ht_node_str *node;
1396 struct lttng_ht_iter iter;
1397 struct ltt_session *ls;
1398
1399 lttng::urcu::read_lock_guard read_lock;
1400
1401 if (!ltt_sessions_ht_by_name) {
1402 found = false;
1403 goto end;
1404 }
1405
1406 lttng_ht_lookup(ltt_sessions_ht_by_name, name, &iter);
1407 node = lttng_ht_iter_get_node_str(&iter);
1408 if (node == nullptr) {
1409 found = false;
1410 goto end;
1411 }
1412
1413 ls = lttng::utils::container_of(node, &ltt_session::node_by_name);
1414 *id = ls->id;
1415 found = true;
1416
1417 DBG3("Session id `%" PRIu64 "` sampled for session `%s", *id, name);
1418 end:
1419 return found;
1420 }
1421
1422 void ltt_session::_locked_session_release(ltt_session *session)
1423 {
1424 if (!session) {
1425 return;
1426 }
1427
1428 session_unlock(session);
1429 session_put(session);
1430 }
1431
1432 void ltt_session::_locked_const_session_release(const ltt_session *session)
1433 {
1434 if (!session) {
1435 return;
1436 }
1437
1438 ltt_session::_const_session_unlock(*session);
1439 ltt_session::_const_session_put(session);
1440 }
1441
1442 ltt_session::locked_ref ltt_session::find_locked_session(ltt_session::id_t id)
1443 {
1444 lttng::urcu::read_lock_guard rcu_lock;
1445 auto session = session_find_by_id(id);
1446
1447 if (!session) {
1448 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1449 }
1450
1451 /*
1452 * The pointer falling out of scope will unlock and release the reference to the
1453 * session.
1454 */
1455 session_lock(session);
1456 return ltt_session::locked_ref(session);
1457 }
1458
1459 ltt_session::locked_ref ltt_session::find_locked_session(lttng::c_string_view name)
1460 {
1461 lttng::urcu::read_lock_guard rcu_lock;
1462 auto session = session_find_by_name(name.data());
1463
1464 if (!session) {
1465 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1466 }
1467
1468 session_lock(session);
1469 return ltt_session::locked_ref(session);
1470 }
1471
1472 ltt_session::const_locked_ref ltt_session::find_locked_const_session(ltt_session::id_t id)
1473 {
1474 lttng::urcu::read_lock_guard rcu_lock;
1475 auto session = session_find_by_id(id);
1476
1477 if (!session) {
1478 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1479 }
1480
1481 session_lock(session);
1482 return ltt_session::const_locked_ref(session);
1483 }
1484
1485 ltt_session::const_locked_ref ltt_session::find_locked_const_session(lttng::c_string_view name)
1486 {
1487 lttng::urcu::read_lock_guard rcu_lock;
1488 auto session = session_find_by_name(name.data());
1489
1490 if (!session) {
1491 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1492 }
1493
1494 session_lock(session);
1495 return ltt_session::const_locked_ref(session);
1496 }
1497
1498 ltt_session::ref ltt_session::find_session(ltt_session::id_t id)
1499 {
1500 lttng::urcu::read_lock_guard rcu_lock;
1501 auto session = session_find_by_id(id);
1502
1503 if (!session) {
1504 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1505 }
1506
1507 return ltt_session::ref(session);
1508 }
1509
1510 ltt_session::ref ltt_session::find_session(lttng::c_string_view name)
1511 {
1512 lttng::urcu::read_lock_guard rcu_lock;
1513 auto session = session_find_by_name(name.data());
1514
1515 if (!session) {
1516 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1517 }
1518
1519 return ltt_session::ref(session);
1520 }
1521
1522 ltt_session::const_ref ltt_session::find_const_session(ltt_session::id_t id)
1523 {
1524 lttng::urcu::read_lock_guard rcu_lock;
1525 auto session = session_find_by_id(id);
1526
1527 if (!session) {
1528 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id);
1529 }
1530
1531 return ltt_session::const_ref(session);
1532 }
1533
1534 ltt_session::const_ref ltt_session::find_const_session(lttng::c_string_view name)
1535 {
1536 lttng::urcu::read_lock_guard rcu_lock;
1537 auto session = session_find_by_name(name.data());
1538
1539 if (!session) {
1540 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name.data());
1541 }
1542
1543 return ltt_session::const_ref(session);
1544 }
1545
1546 void ltt_session::_const_session_put(const ltt_session *session)
1547 {
1548 /*
1549 * The session list lock must be held as any session_put()
1550 * may cause the removal of the session from the session_list.
1551 */
1552 ASSERT_SESSION_LIST_LOCKED();
1553 LTTNG_ASSERT(session->ref_count.refcount);
1554 urcu_ref_put(&session->ref_count, session_release);
1555 }
1556
1557 std::unique_lock<std::mutex> ls::lock_session_list()
1558 {
1559 return std::unique_lock<std::mutex>(the_session_list.lock);
1560 }
This page took 0.059273 seconds and 3 git commands to generate.