diff --git a/docs/app_examples.md b/docs/app_examples.md index 4bf45cb9b..2769ea413 100644 --- a/docs/app_examples.md +++ b/docs/app_examples.md @@ -345,12 +345,6 @@ third-party tool, you can use *QRes* as an example. ### Additional Considerations -#### Linux (Flatpak) - -> [!CAUTION] -> Because Flatpak packages run in a sandboxed environment and do not normally have access to the -> host, the Flatpak of Sunshine requires commands to be prefixed with `flatpak-spawn --host`. - #### Windows **Elevating Commands (Windows)** diff --git a/docs/getting_started.md b/docs/getting_started.md index 20d1dc8e6..07dfd991c 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -725,7 +725,6 @@ All shortcuts start with `Ctrl+Alt+Shift`, just like Moonlight. * The "Desktop" app works the same as any other application except it has no commands. It does not start an application, instead it simply starts a stream. If you removed it and would like to get it back, just add a new application with the name "Desktop" and "desktop.png" as the image path. -* For the Linux flatpak you must prepend commands with `flatpak-spawn --host`. * If inputs (mouse, keyboard, gamepads...) aren't working after connecting: * On FreeBSD/Linux, add the user running sunshine to the `input` group. diff --git a/src/process.cpp b/src/process.cpp index 8a35cca63..b468ce93d 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -149,6 +149,23 @@ namespace proc { return cmd_path.parent_path(); } + std::string prepare_command(const std::string &command) { +#ifdef SUNSHINE_BUILD_FLATPAK + constexpr auto host_command_prefix = "flatpak-spawn --host "sv; + const auto stripped_command = boost::trim_copy(command); + if (stripped_command.empty() || stripped_command.starts_with(host_command_prefix)) { + if (!stripped_command.empty()) { + BOOST_LOG(warning) << "Command ["sv << stripped_command << "] already includes [flatpak-spawn --host], which is no longer required for Flatpak builds."sv; + } + return stripped_command; + } + + return std::string {host_command_prefix} + stripped_command; +#else + return command; +#endif + } + int proc_t::execute(int app_id, std::shared_ptr launch_session) { // Ensure starting from a clean slate terminate(); @@ -221,14 +238,15 @@ namespace proc { continue; } + const auto command = prepare_command(cmd.do_cmd); boost::filesystem::path working_dir = _app.working_dir.empty() ? - find_working_directory(cmd.do_cmd, _env) : + find_working_directory(command, _env) : boost::filesystem::path(_app.working_dir); - BOOST_LOG(info) << "Executing Do Cmd: ["sv << cmd.do_cmd << ']'; - auto child = platf::run_command(cmd.elevated, true, cmd.do_cmd, working_dir, _env, _pipe.get(), ec, nullptr); + BOOST_LOG(info) << "Executing Do Cmd: ["sv << command << ']'; + auto child = platf::run_command(cmd.elevated, true, command, working_dir, _env, _pipe.get(), ec, nullptr); if (ec) { - BOOST_LOG(error) << "Couldn't run ["sv << cmd.do_cmd << "]: System: "sv << ec.message(); + BOOST_LOG(error) << "Couldn't run ["sv << command << "]: System: "sv << ec.message(); // We don't want any prep commands failing launch of the desktop. // This is to prevent the issue where users reboot their PC and need to log in with Sunshine. // permission_denied is typically returned when the user impersonation fails, which can happen when user is not signed in yet. @@ -239,24 +257,25 @@ namespace proc { child.wait(ec); if (ec) { - BOOST_LOG(error) << '[' << cmd.do_cmd << "] wait failed with error code ["sv << ec << ']'; + BOOST_LOG(error) << '[' << command << "] wait failed with error code ["sv << ec << ']'; return -1; } auto ret = child.exit_code(); if (ret != 0) { - BOOST_LOG(error) << '[' << cmd.do_cmd << "] exited with code ["sv << ret << ']'; + BOOST_LOG(error) << '[' << command << "] exited with code ["sv << ret << ']'; return -1; } } for (auto &cmd : _app.detached) { + const auto command = prepare_command(cmd); boost::filesystem::path working_dir = _app.working_dir.empty() ? - find_working_directory(cmd, _env) : + find_working_directory(command, _env) : boost::filesystem::path(_app.working_dir); - BOOST_LOG(info) << "Spawning ["sv << cmd << "] in ["sv << working_dir << ']'; - auto child = platf::run_command(_app.elevated, true, cmd, working_dir, _env, _pipe.get(), ec, nullptr); + BOOST_LOG(info) << "Spawning ["sv << command << "] in ["sv << working_dir << ']'; + auto child = platf::run_command(_app.elevated, true, command, working_dir, _env, _pipe.get(), ec, nullptr); if (ec) { - BOOST_LOG(warning) << "Couldn't spawn ["sv << cmd << "]: System: "sv << ec.message(); + BOOST_LOG(warning) << "Couldn't spawn ["sv << command << "]: System: "sv << ec.message(); } else { child.detach(); } @@ -266,13 +285,14 @@ namespace proc { BOOST_LOG(info) << "Executing [Desktop]"sv; placebo = true; } else { + const auto command = prepare_command(_app.cmd); boost::filesystem::path working_dir = _app.working_dir.empty() ? - find_working_directory(_app.cmd, _env) : + find_working_directory(command, _env) : boost::filesystem::path(_app.working_dir); - BOOST_LOG(info) << "Executing: ["sv << _app.cmd << "] in ["sv << working_dir << ']'; - _process = platf::run_command(_app.elevated, true, _app.cmd, working_dir, _env, _pipe.get(), ec, &_process_group); + BOOST_LOG(info) << "Executing: ["sv << command << "] in ["sv << working_dir << ']'; + _process = platf::run_command(_app.elevated, true, command, working_dir, _env, _pipe.get(), ec, &_process_group); if (ec) { - BOOST_LOG(warning) << "Couldn't run ["sv << _app.cmd << "]: System: "sv << ec.message(); + BOOST_LOG(warning) << "Couldn't run ["sv << command << "]: System: "sv << ec.message(); return -1; } } @@ -334,11 +354,12 @@ namespace proc { continue; } + const auto command = prepare_command(cmd.undo_cmd); boost::filesystem::path working_dir = _app.working_dir.empty() ? - find_working_directory(cmd.undo_cmd, _env) : + find_working_directory(command, _env) : boost::filesystem::path(_app.working_dir); - BOOST_LOG(info) << "Executing Undo Cmd: ["sv << cmd.undo_cmd << ']'; - auto child = platf::run_command(cmd.elevated, true, cmd.undo_cmd, working_dir, _env, _pipe.get(), ec, nullptr); + BOOST_LOG(info) << "Executing Undo Cmd: ["sv << command << ']'; + auto child = platf::run_command(cmd.elevated, true, command, working_dir, _env, _pipe.get(), ec, nullptr); if (ec) { BOOST_LOG(warning) << "System: "sv << ec.message(); diff --git a/src/process.h b/src/process.h index 75a9de09e..233137b15 100644 --- a/src/process.h +++ b/src/process.h @@ -180,6 +180,14 @@ namespace proc { */ std::tuple calculate_app_id(const std::string &app_name, std::string app_image_path, int index); + /** + * @brief Prepare a configured command for execution. + * + * @param command Configured command line. + * @return Command line with any package-specific launcher prefix applied. + */ + std::string prepare_command(const std::string &command); + bool check_valid_png(const std::filesystem::path &path); /** * @brief Validate app image path. diff --git a/tests/unit/test_process.cpp b/tests/unit/test_process.cpp index e584f4fa7..5d5b3c578 100644 --- a/tests/unit/test_process.cpp +++ b/tests/unit/test_process.cpp @@ -14,6 +14,22 @@ namespace fs = std::filesystem; +TEST(ProcessTest, PrepareCommand) { +#ifdef SUNSHINE_BUILD_FLATPAK + EXPECT_EQ(proc::prepare_command("steam"), "flatpak-spawn --host steam"); + EXPECT_EQ(proc::prepare_command("flatpak-spawn --host steam"), "flatpak-spawn --host steam"); + EXPECT_EQ(proc::prepare_command(" flatpak-spawn --host steam "), "flatpak-spawn --host steam"); + EXPECT_EQ(proc::prepare_command(" steam "), "flatpak-spawn --host steam"); + EXPECT_EQ(proc::prepare_command(""), ""); + EXPECT_EQ(proc::prepare_command(" \t"), ""); +#else + EXPECT_EQ(proc::prepare_command("steam"), "steam"); + EXPECT_EQ(proc::prepare_command(" steam "), " steam "); + EXPECT_EQ(proc::prepare_command("flatpak-spawn --host steam"), "flatpak-spawn --host steam"); + EXPECT_EQ(proc::prepare_command(""), ""); +#endif +} + class ProcessPNGTest: public BaseTest { protected: void SetUp() override {