Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 1 | #include "sol_manager.hpp" |
| 2 | |
| 3 | #include "main.hpp" |
| 4 | #include "sol_context.hpp" |
| 5 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 6 | #include <sys/socket.h> |
| 7 | #include <sys/un.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 8 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 9 | #include <boost/asio/basic_stream_socket.hpp> |
| 10 | #include <boost/asio/io_context.hpp> |
| 11 | #include <boost/asio/local/stream_protocol.hpp> |
| 12 | #include <boost/asio/write.hpp> |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 13 | #include <chrono> |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 14 | #include <cmath> |
| 15 | #include <phosphor-logging/log.hpp> |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 16 | |
| 17 | namespace sol |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 22 | void Manager::initConsoleSocket() |
Tom Joseph | b81f761 | 2017-04-25 17:59:02 +0530 | [diff] [blame] | 23 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 24 | // explicit length constructor for NUL-prefixed abstract path |
| 25 | std::string path(CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN); |
| 26 | boost::asio::local::stream_protocol::endpoint ep(path); |
| 27 | consoleSocket = |
| 28 | std::make_unique<boost::asio::local::stream_protocol::socket>(*io); |
| 29 | consoleSocket->connect(ep); |
Tom Joseph | b81f761 | 2017-04-25 17:59:02 +0530 | [diff] [blame] | 30 | } |
| 31 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 32 | void Manager::consoleInputHandler() |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 33 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 34 | boost::system::error_code ec; |
| 35 | boost::asio::socket_base::bytes_readable cmd(true); |
| 36 | consoleSocket->io_control(cmd, ec); |
| 37 | size_t readSize; |
| 38 | if (!ec) |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 39 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 40 | readSize = cmd.get(); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | log<level::ERR>("Reading ready count from host console socket failed:", |
| 45 | entry("EXCEPTION=%s", ec.message().c_str())); |
| 46 | return; |
| 47 | } |
| 48 | std::vector<uint8_t> buffer(readSize); |
| 49 | ec.clear(); |
| 50 | size_t readDataLen = |
| 51 | consoleSocket->read_some(boost::asio::buffer(buffer), ec); |
| 52 | if (ec) |
| 53 | { |
| 54 | log<level::ERR>("Reading from host console socket failed:", |
| 55 | entry("EXCEPTION=%s", ec.message().c_str())); |
| 56 | return; |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 59 | // Update the Console buffer with data read from the socket |
| 60 | buffer.resize(readDataLen); |
| 61 | dataBuffer.write(buffer); |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 62 | } |
| 63 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 64 | int Manager::writeConsoleSocket(const std::vector<uint8_t>& input) const |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 65 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 66 | boost::system::error_code ec; |
| 67 | boost::asio::write(*consoleSocket, boost::asio::buffer(input), ec); |
| 68 | return ec.value(); |
| 69 | } |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 70 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 71 | void Manager::startHostConsole() |
| 72 | { |
| 73 | if (!consoleSocket) |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 74 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 75 | initConsoleSocket(); |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 76 | } |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 77 | consoleSocket->async_wait(boost::asio::socket_base::wait_read, |
| 78 | [this](const boost::system::error_code& ec) { |
| 79 | if (!ec) |
| 80 | { |
| 81 | consoleInputHandler(); |
| 82 | startHostConsole(); |
| 83 | } |
| 84 | }); |
Rashmi RV | 0f63e01 | 2019-11-21 15:38:50 +0530 | [diff] [blame] | 85 | } // namespace sol |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 86 | |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 87 | void Manager::stopHostConsole() |
| 88 | { |
| 89 | if (consoleSocket) |
| 90 | { |
| 91 | consoleSocket->cancel(); |
| 92 | consoleSocket.reset(); |
| 93 | } |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 94 | } |
| 95 | |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 96 | void Manager::startPayloadInstance(uint8_t payloadInstance, |
| 97 | session::SessionID sessionID) |
| 98 | { |
| 99 | if (payloadMap.empty()) |
| 100 | { |
Rashmi RV | 0f63e01 | 2019-11-21 15:38:50 +0530 | [diff] [blame] | 101 | try |
| 102 | { |
| 103 | startHostConsole(); |
| 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
| 107 | log<level::ERR>("Encountered exception when starting host console. " |
| 108 | "Hence stopping host console.", |
| 109 | entry("EXCEPTION=%s", e.what())); |
| 110 | stopHostConsole(); |
| 111 | throw; |
| 112 | } |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Create the SOL Context data for payload instance |
Vernon Mauery | a6ad5e1 | 2020-02-21 14:54:24 -0800 | [diff] [blame] | 116 | std::shared_ptr<Context> context = Context::makeContext( |
| 117 | io, retryCount, sendThreshold, payloadInstance, sessionID); |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 118 | |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 119 | payloadMap.emplace(payloadInstance, std::move(context)); |
| 120 | } |
| 121 | |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 122 | void Manager::stopPayloadInstance(uint8_t payloadInstance) |
| 123 | { |
| 124 | auto iter = payloadMap.find(payloadInstance); |
| 125 | if (iter == payloadMap.end()) |
| 126 | { |
| 127 | throw std::runtime_error("SOL Payload instance not found "); |
| 128 | } |
| 129 | |
| 130 | payloadMap.erase(iter); |
| 131 | |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 132 | if (payloadMap.empty()) |
| 133 | { |
Vernon Mauery | 7e4a651 | 2018-11-09 08:43:36 -0800 | [diff] [blame] | 134 | stopHostConsole(); |
Tom Joseph | de203ce | 2017-08-01 16:48:06 +0530 | [diff] [blame] | 135 | |
| 136 | dataBuffer.erase(dataBuffer.size()); |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 140 | } // namespace sol |