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

[libvirt] [PATCH][take2][1/2] add virGetUIDByUsername() and virGetGIDByGroupname()



Signed-off-by: Ryota Ozaki <ozaki ryota gmail com>

>From de8c57e3a2c4e564ec989016c547ad6754e43871 Mon Sep 17 00:00:00 2001
From: Ryota Ozaki <ozaki ryota gmail com>
Date: Wed, 8 Apr 2009 23:10:46 +0900
Subject: [PATCH] add virGetUIDByUsername() and virGetGIDByGroupname()

---
 configure.in |    2 +-
 src/util.c   |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/util.h   |    8 ++++++++
 3 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/configure.in b/configure.in
index ac2c89e..2ae15e3 100644
--- a/configure.in
+++ b/configure.in
@@ -75,7 +75,7 @@ dnl Availability of various common functions
(non-fatal if missing).
 AC_CHECK_FUNCS([cfmakeraw regexec uname sched_getaffinity getuid
getgid posix_fallocate mmap readlink])

 dnl Availability of various not common threadsafe functions
-AC_CHECK_FUNCS([strerror_r strtok_r getmntent_r getgrnam_r getpwuid_r])
+AC_CHECK_FUNCS([strerror_r strtok_r getmntent_r getgrnam_r getpwuid_r
getpwnam_r])

 dnl Availability of various common headers (non-fatal if missing).
 AC_CHECK_HEADERS([pwd.h paths.h regex.h sys/syslimits.h sys/utsname.h
sys/wait.h winsock2.h sched.h termios.h sys/poll.h syslog.h])
diff --git a/src/util.c b/src/util.c
index 5abdbbc..b939282 100644
--- a/src/util.c
+++ b/src/util.c
@@ -53,9 +53,12 @@
 #include <paths.h>
 #endif
 #include <netdb.h>
-#ifdef HAVE_GETPWUID_R
+#if defined(HAVE_GETPWUID_R) || defined(HAVE_GETPWNAM_R)
 #include <pwd.h>
 #endif
+#ifdef HAVE_GETGRNAM_R
+#include <grp.h>
+#endif

 #include "virterror_internal.h"
 #include "logging.h"
@@ -1686,3 +1689,55 @@ char *virGetUserDirectory(virConnectPtr conn,
     return ret;
 }
 #endif
+
+
+#ifdef HAVE_GETPWNAM_R
+int virGetUIDByUsername(virConnectPtr conn, char *name)
+{
+    struct passwd pwbuf;
+    struct passwd *pw = NULL;
+    char *strbuf;
+    size_t strbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
+
+    if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
+        virReportOOMError(conn);
+        return -1;
+    }
+
+    if (getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) {
+        virReportSystemError(conn, errno,
+                             _("Failed to find user record for name '%s'"),
+                             name);
+        VIR_FREE(strbuf);
+        return -1;
+    }
+    VIR_FREE(strbuf);
+    return pw->pw_uid;
+}
+#endif
+
+
+#ifdef HAVE_GETGRNAM_R
+int virGetGIDByGroupname(virConnectPtr conn, char *name)
+{
+    struct group grbuf;
+    struct group *gr = NULL;
+    char *strbuf;
+    size_t strbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+
+    if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
+        virReportOOMError(conn);
+        return -1;
+    }
+
+    if (getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr) != 0 || gr == NULL) {
+        virReportSystemError(conn, errno,
+                             _("Failed to find group record for name '%s'"),
+                             name);
+        VIR_FREE(strbuf);
+        return -1;
+    }
+    VIR_FREE(strbuf);
+    return gr->gr_gid;
+}
+#endif
diff --git a/src/util.h b/src/util.h
index 6fe03b6..0f75439 100644
--- a/src/util.h
+++ b/src/util.h
@@ -199,6 +199,14 @@ char *virGetUserDirectory(virConnectPtr conn,
                           uid_t uid);
 #endif

+#ifdef HAVE_GETPWNAM_R
+int virGetUIDByUsername(virConnectPtr conn, char *name);
+#endif
+
+#ifdef HAVE_GETGRNAM_R
+int virGetGIDByGroupname(virConnectPtr conn, char *name);
+#endif
+
 int virRandomInitialize(unsigned int seed);
 int virRandom(int max);

-- 
1.6.0.6


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