English | 简体中文
Gatepost is Elsworne's venue entry system. gated runs on a Linux gate device, reads barcodes and drives its lights and turnstile. It can use a local synthetic ticket list when the venue is offline. The venue hub will coordinate gates and distribute ticket lists. libgate owns the local ticket store.
The 1.0.0 release includes offline ticket signatures, device TLS, two uplinks and NAT, and the north-entrance diagnosis. Review SECURITY.md before deployment.
Install Docker. From the repository root, build the debug binaries and open the build container:
./scripts/dev.sh build debug
./scripts/dev.sh shellInside the container, run the two gate walk-through:
python3 scripts/quick-start.py build/debug/gated-test build/debug/gate-hub-testThe walk-through uses the gated-test and gate-hub-test builds, which allow plaintext; production builds require device certificates.
The hub starts, and the north and south gates scan the same synthetic ticket. The answers are ACCEPT then REJECT. The script stops the hub, scans a second ticket at the north gate, and shows its offline ACCEPT and link log. An observed run in the build container printed the decision and the actual daemon log lines:
north: ACCEPT
south: REJECT
north, hub stopped: ACCEPT
time=1793674200093081 level=INFO event=connection_open gate=unknown peer=127.0.0.1:35176
time=1793674200093160 level=INFO event=scan gate=north ticket=GP-000000000000 result=ACCEPT decision_us=2
time=1793674200093205 level=INFO event=connection_close gate=north peer=127.0.0.1:35176
time=1793674200093273 level=INFO event=connection_open gate=unknown peer=127.0.0.1:35188
time=1793674200093367 level=INFO event=connection_open gate=unknown peer=127.0.0.1:35190
time=1793674200093370 level=INFO event=connection_close gate=north peer=127.0.0.1:35188
time=1793674200093435 level=INFO event=scan gate=south ticket=GP-000000000000 result=REJECT decision_us=1
time=1793674200093490 level=INFO event=connection_close gate=south peer=127.0.0.1:35190
time=1793674200093554 level=INFO event=connection_open gate=unknown peer=127.0.0.1:35198
time=1793674200093198 level=INFO event=hub_link gate=north state=online
time=1793674200093889 level=WARN event=hub_link gate=north state=offline reason=refused
time=1793674200093482 level=INFO event=hub_link gate=south state=online
time=1793674200093496 level=INFO event=scan gate=south ticket=GP-000000000000 result=REJECT reason=hub_rejected
The hub logs result=REJECT for the south gate's scan, and the south gate adds its own reason=hub_rejected line, the rejection diagnostic a gate writes for every refused scan. Each gate also checks its hub link about once a second, so a run can include connection_open and connection_close lines that belong to no scan; how many appear depends on timing.
For device installation, build the separate Debian packages with CPack and configure the hardened systemd units as described in packages and device setup. The release workflow builds packages, emits a Syft SBOM, and scans build and lab images with Trivy. Logging compatibility explains the journal and admission records.
Exit the container with exit. For lights and turnstile movement, use the team's separate gatepost-sim repository with the simulator protocol.
./scripts/dev.sh test debug runs the test suite in the pinned Linux build image. The release, asan and tsan presets are also available. The image includes gdb and strace for the investigations in docs/.
hub_integration and hub_link_logs start gates with a host name that cannot resolve and expect the lookup to fail at once. They need name servers that answer or refuse; when the configured name servers silently drop queries, for example on an isolated machine whose containers fall back to public resolvers, both tests time out.
On Docker Engine for Linux, containers write build/ and lab/runtime/ as root. Host-side scripts that write there afterwards, such as scripts/ticket-leaks.sh, scripts/ticket-paging.sh, scripts/lookup-locality.sh and lab/two-gate-scan.sh, then stop with Permission denied. Run sudo chown -R "$USER" build lab/runtime and run the script again. Docker Desktop maps file ownership to your user and is not affected.
gated --hub HOST:PORT asks the hub for every locally known, correctly formatted barcode. The gate bounds each hub scan request to 300 ms; a failed request makes the gate use its own used set and turn the network LED off. A successful response turns that LED on and is authoritative. Unknown or malformed barcodes are rejected locally. While the hub is unreachable, two gates can each admit the same ticket once; the hub detects that duplicate when durable offline reports arrive and exposes the count in /status. Each gate fsyncs an append-only admission record before opening the relay, then uploads reports over framed TCP when the hub returns. The first record also syncs the parent directory so the new file name survives a power cut. If the journal is shortened or replaced while the gate runs, the gate logs offset_reset and uploads it again from the start; the hub ignores reports it already holds. GATEPOST_HUB is the environment fallback. tests/test_hub_integration.py starts two real gates against one hub and checks online decisions, offline admissions and the LED.
gated supervises a scanner reader process. The parent keeps the ticket store and used flags across reader restarts; a self-pipe wakes it for SIGCHLD, SIGHUP and SIGTERM. READY=1 confirms the reader started and exec succeeded, not that hardware is connected. lab/systemd/gated.service adds restart limits, a watchdog and journal integration. gated reads one barcode per line from standard input by default. It writes one ACCEPT or REJECT answer per scan to standard output. A real scanner can use --scanner tty:/dev/ttyACM0; hardware outputs can use --outputs gpio:/dev/gpiochip0. GPIO offsets come from GATEPOST_GPIO_GREEN, GATEPOST_GPIO_RED, GATEPOST_GPIO_RELAY and GATEPOST_GPIO_NET (defaults 0–3). The GPIO backend is only compiled and type-checked in CI; device behaviour is verified with the simulator, not on a physical gate. --scanner sim:HOST:PORT and --outputs sim:HOST:PORT connect to the simulator. Set --gate-id ID to identify the gate. GATEPOST_SCANNER, GATEPOST_OUTPUTS and GATEPOST_GATE_ID are environment fallbacks.
gate-hub uses GATEPOST_TLS_CA for the trust anchor, GATEPOST_TLS_CERT for its certificate chain, and GATEPOST_TLS_KEY for its private key. Production gate-hub requires all three for device-authenticated TLS 1.3 on the gate port; with none set it refuses startup with reason=tls_required. The separately built gate-hub-test allows plaintext when all three are unset. Missing credentials in production log level=ERROR event=startup state=failed reason=tls_required; a test listener logs state=plaintext. A partial configuration refuses to start.
gated --hub HOST:PORT uses the same three variables: GATEPOST_TLS_CA trusts the venue hub, GATEPOST_TLS_CERT supplies the gate certificate chain, and GATEPOST_TLS_KEY supplies its private key. Production gated --hub requires all three for TLS 1.3 and refuses startup with level=ERROR event=startup state=failed reason=tls_required when all are unset. The separately built gated-test --hub allows plaintext when all three are unset; without a hub the variables are ignored. Startup refuses partial configuration (tls_incomplete), unreadable files (tls_unreadable), invalid PEM or an unusable chain (tls_invalid), a mismatched key (tls_key_mismatch), or a missing, invalid, ambiguous or mismatched gate URI identity (tls_gate_mismatch). The gate checks the hub certificate against hub.venue.lan, regardless of the address it connects to.
gated --arp-interface IF watches ARP on that interface and logs event=address_conflict when another device’s broadcast ARP claims the gate’s current IPv4 address; detection happens when such a broadcast arrives, not continuously, and the gate never sends ARP replies or defends the address. Watching requires CAP_NET_RAW (Docker grants it by default; the systemd unit grants only this capability); without the option no watch is started, and if opening it fails, the gate logs event=address_watch state=unavailable and keeps admitting.
gate-hub TICKETS PORT per-connection|pool listens on loopback. It accepts framed LOOKUP, SCAN, HELLO and CONFIG RPCs; the older LOOKUP barcode and SCAN gate-id barcode lines remain temporarily compatible. A 100 ms probe selects the protocol and closes incomplete connections. See the gate protocol. In per-connection mode it starts a thread for each client; pool uses eight worker threads and a 64-connection queue. gated --hub HOST:PORT asks the hub for a known ticket; the hub's first admission wins across gates. A resolver process refreshes the hub address outside the scan path, and hub requests stop after 300 ms. Ticket-list downloads are the exception: libcurl resolves the host name itself, so when --hub names a host and the DNS server does not answer, a download, and any scan waiting behind it, lasts until the system resolver gives up (about 20 seconds with two unresponsive name servers in the default resolv.conf), and the download repeated every 20 seconds meets the same wait. Give --hub an IP address to avoid it; see the known issues in the changelog. When the hub cannot answer, each gate uses its own used set. During an outage, two gates can each admit the same ticket once; later offline reporting detects this for staff.
The hub protects each ticket's admission state with a mutex; a separate atomic CAS mode is used in the admission benchmark. The store lock precedes the statistics lock. A statistics failure is logged but does not undo an admission or change its answer. scripts/two-gates-race.py, scripts/admission-tsan.sh and scripts/lock-order-helgrind.sh reproduce and check the concurrency failures.
On SIGHUP, gated TICKETS LOG inline|background rebuilds the ticket list. inline pauses scans while rebuilding; background builds in a child at nice 10 and installs the result between scans. The loader validates each barcode, rejects duplicate barcodes, sorts unsorted input, and skips sorting input already in order. The rebuilt file is written to a temporary file, synced to disk, then renamed into place. scripts/sync-latency.sh compares scan latency under a one-CPU limit.
Ticket reloads use one arena for barcode bytes and an array of ticket records. The 128 MiB loader budget includes both the old and new store during the switch, counting each arena by its allocated size. tools/memory_churn and scripts/ticket-leaks.sh measure and check reload memory; see ticket memory.
For stadium lists, generate a sorted .gpb file with tools/gen_binary_tickets.py. gated maps its records read-only, keeps admissions in a separate bitset, and carries them across SIGHUP reloads. The mapping has a separate size cap because its file-backed pages can be reclaimed under memory pressure; see ticket paging. Lookups binary-search the sorted records. scripts/lookup-locality.sh compares that with a compact hash index and scripts/scan-order.sh measures whole gate scans; lookup locality explains why gated keeps binary search.
A gate can load a mapped .gpb list only without public keys; signed mode refuses that path, and mapped records hold 32-byte unsigned barcodes, so signed tickets use a text list. The hub loads text lists only.
Tickets in this repository are synthetic; generate them with tools/gen_tickets.py. On a device, choose the scanner and outputs at startup. Standard output carries decisions and standard error carries device errors and, for each rejected scan, one event=scan line with the reason (see rejection diagnostics). The hub writes one event=scan line to standard error for each scan, with time (Unix microseconds), level, gate, ticket (or length for malformed input), result and decision_us (elapsed microseconds). It also logs event=connection_open and event=connection_close with the peer address; the gate is unknown until a SCAN identifies it. On ports 7000 and 8444, handshake failures log event=tls_handshake state=failed reason=<reason> peer=<address> and event=status_client state=handshake_failed reason=<reason> peer=<address>, respectively; a 7000 handshake deadline logs state=timeout peer=<address>. The address comes from the accepted connection. The same peer and reason on a listener produce one line until the reason changes or a handshake succeeds. Each listener remembers 64 peers, forgetting the oldest seen when full. A status connection that runs out of time logs event=status_client state=timeout with the peer address and held_ms. A full 8444 queue logs event=status_queue state=full peer=<address> at most once per second. event=admission_unlock_failed state=after_decision reports a failed ticket mutex unlock, and event=operation_failed reports a failed system operation. gated writes one event=hub_link line to standard error when its hub link becomes online or offline, or the offline reason changes; offline reasons are refused, timeout, resolver, or the TLS reasons below. The resolver logs event=hub_resolver only when DNS changes between failed and resolved. All daemon diagnostics use space-separated key=value fields, time as Unix microseconds, and level and event fields. It does not repeat the line for scans while both state and reason are unchanged. A local decision still appears on standard output and in the optional scan log. An optional scan log path records barcode=... result=... (in signed mode, <field> result=... under the rejection-diagnostic rule above) on a buffered, long-lived file descriptor. Use scripts/scan-syscalls.sh and scripts/scan-latency.sh in the build container to inspect system calls and latency; see the measurements.
TLS failure reasons in the device link logs (see the device-certificate renewal runbook):
| Reason | Meaning | Appears in |
|---|---|---|
certificate_expired |
The peer certificate has expired. | Both |
certificate_not_yet_valid |
The peer certificate is not valid yet. | Both |
certificate_untrusted |
The peer certificate has no trusted chain. | Both |
certificate_invalid |
Other certificate verification or identity failure. | Both |
certificate_name_mismatch |
The hub certificate does not name hub.venue.lan. |
Gate hub_link |
no_certificate |
The peer sent no client certificate. | Hub tls_handshake / status_client |
rejected_by_peer |
The peer sent a fatal TLS alert; when the hub rejects a gate certificate, the gate learns it on its first request after the handshake. | Both |
protocol |
Other hub-side handshake failure. | Hub tls_handshake / status_client |
tls_handshake |
Other gate-side handshake failure. | Gate hub_link |
timeout |
The handshake deadline expired. | Gate hub_link; hub tls_handshake |
At startup, an expired or not-yet-valid own certificate produces one level=ERROR event=tls_certificate state=expired or state=not_yet_valid line. Hub event=dc_sync state=failed reason=<reason> uses the same fetch reasons as ticket_fetch: timeout, connect, tls_certificate, tls_handshake, tls_config, connection, http, install, and other.
Ticket work logs event=ticket_sync for build start, completion or failure and event=ticket_reload state=complete after a mapped list switch; reload failures use event=ticket_reload at error level. A connection that does not send four protocol bytes within 100 ms logs event=protocol_probe state=incomplete once before closing. A compatible old-protocol connection logs event=legacy_protocol with its peer address. These events occur once per attempt.
The data-centre probe logs event=dc_dns when resolution becomes resolved, stale, failed, or recovered. A stale address is usable for at most 30 seconds; reason=stale_expired means the grace period ended, and reason=never_resolved that the name has not resolved since the hub started. resolved is the first successful lookup, recovered one after a failure. It logs event=dc_link state=connected or state=failed only when the connection state changes, and state=connected address=... previous=... when it stays connected but moves to another address; this TCP probe uses port 8443 with device TLS configured, or 8080 otherwise. GATEPOST_DC_HOST sets the host that both the probe and the sync connect to (default api.elsworne.example); the sync always checks the certificate as api.elsworne.example. A failed probe startup or socket close uses event=operation_failed.
Mail alerts also cover two hub faults: Data centre unreachable when probe rounds cannot connect, and Uplink watcher stopped when the configured heartbeat file stops changing. Each uses alert_after_seconds, sends once per incident after that threshold, sends no recovery mail, re-arms on recovery, and keeps the sent state across hub restarts. The watcher writes the file each loop; its modification time is the heartbeat because a shell has no portable monotonic clock and the watcher and hub share a wall clock in the same container. The hub grants a fresh threshold after restart. These alerts log event=mail_alert alert=data_centre state=failed/sent or alert=uplink_watcher state=failed/sent; gate alerts retain event=mail_alert gate=... state=failed once when submission fails and state=sent after submission and durable state update. Persistent alert-state file failures and their recovery use event=mail_state state=failed and state=recovered. Alert initialization failures use event=operation_failed.
In the test builds, without device TLS, the hub serves /tickets, /status and /metrics over HTTP on port 8081. When device TLS and GATEPOST_STATUS are configured, port 8081 serves /status and /metrics but answers GET /tickets with 404; port 8444 serves the list and the other routes over TLS 1.3 at the same bind address. Port 8081 uses one thread for staff; port 8444 uses four workers and a queue of 16. The venue has four gates (gate1, gate2, north1, north2), each with at most one 8444 connection open at a time. A stuck gate holds one worker while the others can still be served; if the north link fails, its two gates hold at most two workers, leaving workers for both venue gates. Four stuck clients at once make others wait again. Port 8444 requires a venue CA-signed gate certificate and accepts only Host: hub.venue.lan or Host: hub.venue.lan:8444. With device TLS configured, a gate fetches https://hub.venue.lan:8444/tickets on startup and periodically, connecting to the --hub host while verifying the hub certificate as hub.venue.lan and presenting its own certificate. In the test builds, without device TLS, it fetches over 8081: with --hub 10.77.20.10:7000, it asks for http://10.77.20.10:8081/tickets and sends Host: 10.77.20.10:8081. Set GATEPOST_TICKETS_URL to override the URL; production builds accept only https:// and file:// overrides, while the http:// override warning appears only in test builds. The hub answers only Host values listed in GATEPOST_STATUS_HOSTS (default hub.venue.lan, hub.venue.lan:8081, 127.0.0.1:8081 and localhost:8081) and returns 400 to any other. For plaintext gates reaching the hub by address, add <address>:8081 to GATEPOST_STATUS_HOSTS; the variable replaces the default list, so keep the names you still use. A gate logs event=ticket_fetch state=failed http=<status> curl=<code> reason=<reason> phase=<phase> received=<bytes> expected=<bytes|unknown> when fetching starts failing or the reason or phase changes. It suppresses repeats with the same reason and phase even if the byte counts differ, and logs event=ticket_fetch state=recovered once when it works again. The gate retains used-ticket state when installing a new list. With device TLS configured, the hub syncs the signed season list from /season.txt over port 8443 using its certificate; without device TLS it uses port 8080. Before switching decisions, it checks that SHA-256 matches the ETag, the complete list loads under the start-file rules, and the downloaded file and its directory were synced. /tickets and decisions use the same immutable in-memory list. A switch logs event=season_list state=installed tickets=<n> used=<n> load_ms=<ms>; a refused list logs event=season_list state=rejected reason=digest or reason=load errno=<n> once per ETag and reason. The hub keeps its current list after a refusal and tries again on the next round.
The hub also serves /metrics on its staff-side status listener, separate from the gate RPC port. In the lab it binds 10.77.20.10:8081, and only staff-subnet or loopback clients may read metrics. See metric names and labels. The hub sends X-Content-SHA256 with each ticket list; a gate verifies the bytes and prepares its ETag metadata before replacing the old list.
After a restart, a gate drops a saved ETag if it no longer matches the installed list and logs event=ticket_etag state=dropped reason=list_mismatch. It then fetches the complete list.
lab/sigterm_latency.py measures stop latency for each scanner backend: from SIGTERM to supervisor exit at random moments with an idle reader, and from just before gdb detaches to reader exit after queuing SIGTERM while the reader is stopped before poll. It needs gdb; open the build container with ./scripts/dev.sh shell, then run python3 lab/sigterm_latency.py build/debug/gated 50.
See venue monitoring, scanner supervision, lock order, the simulator protocol, Gate 3 crash investigation and team.