1 #include "config.h"
2 
3 #include "chassis_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_POH_COUNTER_PERSIST_PATH =
16     "/var/lib/phosphor-state-manager/POHCounter";
17 constexpr auto LEGACY_STATE_CHANGE_PERSIST_PATH =
18     "/var/lib/phosphor-state-manager/chassisStateChangeTime";
19 
20 int main(int argc, char** argv)
21 {
22     size_t chassisId = 0;
23     int arg;
24     int optIndex = 0;
25     static struct option longOpts[] = {{"chassis", required_argument, 0, 'c'},
26                                        {0, 0, 0, 0}};
27 
28     while ((arg = getopt_long(argc, argv, "c:", longOpts, &optIndex)) != -1)
29     {
30         switch (arg)
31         {
32             case 'c':
33                 chassisId = 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 chassisBusName =
45         std::string{CHASSIS_BUSNAME} + std::to_string(chassisId);
46     auto objPathInst = std::string{CHASSIS_OBJPATH} + std::to_string(chassisId);
47 
48     if (chassisId == 0)
49     {
50         // Chassis State Manager was only support single-chassis and there only
51         // two file to store persist values(POH and state change time), to
52         // support multi-chassis state mamagement, each service access new file
53         // paths with prefix 'chassisN', if any legacy persist file is exist,
54         // rename it to the new file format of chassis0.
55 
56         fs::path legacyPohPath{LEGACY_POH_COUNTER_PERSIST_PATH};
57         fs::path legacyStateChangePath{LEGACY_STATE_CHANGE_PERSIST_PATH};
58         fs::path newPohPath{fmt::format(POH_COUNTER_PERSIST_PATH, chassisId)};
59         fs::path newStateChangePath{
60             fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, chassisId)};
61         if (fs::exists(legacyPohPath))
62         {
63             fs::rename(legacyPohPath, newPohPath);
64         }
65         if (fs::exists(legacyStateChangePath))
66         {
67             fs::rename(legacyStateChangePath, newStateChangePath);
68         }
69     }
70 
71     // Add sdbusplus ObjectManager.
72     sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
73     phosphor::state::manager::Chassis manager(bus, objPathInst.c_str(),
74                                               chassisId);
75 
76     // For backwards compatibility, request a busname without chassis id if
77     // input id is 0.
78     if (chassisId == 0)
79     {
80         bus.request_name(CHASSIS_BUSNAME);
81     }
82 
83     bus.request_name(chassisBusName.c_str());
84     manager.startPOHCounter();
85     return 0;
86 }
87