trigger: keep state of if a trigger is currently registered
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Mon, 12 Apr 2021 18:45:24 +0000 (14:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 18 Apr 2021 23:28:57 +0000 (19:28 -0400)
Since a trigger can be referenced even when is was "unregistered" in
other part of lttng-sessiond, namely the action executor queue, we must
keep track of the registration state.

This will allows us to easily skip any actions to be executed
if the associated trigger is "unregistered" at the moment of
execution. This is implemented in a following patch.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I06c9d437fce975c9c8393e8d1be3e66a24618af3

include/lttng/trigger/trigger-internal.h
src/bin/lttng-sessiond/notification-thread-events.c
src/common/trigger.c

index c5ee9c64b4c1db9642251417fa812f3e18a9a68d..f16b68d00fd88412af0e649e02aef3447b434c11 100644 (file)
@@ -8,13 +8,14 @@
 #ifndef LTTNG_TRIGGER_INTERNAL_H
 #define LTTNG_TRIGGER_INTERNAL_H
 
-#include <lttng/trigger/trigger.h>
 #include <common/credentials.h>
 #include <common/dynamic-array.h>
 #include <common/macros.h>
 #include <common/optional.h>
-#include <stdint.h>
+#include <lttng/trigger/trigger.h>
+#include <pthread.h>
 #include <stdbool.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <urcu/ref.h>
 
@@ -36,6 +37,25 @@ struct lttng_trigger {
         * notification.
         */
        LTTNG_OPTIONAL(uint64_t) tracer_token;
+
+       /*
+        * Is the trigger registered?
+        *
+        * This is necessary since a reference holder might be interested in the
+        * overall state of the trigger from the point of view of its owner.
+        *
+        * The main user is the action executor since we want to prevent the
+        * execution of actions related to a trigger that is unregistered.
+        *
+        * Not considered for `is_equal`.
+        */
+       bool registered;
+
+       /*
+        * The lock is used to protect against concurrent trigger execution and
+        * trigger removal.
+        */
+       pthread_mutex_t lock;
 };
 
 struct lttng_triggers {
@@ -182,4 +202,28 @@ struct lttng_trigger *lttng_trigger_copy(const struct lttng_trigger *trigger);
 LTTNG_HIDDEN
 bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger);
 
+LTTNG_HIDDEN
+void lttng_trigger_set_as_registered(struct lttng_trigger *trigger);
+
+LTTNG_HIDDEN
+void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger);
+
+/*
+ * The trigger must be locked before calling lttng_trigger_is_registered.
+ *
+ * The lock is necessary since a trigger can be unregistered at any time.
+ *
+ * Manipulations requiring that the trigger be registered must always acquire
+ * the trigger lock for the duration of the manipulation using
+ * `lttng_trigger_lock` and `lttng_trigger_unlock`.
+ */
+LTTNG_HIDDEN
+bool lttng_trigger_is_registered(struct lttng_trigger *trigger);
+
+LTTNG_HIDDEN
+void lttng_trigger_lock(struct lttng_trigger *trigger);
+
+LTTNG_HIDDEN
+void lttng_trigger_unlock(struct lttng_trigger *trigger);
+
 #endif /* LTTNG_TRIGGER_INTERNAL_H */
index 29318432f842b2f26193388aa563da350ed8ae04..ec4d56bce7d9ec78c6c6926e256e376f59b6297a 100644 (file)
@@ -2693,6 +2693,9 @@ int handle_notification_thread_command_register_trigger(
                goto error_free_ht_element;
        }
 
+       /* From this point consider the trigger registered. */
+       lttng_trigger_set_as_registered(trigger);
+
        /*
         * Some triggers might need a tracer notifier depending on its
         * condition and actions.
@@ -2892,6 +2895,11 @@ error_free_ht_element:
        }
 error:
        if (free_trigger) {
+               /*
+                * Other objects might have a reference to the trigger, mark it
+                * as unregistered.
+                */
+               lttng_trigger_set_as_unregistered(trigger);
                lttng_trigger_destroy(trigger);
        }
 end:
@@ -2973,6 +2981,12 @@ int handle_notification_thread_command_unregister_trigger(
                cmd_reply = LTTNG_OK;
        }
 
+       trigger_ht_element = caa_container_of(triggers_ht_node,
+                       struct lttng_trigger_ht_element, node);
+
+       /* From this point, consider the trigger unregistered no matter what. */
+       lttng_trigger_set_as_unregistered(trigger_ht_element->trigger);
+
        /* Remove trigger from channel_triggers_ht. */
        cds_lfht_for_each_entry(state->channel_triggers_ht, &iter, trigger_list,
                        channel_triggers_ht_node) {
@@ -2995,9 +3009,6 @@ int handle_notification_thread_command_unregister_trigger(
                teardown_tracer_notifier(state, trigger);
        }
 
-       trigger_ht_element = caa_container_of(triggers_ht_node,
-                       struct lttng_trigger_ht_element, node);
-
        if (is_trigger_action_notify(trigger)) {
                /*
                 * Remove and release the client list from
index 125c871eaf80c98d63579713866d88339de4f760..ec96fc80b7d804249c8c4f1f395d4a693080fd1e 100644 (file)
@@ -5,24 +5,24 @@
  *
  */
 
-#include <lttng/trigger/trigger-internal.h>
-#include <lttng/condition/condition-internal.h>
-#include <lttng/condition/on-event-internal.h>
-#include <lttng/condition/on-event.h>
-#include <lttng/condition/on-event-internal.h>
-#include <lttng/condition/buffer-usage.h>
-#include <lttng/event-rule/event-rule-internal.h>
-#include <lttng/event-expr-internal.h>
-#include <lttng/action/action-internal.h>
+#include <assert.h>
 #include <common/credentials.h>
-#include <common/payload.h>
-#include <common/payload-view.h>
-#include <lttng/domain.h>
-#include <common/error.h>
 #include <common/dynamic-array.h>
+#include <common/error.h>
 #include <common/optional.h>
-#include <assert.h>
+#include <common/payload-view.h>
+#include <common/payload.h>
 #include <inttypes.h>
+#include <lttng/action/action-internal.h>
+#include <lttng/condition/buffer-usage.h>
+#include <lttng/condition/condition-internal.h>
+#include <lttng/condition/on-event-internal.h>
+#include <lttng/condition/on-event.h>
+#include <lttng/domain.h>
+#include <lttng/event-expr-internal.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/trigger/trigger-internal.h>
+#include <pthread.h>
 
 LTTNG_HIDDEN
 bool lttng_trigger_validate(const struct lttng_trigger *trigger)
@@ -68,6 +68,9 @@ struct lttng_trigger *lttng_trigger_create(
        lttng_action_get(action);
        trigger->action = action;
 
+       pthread_mutex_init(&trigger->lock, NULL);
+       trigger->registered = false;
+
 end:
        return trigger;
 }
@@ -121,6 +124,8 @@ static void trigger_destroy_ref(struct urcu_ref *ref)
        lttng_action_put(action);
        lttng_condition_put(condition);
 
+       pthread_mutex_destroy(&trigger->lock);
+
        free(trigger->name);
        free(trigger);
 }
@@ -895,3 +900,45 @@ bool lttng_trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
 end:
        return needs_tracer_notifier;
 }
+
+LTTNG_HIDDEN
+void lttng_trigger_set_as_registered(struct lttng_trigger *trigger)
+{
+       pthread_mutex_lock(&trigger->lock);
+       trigger->registered = true;
+       pthread_mutex_unlock(&trigger->lock);
+}
+
+LTTNG_HIDDEN
+void lttng_trigger_set_as_unregistered(struct lttng_trigger *trigger)
+{
+       pthread_mutex_lock(&trigger->lock);
+       trigger->registered = false;
+       pthread_mutex_unlock(&trigger->lock);
+}
+
+/*
+ * The trigger must be locked before calling lttng_trigger_registered.
+ * The lock is necessary since a trigger can be unregistered at anytime.
+ * Manipulations requiring that the trigger be registered must always acquire
+ * the trigger lock for the duration of the manipulation using
+ * `lttng_trigger_lock` and `lttng_trigger_unlock`.
+ */
+LTTNG_HIDDEN
+bool lttng_trigger_is_registered(struct lttng_trigger *trigger)
+{
+       ASSERT_LOCKED(trigger->lock);
+       return trigger->registered;
+}
+
+LTTNG_HIDDEN
+void lttng_trigger_lock(struct lttng_trigger *trigger)
+{
+       pthread_mutex_lock(&trigger->lock);
+}
+
+LTTNG_HIDDEN
+void lttng_trigger_unlock(struct lttng_trigger *trigger)
+{
+       pthread_mutex_unlock(&trigger->lock);
+}
This page took 0.02953 seconds and 4 git commands to generate.