match glibc in ConditionVersion

Extend ConditionVersion= to allow matching against glibc version,
as proposed in https://github.com/systemd/systemd/pull/36468#issuecomment-2674600909
This commit is contained in:
Matteo Croce
2025-03-16 02:48:29 +01:00
committed by Yu Watanabe
parent 966f872880
commit dcde1e903a
4 changed files with 43 additions and 4 deletions

View File

@@ -1487,10 +1487,10 @@
<listitem><para><varname>ConditionVersion=</varname> may be used to check whether a software
version matches a certain expression, or if prefixed with the exclamation mark, does not match.
The first argument is the software whose version has to be checked. Currently
<literal>kernel</literal> and <literal>systemd</literal> are supported. If this argument is
omitted, <literal>kernel</literal> is implied. The second argument must be a list
of (potentially quoted) expressions. Each expression starts with one of <literal>=</literal> or
<literal>!=</literal> for string comparisons, <literal>&lt;</literal>, <literal>&lt;=</literal>,
<literal>kernel</literal>, <literal>systemd</literal> and <literal>glibc</literal> are supported.
If this argument is omitted, <literal>kernel</literal> is implied. The second argument must be a
list of (potentially quoted) expressions. Each expression starts with one of <literal>=</literal>
or <literal>!=</literal> for string comparisons, <literal>&lt;</literal>, <literal>&lt;=</literal>,
<literal>==</literal>, <literal>&lt;&gt;</literal>, <literal>&gt;=</literal>,
<literal>&gt;</literal> for version comparisons, or <literal>$=</literal>, <literal>!$=</literal>
for a shell-style glob match. If no operator is specified, <literal>$=</literal> is implied.</para>

View File

@@ -4,6 +4,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <gnu/libc-version.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/stat.h>
@@ -258,6 +259,9 @@ static int condition_test_version(Condition *c, char **env) {
if (streq(word, "systemd"))
return condition_test_version_cmp(p, STRINGIFY(PROJECT_VERSION));
if (streq(word, "glibc"))
return condition_test_version_cmp(p, gnu_get_libc_version());
/* if no predicate has been set, default to "kernel" and use the whole parameter as condition */
if (!streq(word, "kernel"))
p = c->parameter;

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <gnu/libc-version.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/utsname.h>
@@ -676,6 +677,35 @@ TEST(condition_test_version) {
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, v, false, false)));
ASSERT_OK_ZERO(condition_test(condition, environ));
condition_free(condition);
/* Test glibc version */
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, "glibc > 1", false, false)));
ASSERT_OK_POSITIVE(condition_test(condition, environ));
condition_free(condition);
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, "glibc < 2", false, false)));
ASSERT_OK_ZERO(condition_test(condition, environ));
condition_free(condition);
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, "glibc < 9999", false, false)));
ASSERT_OK_POSITIVE(condition_test(condition, environ));
condition_free(condition);
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, "glibc > 9999", false, false)));
ASSERT_OK_ZERO(condition_test(condition, environ));
condition_free(condition);
v = strjoina("glibc = ", gnu_get_libc_version());
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, v, false, false)));
ASSERT_OK_POSITIVE(condition_test(condition, environ));
condition_free(condition);
v = strjoina("glibc != ", gnu_get_libc_version());
ASSERT_NOT_NULL((condition = condition_new(CONDITION_VERSION, v, false, false)));
ASSERT_OK_ZERO(condition_test(condition, environ));
condition_free(condition);
}
TEST(condition_test_credential) {

View File

@@ -21,6 +21,11 @@ ConditionVersion=systemd ">=20" "<=9000" "!=14"
ConditionVersion=systemd " >= 20" " <= 9000 " "!= 14"
ConditionVersion=systemd " >= 20" " * "
ConditionVersion=glibc ">=2"
ConditionVersion=glibc ">=2" "<=9000" "!=1"
ConditionVersion=glibc " >= 2" " <= 9000 " "!= 1"
ConditionVersion=glibc " >= 2" " * "
[Service]
ExecStart=touch /tmp/a ; /bin/sh -c 'touch /tmp/b' ; touch /tmp/c
ExecStart=test -f /tmp/a