File libvirt-util-Don-t-fail-virGetGroupIDByName-when-group-not-found.patch of Package libvirt

From 18d194e1e22bf6ff164a9a0f11fa3b7a758d1912 Mon Sep 17 00:00:00 2001
Message-Id: <18d194e1e22bf6ff164a9a0f11fa3b7a758d1912.1355319681.git.jdenemar@redhat.com>
From: Christophe Fergeau <cfergeau@redhat.com>
Date: Tue, 11 Dec 2012 15:40:59 +0100
Subject: [PATCH] util: Don't fail virGetGroupIDByName when group not found
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

https://bugzilla.redhat.com/show_bug.cgi?id=883832

virGetGroupIDByName is documented as returning 1 if the groupname
cannot be found. getgrnam_r is documented as returning:
« 0 or ENOENT or ESRCH or EBADF or EPERM or ...  The given name
or gid was not found. »
 and that:
« The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
It  does  not  call  "not  found"  an error, hence does not specify what
value errno might have in this situation.  But that makes it impossible to
recognize errors.  One might argue that according to POSIX errno should be
left unchanged if an entry is not found.  Experiments on various UNIX-like
systems shows that lots of different values occur in this situation: 0,
ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others. »

virGetGroupIDByName returns an error when the return value of getgrnam_r
is non-0. However on my RHEL system, getgrnam_r returns ENOENT when the
requested user cannot be found, which then causes virGetGroupID not
to behave as documented (it returns an error instead of falling back
to parsing the passed-in value as an gid).

This commit makes virGetGroupIDByName only report an error when errno
is set to one of the values in the posix description of getgrnam_r
(which are the same as the ones described in the manpage on my system).
(cherry picked from commit a33f4eae83ecc6fb6e33006650c7f81e16584bd0)
---
 src/util/util.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index d3eb422..0d10355 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -2634,9 +2634,18 @@ virGetGroupIDByName(const char *name, gid_t *gid)
     }
 
     if (rc != 0) {
-        virReportSystemError(rc, _("Failed to get group record for name '%s'"),
-                             name);
-        goto cleanup;
+       /* We explicitly test for the known error values returned by
+        * getgrnam_r as the manpage says:
+        * ERRORS
+        *   0 or ENOENT or ESRCH or EBADF or EPERM or ...
+        *         The given name or gid was not found.
+        */
+       if ((rc == EINTR) || (rc == EIO) || (rc == EMFILE) ||
+           (rc == ENFILE) || (rc == ENOMEM)) {
+           virReportSystemError(rc, _("Failed to get group record for name '%s'"),
+                                name);
+           goto cleanup;
+       }
     }
 
     if (!gr) {
-- 
1.8.0

openSUSE Build Service is sponsored by