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