diff --git a/src/agent/src/unix_daemon.cpp b/src/agent/src/unix_daemon.cpp index 9585e89f35..5d2b685441 100644 --- a/src/agent/src/unix_daemon.cpp +++ b/src/agent/src/unix_daemon.cpp @@ -7,6 +7,14 @@ #include #include +#if defined(__linux__) +#include +#elif defined(__APPLE__) +#include +#else +#error "Unsupported platform" +#endif + namespace fs = std::filesystem; namespace unix_daemon @@ -33,19 +41,16 @@ namespace unix_daemon } } - bool LockFileHandler::createDirectory(const std::string_view relativePath) const + bool LockFileHandler::createDirectory(const std::string& path) const { try { - const fs::path curDir = fs::current_path(); - const fs::path fullPath = curDir / relativePath; - - if (fs::exists(fullPath) && fs::is_directory(fullPath)) + if (fs::exists(path) && fs::is_directory(path)) { return true; } - fs::create_directories(fullPath); + fs::create_directories(path); return true; } catch (const fs::filesystem_error& e) @@ -85,18 +90,38 @@ namespace unix_daemon std::string LockFileHandler::GetExecutablePath() { std::vector pathBuffer(PATH_MAX); - const ssize_t len = readlink("/proc/self/exe", pathBuffer.data(), pathBuffer.size() - 1); +#if defined(__linux__) + ssize_t len = readlink("/proc/self/exe", pathBuffer.data(), pathBuffer.size() - 1); if (len != -1) { pathBuffer[static_cast(len)] = '\0'; - const std::filesystem::path exePath(pathBuffer.data()); - return exePath.parent_path().string(); // Return the directory part only + return std::filesystem::path(pathBuffer.data()).parent_path().string(); + } + else + { + throw std::runtime_error("Failed to retrieve executable path on Linux"); + } + +#elif defined(__APPLE__) + uint32_t size = static_cast(pathBuffer.size()); + if (_NSGetExecutablePath(pathBuffer.data(), &size) == 0) + { + return std::filesystem::path(pathBuffer.data()).parent_path().string(); } else { - return ""; + pathBuffer.resize(size); + if (_NSGetExecutablePath(pathBuffer.data(), &size) == 0) + { + return std::filesystem::path(pathBuffer.data()).parent_path().string(); + } + else + { + throw std::runtime_error("Failed to retrieve executable path on macOS"); + } } +#endif } LockFileHandler GenerateLockFile() diff --git a/src/agent/src/unix_daemon.hpp b/src/agent/src/unix_daemon.hpp index 2a3e2e269b..52f5996c44 100644 --- a/src/agent/src/unix_daemon.hpp +++ b/src/agent/src/unix_daemon.hpp @@ -30,7 +30,7 @@ namespace unix_daemon /// @brief Creates the directory path for the lock file /// @param relativePath The relative path for the lock file /// @return True if the directory is created, false otherwise - bool createDirectory(const std::string_view relativePath) const; + bool createDirectory(const std::string& relativePath) const; /// @brief Creates the lock file /// @return True if the lock file is created, false otherwise