add basic plugin

This commit is contained in:
Green Sky 2023-10-25 17:51:48 +02:00
parent 12df9de1b4
commit 55ed8ffbf9
No known key found for this signature in database
5 changed files with 97 additions and 3 deletions

View File

@ -8,6 +8,9 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
else()
set(SOLANACEAE_CLAMAV_STANDALONE OFF)
endif()
message("II SOLANACEAE_CLAMAV_STANDALONE " ${SOLANACEAE_CLAMAV_STANDALONE})
option(SOLANACEAE_CLAMAV_BUILD_PLUGINS "Build the clamav plugins" ${SOLANACEAE_CLAMAV_STANDALONE})
if (SOLANACEAE_CLAMAV_STANDALONE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@ -61,5 +64,8 @@ endif()
# cmake setup end
add_subdirectory(./src)
#add_subdirectory(./plugins)
if (SOLANACEAE_CLAMAV_BUILD_PLUGINS)
add_subdirectory(./plugins)
endif()

View File

@ -1,6 +1,5 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_subdirectory(./solanaceae_util)
#add_subdirectory(./solanaceae_plugin)
@ -9,6 +8,8 @@ add_subdirectory(./entt)
add_subdirectory(./solanaceae_contact)
add_subdirectory(./solanaceae_message3)
include(FetchContent)
# TODO: move to clamav.cmake
find_package(PkgConfig QUIET)
if (PKG_CONFIG_FOUND)
@ -21,7 +22,6 @@ if (PKG_CONFIG_FOUND)
endif()
if (NOT TARGET EXT_SOL::libclamav)
include(FetchContent)
set(ENABLE_LIBCLAMAV_ONLY ON)
set(ENABLE_APP OFF)
set(ENABLE_TESTS OFF)
@ -38,3 +38,12 @@ if (NOT TARGET EXT_SOL::libclamav)
add_library(EXT_SOL::libclamav ALIAS clamav)
endif()
# TODO: rename when plugin lib is split
if (NOT TARGET solanaceae_plugin)
FetchContent_Declare(solanaceae_plugin
GIT_REPOSITORY https://github.com/Green-Sky/solanaceae_plugin.git
GIT_TAG master
)
FetchContent_MakeAvailable(solanaceae_plugin)
endif()

11
plugins/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
add_library(plugin_clamav SHARED
./plugin_clamav_module.cpp
)
target_compile_features(plugin_clamav PUBLIC cxx_std_17)
target_link_libraries(plugin_clamav PUBLIC
solanaceae_clamav
solanaceae_plugin
)

View File

@ -0,0 +1,65 @@
#include <solanaceae/plugin/solana_plugin_v1.h>
#include <solanaceae/clamav/clamav_module.hpp>
#include <solanaceae/util/config_model.hpp>
#include <memory>
#include <iostream>
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
static std::unique_ptr<ClamAVModule> g_cavm = nullptr;
extern "C" {
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void) {
return "ClamAVModule";
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void) {
return SOLANA_PLUGIN_VERSION;
}
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api) {
std::cout << "PLUGIN CAVM START()\n";
if (solana_api == nullptr) {
return 1;
}
ConfigModelI* conf = nullptr;
{ // make sure required types are loaded
conf = RESOLVE_INSTANCE(ConfigModelI);
if (conf == nullptr) {
std::cerr << "PLUGIN CAVM missing ConfigModelI\n";
return 2;
}
}
// static store, could be anywhere tho
// construct with fetched dependencies
g_cavm = std::make_unique<ClamAVModule>(*conf);
// register types
PROVIDE_INSTANCE(ClamAVModule, "ClamAVModule", g_cavm.get());
return 0;
}
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void) {
std::cout << "PLUGIN CAVM STOP()\n";
g_cavm.reset();
}
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta) {
(void)delta;
//std::cout << "PLUGIN CAVM TICK()\n";
//g_cavm->iterate();
}
} // extern C

View File

@ -1,5 +1,7 @@
#include "./clamav_module_async_wrapper.hpp"
#include <cstdlib>
ClamAVModuleAsyncWrapper::ClamAVModuleAsyncWrapper(ClamAVModuleInterface& cavmi) : _cavmi(cavmi) {
}
@ -7,6 +9,7 @@ std::future<ClamAVModuleAsyncWrapper::ScanResult> ClamAVModuleAsyncWrapper::scan
return std::async(
std::launch::async,
[this](std::string inner_path) {
std::srand(5693);
// TODO: optimize, clamav supports multi threading
std::lock_guard lock_guard{_cavmi_mutex};
return _cavmi.scanFilePath(inner_path);