From a3d9ed2388e96798b60441bba36f29657e4d7d8e Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Mon, 27 Nov 2023 11:32:42 -0500 Subject: [PATCH] sessiond: Add initial support for multiple LTTNG_UST_CTL_PATHs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit LTTNG_UST_CTL_PATH may be separated with ':'. There is no provision for escaping the ':' path separator (similar to `$PATH`). Subsequent paths after the first are ignored. Signed-off-by: Kienan Stewart Signed-off-by: Jérémie Galarneau Change-Id: I80f7d63e4be164f7afe3fe478794bd55c71c31fb --- src/bin/lttng-sessiond/sessiond-config.cpp | 7 ++-- src/common/utils.cpp | 38 ++++++++++++++++++---- src/common/utils.hpp | 2 +- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/bin/lttng-sessiond/sessiond-config.cpp b/src/bin/lttng-sessiond/sessiond-config.cpp index 774a2c143..0a12d9d7f 100644 --- a/src/bin/lttng-sessiond/sessiond-config.cpp +++ b/src/bin/lttng-sessiond/sessiond-config.cpp @@ -45,7 +45,7 @@ static struct sessiond_config sessiond_config_build_defaults = { .apps_unix_sock_path = { nullptr, false }, .client_unix_sock_path = { nullptr, false }, - .wait_shm = { false, {nullptr, false}}, + .wait_shm = { false, { nullptr, false } }, .health_unix_sock_path = { nullptr, false }, .lttng_ust_clock_plugin = { nullptr, false }, .pid_file_path = { nullptr, false }, @@ -272,10 +272,11 @@ end: int sessiond_config_init(struct sessiond_config *config) { - const char *lttng_ust_ctl_path_override = utils_get_lttng_ust_ctl_path_override_dir(); int ret; const bool is_root = (getuid() == 0); char *str; + auto lttng_ust_ctl_path_override = lttng::make_unique_wrapper( + utils_get_lttng_ust_ctl_path_override_dir()); LTTNG_ASSERT(config); memcpy(config, &sessiond_config_build_defaults, sizeof(*config)); @@ -297,7 +298,7 @@ int sessiond_config_init(struct sessiond_config *config) * - wait_shm_path * - agent_port_file_path */ - ret = config_set_ust_ctl_paths(config, lttng_ust_ctl_path_override); + ret = config_set_ust_ctl_paths(config, lttng_ust_ctl_path_override.get()); if (ret < 0) { goto error; } diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 314003330..0f9fae763 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -20,10 +20,12 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -742,20 +744,42 @@ end: } /* - * Obtain the value of LTTNG_UST_CTL_PATH environment variable, if - * exists. Otherwise return NULL. + * Obtain the value of the LTTNG_UST_CTL_PATH environment variable, if + * it is set. Otherwise NULL is returned. Dynamically allocated, must be + * freed by the caller. */ -const char *utils_get_lttng_ust_ctl_path_override_dir() +char *utils_get_lttng_ust_ctl_path_override_dir() { - const auto *val = lttng_secure_getenv(DEFAULT_LTTNG_UST_CTL_PATH_ENV_VAR); - if (val == nullptr) { + const lttng::c_string_view env_val(lttng_secure_getenv(DEFAULT_LTTNG_UST_CTL_PATH_ENV_VAR)); + if (env_val.data() == nullptr) { return nullptr; } DBG_FMT("LTTng ust-ctl override path specified: {}=`{}`", DEFAULT_LTTNG_UST_CTL_PATH_ENV_VAR, - val); - return val; + env_val); + + const auto separator_location = std::find(env_val.begin(), env_val.end(), ':'); + if (separator_location == env_val.end()) { + auto new_val = ::strdup(env_val); + if (!new_val) { + PERROR("Failed to allocate LTTng ust-ctl override path override value"); + } + + return new_val; + } + + WARN("LTTng ust-ctl override path contains multiple values; only the first will be considered"); + + const auto length = std::distance(env_val.begin(), separator_location); + auto new_val = zmalloc(length + 1); + if (new_val == nullptr) { + PERROR("Failed to allocate LTTng ust-ctl override path override value"); + return nullptr; + } + + std::strncpy(new_val, env_val.data(), length); + return new_val; } /* diff --git a/src/common/utils.hpp b/src/common/utils.hpp index e1ff53fc4..ed9cff805 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -44,7 +44,7 @@ int utils_get_count_order_u32(uint32_t x); int utils_get_count_order_u64(uint64_t x); const char *utils_get_home_dir(); char *utils_get_user_home_dir(uid_t uid); -const char *utils_get_lttng_ust_ctl_path_override_dir(); +char *utils_get_lttng_ust_ctl_path_override_dir(); size_t utils_get_current_time_str(const char *format, char *dst, size_t len) ATTR_FORMAT_STRFTIME(1); -- 2.34.1