Repository navigation
Add pystemd.utils.ns.join_namespaces - #128
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ofpid(likensenter --target pid --all) and returns the names it joined.namespacesis an iterable of names ("net", "mnt", ...;NAMESPACESlists them), all by default.pidare 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.pystemd.futurescan 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
loand 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
formattingcheck fails on every PR right now for an unrelated reason: CI installs the latest ruff, which flags existing files in examples/.