summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShinichiro Hamaji <shinichiro.hamaji@gmail.com>2017-10-20 07:09:28 (GMT)
committerGitHub <noreply@github.com>2017-10-20 07:09:28 (GMT)
commitebf81ac4767823fb209c18ff7e11850cd63deafc (patch)
treec48c1cdb3925c39af3073682c98ca36d78ea0cbb /src
parentcb7b26bd7115f421d9d108e1f68b3b76c72f7e12 (diff)
parent9d28cac4690e32eae94e180ea0d6d7ce0b6fc71d (diff)
downloadglog-ebf81ac4767823fb209c18ff7e11850cd63deafc.zip
glog-ebf81ac4767823fb209c18ff7e11850cd63deafc.tar.gz
glog-ebf81ac4767823fb209c18ff7e11850cd63deafc.tar.bz2
Merge pull request #245 from DariuszOstolski/issue211
Fix username lookup in case of missing USER environment variable
Diffstat (limited to 'src')
-rw-r--r--src/utilities.cc26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/utilities.cc b/src/utilities.cc
index 9d5b123..1a3c7e1 100644
--- a/src/utilities.cc
+++ b/src/utilities.cc
@@ -47,6 +47,12 @@
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h> // For geteuid.
+#endif
+#ifdef HAVE_PWD_H
+# include <pwd.h>
+#endif
#include "base/googleinit.h"
@@ -299,8 +305,26 @@ static void MyUserNameInitializer() {
if (user != NULL) {
g_my_user_name = user;
} else {
- g_my_user_name = "invalid-user";
+#if defined(HAVE_PWD_H) && defined(HAVE_UNISTD_H)
+ uid_t uid;
+ struct passwd pwd;
+ struct passwd*result = NULL;
+ char buffer[1024] = {'\0'};
+ uid = geteuid();
+ int pwuid_res = getpwuid_r(uid, &pwd, buffer, sizeof (buffer), &result);
+ if(pwuid_res == 0) {
+ g_my_user_name = pwd.pw_name;
+ }
+ else {
+ snprintf(buffer, sizeof(buffer), "uid%d", uid);
+ g_my_user_name = buffer;
+ }
+#endif
+ if(g_my_user_name.empty()) {
+ g_my_user_name = "invalid-user";
+ }
}
+
}
REGISTER_MODULE_INITIALIZER(utilities, MyUserNameInitializer());