Convert ltt_session to c++
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.cpp
index aea78167160be7abc349758af1ad776d45b3c988..8f2c379584f8710eefd865098278e92aec6db4c8 100644 (file)
 /* Sleep for 100ms between each check for the shm path's deletion. */
 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
 
+namespace lsu = lttng::sessiond::ust;
+
+static enum lttng_error_code wait_on_path(void *path);
+
+namespace {
 struct cmd_destroy_session_reply_context {
        int reply_sock_fd;
        bool implicit_rotation_on_destroy;
@@ -84,15 +89,13 @@ struct cmd_destroy_session_reply_context {
        enum lttng_error_code destruction_status;
 };
 
-static enum lttng_error_code wait_on_path(void *path);
-
 /*
  * Command completion handler that is used by the destroy command
  * when a session that has a non-default shm_path is being destroyed.
  *
  * See comment in cmd_destroy_session() for the rationale.
  */
-static struct destroy_completion_handler {
+struct destroy_completion_handler {
        struct cmd_completion_handler handler;
        char shm_path[member_sizeof(struct ltt_session, shm_path)];
 } destroy_completion_handler = {
@@ -103,17 +106,17 @@ static struct destroy_completion_handler {
        .shm_path = { 0 },
 };
 
-static struct cmd_completion_handler *current_completion_handler;
-
 /*
  * Used to keep a unique index for each relayd socket created where this value
  * is associated with streams on the consumer so it can match the right relayd
  * to send to. It must be accessed with the relayd_net_seq_idx_lock
  * held.
  */
-static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
-static uint64_t relayd_net_seq_idx;
+pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
+uint64_t relayd_net_seq_idx;
+} /* namespace */
 
+static struct cmd_completion_handler *current_completion_handler;
 static int validate_ust_event_name(const char *);
 static int cmd_enable_event_internal(struct ltt_session *session,
                const struct lttng_domain *domain,
@@ -2748,7 +2751,7 @@ int cmd_start_trace(struct ltt_session *session)
                goto error;
        }
 
-       session->active = 1;
+       session->active = true;
        session->rotated_after_last_stop = false;
        session->cleared_after_last_stop = false;
        if (session->output_traces && !session->current_trace_chunk) {
@@ -2843,9 +2846,9 @@ int cmd_start_trace(struct ltt_session *session)
 error:
        if (ret == LTTNG_OK) {
                /* Flag this after a successful start. */
-               session->has_been_started |= 1;
+               session->has_been_started = true;
        } else {
-               session->active = 0;
+               session->active = false;
                /* Restore initial state on error. */
                session->rotated_after_last_stop =
                                session_rotated_after_last_stop;
@@ -3133,6 +3136,28 @@ enum lttng_error_code cmd_create_session_from_descriptor(
                goto end;
        }
 
+       ret_code = notification_thread_command_add_session(the_notification_thread_handle,
+                       new_session->id, new_session->name, new_session->uid, new_session->gid);
+       if (ret_code != LTTNG_OK) {
+               goto end;
+       }
+
+       /* Announce the session's destruction to the notification thread when it is destroyed. */
+       ret = session_add_destroy_notifier(
+                       new_session,
+                       [](const struct ltt_session *session,
+                                       void *user_data __attribute__((unused))) {
+                               (void) notification_thread_command_remove_session(
+                                               the_notification_thread_handle, session->id);
+                       },
+                       NULL);
+       if (ret) {
+               PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
+                               new_session->name);
+               ret = LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
        if (!session_name) {
                ret = lttng_session_descriptor_set_session_name(descriptor,
                                new_session->name);
@@ -3921,23 +3946,38 @@ end:
  * The session list lock MUST be acquired before calling this function. Use
  * session_lock_list() and session_unlock_list().
  */
-void cmd_list_lttng_sessions(struct lttng_session *sessions,
-               size_t session_count, uid_t uid, gid_t gid)
+enum lttng_error_code cmd_list_lttng_sessions(
+               struct lttng_payload *reply_payload, uid_t uid, gid_t gid)
 {
-       int ret;
-       unsigned int i = 0;
+       int buffer_resize_ret;
+       enum lttng_error_code ret_code = LTTNG_OK;
+       struct lttcomm_list_command_header reply_command_header = {};
+       size_t reply_command_header_offset;
        struct ltt_session *session;
        struct ltt_session_list *list = session_get_list();
-       struct lttng_session_extended *extended =
-                       (typeof(extended)) (&sessions[session_count]);
+       int ret;
+       unsigned int i = 0;
+
+       assert(reply_payload);
+
+       /* Reserve space for command reply header. */
+       reply_command_header_offset = reply_payload->buffer.size;
+       buffer_resize_ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
+                       reply_command_header_offset + sizeof(struct lttcomm_list_command_header));
+       if (buffer_resize_ret) {
+               ret_code = LTTNG_ERR_NOMEM;
+               goto error;
+       }
 
        DBG("Getting all available session for UID %d GID %d",
                        uid, gid);
-       /*
-        * Iterate over session list and append data after the control struct in
-        * the buffer.
-        */
+
        cds_list_for_each_entry(session, &list->head, list) {
+               struct lttng_session tmp_session = {};
+               struct lttng_session_extended tmp_extended = {};
+
+               tmp_session.extended.ptr = &tmp_extended;
+
                if (!session_get(session)) {
                        continue;
                }
@@ -3956,10 +3996,10 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
                if (session->consumer->type == CONSUMER_DST_NET ||
                                (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
                                (usess && usess->consumer->type == CONSUMER_DST_NET)) {
-                       ret = build_network_session_path(sessions[i].path,
-                                       sizeof(sessions[i].path), session);
+                       ret = build_network_session_path(
+                                       tmp_session.path, sizeof(tmp_session.path), session);
                } else {
-                       ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
+                       ret = snprintf(tmp_session.path, sizeof(tmp_session.path), "%s",
                                        session->consumer->dst.session_root_path);
                }
                if (ret < 0) {
@@ -3968,16 +4008,33 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions,
                        continue;
                }
 
-               strncpy(sessions[i].name, session->name, NAME_MAX);
-               sessions[i].name[NAME_MAX - 1] = '\0';
-               sessions[i].enabled = session->active;
-               sessions[i].snapshot_mode = session->snapshot_mode;
-               sessions[i].live_timer_interval = session->live_timer;
-               extended[i].creation_time.value = (uint64_t) session->creation_time;
-               extended[i].creation_time.is_set = 1;
+               strncpy(tmp_session.name, session->name, NAME_MAX);
+               tmp_session.name[NAME_MAX - 1] = '\0';
+               tmp_session.enabled = session->active;
+               tmp_session.snapshot_mode = session->snapshot_mode;
+               tmp_session.live_timer_interval = session->live_timer;
+               LTTNG_OPTIONAL_SET(&tmp_extended.creation_time, (uint64_t) session->creation_time);
+               ret = lttng_session_serialize(&tmp_session, reply_payload);
+               if (ret) {
+                       ret_code = LTTNG_ERR_FATAL;
+                       goto error;
+               }
                i++;
                session_put(session);
        }
+
+       if (i > UINT32_MAX) {
+               ret_code = LTTNG_ERR_OVERFLOW;
+               goto error;
+       }
+
+       /* Update command reply header. */
+       reply_command_header.count = (uint32_t) i;
+       memcpy(reply_payload->buffer.data + reply_command_header_offset, &reply_command_header,
+                       sizeof(reply_command_header));
+       ret_code = LTTNG_OK;
+error:
+       return ret_code;
 }
 
 /*
@@ -4295,102 +4352,6 @@ end:
        return ret;
 }
 
-static
-int clear_metadata_file(int fd)
-{
-       int ret;
-       off_t lseek_ret;
-
-       lseek_ret = lseek(fd, 0, SEEK_SET);
-       if (lseek_ret < 0) {
-               PERROR("lseek");
-               ret = -1;
-               goto end;
-       }
-
-       ret = ftruncate(fd, 0);
-       if (ret < 0) {
-               PERROR("ftruncate");
-               goto end;
-       }
-
-end:
-       return ret;
-}
-
-static
-int ust_regenerate_metadata(struct ltt_ust_session *usess)
-{
-       int ret = 0;
-       struct buffer_reg_uid *uid_reg = NULL;
-       struct buffer_reg_session *session_reg = NULL;
-
-       rcu_read_lock();
-       cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
-               struct ust_registry_session *registry;
-               struct ust_registry_channel *chan;
-               struct lttng_ht_iter iter_chan;
-
-               session_reg = uid_reg->registry;
-               registry = session_reg->reg.ust;
-
-               pthread_mutex_lock(&registry->lock);
-               registry->metadata_len_sent = 0;
-               memset(registry->metadata, 0, registry->metadata_alloc_len);
-               registry->metadata_len = 0;
-               registry->metadata_version++;
-               if (registry->metadata_fd > 0) {
-                       /* Clear the metadata file's content. */
-                       ret = clear_metadata_file(registry->metadata_fd);
-                       if (ret) {
-                               pthread_mutex_unlock(&registry->lock);
-                               goto end;
-                       }
-               }
-
-               ret = ust_metadata_session_statedump(registry, NULL,
-                               registry->major, registry->minor);
-               if (ret) {
-                       pthread_mutex_unlock(&registry->lock);
-                       ERR("Failed to generate session metadata (err = %d)",
-                                       ret);
-                       goto end;
-               }
-               cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
-                               chan, node.node) {
-                       struct ust_registry_event *event;
-                       struct lttng_ht_iter iter_event;
-
-                       chan->metadata_dumped = 0;
-
-                       ret = ust_metadata_channel_statedump(registry, chan);
-                       if (ret) {
-                               pthread_mutex_unlock(&registry->lock);
-                               ERR("Failed to generate channel metadata "
-                                               "(err = %d)", ret);
-                               goto end;
-                       }
-                       cds_lfht_for_each_entry(chan->events->ht, &iter_event.iter,
-                                               event, node.node) {
-                               event->metadata_dumped = 0;
-                               ret = ust_metadata_event_statedump(registry,
-                                               chan, event);
-                               if (ret) {
-                                       pthread_mutex_unlock(&registry->lock);
-                                       ERR("Failed to generate event metadata "
-                                                       "(err = %d)", ret);
-                                       goto end;
-                               }
-                       }
-               }
-               pthread_mutex_unlock(&registry->lock);
-       }
-
-end:
-       rcu_read_unlock();
-       return ret;
-}
-
 /*
  * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
  *
@@ -4421,7 +4382,7 @@ int cmd_regenerate_metadata(struct ltt_session *session)
        }
 
        if (session->ust_session) {
-               ret = ust_regenerate_metadata(session->ust_session);
+               ret = trace_ust_regenerate_metadata(session->ust_session);
                if (ret < 0) {
                        ERR("Failed to regenerate the UST metadata");
                        goto end;
@@ -5702,8 +5663,7 @@ int cmd_rotate_session(struct ltt_session *session,
        chunk_being_archived = NULL;
        if (!quiet_rotation) {
                ret = notification_thread_command_session_rotation_ongoing(
-                               the_notification_thread_handle, session->name,
-                               session->uid, session->gid,
+                               the_notification_thread_handle, session->id,
                                ongoing_rotation_chunk_id);
                if (ret != LTTNG_OK) {
                        ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
This page took 0.028292 seconds and 5 git commands to generate.