blob: 484d6e5de6b8bb56983e26a4383af28405220bf3 [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.Wangba182f02022-03-23 19:01:53 +08005#include <fmt/format.h>
Allen.Wang79b45002022-02-10 17:59:20 +08006#include <getopt.h>
7
Andrew Geisslere426b582020-05-28 12:40:55 -05008#include <sdbusplus/bus.hpp>
9
10#include <cstdlib>
11#include <exception>
Patrick Williams6ed41ea2022-04-05 15:50:21 -050012#include <filesystem>
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
Allen.Wang79b45002022-02-10 17:59:20 +080018int main(int argc, char** argv)
Andrew Geissler36529022016-11-29 15:23:54 -060019{
Allen.Wang79b45002022-02-10 17:59:20 +080020 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 Williams6ed41ea2022-04-05 15:50:21 -050040 namespace fs = std::filesystem;
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050041
Andrew Geissler36529022016-11-29 15:23:54 -060042 auto bus = sdbusplus::bus::new_default();
43
Allen.Wang79b45002022-02-10 17:59:20 +080044 auto hostBusName = std::string{HOST_BUSNAME} + std::to_string(hostId);
45 auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId);
Andrew Geissler1cb8b702016-12-13 13:33:06 -060046
Allen.Wangba182f02022-03-23 19:01:53 +080047 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 Geissler36529022016-11-29 15:23:54 -060063 // Add sdbusplus ObjectManager.
Patrick Williamsf053e6f2022-07-22 19:26:54 -050064 sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
Andrew Geissler36529022016-11-29 15:23:54 -060065
Allen.Wang79b45002022-02-10 17:59:20 +080066 phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
Andrew Geisslera90a31a2016-12-13 16:16:28 -060067
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050068 auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
69 fs::create_directories(dir);
70
Allen.Wang79b45002022-02-10 17:59:20 +080071 // 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 Geissler36529022016-11-29 15:23:54 -060079
Andrew Geissler58a18012018-01-19 19:36:05 -080080 while (true)
Andrew Geissler36529022016-11-29 15:23:54 -060081 {
82 bus.process_discard();
83 bus.wait();
84 }
85 return 0;
86}