Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform Abstraction Layer #208

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
cmake_minimum_required(VERSION 3.22)

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
endif()
Comment on lines +4 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about macos?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


get_filename_component(SRC_FOLDER ${CMAKE_SOURCE_DIR}/../ ABSOLUTE)
get_filename_component(CONFIG_FOLDER ${CMAKE_SOURCE_DIR}/../etc/config/ ABSOLUTE)
project(wazuh-agent)
Expand Down
2 changes: 0 additions & 2 deletions src/agent/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ add_executable(agent_test agent_test.cpp)
configure_target(agent_test)
target_include_directories(agent_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
target_link_libraries(agent_test PRIVATE Agent GTest::gtest GTest::gmock)
if(NOT WIN32)
add_test(NAME AgentTest COMMAND agent_test)
endif()

add_executable(task_manager_test task_manager_test.cpp)
configure_target(task_manager_test)
Expand Down
32 changes: 26 additions & 6 deletions src/agent/tests/agent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <gtest/gtest.h>
#include <isignal_handler.hpp>

constexpr auto AGENT_CONFIG_PATH {"/tmp/wazuh-agent.yml"};

class MockSignalHandler : public ISignalHandler
{
public:
Expand All @@ -16,13 +14,34 @@ class MockSignalHandler : public ISignalHandler
class AgentTests : public ::testing::Test
{
protected:
std::string AGENT_CONFIG_PATH;
std::string AGENT_DB_PATH;
std::string AGENT_PATH;

void SetUp() override
{
#ifdef WIN32
char* tmpPath = nullptr;
size_t len = 0;

_dupenv_s(&tmpPath, &len, "TMP");
std::string tempFolder = std::string(tmpPath);
AGENT_CONFIG_PATH = tempFolder + "wazuh-agent.yml";
AGENT_DB_PATH = tempFolder + "agent_info.db";
AGENT_PATH = tempFolder;
#else
AGENT_CONFIG_PATH = "/tmp/wazuh-agent.yml";
AGENT_DB_PATH = "/tmp/agent_info.db";
AGENT_PATH = "/tmp";
#endif

CreateTempConfigFile();

SysInfo sysInfo;
std::unique_ptr<AgentInfo> agent = std::make_unique<AgentInfo>(
"/tmp", [&sysInfo]() mutable { return sysInfo.os(); }, [&sysInfo]() mutable { return sysInfo.networks(); });
AGENT_PATH,
[&sysInfo]() mutable { return sysInfo.os(); },
[&sysInfo]() mutable { return sysInfo.networks(); });

agent->SetKey("4GhT7uFm1zQa9c2Vb7Lk8pYsX0WqZrNj");
agent->SetName("agent_name");
Expand All @@ -42,7 +61,8 @@ class AgentTests : public ::testing::Test
configFilePath << R"(
agent:
server_url: https://localhost:27000
path.data: /tmp/
path.data: )" << AGENT_PATH
<< R"(
retry_interval: 30s
inventory:
enabled: false
Expand All @@ -68,8 +88,8 @@ class AgentTests : public ::testing::Test

void CleanUpTempFiles()
{
std::remove(AGENT_CONFIG_PATH);
std::remove("/tmp/agent_info.db");
std::remove(AGENT_CONFIG_PATH.c_str());
std::remove(AGENT_DB_PATH.c_str());
}
};

Expand Down
1 change: 1 addition & 0 deletions src/cmake/ConfigureTarget.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function(configure_target target)
/w14546 # Function pointer conversion to a different size
/w14547 # Function pointer conversion to a different calling convention
)
add_definitions(-D_WIN32_WINNT=0x0601)
target_compile_options(${target} PRIVATE ${msvc_warnings})
endif()

Expand Down
5 changes: 2 additions & 3 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
add_subdirectory(binaries_op)
add_subdirectory(bzip2_op)
add_subdirectory(config)
add_subdirectory(data_provider)
add_subdirectory(dbsync)
add_subdirectory(error_messages)
add_subdirectory(file_op)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is file_op no longer needed? can we remove the code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed, but it is not needed in Windows. Efforts were made to completely remove it but several other libraries use it.

add_subdirectory(filesystem_wrapper)
add_subdirectory(hashHelper)
add_subdirectory(logger)
add_subdirectory(mem_op)
add_subdirectory(logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was sorted alphabetically before, this should go before mem_op

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

add_subdirectory(networkHelper)
add_subdirectory(pal)
add_subdirectory(privsep_op)
add_subdirectory(pthreads_op)
add_subdirectory(randombytes)
Expand Down
5 changes: 5 additions & 0 deletions src/common/binaries_op/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
add_library(binaries_op STATIC src/binaries_op.c)

get_filename_component(COMMON_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../ ABSOLUTE)
target_include_directories(binaries_op PUBLIC include)
include_directories(${COMMON_FOLDER}/pal/include)
include_directories(${COMMON_FOLDER}/error_messages/include)
include_directories(${COMMON_FOLDER}/utils/include)
include_directories(${COMMON_FOLDER}/file_op/include)
Comment on lines +3 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many places in this PR use the old cmake style of include_directories which has the side effect of adding folders to every target after the command is invoked. Can we change them to use target_include_directories ?


target_link_libraries(binaries_op
utils
Expand Down
19 changes: 14 additions & 5 deletions src/common/binaries_op/src/binaries_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
* License (version 2) as published by the FSF - Free Software
* Foundation
*/
#include <string.h>
#include <stdlib.h>
#include "error_messages.h"
#include "os_err.h"
#include "pal.h"
#include "logger.hpp"
#include "os_macros.h"

#include "shared.h"

#ifndef WIN32
#include "file_op.h"
#endif
Comment on lines +9 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these headers missing before? I don't see from the changes in the code that they are all necessary (ie: logger.hpp)

Copy link
Member Author

@ncvicchi ncvicchi Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those headers were placed in shared.h which is a mess and created include loops and other problems in MSVC. So shared.h was removed and only those necessary headers were included.
Logger.hpp is the new implemententation of debug_op, which was created during the development of this PR.


#ifdef WAZUH_UNIT_TESTING
#ifdef WIN32
Expand All @@ -23,7 +31,7 @@ int get_binary_path(const char *binary, char **validated_comm) {
const char sep[2] = ":";
#endif
char *path;
char *full_path;
char *full_path = NULL;
char *validated = NULL;
char *env_path = NULL;
char *env_path_copy = NULL;
Expand Down Expand Up @@ -57,9 +65,10 @@ int get_binary_path(const char *binary, char **validated_comm) {
#ifdef WIN32
snprintf(full_path, strlen(path) + strlen(binary) + 2, "%s\\%s", path, binary);
#else
snprintf(full_path, strlen(path) + strlen(binary) + 2, "%s/%s", path, binary);
if(full_path)
snprintf(full_path, strlen(path) + strlen(binary) + 2, "%s/%s", path, binary);
#endif
if (IsFile(full_path) == 0) {
if (full_path && IsFile(full_path) == 0) {
validated = strdup(full_path);
os_free(full_path);
break;
Expand Down
13 changes: 13 additions & 0 deletions src/common/byteArrayHelper/include/byteArrayHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@

#include <string>

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
Comment on lines +17 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these warnings handled on clang too? This question applies for all of the #ifdef __GNUC__.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as I understand, if -fgnuc-version=0 is not passed as a parameter to ClangApple, it will continue defining GNUC, which was its normal behaviour.

https://clang.llvm.org/docs/UsersManual.html#cmdoption-fgnuc-version

If you still think it is necessary, it could be changed in every place with clang, but the only advantage would be more clarity when reading it, not the behaviour.


#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4505)
#endif

namespace Utils
{
Expand All @@ -36,6 +43,12 @@ namespace Utils
}
}

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif // _BYTE_ARRAY_HELPER_H
2 changes: 1 addition & 1 deletion src/common/byteArrayHelper/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
pthread
crypto
ssl
-static-libgcc -static-libstdc++
-static-libgcc
jr0me marked this conversation as resolved.
Show resolved Hide resolved
ws2_32
crypt32
)
Expand Down
3 changes: 3 additions & 0 deletions src/common/bzip2_op/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
add_library(bzip2_op STATIC src/bzip2_op.c)

get_filename_component(COMMON_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../ ABSOLUTE)
target_include_directories(bzip2_op PUBLIC include)
include_directories(${COMMON_FOLDER}/pal/include)
include_directories(${COMMON_FOLDER}/error_messages/include)
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to include the directories like this than to link against the targets that normally bring them? I mean, instead of adding the directories just using target_link_libraries with pal and error_messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error_messages are almost always included for definitions, and pal also solves incompatibilities in the header but not in any linkable file.


target_link_libraries(bzip2_op
utils
Expand Down
7 changes: 5 additions & 2 deletions src/common/bzip2_op/src/bzip2_op.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/

#include "shared.h"
#include <stdio.h>
#include <errno.h>
#include "bzip2_op.h"
#include "error_messages.h"
#include "os_err.h"
Comment on lines +10 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like <logger.hpp> was also missing here.



int bzip2_compress(const char *file, const char *filebz2) {
Expand Down
18 changes: 13 additions & 5 deletions src/common/cmdHelper/include/cmdHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
#include <cstdio>
#include <memory>
#include <vector>
#include "pal.h"

#ifndef WIN32
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#else
FILE *popen(const char *command, const char *mode) { return NULL; }
int pclose(FILE *stream){ return 0; }
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4505)
#endif

namespace Utils
Expand All @@ -42,7 +45,7 @@ namespace Utils

if (file)
{
while (fgets(buffer.data(), bufferSize, file.get()))
while (fgets(buffer.data(), static_cast<int>(bufferSize), file.get()))
{
result += buffer.data();
}
Expand All @@ -52,5 +55,10 @@ namespace Utils
}
}

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
#pragma warning(pop)
#endif
2 changes: 1 addition & 1 deletion src/common/cmdHelper/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
pthread
crypto
ssl
-static-libgcc -static-libstdc++
-static-libgcc
ws2_32
crypt32
)
Expand Down
7 changes: 5 additions & 2 deletions src/common/data_provider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,leak,undefined")
endif(FSANITIZE)
else()
set(CMAKE_CXX_FLAGS "/W4 /permissive- /MT")
set(CMAKE_CXX_FLAGS "/W4 /permissive- /MT /EHsc")
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down Expand Up @@ -96,10 +96,10 @@ include_directories(${SRC_FOLDER}/common/linuxHelper/include/)
include_directories(${SRC_FOLDER}/common/stringHelper/include/)
include_directories(${SRC_FOLDER}/common/timeHelper/include/)
include_directories(${SRC_FOLDER}/shared_modules/common/)
include_directories(${SRC_FOLDER}/common/pal/include/)

if(WIN32)
include_directories(${SRC_FOLDER}/common/time_op/include/)
include_directories(${SRC_FOLDER}/common/file_op/include/)
include_directories(${SRC_FOLDER}/common/regex_op/include/)
include_directories(${SRC_FOLDER}/common/bzip2_op/include/)
include_directories(${SRC_FOLDER}/common/validate_op/include/)
Expand All @@ -112,6 +112,8 @@ if(WIN32)
include_directories(${SRC_FOLDER}/common/mem_op/include/)
endif()

get_filename_component(COMMON_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../ ABSOLUTE)

link_directories(${SRC_FOLDER})

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
Expand Down Expand Up @@ -183,6 +185,7 @@ elseif(APPLE)
endif(CMAKE_SYSTEM_NAME STREQUAL "Windows")

target_link_libraries(sysinfo PUBLIC
pal
networkHelper
nlohmann_json::nlohmann_json
cjson)
Expand Down
12 changes: 6 additions & 6 deletions src/common/data_provider/src/network/networkWindowsWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ class NetworkWindowsInterface final : public INetworkInterfaceWrapper
{
const auto txPackets { ifRow->OutUcastPkts + ifRow->OutNUcastPkts };
const auto rxPackets { ifRow->InUcastPkts + ifRow->InNUcastPkts };
retVal.txPackets = txPackets;
retVal.rxPackets = rxPackets;
retVal.txPackets = static_cast<unsigned int>(txPackets);
retVal.rxPackets = static_cast<unsigned int>(rxPackets);
retVal.txBytes = ifRow->OutOctets;
retVal.rxBytes = ifRow->InOctets;
retVal.txErrors = ifRow->OutErrors;
retVal.rxErrors = ifRow->InErrors;
retVal.txDropped = ifRow->OutDiscards;
retVal.rxDropped = ifRow->InDiscards;
retVal.txErrors = static_cast<unsigned int>(ifRow->OutErrors);
retVal.rxErrors = static_cast<unsigned int>(ifRow->InErrors);
retVal.txDropped = static_cast<unsigned int>(ifRow->OutDiscards);
retVal.rxDropped = static_cast<unsigned int>(ifRow->InDiscards);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/common/data_provider/src/osinfo/sysOsInfoWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include "registryHelper.h"
#include "sharedDefs.h"

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996)
#endif

static std::string getVersion(const bool isMinor = false)
{
std::string version;
Expand Down Expand Up @@ -54,7 +59,6 @@ static std::string getVersion(const bool isMinor = false)
{
OSVERSIONINFOEX osvi{};
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

if (GetVersionEx (reinterpret_cast<OSVERSIONINFO*>(&osvi)))
{
version = std::to_string(isMinor ? osvi.dwMinorVersion : osvi.dwMajorVersion);
Expand Down Expand Up @@ -281,6 +285,10 @@ static std::string getName()
return name.empty() ? "Microsoft Windows" : name;
}

#ifdef _MSC_VER
#pragma warning(pop)
#endif

static std::string getMachine()
{
static const std::map<std::string, std::string> ARCH_MAP
Expand Down
13 changes: 13 additions & 0 deletions src/common/data_provider/src/packages/packageLinuxParserHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
#include "timeHelper.h"
#include "sharedDefs.h"

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4505)
#endif

#include <sstream>

Expand Down Expand Up @@ -265,6 +272,12 @@ namespace PackageLinuxHelper

};

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif // _PACKAGE_LINUX_PARSER_HELPER_H
Loading
Loading