From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:36774) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UNW0C-0007sN-Cg for qemu-devel@nongnu.org; Wed, 03 Apr 2013 18:17:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UNW0A-0007E2-KU for qemu-devel@nongnu.org; Wed, 03 Apr 2013 18:17:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:10035) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UNW0A-0007Dv-BU for qemu-devel@nongnu.org; Wed, 03 Apr 2013 18:17:54 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r33MHrHG005815 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 3 Apr 2013 18:17:53 -0400 Received: from choo.home.annexia.org (vpn1-7-22.ams2.redhat.com [10.36.7.22]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r33MHph0029097 for ; Wed, 3 Apr 2013 18:17:52 -0400 From: "Richard W.M. Jones" Date: Wed, 3 Apr 2013 23:17:39 +0100 Message-Id: <1365027461-8884-2-git-send-email-rjones@redhat.com> In-Reply-To: <1365027461-8884-1-git-send-email-rjones@redhat.com> References: <1365027461-8884-1-git-send-email-rjones@redhat.com> Subject: [Qemu-devel] [PATCH v5 1/3] osdep: Add a function to get the current username. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org From: "Richard W.M. Jones" Signed-off-by: Richard W.M. Jones --- include/qemu/osdep.h | 2 ++ util/osdep.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) 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 #include #include +#include /* Needed early for CONFIG_BSD etc. */ #include "config-host.h" @@ -38,13 +39,16 @@ #endif #ifdef CONFIG_SOLARIS -#include #include /* 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 +#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 +} -- 1.8.1.4