summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDariusz Ostolski <dariusz.ostolski@gmail.com>2017-09-19 20:23:53 (GMT)
committerDariusz Ostolski <dariusz.ostolski@gmail.com>2017-09-19 20:23:53 (GMT)
commit2d3cf2681d4bd4019af368d549395fe2c00a0154 (patch)
tree0d28d9427e8bd6bcef2a043e2dfbefd14061baf8 /src
parent2a6df66252d266080489c310b8146e63b66b2add (diff)
downloadglog-2d3cf2681d4bd4019af368d549395fe2c00a0154.zip
glog-2d3cf2681d4bd4019af368d549395fe2c00a0154.tar.gz
glog-2d3cf2681d4bd4019af368d549395fe2c00a0154.tar.bz2
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());