commit:     0a2be9a29d11499d346d09628fabbe4a3d5b2822
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Wed Oct 28 22:21:50 2020 +0000
Commit:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
CommitDate: Wed Oct 28 22:22:23 2020 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=0a2be9a2

wip

Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>

 catalyst/main.py | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/catalyst/main.py b/catalyst/main.py
index f4d3b7d7..0561aa1d 100644
--- a/catalyst/main.py
+++ b/catalyst/main.py
@@ -1,4 +1,5 @@
 import argparse
+import contextlib
 import datetime
 import hashlib
 import os
@@ -7,7 +8,7 @@ import textwrap
 
 import toml
 
-from snakeoil.contexts import Namespace
+from snakeoil.process.namespaces import setns, simple_unshare
 
 from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
                                 CONTENTS_DEFINITIONS)
@@ -22,6 +23,33 @@ from catalyst.version import get_version
 
 conf_values = confdefaults
 
[email protected]
+def namespace(mount=True, uts=True, ipc=True, 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,
+    }
+    pid = os.getpid()
+
+    # Save fds of current namespaces
+    for ns in [ns for ns in namespaces if ns[0]]:
+        fp = open(f"/proc/{pid}/ns/{ns[1]}")
+        namespaces[ns] = fp
+
+    simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user,
+                   hostname=hostname)
+    try:
+        yield None
+    finally:
+        for ns in [ns for ns in namespaces if ns[0]]:
+            fp = namespaces[ns]
+            setns(fp.fileno(), 0)
+            fp.close()
 
 def version():
     log.info(get_version())
@@ -356,13 +384,12 @@ 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).
-    with Namespace(mount=True, uts=True, ipc=True, net=False, pid=False,
+    with namespace(mount=True, uts=True, ipc=True, net=False, pid=False,
                    user=False, hostname='catalyst'):
         # everything is setup, so the build is a go
         try:
             success = build_target(addlargs)
         except KeyboardInterrupt:
-            success = False
             log.critical('Catalyst build aborted due to user interrupt 
(Ctrl-C)')
 
     if not success:

Reply via email to