Skip to content

Commit

Permalink
refactor(userspace/libsinsp)!: file descriptor management
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Dellaluce <[email protected]>
  • Loading branch information
jasondellaluce authored and poiana committed Jan 23, 2024
1 parent 645315e commit 06ec625
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 451 deletions.
46 changes: 22 additions & 24 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ int lua_cbacks::get_machine_info(lua_State *ls)

int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool barebone)
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
uint32_t j;
sinsp_filter_compiler* compiler = NULL;
sinsp_filter* filter = NULL;
Expand Down Expand Up @@ -786,10 +785,10 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
bool match = false;

for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
for(auto fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = &(fdit->second);
tevt.m_fdinfo = fdit->second.get();
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
Expand Down Expand Up @@ -910,10 +909,10 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb

if(include_fds)
{
for(fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
for(auto fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = &(fdit->second);
tevt.m_fdinfo = fdit->second.get();
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
Expand All @@ -932,14 +931,14 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
if(!barebone)
{
lua_pushliteral(ls, "name");
lua_pushstring(ls, fdit->second.tostring_clean().c_str());
lua_pushstring(ls, fdit->second->tostring_clean().c_str());
lua_settable(ls, -3);
lua_pushliteral(ls, "type");
lua_pushstring(ls, fdit->second.get_typestring());
lua_pushstring(ls, fdit->second->get_typestring());
lua_settable(ls, -3);
}

scap_fd_type evt_type = fdit->second.m_type;
scap_fd_type evt_type = fdit->second->m_type;
if(evt_type == SCAP_FD_IPV4_SOCK || evt_type == SCAP_FD_IPV4_SERVSOCK ||
evt_type == SCAP_FD_IPV6_SOCK || evt_type == SCAP_FD_IPV6_SERVSOCK)
{
Expand All @@ -954,38 +953,38 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
include_client = true;
af = AF_INET;
cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dip);
cport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_sport;
sport = fdit->second.m_sockinfo.m_ipv4info.m_fields.m_dport;
is_server = fdit->second.is_role_server();
cip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv4info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv4info.m_fields.m_dip);
cport = fdit->second->m_sockinfo.m_ipv4info.m_fields.m_sport;
sport = fdit->second->m_sockinfo.m_ipv4info.m_fields.m_dport;
is_server = fdit->second->is_role_server();
}
else if (evt_type == SCAP_FD_IPV4_SERVSOCK)
{
include_client = false;
af = AF_INET;
cip = NULL;
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv4serverinfo.m_ip);
sport = fdit->second.m_sockinfo.m_ipv4serverinfo.m_port;
sip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv4serverinfo.m_ip);
sport = fdit->second->m_sockinfo.m_ipv4serverinfo.m_port;
is_server = true;
}
else if (evt_type == SCAP_FD_IPV6_SOCK)
{
include_client = true;
af = AF_INET6;
cip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dip);
cport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_sport;
sport = fdit->second.m_sockinfo.m_ipv6info.m_fields.m_dport;
is_server = fdit->second.is_role_server();
cip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv6info.m_fields.m_sip);
sip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv6info.m_fields.m_dip);
cport = fdit->second->m_sockinfo.m_ipv6info.m_fields.m_sport;
sport = fdit->second->m_sockinfo.m_ipv6info.m_fields.m_dport;
is_server = fdit->second->is_role_server();
}
else
{
include_client = false;
af = AF_INET6;
cip = NULL;
sip = (uint8_t*)&(fdit->second.m_sockinfo.m_ipv6serverinfo.m_ip);
sport = fdit->second.m_sockinfo.m_ipv6serverinfo.m_port;
sip = (uint8_t*)&(fdit->second->m_sockinfo.m_ipv6serverinfo.m_ip);
sport = fdit->second->m_sockinfo.m_ipv6serverinfo.m_port;
is_server = true;
}

Expand Down Expand Up @@ -1036,7 +1035,7 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb

// l4proto
const char* l4ps;
scap_l4_proto l4p = fdit->second.get_l4proto();
scap_l4_proto l4p = fdit->second->get_l4proto();

switch(l4p)
{
Expand Down Expand Up @@ -1111,7 +1110,6 @@ int lua_cbacks::get_thread_table_barebone_nofds(lua_State *ls)
int lua_cbacks::get_container_table(lua_State *ls)
{
#ifndef _WIN32
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
uint32_t j;
sinsp_evt tevt;

Expand Down
1 change: 1 addition & 0 deletions userspace/chisel/chisel_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ void chisel_table::process_proctable(sinsp_evt* evt)
tevt.m_cpuid = 0;
tevt.m_evtnum = 0;
tevt.m_pevt = &tscapevt;
tevt.m_fdinfo_ref.reset();
tevt.m_fdinfo = NULL;

threadtable->loop([&] (sinsp_threadinfo& tinfo) {
Expand Down
9 changes: 5 additions & 4 deletions userspace/libsinsp/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ int sinsp_evt::render_fd_json(Json::Value *ret, int64_t fd, const char** resolve

if(fd >= 0)
{
sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
char tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -695,7 +695,7 @@ char* sinsp_evt::render_fd(int64_t fd, const char** resolved_str, sinsp_evt::par

if(fd >= 0)
{
sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
char tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -1295,7 +1295,7 @@ const char* sinsp_evt::get_param_as_str(uint32_t id, OUT const char** resolved_s
int64_t fd = 0;
memcpy(&fd, param->m_val + pos, sizeof(uint64_t));

sinsp_fdinfo_t *fdinfo = tinfo->get_fd(fd);
sinsp_fdinfo *fdinfo = tinfo->get_fd(fd);
if(fdinfo)
{
tch = fdinfo->get_typechar();
Expand Down Expand Up @@ -2111,11 +2111,12 @@ bool sinsp_evt::clone_event(sinsp_evt &dest, const sinsp_evt &src)

// fd info
dest.m_fdinfo = nullptr;
dest.m_fdinfo_ref.reset();
if (src.m_fdinfo != nullptr)
{
//m_fdinfo_ref is only used to keep a handle to this
// copy of the fdinfo which was copied from the global fdinfo table
dest.m_fdinfo_ref.reset(new sinsp_fdinfo_t(*src.m_fdinfo));
dest.m_fdinfo_ref = std::move(src.m_fdinfo->clone());
dest.m_fdinfo = dest.m_fdinfo_ref.get();
}
dest.m_fdinfo_name_changed = src.m_fdinfo_name_changed;
Expand Down
13 changes: 9 additions & 4 deletions userspace/libsinsp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limitations under the License.
#include <libsinsp/gen_filter.h>
#include <libsinsp/settings.h>
#include <libsinsp/sinsp_exception.h>
#include <libsinsp/fdinfo.h>

class sinsp;
class sinsp_threadinfo;
Expand Down Expand Up @@ -370,7 +371,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
\note For events that are not I/O related, get_fd_info() returns NULL.
*/
inline sinsp_fdinfo_t* get_fd_info() const
inline sinsp_fdinfo* get_fd_info() const
{
return m_fdinfo;
}
Expand Down Expand Up @@ -507,6 +508,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_flags = EF_NONE;
m_info = &(m_event_info_table[m_pevt->type]);
m_fdinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo_name_changed = false;
m_iosize = 0;
m_poriginal_evt = NULL;
Expand All @@ -519,6 +521,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_tinfo_ref.reset();
m_tinfo = NULL;
m_fdinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo_name_changed = false;
}
inline void init(uint8_t* evdata, uint16_t cpuid)
Expand All @@ -528,6 +531,7 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
m_info = &(m_event_info_table[m_pevt->type]);
m_tinfo_ref.reset();
m_tinfo = NULL;
m_fdinfo_ref.reset();
m_fdinfo = NULL;
m_fdinfo_name_changed = false;
m_iosize = 0;
Expand All @@ -539,12 +543,13 @@ class SINSP_PUBLIC sinsp_evt : public gen_event
inline void init(scap_evt *scap_event,
ppm_event_info * ppm_event,
sinsp_threadinfo *threadinfo,
sinsp_fdinfo_t *fdinfo)
sinsp_fdinfo *fdinfo)
{
m_pevt = scap_event;
m_info = ppm_event;
m_tinfo_ref.reset(); // we don't own the threadinfo so don't try to manage its lifetime
m_tinfo = threadinfo;
m_tinfo_ref.reset();
m_fdinfo = fdinfo;
m_source_idx = sinsp_no_event_source_idx;
m_source_name = sinsp_no_event_source_name;
Expand Down Expand Up @@ -684,7 +689,7 @@ VISIBILITY_PRIVATE
// it should either be null, or point to the same place as m_tinfo
std::shared_ptr<sinsp_threadinfo> m_tinfo_ref;
sinsp_threadinfo* m_tinfo;
sinsp_fdinfo_t* m_fdinfo;
sinsp_fdinfo* m_fdinfo;

// If true, then the associated fdinfo changed names as a part
// of parsing this event.
Expand All @@ -696,7 +701,7 @@ VISIBILITY_PRIVATE
bool m_filtered_out;
const struct ppm_event_info* m_event_info_table;

std::shared_ptr<sinsp_fdinfo_t> m_fdinfo_ref;
std::shared_ptr<sinsp_fdinfo> m_fdinfo_ref;
// For some exit events, the "path" argument from the
// corresponding enter event is stored here.
std::unordered_map<std::string, std::string> m_enter_path_param;
Expand Down
Loading

0 comments on commit 06ec625

Please sign in to comment.