upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorMoonEyes <ducoinbenjamin@yahoo.fr>2026-11-28 20:41:46 +0100
committerGitHub <noreply@github.com>2026-11-28 20:41:46 +0100
commitdd5fda18aaa9a08bd52772da5b88ffa092e22859 (patch)
tree727c00c507bc86266ec839a8dce45c336b662308 /scripts
parent6a9c4e06b19a414e42215148894b9faafa000884 (diff)
parentf017b43f9d4b45cbaf57554a82225032a4e0d536 (diff)
Merge pull request #2332 from kirigayakazima/fix/run-game-wine-safety
run_game_wine.sh: harden inner args, PCI matching, bash wrapper, and summary exit
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/run_game_wine.sh48
1 files changed, 35 insertions, 13 deletions
diff --git a/scripts/run_game_wine.sh b/scripts/run_game_wine.sh
index ce01b0a07..9d8f19a37 100755
--- a/scripts/run_game_wine.sh
+++ b/scripts/run_game_wine.sh
@@ -55,11 +55,13 @@ die() { echo "run_game_wine.sh: $*" >&2; exit 2; }
55 55
56if [ "${1:-}" = --inner ]; then 56if [ "${1:-}" = --inner ]; then
57 # ---- inside the session: DISPLAY is kwin's Xwayland (headless) or the caller's display (--visible) ---- 57 # ---- inside the session: DISPLAY is kwin's Xwayland (headless) or the caller's display (--visible) ----
58 [ "$#" -ge 5 ] || die "--inner requires RUN BUILD LIMIT SHOT_EVERY"
58 RUN=$2; BUILD=$3; LIMIT=$4; SHOT_EVERY=$5 59 RUN=$2; BUILD=$3; LIMIT=$4; SHOT_EVERY=$5
59 shift 5 60 shift 5
60 WINE=${WINE:-wine} 61 WINE=${WINE:-wine}
61 WINESERVER=$(dirname "$(command -v "$WINE")")/wineserver 62 wine_bin=$(command -v "$WINE" 2>/dev/null || true)
62 [ -x "$WINESERVER" ] || WINESERVER=wineserver 63 WINESERVER=${wine_bin:+$(dirname "$wine_bin")/wineserver}
64 [ -n "$WINESERVER" ] && [ -x "$WINESERVER" ] || WINESERVER=wineserver
63 unset WAYLAND_DISPLAY # Wine's X11 driver on Xwayland, never its Wayland driver 65 unset WAYLAND_DISPLAY # Wine's X11 driver on Xwayland, never its Wayland driver
64 { 66 {
65 echo "display: ${DISPLAY:-none}" 67 echo "display: ${DISPLAY:-none}"
@@ -70,7 +72,7 @@ if [ "${1:-}" = --inner ]; then
70 mkdir -p "$WINEPREFIX" 72 mkdir -p "$WINEPREFIX"
71 "$WINESERVER" -p 73 "$WINESERVER" -p
72 # Warm-up: starts the prefix's services; no Mono/Gecko prompts in a new prefix. 74 # Warm-up: starts the prefix's services; no Mono/Gecko prompts in a new prefix.
73 WINEDLLOVERRIDES="$WINEDLLOVERRIDES;mscoree,mshtml=" "$WINE" wineboot -u </dev/null >/dev/null 2>&1 75 WINEDLLOVERRIDES="${WINEDLLOVERRIDES:+$WINEDLLOVERRIDES;}mscoree,mshtml=" "$WINE" wineboot -u </dev/null >/dev/null 2>&1
74 SHOTS= 76 SHOTS=
75 if [ "$SHOT_EVERY" != 0 ]; then 77 if [ "$SHOT_EVERY" != 0 ]; then
76 mkdir -p "$RUN/shots" 78 mkdir -p "$RUN/shots"
@@ -97,7 +99,10 @@ if [ "${1:-}" = --inner ]; then
97 code=$? 99 code=$?
98 echo "$(( $(date +%s) - t0 ))" > "$RUN/seconds" 100 echo "$(( $(date +%s) - t0 ))" > "$RUN/seconds"
99 echo "$code" > "$RUN/exit_code" 101 echo "$code" > "$RUN/exit_code"
100 [ -n "$SHOTS" ] && kill "$SHOTS" 2>/dev/null 102 if [ -n "$SHOTS" ]; then
103 kill "$SHOTS" 2>/dev/null || true
104 wait "$SHOTS" 2>/dev/null || true
105 fi
101 "$WINESERVER" -k 2>/dev/null 106 "$WINESERVER" -k 2>/dev/null
102 exit 0 107 exit 0
103fi 108fi
@@ -132,10 +137,11 @@ done
132[ -n "$OUT" ] || die "--out is required: the run folder" 137[ -n "$OUT" ] || die "--out is required: the run folder"
133[ -n "$SHADERS" ] || die "--shaders (or LEONIDA_SHADERS) is required: the translated shader library, or none" 138[ -n "$SHADERS" ] || die "--shaders (or LEONIDA_SHADERS) is required: the translated shader library, or none"
134case $LIMIT in ''|*[!0-9]*|0*) die "--seconds must be a whole number of seconds above 0, not '$LIMIT'" ;; esac 139case $LIMIT in ''|*[!0-9]*|0*) die "--seconds must be a whole number of seconds above 0, not '$LIMIT'" ;; esac
135case $SHOT_EVERY in ''|*[!0-9]*) die "--screenshot-every must be a whole number of seconds, not '$SHOT_EVERY'" ;; esac 140case $SHOT_EVERY in ''|*[!0-9]*|0?*) die "--screenshot-every must be a whole number of seconds, not '$SHOT_EVERY'" ;; esac
136if [ -n "$DEVICE" ] && ! [[ $DEVICE =~ ^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$ ]]; then 141if [ -n "$DEVICE" ] && ! [[ $DEVICE =~ ^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$ ]]; then
137 die "--device takes VID:DID in hex, as lspci -nn prints it (e.g. 1002:744c), not '$DEVICE'" 142 die "--device takes VID:DID in hex, as lspci -nn prints it (e.g. 1002:744c), not '$DEVICE'"
138fi 143fi
144[ -e "$SRC" ] || die "no build path at $SRC"
139[ -f "$EXE" ] || die "no game executable at $EXE" 145[ -f "$EXE" ] || die "no game executable at $EXE"
140[ -d "$GAME" ] || die "no package folder at $GAME" 146[ -d "$GAME" ] || die "no package folder at $GAME"
141if [ "$SHADERS" != none ]; then 147if [ "$SHADERS" != none ]; then
@@ -195,6 +201,7 @@ fi
195# ---- one run at a time: a shared prefix has one wineserver ---- 201# ---- one run at a time: a shared prefix has one wineserver ----
196LOCK=${LOCK:-${XDG_RUNTIME_DIR:+$XDG_RUNTIME_DIR/leonida-run-game-wine.lock}} 202LOCK=${LOCK:-${XDG_RUNTIME_DIR:+$XDG_RUNTIME_DIR/leonida-run-game-wine.lock}}
197LOCK=${LOCK:-$RUN/run.lock} 203LOCK=${LOCK:-$RUN/run.lock}
204mkdir -p "$(dirname "$LOCK")" || die "cannot create lock parent for $LOCK"
198exec 9>"$LOCK" || die "cannot open the lock $LOCK" 205exec 9>"$LOCK" || die "cannot open the lock $LOCK"
199flock -n 9 || die "another run holds $LOCK" 206flock -n 9 || die "another run holds $LOCK"
200mkdir -p "$RUN/overlay" "$RUN/saves" 207mkdir -p "$RUN/overlay" "$RUN/saves"
@@ -217,11 +224,17 @@ else
217 unset DISPLAY XAUTHORITY 224 unset DISPLAY XAUTHORITY
218fi 225fi
219export QT_QPA_PLATFORM=offscreen 226export QT_QPA_PLATFORM=offscreen
227DRM_SYSFS=${LEONIDA_DRM_SYSFS_DIR:-/sys/class/drm}
220if [ -n "$DEVICE" ]; then 228if [ -n "$DEVICE" ]; then
221 export MESA_VK_DEVICE_SELECT="$DEVICE!" 229 export MESA_VK_DEVICE_SELECT="$DEVICE!"
222 # kwin and Xwayland's glamor on the card that renders, so the present path stays on one device. 230 # kwin and Xwayland's glamor on the card that renders, so the present path stays on one device.
223 for n in /sys/class/drm/renderD*; do 231 dev_lc=${DEVICE,,}
224 [ "$(cat "$n/device/vendor" 2>/dev/null):$(cat "$n/device/device" 2>/dev/null)" = "0x${DEVICE%%:*}:0x${DEVICE#*:}" ] \ 232 for n in "$DRM_SYSFS"/renderD*; do
233 [ -e "$n" ] || continue
234 ven=$(cat "$n/device/vendor" 2>/dev/null || true)
235 did=$(cat "$n/device/device" 2>/dev/null || true)
236 ven=${ven//$'\r'/}; did=${did//$'\r'/}
237 [ "${ven,,}:${did,,}" = "0x${dev_lc%%:*}:0x${dev_lc#*:}" ] \
225 && export KWIN_DRM_DEVICES=/dev/dri/${n##*/} 238 && export KWIN_DRM_DEVICES=/dev/dri/${n##*/}
226 done 239 done
227else 240else
@@ -242,8 +255,9 @@ else
242fi 255fi
243env | grep -E '^(LEONIDA_|MESA_VK|WINEDLL|KWIN_DRM)' | sort > "$RUN/env.txt" 256env | grep -E '^(LEONIDA_|MESA_VK|WINEDLL|KWIN_DRM)' | sort > "$RUN/env.txt"
244 257
245WINESERVER=$(dirname "$(command -v "$WINE")")/wineserver 258wine_bin=$(command -v "$WINE" 2>/dev/null || true)
246[ -x "$WINESERVER" ] || WINESERVER=wineserver 259WINESERVER=${wine_bin:+$(dirname "$wine_bin")/wineserver}
260[ -n "$WINESERVER" ] && [ -x "$WINESERVER" ] || WINESERVER=wineserver
247SESSION_PID= 261SESSION_PID=
248teardown() { 262teardown() {
249 "$WINESERVER" -k 2>/dev/null 263 "$WINESERVER" -k 2>/dev/null
@@ -257,7 +271,7 @@ trap 'exit 130' INT TERM HUP
257 271
258# The session command with its arguments baked in (kwin takes one program path). 272# The session command with its arguments baked in (kwin takes one program path).
259{ 273{
260 echo '#!/bin/sh' 274 echo '#!/usr/bin/env bash'
261 printf 'exec %q --inner %q %q %q %q' "$SELF" "$RUN" "$BUILD" "$LIMIT" "$SHOT_EVERY" 275 printf 'exec %q --inner %q %q %q %q' "$SELF" "$RUN" "$BUILD" "$LIMIT" "$SHOT_EVERY"
262 for a in "$@"; do printf ' %q' "$a"; done 276 for a in "$@"; do printf ' %q' "$a"; done
263 echo 277 echo
@@ -276,10 +290,17 @@ else
276fi 290fi
277SESSION_PID=$! 291SESSION_PID=$!
278# The session ends when the inner run returns; this outer limit adds a margin for the start-up. 292# The session ends when the inner run returns; this outer limit adds a margin for the start-up.
279( sleep $((LIMIT + 180)); kill -TERM "$SESSION_PID" 2>/dev/null ) 9>&- 2>/dev/null & 293( trap 'kill "${watch_sleep_pid:-}" 2>/dev/null || true; exit 0' TERM INT
294 watch_sleep_pid=""
295 sleep $((LIMIT + 180)) &
296 watch_sleep_pid=$!
297 wait "$watch_sleep_pid" 2>/dev/null || exit 0
298 kill -TERM "$SESSION_PID" 2>/dev/null ) 9>&- </dev/null >/dev/null 2>&1 &
280WATCH=$! 299WATCH=$!
281wait "$SESSION_PID" 300wait "$SESSION_PID"
282pkill -P "$WATCH" 2>/dev/null; kill "$WATCH" 2>/dev/null 301pkill -P "$WATCH" 2>/dev/null || true
302kill "$WATCH" 2>/dev/null || true
303wait "$WATCH" 2>/dev/null || true
283teardown; SESSION_PID= 304teardown; SESSION_PID=
284T1=$(date +%s) 305T1=$(date +%s)
285 306
@@ -289,5 +310,6 @@ if [ ! -f "$RUN/exit_code" ]; then
289 exit 3 310 exit 3
290fi 311fi
291python3 -I "$ROOT/tools/game_run_summary.py" "$RUN" --limit-seconds "$LIMIT" | tee "$RUN/summary.txt" 312python3 -I "$ROOT/tools/game_run_summary.py" "$RUN" --limit-seconds "$LIMIT" | tee "$RUN/summary.txt"
313summary_rc=${PIPESTATUS[0]}
292echo "output: $RUN (stderr.log, boot.jsonl, summary.txt); total $((T1 - T0)) s. The logs stay on this machine." 314echo "output: $RUN (stderr.log, boot.jsonl, summary.txt); total $((T1 - T0)) s. The logs stay on this machine."
293exit 0 315exit "$summary_rc"