Repository navigation
feat: discover.WithFdCallback for hooking into the fd scan - #106
Merged
Merged
Conversation
thediveo
force-pushed
the
feat/discover-socketprocessesof
branch
from
November 6, 2026 08:46
5e8691d to
968aa79
Compare
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
force-pushed
the
feat/discover-socketprocessesof
branch
from
November 6, 2026 09:11
968aa79 to
b278c93
Compare
thediveo
marked this pull request as ready for review
November 6, 2026 09:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withFromFds()orWithSocketProcesses()already walks all fds of all processes.Change
New discovery option
WithFdCallback(fn FdCallback):fngets called for each open fd of each discovered process, with anFdEntrycarrying theProcess, theFdnumber, and the linkTarget(such assocket:[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 withpidfd_getfd. It must neither be closed nor retained beyond the callback.nilcallbacks are ignored.SocketProcessMapand no namespaces unlessWithSocketProcesses()orFromFds()are also given.DiscoverOpts.fdCallbacksthat gets cleared after the fd scan, so results don't keep callbacks (and what they reference) alive.scanFdnow uses a single lazily opened PID fd per process for both the callbacks and the socket network namespace inspection.The initially proposed
SocketProcessesOfhas 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, thatPidFd()allows duplicating that fd, thatnilcallbacks 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.