diff --git a/include/crucible/execpipe.h b/include/crucible/execpipe.h deleted file mode 100644 index ac14729..0000000 --- a/include/crucible/execpipe.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef CRUCIBLE_EXECPIPE_H -#define CRUCIBLE_EXECPIPE_H - -#include "crucible/fd.h" - -#include -#include -#include - -namespace crucible { - using namespace std; - - void redirect_stdin(const Fd &child_fd); - void redirect_stdin_stdout(const Fd &child_fd); - void redirect_stdin_stdout_stderr(const Fd &child_fd); - void redirect_stdout(const Fd &child_fd); - void redirect_stdout_stderr(const Fd &child_fd); - - // Open a pipe (actually socketpair) to child process, then execute code in that process. - // e.g. popen([] () { system("echo Hello, World!"); }); - // Forked process will exit when function returns. - Fd popen(function f, function import_fd_fn = redirect_stdin_stdout); - - // Read all the data from fd into a string - string read_all(Fd fd, size_t max_bytes = numeric_limits::max(), size_t chunk_bytes = 4096); -}; - -#endif // CRUCIBLE_EXECPIPE_H diff --git a/lib/Makefile b/lib/Makefile index 43113f7..b54492d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -4,7 +4,6 @@ OBJS = \ crc64.o \ chatter.o \ error.o \ - execpipe.o \ extentwalker.o \ fd.o \ fs.o \ diff --git a/lib/execpipe.cc b/lib/execpipe.cc deleted file mode 100644 index 68cfcbd..0000000 --- a/lib/execpipe.cc +++ /dev/null @@ -1,100 +0,0 @@ -#include "crucible/execpipe.h" - -#include "crucible/chatter.h" -#include "crucible/error.h" -#include "crucible/process.h" - -#include -#include -#include -#include - -namespace crucible { - using namespace std; - - void - redirect_stdin(const Fd &child_fd) - { - dup2_or_die(child_fd, STDIN_FILENO); - } - - void - redirect_stdin_stdout(const Fd &child_fd) - { - dup2_or_die(child_fd, STDOUT_FILENO); - dup2_or_die(child_fd, STDIN_FILENO); - } - - void - redirect_stdin_stdout_stderr(const Fd &child_fd) - { - dup2_or_die(child_fd, STDERR_FILENO); - dup2_or_die(child_fd, STDOUT_FILENO); - dup2_or_die(child_fd, STDIN_FILENO); - } - - void - redirect_stdout_stderr(const Fd &child_fd) - { - dup2_or_die(child_fd, STDERR_FILENO); - dup2_or_die(child_fd, STDOUT_FILENO); - } - - void - redirect_stdout(const Fd &child_fd) - { - dup2_or_die(child_fd, STDOUT_FILENO); - } - - void - redirect_stderr(const Fd &child_fd) - { - dup2_or_die(child_fd, STDERR_FILENO); - } - - Fd popen(function f, function import_fd_fn) - { - Fd parent_fd, child_fd; - { - pair fd_pair = socketpair_or_die(); - parent_fd = fd_pair.first; - child_fd = fd_pair.second; - } - - pid_t fv; - DIE_IF_MINUS_ONE(fv = fork()); - - if (fv) { - child_fd->close(); - return parent_fd; - } else { - int rv = EXIT_FAILURE; - catch_all([&]() { - parent_fd->close(); - import_fd_fn(child_fd); - - rv = f(); - }); - _exit(rv); - } - } - - string - read_all(Fd fd, size_t max_bytes, size_t chunk_bytes) - { - char buf[chunk_bytes]; - string str; - size_t rv; - while (1) { - read_partial_or_die(fd, static_cast(buf), chunk_bytes, rv); - if (rv == 0) { - break; - } - if (max_bytes - str.size() < rv) { - THROW_ERROR(out_of_range, "Output size limit " << max_bytes << " exceeded by appending " << rv << " bytes read to " << str.size() << " already in string"); - } - str.append(buf, rv); - } - return str; - } -} diff --git a/test/Makefile b/test/Makefile index e4a4c3f..7de6261 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,7 +1,6 @@ PROGRAMS = \ chatter \ crc64 \ - execpipe \ fd \ limits \ path \ diff --git a/test/execpipe.cc b/test/execpipe.cc deleted file mode 100644 index e28886f..0000000 --- a/test/execpipe.cc +++ /dev/null @@ -1,64 +0,0 @@ -#include "tests.h" - -#include "crucible/execpipe.h" - -#include -#include -#include -#include -#include - -#include - -using namespace crucible; -using namespace std; - -#if 1 // Needs rework -static inline -void -test_hello_world() -{ - // alarm(9); - Fd fd = popen([]() { return system("echo Hello, World!"); }); - char buf[1024]; - size_t rv = -1; - read_partial_or_die(fd, buf, rv); - assert(rv > 0); - string b(buf, buf + rv - 1); - // cerr << "hello_world says: '" << b << "'" << endl; - assert(b == "Hello, World!"); -} - -static inline -void -test_read_limit(size_t limit = 4096) -{ - alarm(9); - Fd fd = popen([]() { return system("yes Hello!"); }); - try { - string b = read_all(fd, limit); - } catch (out_of_range &re) { - return; - } - assert(!"no exception thrown by read_all"); -} -#endif - -namespace crucible { - extern bool assert_no_leaked_fds(); -}; - -int -main(int, char**) -{ -#if 1 - RUN_A_TEST(test_hello_world()); - assert(assert_no_leaked_fds()); - RUN_A_TEST(test_read_limit(4095)); - RUN_A_TEST(test_read_limit(4096)); - RUN_A_TEST(test_read_limit(4097)); - assert(assert_no_leaked_fds()); -#endif - - exit(EXIT_SUCCESS); -}