Figuring out what process has a file locked

Figuring out what process has a file locked

Post by od.. » Fri, 17 Jan 2003 13:42:03



Sometimes you want to unmount a partition but you can't because you
get the classic "device or resource busy".  I don't know of any way
to figure out WHAT process has the device busy (so I could kill it
for example).  The closest I can come is to look in /proc/locks to
see which files are locked, however this will only show the inodes of
those files.  How do I find the corresponding file names (find -inum
works but is SLOW)?  Will this even give me the info I want?  For
example suppose I've got a shell open with a current working
directory of /home/foobar.  It doesn't appear that a lock for this
directory appears in /proc/locks, but I still won't be able to
unmount the file system containing /home/foobar.  Other possabilities
would include digging through the process entries in /proc and
finding what files each process has open, etc. but this seems much
too hard.  Is there an easy way to get this info?

Thanks.

 
 
 

Figuring out what process has a file locked

Post by ynotsso » Fri, 17 Jan 2003 16:30:47



> Sometimes you want to unmount a partition but you can't because you
> get the classic "device or resource busy".  I don't know of any way
> to figure out WHAT process has the device busy (so I could kill it
> for example).  The closest I can come is to look in /proc/locks to
> see which files are locked, however this will only show the inodes of
> those files.  How do I find the corresponding file names (find -inum
> works but is SLOW)?  Will this even give me the info I want?  For
> example suppose I've got a shell open with a current working
> directory of /home/foobar.  It doesn't appear that a lock for this
> directory appears in /proc/locks, but I still won't be able to
> unmount the file system containing /home/foobar.  Other possabilities
> would include digging through the process entries in /proc and
> finding what files each process has open, etc. but this seems much
> too hard.  Is there an easy way to get this info?

Check to see what pid is using the filesystem:

     #   ls -l /proc/[0-9]*/cwd | grep home

Then use :

     #   lsof -g pid

to see the actual process involved. lsof won't show a shell's pid if a
user is in that directory, but the ownership of the symbolic link
returned by the above command will decide which particular user is
involved, and can be the starting point for the next step in your
investigation:

      #  lsof -u {user}

               tony

--
use hotmail.com for any email replies

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 
 
 

Figuring out what process has a file locked

Post by Oliver Dai » Fri, 17 Jan 2003 23:02:49


Quote:> Check to see what pid is using the filesystem:

>      #   ls -l /proc/[0-9]*/cwd | grep home

> Then use :

>      #   lsof -g pid

> to see the actual process involved. lsof won't show a shell's pid if
> a user is in that directory, but the ownership of the symbolic link
> returned by the above command will decide which particular user is
> involved, and can be the starting point for the next step in your
> investigation:

>       #  lsof -u {user}

>                tony

Thanks.  I didn't know the lsof command.  After reading the man page
for it I think its even easier than you said.  If I want all the open
files (including cwd's) on a mounted volume (say /mnt/foobar) then a
simple "lsof /mnt/foobar" does the trick (including, at least on my
machine, the PID of shells using that as cwd).  Just what I was
looking for...
 
 
 

Figuring out what process has a file locked

Post by Vic Abe » Sat, 18 Jan 2003 02:41:39



> ...

> Then use :

>      #   lsof -g pid

That should be "-p".

Quote:> to see the actual process involved. lsof won't show a shell's pid if a
> user is in that directory ...

I don't think that is correct.  There's also a quicker way to do it
by specifying the mounted-on directory path to lsof.  Here's an
abbreviated example that shows lsof finding my shell in the /home
file system.

$ lsof -a -u abe /home
COMMAND   PID USER   FD   TYPE DEVICE SIZE    NODE NAME
ksh     22953  abe  cwd    DIR   58,0 4096 1999250 /home/abe/src/lsof4

The above example was run on:

$ uname -a
Linux xxx 2.4.18-686 #1 Sun Apr 14 11:32:47 EST 2002 i686 unknown

So I'm not sure what Tony means when he says, "lsof won't show
a shell's pid if a user is in that directory ..."  It might have
something to do with the options compiled-in to lsof, such as
HASSECURITY.  (The "-v" option of the latest lsof, revision 4.66,
will show that.)

Vic Abell, lsof author

 
 
 

Figuring out what process has a file locked

Post by ynotsso » Sat, 18 Jan 2003 03:03:45


[...]

Quote:> So I'm not sure what Tony means when he says, "lsof won't show
> a shell's pid if a user is in that directory ..."  It might have
> something to do with the options compiled-in to lsof, such as
> HASSECURITY.  (The "-v" option of the latest lsof, revision 4.66,
> will show that.)

Thank you for the corrections, and for a very valuable tool.

          tony

--
use hotmail.com for any email replies

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----