Skip to content

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

12.3 KB
Raw Blame
import Foundation
import Darwin

/// The processes of a Wine prefix, seen from outside, and how to end them.
///
/// `wineserver -k` asks the server to kill its clients, and the server does so by signalling
/// each client thread. A client parked in the Mac driver's Cocoa run loop does not always act
/// on that before the server is gone, and once the server is gone nobody kills it: on this
/// machine a bottle's `services.exe`, `svchost.exe`, `plugplay.exe`, `explorer.exe` and
/// `rpcss.exe` outlived a day of stops, still holding the prefix's server directory (issue #48).
/// A server that died abnormally leaves the same debris, and `wineserver -w` only waits for
/// the server.
///
/// Every Wine process of a prefix runs with its working directory inside the prefix (Highball
/// launches into `drive_c`, Wine's own system processes sit in `drive_c/windows`), and the
/// server's working directory is the prefix's server directory, named from the prefix path's
/// device and inode. Both come from `proc_pidinfo`, no `lsof` or `ps` parsing. The one gap is
/// a program started with a working directory outside the prefix (a Z: drive launch), which
/// the server itself still kills on a normal stop.
public enum ProcessTable {
    /// Wine's server directory for a prefix: `/tmp/.wine-<uid>/server-<dev>-<inode>`.
    public static func serverDirectory(forPrefix prefix: URL) -> URL? {
        var st = stat()
        guard stat(prefix.path, &st) == 0 else { return nil }
        return URL(fileURLWithPath: "/tmp/.wine-\(getuid())/server-\(String(UInt64(st.st_dev), radix: 16))-\(String(st.st_ino, radix: 16))")
    }

    /// All process ids visible to this user.
    static func allPIDs() -> [pid_t] {
        let bytes = proc_listpids(UInt32(PROC_ALL_PIDS), 0, nil, 0)
        guard bytes > 0 else { return [] }
        var buf = [pid_t](repeating: 0, count: Int(bytes) / MemoryLayout<pid_t>.size + 64)
        let got = proc_listpids(UInt32(PROC_ALL_PIDS), 0, &buf, Int32(buf.count * MemoryLayout<pid_t>.size))
        guard got > 0 else { return [] }
        return buf[0..<Int(got) / MemoryLayout<pid_t>.size].filter { $0 > 0 }
    }

    /// Working directory of a process, or nil when it cannot be read (not ours, gone).
    public static func workingDirectory(of pid: pid_t) -> String? {
        var info = proc_vnodepathinfo()
        let size = Int32(MemoryLayout<proc_vnodepathinfo>.size)
        guard proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &info, size) == size else { return nil }
        return withUnsafePointer(to: &info.pvi_cdir.vip_path) {
            $0.withMemoryRebound(to: CChar.self, capacity: Int(MAXPATHLEN)) { String(cString: $0) }
        }
    }

    /// Resolves symlinks and private/tmp aliases the way the kernel reports them.
    static func canonical(_ path: String) -> String {
        URL(fileURLWithPath: path).resolvingSymlinksInPath().path
    }

    /// Whether a working directory belongs to the prefix: inside it, or its server directory.
    /// Pure, so it is tested without processes.
    public static func belongs(workingDirectory cwd: String, toPrefix prefix: String, serverDirectory: String?) -> Bool {
        let root = prefix.hasSuffix("/") ? prefix : prefix + "/"
        if cwd == prefix || cwd.hasPrefix(root) { return true }
        if let server = serverDirectory, cwd == server || cwd.hasPrefix(server + "/") { return true }
        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 }
            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)
        }
    }

    /// Command line and environment of a process we own, from `KERN_PROCARGS2`. Nil when the
    /// kernel refuses (not ours, restricted) or the process is gone. The environment comes back
    /// empty for Apple platform binaries (`/bin/sleep`, `/bin/sh`); Wine's binaries are not
    /// platform binaries, so theirs is readable.
    public static func commandLineAndEnvironment(of pid: pid_t) -> (arguments: [String], environment: [String: String])? {
        var mib: [Int32] = [CTL_KERN, KERN_PROCARGS2, pid]
        var size = 0
        guard sysctl(&mib, 3, nil, &size, nil, 0) == 0, size > MemoryLayout<Int32>.size else { return nil }
        var buf = [UInt8](repeating: 0, count: size)
        guard sysctl(&mib, 3, &buf, &size, nil, 0) == 0 else { return nil }
        let argc = Int(buf.withUnsafeBytes { $0.load(as: Int32.self) })
        let strings = buf[MemoryLayout<Int32>.size...].split(separator: 0, omittingEmptySubsequences: true)
            .map { String(decoding: $0, as: UTF8.self) }
        guard strings.count >= 1 + argc else { return nil }
        // Layout: executable path, argv[0..argc), then the environment.
        let arguments = Array(strings[1..<(1 + argc)])
        var environment: [String: String] = [:]
        for entry in strings.dropFirst(1 + argc) {
            guard let eq = entry.firstIndex(of: "=") else { continue }
            environment[String(entry[..<eq])] = String(entry[entry.index(after: eq)...])
        }
        return (arguments, environment)
    }

    /// The wineserver currently serving a prefix, if one runs: its working directory is the
    /// prefix's server directory.
    public static func liveServer(forPrefix prefix: URL) -> pid_t? {
        guard let server = serverDirectory(forPrefix: prefix).map({ canonical($0.path) }) else { return nil }
        let me = getpid()
        return allPIDs().first { pid in
            guard pid != me, let cwd = workingDirectory(of: pid) else { return false }
            return canonical(cwd) == server
        }
    }

    /// The sync mode a running wineserver was started with, read from its environment. Wine
    /// fixes the mode when the server starts, and a client started with a different one dies at
    /// `msync_init` ("Failed to open msync shared memory file", issue #32). Nil when no server
    /// runs or its environment cannot be read.
    public static func liveServerSync(forPrefix prefix: URL) -> SyncMode? {
        guard let pid = liveServer(forPrefix: prefix),
              let env = commandLineAndEnvironment(of: pid)?.environment,
              // The kernel hides the environment of Apple platform binaries; Wine's server is not
              // one, and Highball always sets both variables, so their absence means "unknown",
              // never "off".
              env["WINEMSYNC"] != nil || env["WINEESYNC"] != nil else { return nil }
        return SyncMode(environment: env)
    }

    /// The environment a running wineserver was started with, nil when none runs. Wine's
    /// `services.exe` and every Windows service it starts inherit exactly this environment,
    /// however the service was later asked for, because the prefix boots them once from the
    /// first process that started the server.
    public static func liveServerEnvironment(forPrefix prefix: URL) -> [String: String]? {
        guard let pid = liveServer(forPrefix: prefix) else { return nil }
        return commandLineAndEnvironment(of: pid)?.environment
    }

    /// Whether nothing but Wine's own plumbing and Windows services runs in the prefix, so the
    /// server can be restarted without ending anything a person started.
    ///
    /// The tell is the working directory: `services.exe`, `winedevice.exe`, `plugplay.exe`,
    /// `rpcss.exe`, `svchost.exe` and `explorer.exe` work in `drive_c/windows`, and services.exe
    /// starts every Windows service there too (Rockstar's, EA's, anti-cheat services; measured
    /// 2026-09-18). Programs Highball or a launcher started work in their own folder. Pure, so
    /// it is tested without processes.
    public static func isIdle(workingDirectories: [String], prefix: String, serverDirectory: String?) -> Bool {
        let root = prefix.hasSuffix("/") ? prefix : prefix + "/"
        let windows = root + "drive_c/windows"
        return workingDirectories.allSatisfy { cwd in
            if let server = serverDirectory, cwd == server || cwd.hasPrefix(server + "/") { return true }
            return cwd == windows || cwd.hasPrefix(windows + "/")
        }
    }

    /// Whether a prefix process is Wine's own plumbing (or the server) rather than a program:
    /// plumbing works in `drive_c/windows`, the server in its `/tmp` server directory. The
    /// rule `bottle ps` prints and the environment invariant asserts against. Pure.
    public static func isPlumbing(executable exe: String, workingDirectory cwd: String, prefix: String) -> Bool {
        let root = prefix.hasSuffix("/") ? prefix : prefix + "/"
        let windows = root + "drive_c/windows"
        if cwd == windows || cwd.hasPrefix(windows + "/") { return true }
        let name = exe.replacingOccurrences(of: "\\", with: "/").split(separator: "/").last.map(String.init) ?? exe
        return name == "wineserver" || name.hasSuffix("/wineserver")
    }

    /// `isPlumbing` for a running process, by its working directory and, for the server, its
    /// command line. False when the process is already gone.
    public static func isPlumbing(_ pid: pid_t, prefix: URL) -> Bool {
        guard let cwd = workingDirectory(of: pid) else { return false }
        let exe = commandLineAndEnvironment(of: pid)?.arguments.first ?? ""
        return isPlumbing(executable: exe, workingDirectory: canonical(cwd), prefix: canonical(prefix.path))
    }

    public static func isIdle(prefix: URL) -> Bool {
        let root = canonical(prefix.path)
        let server = serverDirectory(forPrefix: prefix).map { canonical($0.path) }
        let cwds = processes(ofPrefix: prefix).compactMap { workingDirectory(of: $0) }.map(canonical)
        return isIdle(workingDirectories: cwds, prefix: root, serverDirectory: server)
    }

    /// Ends the given processes: SIGTERM, a grace period, then SIGKILL for whatever is left.
    /// Returns the ids that had to be killed the hard way.
    @discardableResult
    public static func terminate(_ pids: [pid_t], grace: TimeInterval = 2) -> [pid_t] {
        guard !pids.isEmpty else { return [] }
        for pid in pids { kill(pid, SIGTERM) }
        let deadline = Date().addingTimeInterval(grace)
        var alive = pids
        while !alive.isEmpty && Date() < deadline {
            usleep(100_000)
            alive = alive.filter { kill($0, 0) == 0 }
        }
        for pid in alive { kill(pid, SIGKILL) }
        return alive
    }
}