Repository Public
1import Foundation2import Darwin34/// A child process started with posix_spawn in its own process group, with stdout/stderr pipes and, optionally,5/// two extra pipes mapped to file descriptors 3 (child reads) and 4 (child writes).6/// Foundation's `Process` cannot map extra descriptors, hence the direct use of posix_spawn.7final class ChildProcess: @unchecked Sendable {8 let pid: pid_t9 /// Parent ends. -1 when absent or already closed.10 private(set) var stdoutFD: Int3211 private(set) var stderrFD: Int3212 private(set) var rpcReadFD: Int32 // requests written by the child on its fd 413 private(set) var rpcWriteFD: Int32 // answers read by the child on its fd 314 private let lock = NSLock()15 private var reaped = false1617 struct Options {18 var executable: String19 var arguments: [String]20 var environment: [String: String]21 var workingDirectory: String22 var rpcPipes = false23 }2425 init(_ o: Options) throws {26 var fds: [Int32] = []27 func fail(_ what: String, _ code: Int32) -> PythonRunError {28 fds.forEach { close($0) }29 return .launchFailed("\(what): \(String(cString: strerror(code)))")30 }31 /// A pipe whose two ends are close-on-exec and numbered ≥ 10, so they never collide with 0…4 in the child.32 func makePipe() throws -> (r: Int32, w: Int32) {33 var p: [Int32] = [0, 0]34 guard pipe(&p) == 0 else { throw fail("pipe", errno) }35 let r = fcntl(p[0], F_DUPFD_CLOEXEC, 10), w = fcntl(p[1], F_DUPFD_CLOEXEC, 10)36 close(p[0]); close(p[1])37 guard r >= 0, w >= 0 else { if r >= 0 { close(r) }; if w >= 0 { close(w) }; throw fail("fcntl", errno) }38 fds.append(r); fds.append(w)39 return (r, w)40 }41 let out = try makePipe(), err = try makePipe()42 var toChild: (r: Int32, w: Int32)?, fromChild: (r: Int32, w: Int32)?43 if o.rpcPipes { toChild = try makePipe(); fromChild = try makePipe() }4445 var actions: posix_spawn_file_actions_t?46 posix_spawn_file_actions_init(&actions)47 defer { posix_spawn_file_actions_destroy(&actions) }48 posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_RDONLY, 0)49 posix_spawn_file_actions_adddup2(&actions, out.w, 1)50 posix_spawn_file_actions_adddup2(&actions, err.w, 2)51 if let toChild, let fromChild {52 posix_spawn_file_actions_adddup2(&actions, toChild.r, 3)53 posix_spawn_file_actions_adddup2(&actions, fromChild.w, 4)54 }55 posix_spawn_file_actions_addchdir_np(&actions, o.workingDirectory)5657 var attr: posix_spawnattr_t?58 posix_spawnattr_init(&attr)59 defer { posix_spawnattr_destroy(&attr) }60 var defaults = sigset_t(), mask = sigset_t()61 sigfillset(&defaults); sigemptyset(&mask)62 posix_spawnattr_setsigdefault(&attr, &defaults)63 posix_spawnattr_setsigmask(&attr, &mask)64 posix_spawnattr_setpgroup(&attr, 0) // new process group led by the child65 // CLOEXEC_DEFAULT: only the descriptors set up above are inherited.66 posix_spawnattr_setflags(&attr, Int16(POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_CLOEXEC_DEFAULT))6768 let argv: [UnsafeMutablePointer<CChar>?] = ([o.executable] + o.arguments).map { strdup($0) } + [nil]69 let envp: [UnsafeMutablePointer<CChar>?] = o.environment.map { strdup("\($0.key)=\($0.value)") } + [nil]70 defer { argv.forEach { free($0) }; envp.forEach { free($0) } }71 var child: pid_t = 072 let rc = posix_spawn(&child, o.executable, &actions, &attr, argv, envp)73 guard rc == 0 else { throw fail("posix_spawn \(o.executable)", rc) }7475 close(out.w); close(err.w)76 if let toChild, let fromChild {77 close(toChild.r); close(fromChild.w)78 _ = fcntl(toChild.w, F_SETNOSIGPIPE, 1) // a dead child must give EPIPE, not kill the app79 rpcWriteFD = toChild.w; rpcReadFD = fromChild.r80 } else { rpcWriteFD = -1; rpcReadFD = -1 }81 pid = child; stdoutFD = out.r; stderrFD = err.r82 }8384 deinit { closeAll() }8586 func closeAll() {87 lock.lock(); defer { lock.unlock() }88 for fd in [stdoutFD, stderrFD, rpcReadFD, rpcWriteFD] where fd >= 0 { close(fd) }89 stdoutFD = -1; stderrFD = -1; rpcReadFD = -1; rpcWriteFD = -190 }9192 func closeRPCWrite() {93 lock.lock(); defer { lock.unlock() }94 if rpcWriteFD >= 0 { close(rpcWriteFD); rpcWriteFD = -1 }95 }9697 /// Blocks until the child exits. Returns (exit code, signal): code is 128 + signal when killed.98 func wait() -> (status: Int32, signal: Int32?) {99 var st: Int32 = 0100 while true {101 let r = waitpid(pid, &st, 0)102 if r == pid { break }103 if r < 0 && errno != EINTR { return (-1, nil) }104 }105 lock.lock(); reaped = true; lock.unlock()106 let low = st & 0x7f107 if low == 0 { return ((st >> 8) & 0xff, nil) }108 return (128 + low, low)109 }110111 var isReaped: Bool { lock.lock(); defer { lock.unlock() }; return reaped }112113 /// Writes all bytes to the child's fd 3. Returns false when the pipe is closed.114 func writeRPC(_ bytes: [UInt8]) -> Bool {115 lock.lock(); let fd = rpcWriteFD; lock.unlock()116 guard fd >= 0 else { return false }117 var offset = 0118 while offset < bytes.count {119 let n = bytes.withUnsafeBytes { Darwin.write(fd, $0.baseAddress! + offset, bytes.count - offset) }120 if n > 0 { offset += n } else if n < 0 && errno == EINTR { continue } else { return false }121 }122 return true123 }124}125126/// Process-table helpers (sysctl KERN_PROC_ALL): used to signal a whole tree and to prove no orphan is left.127enum ProcessTable {128 struct Entry: Hashable { var pid: pid_t; var ppid: pid_t; var pgid: pid_t; var start: Int }129130 static func snapshot() -> [Entry] {131 var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0]132 var size = 0133 guard sysctl(&mib, 4, nil, &size, nil, 0) == 0 else { return [] }134 size += 16 * MemoryLayout<kinfo_proc>.stride135 var procs = [kinfo_proc](repeating: kinfo_proc(), count: size / MemoryLayout<kinfo_proc>.stride)136 guard sysctl(&mib, 4, &procs, &size, nil, 0) == 0 else { return [] }137 return procs.prefix(size / MemoryLayout<kinfo_proc>.stride).map {138 Entry(pid: $0.kp_proc.p_pid, ppid: $0.kp_eproc.e_ppid, pgid: $0.kp_eproc.e_pgid,139 start: Int($0.kp_proc.p_un.__p_starttime.tv_sec) * 1_000_000 + Int($0.kp_proc.p_un.__p_starttime.tv_usec))140 }141 }142143 /// The root, every member of its process group, and every descendant of those (alive now).144 static func family(of root: pid_t, in table: [Entry]? = nil) -> [Entry] {145 let all = table ?? snapshot()146 var children: [pid_t: [Entry]] = [:]147 for e in all { children[e.ppid, default: []].append(e) }148 var seen = Set<pid_t>(), out: [Entry] = []149 var queue = all.filter { $0.pid == root || $0.pgid == root }150 while let e = queue.popLast() {151 guard e.pid > 1, seen.insert(e.pid).inserted else { continue }152 out.append(e)153 queue.append(contentsOf: children[e.pid] ?? [])154 }155 return out156 }157158 static func isAlive(_ pid: pid_t) -> Bool { kill(pid, 0) == 0 || errno == EPERM }159160 /// Resident physical memory of one process in bytes (0 when unknown).161 static func footprint(_ pid: pid_t) -> UInt64 {162 var info = rusage_info_v2()163 let rc = withUnsafeMutablePointer(to: &info) { p in164 p.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(pid, RUSAGE_INFO_V2, $0) }165 }166 return rc == 0 ? info.ri_phys_footprint : 0167 }168}169170/// Signals a process group and everything descended from it, remembering who was seen so that a later SIGKILL171/// also reaches children that left the group in the meantime.172final class TreeKiller: @unchecked Sendable {173 private let root: pid_t174 private let lock = NSLock()175 private var known: [pid_t: Int] = [:] // pid → start time, to survive pid reuse176177 init(root: pid_t) { self.root = root }178179 /// Records the current family; call periodically while the script runs.180 func observe() {181 let fam = ProcessTable.family(of: root)182 lock.lock(); for e in fam { known[e.pid] = e.start }; lock.unlock()183 }184185 func signal(_ sig: Int32) {186 let table = ProcessTable.snapshot()187 var targets = Dictionary(uniqueKeysWithValues: ProcessTable.family(of: root, in: table).map { ($0.pid, $0.start) })188 lock.lock()189 let byPid = Dictionary(table.map { ($0.pid, $0) }, uniquingKeysWith: { a, _ in a })190 for (pid, start) in known { if let e = byPid[pid], e.start == start {191 for d in ProcessTable.family(of: pid, in: table) { targets[d.pid] = d.start }192 } }193 for (pid, start) in targets { known[pid] = start }194 lock.unlock()195 killpg(root, sig)196 for pid in targets.keys where pid != getpid() { kill(pid, sig) }197 }198199 /// Pids that were part of the family at any observed moment and are still alive.200 func survivors() -> [pid_t] {201 let table = Dictionary(ProcessTable.snapshot().map { ($0.pid, $0.start) }, uniquingKeysWith: { a, _ in a })202 lock.lock(); defer { lock.unlock() }203 return known.filter { table[$0.key] == $0.value }.map(\.key).sorted()204 }205}206207/// Reads a descriptor on its own thread until EOF or `stop()`, delivering chunks in order.208final class FDReader: @unchecked Sendable {209 private let fd: Int32210 private let lock = NSLock()211 private var stopAt: Date?212 private let done = DispatchSemaphore(value: 0)213 private var finished = false214215 init(fd: Int32, name: String, onChunk: @escaping @Sendable ([UInt8]) -> Void, onEnd: @escaping @Sendable () -> Void) {216 self.fd = fd217 let t = Thread { [self] in218 var buf = [UInt8](repeating: 0, count: 64 * 1024)219 loop: while true {220 var p = pollfd(fd: fd, events: Int16(POLLIN), revents: 0)221 let r = poll(&p, 1, 100)222 if r < 0 { if errno == EINTR { continue }; break }223 if r == 0 {224 lock.lock(); let s = stopAt; lock.unlock()225 if let s, Date() >= s { break }226 continue227 }228 let n = buf.withUnsafeMutableBytes { read(fd, $0.baseAddress, $0.count) }229 if n > 0 { onChunk(Array(buf[0..<n])) } else if n < 0 && (errno == EINTR || errno == EAGAIN) { continue } else { break loop }230 }231 onEnd()232 lock.lock(); finished = true; lock.unlock()233 done.signal()234 }235 t.name = "orix.python.\(name)"236 t.stackSize = 512 * 1024237 t.start()238 }239240 /// Asks the thread to give up once the descriptor has been idle past `deadline` (it still drains pending data).241 func stop(after seconds: TimeInterval) { lock.lock(); if stopAt == nil { stopAt = Date().addingTimeInterval(seconds) }; lock.unlock() }242243 func waitUntilFinished() { done.wait(); done.signal() }244}245246/// Splits a byte stream into complete UTF-8 text, holding back a trailing partial sequence.247struct UTF8Chunker {248 private var pending: [UInt8] = []249 mutating func feed(_ bytes: [UInt8]) -> String {250 pending.append(contentsOf: bytes)251 var cut = pending.count, i = pending.count - 1, back = 0252 while i >= 0 && back < 4 {253 let b = pending[i]254 if b & 0xC0 == 0x80 { i -= 1; back += 1; continue }255 let need = b >= 0xF0 ? 4 : b >= 0xE0 ? 3 : b >= 0xC0 ? 2 : 1256 if need > back + 1 { cut = i }257 break258 }259 let text = String(decoding: pending[0..<cut], as: UTF8.self)260 pending.removeFirst(cut)261 return text262 }263 mutating func flush() -> String { defer { pending = [] }; return String(decoding: pending, as: UTF8.self) }264}265266/// Runs a short helper process to completion and captures its output (used for probes and package listings).267enum CapturedProcess {268 struct Output: Sendable { var status: Int32; var stdout: String; var stderr: String; var timedOut: Bool }269270 static func run(executable: String, arguments: [String], environment: [String: String], workingDirectory: String = "/",271 timeout: TimeInterval, onOutput: (@Sendable (PythonStream, String) -> Void)? = nil) async throws -> Output {272 let child = try ChildProcess(.init(executable: executable, arguments: arguments, environment: environment, workingDirectory: workingDirectory))273 let killer = TreeKiller(root: child.pid)274 final class Box: @unchecked Sendable { let lock = NSLock(); var out = [UInt8](), err = [UInt8](), timedOut = false; var oc = UTF8Chunker(), ec = UTF8Chunker() }275 let box = Box()276 let cap = 16 * 1024 * 1024277 return await withTaskCancellationHandler {278 await withCheckedContinuation { (cont: CheckedContinuation<Output, Never>) in279 let outReader = FDReader(fd: child.stdoutFD, name: "out", onChunk: { c in280 box.lock.lock(); if box.out.count < cap { box.out.append(contentsOf: c) }; let t = onOutput == nil ? "" : box.oc.feed(c); box.lock.unlock()281 if !t.isEmpty { onOutput?(.stdout, t) }282 }, onEnd: {})283 let errReader = FDReader(fd: child.stderrFD, name: "err", onChunk: { c in284 box.lock.lock(); if box.err.count < cap { box.err.append(contentsOf: c) }; let t = onOutput == nil ? "" : box.ec.feed(c); box.lock.unlock()285 if !t.isEmpty { onOutput?(.stderr, t) }286 }, onEnd: {})287 let timer = DispatchSource.makeTimerSource(queue: .global())288 timer.schedule(deadline: .now() + timeout)289 timer.setEventHandler { box.lock.lock(); box.timedOut = true; box.lock.unlock(); killer.signal(SIGKILL) }290 timer.resume()291 Thread.detachNewThread {292 let (status, _) = child.wait()293 timer.cancel()294 killer.signal(SIGKILL)295 outReader.stop(after: 1); errReader.stop(after: 1)296 outReader.waitUntilFinished(); errReader.waitUntilFinished()297 child.closeAll()298 box.lock.lock()299 let o = Output(status: status, stdout: String(decoding: box.out, as: UTF8.self), stderr: String(decoding: box.err, as: UTF8.self), timedOut: box.timedOut)300 box.lock.unlock()301 cont.resume(returning: o)302 }303 }304 } onCancel: { killer.signal(SIGKILL) }305 }306}307