1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
9 #include "conf-parser.h"
11 #include "load-dropin.h"
12 #include "load-fragment.h"
14 #include "stat-util.h"
15 #include "string-util.h"
17 #include "unit-name.h"
20 static int unit_name_compatible(const char *a
, const char *b
) {
21 _cleanup_free_
char *prefix
= NULL
;
24 /* the straightforward case: the symlink name matches the target */
28 r
= unit_name_template(a
, &prefix
);
33 /* oom, or some other failure. Just skip the warning. */
36 /* an instance name points to a target that is just the template name */
43 static int process_deps(Unit
*u
, UnitDependency dependency
, const char *dir_suffix
) {
44 _cleanup_strv_free_
char **paths
= NULL
;
48 r
= unit_file_find_dropin_paths(NULL
,
49 u
->manager
->lookup_paths
.search_path
,
50 u
->manager
->unit_path_cache
,
58 STRV_FOREACH(p
, paths
) {
60 _cleanup_free_
char *target
= NULL
;
64 if (null_or_empty_path(*p
) > 0) {
65 /* an error usually means an invalid symlink, which is not a mask */
66 log_unit_debug(u
, "%s dependency on %s is masked by %s, ignoring.",
67 unit_dependency_to_string(dependency
), entry
, *p
);
73 log_unit_warning_errno(u
, r
, "%s dropin %s unreadable, ignoring: %m",
74 unit_dependency_to_string(dependency
), *p
);
78 log_unit_warning(u
, "%s dependency dropin %s is not a symlink, ignoring.",
79 unit_dependency_to_string(dependency
), *p
);
83 if (!unit_name_is_valid(entry
, UNIT_NAME_ANY
)) {
84 log_unit_warning(u
, "%s dependency dropin %s is not a valid unit name, ignoring.",
85 unit_dependency_to_string(dependency
), *p
);
89 r
= readlink_malloc(*p
, &target
);
91 log_unit_warning_errno(u
, r
, "readlink(\"%s\") failed, ignoring: %m", *p
);
95 /* We don't treat this as an error, especially because we didn't check this for a
96 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
97 r
= unit_name_compatible(entry
, basename(target
));
99 log_unit_warning_errno(u
, r
, "Can't check if names %s and %s are compatible, ignoring: %m", entry
, basename(target
));
103 log_unit_warning(u
, "%s dependency dropin %s target %s has different name",
104 unit_dependency_to_string(dependency
), *p
, target
);
106 r
= unit_add_dependency_by_name(u
, dependency
, entry
, *p
, true, UNIT_DEPENDENCY_FILE
);
108 log_unit_warning_errno(u
, r
, "Cannot add %s dependency on %s, ignoring: %m",
109 unit_dependency_to_string(dependency
), entry
);
115 int unit_load_dropin(Unit
*u
) {
116 _cleanup_strv_free_
char **l
= NULL
;
122 /* Load dependencies from .wants and .requires directories */
123 r
= process_deps(u
, UNIT_WANTS
, ".wants");
127 r
= process_deps(u
, UNIT_REQUIRES
, ".requires");
131 /* Load .conf dropins */
132 r
= unit_find_dropin_paths(u
, &l
);
136 if (!u
->dropin_paths
)
137 u
->dropin_paths
= TAKE_PTR(l
);
139 r
= strv_extend_strv(&u
->dropin_paths
, l
, true);
144 STRV_FOREACH(f
, u
->dropin_paths
)
145 (void) config_parse(u
->id
, *f
, NULL
,
146 UNIT_VTABLE(u
)->sections
,
147 config_item_perf_lookup
, load_fragment_gperf_lookup
,
150 u
->dropin_mtime
= now(CLOCK_REALTIME
);