| 1 |
"""Linux control group constants, classes and utilities.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import codecs |
| 6 |
import dataclasses |
| 7 |
import pathlib |
| 8 |
import re |
| 9 |
|
| 10 |
|
| 11 |
class CGroupPath: |
| 12 |
"""Linux cgroup path constants.""" |
| 13 |
|
| 14 |
ROOT = '/sys/fs/cgroup' |
| 15 |
SYSTEMD = '/sys/fs/cgroup/systemd' |
| 16 |
SYSTEMD_RELEASE_AGENT = '/sys/fs/cgroup/systemd/release_agent' |
| 17 |
|
| 18 |
|
| 19 |
class MountType: |
| 20 |
"""Linux filesystem mount type constants.""" |
| 21 |
|
| 22 |
TMPFS = 'tmpfs' |
| 23 |
CGROUP_V1 = 'cgroup' |
| 24 |
CGROUP_V2 = 'cgroup2' |
| 25 |
|
| 26 |
|
| 27 |
@dataclasses.dataclass(frozen=True) |
| 28 |
class CGroupEntry: |
| 29 |
"""A single cgroup entry parsed from '/proc/{pid}/cgroup' in the proc filesystem.""" |
| 30 |
|
| 31 |
id: int |
| 32 |
subsystem: str |
| 33 |
path: pathlib.PurePosixPath |
| 34 |
|
| 35 |
@property |
| 36 |
def root_path(self) -> pathlib.PurePosixPath: |
| 37 |
"""The root path for this cgroup subsystem.""" |
| 38 |
return pathlib.PurePosixPath(CGroupPath.ROOT, self.subsystem) |
| 39 |
|
| 40 |
@property |
| 41 |
def full_path(self) -> pathlib.PurePosixPath: |
| 42 |
"""The full path for this cgroup subsystem.""" |
| 43 |
return pathlib.PurePosixPath(self.root_path, str(self.path).lstrip('/')) |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def parse(cls, value: str) -> CGroupEntry: |
| 47 |
"""Parse the given cgroup line from the proc filesystem and return a cgroup entry.""" |
| 48 |
cid, subsystem, path = value.split(':', maxsplit=2) |
| 49 |
|
| 50 |
return cls( |
| 51 |
id=int(cid), |
| 52 |
subsystem=subsystem.removeprefix('name='), |
| 53 |
path=pathlib.PurePosixPath(path), |
| 54 |
) |
| 55 |
|
| 56 |
@classmethod |
| 57 |
def loads(cls, value: str) -> tuple[CGroupEntry, ...]: |
| 58 |
"""Parse the given output from the proc filesystem and return a tuple of cgroup entries.""" |
| 59 |
return tuple(cls.parse(line) for line in value.splitlines()) |
| 60 |
|
| 61 |
|
| 62 |
@dataclasses.dataclass(frozen=True) |
| 63 |
class MountEntry: |
| 64 |
"""A single mount info entry parsed from '/proc/{pid}/mountinfo' in the proc filesystem.""" |
| 65 |
|
| 66 |
mount_id: int |
| 67 |
parent_id: int |
| 68 |
device_major: int |
| 69 |
device_minor: int |
| 70 |
root: pathlib.PurePosixPath |
| 71 |
path: pathlib.PurePosixPath |
| 72 |
options: tuple[str, ...] |
| 73 |
fields: tuple[str, ...] |
| 74 |
type: str |
| 75 |
source: pathlib.PurePosixPath |
| 76 |
super_options: tuple[str, ...] |
| 77 |
|
| 78 |
@classmethod |
| 79 |
def parse(cls, value: str) -> MountEntry: |
| 80 |
"""Parse the given mount info line from the proc filesystem and return a mount entry.""" |
| 81 |
# See: https://man7.org/linux/man-pages/man5/proc.5.html |
| 82 |
# See: https://github.com/torvalds/linux/blob/aea23e7c464bfdec04b52cf61edb62030e9e0d0a/fs/proc_namespace.c#L135 |
| 83 |
mount_id, parent_id, device_major_minor, root, path, options, *remainder = value.split(' ') |
| 84 |
fields = remainder[:-4] |
| 85 |
separator, mtype, source, super_options = remainder[-4:] |
| 86 |
|
| 87 |
assert separator == '-' |
| 88 |
|
| 89 |
device_major, device_minor = device_major_minor.split(':') |
| 90 |
|
| 91 |
return cls( |
| 92 |
mount_id=int(mount_id), |
| 93 |
parent_id=int(parent_id), |
| 94 |
device_major=int(device_major), |
| 95 |
device_minor=int(device_minor), |
| 96 |
root=_decode_path(root), |
| 97 |
path=_decode_path(path), |
| 98 |
options=tuple(options.split(',')), |
| 99 |
fields=tuple(fields), |
| 100 |
type=mtype, |
| 101 |
source=_decode_path(source), |
| 102 |
super_options=tuple(super_options.split(',')), |
| 103 |
) |
| 104 |
|
| 105 |
@classmethod |
| 106 |
def loads(cls, value: str) -> tuple[MountEntry, ...]: |
| 107 |
"""Parse the given output from the proc filesystem and return a tuple of mount info entries.""" |
| 108 |
return tuple(cls.parse(line) for line in value.splitlines()) |
| 109 |
|
| 110 |
|
| 111 |
def _decode_path(value: str) -> pathlib.PurePosixPath: |
| 112 |
"""Decode and return a path which may contain octal escape sequences.""" |
| 113 |
# See: https://github.com/torvalds/linux/blob/aea23e7c464bfdec04b52cf61edb62030e9e0d0a/fs/proc_namespace.c#L150 |
| 114 |
path = re.sub(r'(\\[0-7]{3})', lambda m: codecs.decode(m.group(0).encode('ascii'), 'unicode_escape'), value) |
| 115 |
return pathlib.PurePosixPath(path) |