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