Skip to content

Commit

Permalink
cleanup(scap): remove m_platform from scap_t
Browse files Browse the repository at this point in the history
Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek authored and poiana committed Nov 6, 2023
1 parent c0041e3 commit e0aeca5
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 46 deletions.
2 changes: 1 addition & 1 deletion test/drivers/start_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/libscap/test_suites/engines/bpf/bpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/libscap/test_suites/engines/kmod/kmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/examples/01-open/scap_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion userspace/libscap/examples/02-validatebuffer/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion userspace/libscap/scap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
25 changes: 3 additions & 22 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions userspace/libscap/scap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
31 changes: 24 additions & 7 deletions userspace/libsinsp/sinsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1047,7 +1069,6 @@ void sinsp::on_new_entry_from_proc(void* context,
}
else
{
ASSERT(false);
m_num_cpus = 0;
}
}
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e0aeca5

Please sign in to comment.