+ ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
+ if (ret < 0) {
+ ERR("[notification-thread] Error creating lttng_poll_event");
+ goto end;
+ }
+
+ ret = lttng_poll_add(&events, pipe, LPOLLIN);
+ if (ret < 0) {
+ ERR("[notification-thread] Error adding fd event notifier notification pipe to lttng_poll_event: fd = %d",
+ pipe);
+ goto end;
+ }
+
+ while (true) {
+ /*
+ * Continue to consume notifications as long as there are new
+ * ones coming in. The tracer has been asked to stop producing
+ * them.
+ *
+ * LPOLLIN is explicitly checked since LPOLLHUP is implicitly
+ * monitored (on Linux, at least) and will be returned when
+ * the pipe is closed but empty.
+ */
+ ret = lttng_poll_wait_interruptible(&events, 0);
+ if (ret == 0 || (LTTNG_POLL_GETEV(&events, 0) & LPOLLIN) == 0) {
+ /* No more notification to be read on this pipe. */
+ ret = 0;
+ goto end;
+ } else if (ret < 0) {
+ PERROR("Failed on lttng_poll_wait_interruptible() call");
+ ret = -1;
+ goto end;
+ }
+
+ ret = handle_one_event_notifier_notification(state, pipe, domain);
+ if (ret) {
+ ERR("[notification-thread] Error consuming an event notifier notification from pipe: fd = %d",
+ pipe);
+ }
+ }
+end:
+ lttng_poll_clean(&events);
+ return ret;
+}
+
+static
+int handle_notification_thread_command_remove_tracer_event_source(
+ struct notification_thread_state *state,
+ int tracer_event_source_fd,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0;
+ bool found = false;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+ struct notification_event_tracer_event_source_element *source_element = NULL, *tmp;
+
+ cds_list_for_each_entry_safe(source_element, tmp,
+ &state->tracer_event_sources_list, node) {
+ if (source_element->fd != tracer_event_source_fd) {
+ continue;
+ }
+
+ DBG("[notification-thread] Removed tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
+ tracer_event_source_fd,
+ lttng_domain_type_str(source_element->domain));
+ cds_list_del(&source_element->node);
+ found = true;
+ break;
+ }
+
+ if (!found) {
+ /*
+ * This is temporarily allowed since the poll activity set is
+ * not properly cleaned-up for the moment. This is adressed in
+ * an upcoming fix.
+ */
+ source_element = NULL;
+ goto end;
+ }
+
+ if (!source_element->is_fd_in_poll_set) {
+ /* Skip the poll set removal. */
+ goto end;
+ }
+
+ DBG3("[notification-thread] Removing tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
+ tracer_event_source_fd,
+ lttng_domain_type_str(source_element->domain));
+
+ /* Removing the fd from the event poll set. */
+ ret = lttng_poll_del(&state->events, tracer_event_source_fd);
+ if (ret < 0) {
+ ERR("[notification-thread] Failed to remove tracer event source from poll set: tracer_event_source_fd = %d, domain = '%s'",
+ tracer_event_source_fd,
+ lttng_domain_type_str(source_element->domain));
+ cmd_result = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ source_element->is_fd_in_poll_set = false;
+
+ ret = drain_event_notifier_notification_pipe(state, tracer_event_source_fd,
+ source_element->domain);
+ if (ret) {
+ ERR("[notification-thread] Error draining event notifier notification: tracer_event_source_fd = %d, domain = %s",
+ tracer_event_source_fd,
+ lttng_domain_type_str(source_element->domain));
+ cmd_result = LTTNG_ERR_FATAL;
+ goto end;
+ }
+
+ /*
+ * The drain_event_notifier_notification_pipe() call might have read
+ * data from an fd that we received in event in the latest _poll_wait()
+ * call. Make sure the thread call poll_wait() again to ensure we have
+ * a clean state.
+ */
+ state->restart_poll = true;
+
+end:
+ free(source_element);
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static
+int condition_on_event_update_error_count(struct lttng_trigger *trigger)
+{
+ int ret = 0;
+ uint64_t error_count = 0;
+ struct lttng_condition *condition;
+ enum event_notifier_error_accounting_status status;
+
+ condition = lttng_trigger_get_condition(trigger);
+ assert(lttng_condition_get_type(condition) ==
+ LTTNG_CONDITION_TYPE_ON_EVENT);
+
+ status = event_notifier_error_accounting_get_count(trigger, &error_count);
+ if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
+ uid_t trigger_owner_uid;
+ const char *trigger_name;
+ const enum lttng_trigger_status trigger_status =
+ lttng_trigger_get_owner_uid(
+ trigger, &trigger_owner_uid);
+
+ assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
+ if (lttng_trigger_get_name(trigger, &trigger_name) != LTTNG_TRIGGER_STATUS_OK) {
+ trigger_name = "(unnamed)";
+ }
+
+ ERR("Failed to get event notifier error count of trigger for update: trigger owner = %d, trigger name = '%s'",
+ trigger_owner_uid, trigger_name);
+ ret = -1;
+ }
+
+ lttng_condition_on_event_set_error_count(condition, error_count);
+ return ret;
+}
+
+int handle_notification_thread_remove_tracer_event_source_no_result(
+ struct notification_thread_state *state,
+ int tracer_event_source_fd)
+{
+ int ret;
+ enum lttng_error_code cmd_result;
+
+ ret = handle_notification_thread_command_remove_tracer_event_source(
+ state, tracer_event_source_fd, &cmd_result);
+ (void) cmd_result;
+ return ret;
+}
+
+static
+bool action_type_needs_tracer_notifier(enum lttng_action_type action_type)
+{
+ switch (action_type) {
+ case LTTNG_ACTION_TYPE_NOTIFY:
+ case LTTNG_ACTION_TYPE_START_SESSION:
+ case LTTNG_ACTION_TYPE_STOP_SESSION:
+ case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
+ case LTTNG_ACTION_TYPE_ROTATE_SESSION:
+ return true;
+ case LTTNG_ACTION_TYPE_GROUP:
+ case LTTNG_ACTION_TYPE_UNKNOWN:
+ default:
+ abort();
+ }
+}
+
+static
+bool action_needs_tracer_notifier(const struct lttng_action *action)
+{
+ bool needs_tracer_notifier = false;
+ unsigned int i, count;
+ enum lttng_action_status action_status;
+ enum lttng_action_type action_type;
+
+ assert(action);
+ /* If there is only one action. Check if it needs a tracer notifier. */
+ action_type = lttng_action_get_type(action);
+ if (action_type != LTTNG_ACTION_TYPE_GROUP) {
+ needs_tracer_notifier = action_type_needs_tracer_notifier(
+ action_type);
+ goto end;
+ }
+
+ /*
+ * Iterate over all the actions of the action group and check if any of
+ * them needs a tracer notifier.
+ */
+ 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_needs_tracer_notifier(action_type)) {
+ needs_tracer_notifier = true;
+ goto end;
+ }
+ }
+
+end:
+ return needs_tracer_notifier;
+}
+
+/*
+ * A given trigger needs a tracer notifier if
+ * it has an event-rule condition,
+ * AND
+ * it has one or more sessiond-execution action.
+ */
+static
+bool trigger_needs_tracer_notifier(const struct lttng_trigger *trigger)
+{
+ bool needs_tracer_notifier = false;
+ const struct lttng_condition *condition =
+ lttng_trigger_get_const_condition(trigger);
+ const struct lttng_action *action =
+ lttng_trigger_get_const_action(trigger);
+
+ switch (lttng_condition_get_type(condition)) {
+ case LTTNG_CONDITION_TYPE_ON_EVENT:
+ needs_tracer_notifier = action_needs_tracer_notifier(action);
+ goto end;
+ case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
+ case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
+ case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
+ case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
+ case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
+ goto end;
+ case LTTNG_CONDITION_TYPE_UNKNOWN:
+ default:
+ abort();
+ }
+end:
+ return needs_tracer_notifier;
+}
+
+static int handle_notification_thread_command_list_triggers(
+ struct notification_thread_handle *handle,
+ struct notification_thread_state *state,
+ uid_t client_uid,
+ struct lttng_triggers **triggers,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+ struct cds_lfht_iter iter;
+ struct lttng_trigger_ht_element *trigger_ht_element;
+ struct lttng_triggers *local_triggers = NULL;
+ const struct lttng_credentials *creds;
+
+ rcu_read_lock();
+
+ local_triggers = lttng_triggers_create();
+ if (!local_triggers) {
+ /* Not a fatal error. */
+ cmd_result = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ cds_lfht_for_each_entry(state->triggers_ht, &iter,
+ trigger_ht_element, node) {
+ /*
+ * Only return the triggers to which the client has access.
+ * The root user has visibility over all triggers.
+ */
+ creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
+ if (client_uid != lttng_credentials_get_uid(creds) && client_uid != 0) {
+ continue;
+ }
+
+ if (trigger_needs_tracer_notifier(trigger_ht_element->trigger)) {
+ ret = condition_on_event_update_error_count(
+ trigger_ht_element->trigger);
+ assert(!ret);
+ }
+
+ ret = lttng_triggers_add(local_triggers,
+ trigger_ht_element->trigger);
+ if (ret < 0) {
+ /* Not a fatal error. */
+ ret = 0;
+ cmd_result = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+ }
+
+ /* Transferring ownership to the caller. */
+ *triggers = local_triggers;
+ local_triggers = NULL;
+
+end:
+ rcu_read_unlock();
+ lttng_triggers_destroy(local_triggers);
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static
+bool condition_is_supported(struct lttng_condition *condition)
+{
+ bool is_supported;
+