Skip to content

Commit

Permalink
Introduce devset_init
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 Jun 6, 2022
1 parent 74e2c09 commit 53aad03
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 61 deletions.
3 changes: 2 additions & 1 deletion userspace/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ list(APPEND targetfiles
syscall_info_table.c
../../driver/dynamic_params_table.c
../../driver/event_table.c
../../driver/flags_table.c)
../../driver/flags_table.c
ringbuffer/devset.c)

if(NOT APPLE)
list(APPEND targetfiles
Expand Down
40 changes: 40 additions & 0 deletions userspace/libscap/ringbuffer/devset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "devset.h"

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>

#ifndef _WIN32
#include <sys/mman.h>
#endif

#include "../../common/strlcpy.h"
#include "../scap.h"
#include "../scap-int.h"

int32_t devset_init(struct scap_device_set *devset, size_t num_devs, char *lasterr)
{
devset->m_ndevs = num_devs;

devset->m_devs = (scap_device*) calloc(sizeof(scap_device), devset->m_ndevs);
if(!devset->m_devs)
{
strlcpy(lasterr, "error allocating the device handles", SCAP_LASTERR_SIZE);
return SCAP_FAILURE;
}

for(size_t j = 0; j < num_devs; ++j)
{
devset->m_devs[j].m_buffer = MAP_FAILED;
devset->m_devs[j].m_bufinfo = MAP_FAILED;
devset->m_devs[j].m_bufstatus = MAP_FAILED;
devset->m_devs[j].m_fd = -1;
devset->m_devs[j].m_bufinfo_fd = -1;
devset->m_devs[j].m_lastreadsize = 0;
devset->m_devs[j].m_sn_len = 0;
}
devset->m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START;
devset->m_lasterr = lasterr;

return SCAP_SUCCESS;
}
57 changes: 57 additions & 0 deletions userspace/libscap/ringbuffer/devset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (C) 2022 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include <stdint.h>
#include <stddef.h>

struct ppm_ring_buffer_info;
struct udig_ring_buffer_status;

//
// The device descriptor
//
typedef struct scap_device
{
int m_fd;
int m_bufinfo_fd; // used by udig
char* m_buffer;
uint32_t m_buffer_size; // used by udig
uint32_t m_lastreadsize;
char* m_sn_next_event; // Pointer to the next event available for scap_next
uint32_t m_sn_len; // Number of bytes available in the buffer pointed by m_sn_next_event
union
{
// Anonymous struct with ppm stuff
struct
{
struct ppm_ring_buffer_info* m_bufinfo;
struct udig_ring_buffer_status* m_bufstatus; // used by udig
};
};
} scap_device;

struct scap_device_set
{
scap_device* m_devs;
uint32_t m_ndevs;
uint64_t m_buffer_empty_wait_time_us;
char* m_lasterr;
};

int32_t devset_init(struct scap_device_set *devset, size_t num_devs, char *lasterr);
31 changes: 1 addition & 30 deletions userspace/libscap/scap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.

#include "engine_handle.h"
#include "scap_vtable.h"
#include "ringbuffer/devset.h"

#include "settings.h"
#include "plugin_info.h"
Expand Down Expand Up @@ -84,36 +85,6 @@ inline const char *gzerror(FILE *F, int *E) {*E = ferror(F); return "error readi

#define BPF_MAPS_MAX 32

//
// The device descriptor
//
typedef struct scap_device
{
int m_fd;
int m_bufinfo_fd; // used by udig
char* m_buffer;
uint32_t m_buffer_size; // used by udig
uint32_t m_lastreadsize;
char* m_sn_next_event; // Pointer to the next event available for scap_next
uint32_t m_sn_len; // Number of bytes available in the buffer pointed by m_sn_next_event
union
{
// Anonymous struct with ppm stuff
struct
{
struct ppm_ring_buffer_info* m_bufinfo;
struct udig_ring_buffer_status* m_bufstatus; // used by udig
};
};
}scap_device;

struct scap_device_set
{
scap_device* m_devs;
uint32_t m_ndevs;
uint64_t m_buffer_empty_wait_time_us;
};

typedef struct scap_tid
{
uint64_t tid;
Expand Down
34 changes: 4 additions & 30 deletions userspace/libscap/scap.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,13 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
return NULL;
}

handle->m_dev_set.m_devs = (scap_device*) calloc(sizeof(scap_device), ndevs);
if(!handle->m_dev_set.m_devs)
*rc = devset_init(&handle->m_dev_set, ndevs, error);
if(*rc != SCAP_SUCCESS)
{
scap_close(handle);
snprintf(error, SCAP_LASTERR_SIZE, "error allocating the device handles");
*rc = SCAP_FAILURE;
return NULL;
}

for(j = 0; j < ndevs; j++)
{
handle->m_dev_set.m_devs[j].m_buffer = (char*)MAP_FAILED;
if(!handle->m_bpf)
{
handle->m_dev_set.m_devs[j].m_bufinfo = (struct ppm_ring_buffer_info*)MAP_FAILED;
handle->m_dev_set.m_devs[j].m_bufstatus = (struct udig_ring_buffer_status*)MAP_FAILED;
}
}

handle->m_dev_set.m_ndevs = ndevs;

//
// Extract machine information
//
Expand Down Expand Up @@ -302,7 +288,6 @@ scap_t* scap_open_live_int(char *error, int32_t *rc,
handle->m_num_suppressed_comms = 0;
handle->m_suppressed_tids = NULL;
handle->m_num_suppressed_evts = 0;
handle->m_dev_set.m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START;

if ((*rc = copy_comms(handle, suppressed_comms)) != SCAP_SUCCESS)
{
Expand Down Expand Up @@ -623,23 +608,13 @@ scap_t* scap_open_udig_int(char *error, int32_t *rc,
handle->m_udig_capturing = false;
handle->m_ncpus = 1;

handle->m_dev_set.m_ndevs = 1;

handle->m_dev_set.m_devs = (scap_device*) calloc(sizeof(scap_device), handle->m_dev_set.m_ndevs);
if(!handle->m_dev_set.m_devs)
*rc = devset_init(&handle->m_dev_set, 1, error);
if(*rc != SCAP_SUCCESS)
{
scap_close(handle);
snprintf(error, SCAP_LASTERR_SIZE, "error allocating the device handles");
*rc = SCAP_FAILURE;
return NULL;
}

handle->m_dev_set.m_devs[0].m_buffer = MAP_FAILED;
handle->m_dev_set.m_devs[0].m_bufinfo = MAP_FAILED;
handle->m_dev_set.m_devs[0].m_bufstatus = MAP_FAILED;
handle->m_dev_set.m_devs[0].m_fd = -1;
handle->m_dev_set.m_devs[0].m_bufinfo_fd = -1;

//
// Extract machine information
//
Expand Down Expand Up @@ -698,7 +673,6 @@ scap_t* scap_open_udig_int(char *error, int32_t *rc,
handle->m_num_suppressed_comms = 0;
handle->m_suppressed_tids = NULL;
handle->m_num_suppressed_evts = 0;
handle->m_dev_set.m_buffer_empty_wait_time_us = BUFFER_EMPTY_WAIT_TIME_US_START;

#ifdef _WIN32
handle->m_whh = scap_windows_hal_open(error);
Expand Down

0 comments on commit 53aad03

Please sign in to comment.