From 52ada7f35b991d26094293d351995eb621825bec Mon Sep 17 00:00:00 2001 From: Kory Draughn Date: Wed, 4 Sep 2024 17:04:48 -0400 Subject: [PATCH] squash. server stands up, forks agent factory, and responds to shutdown "signals" - SIGINT / SIGTERM. --- server/main_server/src/agent_main.cpp | 13 +++++--- server/main_server/src/main.cpp | 44 ++++++++++++++++----------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/server/main_server/src/agent_main.cpp b/server/main_server/src/agent_main.cpp index 6b9c388d78..fac61f9f8f 100644 --- a/server/main_server/src/agent_main.cpp +++ b/server/main_server/src/agent_main.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #ifdef __cpp_lib_filesystem # include @@ -167,6 +168,7 @@ int main(int _argc, char* _argv[]) // - shared memory for replica access table, dns cache, hostname cache? // - delay server salt + // To see log messages from rsyslog, you must add irodsAgent5 to /etc/rsyslog.d/00-irods.conf. init_logger(config); log_af::info("{}: Initializing loggers for agent factory.", __func__); @@ -198,11 +200,11 @@ int main(int _argc, char* _argv[]) log_af::info("{}: Initializing shared memory for agent factory.", __func__); namespace hnc = irods::experimental::net::hostname_cache; - hnc::init("irods_hostname_cache", irods::get_hostname_cache_shared_memory_size()); + hnc::init("irods_hostname_cache5", irods::get_hostname_cache_shared_memory_size()); irods::at_scope_exit deinit_hostname_cache{[] { hnc::deinit(); }}; namespace dnsc = irods::experimental::net::dns_cache; - dnsc::init("irods_dns_cache", irods::get_dns_cache_shared_memory_size()); + dnsc::init("irods_dns_cache5", irods::get_dns_cache_shared_memory_size()); irods::at_scope_exit deinit_dns_cache{[] { dnsc::deinit(); }}; irods::experimental::replica_access_table::init(); @@ -276,9 +278,11 @@ int main(int _argc, char* _argv[]) unsigned int priority{}; #endif + log_af::info("{}: Waiting for client request.", __func__); while (true) { if (g_terminate) { log_af::info("{}: Received shutdown instruction. Exiting agent factory main loop.", __func__); + // TODO Send shutdown message to main server process. break; } @@ -286,7 +290,6 @@ int main(int _argc, char* _argv[]) log_af::info("{}: Received configuration reload instruction. Reloading configuration.", __func__); } - log_af::info("{}: Received configuration reload instruction. Reloading configuration.", __func__); std::this_thread::sleep_for(std::chrono::seconds{1}); } @@ -322,7 +325,7 @@ namespace // SIGINT struct sigaction sa_terminate; // NOLINT(cppcoreguidelines-pro-type-member-init) sigemptyset(&sa_terminate.sa_mask); - sa_terminate.sa_flags = SIGHUP; + sa_terminate.sa_flags = 0; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) sa_terminate.sa_handler = [](int) { g_terminate = 1; }; if (sigaction(SIGINT, &sa_terminate, nullptr) == -1) { @@ -340,7 +343,7 @@ namespace sa_sighup.sa_flags = 0; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) sa_sighup.sa_handler = [](int) { g_reload_config = 1; }; - if (sigaction(SIGTERM, &sa_sighup, nullptr) == -1) { + if (sigaction(SIGHUP, &sa_sighup, nullptr) == -1) { return -1; } #if 0 diff --git a/server/main_server/src/main.cpp b/server/main_server/src/main.cpp index 70cb9ea5ad..df415cbf62 100644 --- a/server/main_server/src/main.cpp +++ b/server/main_server/src/main.cpp @@ -92,7 +92,7 @@ int main(int _argc, char* _argv[]) ("dump-config-template", "") ("dump-default-jsonschema", "") ("daemonize,d", "") - ("pid-file,P", "") + ("pid-file,P", po::value(), "") ("help,h", "") ("version,v", ""); // clang-format on @@ -184,11 +184,12 @@ int main(int _argc, char* _argv[]) // - delay server salt init_logger(config); - + // This message queue gives child processes a way to notify the parent process. // This will only be used by the agent factory because iRODS 5.0 won't have a control plane. // Or at least that's the plan. - constexpr const auto* mq_name = "irodsd_mq"; // TODO Make this name unique. + log_server::info("{}: Creating communication channel between main server process and agent factory.", __func__); + constexpr const auto* mq_name = "irodsServer5"; // TODO Make this name unique. constexpr auto max_number_of_msg = 1; // Set to 1 to protect against duplicate/spamming of messages. constexpr auto max_msg_size = 512; boost::interprocess::message_queue::remove(mq_name); @@ -199,8 +200,8 @@ int main(int _argc, char* _argv[]) log_server::info("{}: Launching Agent Factory.", __func__); g_pid_af = fork(); if (0 == g_pid_af) { - char pname[] = "irodsAgent5"; - char parent_mq_name[] = "irodsd_mq"; + char pname[] = "/usr/sbin/irodsAgent5"; + char parent_mq_name[] = "irodsServer5"; char* args[] = {pname, config_dir_path.data(), parent_mq_name, nullptr}; execv(pname, args); _exit(1); @@ -255,20 +256,29 @@ int main(int _argc, char* _argv[]) unsigned int priority{}; while (true) { + if (g_terminate) { + log_server::info("{}: Received shutdown instruction. Exiting agent factory main loop.", __func__); + break; + } + + if (g_reload_config) { + log_server::info("{}: Received configuration reload instruction. Reloading configuration.", __func__); + g_reload_config = 0; + } +#if 0 // TODO Handle messages from agent factory: shutdown msg_buf.fill(0); - - // TODO Change to try_receive() or equivalent. - // This MUST NOT block. - pproc_mq.receive(msg_buf.data(), msg_buf.size(), recvd_size, priority); - - std::string_view msg(msg_buf.data(), recvd_size); - fmt::print("irodsd: received message: [{}], recvd_size: [{}]\n", msg, recvd_size); - - if (msg == "shutdown") { - fmt::print("Received shutdown instruction from control plane.\n"); - break; + if (pproc_mq.try_receive(msg_buf.data(), msg_buf.size(), recvd_size, priority)) { + std::string_view msg(msg_buf.data(), recvd_size); + fmt::print("irodsd: received message: [{}], recvd_size: [{}]\n", msg, recvd_size); + + if (msg == "shutdown") { + fmt::print("Received shutdown instruction from control plane.\n"); + break; + } } +#endif + std::this_thread::sleep_for(std::chrono::seconds{1}); // TODO Reap child processes: agent factory, delay server // TODO Fork agent factory and/or delay server again if necessary. @@ -486,7 +496,7 @@ Mandatory arguments to long options are mandatory for short options too. sa_sighup.sa_flags = 0; // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) sa_sighup.sa_handler = [](int) { g_reload_config = 1; }; - if (sigaction(SIGTERM, &sa_sighup, nullptr) == -1) { + if (sigaction(SIGHUP, &sa_sighup, nullptr) == -1) { return -1; } #if 0