Skip to content

Commit

Permalink
fix(Unix): Fix LD_PRELOAD variable interfering with Steam games.
Browse files Browse the repository at this point in the history
  • Loading branch information
shdwmtr committed Nov 27, 2024
1 parent 604f093 commit cbfd653
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
13 changes: 10 additions & 3 deletions scripts/posix/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@
# This function filters out the error message "from LD_PRELOAD cannot be preloaded" from 64 bit executables
# The messages are just failing module side effects and not fatal.
filter_output() {
local pattern='from LD_PRELOAD cannot be preloaded'
local patterns=('from LD_PRELOAD cannot be preloaded' 'Fontconfig warning: "' 'Fontconfig error: "')
while IFS= read -r msg; do
if [[ ! "$msg" =~ $pattern ]]; then
local skip=false
for pattern in "${patterns[@]}"; do
if [[ "$msg" =~ $pattern ]]; then
skip=true
break
fi
done
if [[ "$skip" == false ]]; then
printf '%s\n' "$msg"
fi
done
}

steam_output() {
while IFS= read -r msg; do
printf '\033[35mSTEAM\033[0m %s\n' "$msg"
printf '%s\n' "$msg"
done
}

Expand Down
43 changes: 43 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ extern "C"
#endif
}

void RemoveFromLdPreload()
{
const std::string fileToRemove = std::filesystem::path(std::getenv("HOME")) / ".millennium" / "libMillennium.so";

const char* ldPreload = std::getenv("LD_PRELOAD");
if (!ldPreload)
{
std::cerr << "LD_PRELOAD is not set." << std::endl;
return;
}

std::string token;
std::string ldPreloadStr = ldPreload;
std::istringstream iss(ldPreloadStr);
std::vector<std::string> updatedPreload;

// Split LD_PRELOAD by spaces and filter out fileToRemove
while (iss >> token)
{
if (token != fileToRemove)
{
updatedPreload.push_back(token);
}
}

// Join the remaining paths back into a single string
std::ostringstream oss;
for (size_t i = 0; i < updatedPreload.size(); ++i)
{
if (i > 0) oss << ' ';
oss << updatedPreload[i];
}

// Set the updated LD_PRELOAD
if (setenv("LD_PRELOAD", oss.str().c_str(), 1) != 0)
{
perror("setenv");
}
}

#ifdef MILLENNIUM_SHARED
/*
* Trampoline for __libc_start_main() that replaces the real main
Expand All @@ -197,6 +237,9 @@ extern "C"
return orig(main, argc, argv, init, fini, rtld_fini, stack_end);
}

/* Remove the Millennium library from LD_PRELOAD */
RemoveFromLdPreload();
/* Log that we've loaded Millennium */
Logger.Log("Loaded Millennium on {}, system architecture {}", GetLinuxDistro(), GetSystemArchitecture());
/* ... and call it with our custom main function */
return orig(MainHooked, argc, argv, init, fini, rtld_fini, stack_end);
Expand Down

0 comments on commit cbfd653

Please sign in to comment.