diff options
| -rw-r--r-- | flake.nix | 2 | ||||
| -rw-r--r-- | server/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | server/src/main.cpp | 55 |
3 files changed, 50 insertions, 13 deletions
| @@ -28,7 +28,7 @@ | |||
| 28 | buildInputs = with pkgs; [ boost pugixml expat icu openssl sqlite ]; | 28 | buildInputs = with pkgs; [ boost pugixml expat icu openssl sqlite ]; |
| 29 | nativeBuildInputs = with pkgs; [ | 29 | nativeBuildInputs = with pkgs; [ |
| 30 | llvmPackages.clang-tools boost # not from pkgs | 30 | llvmPackages.clang-tools boost # not from pkgs |
| 31 | cmake ninja gettext pkgconf systemdLibs | 31 | cmake ninja gettext |
| 32 | ]; | 32 | ]; |
| 33 | 33 | ||
| 34 | hardeningDisable = [ | 34 | hardeningDisable = [ |
diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index b071c64..8c7c326 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt | |||
| @@ -112,8 +112,4 @@ gettext_create_translations( | |||
| 112 | ) | 112 | ) |
| 113 | 113 | ||
| 114 | find_package(expat REQUIRED) | 114 | find_package(expat REQUIRED) |
| 115 | target_link_libraries(routemon_lib expat::expat) | 115 | target_link_libraries(routemon_lib expat::expat) \ No newline at end of file |
| 116 | |||
| 117 | find_package(PkgConfig QUIET REQUIRED) | ||
| 118 | pkg_check_modules(systemd REQUIRED IMPORTED_TARGET libsystemd) | ||
| 119 | target_link_libraries(routemon PkgConfig::systemd) | ||
diff --git a/server/src/main.cpp b/server/src/main.cpp index 9d576a2..fa85f78 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | #include <boost/asio.hpp> | 1 | #include <boost/asio.hpp> |
| 2 | #include <malloc.h> // for malloc_trim(3) | 2 | #include <malloc.h> // for malloc_trim(3) |
| 3 | #include <openssl/crypto.h> | 3 | #include <openssl/crypto.h> |
| 4 | #include <systemd/sd-daemon.h> | ||
| 5 | 4 | ||
| 6 | import std; | 5 | import std; |
| 7 | import routemon; | 6 | import routemon; |
| @@ -125,6 +124,33 @@ auto load_situations( | |||
| 125 | return pub; | 124 | return pub; |
| 126 | } | 125 | } |
| 127 | 126 | ||
| 127 | auto sd_notify(std::string_view msg) -> net::awaitable<void> | ||
| 128 | { | ||
| 129 | try | ||
| 130 | { | ||
| 131 | auto endpoint_cstr = std::getenv("NOTIFY_SOCKET"); | ||
| 132 | if (!endpoint_cstr || endpoint_cstr[0] == '\0') | ||
| 133 | co_return; | ||
| 134 | if (endpoint_cstr[0] != '/' && endpoint_cstr[0] != '@') | ||
| 135 | throw std::runtime_error{ | ||
| 136 | "NOTIFY_SOCKET specifies an unsupported endpoint" | ||
| 137 | }; | ||
| 138 | auto endpoint_str = std::string{endpoint_cstr}; | ||
| 139 | if (endpoint_str[0] == '@') | ||
| 140 | endpoint_str[0] = '\0'; | ||
| 141 | auto endpoint = net::local::datagram_protocol::endpoint{endpoint_str}; | ||
| 142 | auto sock = net::local::datagram_protocol::socket{ | ||
| 143 | co_await net::this_coro::executor | ||
| 144 | }; | ||
| 145 | co_await sock.async_send_to(net::buffer(msg), endpoint); | ||
| 146 | } | ||
| 147 | catch (std::exception const& e) | ||
| 148 | { | ||
| 149 | throw std::runtime_error{std::format( | ||
| 150 | "failed to notify service manager: {}", e.what())}; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 128 | auto server_main(routemon::log::logger& l, routemon::config::app config) | 154 | auto server_main(routemon::log::logger& l, routemon::config::app config) |
| 129 | -> exit_status | 155 | -> exit_status |
| 130 | { | 156 | { |
| @@ -185,7 +211,7 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) | |||
| 185 | ioc, | 211 | ioc, |
| 186 | [&]() -> net::awaitable<void> | 212 | [&]() -> net::awaitable<void> |
| 187 | { | 213 | { |
| 188 | sd_notify(0, "READY=1"); | 214 | co_await sd_notify("READY=1"); |
| 189 | 215 | ||
| 190 | while (true) | 216 | while (true) |
| 191 | { | 217 | { |
| @@ -196,8 +222,8 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) | |||
| 196 | std::chrono::duration_cast<std::chrono::microseconds>( | 222 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 197 | std::chrono::steady_clock::now().time_since_epoch()) | 223 | std::chrono::steady_clock::now().time_since_epoch()) |
| 198 | .count()); | 224 | .count()); |
| 199 | auto const reloading_msg = std::format("RELOADING=1\nMONOTONIC_USEC={}", usec); | 225 | co_await sd_notify( |
| 200 | sd_notify(0, reloading_msg.c_str()); | 226 | std::format("RELOADING=1\nMONOTONIC_USEC={}", usec)); |
| 201 | 227 | ||
| 202 | l.info("Received reload request"); | 228 | l.info("Received reload request"); |
| 203 | auto mpub = load_situations(l, config.situations.datex2_filename); | 229 | auto mpub = load_situations(l, config.situations.datex2_filename); |
| @@ -211,11 +237,26 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) | |||
| 211 | l.error("Failed to update situation publication"); | 237 | l.error("Failed to update situation publication"); |
| 212 | } | 238 | } |
| 213 | 239 | ||
| 214 | sd_notify(0, "READY=1"); | 240 | co_await sd_notify("READY=1"); |
| 215 | } | 241 | } |
| 216 | } | 242 | } |
| 217 | }, | 243 | }, |
| 218 | net::detached); | 244 | [&](std::exception_ptr e) |
| 245 | { | ||
| 246 | if (e) | ||
| 247 | { | ||
| 248 | try | ||
| 249 | { | ||
| 250 | std::rethrow_exception(e); | ||
| 251 | } | ||
| 252 | catch (std::exception const& e) | ||
| 253 | { | ||
| 254 | l.sub("signal_handler") | ||
| 255 | .error("Error in signal handler: {}", e.what()); | ||
| 256 | ioc.stop(); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | }); | ||
| 219 | 260 | ||
| 220 | auto threads = std::vector<std::thread>{nthread}; | 261 | auto threads = std::vector<std::thread>{nthread}; |
| 221 | for (auto& thread : threads) | 262 | for (auto& thread : threads) |
| @@ -223,7 +264,7 @@ auto server_main(routemon::log::logger& l, routemon::config::app config) | |||
| 223 | for (auto& thread : threads) | 264 | for (auto& thread : threads) |
| 224 | thread.join(); | 265 | thread.join(); |
| 225 | 266 | ||
| 226 | l.error("I/O contexts stopped"); | 267 | l.error("I/O context event processing loop stoped"); |
| 227 | return exit_status::failure; | 268 | return exit_status::failure; |
| 228 | } | 269 | } |
| 229 | 270 | ||