Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

A process belongs to the environment named by its WINEPREFIX, with the working directory only as the fallback. A game whose folder is a symlink into another environment reported that other environment as its resolved working directory, so stopping it ended the game and stopping the game's own environment missed it (highball#270)

Gauthier Piarrette7ee1b61fbb6e
diff --git a/Sources/HighballKit/ProcessTable.swift b/Sources/HighballKit/ProcessTable.swift
index 9e8ae65..45a9f75 100644
--- a/Sources/HighballKit/ProcessTable.swift
+++ b/Sources/HighballKit/ProcessTable.swift
@@ -59,14 +59,39 @@ public enum ProcessTable {
return false
}
+ /// Whether a process belongs to the prefix. A Wine process carries WINEPREFIX in its
+ /// environment (Wine hands the whole Unix environment on to every process it starts, games
+ /// started by Steam included), and that decides. The working directory is the fallback, for
+ /// processes whose environment cannot be read (Apple platform binaries such as /bin/sh) or
+ /// that have no WINEPREFIX. A working directory alone misattributes a game whose folder is a
+ /// link into another environment, since the kernel reports the resolved path: stopping that
+ /// other environment ended the game, and stopping its own missed it (2026-10-06, Forza
+ /// Horizon 6 running in FH6Rec, its folder linked into M4W10). Pure, so it is tested
+ /// without processes.
+ public static func belongs(environmentPrefix: String?, workingDirectory cwd: String?, toPrefix prefix: String,
+ serverDirectory: String?) -> Bool {
+ if let environmentPrefix, !environmentPrefix.isEmpty { return environmentPrefix == prefix }
+ guard let cwd else { return false }
+ return belongs(workingDirectory: cwd, toPrefix: prefix, serverDirectory: serverDirectory)
+ }
+
/// Process ids that belong to the prefix, never our own.
public static func processes(ofPrefix prefix: URL) -> [pid_t] {
let root = canonical(prefix.path)
let server = serverDirectory(forPrefix: prefix).map { canonical($0.path) }
let me = getpid()
+ var resolved: [String: String] = [:] // a few prefixes recur across many processes
return allPIDs().filter { pid in
+ // Only our own processes have a readable working directory; the rest are skipped here.
guard pid != me, let cwd = workingDirectory(of: pid) else { return false }
- return belongs(workingDirectory: canonical(cwd), toPrefix: root, serverDirectory: server)
+ let declared = commandLineAndEnvironment(of: pid)?.environment["WINEPREFIX"].flatMap { path -> String? in
+ guard !path.isEmpty else { return nil }
+ if let hit = resolved[path] { return hit }
+ let canon = canonical(path)
+ resolved[path] = canon
+ return canon
+ }
+ return belongs(environmentPrefix: declared, workingDirectory: canonical(cwd), toPrefix: root, serverDirectory: server)
}
}
diff --git a/Tests/HighballKitTests/RegressionTests.swift b/Tests/HighballKitTests/RegressionTests.swift
index f48744a..f80588d 100644
--- a/Tests/HighballKitTests/RegressionTests.swift
+++ b/Tests/HighballKitTests/RegressionTests.swift
@@ -659,6 +659,20 @@ extension RegressionTests {
XCTAssertTrue(ProcessTable.belongs(workingDirectory: "/tmp/.wine-501/server-1-2", toPrefix: "/b/Gaming", serverDirectory: "/tmp/.wine-501/server-1-2"))
XCTAssertFalse(ProcessTable.belongs(workingDirectory: "/tmp/.wine-501/server-1-3", toPrefix: "/b/Gaming", serverDirectory: "/tmp/.wine-501/server-1-2"))
+ // WINEPREFIX decides when the environment can be read. A game whose folder is a link
+ // into another environment has that other environment as its resolved working
+ // directory; it still belongs where it runs (2026-10-06, Forza Horizon 6 in FH6Rec,
+ // its folder linked into M4W10: stopping M4W10 ended it).
+ let linkedGame = "/b/M4W10/drive_c/Program Files (x86)/Steam/steamapps/common/ForzaHorizon6"
+ XCTAssertTrue(ProcessTable.belongs(environmentPrefix: "/b/FH6Rec", workingDirectory: linkedGame, toPrefix: "/b/FH6Rec", serverDirectory: nil))
+ XCTAssertFalse(ProcessTable.belongs(environmentPrefix: "/b/FH6Rec", workingDirectory: linkedGame, toPrefix: "/b/M4W10", serverDirectory: nil),
+ "stopping the environment the folder links into leaves the game alone")
+ XCTAssertFalse(ProcessTable.belongs(environmentPrefix: "/b/Gaming2", workingDirectory: "/b/Gaming/drive_c", toPrefix: "/b/Gaming", serverDirectory: nil))
+ // No readable environment, or no WINEPREFIX in it: the working directory decides, as before.
+ XCTAssertTrue(ProcessTable.belongs(environmentPrefix: nil, workingDirectory: "/b/Gaming/drive_c", toPrefix: "/b/Gaming", serverDirectory: nil))
+ XCTAssertTrue(ProcessTable.belongs(environmentPrefix: "", workingDirectory: "/b/Gaming/drive_c", toPrefix: "/b/Gaming", serverDirectory: nil))
+ XCTAssertFalse(ProcessTable.belongs(environmentPrefix: nil, workingDirectory: nil, toPrefix: "/b/Gaming", serverDirectory: nil))
+
// The server directory is derived from the prefix path's device and inode, the way
// Wine names it.
var st = stat(); XCTAssertEqual(stat(prefix.path, &st), 0)