Clean-up: sessiond: move registry_session free functions under class
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.cpp
index dd51d61f272fb482607c100e642bd0000e74a7b2..16848191d6f6d8f8b2bde23e207d3a3f47b8824c 100644 (file)
@@ -86,10 +86,10 @@ namespace {
  * A registry per UID object MUST exists before calling this function or else
  * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
  */
-static ust_registry_session *get_session_registry(
+static lsu::registry_session *get_session_registry(
                const struct ust_app_session *ua_sess)
 {
-       ust_registry_session *registry = NULL;
+       lsu::registry_session *registry = NULL;
 
        LTTNG_ASSERT(ua_sess);
 
@@ -122,7 +122,7 @@ error:
        return registry;
 }
 
-ust_registry_session::locked_ptr
+lsu::registry_session::locked_ptr
 get_locked_session_registry(const struct ust_app_session *ua_sess)
 {
        auto session = get_session_registry(ua_sess);
@@ -130,7 +130,7 @@ get_locked_session_registry(const struct ust_app_session *ua_sess)
                pthread_mutex_lock(&session->_lock);
        }
 
-       return ust_registry_session::locked_ptr{session};
+       return lsu::registry_session::locked_ptr{session};
 }
 } /* namespace */
 
@@ -562,7 +562,7 @@ end:
 static void delete_ust_app_channel(int sock,
                struct ust_app_channel *ua_chan,
                struct ust_app *app,
-               const ust_registry_session::locked_ptr& locked_registry)
+               const lsu::registry_session::locked_ptr& locked_registry)
 {
        int ret;
        struct lttng_ht_iter iter;
@@ -685,7 +685,7 @@ int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data
  * but it can be caused by recoverable errors (e.g. the application has
  * terminated concurrently).
  */
-ssize_t ust_app_push_metadata(const ust_registry_session::locked_ptr& locked_registry,
+ssize_t ust_app_push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
                struct consumer_socket *socket,
                int send_zero_data)
 {
@@ -825,7 +825,7 @@ error_push:
  * but it can be caused by recoverable errors (e.g. the application has
  * terminated concurrently).
  */
-static int push_metadata(const ust_registry_session::locked_ptr& locked_registry,
+static int push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
                struct consumer_output *consumer)
 {
        int ret_val;
@@ -2985,7 +2985,7 @@ error:
  */
 static int do_consumer_create_channel(struct ltt_ust_session *usess,
                struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
-               int bitness, ust_registry_session *registry)
+               int bitness, lsu::registry_session *registry)
 {
        int ret;
        unsigned int nb_fd = 0;
@@ -3504,7 +3504,7 @@ static int create_channel_per_pid(struct ust_app *app,
                struct ust_app_channel *ua_chan)
 {
        int ret;
-       ust_registry_session *registry;
+       lsu::registry_session *registry;
        enum lttng_error_code cmd_ret;
        struct ltt_session *session = NULL;
        uint64_t chan_reg_key;
@@ -5414,7 +5414,7 @@ int ust_app_flush_session(struct ltt_ust_session *usess)
 
                /* Flush all per UID buffers associated to that session. */
                cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
-                       ust_registry_session *ust_session_reg;
+                       lsu::registry_session *ust_session_reg;
                        struct buffer_reg_channel *buf_reg_chan;
                        struct consumer_socket *socket;
 
@@ -6613,16 +6613,16 @@ static int add_event_ust_registry(int sock, int sobjd, int cobjd, const char *na
  *
  * On success 0 is returned else a negative value.
  */
-static int add_enum_ust_registry(int sock, int sobjd, char *name,
-               struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries)
+static int add_enum_ust_registry(int sock, int sobjd, const char *name,
+               struct lttng_ust_ctl_enum_entry *raw_entries, size_t nr_entries)
 {
-       int ret = 0, ret_code;
+       int ret = 0;
        struct ust_app *app;
        struct ust_app_session *ua_sess;
-       ust_registry_session *registry;
        uint64_t enum_id = -1ULL;
-
-       rcu_read_lock();
+       lttng::urcu::read_lock_guard read_lock_guard;
+       auto entries = lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::free>(
+                       raw_entries);
 
        /* Lookup application. If not found, there is a code flow error. */
        app = find_app_by_notify_sock(sock);
@@ -6630,9 +6630,7 @@ static int add_enum_ust_registry(int sock, int sobjd, char *name,
                /* Return an error since this is not an error */
                DBG("Application socket %d is being torn down. Aborting enum registration",
                                sock);
-               free(entries);
-               ret = -1;
-               goto error_rcu_unlock;
+               return -1;
        }
 
        /* Lookup session by UST object descriptor. */
@@ -6640,34 +6638,37 @@ static int add_enum_ust_registry(int sock, int sobjd, char *name,
        if (!ua_sess) {
                /* Return an error since this is not an error */
                DBG("Application session is being torn down (session not found). Aborting enum registration.");
-               free(entries);
-               goto error_rcu_unlock;
+               return 0;
        }
 
-       registry = get_session_registry(ua_sess);
-       if (!registry) {
+       auto locked_registry = get_locked_session_registry(ua_sess);
+       if (!locked_registry) {
                DBG("Application session is being torn down (registry not found). Aborting enum registration.");
-               free(entries);
-               goto error_rcu_unlock;
+               return 0;
        }
 
-       pthread_mutex_lock(&registry->_lock);
-
        /*
         * From this point on, the callee acquires the ownership of
         * entries. The variable entries MUST NOT be read/written after
         * call.
         */
-       ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
-                       entries, nr_entries, &enum_id);
-       entries = NULL;
+       int application_reply_code;
+       try {
+               locked_registry->create_or_find_enum(
+                               sobjd, name, entries.release(), nr_entries, &enum_id);
+               application_reply_code = 0;
+       } catch (const std::exception& ex) {
+               ERR("%s: %s", fmt::format("Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
+                               *app, name).c_str(), ex.what());
+               application_reply_code = -1;
+       }
 
        /*
         * The return value is returned to ustctl so in case of an error, the
         * application can be notified. In case of an error, it's important not to
         * return a negative error or else the application will get closed.
         */
-       ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, ret_code);
+       ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code);
        if (ret < 0) {
                if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
                        DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
@@ -6683,16 +6684,11 @@ static int add_enum_ust_registry(int sock, int sobjd, char *name,
                 * No need to wipe the create enum since the application socket will
                 * get close on error hence cleaning up everything by itself.
                 */
-               goto error;
+               return ret;
        }
 
        DBG3("UST registry enum %s added successfully or already found", name);
-
-error:
-       pthread_mutex_unlock(&registry->_lock);
-error_rcu_unlock:
-       rcu_read_unlock();
-       return ret;
+       return 0;
 }
 
 /*
@@ -7038,7 +7034,7 @@ enum lttng_error_code ust_app_snapshot_record(
                        struct lttng_ht_iter chan_iter;
                        struct ust_app_channel *ua_chan;
                        struct ust_app_session *ua_sess;
-                       ust_registry_session *registry;
+                       lsu::registry_session *registry;
                        char pathname[PATH_MAX];
                        size_t consumer_path_offset = 0;
 
@@ -7433,7 +7429,7 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
                        struct lttng_ht_iter chan_iter;
                        struct ust_app_channel *ua_chan;
                        struct ust_app_session *ua_sess;
-                       ust_registry_session *registry;
+                       lsu::registry_session *registry;
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
@@ -7563,7 +7559,7 @@ enum lttng_error_code ust_app_create_channel_subdirectories(
                cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
                                pid_n.node) {
                        struct ust_app_session *ua_sess;
-                       ust_registry_session *registry;
+                       lsu::registry_session *registry;
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
@@ -7685,7 +7681,7 @@ enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
                        struct lttng_ht_iter chan_iter;
                        struct ust_app_channel *ua_chan;
                        struct ust_app_session *ua_sess;
-                       ust_registry_session *registry;
+                       lsu::registry_session *registry;
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
@@ -7832,7 +7828,7 @@ enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
                        struct lttng_ht_iter chan_iter;
                        struct ust_app_channel *ua_chan;
                        struct ust_app_session *ua_sess;
-                       ust_registry_session *registry;
+                       lsu::registry_session *registry;
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
This page took 0.026679 seconds and 4 git commands to generate.