Skip to content

Add new form of preparation commands that runs before checking for displays #3821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,17 @@ namespace nvhttp {
host_audio = util::from_view(get_arg(args, "localAudioPlayMode"));
auto launch_session = make_launch_session(host_audio, args);

if (appid > 0) {
auto err = proc::proc.prepare(appid, launch_session);
if (err) {
tree.put("root.<xmlattr>.status_code", err);
tree.put("root.<xmlattr>.status_message", "Failed to prepare for the specified application");
tree.put("root.gamesession", 0);

return;
}
}

if (rtsp_stream::session_count() == 0) {
// The display should be restored in case something fails as there are no other sessions.
revert_display_configuration = true;
Expand Down Expand Up @@ -885,7 +896,7 @@ namespace nvhttp {
}

if (appid > 0) {
auto err = proc::proc.execute(appid, launch_session);
auto err = proc::proc.execute();
if (err) {
tree.put("root.<xmlattr>.status_code", err);
tree.put("root.<xmlattr>.status_message", "Failed to start the specified application");
Expand Down
149 changes: 86 additions & 63 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace proc {
}
}

boost::filesystem::path find_working_directory(const std::string &cmd, boost::process::v1::environment &env) {
boost::filesystem::path find_working_directory(const std::string &cmd, const boost::process::v1::environment &env) {
// Parse the raw command string into parts to get the actual command portion
#ifdef _WIN32
auto parts = boost::program_options::split_winmain(cmd);
Expand Down Expand Up @@ -130,23 +130,22 @@ namespace proc {
return cmd_path.parent_path();
}

int proc_t::execute(int app_id, std::shared_ptr<rtsp_stream::launch_session_t> launch_session) {
int proc_t::prepare(int app_id, std::shared_ptr<rtsp_stream::launch_session_t> launch_session) {
// Ensure starting from a clean slate
terminate();

auto iter = std::find_if(_apps.begin(), _apps.end(), [&app_id](const auto app) {
auto app_iter = std::find_if(_apps.begin(), _apps.end(), [&app_id](const auto app) {
return app.id == std::to_string(app_id);
});

if (iter == _apps.end()) {
if (app_iter == _apps.end()) {
BOOST_LOG(error) << "Couldn't find app with ID ["sv << app_id << ']';
return 404;
}

_app_id = app_id;
_app = *iter;
_app_prep_begin = std::begin(_app.prep_cmds);
_app_prep_it = _app_prep_begin;
_app = *app_iter;
_app_prep = prep_cmds_t(_app.prep_cmds);

// Add Stream-specific environment variables
_env["SUNSHINE_APP_ID"] = std::to_string(_app_id);
Expand Down Expand Up @@ -187,43 +186,23 @@ namespace proc {
#endif
}

return 0;
}

int proc_t::execute() {
std::error_code ec;
// Executed when returning from function
auto fg = util::fail_guard([&]() {
terminate();
});

for (; _app_prep_it != std::end(_app.prep_cmds); ++_app_prep_it) {
auto &cmd = *_app_prep_it;

// Skip empty commands
if (cmd.do_cmd.empty()) {
continue;
}

boost::filesystem::path working_dir = _app.working_dir.empty() ?
find_working_directory(cmd.do_cmd, _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);

if (ec) {
BOOST_LOG(error) << "Couldn't run ["sv << cmd.do_cmd << "]: 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.
if (!(_app.cmd.empty() && ec == std::errc::permission_denied)) {
return -1;
}
}

child.wait();
auto ret = child.exit_code();
if (ret != 0 && ec != std::errc::permission_denied) {
BOOST_LOG(error) << '[' << cmd.do_cmd << "] failed with code ["sv << ret << ']';
return -1;
}
}
int err = _app_prep.run_do(_app.working_dir, _env, _pipe.get(),
// 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.
_app.cmd.empty() ? prep_cmds_t::FLAG_IGNORE_PERMISSION_DENIED : 0
);
if (err != 0) return err;

for (auto &cmd : _app.detached) {
boost::filesystem::path working_dir = _app.working_dir.empty() ?
Expand Down Expand Up @@ -297,36 +276,12 @@ namespace proc {
}

void proc_t::terminate() {
std::error_code ec;
placebo = false;
terminate_process_group(_process, _process_group, _app.exit_timeout);
_process = boost::process::v1::child();
_process_group = boost::process::v1::group();

for (; _app_prep_it != _app_prep_begin; --_app_prep_it) {
auto &cmd = *(_app_prep_it - 1);

if (cmd.undo_cmd.empty()) {
continue;
}

boost::filesystem::path working_dir = _app.working_dir.empty() ?
find_working_directory(cmd.undo_cmd, _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);

if (ec) {
BOOST_LOG(warning) << "System: "sv << ec.message();
}

child.wait();
auto ret = child.exit_code();

if (ret != 0) {
BOOST_LOG(warning) << "Return code ["sv << ret << ']';
}
}
_app_prep.run_undo(_app.working_dir, _env, _pipe.get());

_pipe.reset();

Expand Down Expand Up @@ -379,6 +334,74 @@ namespace proc {
assert(!_process.running());
}

int prep_cmds_t::run_do(const std::string &working_dir, const boost::process::v1::environment &env, FILE *output, unsigned int flags) {
std::error_code ec;

for (; _cmds_it != _cmds.end(); ++_cmds_it) {
auto &cmd = *_cmds_it;

// Skip empty commands
if (cmd.do_cmd.empty()) {
continue;
}

boost::filesystem::path working_dir_path = working_dir.empty() ?
find_working_directory(cmd.do_cmd, env) :
boost::filesystem::path(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_path, env, output, ec, nullptr);

if (ec) {
BOOST_LOG(error) << "Couldn't run ["sv << cmd.do_cmd << "]: System: "sv << ec.message();
if (flags & FLAG_IGNORE_PERMISSION_DENIED && ec == std::errc::permission_denied) {
continue;
} else {
return -1;
}
}

child.wait();
auto ret = child.exit_code();
if (ret != 0) {
BOOST_LOG(error) << '[' << cmd.do_cmd << "] failed with code ["sv << ret << ']';
return -1;
}
}

return 0;
}

int prep_cmds_t::run_undo(const std::string &working_dir, const boost::process::v1::environment &env, FILE *output) {
std::error_code ec;

for (; _cmds_it != _cmds.begin(); --_cmds_it) {
auto &cmd = *(_cmds_it - 1);

if (cmd.undo_cmd.empty()) {
continue;
}

boost::filesystem::path working_dir_path = working_dir.empty() ?
find_working_directory(cmd.undo_cmd, env) :
boost::filesystem::path(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_path, env, output, ec, nullptr);

if (ec) {
BOOST_LOG(warning) << "System: "sv << ec.message();
}

child.wait();
auto ret = child.exit_code();

if (ret != 0) {
BOOST_LOG(warning) << "Return code ["sv << ret << ']';
}
}

return 0;
}

std::string_view::iterator find_match(std::string_view::iterator begin, std::string_view::iterator end) {
int stack = 0;

Expand Down
34 changes: 31 additions & 3 deletions src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ namespace proc {
std::chrono::seconds exit_timeout;
};

/**
* @brief A list of preparation commands
*/
class prep_cmds_t {
public:
prep_cmds_t(const std::vector<cmd_t> &cmds) :
_cmds(cmds),
_cmds_it(_cmds.begin()) {}

prep_cmds_t() : prep_cmds_t(std::vector<cmd_t>{}) {}

int run_do(const std::string &working_dir, const boost::process::v1::environment &env, FILE *output, unsigned int flags = 0);
int run_undo(const std::string &working_dir, const boost::process::v1::environment &env, FILE *output);

static const unsigned int FLAG_IGNORE_PERMISSION_DENIED = 1;

private:
std::vector<cmd_t> _cmds;
std::vector<cmd_t>::const_iterator _cmds_it;
};

class proc_t {
public:
KITTY_DEFAULT_CONSTR_MOVE_THROW(proc_t)
Expand All @@ -80,7 +101,15 @@ namespace proc {
_apps(std::move(apps)) {
}

int execute(int app_id, std::shared_ptr<rtsp_stream::launch_session_t> launch_session);
/**
* @brief Check the app's existence and set up environment, but does not actually launch it
*/
int prepare(int app_id, std::shared_ptr<rtsp_stream::launch_session_t> launch_session);

/**
* @brief Launch the app after calling `prepare()`
*/
int execute();

/**
* @return `_app_id` if a process is running, otherwise returns `0`
Expand Down Expand Up @@ -110,8 +139,7 @@ namespace proc {
boost::process::v1::group _process_group;

file_t _pipe;
std::vector<cmd_t>::const_iterator _app_prep_it;
std::vector<cmd_t>::const_iterator _app_prep_begin;
prep_cmds_t _app_prep;
};

/**
Expand Down