Skip to content

Commit

Permalink
fix: fixes status report
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Jan 9, 2025
1 parent 5bf057b commit b7a3076
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/agent/src/process_options_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ void StartAgent(const std::string& configFilePath)

void StatusAgent(const std::string& configFilePath)
{
std::cout << fmt::format("wazuh-agent is {}\n", unix_daemon::GetDaemonStatus(configFilePath));
std::cout << fmt::format("wazuh-agent status: {}\n", unix_daemon::GetDaemonStatus(configFilePath));
}
18 changes: 15 additions & 3 deletions src/agent/src/unix/unix_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cerrno>
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
#include <sys/file.h>
#include <unistd.h>
Expand All @@ -24,6 +25,7 @@ namespace unix_daemon
{
LockFileHandler::LockFileHandler(std::string lockFilePath)
: m_lockFilePath(std::move(lockFilePath))
, m_errno(0)
, m_lockFileCreated(createLockFile())
{
}
Expand Down Expand Up @@ -77,13 +79,15 @@ namespace unix_daemon
int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
LogError("Unable to open lock file: {}. Error: {} ({})", filename.c_str(), errno, std::strerror(errno));
m_errno = errno;
LogError("Unable to open lock file: {}. Error: {} ({})", filename.c_str(), m_errno, std::strerror(m_errno));
return false;
}

if (flock(fd, LOCK_EX | LOCK_NB) == -1)
{
LogDebug("Unable to lock lock file: {}. Error: {} ({})", filename.c_str(), errno, std::strerror(errno));
m_errno = errno;
LogDebug("Unable to lock lock file: {}. Error: {} ({})", filename.c_str(), m_errno, std::strerror(m_errno));
close(fd);
return false;
}
Expand Down Expand Up @@ -111,7 +115,15 @@ namespace unix_daemon

if (!lockFileHandler.isLockFileCreated())
{
return "running";
if (lockFileHandler.getErrno() == EAGAIN)
{
return "running";
}
else
{
return fmt::format(
"Error: {} ({})", lockFileHandler.getErrno(), std::strerror(lockFileHandler.getErrno()));
}
}

return "stopped";
Expand Down
10 changes: 10 additions & 0 deletions src/agent/src/unix/unix_daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ namespace unix_daemon
return m_lockFileCreated;
}

/// @brief Returns the errno from the latest attempt to create/lock the lock-file
/// @return The errno
int getErrno() const
{
return m_errno;
}

private:
/// @brief Creates the directory path for the lock file
/// @param path The path for the lock file
Expand All @@ -43,6 +50,9 @@ namespace unix_daemon

std::string m_lockFilePath;

/// @brief Holds the errno from the lock-file create/lock attempt
int m_errno;

/// @brief Indicates the lock file was created by this instance of the LockFileHandler
bool m_lockFileCreated;
};
Expand Down

0 comments on commit b7a3076

Please sign in to comment.