84b277
From f73a446c8a1db5c255e6cf1bf6c5d68fe6448f10 Mon Sep 17 00:00:00 2001
84b277
From: Lennart Poettering <lennart@poettering.net>
84b277
Date: Tue, 17 Jun 2014 00:13:48 +0200
84b277
Subject: [PATCH] install: when looking for a unit file for enabling, search
84b277
 for templates only after traversing all search directories
84b277
84b277
Let's always make sure to look in all search directories for the full
84b277
unit names first, before looking for templates for them.
84b277
84b277
(cherry-picked from e50bd775163cd96be1888943a8785a436be710e8)
84b277
84b277
Resolves: #1147524
84b277
---
84b277
 src/shared/install.c | 74 +++++++++++++++++++++++++++-------------------------
84b277
 1 file changed, 38 insertions(+), 36 deletions(-)
84b277
84b277
diff --git a/src/shared/install.c b/src/shared/install.c
84b277
index 2c8f907..dc8842b 100644
84b277
--- a/src/shared/install.c
84b277
+++ b/src/shared/install.c
84b277
@@ -1074,67 +1074,69 @@ static int unit_file_search(
84b277
         assert(paths);
84b277
 
84b277
         if (info->path) {
84b277
-                char *full_path = NULL;
84b277
+                const char *path;
84b277
 
84b277
-                if (!isempty(root_dir))
84b277
-                        full_path = strappenda(root_dir, info->path);
84b277
+                if (isempty(root_dir))
84b277
+                        path = info->path;
84b277
+                else
84b277
+                        path = strappenda(root_dir, info->path);
84b277
 
84b277
-                return unit_file_load(c, info, full_path ?: info->path, allow_symlink);
84b277
+                return unit_file_load(c, info, path, allow_symlink);
84b277
         }
84b277
 
84b277
         assert(info->name);
84b277
 
84b277
         STRV_FOREACH(p, paths->unit_path) {
84b277
-                _cleanup_free_ char *path = NULL, *full_path = NULL;
84b277
+                _cleanup_free_ char *path = NULL;
84b277
 
84b277
-                path = strjoin(*p, "/", info->name, NULL);
84b277
+                if (isempty(root_dir))
84b277
+                        path = strjoin(*p, "/", info->name, NULL);
84b277
+                else
84b277
+                        path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
84b277
                 if (!path)
84b277
                         return -ENOMEM;
84b277
 
84b277
-                if (!isempty(root_dir)) {
84b277
-                        full_path = strappend(root_dir, path);
84b277
-                        if (!full_path)
84b277
-                                return -ENOMEM;
84b277
-                }
84b277
-
84b277
-                r = unit_file_load(c, info, full_path ?: path, allow_symlink);
84b277
+                r = unit_file_load(c, info, path, allow_symlink);
84b277
                 if (r >= 0) {
84b277
                         info->path = path;
84b277
                         path = NULL;
84b277
-                } else if (r == -ENOENT && unit_name_is_instance(info->name)) {
84b277
-                        /* Unit file doesn't exist, however instance enablement was requested.
84b277
-                         * We will check if it is possible to load template unit file. */
84b277
-                        _cleanup_free_ char *template = NULL, *template_dir = NULL;
84b277
+                        return r;
84b277
+                }
84b277
+                if (r != -ENOENT && r != -ELOOP)
84b277
+                        return r;
84b277
+        }
84b277
 
84b277
-                        template = unit_name_template(info->name);
84b277
-                        if (!template)
84b277
-                                return -ENOMEM;
84b277
+        if (unit_name_is_instance(info->name)) {
84b277
 
84b277
-                        /* We will reuse path variable since we don't need it anymore. */
84b277
-                        template_dir = path;
84b277
-                        *(strrchr(template_dir, '/') + 1) = '\0';
84b277
+                /* Unit file doesn't exist, however instance
84b277
+                 * enablement was requested.  We will check if it is
84b277
+                 * possible to load template unit file. */
84b277
 
84b277
-                        path = strappend(template_dir, template);
84b277
+                _cleanup_free_ char *template = NULL, *template_dir = NULL;
84b277
+
84b277
+                template = unit_name_template(info->name);
84b277
+                if (!template)
84b277
+                        return -ENOMEM;
84b277
+
84b277
+                STRV_FOREACH(p, paths->unit_path) {
84b277
+                        _cleanup_free_ char *path = NULL;
84b277
+
84b277
+                        if (isempty(root_dir))
84b277
+                                path = strjoin(*p, "/", template, NULL);
84b277
+                        else
84b277
+                                path = strjoin(root_dir, "/", *p, "/", template, NULL);
84b277
                         if (!path)
84b277
                                 return -ENOMEM;
84b277
 
84b277
-                        if (!isempty(root_dir)) {
84b277
-                                free(full_path);
84b277
-                                full_path = strappend(root_dir, path);
84b277
-                                if (!full_path)
84b277
-                                        return -ENOMEM;
84b277
-                        }
84b277
-
84b277
-                        /* Let's try to load template unit. */
84b277
-                        r = unit_file_load(c, info, full_path ?: path, allow_symlink);
84b277
+                        r = unit_file_load(c, info, path, allow_symlink);
84b277
                         if (r >= 0) {
84b277
                                 info->path = path;
84b277
                                 path = NULL;
84b277
+                                return r;
84b277
                         }
84b277
+                        if (r != -ENOENT && r != -ELOOP)
84b277
+                                return r;
84b277
                 }
84b277
-
84b277
-                if (r != -ENOENT && r != -ELOOP)
84b277
-                        return r;
84b277
         }
84b277
 
84b277
         return -ENOENT;