594167
From dc017e5c51e61ddd96d2a94f35223ac7788c8454 Mon Sep 17 00:00:00 2001
594167
From: Andreas Rammhold <andreas@rammhold.de>
594167
Date: Wed, 18 Aug 2021 19:10:08 +0200
594167
Subject: [PATCH] core: handle lookup paths being symlinks
594167
MIME-Version: 1.0
594167
Content-Type: text/plain; charset=UTF-8
594167
Content-Transfer-Encoding: 8bit
594167
594167
With a recent change paths leaving the statically known lookup paths would be
594167
treated differently then those that remained within those. That was done
594167
(AFAIK) to consistently handle alias names. Unfortunately that means that on
594167
some distributions, especially those where /etc/ consists mostly of symlinks,
594167
would trigger that new detection for every single unit in /etc/systemd/system.
594167
The reason for that is that the units directory itself is already a symlink.
594167
594167
Rebased-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
594167
(cherry picked from commit 66c38cd0536c50769eba6abccf383bbaceb268ca)
594167
594167
Resolves: #2082131
594167
---
594167
 src/basic/unit-file.c | 41 ++++++++++++++++++++++++++++++++++++++++-
594167
 1 file changed, 40 insertions(+), 1 deletion(-)
594167
594167
diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c
594167
index 7c1ae515e1..83c29bb25f 100644
594167
--- a/src/basic/unit-file.c
594167
+++ b/src/basic/unit-file.c
594167
@@ -388,6 +388,7 @@ int unit_file_build_name_map(
594167
 
594167
         _cleanup_hashmap_free_ Hashmap *ids = NULL, *names = NULL;
594167
         _cleanup_set_free_free_ Set *paths = NULL;
594167
+        _cleanup_strv_free_ char **expanded_search_path = NULL;
594167
         uint64_t timestamp_hash;
594167
         int r;
594167
 
594167
@@ -406,6 +407,44 @@ int unit_file_build_name_map(
594167
                         return log_oom();
594167
         }
594167
 
594167
+        /* Go over all our search paths, chase their symlinks and store the result in the
594167
+         * expanded_search_path list.
594167
+         *
594167
+         * This is important for cases where any of the unit directories itself are symlinks into other
594167
+         * directories and would therefore cause all of the unit files to be recognized as linked units.
594167
+         *
594167
+         * This is important for distributions such as NixOS where most paths in /etc/ are symlinks to some
594167
+         * other location on the filesystem (e.g.  into /nix/store/).
594167
+         *
594167
+         * Search paths are ordered by priority (highest first), and we need to maintain this order.
594167
+         * If a resolved path is already in the list, we don't need to include.
594167
+         *
594167
+         * Note that we build a list that contains both the original paths and the resolved symlinks:
594167
+         * we need the latter for the case where the directory is symlinked, as described above, and
594167
+         * the former for the case where some unit file alias is a dangling symlink that points to one
594167
+         * of the "original" directories (and can't be followed).
594167
+         */
594167
+        STRV_FOREACH(dir, lp->search_path) {
594167
+                _cleanup_free_ char *resolved_dir = NULL;
594167
+
594167
+                r = strv_extend(&expanded_search_path, *dir);
594167
+                if (r < 0)
594167
+                        return log_oom();
594167
+
594167
+                r = chase_symlinks(*dir, NULL, 0, &resolved_dir, NULL);
594167
+                if (r < 0) {
594167
+                        if (r != -ENOENT)
594167
+                                log_warning_errno(r, "Failed to resolve symlink %s, ignoring: %m", *dir);
594167
+                        continue;
594167
+                }
594167
+
594167
+                if (strv_contains(expanded_search_path, resolved_dir))
594167
+                        continue;
594167
+
594167
+                if (strv_consume(&expanded_search_path, TAKE_PTR(resolved_dir)) < 0)
594167
+                        return log_oom();
594167
+        }
594167
+
594167
         STRV_FOREACH(dir, lp->search_path) {
594167
                 _cleanup_closedir_ DIR *d = NULL;
594167
 
594167
@@ -504,7 +543,7 @@ int unit_file_build_name_map(
594167
                                 /* We don't explicitly check for alias loops here. unit_ids_map_get() which
594167
                                  * limits the number of hops should be used to access the map. */
594167
 
594167
-                                r = unit_file_resolve_symlink(lp->root_dir, lp->search_path,
594167
+                                r = unit_file_resolve_symlink(lp->root_dir, expanded_search_path,
594167
                                                               *dir, dirfd(d), de->d_name,
594167
                                                               /* resolve_destination_target= */ false,
594167
                                                               &dst);