]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
cf0fbc49 | 3 | #include "conf-parser.h" |
317cd27a | 4 | #include "fs-util.h" |
5cb5a6ff | 5 | #include "load-dropin.h" |
cf0fbc49 | 6 | #include "load-fragment.h" |
b952f2e1 | 7 | #include "log.h" |
95778782 | 8 | #include "stat-util.h" |
317cd27a | 9 | #include "string-util.h" |
036643a2 | 10 | #include "strv.h" |
9e2f7c11 | 11 | #include "unit-name.h" |
cf0fbc49 | 12 | #include "unit.h" |
8afbb8e1 | 13 | |
45f4238a | 14 | static int unit_name_compatible(const char *a, const char *b) { |
77cc47d8 | 15 | _cleanup_free_ char *template = NULL; |
317cd27a ZJS |
16 | int r; |
17 | ||
77cc47d8 | 18 | /* The straightforward case: the symlink name matches the target */ |
317cd27a | 19 | if (streq(a, b)) |
45f4238a | 20 | return 1; |
317cd27a | 21 | |
77cc47d8 | 22 | r = unit_name_template(a, &template); |
e450032f | 23 | if (r == -EINVAL) |
77cc47d8 | 24 | return 0; /* Not a template */ |
e450032f | 25 | if (r < 0) |
77cc47d8 | 26 | return r; /* OOM, or some other failure. Just skip the warning. */ |
317cd27a | 27 | |
77cc47d8 LP |
28 | /* An instance name points to a target that is just the template name */ |
29 | return streq(template, b); | |
317cd27a ZJS |
30 | } |
31 | ||
95778782 ZJS |
32 | static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) { |
33 | _cleanup_strv_free_ char **paths = NULL; | |
34 | char **p; | |
9e2f7c11 LP |
35 | int r; |
36 | ||
95778782 ZJS |
37 | r = unit_file_find_dropin_paths(NULL, |
38 | u->manager->lookup_paths.search_path, | |
39 | u->manager->unit_path_cache, | |
40 | dir_suffix, | |
41 | NULL, | |
42 | u->names, | |
43 | &paths); | |
1a7f1b38 | 44 | if (r < 0) |
95778782 ZJS |
45 | return r; |
46 | ||
47 | STRV_FOREACH(p, paths) { | |
317cd27a | 48 | _cleanup_free_ char *target = NULL; |
b119facd | 49 | const char *entry; |
95778782 ZJS |
50 | |
51 | entry = basename(*p); | |
52 | ||
5be5d39b ZJS |
53 | if (null_or_empty_path(*p) > 0) { |
54 | /* an error usually means an invalid symlink, which is not a mask */ | |
55 | log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.", | |
56 | unit_dependency_to_string(dependency), entry, *p); | |
57 | continue; | |
58 | } | |
59 | ||
95778782 ZJS |
60 | r = is_symlink(*p); |
61 | if (r < 0) { | |
62 | log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m", | |
63 | unit_dependency_to_string(dependency), *p); | |
64 | continue; | |
65 | } | |
66 | if (r == 0) { | |
67 | log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.", | |
68 | unit_dependency_to_string(dependency), *p); | |
69 | continue; | |
70 | } | |
71 | ||
317cd27a ZJS |
72 | if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) { |
73 | log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.", | |
74 | unit_dependency_to_string(dependency), *p); | |
75 | continue; | |
76 | } | |
77 | ||
78 | r = readlink_malloc(*p, &target); | |
79 | if (r < 0) { | |
80 | log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p); | |
81 | continue; | |
82 | } | |
83 | ||
84 | /* We don't treat this as an error, especially because we didn't check this for a | |
85 | * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */ | |
45f4238a LP |
86 | r = unit_name_compatible(entry, basename(target)); |
87 | if (r < 0) { | |
88 | log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m", entry, basename(target)); | |
89 | continue; | |
90 | } | |
91 | if (r == 0) | |
317cd27a ZJS |
92 | log_unit_warning(u, "%s dependency dropin %s target %s has different name", |
93 | unit_dependency_to_string(dependency), *p, target); | |
95778782 | 94 | |
35d8c19a | 95 | r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE); |
95778782 | 96 | if (r < 0) |
800f478c LP |
97 | log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m", |
98 | unit_dependency_to_string(dependency), entry); | |
95778782 | 99 | } |
9e2f7c11 | 100 | |
8afbb8e1 | 101 | return 0; |
9e2f7c11 | 102 | } |
5cb5a6ff | 103 | |
ae7a7182 | 104 | int unit_load_dropin(Unit *u) { |
d7e0da1d | 105 | _cleanup_strv_free_ char **l = NULL; |
95778782 | 106 | char **f; |
1a7f1b38 | 107 | int r; |
99f08d14 LP |
108 | |
109 | assert(u); | |
110 | ||
95778782 ZJS |
111 | /* Load dependencies from .wants and .requires directories */ |
112 | r = process_deps(u, UNIT_WANTS, ".wants"); | |
113 | if (r < 0) | |
114 | return r; | |
99f08d14 | 115 | |
95778782 ZJS |
116 | r = process_deps(u, UNIT_REQUIRES, ".requires"); |
117 | if (r < 0) | |
118 | return r; | |
0301abf4 | 119 | |
95778782 | 120 | /* Load .conf dropins */ |
d7e0da1d | 121 | r = unit_find_dropin_paths(u, &l); |
1a7f1b38 | 122 | if (r <= 0) |
ae7a7182 | 123 | return 0; |
5926ccca | 124 | |
1cc6c93a YW |
125 | if (!u->dropin_paths) |
126 | u->dropin_paths = TAKE_PTR(l); | |
127 | else { | |
d7e0da1d LP |
128 | r = strv_extend_strv(&u->dropin_paths, l, true); |
129 | if (r < 0) | |
130 | return log_oom(); | |
131 | } | |
132 | ||
bcde742e LP |
133 | STRV_FOREACH(f, u->dropin_paths) |
134 | (void) config_parse(u->id, *f, NULL, | |
135 | UNIT_VTABLE(u)->sections, | |
136 | config_item_perf_lookup, load_fragment_gperf_lookup, | |
137 | 0, u); | |
5926ccca | 138 | |
ae7a7182 OS |
139 | u->dropin_mtime = now(CLOCK_REALTIME); |
140 | ||
5cb5a6ff LP |
141 | return 0; |
142 | } |