136529022SAndrew Geissler #include "config.h" 2e426b589SAndrew Geissler 336529022SAndrew Geissler #include "host_state_manager.hpp" 436529022SAndrew Geissler 5*ba182f0cSAllen.Wang #include <fmt/format.h> 679b45003SAllen.Wang #include <getopt.h> 779b45003SAllen.Wang 8e426b589SAndrew Geissler #include <sdbusplus/bus.hpp> 9e426b589SAndrew Geissler 10e426b589SAndrew Geissler #include <cstdlib> 11e426b589SAndrew Geissler #include <exception> 126ed41eabSPatrick Williams #include <filesystem> 13e426b589SAndrew Geissler #include <iostream> 14e426b589SAndrew Geissler 15*ba182f0cSAllen.Wang constexpr auto LEGACY_HOST_STATE_PERSIST_PATH = 16*ba182f0cSAllen.Wang "/var/lib/phosphor-state-manager/requestedHostTransition"; 17*ba182f0cSAllen.Wang 1879b45003SAllen.Wang int main(int argc, char** argv) 1936529022SAndrew Geissler { 2079b45003SAllen.Wang size_t hostId = 0; 2179b45003SAllen.Wang 2279b45003SAllen.Wang int arg; 2379b45003SAllen.Wang int optIndex = 0; 2479b45003SAllen.Wang 2579b45003SAllen.Wang static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, 2679b45003SAllen.Wang {0, 0, 0, 0}}; 2779b45003SAllen.Wang 2879b45003SAllen.Wang while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) 2979b45003SAllen.Wang { 3079b45003SAllen.Wang switch (arg) 3179b45003SAllen.Wang { 3279b45003SAllen.Wang case 'h': 3379b45003SAllen.Wang hostId = std::stoul(optarg); 3479b45003SAllen.Wang break; 3579b45003SAllen.Wang default: 3679b45003SAllen.Wang break; 3779b45003SAllen.Wang } 3879b45003SAllen.Wang } 3979b45003SAllen.Wang 406ed41eabSPatrick Williams namespace fs = std::filesystem; 413f475242SDhruvaraj Subhashchandran 4236529022SAndrew Geissler auto bus = sdbusplus::bus::new_default(); 4336529022SAndrew Geissler 4479b45003SAllen.Wang auto hostBusName = std::string{HOST_BUSNAME} + std::to_string(hostId); 4579b45003SAllen.Wang auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId); 461cb8b707SAndrew Geissler 47*ba182f0cSAllen.Wang if (hostId == 0) 48*ba182f0cSAllen.Wang { 49*ba182f0cSAllen.Wang // Host State Manager was only support single-host and there only one 50*ba182f0cSAllen.Wang // file to store persist values, to support multi-host state management, 51*ba182f0cSAllen.Wang // each service instance access new file path format with prefix 'hostN' 52*ba182f0cSAllen.Wang // now.For backward compatibility if there is a legacy persist file 53*ba182f0cSAllen.Wang // exist, rename it to the new file format of host0. 54*ba182f0cSAllen.Wang 55*ba182f0cSAllen.Wang fs::path legacyPath{LEGACY_HOST_STATE_PERSIST_PATH}; 56*ba182f0cSAllen.Wang fs::path newPath{fmt::format(HOST_STATE_PERSIST_PATH, hostId)}; 57*ba182f0cSAllen.Wang if (fs::exists(legacyPath)) 58*ba182f0cSAllen.Wang { 59*ba182f0cSAllen.Wang fs::rename(legacyPath, newPath); 60*ba182f0cSAllen.Wang } 61*ba182f0cSAllen.Wang } 62*ba182f0cSAllen.Wang 6336529022SAndrew Geissler // Add sdbusplus ObjectManager. 641cb8b707SAndrew Geissler sdbusplus::server::manager::manager objManager(bus, objPathInst.c_str()); 6536529022SAndrew Geissler 6679b45003SAllen.Wang phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId); 67a90a31a9SAndrew Geissler 683f475242SDhruvaraj Subhashchandran auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path(); 693f475242SDhruvaraj Subhashchandran fs::create_directories(dir); 703f475242SDhruvaraj Subhashchandran 7179b45003SAllen.Wang // For backwards compatibility, request a busname without host id if 7279b45003SAllen.Wang // input id is 0. 7379b45003SAllen.Wang if (hostId == 0) 7479b45003SAllen.Wang { 75a90a31a9SAndrew Geissler bus.request_name(HOST_BUSNAME); 7679b45003SAllen.Wang } 7779b45003SAllen.Wang 7879b45003SAllen.Wang bus.request_name(hostBusName.c_str()); 7936529022SAndrew Geissler 8036529022SAndrew Geissler while (true) 8136529022SAndrew Geissler { 8236529022SAndrew Geissler bus.process_discard(); 8336529022SAndrew Geissler bus.wait(); 8436529022SAndrew Geissler } 8536529022SAndrew Geissler return 0; 8636529022SAndrew Geissler } 87