Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: wazuh/wazuh-agent
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fdda7100ecd51283b7e725ebf4fadd684b68b1bc
Choose a base ref
..
head repository: wazuh/wazuh-agent
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c009b4aa9e8153b7c6910691e0fef9ebb824b85f
Choose a head ref
31 changes: 3 additions & 28 deletions packages/debs/SPECS/wazuh-agent/debian/prerm
Original file line number Diff line number Diff line change
@@ -18,28 +18,17 @@ case "$1" in
kill -SIGTERM "$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)

# Stop the services before uninstalling the package
# Check for systemd
if command -v systemctl > /dev/null 2>&1 && systemctl > /dev/null 2>&1 && systemctl is-active --quiet wazuh-agent > /dev/null 2>&1; then
echo "Call systemctl stop service"
systemctl stop wazuh-agent > /dev/null 2>&1
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
fi
${BINARY_DIR}wazuh-agent --stop
fi

;;
@@ -50,22 +39,8 @@ case "$1" in
if command -v systemctl > /dev/null 2>&1 && systemctl > /dev/null 2>&1 && systemctl is-active --quiet wazuh-agent > /dev/null 2>&1; then
systemctl stop wazuh-agent > /dev/null 2>&1
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
fi
${BINARY_DIR}wazuh-agent --stop
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
;;

*)
1 change: 0 additions & 1 deletion src/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -77,7 +77,6 @@ target_link_libraries(Agent
fmt::fmt
Logger
Config
systemd
)

include(../cmake/ConfigureTarget.cmake)
2 changes: 1 addition & 1 deletion src/agent/service/wazuh-agent.service
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ Wants=network-online.target
After=network.target network-online.target

[Service]
Type=forking
Type=simple
PIDFile=/run/wazuh-agent.lock
ExecStart=/usr/bin/env WAZUH_HOME/wazuh-agent
ExecStop=/usr/bin/env WAZUH_HOME/wazuh-agent --stop
1 change: 0 additions & 1 deletion src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
@@ -116,5 +116,4 @@ void Agent::Stop()
m_moduleManager.Stop();
m_communicator.Stop();
m_taskManager.Stop();

}
48 changes: 22 additions & 26 deletions src/agent/src/process_options_unix.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <process_options.hpp>
#include <systemd/sd-daemon.h>
// #include <systemd/sd-daemon.h>
#include <unistd.h>
#include <agent.hpp>
#include <fmt/format.h>
@@ -12,20 +12,6 @@
#include <thread>
#include <vector>

// std::string GetParentProcessName() {
// pid_t ppid = getppid();
// std::string procPath = "/proc/" + std::to_string(ppid) + "/comm";
// std::ifstream procFile(procPath);
// if (!procFile.is_open()) {
// return "";
// }

// std::string parentName;
// std::getline(procFile, parentName);
// procFile.close();
// return parentName;
// }


void StartAgent(const std::string& configFilePath)
{
@@ -37,13 +23,6 @@ void StartAgent(const std::string& configFilePath)
return;
}

// if ( "systemctl" != GetParentProcessName() ) {
// // Notify systemd that the service is running
// std::cout << fmt::format("Notify systemctl that is running {}\n", unix_daemon::GetDaemonStatus(configFilePath));
// sd_notify(0, "READY=1");
// sd_notify(0, "STATUS=Service is running");
// }

LogInfo("Starting wazuh-agent");
Agent agent(configFilePath);
agent.Run();
@@ -62,15 +41,15 @@ void StopAgent(const std::string& configFilePath)

if (lockFileHandler.isLockFileCreated())
{
std::cout << "wazuh-agent is not running\n";
LogInfo("wazuh-agent is not running");
return;
}

pid_t pid = lockFileHandler.ReadPIDFromFile();

if (pid < 0)
{
std::cout << "Error reading pid file\n";
LogError("Error reading pid file");
return;
}

@@ -84,9 +63,26 @@ void StopAgent(const std::string& configFilePath)

void RestartAgent(const std::string& configFile)
{
LogInfo("Restarting wazuh-agent...");
StopAgent(configFile);

std::this_thread::sleep_for(std::chrono::seconds(1)); // NOLINT
int timeoutSeconds = 20;
auto startTime = std::chrono::steady_clock::now();

// Periodically check if the agent has stopped
while ( unix_daemon::IsDaemonRunning(configFile) ) {
// Check elapsed time
std::cout << unix_daemon::GetDaemonStatus(configFile) << std::endl;
auto elapsed = std::chrono::steady_clock::now() - startTime;
if (std::chrono::duration_cast<std::chrono::seconds>(elapsed).count() > timeoutSeconds) {
LogError("Timeout reached while stopping wazuh-agent.");
return ;
}

// Sleep briefly before checking again
std::this_thread::sleep_for(std::chrono::milliseconds(100));
LogInfo("Waiting wazuh-agent... be stoped.");
}

StartAgent(configFile);
LogInfo("Restarting wazuh-agent... Done.");
}
18 changes: 10 additions & 8 deletions src/agent/src/unix/unix_daemon.cpp
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ namespace unix_daemon
if (access(filename.c_str(), F_OK) != -1)
{
LogDebug("Lock file already exists: {}", filename);
return false; // The lock file exists, cannot create a new one
return true;
}

if (!createDirectory(m_lockFilePath))
@@ -79,7 +79,6 @@ namespace unix_daemon
return false;
}


// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg, cppcoreguidelines-avoid-magic-numbers)
int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
@@ -147,14 +146,17 @@ namespace unix_daemon

std::string GetDaemonStatus(const std::string& configFilePath)
{
LockFileHandler lockFileHandler = GenerateLockFile(configFilePath);
return IsDaemonRunning(configFilePath) ? "running" : "stopped";
}

if (!lockFileHandler.isLockFileCreated())
{
return "running";
}

bool IsDaemonRunning(const std::string& configFilePath){
LockFileHandler lockFileHandler = GenerateLockFile(configFilePath);
bool ret = lockFileHandler.isLockFileCreated();
lockFileHandler.removeLockFile();
return "stopped";
return ret;
}



} // namespace unix_daemon
2 changes: 2 additions & 0 deletions src/agent/src/unix/unix_daemon.hpp
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ namespace unix_daemon
/// @return A string indicating whether the daemon is "running" or "stopped"
std::string GetDaemonStatus(const std::string& configFilePath);

bool IsDaemonRunning(const std::string& configFilePath);

/// @brief Generates a lock file for the daemon
/// @param configFilePath The path to the configuration file
/// @return A LockFileHandler object