Skip to content

Commit

Permalink
Fix Datadog-Entity-ID detection by skipping a root inode (#6858)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygree committed Mar 29, 2024
1 parent a11ea3c commit e49f1a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public class ContainerInfo {

private static final ContainerInfo INSTANCE;

private static final Long PROC_CGROUP_INIT_INO = 0xEFFFFFFBL;
private static final long PROC_CGROUP_INIT_INO = 0xEFFFFFFBL;
private static final String CGROUPV1_BASE_CONTROLLER = "memory";
private static final String CGROUPV2_BASE_CONTROLLER = "";

private static final String ENTITY_ID;

public String containerId;
Expand Down Expand Up @@ -107,11 +110,10 @@ public void setcGroups(List<CGroupInfo> cGroups) {

/** Checks if the agent is running in the host cgroup namespace. */
static boolean isHostCgroupNamespace() {
Long ino = getIno(HOST_GROUP_NAMESPACE);
// Currently, host namespace inode number is hardcoded, which can be used to detect if it's
// running in host namespace or not (does not work when running in DinD)
// https://github.com/torvalds/linux/blob/5859a2b1991101d6b978f3feb5325dad39421f29/include/linux/proc_ns.h#L41-L49
return PROC_CGROUP_INIT_INO.equals(ino);
return readInode(HOST_GROUP_NAMESPACE) == PROC_CGROUP_INIT_INO;
}

/**
Expand All @@ -121,12 +123,13 @@ static boolean isHostCgroupNamespace() {
static @Nullable String getCgroupInode(Path cgroupMountPath, List<CGroupInfo> cgroups) {
// It first tries to get the cGroupV1 "memory" controller inode, and if that fails, it tries to
// get the cGroupV2 inode.
for (String controller : Arrays.asList("memory", "")) {
for (String controller : Arrays.asList(CGROUPV1_BASE_CONTROLLER, CGROUPV2_BASE_CONTROLLER)) {
for (CGroupInfo cgroup : cgroups) {
if (cgroup.getControllers().contains(controller)) {
Path path = cgroupMountPath.resolve(controller).resolve(cgroup.getPath());
Long inode = getIno(path);
if (inode != null) {
long inode = readInode(path);
// ignore invalid and root inode
if (inode > 2) {
return "in-" + inode;
}
}
Expand All @@ -135,16 +138,14 @@ static boolean isHostCgroupNamespace() {
return null;
}

static @Nullable Long getIno(Path path) {
/** @return 0 - if it couldn't read inode */
static long readInode(Path path) {
try {
Object attr = Files.getAttribute(path, "unix:ino");
if (attr instanceof Long) {
return (Long) attr;
}
return (long) Files.getAttribute(path, "unix:ino");
} catch (Exception e) {
log.debug("Unable to read cgroup inode of {} because of {}", path, e.getClass());
}
return null;
return 0;
}

public static class CGroupInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ class ContainerInfoTest extends DDSpecification {
Path path = f.toPath()

then:
ContainerInfo.getIno(path) == readInode(path)
ContainerInfo.readInode(path) == readInode(path)
}

private Long readInode(Path path) {
private long readInode(Path path) {
ProcessBuilder pb = new ProcessBuilder("ls", "-id", path.toString())
Process ps = pb.start()
BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()))
Expand Down

0 comments on commit e49f1a1

Please sign in to comment.