]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/load-dropin.c
conf-parser: turn three bool function params into a flags fields
[thirdparty/systemd.git] / src / core / load-dropin.c
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
a7334b09
LP
9 (at your option) any later version.
10
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
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
0301abf4 20
cf0fbc49 21#include "conf-parser.h"
317cd27a 22#include "fs-util.h"
5cb5a6ff 23#include "load-dropin.h"
cf0fbc49 24#include "load-fragment.h"
b952f2e1 25#include "log.h"
95778782 26#include "stat-util.h"
317cd27a 27#include "string-util.h"
036643a2 28#include "strv.h"
9e2f7c11 29#include "unit-name.h"
cf0fbc49 30#include "unit.h"
8afbb8e1 31
45f4238a 32static int unit_name_compatible(const char *a, const char *b) {
317cd27a
ZJS
33 _cleanup_free_ char *prefix = NULL;
34 int r;
35
36 /* the straightforward case: the symlink name matches the target */
37 if (streq(a, b))
45f4238a 38 return 1;
317cd27a
ZJS
39
40 r = unit_name_template(a, &prefix);
e450032f
ZJS
41 if (r == -EINVAL)
42 /* not a template */
45f4238a 43 return 0;
e450032f
ZJS
44 if (r < 0)
45 /* oom, or some other failure. Just skip the warning. */
45f4238a 46 return r;
317cd27a
ZJS
47
48 /* an instance name points to a target that is just the template name */
49 if (streq(prefix, b))
45f4238a 50 return 1;
317cd27a 51
45f4238a 52 return 0;
317cd27a
ZJS
53}
54
95778782
ZJS
55static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
56 _cleanup_strv_free_ char **paths = NULL;
57 char **p;
9e2f7c11
LP
58 int r;
59
95778782
ZJS
60 r = unit_file_find_dropin_paths(NULL,
61 u->manager->lookup_paths.search_path,
62 u->manager->unit_path_cache,
63 dir_suffix,
64 NULL,
65 u->names,
66 &paths);
1a7f1b38 67 if (r < 0)
95778782
ZJS
68 return r;
69
70 STRV_FOREACH(p, paths) {
71 const char *entry;
317cd27a 72 _cleanup_free_ char *target = NULL;
95778782
ZJS
73
74 entry = basename(*p);
75
5be5d39b
ZJS
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);
80 continue;
81 }
82
95778782
ZJS
83 r = is_symlink(*p);
84 if (r < 0) {
85 log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
86 unit_dependency_to_string(dependency), *p);
87 continue;
88 }
89 if (r == 0) {
90 log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
91 unit_dependency_to_string(dependency), *p);
92 continue;
93 }
94
317cd27a
ZJS
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);
98 continue;
99 }
100
101 r = readlink_malloc(*p, &target);
102 if (r < 0) {
103 log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
104 continue;
105 }
106
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. */
45f4238a
LP
109 r = unit_name_compatible(entry, basename(target));
110 if (r < 0) {
111 log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m", entry, basename(target));
112 continue;
113 }
114 if (r == 0)
317cd27a
ZJS
115 log_unit_warning(u, "%s dependency dropin %s target %s has different name",
116 unit_dependency_to_string(dependency), *p, target);
95778782 117
eef85c4a 118 r = unit_add_dependency_by_name(u, dependency, entry, *p, true, UNIT_DEPENDENCY_FILE);
95778782
ZJS
119 if (r < 0)
120 log_unit_error_errno(u, r, "cannot add %s dependency on %s, ignoring: %m",
121 unit_dependency_to_string(dependency), entry);
122 }
9e2f7c11 123
8afbb8e1 124 return 0;
9e2f7c11 125}
5cb5a6ff 126
ae7a7182 127int unit_load_dropin(Unit *u) {
d7e0da1d 128 _cleanup_strv_free_ char **l = NULL;
95778782 129 char **f;
1a7f1b38 130 int r;
99f08d14
LP
131
132 assert(u);
133
95778782
ZJS
134 /* Load dependencies from .wants and .requires directories */
135 r = process_deps(u, UNIT_WANTS, ".wants");
136 if (r < 0)
137 return r;
99f08d14 138
95778782
ZJS
139 r = process_deps(u, UNIT_REQUIRES, ".requires");
140 if (r < 0)
141 return r;
0301abf4 142
95778782 143 /* Load .conf dropins */
d7e0da1d 144 r = unit_find_dropin_paths(u, &l);
1a7f1b38 145 if (r <= 0)
ae7a7182 146 return 0;
5926ccca 147
d7e0da1d
LP
148 if (!u->dropin_paths) {
149 u->dropin_paths = l;
150 l = NULL;
151 } else {
152 r = strv_extend_strv(&u->dropin_paths, l, true);
153 if (r < 0)
154 return log_oom();
155 }
156
bcde742e
LP
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,
161 0, u);
5926ccca 162
ae7a7182
OS
163 u->dropin_mtime = now(CLOCK_REALTIME);
164
5cb5a6ff
LP
165 return 0;
166}