-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathhost_state_manager_main.cpp
92 lines (71 loc) · 2.46 KB
/
host_state_manager_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "config.h"
#include "host_state_manager.hpp"
#include <getopt.h>
#include <sdbusplus/bus.hpp>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <format>
#include <iostream>
constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
"/var/lib/phosphor-state-manager/requestedHostTransition";
using HostState = sdbusplus::server::xyz::openbmc_project::state::Host;
int main(int argc, char** argv)
{
size_t hostId = 0;
int arg;
int optIndex = 0;
static struct option longOpts[] = {
{"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
{
switch (arg)
{
case 'h':
hostId = std::stoul(optarg);
break;
default:
break;
}
}
namespace fs = std::filesystem;
auto bus = sdbusplus::bus::new_default();
auto hostBusName = HostState::interface + std::to_string(hostId);
auto hostName = std::string(HostState::namespace_path::host) +
std::to_string(hostId);
const auto* objPath = HostState::namespace_path::value;
std::string objPathInst =
sdbusplus::message::object_path(objPath) / hostName;
if (hostId == 0)
{
// Host State Manager was only support single-host and there only one
// file to store persist values, to support multi-host state management,
// each service instance access new file path format with prefix 'hostN'
// now.For backward compatibility if there is a legacy persist file
// exist, rename it to the new file format of host0.
fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH};
fs::path newPath{std::format(HOST_STATE_PERSIST_PATH, hostId)};
if (fs::exists(legacyPath))
{
fs::rename(legacyPath, newPath);
}
}
// Add sdbusplus ObjectManager.
sdbusplus::server::manager_t objManager(bus, objPath);
phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
fs::create_directories(dir);
// For backwards compatibility, request a busname without host id if
// input id is 0.
if (hostId == 0)
{
bus.request_name(HostState::interface);
}
bus.request_name(hostBusName.c_str());
while (true)
{
bus.process_discard();
bus.wait();
}
return 0;
}