Patchwork [v5,1/3] osdep: Add a function to get the current username.

login
register
mail settings
Submitter Richard W.M. Jones
Date April 3, 2013, 10:17 p.m.
Message ID <1365027461-8884-2-git-send-email-rjones@redhat.com>
Download mbox | patch
Permalink /patch/233591/
State New
Headers show

Comments

Richard W.M. Jones - April 3, 2013, 10:17 p.m.
From: "Richard W.M. Jones" <rjones@redhat.com>

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 include/qemu/osdep.h |  2 ++
 util/osdep.c         | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
Peter Maydell - April 4, 2013, 9:08 a.m.
On 3 April 2013 23:17, Richard W.M. Jones <rjones@redhat.com> wrote:
> +/*
> + * Get the login name of the current user.  The string must be freed
> + * up by the caller.  If the current user could not be determined,
> + * returns NULL and sets errno.
> + */

Any reason we can't just use g_get_user_name() ?

-- PMM
Richard W.M. Jones - April 4, 2013, 9:28 a.m.
On Thu, Apr 04, 2013 at 10:08:10AM +0100, Peter Maydell wrote:
> On 3 April 2013 23:17, Richard W.M. Jones <rjones@redhat.com> wrote:
> > +/*
> > + * Get the login name of the current user.  The string must be freed
> > + * up by the caller.  If the current user could not be determined,
> > + * returns NULL and sets errno.
> > + */
> 
> Any reason we can't just use g_get_user_name() ?

None; I'll change the patch accordingly in the next version.

Rich.

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index df24400..8ed5b78 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -182,4 +182,6 @@  const char *qemu_get_version(void);
 void fips_set_state(bool requested);
 bool fips_get_state(void);
 
+char *qemu_current_user(void);
+
 #endif
diff --git a/util/osdep.c b/util/osdep.c
index bd59ac9..b40ebc8 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -29,6 +29,7 @@ 
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/types.h>
 
 /* Needed early for CONFIG_BSD etc. */
 #include "config-host.h"
@@ -38,13 +39,16 @@ 
 #endif
 
 #ifdef CONFIG_SOLARIS
-#include <sys/types.h>
 #include <sys/statvfs.h>
 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
    discussion about Solaris header problems */
 extern int madvise(caddr_t, size_t, int);
 #endif
 
+#ifndef _WIN32
+#include <pwd.h>
+#endif
+
 #include "qemu-common.h"
 #include "trace.h"
 #include "qemu/sockets.h"
@@ -406,3 +410,46 @@  bool fips_get_state(void)
     return fips_enabled;
 }
 
+/*
+ * Get the login name of the current user.  The string must be freed
+ * up by the caller.  If the current user could not be determined,
+ * returns NULL and sets errno.
+ */
+char *qemu_current_user(void)
+{
+#ifndef _WIN32
+    uid_t euid = geteuid();
+    char *buf;
+    long size;
+    struct passwd pwd, *result_pwd;
+    char *ret;
+
+#ifdef _SC_GETPW_R_SIZE_MAX
+    size = sysconf(_SC_GETPW_R_SIZE_MAX);
+#else
+    size = 256;
+#endif
+
+ again:
+    buf = g_malloc (size);
+    if (getpwuid_r(euid, &pwd, buf, size, &result_pwd) == -1) {
+        g_free(buf);
+        if (errno == ERANGE) {
+            size *= 2;
+            goto again;
+        }
+        return NULL;
+    }
+    if(result_pwd && result_pwd->pw_name) {
+        ret = g_strdup(result_pwd->pw_name);
+    } else {
+        ret = NULL;
+    }
+    g_free(buf);
+    return ret;
+
+#else /* _WIN32 */
+    errno = ENOTSUP;
+    return NULL;                /* Not implemented. */
+#endif
+}