]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/load-dropin.c
conf-parser: return mtime in config_parse() and friends
[thirdparty/systemd.git] / src / core / load-dropin.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "conf-parser.h"
4 #include "fs-util.h"
5 #include "load-dropin.h"
6 #include "load-fragment.h"
7 #include "log.h"
8 #include "stat-util.h"
9 #include "string-util.h"
10 #include "strv.h"
11 #include "unit-name.h"
12 #include "unit.h"
13
14 static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
15 _cleanup_strv_free_ char **paths = NULL;
16 char **p;
17 int r;
18
19 r = unit_file_find_dropin_paths(NULL,
20 u->manager->lookup_paths.search_path,
21 u->manager->unit_path_cache,
22 dir_suffix,
23 NULL,
24 u->names,
25 &paths);
26 if (r < 0)
27 return r;
28
29 STRV_FOREACH(p, paths) {
30 _cleanup_free_ char *target = NULL;
31 const char *entry;
32
33 entry = basename(*p);
34
35 if (null_or_empty_path(*p) > 0) {
36 /* an error usually means an invalid symlink, which is not a mask */
37 log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
38 unit_dependency_to_string(dependency), entry, *p);
39 continue;
40 }
41
42 r = is_symlink(*p);
43 if (r < 0) {
44 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
45 unit_dependency_to_string(dependency), *p);
46 continue;
47 }
48 if (r == 0) {
49 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
50 unit_dependency_to_string(dependency), *p);
51 continue;
52 }
53
54 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
55 log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
56 unit_dependency_to_string(dependency), *p);
57 continue;
58 }
59
60 r = readlink_malloc(*p, &target);
61 if (r < 0) {
62 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
63 continue;
64 }
65
66 /* We don't treat this as an error, especially because we didn't check this for a
67 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
68 r = unit_symlink_name_compatible(entry, basename(target), u->instance);
69 if (r < 0) {
70 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m",
71 entry, basename(target));
72 continue;
73 }
74 if (r == 0)
75 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
76 unit_dependency_to_string(dependency), *p, target);
77
78 r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE);
79 if (r < 0)
80 log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
81 unit_dependency_to_string(dependency), entry);
82 }
83
84 return 0;
85 }
86
87 int unit_load_dropin(Unit *u) {
88 _cleanup_strv_free_ char **l = NULL;
89 char **f;
90 int r;
91
92 assert(u);
93
94 /* Load dependencies from .wants and .requires directories */
95 r = process_deps(u, UNIT_WANTS, ".wants");
96 if (r < 0)
97 return r;
98
99 r = process_deps(u, UNIT_REQUIRES, ".requires");
100 if (r < 0)
101 return r;
102
103 /* Load .conf dropins */
104 r = unit_find_dropin_paths(u, &l);
105 if (r <= 0)
106 return 0;
107
108 if (!u->dropin_paths)
109 u->dropin_paths = TAKE_PTR(l);
110 else {
111 r = strv_extend_strv(&u->dropin_paths, l, true);
112 if (r < 0)
113 return log_oom();
114 }
115
116 STRV_FOREACH(f, u->dropin_paths)
117 (void) config_parse(
118 u->id, *f, NULL,
119 UNIT_VTABLE(u)->sections,
120 config_item_perf_lookup, load_fragment_gperf_lookup,
121 0,
122 u,
123 &u->dropin_mtime);
124
125 return 0;
126 }