diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index bc238ba2..3703ccb5 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -15,6 +15,12 @@ All relevant changes are documented in this file. skipped unless the new `--boot` option is given. Finit gives it at boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect 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 `%` + 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 `v`, `q`, and `Q` entries now make a plain directory, like `d`. Finit has no support for btrfs subvolumes, and before the entries were skipped as unsupported diff --git a/doc/index.md b/doc/index.md index d3704eff..d41dbb8d 100644 --- a/doc/index.md +++ b/doc/index.md @@ -28,7 +28,7 @@ Features * [Conditions](conditions.md) for network/process/custom dependencies * [Readiness notification](config/service-sync.md); PID files (native) for synchronizing system startup, support for systemd [sd_notify()][], or [s6 style][] too - * Limited support for [tmpfiles.d(5)][] (no attributes, subvolumes, or specifiers) + * Limited support for [tmpfiles.d(5)][] (no attributes or subvolumes) * [Pre/Post script actions](config/service-wrappers.md) * Rudimentary [templating support](config/templating.md) * [Tooling](initctl.md) to enable/disable services diff --git a/doc/plugins.md b/doc/plugins.md index 7edb585d..a59044c3 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -23,9 +23,10 @@ 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, subvolumes, or - specifiers. 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, 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. 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 861b7d47..4b2676cb 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -24,11 +24,10 @@ user to call it from cron. 2. Specifiers ------------- -A path with `%` in it is skipped, also the escaped `%%`. The ones that -show up in files from distributions are `%b` (boot ID), `%m` (machine -ID), `%H` (hostname), `%v` (kernel release), and for the argument `%T` -and `%V`. This is the largest gap for using such files unchanged, and -the most work. +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`. 3. Remaining types and modifiers @@ -64,4 +63,7 @@ their own paths. * A `d` or `e` entry for a path that is a link to a directory warns that mode and owner could not be set * An age so large that it overflows is not caught +* 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 * `mksubsys()` in `src/util.c` is unused diff --git a/man/finit.8 b/man/finit.8 index de8f653e..173acf16 100644 --- a/man/finit.8 +++ b/man/finit.8 @@ -94,7 +94,7 @@ and s6 notification is also supported. .It Limited support for .Xr tmpfiles.d 5 , -no attributes, subvolumes, or specifiers +no attributes or subvolumes .It Pre/Post script actions .It diff --git a/src/tmpfiles.c b/src/tmpfiles.c index f40f5ecf..5ddaaf7e 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -847,6 +848,102 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify) return rc; } +/* First line of @file, without @strip characters, empty when it cannot be read */ +static char *specifier_file(const char *file, const char *strip, char *buf, size_t len) +{ + size_t i = 0; + FILE *fp; + int c; + + fp = fopen(file, "r"); + if (fp) { + while ((c = fgetc(fp)) != EOF && c != '\n' && i < len - 1) { + if (!strchr(strip, c)) + buf[i++] = c; + } + fclose(fp); + } + buf[i] = 0; + + return buf; +} + +/* + * 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. + */ +static const char *specifier(char c, char *buf, size_t len) +{ + struct utsname uts; + + switch (c) { + case '%': return "%"; + case 'C': return "/var/cache"; + case 'g': return "root"; + case 'G': return "0"; + case 'h': return "/root"; + case 'L': return "/var/log"; + case 'S': return "/var/lib"; + case 't': return "/run"; + case 'T': return "/tmp"; + case 'u': return "root"; + case 'U': return "0"; + case 'V': return "/var/tmp"; + case 'b': + specifier_file("/proc/sys/kernel/random/boot_id", "-", buf, len); + break; + case 'm': + specifier_file("/etc/machine-id", "", buf, len); + break; + case 'H': + case 'l': + if (gethostname(buf, len)) + return NULL; + buf[len - 1] = 0; + if (c == 'l') + buf[strcspn(buf, ".")] = 0; + break; + case 'v': + if (uname(&uts)) + return NULL; + strlcpy(buf, uts.release, len); + break; + default: + return NULL; + } + + return buf[0] ? buf : NULL; +} + +/* Expand the specifiers in @str to @buf, NULL when one fails or @buf is too small */ +static char *expand(const char *str, char *buf, size_t len) +{ + char val[256]; + size_t n = 0; + + for (; *str; str++) { + const char *ptr = str; + size_t num = 1; + + if (*str == '%') { + /* Also for a % that ends the string, do not step past it */ + ptr = specifier(*++str, val, sizeof(val)); + if (!ptr) + return NULL; + num = strlen(ptr); + } + + if (n + num >= len) + return NULL; + memcpy(&buf[n], ptr, num); + n += num; + } + buf[n] = 0; + + return buf; +} + /* * The configuration format is one line per path, containing type, path, * mode, ownership, age, and argument fields. The lines are separated by @@ -868,7 +965,7 @@ static void tmpfiles(char *line) struct perm perm = { 0 }; int plus = 0, boot = 0; FILE *fp = NULL; - char buf[1024]; + char buf[1024], pathbuf[PATH_MAX], argbuf[1024]; glob_t gl; type = strtok(line, "\t "); @@ -892,9 +989,14 @@ static void tmpfiles(char *line) if (!path) return; if (strchr(path, '%')) { - if (!quiet) - warnx("Path name specifiers unsupported, skipping %s", path); - return; + char *ptr = expand(path, pathbuf, sizeof(pathbuf)); + + if (!ptr) { + if (!quiet) + warnx("Unsupported specifier or too long, skipping %s", path); + return; + } + path = ptr; } if (excluded(path)) return; @@ -955,6 +1057,14 @@ static void tmpfiles(char *line) if (!arg[0] || !strcmp(arg, "-")) arg = NULL; } + if (arg && strchr(arg, '%') && strchr("CfFlLw", type[0])) { + arg = expand(arg, argbuf, sizeof(argbuf)); + if (!arg) { + if (!quiet) + warnx("Unsupported specifier in argument or too long, skipping %s", path); + return; + } + } strc = stat(path, &st); diff --git a/test/Makefile.am b/test/Makefile.am index a1a1f66a..d6b0e097 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -87,6 +87,7 @@ EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-boot.sh EXTRA_DIST += tmpfiles-clean.sh EXTRA_DIST += tmpfiles-order.sh +EXTRA_DIST += tmpfiles-specifier.sh EXTRA_DIST += tmpfiles-unsupported.sh EXTRA_DIST += unexpected-restart.sh EXTRA_DIST += dbus-auth.sh @@ -171,6 +172,7 @@ TESTS += tmpfiles-perm.sh TESTS += tmpfiles-boot.sh TESTS += tmpfiles-clean.sh TESTS += tmpfiles-order.sh +TESTS += tmpfiles-specifier.sh TESTS += tmpfiles-unsupported.sh endif TESTS += unexpected-restart.sh diff --git a/test/tmpfiles-specifier.sh b/test/tmpfiles-specifier.sh new file mode 100755 index 00000000..3b29a596 --- /dev/null +++ b/test/tmpfiles-specifier.sh @@ -0,0 +1,40 @@ +#!/bin/sh +# Verify the % specifiers of tmpfiles.d(5), in the path and the argument +set -eu + +TEST_DIR=$(dirname "$0") + +DIR=/tmp/specifier +CONF=/tmp/specifier.conf + +test_teardown() +{ + say "Running test teardown." + run "rm -rf $DIR $CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "cat > $CONF" < $CONF" <