1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "conf-parser.h"
4 #include "fs-util.h"
5 #include "load-dropin.h"
6 #include "load-fragment.h"
7 #include "log.h"
8 #include "stat-util.h"
9 #include "string-util.h"
10 #include "strv.h"
11 #include "unit-name.h"
12 #include "unit.h"
13 
process_deps(Unit * u,UnitDependency dependency,const char * dir_suffix)14 static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
15         _cleanup_strv_free_ char **paths = NULL;
16         int r;
17 
18         r = unit_file_find_dropin_paths(NULL,
19                                         u->manager->lookup_paths.search_path,
20                                         u->manager->unit_path_cache,
21                                         dir_suffix, NULL,
22                                         u->id, u->aliases,
23                                         &paths);
24         if (r < 0)
25                 return r;
26 
27         STRV_FOREACH(p, paths) {
28                 _cleanup_free_ char *target = NULL;
29                 const char *entry;
30 
31                 entry = basename(*p);
32 
33                 if (null_or_empty_path(*p) > 0) {
34                         /* an error usually means an invalid symlink, which is not a mask */
35                         log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
36                                        unit_dependency_to_string(dependency), entry, *p);
37                         continue;
38                 }
39 
40                 r = is_symlink(*p);
41                 if (r < 0) {
42                         log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
43                                                unit_dependency_to_string(dependency), *p);
44                         continue;
45                 }
46                 if (r == 0) {
47                         log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
48                                          unit_dependency_to_string(dependency), *p);
49                         continue;
50                 }
51 
52                 if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
53                         log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
54                                          unit_dependency_to_string(dependency), *p);
55                         continue;
56                 }
57 
58                 r = readlink_malloc(*p, &target);
59                 if (r < 0) {
60                         log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
61                         continue;
62                 }
63 
64                 /* We don't treat this as an error, especially because we didn't check this for a
65                  * long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
66                 r = unit_symlink_name_compatible(entry, basename(target), u->instance);
67                 if (r < 0) {
68                         log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m",
69                                                entry, basename(target));
70                         continue;
71                 }
72                 if (r == 0)
73                         log_unit_warning(u, "%s dependency dropin %s target %s has different name",
74                                          unit_dependency_to_string(dependency), *p, target);
75 
76                 r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE);
77                 if (r < 0)
78                         log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
79                                                unit_dependency_to_string(dependency), entry);
80         }
81 
82         return 0;
83 }
84 
unit_load_dropin(Unit * u)85 int unit_load_dropin(Unit *u) {
86         _cleanup_strv_free_ char **l = NULL;
87         int r;
88 
89         assert(u);
90 
91         /* Load dependencies from .wants and .requires directories */
92         r = process_deps(u, UNIT_WANTS, ".wants");
93         if (r < 0)
94                 return r;
95 
96         r = process_deps(u, UNIT_REQUIRES, ".requires");
97         if (r < 0)
98                 return r;
99 
100         /* Load .conf dropins */
101         r = unit_find_dropin_paths(u, &l);
102         if (r <= 0)
103                 return 0;
104 
105         if (!u->dropin_paths)
106                 u->dropin_paths = TAKE_PTR(l);
107         else {
108                 r = strv_extend_strv(&u->dropin_paths, l, true);
109                 if (r < 0)
110                         return log_oom();
111         }
112 
113         u->dropin_mtime = 0;
114         STRV_FOREACH(f, u->dropin_paths) {
115                 struct stat st;
116 
117                 r = config_parse(u->id, *f, NULL,
118                                  UNIT_VTABLE(u)->sections,
119                                  config_item_perf_lookup, load_fragment_gperf_lookup,
120                                  0, u, &st);
121                 if (r > 0)
122                         u->dropin_mtime = MAX(u->dropin_mtime, timespec_load(&st.st_mtim));
123         }
124 
125         return 0;
126 }
127