Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
process.cpp File Reference

Definitions for the startup and shutdown of the apps started by a streaming Session. More...

#include <algorithm>
#include <array>
#include <filesystem>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/crc.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/token_functions.hpp>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include "config.h"
#include "crypto.h"
#include "display_device.h"
#include "input.h"
#include "logging.h"
#include "platform/common.h"
#include "process.h"
#include "system_tray.h"
#include "utility.h"
#include "platform/windows/utf_utils.h"
#include <share.h>
Include dependency graph for process.cpp:

Classes

class  proc::deinit_t
 RAII helper that runs shutdown cleanup when destroyed. More...

Functions

std::tuple< std::string, std::string > proc::calculate_app_id (const std::string &app_name, std::string app_image_path, int index)
 Calculate a stable id based on name and image data.
uint32_t proc::calculate_crc32 (const std::string &input)
 Calculate the CRC-32 checksum for a string.
std::optional< std::string > proc::calculate_sha256 (const std::string &filename)
 Calculate the SHA-256 digest for a file.
bool proc::check_valid_png (const std::filesystem::path &path)
 Validates a path whether it is a valid PNG.
std::string_view::iterator proc::find_match (std::string_view::iterator begin, std::string_view::iterator end)
 Find the closing parenthesis for an environment-variable expression.
boost::filesystem::path proc::find_working_directory (const std::string &cmd, boost::process::v1::environment &env)
 Resolve the working directory for a configured command.
std::unique_ptr< platf::deinit_t > proc::init ()
 Initialize proc functions.
std::optional< proc::proc_t > proc::parse (const std::string &file_name)
 Parse serialized text into the corresponding runtime representation.
std::string proc::parse_env_val (boost::process::v1::native_environment &env, const std::string_view &val_raw)
 Parse env val.
std::string proc::prepare_command (const std::string &command)
 Prepare a configured command for execution.
void proc::refresh (const std::string &file_name)
 Refresh cached platform state from the operating system.
void proc::terminate_process_group (boost::process::v1::child &child_process, boost::process::v1::group &group, std::chrono::seconds exit_timeout)
 Terminates all child processes in a process group.
std::string proc::validate_app_image_path (std::string app_image_path)
 Validate app image path.

Variables

proc_t proc::proc
 Global process registry used to track and terminate child processes.

Detailed Description

Definitions for the startup and shutdown of the apps started by a streaming Session.

Function Documentation

◆ calculate_app_id()

std::tuple< std::string, std::string > proc::calculate_app_id ( const std::string & app_name,
std::string app_image_path,
int index )

Calculate a stable id based on name and image data.

Returns
Tuple of id calculated without index (for use if no collision) and one with.
Parameters
app_nameApp name.
app_image_pathApp image path.
indexZero-based index of the item being addressed.

◆ calculate_crc32()

uint32_t proc::calculate_crc32 ( const std::string & input)

Calculate the CRC-32 checksum for a string.

Parameters
inputBytes to include in the checksum.
Returns
CRC-32 value for the input bytes.

◆ calculate_sha256()

std::optional< std::string > proc::calculate_sha256 ( const std::string & filename)

Calculate the SHA-256 digest for a file.

Parameters
filenameFile path whose contents should be hashed.
Returns
Lowercase hexadecimal SHA-256 digest, or std::nullopt on read/hash failure.

◆ check_valid_png()

bool proc::check_valid_png ( const std::filesystem::path & path)

Validates a path whether it is a valid PNG.

Parameters
pathThe path to the PNG file.
Returns
true if the file has a valid PNG signature, false otherwise.

◆ find_match()

std::string_view::iterator proc::find_match ( std::string_view::iterator begin,
std::string_view::iterator end )

Find the closing parenthesis for an environment-variable expression.

Parameters
beginIterator positioned at the opening parenthesis.
endEnd iterator for the expression being scanned.
Returns
Iterator for the matching closing parenthesis, or end when unmatched.

◆ find_working_directory()

boost::filesystem::path proc::find_working_directory ( const std::string & cmd,
boost::process::v1::environment & env )

Resolve the working directory for a configured command.

Parameters
cmdCommand line to execute or inspect.
envEnvironment variables for the child process.
Returns
Directory used to launch the command, falling back to PATH lookup when needed.

◆ init()

std::unique_ptr< platf::deinit_t > proc::init ( )

Initialize proc functions.

Returns
Unique pointer to deinit_t to manage cleanup

◆ parse()

std::optional< proc::proc_t > proc::parse ( const std::string & file_name)

Parse serialized text into the corresponding runtime representation.

Parameters
file_nameFile name.
Returns
Parsed value or parse status.

◆ parse_env_val()

std::string proc::parse_env_val ( boost::process::v1::native_environment & env,
const std::string_view & val_raw )

Parse env val.

Parameters
envEnvironment variables for the child process.
val_rawRaw value that may contain substitutions.
Returns
Value with recognized environment-variable substitutions expanded.

◆ prepare_command()

std::string proc::prepare_command ( const std::string & command)

Prepare a configured command for execution.

Parameters
commandConfigured command line.
Returns
Command line with any package-specific launcher prefix applied.

◆ refresh()

void proc::refresh ( const std::string & file_name)

Refresh cached platform state from the operating system.

This function compares the current last modified time of the file to the time it was last parsed. If the file has been modified since it was last parsed (or if it has never been parsed), it will be re-parsed to update the app list.

An optional is used rather than a bare file_time_type because value-initialized file_time_type{} is not a reliable "never parsed" sentinel. On some toolchains (e.g. MSYS2 UCRT64 GCC 16.2), file_time_type::clock::now() compares less-or-equal to file_time_type{}, which would cause the initial parse to be skipped.

When an app is currently running, only the cached app configuration list and environment are updated — active process state (_app_id, _process, _process_group, etc.) is preserved so that the in-flight streaming session can still be terminated properly.

Parameters
file_nameFile name.

< Timestamp of the last successful apps.json parse, or nullopt if never parsed.

◆ terminate_process_group()

void proc::terminate_process_group ( boost::process::v1::child & child_process,
boost::process::v1::group & group,
std::chrono::seconds exit_timeout )

Terminates all child processes in a process group.

Parameters
child_processThe child process itself.
groupThe group of all children in the process tree.
exit_timeoutThe timeout to wait for the process group to gracefully exit.

◆ validate_app_image_path()

std::string proc::validate_app_image_path ( std::string app_image_path)

Validate app image path.

Parameters
app_image_pathCandidate image path from the application configuration.
Returns
Existing PNG path, or the default application image when validation fails.