2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "lttng-sessiond.hpp"
12 #include "session.hpp"
14 #include "trace-ust.hpp"
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>
23 #include <lttng/location-internal.hpp>
33 #include <sys/types.h>
37 struct ltt_session_destroy_notifier_element
{
38 ltt_session_destroy_notifier notifier
;
42 struct ltt_session_clear_notifier_element
{
43 ltt_session_clear_notifier notifier
;
47 namespace ls
= lttng::sessiond
;
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().
58 /* These characters are forbidden in a session name. Used by validate_name. */
59 const char *forbidden_name_chars
= "/";
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;
67 * Init tracing session list.
69 * Please see session.h for more explanation and correct usage of the list.
71 struct ltt_session_list the_session_list
;
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.
79 struct ltt_session
*session_find_by_name(const char *name
)
81 struct ltt_session
*iter
;
84 ASSERT_SESSION_LIST_LOCKED();
86 DBG2("Trying to find session by name %s", name
);
88 cds_list_for_each_entry (iter
, &the_session_list
.head
, list
) {
89 if (!strncmp(iter
->name
, name
, NAME_MAX
) && !iter
->destroyed
) {
96 return session_get(iter
) ? iter
: nullptr;
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).
104 struct ltt_session
*session_find_by_id(uint64_t id
)
106 struct lttng_ht_node_u64
*node
;
107 struct lttng_ht_iter iter
;
108 struct ltt_session
*ls
;
110 ASSERT_RCU_READ_LOCKED();
111 ASSERT_SESSION_LIST_LOCKED();
113 if (!ltt_sessions_ht_by_id
) {
117 lttng_ht_lookup(ltt_sessions_ht_by_id
, &id
, &iter
);
118 node
= lttng_ht_iter_get_node_u64(&iter
);
119 if (node
== nullptr) {
122 ls
= lttng::utils::container_of(node
, <t_session::node
);
124 DBG3("Session %" PRIu64
" found by id.", id
);
125 return session_get(ls
) ? ls
: nullptr;
128 DBG3("Session %" PRIu64
" NOT found by id", id
);
134 * Validate the session name for forbidden characters.
136 * Return 0 on success else -1 meaning a forbidden char. has been found.
138 static int validate_name(const char *name
)
141 char *tok
, *tmp_name
;
145 tmp_name
= strdup(name
);
152 tok
= strpbrk(tmp_name
, forbidden_name_chars
);
154 DBG("Session name %s contains a forbidden character", name
);
155 /* Forbidden character has been found. */
167 * Add a ltt_session structure to the global list.
169 * The caller MUST acquire the session list lock before.
170 * Returns the unique identifier for the session.
172 static uint64_t add_session_list(struct ltt_session
*ls
)
176 cds_list_add(&ls
->list
, &the_session_list
.head
);
177 return the_session_list
.next_uuid
++;
181 * Delete a ltt_session structure to the global list.
183 * The caller MUST acquire the session list lock before.
185 static void del_session_list(struct ltt_session
*ls
)
189 cds_list_del(&ls
->list
);
193 * Return a pointer to the session list.
195 struct ltt_session_list
*session_get_list()
197 return &the_session_list
;
201 * Returns once the session list is empty.
203 void session_list_wait_empty(std::unique_lock
<std::mutex
> list_lock
)
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
); });
211 * Try to acquire session list lock
213 int session_trylock_list() noexcept
215 /* Return 0 if successfully acquired. */
216 return the_session_list
.lock
.try_lock() ? 0 : 1;
220 * Get the session's consumer destination type.
222 enum consumer_dst_type
session_get_consumer_destination_type(const ltt_session::locked_ref
& session
)
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.
229 return session
->kernel_session
? session
->kernel_session
->consumer
->type
:
230 session
->ust_session
->consumer
->type
;
234 * Get the session's consumer network hostname.
235 * The caller must ensure that the destination is of type "net".
237 const char *session_get_net_consumer_hostname(const ltt_session::locked_ref
& session
)
239 const char *hostname
= nullptr;
240 const struct consumer_output
*output
;
242 output
= session
->kernel_session
? session
->kernel_session
->consumer
:
243 session
->ust_session
->consumer
;
246 * hostname is assumed to be the same for both control and data
249 switch (output
->dst
.net
.control
.dtype
) {
251 hostname
= output
->dst
.net
.control
.dst
.ipv4
;
254 hostname
= output
->dst
.net
.control
.dst
.ipv6
;
263 * Get the session's consumer network control and data ports.
264 * The caller must ensure that the destination is of type "net".
266 void session_get_net_consumer_ports(const ltt_session::locked_ref
& session
,
267 uint16_t *control_port
,
270 const struct consumer_output
*output
;
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
;
279 * Get the location of the latest trace archive produced by a rotation.
281 struct lttng_trace_archive_location
*
282 session_get_trace_archive_location(const ltt_session::locked_ref
& session
)
285 struct lttng_trace_archive_location
*location
= nullptr;
286 char *chunk_path
= nullptr;
288 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
||
289 !session
->last_archived_chunk_name
) {
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
);
302 location
= lttng_trace_archive_location_local_create(chunk_path
);
304 case CONSUMER_DST_NET
:
306 const char *hostname
;
307 uint16_t control_port
, data_port
;
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(
313 LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
,
316 session
->last_chunk_path
);
328 * Allocate the ltt_sessions_ht_by_id and ltt_sessions_ht_by_name HT.
330 * The session list lock must be held.
332 static int ltt_sessions_ht_alloc()
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
) {
340 ERR("Failed to allocate ltt_sessions_ht_by_id");
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
) {
348 ERR("Failed to allocate ltt_sessions_ht_by_name");
357 * Destroy the ltt_sessions_ht_by_id HT.
359 * The session list lock must be held.
361 static void ltt_sessions_ht_destroy()
363 if (ltt_sessions_ht_by_id
) {
364 lttng_ht_destroy(ltt_sessions_ht_by_id
);
365 ltt_sessions_ht_by_id
= nullptr;
368 if (ltt_sessions_ht_by_name
) {
369 lttng_ht_destroy(ltt_sessions_ht_by_name
);
370 ltt_sessions_ht_by_name
= nullptr;
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.
381 static void add_session_ht(struct ltt_session
*ls
)
387 if (!ltt_sessions_ht_by_id
) {
388 ret
= ltt_sessions_ht_alloc();
390 ERR("Error allocating the sessions HT");
395 /* Should always be present with ltt_sessions_ht_by_id. */
396 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
398 lttng_ht_node_init_u64(&ls
->node
, ls
->id
);
399 lttng_ht_add_unique_u64(ltt_sessions_ht_by_id
, &ls
->node
);
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
);
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.
413 static bool ltt_sessions_ht_empty()
417 if (!ltt_sessions_ht_by_id
) {
418 /* The hash tables do not exist yet. */
422 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
424 if (lttng_ht_get_count(ltt_sessions_ht_by_id
) != 0) {
430 * At this point it is expected that the `ltt_sessions_ht_by_name` ht is
433 * The removal from both hash tables is done in two different
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.
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
445 LTTNG_ASSERT(lttng_ht_get_count(ltt_sessions_ht_by_name
) == 0);
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.
456 static void del_session_ht(struct ltt_session
*ls
)
458 struct lttng_ht_iter iter
;
462 LTTNG_ASSERT(ltt_sessions_ht_by_id
);
463 LTTNG_ASSERT(ltt_sessions_ht_by_name
);
465 iter
.iter
.node
= &ls
->node
.node
;
466 ret
= lttng_ht_del(ltt_sessions_ht_by_id
, &iter
);
469 if (ltt_sessions_ht_empty()) {
470 DBG("Empty ltt_sessions_ht_by_id/name, destroying hast tables");
471 ltt_sessions_ht_destroy();
476 * Acquire session lock
478 void session_lock(struct ltt_session
*session
)
480 LTTNG_ASSERT(session
);
484 void ltt_session::lock() const noexcept
486 pthread_mutex_lock(&_lock
);
489 void ltt_session::unlock() const noexcept
491 ltt_session::_const_session_unlock(*this);
495 * Release session lock
497 void session_unlock(struct ltt_session
*session
)
499 LTTNG_ASSERT(session
);
503 void ltt_session::_const_session_unlock(const ltt_session
& session
)
505 pthread_mutex_unlock(&session
._lock
);
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
)
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
;
518 enum lttng_trace_chunk_status chunk_status
;
520 lttng::urcu::read_lock_guard read_lock
;
522 * Ownership of current trace chunk is transferred to
523 * `current_trace_chunk`.
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;
531 if (session
->kernel_session
) {
532 lttng_trace_chunk_put(session
->kernel_session
->current_trace_chunk
);
533 session
->kernel_session
->current_trace_chunk
= nullptr;
535 if (!new_trace_chunk
) {
539 chunk_status
= lttng_trace_chunk_get_id(new_trace_chunk
, &chunk_id
);
540 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
543 refs_to_acquire
+= !!session
->ust_session
;
544 refs_to_acquire
+= !!session
->kernel_session
;
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\"",
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
==
559 session
->ust_session
->current_trace_chunk
= new_trace_chunk
;
560 if (is_local_trace
) {
561 enum lttng_error_code ret_error_code
;
564 ust_app_create_channel_subdirectories(session
->ust_session
);
565 if (ret_error_code
!= LTTNG_OK
) {
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
,
576 DEFAULT_UST_TRACE_DIR
);
577 pthread_mutex_unlock(socket
->lock
);
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
==
588 session
->kernel_session
->current_trace_chunk
= new_trace_chunk
;
589 if (is_local_trace
) {
590 enum lttng_error_code ret_error_code
;
593 kernel_create_channel_subdirectories(session
->kernel_session
);
594 if (ret_error_code
!= LTTNG_OK
) {
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
,
605 DEFAULT_KERNEL_TRACE_DIR
);
606 pthread_mutex_unlock(socket
->lock
);
614 * Update local current trace chunk state last, only if all remote
615 * creations succeeded.
617 session
->current_trace_chunk
= new_trace_chunk
;
618 LTTNG_OPTIONAL_SET(&session
->most_recent_chunk_id
, chunk_id
);
620 if (_current_trace_chunk
) {
621 *_current_trace_chunk
= current_trace_chunk
;
622 current_trace_chunk
= nullptr;
625 lttng_trace_chunk_put(current_trace_chunk
);
628 if (session
->ust_session
) {
629 session
->ust_session
->current_trace_chunk
= nullptr;
631 if (session
->kernel_session
) {
632 session
->kernel_session
->current_trace_chunk
= nullptr;
635 * Release references taken in the case where all references could not
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
);
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
)
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);
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
),
663 uint64_t next_chunk_id
;
664 const struct consumer_output
*output
;
665 const char *new_path
;
667 if (consumer_output_override
) {
668 output
= consumer_output_override
;
670 LTTNG_ASSERT(session
->ust_session
|| session
->kernel_session
);
671 output
= session
->ust_session
? session
->ust_session
->consumer
:
672 session
->kernel_session
->consumer
;
675 is_local_trace
= output
->type
== CONSUMER_DST_LOCAL
;
676 base_path
= session_base_path_override
?: consumer_output_get_base_path(output
);
678 if (chunk_creation_ts
== (time_t) -1) {
679 PERROR("Failed to sample time while creation session \"%s\" trace chunk",
685 session
->most_recent_chunk_id
.is_set
? session
->most_recent_chunk_id
.value
+ 1 : 0;
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
) {
695 if (!session
->current_trace_chunk
) {
696 if (!session
->rotated
) {
702 new_path
= DEFAULT_CHUNK_TMP_NEW_DIRECTORY
;
705 trace_chunk
= lttng_trace_chunk_create(next_chunk_id
, chunk_creation_ts
, new_path
);
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
) {
717 if (!is_local_trace
) {
719 * No need to set crendentials and output directory
720 * for remote trace chunks.
725 chunk_status
= lttng_trace_chunk_set_credentials(trace_chunk
, &session_credentials
);
726 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
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
);
735 session_output_directory
= lttng_directory_handle_create(base_path
);
736 if (!session_output_directory
) {
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
) {
748 lttng_directory_handle_put(session_output_directory
);
749 lttng_trace_chunk_put(trace_chunk
);
750 trace_chunk
= nullptr;
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
)
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
;
767 chunk_status
= lttng_trace_chunk_set_close_command(trace_chunk
, close_command
);
768 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
773 if (chunk_close_timestamp
== (time_t) -1) {
774 ERR("Failed to sample the close timestamp of the current trace chunk of session \"%s\"",
780 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_DELETE
&& !session
->rotated
) {
781 /* New chunk stays in session output directory. */
784 /* Use chunk name for new chunk. */
787 if (session
->current_trace_chunk
&&
788 !lttng_trace_chunk_get_name_overridden(session
->current_trace_chunk
)) {
789 /* Rename new chunk path. */
791 lttng_trace_chunk_rename_path(session
->current_trace_chunk
, new_path
);
792 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
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
;
801 if (!session
->rotated
) {
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
) {
813 if (close_command
== LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
) {
814 session
->rotated
= true;
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\"",
824 if (session
->ust_session
) {
825 const uint64_t relayd_id
= session
->ust_session
->consumer
->net_seq_index
;
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
,
834 closed_trace_chunk_path
);
835 pthread_mutex_unlock(socket
->lock
);
837 ERR("Failed to close trace chunk on user space consumer");
838 error_occurred
= true;
842 if (session
->kernel_session
) {
843 const uint64_t relayd_id
= session
->kernel_session
->consumer
->net_seq_index
;
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
,
852 closed_trace_chunk_path
);
853 pthread_mutex_unlock(socket
->lock
);
855 ERR("Failed to close trace chunk on kernel consumer");
856 error_occurred
= true;
860 ret
= error_occurred
? -1 : 0;
866 * This function skips the metadata channel as the begin/end timestamps of a
867 * metadata packet are useless.
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.
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.
881 static enum lttng_error_code
session_kernel_open_packets(const ltt_session::locked_ref
& session
)
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
;
889 lttng::urcu::read_lock_guard read_lock
;
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
);
895 cds_list_for_each_entry (chan
, &session
->kernel_session
->channel_list
.head
, list
) {
898 DBG("Open packet of kernel channel: channel key = %" PRIu64
899 ", session name = %s, session_id = %" PRIu64
,
904 open_ret
= consumer_open_channel_packets(socket
, chan
->key
);
906 /* General error (no known error expected). */
916 enum lttng_error_code
session_open_packets(const ltt_session::locked_ref
& session
)
918 enum lttng_error_code ret
= LTTNG_OK
;
920 DBG("Opening packets of session channels: session name = %s, session id = %" PRIu64
,
924 if (session
->ust_session
) {
925 ret
= ust_app_open_packets(session
);
926 if (ret
!= LTTNG_OK
) {
931 if (session
->kernel_session
) {
932 ret
= session_kernel_open_packets(session
);
933 if (ret
!= LTTNG_OK
) {
943 * Set a session's current trace chunk.
945 * Must be called with the session lock held.
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
)
951 ASSERT_LOCKED(session
->_lock
);
952 return _session_set_trace_chunk_no_lock_check(
953 session
, new_trace_chunk
, current_trace_chunk
);
956 static void session_notify_destruction(const ltt_session::locked_ref
& session
)
959 const auto count
= lttng_dynamic_array_get_count(&session
->destroy_notifiers
);
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
);
966 element
->notifier(session
, element
->user_data
);
971 * Fire each clear notifier once, and remove them from the array.
973 void session_notify_clear(const ltt_session::locked_ref
& session
)
976 const auto count
= lttng_dynamic_array_get_count(&session
->clear_notifiers
);
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
);
983 element
->notifier(session
, element
->user_data
);
985 lttng_dynamic_array_clear(&session
->clear_notifiers
);
988 static void session_release(struct urcu_ref
*ref
)
991 struct ltt_ust_session
*usess
;
992 struct ltt_kernel_session
*ksess
;
993 struct ltt_session
*session
= lttng::utils::container_of(ref
, <t_session::ref_count
);
994 const bool session_published
= session
->published
;
996 LTTNG_ASSERT(!session
->chunk_being_archived
);
998 usess
= session
->ust_session
;
999 ksess
= session
->kernel_session
;
1001 /* Clean kernel session teardown, keeping data for destroy notifier. */
1002 kernel_destroy_session(ksess
);
1004 /* UST session teardown, keeping data for destroy notifier. */
1006 /* Close any relayd session */
1007 consumer_output_send_destroy_relayd(usess
->consumer
);
1009 /* Destroy every UST application related to this session. */
1010 ret
= ust_app_destroy_trace_all(usess
);
1012 ERR("Error in ust_app_destroy_trace_all");
1015 /* Clean up the rest, keeping destroy notifier data. */
1016 trace_ust_destroy_session(usess
);
1020 * Must notify the kernel thread here to update it's poll set in order to
1021 * remove the channel(s)' fd just destroyed.
1023 ret
= notify_thread_pipe(the_kernel_poll_pipe
[1]);
1025 PERROR("write kernel poll pipe");
1028 DBG("Destroying session %s (id %" PRIu64
")", session
->name
, session
->id
);
1030 snapshot_destroy(&session
->snapshot
);
1032 if (session_published
) {
1033 ASSERT_SESSION_LIST_LOCKED();
1034 del_session_list(session
);
1035 del_session_ht(session
);
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.
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
);
1051 pthread_mutex_destroy(&session
->_lock
);
1053 consumer_output_put(session
->consumer
);
1054 kernel_free_session(ksess
);
1055 session
->kernel_session
= nullptr;
1057 trace_ust_free_session(usess
);
1058 session
->ust_session
= nullptr;
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
);
1066 if (session_published
) {
1068 * Notify after free-ing to ensure the memory is
1069 * reclaimed before the main thread exits (and prevent memory leak
1072 ASSERT_SESSION_LIST_LOCKED();
1073 the_session_list
.removal_cond
.notify_all();
1078 * Acquire a reference to a session.
1079 * This function may fail (return false); its return value must be checked.
1081 bool session_get(struct ltt_session
*session
)
1083 return urcu_ref_get_unless_zero(&session
->ref_count
);
1087 * Release a reference to a session.
1089 void session_put(struct ltt_session
*session
)
1095 * The session list lock must be held as any session_put()
1096 * may cause the removal of the session from the session_list.
1098 ASSERT_SESSION_LIST_LOCKED();
1099 LTTNG_ASSERT(session
->ref_count
.refcount
);
1100 urcu_ref_put(&session
->ref_count
, session_release
);
1104 * Destroy a session.
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.
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.
1114 void session_destroy(struct ltt_session
*session
)
1117 struct lttng_ht_iter iter
;
1119 LTTNG_ASSERT(!session
->destroyed
);
1120 session
->destroyed
= true;
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.
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.
1129 iter
.iter
.node
= &session
->node_by_name
.node
;
1130 ret
= lttng_ht_del(ltt_sessions_ht_by_name
, &iter
);
1133 session_put(session
);
1136 int session_add_destroy_notifier(const ltt_session::locked_ref
& session
,
1137 ltt_session_destroy_notifier notifier
,
1140 const struct ltt_session_destroy_notifier_element element
= { .notifier
= notifier
,
1141 .user_data
= user_data
};
1143 return lttng_dynamic_array_add_element(&session
->destroy_notifiers
, &element
);
1146 int session_add_clear_notifier(const ltt_session::locked_ref
& session
,
1147 ltt_session_clear_notifier notifier
,
1150 const struct ltt_session_clear_notifier_element element
= { .notifier
= notifier
,
1151 .user_data
= user_data
};
1153 return lttng_dynamic_array_add_element(&session
->clear_notifiers
, &element
);
1157 * Create a new session and add it to the session list.
1158 * Session list lock must be held by the caller.
1160 enum lttng_error_code
1161 session_create(const char *name
, uid_t uid
, gid_t gid
, struct ltt_session
**out_session
)
1164 enum lttng_error_code ret_code
;
1165 struct ltt_session
*new_session
= nullptr;
1167 ASSERT_SESSION_LIST_LOCKED();
1169 struct ltt_session
*clashing_session
;
1171 clashing_session
= session_find_by_name(name
);
1172 if (clashing_session
) {
1173 session_put(clashing_session
);
1174 ret_code
= LTTNG_ERR_EXIST_SESS
;
1178 new_session
= zmalloc
<ltt_session
>();
1180 PERROR("Failed to allocate an ltt_session structure");
1181 ret_code
= LTTNG_ERR_NOMEM
;
1185 lttng_dynamic_array_init(&new_session
->destroy_notifiers
,
1186 sizeof(struct ltt_session_destroy_notifier_element
),
1188 lttng_dynamic_array_init(&new_session
->clear_notifiers
,
1189 sizeof(struct ltt_session_clear_notifier_element
),
1191 urcu_ref_init(&new_session
->ref_count
);
1192 pthread_mutex_init(&new_session
->_lock
, nullptr);
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
;
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
;
1209 ret
= lttng_strncpy(new_session
->name
, name
, sizeof(new_session
->name
));
1211 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1214 ret
= validate_name(name
);
1216 ret_code
= LTTNG_ERR_SESSION_INVALID_CHAR
;
1221 bool found_name
= false;
1223 struct tm
*timeinfo
;
1225 timeinfo
= localtime(&new_session
->creation_time
);
1227 ret_code
= LTTNG_ERR_SESSION_FAIL
;
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
;
1235 ret
= snprintf(new_session
->name
,
1236 sizeof(new_session
->name
),
1238 DEFAULT_SESSION_NAME
,
1241 ret
= snprintf(new_session
->name
,
1242 sizeof(new_session
->name
),
1244 DEFAULT_SESSION_NAME
,
1248 new_session
->name_contains_creation_time
= true;
1249 if (ret
== -1 || ret
>= sizeof(new_session
->name
)) {
1251 * Null-terminate in case the name is used
1252 * in logging statements.
1254 new_session
->name
[sizeof(new_session
->name
) - 1] = '\0';
1255 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1259 clashing_session
= session_find_by_name(new_session
->name
);
1260 session_put(clashing_session
);
1261 if (!clashing_session
) {
1267 DBG("Generated session name \"%s\"", new_session
->name
);
1268 new_session
->has_auto_generated_name
= true;
1270 ERR("Failed to auto-generate a session name");
1271 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1276 ret
= gethostname(new_session
->hostname
, sizeof(new_session
->hostname
));
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
);
1283 ret_code
= LTTNG_ERR_SESSION_FAIL
;
1288 new_session
->uid
= uid
;
1289 new_session
->gid
= gid
;
1291 ret
= snapshot_init(&new_session
->snapshot
);
1293 ret_code
= LTTNG_ERR_NOMEM
;
1297 new_session
->rotation_state
= LTTNG_ROTATION_STATE_NO_ROTATION
;
1299 /* Add new session to the session list. */
1300 new_session
->id
= add_session_list(new_session
);
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
1308 add_session_ht(new_session
);
1309 new_session
->published
= true;
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.
1315 DBG("Tracing session %s created with ID %" PRIu64
" by uid = %d, gid = %d",
1320 ret_code
= LTTNG_OK
;
1323 (void) session_get(new_session
);
1324 *out_session
= new_session
;
1328 session_put(new_session
);
1329 new_session
= nullptr;
1334 * Check if the UID matches the session. Root user has access to all
1337 bool session_access_ok(const ltt_session::locked_ref
& session
, uid_t uid
)
1339 return (uid
== session
->uid
) || uid
== 0;
1343 * Set a session's rotation state and reset all associated state.
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.
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.
1355 * Must be called with the session and session_list locks held.
1357 int session_reset_rotation_state(const ltt_session::locked_ref
& session
,
1358 enum lttng_rotation_state result
)
1362 ASSERT_SESSION_LIST_LOCKED();
1364 session
->rotation_state
= result
;
1365 if (session
->rotation_pending_check_timer_enabled
) {
1366 ret
= timer_session_rotation_pending_check_stop(session
);
1368 if (session
->chunk_being_archived
) {
1370 enum lttng_trace_chunk_status chunk_status
;
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;
1378 * Fire the clear reply notifiers if we are completing a clear
1381 session_notify_clear(session
);
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.
1392 * Return 0 when the session is not found,
1393 * Return 1 when the session is found and set `id`.
1395 bool sample_session_id_by_name(const char *name
, uint64_t *id
)
1398 struct lttng_ht_node_str
*node
;
1399 struct lttng_ht_iter iter
;
1400 struct ltt_session
*ls
;
1402 lttng::urcu::read_lock_guard read_lock
;
1404 if (!ltt_sessions_ht_by_name
) {
1409 lttng_ht_lookup(ltt_sessions_ht_by_name
, name
, &iter
);
1410 node
= lttng_ht_iter_get_node_str(&iter
);
1411 if (node
== nullptr) {
1416 ls
= lttng::utils::container_of(node
, <t_session::node_by_name
);
1420 DBG3("Session id `%" PRIu64
"` sampled for session `%s", *id
, name
);
1425 void ltt_session::_locked_session_release(ltt_session
*session
)
1431 session_unlock(session
);
1432 session_put(session
);
1435 void ltt_session::_locked_const_session_release(const ltt_session
*session
)
1441 ltt_session::_const_session_unlock(*session
);
1442 ltt_session::_const_session_put(session
);
1445 ltt_session::locked_ref
ltt_session::find_locked_session(ltt_session::id_t id
)
1447 lttng::urcu::read_lock_guard rcu_lock
;
1448 auto session
= session_find_by_id(id
);
1451 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1455 * The pointer falling out of scope will unlock and release the reference to the
1458 session_lock(session
);
1459 return ltt_session::locked_ref(*session
);
1462 ltt_session::locked_ref
ltt_session::find_locked_session(lttng::c_string_view name
)
1464 lttng::urcu::read_lock_guard rcu_lock
;
1465 auto session
= session_find_by_name(name
.data());
1468 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1471 session_lock(session
);
1472 return ltt_session::locked_ref(*session
);
1475 ltt_session::const_locked_ref
ltt_session::find_locked_const_session(ltt_session::id_t id
)
1477 lttng::urcu::read_lock_guard rcu_lock
;
1478 auto session
= session_find_by_id(id
);
1481 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1484 session_lock(session
);
1485 return ltt_session::const_locked_ref(*session
);
1488 ltt_session::const_locked_ref
ltt_session::find_locked_const_session(lttng::c_string_view name
)
1490 lttng::urcu::read_lock_guard rcu_lock
;
1491 auto session
= session_find_by_name(name
.data());
1494 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1497 session_lock(session
);
1498 return ltt_session::const_locked_ref(*session
);
1501 ltt_session::ref
ltt_session::find_session(ltt_session::id_t id
)
1503 lttng::urcu::read_lock_guard rcu_lock
;
1504 auto session
= session_find_by_id(id
);
1507 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1510 return ltt_session::ref(*session
);
1513 ltt_session::ref
ltt_session::find_session(lttng::c_string_view name
)
1515 lttng::urcu::read_lock_guard rcu_lock
;
1516 auto session
= session_find_by_name(name
.data());
1519 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1522 return ltt_session::ref(*session
);
1525 ltt_session::const_ref
ltt_session::find_const_session(ltt_session::id_t id
)
1527 lttng::urcu::read_lock_guard rcu_lock
;
1528 auto session
= session_find_by_id(id
);
1531 LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id
);
1534 return ltt_session::const_ref(*session
);
1537 ltt_session::const_ref
ltt_session::find_const_session(lttng::c_string_view name
)
1539 lttng::urcu::read_lock_guard rcu_lock
;
1540 auto session
= session_find_by_name(name
.data());
1543 LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(name
.data());
1546 return ltt_session::const_ref(*session
);
1549 void ltt_session::_const_session_put(const ltt_session
*session
)
1552 * The session list lock must be held as any session_put()
1553 * may cause the removal of the session from the session_list.
1555 ASSERT_SESSION_LIST_LOCKED();
1556 LTTNG_ASSERT(session
->ref_count
.refcount
);
1557 urcu_ref_put(&session
->ref_count
, session_release
);
1560 std::unique_lock
<std::mutex
> ls::lock_session_list()
1562 return std::unique_lock
<std::mutex
>(the_session_list
.lock
);