]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/install: split out alias verification function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Dec 2019 14:04:17 +0000 (15:04 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Dec 2019 14:07:19 +0000 (15:07 +0100)
No functional change.

src/shared/install.c
src/shared/install.h

index 115a64f5f4e68c7c0a7fef297b8e7786eba6e52c..236f9a6e59812303755930f0843390f9a785467a 100644 (file)
@@ -1708,6 +1708,39 @@ static int install_info_discover_and_check(
         return install_info_may_process(ret ? *ret : NULL, paths, changes, n_changes);
 }
 
+int unit_file_verify_alias(const UnitFileInstallInfo *i, const char *dst) {
+        int r;
+
+        if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) &&
+            !unit_name_is_valid(dst, UNIT_NAME_TEMPLATE) &&
+            !i->default_instance)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Aliases for templates without DefaultInstance must be templates.");
+
+        if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE | UNIT_NAME_TEMPLATE) &&
+            !unit_name_is_valid(dst, UNIT_NAME_TEMPLATE | UNIT_NAME_INSTANCE))
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "Aliases for template instances must be a templates or template instances containing the instance name.");
+
+        if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE | UNIT_NAME_TEMPLATE) &&
+            unit_name_is_valid(dst, UNIT_NAME_INSTANCE)) {
+                _cleanup_free_ char *src_inst = NULL, *dst_inst = NULL;
+
+                r = unit_name_to_instance(i->name, &src_inst);
+                if (r < 0)
+                        return r;
+
+                r = unit_name_to_instance(dst, &dst_inst);
+                if (r < 0)
+                        return r;
+                if (!strstr(dst_inst, src_inst?src_inst:i->default_instance))
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                               "Aliases for template instances must be a templates or template instances containing the instance name.");
+        }
+
+        return 0;
+}
+
 static int install_info_symlink_alias(
                 UnitFileInstallInfo *i,
                 const LookupPaths *paths,
@@ -1724,46 +1757,25 @@ static int install_info_symlink_alias(
         assert(config_path);
 
         STRV_FOREACH(s, i->aliases) {
-
                 _cleanup_free_ char *alias_path = NULL, *dst = NULL;
 
                 q = install_full_printf(i, *s, &dst);
                 if (q < 0)
                         return q;
 
-                if (unit_name_is_valid(i->name, UNIT_NAME_TEMPLATE) &&
-                    !unit_name_is_valid(dst, UNIT_NAME_TEMPLATE) &&
-                    !i->default_instance) {
-                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Aliases for templates without DefaultInstance must be templates.");
-                }
-
-                if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE | UNIT_NAME_TEMPLATE) &&
-                    !unit_name_is_valid(dst, UNIT_NAME_TEMPLATE | UNIT_NAME_INSTANCE)) {
-                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Aliases for template instances must be a templates or template instances containing the instance name.");
-                }
-
-                if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE | UNIT_NAME_TEMPLATE) &&
-                    unit_name_is_valid(dst, UNIT_NAME_INSTANCE)) {
-                        _cleanup_free_ char *src_inst = NULL, *dst_inst = NULL;
-
-                        q = unit_name_to_instance(i->name, &src_inst);
-                        if (q < 0)
-                                return q;
+                q = unit_file_verify_alias(i, dst);
+                if (q < 0)
+                        continue;
 
-                        q = unit_name_to_instance(dst, &dst_inst);
-                        if (q < 0)
-                                return q;
-                        if (!strstr(dst_inst, src_inst?src_inst:i->default_instance))
-                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Aliases for template instances must be a templates or template instances containing the instance name.");
-                }
+                if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE) &&
+                    unit_name_is_valid(dst, UNIT_NAME_TEMPLATE)) {
 
-                if (unit_name_is_valid(i->name, UNIT_NAME_INSTANCE) && unit_name_is_valid(dst, UNIT_NAME_TEMPLATE)) {
                         _cleanup_free_ char *s_copy = NULL, *instance = NULL;
-                        q = unit_name_to_instance(i->name,&instance);
+                        q = unit_name_to_instance(i->name, &instance);
                         if (q < 0)
                                 return q;
 
-                        q = unit_name_replace_instance(dst,instance,&s_copy);
+                        q = unit_name_replace_instance(dst, instance, &s_copy);
                         if (q < 0)
                                 return q;
                         free_and_replace(dst, s_copy);
index b2ad9c4a71e3e346fc84759fd9f48c68cd346b3f..12de2c66092173e02da5bc94dfc057245d80a934 100644 (file)
@@ -187,6 +187,8 @@ int unit_file_changes_add(UnitFileChange **changes, size_t *n_changes, UnitFileC
 void unit_file_changes_free(UnitFileChange *changes, size_t n_changes);
 void unit_file_dump_changes(int r, const char *verb, const UnitFileChange *changes, size_t n_changes, bool quiet);
 
+int unit_file_verify_alias(const UnitFileInstallInfo *i, const char *dst);
+
 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name);
 
 const char *unit_file_state_to_string(UnitFileState s) _const_;