+ DBG("[notification-thread] Newly registered trigger bound to channel \"%s\"",
+ channel->name);
+ }
+end:
+ return ret;
+}
+
+static
+bool is_trigger_action_notify(const struct lttng_trigger *trigger)
+{
+ bool is_notify = false;
+ unsigned int i, count;
+ enum lttng_action_status action_status;
+ const struct lttng_action *action =
+ lttng_trigger_get_const_action(trigger);
+ enum lttng_action_type action_type;
+
+ assert(action);
+ action_type = lttng_action_get_type(action);
+ if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
+ is_notify = true;
+ goto end;
+ } else if (action_type != LTTNG_ACTION_TYPE_GROUP) {
+ goto end;
+ }
+
+ action_status = lttng_action_group_get_count(action, &count);
+ assert(action_status == LTTNG_ACTION_STATUS_OK);
+
+ for (i = 0; i < count; i++) {
+ const struct lttng_action *inner_action =
+ lttng_action_group_get_at_index(
+ action, i);
+
+ action_type = lttng_action_get_type(inner_action);
+ if (action_type == LTTNG_ACTION_TYPE_NOTIFY) {
+ is_notify = true;
+ goto end;
+ }
+ }
+
+end:
+ return is_notify;
+}
+
+static bool trigger_name_taken(struct notification_thread_state *state,
+ const struct lttng_trigger *trigger)
+{
+ struct cds_lfht_iter iter;
+
+ /*
+ * 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;
+ }