sessiond: Add initial support for multiple LTTNG_UST_CTL_PATHs
authorKienan Stewart <kstewart@efficios.com>
Mon, 27 Nov 2023 16:32:42 +0000 (11:32 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 25 Oct 2024 20:24:08 +0000 (16:24 -0400)
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 <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I80f7d63e4be164f7afe3fe478794bd55c71c31fb

src/bin/lttng-sessiond/sessiond-config.cpp
src/common/utils.cpp
src/common/utils.hpp

index 774a2c14378963c622c99202b68502715a4a0351..0a12d9d7f80759bff3d174c1a592daa7dd3965b3 100644 (file)
@@ -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<char, lttng::memory::free>(
+               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;
                }
index 3140033309fbbb8921409ec537bdbdda5bf4f8d0..0f9fae7638aa0cb9156223c3edb9db486e8a83da 100644 (file)
 #include <common/format.hpp>
 #include <common/readwrite.hpp>
 #include <common/runas.hpp>
+#include <common/string-utils/c-string-view.hpp>
 #include <common/string-utils/format.hpp>
 
 #include <lttng/constant.h>
 
+#include <algorithm>
 #include <ctype.h>
 #include <fcntl.h>
 #include <grp.h>
@@ -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<char>(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;
 }
 
 /*
index e1ff53fc491555583ec31d4980e842416d881c83..ed9cff8053b85e717fd11391328a8379e81fe1d8 100644 (file)
@@ -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);
This page took 0.027838 seconds and 4 git commands to generate.