Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get files opened by a process #79

Closed
giampaolo opened this issue May 23, 2014 · 13 comments
Closed

Get files opened by a process #79

giampaolo opened this issue May 23, 2014 · 13 comments

Comments

@giampaolo
Copy link
Owner

From [email protected] on March 08, 2010 20:49:57

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: http://code.google.com/p/psutil/issues/detail?id=79

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From [email protected] on March 08, 2010 20:43:52

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.

Status: Accepted

@giampaolo
Copy link
Owner Author

From [email protected] on March 12, 2010 16:27:57

Added Linux implementation as r538 .

Labels: -Progress-0in4 Progress-1in4

@giampaolo
Copy link
Owner Author

From [email protected] on March 13, 2010 10:49:03

Exposed QueryDosDevice() as r543 .
The raw drive name conversion is now made.

Status: Started
Labels: -Progress-1in4 Progress-2in4

@giampaolo
Copy link
Owner Author

From g.rodola on June 22, 2010 12:37:55

Labels: -Milestone-0.1.4 Milestone-0.2.0

@giampaolo
Copy link
Owner Author

From g.rodola on July 07, 2010 16:40:50

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?

Owner: g.rodola
Labels: -Progress-2in4 Progress-4in4

@giampaolo
Copy link
Owner Author

From g.rodola on July 19, 2010 13:18:57

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on July 20, 2010 16:47:36

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())

Status: ReOpened

@giampaolo
Copy link
Owner Author

From g.rodola on August 05, 2010 09:05:51

The culprit seems to be NtQueryObject().
I found different sources describing this problem, like: 
http://www.codeproject.com/Articles/35202/GetFinalPathNameByHandle-API-Hangs.aspx
 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_page2.html

@giampaolo
Copy link
Owner Author

From g.rodola on August 05, 2010 09:18:58

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.

@giampaolo
Copy link
Owner Author

From g.rodola on August 05, 2010 09:45:05

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

@giampaolo
Copy link
Owner Author

From g.rodola on August 05, 2010 12:54:54

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.

@giampaolo
Copy link
Owner Author

From g.rodola on August 05, 2010 12:57:27

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.

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:52:07

Updated csets after the SVN -> Mercurial migration: r538 == revision 
99e11dcca77e r543 == revision 1ebe83c24b7c r591 == revision 6d3522006aed r627 == revision a182f3b140ae

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant