Skip to content

Commit

Permalink
fix(env handling): propagage env correctly to child process
Browse files Browse the repository at this point in the history
Our current env handling does not propagate env from lo2s' environment
to the childs environment.

if you currently do:

```
export FOO=BAR
./lo2s -- echo $FOO
```

it will print nothing.

Instead of building or own envrion and then using execvpe, modify our
own environment and then use execvp, which copies the parents env.
  • Loading branch information
cvonelm committed Feb 12, 2025
1 parent 0a07fa8 commit cee6c02
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/monitor/process_monitor_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,25 @@ std::vector<char*> to_vector_of_c_str(const std::vector<std::string>& vec)
/* we need ptrace to get fork/clone/... */
ptrace(PTRACE_TRACEME, 0, NULL, NULL);

std::vector<std::string> env;
std::map<std::string, std::string> env;
#ifdef HAVE_CUDA
if (config().use_nvidia)
{
env = { "CUDA_INJECTION64_PATH=" + config().cuda_injectionlib_path };
env.emplace("CUDA_INJECTION64_PATH", config().cuda_injectionlib_path);

if (config().use_clockid)
{
env.push_back("LO2S_CLOCKID=" + std::to_string(config().clockid));
env.emplace("LO2S_CLOCKID", std::to_string(config().clockid));
}
}
#endif
std::vector<char*> c_env = to_vector_of_c_str(env);
std::vector<char*> c_args = to_vector_of_c_str(command_and_args);

for (const auto& env_var : env)
{
setenv(env_var.first.c_str(), env_var.second.c_str(), 1);
}

Log::debug() << "Execute the command: " << nitro::lang::join(command_and_args);

// Stop yourself so the parent tracer can do initialize the options
Expand All @@ -179,19 +183,14 @@ std::vector<char*> to_vector_of_c_str(const std::vector<std::string>& vec)
}

// run the application which should be sampled
execvpe(c_args[0], &c_args[0], &c_env[0]);
execvp(c_args[0], &c_args[0]);

// should not be executed -> exec failed, let's clean up anyway.
for (auto cp : c_args)
{
delete[] cp;
}

for (auto cp : c_env)
{
delete[] cp;
}

Log::error() << "Could not execute the command: " << nitro::lang::join(command_and_args);
throw_errno();
}
Expand Down

0 comments on commit cee6c02

Please sign in to comment.