Fix: sessiond: leak of trigger on registration error
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index 061629d11424866cbbadeb173149e2d699a60fcc..8d36e7192c8deb32a46ca471c5d58dafd5e19d63 100644 (file)
 #include <lttng/location-internal.h>
 #include <lttng/trigger/trigger-internal.h>
 #include <lttng/condition/condition.h>
+#include <lttng/condition/condition-internal.h>
+#include <lttng/condition/event-rule.h>
+#include <lttng/condition/event-rule-internal.h>
+#include <lttng/event-rule/event-rule.h>
+#include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/action/action.h>
 #include <lttng/channel.h>
 #include <lttng/channel-internal.h>
@@ -483,7 +488,7 @@ static int list_lttng_agent_events(struct agent *agt,
        cds_lfht_for_each_entry (
                        agt->events->ht, &iter.iter, agent_event, node.node) {
                struct lttng_event event = {
-                       .enabled = agent_event->enabled,
+                       .enabled = AGENT_EVENT_IS_ENABLED(agent_event),
                        .loglevel = agent_event->loglevel_value,
                        .loglevel_type = agent_event->loglevel_type,
                };
@@ -4288,12 +4293,46 @@ end:
        return ret;
 }
 
-int cmd_register_trigger(const struct lttng_credentials *cmd_creds,
+static enum lttng_error_code trigger_modifies_event_notifier(
+               const struct lttng_trigger *trigger, bool *adds_event_notifier)
+{
+       enum lttng_error_code ret_code = LTTNG_OK;
+       const struct lttng_condition *condition = NULL;
+
+       condition = lttng_trigger_get_const_condition(trigger);
+       if (!condition) {
+               ret_code = LTTNG_ERR_INVALID_TRIGGER;
+               goto end;
+       }
+
+       *adds_event_notifier = lttng_condition_get_type(condition) ==
+                       LTTNG_CONDITION_TYPE_EVENT_RULE_HIT;
+end:
+       return ret_code;
+}
+
+enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
                struct lttng_trigger *trigger,
                struct notification_thread_handle *notification_thread,
                struct lttng_trigger **return_trigger)
 {
-       int ret;
+       enum lttng_error_code ret_code;
+       bool must_update_event_notifiers;
+       const char *trigger_name;
+       uid_t trigger_owner;
+       enum lttng_trigger_status trigger_status;
+
+       trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
+       trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
+                       trigger_name : "(unnamed)";
+
+       trigger_status = lttng_trigger_get_owner_uid(
+               trigger, &trigger_owner);
+       assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
+
+       DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
+                       trigger_name, (int) trigger_owner,
+                       (int) lttng_credentials_get_uid(cmd_creds));
 
        /*
         * Validate the trigger credentials against the command credentials.
@@ -4304,8 +4343,10 @@ int cmd_register_trigger(const struct lttng_credentials *cmd_creds,
                        lttng_trigger_get_credentials(trigger),
                        cmd_creds)) {
                if (lttng_credentials_get_uid(cmd_creds) != 0) {
-                       ERR("Trigger credentials do not match the command credentials");
-                       ret = LTTNG_ERR_INVALID_TRIGGER;
+                       ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
+                                       trigger_name, (int) trigger_owner,
+                                       (int) lttng_credentials_get_uid(cmd_creds));
+                       ret_code = LTTNG_ERR_INVALID_TRIGGER;
                        goto end;
                }
        }
@@ -4314,8 +4355,10 @@ int cmd_register_trigger(const struct lttng_credentials *cmd_creds,
         * The bytecode generation also serves as a validation step for the
         * bytecode expressions.
         */
-       ret = lttng_trigger_generate_bytecode(trigger, cmd_creds);
-       if (ret != LTTNG_OK) {
+       ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
+       if (ret_code != LTTNG_OK) {
+               ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                               trigger_name, (int) trigger_owner, ret_code);
                goto end;
        }
 
@@ -4329,10 +4372,84 @@ int cmd_register_trigger(const struct lttng_credentials *cmd_creds,
         * it is safe to use without any locking as its properties are
         * immutable.
         */
-       ret = notification_thread_command_register_trigger(notification_thread,
+       ret_code = notification_thread_command_register_trigger(notification_thread,
                        trigger);
-       if (ret != LTTNG_OK) {
-               goto end_notification_thread;
+       if (ret_code != LTTNG_OK) {
+               DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                               trigger_name, (int) trigger_owner, ret_code);
+               goto end;
+       }
+
+       trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
+       trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
+                       trigger_name : "(unnamed)";
+
+       ret_code = trigger_modifies_event_notifier(trigger, &must_update_event_notifiers);
+       if (ret_code != LTTNG_OK) {
+               ERR("Failed to determine if event modifies event notifiers: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                               trigger_name, (int) trigger_owner, ret_code);
+               goto end;
+       }
+
+       /*
+        * Synchronize tracers if the trigger adds an event notifier.
+        */
+       if (must_update_event_notifiers) {
+               const enum lttng_domain_type trigger_domain =
+                               lttng_trigger_get_underlying_domain_type_restriction(trigger);
+
+               session_lock_list();
+               switch (trigger_domain) {
+               case LTTNG_DOMAIN_KERNEL:
+               {
+                       ret_code = kernel_register_event_notifier(
+                                       trigger, cmd_creds);
+                       if (ret_code != LTTNG_OK) {
+                               const enum lttng_error_code notif_thread_unregister_ret =
+                                               notification_thread_command_unregister_trigger(
+                                                               notification_thread,
+                                                               trigger);
+
+                               if (notif_thread_unregister_ret != LTTNG_OK) {
+                                       /* Return the original error code. */
+                                       ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                                                       trigger_name,
+                                                       (int) trigger_owner,
+                                                       ret_code);
+                               }
+                       }
+                       break;
+               }
+               case LTTNG_DOMAIN_UST:
+                       ust_app_global_update_all_event_notifier_rules();
+                       break;
+               case LTTNG_DOMAIN_NONE:
+                       abort();
+               default:
+               {
+                       /* Agent domains. */
+                       struct agent *agt = agent_find_by_event_notifier_domain(
+                                       trigger_domain);
+
+                       if (!agt) {
+                               agt = agent_create(trigger_domain);
+                               if (!agt) {
+                                       ret_code = LTTNG_ERR_NOMEM;
+                                       goto end_unlock_session_list;
+                               }
+                               agent_add(agt, trigger_agents_ht_by_domain);
+                       }
+
+                       ret_code = trigger_agent_enable(trigger, agt);
+                       if (ret_code != LTTNG_OK) {
+                               goto end_unlock_session_list;
+                       }
+
+                       break;
+               }
+               }
+
+               session_unlock_list();
        }
 
        /*
@@ -4342,21 +4459,38 @@ int cmd_register_trigger(const struct lttng_credentials *cmd_creds,
         * reference to the trigger so the caller doesn't have to care if those
         * are distinct instances or not.
         */
-       lttng_trigger_get(trigger);
-       *return_trigger = trigger;
-
-end_notification_thread:
-       /* Ownership of trigger was transferred. */
-       trigger = NULL;
+       if (ret_code == LTTNG_OK) {
+               lttng_trigger_get(trigger);
+               *return_trigger = trigger;
+               /* Ownership of trigger was transferred to caller. */
+               trigger = NULL;
+       }
 end:
-       return ret;
+       return ret_code;
+end_unlock_session_list:
+       session_unlock_list();
+       return ret_code;
 }
 
-int cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
+enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
                const struct lttng_trigger *trigger,
                struct notification_thread_handle *notification_thread)
 {
-       int ret;
+       enum lttng_error_code ret_code;
+       bool must_update_event_notifiers;
+       const char *trigger_name;
+       uid_t trigger_owner;
+       enum lttng_trigger_status trigger_status;
+
+       trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
+       trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(unnamed)";
+       trigger_status = lttng_trigger_get_owner_uid(
+               trigger, &trigger_owner);
+       assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
+
+       DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
+                       trigger_name, (int) trigger_owner,
+                       (int) lttng_credentials_get_uid(cmd_creds));
 
        /*
         * Validate the trigger credentials against the command credentials.
@@ -4367,17 +4501,84 @@ int cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
                        lttng_trigger_get_credentials(trigger),
                        cmd_creds)) {
                if (lttng_credentials_get_uid(cmd_creds) != 0) {
-                       ERR("Trigger credentials do not match the command credentials");
-                       ret = LTTNG_ERR_INVALID_TRIGGER;
+                       ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
+                                       trigger_name, (int) trigger_owner,
+                                       (int) lttng_credentials_get_uid(cmd_creds));
+                       ret_code = LTTNG_ERR_INVALID_TRIGGER;
                        goto end;
                }
        }
 
-       ret = notification_thread_command_unregister_trigger(notification_thread,
-                       trigger);
+       ret_code = trigger_modifies_event_notifier(trigger, &must_update_event_notifiers);
+       if (ret_code != LTTNG_OK) {
+               ERR("Failed to determine if event modifies event notifiers: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                               trigger_name, (int) trigger_owner, ret_code);
+               goto end;
+       }
+
+       ret_code = notification_thread_command_unregister_trigger(notification_thread,
+                                                                 trigger);
+       if (ret_code != LTTNG_OK) {
+               DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
+                               trigger_name, (int) trigger_owner, ret_code);
+       }
+
+       /*
+        * Synchronize tracers if the trigger removes an event notifier.
+        * Do this even if the trigger unregistration failed to at least stop
+        * the tracers from producing notifications associated with this
+        * event notifier.
+        */
+       if (must_update_event_notifiers) {
+               const enum lttng_domain_type trigger_domain =
+                               lttng_trigger_get_underlying_domain_type_restriction(
+                                               trigger);
+
+               session_lock_list();
+               switch (trigger_domain) {
+               case LTTNG_DOMAIN_KERNEL:
+               {
+                       ret_code = kernel_unregister_event_notifier(
+                                       trigger);
+                       break;
+               }
+               case LTTNG_DOMAIN_UST:
+                       ust_app_global_update_all_event_notifier_rules();
+                       break;
+               case LTTNG_DOMAIN_NONE:
+                       abort();
+               default:
+               {
+                       /* Agent domains. */
+                       struct agent *agt = agent_find_by_event_notifier_domain(
+                                       trigger_domain);
+
+                       if (!agt) {
+                               agt = agent_create(trigger_domain);
+                               if (!agt) {
+                                       ret_code = LTTNG_ERR_NOMEM;
+                                       goto end_unlock_session_list;
+                               }
+                               agent_add(agt, trigger_agents_ht_by_domain);
+                       }
+
+                       ret_code = trigger_agent_disable(trigger, agt);
+                       if (ret_code != LTTNG_OK) {
+                               goto end_unlock_session_list;
+                       }
+
+                       break;
+               }
+               }
+
+               session_unlock_list();
+       }
+
 end:
-       return ret;
-}
+       return ret_code;
+end_unlock_session_list:
+       session_unlock_list();
+       return ret_code;}
 
 int cmd_list_triggers(struct command_ctx *cmd_ctx,
                struct notification_thread_handle *notification_thread,
This page took 0.026687 seconds and 4 git commands to generate.