Skip to content

feat: discover.WithFdCallback for hooking into the fd scan - #106

Merged
thediveo merged 1 commit into
developfrom
feat/discover-socketprocessesof
Nov 6, 2026
Merged

thediveo merged 1 commit into
developfrom
feat/discover-socketprocessesof

Conversation

@thediveo

@thediveo thediveo commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

Problem

Consumers that need to inspect open file descriptors of processes (for instance, Ghostwire finding the processes serving TAP/TUN netdevs) currently have to walk /proc/*/fd (or /proc/*/fdinfo) a second time, although an lxkns discovery with FromFds() or WithSocketProcesses() already walks all fds of all processes.

Change

New discovery option WithFdCallback(fn FdCallback):

  • fn gets called for each open fd of each discovered process, with an FdEntry carrying the Process, the Fd number, and the link Target (such as socket:[123] or /dev/net/tun).
  • FdEntry.PidFd() returns a PID fd for the owning process, opened on demand and shared (via unexported per-process scan state) with the other entries of the same process and the discovery's own socket inspection, for use with pidfd_getfd. It must neither be closed nor retained beyond the callback.
  • Registering a callback implicitly enables the fd scan; the option can be given multiple times; nil callbacks are ignored.
  • Callbacks run synchronously in sequence from the discovering goroutine, before container discovery; fds are a snapshot and might already be closed or reused afterwards.
  • Callbacks alone don't change the discovery result: no SocketProcessMap and no namespaces unless WithSocketProcesses() or FromFds() are also given.
  • The callbacks are kept in an unexported DiscoverOpts.fdCallbacks that gets cleared after the fd scan, so results don't keep callbacks (and what they reference) alive.

scanFd now uses a single lazily opened PID fd per process for both the callbacks and the socket network namespace inspection.

The initially proposed SocketProcessesOf has been dropped: a callback cannot replace it for consumers without a discovery run, but its only use case (a deprecated Ghostwire API) does not warrant an additional export.

Tests

discover/discovery_fd_test.go: new specs (run as non-root and root) check that multiple callbacks get called for a temporary file's fd of our own process with the correct target, that PidFd() allows duplicating that fd, that nil callbacks are ignored, that callbacks alone leave the result unchanged (no socket map, no namespaces, callbacks cleared), and that no fds leak.

go test -race -p 1 ./discover/... passes as non-root and root.

@thediveo thediveo changed the title feat: discover.SocketProcessesOf for already discovered processes feat: discover.WithFdCallback for hooking into the fd scan Nov 6, 2026
@thediveo
thediveo force-pushed the feat/discover-socketprocessesof branch from 5e8691d to 968aa79 Compare November 6, 2026 08:46
Callers can now hook into the existing fd scan of discoveries, instead of
scanning the file descriptors of all processes a second time. Callbacks
get the process, fd number and link target, as well as an on-demand,
shared PID fd for duplicating the file descriptor.

Signed-off-by: thediveo <thediveo@gmx.eu>
@thediveo
thediveo force-pushed the feat/discover-socketprocessesof branch from 968aa79 to b278c93 Compare November 6, 2026 09:11
@thediveo
thediveo marked this pull request as ready for review November 6, 2026 09:16
@thediveo
thediveo merged commit 22555bf into develop Nov 6, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant