commit:     e1be09eedea2dba8c605a49d9211c868c2ee4dcc
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Wed Oct 28 21:59:17 2020 +0000
Commit:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
CommitDate: Wed Oct 28 23:06:09 2020 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=e1be09ee

catalyst: ...

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

 catalyst/main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 50 insertions(+), 9 deletions(-)

diff --git a/catalyst/main.py b/catalyst/main.py
index 543895c6..8f54ba89 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.process import namespaces
+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())
@@ -352,19 +380,32 @@ def _main(parser, opts):
         # catalyst cannot be run as a normal user due to chroots, mounts, etc
         log.critical('This script requires root privileges to operate')
 
+    cxt = libmount.Context()
+    print("Before")
+    while (fs := cxt.mtab.next_fs()) is not None:
+        print(fs.target)
+
     # Start off by creating unique namespaces to run in.  Would be nice to
     # 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, net=False, pid=False,
+                   user=False, hostname='catalyst'):
+        # everything is setup, so the build is a go
+        try:
+            success = build_target(addlargs)
+            cxt = libmount.Context()
+            print("During")
+            while (fs := cxt.mtab.next_fs()) is not None:
+                print(fs.target)
+        except KeyboardInterrupt:
+            log.critical('Catalyst build aborted due to user interrupt 
(Ctrl-C)')
+
+    cxt = libmount.Context()
+    print("After")
+    while (fs := cxt.mtab.next_fs()) is not None:
+        print(fs.target)
 
-    # 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)

Reply via email to