1 #include "config.h"
2 
3 #include "scheduled_host_transition.hpp"
4 
5 #include <getopt.h>
6 
7 #include <sdbusplus/bus.hpp>
8 
9 #include <cstdlib>
10 #include <exception>
11 #include <filesystem>
12 
main(int argc,char ** argv)13 int main(int argc, char** argv)
14 {
15     size_t hostId = 0;
16 
17     int arg;
18     int optIndex = 0;
19 
20     static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
21                                        {0, 0, 0, 0}};
22 
23     while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
24     {
25         switch (arg)
26         {
27             case 'h':
28                 hostId = std::stoul(optarg);
29                 break;
30             default:
31                 break;
32         }
33     }
34 
35     namespace fs = std::filesystem;
36 
37     // Get a default event loop
38     auto event = sdeventplus::Event::get_default();
39 
40     // Get a handle to system dbus
41     auto bus = sdbusplus::bus::new_default();
42 
43     // For now, we only have one instance of the host
44     auto objPathInst = std::string{HOST_SCHED_OBJPATH} + std::to_string(hostId);
45 
46     // Check SCHEDULED_HOST_TRANSITION_PERSIST_PATH
47     auto dir = fs::path(SCHEDULED_HOST_TRANSITION_PERSIST_PATH).parent_path();
48     if (!fs::exists(dir))
49     {
50         fs::create_directories(dir);
51     }
52 
53     // Add sdbusplus ObjectManager.
54     sdbusplus::server::manager_t objManager(bus, objPathInst.c_str());
55 
56     phosphor::state::manager::ScheduledHostTransition manager(
57         bus, objPathInst.c_str(), hostId, event);
58 
59     // For backwards compatibility, request a busname without host id if
60     // input id is 0.
61     if (hostId == 0)
62     {
63         bus.request_name(SCHEDULED_HOST_TRANSITION_BUSNAME);
64     }
65 
66     bus.request_name((std::string{SCHEDULED_HOST_TRANSITION_BUSNAME} +
67                       std::to_string(hostId))
68                          .c_str());
69 
70     // Attach the bus to sd_event to service user requests
71     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
72     event.loop();
73 
74     return 0;
75 }
76