ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
8d419f
From f88f7c68264f9cfef78f4a4e2f68e45de8f1f055 Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Tue, 15 Mar 2022 09:44:39 +0100
8d419f
Subject: [PATCH] shared/install: fix reenable on linked unit files
8d419f
8d419f
(cherry picked from commit 29a7c59abbe594422f1ed7602263420745339a3e)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 src/shared/install.c          | 73 ++++++++++++++++++++++++++++++-----
8d419f
 src/shared/install.h          |  2 +-
8d419f
 test/test-systemctl-enable.sh |  8 ++--
8d419f
 3 files changed, 68 insertions(+), 15 deletions(-)
8d419f
8d419f
diff --git a/src/shared/install.c b/src/shared/install.c
8d419f
index 1018e4fbf3..bf11e5bdce 100644
8d419f
--- a/src/shared/install.c
8d419f
+++ b/src/shared/install.c
8d419f
@@ -2707,17 +2707,74 @@ int unit_file_disable(
8d419f
         return do_unit_file_disable(&lp, scope, flags, config_path, files, changes, n_changes);
8d419f
 }
8d419f
 
8d419f
+static int normalize_linked_files(
8d419f
+                UnitFileScope scope,
8d419f
+                const LookupPaths *lp,
8d419f
+                char **names_or_paths,
8d419f
+                char ***ret_names,
8d419f
+                char ***ret_files) {
8d419f
+
8d419f
+        /* This is similar to normalize_filenames()/normalize_names() in src/systemctl/,
8d419f
+         * but operates on real unit names. For each argument we we look up the actual path
8d419f
+         * where the unit is found. This way linked units can be reenabled successfully. */
8d419f
+
8d419f
+        _cleanup_free_ char **files = NULL, **names = NULL;
8d419f
+        int r;
8d419f
+
8d419f
+        STRV_FOREACH(a, names_or_paths) {
8d419f
+                _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
8d419f
+                UnitFileInstallInfo *i = NULL;
8d419f
+                _cleanup_free_ char *n = NULL;
8d419f
+
8d419f
+                r = path_extract_filename(*a, &n);
8d419f
+                if (r < 0)
8d419f
+                        return r;
8d419f
+                if (r == O_DIRECTORY)
8d419f
+                        return log_debug_errno(SYNTHETIC_ERRNO(EISDIR),
8d419f
+                                               "Unexpected path to a directory \"%s\", refusing.", *a);
8d419f
+
8d419f
+                if (!is_path(*a)) {
8d419f
+                        r = install_info_discover(&ctx, lp, n, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS, &i, NULL, NULL);
8d419f
+                        if (r < 0)
8d419f
+                                log_debug_errno(r, "Failed to discover unit \"%s\", operating on name: %m", n);
8d419f
+                }
8d419f
+
8d419f
+                r = strv_consume(&names, TAKE_PTR(n));
8d419f
+                if (r < 0)
8d419f
+                        return r;
8d419f
+
8d419f
+                const char *p = NULL;
8d419f
+                if (i && i->path)
8d419f
+                        /* Use startswith here, because we know that paths are normalized, and
8d419f
+                         * path_startswith() would give us a relative path, but we need an absolute path
8d419f
+                         * relative to i->root.
8d419f
+                         *
8d419f
+                         * In other words: /var/tmp/instroot.1234/etc/systemd/system/frobnicator.service
8d419f
+                         * is replaced by /etc/systemd/system/frobnicator.service, which is "absolute"
8d419f
+                         * in a sense, but only makes sense "relative" to /var/tmp/instroot.1234/.
8d419f
+                         */
8d419f
+                        p = startswith(i->path, i->root);
8d419f
+
8d419f
+                r = strv_extend(&files, p ?: *a);
8d419f
+                if (r < 0)
8d419f
+                        return r;
8d419f
+        }
8d419f
+
8d419f
+        *ret_names = TAKE_PTR(names);
8d419f
+        *ret_files = TAKE_PTR(files);
8d419f
+        return 0;
8d419f
+}
8d419f
+
8d419f
 int unit_file_reenable(
8d419f
                 UnitFileScope scope,
8d419f
                 UnitFileFlags flags,
8d419f
                 const char *root_dir,
8d419f
-                char **files,
8d419f
+                char **names_or_paths,
8d419f
                 UnitFileChange **changes,
8d419f
                 size_t *n_changes) {
8d419f
 
8d419f
         _cleanup_(lookup_paths_free) LookupPaths lp = {};
8d419f
-        size_t l, i;
8d419f
-        char **names;
8d419f
+        _cleanup_strv_free_ char **names = NULL, **files = NULL;
8d419f
         int r;
8d419f
 
8d419f
         assert(scope >= 0);
8d419f
@@ -2731,13 +2788,11 @@ int unit_file_reenable(
8d419f
         if (!config_path)
8d419f
                 return -ENXIO;
8d419f
 
8d419f
-        /* First, we invoke the disable command with only the basename... */
8d419f
-        l = strv_length(files);
8d419f
-        names = newa(char*, l+1);
8d419f
-        for (i = 0; i < l; i++)
8d419f
-                names[i] = basename(files[i]);
8d419f
-        names[i] = NULL;
8d419f
+        r = normalize_linked_files(scope, &lp, names_or_paths, &names, &files);
8d419f
+        if (r < 0)
8d419f
+                return r;
8d419f
 
8d419f
+        /* First, we invoke the disable command with only the basename... */
8d419f
         r = do_unit_file_disable(&lp, scope, flags, config_path, names, changes, n_changes);
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
diff --git a/src/shared/install.h b/src/shared/install.h
8d419f
index d21e2aaa45..dba6987406 100644
8d419f
--- a/src/shared/install.h
8d419f
+++ b/src/shared/install.h
8d419f
@@ -111,7 +111,7 @@ int unit_file_reenable(
8d419f
                 UnitFileScope scope,
8d419f
                 UnitFileFlags flags,
8d419f
                 const char *root_dir,
8d419f
-                char **files,
8d419f
+                char **names_or_paths,
8d419f
                 UnitFileChange **changes,
8d419f
                 size_t *n_changes);
8d419f
 int unit_file_preset(
8d419f
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
8d419f
index c1fb9626ab..0ed08a9da3 100644
8d419f
--- a/test/test-systemctl-enable.sh
8d419f
+++ b/test/test-systemctl-enable.sh
8d419f
@@ -206,11 +206,9 @@ test ! -h "$root/etc/systemd/system/paths.target.wants/link1.path"
8d419f
 islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
 islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
8d419f
 
8d419f
-# FIXME
8d419f
-# "$systemctl" --root="$root" reenable 'link1.path'
8d419f
-# islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
-# islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
8d419f
-
8d419f
+"$systemctl" --root="$root" reenable 'link1.path'
8d419f
+islink "$root/etc/systemd/system/link1.path" "/link1.path"
8d419f
+islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
8d419f
 
8d419f
 : -------manual link------------------------------------------
8d419f
 cat >"$root/link3.suffix" <