+ * No duplicata is allowed in the triggers_by_name_uid_ht.
+ * The match is done against the trigger name and uid.
+ */
+ cds_lfht_lookup(state->triggers_by_name_uid_ht,
+ hash_trigger_by_name_uid(trigger),
+ match_trigger_by_name_uid,
+ trigger,
+ &iter);
+ return !!cds_lfht_iter_get_node(&iter);
+}
+
+static
+enum lttng_error_code generate_trigger_name(
+ struct notification_thread_state *state,
+ struct lttng_trigger *trigger, const char **name)
+{
+ enum lttng_error_code ret_code = LTTNG_OK;
+ bool taken = false;
+ enum lttng_trigger_status status;
+
+ do {
+ const int ret = lttng_trigger_generate_name(trigger,
+ state->trigger_id.name_offset++);
+ if (ret) {
+ /* The only reason this can fail right now. */
+ ret_code = LTTNG_ERR_NOMEM;
+ break;
+ }
+
+ status = lttng_trigger_get_name(trigger, name);
+ assert(status == LTTNG_TRIGGER_STATUS_OK);
+
+ taken = trigger_name_taken(state, trigger);
+ } while (taken || state->trigger_id.name_offset == UINT64_MAX);
+
+ return ret_code;
+}
+
+/*
+ * FIXME A client's credentials are not checked when registering a trigger.
+ *
+ * The effects of this are benign since:
+ * - The client will succeed in registering the trigger, as it is valid,
+ * - The trigger will, internally, be bound to the channel/session,
+ * - The notifications will not be sent since the client's credentials
+ * are checked against the channel at that moment.
+ *
+ * If this function returns a non-zero value, it means something is
+ * fundamentally broken and the whole subsystem/thread will be torn down.
+ *
+ * If a non-fatal error occurs, just set the cmd_result to the appropriate
+ * error code.
+ */
+static
+int handle_notification_thread_command_register_trigger(
+ struct notification_thread_state *state,
+ struct lttng_trigger *trigger,
+ enum lttng_error_code *cmd_result)
+{
+ int ret = 0;
+ struct lttng_condition *condition;
+ struct notification_client *client;
+ struct notification_client_list *client_list = NULL;
+ struct lttng_trigger_ht_element *trigger_ht_element = NULL;
+ struct notification_client_list_element *client_list_element;
+ struct notification_trigger_tokens_ht_element *trigger_tokens_ht_element = NULL;
+ struct cds_lfht_node *node;
+ struct cds_lfht_iter iter;
+ const char* trigger_name;
+ bool free_trigger = true;
+ struct lttng_evaluation *evaluation = NULL;
+ struct lttng_credentials object_creds;
+ uid_t object_uid;
+ gid_t object_gid;
+ enum action_executor_status executor_status;
+ const uint64_t trigger_tracer_token =
+ state->trigger_id.next_tracer_token++;
+
+ rcu_read_lock();
+
+ /* Set the trigger's tracer token. */
+ lttng_trigger_set_tracer_token(trigger, trigger_tracer_token);
+
+ if (lttng_trigger_get_name(trigger, &trigger_name) ==
+ LTTNG_TRIGGER_STATUS_UNSET) {
+ const enum lttng_error_code ret_code = generate_trigger_name(
+ state, trigger, &trigger_name);
+
+ if (ret_code != LTTNG_OK) {
+ /* Fatal error. */
+ ret = -1;
+ *cmd_result = ret_code;
+ goto error;
+ }
+ } else if (trigger_name_taken(state, trigger)) {
+ /* Not a fatal error. */
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ ret = 0;
+ goto error;
+ }
+
+ condition = lttng_trigger_get_condition(trigger);
+ assert(condition);
+
+ /* Some conditions require tracers to implement a minimal ABI version. */
+ if (!condition_is_supported(condition)) {
+ *cmd_result = LTTNG_ERR_NOT_SUPPORTED;
+ goto error;
+ }
+
+ trigger_ht_element = zmalloc(sizeof(*trigger_ht_element));
+ if (!trigger_ht_element) {
+ ret = -1;
+ goto error;
+ }
+
+ /* Add trigger to the trigger_ht. */
+ cds_lfht_node_init(&trigger_ht_element->node);
+ cds_lfht_node_init(&trigger_ht_element->node_by_name_uid);
+ trigger_ht_element->trigger = trigger;
+
+ node = cds_lfht_add_unique(state->triggers_ht,
+ lttng_condition_hash(condition),
+ match_trigger,
+ trigger,
+ &trigger_ht_element->node);
+ if (node != &trigger_ht_element->node) {
+ /* Not a fatal error, simply report it to the client. */
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ goto error_free_ht_element;
+ }
+
+ node = cds_lfht_add_unique(state->triggers_by_name_uid_ht,
+ hash_trigger_by_name_uid(trigger),
+ match_trigger_by_name_uid,
+ trigger,
+ &trigger_ht_element->node_by_name_uid);
+ if (node != &trigger_ht_element->node_by_name_uid) {
+ /* Not a fatal error, simply report it to the client. */
+ cds_lfht_del(state->triggers_ht, &trigger_ht_element->node);
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ goto error_free_ht_element;
+ }
+
+ if (lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT) {
+ trigger_tokens_ht_element = zmalloc(sizeof(*trigger_tokens_ht_element));
+ if (!trigger_tokens_ht_element) {
+ /* Fatal error. */
+ ret = -1;
+ cds_lfht_del(state->triggers_ht,
+ &trigger_ht_element->node);
+ cds_lfht_del(state->triggers_by_name_uid_ht,
+ &trigger_ht_element->node_by_name_uid);
+ goto error_free_ht_element;
+ }
+
+ /* Add trigger token to the trigger_tokens_ht. */
+ cds_lfht_node_init(&trigger_tokens_ht_element->node);
+ trigger_tokens_ht_element->token =
+ LTTNG_OPTIONAL_GET(trigger->tracer_token);
+ trigger_tokens_ht_element->trigger = trigger;
+
+ node = cds_lfht_add_unique(state->trigger_tokens_ht,
+ hash_key_u64(&trigger_tokens_ht_element->token,
+ lttng_ht_seed),
+ match_trigger_token,
+ &trigger_tokens_ht_element->token,
+ &trigger_tokens_ht_element->node);
+ if (node != &trigger_tokens_ht_element->node) {
+ /* Internal corruption, fatal error. */
+ ret = -1;
+ *cmd_result = LTTNG_ERR_TRIGGER_EXISTS;
+ cds_lfht_del(state->triggers_ht,
+ &trigger_ht_element->node);
+ cds_lfht_del(state->triggers_by_name_uid_ht,
+ &trigger_ht_element->node_by_name_uid);
+ goto error_free_ht_element;
+ }
+ }
+
+ /*
+ * Ownership of the trigger and of its wrapper was transfered to
+ * the triggers_ht. Same for token ht element if necessary.
+ */
+ trigger_tokens_ht_element = NULL;
+ trigger_ht_element = NULL;
+ free_trigger = false;
+
+ /*
+ * The rest only applies to triggers that have a "notify" action.
+ * It is not skipped as this is the only action type currently
+ * supported.
+ */
+ if (is_trigger_action_notify(trigger)) {
+ client_list = notification_client_list_create(trigger);
+ if (!client_list) {
+ ret = -1;
+ goto error_free_ht_element;
+ }
+
+ /* Build a list of clients to which this new trigger applies. */
+ cds_lfht_for_each_entry (state->client_socket_ht, &iter, client,
+ client_socket_ht_node) {
+ if (!trigger_applies_to_client(trigger, client)) {
+ continue;
+ }
+
+ client_list_element =
+ zmalloc(sizeof(*client_list_element));
+ if (!client_list_element) {
+ ret = -1;
+ goto error_put_client_list;
+ }
+
+ CDS_INIT_LIST_HEAD(&client_list_element->node);
+ client_list_element->client = client;
+ cds_list_add(&client_list_element->node,
+ &client_list->list);
+ }
+
+ /*
+ * Client list ownership transferred to the
+ * notification_trigger_clients_ht.
+ */
+ publish_notification_client_list(state, client_list);
+ }
+
+ switch (get_condition_binding_object(condition)) {
+ case LTTNG_OBJECT_TYPE_SESSION:
+ /* Add the trigger to the list if it matches a known session. */
+ ret = bind_trigger_to_matching_session(trigger, state);
+ if (ret) {
+ goto error_put_client_list;
+ }
+ break;
+ case LTTNG_OBJECT_TYPE_CHANNEL:
+ /*
+ * Add the trigger to list of triggers bound to the channels
+ * currently known.
+ */
+ ret = bind_trigger_to_matching_channels(trigger, state);
+ if (ret) {
+ goto error_put_client_list;
+ }
+ break;
+ case LTTNG_OBJECT_TYPE_NONE:
+ break;
+ default:
+ ERR("Unknown object type on which to bind a newly registered trigger was encountered");
+ ret = -1;
+ goto error_put_client_list;
+ }
+
+ /*
+ * The new trigger's condition must be evaluated against the current
+ * state.
+ *
+ * In the case of `notify` action, nothing preventing clients from
+ * subscribing to a condition before the corresponding trigger is
+ * registered, we have to evaluate this new condition right away.