Both \"netstat -p\" and \"lsof -n -i -P\" seems to readlinking all processes fd\'s, like stat /proc/*/fd/*
.
How to do it more efficiently?
My progra
Take a look at this answer, where various methods and programs that perform socket to process mappings are mentioned. You might also try several additional techniques to improve performance:
/proc/net
. This is done by the programs mentioned in the linked answer, but is only viable if your process lasts more than a few seconds.setsockopt()
. Take a look at these examples (they're pretty nasty but the best I could find).
/proc/PID/fd/FD
. A significant part of the overhead is the passing of the requests up and down the VFS layer, the numerous locking that occurs on all the kernel data structures that provide the information given, and the stringyfying and destringyfying at the kernel and your end respectively. You might adapt some of the code in this file to generate this information without many of the intermediate layers, in particular minimizing the locking to once per process, or simply once per scan of the entire data set you're after.My personal recommendation is to just brute force it for now, ideally traverse the processes in /proc
in reverse numerical order, as the more recent and interesting processes will have higher PIDs, and return as soon as you've located the results you're after. Doing this once per incoming connection is relatively cheap, it really depends on how performance critical your application is. You'll definitely find it worthwhile to bypass calling netstat
and directly parse the new connection from /proc/net/PROTO
, then locate the socket in /proc/PID/fd
. If all your traffic is localhost, just switch to Unix sockets and get the credentials directly. Writing a new syscall or proc module that dumps huge amounts of data regarding file descriptors I'd save for last.