Allow setting environment variables with modifications added at runtime

This commit is contained in:
baldurk
2016-04-30 17:18:44 +02:00
parent 375747654d
commit 10ba54e1fd
2 changed files with 61 additions and 0 deletions
+58
View File
@@ -32,6 +32,7 @@
#include <unistd.h>
#include <dlfcn.h>
#include <string.h>
#include <stdlib.h>
#include "serialise/string_utils.h"
@@ -74,8 +75,65 @@ void Process::RegisterEnvironmentModification(Process::EnvironmentModification m
// there is no support for injecting/loading renderdoc into a running program
// in any way, and we also have some environment changes that we *have* to make
// for correct hooking (LD_LIBRARY_PATH/LD_PRELOAD)
//
// However we still set environment variables so that we can modify variables while
// in process (e.g. if we notice a setting and want to enable an env var as a result)
void Process::ApplyEnvironmentModification()
{
// turn environment string to a UTF-8 map
map<string, string> currentEnv = EnvStringToEnvMap((const char **)environ);
vector<EnvironmentModification> &modifications = GetEnvModifications();
for(size_t i=0; i < modifications.size(); i++)
{
EnvironmentModification &m = modifications[i];
string value = currentEnv[m.name];
switch(m.type)
{
case eEnvModification_Replace:
value = m.value;
break;
case eEnvModification_Append:
value += m.value;
break;
case eEnvModification_AppendPlatform:
case eEnvModification_AppendColon:
if(!value.empty())
value += ":";
value += m.value;
break;
case eEnvModification_AppendSemiColon:
if(!value.empty())
value += ";";
value += m.value;
break;
case eEnvModification_Prepend:
value = m.value + value;
break;
case eEnvModification_PrependColon:
if(!value.empty())
value = m.value + ":" + value;
else
value = m.value;
break;
case eEnvModification_PrependPlatform:
case eEnvModification_PrependSemiColon:
if(!value.empty())
value = m.value + ";" + value;
else
value = m.value;
break;
default:
RDCERR("Unexpected environment modification type");
}
setenv(m.name.c_str(), value.c_str(), true);
}
// these have been applied to the current process
modifications.clear();
}
static pid_t RunProcess(const char *app, const char *workingDir, const char *cmdLine, char *const *envp)
+3
View File
@@ -133,6 +133,9 @@ void Process::ApplyEnvironmentModification()
SetEnvironmentVariableW(StringFormat::UTF82Wide(m.name).c_str(), StringFormat::UTF82Wide(value).c_str());
}
// these have been applied to the current process
modifications.clear();
}
// helpers for various shims and dlls etc, not part of the public API