aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-21 20:27:13 +0100
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2010-08-22 09:46:17 +0100
commite11e4f0105cb71dc86524c95a3132492f590e6fc (patch)
tree1a8748e00d575dbe2a4c6649f594ab0444982cd6
parent89487ae270cf49a239cef7c264ff39cb7b35bb5a (diff)
downloadpaludis-e11e4f0105cb71dc86524c95a3132492f590e6fc.tar.gz
paludis-e11e4f0105cb71dc86524c95a3132492f590e6fc.tar.xz
Remove dead code
-rw-r--r--.gitignore3
-rw-r--r--doc/configuration/envvars.html.part3
-rw-r--r--misc/common-makefile.am1
-rw-r--r--paludis/util/Makefile.am.m411
-rw-r--r--paludis/util/files.m43
-rw-r--r--paludis/util/output_wrapper.cc264
-rw-r--r--paludis/util/output_wrapper_TEST.cc463
-rwxr-xr-xpaludis/util/output_wrapper_TEST_cleanup.sh10
-rwxr-xr-xpaludis/util/output_wrapper_TEST_setup.sh139
-rw-r--r--paludis/util/system.cc1330
-rw-r--r--paludis/util/system.hh348
-rw-r--r--paludis/util/system_TEST.cc308
-rw-r--r--paludis/util/system_TEST_become_child.cc36
-rwxr-xr-xpaludis/util/system_TEST_cleanup.sh9
-rwxr-xr-xpaludis/util/system_TEST_setup.sh60
-rw-r--r--paludis/util/test_extras.cc5
16 files changed, 3 insertions, 2990 deletions
diff --git a/.gitignore b/.gitignore
index 2c0ee71..81b4f1b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -452,8 +452,6 @@ paludis-*.*.*.tar.bz2
/paludis/util/member_iterator_TEST
/paludis/util/mutex_TEST
/paludis/util/options_TEST
-/paludis/util/output_wrapper_TEST
-/paludis/util/outputwrapper
/paludis/util/pretty_print_TEST
/paludis/util/process_TEST
/paludis/util/pty_TEST
@@ -472,7 +470,6 @@ paludis-*.*.*.tar.bz2
/paludis/util/string_list_stream_TEST
/paludis/util/strip_TEST
/paludis/util/system_TEST
-/paludis/util/system_TEST_become_child
/paludis/util/tail_output_stream_TEST
/paludis/util/thread_TEST
/paludis/util/thread_pool_TEST
diff --git a/doc/configuration/envvars.html.part b/doc/configuration/envvars.html.part
index 760fe71..00c8b83 100644
--- a/doc/configuration/envvars.html.part
+++ b/doc/configuration/envvars.html.part
@@ -70,9 +70,6 @@ can be dangerous and may break things; many of these variables exist mainly for
<dt><code>PALUDIS_REPOSITORY_SO_DIR</code></dt>
<dd>Where Paludis looks to find repository .so files.</dd>
- <dt><code>PALUDIS_OUTPUTWRAPPER_DIR</code></dt>
- <dd>Where Paludis looks to find outputwrapper.</dd>
-
<dt><code>PALUDIS_DO_NOTHING_SANDBOXY</code></dt>
<dd>If set to a non-empty string, Paludis will do absolutely nothing involving Sandbox.</dd>
diff --git a/misc/common-makefile.am b/misc/common-makefile.am
index ef35bc0..c6cbcca 100644
--- a/misc/common-makefile.am
+++ b/misc/common-makefile.am
@@ -77,7 +77,6 @@ LOG_COMPILER = \
PALUDIS_NO_XTERM_TITLES="yes" \
PALUDIS_OPTIONS="" \
PALUDIS_OUTPUT_MANAGERS_DIR="`$(top_srcdir)/paludis/repositories/e/ebuild/utils/canonicalise $(top_srcdir)/paludis/environments/paludis/output_managers/`" \
- PALUDIS_OUTPUTWRAPPER_DIR="`$(top_srcdir)/paludis/repositories/e/ebuild/utils/canonicalise $(top_builddir)/paludis/util/`" \
PALUDIS_PC_SLOT=@PALUDIS_PC_SLOT@ \
PALUDIS_PIPE_COMMAND_READ_FD="" \
PALUDIS_PIPE_COMMAND_WRITE_FD="" \
diff --git a/paludis/util/Makefile.am.m4 b/paludis/util/Makefile.am.m4
index 5b885b8..6bf6b88 100644
--- a/paludis/util/Makefile.am.m4
+++ b/paludis/util/Makefile.am.m4
@@ -71,13 +71,9 @@ libpaludisutil_@PALUDIS_PC_SLOT@_la_LIBADD = $(PTHREAD_LIBS) $(RT_LIBS)
TESTS = testlist
-check_PROGRAMS = $(TESTS) system_TEST_become_child
+check_PROGRAMS = $(TESTS)
check_SCRIPTS = testscriptlist
-system_TEST_become_child_SOURCES = system_TEST_become_child.cc
-system_TEST_become_child_LDADD = \
- libpaludisutil_@PALUDIS_PC_SLOT@.la
-
lib_LTLIBRARIES = libpaludisutil_@PALUDIS_PC_SLOT@.la
paludis_util_includedir = $(includedir)/paludis-$(PALUDIS_PC_SLOT)/paludis/util/
@@ -89,11 +85,6 @@ Makefile.am : Makefile.am.m4 files.m4
util.hh : util.hh.m4 files.m4
$(top_srcdir)/misc/do_m4.bash util.hh
-libexecutilsdir = $(libexecdir)/paludis/utils
-libexecutils_PROGRAMS = outputwrapper
-
libexecpaludisdir = $(libexecdir)/paludis
libexecpaludis_SCRIPTS = echo_functions.bash
-outputwrapper_SOURCES = output_wrapper.cc
-
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 5862841..e771734 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -58,7 +58,6 @@ add(`named_value', `hh', `cc', `fwd')
add(`no_type', `hh')
add(`operators', `hh')
add(`options', `hh', `fwd', `cc', `test')
-add(`output_wrapper', `test', `testscript')
add(`pimp', `hh', `impl')
add(`pipe', `hh', `cc')
add(`pretty_print', `hh', `cc', `test')
@@ -84,7 +83,7 @@ add(`singleton', `hh', `impl', `test')
add(`stringify', `hh', `test')
add(`string_list_stream', `hh', `cc', `fwd', `test')
add(`strip', `hh', `cc', `test')
-add(`system', `hh', `cc', `test', `testscript')
+add(`system', `hh', `cc', `test')
add(`tail_output_stream', `hh', `cc', `fwd', `test')
add(`tee_output_stream', `hh', `cc', `fwd')
add(`thread', `hh', `cc', `test')
diff --git a/paludis/util/output_wrapper.cc b/paludis/util/output_wrapper.cc
deleted file mode 100644
index 14eff0b..0000000
--- a/paludis/util/output_wrapper.cc
+++ /dev/null
@@ -1,264 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2007 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <string>
-#include <cstring>
-#include <iostream>
-#include <unistd.h>
-#include <errno.h>
-#include <stdio.h>
-#include <cstdlib>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/select.h>
-
-int
-main(int argc, char *argv[])
-{
- int argi(1);
- std::string stdout_prefix, stderr_prefix;
- bool wrap_blanks(false);
- bool discard_blank_output(false);
-
- for ( ; argi < argc ; ++argi)
- {
- std::string s(argv[argi]);
- if (s == "--")
- {
- ++argi;
- break;
- }
- else if (s == "--wrap-blanks")
- wrap_blanks = true;
- else if (s == "--discard-blank-output")
- discard_blank_output = true;
- else if (s == "--stdout-prefix")
- {
- if (++argi >= argc)
- {
- std::cerr << argv[0] << ": no argument for --stdout-prefix" << std::endl;
- return EXIT_FAILURE;
- }
- stdout_prefix = argv[argi];
- }
- else if (s == "--stderr-prefix")
- {
- if (++argi >= argc)
- {
- std::cerr << argv[0] << ": no argument for --stderr-prefix" << std::endl;
- return EXIT_FAILURE;
- }
- stderr_prefix = argv[argi];
- }
- else
- {
- std::cerr << argv[0] << ": bad argument '" << s << "'" << std::endl;
- return EXIT_FAILURE;
- }
- }
-
- if (argi >= argc)
- {
- std::cerr << argv[0] << ": no -- found" << std::endl;
- return EXIT_FAILURE;
- }
-
- int stdout_pipes[2], stderr_pipes[2];
-
- if (0 != pipe(stdout_pipes))
- {
- std::cerr << argv[0] << ": pipe failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
-
- if (0 != pipe(stderr_pipes))
- {
- std::cerr << argv[0] << ": pipe failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
-
- pid_t pid(fork());
-
- if (0 == pid)
- {
- if (-1 == dup2(stdout_pipes[1], 1))
- {
- std::cerr << argv[0] << ": dup2 failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- close(stdout_pipes[0]);
-
- if (-1 == dup2(stderr_pipes[1], 2))
- {
- std::cerr << argv[0] << ": dup2 failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- close(stderr_pipes[0]);
-
- execvp(argv[argi], &argv[argi]);
- std::cerr << argv[0] << ": execvp failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- else if (-1 == pid)
- {
- std::cerr << argv[0] << ": fork failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- else
- {
- close(stdout_pipes[1]);
- close(stderr_pipes[1]);
- bool stdout_done(false), stdout_prefix_shown(false), stdout_had_interesting_char(false),
- stderr_done(false), stderr_prefix_shown(false), stderr_had_interesting_char(false),
- stdout_had_non_blanks(! discard_blank_output), stderr_had_non_blanks(! discard_blank_output);
- unsigned stdout_blanks(0), stderr_blanks(0);
- while ((! stdout_done) || (! stderr_done))
- {
- fd_set fds;
- int ret;
-
- FD_ZERO(&fds);
- FD_SET(stdout_pipes[0], &fds);
- FD_SET(stderr_pipes[0], &fds);
-
- ret = select(std::max(stdout_pipes[0], stderr_pipes[0]) + 1, &fds, 0, 0, 0);
- if (-1 == ret)
- {
- std::cerr << argv[0] << ": select failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- else if (ret)
- {
- char buf[1024];
- if (FD_ISSET(stdout_pipes[0], &fds))
- {
- int c;
- if (0 >= ((c = read(stdout_pipes[0], buf, 1024))))
- stdout_done = true;
- else
- {
- std::string to_write(buf, c);
-
- for (std::string::size_type p(0) ; p < to_write.length() ; ++p)
- {
- if (to_write.at(p) != '\n' && to_write.at(p) != '\r')
- {
- if (! stdout_had_non_blanks)
- {
- for (unsigned x(0) ; x < stdout_blanks ; ++x)
- if (wrap_blanks)
- std::cout << stdout_prefix << std::endl;
- else
- std::cout << std::endl;
- stdout_had_non_blanks = true;
- }
- stdout_had_interesting_char = true;
- }
- else if (! stdout_had_non_blanks)
- {
- if (to_write.at(p) == '\n')
- ++stdout_blanks;
- continue;
- }
-
- if (! stdout_prefix_shown)
- {
- if (stdout_had_interesting_char || wrap_blanks)
- std::cout << stdout_prefix;
- stdout_prefix_shown = true;
- }
-
- if (to_write.at(p) == '\n' || to_write.at(p) == '\r')
- {
- stdout_had_interesting_char = false;
- stdout_prefix_shown = false;
- }
-
- std::cout << to_write.at(p);
- }
-
- std::cout << std::flush;
- }
- }
-
- if (FD_ISSET(stderr_pipes[0], &fds))
- {
- int c;
- if (0 >= ((c = read(stderr_pipes[0], buf, 1024))))
- stderr_done = true;
- else
- {
- std::string to_write(buf, c);
-
- for (std::string::size_type p(0) ; p < to_write.length() ; ++p)
- {
- if (to_write.at(p) != '\n' && to_write.at(p) != '\r')
- {
- if (! stderr_had_non_blanks)
- {
- for (unsigned x(0) ; x < stderr_blanks ; ++x)
- if (wrap_blanks)
- std::cerr << stderr_prefix << std::endl;
- else
- std::cerr << std::endl;
- stderr_had_non_blanks = true;
- }
- stderr_had_interesting_char = true;
- }
- else if (! stderr_had_non_blanks)
- {
- if (to_write.at(p) == '\n')
- ++stderr_blanks;
- continue;
- }
-
- if (! stderr_prefix_shown)
- {
- if (stderr_had_interesting_char || wrap_blanks)
- std::cerr << stderr_prefix;
- stderr_prefix_shown = true;
- }
-
- if (to_write.at(p) == '\n' || to_write.at(p) == '\r')
- {
- stderr_had_interesting_char = false;
- stderr_prefix_shown = false;
- }
-
- std::cerr << to_write.at(p);
- }
-
- std::cerr << std::flush;
- }
- }
- }
- else
- break;
- }
-
- int status(-1);
- if (-1 == wait(&status))
- {
- std::cerr << argv[0] << ": wait failed: " << std::strerror(errno) << std::endl;
- return EXIT_FAILURE;
- }
- return WIFSIGNALED(status) ? WTERMSIG(status) + 128 : WEXITSTATUS(status);
- }
-}
-
diff --git a/paludis/util/output_wrapper_TEST.cc b/paludis/util/output_wrapper_TEST.cc
deleted file mode 100644
index b6ff3cf..0000000
--- a/paludis/util/output_wrapper_TEST.cc
+++ /dev/null
@@ -1,463 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2007, 2009 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <test/test_framework.hh>
-#include <test/test_runner.hh>
-
-#include <paludis/util/system.hh>
-#include <paludis/util/join.hh>
-
-#include <iterator>
-#include <set>
-
-using namespace test;
-using namespace paludis;
-
-namespace test_cases
-{
- struct StdoutPrefixTest : TestCase
- {
- StdoutPrefixTest() : TestCase("stdout prefix") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(run_command(Command("./outputwrapper --stdout-prefix 'o p ' -- "
- "bash output_wrapper_TEST_dir/stdout_prefix.bash")
- .with_captured_stdout_stream(&p)), 0);
- std::string s((std::istreambuf_iterator<char>(p)), std::istreambuf_iterator<char>());
- TEST_CHECK_EQUAL(s, "o p one\no p two\no p three\n");
- }
- } test_stdout_prefix;
-
- struct StderrPrefixTest : TestCase
- {
- StderrPrefixTest() : TestCase("stderr prefix") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stderr-prefix 'e p ' -- "
- "bash output_wrapper_TEST_dir/stderr_prefix.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::string s((std::istreambuf_iterator<char>(p)), std::istreambuf_iterator<char>());
- TEST_CHECK_EQUAL(s, "e p one\ne p two\ne p three\n");
- }
- } test_stderr_prefix;
-
- struct MixedPrefixTest : TestCase
- {
- MixedPrefixTest() : TestCase("mixed prefix") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- "
- "bash output_wrapper_TEST_dir/mixed_prefix.bash 2>&1")
- .with_captured_stdout_stream(&p)), 0);
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(3));
- TEST_CHECK(lines.count("e p one"));
- TEST_CHECK(lines.count("e p three"));
- TEST_CHECK(lines.count("o p two"));
- }
- } test_mixed_prefix;
-
- struct LongLinesTest : TestCase
- {
- LongLinesTest() : TestCase("long lines") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(run_command(Command("bash output_wrapper_TEST_dir/long_lines.bash")
- .with_captured_stdout_stream(&p)), 0);
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(2));
- TEST_CHECK(lines.count("e p " + std::string(10000, 'e')));
- TEST_CHECK(lines.count("o p " + std::string(10000, 'o')));
- }
- } test_long_lines;
-
- struct NoTrailingNewlineTest : TestCase
- {
- NoTrailingNewlineTest() : TestCase("no trailing newline") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("bash output_wrapper_TEST_dir/no_trailing_newlines.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(1));
- TEST_CHECK(lines.count("o p monkeye p pants"));
- }
- } test_no_trailing_newline;
-
- struct ExitStatusTest : TestCase
- {
- ExitStatusTest() : TestCase("exit status") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(5, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- "
- "bash output_wrapper_TEST_dir/exit_status.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(2));
- TEST_CHECK(lines.count("o p lorem ipsum dolor"));
- TEST_CHECK(lines.count("e p sit amet"));
- }
- } test_exit_status;
-
- struct NoWrapBlanksTest : TestCase
- {
- NoWrapBlanksTest() : TestCase("no wrap blanks") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- "
- "bash output_wrapper_TEST_dir/no_wrap_blanks.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(7));
- TEST_CHECK(lines.count("e p one"));
- TEST_CHECK(lines.count("e p three"));
- TEST_CHECK(lines.count("o p two"));
- TEST_CHECK_EQUAL(lines.count(""), static_cast<std::size_t>(4));
- }
- } test_no_wrap_blanks;
-
- struct WrapBlanksTest : TestCase
- {
- WrapBlanksTest() : TestCase("wrap blanks") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/wrap_blanks.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(7));
- TEST_CHECK(lines.count("e p one"));
- TEST_CHECK(lines.count("e p three"));
- TEST_CHECK(lines.count("o p two"));
- TEST_CHECK_EQUAL(lines.count("e p "), static_cast<std::size_t>(2));
- TEST_CHECK_EQUAL(lines.count("o p "), static_cast<std::size_t>(2));
- }
- } test_wrap_blanks;
-
- struct DiscardBlankOutputTest : TestCase
- {
- DiscardBlankOutputTest() : TestCase("discard blank output") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
- "bash output_wrapper_TEST_dir/discard_blank_output.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
- }
- } test_discard_blank_output;
-
- struct DiscardBlankOutputNotBlankTest : TestCase
- {
- DiscardBlankOutputNotBlankTest() : TestCase("discard blank output not blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
- "bash output_wrapper_TEST_dir/discard_blank_output_not_blank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(4));
- TEST_CHECK(lines.count("o p monkey"));
- TEST_CHECK_EQUAL(lines.count(""), static_cast<std::size_t>(3));
- }
- } test_discard_blank_output_not_blank;
-
- struct DiscardBlankWrapBlankOutputTest : TestCase
- {
- DiscardBlankWrapBlankOutputTest() : TestCase("discard wrap blank output") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command(
- "./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/discard_wrap_blank_output.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
- }
- } test_discard_wrap_blank_output;
-
- struct DiscardBlankWrapBlankOutputNotBlankTest : TestCase
- {
- DiscardBlankWrapBlankOutputNotBlankTest() : TestCase("discard wrap blank output not blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(
- Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/discard_wrap_blank_output_not_blank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- lines.insert(line);
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(4));
- TEST_CHECK(lines.count("o p monkey"));
- TEST_CHECK_EQUAL(lines.count("o p "), static_cast<std::size_t>(3));
- }
- } test_discard_wrap_blank_output_not_blank;
-
- struct CarriageReturnTest : TestCase
- {
- CarriageReturnTest() : TestCase("carriage return") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- "
- "bash output_wrapper_TEST_dir/carriage_return.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(10));
- TEST_CHECK(lines.count("o p foo\\ro p bar"));
- TEST_CHECK(lines.count("o p foo\\r"));
- TEST_CHECK(lines.count("o p foo\\r\\ro p bar"));
- TEST_CHECK(lines.count("\\ro p foo"));
- TEST_CHECK(lines.count("e p foo\\re p bar"));
- TEST_CHECK(lines.count("e p foo\\r"));
- TEST_CHECK(lines.count("e p foo\\r\\re p bar"));
- TEST_CHECK(lines.count("\\re p foo"));
- TEST_CHECK_EQUAL(lines.count("\\r"), static_cast<std::size_t>(2));
- }
- } test_carriage_return;
-
- struct CarriageReturnWrapBlankTest : TestCase
- {
- CarriageReturnWrapBlankTest() : TestCase("carriage return wrap blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/carriage_return.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(10));
- TEST_CHECK(lines.count("o p foo\\ro p bar"));
- TEST_CHECK(lines.count("o p foo\\ro p "));
- TEST_CHECK(lines.count("o p foo\\ro p \\ro p bar"));
- TEST_CHECK(lines.count("o p \\ro p foo"));
- TEST_CHECK(lines.count("o p \\ro p "));
- TEST_CHECK(lines.count("e p foo\\re p bar"));
- TEST_CHECK(lines.count("e p foo\\re p "));
- TEST_CHECK(lines.count("e p foo\\re p \\re p bar"));
- TEST_CHECK(lines.count("e p \\re p foo"));
- TEST_CHECK(lines.count("e p \\re p "));
- }
- } test_carriage_return_wrap_blank;
-
- struct CarriageReturnDiscardBlankOutputBlankTest : TestCase
- {
- CarriageReturnDiscardBlankOutputBlankTest() : TestCase("carriage return discard blank output blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
- "bash output_wrapper_TEST_dir/carriage_return_blank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
- }
- } test_carriage_return_discard_blank_output_blank;
-
- struct CarriageReturnDiscardBlankWrapBlankOutputBlankTest : TestCase
- {
- CarriageReturnDiscardBlankWrapBlankOutputBlankTest() : TestCase("carriage return discard blank wrap blank output blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(
- Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/carriage_return_blank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(0));
- }
- } test_carriage_return_discard_blank_wrap_blank_output_blank;
-
- struct CarriageReturnDiscardBlankOutputNotBlankTest : TestCase
- {
- CarriageReturnDiscardBlankOutputNotBlankTest() : TestCase("carriage return discard blank output not blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output -- "
- "bash output_wrapper_TEST_dir/carriage_return_nonblank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(6));
- TEST_CHECK_EQUAL(lines.count(""), static_cast<std::size_t>(4));
- TEST_CHECK(lines.count("o p hello"));
- TEST_CHECK(lines.count("e p hello"));
- }
- } test_carriage_return_discard_blank_output_not_blank;
-
- struct CarriageReturnDiscardBlankWrapBlankOutputNotBlankTest : TestCase
- {
- CarriageReturnDiscardBlankWrapBlankOutputNotBlankTest() : TestCase("carriage return discard blank wrap blank output not blank") { }
-
- void run()
- {
- std::stringstream p;
- TEST_CHECK_EQUAL(0, run_command(
- Command("./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' --discard-blank-output --wrap-blanks -- "
- "bash output_wrapper_TEST_dir/carriage_return_nonblank.bash 2>&1")
- .with_captured_stdout_stream(&p)));
- std::multiset<std::string> lines;
- std::string line;
- while (std::getline(p, line))
- {
- std::string::size_type c(line.length());
- while (std::string::npos != (c = line.rfind('\r', c)))
- line.replace(c, 1, "\\r");
- lines.insert(line);
- }
-
- TestMessageSuffix s("lines=(" + join(lines.begin(), lines.end(), ",") + ")");
- TEST_CHECK_EQUAL(lines.size(), static_cast<std::size_t>(6));
- TEST_CHECK_EQUAL(lines.count("o p "), static_cast<std::size_t>(2));
- TEST_CHECK_EQUAL(lines.count("e p "), static_cast<std::size_t>(2));
- TEST_CHECK(lines.count("o p hello"));
- TEST_CHECK(lines.count("e p hello"));
- }
- } test_carriage_return_discard_blank_wrap_blank_output_not_blank;
-}
-
diff --git a/paludis/util/output_wrapper_TEST_cleanup.sh b/paludis/util/output_wrapper_TEST_cleanup.sh
deleted file mode 100755
index 6110d5c..0000000
--- a/paludis/util/output_wrapper_TEST_cleanup.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-#!/usr/bin/env bash
-# vim: set ft=sh sw=4 sts=4 et :
-
-if [ -d output_wrapper_TEST_dir ] ; then
- rm -fr output_wrapper_TEST_dir
-else
- true
-fi
-
-
diff --git a/paludis/util/output_wrapper_TEST_setup.sh b/paludis/util/output_wrapper_TEST_setup.sh
deleted file mode 100755
index e7211c3..0000000
--- a/paludis/util/output_wrapper_TEST_setup.sh
+++ /dev/null
@@ -1,139 +0,0 @@
-#!/usr/bin/env bash
-# vim: set ft=sh sw=4 sts=4 et :
-
-mkdir output_wrapper_TEST_dir || exit 2
-cd output_wrapper_TEST_dir
-
-cat <<END > stdout_prefix.bash
-echo one
-echo two
-echo three
-END
-
-cat <<END > stderr_prefix.bash
-echo one 1>&2
-echo two 1>&2
-echo three 1>&2
-END
-
-cat <<END > mixed_prefix.bash
-echo one 1>&2
-echo two
-echo three 1>&2
-END
-
-cat <<END > real_long_lines.bash
-for (( a = 0 ; a < 1000 ; ++a )) ; do
- echo -n eeeeeeeeee 1>&2
- echo -n oooooooooo
-done
-echo 1>&2
-echo
-END
-
-cat <<END > long_lines.bash
-./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- \
- bash output_wrapper_TEST_dir/real_long_lines.bash \
- 1>output_wrapper_TEST_dir/long_lines_stdout \
- 2>output_wrapper_TEST_dir/long_lines_stderr
-cat output_wrapper_TEST_dir/long_lines_stdout output_wrapper_TEST_dir/long_lines_stderr
-END
-
-cat <<END > real_no_trailing_newlines.bash
-echo -n monkey
-echo -n pants 1>&2
-END
-
-cat <<END > no_trailing_newlines.bash
-./outputwrapper --stdout-prefix 'o p ' --stderr-prefix 'e p ' -- \
- bash output_wrapper_TEST_dir/real_no_trailing_newlines.bash \
- 1>output_wrapper_TEST_dir/no_trailing_newlines_stdout \
- 2>output_wrapper_TEST_dir/no_trailing_newlines_stderr
-cat output_wrapper_TEST_dir/no_trailing_newlines_stdout output_wrapper_TEST_dir/no_trailing_newlines_stderr
-END
-
-cat <<END > exit_status.bash
-echo lorem ipsum dolor
-echo sit amet 1>&2
-exit 5
-END
-
-cat <<END > no_wrap_blanks.bash
-echo
-echo one 1>&2
-echo 1>&2
-echo two
-echo three 1>&2
-echo 1>&2
-echo
-END
-
-cat <<END > wrap_blanks.bash
-echo
-echo one 1>&2
-echo 1>&2
-echo two
-echo three 1>&2
-echo 1>&2
-echo
-END
-
-cat <<END > discard_blank_output.bash
-echo
-echo 1>&2
-echo
-END
-
-cat <<END > discard_blank_output_not_blank.bash
-echo
-echo 1>&2
-echo
-echo monkey
-echo
-END
-
-cat <<END > discard_wrap_blank_output.bash
-echo
-echo 1>&2
-echo
-END
-
-cat <<END > discard_wrap_blank_output_not_blank.bash
-echo
-echo 1>&2
-echo
-echo monkey
-echo
-END
-
-cat <<END > carriage_return.bash
-echo -e 'foo\rbar'
-echo -e 'foo\r'
-echo -e 'foo\r\rbar'
-echo -e '\rfoo'
-echo -e '\r'
-echo -e 'foo\rbar' 1>&2
-echo -e 'foo\r' 1>&2
-echo -e 'foo\r\rbar' 1>&2
-echo -e '\rfoo' 1>&2
-echo -e '\r' 1>&2
-END
-
-cat <<END > carriage_return_blank.bash
-echo
-echo -e '\r'
-echo
-echo 1>&2
-echo -e '\r' 1>&2
-echo 1>&2
-END
-
-cat <<END > carriage_return_nonblank.bash
-echo
-echo -e '\r'
-echo hello
-echo 1>&2
-echo -e '\r' 1>&2
-echo hello 1>&2
-END
-
diff --git a/paludis/util/system.cc b/paludis/util/system.cc
index 04abfc1..394ef8c 100644
--- a/paludis/util/system.cc
+++ b/paludis/util/system.cc
@@ -20,20 +20,13 @@
#include <cstdlib>
#include <paludis/util/system.hh>
#include <paludis/util/log.hh>
-#include <paludis/util/fs_entry.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/destringify.hh>
-#include <paludis/util/pimp-impl.hh>
-#include <paludis/util/wrapped_forward_iterator-impl.hh>
-#include <paludis/util/strip.hh>
-#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/pipe.hh>
#include <paludis/util/pty.hh>
#include <sys/utsname.h>
#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
@@ -41,33 +34,12 @@
#include <pwd.h>
#include <signal.h>
#include <fcntl.h>
-#include <map>
-#include <vector>
-#include <iostream>
-#include <cstring>
#include "config.h"
using namespace paludis;
-typedef std::map<std::string, std::string> CommandSetenvValues;
-
-namespace paludis
-{
- template <>
- struct WrappedForwardIteratorTraits<Command::ConstIteratorTag>
- {
- typedef CommandSetenvValues::const_iterator UnderlyingIterator;
- };
-}
-
namespace
{
- static int stdout_write_fd = -1;
- static int stdout_close_fd = -1;
-
- static int stderr_write_fd = -1;
- static int stderr_close_fd = -1;
-
pid_t get_paludis_pid()
{
pid_t result(0);
@@ -88,30 +60,11 @@ namespace
static pid_t paludis_pid PALUDIS_ATTRIBUTE((used)) = get_paludis_pid();
}
-void
-paludis::set_run_command_stdout_fds(const int w, const int c)
-{
- stdout_write_fd = w;
- stdout_close_fd = c;
-}
-
-void
-paludis::set_run_command_stderr_fds(const int w, const int c)
-{
- stderr_write_fd = w;
- stderr_close_fd = c;
-}
-
GetenvError::GetenvError(const std::string & key) throw () :
Exception("Environment variable '" + key + "' not set")
{
}
-RunCommandError::RunCommandError(const std::string & our_message) throw () :
- Exception(our_message)
-{
-}
-
std::string
paludis::getenv_with_default(const std::string & key, const std::string & def)
{
@@ -142,11 +95,6 @@ namespace
throw InternalError(PALUDIS_HERE, "uname call failed");
return u.release;
}
-
- std::map<std::string, std::string> make_me_a_frickin_map_because_gcc_sucks()
- {
- return std::map<std::string, std::string>();
- }
}
std::string
@@ -156,1282 +104,6 @@ paludis::kernel_version()
return result;
}
-namespace paludis
-{
- template<>
- struct Imp<Command>
- {
- std::string command;
- bool clearenv;
- CommandSetenvValues setenv_values;
- std::string chdir;
- bool echo_to_stderr;
- std::shared_ptr<uid_t> uid;
- std::shared_ptr<gid_t> gid;
- std::string stdout_prefix;
- std::string stderr_prefix;
- bool prefix_discard_blank_output;
- bool prefix_blank_lines;
- std::string pipe_command_env_var_prefix;
- std::function<std::string (const std::string &)> pipe_command_handler;
- std::ostream * captured_stdout_stream;
- std::ostream * captured_stderr_stream;
- std::ostream * output_stream;
- int output_fd;
- std::string output_fd_env_var;
- std::istream * input_stream;
- int input_fd;
- std::string input_fd_env_var;
- bool ptys;
-
- Imp(const std::string & c, bool cl = false,
- const std::map<std::string, std::string> & s = make_me_a_frickin_map_because_gcc_sucks(),
- const std::string & d = "", bool e = false,
- std::shared_ptr<uid_t> u = std::shared_ptr<uid_t>(),
- std::shared_ptr<gid_t> g = std::shared_ptr<gid_t>(),
- const std::string & p = "", const std::string & q = "",
- const bool b = false, const bool bb = false,
- const std::string & ep = "",
- const std::function<std::string (const std::string &)> & h = std::function<std::string (const std::string &)>(),
- std::ostream * cs = 0,
- std::ostream * ds = 0,
- std::ostream * os = 0,
- int osf = -1,
- const std::string & osfe = "",
- std::istream * is = 0,
- int isf = -1,
- const std::string & isfe = "",
- const bool ps = false) :
- command(c),
- clearenv(cl),
- setenv_values(s),
- chdir(d),
- echo_to_stderr(e),
- uid(u),
- gid(g),
- stdout_prefix(p),
- stderr_prefix(q),
- prefix_discard_blank_output(b),
- prefix_blank_lines(bb),
- pipe_command_env_var_prefix(ep),
- pipe_command_handler(h),
- captured_stdout_stream(cs),
- captured_stderr_stream(ds),
- output_stream(os),
- output_fd(osf),
- output_fd_env_var(osfe),
- input_stream(is),
- input_fd(isf),
- input_fd_env_var(isfe),
- ptys(ps)
- {
- }
- };
-}
-
-Command::Command(const std::string & s) :
- Pimp<Command>(s)
-{
-}
-
-Command::Command(const char * const s) :
- Pimp<Command>(s)
-{
-}
-
-Command::Command(const Command & other) :
- Pimp<Command>(other._imp->command,
- other._imp->clearenv, other._imp->setenv_values, other._imp->chdir, other._imp->echo_to_stderr,
- other._imp->uid, other._imp->gid, other._imp->stdout_prefix, other._imp->stderr_prefix,
- other._imp->prefix_discard_blank_output,
- other._imp->prefix_blank_lines, other._imp->pipe_command_env_var_prefix,
- other._imp->pipe_command_handler, other._imp->captured_stdout_stream,
- other._imp->captured_stderr_stream, other._imp->output_stream, other._imp->output_fd,
- other._imp->output_fd_env_var, other._imp->input_stream, other._imp->input_fd,
- other._imp->input_fd_env_var, other._imp->ptys)
-{
-}
-
-const Command &
-Command::operator= (const Command & other)
-{
- if (this != &other)
- {
- _imp.reset(new Imp<Command>(other._imp->command, other._imp->clearenv, other._imp->setenv_values,
- other._imp->chdir, other._imp->echo_to_stderr,
- std::shared_ptr<uid_t>(),
- std::shared_ptr<gid_t>(),
- other._imp->stdout_prefix,
- other._imp->stderr_prefix,
- other._imp->prefix_discard_blank_output,
- other._imp->prefix_blank_lines,
- other._imp->pipe_command_env_var_prefix,
- other._imp->pipe_command_handler,
- other._imp->captured_stdout_stream,
- other._imp->captured_stderr_stream,
- other._imp->output_stream,
- other._imp->output_fd,
- other._imp->output_fd_env_var,
- other._imp->input_stream,
- other._imp->input_fd,
- other._imp->input_fd_env_var,
- other._imp->ptys));
- if (other.uid() && other.gid())
- with_uid_gid(*other.uid(), *other.gid());
- }
-
- return *this;
-}
-
-Command::~Command()
-{
-}
-
-Command &
-Command::with_chdir(const FSEntry & c)
-{
- _imp->chdir = stringify(c);
- return *this;
-}
-
-Command &
-Command::with_setenv(const std::string & k, const std::string & v)
-{
- _imp->setenv_values.insert(std::make_pair(k, v));
- return *this;
-}
-
-Command &
-Command::with_clearenv()
-{
- _imp->clearenv = true;
- return *this;
-}
-
-Command &
-Command::with_uid_gid(const uid_t u, const gid_t g)
-{
- _imp->uid = std::make_shared<uid_t>(u);
- _imp->gid = std::make_shared<gid_t>(g);
- return *this;
-}
-
-Command &
-Command::with_captured_stdout_stream(std::ostream * const c)
-{
- _imp->captured_stdout_stream = c;
- return *this;
-}
-
-Command &
-Command::with_captured_stderr_stream(std::ostream * const c)
-{
- _imp->captured_stderr_stream = c;
- return *this;
-}
-
-Command &
-Command::with_output_stream(std::ostream * const c, int fd, const std::string & e)
-{
- _imp->output_stream = c;
- _imp->output_fd = fd;
- _imp->output_fd_env_var = e;
- return *this;
-}
-
-Command &
-Command::with_input_stream(std::istream * const c, int fd, const std::string & e)
-{
- _imp->input_stream = c;
- _imp->input_fd = fd;
- _imp->input_fd_env_var = e;
- return *this;
-}
-
-Command &
-Command::with_ptys()
-{
- _imp->ptys = true;
- return *this;
-}
-
-namespace
-{
- bool check_cmd(const std::string & s)
- {
- bool result(0 == run_command(Command(s + " --version >/dev/null 2>/dev/null")));
- if (! result)
- Log::get_instance()->message("util.system.boxless", ll_warning, lc_context) <<
- "I don't seem to be able to use " + s;
- return result;
- }
-}
-
-Command &
-Command::with_sandbox()
-{
- static bool can_use_sandbox(check_cmd("sandbox"));
-
- if (can_use_sandbox)
- {
- if (! getenv_with_default("PALUDIS_DO_NOTHING_SANDBOXY", "").empty())
- Log::get_instance()->message("util.system.nothing_sandboxy", ll_debug, lc_no_context)
- << "PALUDIS_DO_NOTHING_SANDBOXY is set, not using sandbox";
- else if (! getenv_with_default("SANDBOX_ACTIVE", "").empty())
- Log::get_instance()->message("util.system.sandbox_in_sandbox", ll_warning, lc_no_context)
- << "Already inside sandbox, not spawning another sandbox instance";
- else
- {
- _imp->command = "sandbox " + _imp->command;
- if (getenv_with_default("BASH_ENV", "").empty())
- with_setenv("BASH_ENV", "/dev/null");
- }
- }
-
- return *this;
-}
-
-Command &
-Command::with_sydbox()
-{
- static bool can_use_sydbox(check_cmd("sydbox"));
-
- if (can_use_sydbox)
- {
- if (! getenv_with_default("PALUDIS_DO_NOTHING_SANDBOXY", "").empty())
- Log::get_instance()->message("util.system.nothing_sandboxy", ll_debug, lc_no_context)
- << "PALUDIS_DO_NOTHING_SANDBOXY is set, not using sydbox";
- else if (! getenv_with_default("SYDBOX_ACTIVE", "").empty())
- Log::get_instance()->message("util.system.sandbox_in_sandbox", ll_warning, lc_no_context)
- << "Already inside sydbox, not spawning another sydbox instance";
- else
- _imp->command = "sydbox --profile paludis -- " + _imp->command;
- }
-
- return *this;
-}
-
-std::shared_ptr<const uid_t>
-Command::uid() const
-{
- return _imp->uid;
-}
-
-std::shared_ptr<const gid_t>
-Command::gid() const
-{
- return _imp->gid;
-}
-
-namespace
-{
- void wait_handler(int sig)
- {
- std::cerr << "Caught signal " << sig << " in run_command child process" << std::endl;
- }
-
- std::string build_extras(const Command & cmd)
- {
- std::string extras;
-
- if (! cmd.chdir().empty())
- extras.append(" [chdir " + cmd.chdir() + "]");
-
- if (cmd.clearenv())
- extras.append(" [clearenv]");
-
- for (Command::ConstIterator s(cmd.begin_setenvs()), s_end(cmd.end_setenvs()) ; s != s_end ; ++s)
- extras.append(" [setenv " + s->first + "=" + s->second + "]");
-
- if (cmd.gid() && *cmd.gid() != getgid())
- extras.append(" [setgid " + stringify(*cmd.gid()) + "]");
-
- if (cmd.uid() && *cmd.uid() != getuid())
- extras.append(" [setuid " + stringify(*cmd.uid()) + "]");
-
- return extras;
- }
-
- void do_chdir(const Command & cmd)
- {
- if (! cmd.chdir().empty())
- if (-1 == chdir(stringify(cmd.chdir()).c_str()))
- throw RunCommandError("chdir failed: " + stringify(strerror(errno)));
- }
-
- void do_env(const Command & cmd)
- {
- if (cmd.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);
- }
-
- for (Command::ConstIterator s(cmd.begin_setenvs()), s_end(cmd.end_setenvs()) ; s != s_end ; ++s)
- setenv(s->first.c_str(), s->second.c_str(), 1);
-
- /* This is what happens when people are allowed to play with
- * things they don't understand. Yay Gentoo! */
- setenv("PACKAGE_MANAGER", "paludis", 1);
- setenv("ESELECT_PACKAGE_MANAGER", "paludis", 1);
- }
-
- void do_uid_gid(const Command & cmd)
- {
- std::string command(cmd.command());
-
- // If we're not changing uid or gid, do nothing.
- if ((!cmd.uid() || *cmd.uid() == getuid()) &&
- (!cmd.gid() || *cmd.gid() == getgid()))
- {
- return;
- }
-
- int ngids(0);
- std::vector<gid_t> gids;
- gid_t gid = cmd.gid() ? *cmd.gid() : getgid();
-
- if (cmd.uid())
- {
- // Fetch the list of supplemental groups for the given uid
- struct passwd *pwd_entry(getpwuid(*cmd.uid()));
- getgrouplist(pwd_entry->pw_name, gid, 0, &ngids);
- gids.resize(ngids);
- if (-1 == getgrouplist(pwd_entry->pw_name, gid, &gids[0], &ngids))
- {
- std::cerr << "getgrouplist failed. Did something change group?" << std::endl;
- _exit(1);
- }
- }
- else
- {
- gids.push_back(gid);
- }
-
- if (0 != ::setgid(gid))
- {
- std::cerr << "setgid(" << gid << ") failed for exec of '" << command << "': "
- << strerror(errno) << std::endl;
- _exit(1);
- }
- if (0 != ::setgroups(gids.size(), &gids[0]))
- {
- std::cerr << "setgroups failed for exec of '" << command << "': " << strerror(errno) << std::endl;
- _exit(1);
- }
-
- if (cmd.uid() && *cmd.uid() != getuid())
- if (0 != ::setuid(*cmd.uid()))
- {
- std::cerr << "setuid(" << *cmd.uid() << ") failed for exec of '" << command << "': "
- << strerror(errno) << std::endl;
- _exit(1);
- }
- }
-}
-
-int
-paludis::run_command(const Command & cmd)
-{
- Context context("When running command '" + stringify(cmd.command()) + "':");
-
- std::string extras(build_extras(cmd));
- std::string command(cmd.command());
-
- if ((! cmd.stdout_prefix().empty()) || (! cmd.stderr_prefix().empty()))
- command = getenv_with_default("PALUDIS_OUTPUTWRAPPER_DIR", LIBEXECDIR "/paludis/utils") + "/outputwrapper --stdout-prefix '"
- + cmd.stdout_prefix() + "' --stderr-prefix '" + cmd.stderr_prefix() + "' "
- + (cmd.prefix_discard_blank_output() ? " --discard-blank-output " : "")
- + (cmd.prefix_blank_lines() ? " --wrap-blanks " : "")
- + " -- " + command;
-
- cmd.echo_to_stderr();
- Log::get_instance()->message("util.system.execl", ll_debug, lc_no_context) << "execl /bin/sh -c " << command
- << " " << extras;
-
- std::shared_ptr<Pipe> internal_command_reader(std::make_shared<Pipe>()), pipe_command_reader,
- pipe_command_response, input_stream, output_stream;
- std::shared_ptr<Channel> captured_stdout, captured_stderr;
- if (cmd.pipe_command_handler())
- {
- pipe_command_reader = std::make_shared<Pipe>();
- pipe_command_response = std::make_shared<Pipe>();
- }
- if (cmd.captured_stdout_stream())
- captured_stdout.reset(cmd.ptys() ? static_cast<Channel *>(new Pty) : new Pipe);
- if (cmd.captured_stderr_stream())
- captured_stderr.reset(cmd.ptys() ? static_cast<Channel *>(new Pty) : new Pipe);
- if (cmd.output_stream())
- output_stream = std::make_shared<Pipe>();
- if (cmd.input_stream())
- {
- input_stream = std::make_shared<Pipe>();
- int arg(fcntl(input_stream->write_fd(), F_GETFL, NULL));
- if (-1 == arg)
- throw RunCommandError("fcntl F_GETFL failed: " + stringify(strerror(errno)));
- arg |= O_NONBLOCK;
- if (-1 == fcntl(input_stream->write_fd(), F_SETFL, arg))
- throw RunCommandError("fcntl F_SETFL failed: " + stringify(strerror(errno)));
- }
-
- /* Why do we fork twice, rather than install a SIGCHLD handler that writes to a pipe that
- * our pselect watches? Simple: Unix is retarded. APUE 12.8 says "Each thread has its own signal
- * mask, but the signal disposition is shared by all threads in the process.", which means
- * we have to do crazy things... */
-
- /* Temporarily disable SIGINT and SIGTERM to this thread, so that we can set up signal
- * handlers. */
- sigset_t intandterm;
- sigemptyset(&intandterm);
- sigaddset(&intandterm, SIGINT);
- sigaddset(&intandterm, SIGTERM);
- if (0 != pthread_sigmask(SIG_BLOCK, &intandterm, 0))
- throw InternalError(PALUDIS_HERE, "pthread_sigmask failed");
-
- pid_t child(fork());
- if (0 == child)
- {
- pid_t child_child(fork());
- if (0 == child_child)
- {
- /* The pid that does the exec */
-
- /* clear any SIGINT or SIGTERM handlers we inherit, and unblock signals */
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = SIG_DFL;
- act.sa_flags = 0;
- sigaction(SIGINT, &act, 0);
- sigaction(SIGTERM, &act, 0);
- if (0 != pthread_sigmask(SIG_UNBLOCK, &intandterm, 0))
- std::cerr << "pthread_sigmask failed: " + stringify(strerror(errno)) + "'" << std::endl;
- try
- {
- if (cmd.pipe_command_handler())
- {
- close(pipe_command_reader->read_fd());
- pipe_command_reader->clear_read_fd();
-
- close(pipe_command_response->write_fd());
- pipe_command_response->clear_write_fd();
- }
-
- if (cmd.captured_stdout_stream())
- {
- close(captured_stdout->read_fd());
- captured_stdout->clear_read_fd();
- }
-
- if (cmd.captured_stderr_stream())
- {
- close(captured_stderr->read_fd());
- captured_stderr->clear_read_fd();
- }
-
- if (cmd.output_stream())
- {
- close(output_stream->read_fd());
- output_stream->clear_read_fd();
- }
-
- if (cmd.input_stream())
- {
- close(input_stream->write_fd());
- input_stream->clear_write_fd();
- }
-
- close(internal_command_reader->read_fd());
- internal_command_reader->clear_read_fd();
- close(internal_command_reader->write_fd());
- internal_command_reader->clear_write_fd();
-
- do_chdir(cmd);
- do_env(cmd);
-
- if (cmd.pipe_command_handler())
- {
- setenv((cmd.pipe_command_env_var_prefix() + "_WRITE_FD").c_str(),
- stringify(pipe_command_reader->write_fd()).c_str(), 1);
- setenv((cmd.pipe_command_env_var_prefix() + "_READ_FD").c_str(),
- stringify(pipe_command_response->read_fd()).c_str(), 1);
- }
-
- if (cmd.captured_stdout_stream())
- {
- if (-1 == dup2(captured_stdout->write_fd(), 1))
- throw RunCommandError("captured stdout dup2 failed: " + stringify(strerror(errno)));
- close(captured_stdout->write_fd());
- }
- else if (-1 != stdout_write_fd)
- {
- if (-1 == dup2(stdout_write_fd, 1))
- throw RunCommandError("stdout dup2 failed: " + stringify(strerror(errno)));
- close(stdout_write_fd);
-
- if (-1 != stdout_close_fd)
- close(stdout_close_fd);
- }
-
- if (cmd.captured_stderr_stream())
- {
- if (-1 == dup2(captured_stderr->write_fd(), 2))
- throw RunCommandError("captured stderr dup2 failed: " + stringify(strerror(errno)));
- close(captured_stderr->write_fd());
- }
- else if (-1 != stderr_write_fd)
- {
- if (-1 == dup2(stderr_write_fd, 2))
- throw RunCommandError("stderr dup2 failed: " + stringify(strerror(errno)));
- close(stderr_write_fd);
-
- if (-1 != stderr_close_fd)
- close(stderr_close_fd);
- }
-
- if (cmd.output_stream())
- {
- int cmd_output_fd;
-
- if (-1 == cmd.output_fd())
- cmd_output_fd = dup(output_stream->write_fd());
- else
- cmd_output_fd = dup2(output_stream->write_fd(), cmd.output_fd());
-
- if (-1 == cmd_output_fd)
- throw RunCommandError("output dup2 failed: " + stringify(strerror(errno)));
-
- if (! cmd.output_fd_env_var().empty())
- setenv(cmd.output_fd_env_var().c_str(), stringify(cmd_output_fd).c_str(), 1);
- }
-
- if (cmd.input_stream())
- {
- int cmd_input_fd;
-
- if (-1 == cmd.input_fd())
- cmd_input_fd = dup(input_stream->read_fd());
- else
- cmd_input_fd = dup2(input_stream->read_fd(), cmd.input_fd());
-
- if (-1 == cmd_input_fd)
- throw RunCommandError("input dup2 failed: " + stringify(strerror(errno)));
-
- if (! cmd.input_fd_env_var().empty())
- setenv(cmd.input_fd_env_var().c_str(), stringify(cmd_input_fd).c_str(), 1);
- }
-
- do_uid_gid(cmd);
-
- execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char *>(0));
- throw RunCommandError("execl /bin/sh -c '" + command + "' failed:"
- + stringify(strerror(errno)));
- }
- catch (const Exception & e)
- {
- std::cerr << "exec of '" << command << "' failed due to exception '" << e.message()
- << "' (" << e.what() << ")" << std::endl;
- std::exit(123);
- }
- catch (...)
- {
- std::cerr << "exec of '" << command << "' failed due to unknown exception" << std::endl;
- std::exit(124);
- }
- }
- else if (-1 == child_child)
- {
- std::cerr << "fork failed: " + stringify(strerror(errno)) + "'" << std::endl;
- std::exit(125);
- }
- else
- {
- /* The pid that waits for the exec pid and then writes to the done pipe. */
-
- /* On SIGINT or SIGTERM, just output a notice. */
- struct sigaction act, old_act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = &wait_handler;
- act.sa_flags = SA_RESTART;
- sigaddset(&act.sa_mask, SIGINT);
- sigaddset(&act.sa_mask, SIGTERM);
-
- sigaction(SIGINT, 0, &old_act);
- if (SIG_DFL != old_act.sa_handler && SIG_IGN != old_act.sa_handler)
- sigaction(SIGINT, &act, 0);
- sigaction(SIGTERM, 0, &old_act);
- if (SIG_DFL != old_act.sa_handler && SIG_IGN != old_act.sa_handler)
- sigaction(SIGTERM, &act, 0);
-
- if (0 != pthread_sigmask(SIG_UNBLOCK, &intandterm, 0))
- std::cerr << "pthread_sigmask failed: " + stringify(strerror(errno)) + "'" << std::endl;
-
- if (cmd.pipe_command_handler())
- {
- close(pipe_command_reader->read_fd());
- pipe_command_reader->clear_read_fd();
- close(pipe_command_reader->write_fd());
- pipe_command_reader->clear_write_fd();
-
- close(pipe_command_response->read_fd());
- pipe_command_response->clear_read_fd();
- close(pipe_command_response->write_fd());
- pipe_command_response->clear_write_fd();
- }
-
- if (cmd.captured_stdout_stream())
- {
- close(captured_stdout->read_fd());
- captured_stdout->clear_read_fd();
- close(captured_stdout->write_fd());
- captured_stdout->clear_write_fd();
- }
-
- if (cmd.captured_stderr_stream())
- {
- close(captured_stderr->read_fd());
- captured_stderr->clear_read_fd();
- close(captured_stderr->write_fd());
- captured_stderr->clear_write_fd();
- }
-
- if (cmd.output_stream())
- {
- close(output_stream->read_fd());
- output_stream->clear_read_fd();
- close(output_stream->write_fd());
- output_stream->clear_write_fd();
- }
-
- if (cmd.input_stream())
- {
- close(input_stream->read_fd());
- input_stream->clear_read_fd();
- close(input_stream->write_fd());
- input_stream->clear_write_fd();
- }
-
- close(internal_command_reader->read_fd());
- internal_command_reader->clear_read_fd();
-
- int status(-1);
-
- stdout_write_fd = -1;
- stdout_close_fd = -1;
- stderr_write_fd = -1;
- stderr_close_fd = -1;
-
- int ret(-1);
- while (true)
- {
- if (-1 == waitpid(child_child, &status, 0))
- {
- if (errno == EINTR)
- std::cerr << "wait failed: '" + stringify(strerror(errno)) + "', trying once more" << std::endl;
- else
- {
- std::cerr << "wait failed: '" + stringify(strerror(errno)) + "'" << std::endl;
- break;
- }
- }
- else
- {
- ret = (WIFSIGNALED(status) ? WTERMSIG(status) + 128 : WEXITSTATUS(status));
- break;
- }
- }
-
- {
- SafeOFStream stream(internal_command_reader->write_fd());
- stream << "EXIT " << ret << std::endl;
- }
- }
-
- _exit(0);
- }
- else if (-1 == child)
- throw RunCommandError("fork failed: " + stringify(strerror(errno)));
- else
- {
- /* Our original pid */
-
- /* Restore SIGINT and SIGTERM handling */
- if (0 != pthread_sigmask(SIG_UNBLOCK, &intandterm, 0))
- throw InternalError(PALUDIS_HERE, "pthread_sigmask failed");
-
- if (cmd.pipe_command_handler())
- {
- close(pipe_command_reader->write_fd());
- pipe_command_reader->clear_write_fd();
- close(pipe_command_response->read_fd());
- pipe_command_response->clear_read_fd();
- }
-
- if (cmd.captured_stdout_stream())
- {
- close(captured_stdout->write_fd());
- captured_stdout->clear_write_fd();
- }
-
- if (cmd.captured_stderr_stream())
- {
- close(captured_stderr->write_fd());
- captured_stderr->clear_write_fd();
- }
-
- if (cmd.output_stream())
- {
- close(output_stream->write_fd());
- output_stream->clear_write_fd();
- }
-
- if (cmd.input_stream())
- {
- close(input_stream->read_fd());
- input_stream->clear_read_fd();
- }
-
- close(internal_command_reader->write_fd());
- internal_command_reader->clear_write_fd();
-
- std::string pipe_command_buffer, internal_command_buffer;
- std::string input_stream_pending;
- while (true)
- {
- fd_set read_fds, write_fds;
- FD_ZERO(&read_fds);
- FD_ZERO(&write_fds);
- int max_fd(0);
-
- if (cmd.pipe_command_handler())
- {
- FD_SET(pipe_command_reader->read_fd(), &read_fds);
- max_fd = std::max(max_fd, pipe_command_reader->read_fd());
- }
-
- if (cmd.captured_stdout_stream())
- {
- FD_SET(captured_stdout->read_fd(), &read_fds);
- max_fd = std::max(max_fd, captured_stdout->read_fd());
- }
-
- if (cmd.captured_stderr_stream())
- {
- FD_SET(captured_stderr->read_fd(), &read_fds);
- max_fd = std::max(max_fd, captured_stderr->read_fd());
- }
-
- if (cmd.output_stream() && -1 != output_stream->read_fd())
- {
- FD_SET(output_stream->read_fd(), &read_fds);
- max_fd = std::max(max_fd, output_stream->read_fd());
- }
-
- if (cmd.input_stream() && -1 != input_stream->write_fd())
- {
- FD_SET(input_stream->write_fd(), &write_fds);
- max_fd = std::max(max_fd, input_stream->write_fd());
- }
-
- FD_SET(internal_command_reader->read_fd(), &read_fds);
- max_fd = std::max(max_fd, internal_command_reader->read_fd());
-
- timespec tv;
- tv.tv_sec = 5;
- tv.tv_nsec = 0;
-
- int retval(pselect(max_fd + 1, &read_fds, &write_fds, 0, &tv, 0));
- if (-1 == retval)
- throw RunCommandError("select failed: " + stringify(strerror(errno)));
- else if (0 == retval)
- {
- Log::get_instance()->message("util.system.wait", ll_debug, lc_context) << "Waiting for child " << child << " to finish";
- continue;
- }
- else
- {
- char buf[1024];
-
- if (cmd.captured_stdout_stream() && FD_ISSET(captured_stdout->read_fd(), &read_fds))
- {
- int r;
- if (((r = read(captured_stdout->read_fd(), buf, 1024))) > 0)
- {
- *cmd.captured_stdout_stream() << std::string(buf, r);
- /* don't look at the other FDs yet to avoid a partial read from being snipped
- * when capturing output */
- continue;
- }
- }
-
- if (cmd.captured_stderr_stream() && FD_ISSET(captured_stderr->read_fd(), &read_fds))
- {
- int r;
- if (((r = read(captured_stderr->read_fd(), buf, 1024))) > 0)
- {
- *cmd.captured_stderr_stream() << std::string(buf, r);
- /* don't look at the other FDs yet to avoid a partial read from being snipped
- * when capturing output */
- continue;
- }
- }
-
- if (cmd.output_stream() && FD_ISSET(output_stream->read_fd(), &read_fds))
- {
- int r;
- if (((r = read(output_stream->read_fd(), buf, 1024))) > 0)
- *cmd.output_stream() << std::string(buf, r);
- }
-
- if (cmd.pipe_command_handler() && FD_ISSET(pipe_command_reader->read_fd(), &read_fds))
- {
- int r;
- if (((r = read(pipe_command_reader->read_fd(), buf, 1024))) > 0)
- pipe_command_buffer.append(std::string(buf, r));
- }
-
- if (FD_ISSET(internal_command_reader->read_fd(), &read_fds))
- {
- int r;
- if (((r = read(internal_command_reader->read_fd(), buf, 1024))) > 0)
- internal_command_buffer.append(std::string(buf, r));
- }
-
- if (cmd.input_stream() && (-1 != input_stream->write_fd())
- && FD_ISSET(input_stream->write_fd(), &write_fds))
- {
- while ((! input_stream_pending.empty()) || cmd.input_stream()->good())
- {
- if (input_stream_pending.empty() && cmd.input_stream()->good())
- {
- cmd.input_stream()->read(buf, 1024);
- input_stream_pending.assign(buf, cmd.input_stream()->gcount());
- }
-
- int w(write(input_stream->write_fd(), input_stream_pending.c_str(),
- input_stream_pending.length()));
-
- if (0 == w || (-1 == w && (errno == EAGAIN || errno == EWOULDBLOCK)))
- break;
- else if (-1 == w)
- throw RunCommandError("write failed: " + stringify(strerror(errno)));
- else
- input_stream_pending.erase(0, w);
- }
-
- if (input_stream_pending.empty() && ! cmd.input_stream()->good())
- {
- if (0 != close(input_stream->write_fd()))
- throw RunCommandError("close failed: " + stringify(strerror(errno)));
- input_stream->clear_write_fd();
- }
- }
- }
-
- while (! pipe_command_buffer.empty())
- {
- std::string::size_type n_p(pipe_command_buffer.find('\0'));
- if (std::string::npos == n_p)
- break;
-
- std::string op(pipe_command_buffer.substr(0, n_p));
- pipe_command_buffer.erase(0, n_p + 1);
-
- std::string response;
- if (cmd.pipe_command_handler())
- {
- response = cmd.pipe_command_handler()(op);
- Log::get_instance()->message("util.system.pipe_command_op", ll_debug, lc_context)
- << "Pipe command op '" << op << "' response '" << response << "'";
- }
- else
- Log::get_instance()->message("util.system.no_pipe_op_handler", ll_warning, lc_context)
- << "Pipe command op '" << op << "' was requested but no handler defined. This is probably a bug...";
-
- ssize_t n(0);
- while (! response.empty())
- {
- n = write(pipe_command_response->write_fd(), response.c_str(), response.length());
- if (-1 == n)
- throw InternalError(PALUDIS_HERE, "write failed: " + stringify(strerror(errno)));
- else
- response.erase(0, n);
- }
-
- char c(0);
- n = write(pipe_command_response->write_fd(), &c, 1);
- if (1 != n)
- throw InternalError(PALUDIS_HERE, "write failed: " + stringify(strerror(errno)));
- }
-
- while (! internal_command_buffer.empty())
- {
- std::string::size_type n_p(internal_command_buffer.find('\n'));
- if (std::string::npos == n_p)
- break;
-
- std::string op(internal_command_buffer.substr(0, n_p));
- internal_command_buffer.erase(0, n_p + 1);
- if (0 == op.compare(0, 4, "EXIT"))
- {
- op.erase(0, 4);
- int status(-1);
- Log::get_instance()->message("util.system.exit_op", ll_debug, lc_context) << "Got exit op '" << op << "'";
- if (-1 == waitpid(child, &status, 0))
- std::cerr << "wait failed: " + stringify(strerror(errno)) + "'" << std::endl;
- return destringify<int>(strip_leading(strip_trailing(op, " \r\n\t"), " \r\n\t"));
- }
- else
- throw InternalError(PALUDIS_HERE, "unknown op '" + op + "' on internal_command_buffer");
- }
- }
- }
-
- throw InternalError(PALUDIS_HERE, "should never be reached");
-}
-
-void
-paludis::become_command(const Command & cmd)
-{
- Context context("When becoming command '" + stringify(cmd.command()) + "':");
-
- std::string extras(build_extras(cmd));
- std::string command(cmd.command());
-
- cmd.echo_to_stderr();
- Log::get_instance()->message("util.system.execl_parent", ll_debug, lc_no_context) << "execl /bin/sh -c " << command
- << " " << extras;
-
- /* The double fork with the ignoring CLD in the middle may or may not be
- * necessary, and it probably isn't, but POSIX appears to suggest that
- * doing this will guarantee that the feeding process won't be a zombie,
- * whereas it doesn't if we don't. Unless it doesn't. */
-
- /* Temporarily disable SIGINT and SIGTERM to this thread, so that we can set up signal
- * handlers. */
- sigset_t intandterm;
- sigemptyset(&intandterm);
- sigaddset(&intandterm, SIGINT);
- sigaddset(&intandterm, SIGTERM);
- if (0 != pthread_sigmask(SIG_BLOCK, &intandterm, 0))
- throw InternalError(PALUDIS_HERE, "pthread_sigmask failed");
-
- std::shared_ptr<Pipe> input_stream;
- if (cmd.input_stream())
- {
- input_stream = std::make_shared<Pipe>();
-
- int cmd_input_fd;
-
- if (-1 == cmd.input_fd())
- cmd_input_fd = dup(input_stream->read_fd());
- else
- cmd_input_fd = dup2(input_stream->read_fd(), cmd.input_fd());
-
- if (-1 == cmd_input_fd)
- throw RunCommandError("input dup2 failed: " + stringify(strerror(errno)));
-
- if (! cmd.input_fd_env_var().empty())
- setenv(cmd.input_fd_env_var().c_str(), stringify(cmd_input_fd).c_str(), 1);
- }
-
- pid_t child(fork());
- if (0 == child)
- {
- if (cmd.input_stream())
- {
- close(input_stream->read_fd());
- input_stream->clear_read_fd();
- }
-
- /* Ignore CLD. POSIX may or may not say that if we do this, our child will
- * not become a zombie. */
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = SIG_IGN;
- act.sa_flags = 0;
- sigaction(SIGCLD, &act, 0);
-
- pid_t child_child(fork());
- if (0 == child_child)
- {
- /* Restore SIGINT and SIGTERM handling */
- if (0 != pthread_sigmask(SIG_UNBLOCK, &intandterm, 0))
- throw InternalError(PALUDIS_HERE, "pthread_sigmask failed");
-
- /* Feed in any input things */
- if (cmd.input_stream())
- {
- SafeOFStream output_stream(input_stream->write_fd());
- std::copy((std::istreambuf_iterator<char>(*cmd.input_stream())),
- std::istreambuf_iterator<char>(),
- std::ostreambuf_iterator<char>(output_stream));
-
- if (0 != close(input_stream->write_fd()))
- throw RunCommandError("close failed: " + stringify(strerror(errno)));
- input_stream->clear_write_fd();
- }
-
- /* we're done */
- _exit(0);
- }
- else
- {
- _exit(0);
- }
- }
- else if (-1 == child)
- throw RunCommandError("fork failed: " + stringify(strerror(errno)));
- else
- {
- /* Our original pid, which gets exec()ed */
-
- if (cmd.input_stream())
- {
- close(input_stream->write_fd());
- input_stream->clear_write_fd();
- }
-
- /* clear any SIGINT or SIGTERM handlers we inherit, and unblock signals */
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_handler = SIG_DFL;
- act.sa_flags = 0;
- sigaction(SIGINT, &act, 0);
- sigaction(SIGTERM, &act, 0);
- if (0 != pthread_sigmask(SIG_UNBLOCK, &intandterm, 0))
- std::cerr << "pthread_sigmask failed: " + stringify(strerror(errno)) + "'" << std::endl;
-
- /* wait until the child is done */
- while (true)
- {
- int status(-1);
- if (-1 == waitpid(child, &status, 0))
- {
- if (errno == EINTR)
- std::cerr << "wait failed: '" + stringify(strerror(errno)) + "', trying once more" << std::endl;
- else
- {
- std::cerr << "wait failed: '" + stringify(strerror(errno)) + "'" << std::endl;
- break;
- }
- }
- else
- break;
- }
-
- try
- {
- do_chdir(cmd);
- do_env(cmd);
- do_uid_gid(cmd);
-
- execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char *>(0));
- throw RunCommandError("execl /bin/sh -c '" + command + "' failed:"
- + stringify(strerror(errno)));
- }
- catch (const Exception & e)
- {
- std::cerr << "exec of '" << command << "' failed due to exception '" << e.message()
- << "' (" << e.what() << ")" << std::endl;
- std::exit(123);
- }
- catch (...)
- {
- std::cerr << "exec of '" << command << "' failed due to unknown exception" << std::endl;
- std::exit(124);
- }
- }
-
- throw InternalError(PALUDIS_HERE, "should never be reached");
-}
-
-std::string
-Command::command() const
-{
- return _imp->command;
-}
-
-std::string
-Command::chdir() const
-{
- return _imp->chdir;
-}
-
-bool
-Command::clearenv() const
-{
- return _imp->clearenv;
-}
-
-Command::ConstIterator
-Command::begin_setenvs() const
-{
- return ConstIterator(_imp->setenv_values.begin());
-}
-
-Command::ConstIterator
-Command::end_setenvs() const
-{
- return ConstIterator(_imp->setenv_values.end());
-}
-
-void
-Command::echo_to_stderr() const
-{
- if (! _imp->echo_to_stderr)
- return;
-
- std::cerr << command().c_str() << std::endl;
-}
-
-Command &
-Command::with_echo_to_stderr()
-{
- _imp->echo_to_stderr = true;
- return *this;
-}
-
-Command &
-Command::with_prefix_discard_blank_output()
-{
- _imp->prefix_discard_blank_output = true;
- return *this;
-}
-
-Command &
-Command::with_prefix_blank_lines()
-{
- _imp->prefix_blank_lines = true;
- return *this;
-}
-
-Command &
-Command::with_stdout_prefix(const std::string & s)
-{
- _imp->stdout_prefix = s;
- return *this;
-}
-
-Command &
-Command::with_stderr_prefix(const std::string & s)
-{
- _imp->stderr_prefix = s;
- return *this;
-}
-
-Command &
-Command::with_pipe_command_handler(
- const std::string & p,
- const std::function<std::string (const std::string &)> & f)
-{
- _imp->pipe_command_env_var_prefix = p;
- _imp->pipe_command_handler = f;
- return *this;
-}
-
-std::string
-Command::stdout_prefix() const
-{
- return _imp->stdout_prefix;
-}
-
-std::string
-Command::stderr_prefix() const
-{
- return _imp->stderr_prefix;
-}
-
-bool
-Command::prefix_discard_blank_output() const
-{
- return _imp->prefix_discard_blank_output;
-}
-
-bool
-Command::prefix_blank_lines() const
-{
- return _imp->prefix_blank_lines;
-}
-
-const std::string
-Command::pipe_command_env_var_prefix() const
-{
- return _imp->pipe_command_env_var_prefix;
-}
-
-const std::function<std::string (const std::string &)> &
-Command::pipe_command_handler() const
-{
- return _imp->pipe_command_handler;
-}
-
-std::ostream *
-Command::captured_stdout_stream() const
-{
- return _imp->captured_stdout_stream;
-}
-
-std::ostream *
-Command::captured_stderr_stream() const
-{
- return _imp->captured_stderr_stream;
-}
-
-std::istream *
-Command::input_stream() const
-{
- return _imp->input_stream;
-}
-
-int
-Command::input_fd() const
-{
- return _imp->input_fd;
-}
-
-const std::string
-Command::input_fd_env_var() const
-{
- return _imp->input_fd_env_var;
-}
-
-std::ostream *
-Command::output_stream() const
-{
- return _imp->output_stream;
-}
-
-int
-Command::output_fd() const
-{
- return _imp->output_fd;
-}
-
-const std::string
-Command::output_fd_env_var() const
-{
- return _imp->output_fd_env_var;
-}
-
-bool
-Command::ptys() const
-{
- return _imp->ptys;
-}
-
std::string
paludis::get_user_name(const uid_t u)
{
@@ -1462,5 +134,3 @@ paludis::get_group_name(const gid_t u)
}
}
-template class WrappedForwardIterator<Command::ConstIteratorTag, const std::pair<const std::string, std::string> >;
-
diff --git a/paludis/util/system.hh b/paludis/util/system.hh
index 85dc1f9..9d06640 100644
--- a/paludis/util/system.hh
+++ b/paludis/util/system.hh
@@ -21,10 +21,6 @@
#define PALUDIS_GUARD_PALUDIS_UTIL_SYSTEM_HH 1
#include <paludis/util/exception.hh>
-#include <paludis/util/pimp.hh>
-#include <paludis/util/wrapped_forward_iterator-fwd.hh>
-#include <memory>
-#include <functional>
#include <string>
#include <sys/types.h>
@@ -40,8 +36,6 @@
namespace paludis
{
- class FSEntry;
-
/**
* Thrown if getenv_or_error fails.
*
@@ -61,22 +55,6 @@ namespace paludis
};
/**
- * Thrown if fork, wait or chdir fail when running a command.
- *
- * \ingroup g_exceptions
- * \ingroup g_system
- * \nosubgrouping
- */
- class PALUDIS_VISIBLE RunCommandError : public Exception
- {
- public:
- /**
- * Constructor.
- */
- RunCommandError(const std::string & message) throw ();
- };
-
- /**
* Fetch the value of environment variable key, or def if the variable is
* not defined.
*
@@ -100,332 +78,6 @@ namespace paludis
std::string kernel_version() PALUDIS_VISIBLE;
/**
- * A command to be run.
- *
- * \see PStream
- * \see run_command
- * \ingroup g_system
- * \nosubgrouping
- */
- class PALUDIS_VISIBLE Command :
- private Pimp<Command>
- {
- public:
- ///\name Basic operations
- ///\{
-
- Command(const std::string &);
- Command(const char * const);
- Command(const Command &);
- const Command & operator= (const Command &);
- ~Command();
-
- ///\}
-
- ///\name Change command execution options
- ///\{
-
- /**
- * Include a chdir before we run our command.
- */
- Command & with_chdir(const FSEntry &);
-
- /**
- * Add a setenv before we run our command.
- */
- Command & with_setenv(const std::string &, const std::string &);
-
- /**
- * Remove (most) existing environment variables before
- * setting those added with with_setenv.
- *
- * \since 0.36
- */
- Command & with_clearenv();
-
- /**
- * Run our command sandboxed.
- */
- Command & with_sandbox();
-
- /**
- * Run our command sydboxed
- *
- * \since 0.38
- */
- Command & with_sydbox();
-
- /**
- * Echo the command to be run to stderr before running it.
- */
- Command & with_echo_to_stderr();
-
- /**
- * Add a setuid and setgid before running our command.
- */
- Command & with_uid_gid(const uid_t, const gid_t);
-
- /**
- * Prefix stdout output with this.
- */
- Command & with_stdout_prefix(const std::string &);
-
- /**
- * Prefix stderr output with this.
- */
- Command & with_stderr_prefix(const std::string &);
-
- /**
- * If prefixing, and if the output contains only blanks, don't display
- * any output.
- */
- Command & with_prefix_discard_blank_output();
-
- /**
- * If prefixing, prefix blank lines too.
- */
- Command & with_prefix_blank_lines();
-
- /**
- * Specify a pipe command handler.
- *
- * \param env_var_prefix has _READ_FD and _WRITE_FD appended to it.
- * \since 0.44, previously was always PALUDIS_PIPE_COMMAND.
- */
- Command & with_pipe_command_handler(
- const std::string & env_var_prefix,
- const std::function<std::string (const std::string &)> &);
-
- /**
- * Specify a stream to which stdout is captured and written.
- */
- Command & with_captured_stdout_stream(std::ostream * const);
-
- /**
- * Specify a stream to which stderr is captured and written.
- *
- * \since 0.30
- */
- Command & with_captured_stderr_stream(std::ostream * const);
-
- /**
- * Capture input from a particular FD to a stream.
- *
- * May only be called once.
- *
- * \param fd Read to this FD. If -1, pick an unused FD.
- * \param env_var If not empty, put the FD chosen in this env var.
- *
- * \since 0.50
- */
- Command & with_output_stream(
- std::ostream * const,
- int fd,
- const std::string & env_var);
-
- /**
- * Send the contents of a stream in via a particular FD.
- *
- * May only be called once.
- *
- * \param fd Send the stream to this FD. If -1, pick an unused FD.
- * \param env_var If not empty, put the FD chosen in this env var.
- *
- * \since 0.40
- */
- Command & with_input_stream(
- std::istream * const,
- int fd,
- const std::string & env_var);
-
- /**
- * Use ptys instead of pipes to capture stdout and/or stderr.
- *
- * \since 0.38
- */
- Command & with_ptys();
-
- ///\}
-
- ///\name Fetch command execution options
- ///\{
-
- /**
- * Our command, as a string.
- */
- std::string command() const;
-
- /**
- * Where to chdir, as a string.
- */
- std::string chdir() const;
-
- /**
- * Echo ourself to stderr.
- */
- void echo_to_stderr() const;
-
- /**
- * The uid for setuid.
- */
- std::shared_ptr<const uid_t> uid() const;
-
- /**
- * The gid for setgid.
- */
- std::shared_ptr<const gid_t> gid() const;
-
- /**
- * The stdout prefix.
- */
- std::string stdout_prefix() const;
-
- /**
- * The stderr prefix.
- */
- std::string stderr_prefix() const;
-
- /**
- * If prefixing, and if the output contains only blanks, don't display
- * any output?
- */
- bool prefix_discard_blank_output() const;
-
- /**
- * Prefix blank lines?
- */
- bool prefix_blank_lines() const;
-
- /**
- * The pipe command handler.
- */
- const std::function<std::string (const std::string &)> & pipe_command_handler() const;
-
- /**
- * The pipe command env var prefix.
- *
- * \since 0.44
- */
- const std::string pipe_command_env_var_prefix() const;
-
- /**
- * The captured stdout stream, or null.
- */
- std::ostream * captured_stdout_stream() const;
-
- /**
- * The captured stderr stream, or null.
- *
- * \since 0.30
- */
- std::ostream * captured_stderr_stream() const;
-
- /**
- * The input stream, or null.
- *
- * \since 0.40
- */
- std::istream * input_stream() const;
-
- /**
- * The input FD, if input_stream() not null.
- *
- * \since 0.40
- */
- int input_fd() const;
-
- /**
- * The input FD env var, if input_stream() not null.
- *
- * \since 0.40
- */
- const std::string input_fd_env_var() const;
-
- /**
- * The input stream, or null.
- *
- * \since 0.50
- */
- std::ostream * output_stream() const;
-
- /**
- * The output FD, if output_stream() not null.
- *
- * \since 0.50
- */
- int output_fd() const;
-
- /**
- * The output FD env var, if output_stream() not null.
- *
- * \since 0.50
- */
- const std::string output_fd_env_var() const;
-
- /**
- * Uses ptys instead of pipes?
- *
- * \since 0.38
- */
- bool ptys() const;
-
- /**
- * Should we clear existing environment variables?
- *
- * \since 0.36
- */
- bool clearenv() const;
-
- ///\}
-
- ///\name Iterate over our setenvs.
- ///\{
-
- struct ConstIteratorTag;
- typedef WrappedForwardIterator<ConstIteratorTag, const std::pair<const std::string, std::string> > ConstIterator;
- ConstIterator begin_setenvs() const;
- ConstIterator end_setenvs() const;
-
- ///\}
- };
-
- /**
- * Run a command, wait for it to terminate and return its exit status.
- *
- * \ingroup g_system
- */
- int run_command(const Command & cmd) PALUDIS_VISIBLE
- PALUDIS_ATTRIBUTE((warn_unused_result));
-
- /**
- * Become another command.
- *
- * Actions that change the initial state (uid / gid, env) work, as do input
- * streams, but output redirection does not. Pipe commands don't work, but
- * could be made to.
- *
- * \ingroup g_system
- * \since 0.40.1
- */
- void become_command(const Command & cmd) PALUDIS_VISIBLE PALUDIS_ATTRIBUTE((noreturn));
-
- /**
- * Set the stderr and close for stdout fds used by run_command and
- * run_command_in_directory.
- *
- * \ingroup g_system
- */
- void set_run_command_stdout_fds(const int, const int) PALUDIS_VISIBLE;
-
- /**
- * Set the stderr and close for stderr fds used by run_command and
- * run_command_in_directory.
- *
- * \ingroup g_system
- */
- void set_run_command_stderr_fds(const int, const int) PALUDIS_VISIBLE;
-
- /**
* Fetch the username for a uid, or the uid as a string if not available.
*
* \ingroup g_system
diff --git a/paludis/util/system_TEST.cc b/paludis/util/system_TEST.cc
index a004e58..98012ef 100644
--- a/paludis/util/system_TEST.cc
+++ b/paludis/util/system_TEST.cc
@@ -31,42 +31,6 @@
using namespace test;
using namespace paludis;
-namespace
-{
- void repeatedly_log(bool & b)
- {
- while (! b)
- sched_yield();
-
- for (int i(0) ; i < 1000 ; ++i)
- Log::get_instance()->message("test.system", ll_debug, lc_context) << "logging stuff";
- }
-
- void repeatedly_run_command(bool & b)
- {
- while (! b)
- sched_yield();
-
- for (int i(0) ; i < 100 ; ++i)
- if (0 != run_command("/usr/bin/env true"))
- throw InternalError(PALUDIS_HERE, "true isn't");
- }
-
- std::string response_handler(const std::string & s)
- {
- if (s == "ONE")
- return "1";
- else if (s == "TWO")
- return "2";
- else if (s == "THREE")
- return "3";
- else if (s == "FOUR")
- return "4";
- else
- return "9";
- }
-}
-
namespace test_cases
{
struct GetenvWithDefaultTest : TestCase
@@ -110,277 +74,5 @@ namespace test_cases
#endif
}
} test_kernel_version;
-
- struct RunCommandTest : TestCase
- {
- RunCommandTest() : TestCase("run_command") { }
-
- void run()
- {
- TEST_CHECK(0 == run_command("true"));
- TEST_CHECK(0 != run_command("false"));
- TEST_CHECK_EQUAL(77, run_command("exit 77"));
- }
- } test_run_command;
-
- struct RunCommandMutexTest : TestCase
- {
- RunCommandMutexTest() : TestCase("run_command mutex") { }
-
- void run()
- {
- ThreadPool pool;
- bool b(false);
- pool.create_thread(std::bind(&repeatedly_run_command, std::ref(b)));
- pool.create_thread(std::bind(&repeatedly_log, std::ref(b)));
- b = true;
- }
- } test_run_command_mutex;
-
- struct RunCommandInDirectoryTest : TestCase
- {
- RunCommandInDirectoryTest() : TestCase("run_command_in_directory") { }
-
- void run()
- {
- FSEntry dir("system_TEST_dir");
- TEST_CHECK(dir.is_directory());
-
- TEST_CHECK_EQUAL(run_command(Command("touch in_directory").with_chdir(dir)), 0);
- TEST_CHECK(FSEntry(dir / "in_directory").exists());
- TEST_CHECK_EQUAL(run_command(Command("rm in_directory").with_chdir(dir)), 0);
- TEST_CHECK(! FSEntry(dir / "in_directory").exists());
- }
- } test_run_command_in_directory;
-
- struct MakeEnvCommandTest : TestCase
- {
- MakeEnvCommandTest() : TestCase("make_env_command") { }
-
- void run()
- {
- TEST_CHECK(0 != run_command(("printenv PALUDUS_TEST_ENV_VAR")));
- TEST_CHECK(0 == run_command(("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")));
- TEST_CHECK(0 == run_command(Command("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "")));
- TEST_CHECK(0 == run_command(Command("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "foo")));
- TEST_CHECK(0 != run_command(Command("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "foo")));
- TEST_CHECK(0 != run_command(("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")));
- TEST_CHECK(0 != run_command(Command("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "")));
- TEST_CHECK(0 == run_command(Command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "foo")));
- TEST_CHECK(0 != run_command(("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")));
- TEST_CHECK(0 != run_command(Command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "")));
- TEST_CHECK(0 != run_command(Command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "bar")));
- }
- } test_make_env_command;
-
- struct MakeEnvCommandQuoteTest : TestCase
- {
- MakeEnvCommandQuoteTest() : TestCase("make_env_command quotes") { }
-
- void run()
- {
- TEST_CHECK(0 == run_command(Command(
- "bash -c '[[ x$PALUDUS_TEST_ENV_VAR == \"x....\" ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "....")));
- TEST_CHECK(0 == run_command(Command(
- "bash -c '[[ x$PALUDUS_TEST_ENV_VAR == \"x..'\"'\"'..\" ]]'")
- .with_setenv("PALUDUS_TEST_ENV_VAR", "..'..")));
- }
- } test_make_env_command_quotes;
-
- struct PipeCommandTest : TestCase
- {
- PipeCommandTest() : TestCase("pipe command") { }
-
- void run()
- {
- TEST_CHECK_EQUAL(run_command(Command("bash system_TEST_dir/pipe_test.bash ONE TWO")
- .with_pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler)), 12);
- TEST_CHECK_EQUAL(run_command(Command("bash system_TEST_dir/pipe_test.bash THREE FOUR")
- .with_pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler)), 34);
- }
- } test_pipe_command;
-
- struct CapturedTest : TestCase
- {
- CapturedTest() : TestCase("captured stdout") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK_EQUAL(run_command(Command("echo hi").with_captured_stdout_stream(&s)), 0);
- std::string line;
- TEST_CHECK(std::getline(s, line));
- TEST_CHECK_EQUAL(line, "hi");
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured;
-
- struct CapturedErrTest : TestCase
- {
- CapturedErrTest() : TestCase("captured stderr") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK_EQUAL(run_command(Command("echo hi 1>&2").with_captured_stderr_stream(&s)), 0);
- std::string line;
- TEST_CHECK(std::getline(s, line));
- TEST_CHECK_EQUAL(line, "hi");
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_err;
-
- struct CapturedExtraOutTest : TestCase
- {
- CapturedExtraOutTest() : TestCase("captured extra output") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK_EQUAL(run_command(Command("echo hi 1>&$TEST_PIPE_FD").with_output_stream(&s, -1, "TEST_PIPE_FD")), 0);
- std::string line;
- TEST_CHECK(std::getline(s, line));
- TEST_CHECK_EQUAL(line, "hi");
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_extra_out;
-
- struct CapturedNoExistTest : TestCase
- {
- CapturedNoExistTest() : TestCase("captured nonexistent command") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK(run_command(Command("thiscommanddoesnotexist 2>/dev/null").with_captured_stdout_stream(&s)) != 0);
- std::string line;
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_no_exist;
-
- struct CapturedSilentFailTest : TestCase
- {
- CapturedSilentFailTest() : TestCase("captured silent fail") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK(run_command(Command("test -e /doesnotexist").with_captured_stdout_stream(&s)) != 0);
- std::string line;
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_silent_fail;
-
- struct CapturedFailTest : TestCase
- {
- CapturedFailTest() : TestCase("captured fail") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK(run_command(Command("cat /doesnotexist 2>&1").with_captured_stdout_stream(&s)) != 0);
- std::string line;
- TEST_CHECK(std::getline(s, line));
- TEST_CHECK(! line.empty());
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_fail;
-
- struct CapturedPipeCommandTest : TestCase
- {
- CapturedPipeCommandTest() : TestCase("captured pipe command") { }
-
- void run()
- {
- std::stringstream s;
- TEST_CHECK_EQUAL(run_command(Command("bash system_TEST_dir/captured_pipe_test.bash ONE TWO THREE")
- .with_pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler)
- .with_captured_stdout_stream(&s)),
- 13);
- std::string line;
- TEST_CHECK(std::getline(s, line));
- TEST_CHECK_EQUAL(line, "2");
- TEST_CHECK(! std::getline(s, line));
- }
- } test_captured_pipe_command;
-
- struct StdinCommandTest : TestCase
- {
- StdinCommandTest() : TestCase("stdin") { }
-
- void run()
- {
- std::stringstream is, os;
- for (int n(0) ; n < 300 ; ++n)
- is << "I chased a bug around a tree\n";
-
- TEST_CHECK_EQUAL(run_command(Command("cat")
- .with_input_stream(&is, 0, "")
- .with_captured_stdout_stream(&os)
- ),
- 0);
-
- std::string line;
- for (int n(0) ; n < 300 ; ++n)
- {
- std::getline(os, line);
- TEST_CHECK_EQUAL(line, "I chased a bug around a tree");
- }
-
- TEST_CHECK(! std::getline(os, line));
- }
- } test_stdin;
-
- struct InputCommandTest : TestCase
- {
- InputCommandTest() : TestCase("input") { }
-
- void run()
- {
- std::stringstream is, os;
- for (int n(0) ; n < 300 ; ++n)
- is << "The cat crept into the crypt\n";
-
- TEST_CHECK_EQUAL(run_command(Command("cat <&$THE_FD")
- .with_input_stream(&is, -1, "THE_FD")
- .with_captured_stdout_stream(&os)
- ),
- 0);
-
- std::string line;
- for (int n(0) ; n < 300 ; ++n)
- {
- std::getline(os, line);
- TEST_CHECK_EQUAL(line, "The cat crept into the crypt");
- }
-
- TEST_CHECK(! std::getline(os, line));
- }
- } test_input;
-
- struct BecomeChildCommandTest : TestCase
- {
- BecomeChildCommandTest() : TestCase("become child") { }
-
- void run()
- {
- std::stringstream os;
- Command cmd("./system_TEST_become_child");
- cmd
- .with_captured_stdout_stream(&os)
- ;
-
- TEST_CHECK_EQUAL(123, run_command(cmd));
- TEST_CHECK_EQUAL(os.str(), "giant space monkey");
- }
- } test_become_child;
}
diff --git a/paludis/util/system_TEST_become_child.cc b/paludis/util/system_TEST_become_child.cc
deleted file mode 100644
index eaf9841..0000000
--- a/paludis/util/system_TEST_become_child.cc
+++ /dev/null
@@ -1,36 +0,0 @@
-/* vim: set sw=4 sts=4 et foldmethod=syntax : */
-
-/*
- * Copyright (c) 2009 Ciaran McCreesh
- *
- * This file is part of the Paludis package manager. Paludis is free software;
- * you can redistribute it and/or modify it under the terms of the GNU General
- * Public License version 2, as published by the Free Software Foundation.
- *
- * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <paludis/util/system.hh>
-#include <sstream>
-#include <cstdlib>
-
-using namespace paludis;
-
-int main(int, char *[])
-{
- std::stringstream is("giant space monkey");
- Command cmd("cat ; exit 123");
- cmd
- .with_input_stream(&is, 0, "");
-
- become_command(cmd);
- return 27;
-}
-
diff --git a/paludis/util/system_TEST_cleanup.sh b/paludis/util/system_TEST_cleanup.sh
deleted file mode 100755
index 4317415..0000000
--- a/paludis/util/system_TEST_cleanup.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/usr/bin/env bash
-# vim: set ft=sh sw=4 sts=4 et :
-
-if [ -d system_TEST_dir ] ; then
- rm -fr system_TEST_dir
-else
- true
-fi
-
diff --git a/paludis/util/system_TEST_setup.sh b/paludis/util/system_TEST_setup.sh
deleted file mode 100755
index 7bee967..0000000
--- a/paludis/util/system_TEST_setup.sh
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/env bash
-# vim: set ft=sh sw=4 sts=4 et :
-
-mkdir system_TEST_dir || exit 2
-cd system_TEST_dir || exit 3
-
-cat <<'END' > pipe_test.bash
-#!/usr/bin/env bash
-
-echo "$1" | tr "\n" "\0" 1>&$PALUDIS_PIPE_COMMAND_WRITE_FD
-response1=
-while true ; do
- read -n 1 -u $PALUDIS_PIPE_COMMAND_READ_FD c
- [[ "$c" == $'\0' ]] && break
- response1="${response1}${c}"
-done
-
-echo "$2" | tr "\n" "\0" 1>&$PALUDIS_PIPE_COMMAND_WRITE_FD
-response2=
-while true ; do
- read -n 1 -u $PALUDIS_PIPE_COMMAND_READ_FD c
- [[ "$c" == $'\0' ]] && break
- response2="${response2}${c}"
-done
-
-exit $response1$response2
-END
-
-cat <<'END' > captured_pipe_test.bash
-#!/usr/bin/env bash
-
-echo "$1" | tr "\n" "\0" 1>&$PALUDIS_PIPE_COMMAND_WRITE_FD
-response1=
-while true ; do
- read -n 1 -u $PALUDIS_PIPE_COMMAND_READ_FD c
- [[ "$c" == $'\0' ]] && break
- response1="${response1}${c}"
-done
-
-echo "$2" | tr "\n" "\0" 1>&$PALUDIS_PIPE_COMMAND_WRITE_FD
-response2=
-while true ; do
- read -n 1 -u $PALUDIS_PIPE_COMMAND_READ_FD c
- [[ "$c" == $'\0' ]] && break
- response2="${response2}${c}"
-done
-
-echo "$3" | tr "\n" "\0" 1>&$PALUDIS_PIPE_COMMAND_WRITE_FD
-response3=
-while true ; do
- read -n 1 -u $PALUDIS_PIPE_COMMAND_READ_FD c
- [[ "$c" == $'\0' ]] && break
- response3="${response3}${c}"
-done
-
-echo $response2
-
-exit $response1$response3
-END
-
diff --git a/paludis/util/test_extras.cc b/paludis/util/test_extras.cc
index de00468..de2f48a 100644
--- a/paludis/util/test_extras.cc
+++ b/paludis/util/test_extras.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2006, 2007 Ciaran McCreesh
+ * Copyright (c) 2006, 2007, 2010 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -51,9 +51,6 @@ namespace
{
test::set_exception_to_debug_string(&verbose_exception_to_debug_string);
- if (getenv_with_default("PALUDIS_TESTS_KEEP_STDERR", "").empty())
- set_run_command_stderr_fds(dev_null_pid, -1);
-
if (getenv_with_default("PALUDIS_TESTS_KEEP_LOG", "").empty())
Log::get_instance()->set_log_stream(s);