@waffle-os/runtime 0.0.150 → 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.
@@ -84,6 +84,8 @@ import {
84
84
  directoryProcesses,
85
85
  directoryAdmissionVersion,
86
86
  commitDirectoryAdmission,
87
+ peekDirectoryJob,
88
+ peekDirectoryProcess,
87
89
  isCollectedProcess,
88
90
  wakeDirectory,
89
91
  } from "../proc/directory.js";
@@ -99,13 +101,14 @@ import { type FilesystemConnection, type FilesystemIdentity, isRevoked } from ".
99
101
  import type { ProcessSupervisor, RootProcessOptions } from "../proc/supervisor.js";
100
102
  import type { CommandPrograms, ExecutableRegistry, ResolvedExecutable } from "../proc/registry.js";
101
103
  import type { ProcessIdentityAllocator } from "../proc/identity.js";
102
- import type { ProcessRecord, ProcessTable } from "../proc/table.js";
104
+ import type { ProcessJob, ProcessRecord, ProcessTable } from "../proc/table.js";
103
105
  import {
104
106
  type ChildExitEvent,
105
107
  type ProcessContext,
106
108
  type ProcessExit,
107
109
  type CommandProcessContext,
108
110
  type ProcessHandle,
111
+ checkedKillSignal,
109
112
  checkedSignal,
110
113
  checkedProcessCwd,
111
114
  snapshotLogicalProcessIdentity,
@@ -115,6 +118,7 @@ import {
115
118
  } from "../proc/types.js";
116
119
  import { requestSignal, createSignalState, type SignalTransport } from "../proc/signals.js";
117
120
  import {
121
+ OutcomeCell,
118
122
  claimOutcome,
119
123
  outcomeWord,
120
124
  readOutcome,
@@ -3569,8 +3573,38 @@ export class BrowserProcessBroker {
3569
3573
  Array.from(directoryProcesses(root.directory)),
3570
3574
  );
3571
3575
  }
3576
+ private isDirectoryBackedJob(entry: ProcessJob): boolean {
3577
+ if (entry.shared) return true;
3578
+ const leader =
3579
+ entry.leaderPid === undefined ? undefined : this.options.table.get(entry.leaderPid);
3580
+ return leader !== undefined && isInstance(leader.outcome, OutcomeCell);
3581
+ }
3582
+ private hasProcess(pid: number): boolean {
3583
+ for (const root of this.roots.values())
3584
+ if (peekDirectoryProcess(root.directory, pid)) return true;
3585
+ const record = this.options.table.get(pid);
3586
+ if (record && !isInstance(record.outcome, OutcomeCell)) return true;
3587
+ const reserved = this.reservedRoots.get(pid);
3588
+ return reserved !== undefined && !isInstance(reserved.outcome, OutcomeCell);
3589
+ }
3590
+ private hasJob(
3591
+ job: number,
3592
+ reserved: readonly { pid: number; signals: SignalTransport }[],
3593
+ ): boolean {
3594
+ if (reserved.length > 0) return true;
3595
+ for (const root of this.roots.values()) if (peekDirectoryJob(root.directory, job)) return true;
3596
+ const entry = this.options.table.job(job);
3597
+ if (entry && !this.isDirectoryBackedJob(entry)) return true;
3598
+ for (const root of this.reservedRoots.values())
3599
+ if (root.job === job && !isInstance(root.outcome, OutcomeCell)) return true;
3600
+ return false;
3601
+ }
3572
3602
  async kill(pid: number, signal: number): Promise<void> {
3573
- const sig = checkedSignal(signal);
3603
+ const sig = checkedKillSignal(signal);
3604
+ if (sig === 0) {
3605
+ if (this.suspended || this.hasProcess(pid)) return;
3606
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
3607
+ }
3574
3608
  const found = this.publishProcessSignal(pid, sig);
3575
3609
  const supervisor = await this.ready();
3576
3610
  if (this.suspended) return;
@@ -3624,8 +3658,12 @@ export class BrowserProcessBroker {
3624
3658
  signal: number,
3625
3659
  reserved: readonly { pid: number; signals: SignalTransport }[] = [],
3626
3660
  ): Promise<void> {
3627
- const sig = checkedSignal(signal),
3628
- states = this.states().filter((state) => state.job === job);
3661
+ const sig = checkedKillSignal(signal);
3662
+ if (sig === 0) {
3663
+ if (this.suspended || this.hasJob(job, reserved)) return;
3664
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
3665
+ }
3666
+ const states = this.states().filter((state) => state.job === job);
3629
3667
  const seen = new Set(states.map((state) => state.pid));
3630
3668
  const records = Array.from(this.options.table.values()).filter(
3631
3669
  (record) => record.job === job && !seen.has(record.pid),
@@ -60,6 +60,8 @@ import {
60
60
  directoryProcesses,
61
61
  directoryAdmissionVersion,
62
62
  commitDirectoryAdmission,
63
+ peekDirectoryJob,
64
+ peekDirectoryProcess,
63
65
  isCollectedProcess,
64
66
  wakeDirectory
65
67
  } from "../proc/directory.js";
@@ -71,6 +73,7 @@ import {
71
73
  } from "../proc/control.js";
72
74
  import { isRevoked } from "../fs/protocol.js";
73
75
  import {
76
+ checkedKillSignal,
74
77
  checkedSignal,
75
78
  checkedProcessCwd,
76
79
  snapshotLogicalProcessIdentity,
@@ -79,6 +82,7 @@ import {
79
82
  } from "../proc/types.js";
80
83
  import { requestSignal, createSignalState } from "../proc/signals.js";
81
84
  import {
85
+ OutcomeCell,
82
86
  claimOutcome,
83
87
  outcomeWord,
84
88
  readOutcome,
@@ -2789,8 +2793,43 @@ export class BrowserProcessBroker {
2789
2793
  states() {
2790
2794
  return Array.from(this.roots.values()).flatMap((root) => Array.from(directoryProcesses(root.directory)));
2791
2795
  }
2796
+ isDirectoryBackedJob(entry) {
2797
+ if (entry.shared)
2798
+ return true;
2799
+ const leader = entry.leaderPid === undefined ? undefined : this.options.table.get(entry.leaderPid);
2800
+ return leader !== undefined && isInstance(leader.outcome, OutcomeCell);
2801
+ }
2802
+ hasProcess(pid) {
2803
+ for (const root of this.roots.values())
2804
+ if (peekDirectoryProcess(root.directory, pid))
2805
+ return true;
2806
+ const record = this.options.table.get(pid);
2807
+ if (record && !isInstance(record.outcome, OutcomeCell))
2808
+ return true;
2809
+ const reserved = this.reservedRoots.get(pid);
2810
+ return reserved !== undefined && !isInstance(reserved.outcome, OutcomeCell);
2811
+ }
2812
+ hasJob(job, reserved) {
2813
+ if (reserved.length > 0)
2814
+ return true;
2815
+ for (const root of this.roots.values())
2816
+ if (peekDirectoryJob(root.directory, job))
2817
+ return true;
2818
+ const entry = this.options.table.job(job);
2819
+ if (entry && !this.isDirectoryBackedJob(entry))
2820
+ return true;
2821
+ for (const root of this.reservedRoots.values())
2822
+ if (root.job === job && !isInstance(root.outcome, OutcomeCell))
2823
+ return true;
2824
+ return false;
2825
+ }
2792
2826
  async kill(pid, signal) {
2793
- const sig = checkedSignal(signal);
2827
+ const sig = checkedKillSignal(signal);
2828
+ if (sig === 0) {
2829
+ if (this.suspended || this.hasProcess(pid))
2830
+ return;
2831
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
2832
+ }
2794
2833
  const found = this.publishProcessSignal(pid, sig);
2795
2834
  const supervisor = await this.ready();
2796
2835
  if (this.suspended)
@@ -2833,7 +2872,13 @@ export class BrowserProcessBroker {
2833
2872
  }
2834
2873
  }
2835
2874
  async killJob(job, signal, reserved = []) {
2836
- const sig = checkedSignal(signal), states = this.states().filter((state) => state.job === job);
2875
+ const sig = checkedKillSignal(signal);
2876
+ if (sig === 0) {
2877
+ if (this.suspended || this.hasJob(job, reserved))
2878
+ return;
2879
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
2880
+ }
2881
+ const states = this.states().filter((state) => state.job === job);
2837
2882
  const seen = new Set(states.map((state) => state.pid));
2838
2883
  const records = Array.from(this.options.table.values()).filter((record) => record.job === job && !seen.has(record.pid));
2839
2884
  for (const state of states)
@@ -1,4 +1,4 @@
1
- export const INTERRUPT_REWRITER_REVISION = 7;
1
+ export const INTERRUPT_REWRITER_REVISION = 8;
2
2
  const encoder = new TextEncoder;
3
3
  const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
4
4
  const HEADER = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]);
@@ -27,6 +27,7 @@ const runtimeProcSignatures = new Map([
27
27
  ["wait_job", { params: [127, 127, 127], results: [127] }],
28
28
  ["kill", { params: [127, 127], results: [127] }],
29
29
  ["kill_job", { params: [127, 127], results: [127] }],
30
+ ["probe_process", { params: [127], results: [127] }],
30
31
  ["start_status", { params: [127], results: [127] }],
31
32
  ["poll", { params: [127, 127, 127, 127], results: [127] }],
32
33
  ["interrupted", { params: [], results: [127] }],
package/dist/proc/abi.js CHANGED
@@ -53,6 +53,7 @@ import {
53
53
  ProcessError,
54
54
  Signal,
55
55
  checkedJobLifetime,
56
+ checkedKillSignal,
56
57
  checkedProcessCwd,
57
58
  checkedProcessKind,
58
59
  snapshotLogicalProcessIdentity
@@ -146,17 +147,30 @@ export class ProcessABI {
146
147
  wait: (pid, ptr) => this.result(() => this.wait(pid, ptr)),
147
148
  wait_job: (job, flags, ptr) => this.result(() => this.waitJob(job, flags, ptr)),
148
149
  kill: (pid, signal) => this.result(() => {
149
- if (options.service.canSignal?.(options.pid, pid) === false)
150
- return -63;
151
- options.service.kill(pid, signal);
150
+ const sig = checkedKillSignal(signal);
151
+ const allowed = sig === 0 && options.service.canProbe ? options.service.canProbe(options.pid, pid) : options.service.canSignal?.(options.pid, pid);
152
+ if (allowed === false)
153
+ return -ProcessErrno.PERM;
154
+ if (sig === 0 && allowed === true)
155
+ return 0;
156
+ options.service.kill(pid, sig);
152
157
  return 0;
153
158
  }),
154
159
  kill_job: (job, signal) => this.result(() => {
155
- if (options.service.canSignalJob?.(options.pid, job) === false)
156
- return -63;
157
- options.service.killJob(job, signal);
160
+ const sig = checkedKillSignal(signal);
161
+ const allowed = sig === 0 && options.service.canProbeJob ? options.service.canProbeJob(options.pid, job) : options.service.canSignalJob?.(options.pid, job);
162
+ if (allowed === false)
163
+ return -ProcessErrno.PERM;
164
+ if (sig === 0 && allowed === true)
165
+ return 0;
166
+ options.service.killJob(job, sig);
158
167
  return 0;
159
168
  }),
169
+ probe_process: (pid) => this.result(() => {
170
+ if (!options.service.canSignalProcess)
171
+ return -NOSYS;
172
+ return options.service.canSignalProcess(options.pid, pid) ? 0 : -ProcessErrno.PERM;
173
+ }),
160
174
  start_status: (pid) => this.result(() => options.service.startStatus(options.pid, pid)),
161
175
  poll: (ptr, count, timeout, out) => this.result(() => this.poll(ptr, count, timeout, out)),
162
176
  interrupted: () => this.interrupted(),
@@ -290,6 +290,117 @@ export function* directoryGroups(directory) {
290
290
  }
291
291
  }
292
292
  }
293
+ export function peekDirectoryProcess(directory, pid) {
294
+ const capacity = directoryCapacity(directory);
295
+ const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
296
+ for (let slot = 0;slot < capacity; slot++) {
297
+ const token = atomicsLoad(processes, slot * PROCESS_WORDS);
298
+ if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== pid)
299
+ continue;
300
+ try {
301
+ const state = directoryState({ directory, slot, pid });
302
+ if (controlLoad(state.jobPending, 0) >= 0)
303
+ return state;
304
+ } catch (error) {
305
+ if (!isCollectedProcess(error))
306
+ throw error;
307
+ }
308
+ }
309
+ return;
310
+ }
311
+ export function peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid) {
312
+ const matches = [];
313
+ if (!Number.isInteger(processId) || processId <= 0 || processId > 2147483647)
314
+ return matches;
315
+ const capacity = directoryCapacity(directory);
316
+ const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
317
+ for (let slot = 0;slot < capacity; slot++) {
318
+ const base = slot * PROCESS_WORDS;
319
+ const token = atomicsLoad(processes, base);
320
+ if ((token & 0xffffffffn) !== 1n)
321
+ continue;
322
+ const pid = Number(token >> 32n);
323
+ if (callerSlot !== undefined && slot === callerSlot && pid !== callerPid)
324
+ continue;
325
+ if (atomicsLoad(processes, base + PROCESS_ID) !== packedControl(pid, processId))
326
+ continue;
327
+ try {
328
+ const state = directoryState({ directory, slot, pid });
329
+ if (controlLoad(state.jobPending, 0) >= 0)
330
+ arrayAppend(matches, state);
331
+ } catch (error) {
332
+ if (!isCollectedProcess(error))
333
+ throw error;
334
+ }
335
+ }
336
+ return matches;
337
+ }
338
+ export function probeDirectoryProcess(directory, callerPid, pid) {
339
+ const target = peekDirectoryProcess(directory, pid);
340
+ if (!target)
341
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
342
+ try {
343
+ return directoryControls(directory, () => {
344
+ controlLoad(target.controlParent);
345
+ return target.pid;
346
+ }, callerPid);
347
+ } catch (error) {
348
+ if (isCollectedProcess(error))
349
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
350
+ throw error;
351
+ }
352
+ }
353
+ export function probeDirectoryLogicalPid(directory, callerPid, processId, callerSlot) {
354
+ const matches = peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid);
355
+ let found = false;
356
+ for (let index = 0;index < matches.length; index++) {
357
+ const target = matches[index];
358
+ try {
359
+ const controlled = directoryControls(directory, () => {
360
+ controlLoad(target.controlParent);
361
+ return target.pid;
362
+ }, callerPid);
363
+ found = true;
364
+ if (controlled)
365
+ return true;
366
+ } catch (error) {
367
+ if (!isCollectedProcess(error))
368
+ throw error;
369
+ }
370
+ }
371
+ if (!found)
372
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
373
+ return false;
374
+ }
375
+ export function probeDirectoryJob(directory, callerPid, job) {
376
+ const group = peekDirectoryJob(directory, job);
377
+ if (!group)
378
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
379
+ try {
380
+ return directoryControls(directory, () => controlLoad(group.controlParent), callerPid, true);
381
+ } catch (error) {
382
+ if (isCollectedProcess(error))
383
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
384
+ throw error;
385
+ }
386
+ }
387
+ export function peekDirectoryJob(directory, id) {
388
+ for (let slot = 0;slot < directoryCapacity(directory); slot++) {
389
+ const raw = row(directory, slot, true);
390
+ const token = atomicsLoad(raw, 0);
391
+ if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== id)
392
+ continue;
393
+ try {
394
+ if (controlLoad(cells(raw, id, 4)) < 0)
395
+ return;
396
+ return directoryJob(directory, slot, id);
397
+ } catch (error) {
398
+ if (!isCollectedProcess(error))
399
+ throw error;
400
+ }
401
+ }
402
+ return;
403
+ }
293
404
  export function findDirectoryJob(directory, id) {
294
405
  for (let slot = 0;slot < directoryCapacity(directory); slot++) {
295
406
  const raw = row(directory, slot, true), token = atomicsLoad(raw, 0), current = Number(token >> 32n);
@@ -16,5 +16,6 @@ export const ProcessErrno = objectFreeze({
16
16
  INVAL: 28,
17
17
  NOMEM: 48,
18
18
  NOTSUP: 58,
19
+ PERM: 63,
19
20
  SRCH: 71
20
21
  });
@@ -1,5 +1,13 @@
1
1
  import { admitProcessObservation, captureNativeReturn } from "./observation.js";
2
- import { directoryState, directoryHasProcessSibling } from "./directory.js";
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.js";
4
12
  import {
5
13
  isInstance,
@@ -51,7 +59,7 @@ import {
51
59
  ProcessErrno,
52
60
  ProcessError,
53
61
  Signal,
54
- checkedSignal,
62
+ checkedKillSignal,
55
63
  checkedJobLifetime,
56
64
  checkedProcessCwd,
57
65
  snapshotSpawnWorker,
@@ -281,9 +289,23 @@ export class ProcessSupervisor {
281
289
  this.access(pid, id).owner.requests.delete(id);
282
290
  return 0;
283
291
  }
284
- canSignal(callerPid, pid) {
285
- this.table.require(callerPid);
286
- this.table.require(pid);
292
+ requireLiveCaller(callerPid) {
293
+ const caller = this.table.require(callerPid);
294
+ if (this.disposed || caller.result !== null || readOutcome(caller.outcome))
295
+ throw new ProcessError(ProcessErrno.SRCH, "process has exited");
296
+ return caller;
297
+ }
298
+ callerDirectory(caller) {
299
+ return isInstance(caller.outcome, OutcomeCell) ? caller.outcome.validity.buffer : undefined;
300
+ }
301
+ isLiveDirectoryRecord(record) {
302
+ return !isInstance(record.outcome, OutcomeCell) || peekDirectoryProcess(record.outcome.validity.buffer, record.pid) !== undefined;
303
+ }
304
+ isLiveDirectoryJob(jobId) {
305
+ const directory = this.table.jobDirectory(jobId);
306
+ return !directory || peekDirectoryJob(directory, jobId) !== undefined;
307
+ }
308
+ controls(callerPid, pid) {
287
309
  let target = pid;
288
310
  while (target !== undefined && target !== 0) {
289
311
  if (target === callerPid)
@@ -292,13 +314,84 @@ export class ProcessSupervisor {
292
314
  }
293
315
  return false;
294
316
  }
317
+ canSignal(callerPid, pid) {
318
+ const caller = this.requireLiveCaller(callerPid);
319
+ const target = this.table.require(pid);
320
+ if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
321
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
322
+ return this.controls(callerPid, pid);
323
+ }
324
+ canProbe(callerPid, pid) {
325
+ const caller = this.requireLiveCaller(callerPid);
326
+ const target = this.table.get(pid);
327
+ if (target) {
328
+ if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
329
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
330
+ if (isInstance(target.outcome, OutcomeCell)) {
331
+ if (!peekDirectoryProcess(target.outcome.validity.buffer, pid))
332
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
333
+ return this.controls(callerPid, pid) || probeDirectoryProcess(target.outcome.validity.buffer, callerPid, pid);
334
+ }
335
+ return this.controls(callerPid, pid);
336
+ }
337
+ const directory = this.callerDirectory(caller);
338
+ if (directory)
339
+ return probeDirectoryProcess(directory, callerPid, pid);
340
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
341
+ }
342
+ canSignalProcess(callerPid, processId) {
343
+ const caller = this.requireLiveCaller(callerPid);
344
+ if (caller.rootPid === undefined || !Number.isInteger(processId) || processId <= 0 || processId > 2147483647)
345
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
346
+ let found = false;
347
+ for (const record of this.table.values()) {
348
+ if (record.rootPid !== caller.rootPid || record.processIdentity.pid !== processId || !this.isLiveDirectoryRecord(record))
349
+ continue;
350
+ found = true;
351
+ if (this.controls(callerPid, record.pid))
352
+ return true;
353
+ }
354
+ const directory = this.callerDirectory(caller);
355
+ if (directory) {
356
+ try {
357
+ if (probeDirectoryLogicalPid(directory, callerPid, processId))
358
+ return true;
359
+ found = true;
360
+ } catch (error) {
361
+ if (!(error instanceof ProcessError) || error.errno !== ProcessErrno.SRCH)
362
+ throw error;
363
+ }
364
+ }
365
+ if (!found)
366
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
367
+ return false;
368
+ }
295
369
  canSignalJob(callerPid, jobId) {
296
- this.table.require(callerPid);
370
+ const caller = this.requireLiveCaller(callerPid);
297
371
  const job = this.table.job(jobId);
298
- if (!job)
372
+ if (!job || caller.rootPid === undefined || job.rootPid !== caller.rootPid)
299
373
  throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
300
374
  return job.controlParent === callerPid;
301
375
  }
376
+ canProbeJob(callerPid, jobId) {
377
+ const caller = this.requireLiveCaller(callerPid);
378
+ const job = this.table.job(jobId);
379
+ if (job) {
380
+ if (caller.rootPid === undefined || job.rootPid !== caller.rootPid)
381
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
382
+ const directory = this.table.jobDirectory(jobId);
383
+ if (directory) {
384
+ if (!peekDirectoryJob(directory, jobId))
385
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
386
+ return job.controlParent === callerPid || probeDirectoryJob(directory, callerPid, jobId);
387
+ }
388
+ return job.controlParent === callerPid;
389
+ }
390
+ const directory = this.callerDirectory(caller);
391
+ if (directory)
392
+ return probeDirectoryJob(directory, callerPid, jobId);
393
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
394
+ }
302
395
  canWaitJob(callerPid, jobId) {
303
396
  if (this.table.parentOf(callerPid) === undefined)
304
397
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${callerPid}`);
@@ -387,10 +480,11 @@ export class ProcessSupervisor {
387
480
  const observation = admitProcessObservation(pid, outcome, options.observation);
388
481
  const job = options.job !== undefined && this.table.job(options.job) ? this.table.job(options.job) : this.table.createJob(pid, options.foreground ?? true, options.job, isInstance(options.outcome, OutcomeCell) ? {
389
482
  ownerPid: pid,
483
+ rootPid: pid,
390
484
  leaderPid: pid,
391
485
  outcome: options.outcome,
392
486
  foreground: options.foreground ?? true
393
- } : undefined);
487
+ } : undefined, "independent", pid);
394
488
  this.table.insert({
395
489
  pid,
396
490
  parentPid: 0,
@@ -454,20 +548,7 @@ export class ProcessSupervisor {
454
548
  const processIdentity = this.spawnIdentity(pid, processKind, parent, capturedIdentity);
455
549
  this.table.checkPid(pid, admitted ? reservation.outcome : undefined);
456
550
  const observation = admitProcessObservation(pid, outcome, reservation.observation);
457
- let job = reservation.job ?? (selector > 0 ? selector : undefined);
458
- let createdJob = false;
459
- if (job !== undefined && !this.table.job(job)) {
460
- if (reservation.job === undefined)
461
- throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
462
- this.table.createJob(jobIdentity?.ownerPid ?? parentPid, jobIdentity?.foreground ?? selector === 0, job, jobIdentity, jobLifetime);
463
- createdJob = true;
464
- }
465
- if (job === undefined) {
466
- const group = this.table.createJob(parentPid, selector === 0, undefined, undefined, jobLifetime);
467
- group.controlParent = reservation.controlParent ?? parentPid;
468
- job = group.id;
469
- createdJob = true;
470
- }
551
+ const { job, createdJob } = this.resolveSpawnJob(parentPid, parent, selector, jobLifetime, reservation, jobIdentity);
471
552
  if (jobIdentity)
472
553
  this.table.adoptJobIdentity(job, jobIdentity);
473
554
  this.checkJobAdmission(job, admitted);
@@ -532,6 +613,21 @@ export class ProcessSupervisor {
532
613
  }
533
614
  return pid;
534
615
  }
616
+ resolveSpawnJob(parentPid, parent, selector, jobLifetime, reservation, jobIdentity) {
617
+ const rootPid = reservation.rootPid ?? parent?.rootPid;
618
+ const job = reservation.job ?? (selector > 0 ? selector : undefined);
619
+ if (job === undefined) {
620
+ const group = this.table.createJob(parentPid, selector === 0, undefined, undefined, jobLifetime, rootPid);
621
+ group.controlParent = reservation.controlParent ?? parentPid;
622
+ return { job: group.id, createdJob: true };
623
+ }
624
+ if (this.table.job(job))
625
+ return { job, createdJob: false };
626
+ if (reservation.job === undefined)
627
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
628
+ this.table.createJob(jobIdentity?.ownerPid ?? parentPid, jobIdentity?.foreground ?? selector === 0, job, jobIdentity, jobLifetime, rootPid);
629
+ return { job, createdJob: true };
630
+ }
535
631
  checkSpawnParent(parentPid, parent, kind, admitted, retained) {
536
632
  if (!parent && (!admitted || !retained))
537
633
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${parentPid}`);
@@ -939,7 +1035,12 @@ export class ProcessSupervisor {
939
1035
  this.finish(pid, { kind: "exited", code });
940
1036
  }
941
1037
  kill(pid, signal) {
942
- const sig = checkedSignal(signal), record = this.table.require(pid);
1038
+ const sig = checkedKillSignal(signal), record = this.table.require(pid);
1039
+ if (sig === 0) {
1040
+ if (!this.isLiveDirectoryRecord(record))
1041
+ throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
1042
+ return;
1043
+ }
943
1044
  if (record.result)
944
1045
  return;
945
1046
  this.recordSignal(record, sig);
@@ -993,10 +1094,15 @@ export class ProcessSupervisor {
993
1094
  this.finish(pid, winner);
994
1095
  }
995
1096
  killJob(jobId, signal) {
996
- const sig = checkedSignal(signal);
1097
+ const sig = checkedKillSignal(signal);
997
1098
  const job = this.table.job(jobId);
998
1099
  if (!job)
999
1100
  throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
1101
+ if (sig === 0) {
1102
+ if (!this.isLiveDirectoryJob(jobId))
1103
+ throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
1104
+ return;
1105
+ }
1000
1106
  const targets = Array.from(job.members, (pid) => this.table.require(pid));
1001
1107
  for (const record of targets) {
1002
1108
  try {
@@ -62,6 +62,13 @@ export class ProcessTable {
62
62
  const outcome = this.jobs.get(id)?.leaderOutcome;
63
63
  return isInstance(outcome, OutcomeCell) && !outcome.valid();
64
64
  }
65
+ jobDirectory(id) {
66
+ const job = this.jobs.get(id);
67
+ if (!job)
68
+ return;
69
+ const outcome = job.leaderOutcome ?? (job.leaderPid === undefined ? undefined : this.records.get(job.leaderPid)?.outcome);
70
+ return isInstance(outcome, OutcomeCell) ? outcome.validity.buffer : undefined;
71
+ }
65
72
  values() {
66
73
  return this.records.values();
67
74
  }
@@ -74,7 +81,7 @@ export class ProcessTable {
74
81
  throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
75
82
  return record;
76
83
  }
77
- createJob(ownerPid, foreground, id = this.identities.job(), identity, jobLifetime = "independent") {
84
+ createJob(ownerPid, foreground, id = this.identities.job(), identity, jobLifetime = "independent", rootPid) {
78
85
  if (isInstance(identity?.outcome, OutcomeCell))
79
86
  identity.outcome.check();
80
87
  const captured = isInstance(identity?.outcome, OutcomeCell);
@@ -84,6 +91,7 @@ export class ProcessTable {
84
91
  const job = {
85
92
  id,
86
93
  ownerPid,
94
+ rootPid: rootPid ?? identity?.rootPid ?? this.records.get(ownerPid)?.rootPid ?? ownerPid,
87
95
  controlParent: identity?.controlParent ?? ownerPid,
88
96
  foreground,
89
97
  jobLifetime: identity?.jobLifetime ?? jobLifetime,
@@ -75,3 +75,8 @@ export function checkedSignal(signal) {
75
75
  }
76
76
  return signal;
77
77
  }
78
+ export function checkedKillSignal(signal) {
79
+ if (signal === 0)
80
+ return 0;
81
+ return checkedSignal(signal);
82
+ }