diff --git a/src/process.cpp b/src/process.cpp index 867b64588..bb80985de 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -140,7 +140,15 @@ namespace proc { // If the cmd path is not an absolute path, resolve it using our PATH variable boost::filesystem::path cmd_path(parts.at(0)); if (!cmd_path.is_absolute()) { - cmd_path = boost::process::v1::search_path(parts.at(0)); + // Search the child's PATH. Boost's default search list reads this process's environment through a + // std::locale("") on POSIX, which throws when an LC_* variable names a locale that is not installed. + if (const auto path_var = env.find("PATH"); path_var != env.end()) { + const auto path_entries = path_var->to_vector(); + const std::vector search_dirs {path_entries.begin(), path_entries.end()}; + cmd_path = boost::process::v1::search_path(parts.at(0), search_dirs); + } else { + cmd_path = boost::process::v1::search_path(parts.at(0)); + } if (cmd_path.empty()) { BOOST_LOG(error) << "Unable to find executable ["sv << parts.at(0) << "]. Is it in your PATH?"sv; return boost::filesystem::path(); diff --git a/tests/unit/test_process.cpp b/tests/unit/test_process.cpp index d8561d13e..3c763fd34 100644 --- a/tests/unit/test_process.cpp +++ b/tests/unit/test_process.cpp @@ -6,8 +6,10 @@ #include "../tests_common.h" // standard includes +#include #include #include +#include #include // lib imports @@ -66,6 +68,46 @@ TEST(ProcessTest, UrlCommandsDoNotResolveAnExecutableWorkingDirectory) { EXPECT_TRUE(proc::find_working_directory("custom+scheme://host/path", env).empty()); } +#ifndef _WIN32 +TEST(ProcessTest, UnavailableLocaleDoesNotAbortCommandLookup) { + const char *original = std::getenv("LC_ALL"); + const std::optional saved = original ? std::optional {original} : std::nullopt; + const auto restore = util::fail_guard([&saved]() { + if (saved.has_value()) { + setenv("LC_ALL", saved->c_str(), 1); + } else { + unsetenv("LC_ALL"); + } + }); + setenv("LC_ALL", "xx_INVALID.UTF-8", 1); + + boost::process::v1::environment env; + env["PATH"] = "/usr/bin:/bin"; + + boost::filesystem::path working_dir; + EXPECT_NO_THROW(working_dir = proc::find_working_directory("sh -c true", env)); + EXPECT_FALSE(working_dir.empty()); +} + +TEST(ProcessTest, ResolvesRelativeCommandsFromTheChildPath) { + const auto bin_dir = fs::temp_directory_path() / "sunshine_find_working_directory_test"; // NOSONAR(cpp:S5443): safe for tests + fs::create_directories(bin_dir); + const auto cleanup = util::fail_guard([&bin_dir]() { + std::error_code ec; + fs::remove_all(bin_dir, ec); + }); + const auto executable = bin_dir / "sunshine-path-probe"; + std::ofstream(executable) << "#!/bin/sh\n"; + fs::permissions(executable, fs::perms::owner_all); + + boost::process::v1::environment env; + env["PATH"] = bin_dir.string(); + + EXPECT_EQ(proc::find_working_directory("sunshine-path-probe --flag", env), boost::filesystem::path(bin_dir.string())); + EXPECT_TRUE(proc::find_working_directory("sunshine-missing-probe", env).empty()); +} +#endif + TEST(ProcessTest, PrepareCommand) { #ifdef SUNSHINE_BUILD_FLATPAK EXPECT_EQ(proc::prepare_command("steam"), "flatpak-spawn --host steam");