_util
_util
¶
Vendored utility functions for filesystem, templates, logging, naming, and sanitization.
__all__ = ['effective_ssh_key_name', 'ensure_dir', 'ensure_dir_writable', 'log_debug', 'log_warning', 'read_pidfile_safely', 'sanitize_tty', 'systemd_escape', 'systemd_exec_argv', 'systemd_user_unit_dir', 'unlink_pidfile_safely', 'warn_user', 'write_sensitive_file']
module-attribute
¶
systemd_user_unit_dir()
¶
Return the systemd user unit directory, validated against path traversal.
Refuses to run as root (euid == 0) and resolves $XDG_CONFIG_HOME
to ensure the result stays beneath the user's home directory.
Raises SystemExit on validation failure.
Source code in src/terok_sandbox/_util/_fs.py
log_debug(message)
¶
log_warning(message)
¶
warn_user(component, message)
¶
Print a structured warning to stderr and log it to the sandbox's default log.
effective_ssh_key_name(scope, *, ssh_key_name=None, key_type='ed25519')
¶
Return the SSH key filename to use.
Precedence
- Explicit ssh_key_name (from caller config)
- Derived default:
id_<type>_<scope>
Source code in src/terok_sandbox/_util/_naming.py
read_pidfile_safely(pidfile)
¶
Read a PID from pidfile without following symlinks.
Returns the integer PID on success, or None on any failure path
(missing file, symlink, non-regular file, invalid contents).
Source code in src/terok_sandbox/_util/_pidfile.py
unlink_pidfile_safely(pidfile)
¶
Unlink pidfile only if it's a regular file (refusing symlinks).
The lstat check is what makes the difference: a plain
Path.unlink() happily removes a symlink target, which is the
file-clobber vector
(CWE-59) the
safe-handling guidance calls out. Any failure is silently ignored
— this is cleanup, not a critical path.
Source code in src/terok_sandbox/_util/_pidfile.py
systemd_escape(arg)
¶
Escape arg for a systemd unit-file value position (e.g. ExecStart=).
Three systemd-parser hazards a POSIX-style quoter (shlex.join)
handles wrong:
%introduces specifier expansion — escaped as%%.- Whitespace separates
ExecStart=tokens. Systemd treats both space and tab as separators (persystemd.syntax(7)); escape both as\x20/\x09so a path containing either stays one argv element.
Common Python paths (/usr/bin/python3.12) round-trip unchanged.
Atypical paths (pipx-on-macOS framework prefixes, multi-Python
rootfs layouts with % in directory names, custom build trees
with tabs in names) survive instead of silently mis-parsing.
Source code in src/terok_sandbox/_util/_templates.py
systemd_exec_argv(prefix)
¶
Join prefix into a single ExecStart= value, each element escaped.
Use this instead of :func:shlex.join for unit-file ExecStart,
ExecStartPre, etc. — POSIX shell quoting and systemd unit
quoting diverge enough that shlex.join mis-renders paths that
contain % or whitespace.