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 | |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 9 | #include <chrono> |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 10 | #include <cmath> |
| 11 | #include <phosphor-logging/log.hpp> |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 12 | |
| 13 | namespace sol |
| 14 | { |
| 15 | |
| 16 | using namespace phosphor::logging; |
| 17 | |
Tom Joseph | b81f761 | 2017-04-25 17:59:02 +0530 | [diff] [blame] | 18 | CustomFD::~CustomFD() |
| 19 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 20 | if (fd >= 0) |
Tom Joseph | b81f761 | 2017-04-25 17:59:02 +0530 | [diff] [blame] | 21 | { |
| 22 | // Remove the host console descriptor from the sd_event_loop |
| 23 | std::get<eventloop::EventLoop&>(singletonPool).stopHostConsole(); |
| 24 | close(fd); |
| 25 | } |
| 26 | } |
| 27 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 28 | void Manager::initHostConsoleFd() |
| 29 | { |
| 30 | struct sockaddr_un addr; |
| 31 | int rc = 0; |
| 32 | int fd = 0; |
| 33 | |
| 34 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 35 | if (fd < 0) |
| 36 | { |
| 37 | log<level::ERR>("Failed to open the host console socket", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 38 | entry("ERRNO=%d", errno)); |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 39 | throw std::runtime_error("Failed to open the host console socket"); |
| 40 | } |
| 41 | |
| 42 | memset(&addr, 0, sizeof(addr)); |
| 43 | addr.sun_family = AF_UNIX; |
| 44 | memcpy(&addr.sun_path, &CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN); |
| 45 | consoleFD = std::make_unique<CustomFD>(fd); |
| 46 | auto& conFD = *(consoleFD.get()); |
| 47 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 48 | rc = connect(conFD(), (struct sockaddr*)&addr, sizeof(addr)); |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 49 | if (rc < 0) |
| 50 | { |
| 51 | log<level::ERR>("Failed to connect to host console socket address", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 52 | entry("ERRNO=%d", errno)); |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 53 | consoleFD.reset(); |
| 54 | throw std::runtime_error("Failed to connect to console server"); |
| 55 | } |
| 56 | } |
| 57 | |
Vernon Mauery | 70fd29c | 2017-11-30 13:11:43 -0800 | [diff] [blame] | 58 | int Manager::writeConsoleSocket(const std::vector<uint8_t>& input) const |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 59 | { |
| 60 | auto inBuffer = input.data(); |
| 61 | auto inBufferSize = input.size(); |
| 62 | size_t pos = 0; |
| 63 | ssize_t rc = 0; |
| 64 | int errVal = 0; |
| 65 | auto& conFD = *(consoleFD.get()); |
| 66 | |
| 67 | for (pos = 0; pos < inBufferSize; pos += rc) |
| 68 | { |
| 69 | rc = write(conFD(), inBuffer + pos, inBufferSize - pos); |
| 70 | if (rc <= 0) |
| 71 | { |
| 72 | if (errno == EINTR) |
| 73 | { |
| 74 | log<level::INFO>(" Retrying to handle EINTR", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 75 | entry("ERRNO=%d", errno)); |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 76 | rc = 0; |
| 77 | continue; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | errVal = errno; |
| 82 | log<level::ERR>("Failed to write to host console socket", |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 83 | entry("ERRNO=%d", errno)); |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 84 | return -errVal; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 92 | void Manager::startPayloadInstance(uint8_t payloadInstance, |
| 93 | session::SessionID sessionID) |
| 94 | { |
| 95 | if (payloadMap.empty()) |
| 96 | { |
| 97 | initHostConsoleFd(); |
| 98 | |
| 99 | // Register the fd in the sd_event_loop |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 100 | std::get<eventloop::EventLoop&>(singletonPool) |
| 101 | .startHostConsole(*(consoleFD.get())); |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Create the SOL Context data for payload instance |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 105 | auto context = std::make_unique<Context>(retryCount, sendThreshold, |
| 106 | payloadInstance, sessionID); |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 107 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 108 | std::get<eventloop::EventLoop&>(singletonPool) |
| 109 | .startSOLPayloadInstance( |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 110 | payloadInstance, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 111 | std::chrono::duration_cast<eventloop::IntervalType>( |
| 112 | accumulateInterval), |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 113 | std::chrono::duration_cast<eventloop::IntervalType>(retryInterval)); |
| 114 | |
| 115 | payloadMap.emplace(payloadInstance, std::move(context)); |
| 116 | } |
| 117 | |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 118 | void Manager::stopPayloadInstance(uint8_t payloadInstance) |
| 119 | { |
| 120 | auto iter = payloadMap.find(payloadInstance); |
| 121 | if (iter == payloadMap.end()) |
| 122 | { |
| 123 | throw std::runtime_error("SOL Payload instance not found "); |
| 124 | } |
| 125 | |
| 126 | payloadMap.erase(iter); |
| 127 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 128 | std::get<eventloop::EventLoop&>(singletonPool) |
| 129 | .stopSOLPayloadInstance(payloadInstance); |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 130 | |
| 131 | if (payloadMap.empty()) |
| 132 | { |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 133 | consoleFD.reset(); |
Tom Joseph | de203ce | 2017-08-01 16:48:06 +0530 | [diff] [blame] | 134 | |
| 135 | dataBuffer.erase(dataBuffer.size()); |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 139 | } // namespace sol |