summaryrefslogtreecommitdiff
path: root/linuxnamespaces
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2025-04-29 14:20:22 +0200
committerHelmut Grohne <helmut@subdivi.de>2025-04-29 14:20:22 +0200
commitd83171b1fbbb809f7e5ed0daeed4435132817158 (patch)
treed1d538c43fc1a30654c6fb5d013286cf46554c20 /linuxnamespaces
parent380383da813b1066dc6768f9e04b1aa0b69571bd (diff)
downloadpython-linuxnamespaces-d83171b1fbbb809f7e5ed0daeed4435132817158.tar.gz
move the get_cgroup utility function into the library
Diffstat (limited to 'linuxnamespaces')
-rw-r--r--linuxnamespaces/__init__.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/linuxnamespaces/__init__.py b/linuxnamespaces/__init__.py
index a2c7985..cd30498 100644
--- a/linuxnamespaces/__init__.py
+++ b/linuxnamespaces/__init__.py
@@ -367,6 +367,17 @@ def bind_mount(
mount(srcloc, tgtloc, None, mflags)
+def get_cgroup(pid: int = -1) -> pathlib.PurePath:
+ """Look up the cgroup that the given pid or the current process belongs
+ to.
+ """
+ return pathlib.PurePath(
+ pathlib.Path(
+ f"/proc/{pid}/cgroup" if pid > 0 else "/proc/self/cgroup"
+ ).read_text().split(":", 2)[2].strip()
+ )
+
+
_P = typing.ParamSpec("_P")
class _ExceptionExitCallback: