]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysext: Run unmerge in a subprocess
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 12 Sep 2024 18:28:01 +0000 (20:28 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 16 Oct 2024 13:18:24 +0000 (14:18 +0100)
Preparation for later commit where we need a private mount namespace
for unmerge.

src/sysext/sysext.c

index f6b38d66152f43902fc72162c60472d00f2f1316..94669dbd5282bd08863a5e687d3114fcc0924e76 100644 (file)
@@ -354,38 +354,60 @@ static int unmerge_hierarchy(
         return 0;
 }
 
+static int unmerge_subprocess(
+                ImageClass image_class,
+                char **hierarchies) {
+
+        int r, ret = 0;
+
+        STRV_FOREACH(h, hierarchies) {
+                _cleanup_free_ char *resolved = NULL;
+
+                r = chase(*h, arg_root, CHASE_PREFIX_ROOT, &resolved, NULL);
+                if (r == -ENOENT) {
+                        log_debug_errno(r, "Hierarchy '%s%s' does not exist, ignoring.", strempty(arg_root), *h);
+                        continue;
+                }
+                if (r < 0) {
+                        RET_GATHER(ret, log_error_errno(r, "Failed to resolve path to hierarchy '%s%s': %m", strempty(arg_root), *h));
+                        continue;
+                }
+
+                r = unmerge_hierarchy(image_class, resolved);
+                if (r < 0) {
+                        RET_GATHER(ret, r);
+                        continue;
+                }
+        }
+
+        return ret;
+}
+
 static int unmerge(
                 ImageClass image_class,
                 char **hierarchies,
                 bool no_reload) {
 
-        int r, ret = 0;
         bool need_to_reload;
+        int r;
 
         r = need_reload(image_class, hierarchies, no_reload);
         if (r < 0)
                 return r;
         need_to_reload = r > 0;
 
-        STRV_FOREACH(p, hierarchies) {
-                _cleanup_free_ char *resolved = NULL;
+        r = safe_fork("(sd-unmerge)", FORK_WAIT|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_NEW_MOUNTNS, /* ret_pid= */ NULL);
+        if (r < 0)
+                return r;
+        if (r == 0) {
+                /* Child with its own mount namespace */
 
-                r = chase(*p, arg_root, CHASE_PREFIX_ROOT, &resolved, NULL);
-                if (r == -ENOENT) {
-                        log_debug_errno(r, "Hierarchy '%s%s' does not exist, ignoring.", strempty(arg_root), *p);
-                        continue;
-                }
-                if (r < 0) {
-                        log_error_errno(r, "Failed to resolve path to hierarchy '%s%s': %m", strempty(arg_root), *p);
-                        if (ret == 0)
-                                ret = r;
+                r = unmerge_subprocess(image_class, hierarchies);
 
-                        continue;
-                }
+                /* Our namespace ceases to exist here, also implicitly detaching all temporary mounts we
+                 * created below /run. Nice! */
 
-                r = unmerge_hierarchy(image_class, resolved);
-                if (r < 0 && ret == 0)
-                        ret = r;
+                _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
         }
 
         if (need_to_reload) {
@@ -394,7 +416,7 @@ static int unmerge(
                         return r;
         }
 
-        return ret;
+        return 0;
 }
 
 static int verb_unmerge(int argc, char **argv, void *userdata) {