Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sinsp_cgroup goodies #1861

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions userspace/libscap/linux/scap_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ static bool scap_in_cgroupns(const char* host_root)
int32_t scap_cgroup_interface_init(struct scap_cgroup_interface* cgi, const char* host_root, char* error, bool with_self_cg)
{
char filename[SCAP_MAX_PATH_SIZE];
bool in_cgroupns = false;
char pid_str[40];

cgi->m_use_cache = true;
Expand All @@ -645,15 +644,16 @@ int32_t scap_cgroup_interface_init(struct scap_cgroup_interface* cgi, const char
cgi->m_mount_v2[0] = 0;
cgi->m_self_v1.len = 0;
cgi->m_self_v2[0] = 0;
cgi->m_in_cgroupns = false;

// if we don't need our cgroup name (will just use the mountpoints, with the full cgroup names coming
// from elsewhere), we can simply assume we're not in a cgroup namespace (the result is the same)
if(with_self_cg)
{
in_cgroupns = scap_in_cgroupns(host_root);
cgi->m_in_cgroupns = scap_in_cgroupns(host_root);
}

if(in_cgroupns)
if(cgi->m_in_cgroupns)
{
snprintf(pid_str, sizeof(pid_str), "%d\n", getpid());
}
Expand All @@ -673,7 +673,7 @@ int32_t scap_cgroup_interface_init(struct scap_cgroup_interface* cgi, const char
if(strcmp(de->mnt_type, "cgroup") == 0)
{
scap_get_cgroup_mount_v1(de, &cgi->m_mounts_v1, &cgi->m_subsystems_v1, host_root, error);
if(in_cgroupns)
if(cgi->m_in_cgroupns)
{
scap_get_cgroup_self_v1_cgroupns(de, &cgi->m_self_v1, &cgi->m_subsystems_v1, host_root, pid_str, error);
}
Expand All @@ -682,7 +682,7 @@ int32_t scap_cgroup_interface_init(struct scap_cgroup_interface* cgi, const char
{
scap_get_cgroup_mount_v2(de, cgi->m_mount_v2, host_root);
get_cgroup_subsystems_v2(cgi, &cgi->m_subsystems_v2, cgi->m_mount_v2);
if(in_cgroupns)
if(cgi->m_in_cgroupns)
{
scap_get_cgroup_self_v2_cgroupns(de, cgi->m_self_v2, host_root, pid_str);
}
Expand Down
2 changes: 2 additions & 0 deletions userspace/libscap/linux/scap_cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern "C"
// - the `scap_cgroup_interface` was created `with_self_cg=false`
struct scap_cgroup_set m_self_v1;
char m_self_v2[SCAP_MAX_PATH_SIZE];

bool m_in_cgroupns;
};

int32_t scap_cgroup_interface_init(struct scap_cgroup_interface* cgi, const char* host_root, char* error, bool with_self_cg);
Expand Down
22 changes: 18 additions & 4 deletions userspace/libsinsp/sinsp_cgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ limitations under the License.
#include <libscap/scap.h>
#include <libsinsp/sinsp.h>

sinsp_cgroup::sinsp_cgroup() :
sinsp_cgroup(scap_get_host_root())
sinsp_cgroup::sinsp_cgroup(bool with_self_cg) :
sinsp_cgroup(scap_get_host_root(), with_self_cg)
{
}

sinsp_cgroup::sinsp_cgroup(std::string &&root) :
sinsp_cgroup::sinsp_cgroup(std::string &&root, bool with_self_cg) :
m_root(std::move(root)),
m_scap_cgroup({})
{
char error[SCAP_LASTERR_SIZE];
scap_cgroup_interface_init(&m_scap_cgroup, m_root.c_str(), error, false);
scap_cgroup_interface_init(&m_scap_cgroup, m_root.c_str(), error, with_self_cg);
}

std::shared_ptr<std::string> sinsp_cgroup::lookup_cgroup_dir(const std::string &subsys, int &version)
Expand Down Expand Up @@ -89,3 +89,17 @@ sinsp_cgroup::~sinsp_cgroup()
scap_cgroup_clear_cache(&m_scap_cgroup);
#endif // __linux__
}

bool sinsp_cgroup::in_cgroupns() const
{
return m_scap_cgroup.m_in_cgroupns;
}

std::string sinsp_cgroup::self_v2() const
{
if(!m_scap_cgroup.m_self_v2[0])
{
return {};
}
return m_scap_cgroup.m_self_v2;
}
8 changes: 6 additions & 2 deletions userspace/libsinsp/sinsp_cgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class sinsp_threadinfo;

class sinsp_cgroup {
public:
sinsp_cgroup();
sinsp_cgroup(bool with_self_cg = false);

explicit sinsp_cgroup(std::string &&root);
explicit sinsp_cgroup(std::string &&root, bool with_self_cg = false);

virtual ~sinsp_cgroup();

Expand All @@ -40,6 +40,10 @@ class sinsp_cgroup {

static sinsp_cgroup &instance();

bool in_cgroupns() const;

std::string self_v2() const;

protected:
std::string m_root;
struct scap_cgroup_interface m_scap_cgroup;
Expand Down
Loading