Re: Proper way to handle errors from python restorecon binding?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Brian J. Murrell" <brian@xxxxxxxxxxxxxxx> writes:

> It's not clear to me in which ways the restorecon() python binding can return an error/failure?
>
> Can it raise an exception or does it always simply report errors in it's return code? I.e. I need to do:
>
> rc = selinux.restorecon(path)
> if rc != 0:
>     [error handling]
>
> And/or do I need a try/except block around restorecon() as something it calls could raise an exception?
>
> I have seen reference to code that calls it as such:
>
> try:
>     selinux.restorecon(path)
> except selinux.SELinuxError:
>     [error handling]

selinux.restorecon() is a python swig wrapper around
selinux_restorecon_parallel() which is a generated libselinux python
binding:

https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinuxswig_python.i#L23
https://github.com/SELinuxProject/selinux/blob/main/libselinux/src/selinuxswig_python_exception.i#L1202

Based on this, you need to use try/except block around selinux.restorecon() if
you want to handle errors.

>>> import selinux
>>> selinux.restorecon('/root/ahoj')
lstat(/var/roothome/ahoj) failed: Permission denied
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.12/site-packages/selinux/__init__.py", line 93, in restorecon
    selinux_restorecon_parallel(os.path.expanduser(path), restorecon_flags, nthreads)
  File "/usr/lib64/python3.12/site-packages/selinux/__init__.py", line 451, in selinux_restorecon_parallel
    return _selinux.selinux_restorecon_parallel(pathname, restorecon_flags, nthreads)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied



--
_______________________________________________
selinux mailing list -- selinux@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to selinux-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/selinux@xxxxxxxxxxxxxxxxxxxxxxx
Do not reply to spam, report it: https://pagure.io/fedora-infrastructure/new_issue




[Index of Archives]     [Fedora Users]     [Fedora Desktop]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Campsites]     [KDE Users]     [Gnome Users]

  Powered by Linux