12 #include "includes/subprocess.h"
13 #include "includes/process.h"
14 #include "includes/pipe_reader.h"
15 #include "includes/assert.h"
17 namespace linuxdeploy {
18 namespace subprocess {
19 subprocess::subprocess(std::initializer_list<std::string> args, subprocess_env_map_t env)
20 : subprocess(std::vector<std::string>(args), std::move(env)) {}
22 subprocess::subprocess(std::vector<std::string> args, subprocess_env_map_t env)
23 : args_(std::move(args)), env_(std::move(env)) {
25 util::assert::assert_not_empty(args_);
28 subprocess_result subprocess::run() const {
29 process proc{args_, env_};
31 // create pipe readers and empty buffers for both stdout and stderr
32 // we manage them in this (admittedly, kind of complex-looking) array so we can later easily perform the
33 // operations in a loop
34 std::array<std::pair<pipe_reader, subprocess_result_buffer_t>, 2> buffers{
35 std::make_pair(pipe_reader(proc.stdout_fd()), subprocess_result_buffer_t{}),
36 std::make_pair(pipe_reader(proc.stderr_fd()), subprocess_result_buffer_t{}),
40 for (auto& pair : buffers) {
41 // make code more readable
42 auto& reader = pair.first;
43 auto& buffer = pair.second;
45 // read some bytes into smaller intermediate buffer to prevent either of the pipes to overflow
46 // the results are immediately appended to the main buffer
47 subprocess_result_buffer_t intermediate_buffer(4096);
49 // (try to) read all available data from pipe
51 const auto bytes_read = reader.read(intermediate_buffer);
53 if (bytes_read == 0) {
57 // append to main buffer
58 buffer.reserve(buffer.size() + bytes_read);
59 std::copy(intermediate_buffer.begin(), (intermediate_buffer.begin() + bytes_read),
60 std::back_inserter(buffer));
64 // do-while might be a little more elegant, but we can save this one unnecessary sleep, so...
65 if (proc.is_running()) {
67 std::this_thread::sleep_for(std::chrono::milliseconds(50));
73 // make sure contents are null-terminated
74 buffers[0].second.emplace_back('\0');
75 buffers[1].second.emplace_back('\0');
77 auto exit_code = proc.close();
79 return subprocess_result{exit_code, buffers[0].second, buffers[1].second};
82 std::string subprocess::check_output() const {
83 const auto result = run();
85 if (result.exit_code() != 0) {
86 throw std::logic_error{"subprocess failed (exit code " + std::to_string(result.exit_code()) + ")"};
89 return result.stdout_string();