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