Skip to content

Commit

Permalink
Merge pull request #611 from wazuh/enhancement/553-restart-command-macos
Browse files Browse the repository at this point in the history
Restart command support in macOS
  • Loading branch information
TomasTurina authored Feb 21, 2025
2 parents 68fc39f + 1c46924 commit 7a3d279
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 44 deletions.
4 changes: 3 additions & 1 deletion src/agent/communicator/src/communicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ namespace communicator
{
m_tokenExpTimer->expires_after(
std::chrono::milliseconds((remainingSecs - TOKEN_PRE_EXPIRY_SECS) * A_SECOND_IN_MILLIS));
co_await m_tokenExpTimer->async_wait(boost::asio::use_awaitable);

boost::system::error_code ec;
co_await m_tokenExpTimer->async_wait(boost::asio::redirect_error(boost::asio::use_awaitable, ec));
}

while (m_keepRunning.load())
Expand Down
4 changes: 3 additions & 1 deletion src/agent/restart_handler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ find_package(Boost REQUIRED COMPONENTS asio)

if(WIN32)
set(SOURCES src/restart_handler_win.cpp)
elseif(APPLE)
set(SOURCES src/restart_handler_unix.cpp src/restart_handler_mac.cpp)
else()
set(SOURCES src/restart_handler_unix.cpp)
set(SOURCES src/restart_handler_unix.cpp src/restart_handler_lin.cpp)
endif()

add_library(RestartHandler ${SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion src/agent/restart_handler/include/restart_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ namespace restart_handler

/// @brief Executes the restart command.
/// @return Result of the restart command execution.
static boost::asio::awaitable<module_command::CommandExecutionResult> RestartCommand();
static boost::asio::awaitable<module_command::CommandExecutionResult> RestartAgent();
};
} // namespace restart_handler
26 changes: 26 additions & 0 deletions src/agent/restart_handler/src/restart_handler_lin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <restart_handler_unix.hpp>

#include <logger.hpp>

namespace restart_handler
{
bool RunningAsService()
{
return (0 == std::system("which systemctl > /dev/null 2>&1") && nullptr != std::getenv("INVOCATION_ID"));
}

boost::asio::awaitable<module_command::CommandExecutionResult> RestartService()
{
if (std::system("systemctl restart wazuh-agent") != 0)
{
co_return module_command::CommandExecutionResult {module_command::Status::IN_PROGRESS,
"Systemctl restart execution"};
}
else
{
LogError("Systemctl restart failed");
co_return module_command::CommandExecutionResult {module_command::Status::FAILURE,
"Systemctl restart failed"};
}
}
} // namespace restart_handler
26 changes: 26 additions & 0 deletions src/agent/restart_handler/src/restart_handler_mac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <restart_handler_unix.hpp>

#include <logger.hpp>

namespace restart_handler
{
bool RunningAsService()
{
return (getppid() == 1);
}

boost::asio::awaitable<module_command::CommandExecutionResult> RestartService()
{
if (std::system("launchctl kickstart -k system/com.wazuh.agent") != 0)
{
co_return module_command::CommandExecutionResult {module_command::Status::IN_PROGRESS,
"launchctl restart execution"};
}
else
{
LogError("launchctl failed to restart Agent");
co_return module_command::CommandExecutionResult {module_command::Status::FAILURE,
"launchctl restart failed"};
}
}
} // namespace restart_handler
30 changes: 4 additions & 26 deletions src/agent/restart_handler/src/restart_handler_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,8 @@

namespace restart_handler
{

std::vector<char*> RestartHandler::startupCmdLineArgs;

bool UsingSystemctl()
{
return (0 == std::system("which systemctl > /dev/null 2>&1") && nullptr != std::getenv("INVOCATION_ID"));
}

boost::asio::awaitable<module_command::CommandExecutionResult> RestartWithSystemd()
{
LogInfo("Systemctl restarting wazuh agent service.");
if (std::system("systemctl restart wazuh-agent") != 0)
{
co_return module_command::CommandExecutionResult {module_command::Status::IN_PROGRESS,
"Systemctl restart execution"};
}
else
{
LogError("Failed using systemctl.");
co_return module_command::CommandExecutionResult {module_command::Status::FAILURE,
"Systemctl restart failed"};
}
}

void StopAgent(const pid_t pid, const int timeoutInSecs)
{
const time_t startTime = time(nullptr);
Expand Down Expand Up @@ -80,12 +58,12 @@ namespace restart_handler
"Pending restart execution"};
}

boost::asio::awaitable<module_command::CommandExecutionResult> RestartHandler::RestartCommand()
boost::asio::awaitable<module_command::CommandExecutionResult> RestartHandler::RestartAgent()
{

if (UsingSystemctl())
LogInfo("Restarting Wazuh agent.");
if (RunningAsService())
{
return RestartWithSystemd();
return RestartService();
}
else
{
Expand Down
16 changes: 7 additions & 9 deletions src/agent/restart_handler/src/restart_handler_unix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

namespace restart_handler
{

/// @brief Checks if systemctl is available and running as a systemd service.
/// @brief Checks if the Agent is running as a service
///
/// Determines if systemctl can be used in the current environment.
/// @return true if systemctl is available and running as a systemd service, otherwise returns false.
bool UsingSystemctl();
/// @details Looks for indications that the Agent was started by systemd, launchd or SCM
/// @return true if the Agent is running as a service, otherwise returns false.
bool RunningAsService();

/// @brief Stops the agent by terminating its process.
///
Expand All @@ -31,12 +30,11 @@ namespace restart_handler
/// @return A boost::asio::awaitable containing the result of the command execution.
boost::asio::awaitable<module_command::CommandExecutionResult> RestartWithFork();

/// @brief Restarts the module using systemd service management.
/// @brief Restarts the module as a service
///
/// This function restarts the module via systemd, ensuring the module is properly restarted using
/// This function restarts the module, ensuring the module is properly restarted using
/// system service management mechanisms.
///
/// @return A boost::asio::awaitable containing the result of the command execution.
boost::asio::awaitable<module_command::CommandExecutionResult> RestartWithSystemd();

boost::asio::awaitable<module_command::CommandExecutionResult> RestartService();
} // namespace restart_handler
2 changes: 1 addition & 1 deletion src/agent/restart_handler/src/restart_handler_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace restart_handler

std::vector<char*> RestartHandler::startupCmdLineArgs;

boost::asio::awaitable<module_command::CommandExecutionResult> RestartHandler::RestartCommand()
boost::asio::awaitable<module_command::CommandExecutionResult> RestartHandler::RestartAgent()
{
// TODO
co_return module_command::CommandExecutionResult {module_command::Status::FAILURE,
Expand Down
8 changes: 4 additions & 4 deletions src/agent/restart_handler/tests/restart_handler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ TEST(TestUsingSystemctl, ReturnsTrueWhenSystemctlIsAvailable)
setenv("INVOCATION_ID", "testing", 1);
if (0 == std::system("which systemctl > /dev/null 2>&1"))
{
EXPECT_TRUE(restart_handler::UsingSystemctl());
EXPECT_TRUE(restart_handler::RunningAsService());
}
else
{
EXPECT_FALSE(restart_handler::UsingSystemctl());
EXPECT_FALSE(restart_handler::RunningAsService());
}
}

TEST(TestUsingSystemctl, ReturnsFalseWhenSystemctlIsNotAvailable)
{
unsetenv("INVOCATION_ID");
EXPECT_FALSE(restart_handler::UsingSystemctl());
EXPECT_FALSE(restart_handler::RunningAsService());
}

TEST(StopAgentTest, GratefullyStop)
TEST(StopAgentTest, GracefullyStop)
{
testing::internal::CaptureStdout();
signal(SIGCHLD, SigchldHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void Agent::Run()
else if (cmd.Module == module_command::RESTART_HANDLER_MODULE)
{
LogInfo("Restart: Initiating restart");
return restart_handler::RestartHandler::RestartCommand();
return restart_handler::RestartHandler::RestartAgent();
}
return DispatchCommand(cmd, m_moduleManager.GetModule(cmd.Module), m_messageQueue);
}),
Expand Down

0 comments on commit 7a3d279

Please sign in to comment.