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

process.get_connections() requires root access under FreeBSD #370

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

process.get_connections() requires root access under FreeBSD #370

giampaolo opened this issue May 23, 2014 · 6 comments

Comments

@giampaolo
Copy link
Owner

From [email protected] on April 18, 2013 23:02:37

What steps will reproduce the problem?  
1. Try to use process.get_connections() to examine a process as non-root and 
get AccessDenied when kvm_openfiles() fails. 

What version of psutil are you using? What Python version?  
0.6.1 on Python 2.7.  Examining 0.7.0's source shows it to have the same issue. 

On what operating system? Is it 32bit or 64bit version?  
FreeBSD.  I have tested on 64-bit, but it is true of all platforms supported by FreeBSD. 

Please provide any additional information below.  
The current implementation of get_connections() uses direct KVM access to read 
in-kernel structures based on the code in fstat.  This requires root access so 
that it can open /dev/mem.  I have written an alternate implementation that 
uses unprivileged sysctls to fetch the data.  kinfo_getfile() gets most of the 
needed data except for the TCP state.  To handle TCP state I use a sysctl to 
fetch information about all the TCP connections in the system borrowing from 
the implementation of the netstat command.  I then walk this list of TCP 
connections each time the main loop encounters a TCP socket to fetch the TCP 
state.  Using sysctl's also avoids races that can occur with sockets coming and 
going and KVM access failing because a socket has been freed while 
get_connections() was running.

Attachment: psutil_sysctl.patch

Original issue: http://code.google.com/p/psutil/issues/detail?id=370

@giampaolo
Copy link
Owner Author

From [email protected] on April 23, 2013 11:05:19

The patch contains a memory leak.  It needs a 'free(tcplist)' added before the 
non-error return in get_connections().

@giampaolo
Copy link
Owner Author

From g.rodola on April 24, 2013 04:06:53

Wonderful! I cannot apply the patch though.
I tried "hg import psutil_sysctl.patch" and "patch -p0 < psutil_sysctl.patch" 
and both failed.
Did you use latest mercurial cset (1425:1445c0926b81)?

Labels: OpSys-FreeBSD

@giampaolo
Copy link
Owner Author

From g.rodola on April 30, 2013 12:33:47

@bsdknife: perhaps you can take a look at issue 373 , which code might kind of 
be related with process connections code?

@giampaolo
Copy link
Owner Author

From g.rodola on April 30, 2013 13:01:17

Ok, I managed to adapt the patch and committed it as revision 7eb9b863271e .
Works like a charm. Thanks a lot bsdknife, that was a wonderful piece of code. 
Ping me if you care about having your real name in the CREDITS file.

Status: FixedInHG

@giampaolo
Copy link
Owner Author

From g.rodola on April 30, 2013 13:02:38

Labels: Milestone-0.7.1

@giampaolo
Copy link
Owner Author

From g.rodola on May 03, 2013 08:43:00

Status: Fixed

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