| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import contextlib |
| 5 |
import os |
| 6 |
from pathlib import Path |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
|
| 10 |
from drgn import NULL, cast |
| 11 |
from drgn.helpers.linux.cgroup import ( |
| 12 |
cgroup_get_from_path, |
| 13 |
cgroup_name, |
| 14 |
cgroup_parent, |
| 15 |
cgroup_path, |
| 16 |
css_for_each_child, |
| 17 |
css_for_each_descendant_pre, |
| 18 |
sock_cgroup_ptr, |
| 19 |
) |
| 20 |
from drgn.helpers.linux.fs import fget |
| 21 |
from drgn.helpers.linux.pid import find_task |
| 22 |
from tests.linux_kernel import ( |
| 23 |
LinuxKernelTestCase, |
| 24 |
create_socket, |
| 25 |
fork_and_stop, |
| 26 |
iter_mounts, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
@contextlib.contextmanager |
| 31 |
def tmp_cgroup(): |
| 32 |
for mnt in iter_mounts(): |
| 33 |
if mnt.fstype == "cgroup2": |
| 34 |
break |
| 35 |
else: |
| 36 |
raise unittest.SkipTest("cgroup2 not mounted") |
| 37 |
|
| 38 |
path = Path(tempfile.mkdtemp(prefix="drgn-tests-", dir=mnt.mount_point)) |
| 39 |
try: |
| 40 |
yield path |
| 41 |
finally: |
| 42 |
path.rmdir() |
| 43 |
|
| 44 |
|
| 45 |
@contextlib.contextmanager |
| 46 |
def tmp_cgroups(): |
| 47 |
with tmp_cgroup() as parent: |
| 48 |
child = parent / "child" |
| 49 |
child.mkdir() |
| 50 |
try: |
| 51 |
yield parent, child |
| 52 |
finally: |
| 53 |
child.rmdir() |
| 54 |
|
| 55 |
|
| 56 |
class TestCgroup(LinuxKernelTestCase): |
| 57 |
@classmethod |
| 58 |
def setUpClass(cls): |
| 59 |
super().setUpClass() |
| 60 |
|
| 61 |
parent_cgroup_dir, child_cgroup_dir = cls.enterClassContext(tmp_cgroups()) |
| 62 |
|
| 63 |
cls.root_cgroup = cls.prog["cgrp_dfl_root"].cgrp.address_of_() |
| 64 |
|
| 65 |
with fork_and_stop() as pid: |
| 66 |
task = find_task(cls.prog, pid) |
| 67 |
|
| 68 |
cls.parent_cgroup_name = os.fsencode(parent_cgroup_dir.name) |
| 69 |
cls.parent_cgroup_path = b"/" + cls.parent_cgroup_name |
| 70 |
|
| 71 |
(parent_cgroup_dir / "cgroup.procs").write_text(str(pid)) |
| 72 |
cls.parent_cgroup = task.cgroups.dfl_cgrp.read_() |
| 73 |
|
| 74 |
cls.child_cgroup_name = os.fsencode(child_cgroup_dir.name) |
| 75 |
cls.child_cgroup_path = ( |
| 76 |
cls.parent_cgroup_path + b"/" + cls.child_cgroup_name |
| 77 |
) |
| 78 |
|
| 79 |
(child_cgroup_dir / "cgroup.procs").write_text(str(pid)) |
| 80 |
cls.child_cgroup = task.cgroups.dfl_cgrp.read_() |
| 81 |
|
| 82 |
def test_cgroup_parent(self): |
| 83 |
self.assertEqual(cgroup_parent(self.child_cgroup), self.parent_cgroup) |
| 84 |
self.assertEqual(cgroup_parent(self.parent_cgroup), self.root_cgroup) |
| 85 |
self.assertEqual( |
| 86 |
cgroup_parent(self.root_cgroup), NULL(self.prog, "struct cgroup *") |
| 87 |
) |
| 88 |
|
| 89 |
def test_cgroup_name(self): |
| 90 |
self.assertEqual(cgroup_name(self.root_cgroup), b"/") |
| 91 |
self.assertEqual(cgroup_name(self.parent_cgroup), self.parent_cgroup_name) |
| 92 |
self.assertEqual(cgroup_name(self.child_cgroup), self.child_cgroup_name) |
| 93 |
|
| 94 |
def test_cgroup_path(self): |
| 95 |
self.assertEqual(cgroup_path(self.root_cgroup), b"/") |
| 96 |
self.assertEqual(cgroup_path(self.parent_cgroup), self.parent_cgroup_path) |
| 97 |
self.assertEqual(cgroup_path(self.child_cgroup), self.child_cgroup_path) |
| 98 |
|
| 99 |
def test_cgroup_get_from_path(self): |
| 100 |
self.assertEqual(cgroup_get_from_path(self.prog, "/"), self.root_cgroup) |
| 101 |
self.assertEqual( |
| 102 |
cgroup_get_from_path(self.prog, self.parent_cgroup_path), self.parent_cgroup |
| 103 |
) |
| 104 |
self.assertEqual( |
| 105 |
cgroup_get_from_path(self.prog, self.child_cgroup_path), self.child_cgroup |
| 106 |
) |
| 107 |
self.assertEqual( |
| 108 |
cgroup_get_from_path(self.prog, self.parent_cgroup_path + b"/foo"), |
| 109 |
NULL(self.prog, "struct cgroup *"), |
| 110 |
) |
| 111 |
|
| 112 |
def test_cgroup_socket(self): |
| 113 |
with create_socket() as sock: |
| 114 |
task = find_task(self.prog, os.getpid()) |
| 115 |
file = fget(task, sock.fileno()) |
| 116 |
sk = cast("struct socket *", file.private_data).sk |
| 117 |
self.assertEqual(sock_cgroup_ptr(sk.sk_cgrp_data), task.cgroups.dfl_cgrp) |
| 118 |
|
| 119 |
@staticmethod |
| 120 |
def _cgroup_iter_paths(fn, cgroup): |
| 121 |
return [cgroup_path(css.cgroup) for css in fn(cgroup.self.address_of_())] |
| 122 |
|
| 123 |
def test_css_for_each_child(self): |
| 124 |
children = self._cgroup_iter_paths(css_for_each_child, self.root_cgroup) |
| 125 |
self.assertIn(self.parent_cgroup_path, children) |
| 126 |
self.assertNotIn(self.child_cgroup_path, children) |
| 127 |
|
| 128 |
self.assertEqual( |
| 129 |
self._cgroup_iter_paths(css_for_each_child, self.parent_cgroup), |
| 130 |
[self.child_cgroup_path], |
| 131 |
) |
| 132 |
|
| 133 |
self.assertEqual( |
| 134 |
self._cgroup_iter_paths(css_for_each_child, self.child_cgroup), [] |
| 135 |
) |
| 136 |
|
| 137 |
def test_css_for_each_descendant_pre(self): |
| 138 |
descendants = self._cgroup_iter_paths( |
| 139 |
css_for_each_descendant_pre, self.root_cgroup |
| 140 |
) |
| 141 |
self.assertEqual(descendants[0], b"/") |
| 142 |
self.assertIn(self.parent_cgroup_path, descendants) |
| 143 |
self.assertIn(self.child_cgroup_path, descendants) |
| 144 |
self.assertLess( |
| 145 |
descendants.index(self.parent_cgroup_path), |
| 146 |
descendants.index(self.child_cgroup_path), |
| 147 |
) |
| 148 |
|
| 149 |
self.assertEqual( |
| 150 |
self._cgroup_iter_paths(css_for_each_descendant_pre, self.parent_cgroup), |
| 151 |
[self.parent_cgroup_path, self.child_cgroup_path], |
| 152 |
) |
| 153 |
|
| 154 |
self.assertEqual( |
| 155 |
self._cgroup_iter_paths(css_for_each_descendant_pre, self.child_cgroup), |
| 156 |
[self.child_cgroup_path], |
| 157 |
) |