Skip to content

Add pystemd.utils.ns.join_namespaces - #128

Merged
aleivag merged 1 commit into
systemd:mainfrom
aleivag:utils-ns
Nov 19, 2026
Merged

aleivag merged 1 commit into
systemd:mainfrom
aleivag:utils-ns

Conversation

@aleivag

@aleivag aleivag commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #127 (pystemd.utils as a package): until that merges, this PR also shows its commit; only "Add pystemd.utils.ns.join_namespaces" is new here.

join_namespaces(pid, namespaces=None) moves the calling process into the namespaces of pid (like nsenter --target pid --all) and returns the names it joined. namespaces is an iterable of names ("net", "mnt", ...; NAMESPACES lists them), all by default.

  • Namespaces already shared with pid are skipped (compared by inode under /proc/*/ns/), so asking for "all" works when we share e.g. the user namespace, which setns() would otherwise reject with EINVAL.
  • First tries a single setns() on a pidfd (Python >= 3.12, Linux >= 5.8), which joins everything atomically; otherwise (Python 3.11, older kernels) joins one namespace at a time in nsenter's order, closing each fd.
  • Failures raise OSError instead of being ignored.

pystemd.futures can use it in place of its own setns() loop, which leaks one fd per namespace in every worker; that change is a follow-up, once #126 and this PR land.

Tested on Linux 6.19 / Python 3.14: joining a PrivateNetwork= + ProtectHostnameEx= unit gives ['uts', 'net', 'mnt'] with only lo and the unit's hostname, through both the pidfd path and the forced fallback, with no fds leaked. New unit tests in tests/test_ns.py.

Lint: ruff 0.9.6 (as pinned in .pre-commit-config.yaml), isort and pyrefly pass. The formatting check fails on every PR right now for an unrelated reason: CI installs the latest ruff, which flags existing files in examples/.

@aleivag aleivag changed the title Utils ns Add pystemd.utils.ns.join_namespaces Nov 16, 2026
join_namespaces(pid, namespaces=None) moves the calling process into the
namespaces of `pid` (like `nsenter --target pid --all`) and returns the
names it joined. `namespaces` is an iterable of names ("net", "mnt", ...;
NAMESPACES lists them), all by default.

- Namespaces already shared with `pid` are skipped (compared by inode
  under /proc/*/ns/), so asking for "all" works when we share e.g. the
  user namespace, which setns() would otherwise reject with EINVAL.
- First tries a single setns() on a pidfd (Python >= 3.12, Linux >= 5.8),
  which joins everything atomically; otherwise (Python 3.11, older kernels)
  joins one namespace at a time in nsenter's order, closing each fd.
- Failures raise OSError instead of being ignored.

pystemd.futures can use it in place of its own setns() loop, which leaks
one fd per namespace in every worker; that change is a follow-up, once the
futures fixes land.

Tested on Linux 6.19 / Python 3.14: joining a PrivateNetwork= +
ProtectHostnameEx= unit gives ['uts', 'net', 'mnt'] with only `lo` and the
unit's hostname, through both the pidfd path and the forced fallback, with
no fds leaked.
@aleivag
aleivag merged commit 4b28003 into systemd:main Nov 19, 2026
49 checks passed
@aleivag
aleivag deleted the utils-ns branch November 19, 2026 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant