ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
594167
From 56bc8e8eef5fcbfcf72dd1b3caa56b9186e1011d Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Fri, 25 Mar 2022 15:43:27 +0100
594167
Subject: [PATCH] shared/install: when creating symlinks, accept different but
594167
 equivalent symlinks
594167
MIME-Version: 1.0
594167
Content-Type: text/plain; charset=UTF-8
594167
Content-Transfer-Encoding: 8bit
594167
594167
We would only accept "identical" links, but having e.g. a symlink
594167
/usr/lib/systemd/system/foo-alias.service → /usr/lib/systemd/system/foo.service
594167
when we're trying to create /usr/lib/systemd/system/foo-alias.service →
594167
./foo.service is OK. This fixes an issue found in ubuntuautopkg package
594167
installation, where we'd fail when enabling systemd-resolved.service, because
594167
the existing alias was absolute, and (with the recent patches) we were trying
594167
to create a relative one.
594167
594167
A test is added.
594167
(For .wants/.requires symlinks we were already doing OK. A test is also
594167
added, to verify.)
594167
594167
(cherry picked from commit 3fc53351dc8f37355f5a4ee8f922d3e13a5182c2)
594167
594167
Related: #2082131
594167
---
594167
 src/shared/install.c          | 59 ++++++++++++++++++++++++++---------
594167
 test/test-systemctl-enable.sh | 39 +++++++++++++++++++++--
594167
 2 files changed, 81 insertions(+), 17 deletions(-)
594167
594167
diff --git a/src/shared/install.c b/src/shared/install.c
594167
index d6951b805d..22b16ad453 100644
594167
--- a/src/shared/install.c
594167
+++ b/src/shared/install.c
594167
@@ -423,21 +423,54 @@ void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *chang
594167
 }
594167
 
594167
 /**
594167
- * Checks if two paths or symlinks from wd are the same, when root is the root of the filesystem.
594167
- * wc should be the full path in the host file system.
594167
+ * Checks if two symlink targets (starting from src) are equivalent as far as the unit enablement logic is
594167
+ * concerned. If the target is in the unit search path, then anything with the same name is equivalent.
594167
+ * If outside the unit search path, paths must be identical.
594167
  */
594167
-static bool chroot_symlinks_same(const char *root, const char *wd, const char *a, const char *b) {
594167
-        assert(path_is_absolute(wd));
594167
+static int chroot_unit_symlinks_equivalent(
594167
+                const LookupPaths *lp,
594167
+                const char *src,
594167
+                const char *target_a,
594167
+                const char *target_b) {
594167
+
594167
+        assert(lp);
594167
+        assert(src);
594167
+        assert(target_a);
594167
+        assert(target_b);
594167
 
594167
         /* This will give incorrect results if the paths are relative and go outside
594167
          * of the chroot. False negatives are possible. */
594167
 
594167
-        if (!root)
594167
-                root = "/";
594167
+        const char *root = lp->root_dir ?: "/";
594167
+        _cleanup_free_ char *dirname = NULL;
594167
+        int r;
594167
+
594167
+        if (!path_is_absolute(target_a) || !path_is_absolute(target_b)) {
594167
+                r = path_extract_directory(src, &dirname);
594167
+                if (r < 0)
594167
+                        return r;
594167
+        }
594167
 
594167
-        a = strjoina(path_is_absolute(a) ? root : wd, "/", a);
594167
-        b = strjoina(path_is_absolute(b) ? root : wd, "/", b);
594167
-        return path_equal_or_files_same(a, b, 0);
594167
+        _cleanup_free_ char *a = path_join(path_is_absolute(target_a) ? root : dirname, target_a);
594167
+        _cleanup_free_ char *b = path_join(path_is_absolute(target_b) ? root : dirname, target_b);
594167
+        if (!a || !b)
594167
+                return log_oom();
594167
+
594167
+        r = path_equal_or_files_same(a, b, 0);
594167
+        if (r != 0)
594167
+                return r;
594167
+
594167
+        _cleanup_free_ char *a_name = NULL, *b_name = NULL;
594167
+        r = path_extract_filename(a, &a_name);
594167
+        if (r < 0)
594167
+                return r;
594167
+        r = path_extract_filename(b, &b_name);
594167
+        if (r < 0)
594167
+                return r;
594167
+
594167
+        return streq(a_name, b_name) &&
594167
+               path_startswith_strv(a, lp->search_path) &&
594167
+               path_startswith_strv(b, lp->search_path);
594167
 }
594167
 
594167
 static int create_symlink(
594167
@@ -448,7 +481,7 @@ static int create_symlink(
594167
                 UnitFileChange **changes,
594167
                 size_t *n_changes) {
594167
 
594167
-        _cleanup_free_ char *dest = NULL, *dirname = NULL;
594167
+        _cleanup_free_ char *dest = NULL;
594167
         const char *rp;
594167
         int r;
594167
 
594167
@@ -489,11 +522,7 @@ static int create_symlink(
594167
                 return r;
594167
         }
594167
 
594167
-        dirname = dirname_malloc(new_path);
594167
-        if (!dirname)
594167
-                return -ENOMEM;
594167
-
594167
-        if (chroot_symlinks_same(lp->root_dir, dirname, dest, old_path)) {
594167
+        if (chroot_unit_symlinks_equivalent(lp, new_path, dest, old_path)) {
594167
                 log_debug("Symlink %s → %s already exists", new_path, dest);
594167
                 return 1;
594167
         }
594167
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
594167
index 3b30f090a5..0f66af309a 100644
594167
--- a/test/test-systemctl-enable.sh
594167
+++ b/test/test-systemctl-enable.sh
594167
@@ -39,8 +39,29 @@ test -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
 test -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
 
594167
 "$systemctl" --root="$root" disable test1.service
594167
-test ! -e "$root/etc/systemd/system/default.target.wants/test1.service"
594167
-test ! -e "$root/etc/systemd/system/special.target.requires/test1.service"
594167
+test ! -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
+test ! -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
+
594167
+: '------enable when link already exists-----------------------'
594167
+# We don't read the symlink target, so it's OK for the symlink to point
594167
+# to something else. We should just silently accept this.
594167
+
594167
+mkdir -p "$root/etc/systemd/system/default.target.wants"
594167
+mkdir -p "$root/etc/systemd/system/special.target.requires"
594167
+ln -s /usr/lib/systemd/system/test1.service "$root/etc/systemd/system/default.target.wants/test1.service"
594167
+ln -s /usr/lib/systemd/system/test1.service "$root/etc/systemd/system/special.target.requires/test1.service"
594167
+
594167
+"$systemctl" --root="$root" enable test1.service
594167
+test -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
+test -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
+
594167
+"$systemctl" --root="$root" reenable test1.service
594167
+test -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
+test -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
+
594167
+"$systemctl" --root="$root" disable test1.service
594167
+test ! -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
+test ! -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
 
594167
 : '------suffix guessing---------------------------------------'
594167
 "$systemctl" --root="$root" enable test1
594167
@@ -90,6 +111,20 @@ test ! -h "$root/etc/systemd/system/default.target.wants/test1.service"
594167
 test ! -h "$root/etc/systemd/system/special.target.requires/test1.service"
594167
 test ! -h "$root/etc/systemd/system/test1-goodalias.service"
594167
 
594167
+: '-------aliases when link already exists---------------------'
594167
+cat >"$root/etc/systemd/system/test1a.service" <
594167
+[Install]
594167
+Alias=test1a-alias.service
594167
+EOF
594167
+
594167
+ln -s /usr/lib/systemd/system/test1a.service "$root/etc/systemd/system/test1a-alias.service"
594167
+
594167
+"$systemctl" --root="$root" enable test1a.service
594167
+test -h "$root/etc/systemd/system/test1a-alias.service"
594167
+
594167
+"$systemctl" --root="$root" disable test1a.service
594167
+test ! -h "$root/etc/systemd/system/test1a-alias.service"
594167
+
594167
 : '-------also units-------------------------------------------'
594167
 cat >"$root/etc/systemd/system/test2.socket" <
594167
 [Install]