Linux – file corresponds to /proc/locks

fileslinuxlock

$ cat /proc/locks
1: POSIX ADVISORY  WRITE 458 03:07:133880 0 EOF
2: FLOCK ADVISORY  WRITE 404 03:07:133879 0 EOF

The fields are: ordinal number(1), type(2), mode(3), type(4), pid(5), maj:min:inode(6) start(7) end(8).

Question: how to find the corresponding file is being locked?

Best Answer

sudo find -L /proc/458/fd -maxdepth 1 -inum 133880 -print -exec readlink {} \;

To get all of them:

while IFS=': ' read x x x x p x x i x; do
  sudo find -L "/proc/$p/fd" -maxdepth 1 -inum "$i" -exec readlink {} \; -quit
done < /proc/locks

Sometimes, the process whose pid is referenced in /proc/lock will have died. You can change the "/proc/$p/fd" above to /proc/*/fd to look for them among all processes fds.

(note that it is an approximation as we're only checking the inode number, not the device number, but chances that we pick the wrong file (same inum on a different fs) are very slim).

Related Question