On board kernels without /proc/<pid>/task/*/children, children() scans /proc and reads each stat; a process that exits between the listing and the read answers ESRCH (ProcessLookupError), which was not caught. 10-20 staging: the board guard crashed in its own cleanup after a 20-minute self-test, wrote no result, and the platform recorded BOARD_CLEANUP_UNCONFIRMED, quarantining a 5-hour rollout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4.5 KiB
Python
102 lines
4.5 KiB
Python
"""Linux kernel ownership primitive shared by host scopes and the board guard.
|
|
|
|
Use only in a dedicated single-threaded supervisor, before launching work.
|
|
No marker, process-group membership or sampled empty list proves completion.
|
|
"""
|
|
import ctypes
|
|
import os
|
|
from pathlib import Path
|
|
import signal
|
|
import socket
|
|
import time
|
|
|
|
|
|
def children(pid, proc=Path('/proc')):
|
|
result, fallback = set(), False
|
|
for task in (proc / str(pid) / 'task').iterdir():
|
|
try:
|
|
result.update(map(int, (task / 'children').read_text().split()))
|
|
except FileNotFoundError:
|
|
# Some board kernels omit CONFIG_CHECKPOINT_RESTORE's children ABI.
|
|
# A live task with no children file is not evidence of no children.
|
|
fallback = fallback or task.exists()
|
|
if fallback:
|
|
for process in proc.iterdir():
|
|
if not process.name.isdigit():
|
|
continue
|
|
try:
|
|
row = (process / 'stat').read_bytes().rsplit(b')', 1)[1].split()
|
|
except (FileNotFoundError, ProcessLookupError):
|
|
# Exited between listing and reading (ESRCH); 10-20 staging: this crashed the board guard's
|
|
# cleanup, so a finished self-test read as BOARD_CLEANUP_UNCONFIRMED and killed the rollout.
|
|
continue
|
|
if int(row[1]) == pid:
|
|
result.add(int(process.name))
|
|
return result
|
|
|
|
|
|
class Reaper:
|
|
def __init__(self):
|
|
if not hasattr(os, 'pidfd_open') or not hasattr(signal, 'pidfd_send_signal'):
|
|
raise OSError('pidfd capability is required')
|
|
descriptor = os.pidfd_open(os.getpid())
|
|
os.close(descriptor)
|
|
libc = ctypes.CDLL(None, use_errno=True)
|
|
if libc.prctl(36, 1, 0, 0, 0) != 0: # PR_SET_CHILD_SUBREAPER
|
|
raise OSError(ctypes.get_errno(), 'subreaper capability is required')
|
|
self.pid, self.members, self.exits = os.getpid(), {}, {}
|
|
self.host = socket.gethostname()
|
|
self.boot_id = Path('/proc/sys/kernel/random/boot_id').read_text().strip()
|
|
|
|
def poll(self, child=None):
|
|
"""True means the kernel returned ECHILD; every descendant has been reaped."""
|
|
while True:
|
|
try:
|
|
pid, status = os.waitpid(-1, os.WNOHANG)
|
|
except ChildProcessError:
|
|
return True
|
|
if not pid:
|
|
return False
|
|
self.exits[pid] = os.waitstatus_to_exitcode(status)
|
|
if child is not None and pid == child.pid:
|
|
child.returncode = self.exits[pid]
|
|
|
|
def signal(self, sig=None):
|
|
# Unreaped direct children cannot have their PIDs reused. pidfds and a
|
|
# second birth/PPID check also fence changes between reading and signalling.
|
|
for pid in children(self.pid):
|
|
try:
|
|
raw = Path(f'/proc/{pid}/stat').read_bytes()
|
|
row = raw.rsplit(b')', 1)[1].split()
|
|
if int(row[1]) != self.pid:
|
|
continue
|
|
identity = dict(host=self.host, boot_id=self.boot_id, pid=pid,
|
|
proc_start_ticks=int(row[19]), ppid=int(row[1]), pgrp=int(row[2]), session=int(row[3]),
|
|
state=row[0].decode('ascii'), cpu_ticks=int(row[11])+int(row[12]), status='KNOWN')
|
|
self.members[(pid, identity['proc_start_ticks'])] = identity
|
|
if sig is None or row[0] == b'Z':
|
|
continue
|
|
descriptor = os.pidfd_open(pid)
|
|
try:
|
|
check = Path(f'/proc/{pid}/stat').read_bytes()
|
|
current = check.rsplit(b')', 1)[1].split()
|
|
if int(current[1]) == self.pid and int(current[19]) == identity['proc_start_ticks']:
|
|
signal.pidfd_send_signal(descriptor, sig)
|
|
finally:
|
|
os.close(descriptor)
|
|
except (ProcessLookupError, FileNotFoundError):
|
|
continue
|
|
|
|
def cleanup(self, child, grace_s, deadline_s):
|
|
start = time.monotonic()
|
|
while True:
|
|
if self.poll(child):
|
|
return dict(cleanup_state='VERIFIED', all_children_reaped=True,
|
|
proof='waitpid:ECHILD', remaining=[])
|
|
elapsed = time.monotonic() - start
|
|
self.signal(signal.SIGTERM if elapsed < grace_s else signal.SIGKILL)
|
|
if elapsed >= deadline_s:
|
|
return dict(cleanup_state='QUARANTINED', all_children_reaped=False,
|
|
proof='waitpid:ECHILD', remaining=sorted(children(self.pid)))
|
|
time.sleep(.005)
|