Normalize elements in PATH for launch by systemd
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
#include "systemd_manager.h"
|
#include "systemd_manager.h"
|
||||||
#include "systemd_unit.h"
|
#include "systemd_unit.h"
|
||||||
#include "utils/strings.h"
|
#include "utils/strings.h"
|
||||||
|
#include <filesystem>
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include <glib-object.h>
|
#include <glib-object.h>
|
||||||
#include <iostream>
|
#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
|
}; // namespace
|
||||||
|
|
||||||
void systemd_units_manager::start_application(const std::vector<std::string> & args, const std::optional<std::string> & path)
|
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"));
|
variant_builder pathv(G_VARIANT_TYPE("as"));
|
||||||
for (auto item: utils::split(path, ":"))
|
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,
|
g_variant_builder_add(&b,
|
||||||
"(sv)",
|
"(sv)",
|
||||||
"ExecSearchPath",
|
"ExecSearchPath",
|
||||||
|
|||||||
Reference in New Issue
Block a user