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