mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
SteamDeck: a standalone launcher for the native ARM64 Linux Steam client
Lifts Bannerlator's Linux/gamescope runtime out into an app of its own: the Wayland compositor, the proot session, the runtime installer, the fake-evdev controller path and the PulseAudio server, with none of the Wine, container or store machinery around them. One activity installs the runtime from the same winlator-contents catalog and a second one runs the session: compositor on a SurfaceView, gamescope and Valve's native arm64 client under proot, a physical pad republished into the fake-input rings, and the session's milestones on screen until the first frame arrives.
This commit is contained in:
commit
4022351e22
210 files changed
+73587
No files matched your search
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
# Marks Steam's shared redistributables as already run in every Proton prefix.
|
||||
#
|
||||
# A game whose depot lists _CommonRedist makes the client run the real installer inside the
|
||||
# prefix on first launch. Those are x86 bundles, so they run under FEX, and the 2019 one leaves
|
||||
# a task behind every time: its main thread exits while a helper stays blocked reading a pipe it
|
||||
# also holds the write end of, so the process never reaps and the client keeps waiting on it.
|
||||
#
|
||||
# The client skips a "Run Process" block whose HasRunKey is already set, which is the same escape
|
||||
# WineUtils.seedVcRedistRegistryIfDllsPresent() takes for the app's own containers. The names and
|
||||
# versions come out of the install scripts rather than a list kept here, because Valve revises
|
||||
# them; a prefix already carrying the same set is left alone.
|
||||
set -u
|
||||
steam_root=${1:?steam root}
|
||||
|
||||
redist="$steam_root/steamapps/common/Steamworks Shared/_CommonRedist"
|
||||
[ -d "$redist" ] || exit 0
|
||||
|
||||
# Every process an install script would run becomes the registry value the client writes once it
|
||||
# has run it. Names sit one level inside "Run Process", the key they share a level further in.
|
||||
block=$(find "$redist" -name installscript.vdf -print0 2>/dev/null | sort -z | xargs -0 -r awk -v now="$(date +%s)" '
|
||||
function emit(key, name, path) {
|
||||
path = key
|
||||
sub(/^HKEY_LOCAL_MACHINE\\\\/, "", path)
|
||||
if (path == key)
|
||||
return
|
||||
printf "\n[%s] %d\n\"%s\"=dword:00000001\n", path, now, name
|
||||
# The client writes these from 32-bit steam.exe, so its own copy lands under Wow6432Node;
|
||||
# both spellings are seeded because the reader picks by its own word size.
|
||||
sub(/^Software\\\\/, "Software\\\\\\\\Wow6432Node\\\\\\\\", path)
|
||||
printf "\n[%s] %d\n\"%s\"=dword:00000001\n", path, now, name
|
||||
}
|
||||
FNR == 1 { depth = 0; name = ""; key = "" }
|
||||
/^[[:space:]]*\{/ { depth++; next }
|
||||
/^[[:space:]]*\}/ {
|
||||
if (depth == 3 && name != "" && key != "")
|
||||
emit(key, name)
|
||||
depth--
|
||||
next
|
||||
}
|
||||
{
|
||||
n = split($0, part, "\"")
|
||||
if (n >= 5 && tolower(part[2]) == "hasrunkey") { key = part[4]; next }
|
||||
if (n >= 3 && depth == 2) { name = part[2]; key = "" }
|
||||
}
|
||||
')
|
||||
[ -n "$block" ] || exit 0
|
||||
stamp=$(printf '%s' "$block" | cksum)
|
||||
|
||||
for reg in "$steam_root"/steamapps/compatdata/*/pfx/system.reg \
|
||||
/mnt/bannerlator-sd/steamapps/compatdata/*/pfx/system.reg; do
|
||||
[ -f "$reg" ] || continue
|
||||
seeded=$(dirname "$reg")/.bannerlator-redists
|
||||
[ "$(cat "$seeded" 2>/dev/null)" = "$stamp" ] && continue
|
||||
printf '%s\n' "$block" >>"$reg" || continue
|
||||
printf '%s\n' "$stamp" >"$seeded"
|
||||
echo "== seeded redist markers into $reg"
|
||||
done
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
#!/bin/bash
|
||||
# The Linux runtime's session. Bannerlator execs this under proot with the output size and mode;
|
||||
# the first pass starts gamescope, which execs this script again inside it for the program:
|
||||
# bannerlator-session desktop the file manager
|
||||
# bannerlator-session run <path> a Linux program (AppImage, script or ELF)
|
||||
# bannerlator-session steam [steam-url] the native Steam client
|
||||
# BL_WIDTH / BL_HEIGHT size the output, BL_FPS limits it, BL_LOG collects everything said here.
|
||||
set -u
|
||||
export HOME=${HOME:-/root}
|
||||
mode=${1:-desktop}
|
||||
shift || true
|
||||
# One line per milestone, greppable as "== STEP", so a first run reads as a narrative rather than
|
||||
# something to be reconstructed from Steam's own chatter. The app shows these while it waits.
|
||||
step() { echo "== STEP $(date -u +%H:%M:%S) $*"; }
|
||||
if [ -n "${BL_LOG:-}" ] && [ -z "${BL_INSIDE:-}" ]; then
|
||||
mkdir -p "$(dirname "$BL_LOG")" 2>/dev/null
|
||||
# The client alone writes megabytes an hour; one earlier log is kept, not every session's.
|
||||
if [ "$(stat -c %s "$BL_LOG" 2>/dev/null || echo 0)" -gt 8388608 ]; then
|
||||
mv -f "$BL_LOG" "$BL_LOG.1"
|
||||
fi
|
||||
exec >>"$BL_LOG" 2>&1
|
||||
echo "== bannerlator-session $(date) mode=$mode $*"
|
||||
echo "== uid=$(id -u) size=${BL_WIDTH:-?}x${BL_HEIGHT:-?} fps=${BL_FPS:-0} icd=${VK_ICD_FILENAMES:-none}"
|
||||
echo "== gamescope: $(gamescope --version 2>&1 | head -1)"
|
||||
echo "== rootfs: $(cat /.version 2>/dev/null || echo unversioned)"
|
||||
fi
|
||||
|
||||
# Until the runtime is stable, Steam's own logs are copied beside the session log at exit so a
|
||||
# failed launch can be handed over as one folder.
|
||||
collect_steam_logs() {
|
||||
[ -n "${BL_DEBUG_DIR:-}" ] || return 0
|
||||
local steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
|
||||
mkdir -p "$BL_DEBUG_DIR" 2>/dev/null || return 0
|
||||
[ -d "$steam_root/logs" ] && cp -r "$steam_root/logs" "$BL_DEBUG_DIR/steam-logs" 2>/dev/null
|
||||
[ -f "$HOME/.steam/steam/logs/steamwebhelper.log" ] && cp "$HOME/.steam/steam/logs/steamwebhelper.log" "$BL_DEBUG_DIR/" 2>/dev/null
|
||||
[ -f "$steam_root/steam.pid" ] && cp "$steam_root/steam.pid" "$BL_DEBUG_DIR/" 2>/dev/null
|
||||
echo "== steam logs copied to $BL_DEBUG_DIR"
|
||||
}
|
||||
export XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
|
||||
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
|
||||
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
|
||||
mkdir -p "$XDG_DATA_HOME" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"
|
||||
|
||||
if [ -z "${BL_INSIDE:-}" ]; then
|
||||
# The compositor may still be starting, and a socket left by an earlier session is not
|
||||
# a compositor: probe until a connection succeeds.
|
||||
for _ in $(seq 1 100); do
|
||||
wayland-info >/dev/null 2>&1 && break
|
||||
sleep 0.1
|
||||
done
|
||||
width=${BL_WIDTH:-1280}
|
||||
height=${BL_HEIGHT:-720}
|
||||
args=(--backend wayland --expose-wayland -f -W "$width" -H "$height" -w "$width" -h "$height")
|
||||
# -r is what gamescope advertises as the refresh rate, and what a game reads as the display's.
|
||||
# Left unset it defaults to 60, so a 120 Hz panel offers only 60 Hz in game settings. The FPS
|
||||
# limiter wins when it is on (it is a deliberate cap); otherwise use the panel's own rate.
|
||||
if [ "${BL_FPS:-0}" -gt 0 ]; then
|
||||
args+=(-r "$BL_FPS")
|
||||
elif [ "${BL_REFRESH:-0}" -gt 0 ]; then
|
||||
args+=(-r "$BL_REFRESH")
|
||||
fi
|
||||
[ "$mode" = steam ] && args+=(-e)
|
||||
export BL_INSIDE=1
|
||||
exec gamescope "${args[@]}" -- "$0" "$mode" "$@"
|
||||
fi
|
||||
|
||||
# gamescope manages X windows: toolkits go through Xwayland, not gamescope's own Wayland socket.
|
||||
export GDK_BACKEND=x11 QT_QPA_PLATFORM=xcb SDL_VIDEODRIVER=x11
|
||||
# gamescope's reaper waits for every process left under it before the compositor comes down. A
|
||||
# process a game left behind - a Wine service blocked on a server that is already gone - is
|
||||
# reparented there when the client exits, and the screen stays black on it for good. Nothing of
|
||||
# the session outlives the program it was started for.
|
||||
reap_orphans() {
|
||||
local round p stat pid left
|
||||
for round in 1 2 3 4 5; do
|
||||
left=
|
||||
for p in /proc/[0-9]*; do
|
||||
pid=${p#/proc/}
|
||||
[ "$pid" = "$$" ] && continue
|
||||
stat=$(cat "$p/stat" 2>/dev/null) || continue
|
||||
stat=${stat##*) }
|
||||
set -- $stat
|
||||
[ "${2:-}" = "$PPID" ] && left="$left $pid"
|
||||
done
|
||||
[ -n "$left" ] || return 0
|
||||
echo "== leaving behind:$left"
|
||||
kill -9 $left 2>/dev/null
|
||||
sleep 0.2
|
||||
done
|
||||
}
|
||||
|
||||
case $mode in
|
||||
desktop)
|
||||
exec pcmanfm
|
||||
;;
|
||||
run)
|
||||
target=${1:?program path}
|
||||
shift
|
||||
case ${target,,} in
|
||||
*.appimage)
|
||||
export APPIMAGE_EXTRACT_AND_RUN=1
|
||||
chmod +x "$target" 2>/dev/null
|
||||
exec "$target" "$@"
|
||||
;;
|
||||
*.sh)
|
||||
exec bash "$target" "$@"
|
||||
;;
|
||||
*)
|
||||
chmod +x "$target" 2>/dev/null
|
||||
cd "$(dirname "$target")" && exec "$target" "$@"
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
steam)
|
||||
step "checking the Steam client (first run downloads it, ~1-2 min with nothing on screen)"
|
||||
bannerlator-steam-install || exit $?
|
||||
step "Steam client ready"
|
||||
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.
|
||||
step "registering the app's game library with Steam"
|
||||
bannerlator-steam-library "$steam_root" || true
|
||||
step "setting up the ARM64 Proton compatibility tool"
|
||||
bannerlator-steam-compat "$steam_root" || true
|
||||
# Steam runs a game's shared redistributables inside its prefix on first launch; marking
|
||||
# them as already run keeps those x86 installers from starting under emulation at all.
|
||||
bannerlator-seed-redists "$steam_root" || true
|
||||
# The compatibility tool runs Valve's arm64 Proton, which the client fetches like any depot.
|
||||
# Asked for on the command line the first time, so a Windows title has it before it is needed.
|
||||
if [ ! -f "$steam_root/steamapps/appmanifest_4427310.acf" ]; then
|
||||
step "asking the client to fetch the ARM64 Proton"
|
||||
set -- "$@" steam://install/4427310
|
||||
fi
|
||||
# The tool is registered before the client starts and finds the depot at launch time, so
|
||||
# nothing here waits for a download. What can still change under a running client is which
|
||||
# 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.
|
||||
(
|
||||
for _ in $(seq 1 240); do
|
||||
sleep 15
|
||||
bannerlator-steam-compat "$steam_root" >/dev/null 2>&1 || true
|
||||
done
|
||||
) &
|
||||
# Debug aid while the runtime is being brought up: an app id dropped in ~/.bl-autolaunch is
|
||||
# handed to the client as a rungameid URL, so a launch can be reproduced without touching the
|
||||
# screen. Removed once the runtime is stable.
|
||||
if [ -s "$HOME/.bl-autolaunch" ]; then
|
||||
_bl_app=$(cat "$HOME/.bl-autolaunch")
|
||||
# A bare app id runs the game; a full steam:// URL is passed through, which is how a
|
||||
# validate or an install is driven without touching the screen.
|
||||
case $_bl_app in
|
||||
steam://*) _bl_url=$_bl_app ;;
|
||||
*) _bl_url=steam://rungameid/$_bl_app ;;
|
||||
esac
|
||||
echo "== autolaunch $_bl_url"
|
||||
set -- "$@" "$_bl_url"
|
||||
fi
|
||||
export STEAM_RUNTIME=1
|
||||
export LD_LIBRARY_PATH=$steam_root/steamrtarm64:$steam_root/steamrtarm64/panorama${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
|
||||
export SDL_VIDEO_X11_DGAMOUSE=0
|
||||
# Native-Linux x86 games go through Valve's FEX compat tool, which runs FEX with FEX_PORTABLE=1
|
||||
# - that makes FEX look for its config next to the binary, while the tool writes one into the
|
||||
# app's compatdata instead. FEX therefore starts with no rootfs, every x86_64 library lookup
|
||||
# falls through to the host, and the guest shell dies on libreadline before the game is reached.
|
||||
# FEX_ROOTFS names it outright and is read whatever the config situation.
|
||||
[ -d /usr/share/guestos/fex-mesa ] && export FEX_ROOTFS=/usr/share/guestos/fex-mesa
|
||||
# The client exits 42 when it has updated itself and wants a fresh start (steam.sh does this).
|
||||
while :; do
|
||||
step "starting the Steam client"
|
||||
"$steam_root/steamrtarm64/steam" -gamepadui -clientbeta publicbeta \
|
||||
-no-cef-sandbox -cef-force-gpu -cef-ozone-platform=x11 \
|
||||
-cef-use-gl=angle -cef-use-angle=vulkan "$@"
|
||||
rc=$?
|
||||
echo "== steam exited rc=$rc"
|
||||
collect_steam_logs
|
||||
[ "$rc" -eq 42 ] || break
|
||||
done
|
||||
reap_orphans
|
||||
exit "$rc"
|
||||
;;
|
||||
*)
|
||||
echo "bannerlator-session: unknown mode '$mode'" >&2
|
||||
exit 64
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,285 @@
|
||||
#!/usr/bin/python3
|
||||
# Makes the ARM64 Proton the client already downloaded usable here, and sets it as the default tool
|
||||
# for Windows titles (CompatToolMapping "0" in config.vdf) when the user has not chosen one. Run
|
||||
# before the client starts; it rewrites config.vdf on exit.
|
||||
#
|
||||
# bannerlator-steam-compat <steam root>
|
||||
#
|
||||
# Valve ships a native aarch64 Proton (arm64ec Wine with FEX as the x86 layer) as an ordinary depot,
|
||||
# which is exactly what this runtime wants. Its toolmanifest declares require_tool_appid 4185400 -
|
||||
# Steam Linux Runtime 4 for arm64 - so Steam stacks pressure-vessel underneath it, and
|
||||
# pressure-vessel needs unprivileged user namespaces, which an Android app does not get. Steam then
|
||||
# drops the launch on the floor: no window, no log, straight back to the library.
|
||||
#
|
||||
# So the depot is re-registered here as a compatibility tool of our own: every entry symlinked so
|
||||
# nothing is copied and Steam still updates the real depot, but with a toolmanifest that leaves out
|
||||
# require_tool_appid. Proton takes its base directory from dirname(sys.argv[0]) without resolving
|
||||
# it, so it reads files/ through those symlinks and runs directly on this rootfs.
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
TOOL = "bannerlator-proton-arm64"
|
||||
# Valve's own ARM64 Proton, by the name the client registers it under. Used only to bootstrap a
|
||||
# fresh install: naming it is what makes the client offer it and fetch the depot.
|
||||
SEED_TOOL = "proton-experimental-arm64"
|
||||
# The ARM64 Proton depot the client downloads for itself, as a compatibility-tool appid.
|
||||
# The session asks the client to install it on the command line the first time (steam://install),
|
||||
# so a new client has it before a Windows title is first launched. It is deliberately NOT named as
|
||||
# this tool's dependency: a dependency is composed with its own dependencies, and this depot
|
||||
# requires 4185400, the arm64 Steam Linux Runtime, whose container needs user namespaces Android
|
||||
# does not give - the launch would be wrapped in it and die before this tool ever ran.
|
||||
PROTON_ARM64_APPID = "4427310"
|
||||
LAUNCHER = "bannerlator-proton"
|
||||
# The extra Steam library the app mounts the installed games into (see bannerlator-steam-library).
|
||||
# installed_apps() scans it too; it lives only in the sibling script, so define it here as well.
|
||||
LIBRARY = "/mnt/bannerlator" # the card library is LIBRARY + "-sd"; internal storage is steam_root itself
|
||||
# The client lists its own "Proton Experimental (ARM64)" and "Proton 11.0 (ARM64)" in the same
|
||||
# Compatibility dropdown, and those are the two that cannot start a game here. Ours has to be
|
||||
# unmistakable at a glance, or a user picks a neighbour and gets a launch that fails silently.
|
||||
# Newest first: whichever the client has downloaded is used.
|
||||
SOURCES = ("Proton Experimental (ARM64)", "Proton 11.0 (ARM64)")
|
||||
# Written by us; everything else in the depot is symlinked through.
|
||||
OWN_FILES = ("toolmanifest.vdf", "compatibilitytool.vdf", "bannerlator-proton")
|
||||
# The client assigns a Proton to a Windows title by itself, as a per-app CompatToolMapping entry at
|
||||
# priority 250 - which outranks the "0" default, so setting that default alone changes nothing. Every
|
||||
# Proton it can pick is one this runtime cannot start: the ARM64 ones want pressure-vessel (see
|
||||
# above) and the x86_64 ones want pressure-vessel and FEX on top. Ours is the only one that runs
|
||||
# here, so every Proton mapping is repointed at it. Tools that are not Proton are left alone.
|
||||
COMPAT_TOOL_VDF = """"compatibilitytools"
|
||||
{
|
||||
"compat_tools"
|
||||
{
|
||||
"%s"
|
||||
{
|
||||
"install_path" "."
|
||||
"display_name" "Bannerlator Proton (ARM64)"
|
||||
"from_oslist" "windows"
|
||||
"to_oslist" "linux"
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
TOOL_MANIFEST_VDF = """"manifest"
|
||||
{
|
||||
"version" "2"
|
||||
"commandline" "/%s %%verb%%"
|
||||
"use_sessions" "1"
|
||||
"compatmanager_layer_name" "proton"
|
||||
}
|
||||
""" % LAUNCHER
|
||||
|
||||
# Resolves the depot at launch time, so the tool can be registered before the depot exists and
|
||||
# survives the client updating or replacing it. Proton takes its base directory from
|
||||
# dirname(sys.argv[0]) without resolving it, so it is run from the depot itself.
|
||||
LAUNCHER_SH = """#!/bin/bash
|
||||
# Bannerlator Proton (ARM64): runs the ARM64 Proton the Steam client downloaded, without the
|
||||
# Steam Linux Runtime container its own manifest asks for (no user namespaces on Android).
|
||||
verb=$1; shift
|
||||
steam_root=${STEAM_COMPAT_CLIENT_INSTALL_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/Steam}
|
||||
# The client preloads its overlay library into every game. In a gamescope session the overlay is
|
||||
# the client's own window, so the library draws nothing - but it stands in front of open() and
|
||||
# read() on /dev/input, lets Wine open the session's pad and answers every read of it with nothing:
|
||||
# a controller that never reports, and Wine's HID thread spinning a core on it. 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
|
||||
for name in %s; do
|
||||
depot="$steam_root/steamapps/common/$name"
|
||||
if [ -d "$depot/files/bin-arm64" ] && [ -x "$depot/proton" ]; then
|
||||
exec "$depot/proton" "$verb" "$@"
|
||||
fi
|
||||
done
|
||||
echo "bannerlator-proton: no ARM64 Proton depot yet - the client downloads one as this tool's dependency; retry the launch once it has" >&2
|
||||
exit 1
|
||||
""" % " ".join('"%s"' % n for n in SOURCES)
|
||||
|
||||
TOKEN = re.compile(r'\s*(?:"((?:[^"\\]|\\.)*)"|([{}])|//[^\n]*)')
|
||||
|
||||
|
||||
def tokenize(text):
|
||||
tokens = []
|
||||
pos = 0
|
||||
while pos < len(text):
|
||||
m = TOKEN.match(text, pos)
|
||||
if not m or m.end() == pos:
|
||||
break
|
||||
pos = m.end()
|
||||
if m.group(2):
|
||||
tokens.append(m.group(2))
|
||||
elif m.group(1) is not None:
|
||||
tokens.append('"' + m.group(1) + '"')
|
||||
return tokens
|
||||
|
||||
|
||||
def find_block(tokens, path):
|
||||
"""Index of the '{' opening the block at path, or None."""
|
||||
depth, want, i = 0, 0, 0
|
||||
while i < len(tokens):
|
||||
t = tokens[i]
|
||||
if t == "{":
|
||||
depth += 1
|
||||
elif t == "}":
|
||||
depth -= 1
|
||||
if depth < want:
|
||||
return None
|
||||
elif depth == want and i + 1 < len(tokens) and tokens[i + 1] == "{":
|
||||
if t.lower() == '"%s"' % path[want].lower():
|
||||
if want == len(path) - 1:
|
||||
return i + 1
|
||||
want += 1
|
||||
i += 1
|
||||
return None
|
||||
|
||||
|
||||
def block_end(tokens, start):
|
||||
depth = 0
|
||||
for i in range(start, len(tokens)):
|
||||
if tokens[i] == "{":
|
||||
depth += 1
|
||||
elif tokens[i] == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return i
|
||||
return len(tokens)
|
||||
|
||||
|
||||
def serialize(tokens):
|
||||
out, depth, i = [], 0, 0
|
||||
while i < len(tokens):
|
||||
t = tokens[i]
|
||||
if t == "}":
|
||||
depth -= 1
|
||||
out.append("\t" * depth + "}")
|
||||
i += 1
|
||||
elif i + 1 < len(tokens) and tokens[i + 1] == "{":
|
||||
out.append("\t" * depth + t)
|
||||
out.append("\t" * depth + "{")
|
||||
depth += 1
|
||||
i += 2
|
||||
else:
|
||||
out.append("\t" * depth + t + "\t\t" + tokens[i + 1])
|
||||
i += 2
|
||||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
# 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((
|
||||
"228980", # Steamworks Common Redistributables
|
||||
"1070560", # Steam Linux Runtime 1.0 (scout)
|
||||
"1391110", # Steam Linux Runtime 2.0 (soldier)
|
||||
"1493710", # Proton Experimental
|
||||
"3127680", # FEX
|
||||
"4183110", # Steam Linux Runtime 4.0
|
||||
"4185400", # Steam Linux Runtime 4.0 - Arm64
|
||||
"4427310", # Proton Experimental (ARM64)
|
||||
"4628740", # Proton 11.0 (ARM64)
|
||||
))
|
||||
|
||||
|
||||
def installed_apps(steam_root):
|
||||
"""App ids of the titles the client has installed, tools and runtimes excluded."""
|
||||
apps = set()
|
||||
for library in (steam_root, LIBRARY + "-sd"):
|
||||
steamapps = os.path.join(library, "steamapps")
|
||||
try:
|
||||
names = os.listdir(steamapps)
|
||||
except OSError:
|
||||
continue
|
||||
for name in names:
|
||||
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
|
||||
if m and m.group(1) not in NOT_GAMES:
|
||||
apps.add(m.group(1))
|
||||
return sorted(apps, key=int)
|
||||
|
||||
|
||||
def register_default(config_path, apps=(), name=None):
|
||||
"""Writes the mapping. `name` defaults to our own tool; the first-run seed passes Valve's."""
|
||||
tool = name or TOOL
|
||||
with open(config_path, encoding="utf-8", errors="replace") as f:
|
||||
tokens = tokenize(f.read())
|
||||
steam = find_block(tokens, ["InstallConfigStore", "Software", "Valve", "Steam"])
|
||||
if steam is None:
|
||||
return
|
||||
mapping = find_block(tokens[steam + 1:], ["CompatToolMapping"])
|
||||
entry = ['"0"', "{", '"name"', '"%s"' % tool, '"config"', '""', '"priority"', '"75"', "}"]
|
||||
if mapping is None:
|
||||
end = block_end(tokens, steam)
|
||||
tokens[end:end] = ['"CompatToolMapping"', "{"] + entry + ["}"]
|
||||
else:
|
||||
mapping += steam + 1
|
||||
# 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:
|
||||
tokens[i + 1] = '"%s"' % tool
|
||||
if find_block(tokens[mapping + 1:], ["0"]) is None:
|
||||
tokens[mapping + 1:mapping + 1] = entry
|
||||
# The "0" default only decides how a title is installed, so it settles new downloads and
|
||||
# leaves anything already installed as a Linux build running as one. An entry per installed
|
||||
# app is what the client's own Compatibility checkbox writes, and is what makes it switch.
|
||||
for app in apps:
|
||||
if find_block(tokens[mapping + 1:], [app]) is not None:
|
||||
continue
|
||||
tokens[mapping + 1:mapping + 1] = [
|
||||
'"%s"' % app, "{", '"name"', '"%s"' % tool, '"config"', '""', '"priority"', '"250"', "}"]
|
||||
tmp = config_path + ".bannerlator"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
f.write(serialize(tokens))
|
||||
os.replace(tmp, config_path)
|
||||
|
||||
|
||||
def find_source(steam_root):
|
||||
"""The ARM64 Proton depot the client has downloaded, or None."""
|
||||
common = os.path.join(steam_root, "steamapps", "common")
|
||||
for name in SOURCES:
|
||||
depot = os.path.join(common, name)
|
||||
# bin-arm64 is what makes it the native build rather than the x86_64 one.
|
||||
if os.path.isdir(os.path.join(depot, "files", "bin-arm64")):
|
||||
return depot
|
||||
return None
|
||||
|
||||
|
||||
def build_tool(tool_dir):
|
||||
"""Our own manifests and the launcher. The depot is found at launch time, not here."""
|
||||
if os.path.islink(tool_dir):
|
||||
os.unlink(tool_dir)
|
||||
elif os.path.isdir(tool_dir):
|
||||
# Earlier versions mirrored the depot in here as symlinks; start clean.
|
||||
shutil.rmtree(tool_dir)
|
||||
os.makedirs(tool_dir)
|
||||
with open(os.path.join(tool_dir, "toolmanifest.vdf"), "w", encoding="utf-8") as f:
|
||||
f.write(TOOL_MANIFEST_VDF)
|
||||
with open(os.path.join(tool_dir, "compatibilitytool.vdf"), "w", encoding="utf-8") as f:
|
||||
f.write(COMPAT_TOOL_VDF % TOOL)
|
||||
launcher = os.path.join(tool_dir, LAUNCHER)
|
||||
with open(launcher, "w", encoding="utf-8") as f:
|
||||
f.write(LAUNCHER_SH)
|
||||
os.chmod(launcher, 0o755)
|
||||
|
||||
|
||||
def main():
|
||||
steam_root = sys.argv[1]
|
||||
tools = os.path.join(steam_root, "compatibilitytools.d")
|
||||
os.makedirs(tools, exist_ok=True)
|
||||
build_tool(os.path.join(tools, TOOL))
|
||||
depot = find_source(steam_root)
|
||||
print("bannerlator-steam-compat: registered %s (%s)" % (
|
||||
TOOL, "depot %s present" % os.path.basename(depot) if depot else
|
||||
"no ARM64 Proton depot yet; the session asks the client to fetch it"))
|
||||
config = os.path.join(steam_root, "config", "config.vdf")
|
||||
if os.path.isfile(config):
|
||||
apps = installed_apps(steam_root)
|
||||
register_default(config, apps)
|
||||
print("bannerlator-steam-compat: default and %d installed title(s) set to %s" % (len(apps), TOOL))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/bin/bash
|
||||
# Fetches the native arm64 Steam client from Valve's client-update CDN into ~/.local/share/Steam.
|
||||
# The client is not redistributable, so the runtime ships this script instead of the client.
|
||||
set -eu
|
||||
cdn=https://client-update.fastly.steamstatic.com
|
||||
manifest=steam_client_publicbeta_linuxarm64
|
||||
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
|
||||
stamp=$steam_root/package/bannerlator-installed
|
||||
[ -x "$steam_root/steamrtarm64/steam" ] && [ -f "$stamp" ] && exit 0
|
||||
|
||||
work=$(mktemp -d)
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
curl -fsSL "$cdn/$manifest" -o "$work/manifest"
|
||||
version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
|
||||
# Every component block: its name, then the plain zip's file name and sha256.
|
||||
awk -F'"' '
|
||||
/^\t"/ && NF >= 3 && $4 == "" { block = $2 }
|
||||
$2 == "file" { file[block] = $4 }
|
||||
$2 == "sha2" { sha[block] = $4 }
|
||||
END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b] }
|
||||
' "$work/manifest" | sort > "$work/parts"
|
||||
[ -s "$work/parts" ] || { echo "bannerlator-steam-install: no client components in $manifest" >&2; exit 1; }
|
||||
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
|
||||
|
||||
mkdir -p "$steam_root/package"
|
||||
while read -r block file sum; do
|
||||
i=$((${i:-0} + 1))
|
||||
echo "== STEP $(date -u +%H:%M:%S) downloading Steam: $block ($i/$total)"
|
||||
curl -fsSL "$cdn/$file" -o "$work/$file"
|
||||
echo "$sum $work/$file" | sha256sum -c --quiet
|
||||
unzip -qo "$work/$file" -d "$steam_root"
|
||||
rm -f "$work/$file"
|
||||
done < "$work/parts"
|
||||
# Some entries are zipped with Windows separators; unzip keeps the backslash in the file name.
|
||||
find "$steam_root" -depth -name '*\\*' | while read -r odd; do
|
||||
fixed=${odd//\\//}
|
||||
mkdir -p "$(dirname "$fixed")"
|
||||
if [ -d "$odd" ]; then
|
||||
cp -a "$odd/." "$fixed/" && rm -rf "$odd"
|
||||
else
|
||||
mv -f "$odd" "$fixed"
|
||||
fi
|
||||
done
|
||||
echo publicbeta > "$steam_root/package/beta"
|
||||
echo "$version" > "$stamp"
|
||||
ln -sfn "$steam_root/steamrtarm64" "$steam_root/steamrtarm32"
|
||||
chmod +x "$steam_root/steamrtarm64/steam" 2>/dev/null || true
|
||||
# What Valve's steam.sh sets up before it runs the client (it has no arm64 branch itself).
|
||||
mkdir -p "$HOME/.steam"
|
||||
ln -sfn "$steam_root" "$HOME/.steam/root"
|
||||
ln -sfn "$steam_root" "$HOME/.steam/steam"
|
||||
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/bin64"
|
||||
ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdk64"
|
||||
# The arm64 client runs every game through $HOME/.steam/sdkarm64/steam-launch-wrapper. Valve's
|
||||
# steam.sh has no arm64 branch to create it, and without it a launch dies in /bin/sh before Proton
|
||||
# is ever invoked - "No such file or directory", no Proton log, and the only other clue is an
|
||||
# unrelated-looking installscript assertion. Same target as sdk64; the client picks the name by
|
||||
# architecture.
|
||||
ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
|
||||
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
|
||||
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
|
||||
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/python3
|
||||
# Registers the card the app installs to (bound at /mnt/bannerlator-sd) as a library folder of the
|
||||
# native Steam client, so they show as installed there instead of being downloaded a second time.
|
||||
#
|
||||
# bannerlator-steam-library <steam root>
|
||||
#
|
||||
# Ported from WinNative's gamescope runtime (GPL-3.0).
|
||||
import os
|
||||
import re
|
||||
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)"),)
|
||||
|
||||
|
||||
def register(steam_root, library, label):
|
||||
"""Adds one library folder to the client's list; the client then offers it as an install
|
||||
location as well as reading the games already in it. Registered even when empty: a new user
|
||||
should be able to install straight into it."""
|
||||
steamapps = os.path.join(library, "steamapps")
|
||||
if not os.path.isdir(steamapps):
|
||||
return 0
|
||||
apps = sorted(
|
||||
int(m.group(1))
|
||||
for m in (re.fullmatch(r"appmanifest_(\d+)\.acf", n) for n in os.listdir(steamapps))
|
||||
if m
|
||||
)
|
||||
marker = os.path.join(library, "libraryfolder.vdf")
|
||||
if not os.path.isfile(marker):
|
||||
with open(marker, "w", encoding="utf-8") as f:
|
||||
f.write('"libraryfolder"\n{\n\t"contentid"\t\t"%d"\n\t"label"\t\t"%s"\n}\n'
|
||||
% (int.from_bytes(os.urandom(8), "little"), label))
|
||||
steamapps_dir = os.path.join(steam_root, "steamapps")
|
||||
path = os.path.join(steamapps_dir, "libraryfolders.vdf")
|
||||
text = ""
|
||||
if os.path.isfile(path):
|
||||
with open(path, encoding="utf-8", errors="replace") as f:
|
||||
text = f.read()
|
||||
if re.search(r'"path"\s+"%s"' % re.escape(library), text):
|
||||
return len(apps)
|
||||
indexes = [int(i) for i in re.findall(r'^\t"(\d+)"\s*$', text, re.M)]
|
||||
if not text.strip():
|
||||
text = ('"libraryfolders"\n{\n\t"0"\n\t{\n\t\t"path"\t\t"%s"\n\t\t"label"\t\t""\n\t}\n}\n'
|
||||
% steam_root)
|
||||
indexes = [0]
|
||||
entry = ['\t"%d"' % (max(indexes) + 1), "\t{", '\t\t"path"\t\t"%s"' % library,
|
||||
'\t\t"label"\t\t"%s"' % label, '\t\t"apps"', "\t\t{"]
|
||||
entry += ['\t\t\t"%d"\t\t"0"' % app for app in apps]
|
||||
entry += ["\t\t}", "\t}"]
|
||||
closing = text.rstrip().rfind("}")
|
||||
if closing < 0:
|
||||
return len(apps)
|
||||
text = text[:closing] + "\n".join(entry) + "\n" + text[closing:]
|
||||
os.makedirs(steamapps_dir, exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
print("bannerlator-steam-library: registered %s with %d game(s)" % (library, len(apps)))
|
||||
return len(apps)
|
||||
|
||||
|
||||
RETIRED = ("/mnt/bannerlator",)
|
||||
|
||||
|
||||
def forget(steam_root, library):
|
||||
"""Removes a library folder earlier builds registered and this one no longer binds, so the
|
||||
client does not keep offering a location that is not there."""
|
||||
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, block, depth = [], None, 0
|
||||
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:
|
||||
if not re.search(r'"path"\s+"%s"' % re.escape(library), "\n".join(block)):
|
||||
out.extend(block)
|
||||
block = None
|
||||
continue
|
||||
depth += line.count("{") - line.count("}")
|
||||
out.append(line)
|
||||
text = "\n".join(out)
|
||||
if text != "\n".join(lines):
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
print("bannerlator-steam-library: forgot %s" % library)
|
||||
|
||||
|
||||
def main():
|
||||
steam_root = sys.argv[1]
|
||||
for library in RETIRED:
|
||||
forget(steam_root, library)
|
||||
for library, label in LIBRARIES:
|
||||
register(steam_root, library, label)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* GEM handle emulation for the KGSL device presented as a DRM render node.
|
||||
*
|
||||
* Compositors and Mesa validate or convert dma-bufs with PRIME ioctls on the render node; KGSL
|
||||
* has no GEM. Handles only serve as tokens to these callers, so a table of duplicated dma-buf
|
||||
* descriptors stands in for the kernel's, and every other device is left to libdrm.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_HANDLES 4096
|
||||
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int handle_fds[MAX_HANDLES];
|
||||
static dev_t kgsl_dev;
|
||||
static int kgsl_state; /* 0 unknown, 1 known, -1 absent */
|
||||
|
||||
static int is_kgsl(int fd) {
|
||||
struct stat st;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (kgsl_state == 0) {
|
||||
kgsl_state = stat("/dev/kgsl-3d0", &st) == 0 ? 1 : -1;
|
||||
kgsl_dev = st.st_rdev;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return kgsl_state > 0 && fstat(fd, &st) == 0 && S_ISCHR(st.st_mode) && st.st_rdev == kgsl_dev;
|
||||
}
|
||||
|
||||
static void *real(const char *name) {
|
||||
void *fn = dlsym(RTLD_NEXT, name);
|
||||
if (!fn) {
|
||||
errno = ENOSYS;
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) {
|
||||
if (!is_kgsl(fd)) {
|
||||
int (*fn)(int, int, uint32_t *) = (int (*)(int, int, uint32_t *)) real("drmPrimeFDToHandle");
|
||||
return fn ? fn(fd, prime_fd, handle) : -ENOSYS;
|
||||
}
|
||||
int dup_fd = fcntl(prime_fd, F_DUPFD_CLOEXEC, 0);
|
||||
if (dup_fd < 0) {
|
||||
return -errno;
|
||||
}
|
||||
int ret = -ENOMEM;
|
||||
pthread_mutex_lock(&lock);
|
||||
for (uint32_t i = 1; i < MAX_HANDLES; i++) {
|
||||
if (handle_fds[i] == 0) {
|
||||
handle_fds[i] = dup_fd;
|
||||
*handle = i;
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
if (ret) {
|
||||
close(dup_fd);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd) {
|
||||
if (!is_kgsl(fd)) {
|
||||
int (*fn)(int, uint32_t, uint32_t, int *) = (int (*)(int, uint32_t, uint32_t, int *)) real("drmPrimeHandleToFD");
|
||||
return fn ? fn(fd, handle, flags, prime_fd) : -ENOSYS;
|
||||
}
|
||||
int ret = -EINVAL;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) {
|
||||
*prime_fd = fcntl(handle_fds[handle], F_DUPFD_CLOEXEC, 0);
|
||||
ret = *prime_fd < 0 ? -errno : 0;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int drmCloseBufferHandle(int fd, uint32_t handle) {
|
||||
if (!is_kgsl(fd)) {
|
||||
int (*fn)(int, uint32_t) = (int (*)(int, uint32_t)) real("drmCloseBufferHandle");
|
||||
return fn ? fn(fd, handle) : -ENOSYS;
|
||||
}
|
||||
int ret = -EINVAL;
|
||||
pthread_mutex_lock(&lock);
|
||||
if (handle > 0 && handle < MAX_HANDLES && handle_fds[handle] > 0) {
|
||||
close(handle_fds[handle]);
|
||||
handle_fds[handle] = 0;
|
||||
ret = 0;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* fork() carries over only the calling thread, so a lock another thread was holding at that
|
||||
* instant stays held in the child by a thread that is not there to release it. Taking it before
|
||||
* the fork makes the copy consistent; the parent then unlocks it and the child, whose one thread
|
||||
* never locked it, gets a fresh one. (WinNative 79aa7f68.)
|
||||
*/
|
||||
static void drm_lock_before_fork(void) { pthread_mutex_lock(&lock); }
|
||||
static void drm_unlock_after_fork(void) { pthread_mutex_unlock(&lock); }
|
||||
static void drm_reset_after_fork(void) { pthread_mutex_init(&lock, NULL); }
|
||||
|
||||
__attribute__((constructor)) static void install_drm_fork_handlers(void) {
|
||||
pthread_atfork(drm_lock_before_fork, drm_unlock_after_fork, drm_reset_after_fork);
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
|
||||
* changed to this project's. The reasoning below is the original author's.
|
||||
*
|
||||
* The session's controllers, described to libudev's callers.
|
||||
*
|
||||
* The pads are evdev nodes the app backs with its own input rings, bound over /dev/input. Wine's
|
||||
* HID bus finds them there, but it identifies a node by resolving /sys/class/input/<name> and
|
||||
* asking libudev about the result: on Android that resolves to whatever real input device the
|
||||
* host numbered the same, whose device node libudev cannot read, so every pad is discarded before
|
||||
* its identity is ever looked at. The fallback is Wine's SDL bus, and SDL enumerates through udev
|
||||
* too, over a netlink socket the app sandbox refuses. A game was left with no controller at all
|
||||
* while the Steam client, which reads the nodes itself, had one.
|
||||
*
|
||||
* So the pads are described here instead: the resolved path is answered for the session's own
|
||||
* nodes, and libudev's accessors answer for that path from what the app already knows. They carry
|
||||
* the identity of the pad Steam Input would present if it could - it has no /dev/uinput here to
|
||||
* make one - which is the one identity Wine's own bus keeps rather than handing on to SDL.
|
||||
*
|
||||
* Only Wine's device host is answered, and only for the session's own nodes: the Steam client
|
||||
* enumerates the same nodes for itself and would take a pad wearing Steam Input's identity for one
|
||||
* of its own and ignore it. Every other caller, path and device object goes straight through.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* The app opens one ring per configured controller; four is what its launcher offers. */
|
||||
#define PAD_MAX 4
|
||||
#define EVENT_MINOR_BASE 64
|
||||
|
||||
/* BUS_VIRTUAL, and the identity Steam Input gives the gamepad it presents to a game. */
|
||||
#define PAD_UEVENT_FORMAT \
|
||||
"PRODUCT=6/28de/11ff/110\n" \
|
||||
"NAME=\"Generic HID Gamepad %d\"\n" \
|
||||
"PHYS=\"usb-fakeinput/input%d\"\n"
|
||||
|
||||
struct pad {
|
||||
char syspath[64];
|
||||
char devnode[32];
|
||||
char sysname[16];
|
||||
char uevent[128];
|
||||
dev_t devnum;
|
||||
};
|
||||
|
||||
static struct pad pads[PAD_MAX];
|
||||
static int pad_count;
|
||||
|
||||
/*
|
||||
* The slots the app published rings for, named in FAKE_EVDEV_MEMFD_PATHS as "<slot>=<path>"
|
||||
* separated by semicolons. Without it the process has no pads of the session's and every call
|
||||
* here is a passthrough.
|
||||
*/
|
||||
static void discover_pads(void) {
|
||||
const char *spec = getenv("FAKE_EVDEV_MEMFD_PATHS");
|
||||
const char *entry;
|
||||
|
||||
if (spec == NULL) return;
|
||||
for (entry = spec; *entry != '\0';) {
|
||||
char *end;
|
||||
long slot = strtol(entry, &end, 10);
|
||||
|
||||
if (end != entry && *end == '=' && slot >= 0 && slot < PAD_MAX) {
|
||||
struct pad *pad = &pads[slot];
|
||||
|
||||
if (pad->devnode[0] == '\0') {
|
||||
snprintf(pad->sysname, sizeof(pad->sysname), "event%ld", slot);
|
||||
snprintf(pad->syspath, sizeof(pad->syspath), "/sys/devices/virtual/input/bannerlator%ld",
|
||||
slot);
|
||||
snprintf(pad->devnode, sizeof(pad->devnode), "/dev/input/event%ld", slot);
|
||||
snprintf(pad->uevent, sizeof(pad->uevent), PAD_UEVENT_FORMAT, (int)slot, (int)slot);
|
||||
pad->devnum = makedev(13, EVENT_MINOR_BASE + (unsigned int)slot);
|
||||
if (slot >= pad_count) pad_count = (int)slot + 1;
|
||||
}
|
||||
}
|
||||
entry = strchr(entry, ';');
|
||||
if (entry == NULL) break;
|
||||
entry++;
|
||||
}
|
||||
}
|
||||
|
||||
static void ensure_pads(void) {
|
||||
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
||||
pthread_once(&once, discover_pads);
|
||||
}
|
||||
|
||||
/* The pad a synthetic syspath names, or NULL when the path is not one of ours. */
|
||||
static struct pad *pad_for_syspath(const char *syspath) {
|
||||
int i;
|
||||
|
||||
if (syspath == NULL) return NULL;
|
||||
ensure_pads();
|
||||
for (i = 0; i < pad_count; i++) {
|
||||
if (pads[i].devnode[0] != '\0' && strcmp(pads[i].syspath, syspath) == 0) return &pads[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* A device object is ours exactly when it is one of the pads. */
|
||||
static struct pad *pad_for_device(void *device) {
|
||||
int i;
|
||||
|
||||
if (device == NULL) return NULL;
|
||||
ensure_pads();
|
||||
for (i = 0; i < pad_count; i++) {
|
||||
if (&pads[i] == device) return pads[i].devnode[0] != '\0' ? &pads[i] : NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The pad /sys/class/input/<name> stands for, or NULL when the path names something else. */
|
||||
static struct pad *pad_for_syslink(const char *path) {
|
||||
static const char prefix[] = "/sys/class/input/";
|
||||
const char *name;
|
||||
int i;
|
||||
|
||||
if (path == NULL || strncmp(path, prefix, sizeof(prefix) - 1) != 0) return NULL;
|
||||
name = path + sizeof(prefix) - 1;
|
||||
ensure_pads();
|
||||
for (i = 0; i < pad_count; i++) {
|
||||
if (pads[i].devnode[0] != '\0' && strcmp(pads[i].sysname, name) == 0) return &pads[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *next_symbol(const char *name) { return dlsym(RTLD_NEXT, name); }
|
||||
|
||||
/* Wine reaches libudev from its HID bus driver, which is the only caller these pads are for. */
|
||||
static int caller_is_winebus(void *caller) {
|
||||
Dl_info info;
|
||||
|
||||
if (dladdr(caller, &info) == 0 || info.dli_fname == NULL) return 0;
|
||||
return strstr(info.dli_fname, "winebus.so") != NULL;
|
||||
}
|
||||
|
||||
char *realpath(const char *path, char *resolved) {
|
||||
static char *(*real_realpath)(const char *, char *);
|
||||
void *caller = __builtin_return_address(0);
|
||||
struct pad *pad = caller_is_winebus(caller) ? pad_for_syslink(path) : NULL;
|
||||
|
||||
if (pad != NULL) {
|
||||
if (resolved == NULL) return strdup(pad->syspath);
|
||||
/* PATH_MAX is what the caller promises when it passes a buffer. */
|
||||
memcpy(resolved, pad->syspath, strlen(pad->syspath) + 1);
|
||||
return resolved;
|
||||
}
|
||||
if (real_realpath == NULL) real_realpath = next_symbol("realpath");
|
||||
if (real_realpath == NULL) return NULL;
|
||||
return real_realpath(path, resolved);
|
||||
}
|
||||
|
||||
void *udev_device_new_from_syspath(void *udev, const char *syspath) {
|
||||
static void *(*real_new)(void *, const char *);
|
||||
void *caller = __builtin_return_address(0);
|
||||
struct pad *pad = caller_is_winebus(caller) ? pad_for_syspath(syspath) : NULL;
|
||||
|
||||
if (pad != NULL) return pad;
|
||||
if (real_new == NULL) real_new = next_symbol("udev_device_new_from_syspath");
|
||||
if (real_new == NULL) return NULL;
|
||||
return real_new(udev, syspath);
|
||||
}
|
||||
|
||||
void *udev_device_ref(void *device) {
|
||||
static void *(*real_ref)(void *);
|
||||
|
||||
if (pad_for_device(device) != NULL) return device;
|
||||
if (real_ref == NULL) real_ref = next_symbol("udev_device_ref");
|
||||
if (real_ref == NULL) return NULL;
|
||||
return real_ref(device);
|
||||
}
|
||||
|
||||
void *udev_device_unref(void *device) {
|
||||
static void *(*real_unref)(void *);
|
||||
|
||||
if (pad_for_device(device) != NULL) return NULL;
|
||||
if (real_unref == NULL) real_unref = next_symbol("udev_device_unref");
|
||||
if (real_unref == NULL) return NULL;
|
||||
return real_unref(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_devnode(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return pad->devnode;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_devnode");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_syspath(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return pad->syspath;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_syspath");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_sysname(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return pad->sysname;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_sysname");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_subsystem(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return "input";
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_subsystem");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_devtype(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return NULL;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_devtype");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
const char *udev_device_get_action(void *device) {
|
||||
static const char *(*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return NULL;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_action");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
dev_t udev_device_get_devnum(void *device) {
|
||||
static dev_t (*real_get)(void *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return pad->devnum;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_devnum");
|
||||
if (real_get == NULL) return makedev(0, 0);
|
||||
return real_get(device);
|
||||
}
|
||||
|
||||
/*
|
||||
* A pad has no bus behind it, so it stands in for its own input device and has no other parent.
|
||||
* The reference is a borrowed one either way.
|
||||
*/
|
||||
void *udev_device_get_parent_with_subsystem_devtype(void *device, const char *subsystem,
|
||||
const char *devtype) {
|
||||
static void *(*real_get)(void *, const char *, const char *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) {
|
||||
if (subsystem != NULL && strcmp(subsystem, "input") == 0 && devtype == NULL) return pad;
|
||||
return NULL;
|
||||
}
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_parent_with_subsystem_devtype");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device, subsystem, devtype);
|
||||
}
|
||||
|
||||
const char *udev_device_get_sysattr_value(void *device, const char *attr) {
|
||||
static const char *(*real_get)(void *, const char *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return attr != NULL && strcmp(attr, "uevent") == 0 ? pad->uevent : NULL;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_sysattr_value");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device, attr);
|
||||
}
|
||||
|
||||
const char *udev_device_get_property_value(void *device, const char *key) {
|
||||
static const char *(*real_get)(void *, const char *);
|
||||
struct pad *pad = pad_for_device(device);
|
||||
|
||||
if (pad != NULL) return key != NULL && strcmp(key, "ID_INPUT_JOYSTICK") == 0 ? "1" : NULL;
|
||||
if (real_get == NULL) real_get = next_symbol("udev_device_get_property_value");
|
||||
if (real_get == NULL) return NULL;
|
||||
return real_get(device, key);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
|
||||
* changed to this project's. The reasoning below is the original author's.
|
||||
*
|
||||
* File locking for the game library on external storage.
|
||||
*
|
||||
* The user's games are bound in from /storage/emulated, which Android serves over FUSE, and that
|
||||
* filesystem implements no locking at all: flock() and fcntl()'s lock commands answer ENOSYS for
|
||||
* every file under it. The Steam client takes a shared lock on each content file as it validates
|
||||
* or updates a title and treats the refusal as a failure, so a download walks its progress back
|
||||
* and forth instead of finishing; a game reading its own data hits the same wall.
|
||||
*
|
||||
* Where the kernel can lock, it does: the real call runs first and its answer stands, so files on
|
||||
* the app's own f2fs storage keep genuine cross-process exclusion. Only the filesystem that cannot
|
||||
* lock at all is answered here, and granting is the honest reading of it - a filesystem with no
|
||||
* lock state has nothing to contend over, and the one other party that could contend is inside
|
||||
* this same session. Reporting failure instead buys no safety and costs the download.
|
||||
*
|
||||
* F_GETLK is the exception: it asks who holds a lock, and on a filesystem holding none the true
|
||||
* answer is "nobody", so it is reported unlocked rather than granted.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/file.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* The filesystem refuses the whole operation, rather than declining this particular lock. */
|
||||
static int unsupported(int error) {
|
||||
return error == ENOSYS || error == ENOTSUP || error == EOPNOTSUPP;
|
||||
}
|
||||
|
||||
int flock(int fd, int operation) {
|
||||
static int (*real_flock)(int, int);
|
||||
int result;
|
||||
|
||||
if (real_flock == NULL) real_flock = dlsym(RTLD_NEXT, "flock");
|
||||
if (real_flock == NULL) return 0;
|
||||
result = real_flock(fd, operation);
|
||||
if (result < 0 && unsupported(errno)) return 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* fcntl()'s third argument is an int, a pointer, or absent depending on the command; only the
|
||||
* lock commands are of interest here and all of those take a struct flock *. Anything else is
|
||||
* passed straight through as a void * - wide enough for both an int and a pointer on every ABI
|
||||
* the runtime is built for.
|
||||
*/
|
||||
static int is_lock_command(int cmd) {
|
||||
switch (cmd) {
|
||||
case F_SETLK:
|
||||
case F_SETLKW:
|
||||
#ifdef F_OFD_SETLK
|
||||
case F_OFD_SETLK:
|
||||
#endif
|
||||
#ifdef F_OFD_SETLKW
|
||||
case F_OFD_SETLKW:
|
||||
#endif
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int is_lock_query(int cmd) {
|
||||
switch (cmd) {
|
||||
case F_GETLK:
|
||||
#ifdef F_OFD_GETLK
|
||||
case F_OFD_GETLK:
|
||||
#endif
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int fcntl(int fd, int cmd, ...) {
|
||||
static int (*real_fcntl)(int, int, ...);
|
||||
va_list ap;
|
||||
void *arg;
|
||||
int result;
|
||||
|
||||
va_start(ap, cmd);
|
||||
arg = va_arg(ap, void *);
|
||||
va_end(ap);
|
||||
|
||||
if (real_fcntl == NULL) real_fcntl = dlsym(RTLD_NEXT, "fcntl");
|
||||
if (real_fcntl == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
result = real_fcntl(fd, cmd, arg);
|
||||
if (result >= 0 || !unsupported(errno)) return result;
|
||||
if (is_lock_command(cmd)) return 0;
|
||||
if (is_lock_query(cmd) && arg != NULL) {
|
||||
((struct flock *)arg)->l_type = F_UNLCK;
|
||||
return 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#if defined(__GLIBC__) && defined(F_SETLK64)
|
||||
/* glibc's large-file build redirects both names; the runtime is 64-bit, so they are the same call. */
|
||||
int fcntl64(int fd, int cmd, ...) __attribute__((alias("fcntl")));
|
||||
#endif
|
||||
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Loopback socket ownership for Steam's IPC peer check.
|
||||
*
|
||||
* Steam validates a websocket peer by running lsof to find which process owns a 127.0.0.1 port.
|
||||
* The Android sandbox denies both /proc/net/tcp and sock_diag, so no tool can answer. Every Steam
|
||||
* process is preloaded with this library, so each records the ports it binds and connects, and the
|
||||
* lsof Steam spawns is answered from that shared registry instead of being run.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <arpa/inet.h>
|
||||
#include <spawn.h>
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const char *net_dir(void) {
|
||||
static char dir[256];
|
||||
if (!dir[0]) {
|
||||
const char *base = getenv("BL_SYSV_DIR");
|
||||
snprintf(dir, sizeof(dir), "%s/net", base && *base ? base : "/dev/shm/wnsysv");
|
||||
char parent[256];
|
||||
snprintf(parent, sizeof(parent), "%s", dir);
|
||||
char *slash = strrchr(parent, '/');
|
||||
if (slash) {
|
||||
*slash = '\0';
|
||||
mkdir(parent, 0777);
|
||||
}
|
||||
mkdir(dir, 0777);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
/* Record that this process owns a loopback socket on its local port; peer is the far end's port,
|
||||
* 0 for a listening socket. */
|
||||
static void record_port(int fd, unsigned peer) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof(ss);
|
||||
if (getsockname(fd, (struct sockaddr *)&ss, &len) != 0) {
|
||||
return;
|
||||
}
|
||||
unsigned port;
|
||||
if (ss.ss_family == AF_INET) {
|
||||
struct sockaddr_in *in = (struct sockaddr_in *)&ss;
|
||||
if (in->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
|
||||
return;
|
||||
}
|
||||
port = ntohs(in->sin_port);
|
||||
} else if (ss.ss_family == AF_INET6) {
|
||||
port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
if (port == 0) {
|
||||
return;
|
||||
}
|
||||
char path[300];
|
||||
snprintf(path, sizeof(path), "%s/p%u", net_dir(), port);
|
||||
char line[64];
|
||||
int n = snprintf(line, sizeof(line), "%d %d %d %u\n", (int)getpid(), (int)getppid(), (int)getuid(), peer);
|
||||
int out = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
if (out >= 0) {
|
||||
if (write(out, line, n) != n) { /* best effort */ }
|
||||
close(out);
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned addr_port(const struct sockaddr *addr, socklen_t len) {
|
||||
if (addr && addr->sa_family == AF_INET && len >= sizeof(struct sockaddr_in)) {
|
||||
return ntohs(((const struct sockaddr_in *)addr)->sin_port);
|
||||
}
|
||||
if (addr && addr->sa_family == AF_INET6 && len >= sizeof(struct sockaddr_in6)) {
|
||||
return ntohs(((const struct sockaddr_in6 *)addr)->sin6_port);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned peer_port(int fd) {
|
||||
struct sockaddr_storage ps;
|
||||
socklen_t plen = sizeof(ps);
|
||||
if (getpeername(fd, (struct sockaddr *)&ps, &plen) != 0) {
|
||||
return 0;
|
||||
}
|
||||
return addr_port((struct sockaddr *)&ps, plen);
|
||||
}
|
||||
|
||||
typedef int (*bind_fn)(int, const struct sockaddr *, socklen_t);
|
||||
typedef int (*listen_fn)(int, int);
|
||||
typedef int (*connect_fn)(int, const struct sockaddr *, socklen_t);
|
||||
typedef int (*accept4_fn)(int, struct sockaddr *, socklen_t *, int);
|
||||
|
||||
/* udevmon.c: a stand-in for the netlink socket the sandbox refuses, bound here without the kernel. */
|
||||
__attribute__((visibility("hidden"))) int bl_udevmon_stand_in(int fd);
|
||||
|
||||
int bind(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
static bind_fn real;
|
||||
if (!real) real = (bind_fn)dlsym(RTLD_NEXT, "bind");
|
||||
if (bl_udevmon_stand_in(fd)) {
|
||||
if (addr == NULL || len < sizeof(struct sockaddr_nl) || addr->sa_family != AF_NETLINK) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int ret = real(fd, addr, len);
|
||||
if (ret == 0) record_port(fd, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int listen(int fd, int backlog) {
|
||||
static listen_fn real;
|
||||
if (!real) real = (listen_fn)dlsym(RTLD_NEXT, "listen");
|
||||
int ret = real(fd, backlog);
|
||||
if (ret == 0) record_port(fd, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int connect(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
static connect_fn real;
|
||||
if (!real) real = (connect_fn)dlsym(RTLD_NEXT, "connect");
|
||||
int ret = real(fd, addr, len);
|
||||
if (ret == 0 || errno == EINPROGRESS) {
|
||||
/* The caller goes on to test for EINPROGRESS, which the bookkeeping must not disturb. */
|
||||
int saved = errno;
|
||||
record_port(fd, addr_port(addr, len));
|
||||
errno = saved;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int accept4(int fd, struct sockaddr *addr, socklen_t *len, int flags) {
|
||||
static accept4_fn real;
|
||||
if (!real) real = (accept4_fn)dlsym(RTLD_NEXT, "accept4");
|
||||
int ret = real(fd, addr, len, flags);
|
||||
if (ret >= 0) record_port(ret, peer_port(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
int accept(int fd, struct sockaddr *addr, socklen_t *len) {
|
||||
return accept4(fd, addr, len, 0);
|
||||
}
|
||||
|
||||
/* lsof would read the denied /proc/net/tcp; answer its -i TCP@host:port query from the registry
|
||||
* instead of executing it. Recognised only when invoked as lsof with that query. */
|
||||
static int maybe_answer_lsof(const char *path, char *const argv[]) {
|
||||
const char *base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
if (strcmp(base, "lsof") != 0) {
|
||||
return 0;
|
||||
}
|
||||
const char *port_str = NULL;
|
||||
for (int i = 0; argv[i]; i++) {
|
||||
const char *at = strstr(argv[i], "TCP@");
|
||||
if (at) {
|
||||
const char *colon = strrchr(at, ':');
|
||||
if (colon) port_str = colon + 1;
|
||||
}
|
||||
}
|
||||
if (!port_str) {
|
||||
return 0;
|
||||
}
|
||||
char path_buf[300];
|
||||
snprintf(path_buf, sizeof(path_buf), "%s/p%u", net_dir(), (unsigned)atoi(port_str));
|
||||
int in = open(path_buf, O_RDONLY);
|
||||
if (in < 0) {
|
||||
_exit(1); /* lsof exit code for "nothing found" */
|
||||
}
|
||||
char buf[64];
|
||||
int n = read(in, buf, sizeof(buf) - 1);
|
||||
close(in);
|
||||
if (n <= 0) {
|
||||
_exit(1);
|
||||
}
|
||||
buf[n] = '\0';
|
||||
int pid = 0, ppid = 0, uid = 0;
|
||||
unsigned peer = 0;
|
||||
sscanf(buf, "%d %d %d %u", &pid, &ppid, &uid, &peer);
|
||||
/* Steam splits the address on "->" and wants exactly two halves, taking the port it asked
|
||||
* about from the left one; only a listening socket has no far end. */
|
||||
char out[160];
|
||||
int m = peer ? snprintf(out, sizeof(out), "p%d\nR%d\nu%d\nn127.0.0.1:%u->127.0.0.1:%u\n",
|
||||
pid, ppid, uid, (unsigned)atoi(port_str), peer)
|
||||
: snprintf(out, sizeof(out), "p%d\nR%d\nu%d\nn127.0.0.1:%u\n",
|
||||
pid, ppid, uid, (unsigned)atoi(port_str));
|
||||
if (write(1, out, m) != m) { /* best effort */ }
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
int execve(const char *path, char *const argv[], char *const envp[]) {
|
||||
static int (*real)(const char *, char *const[], char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[], char *const[]))dlsym(RTLD_NEXT, "execve");
|
||||
maybe_answer_lsof(path, argv);
|
||||
return real(path, argv, envp);
|
||||
}
|
||||
|
||||
/* popen(3) and posix_spawn(3) reach lsof without going through the execve symbol above. The
|
||||
* check is cheap, so a spawned lsof is answered here too by handling it in a short-lived child. */
|
||||
static int answer_lsof_via_spawn(const char *path, char *const argv[], pid_t *pid) {
|
||||
pid_t child = fork();
|
||||
if (child < 0) {
|
||||
return errno;
|
||||
}
|
||||
if (child == 0) {
|
||||
maybe_answer_lsof(path, argv);
|
||||
_exit(127);
|
||||
}
|
||||
if (pid) {
|
||||
*pid = child;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int execvp(const char *file, char *const argv[]) {
|
||||
static int (*real)(const char *, char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execvp");
|
||||
maybe_answer_lsof(file, argv);
|
||||
return real(file, argv);
|
||||
}
|
||||
|
||||
int execv(const char *path, char *const argv[]) {
|
||||
static int (*real)(const char *, char *const[]);
|
||||
if (!real) real = (int (*)(const char *, char *const[]))dlsym(RTLD_NEXT, "execv");
|
||||
maybe_answer_lsof(path, argv);
|
||||
return real(path, argv);
|
||||
}
|
||||
|
||||
int posix_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *fa,
|
||||
const posix_spawnattr_t *attr, char *const argv[], char *const envp[]) {
|
||||
const char *base = strrchr(path, '/');
|
||||
base = base ? base + 1 : path;
|
||||
if (strcmp(base, "lsof") == 0 && !fa) {
|
||||
return answer_lsof_via_spawn(path, argv, pid);
|
||||
}
|
||||
static int (*real)(pid_t *, const char *, const posix_spawn_file_actions_t *,
|
||||
const posix_spawnattr_t *, char *const[], char *const[]);
|
||||
if (!real) real = (int (*)(pid_t *, const char *, const posix_spawn_file_actions_t *,
|
||||
const posix_spawnattr_t *, char *const[], char *const[]))dlsym(RTLD_NEXT, "posix_spawn");
|
||||
return real(pid, path, fa, attr, argv, envp);
|
||||
}
|
||||
@@ -0,0 +1,506 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
|
||||
* changed to this project's. The reasoning below is the original author's.
|
||||
*
|
||||
* Network interfaces for a sandbox that hides them.
|
||||
*
|
||||
* Android denies an app the rtnetlink dump behind getifaddrs() and if_nameindex(), the hardware
|
||||
* address ioctl, and every table under /proc/net. Wine builds its adapter, address, route and
|
||||
* neighbour tables from exactly those, so a game saw a machine with no adapter, no address, no
|
||||
* gateway and no MAC, and one that checks its adapters before going online waited for good.
|
||||
* The app knows the real link from ConnectivityManager and writes it to /etc/bannerlator-net;
|
||||
* whatever the kernel refuses is answered from that file. Anything the kernel does answer wins.
|
||||
*/
|
||||
#define _GNU_SOURCE 1
|
||||
#include <arpa/inet.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LINK_FILE "/etc/bannerlator-net"
|
||||
#define LO_NAME "lo"
|
||||
#define LO_INDEX 1
|
||||
#define LO_MTU 65536
|
||||
#define MAX_ADDRS 16
|
||||
|
||||
struct link_addr {
|
||||
int family;
|
||||
unsigned char bytes[16];
|
||||
unsigned prefix;
|
||||
};
|
||||
|
||||
struct link {
|
||||
char name[IFNAMSIZ];
|
||||
unsigned index;
|
||||
int mtu;
|
||||
unsigned char mac[6];
|
||||
struct link_addr addrs[MAX_ADDRS];
|
||||
int addr_count;
|
||||
struct link_addr gateways[2];
|
||||
int gateway_count;
|
||||
};
|
||||
|
||||
static int parse_addr(const char *text, struct link_addr *out) {
|
||||
memset(out, 0, sizeof(*out));
|
||||
if (inet_pton(AF_INET, text, out->bytes) == 1) out->family = AF_INET;
|
||||
else if (inet_pton(AF_INET6, text, out->bytes) == 1) out->family = AF_INET6;
|
||||
else return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read on every call: the app rewrites the file when the device changes network. */
|
||||
static int load_link(struct link *link) {
|
||||
typedef FILE *(*fopen_fn)(const char *, const char *);
|
||||
fopen_fn real_fopen = (fopen_fn) dlsym(RTLD_NEXT, "fopen");
|
||||
FILE *f = real_fopen ? real_fopen(LINK_FILE, "re") : NULL;
|
||||
if (f == NULL) return 0;
|
||||
|
||||
memset(link, 0, sizeof(*link));
|
||||
char line[160], text[64];
|
||||
unsigned m[6], prefix;
|
||||
while (fgets(line, sizeof(line), f)) {
|
||||
if (sscanf(line, "if %15s %u %d", link->name, &link->index, &link->mtu) == 3) continue;
|
||||
if (sscanf(line, "mac %2x:%2x:%2x:%2x:%2x:%2x", m, m + 1, m + 2, m + 3, m + 4, m + 5) == 6) {
|
||||
for (int i = 0; i < 6; i++) link->mac[i] = (unsigned char) m[i];
|
||||
} else if (sscanf(line, "addr %63s %u", text, &prefix) == 2) {
|
||||
struct link_addr addr;
|
||||
if (link->addr_count < MAX_ADDRS && parse_addr(text, &addr)
|
||||
&& prefix <= (addr.family == AF_INET ? 32u : 128u)) {
|
||||
addr.prefix = prefix;
|
||||
link->addrs[link->addr_count++] = addr;
|
||||
}
|
||||
} else if (sscanf(line, "gw %63s", text) == 1) {
|
||||
struct link_addr addr;
|
||||
if (link->gateway_count < 2 && parse_addr(text, &addr)) {
|
||||
link->gateways[link->gateway_count++] = addr;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return link->name[0] != '\0' && link->index > LO_INDEX && link->addr_count > 0;
|
||||
}
|
||||
|
||||
static void prefix_mask(int family, unsigned prefix, unsigned char *out) {
|
||||
int len = family == AF_INET ? 4 : 16;
|
||||
for (int i = 0; i < len; i++, prefix -= prefix < 8 ? prefix : 8) {
|
||||
out[i] = prefix >= 8 ? 0xff : (unsigned char) (0xff00 >> prefix);
|
||||
}
|
||||
}
|
||||
|
||||
static int is_link_local(const struct link_addr *addr) {
|
||||
return addr->family == AF_INET6 && addr->bytes[0] == 0xfe && (addr->bytes[1] & 0xc0) == 0x80;
|
||||
}
|
||||
|
||||
/* One allocation per list. ifa_data of the head points at the head itself, which glibc never
|
||||
* does, so freeifaddrs() can tell the two kinds of list apart without keeping any state. */
|
||||
struct entry {
|
||||
struct ifaddrs ifa;
|
||||
struct sockaddr_storage addr, mask, extra;
|
||||
char name[IFNAMSIZ];
|
||||
};
|
||||
|
||||
static struct entry *fill_entry(struct entry *e, const char *name, unsigned flags) {
|
||||
snprintf(e->name, sizeof(e->name), "%s", name);
|
||||
e->ifa.ifa_name = e->name;
|
||||
e->ifa.ifa_flags = flags;
|
||||
e->ifa.ifa_addr = (struct sockaddr *) &e->addr;
|
||||
e[-1].ifa.ifa_next = &e->ifa;
|
||||
return e;
|
||||
}
|
||||
|
||||
static void fill_packet(struct entry *e, unsigned index, const unsigned char *mac) {
|
||||
struct sockaddr_ll *ll = (struct sockaddr_ll *) &e->addr;
|
||||
ll->sll_family = AF_PACKET;
|
||||
ll->sll_ifindex = (int) index;
|
||||
ll->sll_hatype = mac ? ARPHRD_ETHER : ARPHRD_LOOPBACK;
|
||||
ll->sll_halen = 6;
|
||||
if (mac) memcpy(ll->sll_addr, mac, 6);
|
||||
}
|
||||
|
||||
static void fill_inet(struct entry *e, const struct link_addr *addr, unsigned index) {
|
||||
unsigned char mask[16];
|
||||
prefix_mask(addr->family, addr->prefix, mask);
|
||||
e->ifa.ifa_netmask = (struct sockaddr *) &e->mask;
|
||||
if (addr->family == AF_INET) {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *) &e->addr;
|
||||
struct sockaddr_in *smask = (struct sockaddr_in *) &e->mask;
|
||||
sin->sin_family = smask->sin_family = AF_INET;
|
||||
memcpy(&sin->sin_addr, addr->bytes, 4);
|
||||
memcpy(&smask->sin_addr, mask, 4);
|
||||
if (e->ifa.ifa_flags & IFF_BROADCAST) {
|
||||
struct sockaddr_in *bcast = (struct sockaddr_in *) &e->extra;
|
||||
bcast->sin_family = AF_INET;
|
||||
bcast->sin_addr.s_addr = sin->sin_addr.s_addr | ~smask->sin_addr.s_addr;
|
||||
e->ifa.ifa_broadaddr = (struct sockaddr *) bcast;
|
||||
}
|
||||
} else {
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &e->addr;
|
||||
struct sockaddr_in6 *smask = (struct sockaddr_in6 *) &e->mask;
|
||||
sin6->sin6_family = smask->sin6_family = AF_INET6;
|
||||
memcpy(&sin6->sin6_addr, addr->bytes, 16);
|
||||
memcpy(&smask->sin6_addr, mask, 16);
|
||||
if (is_link_local(addr)) sin6->sin6_scope_id = index;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct link_addr LO_V4 = { AF_INET, { 127, 0, 0, 1 }, 8 };
|
||||
static const struct link_addr LO_V6 = { AF_INET6, { [15] = 1 }, 128 };
|
||||
|
||||
static int real_list_usable(const struct ifaddrs *list) {
|
||||
for (const struct ifaddrs *e = list; e; e = e->ifa_next) {
|
||||
if (e->ifa_addr && e->ifa_addr->sa_family == AF_INET && !(e->ifa_flags & IFF_LOOPBACK)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getifaddrs(struct ifaddrs **out) {
|
||||
typedef int (*getifaddrs_fn)(struct ifaddrs **);
|
||||
typedef void (*freeifaddrs_fn)(struct ifaddrs *);
|
||||
getifaddrs_fn real = (getifaddrs_fn) dlsym(RTLD_NEXT, "getifaddrs");
|
||||
struct ifaddrs *list = NULL;
|
||||
int rc = real(&list);
|
||||
int saved = errno;
|
||||
if (rc == 0 && real_list_usable(list)) {
|
||||
*out = list;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct link link;
|
||||
if (!load_link(&link)) {
|
||||
if (rc == 0) *out = list;
|
||||
errno = saved;
|
||||
return rc;
|
||||
}
|
||||
if (rc == 0 && list) ((freeifaddrs_fn) dlsym(RTLD_NEXT, "freeifaddrs"))(list);
|
||||
|
||||
/* A leading slot keeps fill_entry()'s back link in bounds; the list starts after it. */
|
||||
struct entry *block = calloc((size_t) link.addr_count + 5, sizeof(*block));
|
||||
if (block == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
unsigned lo_flags = IFF_UP | IFF_RUNNING | IFF_LOOPBACK;
|
||||
unsigned up_flags = IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST;
|
||||
struct entry *e = block;
|
||||
fill_packet(fill_entry(++e, LO_NAME, lo_flags), LO_INDEX, NULL);
|
||||
fill_inet(fill_entry(++e, LO_NAME, lo_flags), &LO_V4, LO_INDEX);
|
||||
fill_inet(fill_entry(++e, LO_NAME, lo_flags), &LO_V6, LO_INDEX);
|
||||
fill_packet(fill_entry(++e, link.name, up_flags), link.index, link.mac);
|
||||
for (int i = 0; i < link.addr_count; i++) {
|
||||
fill_inet(fill_entry(++e, link.name, up_flags), &link.addrs[i], link.index);
|
||||
}
|
||||
block[1].ifa.ifa_data = &block[1].ifa;
|
||||
*out = &block[1].ifa;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void freeifaddrs(struct ifaddrs *list) {
|
||||
typedef void (*freeifaddrs_fn)(struct ifaddrs *);
|
||||
if (list == NULL) return;
|
||||
if (list->ifa_data == list) {
|
||||
free((struct entry *) list - 1);
|
||||
return;
|
||||
}
|
||||
((freeifaddrs_fn) dlsym(RTLD_NEXT, "freeifaddrs"))(list);
|
||||
}
|
||||
|
||||
/* Names live in the same allocation as the array, right behind it; glibc strdup()s each one. */
|
||||
struct name_block {
|
||||
struct if_nameindex index[3];
|
||||
char names[2][IFNAMSIZ];
|
||||
};
|
||||
|
||||
struct if_nameindex *if_nameindex(void) {
|
||||
typedef struct if_nameindex *(*if_nameindex_fn)(void);
|
||||
struct if_nameindex *list = ((if_nameindex_fn) dlsym(RTLD_NEXT, "if_nameindex"))();
|
||||
int saved = errno;
|
||||
if (list && list[0].if_index != 0) return list;
|
||||
|
||||
struct link link;
|
||||
struct name_block *block;
|
||||
if (!load_link(&link) || (block = calloc(1, sizeof(*block))) == NULL) {
|
||||
errno = saved;
|
||||
return list;
|
||||
}
|
||||
if (list) if_freenameindex(list);
|
||||
snprintf(block->names[0], IFNAMSIZ, "%s", LO_NAME);
|
||||
snprintf(block->names[1], IFNAMSIZ, "%s", link.name);
|
||||
block->index[0] = (struct if_nameindex) { LO_INDEX, block->names[0] };
|
||||
block->index[1] = (struct if_nameindex) { link.index, block->names[1] };
|
||||
return block->index;
|
||||
}
|
||||
|
||||
void if_freenameindex(struct if_nameindex *list) {
|
||||
typedef void (*if_freenameindex_fn)(struct if_nameindex *);
|
||||
if (list == NULL) return;
|
||||
if (list[0].if_name == ((struct name_block *) list)->names[0]) {
|
||||
free(list);
|
||||
return;
|
||||
}
|
||||
((if_freenameindex_fn) dlsym(RTLD_NEXT, "if_freenameindex"))(list);
|
||||
}
|
||||
|
||||
unsigned int if_nametoindex(const char *name) {
|
||||
typedef unsigned int (*if_nametoindex_fn)(const char *);
|
||||
unsigned int index = ((if_nametoindex_fn) dlsym(RTLD_NEXT, "if_nametoindex"))(name);
|
||||
if (index != 0 || name == NULL) return index;
|
||||
int saved = errno;
|
||||
struct link link;
|
||||
if (strcmp(name, LO_NAME) == 0) return LO_INDEX;
|
||||
if (load_link(&link) && strcmp(name, link.name) == 0) return link.index;
|
||||
errno = saved;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *if_indextoname(unsigned int index, char name[IF_NAMESIZE]) {
|
||||
typedef char *(*if_indextoname_fn)(unsigned int, char *);
|
||||
char *found = ((if_indextoname_fn) dlsym(RTLD_NEXT, "if_indextoname"))(index, name);
|
||||
if (found || name == NULL) return found;
|
||||
int saved = errno;
|
||||
struct link link;
|
||||
if (index == LO_INDEX) return strcpy(name, LO_NAME);
|
||||
if (load_link(&link) && index == link.index) return strcpy(name, link.name);
|
||||
errno = saved;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int mac_unusable(const struct ifreq *ifr) {
|
||||
const unsigned char *a = (const unsigned char *) ifr->ifr_hwaddr.sa_data;
|
||||
if (ifr->ifr_hwaddr.sa_family != ARPHRD_ETHER) return 1;
|
||||
/* Android hands out 02:00:00:00:00:00 where it hides the real address. */
|
||||
int rest = a[1] | a[2] | a[3] | a[4] | a[5];
|
||||
return (a[0] & 0x03) || (rest == 0 && (a[0] == 0 || a[0] == 0x02));
|
||||
}
|
||||
|
||||
static const struct link_addr *first_v4(const struct link *link) {
|
||||
for (int i = 0; i < link->addr_count; i++) {
|
||||
if (link->addrs[i].family == AF_INET) return &link->addrs[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int answer_ifreq(unsigned long request, struct ifreq *ifr, int real_rc) {
|
||||
char name[IFNAMSIZ + 1] = { 0 };
|
||||
memcpy(name, ifr->ifr_name, IFNAMSIZ);
|
||||
int loopback = strcmp(name, LO_NAME) == 0;
|
||||
struct link link;
|
||||
if (!load_link(&link) || (!loopback && strcmp(name, link.name) != 0)) return 0;
|
||||
|
||||
if (request == SIOCGIFHWADDR) {
|
||||
if (real_rc == 0 && (loopback || !mac_unusable(ifr))) return 0;
|
||||
memset(&ifr->ifr_hwaddr, 0, sizeof(ifr->ifr_hwaddr));
|
||||
ifr->ifr_hwaddr.sa_family = loopback ? ARPHRD_LOOPBACK : ARPHRD_ETHER;
|
||||
if (!loopback) memcpy(ifr->ifr_hwaddr.sa_data, link.mac, 6);
|
||||
return 1;
|
||||
}
|
||||
if (real_rc == 0) return 0;
|
||||
|
||||
const struct link_addr *v4 = loopback ? &LO_V4 : first_v4(&link);
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr->ifr_addr;
|
||||
unsigned char mask[16];
|
||||
switch (request) {
|
||||
case SIOCGIFFLAGS:
|
||||
ifr->ifr_flags = loopback ? IFF_UP | IFF_RUNNING | IFF_LOOPBACK
|
||||
: IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST;
|
||||
return 1;
|
||||
case SIOCGIFMTU:
|
||||
ifr->ifr_mtu = loopback ? LO_MTU : link.mtu;
|
||||
return 1;
|
||||
case SIOCGIFINDEX:
|
||||
ifr->ifr_ifindex = loopback ? LO_INDEX : (int) link.index;
|
||||
return 1;
|
||||
case SIOCGIFADDR:
|
||||
case SIOCGIFNETMASK:
|
||||
case SIOCGIFBRDADDR:
|
||||
if (v4 == NULL) return 0;
|
||||
prefix_mask(AF_INET, v4->prefix, mask);
|
||||
memset(sin, 0, sizeof(*sin));
|
||||
sin->sin_family = AF_INET;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
unsigned char *out = (unsigned char *) &sin->sin_addr + i;
|
||||
*out = request == SIOCGIFNETMASK ? mask[i]
|
||||
: request == SIOCGIFADDR ? v4->bytes[i]
|
||||
: (unsigned char) (v4->bytes[i] | ~mask[i]);
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int is_link_query(unsigned long request) {
|
||||
return request == SIOCGIFHWADDR || request == SIOCGIFFLAGS || request == SIOCGIFMTU
|
||||
|| request == SIOCGIFINDEX || request == SIOCGIFADDR || request == SIOCGIFNETMASK
|
||||
|| request == SIOCGIFBRDADDR;
|
||||
}
|
||||
|
||||
/* Every DRM and evdev call comes through here, so the lookup is done once. Two threads racing
|
||||
* to store it store the same pointer. */
|
||||
int ioctl(int fd, unsigned long request, ...) {
|
||||
typedef int (*ioctl_fn)(int, unsigned long, void *);
|
||||
static ioctl_fn real;
|
||||
va_list ap;
|
||||
va_start(ap, request);
|
||||
void *arg = va_arg(ap, void *);
|
||||
va_end(ap);
|
||||
|
||||
if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl");
|
||||
int rc = real(fd, request, arg);
|
||||
if (arg == NULL || !is_link_query(request)) return rc;
|
||||
int saved = errno;
|
||||
if (answer_ifreq(request, (struct ifreq *) arg, rc)) return 0;
|
||||
errno = saved;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void print_v6(FILE *out, const unsigned char *bytes) {
|
||||
for (int i = 0; i < 16; i++) fprintf(out, "%02x", bytes[i]);
|
||||
}
|
||||
|
||||
static unsigned v6_scope(const struct link_addr *addr) {
|
||||
if (memcmp(addr, &LO_V6, sizeof(*addr)) == 0) return 0x10;
|
||||
return is_link_local(addr) ? 0x20 : 0x00;
|
||||
}
|
||||
|
||||
static void write_table(FILE *out, const char *table, const struct link *link) {
|
||||
static const unsigned char zero[16];
|
||||
unsigned char mask[16];
|
||||
if (strcmp(table, "route") == 0) {
|
||||
fprintf(out, "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT\n");
|
||||
for (int i = 0; i < link->gateway_count; i++) {
|
||||
if (link->gateways[i].family != AF_INET) continue;
|
||||
unsigned gw;
|
||||
memcpy(&gw, link->gateways[i].bytes, 4);
|
||||
fprintf(out, "%s\t00000000\t%08X\t0003\t0\t0\t0\t00000000\t0\t0\t0\n", link->name, gw);
|
||||
}
|
||||
for (int i = 0; i < link->addr_count; i++) {
|
||||
if (link->addrs[i].family != AF_INET) continue;
|
||||
unsigned addr, net;
|
||||
prefix_mask(AF_INET, link->addrs[i].prefix, mask);
|
||||
memcpy(&addr, link->addrs[i].bytes, 4);
|
||||
memcpy(&net, mask, 4);
|
||||
fprintf(out, "%s\t%08X\t00000000\t0001\t0\t0\t0\t%08X\t0\t0\t0\n",
|
||||
link->name, addr & net, net);
|
||||
}
|
||||
} else if (strcmp(table, "ipv6_route") == 0) {
|
||||
for (int i = 0; i < link->gateway_count; i++) {
|
||||
if (link->gateways[i].family != AF_INET6) continue;
|
||||
print_v6(out, zero);
|
||||
fprintf(out, " 00 ");
|
||||
print_v6(out, zero);
|
||||
fprintf(out, " 00 ");
|
||||
print_v6(out, link->gateways[i].bytes);
|
||||
fprintf(out, " 00000400 00000001 00000000 00000003 %8s\n", link->name);
|
||||
}
|
||||
for (int i = 0; i < link->addr_count; i++) {
|
||||
if (link->addrs[i].family != AF_INET6) continue;
|
||||
unsigned char net[16];
|
||||
prefix_mask(AF_INET6, link->addrs[i].prefix, mask);
|
||||
for (int b = 0; b < 16; b++) net[b] = link->addrs[i].bytes[b] & mask[b];
|
||||
print_v6(out, net);
|
||||
fprintf(out, " %02x ", link->addrs[i].prefix);
|
||||
print_v6(out, zero);
|
||||
fprintf(out, " 00 ");
|
||||
print_v6(out, zero);
|
||||
fprintf(out, " 00000100 00000001 00000000 00000001 %8s\n", link->name);
|
||||
}
|
||||
} else if (strcmp(table, "if_inet6") == 0) {
|
||||
print_v6(out, LO_V6.bytes);
|
||||
fprintf(out, " %02x %02x %02x 80 %8s\n", LO_INDEX, LO_V6.prefix, v6_scope(&LO_V6), LO_NAME);
|
||||
for (int i = 0; i < link->addr_count; i++) {
|
||||
if (link->addrs[i].family != AF_INET6) continue;
|
||||
print_v6(out, link->addrs[i].bytes);
|
||||
fprintf(out, " %02x %02x %02x 80 %8s\n", link->index, link->addrs[i].prefix,
|
||||
v6_scope(&link->addrs[i]), link->name);
|
||||
}
|
||||
} else if (strcmp(table, "dev") == 0) {
|
||||
fprintf(out, "Inter-| Receive | Transmit\n"
|
||||
" face |bytes packets errs drop fifo frame compressed multicast|"
|
||||
"bytes packets errs drop fifo colls carrier compressed\n");
|
||||
fprintf(out, "%6s: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", LO_NAME);
|
||||
fprintf(out, "%6s: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", link->name);
|
||||
} else if (strcmp(table, "arp") == 0) {
|
||||
fprintf(out, "IP address HW type Flags HW address Mask Device\n");
|
||||
} else {
|
||||
fprintf(out, "1\n");
|
||||
}
|
||||
}
|
||||
|
||||
static const char *denied_table(const char *path) {
|
||||
static const char *const tables[] = { "route", "ipv6_route", "if_inet6", "dev", "arp" };
|
||||
if (strncmp(path, "/proc/net/", 10) == 0) {
|
||||
for (size_t i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) {
|
||||
if (strcmp(path + 10, tables[i]) == 0) return tables[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
size_t len = strlen(path);
|
||||
if (strncmp(path, "/sys/class/net/", 15) == 0 && len > 8 && strcmp(path + len - 8, "/carrier") == 0) {
|
||||
return "carrier";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* A memfd rather than fmemopen(): the stream owns its contents, so fclose() frees everything. */
|
||||
static FILE *synthesize(const char *table) {
|
||||
struct link link;
|
||||
if (!load_link(&link)) return NULL;
|
||||
int fd = memfd_create("bannerlator-net", MFD_CLOEXEC);
|
||||
if (fd < 0) return NULL;
|
||||
FILE *out = fdopen(fd, "w+");
|
||||
if (out == NULL) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
write_table(out, table, &link);
|
||||
if (fflush(out) != 0) {
|
||||
fclose(out);
|
||||
return NULL;
|
||||
}
|
||||
rewind(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/* tracer.c */
|
||||
int bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
|
||||
static FILE *open_stream(const char *symbol, const char *path, const char *mode) {
|
||||
typedef FILE *(*fopen_fn)(const char *, const char *);
|
||||
int status = mode && mode[0] == 'r' && !strchr(mode, '+')
|
||||
? bl_status_without_tracer(path, O_RDONLY | O_CLOEXEC) : -1;
|
||||
if (status >= 0) {
|
||||
FILE *copy = fdopen(status, "r");
|
||||
if (copy == NULL) close(status);
|
||||
return copy;
|
||||
}
|
||||
FILE *f = ((fopen_fn) dlsym(RTLD_NEXT, symbol))(path, mode);
|
||||
if (f || path == NULL || mode == NULL || mode[0] != 'r') return f;
|
||||
int saved = errno;
|
||||
const char *table = denied_table(path);
|
||||
if (table && (f = synthesize(table)) != NULL) return f;
|
||||
errno = saved;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
FILE *fopen(const char *path, const char *mode) {
|
||||
return open_stream("fopen", path, mode);
|
||||
}
|
||||
|
||||
FILE *fopen64(const char *path, const char *mode) {
|
||||
return open_stream("fopen64", path, mode);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
|
||||
* changed to this project's. The reasoning below is the original author's.
|
||||
*
|
||||
* The adjustments a session's opens need.
|
||||
*
|
||||
* O_NOATIME on the game library: the user's games are bound in from shared storage, which Android
|
||||
* serves over FUSE with every file owned by its media provider rather than by the app. O_NOATIME
|
||||
* is reserved for a file's owner, so the kernel refuses such an open outright with EPERM. The
|
||||
* Steam client opens every content file it reads that way, and treats the refusal as the file
|
||||
* being unreadable or missing: a validation reports corrupt or missing files and an update is
|
||||
* cancelled. The flag only asks the filesystem to skip an access-time update; the open is retried
|
||||
* without it. Every other refusal is passed through untouched, and a file the process may open
|
||||
* with the flag still is.
|
||||
*
|
||||
* The kernel's trace marker: tracefs is world-writable here, so the client's tier0 opens
|
||||
* /sys/kernel/tracing/trace_marker at startup - three times over, once per module that profiles -
|
||||
* and writes a "[%s] tgid=%d begin_ctx=%lld" line for every scope it enters. Nothing in the
|
||||
* session reads those markers, and they are one more thing that can land in the wrong file when
|
||||
* a descriptor number changes hands (tracer.c has the cause). They have been found appended to an
|
||||
* app manifest, which the client then cannot parse, to a download's state file, and to content
|
||||
* chunks, which it quarantines as corrupt. Refused the descriptor, tier0 writes nothing at all and
|
||||
* has nothing to lose.
|
||||
*
|
||||
* A process status is served without the container's tracer in it; see tracer.c.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int bl_status_without_tracer(const char *path, int flags) __attribute__((visibility("hidden")));
|
||||
|
||||
static int retry_without_noatime(int fd, int flags) {
|
||||
return fd < 0 && errno == EPERM && (flags & O_NOATIME);
|
||||
}
|
||||
|
||||
/* Both spellings tier0 tries, the debugfs one first. */
|
||||
static int is_trace_marker(const char *path) {
|
||||
return path != NULL
|
||||
&& (strcmp(path, "/sys/kernel/tracing/trace_marker") == 0
|
||||
|| strcmp(path, "/sys/kernel/debug/tracing/trace_marker") == 0);
|
||||
}
|
||||
|
||||
int open(const char *path, int flags, ...) {
|
||||
static int (*real_open)(const char *, int, ...);
|
||||
va_list ap;
|
||||
mode_t mode;
|
||||
int fd;
|
||||
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, mode_t);
|
||||
va_end(ap);
|
||||
if (is_trace_marker(path)) {
|
||||
errno = EACCES;
|
||||
return -1;
|
||||
}
|
||||
if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd;
|
||||
if (real_open == NULL) real_open = dlsym(RTLD_NEXT, "open");
|
||||
if (real_open == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
fd = real_open(path, flags, mode);
|
||||
if (retry_without_noatime(fd, flags)) fd = real_open(path, flags & ~O_NOATIME, mode);
|
||||
return fd;
|
||||
}
|
||||
|
||||
int openat(int dirfd, const char *path, int flags, ...) {
|
||||
static int (*real_openat)(int, const char *, int, ...);
|
||||
va_list ap;
|
||||
mode_t mode;
|
||||
int fd;
|
||||
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, mode_t);
|
||||
va_end(ap);
|
||||
if (is_trace_marker(path)) {
|
||||
errno = EACCES;
|
||||
return -1;
|
||||
}
|
||||
if ((fd = bl_status_without_tracer(path, flags)) >= 0) return fd;
|
||||
if (real_openat == NULL) real_openat = dlsym(RTLD_NEXT, "openat");
|
||||
if (real_openat == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
fd = real_openat(dirfd, path, flags, mode);
|
||||
if (retry_without_noatime(fd, flags)) fd = real_openat(dirfd, path, flags & ~O_NOATIME, mode);
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* The runtime is 64-bit, so the large-file names are the same calls. */
|
||||
int open64(const char *path, int flags, ...) __attribute__((alias("open")));
|
||||
int openat64(int dirfd, const char *path, int flags, ...) __attribute__((alias("openat")));
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* get_robust_list(2) for the session's own threads.
|
||||
*
|
||||
* Android's app policy refuses the call, and Steam's cross-process mutex insists on seeing the
|
||||
* head glibc registered. The kernel's list protocol gives it away: a robust mutex this thread
|
||||
* holds is linked directly behind the registered head, so locking a private one and following
|
||||
* the link back finds the head without depending on libc's layout.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static long (*real_syscall)(long, ...);
|
||||
|
||||
static struct robust_list_head *thread_robust_head(void) {
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutex_t mutex;
|
||||
struct robust_list_head *head = NULL;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
|
||||
pthread_mutex_init(&mutex, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
if (pthread_mutex_lock(&mutex) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
/* The mutex holds tids and lock words beside the link, so a word is only followed
|
||||
* through a read that fails instead of faulting on a non-pointer. */
|
||||
uintptr_t lo = (uintptr_t)&mutex, hi = lo + sizeof(mutex);
|
||||
for (uintptr_t p = lo; p + sizeof(void *) <= hi && !head; p += sizeof(void *)) {
|
||||
struct robust_list_head *candidate = *(struct robust_list_head **)p;
|
||||
struct robust_list_head copy;
|
||||
struct iovec local = {©, sizeof(copy)}, remote = {candidate, sizeof(copy)};
|
||||
if ((uintptr_t)candidate & (sizeof(void *) - 1)) {
|
||||
continue;
|
||||
}
|
||||
if (process_vm_readv(getpid(), &local, 1, &remote, 1, 0) != (ssize_t)sizeof(copy)) {
|
||||
continue;
|
||||
}
|
||||
uintptr_t entry = (uintptr_t)copy.list.next;
|
||||
if (entry >= lo && entry < hi && copy.futex_offset < 0 && copy.futex_offset > -256) {
|
||||
head = candidate;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_mutex_destroy(&mutex);
|
||||
return head;
|
||||
}
|
||||
|
||||
long syscall(long number, ...) {
|
||||
va_list ap;
|
||||
long args[6];
|
||||
va_start(ap, number);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
args[i] = va_arg(ap, long);
|
||||
}
|
||||
va_end(ap);
|
||||
if (number == SYS_get_robust_list && (args[0] == 0 || args[0] == gettid())) {
|
||||
struct robust_list_head *head = thread_robust_head();
|
||||
if (!head) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
*(struct robust_list_head **)args[1] = head;
|
||||
*(size_t *)args[2] = sizeof(*head);
|
||||
return 0;
|
||||
}
|
||||
if (!real_syscall) {
|
||||
real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall");
|
||||
}
|
||||
return real_syscall(number, args[0], args[1], args[2], args[3], args[4], args[5]);
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
/* System V semaphores and shared memory for programs in the Linux runtime.
|
||||
*
|
||||
* Android kernels are built without System V IPC, so semget/shmget fail with ENOSYS. Steam's tier0
|
||||
* creates its client-instance semaphore with semget and gives up when that fails. This library is
|
||||
* LD_PRELOADed into the session and keeps each object in a file under /dev/shm (a bound directory
|
||||
* the whole session shares): a semaphore set is an array of counters waited on with futexes, a
|
||||
* segment is a plain file that shmat maps. Objects are found by the inode of their file, which is
|
||||
* what the id is. semop applies its operations one by one (no all-or-nothing across a set) and
|
||||
* SEM_UNDO is accepted but not performed - enough for the client, which uses single-counter sets.
|
||||
* Message queues stay unsupported. */
|
||||
#define _GNU_SOURCE
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <linux/futex.h>
|
||||
#include <pthread.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/sem.h>
|
||||
#include <sys/shm.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DIR_DEFAULT "/dev/shm/wnsysv"
|
||||
#define SEM_MAGIC 0x574e5345u /* "WNSE" */
|
||||
#define MAX_OBJECTS 64
|
||||
|
||||
struct sem_header {
|
||||
uint32_t magic;
|
||||
uint32_t nsems;
|
||||
uint32_t key;
|
||||
uint32_t mode;
|
||||
int32_t creator_pid;
|
||||
int32_t pad[11];
|
||||
};
|
||||
/* The counters follow the header, one 32-bit word each (a futex word). */
|
||||
|
||||
struct sem_map { int id; struct sem_header *hdr; size_t len; };
|
||||
struct shm_map { void *addr; size_t len; int id; };
|
||||
|
||||
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct sem_map g_sems[MAX_OBJECTS];
|
||||
static struct shm_map g_shms[MAX_OBJECTS];
|
||||
static char g_dir[PATH_MAX - NAME_MAX - 2];
|
||||
|
||||
static const char *object_dir(void) {
|
||||
if (!g_dir[0]) {
|
||||
const char *d = getenv("BL_SYSV_DIR");
|
||||
snprintf(g_dir, sizeof(g_dir), "%s", d && *d ? d : DIR_DEFAULT);
|
||||
mkdir(g_dir, 0700);
|
||||
}
|
||||
return g_dir;
|
||||
}
|
||||
|
||||
static int object_id(int fd) {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) != 0) return -1;
|
||||
return (int)(st.st_ino & 0x7fffffff);
|
||||
}
|
||||
|
||||
/* Open the object file of kind ("sem"/"shm") for key, creating it as flags ask. */
|
||||
static int open_object(const char *kind, key_t key, int flags, int *created) {
|
||||
char path[PATH_MAX];
|
||||
int oflags = O_RDWR | O_CLOEXEC;
|
||||
*created = 0;
|
||||
if (key == IPC_PRIVATE) {
|
||||
static atomic_uint serial;
|
||||
snprintf(path, sizeof(path), "%s/%s.p%d.%u", object_dir(), kind, (int)getpid(),
|
||||
atomic_fetch_add(&serial, 1));
|
||||
oflags |= O_CREAT | O_EXCL;
|
||||
} else {
|
||||
snprintf(path, sizeof(path), "%s/%s.k%08x", object_dir(), kind, (unsigned)key);
|
||||
if (flags & IPC_CREAT) oflags |= O_CREAT;
|
||||
if ((flags & (IPC_CREAT | IPC_EXCL)) == (IPC_CREAT | IPC_EXCL)) oflags |= O_EXCL;
|
||||
}
|
||||
int fd = open(path, oflags, (flags & 0777) ? (flags & 0777) : 0600);
|
||||
if (fd < 0) return -1;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0 && st.st_size == 0) *created = 1;
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* The object file for an id, found by inode. */
|
||||
static int open_by_id(const char *kind, int id) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return -1;
|
||||
struct dirent *e;
|
||||
int fd = -1;
|
||||
size_t klen = strlen(kind);
|
||||
while ((e = readdir(d))) {
|
||||
if (strncmp(e->d_name, kind, klen) || e->d_name[klen] != '.') continue;
|
||||
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
|
||||
char path[PATH_MAX];
|
||||
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
|
||||
fd = open(path, O_RDWR | O_CLOEXEC);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
if (fd < 0) errno = EINVAL;
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int unlink_by_id(const char *kind, int id) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return -1;
|
||||
struct dirent *e;
|
||||
int rc = -1;
|
||||
size_t klen = strlen(kind);
|
||||
while ((e = readdir(d))) {
|
||||
if (strncmp(e->d_name, kind, klen) || e->d_name[klen] != '.') continue;
|
||||
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
|
||||
char path[PATH_MAX];
|
||||
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
|
||||
rc = unlink(path);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
if (rc < 0) errno = EINVAL;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static long futex(void *addr, int op, int val, const struct timespec *timeout) {
|
||||
return syscall(SYS_futex, addr, op, val, timeout, NULL, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ semaphores */
|
||||
|
||||
static struct sem_map *sem_lookup(int id) {
|
||||
for (int i = 0; i < MAX_OBJECTS; i++)
|
||||
if (g_sems[i].hdr && g_sems[i].id == id) return &g_sems[i];
|
||||
int fd = open_by_id("sem", id);
|
||||
if (fd < 0) return NULL;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) != 0 || st.st_size < (off_t)sizeof(struct sem_header)) { close(fd); errno = EINVAL; return NULL; }
|
||||
void *m = mmap(NULL, (size_t)st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
if (m == MAP_FAILED) return NULL;
|
||||
struct sem_header *h = m;
|
||||
if (h->magic != SEM_MAGIC) { munmap(m, (size_t)st.st_size); errno = EINVAL; return NULL; }
|
||||
for (int i = 0; i < MAX_OBJECTS; i++) {
|
||||
if (g_sems[i].hdr) continue;
|
||||
g_sems[i].id = id; g_sems[i].hdr = h; g_sems[i].len = (size_t)st.st_size;
|
||||
return &g_sems[i];
|
||||
}
|
||||
munmap(m, (size_t)st.st_size);
|
||||
errno = ENOSPC;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void sem_forget(int id) {
|
||||
for (int i = 0; i < MAX_OBJECTS; i++)
|
||||
if (g_sems[i].hdr && g_sems[i].id == id) { munmap(g_sems[i].hdr, g_sems[i].len); g_sems[i].hdr = NULL; }
|
||||
}
|
||||
|
||||
static _Atomic int32_t *sem_words(struct sem_header *h) { return (_Atomic int32_t *)(h + 1); }
|
||||
|
||||
int semget(key_t key, int nsems, int semflg) {
|
||||
if (nsems < 0 || nsems > 250) { errno = EINVAL; return -1; }
|
||||
int created;
|
||||
int fd = open_object("sem", key, semflg, &created);
|
||||
if (fd < 0) return -1;
|
||||
if (created) {
|
||||
if (nsems == 0) { close(fd); errno = EINVAL; return -1; }
|
||||
size_t len = sizeof(struct sem_header) + (size_t)nsems * sizeof(int32_t);
|
||||
if (ftruncate(fd, (off_t)len) != 0) { close(fd); return -1; }
|
||||
struct sem_header h = {.magic = SEM_MAGIC, .nsems = (uint32_t)nsems, .key = (uint32_t)key,
|
||||
.mode = (uint32_t)(semflg & 0777), .creator_pid = (int32_t)getpid()};
|
||||
if (pwrite(fd, &h, sizeof(h), 0) != (ssize_t)sizeof(h)) { close(fd); return -1; }
|
||||
} else {
|
||||
struct sem_header h;
|
||||
if (pread(fd, &h, sizeof(h), 0) != (ssize_t)sizeof(h) || h.magic != SEM_MAGIC) { close(fd); errno = EINVAL; return -1; }
|
||||
if (nsems > 0 && (uint32_t)nsems > h.nsems) { close(fd); errno = EINVAL; return -1; }
|
||||
}
|
||||
int id = object_id(fd);
|
||||
close(fd);
|
||||
return id;
|
||||
}
|
||||
|
||||
static int sem_wait_op(_Atomic int32_t *word, short op, short flg, const struct timespec *timeout) {
|
||||
for (;;) {
|
||||
int32_t v = atomic_load(word);
|
||||
if (op == 0) {
|
||||
if (v == 0) return 0;
|
||||
} else if (v >= -op) {
|
||||
if (atomic_compare_exchange_weak(word, &v, v + op)) return 0;
|
||||
continue;
|
||||
}
|
||||
if (flg & IPC_NOWAIT) { errno = EAGAIN; return -1; }
|
||||
long r = futex(word, FUTEX_WAIT, v, timeout);
|
||||
if (r != 0 && errno == ETIMEDOUT) return -1;
|
||||
if (r != 0 && errno == EINTR) { errno = EINTR; return -1; }
|
||||
}
|
||||
}
|
||||
|
||||
int semtimedop(int semid, struct sembuf *sops, size_t nsops, const struct timespec *timeout) {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
struct sem_map *m = sem_lookup(semid);
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
if (!m) return -1;
|
||||
struct sem_header *h = m->hdr;
|
||||
_Atomic int32_t *words = sem_words(h);
|
||||
for (size_t i = 0; i < nsops; i++) {
|
||||
if (sops[i].sem_num >= h->nsems) { errno = EFBIG; return -1; }
|
||||
_Atomic int32_t *w = &words[sops[i].sem_num];
|
||||
if (sops[i].sem_op > 0) {
|
||||
atomic_fetch_add(w, sops[i].sem_op);
|
||||
futex(w, FUTEX_WAKE, INT_MAX, NULL);
|
||||
} else if (sem_wait_op(w, sops[i].sem_op, sops[i].sem_flg, timeout) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (sops[i].sem_op < 0) futex(w, FUTEX_WAKE, INT_MAX, NULL); /* zero-waiters see the change */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int semop(int semid, struct sembuf *sops, size_t nsops) { return semtimedop(semid, sops, nsops, NULL); }
|
||||
|
||||
int semctl(int semid, int semnum, int cmd, ...) {
|
||||
union { int val; struct semid_ds *buf; unsigned short *array; } arg = {0};
|
||||
if (cmd == SETVAL || cmd == IPC_STAT || cmd == IPC_SET || cmd == GETALL || cmd == SETALL) {
|
||||
va_list ap;
|
||||
va_start(ap, cmd);
|
||||
arg.buf = va_arg(ap, struct semid_ds *);
|
||||
va_end(ap);
|
||||
}
|
||||
if (cmd == IPC_RMID) {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
sem_forget(semid);
|
||||
int rc = unlink_by_id("sem", semid);
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
return rc;
|
||||
}
|
||||
pthread_mutex_lock(&g_lock);
|
||||
struct sem_map *m = sem_lookup(semid);
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
if (!m) return -1;
|
||||
struct sem_header *h = m->hdr;
|
||||
_Atomic int32_t *words = sem_words(h);
|
||||
switch (cmd) {
|
||||
case GETVAL:
|
||||
if (semnum < 0 || (uint32_t)semnum >= h->nsems) { errno = EINVAL; return -1; }
|
||||
return atomic_load(&words[semnum]);
|
||||
case SETVAL:
|
||||
if (semnum < 0 || (uint32_t)semnum >= h->nsems) { errno = EINVAL; return -1; }
|
||||
atomic_store(&words[semnum], (int32_t)(intptr_t)arg.buf);
|
||||
futex(&words[semnum], FUTEX_WAKE, INT_MAX, NULL);
|
||||
return 0;
|
||||
case GETALL:
|
||||
for (uint32_t i = 0; i < h->nsems; i++) arg.array[i] = (unsigned short)atomic_load(&words[i]);
|
||||
return 0;
|
||||
case SETALL:
|
||||
for (uint32_t i = 0; i < h->nsems; i++) {
|
||||
atomic_store(&words[i], arg.array[i]);
|
||||
futex(&words[i], FUTEX_WAKE, INT_MAX, NULL);
|
||||
}
|
||||
return 0;
|
||||
case GETPID: return h->creator_pid;
|
||||
case GETNCNT: case GETZCNT: return 0;
|
||||
case IPC_STAT:
|
||||
memset(arg.buf, 0, sizeof(*arg.buf));
|
||||
arg.buf->sem_perm.__key = (key_t)h->key;
|
||||
arg.buf->sem_perm.uid = arg.buf->sem_perm.cuid = getuid();
|
||||
arg.buf->sem_perm.gid = arg.buf->sem_perm.cgid = getgid();
|
||||
arg.buf->sem_perm.mode = (unsigned short)h->mode;
|
||||
arg.buf->sem_nsems = h->nsems;
|
||||
return 0;
|
||||
case IPC_SET: return 0;
|
||||
default: errno = EINVAL; return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ shared memory */
|
||||
|
||||
/* Linux keeps a removed segment attachable until the last detach, which X clients rely on: they
|
||||
* remove the segment right after asking the server to attach it. A removed segment is renamed out
|
||||
* of key lookup instead, and removed for good once it has been out of use for a while. */
|
||||
static int remove_shm(int id) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return -1;
|
||||
struct dirent *e;
|
||||
int rc = -1;
|
||||
while ((e = readdir(d))) {
|
||||
if (strncmp(e->d_name, "shm.", 4) || !strncmp(e->d_name, "shm.d.", 6)) continue;
|
||||
if ((int)(e->d_ino & 0x7fffffff) != id) continue;
|
||||
char from[PATH_MAX], to[PATH_MAX];
|
||||
snprintf(from, sizeof(from), "%s/%s", g_dir, e->d_name);
|
||||
snprintf(to, sizeof(to), "%s/shm.d.%s", g_dir, e->d_name + 4);
|
||||
rc = rename(from, to);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
if (rc < 0) errno = EINVAL;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void expire_removed_shm(void) {
|
||||
DIR *d = opendir(object_dir());
|
||||
if (!d) return;
|
||||
struct dirent *e;
|
||||
time_t now = time(NULL);
|
||||
while ((e = readdir(d))) {
|
||||
if (strncmp(e->d_name, "shm.d.", 6)) continue;
|
||||
char path[PATH_MAX];
|
||||
struct stat st;
|
||||
snprintf(path, sizeof(path), "%s/%s", g_dir, e->d_name);
|
||||
if (stat(path, &st) == 0 && now - st.st_mtime > 30) unlink(path);
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
int shmget(key_t key, size_t size, int shmflg) {
|
||||
int created;
|
||||
expire_removed_shm();
|
||||
int fd = open_object("shm", key, shmflg, &created);
|
||||
if (fd < 0) return -1;
|
||||
if (created) {
|
||||
if (size == 0) { close(fd); errno = EINVAL; return -1; }
|
||||
if (ftruncate(fd, (off_t)size) != 0) { close(fd); return -1; }
|
||||
} else {
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) != 0 || (size_t)st.st_size < size) { close(fd); errno = EINVAL; return -1; }
|
||||
}
|
||||
int id = object_id(fd);
|
||||
close(fd);
|
||||
return id;
|
||||
}
|
||||
|
||||
void *shmat(int shmid, const void *shmaddr, int shmflg) {
|
||||
int fd = open_by_id("shm", shmid);
|
||||
if (fd < 0) return (void *)-1;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) != 0) { close(fd); return (void *)-1; }
|
||||
int prot = PROT_READ | ((shmflg & SHM_RDONLY) ? 0 : PROT_WRITE);
|
||||
void *m = mmap((void *)shmaddr, (size_t)st.st_size, prot, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
if (m == MAP_FAILED) return (void *)-1;
|
||||
pthread_mutex_lock(&g_lock);
|
||||
for (int i = 0; i < MAX_OBJECTS; i++) {
|
||||
if (g_shms[i].addr) continue;
|
||||
g_shms[i].addr = m; g_shms[i].len = (size_t)st.st_size; g_shms[i].id = shmid;
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
return m;
|
||||
}
|
||||
|
||||
int shmdt(const void *shmaddr) {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
for (int i = 0; i < MAX_OBJECTS; i++) {
|
||||
if (g_shms[i].addr != shmaddr) continue;
|
||||
munmap(g_shms[i].addr, g_shms[i].len);
|
||||
g_shms[i].addr = NULL;
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
return 0;
|
||||
}
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int shmctl(int shmid, int cmd, struct shmid_ds *buf) {
|
||||
switch (cmd) {
|
||||
case IPC_RMID: {
|
||||
pthread_mutex_lock(&g_lock);
|
||||
int rc = remove_shm(shmid);
|
||||
pthread_mutex_unlock(&g_lock);
|
||||
return rc;
|
||||
}
|
||||
case IPC_STAT: {
|
||||
int fd = open_by_id("shm", shmid);
|
||||
if (fd < 0) return -1;
|
||||
struct stat st;
|
||||
int rc = fstat(fd, &st);
|
||||
close(fd);
|
||||
if (rc != 0) return -1;
|
||||
memset(buf, 0, sizeof(*buf));
|
||||
buf->shm_perm.uid = buf->shm_perm.cuid = getuid();
|
||||
buf->shm_perm.gid = buf->shm_perm.cgid = getgid();
|
||||
buf->shm_perm.mode = 0600;
|
||||
buf->shm_segsz = (size_t)st.st_size;
|
||||
buf->shm_nattch = 1;
|
||||
buf->shm_cpid = (pid_t)getpid();
|
||||
return 0;
|
||||
}
|
||||
case IPC_SET: case SHM_LOCK: case SHM_UNLOCK: return 0;
|
||||
default: errno = EINVAL; return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* fork() carries over only the calling thread, so a lock another thread was holding at that
|
||||
* instant stays held in the child by a thread that is not there to release it. Taking it before
|
||||
* the fork makes the copy consistent; the parent then unlocks it and the child, whose one thread
|
||||
* never locked it, gets a fresh one. (WinNative 79aa7f68.)
|
||||
*/
|
||||
static void sysv_lock_before_fork(void) { pthread_mutex_lock(&g_lock); }
|
||||
static void sysv_unlock_after_fork(void) { pthread_mutex_unlock(&g_lock); }
|
||||
static void sysv_reset_after_fork(void) { pthread_mutex_init(&g_lock, NULL); }
|
||||
|
||||
__attribute__((constructor)) static void install_sysv_fork_handlers(void) {
|
||||
pthread_atfork(sysv_lock_before_fork, sysv_unlock_after_fork, sysv_reset_after_fork);
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with names
|
||||
* changed to this project's. The reasoning below is the original author's.
|
||||
*
|
||||
* A process status without the container's tracer in it.
|
||||
*
|
||||
* proot runs the session under ptrace, so every process here reads a TracerPid in its
|
||||
* /proc/<pid>/status. The Steam client takes that to mean a debugger is attached, and its arm64
|
||||
* build breaks into the supposed debugger with a bare "svc #0" after every failed assertion. That
|
||||
* is no breakpoint: the registers still hold the call Plat_IsInDebugSession() just made, close(),
|
||||
* with its own result, 1, as the argument - so each assertion closes descriptor 1. The number then
|
||||
* goes to whatever the client opens next, and its standard output is written into that: app
|
||||
* manifests and content chunks, its own connections, and the socket a game's Steam API talks
|
||||
* over, which is how a game that stayed silent while loading found its pipe dead.
|
||||
*
|
||||
* The tracer is part of the container, not something a program should react to, so the status
|
||||
* files are served with TracerPid 0.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define STATUS_MAX 16384
|
||||
#define TRACER_FIELD "\nTracerPid:\t"
|
||||
|
||||
static const char *skip_digits(const char *s) {
|
||||
const char *end = s;
|
||||
while (isdigit((unsigned char)*end)) end++;
|
||||
return end == s ? NULL : end;
|
||||
}
|
||||
|
||||
/* /proc/{self,thread-self,<pid>}[/task/<tid>]/status */
|
||||
static int is_status_path(const char *path) {
|
||||
const char *rest;
|
||||
if (path == NULL || strncmp(path, "/proc/", 6) != 0) return 0;
|
||||
path += 6;
|
||||
if (strncmp(path, "self/", 5) == 0) rest = path + 4;
|
||||
else if (strncmp(path, "thread-self/", 12) == 0) rest = path + 11;
|
||||
else if ((rest = skip_digits(path)) == NULL) return 0;
|
||||
if (strncmp(rest, "/task/", 6) == 0 && (rest = skip_digits(rest + 6)) == NULL) return 0;
|
||||
return strcmp(rest, "/status") == 0;
|
||||
}
|
||||
|
||||
/* Chromium stands its hang watchdogs down when it sees a tracer, and the client's web helper does
|
||||
* not come up within their patience here: it is left reading the truth. */
|
||||
static int is_web_helper(void) {
|
||||
return strcmp(program_invocation_short_name, "steamwebhelper") == 0;
|
||||
}
|
||||
|
||||
static int write_all(int fd, const char *data, size_t len) {
|
||||
while (len > 0) {
|
||||
ssize_t n = write(fd, data, len);
|
||||
if (n < 0 && errno == EINTR) continue;
|
||||
if (n <= 0) return -1;
|
||||
data += n;
|
||||
len -= (size_t)n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The descriptor to hand back for a read-only open of a status file, or -1 with errno untouched
|
||||
* when the path is something else or the copy cannot be made, and the real open should run. */
|
||||
__attribute__((visibility("hidden"))) int bl_status_without_tracer(const char *path, int flags) {
|
||||
char text[STATUS_MAX];
|
||||
size_t len = 0;
|
||||
int saved = errno;
|
||||
int out = -1;
|
||||
|
||||
if ((flags & O_ACCMODE) != O_RDONLY || !is_status_path(path) || is_web_helper()) return -1;
|
||||
int in = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
|
||||
if (in < 0) goto done;
|
||||
for (;;) {
|
||||
ssize_t n = read(in, text + len, sizeof(text) - 1 - len);
|
||||
if (n < 0 && errno == EINTR) continue;
|
||||
if (n <= 0) break;
|
||||
len += (size_t)n;
|
||||
}
|
||||
close(in);
|
||||
text[len] = '\0';
|
||||
|
||||
char *field = strstr(text, TRACER_FIELD);
|
||||
if (field == NULL) goto done;
|
||||
char *value = field + strlen(TRACER_FIELD);
|
||||
char *line_end = strchr(value, '\n');
|
||||
if (line_end == NULL) goto done;
|
||||
|
||||
out = memfd_create("status", (flags & O_CLOEXEC) ? MFD_CLOEXEC : 0);
|
||||
if (out < 0) goto done;
|
||||
if (write_all(out, text, (size_t)(value - text)) != 0 || write_all(out, "0", 1) != 0
|
||||
|| write_all(out, line_end, len - (size_t)(line_end - text)) != 0
|
||||
|| lseek(out, 0, SEEK_SET) != 0) {
|
||||
close(out);
|
||||
out = -1;
|
||||
}
|
||||
done:
|
||||
errno = saved;
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Ported from WinNative (maxjivi05, feature/wayland-gamescope, 2026-09-18/19), GPL-3.0, with the
|
||||
* symbol prefix changed to match this library. The reasoning below is the original author's.
|
||||
*
|
||||
* udev's netlink monitor, stood in for where the sandbox refuses the socket it is built on.
|
||||
*
|
||||
* The app sandbox denies a NETLINK_KOBJECT_UEVENT socket outright. libudev cannot create a
|
||||
* monitor without one, and SDL's HID layer cannot start without a monitor: every SDL_hid_init()
|
||||
* fails, the Steam client's controller code retries it hundreds of times a second, its main loop
|
||||
* stalls, and the client asserts out from under the running game. Refusing SDL the library
|
||||
* altogether (the earlier answer here) is what produced that failure.
|
||||
*
|
||||
* So the socket is answered instead: one end of a datagram socketpair stands in for the netlink
|
||||
* socket, its peer held open so it never reads as hung up, and the few calls libudev makes on
|
||||
* the socket that only a netlink one could answer are answered here. Nothing is ever written to
|
||||
* it, so the monitor is live and silent, which is the most the kernel would deliver in a
|
||||
* container anyway. The real socket is tried first, so this stays out of the way wherever a
|
||||
* monitor could genuinely be created.
|
||||
*
|
||||
* Wine is left without one. Its HID bus reads the session's pads itself only when its SDL bus
|
||||
* cannot start, and that is the path inputudev.c describes them on. Given a monitor the SDL bus
|
||||
* comes up and takes the pads over from it.
|
||||
*/
|
||||
#define _GNU_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct stand_in {
|
||||
int fd; /* the descriptor handed out, -1 when the slot is free */
|
||||
int peer; /* the other end, kept open */
|
||||
ino_t inode; /* identifies the socket after fd is closed and its number reused */
|
||||
};
|
||||
|
||||
static struct stand_in stand_ins[16];
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void lock_before_fork(void) { pthread_mutex_lock(&lock); }
|
||||
static void unlock_after_fork(void) { pthread_mutex_unlock(&lock); }
|
||||
static void reset_after_fork(void) { pthread_mutex_init(&lock, NULL); }
|
||||
|
||||
__attribute__((constructor)) static void init_udevmon(void) {
|
||||
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++)
|
||||
stand_ins[i].fd = -1;
|
||||
pthread_atfork(lock_before_fork, unlock_after_fork, reset_after_fork);
|
||||
}
|
||||
|
||||
static ino_t socket_inode(int fd) {
|
||||
struct stat st;
|
||||
return fstat(fd, &st) == 0 ? st.st_ino : 0;
|
||||
}
|
||||
|
||||
/* Whether fd is one of the stand-in sockets. A slot whose descriptor was closed and reused by
|
||||
* something else is retired on sight, so a later socket with the same number is not mistaken. */
|
||||
static int is_stand_in(int fd) {
|
||||
int found = 0;
|
||||
if (fd < 0)
|
||||
return 0;
|
||||
pthread_mutex_lock(&lock);
|
||||
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++) {
|
||||
if (stand_ins[i].fd != fd)
|
||||
continue;
|
||||
if (stand_ins[i].inode == socket_inode(fd)) {
|
||||
found = 1;
|
||||
} else {
|
||||
close(stand_ins[i].peer);
|
||||
stand_ins[i].fd = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
return found;
|
||||
}
|
||||
|
||||
static int is_wine_process(void) {
|
||||
static int known;
|
||||
if (known == 0) {
|
||||
char exe[PATH_MAX];
|
||||
ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
const char *name = exe;
|
||||
if (n > 0) {
|
||||
exe[n] = '\0';
|
||||
name = strrchr(exe, '/');
|
||||
name = name != NULL ? name + 1 : exe;
|
||||
}
|
||||
known = n > 0 && strncmp(name, "wine", 4) == 0 ? 1 : -1;
|
||||
}
|
||||
return known == 1;
|
||||
}
|
||||
|
||||
static int make_stand_in(int type) {
|
||||
int sv[2];
|
||||
int saved = errno;
|
||||
int flags = SOCK_DGRAM | (type & (SOCK_CLOEXEC | SOCK_NONBLOCK));
|
||||
if (socketpair(AF_UNIX, flags, 0, sv) != 0) {
|
||||
errno = saved;
|
||||
return -1;
|
||||
}
|
||||
/* The peer is ours alone: it must not follow the socket into a child. */
|
||||
fcntl(sv[1], F_SETFD, FD_CLOEXEC);
|
||||
pthread_mutex_lock(&lock);
|
||||
for (size_t i = 0; i < sizeof(stand_ins) / sizeof(stand_ins[0]); i++) {
|
||||
if (stand_ins[i].fd == -1) {
|
||||
stand_ins[i].fd = sv[0];
|
||||
stand_ins[i].peer = sv[1];
|
||||
stand_ins[i].inode = socket_inode(sv[0]);
|
||||
pthread_mutex_unlock(&lock);
|
||||
return sv[0];
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&lock);
|
||||
close(sv[0]);
|
||||
close(sv[1]);
|
||||
errno = saved;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int socket(int domain, int type, int protocol) {
|
||||
static int (*real_socket)(int, int, int);
|
||||
int fd;
|
||||
|
||||
if (real_socket == NULL) real_socket = dlsym(RTLD_NEXT, "socket");
|
||||
if (real_socket == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
fd = real_socket(domain, type, protocol);
|
||||
if (fd >= 0 || domain != AF_NETLINK || protocol != NETLINK_KOBJECT_UEVENT)
|
||||
return fd;
|
||||
int refused = errno;
|
||||
if (is_wine_process()) {
|
||||
errno = refused;
|
||||
return -1;
|
||||
}
|
||||
return make_stand_in(type);
|
||||
}
|
||||
|
||||
/* For net.c's bind(): whether fd is a stand-in, which accepts any netlink address as bound. */
|
||||
__attribute__((visibility("hidden"))) int bl_udevmon_stand_in(int fd) {
|
||||
return is_stand_in(fd);
|
||||
}
|
||||
|
||||
int getsockname(int fd, struct sockaddr *addr, socklen_t *len) {
|
||||
static int (*real_getsockname)(int, struct sockaddr *, socklen_t *);
|
||||
struct sockaddr_nl nl;
|
||||
|
||||
if (is_stand_in(fd)) {
|
||||
if (addr == NULL || len == NULL) {
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
memset(&nl, 0, sizeof(nl));
|
||||
nl.nl_family = AF_NETLINK;
|
||||
nl.nl_pid = (unsigned)getpid();
|
||||
memcpy(addr, &nl, *len < sizeof(nl) ? *len : sizeof(nl));
|
||||
*len = sizeof(nl);
|
||||
return 0;
|
||||
}
|
||||
if (real_getsockname == NULL) real_getsockname = dlsym(RTLD_NEXT, "getsockname");
|
||||
if (real_getsockname == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return real_getsockname(fd, addr, len);
|
||||
}
|
||||
|
||||
int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) {
|
||||
static int (*real_setsockopt)(int, int, int, const void *, socklen_t);
|
||||
|
||||
if (is_stand_in(fd)) {
|
||||
/* Group membership and packet filters only mean something on a socket that receives; this
|
||||
* one never does, so they are accepted as set. The rest is genuine on a unix socket. */
|
||||
if (level == SOL_NETLINK)
|
||||
return 0;
|
||||
if (level == SOL_SOCKET && (optname == SO_ATTACH_FILTER || optname == SO_DETACH_FILTER))
|
||||
return 0;
|
||||
}
|
||||
if (real_setsockopt == NULL) real_setsockopt = dlsym(RTLD_NEXT, "setsockopt");
|
||||
if (real_setsockopt == NULL) {
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
return real_setsockopt(fd, level, optname, optval, optlen);
|
||||
}
|
||||
Reference in new issue
Block a user