2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "conf-parser.h"
23 #include "load-dropin.h"
24 #include "load-fragment.h"
26 #include "stat-util.h"
27 #include "string-util.h"
29 #include "unit-name.h"
32 static int unit_name_compatible(const char *a
, const char *b
) {
33 _cleanup_free_
char *prefix
= NULL
;
36 /* the straightforward case: the symlink name matches the target */
40 r
= unit_name_template(a
, &prefix
);
45 /* oom, or some other failure. Just skip the warning. */
48 /* an instance name points to a target that is just the template name */
55 static int process_deps(Unit
*u
, UnitDependency dependency
, const char *dir_suffix
) {
56 _cleanup_strv_free_
char **paths
= NULL
;
60 r
= unit_file_find_dropin_paths(NULL
,
61 u
->manager
->lookup_paths
.search_path
,
62 u
->manager
->unit_path_cache
,
70 STRV_FOREACH(p
, paths
) {
72 _cleanup_free_
char *target
= NULL
;
76 if (null_or_empty_path(*p
) > 0) {
77 /* an error usually means an invalid symlink, which is not a mask */
78 log_unit_debug(u
, "%s dependency on %s is masked by %s, ignoring.",
79 unit_dependency_to_string(dependency
), entry
, *p
);
85 log_unit_warning_errno(u
, r
, "%s dropin %s unreadable, ignoring: %m",
86 unit_dependency_to_string(dependency
), *p
);
90 log_unit_warning(u
, "%s dependency dropin %s is not a symlink, ignoring.",
91 unit_dependency_to_string(dependency
), *p
);
95 if (!unit_name_is_valid(entry
, UNIT_NAME_ANY
)) {
96 log_unit_warning(u
, "%s dependency dropin %s is not a valid unit name, ignoring.",
97 unit_dependency_to_string(dependency
), *p
);
101 r
= readlink_malloc(*p
, &target
);
103 log_unit_warning_errno(u
, r
, "readlink(\"%s\") failed, ignoring: %m", *p
);
107 /* We don't treat this as an error, especially because we didn't check this for a
108 * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
109 r
= unit_name_compatible(entry
, basename(target
));
111 log_unit_warning_errno(u
, r
, "Can't check if names %s and %s are compatible, ignoring: %m", entry
, basename(target
));
115 log_unit_warning(u
, "%s dependency dropin %s target %s has different name",
116 unit_dependency_to_string(dependency
), *p
, target
);
118 r
= unit_add_dependency_by_name(u
, dependency
, entry
, *p
, true, UNIT_DEPENDENCY_FILE
);
120 log_unit_error_errno(u
, r
, "cannot add %s dependency on %s, ignoring: %m",
121 unit_dependency_to_string(dependency
), entry
);
127 int unit_load_dropin(Unit
*u
) {
128 _cleanup_strv_free_
char **l
= NULL
;
134 /* Load dependencies from .wants and .requires directories */
135 r
= process_deps(u
, UNIT_WANTS
, ".wants");
139 r
= process_deps(u
, UNIT_REQUIRES
, ".requires");
143 /* Load .conf dropins */
144 r
= unit_find_dropin_paths(u
, &l
);
148 if (!u
->dropin_paths
) {
152 r
= strv_extend_strv(&u
->dropin_paths
, l
, true);
157 STRV_FOREACH(f
, u
->dropin_paths
)
158 (void) config_parse(u
->id
, *f
, NULL
,
159 UNIT_VTABLE(u
)->sections
,
160 config_item_perf_lookup
, load_fragment_gperf_lookup
,
163 u
->dropin_mtime
= now(CLOCK_REALTIME
);