Socket-table snapshots (macOS)
This page is published from the agent repository. Edit on GitHub View source
sensor-macos-sockets (crates/sensors/macos/sockets) snapshots listening TCP sockets for the LISTENER-DRIFT rule. It is the one macOS source that needs no entitlement, so it delivers listening-port telemetry even on hosts where the EndpointSecurity grant is missing. It is the macOS sibling of the Windows IP Helper snapshots and the Linux sock_diag path.
Mechanism
A libproc walk done in a C shim (shim/sockets_shim.c, built by build.rs): proc_listpids, then each process's fd table, then PROC_PIDFDSOCKETINFO per socket fd. socket_fdinfo is a large union-heavy struct, so as with the ES shim the layout stays in C and Rust receives one flat syn_sock_record per TCP socket. TCP states are mapped to shim-owned values (LISTEN, ESTABLISHED, other), never transcribed from TCPS_*.
Because the walk is per process, attribution is direct: every entry carries pid, the real ppid, effective uid/gid and the executable path (SocketSnapshotEntry).
Visibility follows libproc's rules. Unprivileged, only the caller's own processes resolve; as root the walk sees the whole table. Processes that exit mid-walk or are unreadable are skipped silently, and partial visibility is returned as is. Only a failure of the initial pid enumeration is an error (SocketsError::Snapshot).
The callback into Rust only collects records, so no user code runs inside the C walk and nothing can unwind across the FFI boundary (snapshot.rs).
Events
snapshot returns every TCP socket, listening and established. listen_port_events maps only listeners to Event::ListenPort through listen_port_event. Established sockets produce no event: connections come from the Network Extension flow stream, and a poll echo would double-count. Snapshot semantics are those of schema::ListenPortEvent (poll-time timestamp, short-lived listeners invisible, value in the drift).
How the agent uses it
seeded_rule_state seeds the LISTENER-DRIFT baseline from one snapshot at startup (listeners only, protocol TCP). spawn_socket_poller snapshots every 10 s and forwards the listener events. There is no heartbeat for this poller (#34).
Gaps
- TCP only. UDP bound-socket snapshots are tracked in #40.
- No container attribution or incarnation stamp.
Testing
Unit tests for the raw and normalize halves run on every OS. tests/live_snapshot.rs (macOS only, unprivileged, in the default suite) binds a listener in the test process and checks the walk finds it with full self-attribution.