Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
| 3 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 4 | #include "buffer_service.hpp" |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 5 | |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | using namespace phosphor::logging; |
| 11 | |
| 12 | // clang-format off |
Artem Senichev | 90608dc | 2021-04-07 15:32:24 +0300 | [diff] [blame] | 13 | /** @brief Host state monitor properties. |
| 14 | * Used for automatic flushing the log buffer to the persistent file. |
| 15 | * Contains a list of properties and a set of their values that trigger the |
| 16 | * flush operation. |
| 17 | * For example, the current log buffer will be saved to a file when the |
| 18 | * "OperatingSystemState" property obtains one of the |
| 19 | * listed values ("xyz.openbmc_project...BootComplete", "Inactive", etc). |
| 20 | */ |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 21 | static const DbusLoop::WatchProperties watchProperties{ |
| 22 | {"xyz.openbmc_project.State.Host", {{ |
| 23 | "RequestedHostTransition", { |
| 24 | "xyz.openbmc_project.State.Host.Transition.On"}}}}, |
| 25 | {"xyz.openbmc_project.State.OperatingSystem.Status", {{ |
| 26 | "OperatingSystemState", { |
| 27 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete", |
Artem Senichev | 90608dc | 2021-04-07 15:32:24 +0300 | [diff] [blame] | 28 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive", |
| 29 | "Inactive", |
| 30 | "Standby"}}}} |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 31 | }; |
| 32 | // clang-format on |
| 33 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 34 | BufferService::BufferService(const Config& config, DbusLoop& dbusLoop, |
| 35 | HostConsole& hostConsole, LogBuffer& logBuffer, |
| 36 | FileStorage& fileStorage) : |
| 37 | config(config), |
| 38 | dbusLoop(&dbusLoop), hostConsole(&hostConsole), logBuffer(&logBuffer), |
| 39 | fileStorage(&fileStorage) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 40 | {} |
| 41 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 42 | void BufferService::run() |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 43 | { |
| 44 | if (config.bufFlushFull) |
| 45 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 46 | logBuffer->setFullHandler([this]() { this->flush(); }); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 47 | } |
| 48 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 49 | hostConsole->connect(); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 50 | |
| 51 | // Add SIGUSR1 signal handler for manual flushing |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 52 | dbusLoop->addSignalHandler(SIGUSR1, [this]() { this->flush(); }); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 53 | // Add SIGTERM signal handler for service shutdown |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 54 | dbusLoop->addSignalHandler(SIGTERM, [this]() { this->dbusLoop->stop(0); }); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 55 | |
| 56 | // Register callback for socket IO |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 57 | dbusLoop->addIoHandler(*hostConsole, [this]() { this->readConsole(); }); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 58 | |
| 59 | // Register host state watcher |
| 60 | if (*config.hostState) |
| 61 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 62 | dbusLoop->addPropertyHandler(config.hostState, watchProperties, |
| 63 | [this]() { this->flush(); }); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (!*config.hostState && !config.bufFlushFull) |
| 67 | { |
| 68 | log<level::WARNING>("Automatic flush disabled"); |
| 69 | } |
| 70 | |
| 71 | log<level::DEBUG>("Initialization complete", |
| 72 | entry("SocketId=%s", config.socketId), |
| 73 | entry("BufMaxSize=%lu", config.bufMaxSize), |
| 74 | entry("BufMaxTime=%lu", config.bufMaxTime), |
| 75 | entry("BufFlushFull=%s", config.bufFlushFull ? "y" : "n"), |
| 76 | entry("HostState=%s", config.hostState), |
| 77 | entry("OutDir=%s", config.outDir), |
| 78 | entry("MaxFiles=%lu", config.maxFiles)); |
| 79 | |
| 80 | // Run D-Bus event loop |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 81 | const int rc = dbusLoop->run(); |
| 82 | if (!logBuffer->empty()) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 83 | { |
| 84 | flush(); |
| 85 | } |
| 86 | if (rc < 0) |
| 87 | { |
| 88 | std::error_code ec(-rc, std::generic_category()); |
| 89 | throw std::system_error(ec, "Error in event loop"); |
| 90 | } |
| 91 | } |
| 92 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 93 | void BufferService::flush() |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 94 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 95 | if (logBuffer->empty()) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 96 | { |
| 97 | log<level::INFO>("Ignore flush: buffer is empty"); |
| 98 | return; |
| 99 | } |
| 100 | try |
| 101 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 102 | const std::string fileName = fileStorage->save(*logBuffer); |
| 103 | logBuffer->clear(); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 104 | |
| 105 | std::string msg = "Host logs flushed to "; |
| 106 | msg += fileName; |
| 107 | log<level::INFO>(msg.c_str()); |
| 108 | } |
| 109 | catch (const std::exception& ex) |
| 110 | { |
| 111 | log<level::ERR>(ex.what()); |
| 112 | } |
| 113 | } |
| 114 | |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 115 | void BufferService::readConsole() |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 116 | { |
| 117 | constexpr size_t bufSize = 128; // enough for most line-oriented output |
| 118 | std::vector<char> bufData(bufSize); |
| 119 | char* buf = bufData.data(); |
| 120 | |
| 121 | try |
| 122 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 123 | while (const size_t rsz = hostConsole->read(buf, bufSize)) |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 124 | { |
Nan Zhou | 042b5ba | 2021-06-18 09:32:45 -0700 | [diff] [blame] | 125 | logBuffer->append(buf, rsz); |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | catch (const std::system_error& ex) |
| 129 | { |
| 130 | log<level::ERR>(ex.what()); |
| 131 | } |
| 132 | } |