-ust_registry_session::ust_registry_session(uint32_t bits_per_long,
- uint32_t uint8_t_alignment,
- uint32_t uint16_t_alignment,
- uint32_t uint32_t_alignment,
- uint32_t uint64_t_alignment,
- uint32_t long_alignment,
- int byte_order,
- uint32_t major,
- uint32_t minor,
- const char *root_shm_path,
- const char *shm_path,
- uid_t euid,
- gid_t egid,
- uint64_t tracing_id) :
- _bits_per_long{bits_per_long},
- _uint8_t_alignment{uint8_t_alignment},
- _uint16_t_alignment{uint16_t_alignment},
- _uint32_t_alignment{uint32_t_alignment},
- _uint64_t_alignment{uint64_t_alignment},
- _long_alignment{long_alignment},
- _byte_order{byte_order},
- _uid{euid},
- _gid{egid},
- _app_tracer_version_major{major},
- _app_tracer_version_minor{minor},
- _tracing_id{tracing_id}
-{
- pthread_mutex_init(&_lock, NULL);
- strncpy(_root_shm_path, root_shm_path, sizeof(_root_shm_path));
- _root_shm_path[sizeof(_root_shm_path) - 1] = '\0';
- if (shm_path[0]) {
- strncpy(_shm_path, shm_path, sizeof(_shm_path));
- _shm_path[sizeof(_shm_path) - 1] = '\0';
- strncpy(_metadata_path, shm_path, sizeof(_metadata_path));
- _metadata_path[sizeof(_metadata_path) - 1] = '\0';
- strncat(_metadata_path, "/metadata",
- sizeof(_metadata_path) - strlen(_metadata_path) - 1);
- }
-
- if (_shm_path[0]) {
- if (run_as_mkdir_recursive(_shm_path, S_IRWXU | S_IRWXG, euid, egid)) {
+namespace ls = lttng::sessiond;
+namespace lst = lttng::sessiond::trace;
+namespace lsu = lttng::sessiond::ust;
+
+namespace {
+lttng_uuid generate_uuid_or_throw()
+{
+ lttng_uuid new_uuid;
+
+ if (lttng_uuid_generate(new_uuid)) {
+ LTTNG_THROW_POSIX("Failed to generate UST uuid", errno);
+ }
+
+ return new_uuid;
+}
+
+int get_count_order(unsigned int count)
+{
+ int order;
+
+ order = lttng_fls(count) - 1;
+ if (count & (count - 1)) {
+ order++;
+ }
+
+ LTTNG_ASSERT(order >= 0);
+ return order;
+}
+
+void clear_metadata_file(int fd)
+{
+ const auto lseek_ret = lseek(fd, 0, SEEK_SET);
+ if (lseek_ret < 0) {
+ LTTNG_THROW_POSIX(
+ "Failed to seek to the beginning of the metadata file while clearing it",
+ errno);
+ }
+
+ const auto ret = ftruncate(fd, 0);
+ if (ret < 0) {
+ LTTNG_THROW_POSIX("Failed to truncate the metadata file while clearing it", errno);
+ }
+}
+
+/*
+ * Validate that the id has reached the maximum allowed or not.
+ */
+bool is_max_channel_id(uint32_t id)
+{
+ return id == UINT32_MAX;
+}
+
+void destroy_channel_rcu(struct rcu_head *head)
+{
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ lsu::registry_channel *chan =
+ lttng::utils::container_of(head, &lsu::registry_channel::_rcu_head);
+ DIAGNOSTIC_POP
+
+ delete chan;
+}
+
+/*
+ * Destroy every element of the registry and free the memory. This does NOT
+ * free the registry pointer since it might not have been allocated before so
+ * it's the caller responsability.
+ *
+ * Called from ~registry_session(), must not throw.
+ */
+void destroy_channel(lsu::registry_channel *chan, bool notify) noexcept
+{
+ struct lttng_ht_iter iter;
+ lttng::sessiond::ust::registry_event *event;
+ enum lttng_error_code cmd_ret;
+
+ LTTNG_ASSERT(chan);
+
+ if (notify) {
+ cmd_ret = notification_thread_command_remove_channel(
+ the_notification_thread_handle, chan->_consumer_key, LTTNG_DOMAIN_UST);
+ if (cmd_ret != LTTNG_OK) {
+ ERR("Failed to remove channel from notification thread");
+ }
+ }
+
+ if (chan->_events) {
+ lttng::urcu::read_lock_guard read_lock_guard;
+
+ /* Destroy all event associated with this registry. */
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ cds_lfht_for_each_entry (chan->_events->ht, &iter.iter, event, _node) {
+ /* Delete the node from the ht and free it. */
+ ust_registry_channel_destroy_event(chan, event);
+ }
+ DIAGNOSTIC_POP
+ }
+
+ call_rcu(&chan->_rcu_head, destroy_channel_rcu);
+}
+
+void destroy_enum(lsu::registry_enum *reg_enum)
+{
+ if (!reg_enum) {
+ return;
+ }
+
+ delete reg_enum;
+}
+
+void destroy_enum_rcu(struct rcu_head *head)
+{
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ lsu::registry_enum *reg_enum =
+ lttng::utils::container_of(head, &lsu::registry_enum::rcu_head);
+ DIAGNOSTIC_POP
+
+ destroy_enum(reg_enum);
+}
+
+/*
+ * Hash table match function for enumerations in the session. Match is
+ * performed on enumeration name, and confirmed by comparing the enum
+ * entries.
+ */
+int ht_match_enum(struct cds_lfht_node *node, const void *_key)
+{
+ lsu::registry_enum *_enum;
+ const lsu::registry_enum *key;
+
+ LTTNG_ASSERT(node);
+ LTTNG_ASSERT(_key);
+
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ _enum = caa_container_of(node, lsu::registry_enum, node.node);
+ DIAGNOSTIC_POP
+
+ LTTNG_ASSERT(_enum);
+ key = (lsu::registry_enum *) _key;
+
+ return *_enum == *key;
+}
+
+/*
+ * Hash table match function for enumerations in the session. Match is
+ * performed by enumeration ID.
+ */
+int ht_match_enum_id(struct cds_lfht_node *node, const void *_key)
+{
+ lsu::registry_enum *_enum;
+ const lsu::registry_enum *key = (lsu::registry_enum *) _key;
+
+ LTTNG_ASSERT(node);
+ LTTNG_ASSERT(_key);
+
+ DIAGNOSTIC_PUSH
+ DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
+ _enum = caa_container_of(node, lsu::registry_enum, node.node);
+ DIAGNOSTIC_POP
+
+ LTTNG_ASSERT(_enum);
+
+ if (_enum->id != key->id) {
+ goto no_match;
+ }
+
+ /* Match. */
+ return 1;
+
+no_match:
+ return 0;
+}
+
+/*
+ * Hash table hash function for enumerations in the session. The
+ * enumeration name is used for hashing.
+ */
+unsigned long ht_hash_enum(void *_key, unsigned long seed)
+{
+ lsu::registry_enum *key = (lsu::registry_enum *) _key;
+
+ LTTNG_ASSERT(key);
+ return hash_key_str(key->name.c_str(), seed);
+}
+} /* namespace */
+
+void lsu::details::locked_registry_session_release(lsu::registry_session *session)
+{
+ pthread_mutex_unlock(&session->_lock);
+}
+
+lsu::registry_session::registry_session(const struct lst::abi& in_abi,
+ uint32_t major,
+ uint32_t minor,
+ const char *root_shm_path,
+ const char *shm_path,
+ uid_t euid,
+ gid_t egid,
+ uint64_t tracing_id) :
+ lst::trace_class(in_abi, generate_uuid_or_throw()),
+ _root_shm_path{ root_shm_path ? root_shm_path : "" },
+ _shm_path{ shm_path ? shm_path : "" },
+ _metadata_path{ _shm_path.size() > 0 ? fmt::format("{}/metadata", _shm_path) :
+ std::string("") },
+ _uid{ euid },
+ _gid{ egid },
+ _app_tracer_version{ .major = major, .minor = minor },
+ _tracing_id{ tracing_id },
+ _clock{ lttng::make_unique<lsu::clock_class>() },
+ _metadata_generating_visitor{ lttng::make_unique<ls::tsdl::trace_class_visitor>(
+ abi,
+ [this](const std::string& fragment) { _append_metadata_fragment(fragment); }) },
+ _packet_header{ _create_packet_header() }
+{
+ pthread_mutex_init(&_lock, nullptr);
+ if (_shm_path.size() > 0) {
+ if (run_as_mkdir_recursive(_shm_path.c_str(), S_IRWXU | S_IRWXG, euid, egid)) {