nikademus79 / psutil

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

OSX: psutil.error.AccessDenied on p.get_threads() #215

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Computer (uname -a): Darwin macbook-pro.local 9.8.0 Darwin Kernel Version 
9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386
Using python2.7.2 and psutil-0.3.0, the following script:

#!/usr/bin/env python
import psutil
import subprocess

sp = subprocess.Popen(['sleep', '30'])
p = psutil.Process(sp.pid)
print p.get_threads()

produce the error:
Traceback (most recent call last):
  File "./test-psutil.py", line 7, in <module>
    print p.get_threads()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil-0.3.0-py2.7-macosx-10.5-i386.egg/psutil/__init__.py", line 297, in get_threads
    return self._platform_impl.get_process_threads()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psutil-0.3.0-py2.7-macosx-10.5-i386.egg/psutil/_psosx.py", line 90, in wrapper
    raise AccessDenied(self.pid, self._process_name)
psutil.error.AccessDenied: (pid=8455)

exepted results is a array of threads.

Pierre

Original issue reported on code.google.com by pierre.g...@gmail.com on 30 Sep 2011 at 2:47

GoogleCodeExporter commented 8 years ago
Hi Pierre, 

If you run this test script as root, what are the results? Can you post the 
contents of your test_psutil.py just for clarity's sake? 

The OS X security model is very strict and only root can read information from 
another process, unless the application is signed and specifically given access 
to another app. That applies even to other processes opened by your user 
account; you still need to be root to read the information. The unix utils like 
ps and top on OS X are setuid root in order to access this information. 

-Jay

Original comment by jlo...@gmail.com on 30 Sep 2011 at 2:50

GoogleCodeExporter commented 8 years ago
$ cat /tmp/test-psutil.py 
#!/usr/bin/env python
import psutil
import subprocess

sp = subprocess.Popen(['sleep', '30'])
p = psutil.Process(sp.pid)
print p.get_threads()
$ sudo /tmp/test-psutil.py 
[thread(id=1, user_time=0.000625, system_time=0.001875)]
$ 

( tested code from svn also and got same error but it sounds predictable from 
your explicationf of OS X security model.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/psutil-read-only/install//lib/python2.7/site-packages/psutil-0.3.1-py2.7-macosx-10.5-i386.egg/psutil/__init__.py", line 286, in get_threads
    return self._platform_impl.get_process_threads()
  File "/tmp/psutil-read-only/install//lib/python2.7/site-packages/psutil-0.3.1-py2.7-macosx-10.5-i386.egg/psutil/_psosx.py", line 92, in wrapper
    raise AccessDenied(self.pid, self._process_name)
psutil.error.AccessDenied: (pid=9124)
)

Original comment by pierre.g...@gmail.com on 30 Sep 2011 at 3:17

GoogleCodeExporter commented 8 years ago
Yeah, this looks like expected results for the OS X security model. In general 
psutil is only really useful on OS X when run as root, or when the process 
you're querying is the current process. There is some information that is 
obtainable for other processes, but only basic things like PID and name, 
everything else that uses the OS X specific interfaces is limited by this 
security restriction. 

I'll  close this defect out since it's just a feature of OS X. 

Original comment by jlo...@gmail.com on 30 Sep 2011 at 3:26