Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

sweep and reaper: attribute service processes to their wineserver, clean orphans by prefix

mikey92cd686f5c254a
diff --git a/README.md b/README.md
index c4e8bd5e882..7e9d7b1b1ad 100644
--- a/README.md
+++ b/README.md
@@ -139,7 +139,7 @@ Start with `soju doctor`: it checks most of the items below and tells you which
### Leftover Wine processes
-Every bottle start also spawns idle Windows service processes (`services.exe`, `winedevice.exe`, `plugplay.exe`, `rpcss.exe`, `explorer.exe /desktop`, ~100 MB per set). If a bottle's `wineserver` is killed abruptly (a crash, an aborted run), those services never notice and linger. `scripts/soju-sweep.sh` removes them, and `play.sh kill` / `epic-kill` / `steam-kill` and the reaper call it automatically. It only runs when no `wineserver` is running at all, so a live game or launcher is never touched.
+Every bottle start also spawns idle Windows service processes (`services.exe`, `winedevice.exe`, `plugplay.exe`, `rpcss.exe`, `explorer.exe /desktop`, ~100 MB per set). If a bottle's `wineserver` is killed abruptly (a crash, an aborted run), those services never notice and linger. `scripts/soju-sweep.sh` removes them, and `play.sh kill` / `epic-kill` / `steam-kill` and the reaper call it automatically. It only removes service processes that belong to no running `wineserver` (every live bottle's processes hold a file open in their server's socket directory), so a live game or launcher is never touched, even while other bottles are up. `soju sweep` runs it by hand.
## What's in the repo
diff --git a/README.zh.md b/README.zh.md
index 668c5f89c40..810cd2f4dc3 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -139,7 +139,7 @@ scripts/play.sh steam-kill # 停止 Steam bottle
### 残留的 Wine 进程
-每次启动 bottle 都会附带一组空闲的 Windows 服务进程(`services.exe`、`winedevice.exe`、`plugplay.exe`、`rpcss.exe`、`explorer.exe /desktop`,每组约 100 MB)。如果 bottle 的 `wineserver` 被强杀(崩溃、中断的运行),这些服务不会察觉,会一直挂着。`scripts/soju-sweep.sh` 负责清掉它们,`play.sh kill` / `epic-kill` / `steam-kill` 和守护进程会自动调用。它只在完全没有 `wineserver` 运行时才动手,正在运行的游戏或登录器绝不会被误伤。
+每次启动 bottle 都会附带一组空闲的 Windows 服务进程(`services.exe`、`winedevice.exe`、`plugplay.exe`、`rpcss.exe`、`explorer.exe /desktop`,每组约 100 MB)。如果 bottle 的 `wineserver` 被强杀(崩溃、中断的运行),这些服务不会察觉,会一直挂着。`scripts/soju-sweep.sh` 负责清掉它们,`play.sh kill` / `epic-kill` / `steam-kill` 和守护进程会自动调用。它它只清理不属于任何运行中 `wineserver` 的服务进程(存活 bottle 的每个进程都在其服务端 socket 目录里打开着文件),因此正在运行的游戏或登录器绝不会被误伤,即使其他 bottle 还在运行也一样。可用 `soju sweep` 手动执行。
## 仓库里有什么
diff --git a/scripts/soju-reaper.sh b/scripts/soju-reaper.sh
index df6d104926a..c8c752e9a60 100755
--- a/scripts/soju-reaper.sh
+++ b/scripts/soju-reaper.sh
@@ -113,20 +113,24 @@ classify() { # sets ALIVE_PIDS, GAME_PIDS
done
}
+# PIDs of Wine processes holding any file open under this prefix: every
+# process of a bottle keeps at least drive_c/windows open, so this finds them
+# without the server (which the orphan case has lost).
+prefix_pids() {
+ local cand real
+ cand=$(pgrep -f '\.exe' 2>/dev/null | tr '\n' ',' | sed 's/,$//')
+ [ -n "$cand" ] || return 0
+ real="$(cd "$PREFIX" 2>/dev/null && pwd -P)" || return 0
+ lsof -a -p "$cand" -Fpn 2>/dev/null | awk -v P="n$real/" '
+ /^p/ { pid = substr($0, 2) }
+ /^n/ { if (index($0, P) == 1) seen[pid] = 1 }
+ END { for (p in seen) print p }' | sort -un
+}
+
reap_orphans() {
local pids
echo "soju-reaper: the wineserver for $PREFIX is gone, cleaning up its leftovers"
- # Without a server the tmpmap files are gone too, so attribution by server
- # directory no longer works: fall back to the launcher/helper/game names of
- # this mode, and let the sweep take the service set.
- pids=$(pgrep -f "$HELPER_RE" 2>/dev/null || true)
- [ -n "$GAME_RE" ] && pids="$pids $(pgrep -f "$GAME_RE" 2>/dev/null || true)"
- case "$MODE" in
- steam) pids="$pids $(pgrep -f "$(exe_re 'steam\.exe')" 2>/dev/null || true)" ;;
- epic) pids="$pids $(pgrep -f "$(exe_re 'EpicGamesLauncher\.exe')" 2>/dev/null || true)" ;;
- gog) pids="$pids $(pgrep -f "$(exe_re 'GalaxyClient\.exe')" 2>/dev/null || true)" ;;
- *) pids="$pids $(pgrep -f "$(exe_re 'Battle\.net\.exe|Battle\.net Launcher\.exe')" 2>/dev/null || true)" ;;
- esac
+ pids=$(prefix_pids)
# shellcheck disable=SC2086
[ -n "${pids// /}" ] && kill -9 $pids 2>/dev/null || true
sleep 2; [ -x "$SWEEP" ] && "$SWEEP" >/dev/null 2>&1
diff --git a/scripts/soju-sweep.sh b/scripts/soju-sweep.sh
index a65f5c17348..37fdeb63af6 100755
--- a/scripts/soju-sweep.sh
+++ b/scripts/soju-sweep.sh
@@ -6,19 +6,46 @@
# /desktop: ~100 MB together). When the bottle's wineserver is killed
# (wineserver -k, a crashed launcher, an aborted test) the apps die, but these
# sleeping services never talk to the server again and so never notice it is
-# gone; they linger for hours. This sweeps them: but ONLY when no wineserver
-# is running at all, i.e. when nothing in any bottle can still be alive, so a
-# running game or launcher is never touched.
+# gone; they linger for hours. This sweeps them.
#
-# Usage: soju-sweep.sh (called by play.sh *-kill and by soju-reaper.sh)
+# Only service processes are candidates, never launchers or games, and a
+# candidate is removed only if it is attached to no running wineserver: every
+# process of a live bottle holds a file open in its server's socket directory
+# (the server's working directory), so the ones without such a file belong to
+# a server that is gone. Safe to run while other bottles are up.
+#
+# Usage: soju-sweep.sh (called by play.sh *-kill, soju-reaper.sh, soju sweep)
+# SOJU_SWEEP_DRY=1 soju-sweep.sh only print what would be removed
set -u
-if pgrep -x wineserver >/dev/null 2>&1 || pgrep -x wineserver64 >/dev/null 2>&1; then
- exit 0 # something is live, do nothing
-fi
# Also Wine's own prompts (the "Wine Mono Installer" dialog is control.exe
# appwiz.cpl install_mono): orphaned, they sit in the Dock as "wine" for hours.
RE='(explorer\.exe /desktop|services\.exe|winedevice\.exe|plugplay\.exe|rpcss\.exe|svchost\.exe|conhost\.exe|tabtip\.exe|control\.exe appwiz\.cpl|wineboot\.exe|winedbg\.exe)'
-PIDS=$(pgrep -f "$RE" 2>/dev/null || true)
-[ -n "$PIDS" ] || exit 0
+CAND=$(pgrep -f "$RE" 2>/dev/null || true)
+[ -n "$CAND" ] || exit 0
+
+SERVERS=$( { pgrep -x wineserver; pgrep -x wineserver64; } 2>/dev/null | tr '\n' ',' | sed 's/,$//')
+ATTACHED=""
+if [ -n "$SERVERS" ]; then
+ # One line per server directory, joined with "|" (macOS awk rejects a
+ # newline inside -v, and would silently see no directories at all).
+ DIRS=$(lsof -a -p "$SERVERS" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | tr '\n' '|')
+ ATTACHED=$(lsof -a -p "$(echo "$CAND" | tr '\n' ',' | sed 's/,$//')" -Fpn 2>/dev/null | awk -v dirs="$DIRS" '
+ BEGIN { n = split(dirs, d, "|") }
+ /^p/ { pid = substr($0, 2) }
+ /^n/ { for (i = 1; i <= n; i++) if (d[i] != "" && index($0, "n" d[i] "/") == 1) { seen[pid] = 1; break } }
+ END { for (p in seen) print p }' | tr '\n' ' ')
+fi
+
+PIDS=""
+for p in $CAND; do
+ case " $ATTACHED " in *" $p "*) ;; *) PIDS="$PIDS $p" ;; esac
+done
+[ -n "${PIDS// /}" ] || exit 0
+if [ "${SOJU_SWEEP_DRY:-0}" = 1 ]; then
+ echo "soju-sweep (dry run): would remove $(echo $PIDS | wc -w | tr -d ' ') orphaned Wine services:"
+ for p in $PIDS; do echo " $p $(ps -o args= -p "$p" 2>/dev/null | cut -c1-80)"; done
+ exit 0
+fi
echo "soju-sweep: removing orphaned Wine services: $(echo $PIDS | wc -w | tr -d ' ') processes"
+# shellcheck disable=SC2086
kill -9 $PIDS 2>/dev/null || true
diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh
index 7d24e61891c..94b24b4b253 100755
--- a/scripts/uninstall.sh
+++ b/scripts/uninstall.sh
@@ -27,9 +27,12 @@ WS="/Applications/Wine Stable.app/Contents/Resources/wine/bin/wineserver"
sleep 1
# 2. App bundles.
+# Only bundles install.sh wrote for this install (their launcher script names
+# this engine), so a custom SOJU_BASE never removes another install's apps.
apps=()
for a in "Battle.net" "Steam (Windows)" "Epic Games Launcher" "GOG GALAXY"; do
- [ -d "$HOME/Applications/$a.app" ] && apps+=("$HOME/Applications/$a.app")
+ l="$HOME/Applications/$a.app/Contents/MacOS/launcher"
+ [ -f "$l" ] && grep -q "scripts/play.sh" "$l" && grep -qF "ENGINE=\"$ENGINE\"" "$l" && apps+=("$HOME/Applications/$a.app")
done
if [ "${#apps[@]}" -gt 0 ]; then
printf 'Apps: %s\n' "${apps[@]}"