Skip to content

Commit

Permalink
Merge pull request #4343 from Sonicadvance1/fix_fexserver_search
Browse files Browse the repository at this point in the history
FEXServerClient: Fix searching for FEXServer
  • Loading branch information
Sonicadvance1 authored Feb 10, 2025
2 parents a49d30f + aff3914 commit 7a03681
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Source/Common/FEXServerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Common/FEXServerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 16 additions & 8 deletions Source/Tools/CommonTools/PortabilityInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
#include "Common/Config.h"

namespace FEX {
static inline std::optional<fextl::string> 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<size_t>(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");
Expand All @@ -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<size_t>(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
3 changes: 2 additions & 1 deletion Source/Tools/FEXLoader/FEXLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7a03681

Please sign in to comment.