Fix: tests: fix unused-but-set warning in test_fd_tracker.c
[lttng-tools.git] / src / bin / lttng-sessiond / action-executor.c
index 9a89865a930c142e395f134d6fb23303b3d3508d..94b819c46e5e4baeef67caabe6f844a85d20b8c3 100644 (file)
@@ -77,7 +77,7 @@ struct action_work_item {
         * The actions to be executed with their respective execution context.
         * See struct `action_work_subitem`.
         */
-       struct lttng_dynamic_array *subitems;
+       struct lttng_dynamic_array subitems;
 
        /* Execution context data */
        struct lttng_trigger *trigger;
@@ -135,7 +135,7 @@ static int action_executor_snapshot_session_handler(
                struct action_executor *executor,
                const struct action_work_item *,
                struct action_work_subitem *);
-static int action_executor_group_handler(struct action_executor *executor,
+static int action_executor_list_handler(struct action_executor *executor,
                const struct action_work_item *,
                struct action_work_subitem *);
 static int action_executor_generic_handler(struct action_executor *executor,
@@ -148,7 +148,7 @@ static const action_executor_handler action_executors[] = {
        [LTTNG_ACTION_TYPE_STOP_SESSION] = action_executor_stop_session_handler,
        [LTTNG_ACTION_TYPE_ROTATE_SESSION] = action_executor_rotate_session_handler,
        [LTTNG_ACTION_TYPE_SNAPSHOT_SESSION] = action_executor_snapshot_session_handler,
-       [LTTNG_ACTION_TYPE_GROUP] = action_executor_group_handler,
+       [LTTNG_ACTION_TYPE_LIST] = action_executor_list_handler,
 };
 
 /* Forward declaration */
@@ -169,7 +169,7 @@ static const char *get_action_name(const struct lttng_action *action)
 {
        const enum lttng_action_type action_type = lttng_action_get_type(action);
 
-       assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
+       LTTNG_ASSERT(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
 
        return lttng_action_type_string(action_type);
 }
@@ -660,11 +660,11 @@ end:
        return ret;
 }
 
-static int action_executor_group_handler(struct action_executor *executor,
+static int action_executor_list_handler(struct action_executor *executor,
                const struct action_work_item *work_item,
                struct action_work_subitem *item)
 {
-       ERR("Execution of a group action by the action executor should never occur");
+       ERR("Execution of a list action by the action executor should never occur");
        abort();
 }
 
@@ -676,7 +676,7 @@ static int action_executor_generic_handler(struct action_executor *executor,
        struct lttng_action *action = item->action;
        const enum lttng_action_type action_type = lttng_action_get_type(action);
 
-       assert(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
+       LTTNG_ASSERT(action_type != LTTNG_ACTION_TYPE_UNKNOWN);
 
        lttng_action_increase_execution_request_count(action);
        if (!lttng_action_should_execute(action)) {
@@ -707,11 +707,11 @@ static int action_work_item_execute(struct action_executor *executor,
        DBG("Starting execution of action work item %" PRIu64 " of trigger `%s`",
                        work_item->id, get_trigger_name(work_item->trigger));
 
-       count = lttng_dynamic_array_get_count(work_item->subitems);
+       count = lttng_dynamic_array_get_count(&work_item->subitems);
        for (i = 0; i < count; i++) {
                struct action_work_subitem *item;
 
-               item = lttng_dynamic_array_get_element(work_item->subitems, i);
+               item = lttng_dynamic_array_get_element(&work_item->subitems, i);
                ret = action_executor_generic_handler(
                                executor, work_item, item);
                if (ret) {
@@ -729,7 +729,7 @@ static void action_work_item_destroy(struct action_work_item *work_item)
        lttng_trigger_put(work_item->trigger);
        lttng_evaluation_destroy(work_item->evaluation);
        notification_client_list_put(work_item->client_list);
-       lttng_dynamic_array_reset(work_item->subitems);
+       lttng_dynamic_array_reset(&work_item->subitems);
        free(work_item);
 }
 
@@ -737,7 +737,7 @@ static void *action_executor_thread(void *_data)
 {
        struct action_executor *executor = _data;
 
-       assert(executor);
+       LTTNG_ASSERT(executor);
 
        health_register(the_health_sessiond,
                        HEALTH_SESSIOND_TYPE_ACTION_EXECUTOR);
@@ -748,7 +748,7 @@ static void *action_executor_thread(void *_data)
        DBG("Entering work execution loop");
        pthread_mutex_lock(&executor->work.lock);
        while (!executor->should_quit) {
-               int ret;
+               int ret = 0;
                struct action_work_item *work_item;
 
                health_code_update();
@@ -785,7 +785,7 @@ static void *action_executor_thread(void *_data)
 
                        trigger_status = lttng_trigger_get_owner_uid(
                                        work_item->trigger, &trigger_owner_uid);
-                       assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
+                       LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
 
                        DBG("Work item skipped since the associated trigger is no longer registered: work item id = %" PRIu64 ", trigger name = '%s', trigger owner uid = %d",
                                        work_item->id, trigger_name,
@@ -837,7 +837,7 @@ static void clean_up_action_executor_thread(void *_data)
 {
        struct action_executor *executor = _data;
 
-       assert(cds_list_empty(&executor->work.list));
+       LTTNG_ASSERT(cds_list_empty(&executor->work.list));
 
        pthread_mutex_destroy(&executor->work.lock);
        pthread_cond_destroy(&executor->work.cond);
@@ -905,29 +905,8 @@ enum action_executor_status action_executor_enqueue_trigger(
        const uint64_t work_item_id = executor->next_work_item_id++;
        struct action_work_item *work_item;
        bool signal = false;
-       struct lttng_dynamic_array *subitems = NULL;
 
-       assert(trigger);
-
-       /* Build the array of action work subitems for the passed trigger. */
-       subitems = zmalloc(sizeof(*subitems));
-       if (!subitems) {
-               PERROR("Failed to allocate action executor subitems array: trigger name = `%s`",
-                               get_trigger_name(trigger));
-               executor_status = ACTION_EXECUTOR_STATUS_ERROR;
-               goto error_unlock;
-       }
-
-       lttng_dynamic_array_init(subitems, sizeof(struct action_work_subitem),
-                       action_work_subitem_destructor);
-
-       ret = populate_subitem_array_from_trigger(trigger, subitems);
-       if (ret) {
-               ERR("Failed to populate work item sub items on behalf of trigger: trigger name = `%s`",
-                               get_trigger_name(trigger));
-               executor_status = ACTION_EXECUTOR_STATUS_ERROR;
-               goto error_unlock;
-       }
+       LTTNG_ASSERT(trigger);
 
        pthread_mutex_lock(&executor->work.lock);
        /* Check for queue overflow. */
@@ -952,13 +931,11 @@ enum action_executor_status action_executor_enqueue_trigger(
                const bool reference_acquired =
                                notification_client_list_get(client_list);
 
-               assert(reference_acquired);
+               LTTNG_ASSERT(reference_acquired);
        }
 
        *work_item = (typeof(*work_item)){
                        .id = work_item_id,
-                       /* Ownership transferred to the work item. */
-                       .subitems = subitems,
                        .trigger = trigger,
                        /* Ownership transferred to the work item. */
                        .evaluation = evaluation,
@@ -972,7 +949,21 @@ enum action_executor_status action_executor_enqueue_trigger(
        };
 
        evaluation = NULL;
-       subitems = NULL;
+
+       /* Build the array of action work subitems for the passed trigger. */
+       lttng_dynamic_array_init(&work_item->subitems,
+                       sizeof(struct action_work_subitem),
+                       action_work_subitem_destructor);
+
+       ret = populate_subitem_array_from_trigger(
+                       trigger, &work_item->subitems);
+       if (ret) {
+               ERR("Failed to populate work item sub items on behalf of trigger: trigger name = `%s`",
+                               get_trigger_name(trigger));
+               executor_status = ACTION_EXECUTOR_STATUS_ERROR;
+               goto error_unlock;
+       }
+
        cds_list_add_tail(&work_item->list_node, &executor->work.list);
        executor->work.pending_count++;
        DBG("Enqueued action for trigger: trigger name = `%s`, work item id = %" PRIu64,
@@ -983,20 +974,16 @@ error_unlock:
        if (signal) {
                pthread_cond_signal(&executor->work.cond);
        }
-       pthread_mutex_unlock(&executor->work.lock);
 
+       pthread_mutex_unlock(&executor->work.lock);
        lttng_evaluation_destroy(evaluation);
-       if (subitems) {
-               lttng_dynamic_array_reset(subitems);
-               free(subitems);
-       }
        return executor_status;
 }
 
 static int add_action_to_subitem_array(struct lttng_action *action,
                struct lttng_dynamic_array *subitems)
 {
-       int ret;
+       int ret = 0;
        enum lttng_action_type type = lttng_action_get_type(action);
        const char *session_name = NULL;
        enum lttng_action_status status;
@@ -1007,21 +994,21 @@ static int add_action_to_subitem_array(struct lttng_action *action,
                },
        };
 
-       assert(action);
-       assert(subitems);
+       LTTNG_ASSERT(action);
+       LTTNG_ASSERT(subitems);
 
-       if (type == LTTNG_ACTION_TYPE_GROUP) {
+       if (type == LTTNG_ACTION_TYPE_LIST) {
                unsigned int count, i;
 
                status = lttng_action_list_get_count(action, &count);
-               assert(status == LTTNG_ACTION_STATUS_OK);
+               LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
 
                for (i = 0; i < count; i++) {
                        struct lttng_action *inner_action = NULL;
 
                        inner_action = lttng_action_list_borrow_mutable_at_index(
                                        action, i);
-                       assert(inner_action);
+                       LTTNG_ASSERT(inner_action);
                        ret = add_action_to_subitem_array(
                                        inner_action, subitems);
                        if (ret) {
@@ -1031,7 +1018,7 @@ static int add_action_to_subitem_array(struct lttng_action *action,
 
                /*
                 * Go directly to the end since there is no need to add the
-                * group action by itself to the subitems array.
+                * list action by itself to the subitems array.
                 */
                goto end;
        }
@@ -1043,24 +1030,24 @@ static int add_action_to_subitem_array(struct lttng_action *action,
        case LTTNG_ACTION_TYPE_START_SESSION:
                status = lttng_action_start_session_get_session_name(
                                action, &session_name);
-               assert(status == LTTNG_ACTION_STATUS_OK);
+               LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
                break;
        case LTTNG_ACTION_TYPE_STOP_SESSION:
                status = lttng_action_stop_session_get_session_name(
                                action, &session_name);
-               assert(status == LTTNG_ACTION_STATUS_OK);
+               LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
                break;
        case LTTNG_ACTION_TYPE_ROTATE_SESSION:
                status = lttng_action_rotate_session_get_session_name(
                                action, &session_name);
-               assert(status == LTTNG_ACTION_STATUS_OK);
+               LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
                break;
        case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
                status = lttng_action_snapshot_session_get_session_name(
                                action, &session_name);
-               assert(status == LTTNG_ACTION_STATUS_OK);
+               LTTNG_ASSERT(status == LTTNG_ACTION_STATUS_OK);
                break;
-       case LTTNG_ACTION_TYPE_GROUP:
+       case LTTNG_ACTION_TYPE_LIST:
        case LTTNG_ACTION_TYPE_UNKNOWN:
                /* Fallthrough */
        default:
@@ -1076,17 +1063,26 @@ static int add_action_to_subitem_array(struct lttng_action *action,
         * simplicity and consistency.
         */
        if (session_name != NULL) {
-               struct ltt_session *session = NULL;
+               uint64_t session_id;
 
-               session_lock_list();
-               session = session_find_by_name(session_name);
-               if (session) {
+               /*
+                * Instantaneous sampling of the session id if present.
+                *
+                * This method is preferred over `sessiond_find_by_name` then
+                * fetching the session'd id since `sessiond_find_by_name`
+                * requires the session list lock to be taken.
+                *
+                * Taking the session list lock can lead to a deadlock
+                * between the action executor and the notification thread
+                * (caller of add_action_to_subitem_array). It is okay if the
+                * session state changes between the enqueuing time and the
+                * execution time. The execution context is validated at
+                * execution time.
+                */
+               if (sample_session_id_by_name(session_name, &session_id)) {
                        LTTNG_OPTIONAL_SET(&subitem.context.session_id,
-                                       session->id);
-                       session_put(session);
+                                       session_id);
                }
-
-               session_unlock_list();
        }
 
        /* Get a reference to the action. */
@@ -1111,7 +1107,7 @@ static int populate_subitem_array_from_trigger(struct lttng_trigger *trigger,
        struct lttng_action *action;
 
        action = lttng_trigger_get_action(trigger);
-       assert(action);
+       LTTNG_ASSERT(action);
 
        return add_action_to_subitem_array(action, subitems);
 }
This page took 0.028701 seconds and 4 git commands to generate.