aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-20 21:44:30 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-21 14:58:18 +0100
commit3342ab3add1fb1a60cb60f8e0b396c07db8a9b8b (patch)
treeb187dfb051338c9c29c9408807de9b16bdbc1bee
parente0829dd6ab62d8bd25308fc806072d1b378a1523 (diff)
downloadpaludis-3342ab3add1fb1a60cb60f8e0b396c07db8a9b8b.tar.gz
paludis-3342ab3add1fb1a60cb60f8e0b396c07db8a9b8b.tar.xz
Process::clearenv
-rw-r--r--paludis/util/process.cc29
-rw-r--r--paludis/util/process.hh2
-rw-r--r--paludis/util/process_TEST.cc17
3 files changed, 48 insertions, 0 deletions
diff --git a/paludis/util/process.cc b/paludis/util/process.cc
index 82aa7b7..750473e 100644
--- a/paludis/util/process.cc
+++ b/paludis/util/process.cc
@@ -376,6 +376,7 @@ namespace paludis
int set_stdin_fd;
std::map<std::string, std::string> setenvs;
+ bool clearenv;
std::string chdir;
uid_t setuid;
gid_t setgid;
@@ -393,6 +394,7 @@ namespace paludis
capture_output_to_fd_stream(0),
capture_output_to_fd_fd(-1),
set_stdin_fd(-1),
+ clearenv(false),
setuid(getuid()),
setgid(getgid()),
echo_command_to(0)
@@ -480,6 +482,26 @@ Process::run()
{
try
{
+ if (_imp->clearenv)
+ {
+ std::map<std::string, std::string> setenvs;
+ for (const char * const * it(environ) ; 0 != *it ; ++it)
+ {
+ std::string var(*it);
+ if (std::string::npos != var.find('=') &&
+ ("PALUDIS_" == var.substr(0, 8) ||
+ "PATH=" == var.substr(0, 5) ||
+ "HOME=" == var.substr(0, 5) ||
+ "LD_LIBRARY_PATH=" == var.substr(0, 16)))
+ setenvs.insert(std::make_pair(var.substr(0, var.find('=')), var.substr(var.find('=') + 1)));
+ }
+ ::clearenv();
+
+ for (std::map<std::string, std::string>::const_iterator it(setenvs.begin()),
+ it_end(setenvs.end()) ; it_end != it ; ++it)
+ ::setenv(it->first.c_str(), it->second.c_str(), 1);
+ }
+
if (thread && thread->capture_stdout_pipe)
{
if (-1 == ::dup2(thread->capture_stdout_pipe->write_fd(), STDOUT_FILENO))
@@ -625,6 +647,13 @@ Process::setenv(const std::string & a, const std::string & b)
}
Process &
+Process::clearenv()
+{
+ _imp->clearenv = true;
+ return *this;
+}
+
+Process &
Process::chdir(const FSEntry & f)
{
_imp->chdir = stringify(f.realpath_if_exists());
diff --git a/paludis/util/process.hh b/paludis/util/process.hh
index ddc17a1..1ec4b24 100644
--- a/paludis/util/process.hh
+++ b/paludis/util/process.hh
@@ -85,6 +85,8 @@ namespace paludis
Process & pipe_command_handler(const std::string &, const ProcessPipeCommandFunction &);
Process & setenv(const std::string &, const std::string &);
+ Process & clearenv();
+
Process & chdir(const FSEntry &);
Process & use_ptys();
Process & setuid_setgid(uid_t, gid_t);
diff --git a/paludis/util/process_TEST.cc b/paludis/util/process_TEST.cc
index 69fc5fd..0686e8c 100644
--- a/paludis/util/process_TEST.cc
+++ b/paludis/util/process_TEST.cc
@@ -381,5 +381,22 @@ namespace test_cases
TEST_CHECK_EQUAL(stderr_stream.str(), "prefix> monkey\nprefix> in\nprefix> space\n");
}
} test_prefix_stderr;
+
+ struct ClearenvTest : TestCase
+ {
+ ClearenvTest() : TestCase("clearenv") { }
+
+ void run()
+ {
+ ::setenv("BANANAS", "IN PYJAMAS", 1);
+ std::stringstream stdout_stream;
+ Process printenv_process(ProcessCommand({"printenv", "BANANAS"}));
+ printenv_process.capture_stdout(stdout_stream);
+ printenv_process.clearenv();
+
+ TEST_CHECK_EQUAL(printenv_process.run().wait(), 1);
+ TEST_CHECK_EQUAL(stdout_stream.str(), "");
+ }
+ } test_clearenv;
}