From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 7DA2C13835B for ; Sat, 19 Dec 2020 19:56:05 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 61B27E093D; Sat, 19 Dec 2020 19:56:04 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 3D7C2E093D for ; Sat, 19 Dec 2020 19:56:04 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 467B0340F42 for ; Sat, 19 Dec 2020 19:56:03 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D86722C5 for ; Sat, 19 Dec 2020 19:56:01 +0000 (UTC) From: "Matt Turner" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Matt Turner" Message-ID: <1604097652.4bcc64dcf2b27ab3d0effed0fa2516e3513a9171.mattst88@gentoo> Subject: [gentoo-commits] proj/catalyst:wip/mattst88 commit in: catalyst/ X-VCS-Repository: proj/catalyst X-VCS-Files: catalyst/context.py catalyst/main.py X-VCS-Directories: catalyst/ X-VCS-Committer: mattst88 X-VCS-Committer-Name: Matt Turner X-VCS-Revision: 4bcc64dcf2b27ab3d0effed0fa2516e3513a9171 X-VCS-Branch: wip/mattst88 Date: Sat, 19 Dec 2020 19:56:01 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: e3b94235-ec05-4369-be85-f761269ccec1 X-Archives-Hash: 05622bb985a3e429edb200f866b8620f Message-ID: <20201219195601.2Hd1Ol3F-pst_b09FOUHvnh3QMzQ3FmgkJlArWbrTks@z> commit: 4bcc64dcf2b27ab3d0effed0fa2516e3513a9171 Author: Matt Turner gentoo org> AuthorDate: Wed Oct 28 21:59:17 2020 +0000 Commit: Matt Turner gentoo org> CommitDate: Fri Oct 30 22:40:52 2020 +0000 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=4bcc64dc 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 gentoo.org> catalyst/context.py | 32 ++++++++++++++++++++++++++++++++ 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)