Let /var/run/mysqld/mysqld.sock/ be an open socket. I can stat it to get inode of the file (here 1200)

# stat /var/run/mysqld/mysqld.sock
  File: /var/run/mysqld/mysqld.sock
  Size: 0               Blocks: 0          IO Block: 4096   socket
Device: 17h/23d Inode: 1200        Links: 1
Access: (0777/srwxrwxrwx)  Uid: (  109/   mysql)   Gid: (  121/   mysql)
Access: 2020-08-24 07:06:50.716419994 +0200
Modify: 2020-07-20 14:51:30.892060665 +0200
Change: 2020-07-20 14:51:30.892060665 +0200

However, what I want is not the disk filesystem inode (ext4 or similar), but OS socket inode as reported in /proc/net/unix (4517115):

# grep mysqld.sock /proc/net/unix
ffff9efa85f1bc00: 00000002 00000000 00010000 0001 01 4517115 /var/run/mysqld/mysqld.sock

The question is, given filesystem socket inode (1200), how can I retrieve OS socket inode (4517115)? Unfortunately, I can rely on neither ss, lsof, or even /proc/net/unix as those sources are unreliable when the socket file has been created (for example) by a container:

  • ss, lsof and others often do not list these sockets at all (maybe those tools rely on /proc/net/unix?),
  • /proc/[pid]/net/unix is unreliable as it may be impossible (or at least hard) to match a file mapped from host to a different path in container. Furthermore, /proc/net/unix itself might not list these sockets.
  • if the problem is related to using namespaces, the solution must rely on namespaces. Why would an other tool work better than ss in the wrong (namespace) environment? – A.B Aug 25 '20 at 13:52
  • 1
    You can't. Everytime you listen() on a unix socket, you get a different inode. Everytime you (successfully) connect to a unix socket, you get a different inode. Everytime you accept() a connection on a unix socket, you get a different inode. Neither is related to the on-disk inode. On Linux, you can get them from the target of /proc/<pid>/fd (the number from socket:[num]). – pizdelect Aug 25 '20 at 13:54
  • 2
    ss and other tools don't use /proc/net -> /proc/self/net they get their info via unix_diag netlink sockets (a newer interface). Use /proc/pid-from-other-net-namespace/net/unix or run ss, etc via nsenter if you want to gather info from other containers. – pizdelect Aug 25 '20 at 14:01
  • @pizdelect Yeah, I'm currently using nsenter + /proc/self/net/unix + stat on the file listed there to do that. It's slow though as I do that for every PID in the host /proc/. – matt Aug 25 '20 at 14:05
  • 1
    You don't have to do it for every pid; only for every network namespace. Related – pizdelect Aug 25 '20 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.