]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: when loading .wants and .requires, follow the same logic as .d conf dropins
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 5 Feb 2017 01:50:44 +0000 (20:50 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 8 Feb 2017 02:31:22 +0000 (21:31 -0500)
Essentially, instead of sequentially adding deps based on all symlinks
encountered in .wants and .requires dirs for each name and each unit file load
path, iteratate over the load paths and unit names gathering symlinks, then
order them based on priority, and then iterate over the final list, adding
dependencies.

This patch doesn't change the logic too much, except that the order in which
dependencies are applied might be different. It wasn't defined before, so that
not really a change. Adding filtering on the symlinks is left for later
patches.

src/core/load-dropin.c
src/core/load-dropin.h
src/shared/dropin.c
src/shared/dropin.h
src/systemctl/systemctl.c

index fc07151d37ec074fcae174f32a142def81f8469f..0511fb38efc7ef9cb1986aadefdf5aea6931bd9f 100644 (file)
 #include "load-dropin.h"
 #include "load-fragment.h"
 #include "log.h"
+#include "stat-util.h"
 #include "strv.h"
 #include "unit-name.h"
 #include "unit.h"
 
-static int add_dependency_consumer(
-                UnitDependency dependency,
-                const char *entry,
-                const char* filepath,
-                void *arg) {
-        Unit *u = arg;
+static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
+        _cleanup_strv_free_ char **paths = NULL;
+        char **p;
         int r;
 
-        assert(u);
-
-        r = unit_add_dependency_by_name(u, dependency, entry, filepath, true);
+        r = unit_file_find_dropin_paths(NULL,
+                                        u->manager->lookup_paths.search_path,
+                                        u->manager->unit_path_cache,
+                                        dir_suffix,
+                                        NULL,
+                                        u->names,
+                                        &paths);
         if (r < 0)
-                log_error_errno(r, "Cannot add dependency %s to %s, ignoring: %m", entry, u->id);
+                return r;
+
+        STRV_FOREACH(p, paths) {
+                const char *entry;
+
+                entry = basename(*p);
+
+                r = is_symlink(*p);
+                if (r < 0) {
+                        log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
+                                               unit_dependency_to_string(dependency), *p);
+                        continue;
+                }
+                if (r == 0) {
+                        log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
+                                         unit_dependency_to_string(dependency), *p);
+                        continue;
+                }
+
+                /* TODO: add more checks on the symlink name and target here */
+
+                r = unit_add_dependency_by_name(u, dependency, entry, *p, true);
+                if (r < 0)
+                        log_unit_error_errno(u, r, "cannot add %s dependency on %s, ignoring: %m",
+                                             unit_dependency_to_string(dependency), entry);
+        }
 
         return 0;
 }
 
 int unit_load_dropin(Unit *u) {
         _cleanup_strv_free_ char **l = NULL;
-        Iterator i;
-        char *t, **f;
+        char **f;
         int r;
 
         assert(u);
 
-        /* Load dependencies from supplementary drop-in directories */
-
-        SET_FOREACH(t, u->names, i) {
-                char **p;
+        /* Load dependencies from .wants and .requires directories */
+        r = process_deps(u, UNIT_WANTS, ".wants");
+        if (r < 0)
+                return r;
 
-                STRV_FOREACH(p, u->manager->lookup_paths.search_path) {
-                        unit_file_process_dir(NULL, u->manager->unit_path_cache, *p, t,
-                                              ".wants", UNIT_WANTS,
-                                              add_dependency_consumer, u, NULL);
-                        unit_file_process_dir(NULL, u->manager->unit_path_cache, *p, t,
-                                              ".requires", UNIT_REQUIRES,
-                                              add_dependency_consumer, u, NULL);
-                }
-        }
+        r = process_deps(u, UNIT_REQUIRES, ".requires");
+        if (r < 0)
+                return r;
 
+        /* Load .conf dropins */
         r = unit_find_dropin_paths(u, &l);
         if (r <= 0)
                 return 0;
index 319827dfb99803036495e11d97dd252fcc874b0f..5828a223cee7c2e40a0a024f934cafa8448c416b 100644 (file)
 /* Read service data supplementary drop-in directories */
 
 static inline int unit_find_dropin_paths(Unit *u, char ***paths) {
-        return unit_file_find_dropin_paths(NULL,
-                                           u->manager->lookup_paths.search_path,
-                                           u->manager->unit_path_cache,
-                                           u->names,
-                                           paths);
+        return unit_file_find_dropin_conf_paths(NULL,
+                                                u->manager->lookup_paths.search_path,
+                                                u->manager->unit_path_cache,
+                                                u->names,
+                                                paths);
 }
 
 int unit_load_dropin(Unit *u);
index 06cf3de6202d5c4a4ae461a788d29dd2cd2d0b24..3944ccec44f13ffa45793ce095a8c06b955201d4 100644 (file)
@@ -173,7 +173,7 @@ static int iterate_dir(
         return 0;
 }
 
-int unit_file_process_dir(
+static int unit_file_process_dir(
                 const char *original_root,
                 Set *unit_path_cache,
                 const char *unit_path,
@@ -221,6 +221,8 @@ int unit_file_find_dropin_paths(
                 const char *original_root,
                 char **lookup_path,
                 Set *unit_path_cache,
+                const char *dir_suffix,
+                const char *file_suffix,
                 Set *names,
                 char ***paths) {
 
@@ -235,16 +237,17 @@ int unit_file_find_dropin_paths(
                 char **p;
 
                 STRV_FOREACH(p, lookup_path)
-                        unit_file_process_dir(original_root, unit_path_cache, *p, t, ".d",
+                        unit_file_process_dir(original_root, unit_path_cache,
+                                              *p, t, dir_suffix,
                                               _UNIT_DEPENDENCY_INVALID, NULL, NULL, &strv);
         }
 
         if (strv_isempty(strv))
                 return 0;
 
-        r = conf_files_list_strv(&ans, ".conf", NULL, (const char**) strv);
+        r = conf_files_list_strv(&ans, file_suffix, NULL, (const char**) strv);
         if (r < 0)
-                return log_warning_errno(r, "Failed to get list of configuration files: %m");
+                return log_warning_errno(r, "Failed to sort the list of configuration files: %m");
 
         *paths = ans;
         ans = NULL;
index 761b25088601c432b8b3a085282710e9a0df132d..22cd2fa22968f908b05eb3f460054642a90e552e 100644 (file)
@@ -44,20 +44,24 @@ typedef int (*dependency_consumer_t)(UnitDependency dependency,
                                      const char* filepath,
                                      void *arg);
 
-int unit_file_process_dir(
-                const char *original_root,
-                Set * unit_path_cache,
-                const char *unit_path,
-                const char *name,
-                const char *suffix,
-                UnitDependency dependency,
-                dependency_consumer_t consumer,
-                void *arg,
-                char ***strv);
-
 int unit_file_find_dropin_paths(
                 const char *original_root,
                 char **lookup_path,
                 Set *unit_path_cache,
+                const char *dir_suffix,
+                const char *file_suffix,
                 Set *names,
                 char ***paths);
+
+static inline int unit_file_find_dropin_conf_paths(
+                const char *original_root,
+                char **lookup_path,
+                Set *unit_path_cache,
+                Set *names,
+                char ***paths) {
+        return unit_file_find_dropin_paths(original_root,
+                                           lookup_path,
+                                           unit_path_cache,
+                                           ".d", ".conf",
+                                           names, paths);
+}
index 2964b4e6b28b0df05d2ba6d418c8d4b97aad1e2f..5cb07739d43995e328027bb581f1ed2980f57768 100644 (file)
@@ -2601,7 +2601,8 @@ static int unit_find_paths(
                         return log_error_errno(r, "Failed to add unit name: %m");
 
                 if (dropin_paths) {
-                        r = unit_file_find_dropin_paths(arg_root, lp->search_path, NULL, names, &dropins);
+                        r = unit_file_find_dropin_conf_paths(arg_root, lp->search_path,
+                                                             NULL, names, &dropins);
                         if (r < 0)
                                 return r;
                 }