From: Zbigniew Jędrzejewski-Szmek Date: Thu, 19 Dec 2019 14:04:17 +0000 (+0100) Subject: shared/install: split out alias verification function X-Git-Tag: v245-rc1~137^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aa0f357fd833feecbea6c3e9be80b643e433bced;p=thirdparty%2Fsystemd.git shared/install: split out alias verification function No functional change. --- diff --git a/src/shared/install.c b/src/shared/install.c index 115a64f5f4e..236f9a6e598 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -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); diff --git a/src/shared/install.h b/src/shared/install.h index b2ad9c4a71e..12de2c66092 100644 --- a/src/shared/install.h +++ b/src/shared/install.h @@ -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_;