diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index dc350443..af07333c 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -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 diff --git a/doc/plugins.md b/doc/plugins.md index fe2e1a22..d30c67ca 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -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 diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index e3bf0619..f1b95bd6 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 5418c4e2..ed70907a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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) diff --git a/src/tmpfiles.c b/src/tmpfiles.c index d2a07f8b..0f279188 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -49,6 +49,7 @@ # include #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; } diff --git a/test/tmpfiles-specifier.sh b/test/tmpfiles-specifier.sh index 3b29a596..e4715a2c 100755 --- a/test/tmpfiles-specifier.sh +++ b/test/tmpfiles-specifier.sh @@ -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" < $CONF" <