]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-dropin.c
Merge pull request #17732 from yuwata/core-use-synthetic_errno
[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"
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
95778782
ZJS
14static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
15 _cleanup_strv_free_ char **paths = NULL;
16 char **p;
9e2f7c11
LP
17 int r;
18
95778782
ZJS
19 r = unit_file_find_dropin_paths(NULL,
20 u->manager->lookup_paths.search_path,
21 u->manager->unit_path_cache,
4562c355
ZJS
22 dir_suffix, NULL,
23 u->id, u->aliases,
95778782 24 &paths);
1a7f1b38 25 if (r < 0)
95778782
ZJS
26 return r;
27
28 STRV_FOREACH(p, paths) {
317cd27a 29 _cleanup_free_ char *target = NULL;
b119facd 30 const char *entry;
95778782
ZJS
31
32 entry = basename(*p);
33
5be5d39b
ZJS
34 if (null_or_empty_path(*p) > 0) {
35 /* an error usually means an invalid symlink, which is not a mask */
36 log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
37 unit_dependency_to_string(dependency), entry, *p);
38 continue;
39 }
40
95778782
ZJS
41 r = is_symlink(*p);
42 if (r < 0) {
43 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
44 unit_dependency_to_string(dependency), *p);
45 continue;
46 }
47 if (r == 0) {
48 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
49 unit_dependency_to_string(dependency), *p);
50 continue;
51 }
52
317cd27a
ZJS
53 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
54 log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
55 unit_dependency_to_string(dependency), *p);
56 continue;
57 }
58
59 r = readlink_malloc(*p, &target);
60 if (r < 0) {
61 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
62 continue;
63 }
64
65 /* We don't treat this as an error, especially because we didn't check this for a
66 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
1bf15585 67 r = unit_symlink_name_compatible(entry, basename(target), u->instance);
45f4238a 68 if (r < 0) {
9a4f9e69
ZJS
69 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m",
70 entry, basename(target));
45f4238a
LP
71 continue;
72 }
73 if (r == 0)
317cd27a
ZJS
74 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
75 unit_dependency_to_string(dependency), *p, target);
95778782 76
35d8c19a 77 r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE);
95778782 78 if (r < 0)
800f478c
LP
79 log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
80 unit_dependency_to_string(dependency), entry);
95778782 81 }
9e2f7c11 82
8afbb8e1 83 return 0;
9e2f7c11 84}
5cb5a6ff 85
ae7a7182 86int unit_load_dropin(Unit *u) {
d7e0da1d 87 _cleanup_strv_free_ char **l = NULL;
95778782 88 char **f;
1a7f1b38 89 int r;
99f08d14
LP
90
91 assert(u);
92
95778782
ZJS
93 /* Load dependencies from .wants and .requires directories */
94 r = process_deps(u, UNIT_WANTS, ".wants");
95 if (r < 0)
96 return r;
99f08d14 97
95778782
ZJS
98 r = process_deps(u, UNIT_REQUIRES, ".requires");
99 if (r < 0)
100 return r;
0301abf4 101
95778782 102 /* Load .conf dropins */
d7e0da1d 103 r = unit_find_dropin_paths(u, &l);
1a7f1b38 104 if (r <= 0)
ae7a7182 105 return 0;
5926ccca 106
1cc6c93a
YW
107 if (!u->dropin_paths)
108 u->dropin_paths = TAKE_PTR(l);
109 else {
d7e0da1d
LP
110 r = strv_extend_strv(&u->dropin_paths, l, true);
111 if (r < 0)
112 return log_oom();
113 }
114
bcde742e 115 STRV_FOREACH(f, u->dropin_paths)
4f9ff96a
LP
116 (void) config_parse(
117 u->id, *f, NULL,
118 UNIT_VTABLE(u)->sections,
119 config_item_perf_lookup, load_fragment_gperf_lookup,
120 0,
121 u,
122 &u->dropin_mtime);
ae7a7182 123
5cb5a6ff
LP
124 return 0;
125}