diff --git a/Source/Common/FEXServerClient.cpp b/Source/Common/FEXServerClient.cpp index 32180693e..70d310fec 100644 --- a/Source/Common/FEXServerClient.cpp +++ b/Source/Common/FEXServerClient.cpp @@ -220,7 +220,7 @@ int ConnectToServer(ConnectionOption ConnectionOption) { return -1; } -bool SetupClient(char* InterpreterPath) { +bool SetupClient(std::string_view InterpreterPath) { ServerFD = FEXServerClient::ConnectToAndStartServer(InterpreterPath); if (ServerFD == -1) { return false; @@ -238,7 +238,7 @@ bool SetupClient(char* InterpreterPath) { return true; } -int ConnectToAndStartServer(char* InterpreterPath) { +int ConnectToAndStartServer(std::string_view InterpreterPath) { int ServerFD = ConnectToServer(ConnectionOption::NoPrintConnectionError); if (ServerFD == -1) { // Couldn't connect to the server. Start one @@ -250,7 +250,7 @@ int ConnectToAndStartServer(char* InterpreterPath) { return -1; } - fextl::string FEXServerPath = FHU::Filesystem::ParentPath(InterpreterPath) + "/FEXServer"; + fextl::string FEXServerPath = fextl::fmt::format("{}/FEXServer", InterpreterPath); // Check if a local FEXServer next to FEXInterpreter exists // If it does then it takes priority over the installed one if (!FHU::Filesystem::Exists(FEXServerPath)) { diff --git a/Source/Common/FEXServerClient.h b/Source/Common/FEXServerClient.h index 6bad43fc0..095885043 100644 --- a/Source/Common/FEXServerClient.h +++ b/Source/Common/FEXServerClient.h @@ -56,14 +56,14 @@ fextl::string GetServerSocketName(); fextl::string GetServerSocketPath(); int GetServerFD(); -bool SetupClient(char* InterpreterPath); +bool SetupClient(std::string_view InterpreterPath); /** * @brief Connect to and start a FEXServer instance if required * * @return socket FD for communicating with server */ -int ConnectToAndStartServer(char* InterpreterPath); +int ConnectToAndStartServer(std::string_view InterpreterPath); enum class ConnectionOption { Default, diff --git a/Source/Tools/CommonTools/PortabilityInfo.h b/Source/Tools/CommonTools/PortabilityInfo.h index 13e01080c..505819f44 100644 --- a/Source/Tools/CommonTools/PortabilityInfo.h +++ b/Source/Tools/CommonTools/PortabilityInfo.h @@ -3,6 +3,19 @@ #include "Common/Config.h" namespace FEX { +static inline std::optional GetSelfPath() { + // Read the FEXInterpreter path from `/proc/self/exe` which is always a symlink to the absolute path of the executable running. + // This way we can get the parent path that the application is executing from. + char SelfPath[PATH_MAX]; + auto Result = readlink("/proc/self/exe", SelfPath, PATH_MAX); + if (Result == -1) { + return std::nullopt; + } + + std::string_view SelfPathView {SelfPath, std::min(PATH_MAX, Result)}; + return fextl::string {SelfPathView.substr(0, SelfPathView.find_last_of('/') + 1)}; +} + static inline FEX::Config::PortableInformation ReadPortabilityInformation() { const FEX::Config::PortableInformation BadResult {false, {}}; const char* PortableConfig = getenv("FEX_PORTABLE"); @@ -17,17 +30,12 @@ static inline FEX::Config::PortableInformation ReadPortabilityInformation() { return BadResult; } - // Read the FEXInterpreter path from `/proc/self/exe` which is always a symlink to the absolute path of the executable running. - // This way we can get the parent path that the application is executing from. - char SelfPath[PATH_MAX]; - auto Result = readlink("/proc/self/exe", SelfPath, PATH_MAX); - if (Result == -1) { + auto SelfPath = GetSelfPath(); + if (!SelfPath) { return BadResult; } - std::string_view SelfPathView {SelfPath, std::min(PATH_MAX, Result)}; - // Extract the absolute path from the FEXInterpreter path - return {true, fextl::string {SelfPathView.substr(0, SelfPathView.find_last_of('/') + 1)}}; + return {true, *SelfPath}; } } // namespace FEX diff --git a/Source/Tools/FEXLoader/FEXLoader.cpp b/Source/Tools/FEXLoader/FEXLoader.cpp index 6de4ba572..de6140228 100644 --- a/Source/Tools/FEXLoader/FEXLoader.cpp +++ b/Source/Tools/FEXLoader/FEXLoader.cpp @@ -340,7 +340,8 @@ int main(int argc, char** argv, char** const envp) { } // Ensure FEXServer is setup before config options try to pull CONFIG_ROOTFS - if (!FEXServerClient::SetupClient(argv[0])) { + auto SelfPath = FEX::GetSelfPath(); + if (!FEXServerClient::SetupClient(SelfPath.value_or(argv[0]))) { LogMan::Msg::EFmt("FEXServerClient: Failure to setup client"); return -1; }