commit:     a2b958f6af818816b2207b3e66836ee8e7683f12
Author:     Sven Vermeulen <swift <AT> gentoo <DOT> org>
AuthorDate: Tue Nov 11 21:43:41 2014 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Tue Nov 11 22:27:49 2014 +0000
URL:        
http://sources.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=a2b958f6

_selinux.setexec: improve failure message (525726)

This converts "OSError: [Errno 22] Invalid argument" into a more
meaningful error message that is designed to guide users in the
right direction. Also, add an import that was missing for the "sys"
module, since it is referenced in the error paths of many functions
in this module.

X-Gentoo-Bug: 525726
X-Gentoo-Bug-URL: https://bugs.gentoo.org/show_bug.cgi?id=525726
Acked-by: Brian Dolbec <dolsen <AT> gentoo.org>
Acked-by: Zac Medico <zmedico <AT> gentoo.org>

---
 pym/portage/_selinux.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/pym/portage/_selinux.py b/pym/portage/_selinux.py
index 2a7194c..c5e8b2c 100644
--- a/pym/portage/_selinux.py
+++ b/pym/portage/_selinux.py
@@ -5,6 +5,7 @@
 # the whole _selinux module itself will be wrapped.
 import os
 import shutil
+import sys
 
 import portage
 from portage import _encodings
@@ -77,7 +78,18 @@ def settype(newtype):
 
 def setexec(ctx="\n"):
        ctx = _native_string(ctx, encoding=_encodings['content'], 
errors='strict')
-       if selinux.setexeccon(ctx) < 0:
+       rc = 0
+       try:
+               rc = selinux.setexeccon(ctx)
+       except OSError:
+               msg = _("Failed to set new SELinux execution context. " + \
+                       "Is your current SELinux context allowed to run 
Portage?")
+               if selinux.security_getenforce() == 1:
+                       raise OSError(msg)
+               else:
+                       portage.writemsg("!!! %s\n" % msg, noiselevel=-1)
+
+       if rc < 0:
                if sys.hexversion < 0x3000000:
                        ctx = _unicode_decode(ctx, 
encoding=_encodings['content'], errors='replace')
                if selinux.security_getenforce() == 1:

Reply via email to