Skip to content

Commit

Permalink
Implement PR review's suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Aponte <[email protected]>
  • Loading branch information
federico-sysdig committed Aug 1, 2024
1 parent d5f662d commit f0346c0
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 145 deletions.
42 changes: 21 additions & 21 deletions userspace/libscap/engine/bpf/scap_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ limitations under the License.
#include <dirent.h>
#include <libscap/strl.h>

#define SCAP_HANDLE_T struct bpf_engine
#define HANDLE(engine) ((struct bpf_engine*)(engine.m_handle))

#include <libscap/engine/bpf/bpf.h>
#include <libscap/engine_handle.h>
Expand Down Expand Up @@ -114,7 +114,7 @@ struct bpf_map_data {
struct bpf_map_def def;
};

static struct bpf_engine* alloc_handle(scap_t* main_handle, char* lasterr_ptr)
static void* alloc_handle(scap_t* main_handle, char* lasterr_ptr)

Check warning on line 117 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L117

Added line #L117 was not covered by tests
{
struct bpf_engine *engine = calloc(1, sizeof(struct bpf_engine));
if(engine)
Expand Down Expand Up @@ -1163,23 +1163,23 @@ int32_t scap_bpf_stop_capture(struct scap_engine_handle engine)
static int32_t calibrate_socket_file_ops(struct scap_engine_handle engine)
{
/* We just need to enable the socket syscall for the socket calibration */
((SCAP_HANDLE_T*)engine.m_handle)->curr_sc_set.ppm_sc[PPM_SC_SOCKET] = 1;
HANDLE(engine)->curr_sc_set.ppm_sc[PPM_SC_SOCKET] = 1;

Check warning on line 1166 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1166

Added line #L1166 was not covered by tests
if(scap_bpf_start_capture(engine) != SCAP_SUCCESS)
{
return scap_errprintf(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, errno, "unable to set the socket syscall for the calibration");
return scap_errprintf(HANDLE(engine)->m_lasterr, errno, "unable to set the socket syscall for the calibration");

Check warning on line 1169 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1169

Added line #L1169 was not covered by tests
}

int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1)
{
return scap_errprintf(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, errno, "unable to create a socket for the calibration");
return scap_errprintf(HANDLE(engine)->m_lasterr, errno, "unable to create a socket for the calibration");

Check warning on line 1175 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1175

Added line #L1175 was not covered by tests
}
close(fd);

/* We need to stop the capture */
if(scap_bpf_stop_capture(engine) != SCAP_SUCCESS)
{
return scap_errprintf(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, errno, "unable to stop the capture after the calibration");
return scap_errprintf(HANDLE(engine)->m_lasterr, errno, "unable to stop the capture after the calibration");

Check warning on line 1182 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1182

Added line #L1182 was not covered by tests
}

return SCAP_SUCCESS;
Expand Down Expand Up @@ -1890,7 +1890,7 @@ int32_t scap_bpf_get_n_tracepoint_hit(struct scap_engine_handle engine, long* re

static int32_t next(struct scap_engine_handle engine, scap_evt **pevent, uint16_t *pdevid, uint32_t *pflags)
{
return ringbuffer_next(&((SCAP_HANDLE_T*)engine.m_handle)->m_dev_set, pevent, pdevid, pflags);
return ringbuffer_next(&HANDLE(engine)->m_dev_set, pevent, pdevid, pflags);

Check warning on line 1893 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1893

Added line #L1893 was not covered by tests
}

static int32_t unsupported_config(struct scap_engine_handle engine, const char* msg)
Expand Down Expand Up @@ -1985,7 +1985,7 @@ static int32_t init(scap_t* handle, scap_open_args *oargs)
struct scap_bpf_engine_params *params = oargs->engine_params;
strlcpy(bpf_probe_buf, params->bpf_probe, SCAP_MAX_PATH_SIZE);

if(check_buffer_bytes_dim(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, params->buffer_bytes_dim) != SCAP_SUCCESS)
if(check_buffer_bytes_dim(HANDLE(engine)->m_lasterr, params->buffer_bytes_dim) != SCAP_SUCCESS)
{
return SCAP_FAILURE;
}
Expand All @@ -1996,18 +1996,18 @@ static int32_t init(scap_t* handle, scap_open_args *oargs)
ssize_t num_cpus = sysconf(_SC_NPROCESSORS_CONF);
if(num_cpus == -1)
{
return scap_errprintf(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, errno, "cannot obtain the number of available CPUs from '_SC_NPROCESSORS_CONF'");
return scap_errprintf(HANDLE(engine)->m_lasterr, errno, "cannot obtain the number of available CPUs from '_SC_NPROCESSORS_CONF'");

Check warning on line 1999 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L1999

Added line #L1999 was not covered by tests
}

((SCAP_HANDLE_T*)engine.m_handle)->m_ncpus = num_cpus;
HANDLE(engine)->m_ncpus = num_cpus;

Check warning on line 2002 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2002

Added line #L2002 was not covered by tests

ssize_t num_devs = sysconf(_SC_NPROCESSORS_ONLN);
if(num_devs == -1)
{
return scap_errprintf(((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr, errno, "cannot obtain the number of online CPUs from '_SC_NPROCESSORS_ONLN'");
return scap_errprintf(HANDLE(engine)->m_lasterr, errno, "cannot obtain the number of online CPUs from '_SC_NPROCESSORS_ONLN'");

Check warning on line 2007 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2007

Added line #L2007 was not covered by tests
}

rc = devset_init(&((SCAP_HANDLE_T*)engine.m_handle)->m_dev_set, num_devs, ((SCAP_HANDLE_T*)engine.m_handle)->m_lasterr);
rc = devset_init(&HANDLE(engine)->m_dev_set, num_devs, HANDLE(engine)->m_lasterr);

Check warning on line 2010 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2010

Added line #L2010 was not covered by tests
if(rc != SCAP_SUCCESS)
{
return rc;
Expand All @@ -2028,32 +2028,32 @@ static int32_t init(scap_t* handle, scap_open_args *oargs)
}

/* Store interesting sc codes */
memcpy(&((SCAP_HANDLE_T*)engine.m_handle)->curr_sc_set, &oargs->ppm_sc_of_interest, sizeof(interesting_ppm_sc_set));
memcpy(&HANDLE(engine)->curr_sc_set, &oargs->ppm_sc_of_interest, sizeof(interesting_ppm_sc_set));

Check warning on line 2031 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2031

Added line #L2031 was not covered by tests

((SCAP_HANDLE_T*)engine.m_handle)->m_flags = 0;
HANDLE(engine)->m_flags = 0;

Check warning on line 2033 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2033

Added line #L2033 was not covered by tests
if(scap_get_bpf_stats_enabled())
{
((SCAP_HANDLE_T*)engine.m_handle)->m_flags |= ENGINE_FLAG_BPF_STATS_ENABLED;
HANDLE(engine)->m_flags |= ENGINE_FLAG_BPF_STATS_ENABLED;

Check warning on line 2036 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2036

Added line #L2036 was not covered by tests
}

return SCAP_SUCCESS;
}

static uint64_t get_flags(struct scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->m_flags;
return HANDLE(engine)->m_flags;

Check warning on line 2044 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2044

Added line #L2044 was not covered by tests
}

static uint32_t get_n_devs(struct scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->m_dev_set.m_ndevs;
return HANDLE(engine)->m_dev_set.m_ndevs;

Check warning on line 2049 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2049

Added line #L2049 was not covered by tests
}

static uint64_t get_max_buf_used(struct scap_engine_handle engine)
{
uint64_t i;
uint64_t max = 0;
struct scap_device_set *devset = &((SCAP_HANDLE_T*)engine.m_handle)->m_dev_set;
struct scap_device_set *devset = &HANDLE(engine)->m_dev_set;

Check warning on line 2056 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2056

Added line #L2056 was not covered by tests

for(i = 0; i < devset->m_ndevs; i++)
{
Expand All @@ -2066,19 +2066,19 @@ static uint64_t get_max_buf_used(struct scap_engine_handle engine)

uint64_t scap_bpf_get_api_version(struct scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->m_api_version;
return HANDLE(engine)->m_api_version;

Check warning on line 2069 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2069

Added line #L2069 was not covered by tests
}

uint64_t scap_bpf_get_schema_version(struct scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->m_schema_version;
return HANDLE(engine)->m_schema_version;

Check warning on line 2074 in userspace/libscap/engine/bpf/scap_bpf.c

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/bpf/scap_bpf.c#L2074

Added line #L2074 was not covered by tests
}

const struct scap_vtable scap_bpf_engine = {
.name = BPF_ENGINE,
.savefile_ops = NULL,

.alloc_handle = (void* (*)(scap_t*, char*))alloc_handle,
.alloc_handle = alloc_handle,
.init = init,
.get_flags = get_flags,
.free_handle = free_handle,
Expand Down
27 changes: 11 additions & 16 deletions userspace/libscap/engine/gvisor/gvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

namespace scap_gvisor {
class engine;
}

#define SCAP_HANDLE_T scap_gvisor::engine
#define HANDLE(engine) ((scap_gvisor::engine*)(engine.m_handle))

#include <libscap/engine/gvisor/gvisor.h>
#include <libscap/engine/gvisor/gvisor_platform.h>
Expand Down Expand Up @@ -142,41 +137,41 @@ scap_platform* scap_gvisor_alloc_platform(proc_entry_callback proc_callback, voi
return &platform->m_generic;
}

SCAP_HANDLE_T* gvisor_alloc_handle(scap_t* main_handle, char* lasterr_ptr)
void* gvisor_alloc_handle(scap_t* main_handle, char* lasterr_ptr)

Check warning on line 140 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L140

Added line #L140 was not covered by tests
{
return new scap_gvisor::engine(lasterr_ptr);
}

int32_t gvisor_init(scap_t* main_handle, scap_open_args* oargs)
{
auto gv = (SCAP_HANDLE_T*)main_handle->m_engine.m_handle;
auto gv = reinterpret_cast<scap_gvisor::engine*>(main_handle->m_engine.m_handle);

Check warning on line 147 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L147

Added line #L147 was not covered by tests
auto params = (scap_gvisor_engine_params*)oargs->engine_params;
return gv->init(params->gvisor_config_path, params->gvisor_root_path, params->no_events, params->gvisor_epoll_timeout, params->gvisor_platform);
}

void gvisor_free_handle(scap_engine_handle engine)
{
delete (SCAP_HANDLE_T*)engine.m_handle;
delete reinterpret_cast<scap_gvisor::engine*>(engine.m_handle);
}

int32_t gvisor_start_capture(scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->start_capture();
return HANDLE(engine)->start_capture();

Check warning on line 159 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L159

Added line #L159 was not covered by tests
}

int32_t gvisor_close(scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->close();
return HANDLE(engine)->close();

Check warning on line 164 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L164

Added line #L164 was not covered by tests
}

int32_t gvisor_stop_capture(scap_engine_handle engine)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->stop_capture();
return HANDLE(engine)->stop_capture();

Check warning on line 169 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L169

Added line #L169 was not covered by tests
}

int32_t gvisor_next(scap_engine_handle engine, scap_evt** pevent, uint16_t* pdevid, uint32_t* pflags)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->next(pevent, pdevid, pflags);
return HANDLE(engine)->next(pevent, pdevid, pflags);

Check warning on line 174 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L174

Added line #L174 was not covered by tests
}

int32_t gvisor_configure(scap_engine_handle engine, scap_setting setting, unsigned long arg1, unsigned long arg2)
Expand All @@ -186,12 +181,12 @@ int32_t gvisor_configure(scap_engine_handle engine, scap_setting setting, unsign

int32_t gvisor_get_stats(scap_engine_handle engine, scap_stats* stats)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->get_stats(stats);
return HANDLE(engine)->get_stats(stats);

Check warning on line 184 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L184

Added line #L184 was not covered by tests
}

const metrics_v2* gvisor_get_stats_v2(scap_engine_handle engine, uint32_t flags, uint32_t* nstats, int32_t* rc)
{
return ((SCAP_HANDLE_T*)engine.m_handle)->get_stats_v2(flags, nstats, rc);
return HANDLE(engine)->get_stats_v2(flags, nstats, rc);

Check warning on line 189 in userspace/libscap/engine/gvisor/gvisor.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libscap/engine/gvisor/gvisor.cpp#L189

Added line #L189 was not covered by tests
}

int32_t gvisor_get_n_tracepoint_hit(scap_engine_handle engine, long* ret)
Expand All @@ -215,7 +210,7 @@ extern const scap_vtable scap_gvisor_engine = {
.name = GVISOR_ENGINE,
.savefile_ops = nullptr,

.alloc_handle = (void* (*)(scap_t*, char*))gvisor_alloc_handle,
.alloc_handle = gvisor_alloc_handle,
.init = gvisor_init,
.get_flags = nullptr,
.free_handle = gvisor_free_handle,
Expand Down
Loading

0 comments on commit f0346c0

Please sign in to comment.