@feigi/fleet-ctl 3.71.1 → 3.71.3
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/package.json +1 -1
- package/scripts/proc.mjs +65 -18
package/package.json
CHANGED
package/scripts/proc.mjs
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { readFileSync } from "node:fs";
|
|
6
6
|
import { spawnSync } from "node:child_process";
|
|
7
|
-
import { basename } from "node:path";
|
|
7
|
+
import { basename, join } from "node:path";
|
|
8
|
+
import { isDigits } from "./arg.mjs";
|
|
8
9
|
|
|
9
10
|
// ESRCH alone is dead. EPERM is a live process we may not signal, and any
|
|
10
11
|
// other error is not evidence of death either.
|
|
@@ -17,23 +18,29 @@ export function isDead(pid) {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
// Thrown when the process tree or a start
|
|
21
|
-
// would act on a verdict refuses instead: a controller nobody could judge
|
|
22
|
-
// never judged dead.
|
|
21
|
+
// Thrown when the process tree or a start identity cannot be read. A caller
|
|
22
|
+
// that would act on a verdict refuses instead: a controller nobody could judge
|
|
23
|
+
// is never judged dead.
|
|
23
24
|
export class ProcUnreadable extends Error {}
|
|
24
25
|
|
|
25
26
|
// The two reads the controller record needs — a process's parent and argv,
|
|
26
|
-
// and its start
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
27
|
+
// and its start identity — behind one source. The parent comes from `ps` on
|
|
28
|
+
// every platform. The start identity comes from procfs on Linux and from `ps`
|
|
29
|
+
// everywhere else, chosen by platform rather than by whether /proc can be
|
|
30
|
+
// read: the run that writes a record and the run that judges it must read it
|
|
31
|
+
// the same way, and a key read one way never equals one read the other way.
|
|
32
|
+
// A FLEET_PROC_TABLE file replaces both whole: a test suite run from inside a
|
|
33
|
+
// live omp session would otherwise find that session's omp as its ancestor.
|
|
34
|
+
// The file is a JSON object keyed by pid:
|
|
30
35
|
//
|
|
31
36
|
// { "<pid>": { "ppid": <pid>, "argv": ["bun", "/…/omp"], "lstart": "<start>" } }
|
|
32
37
|
//
|
|
33
|
-
// A pid the table does not name has no parent and no start
|
|
34
|
-
//
|
|
38
|
+
// A pid the table does not name has no parent and no start identity, as a
|
|
39
|
+
// pid that is gone.
|
|
35
40
|
export function processSource(env = process.env) {
|
|
36
|
-
|
|
41
|
+
if (env.FLEET_PROC_TABLE) return tableSource(env.FLEET_PROC_TABLE);
|
|
42
|
+
const ps = psSource(env);
|
|
43
|
+
return process.platform === "linux" ? { ...ps, startTime: (pid) => procfsStartTime(pid) } : ps;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
function tableSource(path) {
|
|
@@ -69,10 +76,13 @@ function tableProblem(table) {
|
|
|
69
76
|
return null;
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
// The `ps` source: the process tree, and a start identity that is the
|
|
80
|
+
// process's start time as `ps -o lstart=` prints it. On macOS that time is
|
|
81
|
+
// written once, when the process forks, so it never changes for a live
|
|
82
|
+
// process. It is printed in the caller's zone and locale, and a later run —
|
|
83
|
+
// with its own TZ — compares it as an opaque string. Pinning both makes the
|
|
84
|
+
// string a property of the process, not of whoever asked.
|
|
85
|
+
export function psSource(env) {
|
|
76
86
|
const psEnv = { ...env, TZ: "UTC", LC_ALL: "C" };
|
|
77
87
|
const ps = (args) => {
|
|
78
88
|
const r = spawnSync("ps", args, { encoding: "utf8", env: psEnv });
|
|
@@ -103,6 +113,40 @@ function psSource(env) {
|
|
|
103
113
|
};
|
|
104
114
|
}
|
|
105
115
|
|
|
116
|
+
// The Linux start identity, `<boot id> <start ticks>`, read from the procfs
|
|
117
|
+
// mounted at `root`. The ticks are field 22 of `<pid>/stat`, the process's
|
|
118
|
+
// start time in clock ticks since boot, which never changes for a live
|
|
119
|
+
// process; `ps -o lstart=` adds them to a boot time that moves with every
|
|
120
|
+
// step of the wall clock. Within a boot a reused pid starts later, and the
|
|
121
|
+
// boot id tells two boots' processes apart. Only a missing stat file is a gone
|
|
122
|
+
// process; any other failure, and any failure to read the boot id, is
|
|
123
|
+
// unreadable — never a reason to ask `ps` instead.
|
|
124
|
+
export function procfsStartTime(pid, root = "/proc") {
|
|
125
|
+
const unreadable = (why) => new ProcUnreadable(`cannot read the start identity of pid ${pid}: ${why}`);
|
|
126
|
+
const bootIdPath = join(root, "sys", "kernel", "random", "boot_id");
|
|
127
|
+
let bootId;
|
|
128
|
+
try {
|
|
129
|
+
bootId = readFileSync(bootIdPath, "utf8").trim();
|
|
130
|
+
} catch (e) {
|
|
131
|
+
throw unreadable(e.message);
|
|
132
|
+
}
|
|
133
|
+
if (bootId === "") throw unreadable(`${bootIdPath} is empty`);
|
|
134
|
+
const statPath = join(root, String(pid), "stat");
|
|
135
|
+
let stat;
|
|
136
|
+
try {
|
|
137
|
+
stat = readFileSync(statPath, "utf8");
|
|
138
|
+
} catch (e) {
|
|
139
|
+
if (e.code === "ENOENT") return null;
|
|
140
|
+
throw unreadable(e.message);
|
|
141
|
+
}
|
|
142
|
+
// Field 2, comm, is wrapped in parentheses and may itself hold spaces and
|
|
143
|
+
// `)`, so the fields are counted from the last `)`: field 3 comes first.
|
|
144
|
+
const close = stat.lastIndexOf(")");
|
|
145
|
+
const ticks = close === -1 ? undefined : stat.slice(close + 1).trim().split(/\s+/)[22 - 3];
|
|
146
|
+
if (!isDigits(ticks)) throw unreadable(`${statPath} has no start time in field 22`);
|
|
147
|
+
return `${bootId} ${ticks}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
106
150
|
// `args` is argv joined by single spaces, so an argument that itself holds a
|
|
107
151
|
// space splits here; the omp match only reads argv[0] and argv[1], and a path
|
|
108
152
|
// with a space in it misses the match rather than mismatching.
|
|
@@ -147,11 +191,14 @@ export function controllerOf(source, chain) {
|
|
|
147
191
|
|
|
148
192
|
// What a recorded controller is now, judged against the caller's chain:
|
|
149
193
|
// none no record
|
|
150
|
-
// dead isDead() says so, or the pid's start
|
|
151
|
-
// one — a reused pid, which the record must outlast for
|
|
152
|
-
// where the ledger lock tolerates it for seconds
|
|
194
|
+
// dead isDead() says so, or the pid's start identity is not the
|
|
195
|
+
// recorded one — a reused pid, which the record must outlast for
|
|
196
|
+
// days where the ledger lock tolerates it for seconds
|
|
153
197
|
// ancestor the record names one of the caller's own ancestors
|
|
154
198
|
// alive any other live controller
|
|
199
|
+
// `lstart` is a start identity, not a printed time: it never changes for a
|
|
200
|
+
// live process and differs for a reused pid — on Linux both within a boot and
|
|
201
|
+
// across boots — so an exact comparison is the whole test.
|
|
155
202
|
export function judgeController(record, source, chain) {
|
|
156
203
|
if (!record) return "none";
|
|
157
204
|
if (isDead(record.pid)) return "dead";
|