Re: [PATCH 1/2] util: Keep errno set to the root error after when returning from virSetUIDGID

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

 



On 05/22/2011 10:55 AM, Jiri Denemark wrote:
---
  src/util/util.c |   31 ++++++++++++++++++++-----------
  1 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index d1a08a6..0b4370b 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -2889,17 +2889,20 @@ int virGetGroupID(const char *name,

  /* Set the real and effective uid and gid to the given values, and call
   * initgroups so that the process has all the assumed group membership of
- * that uid. return 0 on success, -1 on failure.
+ * that uid. return 0 on success, -1 on failure (the original system error
+ * remains in errno).
   */
  int
  virSetUIDGID(uid_t uid, gid_t gid)
  {
+    int err;
+
      if (gid>  0) {
          if (setregid(gid, gid)<  0) {
-            virReportSystemError(errno,
+            virReportSystemError(err = errno,
                                   _("cannot change to '%d' group"),
                                   (unsigned int) gid);
-            return -1;
+            goto error;
          }
      }

@@ -2916,39 +2919,45 @@ virSetUIDGID(uid_t uid, gid_t gid)

          if (VIR_ALLOC_N(buf, bufsize)<  0) {
              virReportOOMError();
-            return -1;
+            err = ENOMEM;
+            goto error;
          }
          while ((rc = getpwuid_r(uid,&pwd, buf, bufsize,
                                  &pwd_result)) == ERANGE) {
              if (VIR_RESIZE_N(buf, bufsize, bufsize, bufsize)<  0) {
                  virReportOOMError();
                  VIR_FREE(buf);
-                return -1;
+                err = ENOMEM;
+                goto error;
              }
          }
          if (rc || !pwd_result) {
-            virReportSystemError(rc, _("cannot getpwuid_r(%d)"),
+            virReportSystemError(err = rc, _("cannot getpwuid_r(%d)"),
                                   (unsigned int) uid);
              VIR_FREE(buf);
-            return -1;
+            goto error;
          }
          if (initgroups(pwd.pw_name, pwd.pw_gid)<  0) {
-            virReportSystemError(errno,
+            virReportSystemError(err = errno,
                                   _("cannot initgroups(\"%s\", %d)"),
                                   pwd.pw_name, (unsigned int) pwd.pw_gid);
              VIR_FREE(buf);
-            return -1;
+            goto error;
          }
          VIR_FREE(buf);
  # endif
          if (setreuid(uid, uid)<  0) {
-            virReportSystemError(errno,
+            virReportSystemError(err = errno,
                                   _("cannot change to uid to '%d'"),
                                   (unsigned int) uid);
-            return -1;
+            goto error;
          }
      }
      return 0;
+
+error:
+    errno = err;
+    return -1;
  }

  #else /* HAVE_GETPWUID_R */

ACK.

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]