chiark / gitweb /
install: "systemctl enable" should be a nop for template units lacking a DefaultInsta...
[elogind.git] / src / shared / install.c
index 1e7863acbf45efad707fe80b8abfd05524ef9007..39f5dd2d2d999b72b99697c90a9710b1082f7db9 100644 (file)
@@ -47,6 +47,37 @@ typedef struct {
 
 #define _cleanup_install_context_done_ _cleanup_(install_context_done)
 
+static int in_search_path(const char *path, char **search, const char *root_dir) {
+        _cleanup_free_ char *parent = NULL;
+        char **i;
+        int r;
+
+        assert(path);
+
+        r = path_get_parent(path, &parent);
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH(i, search) {
+                _cleanup_free_ char *buf = NULL;
+                const char *p;
+
+                if (root_dir) {
+                        buf = strjoin(root_dir, "/", *i, NULL);
+                        if (!buf)
+                                return -ENOMEM;
+
+                        p = buf;
+                } else
+                        p = *i;
+
+                if (path_equal(parent, p))
+                        return 1;
+        }
+
+        return 0;
+}
+
 static int lookup_paths_init_from_scope(LookupPaths *paths,
                                         UnitFileScope scope,
                                         const char *root_dir) {
@@ -266,8 +297,22 @@ static int remove_marked_symlinks_fd(
 
                         if (unit_name_is_instance(de->d_name) &&
                             instance_whitelist &&
-                            !strv_contains(instance_whitelist, de->d_name))
-                                continue;
+                            !strv_contains(instance_whitelist, de->d_name)) {
+
+                                _cleanup_free_ char *w;
+
+                                /* OK, the file is not listed directly
+                                 * in the whitelist, so let's check if
+                                 * the template of it might be
+                                 * listed. */
+
+                                w = unit_name_template(de->d_name);
+                                if (!w)
+                                        return -ENOMEM;
+
+                                if (!strv_contains(instance_whitelist, w))
+                                        continue;
+                        }
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p)
@@ -290,18 +335,14 @@ static int remove_marked_symlinks_fd(
                         if (!found)
                                 continue;
 
-
                         if (unlink(p) < 0 && errno != ENOENT) {
-
                                 if (r == 0)
                                         r = -errno;
                                 continue;
                         }
 
-                        rmdir_parents(p, config_path);
-
                         path_kill_slashes(p);
-
+                        rmdir_parents(p, config_path);
                         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
 
                         if (!set_get(remove_symlinks_to, p)) {
@@ -736,7 +777,7 @@ int unit_file_link(
                         continue;
                 }
 
-                q = in_search_path(*i, paths.unit_path);
+                q = in_search_path(*i, paths.unit_path, root_dir);
                 if (q < 0)
                         return q;
 
@@ -1261,7 +1302,14 @@ static int install_info_symlink_wants(
         assert(i);
         assert(config_path);
 
-        if (unit_name_is_template(i->name) && i->default_instance) {
+        if (unit_name_is_template(i->name)) {
+
+                /* Don't install any symlink if there's no default
+                 * instance configured */
+
+                if (!i->default_instance)
+                        return 0;
+
                 buf = unit_name_replace_instance(i->name, i->default_instance);
                 if (!buf)
                         return -ENOMEM;
@@ -1298,6 +1346,7 @@ static int install_info_symlink_link(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1310,7 +1359,7 @@ static int install_info_symlink_link(
         assert(config_path);
         assert(i->path);
 
-        r = in_search_path(i->path, paths->unit_path);
+        r = in_search_path(i->path, paths->unit_path, root_dir);
         if (r != 0)
                 return r;
 
@@ -1325,6 +1374,7 @@ static int install_info_apply(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1345,7 +1395,7 @@ static int install_info_apply(
         if (r == 0)
                 r = q;
 
-        q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes);
+        q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
         if (r == 0)
                 r = q;
 
@@ -1385,7 +1435,7 @@ static int install_context_apply(
                 } else if (r >= 0)
                         r += q;
 
-                q = install_info_apply(i, paths, config_path, force, changes, n_changes);
+                q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
                 if (r >= 0 && q < 0)
                         r = q;
         }