#!/usr/bin/env python3 """Measure vectored byte boundaries in private files, without crash claims.""" import errno import hashlib import json import os import platform import resource import signal import subprocess import sys import tempfile from datetime import datetime, timezone from pathlib import Path def remaining_buffers(buffers, accepted): """Advance by bytes, including a partial final buffer; not a retry loop.""" if type(accepted) is not int or not 0 <= accepted <= sum(map(len, buffers)): raise ValueError("Accepted count must be an integer within the request") result = [] for buffer in buffers: used = min(accepted, len(buffer)) accepted -= used if used < len(buffer): result.append(buffer[used:]) return result def child_case(limit): if limit not in (2, 3, 6): raise ValueError("Only the three documented six-byte cases are supported") signal.signal(signal.SIGXFSZ, signal.SIG_IGN) with tempfile.TemporaryDirectory(prefix="hesela-writev-") as directory: fs = subprocess.run(["stat", "-f", "-c", "%T", directory], check=True, capture_output=True, text=True, timeout=5).stdout.strip() fd = os.open(Path(directory) / "payload", os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o600) try: # The disposable child owns this limit; the parent is untouched. resource.setrlimit(resource.RLIMIT_FSIZE, (limit, limit)) buffers = [b"AB", b"CDEF"] count = os.writev(fd, buffers) remainder = remaining_buffers(buffers, count) retry_count = None retry_error = None if remainder: try: retry_count = os.writev(fd, remainder) except OSError as exc: retry_error = errno.errorcode.get(exc.errno, str(exc.errno)) return {"limitBytes": limit, "bufferLengths": list(map(len, buffers)), "requestedBytes": sum(map(len, buffers)), "returnedBytes": count, "remainingBuffers": [part.decode("ascii") for part in remainder], "retryAttempted": bool(remainder), "retryReturnedBytes": retry_count, "retryError": retry_error, "offsetAfter": os.lseek(fd, 0, os.SEEK_CUR), "readback": os.pread(fd, 16, 0).decode("ascii"), "temporaryFilesystem": fs} finally: os.close(fd) def observe(): if sys.platform != "linux": raise RuntimeError("This observation is scoped to Linux") rows = [] for limit in (2, 3, 6): child = subprocess.run([sys.executable, str(Path(__file__).resolve()), "--child", str(limit)], check=True, capture_output=True, text=True, timeout=15) rows.append(json.loads(child.stdout)) return {"schemaVersion": 1, "generatedAt": datetime.now(timezone.utc).isoformat(), "sourceSha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "environment": {"python": platform.python_version(), "kernelRelease": platform.release(), "libc": list(platform.libc_ver())}, "method": "Three separate Linux child processes, two input buffers AB and CDEF, controlled file-size limits of 2, 3 and 6 bytes.", "limits": ["No fsync, power interruption, concurrent writer or network filesystem.", "Resource limits are deliberate, not observed device failures.", "Readback and current offset measure runtime behavior, not persistence.", "The remainder helper is byte accounting, not a general retry or recovery protocol."], "cases": rows} if __name__ == "__main__": args = sys.argv[1:] if args and (len(args) != 2 or args[0] != "--child"): raise SystemExit("Usage: vectored.py [--child 2|3|6]") result = child_case(int(args[1])) if args else observe() print(json.dumps(result, indent=2))