+ /* At this point, we have 4 possibilities */
+
+ if (uchan && uevent) { /* Add ctx to event in channel */
+ ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
+ } else if (uchan && !have_event) { /* Add ctx to channel */
+ ret = add_uctx_to_channel(usess, domain, uchan, ctx);
+ } else if (!uchan && have_event) { /* Add ctx to event */
+ /* Add context to event without having the channel name */
+ cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
+ uevent = trace_ust_find_event_by_name(uchan->events, event_name);
+ if (uevent != NULL) {
+ ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
+ /*
+ * LTTng UST does not allowed the same event to be registered
+ * multiple time in different or the same channel. So, if we
+ * found our event, we stop.
+ */
+ goto end;
+ }
+ }
+ ret = LTTCOMM_UST_EVENT_NOT_FOUND;
+ goto error;
+ } else if (!uchan && !have_event) { /* Add ctx all events, all channels */
+ /* For all channels */
+ cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
+ ret = add_uctx_to_channel(usess, domain, uchan, ctx);
+ if (ret < 0) {
+ ERR("Context added to channel %s failed", uchan->name);
+ continue;
+ }
+
+ /* For all events in channel */
+ cds_lfht_for_each_entry(uchan->events->ht, &uiter.iter, uevent,
+ node.node) {
+ ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx);
+ if (ret < 0) {
+ ERR("Context add to event %s in channel %s failed",
+ uevent->attr.name, uchan->name);
+ continue;
+ }
+ }
+ }
+ }
+
+end:
+ switch (ret) {
+ case -EEXIST:
+ ret = LTTCOMM_UST_CONTEXT_EXIST;
+ goto error;
+ case -ENOMEM:
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+
+ return LTTCOMM_OK;