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::seconds 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::seconds 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 Manager manager(bus, reload, DEFAULT_OBJPATH, "/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 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 fmt::print(stderr, "FAILED: {}", e.what()); 93 fflush(stderr); 94 return 1; 95 } 96 } 97