Skip to content

Commit e49f1a1

Browse files
authored
Fix Datadog-Entity-ID detection by skipping a root inode (#6858)
1 parent a11ea3c commit e49f1a1

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

utils/container-utils/src/main/java/datadog/common/container/ContainerInfo.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class ContainerInfo {
4747

4848
private static final ContainerInfo INSTANCE;
4949

50-
private static final Long PROC_CGROUP_INIT_INO = 0xEFFFFFFBL;
50+
private static final long PROC_CGROUP_INIT_INO = 0xEFFFFFFBL;
51+
private static final String CGROUPV1_BASE_CONTROLLER = "memory";
52+
private static final String CGROUPV2_BASE_CONTROLLER = "";
53+
5154
private static final String ENTITY_ID;
5255

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

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

117119
/**
@@ -121,12 +123,13 @@ static boolean isHostCgroupNamespace() {
121123
static @Nullable String getCgroupInode(Path cgroupMountPath, List<CGroupInfo> cgroups) {
122124
// It first tries to get the cGroupV1 "memory" controller inode, and if that fails, it tries to
123125
// get the cGroupV2 inode.
124-
for (String controller : Arrays.asList("memory", "")) {
126+
for (String controller : Arrays.asList(CGROUPV1_BASE_CONTROLLER, CGROUPV2_BASE_CONTROLLER)) {
125127
for (CGroupInfo cgroup : cgroups) {
126128
if (cgroup.getControllers().contains(controller)) {
127129
Path path = cgroupMountPath.resolve(controller).resolve(cgroup.getPath());
128-
Long inode = getIno(path);
129-
if (inode != null) {
130+
long inode = readInode(path);
131+
// ignore invalid and root inode
132+
if (inode > 2) {
130133
return "in-" + inode;
131134
}
132135
}
@@ -135,16 +138,14 @@ static boolean isHostCgroupNamespace() {
135138
return null;
136139
}
137140

138-
static @Nullable Long getIno(Path path) {
141+
/** @return 0 - if it couldn't read inode */
142+
static long readInode(Path path) {
139143
try {
140-
Object attr = Files.getAttribute(path, "unix:ino");
141-
if (attr instanceof Long) {
142-
return (Long) attr;
143-
}
144+
return (long) Files.getAttribute(path, "unix:ino");
144145
} catch (Exception e) {
145146
log.debug("Unable to read cgroup inode of {} because of {}", path, e.getClass());
146147
}
147-
return null;
148+
return 0;
148149
}
149150

150151
public static class CGroupInfo {

utils/container-utils/src/test/groovy/datadog/common/container/ContainerInfoTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,10 @@ class ContainerInfoTest extends DDSpecification {
231231
Path path = f.toPath()
232232

233233
then:
234-
ContainerInfo.getIno(path) == readInode(path)
234+
ContainerInfo.readInode(path) == readInode(path)
235235
}
236236

237-
private Long readInode(Path path) {
237+
private long readInode(Path path) {
238238
ProcessBuilder pb = new ProcessBuilder("ls", "-id", path.toString())
239239
Process ps = pb.start()
240240
BufferedReader reader = new BufferedReader(new InputStreamReader(ps.getInputStream()))

0 commit comments

Comments
 (0)