+ int ret = 0;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+ struct lttng_session_trigger_list *trigger_list;
+ struct lttng_trigger_list_element *trigger_list_element;
+ struct session_info *session_info;
+
+ rcu_read_lock();
+
+ session_info = find_or_create_session_info(state, session_name,
+ session_uid, session_gid);
+ if (!session_info) {
+ /* Allocation error or an internal error occurred. */
+ ret = -1;
+ cmd_result = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ session_info->rotation.ongoing =
+ cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING;
+ session_info->rotation.id = trace_archive_chunk_id;
+ trigger_list = get_session_trigger_list(state, session_name);
+ if (!trigger_list) {
+ DBG("[notification-thread] No triggers applying to session \"%s\" found",
+ session_name);
+ goto end;
+ }
+
+ cds_list_for_each_entry(trigger_list_element, &trigger_list->list,
+ node) {
+ const struct lttng_condition *condition;
+ const struct lttng_action *action;
+ const struct lttng_trigger *trigger;
+ struct notification_client_list *client_list;
+ struct lttng_evaluation *evaluation = NULL;
+ enum lttng_condition_type condition_type;
+ bool client_list_is_empty;
+
+ trigger = trigger_list_element->trigger;
+ condition = lttng_trigger_get_const_condition(trigger);
+ assert(condition);
+ condition_type = lttng_condition_get_type(condition);
+
+ if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING &&
+ cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
+ continue;
+ } else if (condition_type == LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED &&
+ cmd_type != NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_COMPLETED) {
+ continue;
+ }
+
+ action = lttng_trigger_get_const_action(trigger);
+
+ /* Notify actions are the only type currently supported. */
+ assert(lttng_action_get_type_const(action) ==
+ LTTNG_ACTION_TYPE_NOTIFY);
+
+ client_list = get_client_list_from_condition(state, condition);
+ assert(client_list);
+
+ pthread_mutex_lock(&client_list->lock);
+ client_list_is_empty = cds_list_empty(&client_list->list);
+ pthread_mutex_unlock(&client_list->lock);
+ if (client_list_is_empty) {
+ /*
+ * No clients interested in the evaluation's result,
+ * skip it.
+ */
+ continue;
+ }
+
+ if (cmd_type == NOTIFICATION_COMMAND_TYPE_SESSION_ROTATION_ONGOING) {
+ evaluation = lttng_evaluation_session_rotation_ongoing_create(
+ trace_archive_chunk_id);
+ } else {
+ evaluation = lttng_evaluation_session_rotation_completed_create(
+ trace_archive_chunk_id, location);
+ }
+
+ if (!evaluation) {
+ /* Internal error */
+ ret = -1;
+ cmd_result = LTTNG_ERR_UNK;
+ goto put_list;
+ }
+
+ /* Dispatch evaluation result to all clients. */
+ ret = send_evaluation_to_clients(trigger_list_element->trigger,
+ evaluation, client_list, state,
+ session_info->uid,
+ session_info->gid);
+ lttng_evaluation_destroy(evaluation);
+put_list:
+ notification_client_list_put(client_list);
+ if (caa_unlikely(ret)) {
+ break;
+ }
+ }
+end:
+ session_info_put(session_info);
+ *_cmd_result = cmd_result;
+ rcu_read_unlock();
+ return ret;
+}
+
+static
+int handle_notification_thread_command_add_application(
+ struct notification_thread_handle *handle,
+ struct notification_thread_state *state,
+ int read_side_trigger_event_application_pipe,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+ struct notification_event_trigger_source_element *element = NULL;
+
+ element = zmalloc(sizeof(*element));
+ if (!element) {
+ cmd_result = LTTNG_ERR_NOMEM;
+ ret = -1;
+ goto end;
+ }
+
+ CDS_INIT_LIST_HEAD(&element->node);
+ element->fd = read_side_trigger_event_application_pipe;
+
+ pthread_mutex_lock(&handle->event_trigger_sources.lock);
+ cds_list_add(&element->node, &handle->event_trigger_sources.list);
+ pthread_mutex_unlock(&handle->event_trigger_sources.lock);
+
+ /* TODO: remove on failure to add to list? */
+
+ /* Adding the read side pipe to the event poll */
+ ret = lttng_poll_add(&state->events,
+ read_side_trigger_event_application_pipe,
+ LPOLLIN | LPOLLERR);
+
+ DBG3("[notification-thread] Adding application event source from fd: %d", read_side_trigger_event_application_pipe);
+ if (ret < 0) {
+ /* TODO: what should be the value of cmd_result??? */
+ ERR("[notification-thread] Failed to add event source pipe fd to pollset");
+ goto end;
+ }
+
+end:
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static
+int handle_notification_thread_command_remove_application(
+ struct notification_thread_handle *handle,
+ struct notification_thread_state *state,
+ int read_side_trigger_event_application_pipe,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+
+ /* TODO: missing a lock propably to revisit */
+ struct notification_event_trigger_source_element *source_element, *tmp;
+ cds_list_for_each_entry_safe(source_element, tmp,
+ &handle->event_trigger_sources.list, node) {
+ if (source_element->fd != read_side_trigger_event_application_pipe) {
+ continue;
+ }
+
+ DBG("[notification-thread] Removed event source from event source list");
+ cds_list_del(&source_element->node);
+ break;
+ }
+
+ DBG3("[notification-thread] Removing application event source from fd: %d", read_side_trigger_event_application_pipe);
+ /* Removing the read side pipe to the event poll */
+ ret = lttng_poll_del(&state->events,
+ read_side_trigger_event_application_pipe);
+ if (ret < 0) {
+ /* TODO: what should be the value of cmd_result??? */
+ ERR("[notification-thread] Failed to remove event source pipe fd from pollset");
+ goto end;
+ }
+
+end:
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static int handle_notification_thread_command_get_tokens(
+ struct notification_thread_handle *handle,
+ struct notification_thread_state *state,
+ struct lttng_triggers **triggers,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0, i = 0;
+ enum lttng_error_code cmd_result = LTTNG_OK;
+ struct cds_lfht_iter iter;
+ struct notification_trigger_tokens_ht_element *element;
+ struct lttng_triggers *local_triggers = NULL;
+
+ local_triggers = lttng_triggers_create();
+ if (!local_triggers) {
+ cmd_result = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ rcu_read_lock();
+ cds_lfht_for_each_entry (
+ state->trigger_tokens_ht, &iter, element, node) {
+ ret = lttng_triggers_add(local_triggers, element->trigger);
+ if (ret < 0) {
+ cmd_result = LTTNG_ERR_FATAL;
+ ret = -1;
+ goto end;
+ }
+
+ /* Ownership is shared with the lttng_triggers object */
+ lttng_trigger_get(element->trigger);
+
+ i++;
+ }
+
+ /* Passing ownership up */
+ *triggers = local_triggers;
+ local_triggers = NULL;
+
+end:
+ rcu_read_unlock();
+ lttng_triggers_destroy(local_triggers);
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static
+int handle_notification_thread_command_list_triggers(
+ struct notification_thread_handle *handle,
+ struct notification_thread_state *state,
+ uid_t uid,
+ gid_t gid,
+ struct lttng_triggers **triggers,
+ enum lttng_error_code *_cmd_result)
+{
+ int ret = 0, i = 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;
+
+ long scb, sca;
+ unsigned long count;
+
+ rcu_read_lock();
+ cds_lfht_count_nodes(state->triggers_ht, &scb, &count, &sca);
+
+ /* TODO check downcasting */
+ local_triggers = lttng_triggers_create();
+ if (!local_triggers) {
+ cmd_result = LTTNG_ERR_NOMEM;
+ goto end;
+ }
+
+ cds_lfht_for_each_entry (state->triggers_ht, &iter,
+ trigger_ht_element, node) {
+ /* Only return the trigger for which the requestion client have
+ * access. For now the root user can only list its own
+ * triggers.
+ * TODO: root user behavior
+ */
+ creds = lttng_trigger_get_credentials(trigger_ht_element->trigger);
+ if ((uid != creds->uid) || (gid != creds->gid)) {
+ continue;
+ }
+
+ ret = lttng_triggers_add(local_triggers, trigger_ht_element->trigger);
+ if (ret < 0) {
+ ret = -1;
+ goto end;
+ }
+ /* Ownership is shared with the lttng_triggers object */
+ lttng_trigger_get(trigger_ht_element->trigger);
+
+ i++;
+ }
+
+ /* Passing ownership up */
+ *triggers = local_triggers;
+ local_triggers = NULL;
+
+end:
+ rcu_read_unlock();
+ lttng_triggers_destroy(local_triggers);
+ *_cmd_result = cmd_result;
+ return ret;
+}
+
+static
+int condition_is_supported(struct lttng_condition *condition)
+{
+ int ret;