summaryrefslogtreecommitdiff
path: root/src/utilities.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/utilities.cc')
-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());