Skip to content

Commit 587405f

Browse files
Restore the cgroup logic, but also include TMPFS_MAGIC as a valid value for using our cgroup v1 lookup
1 parent 565c4e0 commit 587405f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/datadog/platform_util.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ namespace {
297297
#if defined(__linux__) || defined(__unix__)
298298
/// Magic numbers from linux/magic.h:
299299
/// <https://github.com/torvalds/linux/blob/ca91b9500108d4cf083a635c2e11c884d5dd20ea/include/uapi/linux/magic.h#L71>
300+
constexpr uint64_t TMPFS_MAGIC = 0x01021994;
300301
constexpr uint64_t CGROUP_SUPER_MAGIC = 0x27e0eb;
301302
constexpr uint64_t CGROUP2_SUPER_MAGIC = 0x63677270;
302303

@@ -340,7 +341,7 @@ Optional<Cgroup> get_cgroup_version(const std::shared_ptr<tracing::Logger>& logg
340341
logger->log_error([&](auto& stream) {
341342
stream << "statfs /sys/fs/cgroup: f_type = " << buf.f_type;
342343
});
343-
if (buf.f_type == CGROUP_SUPER_MAGIC)
344+
if (buf.f_type == CGROUP_SUPER_MAGIC || buf.f_type == TMPFS_MAGIC)
344345
return Cgroup::v1;
345346
else if (buf.f_type == CGROUP2_SUPER_MAGIC)
346347
return Cgroup::v2;
@@ -392,16 +393,26 @@ Optional<ContainerID> get_id(const std::shared_ptr<tracing::Logger>& logger) {
392393

393394
// Determine the container ID or inode
394395
ContainerID id;
395-
if (auto maybe_id = find_container_id_from_cgroup()) {
396-
id.value = *maybe_id;
397-
id.type = ContainerID::Type::container_id;
398-
} else if (!is_running_in_host_namespace()) {
399-
// NOTE(@dmehala): failed to find the container ID, try getting the cgroup inode.
400-
auto maybe_inode = get_inode("/sys/fs/cgroup");
401-
if (maybe_inode) {
402-
id.type = ContainerID::Type::cgroup_inode;
403-
id.value = std::to_string(*maybe_inode);
396+
switch (*maybe_cgroup) {
397+
case Cgroup::v1: {
398+
if (auto maybe_id = find_container_id_from_cgroup()) {
399+
id.value = *maybe_id;
400+
id.type = ContainerID::Type::container_id;
401+
break;
402+
}
404403
}
404+
// NOTE(@dmehala): failed to find the container ID, try getting the cgroup
405+
// inode.
406+
[[fallthrough]];
407+
case Cgroup::v2: {
408+
if (!is_running_in_host_namespace()) {
409+
auto maybe_inode = get_inode("/sys/fs/cgroup");
410+
if (maybe_inode) {
411+
id.type = ContainerID::Type::cgroup_inode;
412+
id.value = std::to_string(*maybe_inode);
413+
}
414+
}
415+
}; break;
405416
}
406417

407418
return id;

0 commit comments

Comments
 (0)