{"id":3410,"date":"2017-07-14T22:19:06","date_gmt":"2017-07-14T14:19:06","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3410"},"modified":"2017-07-14T22:19:06","modified_gmt":"2017-07-14T14:19:06","slug":"linux%e4%b8%8b%e9%80%9a%e8%bf%87inode%e6%89%bepid","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3410.html","title":{"rendered":"Linux\u4e0b\u901a\u8fc7inode\u627epid"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"Linux\u4e0b\u901a\u8fc7inode\u627epid-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6211\u77e5\u9053\u5728Linux\u4e0b\u5982\u4f55\u901a\u8fc7pid\u627e\u5230\u5bf9\u5e94\u7684inode\u4fe1\u606f\uff0c\u4f46\u662f\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\u5c31\u662f\u901a\u8fc7inode\u627e\u5230\u5bf9\u5e94\u7684pid\u3002<\/p>\n<h4 id=\"Linux\u4e0b\u901a\u8fc7inode\u627epid-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"Linux\u4e0b\u901a\u8fc7inode\u627epid-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p><strong>In general, to find what&#8217;s at the other end of a socket, you need to\u00a0<span style=\"color: #0000ff;\">iterate through \/proc<\/span>.<\/strong>\u00a0This can of course mean letting an existing tool such as netstat, lsof or ss do it for you.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>The reason for this is that a socket can be opened by different processes.<\/strong><\/span>\u00a0Once a process has opened a socket, it can fork, and the children can change their privileges and switch to different namespaces. It&#8217;s also possible to pass file descriptors from a process to an unrelated process, using anciliary data on a unix socket.\u00a0<span style=\"color: #ff0000;\">So arbitrary processes can have the same socket open.<\/span>\u00a0There is no such thing as \u201cthe PID of a socket\u201d, only \u201cthe PIDs of a socket\u201d.<\/p>\n<p>While this doesn&#8217;t prove that there&#8217;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&#8217;t get to know when files are opened by processes running as a different user. Access through \/proc enforces security via permissions, and I&#8217;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&#8217;d expect someone to use it.\u00a0<span style=\"color: #0000ff;\"><strong>So I&#8217;m pretty sure that there&#8217;s no other way.<\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"Linux\u4e0b\u901a\u8fc7inode\u627epid-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<p><a class=\"external-link\" href=\"https:\/\/unix.stackexchange.com\/questions\/302152\/is-there-a-way-to-identify-the-pid-or-cgroup-of-a-socket-without-iterating-thr\" rel=\"nofollow\">https:\/\/unix.stackexchange.com\/questions\/302152\/is-there-a-way-to-identify-the-pid-or-cgroup-of-a-socket-without-iterating-thr<\/a><\/p>\n<p><a class=\"external-link\" href=\"https:\/\/stackoverflow.com\/questions\/3319521\/how-can-i-match-each-proc-net-tcp-entry-to-each-opened-socket\/\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/3319521\/how-can-i-match-each-proc-net-tcp-entry-to-each-opened-socket\/<\/a><\/p>\n<p><a class=\"external-link\" href=\"https:\/\/stackoverflow.com\/questions\/14667215\/finding-a-process-id-given-a-socket-and-inode-in-python-3\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/14667215\/finding-a-process-id-given-a-socket-and-inode-in-python-3<\/a><\/p>\n<p><a class=\"external-link\" href=\"https:\/\/emailcommand.wordpress.com\/linux\/how-does-inode-in-procnettcp-related-with-the-procpidfdnum-socketnum\/\" rel=\"nofollow\">https:\/\/emailcommand.wordpress.com\/linux\/how-does-inode-in-procnettcp-related-with-the-procpidfdnum-socketnum\/<\/a><\/p>\n<p><a class=\"external-link\" href=\"https:\/\/serverfault.com\/questions\/707490\/how-to-determine-which-processes-have-most-inodes-opened\" rel=\"nofollow\">https:\/\/serverfault.com\/questions\/707490\/how-to-determine-which-processes-have-most-inodes-opened<\/a>\u00a0#\u5982\u4f55\u5224\u65ad\u54ea\u4e2a\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6700\u591a\uff1f<\/p>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6211\u77e5\u9053\u5728Linux\u4e0b\u5982\u4f55\u901a\u8fc7pid\u627e\u5230\u5bf9\u5e94\u7684inode\u4fe1\u606f\uff0c\u4f46\u662f\u73b0\u5728\u6709\u4e00\u4e2a\u9700\u6c42\u5c31\u662f\u901a\u8fc7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[589,758,30,646],"class_list":["post-3410","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-proc","tag-inode","tag-linux","tag-pid"],"views":7732,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3410"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3410\/revisions"}],"predecessor-version":[{"id":3411,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3410\/revisions\/3411"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}