Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 1 | #include "config.h" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 3 | #include "host_state_manager.hpp" |
| 4 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 5 | #include <fmt/format.h> |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 6 | #include <getopt.h> |
| 7 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
| 9 | |
| 10 | #include <cstdlib> |
| 11 | #include <exception> |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 12 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 13 | #include <iostream> |
| 14 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 15 | constexpr auto LEGACY_HOST_STATE_PERSIST_PATH = |
| 16 | "/var/lib/phosphor-state-manager/requestedHostTransition"; |
| 17 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 18 | int main(int argc, char** argv) |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 19 | { |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 20 | size_t hostId = 0; |
| 21 | |
| 22 | int arg; |
| 23 | int optIndex = 0; |
| 24 | |
| 25 | static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, |
| 26 | {0, 0, 0, 0}}; |
| 27 | |
| 28 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
| 29 | { |
| 30 | switch (arg) |
| 31 | { |
| 32 | case 'h': |
| 33 | hostId = std::stoul(optarg); |
| 34 | break; |
| 35 | default: |
| 36 | break; |
| 37 | } |
| 38 | } |
| 39 | |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 40 | namespace fs = std::filesystem; |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 41 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 42 | auto bus = sdbusplus::bus::new_default(); |
| 43 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 44 | auto hostBusName = std::string{HOST_BUSNAME} + std::to_string(hostId); |
| 45 | auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId); |
Andrew Geissler | 1cb8b70 | 2016-12-13 13:33:06 -0600 | [diff] [blame] | 46 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 47 | if (hostId == 0) |
| 48 | { |
| 49 | // Host State Manager was only support single-host and there only one |
| 50 | // file to store persist values, to support multi-host state management, |
| 51 | // each service instance access new file path format with prefix 'hostN' |
| 52 | // now.For backward compatibility if there is a legacy persist file |
| 53 | // exist, rename it to the new file format of host0. |
| 54 | |
| 55 | fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH}; |
| 56 | fs::path newPath{fmt::format(HOST_STATE_PERSIST_PATH, hostId)}; |
| 57 | if (fs::exists(legacyPath)) |
| 58 | { |
| 59 | fs::rename(legacyPath, newPath); |
| 60 | } |
| 61 | } |
| 62 | |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 63 | // Add sdbusplus ObjectManager. |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 64 | sdbusplus::server::manager_t objManager(bus, objPathInst.c_str()); |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 65 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 66 | phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 67 | |
Dhruvaraj Subhashchandran | 3f47524 | 2017-07-12 00:44:27 -0500 | [diff] [blame] | 68 | auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path(); |
| 69 | fs::create_directories(dir); |
| 70 | |
Allen.Wang | 79b4500 | 2022-02-10 17:59:20 +0800 | [diff] [blame] | 71 | // For backwards compatibility, request a busname without host id if |
| 72 | // input id is 0. |
| 73 | if (hostId == 0) |
| 74 | { |
| 75 | bus.request_name(HOST_BUSNAME); |
| 76 | } |
| 77 | |
| 78 | bus.request_name(hostBusName.c_str()); |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 79 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 80 | while (true) |
Andrew Geissler | 3652902 | 2016-11-29 15:23:54 -0600 | [diff] [blame] | 81 | { |
| 82 | bus.process_discard(); |
| 83 | bus.wait(); |
| 84 | } |
| 85 | return 0; |
| 86 | } |