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

Switch OSError to psutil.AccessDenied for net_if_stats failure #860

Closed
jakirkham opened this issue Jul 12, 2016 · 5 comments
Closed

Switch OSError to psutil.AccessDenied for net_if_stats failure #860

jakirkham opened this issue Jul 12, 2016 · 5 comments

Comments

@jakirkham
Copy link

jakirkham commented Jul 12, 2016

In some cases, a user may not have the proper permissions to run net_if_stats. If that is the case, an OSError is currently raised. It may be more appropriate to raise psutil.AccessDenied or at least it is a possibility worth considering ( #797 (comment) ). See an example of what this error looks like now below.

======================================================================
ERROR: test_ifconfig (test_misc.TestScripts)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/groups/dudman/home/kirkhamj/psutil/psutil/tests/test_misc.py", line 407, in test_ifconfig
    self.assert_stdout('ifconfig.py')
  File "/groups/dudman/home/kirkhamj/psutil/psutil/tests/test_misc.py", line 347, in assert_stdout
    out = sh(sys.executable + ' ' + exe).strip()
  File "/groups/dudman/home/kirkhamj/psutil/psutil/tests/__init__.py", line 257, in sh
    raise RuntimeError(stderr)
RuntimeError: Traceback (most recent call last):
  File "/groups/dudman/home/kirkhamj/psutil/scripts/ifconfig.py", line 80, in <module>
    main()
  File "/groups/dudman/home/kirkhamj/psutil/scripts/ifconfig.py", line 59, in main
    stats = psutil.net_if_stats()
  File "/groups/dudman/home/kirkhamj/psutil/psutil/__init__.py", line 1971, in net_if_stats
    return _psplatform.net_if_stats()
  File "/groups/dudman/home/kirkhamj/psutil/psutil/_pslinux.py", line 791, in net_if_stats
    isup, duplex, speed, mtu = cext.net_if_stats(name)
OSError: [Errno 1] Operation not permitted
@jakirkham
Copy link
Author

What is the reason for this exception in the first place? Does this amount to needing to run psutil with sudo?

@jakirkham
Copy link
Author

jakirkham commented Jul 12, 2016

As a slightly simpler example of this problem (as has been suggested to me), please see the following. This one is done with Python 2.7.12 in iPython 5.0.0. Though a PermissionError failure can still be gotten when using Python 3.5.2.

In [1]: import psutil

In [2]: psutil.net_if_stats()
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-06c5562e3b64> in <module>()
----> 1 psutil.net_if_stats()

/groups/dudman/home/kirkhamj/miniconda/envs/nanshenv2/lib/python2.7/site-packages/psutil/__init__.pyc in net_if_stats()
   1959      - mtu: the maximum transmission unit expressed in bytes.
   1960     """
-> 1961     return _psplatform.net_if_stats()
   1962 
   1963 

/groups/dudman/home/kirkhamj/miniconda/envs/nanshenv2/lib/python2.7/site-packages/psutil/_pslinux.pyc in net_if_stats()
    789     ret = {}
    790     for name in names:
--> 791         isup, duplex, speed, mtu = cext.net_if_stats(name)
    792         duplex = duplex_map[duplex]
    793         ret[name] = _common.snicstats(isup, duplex, speed, mtu)

OSError: [Errno 1] Operation not permitted

@giampaolo
Copy link
Owner

What is the reason for this exception in the first place?
Does this amount to needing to run psutil with sudo?

The reason is to have a single custom exception which can be catched across all different supported platforms and Python versions. On UNIX we may get OSError with EACCES or EPERM. On Windows we may get WindowsError with EACCES, EPERM or ERROR_ACCESS_DENIED, which is not even exposed in the errno module. Plus, on Python 3 this changed so that instead of OSError you'll get a new PermissionError. psutil.AccessDenied is there to abstract all that complexity/differences away so the user can simply do:

try: 
     p.exe()   # or whatever
except psutil.AccessDenied:
     ...

...and that's it.

The exact same reason applies to psutil.NoSuchProcess. Depending on the platform we may get OSError with ESRCH, ENOENT or whatever. In python 3 you'll get ProcessLookupError, psutil.NoSuchProcess abstracts that away in the same way.

@giampaolo
Copy link
Owner

giampaolo commented Jul 12, 2016

Does this amount to needing to run psutil with sudo?

Yes, there's nothing else you can do. The error comes from the kernel, which is preventing you to perform that specific operation 'cause you're a limited user, and using sudo / su is the only possible workaround.

@giampaolo
Copy link
Owner

Looking back at this, I think it is better to let OSError propagate. So far AccessDenied is only raised for methods of the Process class. Extending the same behavior to system-related APIs would be a first in psutil, and since I consider this a pretty rare corner case I don't want to introduce this "difference".
Closing this out.

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

2 participants