Gentoo Archives: gentoo-commits

From: Matt Turner <mattst88@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:pending/mattst88 commit in: catalyst/
Date: Thu, 29 Oct 2020 15:47:43
Message-Id: 1603985572.a27f635128e7aa0f125aa02df33ab431b991254e.mattst88@gentoo
1 commit: a27f635128e7aa0f125aa02df33ab431b991254e
2 Author: Matt Turner <mattst88 <AT> gentoo <DOT> org>
3 AuthorDate: Wed Oct 28 21:59:17 2020 +0000
4 Commit: Matt Turner <mattst88 <AT> gentoo <DOT> org>
5 CommitDate: Thu Oct 29 15:32:52 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=a27f6351
7
8 catalyst: Add and use namespace context manager
9
10 Wraps snakeoil's simple_unshare; returns to the previous namespaces on
11 context exit. Will be used by the next commit.
12
13 Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>
14
15 catalyst/context.py | 33 +++++++++++++++++++++++++++++++++
16 catalyst/main.py | 17 +++++++----------
17 2 files changed, 40 insertions(+), 10 deletions(-)
18
19 diff --git a/catalyst/context.py b/catalyst/context.py
20 new file mode 100644
21 index 00000000..b53be56e
22 --- /dev/null
23 +++ b/catalyst/context.py
24 @@ -0,0 +1,33 @@
25 +
26 +import contextlib
27 +import os
28 +
29 +from snakeoil.process.namespaces import setns, simple_unshare
30 +
31 +@××××××××××.contextmanager
32 +def namespace(mount=False, uts=False, ipc=False, net=False, pid=False,
33 + user=False, hostname=None):
34 + namespaces = {
35 + (mount, "mnt"): None,
36 + (uts, "uts"): None,
37 + (ipc, "ipc"): None,
38 + (net, "net"): None,
39 + (pid, "pid"): None,
40 + (user, "user"): None,
41 + }
42 + process_id = os.getpid()
43 +
44 + # Save fds of current namespaces
45 + for ns in [ns for ns in namespaces if ns[0]]:
46 + fp = open(f"/proc/{process_id}/ns/{ns[1]}")
47 + namespaces[ns] = fp
48 +
49 + simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user,
50 + hostname=hostname)
51 + try:
52 + yield None
53 + finally:
54 + for ns in [ns for ns in namespaces if ns[0]]:
55 + fp = namespaces[ns]
56 + setns(fp.fileno(), 0)
57 + fp.close()
58
59 diff --git a/catalyst/main.py b/catalyst/main.py
60 index 543895c6..93a4a0d3 100644
61 --- a/catalyst/main.py
62 +++ b/catalyst/main.py
63 @@ -7,14 +7,13 @@ import textwrap
64
65 import toml
66
67 -from snakeoil.process import namespaces
68 -
69 from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
70 CONTENTS_DEFINITIONS)
71 from DeComp.contents import ContentsMap
72
73 from catalyst import log
74 import catalyst.config
75 +from catalyst.context import namespace
76 from catalyst.defaults import (confdefaults, option_messages,
77 DEFAULT_CONFIG_FILE, valid_config_file_values)
78 from catalyst.support import CatalystError
79 @@ -356,15 +355,13 @@ def _main(parser, opts):
80 # use pid & user namespaces, but snakeoil's namespace module has signal
81 # transfer issues (CTRL+C doesn't propagate), and user namespaces need
82 # more work due to Gentoo build process (uses sudo/root/portage).
83 - namespaces.simple_unshare(
84 - mount=True, uts=True, ipc=True, pid=False, net=False, user=False,
85 - hostname='catalyst')
86 + with namespace(mount=True, uts=True, ipc=True, hostname='catalyst'):
87 + # everything is setup, so the build is a go
88 + try:
89 + success = build_target(addlargs)
90 + except KeyboardInterrupt:
91 + log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
92
93 - # everything is setup, so the build is a go
94 - try:
95 - success = build_target(addlargs)
96 - except KeyboardInterrupt:
97 - log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
98 if not success:
99 sys.exit(2)
100 sys.exit(0)