Skip to content

Commit

Permalink
cleanup(libsinsp): use string_view in user/group functions
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Guerra <[email protected]>
  • Loading branch information
LucaGuerra authored and poiana committed Jan 17, 2024
1 parent 393da1e commit fc4b79c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
4 changes: 2 additions & 2 deletions userspace/libsinsp/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5310,10 +5310,10 @@ void sinsp_parser::parse_user_evt(sinsp_evt *evt)

if (evt->m_pevt->type == PPME_USER_ADDED_E)
{
m_inspector->m_usergroup_manager.add_user(container_id.data(), -1, uid, gid, name.data(), home.data(), shell.data());
m_inspector->m_usergroup_manager.add_user(std::string(container_id), -1, uid, gid, name, home, shell);
} else
{
m_inspector->m_usergroup_manager.rm_user(container_id.data(), uid);
m_inspector->m_usergroup_manager.rm_user(std::string(container_id), uid);
}
}

Expand Down
8 changes: 4 additions & 4 deletions userspace/libsinsp/test/user.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ TEST_F(usergroup_manager_test, system_lookup)

sinsp_usergroup_manager mgr(&m_inspector);

mgr.add_user(container_id, -1, 0, 0, nullptr, nullptr, nullptr);
mgr.add_user(container_id, -1, 0, 0, {}, {}, {});
auto* user = mgr.get_user(container_id, 0);
ASSERT_NE(user, nullptr);
ASSERT_EQ(user->uid, 0);
Expand All @@ -105,7 +105,7 @@ TEST_F(usergroup_manager_test, system_lookup)
#endif
ASSERT_EQ(std::string(user->shell).empty(), false);

mgr.add_group(container_id, -1, 0, nullptr);
mgr.add_group(container_id, -1, 0, {});
auto* group = mgr.get_group(container_id, 0);
ASSERT_NE(group, nullptr);
ASSERT_EQ(group->gid, 0);
Expand Down Expand Up @@ -196,7 +196,7 @@ TEST_F(usergroup_manager_host_root_test, host_root_lookup)

sinsp_usergroup_manager mgr(&m_inspector);

mgr.add_user(container_id, -1, 0, 0, nullptr, nullptr, nullptr);
mgr.add_user(container_id, -1, 0, 0, {}, {}, {});
auto* user = mgr.get_user(container_id, 0);
ASSERT_NE(user, nullptr);
ASSERT_EQ(user->uid, 0);
Expand All @@ -205,7 +205,7 @@ TEST_F(usergroup_manager_host_root_test, host_root_lookup)
ASSERT_STREQ(user->homedir, "/toor");
ASSERT_STREQ(user->shell, "/bin/ash");

mgr.add_group(container_id, -1, 0, nullptr);
mgr.add_group(container_id, -1, 0, {});
auto* group = mgr.get_group(container_id, 0);
ASSERT_NE(group, nullptr);
ASSERT_EQ(group->gid, 0);
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/threadinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ void sinsp_threadinfo::set_user(uint32_t uid)
if (!user)
{
auto notify = m_inspector->is_live() || m_inspector->is_syscall_plugin();
user = m_inspector->m_usergroup_manager.add_user(m_container_id, m_pid, uid, m_group.gid, NULL, NULL, NULL, notify);
user = m_inspector->m_usergroup_manager.add_user(m_container_id, m_pid, uid, m_group.gid, {}, {}, {}, notify);
}
if (user)
{
Expand All @@ -538,7 +538,7 @@ void sinsp_threadinfo::set_group(uint32_t gid)
if (!group)
{
auto notify = m_inspector->is_live() || m_inspector->is_syscall_plugin();
group = m_inspector->m_usergroup_manager.add_group(m_container_id, m_pid, gid, NULL, notify);
group = m_inspector->m_usergroup_manager.add_group(m_container_id, m_pid, gid, {}, notify);
}
if (group)
{
Expand Down
46 changes: 20 additions & 26 deletions userspace/libsinsp/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,41 +242,35 @@ scap_userinfo *sinsp_usergroup_manager::userinfo_map_insert(
userinfo_map &map,
uint32_t uid,
uint32_t gid,
const char *name,
const char *home,
const char *shell)
std::string_view name,
std::string_view home,
std::string_view shell)
{
ASSERT(name);
ASSERT(home);
ASSERT(shell);

auto &usr = map[uid];
usr.uid = uid;
usr.gid = gid;
// In case the node is configured to use NIS,
// some struct passwd* fields may be set to NULL.
strlcpy(usr.name, (name != nullptr) ? name : "<NA>", MAX_CREDENTIALS_STR_LEN);
strlcpy(usr.homedir, (home != nullptr) ? home : "<NA>", SCAP_MAX_PATH_SIZE);
strlcpy(usr.shell, (shell != nullptr) ? shell : "<NA>", SCAP_MAX_PATH_SIZE);
strlcpy(usr.name, (name.data() != nullptr) ? std::string(name).c_str() : "<NA>", MAX_CREDENTIALS_STR_LEN);
strlcpy(usr.homedir, (home.data() != nullptr) ? std::string(home).c_str() : "<NA>", SCAP_MAX_PATH_SIZE);
strlcpy(usr.shell, (shell.data() != nullptr) ? std::string(shell).c_str() : "<NA>", SCAP_MAX_PATH_SIZE);

return &usr;
}

scap_groupinfo *sinsp_usergroup_manager::groupinfo_map_insert(
groupinfo_map &map,
uint32_t gid,
const char *name)
std::string_view name)
{
ASSERT(name);

auto &grp = map[gid];
grp.gid = gid;
strlcpy(grp.name, (name != nullptr) ? name : "<NA>", MAX_CREDENTIALS_STR_LEN);
strlcpy(grp.name, (name.data() != nullptr) ? std::string(name).c_str() : "<NA>", MAX_CREDENTIALS_STR_LEN);

return &grp;
}

scap_userinfo *sinsp_usergroup_manager::add_user(const string &container_id, int64_t pid, uint32_t uid, uint32_t gid, const char *name, const char *home, const char *shell, bool notify)
scap_userinfo *sinsp_usergroup_manager::add_user(const std::string &container_id, int64_t pid, uint32_t uid, uint32_t gid, std::string_view name, std::string_view home, std::string_view shell, bool notify)
{
if (!m_import_users)
{
Expand All @@ -289,11 +283,11 @@ scap_userinfo *sinsp_usergroup_manager::add_user(const string &container_id, int
if(usr)
{
// Update user if it was already there
if (name)
if (name.data() != nullptr)
{
strlcpy(usr->name, name, MAX_CREDENTIALS_STR_LEN);
strlcpy(usr->homedir, home, SCAP_MAX_PATH_SIZE);
strlcpy(usr->shell, shell, SCAP_MAX_PATH_SIZE);
strlcpy(usr->name, std::string(name).c_str(), MAX_CREDENTIALS_STR_LEN);
strlcpy(usr->homedir, std::string(home).c_str(), SCAP_MAX_PATH_SIZE);
strlcpy(usr->shell, std::string(shell).c_str(), SCAP_MAX_PATH_SIZE);
}
return usr;
}
Expand All @@ -305,13 +299,13 @@ scap_userinfo *sinsp_usergroup_manager::add_user(const string &container_id, int
return add_container_user(container_id, pid, uid, notify);
}

scap_userinfo *sinsp_usergroup_manager::add_host_user(uint32_t uid, uint32_t gid, const char *name, const char *home, const char *shell, bool notify)
scap_userinfo *sinsp_usergroup_manager::add_host_user(uint32_t uid, uint32_t gid, std::string_view name, std::string_view home, std::string_view shell, bool notify)
{
libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
"adding host user: name: %s", name);

scap_userinfo *retval{nullptr};
if (name)
if (name.data() != nullptr)
{
retval = userinfo_map_insert(
m_userlist[""],
Expand Down Expand Up @@ -411,7 +405,7 @@ bool sinsp_usergroup_manager::rm_user(const string &container_id, uint32_t uid,
return res;
}

scap_groupinfo *sinsp_usergroup_manager::add_group(const string &container_id, int64_t pid, uint32_t gid, const char *name, bool notify)
scap_groupinfo *sinsp_usergroup_manager::add_group(const string &container_id, int64_t pid, uint32_t gid, std::string_view name, bool notify)
{
if (!m_import_users)
{
Expand All @@ -423,9 +417,9 @@ scap_groupinfo *sinsp_usergroup_manager::add_group(const string &container_id, i
if (gr)
{
// Update group if it was already there
if (name != nullptr)
if (name.data() != nullptr)
{
strlcpy(gr->name, name, MAX_CREDENTIALS_STR_LEN);
strlcpy(gr->name, std::string(name).c_str(), MAX_CREDENTIALS_STR_LEN);
}
return gr;
}
Expand All @@ -437,13 +431,13 @@ scap_groupinfo *sinsp_usergroup_manager::add_group(const string &container_id, i
return add_container_group(container_id, pid, gid, notify);
}

scap_groupinfo *sinsp_usergroup_manager::add_host_group(uint32_t gid, const char *name, bool notify)
scap_groupinfo *sinsp_usergroup_manager::add_host_group(uint32_t gid, std::string_view name, bool notify)
{
libsinsp_logger()->format(sinsp_logger::SEV_DEBUG,
"adding host group: name: %s", name);

scap_groupinfo *gr = nullptr;
if (name)
if (name.data())
{
gr = groupinfo_map_insert(m_grouplist[""], gid, name);
}
Expand Down
17 changes: 9 additions & 8 deletions userspace/libsinsp/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class sinsp_usergroup_manager

// Note: pid is an unused parameter when container_id is an empty string
// ie: it is only used when adding users/groups from containers.
scap_userinfo *add_user(const std::string &container_id, int64_t pid, uint32_t uid, uint32_t gid, const char *name, const char *home, const char *shell, bool notify = false);
scap_groupinfo *add_group(const std::string &container_id, int64_t pid, uint32_t gid, const char *name, bool notify = false);
scap_userinfo *add_user(const std::string &container_id, int64_t pid, uint32_t uid, uint32_t gid, std::string_view name, std::string_view home, std::string_view shell, bool notify = false);
scap_groupinfo *add_group(const std::string &container_id, int64_t pid, uint32_t gid, std::string_view name, bool notify = false);

bool rm_user(const std::string &container_id, uint32_t uid, bool notify = false);
bool rm_group(const std::string &container_id, uint32_t gid, bool notify = false);
Expand All @@ -135,10 +135,10 @@ class sinsp_usergroup_manager
bool m_import_users;

private:
scap_userinfo *add_host_user(uint32_t uid, uint32_t gid, const char *name, const char *home, const char *shell, bool notify);
scap_userinfo *add_host_user(uint32_t uid, uint32_t gid, std::string_view name, std::string_view home, std::string_view shell, bool notify);
scap_userinfo *add_container_user(const std::string &container_id, int64_t pid, uint32_t uid, bool notify);

scap_groupinfo *add_host_group(uint32_t gid, const char *name, bool notify);
scap_groupinfo *add_host_group(uint32_t gid, std::string_view name, bool notify);
scap_groupinfo *add_container_group(const std::string &container_id, int64_t pid, uint32_t gid, bool notify);

bool user_to_sinsp_event(const scap_userinfo *user, sinsp_evt* evt, const std::string &container_id, uint16_t ev_type);
Expand All @@ -156,13 +156,14 @@ class sinsp_usergroup_manager
userinfo_map &map,
uint32_t uid,
uint32_t gid,
const char *name,
const char *home,
const char *shell);
std::string_view name,
std::string_view home,
std::string_view shell);

scap_groupinfo *groupinfo_map_insert(
groupinfo_map &map,
uint32_t gid,
const char *name);
std::string_view name);

std::unordered_map<std::string, userinfo_map> m_userlist;
std::unordered_map<std::string, groupinfo_map> m_grouplist;
Expand Down

0 comments on commit fc4b79c

Please sign in to comment.