Implement --default-output option of create command
[lttng-tools.git] / src / bin / lttng / commands / create.c
index 60f3d7c685b29e266bb94ae3c784fa338686a28f..75edd2bee22db679925db1e88ecd382f7f6c4977 100644 (file)
@@ -52,20 +52,24 @@ static int opt_no_consumer;
 static int opt_no_output;
 static int opt_snapshot;
 static unsigned int opt_live_timer;
+static bool opt_default_name;
+static bool opt_default_output;
 
 enum {
        OPT_HELP = 1,
        OPT_LIST_OPTIONS,
        OPT_LIVE_TIMER,
+       OPT_DEFAULT_NAME,
+       OPT_DEFAULT_OUTPUT,
 };
 
-enum {
+enum output_type {
        OUTPUT_UNKNOWN = -1,
        OUTPUT_NONE,
        OUTPUT_LOCAL,
        OUTPUT_NET,
 };
-enum {
+enum session_type {
        SESSION_UNKNOWN = -1,
        SESSION_NORMAL,
        SESSION_LIVE,
@@ -89,6 +93,8 @@ static struct poptOption long_options[] = {
        {"live",            0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
        {"shm-path",        0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
        {"template-path",        0, POPT_ARG_STRING, &opt_template_path, 0, 0, 0},
+       {"default-name",    0, POPT_ARG_NONE, NULL, OPT_DEFAULT_NAME, 0, 0},
+       {"default-output",  0, POPT_ARG_NONE, NULL, OPT_DEFAULT_OUTPUT, 0, 0},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -228,7 +234,7 @@ error_create:
  * CMD_ERROR on error
  * CMD_SUCCESS on success
  */
-static int validate_command_options(int session_type)
+static int validate_command_options(enum session_type type)
 {
        int ret = CMD_SUCCESS;
 
@@ -248,14 +254,26 @@ static int validate_command_options(int session_type)
                /* Restriction on flags exist when using template */
                /* Session type flags are not permitted */
                /* --live & --snapshot */
-               if ((opt_live_timer && session_type != SESSION_LIVE) ||
-                               (opt_snapshot && session_type != SESSION_SNAPSHOT)) {
+               if ((opt_live_timer && type != SESSION_LIVE) ||
+                               (opt_snapshot && type != SESSION_SNAPSHOT)) {
                        ERR("It is not possible to change the session type of a template");
                        ret = CMD_ERROR;
                        goto error;
                }
        }
 
+       if (opt_session_name && opt_default_name) {
+               ERR("A session name must not be provided if the --default-name option is used");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       if (opt_default_output && (opt_data_url || opt_ctrl_url || opt_url ||
+                       opt_output_path)) {
+               ERR("The --default-output option may not be used with the --output/-o, --set-url/-U, --ctrl-url/-C, or --data-url/-D options");
+               ret = CMD_ERROR;
+               goto error;
+       }
 error:
        return ret;
 }
@@ -267,9 +285,9 @@ error:
  * value on command errors.
  */
 static int create_session_basic (const char *session_name,
-               int session_type,
+               enum session_type session_type,
                int live_timer,
-               int output_type,
+               enum output_type output_type,
                const char* url,
                const char* ctrl_url,
                const char* data_url,
@@ -385,9 +403,9 @@ error:
 }
 
 static int generate_output(const char *session_name,
-               int session_type,
+               enum session_type session_type,
                int live_timer,
-               int output_type,
+               enum output_type output_type,
                const char* url,
                const char* ctrl_url,
                const char* data_url,
@@ -450,15 +468,16 @@ error:
 
 static int parse_template (struct config_document *template,
                char **session_name,
-               int *session_type,
+               enum session_type *session_type,
                int *live_timer,
-               int *output_type,
+               enum output_type *output_type,
                char **url,
                char **ctrl_url,
                char **data_url,
                char **shm_path)
 {
        int ret = 0;
+       int printed_bytes;
        char *raw_value = NULL;
 
        assert(template);
@@ -495,8 +514,8 @@ static int parse_template (struct config_document *template,
 
                        if (strlen(raw_value) > 0) {
                                *output_type = OUTPUT_LOCAL;
-                               ret = asprintf(url, "file://%s", raw_value);
-                               if (ret < 0) {
+                               printed_bytes = asprintf(url, "file://%s", raw_value);
+                               if (printed_bytes < 0) {
                                        ret = -1;
                                        goto error;
                                }
@@ -532,8 +551,8 @@ static int parse_template (struct config_document *template,
 
                        if (strlen(raw_value) > 0) {
                                *output_type = OUTPUT_LOCAL;
-                               ret = asprintf(url, "file://%s", raw_value);
-                               if (ret < 0) {
+                               printed_bytes = asprintf(url, "file://%s", raw_value);
+                               if (printed_bytes < 0) {
                                        ret = -1;
                                        goto error;
                                }
@@ -556,6 +575,9 @@ static int parse_template (struct config_document *template,
                        /* There is no output definition */
                }
                break;
+       case SESSION_UNKNOWN:
+               ret = -1;
+               goto error;
        }
 
 
@@ -569,9 +591,9 @@ error:
 }
 static int create_session_from_template(struct config_document *template,
                const char *session_name,
-               int session_type,
+               enum session_type session_type,
                int live_timer,
-               int output_type,
+               enum output_type output_type,
                const char *url,
                const char *ctrl_url,
                const char *data_url,
@@ -579,6 +601,7 @@ static int create_session_from_template(struct config_document *template,
                const char *datetime)
 {
        int ret = CMD_SUCCESS;
+       int printed_bytes;
        struct config_element *temp_element = NULL;
        struct config_element *temp_element_child = NULL;
        char tmp_ctrl_uri[PATH_MAX];
@@ -619,8 +642,21 @@ static int create_session_from_template(struct config_document *template,
         */
        if (session_type == SESSION_LIVE) {
                if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
-                       asprintf(&tmp_string, "%d", live_timer);
-                       config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string);
+                       printed_bytes = asprintf(&tmp_string, "%d", live_timer);
+                       if (printed_bytes < 0) {
+                               ERR("Asprintf failed for live timer");
+                               ret = CMD_ERROR;
+                               goto error;
+                       }
+                       ret = config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string);
+
+                       if (ret) {
+                               printf("error: %d\n", ret);
+                               ERR("Replacement of live_timer_interval failed");
+                               ret = CMD_ERROR;
+                               goto error;
+                       }
+
                        free(tmp_string);
                        tmp_string = NULL;
                } else {
@@ -704,7 +740,9 @@ static int create_session_from_template(struct config_document *template,
                        ret = CMD_ERROR;
                        goto error;
                }
+
                config_element_free(temp_element);
+               temp_element = NULL;
 
                temp_element = config_element_create("data_uri", tmp_data_uri);
 
@@ -721,6 +759,7 @@ static int create_session_from_template(struct config_document *template,
                        goto error;
                }
                config_element_free(temp_element);
+               temp_element = NULL;
                break;
        default:
                ret = CMD_ERROR;
@@ -741,6 +780,63 @@ static int create_session_from_template(struct config_document *template,
                goto error;
        }
 
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+       /* Construct the output/consumer_output/ node*/
+       temp_element_child = temp_element;
+       temp_element = config_element_create("consumer_output", NULL);
+       if (!temp_element) {
+               ERR("Could not create consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append output data to the consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = config_element_create("enabled", "true");
+       if (!temp_element_child) {
+               ERR("Could not create enbaled node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append node to the consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+
+       temp_element_child = temp_element;
+       temp_element = config_element_create("output", NULL);
+       if (!temp_element) {
+               ERR("Could not create output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append output data to the output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+
        /*
         * validate and replace the destination node for each session type
         * TODO: export string as const and simply assign a base path for the
@@ -749,22 +845,44 @@ static int create_session_from_template(struct config_document *template,
        switch (session_type) {
        case SESSION_NORMAL:
        case SESSION_LIVE:
-               if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")) {
-                       ERR("Invalid template no destination node configuration present");
+               break;
+       case SESSION_SNAPSHOT:
+               /* construct the output/snapshots_outputs/ */
+               temp_element_child = temp_element;
+               temp_element = config_element_create("snapshot_outputs", NULL);
+               if (!temp_element) {
+                       ERR("Could not create snapshot_outputs node configuration");
                        ret = CMD_ERROR;
                        goto error;
                }
 
-               ret = config_document_replace_element(template, "/sessions/session/output/consumer_output/destination", temp_element);
-               break;
-       case SESSION_SNAPSHOT:
-               if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")) {
-                       ERR("Invalid template no destination node configuration present");
+               ret = config_element_add_child(temp_element, temp_element_child);
+               if (ret) {
+                       ERR("Could not append output data to the snapshot_outputs node configuration");
+                       ret = CMD_ERROR;
+                       goto error;
+               }
+
+               config_element_free(temp_element_child);
+               temp_element_child = NULL;
+
+               temp_element_child = temp_element;
+               temp_element = config_element_create("output", NULL);
+               if (!temp_element) {
+                       ERR("Could not create output node configuration");
+                       ret = CMD_ERROR;
+                       goto error;
+               }
+
+               ret = config_element_add_child(temp_element, temp_element_child);
+               if (ret) {
+                       ERR("Could not append output data to the output node configuration");
                        ret = CMD_ERROR;
                        goto error;
                }
 
-               ret = config_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element);
+               config_element_free(temp_element_child);
+               temp_element_child = NULL;
                break;
        default:
                ERR("Invalid session type");
@@ -772,6 +890,14 @@ static int create_session_from_template(struct config_document *template,
                goto error;
        }
 
+       if (!config_document_element_exist(template, "/sessions/session/output")) {
+               ret = config_document_insert_element(template, "/sessions/session", temp_element);
+       } else {
+               ret = config_document_replace_element(template, "/sessions/session/output", temp_element);
+       }
+
+       config_element_free(temp_element);
+       temp_element = NULL;
 
        if (ret) {
                ERR("%s", lttng_strerror(ret));
@@ -798,6 +924,9 @@ static int create_session_from_template(struct config_document *template,
                        ret = CMD_ERROR;
                        goto error;
                }
+
+               config_element_free(temp_element);
+               temp_element = NULL;
        }
 
        ret = config_load_configuration_sessions(template, session_name, 0);
@@ -821,14 +950,15 @@ error:
 static int create_session(void)
 {
        int ret;
+       int printed_bytes;
 
 
        /* Template */
        struct config_document *template = NULL;
 
        /* Base data */
-       int base_session_type = SESSION_UNKNOWN;
-       int base_output_type = OUTPUT_UNKNOWN;
+       enum session_type base_session_type = SESSION_UNKNOWN;
+       enum output_type base_output_type = OUTPUT_UNKNOWN;
        char *base_session_name = NULL;
        char *base_url = NULL;
        char *base_ctrl_url = NULL;
@@ -934,23 +1064,26 @@ static int create_session(void)
                        goto error;
                }
 
-               ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
-               if (ret < 0) {
+               printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
+                       ret = CMD_ERROR;
                        goto error;
                }
                DBG("Session name from command option set to %s", base_session_name);
-       } else if (base_session_name) {
-               ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
-               if (ret < 0) {
+       } else if (base_session_name && !opt_default_name) {
+               printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
+                       ret = CMD_ERROR;
                        goto error;
                }
        } else {
                /* Generate a name */
-               ret = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
-               if (ret < 0) {
+               printed_bytes = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
+                       ret = CMD_ERROR;
                        goto error;
                }
                session_name_date = strdup(base_session_name);
@@ -984,8 +1117,8 @@ static int create_session(void)
                }
 
                /* Create URL string from the local file system path */
-               ret = asprintf(&temp_url, "file://%s", traces_path);
-               if (ret < 0) {
+               printed_bytes = asprintf(&temp_url, "file://%s", traces_path);
+               if (printed_bytes < 0) {
                        PERROR("asprintf url path");
                        ret = CMD_FATAL;
                        goto error;
@@ -1001,8 +1134,9 @@ static int create_session(void)
                 */
                base_ctrl_url = strdup(opt_ctrl_url);
                base_data_url = strdup(opt_data_url);
-       } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
-                               base_url || base_ctrl_url || base_data_url)) {
+       } else if (opt_default_output || !(opt_no_output ||
+                       base_output_type == OUTPUT_NONE || base_url ||
+                       base_ctrl_url || base_data_url)) {
                /* Generate default output depending on the session type */
                switch (base_session_type) {
                case SESSION_NORMAL:
@@ -1017,11 +1151,11 @@ static int create_session(void)
                                goto error;
                        }
 
-                       ret = asprintf(&tmp_url,
+                       printed_bytes = asprintf(&tmp_url,
                                        "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
                                        tmp_home_path, session_name_date);
 
-                       if (ret < 0) {
+                       if (printed_bytes < 0) {
                                PERROR("asprintf trace dir name");
                                ret = CMD_FATAL;
                                goto error;
@@ -1031,8 +1165,8 @@ static int create_session(void)
                        break;
                case SESSION_LIVE:
                        /* Default to a net output */
-                       ret = asprintf(&tmp_url, "net://127.0.0.1");
-                       if (ret < 0) {
+                       printed_bytes = asprintf(&tmp_url, "net://127.0.0.1");
+                       if (printed_bytes < 0) {
                                PERROR("asprintf default live URL");
                                ret = CMD_FATAL;
                                goto error;
@@ -1050,9 +1184,12 @@ static int create_session(void)
          * Shared memory path handling
          */
        if (opt_shm_path) {
-               ret = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
-               if (ret < 0) {
+               /* Overwrite shm_path so clear any previously defined one */
+               free(base_shm_path);
+               printed_bytes = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
+               if (printed_bytes < 0) {
                        PERROR("asprintf shm_path");
+                       ret = CMD_FATAL;
                        goto error;
                }
        }
@@ -1065,6 +1202,7 @@ static int create_session(void)
        }
 
        /* Get output type from urls */
+       /* TODO: Find a better way of inferring output type */
        if (base_url) {
                /* Get lttng uris from single url */
                uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
@@ -1110,7 +1248,7 @@ static int create_session(void)
                                base_shm_path,
                                datetime);
        } else {
-               ret = create_session_basic (base_session_name,
+               ret = create_session_basic(base_session_name,
                                base_session_type,
                                base_live_timer,
                                base_output_type,
@@ -1124,7 +1262,7 @@ static int create_session(void)
                goto error;
        }
 
-       ret = generate_output (base_session_name,
+       ret = generate_output(base_session_name,
                        base_session_type,
                        base_live_timer,
                        base_output_type,
@@ -1147,8 +1285,10 @@ static int create_session(void)
 
 error:
        /* Session temp stuff */
+       config_document_free(template);
        free(session_name_date);
        free(uris);
+       free(traces_path);
 
        if (ret < 0) {
                ERR("%s", lttng_strerror(ret));
@@ -1349,6 +1489,12 @@ int cmd_create(int argc, const char **argv)
                        DBG("Session live timer interval set to %d", opt_live_timer);
                        break;
                }
+               case OPT_DEFAULT_NAME:
+                       opt_default_name = true;
+                       break;
+               case OPT_DEFAULT_OUTPUT:
+                       opt_default_output = true;
+                       break;
                default:
                        ret = CMD_UNDEFINED;
                        goto end;
This page took 0.040383 seconds and 5 git commands to generate.