nikademus79 / psutil

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

Rewrite get_process_connections() in C for Windows #115

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Current implementation parses netstat.exe output:
http://code.google.com/p/psutil/source/browse/trunk/psutil/_psmswindows.py?spec=
svn670&r=669#176
It would be good for both speed and a more reliable error management to rewrite 
this in C.

Original issue reported on code.google.com by g.rodola on 11 Oct 2010 at 10:18

GoogleCodeExporter commented 8 years ago
Patch. This will only work on XP SP2 and above, so you may want to add a 
fallback method if needed.

Original comment by wj32...@gmail.com on 13 Oct 2010 at 6:05

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks a lot for this; I'll try it soon and let you know how it goes.

Original comment by g.rodola on 13 Oct 2010 at 6:19

GoogleCodeExporter commented 8 years ago
Committed in r679 and r680.

As for Windows < XP-SP2 and other incompatible versions  it's fine for me to 
fall back on parsing netstat output but I'm not sure on how to proceed.
I think one way to do this is to decide whether calling the C function at 
runtime from Python by previously detecting the Windows version although it's 
gonna be a bit tricky.

The support for GetExtended[Tcp/Udp]Table functions is listed here:
http://www.ureader.com/message/3207232.aspx
...and forces us to check 4 different Windows platforms including the SP 
version installed.

By using GetVersionEx():
http://msdn.microsoft.com/en-us/library/ms724451(v=VS.85).aspx
...we can detect both OS and SP versions.

Python exposes GetVersionEx() via sys.getwindowsversion() function which on my 
Windows 7 returns (6, 1, 7600, 2, '') and on Windows XP returns (5, 1, 2600, 2, 
'Service Pack 3'). 
As for an approximate list of name mappings we can take a look here:
http://www.msigeek.com/442/windows-os-version-numbers
This doesn't include Windows server versions though.

If this is gonna be too tricky it's fine for me remove support for incompatible 
Windows versions. 
Question is how to detect this at runtime and avoid to call the underlying C 
code.

Original comment by g.rodola on 14 Oct 2010 at 11:13

GoogleCodeExporter commented 8 years ago
I managed to install a Windows 2000 VM and implemented the logic described 
above in r685.
We can't rely on netstat.exe because the -o option is not available at all, in 
which case NotImplementedError is just raised:
http://articles.techrepublic.com.com/5100-10878_11-5630313.html

Original comment by g.rodola on 15 Oct 2010 at 9:31

GoogleCodeExporter commented 8 years ago
I'd like to include socket file descriptor number as well.
wj32, do you know if this is possible on Windows?

Original comment by g.rodola on 16 Oct 2010 at 7:38

GoogleCodeExporter commented 8 years ago
Windows doesn't have "file descriptor numbers" per se. Do you mean handle 
value? I don't think it's possible.

Original comment by wj32...@gmail.com on 16 Oct 2010 at 10:29

GoogleCodeExporter commented 8 years ago
I mean socket file descriptors as intended in the UNIX world which basically 
are integers that can be used in later function calls that operate on sockets, 
like select(), for example.
I know that Python implements this concept also for Windows sockets:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.fileno()
3
>>> 

Starting from that integer one might get a duplicate socket object and then 
perform actions against it, like closing the socket/connection:

>>> dup_socket = socket.fromfd(s.fileno(), socket.AF_INET, socket.SOCK_STREAM)
>>> dup_socket
<socket object, fd=4, family=2, type=1, protocol=0>
>>> dup_socket.close()
>>>

Original comment by g.rodola on 17 Oct 2010 at 8:53

GoogleCodeExporter commented 8 years ago
If this can't be done on Windows then it's ok for me to return -1 but I'd like 
to provide support on UNIX because closing process connections is something 
that might be useful.
I'm gonna modify Linux and BSD/OSX implementations so that they return the fd 
as well.

Original comment by g.rodola on 17 Oct 2010 at 9:06

GoogleCodeExporter commented 8 years ago
If you want to close network connections, you don't need any handles. A 
SetTcpEntry call will suffice.

Original comment by wj32...@gmail.com on 18 Oct 2010 at 2:14

GoogleCodeExporter commented 8 years ago
Unfortunately I see no way for SetTcpEntry to fit in the current API in a 
cross-platform way.
Closing this out as fixed and thanks again for the great work.

Original comment by g.rodola on 29 Oct 2010 at 5:36

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 13 Nov 2010 at 3:14

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Updated csets after the SVN -> Mercurial migration:
r679 == revision b9a8709f88f2
r685 == revision ec2059e2a06e

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