diff --git a/server/start_systemd_unit.cpp b/server/start_systemd_unit.cpp index da560cc5..e1e9353a 100644 --- a/server/start_systemd_unit.cpp +++ b/server/start_systemd_unit.cpp @@ -20,6 +20,7 @@ #include "systemd_manager.h" #include "systemd_unit.h" #include "utils/strings.h" +#include #include #include #include @@ -194,6 +195,32 @@ struct variant_builder } }; +// Almost the same as canonical, but don't try to follow symlinks +std::filesystem::path normalize(std::filesystem::path path) +{ + if (path.is_relative()) + path = std::filesystem::current_path() / path; + + std::vector items; + for (auto && item: path) + { + if (item.empty() or item == ".") + continue; + if (item == "..") + { + if (items.size() < 2) + throw std::runtime_error("too many .."); + items.pop_back(); + } + else + items.push_back(std::move(item)); + } + path.clear(); + for (const auto & item: items) + path /= item; + return path; +} + }; // namespace void systemd_units_manager::start_application(const std::vector & args, const std::optional & path) @@ -239,7 +266,19 @@ void systemd_units_manager::start_application(const std::vector & a { variant_builder pathv(G_VARIANT_TYPE("as")); for (auto item: utils::split(path, ":")) - g_variant_builder_add(&pathv, "s", item.c_str()); + { + if (item.empty()) + continue; + try + { + item = normalize(item); + g_variant_builder_add(&pathv, "s", item.c_str()); + } + catch (std::exception & e) + { + std::cerr << "Failed to normalize element " << std::quoted(item) << " from $PATH: " << e.what() << std::endl; + } + } g_variant_builder_add(&b, "(sv)", "ExecSearchPath",