diff --git a/src/agent/include/agent.hpp b/src/agent/include/agent.hpp index 835f9b3eee..f7f0c652f4 100644 --- a/src/agent/include/agent.hpp +++ b/src/agent/include/agent.hpp @@ -37,6 +37,11 @@ class Agent /// This method sets up the agent and starts the task manager. void Run(); + /// @brief Stop the agent + /// + /// This method stops the agent by stopping the task manager, modules, and communicator. + void Stop(); + private: /// @brief Task manager TaskManager m_taskManager; diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index d77eecb354..437c130ba1 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -110,3 +110,22 @@ void Agent::Run() m_moduleManager.Stop(); m_communicator.Stop(); } + +void Agent::Stop() +{ + std::cout << "Stopping Agent..." << std::endl; + + // Stop accepting new tasks and cancel ongoing ones + m_taskManager.Stop(); + + // Stop modules + m_moduleManager.Stop(); + + // Stop communicator + m_communicator.Stop(); + + // If signal handler is blocking, unblocking it is necessary + m_signalHandler->Stop(); + + std::cout << "Agent stopped successfully." << std::endl; +} \ No newline at end of file diff --git a/src/agent/src/main.cpp b/src/agent/src/main.cpp index cbaed1ae13..f6057eea47 100644 --- a/src/agent/src/main.cpp +++ b/src/agent/src/main.cpp @@ -12,6 +12,7 @@ namespace program_options = boost::program_options; static const auto OPT_HELP {"help"}; static const auto OPT_RUN {"run"}; static const auto OPT_STATUS {"status"}; +static const auto OPT_STOP {"stop"}; static const auto OPT_CONFIG_FILE {"config-file"}; static const auto OPT_REGISTER_AGENT {"register-agent"}; static const auto OPT_URL {"url"}; @@ -34,6 +35,7 @@ int main(int argc, char* argv[]) program_options::options_description cmdParser("Allowed options"); cmdParser.add_options()(OPT_HELP, "Display this help menu")( OPT_RUN, "Run agent in foreground (this is the default behavior)")( + OPT_STOP, "Stop agent")( OPT_STATUS, "Check if the agent is running (running or stopped)")( OPT_CONFIG_FILE, program_options::value(), "Path to the Wazuh configuration file (optional)")( OPT_REGISTER_AGENT, "Use this option to register as a new agent")( @@ -86,6 +88,10 @@ int main(int argc, char* argv[]) { std::cout << cmdParser << '\n'; } + else if (validOptions.count(OPT_STOP) > 0) + { + StopAgent(validOptions.count(OPT_CONFIG_FILE) ? validOptions[OPT_CONFIG_FILE].as() : ""); + } else { StartAgent(validOptions.count(OPT_CONFIG_FILE) ? validOptions[OPT_CONFIG_FILE].as() : ""); @@ -98,4 +104,4 @@ int main(int argc, char* argv[]) LogCritical("An error occurred: {}.", e.what()); return 1; } -} +} \ No newline at end of file diff --git a/src/agent/src/process_options.hpp b/src/agent/src/process_options.hpp index 13df711573..8f3e76d767 100644 --- a/src/agent/src/process_options.hpp +++ b/src/agent/src/process_options.hpp @@ -25,6 +25,10 @@ void StartAgent(const std::string& configFilePath); /// @param configFilePath The file path to the configuration file used to get the status of the agent. void StatusAgent(const std::string& configFilePath); +/// @brief Stop the agent using the specified configuration file. +/// @param configFilePath The file path to the configuration file used for stop the agent. +void StopAgent(const std::string& configFilePath) + #ifdef _WIN32 /// @brief Installs the agent as a service. /// @return True if the installation is successful, false otherwise. diff --git a/src/agent/src/process_options_unix.cpp b/src/agent/src/process_options_unix.cpp index c58874dc91..602d49302c 100644 --- a/src/agent/src/process_options_unix.cpp +++ b/src/agent/src/process_options_unix.cpp @@ -32,3 +32,22 @@ void StatusAgent(const std::string& configFilePath) { std::cout << fmt::format("wazuh-agent is {}\n", unix_daemon::GetDaemonStatus(configFilePath)); } + +void StopAgent(const std::string& configFilePath) +{ + unix_daemon::LockFileHandler lockFileHandler = unix_daemon::GenerateLockFile(configFilePath); + + if (!lockFileHandler.isLockFileCreated()) + { + std::cout << "wazuh-agent is not running\n"; + return; + } + + LogInfo("Stopping wazuh-agent"); + Agent agent(configFilePath); + agent.Stop(); + + lockFileHandler.removeLockFile(); + + LogInfo("wazuh-agent stopped successfully"); +} \ No newline at end of file