From b74aa979967db5c979e1d710cffaa189f402dd6c Mon Sep 17 00:00:00 2001 From: The412Banner Date: Sun, 20 Sep 2026 00:55:37 -0400 Subject: [PATCH] Bring the runtime up to date with Bannerlator, and end a session the way proot can survive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From Bannerlator's feat/linux-gamescope-runtime since this repo was cut: - Session teardown (4509d788): proot was SIGKILLed, which never lets its --kill-on-exit cleanup run; its tracees lived on with no tracer, every seccomp-trapped syscall failing with ENOSYS, spinning at a full core for over an hour. Now SIGTERM, a grace for proot's own cleanup, SIGKILL only if it will not go, then a sweep of the tree it had — snapshotted first, each pid checked against its start time. - The Steam client's controller interposer is its own source (fakeinput_steam.cpp, 28922e7c), separate from the one preloaded into Wine. - The seeder runs on a beat so a prefix the client creates mid-session is covered before its install scripts run (6ee86bff). - GE-Proton and proton-cachyos as compatibility tools: the registrar bannerlator-proton-extra and the compat script's adopt_extras (03867656). - space.c in the session shim: a library's own free space, not the phone's. The env-vs-args ordering bug (98d38931) does not apply here: nothing is added after the script name. --- .github/workflows/build.yml | 7 +- .../{fakeinput.cpp => fakeinput_steam.cpp} | 0 .../launcher/session/SessionFiles.kt | 1 + .../launcher/session/SessionService.kt | 73 +++++- .../usr/local/bin/bannerlator-proton-extra | 217 ++++++++++++++++++ .../overlay/usr/local/bin/bannerlator-session | 56 ++++- .../usr/local/bin/bannerlator-steam-compat | 144 +++++++++++- .../usr/local/bin/bannerlator-steam-library | 57 ++++- tools/linuxfs/preload/space.c | 79 +++++++ 9 files changed, 622 insertions(+), 12 deletions(-) rename app/src/main/cpp/{fakeinput.cpp => fakeinput_steam.cpp} (100%) create mode 100644 tools/linuxfs/overlay/usr/local/bin/bannerlator-proton-extra create mode 100644 tools/linuxfs/preload/space.c diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5c3366b796..7116775464 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -67,7 +67,7 @@ jobs: uses: actions/cache@v4 with: path: app/src/main/assets/linuxfs - key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/overlay/usr/local/bin/*') }} + key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/overlay/usr/local/bin/*') }} - name: Build libfakeinput.so and libblsession.so (glibc aarch64) if: steps.preloads.outputs.cache-hit != 'true' @@ -82,7 +82,7 @@ jobs: # ahead of it is a version conflict waiting to happen. Only libc is left as a dependency. aarch64-linux-gnu-g++ -shared -fPIC -O2 -Wall -Wno-attributes -Wno-nonnull-compare \ -pthread -std=c++17 -static-libstdc++ -static-libgcc \ - -o "$D/libfakeinput.so" app/src/main/cpp/fakeinput.cpp -ldl + -o "$D/libfakeinput.so" app/src/main/cpp/fakeinput_steam.cpp -ldl aarch64-linux-gnu-strip --strip-unneeded "$D/libfakeinput.so" aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$D/libblsession.so" tools/linuxfs/preload/*.c -ldl @@ -113,10 +113,11 @@ jobs: grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; } done SESSION=$(syms "$D/libblsession.so") - for sym in socket bind getsockname setsockopt; do + for sym in socket bind getsockname setsockopt statfs statvfs; do grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; } done test -f "$D/usr/local/bin/bannerlator-session" + test -f "$D/usr/local/bin/bannerlator-proton-extra" echo "preloads check out" # ── The native build ───────────────────────────────────────────────────────────────── diff --git a/app/src/main/cpp/fakeinput.cpp b/app/src/main/cpp/fakeinput_steam.cpp similarity index 100% rename from app/src/main/cpp/fakeinput.cpp rename to app/src/main/cpp/fakeinput_steam.cpp diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt index 097400b2de..ebcb2862f0 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt @@ -30,6 +30,7 @@ object SessionFiles { "usr/local/bin/bannerlator-steam-install" to "usr/local/bin/bannerlator-steam-install", "usr/local/bin/bannerlator-steam-library" to "usr/local/bin/bannerlator-steam-library", "usr/local/bin/bannerlator-seed-redists" to "usr/local/bin/bannerlator-seed-redists", + "usr/local/bin/bannerlator-proton-extra" to "usr/local/bin/bannerlator-proton-extra", ) for ((asset, relative) in files) { val target = File(root, relative) diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt index 068754741a..ca2f2676be 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt @@ -175,6 +175,66 @@ class SessionService : Service() { Log.i(TAG, "session pid $sessionPid, log ${sessionLog.path}") } + private fun teardown(prootPid: Int) { + val tree = descendants(prootPid) + android.os.Process.sendSignal(prootPid, 15) // SIGTERM + if (!waitForExit(prootPid, GRACE_MS)) { + Log.w(TAG, "proot $prootPid did not exit on SIGTERM; killing it") + android.os.Process.killProcess(prootPid) + } + var killed = 0 + for ((pid, started) in tree) { + val stat = readStat(pid) ?: continue // already gone + if (stat.second != started) continue // same number, different process + android.os.Process.killProcess(pid) + killed++ + } + if (killed > 0) Log.i(TAG, "swept $killed process(es) proot left behind") + } + + /** Every process under [root], as (pid, start time), from a single walk of /proc. */ + private fun descendants(root: Int): List> { + val started = HashMap() + val children = HashMap>() + File("/proc").listFiles()?.forEach { entry -> + val pid = entry.name.toIntOrNull() ?: return@forEach + val stat = readStat(pid) ?: return@forEach + started[pid] = stat.second + children.getOrPut(stat.first) { ArrayList() }.add(pid) + } + val out = ArrayList>() + val queue = ArrayDeque().apply { add(root) } + val me = android.os.Process.myPid() + while (queue.isNotEmpty()) { + for (kid in children[queue.removeFirst()] ?: continue) { + if (kid <= 1 || kid == me || kid == root) continue + val when_ = started[kid] ?: continue + out.add(Pair(kid, when_)) + queue.add(kid) + } + } + return out + } + + /** (parent pid, start time) from /proc/pid/stat, or null when the process is gone. */ + private fun readStat(pid: Int): Pair? { + val stat = try { File("/proc/$pid/stat").readText() } catch (e: Exception) { return null } + val close = stat.lastIndexOf(')') + if (close < 0 || close + 2 >= stat.length) return null + val fields = stat.substring(close + 2).trim().split(Regex("\\s+")) + if (fields.size < 20) return null + return try { Pair(fields[1].toInt(), fields[19].toLong()) } catch (e: NumberFormatException) { null } + } + + private fun waitForExit(pid: Int, timeoutMs: Long): Boolean { + val deadline = System.currentTimeMillis() + timeoutMs + while (System.currentTimeMillis() < deadline) { + if (!File("/proc/$pid").exists()) return true + try { Thread.sleep(50) } catch (e: InterruptedException) { return false } + } + return !File("/proc/$pid").exists() + } + /** * proot's --kill-on-exit takes its tracees down, but a session that died from the inside * (the client asserting, Xwayland going) leaves gamescopereaper and the session script @@ -203,10 +263,17 @@ class SessionService : Service() { private fun stopSession(status: Int) { if (!SessionState.running) return SessionState.running = false - // proot runs with --kill-on-exit, so killing it takes the whole guest tree with it. + // proot's --kill-on-exit takes the guest tree down only if proot gets to run it, and + // SIGKILL never lets it. A SIGKILLed proot left its tracees alive with no tracer: every + // seccomp-trapped syscall then failed with ENOSYS, they spun on retries at a full core + // for over an hour, and the reaper could not even open /proc to kill them. So: SIGTERM, + // a grace for proot's own cleanup, SIGKILL only if it will not go, then a sweep of the + // tree it had — snapshotted first, each pid checked against its start time so a number + // reused by a new process is never touched. Same shape as Bannerlator's fix (4509d788). if (sessionPid != -1) { - android.os.Process.killProcess(sessionPid) + val prootPid = sessionPid sessionPid = -1 + Thread({ teardown(prootPid) }, "session-teardown").start() } components.reversed().forEach { try { @@ -330,6 +397,8 @@ class SessionService : Service() { /** Command lines that can only belong to a session of ours. */ private val STRAGGLERS = listOf("bannerlator-session", "gamescope", "Xwayland", "steamrtarm64", "steamwebhelper", "linuxfs/opt/android-host/proot", "pulseaudio/libpulseaudio.so") + /** How long proot gets to run its own cleanup before it is killed outright. */ + private const val GRACE_MS = 1200L private const val NO_PAD_SWITCH = "Download/steamdeck-no-pad" private const val PAD_LOG_SWITCH = "Download/steamdeck-pad-log" diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-proton-extra b/tools/linuxfs/overlay/usr/local/bin/bannerlator-proton-extra new file mode 100644 index 0000000000..fd63f6320c --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-proton-extra @@ -0,0 +1,217 @@ +#!/usr/bin/python3 +# Installs a third-party Proton build into the client's compatibilitytools.d, so it appears in the +# Compatibility dropdown beside Bannerlator Proton (ARM64) and can be picked per game. +# +# bannerlator-proton-extra ge [--tag GE-Proton11-7] +# bannerlator-proton-extra cachyos [--tag cachyos-11.0-20260703-slr] +# bannerlator-proton-extra +# bannerlator-proton-extra /path/to/an/aarch64/tarball +# bannerlator-proton-extra --list what is installed now +# +# Both projects publish native ARM64 builds - GloriousEggroll as "*-aarch64.tar.gz", CachyOS as +# "*-arm64.tar.xz" - and both are ordinary Proton trees: a python entry point beside a files/ +# directory holding Wine, DXVK and VKD3D. That is the same shape as the depot the Steam client +# downloads, so the runtime can run them the same way. +# +# What it cannot use is their toolmanifest. Like Valve's ARM64 depot they declare +# require_tool_appid, which makes Steam stack pressure-vessel underneath - and pressure-vessel +# needs unprivileged user namespaces, which an Android app does not get. Patching that out is +# bannerlator-steam-compat's job (adopt_extras); this script only puts the tree in place and then +# calls it, so an install is usable without restarting the client. +import hashlib +import json +import os +import re +import shutil +import subprocess +import sys +import urllib.error +import urllib.request + +RELEASES = "https://api.github.com/repos/%s/releases" +# Each source: the GitHub repo, and how its ARM64 asset is named. Ordered newest-release-first by +# the API, so "latest usable" is the first release that actually carries an ARM64 asset - the +# projects do not build one for every tag. +SOURCES = { + "ge": ("GloriousEggroll/proton-ge-custom", re.compile(r"aarch64\.tar\.(gz|xz)$")), + "cachyos": ("CachyOS/proton-cachyos", re.compile(r"arm64\.tar\.(gz|xz)$")), +} +USER_AGENT = "bannerlator-proton-extra" +# Room for the tarball and the tree it unpacks to. GE-Proton's aarch64 build is ~650 MB packed and +# unpacks to about three times that; refusing early beats a half-written tool directory. +NEED_BYTES = 4 * 1024 * 1024 * 1024 + + +def log(*args): + print("bannerlator-proton-extra:", *args, flush=True) + + +def fetch_json(url): + req = urllib.request.Request(url, headers={ + "User-Agent": USER_AGENT, "Accept": "application/vnd.github+json"}) + with urllib.request.urlopen(req, timeout=60) as r: + return json.load(r) + + +def pick_asset(source): + """(release tag, asset url, sha512 url or None) for the newest release with an ARM64 build.""" + repo, pattern = SOURCES[source] + for release in fetch_json(RELEASES % repo + "?per_page=15"): + if release.get("draft"): + continue + assets = {a["name"]: a["browser_download_url"] for a in release.get("assets", ())} + for name in sorted(assets): + if pattern.search(name): + sums = [u for n, u in assets.items() + if n.startswith(name.split(".tar")[0]) and "sha512" in n] + return release["tag_name"], assets[name], (sums[0] if sums else None) + return None, None, None + + +def release_asset(source, tag): + repo, pattern = SOURCES[source] + release = fetch_json(RELEASES % repo + "/tags/" + tag) + assets = {a["name"]: a["browser_download_url"] for a in release.get("assets", ())} + for name in sorted(assets): + if pattern.search(name): + sums = [u for n, u in assets.items() + if n.startswith(name.split(".tar")[0]) and "sha512" in n] + return tag, assets[name], (sums[0] if sums else None) + raise SystemExit("bannerlator-proton-extra: %s has no ARM64 asset" % tag) + + +def download(url, path): + """Resumable, because this is hundreds of megabytes over a phone's connection. curl is used + rather than urllib so a retry continues rather than starting again.""" + log("downloading %s" % os.path.basename(path)) + rc = subprocess.call([ + "curl", "-fL", "--retry", "5", "--retry-delay", "3", "--retry-connrefused", + "-C", "-", "-A", USER_AGENT, "-o", path, url]) + if rc != 0: + raise SystemExit("bannerlator-proton-extra: download failed (curl %d)" % rc) + log("downloaded %.0f MB" % (os.path.getsize(path) / 1048576.0)) + + +def verify(path, sums_url): + if not sums_url: + log("no checksum published; skipping verification") + return + try: + req = urllib.request.Request(sums_url, headers={"User-Agent": USER_AGENT}) + with urllib.request.urlopen(req, timeout=60) as r: + want = r.read().decode("utf-8", "replace").split()[0].lower() + except (urllib.error.URLError, IndexError, OSError) as e: + log("could not read the checksum (%s); skipping verification" % e) + return + h = hashlib.sha512() + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(4 << 20), b""): + h.update(chunk) + if h.hexdigest() != want: + os.unlink(path) + raise SystemExit("bannerlator-proton-extra: checksum mismatch; the download was deleted") + log("checksum ok") + + +def extract(tarball, into): + os.makedirs(into, exist_ok=True) + log("unpacking (a few minutes; nothing is shown while tar runs)") + rc = subprocess.call(["tar", "-xf", tarball, "-C", into]) + if rc != 0: + raise SystemExit("bannerlator-proton-extra: tar failed (%d)" % rc) + # A Proton tarball holds one top-level directory. Take whichever entry has the entry point so a + # stray ._ file or a nested layout does not decide it. + entries = [e for e in sorted(os.listdir(into)) if not e.startswith(".")] + for entry in entries: + path = os.path.join(into, entry) + if os.path.isdir(path) and os.path.isfile(os.path.join(path, "toolmanifest.vdf")): + return path + if len(entries) == 1 and os.path.isdir(os.path.join(into, entries[0])): + return os.path.join(into, entries[0]) + raise SystemExit("bannerlator-proton-extra: no Proton tree in the archive") + + +def installed(tools): + out = [] + for entry in sorted(os.listdir(tools)) if os.path.isdir(tools) else (): + if entry.startswith("."): + continue + if os.path.isfile(os.path.join(tools, entry, "toolmanifest.vdf")): + out.append(entry) + return out + + +def main(): + argv = sys.argv[1:] + if not argv: + raise SystemExit(__doc__ or "usage: bannerlator-proton-extra ge|cachyos|") + steam_root = argv.pop(0) + tools = os.path.join(steam_root, "compatibilitytools.d") + os.makedirs(tools, exist_ok=True) + + if not argv or argv[0] in ("--list", "-l"): + for name in installed(tools): + log("installed: %s" % name) + return + + what = argv.pop(0) + tag = None + if argv and argv[0] in ("--tag", "-t"): + argv.pop(0) + tag = argv.pop(0) if argv else None + + # A tarball already on the device is used as it is: a session that has to pull 650 MB down + # first shows nothing on screen for ten minutes, so the download can be done ahead of time and + # handed over by path. + local = None + if what in SOURCES: + tag, url, sums = release_asset(what, tag) if tag else pick_asset(what) + if not url: + raise SystemExit("bannerlator-proton-extra: no ARM64 build published for %s" % what) + log("%s -> %s" % (what, tag)) + elif what.startswith("http"): + url, sums = what, None + elif os.path.isfile(what): + local, url, sums = what, None, None + log("using %s" % what) + else: + raise SystemExit( + "bannerlator-proton-extra: expected ge, cachyos, a URL or a tarball path, got %r" % what) + + free = shutil.disk_usage(tools).free + if free < NEED_BYTES: + raise SystemExit("bannerlator-proton-extra: needs about %d GB free, has %.1f GB" % ( + NEED_BYTES // (1024 ** 3), free / float(1024 ** 3))) + + staging = os.path.join(tools, ".bannerlator-download") + os.makedirs(staging, exist_ok=True) + if local: + tarball = local + else: + tarball = os.path.join(staging, os.path.basename(url.split("?")[0])) + download(url, tarball) + verify(tarball, sums) + + unpacked = os.path.join(staging, "unpacked") + shutil.rmtree(unpacked, ignore_errors=True) + tree = extract(tarball, unpacked) + name = os.path.basename(tree) + final = os.path.join(tools, name) + # Replaced rather than merged: a half-written tree from an interrupted run would otherwise + # leave stale files behind that Proton would load. + if os.path.exists(final): + shutil.rmtree(final, ignore_errors=True) + os.rename(tree, final) + if local: + shutil.rmtree(os.path.join(staging, "unpacked"), ignore_errors=True) + else: + shutil.rmtree(staging, ignore_errors=True) + log("installed %s" % name) + + # Patches out require_tool_appid and wraps the entry point, which is what makes it launchable + # here. Doing it now means the client picks the tool up on its next restart with no extra step. + subprocess.call(["bannerlator-steam-compat", steam_root]) + + +if __name__ == "__main__": + main() diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-session b/tools/linuxfs/overlay/usr/local/bin/bannerlator-session index 73b9275d8f..78b75704f2 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-session +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-session @@ -118,6 +118,39 @@ case $mode in steam_root=$XDG_DATA_HOME/Steam # Register the card library (bound at /mnt/bannerlator-sd) with the client - internal storage is # its main library already - and the ARM64 Proton that lets it launch the Windows ones. + # A request left in ~/.bl-proton-extra installs a third-party Proton before the client starts, + # so it is in the Compatibility dropdown the first time the user looks. One per line, each the + # arguments to bannerlator-proton-extra: "ge", "cachyos", "ge --tag GE-Proton11-7", or a URL. + # Hundreds of megabytes each, so it is only ever done on request, and the line is dropped once + # it has succeeded - a failed download stays and is retried next session. + if [ -s "$HOME/.bl-proton-extra" ]; then + _bl_left=$HOME/.bl-proton-extra.left + : > "$_bl_left" + while read -r _bl_extra || [ -n "$_bl_extra" ]; do + case ${_bl_extra# } in ""|"#"*) continue ;; esac + step "installing Proton '$_bl_extra' (large download, several minutes)" + if bannerlator-proton-extra "$steam_root" $_bl_extra; then + step "installed Proton '$_bl_extra'" + else + step "Proton '$_bl_extra' failed; will retry next session" + echo "$_bl_extra" >> "$_bl_left" + fi + done < "$HOME/.bl-proton-extra" + mv -f "$_bl_left" "$HOME/.bl-proton-extra" + fi + # steam:// URLs left in ~/.bl-steam-urls are handed to the client on its command line. The app + # writes one when the user asks for a Proton that is one of Valve's own depots rather than a + # tarball: those are not files anywhere, the client fetches them like any other app. One per + # line, and the file is cleared once they have been passed on - the client either acts on a URL + # or it does not, and repeating it every session would re-ask forever. + if [ -s "$HOME/.bl-steam-urls" ]; then + while read -r _bl_url || [ -n "$_bl_url" ]; do + case ${_bl_url# } in steam://*) ;; *) continue ;; esac + step "asking the client for $_bl_url" + set -- "$@" "$_bl_url" + done < "$HOME/.bl-steam-urls" + rm -f "$HOME/.bl-steam-urls" + fi step "registering the app's game library with Steam" bannerlator-steam-library "$steam_root" || true step "setting up the ARM64 Proton compatibility tool" @@ -136,10 +169,27 @@ case $mode in # titles are installed: a title installed this session gets the client's own per-app Proton # choice, which it cannot start. Refresh the per-app entries every so often; the registrar is # idempotent, so a repeat costs nothing. + # The seeder has the same problem and a worse consequence. It marks the shared redistributables + # as already run in each prefix that exists, and a game's prefix is not created until the + # client launches it - so a title played for the first time this session was never covered, and + # its install scripts run for real. That is exactly the hang the seeder exists to prevent: Need + # for Speed Payback sat on "Running install script" with vcredist_x86.exe blocked in pipe_read, + # holding both ends of a pipe nobody would write to, while the client waited for it to exit. + # Seeding again on the same beat covers a prefix the moment it appears. + # The seeder runs on a shorter beat than the registrar because it is racing something. The + # client creates a prefix when it launches a title and runs that title's install scripts + # moments later, so the seeder has only that gap to mark the redistributables as already run. + # It stamps each prefix it has covered and skips it thereafter, so the extra passes cost + # almost nothing. ( - for _ in $(seq 1 240); do - sleep 15 - bannerlator-steam-compat "$steam_root" >/dev/null 2>&1 || true + beat=0 + for _ in $(seq 1 720); do + sleep 5 + bannerlator-seed-redists "$steam_root" >/dev/null 2>&1 || true + beat=$((beat + 1)) + if [ "$((beat % 3))" -eq 0 ]; then + bannerlator-steam-compat "$steam_root" >/dev/null 2>&1 || true + fi done ) & # Debug aid while the runtime is being brought up: an app id dropped in ~/.bl-autolaunch is diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat index 73a5feda45..44d2416bc8 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat @@ -100,6 +100,56 @@ echo "bannerlator-proton: no ARM64 Proton depot yet - the client downloads one a exit 1 """ % " ".join('"%s"' % n for n in SOURCES) +# A third-party Proton dropped into compatibilitytools.d - GE-Proton, proton-cachyos, anything of +# that shape - is adopted rather than used as shipped, for the same two reasons Valve's ARM64 depot +# is: its manifest declares require_tool_appid, and the client's overlay library has to come out of +# LD_PRELOAD. Its own entry point is left untouched and called through this wrapper. +EXTRA_WRAPPER = "bannerlator-proton-wrap" +EXTRA_MANIFEST_VDF = """"manifest" +{ + "version" "2" + "commandline" "/%s %%verb%%" + "use_sessions" "1" + "compatmanager_layer_name" "proton" +} +""" % EXTRA_WRAPPER +# %s is the tool's own entry point, read out of the manifest it shipped with. +EXTRA_WRAPPER_SH = """#!/bin/bash +# Bannerlator: runs a Proton installed into compatibilitytools.d without the Steam Linux Runtime +# container its own manifest asks for - pressure-vessel needs unprivileged user namespaces, which +# an Android app does not get, and the launch dies before Proton is reached. +verb=$1; shift +here=$(dirname "$0") +# See bannerlator-proton: in a gamescope session the client's overlay draws nothing, but it stands +# in front of open() and read() on /dev/input and answers every read of the session's pad with +# nothing - a controller that never reports. Dropped here; anything else in LD_PRELOAD is kept. +kept= +IFS=': ' +for lib in ${LD_PRELOAD:-}; do + case $lib in */gameoverlayrenderer.so) ;; *) kept=${kept:+$kept:}$lib ;; esac +done +unset IFS +if [ -n "$kept" ]; then export LD_PRELOAD="$kept"; else unset LD_PRELOAD; fi +# Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be +# started from its own tree. +exec "$here/%s" "$verb" "$@" +""" +EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools" +{ + "compat_tools" + { + "%s" + { + "install_path" "." + "display_name" "%s (Bannerlator)" + "from_oslist" "windows" + "to_oslist" "linux" + } + } +} +""" + + TOKEN = re.compile(r'\s*(?:"((?:[^"\\]|\\.)*)"|([{}])|//[^\n]*)') @@ -169,6 +219,88 @@ def serialize(tokens): return "\n".join(out) + "\n" +def first_key(path, section): + """The single key naming a block inside `section`, or None. Used to keep a third-party tool's + own internal name, so a mapping the user already made against it keeps working.""" + try: + with open(path, encoding="utf-8", errors="replace") as f: + tokens = tokenize(f.read()) + except OSError: + return None + block = find_block(tokens, section) + if block is None: + return None + for i in range(block + 1, block_end(tokens, block)): + if tokens[i] == "}": + break + if i + 1 < len(tokens) and tokens[i + 1] == "{": + return tokens[i].strip('"') + return None + + +def manifest_entry_point(path): + """The script a toolmanifest's commandline runs, relative to the tool directory.""" + try: + with open(path, encoding="utf-8", errors="replace") as f: + tokens = tokenize(f.read()) + except OSError: + return None + for i in range(len(tokens) - 1): + if tokens[i].lower() == '"commandline"': + command = tokens[i + 1].strip('"').split() + if command: + return command[0].lstrip("/") + return None + + +def adopt_extras(tools): + """Makes every other Proton in compatibilitytools.d launchable here, and returns the tool names + to leave alone in CompatToolMapping - the whole point of installing one is to pick it per game, + so register_default must not repoint it back at ours.""" + adopted = {} + try: + entries = sorted(os.listdir(tools)) + except OSError: + return adopted + for entry in entries: + if entry == TOOL or entry.startswith("."): + continue + tool_dir = os.path.join(tools, entry) + manifest = os.path.join(tool_dir, "toolmanifest.vdf") + if not os.path.isdir(tool_dir) or not os.path.isfile(manifest): + continue + # The shipped manifest is kept, once, and stays the source of the entry point: re-running + # must not read back our own rewritten commandline and wrap the wrapper. + original = manifest + ".bannerlator-orig" + if not os.path.isfile(original): + try: + shutil.copy2(manifest, original) + except OSError: + continue + entry_point = manifest_entry_point(original) + if not entry_point or entry_point == EXTRA_WRAPPER: + entry_point = "proton" + if not os.path.exists(os.path.join(tool_dir, entry_point)): + print("bannerlator-steam-compat: %s has no %s; left alone" % (entry, entry_point)) + continue + compat_vdf = os.path.join(tool_dir, "compatibilitytool.vdf") + name = first_key(compat_vdf, ["compatibilitytools", "compat_tools"]) or entry + wrapper = os.path.join(tool_dir, EXTRA_WRAPPER) + try: + with open(wrapper, "w", encoding="utf-8") as f: + f.write(EXTRA_WRAPPER_SH % entry_point) + os.chmod(wrapper, 0o755) + with open(manifest, "w", encoding="utf-8") as f: + f.write(EXTRA_MANIFEST_VDF) + with open(compat_vdf, "w", encoding="utf-8") as f: + f.write(EXTRA_COMPAT_TOOL_VDF % (name, entry)) + except OSError as e: + print("bannerlator-steam-compat: could not adopt %s (%s)" % (entry, e)) + continue + adopted[name] = entry + return adopted + + # Steam's own tools and runtimes live in steamapps beside the games; they are not titles and must # not be mapped to a compatibility tool. NOT_GAMES = frozenset(( @@ -200,9 +332,11 @@ def installed_apps(steam_root): return sorted(apps, key=int) -def register_default(config_path, apps=(), name=None): +def register_default(config_path, apps=(), name=None, protect=()): """Writes the mapping. `name` defaults to our own tool; the first-run seed passes Valve's.""" tool = name or TOOL + # Tool names the user may deliberately have chosen (see adopt_extras); never repointed. + keep = {n.lower() for n in protect} with open(config_path, encoding="utf-8", errors="replace") as f: tokens = tokenize(f.read()) steam = find_block(tokens, ["InstallConfigStore", "Software", "Valve", "Steam"]) @@ -218,7 +352,8 @@ def register_default(config_path, apps=(), name=None): # Repoint every per-app entry the client aimed at a Proton it cannot actually start. for i in range(mapping, block_end(tokens, mapping)): named = tokens[i + 1].strip('"').lower() - if tokens[i] == '"name"' and named.startswith("proton") and named != tool: + if (tokens[i] == '"name"' and named.startswith("proton") + and named != tool.lower() and named not in keep): tokens[i + 1] = '"%s"' % tool if find_block(tokens[mapping + 1:], ["0"]) is None: tokens[mapping + 1:mapping + 1] = entry @@ -270,6 +405,9 @@ def main(): tools = os.path.join(steam_root, "compatibilitytools.d") os.makedirs(tools, exist_ok=True) build_tool(os.path.join(tools, TOOL)) + extras = adopt_extras(tools) + for name, directory in sorted(extras.items()): + print("bannerlator-steam-compat: adopted %s as %s" % (directory, name)) depot = find_source(steam_root) print("bannerlator-steam-compat: registered %s (%s)" % ( TOOL, "depot %s present" % os.path.basename(depot) if depot else @@ -277,7 +415,7 @@ def main(): config = os.path.join(steam_root, "config", "config.vdf") if os.path.isfile(config): apps = installed_apps(steam_root) - register_default(config, apps) + register_default(config, apps, protect=extras) print("bannerlator-steam-compat: default and %d installed title(s) set to %s" % (len(apps), TOOL)) diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-library b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-library index de553f43d6..cea880db4c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-library +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-library @@ -11,7 +11,10 @@ import sys # The client's own library is internal storage (its steamapps/common is bound to the app's root); # the card is the one extra library folder. -LIBRARIES = (("/mnt/bannerlator-sd", "Bannerlator (card)"),) +LIBRARIES = (("/mnt/bannerlator-sd", "SD Card"),) +# The client calls its own library "Local Drive (/)", which says nothing about where that is. +# It is the phone's internal storage, so say so. +MAIN_LABEL = "Internal Storage" def register(steam_root, library, label): @@ -92,12 +95,64 @@ def forget(steam_root, library): print("bannerlator-steam-library: forgot %s" % library) +def relabel(steam_root, target, label): + """Names a library for what it is. The client writes its own entry with an empty label and + falls back to showing the mount point - "Local Drive (/)" - so the install dialog gives no + clue which of the two places is the phone and which is the card. A library registered on an + earlier run keeps whatever label it was given then, so both are named here every time.""" + path = os.path.join(steam_root, "steamapps", "libraryfolders.vdf") + if not os.path.isfile(path): + return + with open(path, encoding="utf-8", errors="replace") as f: + lines = f.read().split("\n") + out, depth, block, changed = [], 0, None, False + for line in lines: + if depth == 1 and re.fullmatch(r'\t"\d+"', line): + block = [line] + continue + if block is not None: + block.append(line) + depth += line.count("{") - line.count("}") + if depth == 1: + text = "\n".join(block) + if re.search(r'"path"\s+"%s"' % re.escape(target), text): + new = re.sub(r'("label"\s+")[^"]*(")', r"\g<1>%s\g<2>" % label, text, count=1) + if new != text: + changed = True + text = new + out.extend(text.split("\n")) + block = None + continue + depth += line.count("{") - line.count("}") + out.append(line) + if changed: + with open(path, "w", encoding="utf-8") as f: + f.write("\n".join(out)) + print("bannerlator-steam-library: labelled %s %r" % (target, label)) + # A library also carries its own libraryfolder.vdf, and the client takes the label from there + # back into the list above - so editing the list alone is undone on the next start. The + # client's own library has no such file, which is why that one kept the new name and the card + # went back to the name it was first registered with. + marker = os.path.join(target, "libraryfolder.vdf") + if not os.path.isfile(marker): + return + with open(marker, encoding="utf-8", errors="replace") as f: + text = f.read() + new = re.sub(r'("label"\s+")[^"]*(")', r"\g<1>%s\g<2>" % label, text, count=1) + if new != text: + with open(marker, "w", encoding="utf-8") as f: + f.write(new) + print("bannerlator-steam-library: labelled %s in its own marker too" % target) + + def main(): steam_root = sys.argv[1] for library in RETIRED: forget(steam_root, library) + relabel(steam_root, steam_root, MAIN_LABEL) for library, label in LIBRARIES: register(steam_root, library, label) + relabel(steam_root, library, label) if __name__ == "__main__": diff --git a/tools/linuxfs/preload/space.c b/tools/linuxfs/preload/space.c new file mode 100644 index 0000000000..5edc0bd354 --- /dev/null +++ b/tools/linuxfs/preload/space.c @@ -0,0 +1,79 @@ +/* + * The free space a library reports. + * + * A library's games can live somewhere the folder that names the library does not. The card + * library is exactly that: its steamapps/common is bound to the card, but the directory above it + * belongs to the runtime image in the phone's own storage, and that directory is what the client + * measures. So it offers both install locations with the phone's free space against each, and the + * card's real figure - seventeen gigabytes more, on the device this was found on - never appears. + * + * The number is not only cosmetic: the client refuses an install it believes will not fit. Once + * the phone fills up it would turn down a card install with room to spare, and say nothing about + * why. + * + * Moving the whole library onto the card would fix the number and break something worse. The + * prefixes under steamapps/compatdata want symlinks and file locks, and the card is served over + * FUSE, which gives us neither - that is why they are kept in the runtime image in the first + * place. So the question is answered where the content actually is: a query about a library root + * named in BL_LIBRARY_SPACE is served from its steamapps/common, the bind that points at the + * card. Every other path is passed through untouched. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +/* A library root, or its steamapps, resolved to the directory that holds the games. Returns the + * rewritten path in `buf`, or NULL when `path` is not one of ours and should be left alone. */ +static const char *content_of(const char *path, char *buf, size_t len) { + const char *roots = getenv("BL_LIBRARY_SPACE"); + if (!path || !roots || !*roots) + return NULL; + + size_t path_len = strlen(path); + while (*roots) { + const char *end = strchr(roots, ':'); + size_t root_len = end ? (size_t)(end - roots) : strlen(roots); + if (root_len > 0) { + /* The root itself, or the steamapps directly beneath it. Anything deeper is already inside + * a bind and answers for itself. */ + int match = (path_len == root_len && !strncmp(path, roots, root_len)) || + (path_len == root_len + 10 && !strncmp(path, roots, root_len) && + !strcmp(path + root_len, "/steamapps")); + if (match) { + if (root_len + sizeof("/steamapps/common") > len) + return NULL; + memcpy(buf, roots, root_len); + strcpy(buf + root_len, "/steamapps/common"); + return buf; + } + } + if (!end) + break; + roots = end + 1; + } + return NULL; +} + +#define FORWARD(name, type) \ + int name(const char *path, type *out) { \ + static int (*real)(const char *, type *); \ + char buf[4096]; \ + const char *content; \ + if (!real) \ + real = dlsym(RTLD_NEXT, #name); \ + if (!real) \ + return -1; \ + content = content_of(path, buf, sizeof(buf)); \ + return real(content ? content : path, out); \ + } + +FORWARD(statfs, struct statfs) +FORWARD(statvfs, struct statvfs) +#ifdef __USE_LARGEFILE64 +FORWARD(statfs64, struct statfs64) +FORWARD(statvfs64, struct statvfs64) +#endif