1 #include "config.h"
2 
3 #ifdef SYNC_MAC_FROM_INVENTORY
4 #include "inventory_mac.hpp"
5 #endif
6 #include "network_manager.hpp"
7 #include "rtnetlink_server.hpp"
8 #include "types.hpp"
9 
10 #include <fmt/format.h>
11 
12 #include <chrono>
13 #include <phosphor-logging/log.hpp>
14 #include <sdbusplus/bus.hpp>
15 #include <sdbusplus/server/manager.hpp>
16 #include <sdeventplus/clock.hpp>
17 #include <sdeventplus/event.hpp>
18 #include <sdeventplus/source/signal.hpp>
19 #include <sdeventplus/utility/timer.hpp>
20 #include <stdplus/signal.hpp>
21 
22 using phosphor::logging::level;
23 using phosphor::logging::log;
24 
25 constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
26 
27 namespace phosphor::network
28 {
29 
30 class TimerExecutor : public DelayedExecutor
31 {
32   private:
33     using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
34 
35   public:
36     TimerExecutor(sdeventplus::Event& event, std::chrono::seconds delay) :
37         delay(delay), timer(event, nullptr)
38     {
39     }
40 
41     void schedule() override
42     {
43         timer.restartOnce(delay);
44     }
45 
46     void setCallback(fu2::unique_function<void()>&& cb) override
47     {
48         timer.set_callback([cb = std::move(cb)](Timer&) mutable { cb(); });
49     }
50 
51   private:
52     std::chrono::seconds delay;
53     Timer timer;
54 };
55 
56 void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
57 {
58     log<level::NOTICE>("Got TERM, exiting");
59     signal.get_event().exit(0);
60 }
61 
62 int main()
63 {
64     auto event = sdeventplus::Event::get_default();
65     stdplus::signal::block(SIGTERM);
66     sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
67 
68     auto bus = sdbusplus::bus::new_default();
69     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
70     sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
71 
72     TimerExecutor reload(event, std::chrono::seconds(3));
73     Manager manager(bus, reload, DEFAULT_OBJPATH, "/etc/systemd/network");
74     netlink::Server svr(event, manager);
75 
76 #ifdef SYNC_MAC_FROM_INVENTORY
77     auto runtime = inventory::watch(bus, manager);
78 #endif
79 
80     bus.request_name(DEFAULT_BUSNAME);
81     return event.loop();
82 }
83 
84 } // namespace phosphor::network
85 
86 int main(int /*argc*/, char** /*argv*/)
87 {
88     try
89     {
90         return phosphor::network::main();
91     }
92     catch (const std::exception& e)
93     {
94         fmt::print(stderr, "FAILED: {}", e.what());
95         fflush(stderr);
96         return 1;
97     }
98 }
99