diff options
author | 2020-10-28 17:59:17 -0400 | |
---|---|---|
committer | 2020-10-30 18:40:52 -0400 | |
commit | 4bcc64dcf2b27ab3d0effed0fa2516e3513a9171 (patch) | |
tree | 5abef53cc938479bdae3a170cd7d32c470a81af6 | |
parent | catalyst: Factor out run_sequence() (diff) | |
download | catalyst-4bcc64dcf2b27ab3d0effed0fa2516e3513a9171.tar.gz catalyst-4bcc64dcf2b27ab3d0effed0fa2516e3513a9171.tar.bz2 catalyst-4bcc64dcf2b27ab3d0effed0fa2516e3513a9171.zip |
catalyst: Add and use namespace context manager
Wraps snakeoil's simple_unshare; returns to the previous namespaces on
context exit. Will be used by the next commit.
Signed-off-by: Matt Turner <mattst88@gentoo.org>
-rw-r--r-- | catalyst/context.py | 32 | ||||
-rw-r--r-- | catalyst/main.py | 17 |
2 files changed, 39 insertions, 10 deletions
diff --git a/catalyst/context.py b/catalyst/context.py new file mode 100644 index 00000000..8a58f33d --- /dev/null +++ b/catalyst/context.py @@ -0,0 +1,32 @@ + +import contextlib +import os + +from snakeoil.process.namespaces import setns, simple_unshare + +@contextlib.contextmanager +def namespace(mount=False, uts=False, ipc=False, net=False, pid=False, + user=False, hostname=None): + namespaces = { + (mount, "mnt"): None, + (uts, "uts"): None, + (ipc, "ipc"): None, + (net, "net"): None, + (pid, "pid"): None, + (user, "user"): None, + } + + # Save fds of current namespaces + for ns in [ns for ns in namespaces if ns[0]]: + fp = open(f"/proc/self/ns/{ns[1]}") + namespaces[ns] = fp + + simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user, + hostname=hostname) + try: + yield + finally: + for ns in [ns for ns in namespaces if ns[0]]: + fp = namespaces[ns] + setns(fp.fileno(), 0) + fp.close() diff --git a/catalyst/main.py b/catalyst/main.py index 543895c6..93a4a0d3 100644 --- a/catalyst/main.py +++ b/catalyst/main.py @@ -7,14 +7,13 @@ import textwrap import toml -from snakeoil.process import namespaces - from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS, CONTENTS_DEFINITIONS) from DeComp.contents import ContentsMap from catalyst import log import catalyst.config +from catalyst.context import namespace from catalyst.defaults import (confdefaults, option_messages, DEFAULT_CONFIG_FILE, valid_config_file_values) from catalyst.support import CatalystError @@ -356,15 +355,13 @@ def _main(parser, opts): # use pid & user namespaces, but snakeoil's namespace module has signal # transfer issues (CTRL+C doesn't propagate), and user namespaces need # more work due to Gentoo build process (uses sudo/root/portage). - namespaces.simple_unshare( - mount=True, uts=True, ipc=True, pid=False, net=False, user=False, - hostname='catalyst') + with namespace(mount=True, uts=True, ipc=True, hostname='catalyst'): + # everything is setup, so the build is a go + try: + success = build_target(addlargs) + except KeyboardInterrupt: + log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)') - # everything is setup, so the build is a go - try: - success = build_target(addlargs) - except KeyboardInterrupt: - log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)') if not success: sys.exit(2) sys.exit(0) |