openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 3.91 kB
JavaScript
import { n as isNodeRuntime, t as isBunRuntime } from "./runtime-binary-QwVVJ9cA.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { a as isPidDefinitelyDead } from "./pid-alive-D899x-ak.mjs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { ProcSafeError } from "@openclaw/proc-safe/errors";
import { readProcessCommand } from "@openclaw/proc-safe/inspect";
//#region src/process/supervisor/darwin-process-command.ts
let reportedForeignArguments = false;
const REDACTED_ARGUMENTS_ENTITLEMENT = /\[Key\] com\.apple\.private\.no-read-procargs\s*\[Value\]\s*\[Bool\] true|<key>com\.apple\.private\.no-read-procargs<\/key>\s*<true\/>/u;
/** The running image: the first text mapping, confirmed by the kernel accounting
* name (exec basename, truncated to 16 bytes), so a mapped binary cannot pose as it. */
function readDarwinImagePath(e) {
const t = {
encoding: "utf8",
timeout: 2e3
};
const n = spawnSync("/usr/sbin/lsof", [
"-a",
"-p",
String(e),
"-d",
"txt",
"-Fn"
], t);
const r = n.status === 0 ? /^n(\/.+)$/mu.exec(n.stdout)?.[1] : void 0;
const i = r && spawnSync("/bin/ps", [
"-o",
"ucomm=",
"-p",
String(e)
], t);
return i && i.status === 0 && i.stdout.trim() === path.basename(r).slice(0, 16) ? r : void 0;
}
/** A sealed Apple binary that opts out of argument inspection, such as mdworker. */
function isRedactedSystemImage(e) {
if (!e.startsWith("/System/")) return false;
const t = spawnSync("/usr/bin/codesign", [
"-d",
"--entitlements",
"-",
e
], {
encoding: "utf8",
timeout: 2e3
});
return t.status === 0 && REDACTED_ARGUMENTS_ENTITLEMENT.test(t.stdout);
}
function readCommandWithServiceMarker(e) {
try {
return readProcessCommand(e, { environmentKeys: ["OPENCLAW_SERVICE_MARKER"] });
} catch (t) {
if (!(t instanceof ProcSafeError) || t.code !== "incomplete") throw t;
const n = readProcessCommand(e);
return n && [n.executable, n.argv[0] ?? ""].some((e) => isNodeRuntime(e) || isBunRuntime(e) || path.basename(e) === "tsx") ? {
...n,
serviceMarkerUnavailable: true
} : n;
}
}
/** Exact argv, or observed foreign ownership when Darwin denies argument inspection.
* Callers that never consult the service marker skip the environment: some macOS
* argument images end without the separator that bounds it. */
function readDarwinProcessCommand(e, t, n = {}) {
try {
const t = n.serviceMarker === false ? readProcessCommand(e) : readCommandWithServiceMarker(e);
if (!t) return;
const r = t.environment.OPENCLAW_SERVICE_MARKER;
return {
argv: [...t.argv],
executable: t.executable,
...r === void 0 ? {} : { serviceMarker: r },
..."serviceMarkerUnavailable" in t ? { serviceMarkerUnavailable: true } : {}
};
} catch (n) {
if (!(n instanceof ProcSafeError)) throw n;
if (n.code === "layout-mismatch" || n.code === "incomplete") {
const r = readDarwinImagePath(e);
if (r && isRedactedSystemImage(r)) return {
argvUnavailable: true,
executable: r,
...t ? { owner: t } : {}
};
throw n;
}
if (n.code === "unsupported-platform" && process.platform === "darwin") throw new Error("Cannot inspect Darwin process arguments under Rosetta; run OpenClaw with native arm64 Node.js.", { cause: n });
if (n.code !== "access-denied" && n.code !== "operation-failed") throw n;
if (isPidDefinitelyDead(e)) return;
if (t === "different") {
if (!reportedForeignArguments) {
reportedForeignArguments = true;
createSubsystemLogger("process/census").debug("Unreadable Darwin arguments for another UID do not establish capture custody.", {
pid: e,
code: n.code
});
}
return {
argvUnavailable: true,
owner: t
};
}
throw new Error(`Could not classify PID ${e}: cannot inspect Darwin arguments (${n.code}).`, { cause: n });
}
}
//#endregion
export { readDarwinProcessCommand as t };