naccyde / rpms / systemd

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