in reply to How to detect if file is in use?

How does fuser do it?

Replies are listed 'Best First'.
Re^2: How to detect if file is in use?
by gellyfish (Monsignor) on Mar 17, 2005 at 11:45 UTC

    I guessed when I wrote Linux::Fuser and looked in /proc/*/fd and found all the file descriptors that had the same inode as the file I was interested in. Having just looked the source of fuser it appears it was a good guess ;-)

    /J\

      Is this a proper assumption? From what I know, inodes are unique based on filesystem, not on a system-wide basis. That is to say that you could have two file systems with two different files that have the same inode number.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        Yes you are correct and I over-simplified, Linux::Fuser indeed does compare the device numbers of the file as well as the inode number:

        if (my @statinfo = stat $fd) { if (($dev == $statinfo[0]) && ($ino == $statinfo[1]) ) {
        You had me worried there for a second ;-)

        /J\

        I don't think gellyfish said anything about inodes.
        If you look into /proc/*/fd/, you will find symlinks to all the files opened by the programs. By using readlink(), you can find out the path of those files (not the inodes).
Re^2: How to detect if file is in use?
by blazar (Canon) on Mar 17, 2005 at 11:40 UTC
    How does fuser do it?
    I wish I knew. The manpage mentions /proc, maybe that could be relevant, but then if I knew I wouldn't have asked. And no, I've not checked the src, as it is C, I suppose, and I don't know much C any more...