diff --git a/rare/commands/subreaper/subreaper_linux.py b/rare/commands/subreaper/subreaper_linux.py index 87402d773..3d2a4d415 100755 --- a/rare/commands/subreaper/subreaper_linux.py +++ b/rare/commands/subreaper/subreaper_linux.py @@ -11,6 +11,8 @@ from pathlib import Path from typing import Any, Generator, List +from .util import find_mangohud_bin, find_mangohud_shim + # Constants defined in prctl.h # See prctl(2) for more details PR_SET_NAME = 15 @@ -99,6 +101,12 @@ def signal_handler(sig, frame): prctl_ret = prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0, 0) logger.debug("prctl PR_SET_CHILD_SUBREAPER exited with status: %s", prctl_ret) + if os.environ.get("MANGOHUD") == "1": + if mangoshim := find_mangohud_shim(): + os.environ["LD_PRELOAD"] = f'{os.environ.get("LD_PRELOAD", "")}:{mangoshim}' + elif mangobin := find_mangohud_bin(): + command.insert(0, mangobin) + pid = os.fork() # pylint: disable=E1101 if pid == -1: logger.error("Fork failed") diff --git a/rare/commands/subreaper/util.py b/rare/commands/subreaper/util.py new file mode 100644 index 000000000..b89d800e3 --- /dev/null +++ b/rare/commands/subreaper/util.py @@ -0,0 +1,39 @@ +import os +import shutil +from typing import List + + +def find_all(name, path) -> List: + result = [] + for root, dirs, files in os.walk(path): + if name in files: + result.append(os.path.join(root, name)) + return result + + +def find_mangohud_shim() -> str: + libs = find_all("libMangoHud_shim.so", "/usr") + if 1 > len(libs) > 2: + return "" + if len(libs) == 1: + return libs[0] + ret = [] + for left, right in zip(*(lib.split("/") for lib in libs)): + if left == right: + ret.append(left) + elif left.startswith("lib") and right.startswith("lib"): + ret.append("$LIB") + else: + return "" + return "/".join(ret) + + +def find_mangohud_bin() -> str: + return shutil.which("mangohud") + + +if __name__ == "__main__": + print(find_mangohud_shim()) + + +__all__ = ["find_mangohud_shim", "find_mangohud_bin"]