1 #include "config.h"
2 
3 #include "host_state_manager.hpp"
4 
5 #include <fmt/format.h>
6 #include <getopt.h>
7 
8 #include <sdbusplus/bus.hpp>
9 
10 #include <cstdlib>
11 #include <exception>
12 #include <filesystem>
13 #include <iostream>
14 
15 constexpr auto LEGACY_HOST_STATE_PERSIST_PATH =
16     "/var/lib/phosphor-state-manager/requestedHostTransition";
17 
18 int main(int argc, char** argv)
19 {
20     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 
40     namespace fs = std::filesystem;
41 
42     auto bus = sdbusplus::bus::new_default();
43 
44     auto hostBusName = std::string{HOST_BUSNAME} + std::to_string(hostId);
45     auto objPathInst = std::string{HOST_OBJPATH} + std::to_string(hostId);
46 
47     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 
63     // Add sdbusplus ObjectManager.
64     sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
65 
66     phosphor::state::manager::Host manager(bus, objPathInst.c_str(), hostId);
67 
68     auto dir = fs::path(HOST_STATE_PERSIST_PATH).parent_path();
69     fs::create_directories(dir);
70 
71     // 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());
79 
80     while (true)
81     {
82         bus.process_discard();
83         bus.wait();
84     }
85     return 0;
86 }
87