diff --git a/packages/debs/SPECS/wazuh-agent/debian/prerm b/packages/debs/SPECS/wazuh-agent/debian/prerm index 03ba9a4131..7c5853872e 100644 --- a/packages/debs/SPECS/wazuh-agent/debian/prerm +++ b/packages/debs/SPECS/wazuh-agent/debian/prerm @@ -15,18 +15,9 @@ case "$1" in elif ${BINARY_DIR}wazuh-agent --status 2>/dev/null | grep "is running" > /dev/null 2>&1; then pid=$(ps -ef | grep "${BINARY_DIR}wazuh-agent" | grep -v grep | awk '{print $2}') if [ -n "$pid" ]; then - kill -SIGTERM "$pid" 2>/dev/null + kill -15 "$pid" 2>/dev/null fi fi - - # # Process: wazuh-agent - # if pgrep -f "wazuh-agent" > /dev/null 2>&1; then - # kill -15 $(pgrep -f "wazuh-agent") > /dev/null 2>&1 - # fi - - # if pgrep -f "wazuh-agent" > /dev/null 2>&1; then - # kill -9 $(pgrep -f "wazuh-agent") > /dev/null 2>&1 - # fi ;; remove) @@ -38,10 +29,9 @@ case "$1" in elif ${BINARY_DIR}wazuh-agent --status 2>/dev/null | grep "is running" > /dev/null 2>&1; then pid=$(ps -ef | grep "${BINARY_DIR}wazuh-agent" | grep -v grep | awk '{print $2}') if [ -n "$pid" ]; then - kill -SIGTERM "$pid" 2>/dev/null + kill -15 "$pid" 2>/dev/null fi fi - ;; failed-upgrade) @@ -52,20 +42,9 @@ case "$1" in elif ${BINARY_DIR}wazuh-agent --status 2>/dev/null | grep "is running" > /dev/null 2>&1; then pid=$(ps -ef | grep "${BINARY_DIR}wazuh-agent" | grep -v grep | awk '{print $2}') if [ -n "$pid" ]; then - kill -SIGTERM "$pid" 2>/dev/null + kill -15 "$pid" 2>/dev/null fi fi - - # if [ -f ${INSTALLATION_WAZUH_DIR}/bin/wazuh-agent ]; then - # # pkill wazuh-agent - # if pgrep -f "wazuh-agent" > /dev/null 2>&1; then - # kill -15 $(pgrep -f "wazuh-agent") > /dev/null 2>&1 - # fi - - # if pgrep -f "wazuh-agent" > /dev/null 2>&1; then - # kill -9 $(pgrep -f "wazuh-agent") > /dev/null 2>&1 - # fi - # fi ;; *) diff --git a/src/agent/CMakeLists.txt b/src/agent/CMakeLists.txt index 6d50e37dfc..9c61c6cfd6 100644 --- a/src/agent/CMakeLists.txt +++ b/src/agent/CMakeLists.txt @@ -65,7 +65,6 @@ target_link_libraries(Agent MultiTypeQueue ModuleManager ModuleCommand - CentralizedConfiguration Boost::asio sysinfo PRIVATE diff --git a/src/agent/command_handler/include/command_handler.hpp b/src/agent/command_handler/include/command_handler.hpp index 5c39b8ef87..cd61e7f128 100644 --- a/src/agent/command_handler/include/command_handler.hpp +++ b/src/agent/command_handler/include/command_handler.hpp @@ -123,8 +123,13 @@ namespace command_handler { for (auto& cmd : *cmds) { - cmd.ExecutionResult.ErrorCode = module_command::Status::FAILURE; - cmd.ExecutionResult.Message = "Agent stopped during execution"; + if (cmd.Command == "restart") { + cmd.ExecutionResult.ErrorCode = module_command::Status::IN_PROGRESS; + cmd.ExecutionResult.Message = "Restarting Agent..."; + } else { + cmd.ExecutionResult.ErrorCode = module_command::Status::FAILURE; + cmd.ExecutionResult.Message = "Agent stopped during execution"; + } ReportCommandResult(cmd); m_commandStore.UpdateCommand(cmd); } diff --git a/src/agent/command_handler/src/command_handler.cpp b/src/agent/command_handler/src/command_handler.cpp index b5a7687e71..85a0ce40e0 100644 --- a/src/agent/command_handler/src/command_handler.cpp +++ b/src/agent/command_handler/src/command_handler.cpp @@ -3,7 +3,7 @@ namespace command_handler { const std::unordered_map VALID_COMMANDS_MAP = { - {"set-group", "CentralizedConfiguration"}, {"update-group", "CentralizedConfiguration"}}; + {"set-group", "CentralizedConfiguration"}, {"update-group", "CentralizedConfiguration"}, {"restart", "restart"}}; void CommandHandler::Stop() { diff --git a/src/agent/include/agent.hpp b/src/agent/include/agent.hpp index 04b228be95..8384ea17f1 100644 --- a/src/agent/include/agent.hpp +++ b/src/agent/include/agent.hpp @@ -46,6 +46,12 @@ class Agent /// This method stops all modules launched by moduleManager, and starts them again. void ReloadModules(); + /// @brief Checks if a restart is required + /// + /// This method returns `true` if the agent needs to be restarted, + /// and `false` otherwise. + bool IsRestartRequired(); + private: /// @brief Task manager TaskManager m_taskManager; @@ -87,4 +93,7 @@ class Agent /// @brief Agent thread count size_t m_agentThreadCount; + + /// @brief Define if the agent needs to be restarted after it ends. + bool requires_restart; }; diff --git a/src/agent/service/wazuh-agent.service b/src/agent/service/wazuh-agent.service index e6a65bf449..bbbd58b583 100644 --- a/src/agent/service/wazuh-agent.service +++ b/src/agent/service/wazuh-agent.service @@ -5,12 +5,13 @@ After=network.target network-online.target [Service] Type=simple - +PIDFile=/var/run/wazuh-agent.lock ExecStart=/usr/bin/env WAZUH_HOME/wazuh-agent +TimeoutStopSec=30s # Wait for 30 seconds before killing the service KillSignal=SIGTERM -KillMode=process +KillMode=mixed SendSIGKILL=no diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index 3007c07d3c..216c14f9ac 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -13,7 +13,7 @@ Agent::Agent(const std::string& configFilePath, std::unique_ptr signalHandler) : m_configurationParser(configFilePath.empty() ? std::make_shared() : std::make_shared( - std::filesystem::path(configFilePath))) + std::filesystem::path(configFilePath))) , m_dataPath( m_configurationParser->GetConfig("agent", "path.data").value_or(config::DEFAULT_DATA_PATH)) , m_messageQueue(std::make_shared( @@ -33,6 +33,7 @@ Agent::Agent(const std::string& configFilePath, std::unique_ptr m_configurationParser, m_agentInfo.GetUUID()) , m_commandHandler(m_dataPath) + , requires_restart(false) { // Check if agent is registered if (m_agentInfo.GetName().empty() || m_agentInfo.GetKey().empty() || m_agentInfo.GetUUID().empty()) @@ -101,6 +102,10 @@ void Agent::ReloadModules() } } +bool Agent::IsRestartRequired(){ + return requires_restart; +} + void Agent::Run() { m_taskManager.Start(m_agentThreadCount); @@ -158,6 +163,16 @@ void Agent::Run() return m_centralizedConfiguration.ExecuteCommand(std::move(command), std::move(parameters)); }, m_messageQueue); + } else if (cmd.Module == "restart") { + requires_restart = true; + SignalHandler::HandleSignal(SIGTERM); + auto RestartExecuteCommand = []() -> boost::asio::awaitable { + co_return module_command::CommandExecutionResult{ + module_command::Status::IN_PROGRESS, + "Pending restart execution" + }; + }; + return RestartExecuteCommand(); } return DispatchCommand(cmd, m_moduleManager.GetModule(cmd.Module), m_messageQueue); }), diff --git a/src/agent/src/process_options_unix.cpp b/src/agent/src/process_options_unix.cpp index df99ebeac2..78c5210fca 100644 --- a/src/agent/src/process_options_unix.cpp +++ b/src/agent/src/process_options_unix.cpp @@ -13,6 +13,7 @@ void StartAgent(const std::string& configFilePath) { + bool is_restart_required = false; unix_daemon::LockFileHandler lockFileHandler = unix_daemon::GenerateLockFile(configFilePath); if (!lockFileHandler.isLockFileCreated()) @@ -27,6 +28,7 @@ void StartAgent(const std::string& configFilePath) { Agent agent(configFilePath); agent.Run(); + is_restart_required = agent.IsRestartRequired(); } catch (const std::exception& e) { @@ -34,6 +36,10 @@ void StartAgent(const std::string& configFilePath) } lockFileHandler.removeLockFile(); + + if ( is_restart_required ){ + LogInfo("## RESTARTING STARTED"); + } } void StatusAgent(const std::string& configFilePath) diff --git a/src/agent/src/unix/unix_daemon.cpp b/src/agent/src/unix/unix_daemon.cpp index 86498529c0..c9c928c0b6 100644 --- a/src/agent/src/unix/unix_daemon.cpp +++ b/src/agent/src/unix/unix_daemon.cpp @@ -86,8 +86,16 @@ namespace unix_daemon return false; } - LogDebug("Lock file created: {}", filename); + // Write the PID to the lock file + const std::string pidStr = std::to_string(getpid()) + "\n"; + if (write(fd, pidStr.c_str(), pidStr.size()) == -1) + { + LogError("Unable to write PID to lock file: {}. Error: {} ({})", filename.c_str(), errno, std::strerror(errno)); + close(fd); + return false; + } + LogDebug("Lock file created: {}", filename); return true; }