⬢ DragonFlyBSD Kernel Audit
DF-5720 / experiment.py
← back to finding ↓ download raw
#!/usr/bin/env python3
"""DF-5720 — controlled continue-resume experiment.

Sends: <pre-wait> delay at db>, then optional command battery, then 'c'.
argv: delay_seconds [battery]
  battery in {'none','examine','print','write','full'}
"""
import socket, sys, threading, time

SOCK = "/tmp/opencode/df-5720-ser.sock"
DRAIN = "/tmp/opencode/df-5720-drain.log"

def connect():
    while True:
        try:
            s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
            s.connect(SOCK)
            return s
        except (FileNotFoundError, ConnectionRefusedError):
            time.sleep(2)

def drainer(s):
    with open(DRAIN, "ab", buffering=0) as d:
        while True:
            try:
                b = s.recv(65536)
            except OSError:
                return
            if not b:
                return
            d.write(b)

def send_line(s, line):
    data = (line if line.endswith("\n") else line + "\n").encode()
    for i in range(0, len(data), 8):
        s.sendall(data[i:i+8])
        time.sleep(0.15)

BATTERIES = {
    "none":     [],
    "examine":  ["examine/4 db_active"],
    "print":    ["print/4 0x41"],
    "write":    ["write/l db_active 1"],
    "full":     ["x/x db_active", "x/4 db_active", "examine/4 db_active",
                 "print 0x41", "print/4 0x41", "print/x 0x41",
                 "write/4 db_active 1", "write/l db_active 1",
                 "x/x db_active"],
}

def main():
    delay = float(sys.argv[1])
    battery = BATTERIES[sys.argv[2] if len(sys.argv) > 2 else "none"]
    s = connect()
    t = threading.Thread(target=drainer, args=(s,), daemon=True)
    t.start()
    time.sleep(2)
    print(f"waiting {delay}s at db> ...", flush=True)
    time.sleep(delay)
    for cmd in battery:
        print(f">>> {cmd}", flush=True)
        send_line(s, cmd)
        time.sleep(3)
    print(">>> c", flush=True)
    send_line(s, "c")
    time.sleep(8)
    print("done", flush=True)

if __name__ == "__main__":
    main()