diff options
author | Damien Martin-Guillerez <dmarting@google.com> | 2015-03-23 13:00:28 +0000 |
---|---|---|
committer | Han-Wen Nienhuys <hanwen@google.com> | 2015-03-24 16:41:13 +0000 |
commit | 27b94751b74fcc16e93157649649337e8456da67 (patch) | |
tree | 65b395c6e5b91c7fdc7ae939463636c5f227dd91 /src/main/cpp/blaze_util_linux.cc | |
parent | 07072b0d29835eb2cfeca4af43bb0daa533dba5c (diff) |
Use getpwuid(getuid()) to determine the output root under Linux
Also Fallback the default ouput root to /var/tmp if $HOME is not set
under Linux. If $HOME environment variable was not set, Bazel would
crash with a std::logic_error. Using getpwuid() make Bazel more
resilient to wrong environment.
--
MOS_MIGRATED_REVID=89292008
Diffstat (limited to 'src/main/cpp/blaze_util_linux.cc')
-rw-r--r-- | src/main/cpp/blaze_util_linux.cc | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc index c5c32daafc..1ccfd232ce 100644 --- a/src/main/cpp/blaze_util_linux.cc +++ b/src/main/cpp/blaze_util_linux.cc @@ -16,6 +16,8 @@ #include <string.h> // strerror #include <sys/statfs.h> #include <unistd.h> +#include <sys/types.h> +#include <pwd.h> #include "blaze_exit_code.h" #include "blaze_util_platform.h" @@ -28,7 +30,16 @@ namespace blaze { using std::string; string GetOutputRoot() { - return blaze_util::JoinPath(getenv("HOME"), ".cache/bazel"); + char buf[2048]; + struct passwd pwbuf; + struct passwd *pw = NULL; + int uid = getuid(); + int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw); + if (r != -1 && pw != NULL) { + return blaze_util::JoinPath(pw->pw_dir, ".cache/bazel"); + } else { + return "/tmp"; + } } void WarnFilesystemType(const string& output_base) { |