From fc4b79c87ab1bda32c172e51f505079449c10a0f Mon Sep 17 00:00:00 2001 From: Luca Guerra Date: Fri, 12 Jan 2024 16:51:34 +0000 Subject: [PATCH] cleanup(libsinsp): use string_view in user/group functions Signed-off-by: Luca Guerra --- userspace/libsinsp/parsers.cpp | 4 +-- userspace/libsinsp/test/user.ut.cpp | 8 ++--- userspace/libsinsp/threadinfo.cpp | 4 +-- userspace/libsinsp/user.cpp | 46 +++++++++++++---------------- userspace/libsinsp/user.h | 17 ++++++----- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/userspace/libsinsp/parsers.cpp b/userspace/libsinsp/parsers.cpp index f2ac2735be..ff84de049d 100644 --- a/userspace/libsinsp/parsers.cpp +++ b/userspace/libsinsp/parsers.cpp @@ -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); } } diff --git a/userspace/libsinsp/test/user.ut.cpp b/userspace/libsinsp/test/user.ut.cpp index 1a14e7b726..a7421da9e2 100644 --- a/userspace/libsinsp/test/user.ut.cpp +++ b/userspace/libsinsp/test/user.ut.cpp @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/userspace/libsinsp/threadinfo.cpp b/userspace/libsinsp/threadinfo.cpp index d3a06ed0c5..a3c2d8e222 100644 --- a/userspace/libsinsp/threadinfo.cpp +++ b/userspace/libsinsp/threadinfo.cpp @@ -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) { @@ -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) { diff --git a/userspace/libsinsp/user.cpp b/userspace/libsinsp/user.cpp index c337017436..67971c4ed4 100644 --- a/userspace/libsinsp/user.cpp +++ b/userspace/libsinsp/user.cpp @@ -242,22 +242,18 @@ 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 : "", MAX_CREDENTIALS_STR_LEN); - strlcpy(usr.homedir, (home != nullptr) ? home : "", SCAP_MAX_PATH_SIZE); - strlcpy(usr.shell, (shell != nullptr) ? shell : "", SCAP_MAX_PATH_SIZE); + strlcpy(usr.name, (name.data() != nullptr) ? std::string(name).c_str() : "", MAX_CREDENTIALS_STR_LEN); + strlcpy(usr.homedir, (home.data() != nullptr) ? std::string(home).c_str() : "", SCAP_MAX_PATH_SIZE); + strlcpy(usr.shell, (shell.data() != nullptr) ? std::string(shell).c_str() : "", SCAP_MAX_PATH_SIZE); return &usr; } @@ -265,18 +261,16 @@ scap_userinfo *sinsp_usergroup_manager::userinfo_map_insert( 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 : "", MAX_CREDENTIALS_STR_LEN); + strlcpy(grp.name, (name.data() != nullptr) ? std::string(name).c_str() : "", 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) { @@ -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; } @@ -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[""], @@ -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) { @@ -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; } @@ -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); } diff --git a/userspace/libsinsp/user.h b/userspace/libsinsp/user.h index 0c883bb493..fd939affd6 100644 --- a/userspace/libsinsp/user.h +++ b/userspace/libsinsp/user.h @@ -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); @@ -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); @@ -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 m_userlist; std::unordered_map m_grouplist;