Revert r180082 and add a test for SetEnv function

llvm-svn: 180098
This commit is contained in:
Alexey Samsonov 2013-04-23 12:49:12 +00:00
parent 262eb01bbf
commit f36c5a8431
3 changed files with 24 additions and 0 deletions

View File

@ -130,6 +130,7 @@ void DisableCoreDumper();
void DumpProcessMap();
bool FileExists(const char *filename);
const char *GetEnv(const char *name);
bool SetEnv(const char *name, const char *value);
const char *GetPwd();
u32 GetUid();
void ReExec();

View File

@ -264,6 +264,20 @@ const char *GetEnv(const char *name) {
return 0; // Not found.
}
// Does not compile for Go because dlsym() requires -ldl
#ifndef SANITIZER_GO
bool SetEnv(const char *name, const char *value) {
void *f = dlsym(RTLD_NEXT, "setenv");
if (f == 0)
return false;
typedef int(*setenv_ft)(const char *name, const char *value, int overwrite);
setenv_ft setenv_f;
CHECK_EQ(sizeof(setenv_f), sizeof(f));
internal_memcpy(&setenv_f, &f, sizeof(f));
return setenv_f(name, value, 1) == 0;
}
#endif
#ifdef __GLIBC__
extern "C" {

View File

@ -20,6 +20,7 @@
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
@ -185,6 +186,14 @@ TEST_F(ThreadListerTest, ResetMakesNewThreadsKnown) {
ASSERT_TRUE(HasElement(threads_after_extra, extra_tid));
}
TEST(SanitizerCommon, SetEnvTest) {
const char kEnvName[] = "ENV_FOO";
SetEnv(kEnvName, "value");
EXPECT_STREQ("value", getenv(kEnvName));
unsetenv(kEnvName);
EXPECT_EQ(0, getenv(kEnvName));
}
} // namespace __sanitizer
#endif // __linux__