+
+error:
+ return ret;
+}
+
+/*
+ * set_opt_session_info
+ *
+ * Setup session_name, current_uuid, short_str_uuid and
+ * long_str_uuid using the command line options.
+ */
+static void set_opt_session_info(void)
+{
+ int count, i, short_len;
+ char *tok;
+ struct lttng_session *sessions;
+
+ if (opt_session_uuid != NULL) {
+ short_len = sizeof(short_str_uuid) - 1;
+ /* Shorten uuid */
+ tok = strchr(opt_session_uuid, '.');
+ if (strlen(tok + 1) == short_len) {
+ memcpy(short_str_uuid, tok + 1, short_len);
+ short_str_uuid[short_len] = '\0';
+ }
+
+ /* Get long real uuid_t from session daemon */
+ count = lttng_list_sessions(&sessions);
+ for (i = 0; i < count; i++) {
+ uuid_unparse(sessions[i].uuid, long_str_uuid);
+ if (strncmp(long_str_uuid, short_str_uuid, 8) == 0) {
+ uuid_copy(current_uuid, sessions[i].uuid);
+ break;
+ }
+ }
+ }
+
+ if (opt_session_name != NULL) {
+ session_name = strndup(opt_session_name, NAME_MAX);
+ }
+}
+
+/*
+ * shorten_uuid
+ *
+ * Small function to shorten the 37 bytes long uuid_t
+ * string representation to 8 characters.
+ */
+static void shorten_uuid(char *long_u, char *short_u)
+{
+ memcpy(short_u, long_u, 8);
+ short_u[UUID_SHORT_STR_LEN - 1] = '\0';
+}
+
+/*
+ * set_session_uuid
+ *
+ * Set current session uuid to the current flow of
+ * command(s) using the already shorten uuid or
+ * current full uuid.
+ */
+static int set_session_uuid(void)
+{
+ int ret, count, i;
+ char str_uuid[37];
+ struct lttng_session *sessions;
+
+ if (!uuid_is_null(current_uuid)) {
+ lttng_set_current_session_uuid(¤t_uuid);
+ goto end;
+ }
+
+ count = lttng_list_sessions(&sessions);
+ if (count < 0) {
+ ret = count;
+ goto error;
+ }
+
+ for (i = 0; i < count; i++) {
+ uuid_unparse(sessions[i].uuid, str_uuid);
+ if (strncmp(str_uuid, short_str_uuid, 8) == 0) {
+ lttng_set_current_session_uuid(&sessions[i].uuid);
+ break;
+ }
+ }
+
+ free(sessions);
+
+end:
+ return 0;
+
+error:
+ return ret;