More fine-grained exception handling when setting UID/GID
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 16 Feb 2015 13:22:16 +0000 (15:22 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Mon, 16 Feb 2015 14:53:10 +0000 (16:53 +0200)
Change-Id: Iabce1dbd5b9e5f533ec454ac31a49a5c9e336dc2
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
obs_service_gbp_utils/__init__.py

index adab9f601cf877f1c4dc695b43970412cc836a1f..5e64a0e763256bd48ce29c0ce33b072e061cf3d5 100644 (file)
@@ -54,17 +54,22 @@ class GbpChildBTError(Exception):
 def _demoted_child_call(uid, gid, ret_data_q, func):
     """Call a function/method with different uid/gid"""
     # Set UID and GID
-    try:
-        if gid and gid > 0:
+    if gid and gid > 0:
+        try:
             os.setresgid(gid, gid, gid)
-        if uid and uid > 0:
+        except OSError as err:
+            ret_data_q.put(GbpServiceError("Setting GID (%s) failed: %s" %
+                                           (gid, err)))
+            sys.exit(_RET_FORK_ERR)
+    if uid and uid > 0:
+        try:
             os.setresuid(uid, uid, uid)
             # Set environment
             os.environ['HOME'] = pwd.getpwuid(uid).pw_dir
-    except OSError as err:
-        ret_data_q.put(GbpServiceError("Setting UID/GID (%s:%s) failed: %s" %
-                                       (uid, gid, err)))
-        sys.exit(_RET_FORK_ERR)
+        except OSError as err:
+            ret_data_q.put(GbpServiceError("Setting UID (%s) failed: %s" %
+                                           (uid, err)))
+            sys.exit(_RET_FORK_ERR)
     # Call the function
     try:
         # Func must be a callable without arguments