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

OSX - AccessDenied for memory_info() / proc_pidinfo() #883

Closed
ckaran opened this issue Sep 6, 2016 · 9 comments
Closed

OSX - AccessDenied for memory_info() / proc_pidinfo() #883

ckaran opened this issue Sep 6, 2016 · 9 comments
Labels

Comments

@ckaran
Copy link

ckaran commented Sep 6, 2016

I'm attempting to use psutil to determine when it's reasonable to spawn extra simulators for my work (too many simulations at once == using swap which slows things down). However, I'm getting a permissions error on the child process.

uname -a = Darwin XXX.local 13.4.0 Darwin Kernel Version 13.4.0: Mon Jan 11 18:17:34 PST 2016; root:xnu-2422.115.15~1/RELEASE_X86_64 x86_64

OS X 10.9.5

Python 3.4.5 (default, Jun 27 2016, 12:04:15) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information

psutil.__version__ == 4.3.1

Here are the steps to reproduce:

  • Copy the code below to a file named sleeper.c
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    sleep(1000);
    return EXIT_SUCCESS;
}
  • Compile it using clang -Wall -Wextra -pedantic -std=c11 sleeper.c, and let the output file be named a.out.
  • Copy the code below to a file named ps_test.py in the same directory as sleeper.c.
import os
import os.path
import psutil

path = os.path.realpath(os.path.join(os.path.curdir, 'a.out'))
child = os.spawnl(os.P_NOWAIT, path)
process = psutil.Process(pid=child)
print(process.memory_info())
  • Run the python script as python ps_test.py.

Doing the above will result in the following:

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psutil/_psosx.py", line 244, in wrapper
    return fun(self, *args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psutil/_psosx.py", line 317, in memory_info
    rss, vms, pfaults, pageins = cext.proc_memory_info(self.pid)
PermissionError: [Errno 13] Permission denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ps_test.py", line 8, in <module>
    print(process.memory_info())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psutil/__init__.py", line 998, in memory_info
    return self._proc.memory_info()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/psutil/_psosx.py", line 252, in wrapper
    raise AccessDenied(self.pid, self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=94444)
@giampaolo
Copy link
Owner

Note to self: for getting memory_info() proc_pidinfo is used. See if psutil_get_kinfo_proc can be used instead.

@giampaolo
Copy link
Owner

I checked. Unfortunately it seems we're stuck with proc_pidinfo.

@ckaran
Copy link
Author

ckaran commented Oct 19, 2016

@giampaolo Does that mean that there is no possible fix?

@giampaolo
Copy link
Owner

giampaolo commented Oct 25, 2016

Apparently no. AFAIK proc_pidinfo(PROC_PIDTASKINFO) (http://opensource.apple.com//source/xnu/xnu-1456.1.26/bsd/kern/proc_info.c) is the only available syscall on OSX which lets you query process memory for all PIDs, which is stupid.
You are fine as long as you query processes owned by your user, but for all other processes you'll get AccessDenied.

@giampaolo giampaolo changed the title Permission denied error on OS X OSX - AccessDenied for memory_info() / proc_pidinfo() Oct 25, 2016
@ckaran
Copy link
Author

ckaran commented Oct 25, 2016

That is seriously annoying. And yet there has to be a way; top doesn't need any special rights to figure out how much memory is in use. I found http://stackoverflow.com/questions/14789672/why-does-host-statistics64-return-inconsistent-results which suggests the start of a method. Would it work? (By the way, the code has some minor typos in it that you'll need to correct before it will compile).

Thanks,
Cem Karan

@giampaolo
Copy link
Owner

Yes, it is seriously annoying. top manages to do that because of the setuid flag:

osx-elcaptain:~ vagrant$ ls -l /usr/bin/top
-r-sr-xr-x  1 root  wheel  87952 12 Mar  2016 /usr/bin/top

As for your link, unfortunately that retrieves system memory, not per-process memory.

@ckaran
Copy link
Author

ckaran commented Oct 26, 2016

Yeah, I should have looked into top more before I made that comment.

I know that retrieves system memory and not per-process memory, but that might be the best we can do. The only other method I can think of is something to do with either host_statistics() or host_statistics64(), but so far I haven't been able to get them to work, and even then they only do host statistics, not per-process statistics.

And... I just remembered that Apple put in SIP, which may interfere with a LOT of things; it certainly broke some types of debugging (http://internals.exposed/blog/dtrace-vs-sip.html). Oh well, I guess I'll have to stick to Linux, and assume that OS X is unusable for my purposes.

@giampaolo
Copy link
Owner

I know that retrieves system memory and not per-process memory, but that might be the best we can do.

I don't understand. psutil.virtual_memory() (aka system memory) is fine, so we don't need to do anything. The problem is with Process.memory_info() for PIDs not owned by current unprivileged user.
psutil already uses host_statistics() for retrieving system mem (psutil.virtual_memory()).

@ckaran
Copy link
Author

ckaran commented Oct 26, 2016

OK, if that works, then I'll be fine. Thank you.

altendky added a commit to ericaltendorf/plotman that referenced this issue Apr 7, 2021
Fixes #91

Help on macOS where I think you get limited access to other user's processes.

giampaolo/psutil#883 (comment)
hqc-destroy added a commit to hqc-destroy/getiwn that referenced this issue Jun 2, 2021
Fixes #91

Help on macOS where I think you get limited access to other user's processes.

giampaolo/psutil#883 (comment)
wvolz added a commit to wvolz/not1mm that referenced this issue Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants