@waffle-os/runtime 0.0.150 → 0.0.152

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.
@@ -340,6 +340,12 @@ export type PeerMessage =
340
340
  readonly totalBytes: number;
341
341
  readonly chunk: Uint8Array;
342
342
  }
343
+ | {
344
+ readonly kind: "chunk-abort";
345
+ readonly hostTerm: string;
346
+ readonly sessionId: string;
347
+ readonly uploadId: string;
348
+ }
343
349
  | {
344
350
  readonly kind: "chunk-get";
345
351
  readonly hostTerm: string;
@@ -378,6 +384,7 @@ export type PeerMessage =
378
384
  readonly pendingWatchAcks?: readonly (readonly [number, boolean])[];
379
385
  readonly pendingUnpins?: readonly number[];
380
386
  readonly pendingWatchCloses?: readonly number[];
387
+ readonly pendingUploadAborts?: readonly string[];
381
388
  }
382
389
  | {
383
390
  readonly kind: "pong";
@@ -389,6 +396,7 @@ export type PeerMessage =
389
396
  readonly settledWatchAcks?: readonly number[];
390
397
  readonly settledUnpins?: readonly number[];
391
398
  readonly settledWatchCloses?: readonly number[];
399
+ readonly settledUploadAborts?: readonly string[];
392
400
  readonly creditLimit: PeerCreditLimit;
393
401
  readonly hostState: HostHealthState;
394
402
  }
@@ -430,6 +438,7 @@ export type PeerMessage =
430
438
  readonly watchAcks?: readonly number[];
431
439
  readonly unpins?: readonly number[];
432
440
  readonly watchCloses?: readonly number[];
441
+ readonly uploadAborts?: readonly string[];
433
442
  }
434
443
  | {
435
444
  readonly kind: "detach";
@@ -534,6 +543,8 @@ export function isPeerMessage(value: unknown): value is PeerMessage {
534
543
  typeof value.totalBytes === "number" &&
535
544
  value.chunk instanceof Uint8Array
536
545
  );
546
+ case "chunk-abort":
547
+ return typeof value.uploadId === "string";
537
548
  case "chunk-get":
538
549
  return (
539
550
  typeof value.reqId === "number" &&
package/fs/service.ts CHANGED
@@ -1169,18 +1169,40 @@ export class FilesystemService {
1169
1169
  return;
1170
1170
  }
1171
1171
  if (operation === "flush" || operation === "sync") {
1172
- const flushing = this.backend.flush?.(
1173
- operation === "sync" ? a : typeof a === "string" ? a : undefined,
1174
- );
1175
- return this.trackRequestWork(session, Promise.resolve(flushing)).then(() =>
1176
- this.finishResult(session, id, operation, operation === "sync" ? 0 : undefined),
1172
+ const target = operation === "sync" ? a : typeof a === "string" ? a : undefined;
1173
+ if (operation === "sync") this.backend.retain(a);
1174
+ let flushing: Promise<void> | undefined;
1175
+ try {
1176
+ flushing = this.backend.flush?.(target);
1177
+ } catch (error) {
1178
+ if (operation === "sync") this.backend.release(a);
1179
+ throw error;
1180
+ }
1181
+ return this.trackRequestWork(session, Promise.resolve(flushing)).then(
1182
+ () => {
1183
+ if (operation === "sync") this.backend.release(a);
1184
+ return this.finishResult(session, id, operation, operation === "sync" ? 0 : undefined);
1185
+ },
1186
+ (error) => {
1187
+ if (operation === "sync") this.backend.release(a);
1188
+ throw error;
1189
+ },
1177
1190
  );
1178
1191
  }
1179
1192
  if (operation === "resolve" && session.socketScope && this.backend instanceof MemoryBackend) {
1180
1193
  const result = this.backend.fs.resolveSocket(a, b, c === true);
1181
1194
  return this.finishResult(session, id, operation, result, args);
1182
1195
  }
1183
- const result = this.backend.execute(operation, args, data);
1196
+ const isCancelled = () => {
1197
+ const pending = session.pending.get(id);
1198
+ return (
1199
+ !pending ||
1200
+ sessionRevoked(session) ||
1201
+ !this.sessions.has(session) ||
1202
+ requestCancelled(pending)
1203
+ );
1204
+ };
1205
+ const result = this.backend.execute(operation, args, data, isCancelled);
1184
1206
  if (result instanceof Promise)
1185
1207
  return this.trackRequestWork(session, result).then((value) =>
1186
1208
  this.finishResult(session, id, operation, value, args),
@@ -1,5 +1,5 @@
1
1
  // Bump on every edit. interrupt-transform-pin.test.ts binds this revision to the implementation.
2
- export const INTERRUPT_REWRITER_REVISION = 7;
2
+ export const INTERRUPT_REWRITER_REVISION = 8;
3
3
 
4
4
  /**
5
5
  * Fail-closed core-Wasm interrupt instrumentation. Supported features are MVP,
@@ -61,6 +61,7 @@ const runtimeProcSignatures = new Map<string, FunctionSignature>([
61
61
  ["wait_job", { params: [0x7f, 0x7f, 0x7f], results: [0x7f] }],
62
62
  ["kill", { params: [0x7f, 0x7f], results: [0x7f] }],
63
63
  ["kill_job", { params: [0x7f, 0x7f], results: [0x7f] }],
64
+ ["probe_process", { params: [0x7f], results: [0x7f] }],
64
65
  ["start_status", { params: [0x7f], results: [0x7f] }],
65
66
  ["poll", { params: [0x7f, 0x7f, 0x7f, 0x7f], results: [0x7f] }],
66
67
  ["interrupted", { params: [], results: [0x7f] }],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waffle-os/runtime",
3
- "version": "0.0.150",
3
+ "version": "0.0.152",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ShaulLavo/waffle.git",
package/proc/abi.ts CHANGED
@@ -61,6 +61,7 @@ import {
61
61
  ProcessError,
62
62
  Signal,
63
63
  checkedJobLifetime,
64
+ checkedKillSignal,
64
65
  checkedProcessCwd,
65
66
  checkedProcessKind,
66
67
  snapshotLogicalProcessIdentity,
@@ -96,7 +97,10 @@ export interface ProcessService {
96
97
  defaultSignal(pid: number, signal: number): void;
97
98
  /** Privileged local services check guest authority; broker clients are already caller-bound. */
98
99
  canSignal?(callerPid: number, targetPid: number): boolean;
100
+ canProbe?(callerPid: number, targetPid: number): boolean;
99
101
  canSignalJob?(callerPid: number, job: number): boolean;
102
+ canProbeJob?(callerPid: number, job: number): boolean;
103
+ canSignalProcess?(callerPid: number, processId: number): boolean;
100
104
  accessStart?(pid: number, path: string, cwd: string, wake?: Int32Array): number;
101
105
  accessTake?(pid: number, request: number): number;
102
106
  accessCancel?(pid: number, request: number): number;
@@ -288,16 +292,33 @@ export class ProcessABI {
288
292
  wait_job: (job, flags, ptr) => this.result(() => this.waitJob(job!, flags!, ptr!)),
289
293
  kill: (pid, signal) =>
290
294
  this.result(() => {
291
- if (options.service.canSignal?.(options.pid, pid!) === false) return -63;
292
- options.service.kill(pid!, signal!);
295
+ const sig = checkedKillSignal(signal!);
296
+ const allowed =
297
+ sig === 0 && options.service.canProbe
298
+ ? options.service.canProbe(options.pid, pid!)
299
+ : options.service.canSignal?.(options.pid, pid!);
300
+ if (allowed === false) return -ProcessErrno.PERM;
301
+ if (sig === 0 && allowed === true) return 0;
302
+ options.service.kill(pid!, sig);
293
303
  return 0;
294
304
  }),
295
305
  kill_job: (job, signal) =>
296
306
  this.result(() => {
297
- if (options.service.canSignalJob?.(options.pid, job!) === false) return -63;
298
- options.service.killJob(job!, signal!);
307
+ const sig = checkedKillSignal(signal!);
308
+ const allowed =
309
+ sig === 0 && options.service.canProbeJob
310
+ ? options.service.canProbeJob(options.pid, job!)
311
+ : options.service.canSignalJob?.(options.pid, job!);
312
+ if (allowed === false) return -ProcessErrno.PERM;
313
+ if (sig === 0 && allowed === true) return 0;
314
+ options.service.killJob(job!, sig);
299
315
  return 0;
300
316
  }),
317
+ probe_process: (pid) =>
318
+ this.result(() => {
319
+ if (!options.service.canSignalProcess) return -NOSYS;
320
+ return options.service.canSignalProcess(options.pid, pid!) ? 0 : -ProcessErrno.PERM;
321
+ }),
301
322
  start_status: (pid) => this.result(() => options.service.startStatus(options.pid, pid!)),
302
323
  poll: (ptr, count, timeout, out) =>
303
324
  this.result(() => this.poll(ptr!, count!, timeout!, out!)),
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
@@ -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.ts";
4
12
  import {
5
13
  isInstance,
@@ -56,13 +64,14 @@ import {
56
64
  ProcessErrno,
57
65
  ProcessError,
58
66
  Signal,
59
- checkedSignal,
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
- /** Guest adapters may signal themselves and descendants, never a sibling or ancestor. */
382
- canSignal(callerPid: number, pid: number): boolean {
383
- this.table.require(callerPid);
384
- this.table.require(pid);
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.table.require(callerPid);
480
+ const caller = this.requireLiveCaller(callerPid);
394
481
  const job = this.table.job(jobId);
395
- if (!job) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
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
- let job = reservation.job ?? (selector > 0 ? selector : undefined);
621
- let createdJob = false;
622
- if (job !== undefined && !this.table.job(job)) {
623
- if (reservation.job === undefined)
624
- throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
625
- this.table.createJob(
626
- jobIdentity?.ownerPid ?? parentPid,
627
- jobIdentity?.foreground ?? selector === 0,
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 = checkedSignal(signal),
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 = checkedSignal(signal);
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
+ }