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

+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;
}