tools.linux_cgroup_ns_tools module
Direct Linux cgroup v2 and namespace management.
Read/write cgroup pseudo-files under the unified hierarchy, create/remove cgroup
directories, move processes via cgroup.procs, and run commands under
unshare or nsenter (util-linux). All operations require the
UNSANDBOXED_EXEC privilege.
Paths are always canonicalized and must resolve under /sys/fs/cgroup (no
.. escapes to other filesystem trees).
- async tools.linux_cgroup_ns_tools.cgroup_read_file(path, max_bytes=262144, ctx=None)[source]
Read a cgroup v2 pseudo-file under the unified hierarchy as text.
Handler for the
cgroup_read_filetool: it lets a privileged user inspect cgroup controller state (e.g.memory.current,cgroup.controllers) by reading the pseudo-file atpath. The read is bounded so a large or pathological file cannot blow up the response.Requires the
UNSANDBOXED_EXECprivilege, enforced via_check_priv(). Thepathis confined to/sys/fs/cgroupby_resolve_under_cgroup(), then the bytes are read directly off the filesystem and decoded witherrors="replace", truncating pastmax_bytes(capped at_MAX_READ_BYTES_CAP). Results are wrapped with_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist (seetool_loader.py); no direct in-repo callers.- Parameters:
- Returns:
A JSON success envelope with
path,contentandbytes_read, or a JSON error envelope on a missing context, denied privilege, path-validation failure, orOSError.- Return type:
- async tools.linux_cgroup_ns_tools.cgroup_write_file(path, content, ctx=None)[source]
Write UTF-8 text into a cgroup v2 pseudo-file (a control operation).
Handler for the
cgroup_write_filetool: this is how the bot actually reconfigures a cgroup, since cgroup v2 is driven entirely by writes to interface files (e.g. enabling controllers viacgroup.subtree_controlor setting a limit viamemory.max). The write goes straight to the kernel-backed pseudo-file, so its semantics depend on which file is targeted.Requires the
UNSANDBOXED_EXECprivilege via_check_priv(), and thepathis confined under/sys/fs/cgroupby_resolve_under_cgroup()beforepathlib.Path.write_text()is called. Envelopes come from_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
- Returns:
A JSON success envelope with
pathandbytes_written, or a JSON error envelope on a missing context, denied privilege, path-validation failure, orOSErrorfrom the write.- Return type:
- async tools.linux_cgroup_ns_tools.cgroup_mkdir(path, parents=True, ctx=None)[source]
Create a cgroup directory, optionally with its parents.
Handler for the
cgroup_mkdirtool. Creating a directory under the unified hierarchy is how a new child cgroup is materialized: the kernel auto-populates it with the relevantcgroup.*interface files, after which processes can be moved in and limits applied. Withparentstrue this behaves likemkdir -pand is idempotent (exist_ok=True).Requires the
UNSANDBOXED_EXECprivilege via_check_priv(); thepathis confined under/sys/fs/cgroupby_resolve_under_cgroup()beforepathlib.Path.mkdir(). Results use_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
- Returns:
A JSON success envelope with the created
path, or a JSON error envelope on a missing context, denied privilege, path-validation failure, orOSErrorfrom the mkdir.- Return type:
- async tools.linux_cgroup_ns_tools.cgroup_rmdir(path, ctx=None)[source]
Remove an empty cgroup directory under the unified hierarchy.
Handler for the
cgroup_rmdirtool, the inverse ofcgroup_mkdir(). The kernel only allows an empty cgroup to be removed, so this cleans up a child cgroup once every process has been migrated out of it. As an extra guard it refuses to delete the mount root itself.Requires the
UNSANDBOXED_EXECprivilege via_check_priv(); thepathis confined under/sys/fs/cgroupby_resolve_under_cgroup()and compared against_cgroup_mount_resolved()beforepathlib.Path.rmdir(). Results use_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
- Returns:
A JSON success envelope with the removed
path, or a JSON error envelope on a missing context, denied privilege, path-validation failure, an attempt to remove the mount root, or anOSError(notably when the directory is non-empty).- Return type:
- async tools.linux_cgroup_ns_tools.cgroup_move_pid(cgroup_path, pid, ctx=None)[source]
Move a process into a cgroup by writing its PID to
cgroup.procs.Handler for the
cgroup_move_pidtool. In cgroup v2 a task is assigned to a cgroup by writing its PID into that cgroup’scgroup.procsfile; this is the step that actually subjects a running process to the cgroup’s controllers and limits. The target directory must already exist (created viacgroup_mkdir()).Requires the
UNSANDBOXED_EXECprivilege via_check_priv(). Thecgroup_pathis confined under/sys/fs/cgroupby_resolve_under_cgroup(), checked to be an existing directory, and the PID is written tocgroup.procsinside it. Results use_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
- Returns:
A JSON success envelope with
cgroup_pathandpid, or a JSON error envelope on a missing context, denied privilege, path-validation failure, a non-directory target, or anOSErrorfrom the write.- Return type:
Run a command in fresh Linux namespaces via
unshare(util-linux).Handler for the
linux_unshare_exectool. It assembles and runsunshare [unshare_flags] -- command ..., letting a privileged user execute a process inside newly unshared namespaces (mount, PID, net, user, etc.) for isolation or experimentation. The flag tokens are accepted in a forgiving form and normalized to real options.Requires the
UNSANDBOXED_EXECprivilege via_check_priv(), validates thatcommandis a non-empty list, normalizesunshare_flagsthrough_sanitize_flag_list(), resolves theunsharebinary withshutil.which(), clampstimeoutto_MAX_SUBPROCESS_TIMEOUT, and executes the assembled argv via_run_exec_limited()(no shell). Results use_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
command (
list[str]) – Executable and arguments to run after--; must be a non-empty list of strings.unshare_flags (
list[str] |None) – Optional namespace flags, accepted as bare names or short/long options and normalized before use.timeout (
int) – Wall-clock seconds before the child is killed, clamped to[1, _MAX_SUBPROCESS_TIMEOUT].ctx (
Any) – TheToolContext; required for the privilege check.
- Returns:
A JSON success envelope with
returncode,stdout,stderrand the fullcommand, or a JSON error envelope on a missing context, denied privilege, invalid arguments, or a missingunshareexecutable.- Return type:
- async tools.linux_cgroup_ns_tools.linux_nsenter_exec(target_pid, command, nsenter_flags=None, timeout=120, ctx=None)[source]
Run a command inside another process’s namespaces via
nsenter.Handler for the
linux_nsenter_exectool. It assembles and runsnsenter -t <pid> [nsenter_flags] -- command ...(util-linux), so a privileged user can join an existing task’s namespaces – for example to debug inside a container by entering its mount and network namespaces. This is the counterpart tolinux_unshare_exec(), which instead creates new namespaces.Requires the
UNSANDBOXED_EXECprivilege via_check_priv(), validatestarget_pidis positive andcommandis a non-empty list, normalizesnsenter_flagsthrough_sanitize_flag_list(), resolves thensenterbinary withshutil.which(), clampstimeoutto_MAX_SUBPROCESS_TIMEOUT, and executes the assembled argv via_run_exec_limited()(no shell). Results use_json_ok()/_json_err(). Invoked through the tool dispatcher via thehandlerentry in this module’sTOOLSlist; no direct in-repo callers.- Parameters:
target_pid (
int) – PID of the process whose namespaces to enter; must be a positive integer.command (
list[str]) – Executable and arguments to run after--; must be a non-empty list of strings.nsenter_flags (
list[str] |None) – Optional namespace flags, accepted as bare names or short/long options and normalized before use.timeout (
int) – Wall-clock seconds before the child is killed, clamped to[1, _MAX_SUBPROCESS_TIMEOUT].ctx (
Any) – TheToolContext; required for the privilege check.
- Returns:
A JSON success envelope with
returncode,stdout,stderrand the fullcommand, or a JSON error envelope on a missing context, denied privilege, invalid arguments, or a missingnsenterexecutable.- Return type: