+/*
+ * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
+ */
+static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
+ size_t nb_uri, struct lttng_uri *uris)
+{
+ int ret, i;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+ struct ltt_ust_session *usess = session->ust_session;
+ struct consumer_output *consumer = NULL;
+
+ assert(session);
+ assert(uris);
+ assert(nb_uri > 0);
+
+ /* Can't enable consumer after session started. */
+ if (session->enabled) {
+ ret = LTTCOMM_TRACE_ALREADY_STARTED;
+ goto error;
+ }
+
+ if (!session->start_consumer) {
+ ret = LTTCOMM_NO_CONSUMER;
+ goto error;
+ }
+
+ /*
+ * This case switch makes sure the domain session has a temporary consumer
+ * so the URL can be set.
+ */
+ switch (domain) {
+ case 0:
+ /* Code flow error. A session MUST always have a consumer object */
+ assert(session->consumer);
+ /*
+ * The URL will be added to the tracing session consumer instead of a
+ * specific domain consumer.
+ */
+ consumer = session->consumer;
+ break;
+ case LTTNG_DOMAIN_KERNEL:
+ /* Code flow error if we don't have a kernel session here. */
+ assert(ksess);
+
+ /* Create consumer output if none exists */
+ consumer = ksess->tmp_consumer;
+ if (consumer == NULL) {
+ consumer = consumer_copy_output(ksess->consumer);
+ if (consumer == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ ksess->tmp_consumer = consumer;
+ }
+
+ break;
+ case LTTNG_DOMAIN_UST:
+ /* Code flow error if we don't have a kernel session here. */
+ assert(usess);
+
+ /* Create consumer output if none exists */
+ consumer = usess->tmp_consumer;
+ if (consumer == NULL) {
+ consumer = consumer_copy_output(usess->consumer);
+ if (consumer == NULL) {
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ usess->tmp_consumer = consumer;
+ }
+
+ break;
+ }
+
+ for (i = 0; i < nb_uri; i++) {
+ struct consumer_socket *socket;
+ struct lttng_ht_iter iter;
+
+ ret = add_uri_to_consumer(consumer, &uris[i], domain);
+ if (ret < 0) {
+ goto error;
+ }
+
+ /*
+ * Don't send relayd socket if URI is NOT remote or if the relayd
+ * sockets for the session are already sent.
+ */
+ if (uris[i].dtype == LTTNG_DST_PATH ||
+ consumer->dst.net.relayd_socks_sent) {
+ continue;
+ }
+
+ /* Try to send relayd URI to the consumer if exist. */
+ cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
+ socket, node.node) {
+
+ /* A socket in the HT should never have a negative fd */
+ assert(socket->fd >= 0);
+
+ pthread_mutex_lock(socket->lock);
+ ret = send_socket_relayd_consumer(domain, session, &uris[i],
+ consumer, socket->fd);
+ pthread_mutex_unlock(socket->lock);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+ }
+ }
+
+ /* All good! */
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
+ */
+static int cmd_disable_consumer(int domain, struct ltt_session *session)
+{
+ int ret;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+ struct ltt_ust_session *usess = session->ust_session;
+ struct consumer_output *consumer;
+
+ assert(session);
+
+ if (session->enabled) {
+ /* Can't disable consumer on an already started session */
+ ret = LTTCOMM_TRACE_ALREADY_STARTED;
+ goto error;
+ }
+
+ if (!session->start_consumer) {
+ ret = LTTCOMM_NO_CONSUMER;
+ goto error;
+ }
+
+ switch (domain) {
+ case 0:
+ DBG("Disable tracing session %s consumer", session->name);
+ consumer = session->consumer;
+ break;
+ case LTTNG_DOMAIN_KERNEL:
+ /* Code flow error if we don't have a kernel session here. */
+ assert(ksess);
+
+ DBG("Disabling kernel consumer");
+ consumer = ksess->consumer;
+
+ break;
+ case LTTNG_DOMAIN_UST:
+ /* Code flow error if we don't have a UST session here. */
+ assert(usess);
+
+ DBG("Disabling UST consumer");
+ consumer = usess->consumer;
+
+ break;
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ if (consumer) {
+ consumer->enabled = 0;
+ /* Success at this point */
+ ret = LTTCOMM_OK;
+ } else {
+ ret = LTTCOMM_NO_CONSUMER;
+ }
+
+error:
+ return ret;
+}
+
+/*
+ * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
+ */
+static int cmd_enable_consumer(int domain, struct ltt_session *session)
+{
+ int ret;
+ struct ltt_kernel_session *ksess = session->kernel_session;
+ struct ltt_ust_session *usess = session->ust_session;
+ struct consumer_output *consumer = NULL;
+
+ assert(session);
+
+ /* Can't enable consumer after session started. */
+ if (session->enabled) {
+ ret = LTTCOMM_TRACE_ALREADY_STARTED;
+ goto error;
+ }
+
+ if (!session->start_consumer) {
+ ret = LTTCOMM_NO_CONSUMER;
+ goto error;
+ }
+
+ switch (domain) {
+ case 0:
+ assert(session->consumer);
+ consumer = session->consumer;
+ break;
+ case LTTNG_DOMAIN_KERNEL:
+ /* Code flow error if we don't have a kernel session here. */
+ assert(ksess);
+
+ /*
+ * Check if we have already sent fds to the consumer. In that case,
+ * the enable-consumer command can't be used because a start trace
+ * had previously occured.
+ */
+ if (ksess->consumer_fds_sent) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ goto error;
+ }
+
+ consumer = ksess->tmp_consumer;
+ if (consumer == NULL) {
+ ret = LTTCOMM_OK;
+ /* No temp. consumer output exists. Using the current one. */
+ DBG3("No temporary consumer. Using default");
+ consumer = ksess->consumer;
+ goto error;
+ }
+
+ switch (consumer->type) {
+ case CONSUMER_DST_LOCAL:
+ DBG2("Consumer output is local. Creating directory(ies)");
+
+ /* Create directory(ies) */
+ ret = run_as_mkdir_recursive(consumer->dst.trace_path,
+ S_IRWXU | S_IRWXG, session->uid, session->gid);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ }
+ break;
+ case CONSUMER_DST_NET:
+ DBG2("Consumer output is network. Validating URIs");
+ /* Validate if we have both control and data path set. */
+ if (!consumer->dst.net.control_isset) {
+ ret = LTTCOMM_URL_CTRL_MISS;
+ goto error;
+ }
+
+ if (!consumer->dst.net.data_isset) {
+ ret = LTTCOMM_URL_DATA_MISS;
+ goto error;
+ }
+
+ /* Check established network session state */
+ if (session->net_handle == 0) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ ERR("Session network handle is not set on enable-consumer");
+ goto error;
+ }
+
+ /* Append default kernel trace dir to subdir */
+ strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
+ sizeof(ksess->consumer->subdir));
+
+ break;
+ }
+
+ /*
+ * @session-lock
+ * This is race free for now since the session lock is acquired before
+ * ending up in this function. No other threads can access this kernel
+ * session without this lock hence freeing the consumer output object
+ * is valid.
+ */
+ rcu_read_lock();
+ consumer_destroy_output(ksess->consumer);
+ rcu_read_unlock();
+ ksess->consumer = consumer;
+ ksess->tmp_consumer = NULL;
+
+ break;
+ case LTTNG_DOMAIN_UST:
+ /* Code flow error if we don't have a UST session here. */
+ assert(usess);
+
+ /*
+ * Check if we have already sent fds to the consumer. In that case,
+ * the enable-consumer command can't be used because a start trace
+ * had previously occured.
+ */
+ if (usess->start_trace) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ goto error;
+ }
+
+ consumer = usess->tmp_consumer;
+ if (consumer == NULL) {
+ ret = LTTCOMM_OK;
+ /* No temp. consumer output exists. Using the current one. */
+ DBG3("No temporary consumer. Using default");
+ consumer = usess->consumer;
+ goto error;
+ }
+
+ switch (consumer->type) {
+ case CONSUMER_DST_LOCAL:
+ DBG2("Consumer output is local. Creating directory(ies)");
+
+ /* Create directory(ies) */
+ ret = run_as_mkdir_recursive(consumer->dst.trace_path,
+ S_IRWXU | S_IRWXG, session->uid, session->gid);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ ret = LTTCOMM_FATAL;
+ goto error;
+ }
+ }
+ break;
+ case CONSUMER_DST_NET:
+ DBG2("Consumer output is network. Validating URIs");
+ /* Validate if we have both control and data path set. */
+ if (!consumer->dst.net.control_isset) {
+ ret = LTTCOMM_URL_CTRL_MISS;
+ goto error;
+ }
+
+ if (!consumer->dst.net.data_isset) {
+ ret = LTTCOMM_URL_DATA_MISS;
+ goto error;
+ }
+
+ /* Check established network session state */
+ if (session->net_handle == 0) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ DBG2("Session network handle is not set on enable-consumer");
+ goto error;
+ }
+
+ if (consumer->net_seq_index == -1) {
+ ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
+ DBG2("Network index is not set on the consumer");
+ goto error;
+ }
+
+ /* Append default kernel trace dir to subdir */
+ strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
+ sizeof(usess->consumer->subdir));
+
+ break;
+ }
+
+ /*
+ * @session-lock
+ * This is race free for now since the session lock is acquired before
+ * ending up in this function. No other threads can access this kernel
+ * session without this lock hence freeing the consumer output object
+ * is valid.
+ */
+ rcu_read_lock();
+ consumer_destroy_output(usess->consumer);
+ rcu_read_unlock();
+ usess->consumer = consumer;
+ usess->tmp_consumer = NULL;
+
+ break;
+ }
+
+ /* Enable it */
+ if (consumer) {
+ consumer->enabled = 1;
+ /* Success at this point */
+ ret = LTTCOMM_OK;
+ } else {
+ /* Should not really happend... */
+ ret = LTTCOMM_NO_CONSUMER;
+ }
+
+error:
+ return ret;
+}
+