Implement create command --default-name option
[lttng-tools.git] / src / bin / lttng / commands / create.c
index accb7edf3173906811e221153903476e4fa54947..62607a0383311b065b44661d47d4f9a59ea13b1d 100644 (file)
@@ -52,11 +52,13 @@ 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;
 
 enum {
        OPT_HELP = 1,
        OPT_LIST_OPTIONS,
        OPT_LIVE_TIMER,
+       OPT_DEFAULT_NAME,
 };
 
 enum output_type {
@@ -89,6 +91,7 @@ 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},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -256,6 +259,11 @@ static int validate_command_options(enum session_type type)
                }
        }
 
+       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;
+       }
 error:
        return ret;
 }
@@ -459,6 +467,7 @@ static int parse_template (struct config_document *template,
                char **shm_path)
 {
        int ret = 0;
+       int printed_bytes;
        char *raw_value = NULL;
 
        assert(template);
@@ -495,8 +504,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 +541,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;
                                }
@@ -582,6 +591,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];
@@ -622,8 +632,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 {
@@ -707,7 +730,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);
 
@@ -724,6 +749,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;
@@ -744,6 +770,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
@@ -752,22 +835,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_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element);
+               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;
                break;
        default:
                ERR("Invalid session type");
@@ -775,6 +880,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));
@@ -801,6 +914,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);
@@ -824,6 +940,7 @@ error:
 static int create_session(void)
 {
        int ret;
+       int printed_bytes;
 
 
        /* Template */
@@ -937,23 +1054,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);
@@ -987,8 +1107,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;
@@ -1020,11 +1140,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;
@@ -1034,8 +1154,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;
@@ -1053,9 +1173,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;
                }
        }
@@ -1068,6 +1191,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);
@@ -1113,7 +1237,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,
@@ -1127,7 +1251,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,
@@ -1150,8 +1274,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));
@@ -1352,6 +1478,9 @@ 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;
                default:
                        ret = CMD_UNDEFINED;
                        goto end;
This page took 0.041821 seconds and 5 git commands to generate.