2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-02-03 13:03:47 +01:00
|
|
|
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "conf-parser.h"
|
2017-02-05 04:36:17 +01:00
|
|
|
#include "fs-util.h"
|
2010-01-23 01:52:57 +01:00
|
|
|
#include "load-dropin.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "load-fragment.h"
|
2010-01-27 04:55:19 +01:00
|
|
|
#include "log.h"
|
2017-02-05 02:50:44 +01:00
|
|
|
#include "stat-util.h"
|
2017-02-05 04:36:17 +01:00
|
|
|
#include "string-util.h"
|
2010-02-13 01:07:02 +01:00
|
|
|
#include "strv.h"
|
2010-04-15 03:11:11 +02:00
|
|
|
#include "unit-name.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "unit.h"
|
2013-01-11 00:21:06 +01:00
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
static int process_deps(Unit *u, UnitDependency dependency, const char *dir_suffix) {
|
|
|
|
_cleanup_strv_free_ char **paths = NULL;
|
|
|
|
char **p;
|
2010-04-15 03:11:11 +02:00
|
|
|
int r;
|
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
r = unit_file_find_dropin_paths(NULL,
|
|
|
|
u->manager->lookup_paths.search_path,
|
|
|
|
u->manager->unit_path_cache,
|
2020-05-27 15:49:17 +02:00
|
|
|
dir_suffix, NULL,
|
|
|
|
u->id, u->aliases,
|
2017-02-05 02:50:44 +01:00
|
|
|
&paths);
|
2014-12-15 05:12:40 +01:00
|
|
|
if (r < 0)
|
2017-02-05 02:50:44 +01:00
|
|
|
return r;
|
|
|
|
|
|
|
|
STRV_FOREACH(p, paths) {
|
2017-02-05 04:36:17 +01:00
|
|
|
_cleanup_free_ char *target = NULL;
|
2018-02-23 12:30:14 +01:00
|
|
|
const char *entry;
|
2017-02-05 02:50:44 +01:00
|
|
|
|
|
|
|
entry = basename(*p);
|
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
if (null_or_empty_path(*p) > 0) {
|
|
|
|
/* an error usually means an invalid symlink, which is not a mask */
|
|
|
|
log_unit_debug(u, "%s dependency on %s is masked by %s, ignoring.",
|
|
|
|
unit_dependency_to_string(dependency), entry, *p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
r = is_symlink(*p);
|
|
|
|
if (r < 0) {
|
|
|
|
log_unit_warning_errno(u, r, "%s dropin %s unreadable, ignoring: %m",
|
|
|
|
unit_dependency_to_string(dependency), *p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
log_unit_warning(u, "%s dependency dropin %s is not a symlink, ignoring.",
|
|
|
|
unit_dependency_to_string(dependency), *p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-05 04:36:17 +01:00
|
|
|
if (!unit_name_is_valid(entry, UNIT_NAME_ANY)) {
|
|
|
|
log_unit_warning(u, "%s dependency dropin %s is not a valid unit name, ignoring.",
|
|
|
|
unit_dependency_to_string(dependency), *p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = readlink_malloc(*p, &target);
|
|
|
|
if (r < 0) {
|
|
|
|
log_unit_warning_errno(u, r, "readlink(\"%s\") failed, ignoring: %m", *p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't treat this as an error, especially because we didn't check this for a
|
|
|
|
* long time. Nevertheless, we warn, because such mismatch can be mighty confusing. */
|
2019-12-21 16:09:35 +01:00
|
|
|
r = unit_symlink_name_compatible(entry, basename(target), u->instance);
|
2017-05-22 15:18:00 +02:00
|
|
|
if (r < 0) {
|
2019-12-20 19:12:34 +01:00
|
|
|
log_unit_warning_errno(u, r, "Can't check if names %s and %s are compatible, ignoring: %m",
|
|
|
|
entry, basename(target));
|
2017-05-22 15:18:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r == 0)
|
2017-02-05 04:36:17 +01:00
|
|
|
log_unit_warning(u, "%s dependency dropin %s target %s has different name",
|
|
|
|
unit_dependency_to_string(dependency), *p, target);
|
2017-02-05 02:50:44 +01:00
|
|
|
|
2018-09-15 19:57:52 +02:00
|
|
|
r = unit_add_dependency_by_name(u, dependency, entry, true, UNIT_DEPENDENCY_FILE);
|
2017-02-05 02:50:44 +01:00
|
|
|
if (r < 0)
|
2017-11-09 11:13:36 +01:00
|
|
|
log_unit_warning_errno(u, r, "Cannot add %s dependency on %s, ignoring: %m",
|
|
|
|
unit_dependency_to_string(dependency), entry);
|
2017-02-05 02:50:44 +01:00
|
|
|
}
|
2010-04-15 03:11:11 +02:00
|
|
|
|
2013-01-11 00:21:06 +01:00
|
|
|
return 0;
|
2010-04-15 03:11:11 +02:00
|
|
|
}
|
2010-01-23 01:52:57 +01:00
|
|
|
|
2013-04-01 12:32:35 +02:00
|
|
|
int unit_load_dropin(Unit *u) {
|
2016-02-26 17:05:33 +01:00
|
|
|
_cleanup_strv_free_ char **l = NULL;
|
2017-02-05 02:50:44 +01:00
|
|
|
char **f;
|
2014-12-15 05:12:40 +01:00
|
|
|
int r;
|
2010-11-15 04:24:04 +01:00
|
|
|
|
|
|
|
assert(u);
|
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
/* Load dependencies from .wants and .requires directories */
|
|
|
|
r = process_deps(u, UNIT_WANTS, ".wants");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-11-15 04:24:04 +01:00
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
r = process_deps(u, UNIT_REQUIRES, ".requires");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-01-27 00:15:56 +01:00
|
|
|
|
2017-02-05 02:50:44 +01:00
|
|
|
/* Load .conf dropins */
|
2016-02-26 17:05:33 +01:00
|
|
|
r = unit_find_dropin_paths(u, &l);
|
2014-12-15 05:12:40 +01:00
|
|
|
if (r <= 0)
|
2013-04-01 12:32:35 +02:00
|
|
|
return 0;
|
2013-01-11 01:04:11 +01:00
|
|
|
|
2018-04-05 07:26:26 +02:00
|
|
|
if (!u->dropin_paths)
|
|
|
|
u->dropin_paths = TAKE_PTR(l);
|
|
|
|
else {
|
2016-02-26 17:05:33 +01:00
|
|
|
r = strv_extend_strv(&u->dropin_paths, l, true);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
2017-11-09 00:26:11 +01:00
|
|
|
STRV_FOREACH(f, u->dropin_paths)
|
2020-06-02 14:55:12 +02:00
|
|
|
(void) config_parse(
|
|
|
|
u->id, *f, NULL,
|
|
|
|
UNIT_VTABLE(u)->sections,
|
|
|
|
config_item_perf_lookup, load_fragment_gperf_lookup,
|
|
|
|
0,
|
|
|
|
u,
|
|
|
|
&u->dropin_mtime);
|
2013-04-01 12:32:35 +02:00
|
|
|
|
2010-01-23 01:52:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|