From: Jonathan Rajotte Date: Thu, 8 Sep 2016 22:02:51 +0000 (-0400) Subject: lttng-ctl: add override helper to load api X-Git-Tag: v2.9.0-rc1~26 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=a2a75fa4301617ab0d2b97614a989e3ad6efa4bb;p=lttng-tools.git lttng-ctl: add override helper to load api 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 Signed-off-by: Jérémie Galarneau --- diff --git a/include/lttng/load-internal.h b/include/lttng/load-internal.h index 78cf86a2d..b644717ce 100644 --- a/include/lttng/load-internal.h +++ b/include/lttng/load-internal.h @@ -24,6 +24,7 @@ #include #include +#include /* * 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 */ diff --git a/include/lttng/load.h b/include/lttng/load.h index d0adb74ee..9f4c7ab8e 100644 --- a/include/lttng/load.h +++ b/include/lttng/load.h @@ -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). * diff --git a/src/common/config/session-config.h b/src/common/config/session-config.h index c4e4797e5..cb006f18e 100644 --- a/src/common/config/session-config.h +++ b/src/common/config/session-config.h @@ -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; diff --git a/src/lib/lttng-ctl/load.c b/src/lib/lttng-ctl/load.c index fb28f4eae..1e17d481b 100644 --- a/src/lib/lttng-ctl/load.c +++ b/src/lib/lttng-ctl/load.c @@ -18,12 +18,16 @@ #define _LGPL_SOURCE #include #include +#include #include #include #include #include #include +#include +#include +#include #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