[PATCHv2 1/3] util: grab primary group when doing user lookup

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

 



A future patch needs to look up pw_gid; but it is wasteful
to crawl through getpwuid_r twice for two separate pieces
of information.  Alter the internal-only virGetUserEnt to
give us multiple pieces of information on one traversal.

* src/util/virutil.c (virGetUserEnt): Alter signature.
(virGetUserDirectory, virGetXDGDirectory, virGetUserName): Adjust
callers.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
---
 src/util/virutil.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/util/virutil.c b/src/util/virutil.c
index 569d035..de7b532 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -656,8 +656,12 @@ enum {
     VIR_USER_ENT_NAME,
 };

-static char *virGetUserEnt(uid_t uid,
-                           int field)
+/* Look up either the name or home directory for a given uid; if group
+   is non-NULL, also look up the primary group for that user.  On
+   failure, error has been reported, errno is set, and NULL is
+   returned.  */
+static char *
+virGetUserEnt(uid_t uid, int field, gid_t *group)
 {
     char *strbuf;
     char *ret;
@@ -698,6 +702,8 @@ static char *virGetUserEnt(uid_t uid,
         return NULL;
     }

+    if (group)
+        *group = pw->pw_gid;
     ignore_value(VIR_STRDUP(ret, field == VIR_USER_ENT_DIRECTORY ?
                             pw->pw_dir : pw->pw_name));
     VIR_FREE(strbuf);
@@ -752,7 +758,7 @@ static char *virGetGroupEnt(gid_t gid)

 char *virGetUserDirectory(void)
 {
-    return virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY);
+    return virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY, NULL);
 }

 static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
@@ -765,7 +771,7 @@ static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
         if (virAsprintf(&ret, "%s/libvirt", path) < 0)
             goto no_memory;
     } else {
-        home = virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY);
+        home = virGetUserEnt(geteuid(), VIR_USER_ENT_DIRECTORY, NULL);
         if (virAsprintf(&ret, "%s/%s/libvirt", home, xdgdefdir) < 0)
             goto no_memory;
     }
@@ -808,7 +814,7 @@ char *virGetUserRuntimeDirectory(void)

 char *virGetUserName(uid_t uid)
 {
-    return virGetUserEnt(uid, VIR_USER_ENT_NAME);
+    return virGetUserEnt(uid, VIR_USER_ENT_NAME, NULL);
 }

 char *virGetGroupName(gid_t gid)
-- 
1.8.1.4

--
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]