Normalize elements in PATH for launch by systemd

This commit is contained in:
Patrick Nicolas committed 2026-02-05 09:44:34 +01:00
1 parent ab7e497735
commit 3fc3dfa575
1 file changed
+40 -1
+40 -1
View File
@@ -20,6 +20,7 @@
#include "systemd_manager.h"
#include "systemd_unit.h"
#include "utils/strings.h"
#include <filesystem>
#include <gio/gio.h>
#include <glib-object.h>
#include <iostream>
@@ -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<std::filesystem::path> 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<std::string> & args, const std::optional<std::string> & path)
@@ -239,7 +266,19 @@ void systemd_units_manager::start_application(const std::vector<std::string> & 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",