blob: 75ab60f01c862134f5fee6bbe2e363f358f030e6 [file] [log] [blame]
Andrew Geissler36529022016-11-29 15:23:54 -06001#include "config.h"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Andrew Geissler36529022016-11-29 15:23:54 -06003#include "host_state_manager.hpp"
4
Allen.Wang79b45002022-02-10 17:59:20 +08005#include <getopt.h>
6
Andrew Geisslere426b582020-05-28 12:40:55 -05007#include <sdbusplus/bus.hpp>
8
9#include <cstdlib>
10#include <exception>
Patrick Williams6ed41ea2022-04-05 15:50:21 -050011#include <filesystem>
Patrick Williams78c066f2024-02-13 12:25:58 -060012#include <format>
Andrew Geisslere426b582020-05-28 12:40:55 -050013#include <iostream>
14
Allen.Wangba182f02022-03-23 19:01:53 +080015constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
16 "/var/lib/phosphor-state-manager/requestedHostTransition";
17
Amithash Prasasdf566c962024-07-22 20:28:42 -070018using HostState = sdbusplus::server::xyz::openbmc_project::state::Host;
19
Allen.Wang79b45002022-02-10 17:59:20 +080020int main(int argc, char** argv)
Andrew Geissler36529022016-11-29 15:23:54 -060021{
Allen.Wang79b45002022-02-10 17:59:20 +080022 size_t hostId = 0;
23
24 int arg;
25 int optIndex = 0;
26
Pavithra Barithayaf15b9542024-06-21 08:18:48 -050027 static struct option longOpts[] = {
28 {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
Allen.Wang79b45002022-02-10 17:59:20 +080029
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 Williams6ed41ea2022-04-05 15:50:21 -050042 namespace fs = std::filesystem;
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050043
Andrew Geissler36529022016-11-29 15:23:54 -060044 auto bus = sdbusplus::bus::new_default();
45
Amithash Prasasdf566c962024-07-22 20:28:42 -070046 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 Geissler1cb8b702016-12-13 13:33:06 -060052
Allen.Wangba182f02022-03-23 19:01:53 +080053 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 Williams78c066f2024-02-13 12:25:58 -060062 fs::path newPath{std::format(HOST_STATE_PERSIST_PATH, hostId)};
Allen.Wangba182f02022-03-23 19:01:53 +080063 if (fs::exists(legacyPath))
64 {
65 fs::rename(legacyPath, newPath);
66 }
67 }
68
Andrew Geissler36529022016-11-29 15:23:54 -060069 // Add sdbusplus ObjectManager.
Amithash Prasasd2eb60292024-08-13 19:19:07 -070070 sdbusplus::server::manager_t objManager(bus, objPath);
Andrew Geissler36529022016-11-29 15:23:54 -060071
Allen.Wang79b45002022-02-10 17:59:20 +080072 phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
Andrew Geisslera90a31a2016-12-13 16:16:28 -060073
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050074 auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
75 fs::create_directories(dir);
76
Allen.Wang79b45002022-02-10 17:59:20 +080077 // For backwards compatibility, request a busname without host id if
78 // input id is 0.
79 if (hostId == 0)
80 {
Amithash Prasasdf566c962024-07-22 20:28:42 -070081 bus.request_name(HostState::interface);
Allen.Wang79b45002022-02-10 17:59:20 +080082 }
83
84 bus.request_name(hostBusName.c_str());
Andrew Geissler36529022016-11-29 15:23:54 -060085
Andrew Geissler58a18012018-01-19 19:36:05 -080086 while (true)
Andrew Geissler36529022016-11-29 15:23:54 -060087 {
88 bus.process_discard();
89 bus.wait();
90 }
91 return 0;
92}