@rbbtsn0w/adg 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/process-identity.js +38 -15
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { win32 } from "node:path";
|
|
3
3
|
import { runSubprocessSync } from "./subprocess.js";
|
|
4
4
|
let ownIdentity;
|
|
5
|
+
let ownWindowsFailure;
|
|
5
6
|
/** Return only OS birth evidence. Failure is uncertainty, never proof of death. */
|
|
6
7
|
export function queryProcessIdentity(pid) {
|
|
7
8
|
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
@@ -37,21 +38,43 @@ export function queryProcessIdentity(pid) {
|
|
|
37
38
|
return { kind: "darwin-ps-lstart", version: 1, birthToken: token };
|
|
38
39
|
}
|
|
39
40
|
if (process.platform === "win32") {
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
// Retain only bounded categories for self failures; never expose OS output,
|
|
42
|
+
// paths, or another process's diagnostic in the mutation error.
|
|
43
|
+
const fail = (reason) => {
|
|
44
|
+
if (pid === process.pid)
|
|
45
|
+
ownWindowsFailure = `Windows process query: ${reason}`;
|
|
42
46
|
return undefined;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
const systemRoot = process.env.SystemRoot;
|
|
50
|
+
if (!systemRoot || !win32.isAbsolute(systemRoot))
|
|
51
|
+
return fail("missing or non-absolute SystemRoot");
|
|
52
|
+
const executable = win32.join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
53
|
+
// Capture self identity before publishing a contender, allowing a cold shell start.
|
|
54
|
+
const timeout = pid === process.pid ? 10_000 : 2000;
|
|
55
|
+
const result = runSubprocessSync(executable, ["-NoProfile", "-NonInteractive", "-Command",
|
|
56
|
+
`(Get-Process -Id ${pid} -ErrorAction Stop).StartTime.ToFileTimeUtc().ToString([System.Globalization.CultureInfo]::InvariantCulture)`], {
|
|
57
|
+
encoding: "utf8", timeout, maxBuffer: 1024, windowsHide: true,
|
|
58
|
+
});
|
|
59
|
+
if (result.error) {
|
|
60
|
+
const code = result.error.code;
|
|
61
|
+
if (code === "ETIMEDOUT")
|
|
62
|
+
return fail(`timeout after ${timeout}ms (ETIMEDOUT)`);
|
|
63
|
+
const category = code && ["ENOENT", "EACCES", "EPERM", "ENOBUFS"].includes(code) ? code : "unclassified";
|
|
64
|
+
return fail(`spawn failed (${category})`);
|
|
65
|
+
}
|
|
66
|
+
if (result.signal)
|
|
67
|
+
return fail("terminated by signal");
|
|
68
|
+
if (result.status !== 0)
|
|
69
|
+
return fail(Number.isSafeInteger(result.status) ? `exit status ${result.status}` : "missing exit status");
|
|
70
|
+
const token = result.stdout.trim();
|
|
71
|
+
if (!/^[1-9]\d*$/.test(token))
|
|
72
|
+
return fail("invalid birth token output");
|
|
73
|
+
return { kind: "windows-start-filetime", version: 1, birthToken: token };
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return fail("unexpected query failure");
|
|
77
|
+
}
|
|
55
78
|
}
|
|
56
79
|
}
|
|
57
80
|
catch { /* Access failures do not establish process identity. */ }
|
|
@@ -60,6 +83,6 @@ export function queryProcessIdentity(pid) {
|
|
|
60
83
|
export function getCurrentProcessIdentity() {
|
|
61
84
|
ownIdentity ??= queryProcessIdentity(process.pid);
|
|
62
85
|
if (!ownIdentity)
|
|
63
|
-
throw new Error(
|
|
86
|
+
throw new Error(`Cannot establish ADG mutation process identity; restore access to OS process information and retry.${ownWindowsFailure ? ` ${ownWindowsFailure}.` : ""}`);
|
|
64
87
|
return ownIdentity;
|
|
65
88
|
}
|