+ DBG3("Copying tracing session consumer output in kernel session");
+ session->kernel_session->consumer =
+ consumer_copy_output(session->consumer);
+ /* Ease our life a bit for the next part */
+ consumer = session->kernel_session->consumer;
+ dir_name = DEFAULT_KERNEL_TRACE_DIR;
+ break;
+ case LTTNG_DOMAIN_UST:
+ DBG3("Copying tracing session consumer output in UST session");
+ session->ust_session->consumer =
+ consumer_copy_output(session->consumer);
+ /* Ease our life a bit for the next part */
+ consumer = session->ust_session->consumer;
+ dir_name = DEFAULT_UST_TRACE_DIR;
+ break;
+ default:
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ /* Append correct directory to subdir */
+ strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
+ DBG3("Copy session consumer subdir %s", consumer->subdir);
+
+ /* Add default trace directory name */
+ if (consumer->type == CONSUMER_DST_LOCAL) {
+ strncat(consumer->dst.trace_path, dir_name,
+ sizeof(consumer->dst.trace_path));
+ }
+
+ ret = LTTCOMM_OK;
+
+error:
+ return ret;
+}
+
+/*
+ * Create an UST session and add it to the session ust list.
+ */
+static int create_ust_session(struct ltt_session *session,
+ struct lttng_domain *domain)
+{
+ int ret;
+ struct ltt_ust_session *lus = NULL;
+
+ assert(session);
+ assert(session->consumer);
+
+ switch (domain->type) {
+ case LTTNG_DOMAIN_UST:
+ break;
+ default:
+ ERR("Unknown UST domain on create session %d", domain->type);
+ ret = LTTCOMM_UNKNOWN_DOMAIN;
+ goto error;
+ }
+
+ DBG("Creating UST session");
+
+ lus = trace_ust_create_session(session->path, session->id, domain);
+ if (lus == NULL) {
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+
+ if (session->consumer->type == CONSUMER_DST_LOCAL) {
+ ret = run_as_mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
+ session->uid, session->gid);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ ret = LTTCOMM_UST_SESS_FAIL;
+ goto error;
+ }
+ }
+ }
+
+ lus->uid = session->uid;
+ lus->gid = session->gid;
+ session->ust_session = lus;
+
+ /* Copy session output to the newly created UST session */
+ ret = copy_session_consumer(domain->type, session);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ return LTTCOMM_OK;
+
+error:
+ free(lus);
+ session->ust_session = NULL;
+ return ret;
+}
+
+/*
+ * Create a kernel tracer session then create the default channel.
+ */
+static int create_kernel_session(struct ltt_session *session)
+{
+ int ret;
+
+ DBG("Creating kernel session");
+
+ ret = kernel_create_session(session, kernel_tracer_fd);
+ if (ret < 0) {
+ ret = LTTCOMM_KERN_SESS_FAIL;
+ goto error;
+ }
+
+ /* Set kernel consumer socket fd */
+ if (kconsumer_data.cmd_sock >= 0) {
+ session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
+ }
+
+ /* Copy session output to the newly created Kernel session */
+ ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
+ if (ret != LTTCOMM_OK) {
+ goto error;
+ }
+
+ /* Create directory(ies) on local filesystem. */
+ if (session->consumer->type == CONSUMER_DST_LOCAL) {
+ ret = run_as_mkdir_recursive(
+ session->kernel_session->consumer->dst.trace_path,
+ S_IRWXU | S_IRWXG, session->uid, session->gid);
+ if (ret < 0) {
+ if (ret != -EEXIST) {
+ ERR("Trace directory creation error");
+ goto error;
+ }
+ }
+ }
+
+ session->kernel_session->uid = session->uid;
+ session->kernel_session->gid = session->gid;
+
+ return LTTCOMM_OK;
+
+error:
+ trace_kernel_destroy_session(session->kernel_session);
+ session->kernel_session = NULL;
+ return ret;
+}
+
+/*
+ * Check if the UID or GID match the session. Root user has access to all
+ * sessions.
+ */
+static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
+{
+ if (uid != session->uid && gid != session->gid && uid != 0) {
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+/*
+ * Count number of session permitted by uid/gid.
+ */
+static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
+{
+ unsigned int i = 0;
+ struct ltt_session *session;
+
+ DBG("Counting number of available session for UID %d GID %d",
+ uid, gid);
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ /*
+ * Only list the sessions the user can control.
+ */
+ if (!session_access_ok(session, uid, gid)) {
+ continue;
+ }
+ i++;
+ }
+ return i;
+}
+
+/*
+ * Using the session list, filled a lttng_session array to send back to the
+ * client for session listing.
+ *
+ * The session list lock MUST be acquired before calling this function. Use
+ * session_lock_list() and session_unlock_list().
+ */
+static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
+ gid_t gid)
+{
+ unsigned int i = 0;
+ struct ltt_session *session;
+
+ DBG("Getting all available session for UID %d GID %d",
+ uid, gid);
+ /*
+ * Iterate over session list and append data after the control struct in
+ * the buffer.
+ */
+ cds_list_for_each_entry(session, &session_list_ptr->head, list) {
+ /*
+ * Only list the sessions the user can control.
+ */
+ if (!session_access_ok(session, uid, gid)) {
+ continue;
+ }
+ strncpy(sessions[i].path, session->path, PATH_MAX);
+ sessions[i].path[PATH_MAX - 1] = '\0';
+ strncpy(sessions[i].name, session->name, NAME_MAX);
+ sessions[i].name[NAME_MAX - 1] = '\0';
+ sessions[i].enabled = session->enabled;
+ i++;
+ }
+}
+
+/*
+ * Fill lttng_channel array of all channels.
+ */
+static void list_lttng_channels(int domain, struct ltt_session *session,
+ struct lttng_channel *channels)
+{
+ int i = 0;
+ struct ltt_kernel_channel *kchan;
+
+ DBG("Listing channels for session %s", session->name);
+
+ switch (domain) {
+ case LTTNG_DOMAIN_KERNEL:
+ /* Kernel channels */
+ if (session->kernel_session != NULL) {
+ cds_list_for_each_entry(kchan,