Keep a run-scoped evidence owner from acquisition through settlement and cleanup retry. Bind token observations to native generations and retain related generations and unresolved identity lookups across table omissions, reparenting and process-group changes without treating PID hints as authority. Use native effective UID facts for withholding decisions, corroborate Linux procfs absence, and share the Linux stat parser with sampling and debugger identity checks. Signal only after current token and group revalidation. Complete swept cleanup across all groups with one bounded final family check; retain unresolved owners and successful-peer completion across retries. Validation: npm run check, docs:check and test discovery passed. Focused ownership/consumer coverage totals 513 passed and 1 skipped across 35 files, including the corrected Electron cancellation test rerun independently. Darwin helper compilation, real owned Node child cleanup, CLI/MCP capture and retained cleanup retries ran. Real Electron applications, native Linux races/PID reuse and Windows cleanup were not qualified. The original Linux reports remain open for that work. Fixes #5238 Refs #4322, #5206
67 lines
2.6 KiB
Swift
67 lines
2.6 KiB
Swift
import Darwin
|
|
import Foundation
|
|
|
|
@main
|
|
struct ProcessRunTokenSnapshotProbe {
|
|
static func main() throws {
|
|
var sizeQueries = 0
|
|
var readAttempts = 0
|
|
let retried = readProcessSnapshot { _, output, size in
|
|
guard output != nil else {
|
|
sizeQueries += 1
|
|
size = MemoryLayout<kinfo_proc>.stride
|
|
return 0
|
|
}
|
|
readAttempts += 1
|
|
if readAttempts == 1 {
|
|
errno = ENOMEM
|
|
return -1
|
|
}
|
|
size = 0
|
|
return 0
|
|
}
|
|
|
|
let otherError = readProcessSnapshot { _, output, size in
|
|
if output == nil {
|
|
size = MemoryLayout<kinfo_proc>.stride
|
|
return 0
|
|
}
|
|
errno = EACCES
|
|
return -1
|
|
}
|
|
|
|
let absentIdentities = readProcessIdentities(
|
|
pids: [42],
|
|
snapshot: ProcessSnapshot(metadata: [:], failure: nil)
|
|
)
|
|
let failedIdentities = readProcessIdentities(
|
|
pids: [42],
|
|
snapshot: ProcessSnapshot(metadata: [:], failure: "process_table_failed_13")
|
|
)
|
|
let invalidIdentity = readProcessIdentities(
|
|
pids: [0],
|
|
snapshot: ProcessSnapshot(metadata: [:], failure: nil)
|
|
)
|
|
let zombieIdentity = readProcessIdentities(
|
|
pids: [42],
|
|
snapshot: ProcessSnapshot(
|
|
metadata: [42: ProcessMetadata(identity: "start:42", effectiveUid: 501, lifecycle: "zombie", parentPid: 7, processGroupId: 9, pointerSize: 8)],
|
|
failure: nil
|
|
)
|
|
)
|
|
|
|
let output: [String: Any] = [
|
|
"retriedAfterFreshSize": retried.failure == nil && sizeQueries == 2 && readAttempts == 2,
|
|
"preservedOtherErrno": otherError.failure == "process_table_failed_\(EACCES)",
|
|
"successfulSnapshotMissingPidIsAbsent": absentIdentities.first?.state == "absent" && absentIdentities.first?.reason == "not-in-snapshot",
|
|
"failedSnapshotMissingPidIsUnavailable": failedIdentities.first?.state == "unavailable" && failedIdentities.first?.reason == "process_table_failed_13",
|
|
"invalidPidIsUnavailable": invalidIdentity.first?.state == "unavailable" && invalidIdentity.first?.reason == "invalid_process_id",
|
|
"zombieLifecycleIsSeparateFromIdentity": zombieIdentity.first?.state == "readable" && zombieIdentity.first?.identity == "start:42" && zombieIdentity.first?.lifecycle == "zombie",
|
|
"effectiveUidComesFromNativeIdentity": zombieIdentity.first?.effective_uid == 501,
|
|
"nativeRelationIsSeparateFromIdentity": zombieIdentity.first?.relation?.parent_pid == 7 && zombieIdentity.first?.relation?.process_group_id == 9
|
|
]
|
|
let data = try JSONSerialization.data(withJSONObject: output, options: [.sortedKeys])
|
|
FileHandle.standardOutput.write(data)
|
|
}
|
|
}
|