In general, to find what’s at the other end of a socket, you need to iterate through /proc. This can of course mean letting an existing tool such as netstat, lsof or ss do it for you.
The reason for this is that a socket can be opened by different processes. Once a process has opened a socket, it can fork, and the children can change their privileges and switch to different namespaces. It’s also possible to pass file descriptors from a process to an unrelated process, using anciliary data on a unix socket. So arbitrary processes can have the same socket open. There is no such thing as “the PID of a socket”, only “the PIDs of a socket”.
While this doesn’t prove that there’s no way to enumerate the PIDs given the socket, it would be very difficult to design such an interface, especially when you consider that non-root users don’t get to know when files are opened by processes running as a different user. Access through /proc enforces security via permissions, and I’m not aware of any other interface to get the same data. Also, fuser, lsof, netstat and ss all enumerate /proc; if there was another interface, I’d expect someone to use it. So I’m pretty sure that there’s no other way.
《“Linux下通过inode找pid”》 有 4 条评论
这几个示例，帮你深入理解 cgroup 中的 cpuset