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 <phosphor-logging/lg2.hpp>
13 #include <sdbusplus/bus.hpp>
14 #include <sdbusplus/server/manager.hpp>
15 #include <sdeventplus/clock.hpp>
16 #include <sdeventplus/event.hpp>
17 #include <sdeventplus/source/signal.hpp>
18 #include <sdeventplus/utility/sdbus.hpp>
19 #include <sdeventplus/utility/timer.hpp>
20 #include <stdplus/pinned.hpp>
21 #include <stdplus/signal.hpp>
22 
23 #include <chrono>
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::milliseconds delay) :
37         delay(delay), timer(event, nullptr)
38     {}
39 
40     void schedule() override
41     {
42         timer.restartOnce(delay);
43     }
44 
45     void setCallback(fu2::unique_function<void()>&& cb) override
46     {
47         timer.set_callback([cb = std::move(cb)](Timer&) mutable { cb(); });
48     }
49 
50   private:
51     std::chrono::milliseconds delay;
52     Timer timer;
53 };
54 
55 void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
56 {
57     lg2::notice("Received request to terminate, exiting");
58     signal.get_event().exit(0);
59 }
60 
61 int main()
62 {
63     auto event = sdeventplus::Event::get_default();
64     stdplus::signal::block(SIGTERM);
65     sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
66 
67     stdplus::Pinned bus = sdbusplus::bus::new_default();
68     sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
69 
70     stdplus::Pinned<TimerExecutor> reload(event, std::chrono::seconds(3));
71     stdplus::Pinned<Manager> manager(bus, reload, DEFAULT_OBJPATH,
72                                      "/etc/systemd/network");
73     netlink::Server svr(event, manager);
74 
75 #ifdef SYNC_MAC_FROM_INVENTORY
76     auto runtime = inventory::watch(bus, manager);
77 #endif
78 
79     bus.request_name(DEFAULT_BUSNAME);
80     return sdeventplus::utility::loopWithBus(event, bus);
81 }
82 
83 } // namespace phosphor::network
84 
85 int main(int /*argc*/, char** /*argv*/)
86 {
87     try
88     {
89         return phosphor::network::main();
90     }
91     catch (const std::exception& e)
92     {
93         fmt::print(stderr, "FAILED: {}", e.what());
94         fflush(stderr);
95         return 1;
96     }
97 }
98