lttng-ctl: add override helper to load api
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Thu, 8 Sep 2016 22:02:51 +0000 (18:02 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 7 Oct 2016 04:44:05 +0000 (00:44 -0400)
User can provide an url override that will be used during the loading of a
configuration file.

The url format is the same found at lttng-create(1).

This correspond to a --set-url operation on lttng-create.

Note: an url might be invalid for a trace configuration. The override will fail
during the session load (call to lttng_load_session) and simply cancel the
loading.

e.g Loading a live session with a file:// url override will fail.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/lttng/load-internal.h
include/lttng/load.h
src/common/config/session-config.h
src/lib/lttng-ctl/load.c

index 78cf86a2dc314037c9f4a962164a02f59d9de983..b644717ce39a08e8646f154e815a207c3b9a28b0 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <lttng/constant.h>
 #include <common/macros.h>
+#include <common/config/session-config.h>
 
 /*
  * Object used by the load_session API. This is opaque to the public library.
@@ -35,6 +36,16 @@ struct lttng_load_session_attr {
        char input_url[PATH_MAX];
        /* Overwrite the session if it exists. */
        uint32_t overwrite;
+       /* The raw override url for getter */
+       char *raw_override_url;
+       /* The raw override path url for getter */
+       char *raw_override_path_url;
+       /* The raw override ctrl url for getter */
+       char *raw_override_ctrl_url;
+       /* The raw override data url for getter */
+       char *raw_override_data_url;
+       /* Override struct */
+       struct config_load_session_override_attr override_attr;
 } LTTNG_PACKED;
 
 #endif /* LTTNG_LOAD_INTERNAL_ABI_H */
index d0adb74ee0e49c0a6d2838f2175cc9c1b93e1d71..9f4c7ab8ee81f3f47c5de4a96f3d9cd56d53bb9c 100644 (file)
@@ -64,6 +64,42 @@ const char *lttng_load_session_attr_get_input_url(
 int lttng_load_session_attr_get_overwrite(
        struct lttng_load_session_attr *attr);
 
+/*
+ * Return the destination URL configuration override attribute. This attribute
+ * indicates a destination URL override to be applied during the loading of the
+ * configuration.
+ *
+ * NULL indicates no override will be applied on configuration load.
+ *
+ * Caller is responsible for freeing the return value.
+ */
+const char *lttng_load_session_attr_get_override_url(
+       struct lttng_load_session_attr *attr);
+
+/*
+ * Return the configuration override control URL attribute. This attribute
+ * indicates a control URL override to be applied during the loading of the
+ * configuration(s).
+ *
+ * NULL indicates no control URL override will be applied on configuration load.
+ *
+ * Caller is responsible for freeing the return value.
+ */
+const char *lttng_load_session_attr_get_override_ctrl_url(
+       struct lttng_load_session_attr *attr);
+
+/*
+ * Return the configuration override data URL attribute. This attribute
+ * indicate a data URL override to be applied during the loading of the
+ * configuration(s).
+ *
+ * NULL indicates no data URL override will be applied on configuration load.
+ *
+ * Caller is responsible for freeing the return value.
+ */
+const char *lttng_load_session_attr_get_override_data_url(
+       struct lttng_load_session_attr *attr);
+
 /*
  * Load session attribute setter family of functions.
  *
@@ -89,12 +125,58 @@ int lttng_load_session_attr_set_input_url(
 
 /*
  * Set the overwrite attribute. If set to true, current sessions matching the
- * loaded sessions will be destroyed and the replaced by the sessions being
+ * loaded sessions will be destroyed and be replaced by the session(s) being
  * loaded.
  */
 int lttng_load_session_attr_set_overwrite(
        struct lttng_load_session_attr *attr, int overwrite);
 
+/*
+ * The following setter are for overriding sessions attributes during the
+ * loading of a configuration files. Those attributes prevail upon those
+ * specified in the loaded configuration file.
+ * */
+
+/*
+ * Set the control url override attribute.
+ *
+ * Supported format:
+ *     NETPROTO://(HOST | IPADDR)[:PORT][/TRACEPATH]
+ *
+ *     Where NETPROTO is one of {tcp, tcp6}
+ *
+ * See lttng-create(1) for more detail.
+ */
+int lttng_load_session_attr_set_override_ctrl_url(
+       struct lttng_load_session_attr *attr, const char *url);
+
+/*
+ * Set the data url override attribute.
+ *
+ * Supported format:
+ *     NETPROTO://(HOST | IPADDR)[:PORT][/TRACEPATH]
+ *
+ *     Where NETPROTO is one of {tcp, tcp6}
+ *
+ * See lttng-create(1) for more detail.
+ */
+int lttng_load_session_attr_set_override_data_url(
+       struct lttng_load_session_attr *attr, const char *url);
+
+/*
+ * Set the url override attribute.
+ *
+ * Supported format:
+ *    file://TRACEPATH
+ *    NETPROTO://(HOST | IPADDR)[:CTRLPORT[:DATAPORT]][/TRACEPATH]
+ *
+ *     Where NETPROTO is one of {tcp, tcp6}
+ *
+ * See lttng-create(1) for more detail.
+ */
+int lttng_load_session_attr_set_override_url(
+       struct lttng_load_session_attr *attr, const char *url);
+
 /*
  * Load session configuration(s).
  *
index c4e4797e5c7c0fcf6166126ee6cdd4839cda16aa..cb006f18e6824b6406c3469b246330a06ff03039 100644 (file)
@@ -30,6 +30,12 @@ struct config_entry {
        const char *value;
 };
 
+struct config_load_session_override_attr {
+       char *path_url;
+       char *ctrl_url;
+       char *data_url;
+};
+
 /* Instance of a configuration writer. */
 struct config_writer;
 
index fb28f4eaeef17a19bae584170c24ceb5563cf3d0..1e17d481b1041750b3dca811c1721befb6e88016 100644 (file)
 #define _LGPL_SOURCE
 #include <assert.h>
 #include <string.h>
+#include <limits.h>
 
 #include <lttng/lttng-error.h>
 #include <lttng/load.h>
 #include <lttng/load-internal.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/config/session-config.h>
+#include <common/uri.h>
+#include <common/macros.h>
+#include <common/compat/string.h>
 
 #include "lttng-ctl-helper.h"
 
@@ -32,9 +36,22 @@ struct lttng_load_session_attr *lttng_load_session_attr_create(void)
        return zmalloc(sizeof(struct lttng_load_session_attr));
 }
 
+static
+void reset_load_session_attr_urls(struct lttng_load_session_attr *attr)
+{
+       free(attr->raw_override_url);
+       free(attr->raw_override_path_url);
+       free(attr->raw_override_ctrl_url);
+       free(attr->raw_override_data_url);
+       free(attr->override_attr.path_url);
+       free(attr->override_attr.ctrl_url);
+       free(attr->override_attr.data_url);
+}
+
 void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr)
 {
        if (attr) {
+               reset_load_session_attr_urls(attr);
                free(attr);
        }
 }
@@ -69,6 +86,56 @@ int lttng_load_session_attr_get_overwrite(
        return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
 }
 
+const char *lttng_load_session_attr_get_override_path_url(
+       struct lttng_load_session_attr *attr)
+{
+       const char *ret = NULL;
+
+       if (attr && attr->override_attr.path_url) {
+               ret = attr->raw_override_path_url;
+       }
+
+       return ret;
+}
+
+const char *lttng_load_session_attr_get_override_ctrl_url(
+       struct lttng_load_session_attr *attr)
+{
+       const char *ret = NULL;
+
+       if (attr && attr->override_attr.ctrl_url) {
+               ret = attr->raw_override_ctrl_url;
+       }
+
+       return ret;
+}
+
+const char *lttng_load_session_attr_get_override_data_url(
+       struct lttng_load_session_attr *attr)
+{
+       const char *ret = NULL;
+
+       if (attr && attr->override_attr.data_url) {
+               ret = attr->raw_override_data_url;
+       }
+
+       return ret;
+}
+
+const char *lttng_load_session_attr_get_override_url(
+               struct lttng_load_session_attr *attr)
+{
+       const char *ret = NULL;
+
+       if (attr && (attr->override_attr.path_url ||
+               (attr->override_attr.ctrl_url &&
+                attr->override_attr.data_url))) {
+               ret = attr->raw_override_url;
+       }
+
+       return ret;
+}
+
 int lttng_load_session_attr_set_session_name(
        struct lttng_load_session_attr *attr, const char *session_name)
 {
@@ -152,6 +219,291 @@ end:
        return ret;
 }
 
+int lttng_load_session_attr_set_override_ctrl_url(
+       struct lttng_load_session_attr *attr, const char *url)
+{
+       int ret = 0;
+       ssize_t ret_size;
+       struct lttng_uri *uri = NULL;
+       char *url_str = NULL;
+       char *raw_str = NULL;
+
+       if (!attr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (attr->override_attr.path_url) {
+               /*
+                * FIXME: return a more meaningful error.
+                * Setting a ctrl override after a path override make no
+                * sense.
+                * */
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /*
+        * FIXME: uri_parse should be able to take as parameter the protocol
+        * type to validate "url". For now only check the parsing goes through;
+        * it will fail later on.
+        */
+       ret_size = uri_parse(url, &uri);
+       if (ret_size < 0) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (uri[0].port == 0) {
+               uri[0].port = DEFAULT_NETWORK_CONTROL_PORT;
+       }
+
+       url_str = zmalloc(PATH_MAX);
+       if (!url_str) {
+               /* FIXME: return valid error */
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
+       if (ret < 0) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+       ret = 0;
+
+       raw_str = lttng_strndup(url, PATH_MAX);
+       if (!raw_str) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       /* Squash old value if any */
+       free(attr->override_attr.ctrl_url);
+       free(attr->raw_override_ctrl_url);
+
+       /* Populate the object */
+       attr->override_attr.ctrl_url = url_str;
+       attr->raw_override_ctrl_url = raw_str;
+
+       /* Ownership passed to attr. */
+       url_str = NULL;
+       raw_str = NULL;
+
+end:
+       free(raw_str);
+       free(url_str);
+       free(uri);
+       return ret;
+}
+
+int lttng_load_session_attr_set_override_data_url(
+       struct lttng_load_session_attr *attr, const char *url)
+{
+       int ret = 0;
+       ssize_t ret_size;
+       struct lttng_uri *uri = NULL;
+       char *url_str = NULL;
+       char *raw_str = NULL;
+
+       if (!attr) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (attr->override_attr.path_url) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /*
+        * FIXME: uri_parse should be able to take as parameter the protocol
+        * type to validate "url". For now only check the parsing goes through;
+        * it will fail later on.
+        */
+       ret_size = uri_parse(url, &uri);
+       if (ret_size < 0) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       if (uri[0].port == 0) {
+               uri[0].port = DEFAULT_NETWORK_DATA_PORT;
+       }
+
+       url_str = zmalloc(PATH_MAX);
+       if (!url_str) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       ret = uri_to_str_url(&uri[0], url_str, PATH_MAX);
+       if (ret < 0) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+       ret = 0;
+
+       raw_str = lttng_strndup(url, PATH_MAX);
+       if (!raw_str) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       /* Squash old value if any */
+       free(attr->override_attr.data_url);
+       free(attr->raw_override_data_url);
+
+       /* Populate the object */
+       attr->override_attr.data_url = url_str;
+       attr->raw_override_data_url = raw_str;
+
+       /* Ownership passed to attr. */
+       url_str = NULL;
+       raw_str = NULL;
+end:
+       free(raw_str);
+       free(url_str);
+       free(uri);
+       return ret;
+}
+
+int lttng_load_session_attr_set_override_url(
+               struct lttng_load_session_attr *attr, const char *url)
+{
+       int ret = 0;
+       ssize_t ret_size;
+       struct lttng_uri *uri = NULL;
+       char *raw_url_str = NULL;
+       char *raw_path_str = NULL;
+       char *path_str = NULL;
+       char *raw_ctrl_str = NULL;
+       char *ctrl_str = NULL;
+       char *raw_data_str = NULL;
+       char *data_str = NULL;
+       char buffer[PATH_MAX];
+
+       if (!attr || !url || strlen(url) >= PATH_MAX) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       /*
+        * FIXME: uri_parse should be able to take as parameter the protocol
+        * type to validate "url". For now only check the parsing goes through;
+        * it will fail later on.
+        */
+       ret_size = uri_parse_str_urls(url, NULL, &uri);
+       if (ret_size < 0 || ret_size > 2) {
+               /* Unexpected URL format. */
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       raw_url_str = lttng_strndup(url, PATH_MAX);
+       if (!raw_url_str) {
+               ret = -LTTNG_ERR_NOMEM;
+               goto end;
+       }
+
+       /* Get path | ctrl && data string URL. */
+       ret = uri_to_str_url(&uri[0], buffer, sizeof(buffer));
+       if (ret < 0 || ret >= PATH_MAX) {
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+       ret = 0;
+
+       switch (uri[0].dtype) {
+       case LTTNG_DST_PATH:
+               raw_path_str = lttng_strndup(buffer, PATH_MAX);
+               if (!raw_path_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               path_str = lttng_strndup(raw_path_str, PATH_MAX);
+               if (!path_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+               break;
+       case LTTNG_DST_IPV4:
+       case LTTNG_DST_IPV6:
+               if (ret_size != 2) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto end;
+               }
+
+               raw_ctrl_str = lttng_strndup(buffer, PATH_MAX);
+               if (!raw_ctrl_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               ctrl_str = lttng_strndup(raw_ctrl_str, PATH_MAX);
+               if (!ctrl_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               /* Get the data uri. */
+               ret = uri_to_str_url(&uri[1], buffer, sizeof(buffer));
+               if (ret < 0) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto end;
+               }
+               ret = 0;
+
+               raw_data_str = lttng_strndup(buffer, PATH_MAX);
+               if (!raw_data_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               data_str = lttng_strndup(raw_data_str, PATH_MAX);
+               if (!data_str) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               break;
+       default:
+               ret = -LTTNG_ERR_INVALID;
+               goto end;
+       }
+
+       reset_load_session_attr_urls(attr);
+
+       attr->override_attr.path_url = path_str;
+       attr->override_attr.ctrl_url = ctrl_str;
+       attr->override_attr.data_url = data_str;
+
+       attr->raw_override_url = raw_url_str;
+       attr->raw_override_path_url = raw_path_str;
+       attr->raw_override_ctrl_url = raw_ctrl_str;
+       attr->raw_override_data_url = raw_data_str;
+
+       /* Pass data ownership to attr. */
+       raw_url_str = NULL;
+       raw_path_str = NULL;
+       path_str = NULL;
+       raw_ctrl_str = NULL;
+       ctrl_str = NULL;
+       raw_data_str = NULL;
+       data_str = NULL;
+
+end:
+       free(raw_path_str);
+       free(path_str);
+       free(raw_ctrl_str);
+       free(ctrl_str);
+       free(raw_data_str);
+       free(data_str);
+       free(raw_url_str);
+       free(uri);
+       return ret;
+}
 /*
  * The lttng-ctl API does not expose all the information needed to load the
  * session configurations. Thus, we must send a load command to the session
This page took 0.030908 seconds and 4 git commands to generate.