]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-dropin.c
tree-wide: Add more socket units (#37991)
[thirdparty/systemd.git] / src / core / load-dropin.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
cf0fbc49 3#include "conf-parser.h"
4ea4abb6 4#include "dropin.h"
317cd27a 5#include "fs-util.h"
5cb5a6ff 6#include "load-dropin.h"
cf0fbc49 7#include "load-fragment.h"
b952f2e1 8#include "log.h"
4ea4abb6 9#include "manager.h"
6b783209 10#include "path-util.h"
95778782 11#include "stat-util.h"
036643a2 12#include "strv.h"
cf0fbc49 13#include "unit.h"
1cf40697 14#include "unit-name.h"
8afbb8e1 15
4ea4abb6
DDM
16int unit_find_dropin_paths(Unit *u, bool use_unit_path_cache, char ***paths) {
17 assert(u);
18
19 return unit_file_find_dropin_paths(NULL,
20 u->manager->lookup_paths.search_path,
21 use_unit_path_cache ? u->manager->unit_path_cache : NULL,
22 ".d", ".conf",
23 u->id, u->aliases,
24 paths);
25}
26
95778782
ZJS
27static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
28 _cleanup_strv_free_ char **paths = NULL;
9e2f7c11
LP
29 int r;
30
95778782
ZJS
31 r = unit_file_find_dropin_paths(NULL,
32 u->manager->lookup_paths.search_path,
33 u->manager->unit_path_cache,
4562c355
ZJS
34 dir_suffix, NULL,
35 u->id, u->aliases,
95778782 36 &paths);
1a7f1b38 37 if (r < 0)
95778782
ZJS
38 return r;
39
40 STRV_FOREACH(p, paths) {
6b783209 41 _cleanup_free_ char *target = NULL, *target_file = NULL, *entry = NULL;
95778782 42
5be5d39b
ZJS
43 if (null_or_empty_path(*p) > 0) {
44 /* an error usually means an invalid symlink, which is not a mask */
45 log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
46 unit_dependency_to_string(dependency), entry, *p);
47 continue;
48 }
49
95778782
ZJS
50 r = is_symlink(*p);
51 if (r < 0) {
52 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
53 unit_dependency_to_string(dependency), *p);
54 continue;
55 }
56 if (r == 0) {
57 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
58 unit_dependency_to_string(dependency), *p);
59 continue;
60 }
61
6b783209
W
62 r = path_extract_filename(*p, &entry);
63 if (r < 0) {
64 log_unit_warning_errno(u, r, "Failed to extract file name of %s dependency dropin %s, ignoring: %m",
65 unit_dependency_to_string(dependency), *p);
66 continue;
67 }
68
317cd27a
ZJS
69 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
70 log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
71 unit_dependency_to_string(dependency), *p);
72 continue;
73 }
74
75 r = readlink_malloc(*p, &target);
76 if (r < 0) {
77 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
78 continue;
79 }
80
6b783209
W
81 r = path_extract_filename(target, &target_file);
82 if (r < 0) {
83 log_unit_warning_errno(u, r, "Failed to extract file name for dropin target %s, ignoring: %m",
84 target);
85 continue;
86 }
87
317cd27a
ZJS
88 /* We don't treat this as an error, especially because we didn't check this for a
89 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
6b783209 90 r = unit_symlink_name_compatible(entry, target_file, u->instance);
45f4238a 91 if (r < 0) {
9a4f9e69 92 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m",
6b783209 93 entry, target_file);
45f4238a
LP
94 continue;
95 }
96 if (r == 0)
317cd27a
ZJS
97 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
98 unit_dependency_to_string(dependency), *p, target);
95778782 99
35d8c19a 100 r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE);
95778782 101 if (r < 0)
800f478c
LP
102 log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
103 unit_dependency_to_string(dependency), entry);
95778782 104 }
9e2f7c11 105
8afbb8e1 106 return 0;
9e2f7c11 107}
5cb5a6ff 108
ae7a7182 109int unit_load_dropin(Unit *u) {
d7e0da1d 110 _cleanup_strv_free_ char **l = NULL;
1a7f1b38 111 int r;
99f08d14
LP
112
113 assert(u);
114
38f90179 115 /* Load dependencies from .wants, .requires and .upholds directories */
95778782
ZJS
116 r = process_deps(u, UNIT_WANTS, ".wants");
117 if (r < 0)
118 return r;
99f08d14 119
95778782
ZJS
120 r = process_deps(u, UNIT_REQUIRES, ".requires");
121 if (r < 0)
122 return r;
0301abf4 123
38f90179
MY
124 r = process_deps(u, UNIT_UPHOLDS, ".upholds");
125 if (r < 0)
126 return r;
127
95778782 128 /* Load .conf dropins */
82c482d5 129 r = unit_find_dropin_paths(u, /* use_unit_path_cache = */ true, &l);
1a7f1b38 130 if (r <= 0)
ae7a7182 131 return 0;
5926ccca 132
a2c8652a
MY
133 r = strv_extend_strv_consume(&u->dropin_paths, TAKE_PTR(l), /* filter_duplicates = */ true);
134 if (r < 0)
135 return log_oom();
d7e0da1d 136
da46a1bc 137 u->dropin_mtime = 0;
8524db50
YW
138 STRV_FOREACH(f, u->dropin_paths) {
139 struct stat st;
140
141 r = config_parse(u->id, *f, NULL,
142 UNIT_VTABLE(u)->sections,
143 config_item_perf_lookup, load_fragment_gperf_lookup,
144 0, u, &st);
145 if (r > 0)
146 u->dropin_mtime = MAX(u->dropin_mtime, timespec_load(&st.st_mtim));
147 }
ae7a7182 148
5cb5a6ff
LP
149 return 0;
150}