tmpfiles: add the specifiers for the OS release and architecture

An entry with %a, or one of the specifiers that come from os-release,
was skipped as unsupported:

    d /var/lib/app/%o-%w 0755 - - -

Add %o, %w, %W, %B, %M, and %A, read from /etc/os-release with
/usr/lib/os-release as fallback, and %a with the names tmpfiles.d(5)
has for the architectures.  A field that os-release lacks gives an
empty string, as in tmpfiles.d(5).  An entry is still skipped when
the architecture is not one of those known.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:35:36 +01:00
1 parent bccd04d81d
commit fb3b13a245
6 files changed
+113 -22

No files matched your search

+3 -2
View File
@@ -17,8 +17,9 @@ All relevant changes are documented in this file.
at boot and no longer need a separate call
- tmpfiles: add the `%` specifiers of tmpfiles.d(5) for the path, and for
the argument of `C`, `f`, `F`, `L`, and `w` entries: `%b`, `%m`, `%H`,
`%l`, `%v`, the directories `%t`, `%T`, `%V`, `%S`, `%C`, `%L`, `%h`,
and the user ones `%u`, `%U`, `%g`, `%G`. Before, an entry with a `%`
`%l`, `%v`, `%a`, the directories `%t`, `%T`, `%V`, `%S`, `%C`, `%L`,
`%h`, the user ones `%u`, `%U`, `%g`, `%G`, and from os-release `%o`,
`%w`, `%W`, `%B`, `%M`, and `%A`. Before, an entry with a `%`
in the path was skipped. **Note:** a literal `%` in such an argument,
e.g. in a `w` entry for `core_pattern`, must now be written `%%`
- tmpfiles: the age of an entry can now have several units, as in
+3 -4
View File
@@ -23,10 +23,9 @@ For your convenience a set of *optional* plugins are available:
file systems must be writable for this plugin to work.
This plugin is a wrapper for the [tmpfiles.d(5)][] implementation that
Finit has. Capable but limited: no attributes or subvolumes, and
of the `%` specifiers not those for the OS release and architecture.
It runs `tmpfiles --create --remove --boot`, so entries marked
boot-only with a `!` are run here and at no other time.
Finit has. Capable but limited: no attributes or subvolumes. It
runs `tmpfiles --create --remove --boot`, so entries marked boot-only
with a `!` are run here and at no other time.
By default, `/lib/finit/tmpfiles.d` carries all the default .conf
files distributed with Finit. It is read first but can be overridden
+5 -12
View File
@@ -8,16 +8,7 @@ files or common packages already rely on, last what few will miss.
[tmpfiles.d(5)]: https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html
1. Specifiers
-------------
Missing are the ones for the OS release and the architecture: `%a`,
`%A`, `%B`, `%M`, `%o`, `%w`, and `%W`. An entry with one of them is
skipped. `%T` and `%V` are always `/tmp` and `/var/tmp`, they do not
look at `$TMPDIR`.
2. Remaining types and modifiers
1. Remaining types and modifiers
--------------------------------
* `L?`, and `C+`, which is read as `C`
@@ -28,7 +19,7 @@ look at `$TMPDIR`.
* Quoted paths, a path with a space in it cannot be written
3. Command line
2. Command line
---------------
Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`,
@@ -37,7 +28,7 @@ is the useful one, for packages that run `tmpfiles --create` on only
their own paths.
4. Smaller things
3. Smaller things
------------------
* Files are processed one directory at a time. tmpfiles.d(5) sorts
@@ -54,4 +45,6 @@ their own paths.
* A `w` entry with a glob that matches several paths writes its
argument only up to the first backslash for all but the first,
`write_arg()` cuts the argument up as it goes
* The `%T` and `%V` specifiers are always `/tmp` and `/var/tmp`, they
do not look at `$TMPDIR`
* `mksubsys()` in `src/util.c` is unused
+1 -1
View File
@@ -36,7 +36,7 @@ sulogin_SOURCES = sulogin.c
sulogin_CFLAGS = -W -Wall -Wextra -std=gnu99
sulogin_LDADD = -lcrypt
tmpfiles_SOURCES = tmpfiles.c util.c util.h log.h
tmpfiles_SOURCES = tmpfiles.c helpers.h util.c util.h log.h
tmpfiles_CFLAGS = -W -Wall -Wextra -std=gnu99
tmpfiles_CFLAGS += $(lite_CFLAGS) $(uev_CFLAGS)
tmpfiles_LDADD = $(lite_LIBS) $(uev_LIBS)
+88 -2
View File
@@ -49,6 +49,7 @@
# include <lite/lite.h>
#endif
#include "helpers.h"
#include "log.h"
#include "util.h"
@@ -953,10 +954,79 @@ static char *specifier_file(const char *file, const char *strip, char *buf, size
return buf;
}
/* Value of @key in os-release, empty when it is not there, as in tmpfiles.d(5) */
static char *specifier_os(const char *key, char *buf, size_t len)
{
char line[256];
FILE *fp;
buf[0] = 0;
fp = fopen("/etc/os-release", "r");
if (!fp)
fp = fopen("/usr/lib/os-release", "r");
if (!fp)
return buf;
while (fgets(line, sizeof(line), fp)) {
char *val;
line[strcspn(line, "\n")] = 0;
val = fgetval(line, key, "=");
if (val) {
strlcpy(buf, val, len);
free(val);
break;
}
}
fclose(fp);
return buf;
}
/* The name tmpfiles.d(5) has for the architecture, NULL when not known */
static const char *specifier_arch(const char *machine)
{
/* By prefix, uname has armv7l, armv5tejl, and the like. First match wins */
static const char *names[][2] = {
{ "x86_64", "x86-64" }, { "i386", "x86" },
{ "i486", "x86" }, { "i586", "x86" },
{ "i686", "x86" }, { "aarch64_be", "arm64-be" },
{ "aarch64", "arm64" }, { "arm", "arm" },
{ "riscv64", "riscv64" }, { "riscv32", "riscv32" },
{ "ppc64le", "ppc64-le" }, { "ppc64", "ppc64" },
{ "ppcle", "ppc-le" }, { "ppc", "ppc" },
{ "mips64", "mips64" }, { "mips", "mips" },
{ "s390x", "s390x" }, { "s390", "s390" },
{ "sparc64", "sparc64" }, { "sparc", "sparc" },
{ "loongarch64", "loongarch64" }, { "alpha", "alpha" },
{ "ia64", "ia64" }, { "m68k", "m68k" },
};
static char name[16];
for (size_t i = 0; i < NELEMS(names); i++) {
size_t len = strlen(names[i][0]);
if (strncmp(machine, names[i][0], len))
continue;
/* uname does not tell: armv7b is big endian, mips is either */
strlcpy(name, names[i][1], sizeof(name));
if (!strcmp(name, "arm") && machine[strlen(machine) - 1] == 'b')
strlcat(name, "-be", sizeof(name));
#if __BYTE_ORDER == __LITTLE_ENDIAN
if (!strncmp(name, "mips", 4))
strlcat(name, "-le", sizeof(name));
#endif
return name;
}
return NULL;
}
/*
* The % specifiers of tmpfiles.d(5). tmpfiles runs as root, so the
* user ones are fixed. Not here are those for the OS release and the
* architecture. Returns NULL for one that is unknown or has no value.
* user ones are fixed. Returns NULL for one that is unknown or has
* no value.
*/
static const char *specifier(char c, char *buf, size_t len)
{
@@ -994,6 +1064,22 @@ static const char *specifier(char c, char *buf, size_t len)
return NULL;
strlcpy(buf, uts.release, len);
break;
case 'a':
if (uname(&uts))
return NULL;
return specifier_arch(uts.machine);
case 'A':
return specifier_os("IMAGE_VERSION", buf, len);
case 'B':
return specifier_os("BUILD_ID", buf, len);
case 'M':
return specifier_os("IMAGE_ID", buf, len);
case 'o':
return specifier_os("ID", buf, len);
case 'w':
return specifier_os("VERSION_ID", buf, len);
case 'W':
return specifier_os("VARIANT_ID", buf, len);
default:
return NULL;
}
+13 -1
View File
@@ -10,14 +10,23 @@ CONF=/tmp/specifier.conf
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
run "rm -rf $DIR $CONF /etc/os-release"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "cat > /etc/os-release" <<OS
NAME="Finit Test"
ID=finit-test
VERSION_ID="1.2"
OS
run "cat > $CONF" <<CONF
d $DIR
f $DIR/os-%o-%w
f $DIR/arch-%a
f $DIR/variant-%W
f $DIR/host-%H
f $DIR/kernel-%v
f $DIR/machine-%m
@@ -38,3 +47,6 @@ assert "Boot ID" "$(texec ls $DIR/boot-"$boot")" = "$D
assert "Percent" "$(texec ls $DIR/100%)" = "$DIR/100%"
assert "In the argument" "$(texec cat $DIR/arg)" = "/tmp /var/tmp /run /var/lib /var/cache /var/log /root root 0 root 0 ${host%%.*}"
assert "Skipped: a lone % and an unknown specifier" "$(texec sh -c "ls $DIR | grep -c -e end -e unknown")" = "0"
assert "OS ID and version" "$(texec ls $DIR/os-finit-test-1.2)" = "$DIR/os-finit-test-1.2"
assert "Architecture" "$(texec sh -c "ls $DIR | grep -c '^arch-.'")" = "1"
assert "Empty when not in os-release" "$(texec ls $DIR/variant-)" = "$DIR/variant-"