execute command and check if exit code is zero
| 67 | |
| 68 | //! execute command and check if exit code is zero |
| 69 | static void check_exec(const std::string& cmd) { |
| 70 | #if MGB_ENABLE_DEBUG_UTIL |
| 71 | debug::ScopedForkWarningSupress no_fork_warning; |
| 72 | #endif |
| 73 | std::string out; |
| 74 | std::array<char, 128> buffer; |
| 75 | FILE* pipe = popen((cmd + " 2>&1").c_str(), "r"); |
| 76 | mgb_throw_if( |
| 77 | !pipe, SystemError, "popen() for cmd %s failed: %s", cmd.c_str(), |
| 78 | strerror(errno)); |
| 79 | std::unique_ptr<FILE, int (*)(FILE*)> pipe_close{pipe, ::pclose}; |
| 80 | while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) { |
| 81 | out += buffer.data(); |
| 82 | } |
| 83 | pipe_close.release(); |
| 84 | int ret = pclose(pipe); |
| 85 | mgb_throw_if( |
| 86 | ret, SystemError, |
| 87 | "command %s failed: return code=%d; captured output:\n%s", cmd.c_str(), |
| 88 | ret, out.c_str()); |
| 89 | } |
| 90 | |
| 91 | public: |
| 92 | ExecutableHelperImpl() { |