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 条评论


您的电子邮箱地址不会被公开。 必填项已用*标注