| 67 | } |
| 68 | |
| 69 | std::optional<std::string> OHDUtil::run_command_out(const std::string& command, |
| 70 | const bool debug) { |
| 71 | auto console = openhd::log::get_default(); |
| 72 | try { |
| 73 | if (debug) { |
| 74 | console->debug("run command out begin [{}]", command); |
| 75 | } |
| 76 | std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), |
| 77 | pclose); |
| 78 | if (!pipe) { |
| 79 | // if the pipe opening fails, this doesn't mean the command failed (see |
| 80 | // above) But rather we need to find a different way to implement this |
| 81 | // functionality on this platform. |
| 82 | openhd::log::get_default()->error("Cannot execute command [{}]", command); |
| 83 | return std::nullopt; |
| 84 | } |
| 85 | std::string raw_value; |
| 86 | std::array<char, 512> buffer{}; |
| 87 | while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { |
| 88 | raw_value += buffer.data(); |
| 89 | } |
| 90 | return raw_value; |
| 91 | } catch (std::exception& e) { |
| 92 | console->warn("Exception {} on command [{}]", e.what(), command); |
| 93 | } catch (...) { |
| 94 | console->warn("Unknown exception on command [{}]", command); |
| 95 | } |
| 96 | return std::nullopt; |
| 97 | } |
| 98 | |
| 99 | void OHDUtil::keep_alive_until_sigterm() { |
| 100 | static bool quit = false; |