diff options
author | Helmut Grohne <helmut@subdivi.de> | 2025-04-29 14:20:22 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2025-04-29 14:20:22 +0200 |
commit | d83171b1fbbb809f7e5ed0daeed4435132817158 (patch) | |
tree | d1d538c43fc1a30654c6fb5d013286cf46554c20 | |
parent | 380383da813b1066dc6768f9e04b1aa0b69571bd (diff) | |
download | python-linuxnamespaces-d83171b1fbbb809f7e5ed0daeed4435132817158.tar.gz |
move the get_cgroup utility function into the library
-rwxr-xr-x | examples/cgroup.py | 15 | ||||
-rw-r--r-- | linuxnamespaces/__init__.py | 11 |
2 files changed, 13 insertions, 13 deletions
diff --git a/examples/cgroup.py b/examples/cgroup.py index 34bf01b..2a423d3 100755 --- a/examples/cgroup.py +++ b/examples/cgroup.py @@ -18,19 +18,8 @@ import linuxnamespaces import linuxnamespaces.systemd -def get_cgroup(pid: int = -1) -> pathlib.PurePath: - """Look up the cgroup that the given pid or the running 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() - ) - - def main() -> None: - mycgroup = get_cgroup() + mycgroup = linuxnamespaces.get_cgroup() if not os.access( pathlib.Path("/sys/fs/cgroup") / mycgroup.relative_to("/"), os.W_OK, @@ -45,7 +34,7 @@ def main() -> None: properties={"Delegate": True}, ), ) - mycgroup = get_cgroup() + mycgroup = linuxnamespaces.get_cgroup() except NotImplementedError: linuxnamespaces.systemd.reexec_as_transient_unit( properties={"Delegate": True} 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: |