Record the deployed continuation, process reaping, recovery, and cleanup fixes with the current board registry. Add focused regression coverage and document the shared production branch and existing startup configuration.
100 lines
4.3 KiB
Python
100 lines
4.3 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:
|
|
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)
|