Skip to content

Commit

Permalink
Make PrepareEnvironmentForJvm() explicitly inherit environment variab…
Browse files Browse the repository at this point in the history
…les.

The map returned by PrepareEnvironmentForJvm() previously represented a
"delta" to apply to the current environment.  This was used to modify the
environment surrounding fork/execv calls because we could not pass an envp
to execv.

However, when using the resulting map to create the envp value of a call
like posix_spawn, we must be exhaustive and list all variables we expect
the subprocess to have.  And as we are going to be using posix_spawn, we
need to make this happen, so change PrepareEnvironmentForJvm() to make
the returned map exhaustive.

Prerequisite change to address
bazelbuild#7446 because of the need to
switch to posix_spawn.

RELNOTES: None.
PiperOrigin-RevId: 234971246
  • Loading branch information
jmmv authored and irengrig committed Feb 26, 2019
1 parent b2c6602 commit 6e8ef5c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

using blaze_util::GetLastErrorString;

extern char** environ;

namespace blaze {

using std::map;
Expand Down Expand Up @@ -1416,6 +1418,26 @@ static void ComputeBaseDirectories(const WorkspaceLayout *workspace_layout,
static map<string, EnvVarValue> PrepareEnvironmentForJvm() {
map<string, EnvVarValue> result;

// Make sure all existing environment variables appear as part of the
// resulting map unless they are overridden below by UNSET values.
//
// Even though the map we return is intended to represent a "delta" of
// environment variables to modify the current process, we may actually use
// such map to configure a process from scratch (via interfaces like execvpe
// or posix_spawn), so we need to inherit any untouched variables.
for (char** entry = environ; *entry != NULL; entry++) {
const std::string var_value = *entry;
std::string::size_type equals = var_value.find('=');
if (equals == std::string::npos) {
// Ignore possibly-bad environment. We don't control what we see in this
// global variable, so it could be invalid.
continue;
}
const std::string var = var_value.substr(0, equals);
const std::string value = var_value.substr(equals + 1);
result[var] = EnvVarValue(EnvVarAction::SET, value);
}

if (!blaze::GetEnv("LD_ASSUME_KERNEL").empty()) {
// Fix for bug: if ulimit -s and LD_ASSUME_KERNEL are both
// specified, the JVM fails to create threads. See thread_stack_regtest.
Expand Down

0 comments on commit 6e8ef5c

Please sign in to comment.