From: Lennart Poettering Date: Fri, 26 Aug 2022 14:59:47 +0000 (+0200) Subject: condition: allow fnmatch compares for ConditionOSRelease= X-Git-Tag: v252-rc1~273^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8daa674090d47320cd7ed6abb72dfdc0c4aa60f3;p=thirdparty%2Fsystemd.git condition: allow fnmatch compares for ConditionOSRelease= We support this for smbios matches, hence do so for /etc/os-release matches too. --- diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 7c1f7186e2f..78a8db2e608 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -1694,10 +1694,13 @@ Verify that a specific key=value pair is set in the host's os-release5. - Other than exact matching with =, and !=, relative - comparisons are supported for versioned parameters (e.g. VERSION_ID). The - comparator can be one of <, <=, =, - !=, >= and >. + Other than exact string matching with =, and !=, + relative comparisons are supported for versioned parameters (e.g. VERSION_ID), + and shell-style wildcard comparisons (*, ?, + []) are supported with the =$ (match) and + !=$ (non-match). The comparator can be one of <, + <=, =, !=, >=, + >, =$ and !=$. diff --git a/src/shared/condition.c b/src/shared/condition.c index db7a02db7d7..4e5702bc41a 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -257,7 +257,7 @@ static int condition_test_osrelease(Condition *c, char **env) { /* parse_compare_operator() needs the string to start with the comparators */ word = condition; - r = extract_first_word(&word, &key, "!<=>", EXTRACT_RETAIN_SEPARATORS); + r = extract_first_word(&word, &key, "!<=>$", EXTRACT_RETAIN_SEPARATORS); if (r < 0) return log_debug_errno(r, "Failed to parse parameter: %m"); /* The os-release spec mandates env-var-like key names */ @@ -266,7 +266,7 @@ static int condition_test_osrelease(Condition *c, char **env) { "Failed to parse parameter, key/value format expected: %m"); /* Do not allow whitespace after the separator, as that's not a valid os-release format */ - operator = parse_compare_operator(&word, COMPARE_EQUAL_BY_STRING); + operator = parse_compare_operator(&word, COMPARE_ALLOW_FNMATCH|COMPARE_EQUAL_BY_STRING); if (operator < 0 || isempty(word) || strchr(WHITESPACE, *word) != NULL) return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse parameter, key/value format expected: %m"); diff --git a/src/test/test-condition.c b/src/test/test-condition.c index c430110acda..f93619293fa 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -1177,13 +1177,13 @@ TEST(condition_test_os_release) { key_value_pair = strjoina(os_release_pairs[0], "=$", quote, os_release_pairs[1], quote); condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); assert_se(condition); - assert_se(condition_test(condition, environ) == -EINVAL); + assert_se(condition_test(condition, environ) > 0); condition_free(condition); key_value_pair = strjoina(os_release_pairs[0], "!=$", quote, os_release_pairs[1], quote); condition = condition_new(CONDITION_OS_RELEASE, key_value_pair, false, false); assert_se(condition); - assert_se(condition_test(condition, environ) == -EINVAL); + assert_se(condition_test(condition, environ) == 0); condition_free(condition); /* Some distros (eg: Arch) do not set VERSION_ID */