xref: /openbmc/phosphor-state-manager/host_state_manager_main.cpp (revision 2eb6029cd9696b1db92c59e85a6752ac4ba4a5a0)
136529022SAndrew Geissler #include "config.h"
2e426b589SAndrew Geissler 
336529022SAndrew Geissler #include "host_state_manager.hpp"
436529022SAndrew Geissler 
579b45003SAllen.Wang #include <getopt.h>
679b45003SAllen.Wang 
7e426b589SAndrew Geissler #include <sdbusplus/bus.hpp>
8e426b589SAndrew Geissler 
9e426b589SAndrew Geissler #include <cstdlib>
10e426b589SAndrew Geissler #include <exception>
116ed41eabSPatrick Williams #include <filesystem>
1278c066f6SPatrick Williams #include <format>
13e426b589SAndrew Geissler #include <iostream>
14e426b589SAndrew Geissler 
15ba182f0cSAllen.Wang constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
16ba182f0cSAllen.Wang     "/var/lib/phosphor-state-manager/requestedHostTransition";
17ba182f0cSAllen.Wang 
18f566c964SAmithash Prasasd using HostState = sdbusplus::server::xyz::openbmc_project::state::Host;
19f566c964SAmithash Prasasd 
main(int argc,char ** argv)2079b45003SAllen.Wang int main(int argc, char** argv)
2136529022SAndrew Geissler {
2279b45003SAllen.Wang     size_t hostId = 0;
2379b45003SAllen.Wang 
2479b45003SAllen.Wang     int arg;
2579b45003SAllen.Wang     int optIndex = 0;
2679b45003SAllen.Wang 
27f15b9544SPavithra Barithaya     static struct option longOpts[] = {
28f15b9544SPavithra Barithaya         {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
2979b45003SAllen.Wang 
3079b45003SAllen.Wang     while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
3179b45003SAllen.Wang     {
3279b45003SAllen.Wang         switch (arg)
3379b45003SAllen.Wang         {
3479b45003SAllen.Wang             case 'h':
3579b45003SAllen.Wang                 hostId = std::stoul(optarg);
3679b45003SAllen.Wang                 break;
3779b45003SAllen.Wang             default:
3879b45003SAllen.Wang                 break;
3979b45003SAllen.Wang         }
4079b45003SAllen.Wang     }
4179b45003SAllen.Wang 
426ed41eabSPatrick Williams     namespace fs = std::filesystem;
433f475242SDhruvaraj Subhashchandran 
4436529022SAndrew Geissler     auto bus = sdbusplus::bus::new_default();
4536529022SAndrew Geissler 
46f566c964SAmithash Prasasd     auto hostBusName = HostState::interface + std::to_string(hostId);
47f566c964SAmithash Prasasd     auto hostName = std::string(HostState::namespace_path::host) +
48f566c964SAmithash Prasasd                     std::to_string(hostId);
49f566c964SAmithash Prasasd     const auto* objPath = HostState::namespace_path::value;
50f566c964SAmithash Prasasd     std::string objPathInst =
51f566c964SAmithash Prasasd         sdbusplus::message::object_path(objPath) / hostName;
521cb8b707SAndrew Geissler 
53ba182f0cSAllen.Wang     if (hostId == 0)
54ba182f0cSAllen.Wang     {
55ba182f0cSAllen.Wang         // Host State Manager was only support single-host and there only one
56ba182f0cSAllen.Wang         // file to store persist values, to support multi-host state management,
57ba182f0cSAllen.Wang         // each service instance access new file path format with prefix 'hostN'
58ba182f0cSAllen.Wang         // now.For backward compatibility if there is a legacy persist file
59ba182f0cSAllen.Wang         // exist, rename it to the new file format of host0.
60ba182f0cSAllen.Wang 
61ba182f0cSAllen.Wang         fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH};
6278c066f6SPatrick Williams         fs::path newPath{std::format(HOST_STATE_PERSIST_PATH, hostId)};
63ba182f0cSAllen.Wang         if (fs::exists(legacyPath))
64ba182f0cSAllen.Wang         {
65ba182f0cSAllen.Wang             fs::rename(legacyPath, newPath);
66ba182f0cSAllen.Wang         }
67ba182f0cSAllen.Wang     }
68ba182f0cSAllen.Wang 
6936529022SAndrew Geissler     // Add sdbusplus ObjectManager.
70*2eb6029cSAmithash Prasasd     sdbusplus::server::manager_t objManager(bus, objPath);
7136529022SAndrew Geissler 
7279b45003SAllen.Wang     phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
73a90a31a9SAndrew Geissler 
743f475242SDhruvaraj Subhashchandran     auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
753f475242SDhruvaraj Subhashchandran     fs::create_directories(dir);
763f475242SDhruvaraj Subhashchandran 
7779b45003SAllen.Wang     // For backwards compatibility, request a busname without host id if
7879b45003SAllen.Wang     // input id is 0.
7979b45003SAllen.Wang     if (hostId == 0)
8079b45003SAllen.Wang     {
81f566c964SAmithash Prasasd         bus.request_name(HostState::interface);
8279b45003SAllen.Wang     }
8379b45003SAllen.Wang 
8479b45003SAllen.Wang     bus.request_name(hostBusName.c_str());
8536529022SAndrew Geissler 
8636529022SAndrew Geissler     while (true)
8736529022SAndrew Geissler     {
8836529022SAndrew Geissler         bus.process_discard();
8936529022SAndrew Geissler         bus.wait();
9036529022SAndrew Geissler     }
9136529022SAndrew Geissler     return 0;
9236529022SAndrew Geissler }
93