]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
compare: add two new operators "==" and "<>"
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Aug 2022 15:20:56 +0000 (17:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 1 Sep 2022 21:16:13 +0000 (23:16 +0200)
These two operators always indicate ordering comparisons, as opposed to
"=" and "!=" which depending on context mean literal string compares.

This is useful for ConditionOSRelease= for example, as this means
there's now always a way to do version compares.

man/systemd.unit.xml
src/shared/compare-operator.c

index 78a8db2e608837fc3c168d2a9cd17974dcbbd24d..c5ba817579f79e78c79cb552385522f470b121e0 100644 (file)
           <para><literal>device-tree-compatible(<replaceable>value</replaceable>)</literal> for systems with a device tree that is compatible to
           <literal>value</literal>.</para>
 
-          <para><literal>smbios-field(<replaceable>field</replaceable> <replaceable>operator</replaceable> <replaceable>value</replaceable>)</literal>
-          for systems with a SMBIOS field containing a certain value.
-          <literal>field</literal> is the name of the SMBIOS field exposed as <literal>sysfs</literal> attribute file
-          below <filename>/sys/class/dmi/id/</filename>.
+          <para><literal>smbios-field(<replaceable>field</replaceable> <replaceable>operator</replaceable>
+          <replaceable>value</replaceable>)</literal> for systems with a SMBIOS field containing a certain
+          value.  <literal>field</literal> is the name of the SMBIOS field exposed as
+          <literal>sysfs</literal> attribute file below <filename>/sys/class/dmi/id/</filename>.
           <literal>operator</literal> is one of <literal>&lt;</literal>, <literal>&lt;=</literal>,
-          <literal>&gt;=</literal>, <literal>&gt;</literal>, <literal>=</literal>, <literal>!=</literal> for version
-          comparison, or <literal>=$</literal>, <literal>!=$</literal> for string comparison.
-          <literal>value</literal> is the expected value of the SMBIOS field (shell-style globs are possible if
-          <literal>=$</literal> or<literal>!=$</literal> is used).</para>
+          <literal>&gt;=</literal>, <literal>&gt;</literal>, <literal>==</literal>,
+          <literal>&lt;&gt;</literal> for version comparison, <literal>=</literal> and <literal>!=</literal>
+          for literal string comparison, or <literal>=$</literal>, <literal>!=$</literal> for shell-style
+          glob comparison.  <literal>value</literal> is the expected value of the SMBIOS field value
+          (possibly containing shell style globs in case <literal>=$</literal>/<literal>!=$</literal> is
+          used).</para>
           </listitem>
         </varlistentry>
 
           version (as reported by <command>uname -r</command>) matches a certain expression (or if prefixed
           with the exclamation mark does not match it). The argument must be a list of (potentially quoted)
           expressions.  For each of the expressions, if it starts with one of <literal>&lt;</literal>,
-          <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>, <literal>&gt;=</literal>,
-          <literal>&gt;</literal> a relative version comparison is done, otherwise the specified string is
-          matched with shell-style globs.</para>
+          <literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>), <literal>!=</literal>
+          (or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>, <literal>&gt;</literal> a relative
+          version comparison is done, otherwise the specified string is matched with shell-style
+          globs.</para>
 
           <para>Note that using the kernel version string is an unreliable way to determine which features
           are supported by a kernel, because of the widespread practice of backporting drivers, features, and
 
           <listitem><para>Verify that the specified amount of system memory is available to the current
           system. Takes a memory size in bytes as argument, optionally prefixed with a comparison operator
-          <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>,
-          <literal>&gt;=</literal>, <literal>&gt;</literal>. On bare-metal systems compares the amount of
-          physical memory in the system with the specified size, adhering to the specified comparison
-          operator. In containers compares the amount of memory assigned to the container instead.</para>
+          <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>),
+          <literal>!=</literal> (or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>,
+          <literal>&gt;</literal>. On bare-metal systems compares the amount of physical memory in the system
+          with the specified size, adhering to the specified comparison operator. In containers compares the
+          amount of memory assigned to the container instead.</para>
           </listitem>
         </varlistentry>
 
 
           <listitem><para>Verify that the specified number of CPUs is available to the current system. Takes
           a number of CPUs as argument, optionally prefixed with a comparison operator
-          <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal>, <literal>!=</literal>,
-          <literal>&gt;=</literal>, <literal>&gt;</literal>. Compares the number of CPUs in the CPU affinity
-          mask configured of the service manager itself with the specified number, adhering to the specified
-          comparison operator. On physical systems the number of CPUs in the affinity mask of the service
-          manager usually matches the number of physical CPUs, but in special and virtual environments might
-          differ. In particular, in containers the affinity mask usually matches the number of CPUs assigned
-          to the container and not the physically available ones.</para></listitem>
+          <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>=</literal> (or <literal>==</literal>),
+          <literal>!=</literal> (or <literal>&lt;&gt;</literal>), <literal>&gt;=</literal>,
+          <literal>&gt;</literal>. Compares the number of CPUs in the CPU affinity mask configured of the
+          service manager itself with the specified number, adhering to the specified comparison operator. On
+          physical systems the number of CPUs in the affinity mask of the service manager usually matches the
+          number of physical CPUs, but in special and virtual environments might differ. In particular, in
+          containers the affinity mask usually matches the number of CPUs assigned to the container and not
+          the physically available ones.</para></listitem>
         </varlistentry>
 
         <varlistentry>
           <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 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>
+          <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>;
+          with <literal>&lt;</literal>, <literal>&lt;=</literal>, <literal>==</literal>,
+          <literal>&lt;&gt;</literal>, <literal>&gt;=</literal>, <literal>&gt;</literal>), and shell-style
+          wildcard comparisons (<literal>*</literal>, <literal>?</literal>, <literal>[]</literal>) are
+          supported with the <literal>=$</literal> (match) and <literal>!=$</literal> (non-match).</para>
           </listitem>
         </varlistentry>
 
index 43f876cabe0c8256f8212a621e68eb1f65efb765..d7117f077162c2983df17da6a56ec62821a0aabc 100644 (file)
@@ -15,10 +15,12 @@ CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags
                 { COMPARE_FNMATCH_EQUAL,    "=$",  .valid_mask = COMPARE_ALLOW_FNMATCH   },
                 { COMPARE_FNMATCH_UNEQUAL,  "!=$", .valid_mask = COMPARE_ALLOW_FNMATCH   },
 
+                { COMPARE_UNEQUAL,          "<>"                                         },
                 { COMPARE_LOWER_OR_EQUAL,   "<="                                         },
                 { COMPARE_GREATER_OR_EQUAL, ">="                                         },
                 { COMPARE_LOWER,            "<"                                          },
                 { COMPARE_GREATER,          ">"                                          },
+                { COMPARE_EQUAL,            "=="                                         },
                 { COMPARE_STRING_EQUAL,     "=",   .need_mask = COMPARE_EQUAL_BY_STRING  },
                 { COMPARE_EQUAL,            "="                                          },
                 { COMPARE_STRING_UNEQUAL,   "!=",  .need_mask = COMPARE_EQUAL_BY_STRING  },