Skip to content

Commit

Permalink
refactor(userspace/libsinsp)!: access file descriptor tables through …
Browse files Browse the repository at this point in the history
…apis

Signed-off-by: Jason Dellaluce <[email protected]>
  • Loading branch information
jasondellaluce authored and poiana committed Jan 23, 2024
1 parent 06ec625 commit 8b05ac9
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 101 deletions.
63 changes: 32 additions & 31 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,22 +785,22 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
{
bool match = false;

for(auto fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
fdtable->loop([&](int64_t fd, sinsp_fdinfo& fdi) {
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = fdit->second.get();
tevt.m_fdinfo = &fdi;
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
tevt.m_tinfo->m_lastevent_fd = fd;

if(filter->run(&tevt))
{
match = true;
break;
return false; // break out of the loop
}

tevt.m_tinfo->m_lastevent_fd = tlefd;
}
return true;
});

if(!match)
{
Expand Down Expand Up @@ -909,19 +909,19 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb

if(include_fds)
{
for(auto fdit = fdtable->m_table.begin(); fdit != fdtable->m_table.end(); ++fdit)
{
fdtable->loop([&](int64_t fd, sinsp_fdinfo& fdi) {
tevt.m_tinfo = &tinfo;
tevt.m_fdinfo = fdit->second.get();
tevt.m_fdinfo = &fdi;
tscapevt.tid = tinfo.m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdit->first;
tevt.m_tinfo->m_lastevent_fd = fd;

if(filter != NULL)
{
if(filter->run(&tevt) == false)
{
continue;
// continue the loop to next iteration
return true;
}
}

Expand All @@ -931,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, fdi.tostring_clean().c_str());
lua_settable(ls, -3);
lua_pushliteral(ls, "type");
lua_pushstring(ls, fdit->second->get_typestring());
lua_pushstring(ls, fdi.get_typestring());
lua_settable(ls, -3);
}

scap_fd_type evt_type = fdit->second->m_type;
scap_fd_type evt_type = fdi.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 @@ -953,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*)&(fdi.m_sockinfo.m_ipv4info.m_fields.m_sip);
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv4info.m_fields.m_dip);
cport = fdi.m_sockinfo.m_ipv4info.m_fields.m_sport;
sport = fdi.m_sockinfo.m_ipv4info.m_fields.m_dport;
is_server = fdi.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*)&(fdi.m_sockinfo.m_ipv4serverinfo.m_ip);
sport = fdi.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*)&(fdi.m_sockinfo.m_ipv6info.m_fields.m_sip);
sip = (uint8_t*)&(fdi.m_sockinfo.m_ipv6info.m_fields.m_dip);
cport = fdi.m_sockinfo.m_ipv6info.m_fields.m_sport;
sport = fdi.m_sockinfo.m_ipv6info.m_fields.m_dport;
is_server = fdi.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*)&(fdi.m_sockinfo.m_ipv6serverinfo.m_ip);
sport = fdi.m_sockinfo.m_ipv6serverinfo.m_port;
is_server = true;
}

Expand Down Expand Up @@ -1035,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 = fdi.get_l4proto();

switch(l4p)
{
Expand Down Expand Up @@ -1065,8 +1065,9 @@ int lua_cbacks::get_thread_table_int(lua_State *ls, bool include_fds, bool bareb
// is_server
string l4proto;

lua_rawseti(ls,-2, (uint32_t)fdit->first);
}
lua_rawseti(ls,-2, (uint32_t)fd);
return true;
});
}


Expand Down
29 changes: 27 additions & 2 deletions userspace/libsinsp/fdinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ class SINSP_PUBLIC sinsp_fdinfo
class sinsp_fdtable
{
public:
typedef std::function<bool(int64_t, const sinsp_fdinfo&)> fdtable_visitor_t;
typedef std::function<bool(int64_t, sinsp_fdinfo&)> fdtable_visitor_t;

typedef std::function<bool(int64_t, const sinsp_fdinfo&)> fdtable_const_visitor_t;

sinsp_fdtable(sinsp* inspector);

Expand All @@ -456,7 +458,19 @@ class sinsp_fdtable

sinsp_fdinfo* add(int64_t fd, std::unique_ptr<sinsp_fdinfo> fdinfo);

bool loop(const fdtable_visitor_t callback) const
inline bool const_loop(const fdtable_const_visitor_t callback) const
{
for(auto it = m_table.begin(); it != m_table.end(); ++it)
{
if (!callback(it->first, *(it->second.get())))
{
return false;
}
}
return true;
}

inline bool loop(const fdtable_visitor_t callback)
{
for(auto it = m_table.begin(); it != m_table.end(); ++it)
{
Expand All @@ -477,6 +491,17 @@ class sinsp_fdtable

void reset_cache();

inline uint64_t get_tid() const
{
return m_tid;
}

inline void set_tid(uint64_t v)
{
m_tid = v;
}

private:
sinsp* m_inspector;
std::unordered_map<int64_t, std::unique_ptr<sinsp_fdinfo>> m_table;

Expand Down
29 changes: 11 additions & 18 deletions userspace/libsinsp/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,17 +1159,14 @@ void sinsp_parser::parse_clone_exit_caller(sinsp_evt *evt, int64_t child_tid)
if(fd_table_ptr != NULL)
{
child_tinfo->m_fdtable.clear();
fd_table_ptr->loop([child_tinfo](int64_t fd, const sinsp_fdinfo& info) {
child_tinfo->m_fdtable.add(fd, std::move(info.clone()));
fd_table_ptr->const_loop([child_tinfo](int64_t fd, const sinsp_fdinfo& info) {
/* Track down that those are cloned fds */
auto newinfo = info.clone();
newinfo->set_is_cloned();
child_tinfo->m_fdtable.add(fd, std::move(newinfo));
return true;
});

/* Track down that those are cloned fds */
for(auto fdit = child_tinfo->m_fdtable.m_table.begin(); fdit != child_tinfo->m_fdtable.m_table.end(); ++fdit)
{
fdit->second->set_is_cloned();
}

/* It's important to reset the cache of the child thread, to prevent it from
* referring to an element in the parent's table.
*/
Expand Down Expand Up @@ -1757,19 +1754,15 @@ void sinsp_parser::parse_clone_exit_child(sinsp_evt *evt)
{
child_tinfo->m_fdtable.clear();
fd_table_ptr->loop([child_tinfo](int64_t fd, const sinsp_fdinfo& info) {
child_tinfo->m_fdtable.add(fd, std::move(info.clone()));
/* Track down that those are cloned fds.
* This flag `FLAGS_IS_CLONED` seems to be never used...
*/
auto newinfo = info.clone();
newinfo->set_is_cloned();
child_tinfo->m_fdtable.add(fd, std::move(newinfo));
return true;
});

/* Track down that those are cloned fds.
* This flag `FLAGS_IS_CLONED` seems to be never used...
*/
for(auto fdit = child_tinfo->m_fdtable.m_table.begin();
fdit != child_tinfo->m_fdtable.m_table.end(); ++fdit)
{
fdit->second->set_is_cloned();
}

/* It's important to reset the cache of the child thread, to prevent it from
* referring to an element in the parent's table.
*/
Expand Down
Loading

0 comments on commit 8b05ac9

Please sign in to comment.