"""Build and observe native Linux OFD lock calls on disposable files. Keep demo.c beside this script. Requires Linux, Python 3 and a C compiler. No supplied data files are opened; no crash, fsync or network test is performed. """ import argparse from datetime import datetime, timezone import hashlib import json from pathlib import Path import platform import subprocess import tempfile ROOT = Path(__file__).resolve().parent FLAGS = ["-std=c11", "-O2", "-Wall", "-Wextra", "-Werror"] def observe(): if platform.system() != "Linux": raise RuntimeError("This observation requires Linux with OFD locks") source = ROOT / "demo.c" with tempfile.TemporaryDirectory(prefix="hesela-ofd-build-") as temporary: binary = Path(temporary) / "demo" subprocess.run(["cc", *FLAGS, str(source), "-o", str(binary)], check=True, capture_output=True, text=True, timeout=30) result = subprocess.run([str(binary)], check=True, capture_output=True, text=True, timeout=10) data = json.loads(result.stdout) data.update({ "schemaVersion": 1, "generatedAt": datetime.now(timezone.utc).isoformat(), "kind": "local-linux-api-observation", "environment": { "kernelRelease": platform.release(), "architecture": platform.machine(), "python": platform.python_version(), "compiler": subprocess.run(["cc", "--version"], check=True, capture_output=True, text=True, timeout=10).stdout.splitlines()[0], "temporaryFilesystem": subprocess.run( ["stat", "-f", "-c", "%T", "/tmp"], check=True, capture_output=True, text=True, timeout=10).stdout.strip(), }, "compileFlags": FLAGS, "sourceSha256": hashlib.sha256(source.read_bytes()).hexdigest(), "runnerSha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "scope": "Ordered single-process calls on four empty disposable files; " "one-byte write locks at offsets 0 and 1, including bytes beyond EOF. " "Independent open descriptions are contenders; dup shares ownership. " "No fork, threads, blocking waits, network filesystem or crash test.", }) return data def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path) args = parser.parse_args() if args.output and args.output.exists(): parser.error("--output refuses to overwrite an existing file") content = json.dumps(observe(), indent=2) + "\n" if args.output: with args.output.open("x", encoding="utf-8") as output: output.write(content) else: print(content, end="") if __name__ == "__main__": main()