|
|
21255d |
From 4c41ad9418058aefb2d2732b0b65da9c7cdf5151 Mon Sep 17 00:00:00 2001
|
|
|
21255d |
From: Ruixin Bao <rubao@redhat.com>
|
|
|
21255d |
Date: Tue, 21 Aug 2018 20:40:56 +0000
|
|
|
21255d |
Subject: [PATCH] install: allow instantiated units to be enabled via presets
|
|
|
21255d |
|
|
|
21255d |
This patch implements https://github.com/systemd/systemd/issues/9421.
|
|
|
21255d |
|
|
|
21255d |
The .preset file now is able to take a rule in the format of:(e.g)
|
|
|
21255d |
enable foo@.service bar0 bar1 bar2
|
|
|
21255d |
|
|
|
21255d |
In the above example, when preset-all is called, all three instances of
|
|
|
21255d |
foo@bar0.service, foo@bar1.service and foo@bar2.service will be enabled.
|
|
|
21255d |
|
|
|
21255d |
When preset is called on a single service(e.g: foo@bar1.service), only
|
|
|
21255d |
the mentioned one(foo@bar1.service) will be enabled.
|
|
|
21255d |
|
|
|
21255d |
Tests are added for future regression.
|
|
|
21255d |
|
|
|
21255d |
(cherry picked from commit 4c9565eea534cd233a913c8c21f7920dba229743)
|
|
|
21255d |
|
|
|
21255d |
Resolves: #1812972
|
|
|
21255d |
---
|
|
|
21255d |
src/shared/install.c | 155 ++++++++++++++++++++++++++++++-----
|
|
|
21255d |
src/test/test-install-root.c | 57 +++++++++++++
|
|
|
21255d |
2 files changed, 193 insertions(+), 19 deletions(-)
|
|
|
21255d |
|
|
|
21255d |
diff --git a/src/shared/install.c b/src/shared/install.c
|
|
|
21255d |
index 77ae812878..1d4beaa83b 100644
|
|
|
21255d |
--- a/src/shared/install.c
|
|
|
21255d |
+++ b/src/shared/install.c
|
|
|
21255d |
@@ -60,6 +60,7 @@ typedef enum {
|
|
|
21255d |
typedef struct {
|
|
|
21255d |
char *pattern;
|
|
|
21255d |
PresetAction action;
|
|
|
21255d |
+ char **instances;
|
|
|
21255d |
} PresetRule;
|
|
|
21255d |
|
|
|
21255d |
typedef struct {
|
|
|
21255d |
@@ -87,8 +88,10 @@ static inline void presets_freep(Presets *p) {
|
|
|
21255d |
if (!p)
|
|
|
21255d |
return;
|
|
|
21255d |
|
|
|
21255d |
- for (i = 0; i < p->n_rules; i++)
|
|
|
21255d |
+ for (i = 0; i < p->n_rules; i++) {
|
|
|
21255d |
free(p->rules[i].pattern);
|
|
|
21255d |
+ strv_free(p->rules[i].instances);
|
|
|
21255d |
+ }
|
|
|
21255d |
|
|
|
21255d |
free(p->rules);
|
|
|
21255d |
p->n_rules = 0;
|
|
|
21255d |
@@ -2755,6 +2758,39 @@ int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *
|
|
|
21255d |
return 1;
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
+static int split_pattern_into_name_and_instances(const char *pattern, char **out_unit_name, char ***out_instances) {
|
|
|
21255d |
+ _cleanup_strv_free_ char **instances = NULL;
|
|
|
21255d |
+ _cleanup_free_ char *unit_name = NULL;
|
|
|
21255d |
+ int r;
|
|
|
21255d |
+
|
|
|
21255d |
+ assert(pattern);
|
|
|
21255d |
+ assert(out_instances);
|
|
|
21255d |
+ assert(out_unit_name);
|
|
|
21255d |
+
|
|
|
21255d |
+ r = extract_first_word(&pattern, &unit_name, NULL, 0);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+
|
|
|
21255d |
+ /* We handle the instances logic when unit name is extracted */
|
|
|
21255d |
+ if (pattern) {
|
|
|
21255d |
+ /* We only create instances when a rule of templated unit
|
|
|
21255d |
+ * is seen. A rule like enable foo@.service a b c will
|
|
|
21255d |
+ * result in an array of (a, b, c) as instance names */
|
|
|
21255d |
+ if (!unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE))
|
|
|
21255d |
+ return -EINVAL;
|
|
|
21255d |
+
|
|
|
21255d |
+ instances = strv_split(pattern, WHITESPACE);
|
|
|
21255d |
+ if (!instances)
|
|
|
21255d |
+ return -ENOMEM;
|
|
|
21255d |
+
|
|
|
21255d |
+ *out_instances = TAKE_PTR(instances);
|
|
|
21255d |
+ }
|
|
|
21255d |
+
|
|
|
21255d |
+ *out_unit_name = TAKE_PTR(unit_name);
|
|
|
21255d |
+
|
|
|
21255d |
+ return 0;
|
|
|
21255d |
+}
|
|
|
21255d |
+
|
|
|
21255d |
static int read_presets(UnitFileScope scope, const char *root_dir, Presets *presets) {
|
|
|
21255d |
_cleanup_(presets_freep) Presets ps = {};
|
|
|
21255d |
size_t n_allocated = 0;
|
|
|
21255d |
@@ -2824,15 +2860,20 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres
|
|
|
21255d |
|
|
|
21255d |
parameter = first_word(l, "enable");
|
|
|
21255d |
if (parameter) {
|
|
|
21255d |
- char *pattern;
|
|
|
21255d |
+ char *unit_name;
|
|
|
21255d |
+ char **instances = NULL;
|
|
|
21255d |
|
|
|
21255d |
- pattern = strdup(parameter);
|
|
|
21255d |
- if (!pattern)
|
|
|
21255d |
- return -ENOMEM;
|
|
|
21255d |
+ /* Unit_name will remain the same as parameter when no instances are specified */
|
|
|
21255d |
+ r = split_pattern_into_name_and_instances(parameter, &unit_name, &instances);
|
|
|
21255d |
+ if (r < 0) {
|
|
|
21255d |
+ log_syntax(NULL, LOG_WARNING, *p, n, 0, "Couldn't parse line '%s'. Ignoring.", line);
|
|
|
21255d |
+ continue;
|
|
|
21255d |
+ }
|
|
|
21255d |
|
|
|
21255d |
rule = (PresetRule) {
|
|
|
21255d |
- .pattern = pattern,
|
|
|
21255d |
+ .pattern = unit_name,
|
|
|
21255d |
.action = PRESET_ENABLE,
|
|
|
21255d |
+ .instances = instances,
|
|
|
21255d |
};
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
@@ -2868,15 +2909,71 @@ static int read_presets(UnitFileScope scope, const char *root_dir, Presets *pres
|
|
|
21255d |
return 0;
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
-static int query_presets(const char *name, const Presets presets) {
|
|
|
21255d |
+static int pattern_match_multiple_instances(
|
|
|
21255d |
+ const PresetRule rule,
|
|
|
21255d |
+ const char *unit_name,
|
|
|
21255d |
+ char ***ret) {
|
|
|
21255d |
+
|
|
|
21255d |
+ _cleanup_free_ char *templated_name = NULL;
|
|
|
21255d |
+ int r;
|
|
|
21255d |
+
|
|
|
21255d |
+ /* If no ret is needed or the rule itself does not have instances
|
|
|
21255d |
+ * initalized, we return not matching */
|
|
|
21255d |
+ if (!ret || !rule.instances)
|
|
|
21255d |
+ return 0;
|
|
|
21255d |
+
|
|
|
21255d |
+ r = unit_name_template(unit_name, &templated_name);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+ if (!streq(rule.pattern, templated_name))
|
|
|
21255d |
+ return 0;
|
|
|
21255d |
+
|
|
|
21255d |
+ /* Compose a list of specified instances when unit name is a template */
|
|
|
21255d |
+ if (unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
|
|
|
21255d |
+ _cleanup_free_ char *prefix = NULL;
|
|
|
21255d |
+ _cleanup_strv_free_ char **out_strv = NULL;
|
|
|
21255d |
+ char **iter;
|
|
|
21255d |
+
|
|
|
21255d |
+ r = unit_name_to_prefix(unit_name, &prefix);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+
|
|
|
21255d |
+ STRV_FOREACH(iter, rule.instances) {
|
|
|
21255d |
+ _cleanup_free_ char *name = NULL;
|
|
|
21255d |
+ r = unit_name_build(prefix, *iter, ".service", &name);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+ r = strv_extend(&out_strv, name);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+ }
|
|
|
21255d |
+
|
|
|
21255d |
+ *ret = TAKE_PTR(out_strv);
|
|
|
21255d |
+ return 1;
|
|
|
21255d |
+ } else {
|
|
|
21255d |
+ /* We now know the input unit name is an instance name */
|
|
|
21255d |
+ _cleanup_free_ char *instance_name = NULL;
|
|
|
21255d |
+
|
|
|
21255d |
+ r = unit_name_to_instance(unit_name, &instance_name);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+
|
|
|
21255d |
+ if (strv_find(rule.instances, instance_name))
|
|
|
21255d |
+ return 1;
|
|
|
21255d |
+ }
|
|
|
21255d |
+ return 0;
|
|
|
21255d |
+}
|
|
|
21255d |
+
|
|
|
21255d |
+static int query_presets(const char *name, const Presets presets, char ***instance_name_list) {
|
|
|
21255d |
PresetAction action = PRESET_UNKNOWN;
|
|
|
21255d |
size_t i;
|
|
|
21255d |
-
|
|
|
21255d |
+ char **s;
|
|
|
21255d |
if (!unit_name_is_valid(name, UNIT_NAME_ANY))
|
|
|
21255d |
return -EINVAL;
|
|
|
21255d |
|
|
|
21255d |
for (i = 0; i < presets.n_rules; i++)
|
|
|
21255d |
- if (fnmatch(presets.rules[i].pattern, name, FNM_NOESCAPE) == 0) {
|
|
|
21255d |
+ if (pattern_match_multiple_instances(presets.rules[i], name, instance_name_list) > 0 ||
|
|
|
21255d |
+ fnmatch(presets.rules[i].pattern, name, FNM_NOESCAPE) == 0) {
|
|
|
21255d |
action = presets.rules[i].action;
|
|
|
21255d |
break;
|
|
|
21255d |
}
|
|
|
21255d |
@@ -2886,7 +2983,11 @@ static int query_presets(const char *name, const Presets presets) {
|
|
|
21255d |
log_debug("Preset files don't specify rule for %s. Enabling.", name);
|
|
|
21255d |
return 1;
|
|
|
21255d |
case PRESET_ENABLE:
|
|
|
21255d |
- log_debug("Preset files say enable %s.", name);
|
|
|
21255d |
+ if (instance_name_list && *instance_name_list)
|
|
|
21255d |
+ STRV_FOREACH(s, *instance_name_list)
|
|
|
21255d |
+ log_debug("Preset files say enable %s.", *s);
|
|
|
21255d |
+ else
|
|
|
21255d |
+ log_debug("Preset files say enable %s.", name);
|
|
|
21255d |
return 1;
|
|
|
21255d |
case PRESET_DISABLE:
|
|
|
21255d |
log_debug("Preset files say disable %s.", name);
|
|
|
21255d |
@@ -2904,7 +3005,7 @@ int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char
|
|
|
21255d |
if (r < 0)
|
|
|
21255d |
return r;
|
|
|
21255d |
|
|
|
21255d |
- return query_presets(name, presets);
|
|
|
21255d |
+ return query_presets(name, presets, NULL);
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
static int execute_preset(
|
|
|
21255d |
@@ -2964,6 +3065,7 @@ static int preset_prepare_one(
|
|
|
21255d |
size_t *n_changes) {
|
|
|
21255d |
|
|
|
21255d |
_cleanup_(install_context_done) InstallContext tmp = {};
|
|
|
21255d |
+ _cleanup_strv_free_ char **instance_name_list = NULL;
|
|
|
21255d |
UnitFileInstallInfo *i;
|
|
|
21255d |
int r;
|
|
|
21255d |
|
|
|
21255d |
@@ -2979,19 +3081,34 @@ static int preset_prepare_one(
|
|
|
21255d |
return 0;
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
- r = query_presets(name, presets);
|
|
|
21255d |
+ r = query_presets(name, presets, &instance_name_list);
|
|
|
21255d |
if (r < 0)
|
|
|
21255d |
return r;
|
|
|
21255d |
|
|
|
21255d |
if (r > 0) {
|
|
|
21255d |
- r = install_info_discover(scope, plus, paths, name, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
|
|
|
21255d |
- &i, changes, n_changes);
|
|
|
21255d |
- if (r < 0)
|
|
|
21255d |
- return r;
|
|
|
21255d |
+ if (instance_name_list) {
|
|
|
21255d |
+ char **s;
|
|
|
21255d |
+ STRV_FOREACH(s, instance_name_list) {
|
|
|
21255d |
+ r = install_info_discover(scope, plus, paths, *s, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
|
|
|
21255d |
+ &i, changes, n_changes);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+
|
|
|
21255d |
+ r = install_info_may_process(i, paths, changes, n_changes);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+ }
|
|
|
21255d |
+ } else {
|
|
|
21255d |
+ r = install_info_discover(scope, plus, paths, name, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
|
|
|
21255d |
+ &i, changes, n_changes);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+
|
|
|
21255d |
+ r = install_info_may_process(i, paths, changes, n_changes);
|
|
|
21255d |
+ if (r < 0)
|
|
|
21255d |
+ return r;
|
|
|
21255d |
+ }
|
|
|
21255d |
|
|
|
21255d |
- r = install_info_may_process(i, paths, changes, n_changes);
|
|
|
21255d |
- if (r < 0)
|
|
|
21255d |
- return r;
|
|
|
21255d |
} else
|
|
|
21255d |
r = install_info_discover(scope, minus, paths, name, SEARCH_FOLLOW_CONFIG_SYMLINKS,
|
|
|
21255d |
&i, changes, n_changes);
|
|
|
21255d |
diff --git a/src/test/test-install-root.c b/src/test/test-install-root.c
|
|
|
21255d |
index 15dd3c6966..dbbcfe4297 100644
|
|
|
21255d |
--- a/src/test/test-install-root.c
|
|
|
21255d |
+++ b/src/test/test-install-root.c
|
|
|
21255d |
@@ -983,6 +983,62 @@ static void test_with_dropin_template(const char *root) {
|
|
|
21255d |
assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "with-dropin-3@instance-2.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
|
|
|
21255d |
}
|
|
|
21255d |
|
|
|
21255d |
+static void test_preset_multiple_instances(const char *root) {
|
|
|
21255d |
+ UnitFileChange *changes = NULL;
|
|
|
21255d |
+ size_t n_changes = 0;
|
|
|
21255d |
+ const char *p;
|
|
|
21255d |
+ UnitFileState state;
|
|
|
21255d |
+
|
|
|
21255d |
+ /* Set up template service files and preset file */
|
|
|
21255d |
+ p = strjoina(root, "/usr/lib/systemd/system/foo@.service");
|
|
|
21255d |
+ assert_se(write_string_file(p,
|
|
|
21255d |
+ "[Install]\n"
|
|
|
21255d |
+ "DefaultInstance=def\n"
|
|
|
21255d |
+ "WantedBy=multi-user.target\n", WRITE_STRING_FILE_CREATE) >= 0);
|
|
|
21255d |
+
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+
|
|
|
21255d |
+ p = strjoina(root, "/usr/lib/systemd/system-preset/test.preset");
|
|
|
21255d |
+ assert_se(write_string_file(p,
|
|
|
21255d |
+ "enable foo@.service bar0 bar1 bartest\n"
|
|
|
21255d |
+ "enable emptylist@.service\n" /* This line ensures the old functionality for templated unit still works */
|
|
|
21255d |
+ "disable *\n" , WRITE_STRING_FILE_CREATE) >= 0);
|
|
|
21255d |
+
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bar0.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+
|
|
|
21255d |
+ /* Preset a single instantiated unit specified in the list */
|
|
|
21255d |
+ assert_se(unit_file_preset(UNIT_FILE_SYSTEM, 0, root, STRV_MAKE("foo@bar0.service"), UNIT_FILE_PRESET_FULL, &changes, &n_changes) >= 0);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bar0.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
|
|
|
21255d |
+ assert_se(n_changes == 1);
|
|
|
21255d |
+ assert_se(changes[0].type == UNIT_FILE_SYMLINK);
|
|
|
21255d |
+ p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/foo@bar0.service");
|
|
|
21255d |
+ assert_se(streq(changes[0].path, p));
|
|
|
21255d |
+ unit_file_changes_free(changes, n_changes);
|
|
|
21255d |
+ changes = NULL; n_changes = 0;
|
|
|
21255d |
+
|
|
|
21255d |
+ assert_se(unit_file_disable(UNIT_FILE_SYSTEM, 0, root, STRV_MAKE("foo@bar0.service"), &changes, &n_changes) >= 0);
|
|
|
21255d |
+ assert_se(n_changes == 1);
|
|
|
21255d |
+ assert_se(changes[0].type == UNIT_FILE_UNLINK);
|
|
|
21255d |
+ p = strjoina(root, SYSTEM_CONFIG_UNIT_PATH"/multi-user.target.wants/foo@bar0.service");
|
|
|
21255d |
+ assert_se(streq(changes[0].path, p));
|
|
|
21255d |
+ unit_file_changes_free(changes, n_changes);
|
|
|
21255d |
+ changes = NULL; n_changes = 0;
|
|
|
21255d |
+
|
|
|
21255d |
+ /* Check for preset-all case, only instances on the list should be enabled, not including the default instance */
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bar1.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bartest.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+
|
|
|
21255d |
+ assert_se(unit_file_preset_all(UNIT_FILE_SYSTEM, 0, root, UNIT_FILE_PRESET_FULL, &changes, &n_changes) >= 0);
|
|
|
21255d |
+ assert_se(n_changes > 0);
|
|
|
21255d |
+
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@def.service", &state) >= 0 && state == UNIT_FILE_DISABLED);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bar0.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bar1.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
|
|
|
21255d |
+ assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "foo@bartest.service", &state) >= 0 && state == UNIT_FILE_ENABLED);
|
|
|
21255d |
+
|
|
|
21255d |
+}
|
|
|
21255d |
+
|
|
|
21255d |
int main(int argc, char *argv[]) {
|
|
|
21255d |
char root[] = "/tmp/rootXXXXXX";
|
|
|
21255d |
const char *p;
|
|
|
21255d |
@@ -1012,6 +1068,7 @@ int main(int argc, char *argv[]) {
|
|
|
21255d |
test_indirect(root);
|
|
|
21255d |
test_preset_and_list(root);
|
|
|
21255d |
test_preset_order(root);
|
|
|
21255d |
+ test_preset_multiple_instances(root);
|
|
|
21255d |
test_revert(root);
|
|
|
21255d |
test_static_instance(root);
|
|
|
21255d |
test_with_dropin(root);
|