nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Get files opened by a process #79

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Considering that there's nothing like lsof implemented in python yet, it
would be very cool if psutil could provide a method to retrieve all the
files opened by a process as a list of path names.

Example:

>>> import psutil, os
>>> p = psutil.Process(os.getpid())
>>> p.get_open_files()
['/etc/samba.conf', '/home/user/file.txt', ...]

I've found a working code for Windows, thanks to wj32 (we should really
thank the guy):
http://forum.sysinternals.com/forum_posts.asp?TID=18892
...which I managed to adapt to psutil in some way.

This is still not fully reliable as it lacks the routine to convert
"\Device\HarddiskVolume1\" into actual drive letters and a proper exception
handling, but I'd say it looks promising.

Original issue reported on code.google.com by billiej...@gmail.com on 8 Mar 2010 at 7:49

GoogleCodeExporter commented 8 years ago
See GetLogicalDriveStrings() to get a list of all the drive names: 

    http://msdn.microsoft.com/en-us/library/aa364975%28VS.85%29.aspx

And QueryDosDevice() for converting a dos path to an NT device name: 

    http://msdn.microsoft.com/en-us/library/aa365461%28VS.85%29.aspx

There's also code samples in MSDN for converting an NT device name to a drive 
letter: 

    http://msdn.microsoft.com/en-us/library/aa366789%28VS.85%29.aspx

I would be careful about copying any code directly since it doesn't look like
particularly elegant code but it gives us a starting point at least.

Original comment by jlo...@gmail.com on 9 Mar 2010 at 4:43

GoogleCodeExporter commented 8 years ago
Added Linux implementation as r538.

Original comment by billiej...@gmail.com on 13 Mar 2010 at 12:27

GoogleCodeExporter commented 8 years ago
Exposed QueryDosDevice() as r543.
The raw drive name conversion is now made.

Original comment by billiej...@gmail.com on 13 Mar 2010 at 6:49

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 22 Jun 2010 at 7:37

GoogleCodeExporter commented 8 years ago
Because of the extreme complexity deriving from doing this in pure C in two 
different ways for both OSX and BSD, r591 implements this functionality by 
parsing lsof command output.
If lsof is not installed NotImplementedError exception is raised.

Jay could you try this out on OSX?

Original comment by g.rodola on 7 Jul 2010 at 11:40

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 19 Jul 2010 at 8:18

GoogleCodeExporter commented 8 years ago
Reopening as there's a problem on Windows.
The script below hangs when svnchost.exe process is encountered.

import psutil

for p in psutil.process_iter():
    print(p)
    print(p.get_open_files())

Original comment by g.rodola on 20 Jul 2010 at 11:47

GoogleCodeExporter commented 8 years ago
The culprit seems to be NtQueryObject().
I found different sources describing this problem, like:
http://www.codeproject.com/Articles/35202/GetFinalPathNameByHandle-API-Hangs.asp
x

Here is provided a solution which runs NtQueryObject() into a separate thread 
with a timeout of 1 second:
http://forum.sysinternals.com/discussion-howto-enumerate-handles_topic19403_page
2.html

Original comment by g.rodola on 5 Aug 2010 at 4:05

GoogleCodeExporter commented 8 years ago
http://forum.sysinternals.com/topic19403_page7.html

> The thread hack doesn't work on XP (SP2, maybe SP3) because it has a bug 
where you 
> cannot terminate the hanging thread. 
> Also in Windows 7 NtQueryObject has been completely fixed - no hangs at all. 
In 
> Vista the bug is still there but you can at least terminate the hanging 
thread.

Original comment by g.rodola on 5 Aug 2010 at 4:18

GoogleCodeExporter commented 8 years ago
Process explorer provides a hack for this:
http://www.koders.com/csharp/fid7DE6B7813A01C2AA46DAAC5C2C9E49999E7E0E34.aspx?s=
mdef:search#L259

Original comment by g.rodola on 5 Aug 2010 at 4:45

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Added a hack in r627 which skip handlers having certain access codes.
This seems to be enough to fix the problem and pass the test above which was 
commented out in the test suite.
I still have to figure out whether we can avoid this on Windows 7 where 
NtQueryObject() is supposed to have been fixed.

Original comment by g.rodola on 5 Aug 2010 at 7:54

GoogleCodeExporter commented 8 years ago
Tried out on Windows 7 and the same problem occurs.
The only thing which has been fixed is that the process, despite freezed, can 
be killed via taskmgr, while on Windows XP I was forced to reboot the computer.
Closing this out as fixed.

Original comment by g.rodola on 5 Aug 2010 at 7:57

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Updated csets after the SVN -> Mercurial migration:
r538 == revision 99e11dcca77e
r543 == revision 1ebe83c24b7c
r591 == revision 6d3522006aed
r627 == revision a182f3b140ae

Original comment by g.rodola on 2 Mar 2013 at 11:52