"""Bounded Linux lock-family observation; only disposable files are touched.""" import argparse from datetime import datetime, timezone import errno import hashlib import itertools import json from pathlib import Path import platform import subprocess import tempfile APIS = ("flock", "posix", "ofd") OPTIONS = ("-O0", "-O2") REPEATS = 3 def validate(result): expected_keys = set(itertools.product(OPTIONS, range(REPEATS), APIS, APIS)) keys = [(row["optimization"], row["repeat"], row["holder"], row["contender"]) for row in result["rows"]] assert len(keys) == len(expected_keys) and set(keys) == expected_keys for row in result["rows"]: isolated_families = (row["holder"] == "flock") != (row["contender"] == "flock") assert [p["phase"] for p in row["probes"]] == ["held", "released"] for probe, acquired in zip(row["probes"], (isolated_families, True)): assert probe["same_file"] is True assert probe["acquired"] is acquired assert probe["errno"] == 0 if acquired else probe["errno"] in (errno.EAGAIN, errno.EACCES) return True def capture(args): return subprocess.check_output(args, text=True, timeout=15).strip() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", required=True, type=Path) parser.add_argument("--work-dir", required=True, type=Path) args = parser.parse_args() if platform.system() != "Linux": raise SystemExit("This probe requires Linux and F_OFD_SETLK support") source = Path(__file__).resolve().parent result = { "schema_version": 1, "generated_at": datetime.now(timezone.utc).isoformat(), "scope": "Local interprocess exclusive-lock compatibility; not a crash, network or performance test.", "environment": { "system": platform.system(), "kernel": platform.release(), "architecture": platform.machine(), "libc": list(platform.libc_ver()), "python": platform.python_version(), "compiler": capture(["cc", "--version"]).splitlines()[0], "filesystem": capture(["findmnt", "-n", "-o", "FSTYPE", "--target", str(args.work_dir)]), }, "protocol": { "record_range": {"start_byte": 0, "length_bytes": 1}, "flock_range": "whole file", "mode": "exclusive/write, nonblocking", "ownership": "parent holder; child closes inherited fd and opens a new description", "release_control": "new child after explicit parent unlock; same file identity verified", "repeats_per_optimization": REPEATS, "compiler_options": ["-std=c11", "-Wall", "-Wextra", "-Werror"], }, "sources_sha256": {name: hashlib.sha256((source / name).read_bytes()).hexdigest() for name in ("probe.c", "run.py", "test_matrix.py")}, "rows": [], } with tempfile.TemporaryDirectory(prefix="hesela-lock-matrix-", dir=args.work_dir) as temporary: root = Path(temporary) for optimization in OPTIONS: binary = root / ("probe" + optimization) subprocess.run(["cc", *result["protocol"]["compiler_options"], optimization, str(source / "probe.c"), "-o", str(binary)], check=True, timeout=30) for repeat, holder, contender in itertools.product(range(REPEATS), APIS, APIS): file = root / "lock-file" file.write_bytes(b"x") output = capture([str(binary), str(file), holder, contender]) result["rows"].append({"optimization": optimization, "repeat": repeat, "holder": holder, "contender": contender, "probes": [json.loads(line) for line in output.splitlines()]}) file.unlink() validate(result) args.output.write_text(json.dumps(result, indent=2) + "\n") print(f"{len(result['rows'])} pairs, {2 * len(result['rows'])} probes; all controls passed") if __name__ == "__main__": main()