@waffle-os/runtime 0.0.149 → 0.0.151
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/browser/diagnostic.ts +234 -2
- package/browser/process-broker.ts +42 -4
- package/browser/worker.ts +34 -5
- package/core/open-file.ts +126 -52
- package/dist/browser/diagnostic.js +150 -1
- package/dist/browser/process-broker.js +47 -2
- package/dist/browser/worker.js +24 -6
- package/dist/core/open-file.js +112 -50
- package/dist/fs/client.js +290 -46
- package/dist/fs/protocol.js +7 -4
- package/dist/fs/service.js +77 -16
- package/dist/fs/shared.js +6 -6
- package/dist/interrupt/rewriter-internal.js +2 -1
- package/dist/proc/abi.js +152 -16
- package/dist/proc/command-stdio.js +81 -33
- package/dist/proc/directory.js +111 -0
- package/dist/proc/errors.js +1 -0
- package/dist/proc/supervisor.js +130 -24
- package/dist/proc/table.js +9 -1
- package/dist/proc/types.js +5 -0
- package/dist/proc/worker-client.js +34 -5
- package/dist/runtime.js +3 -0
- package/dist/thread/service.js +85 -10
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +298 -44
- package/fs/protocol.ts +9 -4
- package/fs/service.ts +99 -19
- package/fs/shared.ts +7 -7
- package/interrupt/rewriter-internal.ts +2 -1
- package/package.json +1 -1
- package/proc/abi.ts +185 -17
- package/proc/command-stdio.ts +98 -41
- package/proc/directory.ts +131 -0
- package/proc/errors.ts +1 -0
- package/proc/supervisor.ts +173 -36
- package/proc/table.ts +11 -0
- package/proc/types.ts +6 -0
- package/proc/worker-client.ts +36 -5
- package/runtime.ts +3 -0
- package/thread/service.ts +125 -7
- package/tooling/program/program-transformer.json +1 -1
package/proc/command-stdio.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { atomicsWaitAsync } from "./intrinsics.js";
|
|
2
2
|
import type { DescriptorTable, Resource } from "../core/descriptors.js";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
claimFileOffset,
|
|
5
|
+
withFileOffset,
|
|
6
|
+
shareFileResource,
|
|
7
|
+
type AsyncFileOffsetLease,
|
|
8
|
+
} from "../core/open-file.js";
|
|
9
|
+
import { FilesystemClient, type FilesystemCompletion } from "../fs/client.js";
|
|
5
10
|
import { errno, type FilesystemConnection } from "../fs/protocol.js";
|
|
6
11
|
import { CommandStdioError, type CommandInput, type CommandOutput } from "./command.js";
|
|
7
12
|
|
|
@@ -116,42 +121,101 @@ export class CommandStdio {
|
|
|
116
121
|
}
|
|
117
122
|
}
|
|
118
123
|
|
|
119
|
-
private
|
|
124
|
+
private commitAsyncWrite(
|
|
125
|
+
lease: AsyncFileOffsetLease,
|
|
126
|
+
result: { written: number; offset: number } | number,
|
|
127
|
+
): number {
|
|
128
|
+
if (lease.append) {
|
|
129
|
+
const appended = result as { written: number; offset: number };
|
|
130
|
+
lease.commit(appended.offset);
|
|
131
|
+
return appended.written;
|
|
132
|
+
}
|
|
133
|
+
const written = result as number;
|
|
134
|
+
if (written > 0) lease.commit(lease.position + written);
|
|
135
|
+
return written;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private deferAsyncWrite(
|
|
139
|
+
filesystem: FilesystemClient,
|
|
140
|
+
completion: FilesystemCompletion,
|
|
141
|
+
lease: AsyncFileOffsetLease,
|
|
142
|
+
): void {
|
|
143
|
+
filesystem.trackDeferredWrite(completion, () => {
|
|
144
|
+
try {
|
|
145
|
+
const settled = completion.tryConsumeCommitted();
|
|
146
|
+
if (settled !== undefined) this.commitAsyncWrite(lease, settled);
|
|
147
|
+
} catch {
|
|
148
|
+
// Aborted or failed write: leave offset unchanged before releasing the lease.
|
|
149
|
+
} finally {
|
|
150
|
+
completion.release();
|
|
151
|
+
lease.release();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private async writeAsyncFileLease(
|
|
120
157
|
filesystem: FilesystemClient,
|
|
121
158
|
resource: Extract<Resource, { kind: "file" }>,
|
|
122
|
-
lease:
|
|
159
|
+
lease: AsyncFileOffsetLease,
|
|
123
160
|
bytes: Uint8Array,
|
|
124
|
-
|
|
161
|
+
onDefer: () => void,
|
|
125
162
|
): Promise<number> {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
163
|
+
const completion = lease.append
|
|
164
|
+
? filesystem.submit("append", [resource.handle], bytes)
|
|
165
|
+
: filesystem.submit("write", [resource.handle, lease.position], bytes);
|
|
166
|
+
let handedOff = false;
|
|
167
|
+
try {
|
|
168
|
+
const result = await completion.value();
|
|
169
|
+
return this.commitAsyncWrite(lease, result);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
const committed = completion.tryConsumeCommitted();
|
|
172
|
+
if (committed !== undefined) {
|
|
173
|
+
this.commitAsyncWrite(lease, committed);
|
|
174
|
+
} else {
|
|
175
|
+
handedOff = true;
|
|
176
|
+
onDefer();
|
|
177
|
+
this.deferAsyncWrite(filesystem, completion, lease);
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
} finally {
|
|
181
|
+
if (!handedOff) completion.release();
|
|
135
182
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async runLeasedTransfer(
|
|
186
|
+
filesystem: FilesystemClient,
|
|
187
|
+
resource: Extract<Resource, { kind: "file" }>,
|
|
188
|
+
lease: AsyncFileOffsetLease,
|
|
189
|
+
bytes: Uint8Array,
|
|
190
|
+
writing: boolean,
|
|
191
|
+
): Promise<number> {
|
|
192
|
+
let releaseLease = true;
|
|
193
|
+
const defer = () => {
|
|
194
|
+
releaseLease = false;
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
if (writing) {
|
|
198
|
+
const written = await this.writeAsyncFileLease(filesystem, resource, lease, bytes, defer);
|
|
199
|
+
lease.release();
|
|
200
|
+
releaseLease = false;
|
|
201
|
+
this.check();
|
|
202
|
+
return written;
|
|
203
|
+
}
|
|
204
|
+
const read: Uint8Array = await filesystem.asyncCall("read", [
|
|
205
|
+
resource.handle,
|
|
206
|
+
bytes.length,
|
|
207
|
+
lease.position,
|
|
208
|
+
]);
|
|
209
|
+
bytes.set(read, 0);
|
|
210
|
+
if (read.length > 0) lease.commit(lease.position + read.length);
|
|
143
211
|
this.check();
|
|
144
|
-
return
|
|
212
|
+
return read.length;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (error instanceof CommandStdioError) throw error;
|
|
215
|
+
throw stdioError(-errno(error));
|
|
216
|
+
} finally {
|
|
217
|
+
if (releaseLease) lease.release();
|
|
145
218
|
}
|
|
146
|
-
const read: Uint8Array = await filesystem.asyncCall("read", [
|
|
147
|
-
resource.handle,
|
|
148
|
-
bytes.length,
|
|
149
|
-
lease.position,
|
|
150
|
-
]);
|
|
151
|
-
bytes.set(read, 0);
|
|
152
|
-
if (read.length > 0) lease.commit(lease.position + read.length);
|
|
153
|
-
this.check();
|
|
154
|
-
return read.length;
|
|
155
219
|
}
|
|
156
220
|
|
|
157
221
|
private async transferAsyncFile(
|
|
@@ -165,21 +229,13 @@ export class CommandStdio {
|
|
|
165
229
|
shareFileResource(resource, filesystem.connection);
|
|
166
230
|
for (;;) {
|
|
167
231
|
this.check();
|
|
168
|
-
const claim = claimFileOffset(resource);
|
|
232
|
+
const claim = claimFileOffset(resource, writing);
|
|
169
233
|
if (claim.kind === "failed") throw stdioError(-claim.errno);
|
|
170
234
|
if (claim.kind === "busy") {
|
|
171
235
|
await this.wait([claim.word], [claim.observed]);
|
|
172
236
|
continue;
|
|
173
237
|
}
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
return await this.runAsyncFileLease(filesystem, resource, lease, bytes, writing);
|
|
177
|
-
} catch (error) {
|
|
178
|
-
if (error instanceof CommandStdioError) throw error;
|
|
179
|
-
throw stdioError(-errno(error));
|
|
180
|
-
} finally {
|
|
181
|
-
lease.release();
|
|
182
|
-
}
|
|
238
|
+
return await this.runLeasedTransfer(filesystem, resource, claim.lease, bytes, writing);
|
|
183
239
|
}
|
|
184
240
|
}
|
|
185
241
|
|
|
@@ -250,6 +306,7 @@ export class CommandStdio {
|
|
|
250
306
|
|
|
251
307
|
async settle(): Promise<void> {
|
|
252
308
|
while (this.pending.size) await Promise.allSettled(Array.from(this.pending));
|
|
309
|
+
if (this.filesystem?.hasDeferredWrites()) await this.filesystem.settleDeferredWrites();
|
|
253
310
|
await this.filesystem?.stopResidentPublication();
|
|
254
311
|
this.filesystem?.dispose();
|
|
255
312
|
}
|
package/proc/directory.ts
CHANGED
|
@@ -346,6 +346,137 @@ export function* directoryGroups(
|
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
}
|
|
349
|
+
/** Read-only execution-handle lookup: ignores processes whose job was sealed/collected. */
|
|
350
|
+
export function peekDirectoryProcess(
|
|
351
|
+
directory: SharedArrayBuffer,
|
|
352
|
+
pid: number,
|
|
353
|
+
): ReturnType<typeof directoryState> | undefined {
|
|
354
|
+
const capacity = directoryCapacity(directory);
|
|
355
|
+
const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
|
|
356
|
+
for (let slot = 0; slot < capacity; slot++) {
|
|
357
|
+
const token = atomicsLoad(processes, slot * PROCESS_WORDS);
|
|
358
|
+
if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== pid) continue;
|
|
359
|
+
try {
|
|
360
|
+
const state = directoryState({ directory, slot, pid });
|
|
361
|
+
if (controlLoad(state.jobPending, 0) >= 0) return state;
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (!isCollectedProcess(error)) throw error;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
/** Read-only logical-PID scan: returns live/zombie members whose job is not sealed. */
|
|
369
|
+
export function peekDirectoryProcessesByLogicalPid(
|
|
370
|
+
directory: SharedArrayBuffer,
|
|
371
|
+
processId: number,
|
|
372
|
+
callerSlot?: number,
|
|
373
|
+
callerPid?: number,
|
|
374
|
+
): ReturnType<typeof directoryState>[] {
|
|
375
|
+
const matches: ReturnType<typeof directoryState>[] = [];
|
|
376
|
+
if (!Number.isInteger(processId) || processId <= 0 || processId > 0x7fffffff) return matches;
|
|
377
|
+
const capacity = directoryCapacity(directory);
|
|
378
|
+
const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
|
|
379
|
+
for (let slot = 0; slot < capacity; slot++) {
|
|
380
|
+
const base = slot * PROCESS_WORDS;
|
|
381
|
+
const token = atomicsLoad(processes, base);
|
|
382
|
+
if ((token & 0xffffffffn) !== 1n) continue;
|
|
383
|
+
const pid = Number(token >> 32n);
|
|
384
|
+
if (callerSlot !== undefined && slot === callerSlot && pid !== callerPid) continue;
|
|
385
|
+
if (atomicsLoad(processes, base + PROCESS_ID) !== packedControl(pid, processId)) continue;
|
|
386
|
+
try {
|
|
387
|
+
const state = directoryState({ directory, slot, pid });
|
|
388
|
+
if (controlLoad(state.jobPending, 0) >= 0) arrayAppend(matches, state);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
if (!isCollectedProcess(error)) throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return matches;
|
|
394
|
+
}
|
|
395
|
+
/** Read-only execution-handle probe against an authoritative shared directory. */
|
|
396
|
+
export function probeDirectoryProcess(
|
|
397
|
+
directory: SharedArrayBuffer,
|
|
398
|
+
callerPid: number,
|
|
399
|
+
pid: number,
|
|
400
|
+
): boolean {
|
|
401
|
+
const target = peekDirectoryProcess(directory, pid);
|
|
402
|
+
if (!target) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
403
|
+
try {
|
|
404
|
+
return directoryControls(
|
|
405
|
+
directory,
|
|
406
|
+
() => {
|
|
407
|
+
controlLoad(target.controlParent);
|
|
408
|
+
return target.pid;
|
|
409
|
+
},
|
|
410
|
+
callerPid,
|
|
411
|
+
);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
if (isCollectedProcess(error))
|
|
414
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
415
|
+
throw error;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/** Read-only logical-PID probe against an authoritative shared directory. */
|
|
419
|
+
export function probeDirectoryLogicalPid(
|
|
420
|
+
directory: SharedArrayBuffer,
|
|
421
|
+
callerPid: number,
|
|
422
|
+
processId: number,
|
|
423
|
+
callerSlot?: number,
|
|
424
|
+
): boolean {
|
|
425
|
+
const matches = peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid);
|
|
426
|
+
let found = false;
|
|
427
|
+
for (let index = 0; index < matches.length; index++) {
|
|
428
|
+
const target = matches[index]!;
|
|
429
|
+
try {
|
|
430
|
+
const controlled = directoryControls(
|
|
431
|
+
directory,
|
|
432
|
+
() => {
|
|
433
|
+
controlLoad(target.controlParent);
|
|
434
|
+
return target.pid;
|
|
435
|
+
},
|
|
436
|
+
callerPid,
|
|
437
|
+
);
|
|
438
|
+
found = true;
|
|
439
|
+
if (controlled) return true;
|
|
440
|
+
} catch (error) {
|
|
441
|
+
if (!isCollectedProcess(error)) throw error;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (!found) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
/** Read-only job probe against an authoritative shared directory. */
|
|
448
|
+
export function probeDirectoryJob(
|
|
449
|
+
directory: SharedArrayBuffer,
|
|
450
|
+
callerPid: number,
|
|
451
|
+
job: number,
|
|
452
|
+
): boolean {
|
|
453
|
+
const group = peekDirectoryJob(directory, job);
|
|
454
|
+
if (!group) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
455
|
+
try {
|
|
456
|
+
return directoryControls(directory, () => controlLoad(group.controlParent), callerPid, true);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (isCollectedProcess(error)) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/** Read-only job lookup: never assists sealed collection or wakes the directory. */
|
|
463
|
+
export function peekDirectoryJob(
|
|
464
|
+
directory: SharedArrayBuffer,
|
|
465
|
+
id: number,
|
|
466
|
+
): ReturnType<typeof directoryJob> | undefined {
|
|
467
|
+
for (let slot = 0; slot < directoryCapacity(directory); slot++) {
|
|
468
|
+
const raw = row(directory, slot, true);
|
|
469
|
+
const token = atomicsLoad(raw, 0);
|
|
470
|
+
if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== id) continue;
|
|
471
|
+
try {
|
|
472
|
+
if (controlLoad(cells(raw, id, 4)) < 0) return undefined;
|
|
473
|
+
return directoryJob(directory, slot, id);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (!isCollectedProcess(error)) throw error;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return undefined;
|
|
479
|
+
}
|
|
349
480
|
export function findDirectoryJob(
|
|
350
481
|
directory: SharedArrayBuffer,
|
|
351
482
|
id: number,
|
package/proc/errors.ts
CHANGED
package/proc/supervisor.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { admitProcessObservation, captureNativeReturn } from "./observation.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
directoryState,
|
|
4
|
+
directoryHasProcessSibling,
|
|
5
|
+
peekDirectoryJob,
|
|
6
|
+
peekDirectoryProcess,
|
|
7
|
+
probeDirectoryJob,
|
|
8
|
+
probeDirectoryLogicalPid,
|
|
9
|
+
probeDirectoryProcess,
|
|
10
|
+
} from "./directory.js";
|
|
3
11
|
import { allocateSharedControl } from "../memory/control.ts";
|
|
4
12
|
import {
|
|
5
13
|
isInstance,
|
|
@@ -56,13 +64,14 @@ import {
|
|
|
56
64
|
ProcessErrno,
|
|
57
65
|
ProcessError,
|
|
58
66
|
Signal,
|
|
59
|
-
|
|
67
|
+
checkedKillSignal,
|
|
60
68
|
checkedJobLifetime,
|
|
61
69
|
checkedProcessCwd,
|
|
62
70
|
snapshotSpawnWorker,
|
|
63
71
|
checkedProcessKind,
|
|
64
72
|
snapshotLogicalProcessIdentity,
|
|
65
73
|
type AdmittedSpawnReservation,
|
|
74
|
+
type JobLifetime,
|
|
66
75
|
type ProcessKind,
|
|
67
76
|
type LogicalProcessIdentity,
|
|
68
77
|
type ChildExitEvent,
|
|
@@ -378,10 +387,26 @@ export class ProcessSupervisor {
|
|
|
378
387
|
this.access(pid, id).owner.requests.delete(id);
|
|
379
388
|
return 0;
|
|
380
389
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
this.
|
|
384
|
-
|
|
390
|
+
private requireLiveCaller(callerPid: number): ProcessRecord {
|
|
391
|
+
const caller = this.table.require(callerPid);
|
|
392
|
+
if (this.disposed || caller.result !== null || readOutcome(caller.outcome))
|
|
393
|
+
throw new ProcessError(ProcessErrno.SRCH, "process has exited");
|
|
394
|
+
return caller;
|
|
395
|
+
}
|
|
396
|
+
private callerDirectory(caller: ProcessRecord): SharedArrayBuffer | undefined {
|
|
397
|
+
return isInstance(caller.outcome, OutcomeCell) ? caller.outcome.validity.buffer : undefined;
|
|
398
|
+
}
|
|
399
|
+
private isLiveDirectoryRecord(record: ProcessRecord): boolean {
|
|
400
|
+
return (
|
|
401
|
+
!isInstance(record.outcome, OutcomeCell) ||
|
|
402
|
+
peekDirectoryProcess(record.outcome.validity.buffer, record.pid) !== undefined
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
private isLiveDirectoryJob(jobId: number): boolean {
|
|
406
|
+
const directory = this.table.jobDirectory(jobId);
|
|
407
|
+
return !directory || peekDirectoryJob(directory, jobId) !== undefined;
|
|
408
|
+
}
|
|
409
|
+
private controls(callerPid: number, pid: number): boolean {
|
|
385
410
|
let target: number | undefined = pid;
|
|
386
411
|
while (target !== undefined && target !== 0) {
|
|
387
412
|
if (target === callerPid) return true;
|
|
@@ -389,12 +414,94 @@ export class ProcessSupervisor {
|
|
|
389
414
|
}
|
|
390
415
|
return false;
|
|
391
416
|
}
|
|
417
|
+
/** Guest adapters may signal themselves and descendants, never a sibling or ancestor. */
|
|
418
|
+
canSignal(callerPid: number, pid: number): boolean {
|
|
419
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
420
|
+
const target = this.table.require(pid);
|
|
421
|
+
if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
|
|
422
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
423
|
+
return this.controls(callerPid, pid);
|
|
424
|
+
}
|
|
425
|
+
/** Signal 0 execution-handle probe: authoritative shared directory for directory-backed targets. */
|
|
426
|
+
canProbe(callerPid: number, pid: number): boolean {
|
|
427
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
428
|
+
const target = this.table.get(pid);
|
|
429
|
+
if (target) {
|
|
430
|
+
if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
|
|
431
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
432
|
+
if (isInstance(target.outcome, OutcomeCell)) {
|
|
433
|
+
if (!peekDirectoryProcess(target.outcome.validity.buffer, pid))
|
|
434
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
435
|
+
return (
|
|
436
|
+
this.controls(callerPid, pid) ||
|
|
437
|
+
probeDirectoryProcess(target.outcome.validity.buffer, callerPid, pid)
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
return this.controls(callerPid, pid);
|
|
441
|
+
}
|
|
442
|
+
const directory = this.callerDirectory(caller);
|
|
443
|
+
if (directory) return probeDirectoryProcess(directory, callerPid, pid);
|
|
444
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
445
|
+
}
|
|
446
|
+
/** Probe a logical process PID P across live and uncollected zombie members. */
|
|
447
|
+
canSignalProcess(callerPid: number, processId: number): boolean {
|
|
448
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
449
|
+
if (
|
|
450
|
+
caller.rootPid === undefined ||
|
|
451
|
+
!Number.isInteger(processId) ||
|
|
452
|
+
processId <= 0 ||
|
|
453
|
+
processId > 0x7fffffff
|
|
454
|
+
)
|
|
455
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
456
|
+
let found = false;
|
|
457
|
+
for (const record of this.table.values()) {
|
|
458
|
+
if (
|
|
459
|
+
record.rootPid !== caller.rootPid ||
|
|
460
|
+
record.processIdentity.pid !== processId ||
|
|
461
|
+
!this.isLiveDirectoryRecord(record)
|
|
462
|
+
)
|
|
463
|
+
continue;
|
|
464
|
+
found = true;
|
|
465
|
+
if (this.controls(callerPid, record.pid)) return true;
|
|
466
|
+
}
|
|
467
|
+
const directory = this.callerDirectory(caller);
|
|
468
|
+
if (directory) {
|
|
469
|
+
try {
|
|
470
|
+
if (probeDirectoryLogicalPid(directory, callerPid, processId)) return true;
|
|
471
|
+
found = true;
|
|
472
|
+
} catch (error) {
|
|
473
|
+
if (!(error instanceof ProcessError) || error.errno !== ProcessErrno.SRCH) throw error;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (!found) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
392
479
|
canSignalJob(callerPid: number, jobId: number): boolean {
|
|
393
|
-
this.
|
|
480
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
394
481
|
const job = this.table.job(jobId);
|
|
395
|
-
if (!job
|
|
482
|
+
if (!job || caller.rootPid === undefined || job.rootPid !== caller.rootPid)
|
|
483
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
396
484
|
return job.controlParent === callerPid;
|
|
397
485
|
}
|
|
486
|
+
/** Signal 0 job probe: authoritative shared directory for directory-backed jobs. */
|
|
487
|
+
canProbeJob(callerPid: number, jobId: number): boolean {
|
|
488
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
489
|
+
const job = this.table.job(jobId);
|
|
490
|
+
if (job) {
|
|
491
|
+
if (caller.rootPid === undefined || job.rootPid !== caller.rootPid)
|
|
492
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
493
|
+
const directory = this.table.jobDirectory(jobId);
|
|
494
|
+
if (directory) {
|
|
495
|
+
if (!peekDirectoryJob(directory, jobId))
|
|
496
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
497
|
+
return job.controlParent === callerPid || probeDirectoryJob(directory, callerPid, jobId);
|
|
498
|
+
}
|
|
499
|
+
return job.controlParent === callerPid;
|
|
500
|
+
}
|
|
501
|
+
const directory = this.callerDirectory(caller);
|
|
502
|
+
if (directory) return probeDirectoryJob(directory, callerPid, jobId);
|
|
503
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
504
|
+
}
|
|
398
505
|
/** A job is controlled by its owner and every live or retained ancestor of that owner. */
|
|
399
506
|
canWaitJob(callerPid: number, jobId: number): boolean {
|
|
400
507
|
// A synchronously accepted worker reap can reach the page after its caller was reaped
|
|
@@ -514,11 +621,14 @@ export class ProcessSupervisor {
|
|
|
514
621
|
isInstance(options.outcome, OutcomeCell)
|
|
515
622
|
? {
|
|
516
623
|
ownerPid: pid,
|
|
624
|
+
rootPid: pid,
|
|
517
625
|
leaderPid: pid,
|
|
518
626
|
outcome: options.outcome,
|
|
519
627
|
foreground: options.foreground ?? true,
|
|
520
628
|
}
|
|
521
629
|
: undefined,
|
|
630
|
+
"independent",
|
|
631
|
+
pid,
|
|
522
632
|
);
|
|
523
633
|
this.table.insert(
|
|
524
634
|
{
|
|
@@ -617,32 +727,14 @@ export class ProcessSupervisor {
|
|
|
617
727
|
const processIdentity = this.spawnIdentity(pid, processKind, parent, capturedIdentity);
|
|
618
728
|
this.table.checkPid(pid, admitted ? reservation.outcome : undefined);
|
|
619
729
|
const observation = admitProcessObservation(pid, outcome, reservation.observation);
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
job,
|
|
629
|
-
jobIdentity,
|
|
630
|
-
jobLifetime,
|
|
631
|
-
);
|
|
632
|
-
createdJob = true;
|
|
633
|
-
}
|
|
634
|
-
if (job === undefined) {
|
|
635
|
-
const group = this.table.createJob(
|
|
636
|
-
parentPid,
|
|
637
|
-
selector === 0,
|
|
638
|
-
undefined,
|
|
639
|
-
undefined,
|
|
640
|
-
jobLifetime,
|
|
641
|
-
);
|
|
642
|
-
group.controlParent = reservation.controlParent ?? parentPid;
|
|
643
|
-
job = group.id;
|
|
644
|
-
createdJob = true;
|
|
645
|
-
}
|
|
730
|
+
const { job, createdJob } = this.resolveSpawnJob(
|
|
731
|
+
parentPid,
|
|
732
|
+
parent,
|
|
733
|
+
selector,
|
|
734
|
+
jobLifetime,
|
|
735
|
+
reservation,
|
|
736
|
+
jobIdentity,
|
|
737
|
+
);
|
|
646
738
|
if (jobIdentity) this.table.adoptJobIdentity(job, jobIdentity);
|
|
647
739
|
this.checkJobAdmission(job, admitted);
|
|
648
740
|
const descriptors = new DescriptorTable();
|
|
@@ -709,6 +801,41 @@ export class ProcessSupervisor {
|
|
|
709
801
|
}
|
|
710
802
|
return pid;
|
|
711
803
|
}
|
|
804
|
+
private resolveSpawnJob(
|
|
805
|
+
parentPid: number,
|
|
806
|
+
parent: ProcessRecord | undefined,
|
|
807
|
+
selector: number,
|
|
808
|
+
jobLifetime: JobLifetime,
|
|
809
|
+
reservation: SpawnReservation,
|
|
810
|
+
jobIdentity?: SharedJobIdentity,
|
|
811
|
+
): { job: number; createdJob: boolean } {
|
|
812
|
+
const rootPid = reservation.rootPid ?? parent?.rootPid;
|
|
813
|
+
const job = reservation.job ?? (selector > 0 ? selector : undefined);
|
|
814
|
+
if (job === undefined) {
|
|
815
|
+
const group = this.table.createJob(
|
|
816
|
+
parentPid,
|
|
817
|
+
selector === 0,
|
|
818
|
+
undefined,
|
|
819
|
+
undefined,
|
|
820
|
+
jobLifetime,
|
|
821
|
+
rootPid,
|
|
822
|
+
);
|
|
823
|
+
group.controlParent = reservation.controlParent ?? parentPid;
|
|
824
|
+
return { job: group.id, createdJob: true };
|
|
825
|
+
}
|
|
826
|
+
if (this.table.job(job)) return { job, createdJob: false };
|
|
827
|
+
if (reservation.job === undefined)
|
|
828
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
829
|
+
this.table.createJob(
|
|
830
|
+
jobIdentity?.ownerPid ?? parentPid,
|
|
831
|
+
jobIdentity?.foreground ?? selector === 0,
|
|
832
|
+
job,
|
|
833
|
+
jobIdentity,
|
|
834
|
+
jobLifetime,
|
|
835
|
+
rootPid,
|
|
836
|
+
);
|
|
837
|
+
return { job, createdJob: true };
|
|
838
|
+
}
|
|
712
839
|
private checkSpawnParent(
|
|
713
840
|
parentPid: number,
|
|
714
841
|
parent: ProcessRecord | undefined,
|
|
@@ -1213,8 +1340,13 @@ export class ProcessSupervisor {
|
|
|
1213
1340
|
}
|
|
1214
1341
|
/** Default signals terminate immediately; registered handlers receive shared pending delivery. */
|
|
1215
1342
|
kill(pid: number, signal: number): void {
|
|
1216
|
-
const sig =
|
|
1343
|
+
const sig = checkedKillSignal(signal),
|
|
1217
1344
|
record = this.table.require(pid);
|
|
1345
|
+
if (sig === 0) {
|
|
1346
|
+
if (!this.isLiveDirectoryRecord(record))
|
|
1347
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
1348
|
+
return;
|
|
1349
|
+
}
|
|
1218
1350
|
if (record.result) return;
|
|
1219
1351
|
this.recordSignal(record, sig);
|
|
1220
1352
|
if (record.signal === Signal.SIGKILL || readOutcome(record.outcome))
|
|
@@ -1267,9 +1399,14 @@ export class ProcessSupervisor {
|
|
|
1267
1399
|
this.finish(pid, winner);
|
|
1268
1400
|
}
|
|
1269
1401
|
killJob(jobId: number, signal: number): void {
|
|
1270
|
-
const sig =
|
|
1402
|
+
const sig = checkedKillSignal(signal);
|
|
1271
1403
|
const job = this.table.job(jobId);
|
|
1272
1404
|
if (!job) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
1405
|
+
if (sig === 0) {
|
|
1406
|
+
if (!this.isLiveDirectoryJob(jobId))
|
|
1407
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1273
1410
|
// New background jobs are independent, even when their parent belongs to this job.
|
|
1274
1411
|
const targets = Array.from(job.members, (pid) => this.table.require(pid));
|
|
1275
1412
|
// Record every target before callbacks: a dying process cannot spawn through an as-yet
|
package/proc/table.ts
CHANGED
|
@@ -109,6 +109,7 @@ export interface ProcessReplacement {
|
|
|
109
109
|
export interface ProcessJob {
|
|
110
110
|
readonly id: number;
|
|
111
111
|
readonly ownerPid: number;
|
|
112
|
+
readonly rootPid: number;
|
|
112
113
|
controlParent: number;
|
|
113
114
|
readonly foreground: boolean;
|
|
114
115
|
readonly jobLifetime: JobLifetime;
|
|
@@ -181,6 +182,14 @@ export class ProcessTable {
|
|
|
181
182
|
const outcome = this.jobs.get(id)?.leaderOutcome;
|
|
182
183
|
return isInstance(outcome, OutcomeCell) && !outcome.valid();
|
|
183
184
|
}
|
|
185
|
+
jobDirectory(id: number): SharedArrayBuffer | undefined {
|
|
186
|
+
const job = this.jobs.get(id);
|
|
187
|
+
if (!job) return undefined;
|
|
188
|
+
const outcome =
|
|
189
|
+
job.leaderOutcome ??
|
|
190
|
+
(job.leaderPid === undefined ? undefined : this.records.get(job.leaderPid)?.outcome);
|
|
191
|
+
return isInstance(outcome, OutcomeCell) ? outcome.validity.buffer : undefined;
|
|
192
|
+
}
|
|
184
193
|
values(): IterableIterator<ProcessRecord> {
|
|
185
194
|
return this.records.values();
|
|
186
195
|
}
|
|
@@ -198,6 +207,7 @@ export class ProcessTable {
|
|
|
198
207
|
id = this.identities.job(),
|
|
199
208
|
identity?: SharedJobIdentity,
|
|
200
209
|
jobLifetime: JobLifetime = "independent",
|
|
210
|
+
rootPid?: number,
|
|
201
211
|
): ProcessJob {
|
|
202
212
|
if (isInstance(identity?.outcome, OutcomeCell)) identity.outcome.check();
|
|
203
213
|
const captured = isInstance(identity?.outcome, OutcomeCell);
|
|
@@ -207,6 +217,7 @@ export class ProcessTable {
|
|
|
207
217
|
const job: MutableProcessJob = {
|
|
208
218
|
id,
|
|
209
219
|
ownerPid,
|
|
220
|
+
rootPid: rootPid ?? identity?.rootPid ?? this.records.get(ownerPid)?.rootPid ?? ownerPid,
|
|
210
221
|
controlParent: identity?.controlParent ?? ownerPid,
|
|
211
222
|
foreground,
|
|
212
223
|
jobLifetime: identity?.jobLifetime ?? jobLifetime,
|
package/proc/types.ts
CHANGED
|
@@ -122,6 +122,7 @@ export function snapshotSpawnWorker(worker: unknown): Pick<SpawnRequest, "worker
|
|
|
122
122
|
export interface SharedJobIdentity {
|
|
123
123
|
readonly ownerPid: number;
|
|
124
124
|
readonly controlParent?: number;
|
|
125
|
+
readonly rootPid?: number;
|
|
125
126
|
readonly leaderPid: number;
|
|
126
127
|
readonly outcome: OutcomeWord;
|
|
127
128
|
readonly foreground: boolean;
|
|
@@ -198,3 +199,8 @@ export function checkedSignal(signal: number): ProcessSignal {
|
|
|
198
199
|
}
|
|
199
200
|
return signal;
|
|
200
201
|
}
|
|
202
|
+
/** Signal 0 is valid only for existence/permission probes on kill and killJob, never delivery. */
|
|
203
|
+
export function checkedKillSignal(signal: number): number {
|
|
204
|
+
if (signal === 0) return 0;
|
|
205
|
+
return checkedSignal(signal);
|
|
206
|
+
}
|