giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.38k forks source link

[Termux] PermissionError: [Errno 13] Permission denied: '/proc/filesystems' #2191

Open babaric-dev opened 1 year ago

babaric-dev commented 1 year ago

Summary

Description

I ran this in the Python interactive console of PRoot Ubuntu:

>>> import psutil
>>> psutil.disk_partitions()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/psutil/__init__.py", line 2021, in disk_partitions
    ret = _psplatform.disk_partitions(all)
  File "/usr/lib/python3/dist-packages/psutil/_pslinux.py", line 1275, in disk_partitions
    with open_text("%s/filesystems" % procfs_path) as f:
  File "/usr/lib/python3/dist-packages/psutil/_common.py", line 725, in open_text
    return open(fname, "rt", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/filesystems'
>>>

It shows PermissionError.

True that we can't really access /proc/filesystems on Android. But at least handle the error with try except and return []? Better solution: use another way of getting partitions.

giampaolo commented 1 year ago

Notes to self: 1) we read /proc/filesystems only if the function is invoked as disk_partitions(all=False); if all=True, reading /proc/filesystems is superfluous, so it should not raise exception 2) find out how /proc/filesystems is written and provide a fallback function with hard-coded values in case of EPERM

@babaric-dev what happens if you call disk_partitions(all=True)? Does the function succeed? I want to make sure there are no other permission errors

babaric-dev commented 1 year ago

With or without all=True, it shows the exact same error message.

>>> psutil.disk_partitions(all=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/psutil/__init__.py", line 2021, in disk_partitions
    ret = _psplatform.disk_partitions(all)
  File "/usr/lib/python3/dist-packages/psutil/_pslinux.py", line 1275, in disk_partitions
    with open_text("%s/filesystems" % procfs_path) as f:
  File "/usr/lib/python3/dist-packages/psutil/_common.py", line 725, in open_text
    return open(fname, "rt", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/filesystems'
>>> psutil.disk_partitions(all=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/psutil/__init__.py", line 2021, in disk_partitions
    ret = _psplatform.disk_partitions(all)
  File "/usr/lib/python3/dist-packages/psutil/_pslinux.py", line 1275, in disk_partitions
    with open_text("%s/filesystems" % procfs_path) as f:
  File "/usr/lib/python3/dist-packages/psutil/_common.py", line 725, in open_text
    return open(fname, "rt", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/filesystems'
>>>

As I said, /proc/filesystems is unaccessible in Android. cat or stating it shows Permission Denied too.

giampaolo commented 1 year ago

I just pushed 2da99502. Can you try installing psutil from latest master branch and retry running disk_partitions(all=True)? Run:

python3 -m pip install --upgrade git+https://github.com/giampaolo/psutil.git@master
python3 -c "import psutil; print(psutil.disk_partitions(all=True))"
babaric-dev commented 1 year ago

Works! But after updating to git version, I see it can't read /proc/stat. Another error. I will open an issue for this later

babaric-dev commented 1 year ago

Why can't we close this?

giampaolo commented 1 year ago

Because of:

2) find out how /proc/filesystems is written and provide a fallback function with hard-coded values in case of EPERM

Blackwood416 commented 1 year ago

For proot-distro, you can fix this issue by following these steps. First, download fake /proc files(proc.tar.xz) from here. And then extract file named filesystems to your termux home directory. Last, append this --bind ~/filesystems:/proc/filesystems to the end of your proot-login line.