]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
condition: allow fnmatch compares for ConditionOSRelease=
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Aug 2022 14:59:47 +0000 (16:59 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 1 Sep 2022 21:15:14 +0000 (23:15 +0200)
We support this for smbios matches, hence do so for /etc/os-release
matches too.

man/systemd.unit.xml
src/shared/condition.c
src/test/test-condition.c

index 7c1f7186e2fb772fd00925a2bc6644e4162ba838..78a8db2e608837fc3c168d2a9cd17974dcbbd24d 100644 (file)
           <listitem><para>Verify that a specific <literal>key=value</literal> pair is set in the host's
           <citerefentry><refentrytitle>os-release</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
 
-          <para>Other than exact matching with <literal>=</literal>, and <literal>!=</literal>, relative
-          comparisons are supported for versioned parameters (e.g. <literal>VERSION_ID</literal>). The
-          comparator can be one of <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>,
-          <literal>!=</literal>, <literal>&gt;=</literal> and <literal>&gt;</literal>.</para>
+          <para>Other than exact string matching with <literal>=</literal>, and <literal>!=</literal>,
+          relative comparisons are supported for versioned parameters (e.g. <literal>VERSION_ID</literal>),
+          and shell-style wildcard comparisons (<literal>*</literal>, <literal>?</literal>,
+          <literal>[]</literal>) are supported with the <literal>=$</literal> (match) and
+          <literal>!=$</literal> (non-match). The comparator can be one of <literal>&lt;</literal>,
+          <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>, <literal>&gt;=</literal>,
+          <literal>&gt;</literal>, <literal>=$</literal> and <literal>!=$</literal>.</para>
           </listitem>
         </varlistentry>
 
index db7a02db7d71b6f8f5d0c66c0a70a5d76fe548cc..4e5702bc41aea822cb8aefb2db4db6d2c130f582 100644 (file)
@@ -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");
index c430110acda04ca212fdf6f7a84e6519b961f48d..f93619293fa8c32d285c6e47fe8d95a8e0d3bb9a 100644 (file)
@@ -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 */