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