diff --git a/test/drivers/start_tests.cpp b/test/drivers/start_tests.cpp index bbb061f6c3..68a98b619a 100644 --- a/test/drivers/start_tests.cpp +++ b/test/drivers/start_tests.cpp @@ -260,7 +260,7 @@ int open_engine(int argc, char** argv) } char error_buffer[FILENAME_MAX] = {0}; - event_test::s_scap_handle = scap_open(&oargs, vtable, nullptr, error_buffer, &ret); + event_test::s_scap_handle = scap_open(&oargs, vtable, error_buffer, &ret); if(!event_test::s_scap_handle) { std::cerr << "Unable to open the engine: " << error_buffer << std::endl; diff --git a/test/libscap/test_suites/engines/bpf/bpf.cpp b/test/libscap/test_suites/engines/bpf/bpf.cpp index a40d561844..ab274c4c0e 100644 --- a/test/libscap/test_suites/engines/bpf/bpf.cpp +++ b/test/libscap/test_suites/engines/bpf/bpf.cpp @@ -31,7 +31,7 @@ scap_t* open_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim, }; oargs.engine_params = &bpf_params; - return scap_open(&oargs, &scap_bpf_engine, nullptr, error_buf, rc); + return scap_open(&oargs, &scap_bpf_engine, error_buf, rc); } TEST(bpf, open_engine) diff --git a/test/libscap/test_suites/engines/kmod/kmod.cpp b/test/libscap/test_suites/engines/kmod/kmod.cpp index c670ec22b6..389c67f3da 100644 --- a/test/libscap/test_suites/engines/kmod/kmod.cpp +++ b/test/libscap/test_suites/engines/kmod/kmod.cpp @@ -102,7 +102,7 @@ scap_t* open_kmod_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim, }; oargs.engine_params = &kmod_params; - return scap_open(&oargs, &scap_kmod_engine, nullptr, error_buf, rc); + return scap_open(&oargs, &scap_kmod_engine, error_buf, rc); } TEST(kmod, open_engine) diff --git a/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp b/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp index 303ef6cd6c..f60f751f2b 100644 --- a/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp +++ b/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp @@ -33,7 +33,7 @@ scap_t* open_modern_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffe }; oargs.engine_params = &modern_bpf_params; - return scap_open(&oargs, &scap_modern_bpf_engine, nullptr, error_buf, rc); + return scap_open(&oargs, &scap_modern_bpf_engine, error_buf, rc); } TEST(modern_bpf, open_engine) diff --git a/userspace/libscap/CMakeLists.txt b/userspace/libscap/CMakeLists.txt index f5de004f23..1cd41df75a 100644 --- a/userspace/libscap/CMakeLists.txt +++ b/userspace/libscap/CMakeLists.txt @@ -66,19 +66,19 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scap_strl_config.h.in ${CMAKE_CURRENT add_library(scap scap.c scap_api_version.c - scap_fds.c scap_savefile.c - scap_platform.c - scap_platform_api.c - scap_procs.c - scap_userlist.c) + scap_platform_api.c) set_scap_target_properties(scap) add_library(scap_platform_util STATIC + scap_platform.c + scap_fds.c scap_iflist.c - scap_proc_util.c) + scap_proc_util.c + scap_procs.c + scap_userlist.c) add_dependencies(scap_platform_util uthash) target_link_libraries(scap diff --git a/userspace/libscap/examples/01-open/scap_open.c b/userspace/libscap/examples/01-open/scap_open.c index 0f79e625c6..d5ecd52912 100644 --- a/userspace/libscap/examples/01-open/scap_open.c +++ b/userspace/libscap/examples/01-open/scap_open.c @@ -899,7 +899,7 @@ int main(int argc, char** argv) enable_sc_and_print(); - g_h = scap_open(&oargs, vtable, NULL, error, &res); + g_h = scap_open(&oargs, vtable, error, &res); if(g_h == NULL || res != SCAP_SUCCESS) { fprintf(stderr, "%s (%d)\n", error, res); diff --git a/userspace/libscap/examples/02-validatebuffer/test.c b/userspace/libscap/examples/02-validatebuffer/test.c index d606d9cee0..37f6c96cce 100644 --- a/userspace/libscap/examples/02-validatebuffer/test.c +++ b/userspace/libscap/examples/02-validatebuffer/test.c @@ -171,7 +171,7 @@ int main() scap_open_args args = {}; - scap_t* h = scap_open(&args, &scap_kmod_engine, NULL, error, &ret); + scap_t* h = scap_open(&args, &scap_kmod_engine, error, &ret); if(h == NULL) { fprintf(stderr, "%s (%d)\n", error, ret); diff --git a/userspace/libscap/scap-int.h b/userspace/libscap/scap-int.h index 85bed7dd00..d136ff7df7 100644 --- a/userspace/libscap/scap-int.h +++ b/userspace/libscap/scap-int.h @@ -48,7 +48,6 @@ struct scap { const struct scap_vtable *m_vtable; struct scap_engine_handle m_engine; - struct scap_platform *m_platform; char m_lasterr[SCAP_LASTERR_SIZE]; diff --git a/userspace/libscap/scap.c b/userspace/libscap/scap.c index dc406155f9..8b438af5dd 100644 --- a/userspace/libscap/scap.c +++ b/userspace/libscap/scap.c @@ -86,13 +86,10 @@ scap_t* scap_alloc(void) return calloc(1, sizeof(scap_t)); } -int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable, - struct scap_platform* platform) +int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable) { int32_t rc; - handle->m_platform = platform; - ASSERT(vtable != NULL); // Initialize the engine before the platform @@ -109,20 +106,10 @@ int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtabl { return rc; } - - if(handle->m_platform) - { - if((rc = scap_platform_init(handle->m_platform, handle->m_lasterr, handle->m_engine, oargs)) != SCAP_SUCCESS) - { - return rc; - } - } - return SCAP_SUCCESS; } -scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform, char* error, - int32_t* rc) +scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, char* error, int32_t* rc) { scap_t* handle = scap_alloc(); if(!handle) @@ -131,7 +118,7 @@ scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, struc return NULL; } - *rc = scap_init(handle, oargs, vtable, platform); + *rc = scap_init(handle, oargs, vtable); if(*rc != SCAP_SUCCESS) { strlcpy(error, handle->m_lasterr, SCAP_LASTERR_SIZE); @@ -157,12 +144,6 @@ uint32_t scap_restart_capture(scap_t* handle) void scap_deinit(scap_t* handle) { - if(handle->m_platform) - { - scap_platform_close(handle->m_platform); - scap_platform_free(handle->m_platform); - } - if(handle->m_vtable) { /* The capture should be stopped before diff --git a/userspace/libscap/scap.h b/userspace/libscap/scap.h index 2a2d48c944..ac1e86dbe2 100644 --- a/userspace/libscap/scap.h +++ b/userspace/libscap/scap.h @@ -494,8 +494,7 @@ scap_t* scap_alloc(void); If this function fails, the only thing you can safely do with the handle is to call \ref scap_deinit on it. */ -int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable, - struct scap_platform* platform); +int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable); /*! \brief Allocate and initialize a handle @@ -516,8 +515,7 @@ int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtabl \return The capture instance handle in case of success. NULL in case of failure. */ -scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform, char* error, - int32_t* rc); +scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, char* error, int32_t* rc); /*! \brief Deinitialize a capture handle. diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index d72ac512ff..de09476e77 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -500,9 +500,12 @@ void sinsp::open_common(scap_open_args* oargs, const struct scap_vtable* vtable, throw scap_open_exception("failed to allocate scap handle", SCAP_FAILURE); } - int32_t scap_rc = scap_init(m_h, oargs, vtable, platform); + int32_t scap_rc = scap_init(m_h, oargs, vtable); if(scap_rc != SCAP_SUCCESS) { + scap_platform_close(platform); + scap_platform_free(platform); + std::string error = scap_getlasterr(m_h); scap_close(m_h); m_h = NULL; @@ -513,6 +516,18 @@ void sinsp::open_common(scap_open_args* oargs, const struct scap_vtable* vtable, throw scap_open_exception(error, scap_rc); } + scap_rc = scap_platform_init(platform, m_platform_lasterr, m_h->m_engine, oargs); + if(scap_rc != SCAP_SUCCESS) + { + scap_platform_close(platform); + scap_platform_free(platform); + scap_close(m_h); + m_h = NULL; + + throw scap_open_exception(m_platform_lasterr, scap_rc); + } + m_platform = platform; + init(); // enable generation of async meta-events for all loaded plugins supporting @@ -916,6 +931,13 @@ std::string sinsp::get_error_desc(const std::string& msg) void sinsp::close() { + if(m_platform) + { + scap_platform_close(m_platform); + scap_platform_free(m_platform); + m_platform = nullptr; + } + if(m_h) { scap_close(m_h); @@ -1047,7 +1069,6 @@ void sinsp::on_new_entry_from_proc(void* context, } else { - ASSERT(false); m_num_cpus = 0; } } @@ -3024,9 +3045,5 @@ uint64_t sinsp::get_new_ts() struct scap_platform* sinsp::get_scap_platform() { - if(!m_h) - { - return nullptr; - } - return m_h->m_platform; + return m_platform; } diff --git a/userspace/libsinsp/sinsp.h b/userspace/libsinsp/sinsp.h index 568310c3f7..edd8ec01ca 100644 --- a/userspace/libsinsp/sinsp.h +++ b/userspace/libsinsp/sinsp.h @@ -1136,6 +1136,8 @@ VISIBILITY_PRIVATE struct scap_platform* get_scap_platform(); scap_t* m_h; + struct scap_platform* m_platform {}; + char m_platform_lasterr[SCAP_LASTERR_SIZE]; uint64_t m_nevts; int64_t m_filesize; sinsp_mode_t m_mode = SINSP_MODE_NONE;