87 lines
5.7 KiB
Python
87 lines
5.7 KiB
Python
"""Operational process state for the three project services; no customer scan."""
|
|
|
|
|
|
def apt_install_metadata_keys(payload):
|
|
"""Read only the fixed bootstrap metadata schemas; never secret contents."""
|
|
identifier = str(uuid.UUID(payload['id']))
|
|
base = Path('/var/lib/rmm-apt-transaction')
|
|
history = Path('/var/lib/rmm-bootstrap-history') / ('post-reboot-apt-readiness-' + identifier)
|
|
current = base / 'install-metadata.json'
|
|
previous = history / 'var__lib__rmm-apt-transaction__install-metadata.json'
|
|
values = []
|
|
for path in (current, previous):
|
|
require(not path.is_symlink() and path.stat().st_uid == 0
|
|
and path.stat().st_mode & 0o077 == 0 and path.stat().st_size < 4096,
|
|
'fixed private bootstrap metadata')
|
|
values.append(json.loads(path.read_bytes()))
|
|
original = {'schema_version', 'provider_sha256', 'hook_sha256', 'unit_sha256'}
|
|
return dict(current_keys=sorted(values[0]), previous_keys=sorted(values[1]),
|
|
original_strict_schema=set(values[1]) == original,
|
|
original_fields_unchanged=all(values[0].get(k) == values[1][k] for k in original),
|
|
current_sha256=hashlib.sha256(current.read_bytes()).hexdigest(),
|
|
previous_sha256=hashlib.sha256(previous.read_bytes()).hexdigest())
|
|
|
|
|
|
def service_processes(payload):
|
|
result={}
|
|
# Same read-only kernel clock-readiness query required by the runtime;
|
|
# modes stays zero. No time correction or trust override exists here.
|
|
import ctypes
|
|
class TimexPrefix(ctypes.Structure):
|
|
_fields_=[('modes',ctypes.c_uint),('offset',ctypes.c_long),('freq',ctypes.c_long),
|
|
('maxerror',ctypes.c_long),('esterror',ctypes.c_long),('status',ctypes.c_int),('remaining',ctypes.c_byte*256)]
|
|
require(ctypes.sizeof(ctypes.c_long)==8 and TimexPrefix.maxerror.offset==24 and TimexPrefix.status.offset==40,'reviewed Linux x64 timex layout')
|
|
clock=TimexPrefix();libc=ctypes.CDLL(None,use_errno=True);query=libc.adjtimex
|
|
query.argtypes=[ctypes.POINTER(TimexPrefix)];query.restype=ctypes.c_int
|
|
status=query(ctypes.byref(clock))
|
|
result['kernel_clock']=dict(status=status,unsynchronized=bool(clock.status&0x40),maxerror_us=clock.maxerror,
|
|
trusted=status>=0 and status!=5 and not clock.status&0x40 and 0<=clock.maxerror<=5_000_000,modes=clock.modes)
|
|
storage=os.statvfs('/var/lib/rmm-helper')
|
|
result['bootstrap_storage']=dict(available_bytes=storage.f_bavail*storage.f_frsize,available_inodes=storage.f_favail,
|
|
helper_database_bytes=Path('/var/lib/rmm-helper/consumption.sqlite').stat().st_size,
|
|
agent_database_bytes=Path('/var/lib/rmm-agent-remote/journal.sqlite').stat().st_size)
|
|
lock=Path('/var/lib/rmm-helper/executor.lock').stat()
|
|
result['executor_lock_pids']=[]
|
|
for line in Path('/proc/locks').read_text().splitlines():
|
|
parts=line.split()
|
|
if len(parts)>5 and parts[5].split(':')[-1]==str(lock.st_ino):result['executor_lock_pids'].append(parts[4])
|
|
for name in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'):
|
|
values=subprocess.run(['systemctl','show',name,'-p','ActiveState','-p','SubState','-p','MainPID','-p','NRestarts'],
|
|
capture_output=True,check=True,timeout=10).stdout.decode()
|
|
fields=dict(line.split('=',1) for line in values.splitlines() if '=' in line)
|
|
pid=int(fields['MainPID']);processes=[]
|
|
# Kernel scheduling metadata only; never command lines, environment,
|
|
# descriptors, credentials, arbitrary process memory or customer logs.
|
|
if pid:
|
|
pending=[pid]
|
|
while pending and len(processes)<32:
|
|
current=pending.pop(0);root=Path('/proc')/str(current)
|
|
if not root.exists():continue
|
|
processes.append(dict(pid=current,comm=(root/'comm').read_text().strip(),wchan=(root/'wchan').read_text().strip(),
|
|
state=next((s.split(':',1)[1].strip() for s in (root/'status').read_text().splitlines() if s.startswith('State:')),None)))
|
|
processes[-1]['threads']=[dict(tid=int(p.name),comm=(p/'comm').read_text().strip(),wchan=(p/'wchan').read_text().strip())
|
|
for p in sorted((root/'task').iterdir())[:32] if p.is_dir()]
|
|
children=root/'task'/str(current)/'children'
|
|
if children.exists():pending.extend(int(s) for s in children.read_text().split())
|
|
result[name]=dict(service=fields,processes=processes)
|
|
return result
|
|
|
|
|
|
def stored_bootstrap_error_codes(payload):
|
|
"""Only allowlisted stored daemon errors during this runtime deployment."""
|
|
known=('CLOCK_UNTRUSTED','HELPER_VALIDATION_OR_IO_FAILURE','HELPER_PIN_PROFILE_MISMATCH',
|
|
'SYNC_OR_TRANSPORT_DEPENDENCY_UNAVAILABLE','HELPER_CHECKPOINTS_DID_NOT_CONVERGE',
|
|
'HELPER_MANUAL_RECOVERY_REQUIRED','LOCAL_CHECKPOINT_ROLLBACK','SYNC_MANUAL_RECOVERY_REQUIRED')
|
|
result={}
|
|
for unit in ('rmm-agent-remote.service','rmm-helper-cloud.service','rmm-apt-helper.service'):
|
|
process=subprocess.run(['/usr/bin/journalctl','--no-pager','--quiet','--output=json','--unit',unit,
|
|
'--since','2026-11-13 17:34:00 UTC','--lines','100'],capture_output=True,check=True,timeout=10)
|
|
require(len(process.stdout)<=256*1024,'bounded deployment error metadata')
|
|
counts={k:0 for k in known};times=[]
|
|
for line in process.stdout.splitlines():
|
|
row=json.loads(line);message=row.get('MESSAGE','')
|
|
for code in known:
|
|
if code in message:counts[code]+=1;times.append(row.get('__REALTIME_TIMESTAMP'))
|
|
result[unit]=dict(counts={k:v for k,v in counts.items() if v},first=min(times) if times else None,last=max(times) if times else None)
|
|
return result
|