mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: add the % specifiers for path and argument
An entry with a % in the path was skipped, and files from
distributions have them:
d /run/log/journal/%m 2755 root systemd-journal - -
Expand the specifiers that do not need the OS release or the
architecture: %b, %m, %H, %l, %v, the directories %t, %T, %V, %S, %C,
%L, %h, and the user ones %u, %U, %g, %G. tmpfiles runs as root, so
the last ones are fixed. An entry with any other specifier is still
skipped.
The argument of C, f, F, L, and w entries is expanded too, as in
tmpfiles.d(5). A literal % in such an argument must from now on be
written %%.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
9 files changed
+176
-15
No files matched your search
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+4
-3
@@ -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
|
||||
|
||||
@@ -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
|
||||
+1
-1
@@ -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
|
||||
|
||||
+114
-4
@@ -34,6 +34,7 @@
|
||||
#include <grp.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Executable
+40
@@ -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
|
||||
d $DIR
|
||||
f $DIR/host-%H
|
||||
f $DIR/kernel-%v
|
||||
f $DIR/machine-%m
|
||||
f $DIR/boot-%b
|
||||
f $DIR/100%%
|
||||
f $DIR/end%
|
||||
f $DIR/unknown - - - - %!
|
||||
f $DIR/arg - - - - %T %V %t %S %C %L %h %u %U %g %G %l
|
||||
CONF
|
||||
run "/libexec/finit/tmpfiles --create $CONF"
|
||||
|
||||
host=$(texec hostname)
|
||||
assert "Hostname" "$(texec ls $DIR/host-"$host")" = "$DIR/host-$host"
|
||||
assert "Kernel release" "$(texec ls $DIR/kernel-"$(texec uname -r)")" = "$DIR/kernel-$(texec uname -r)"
|
||||
assert "Machine ID" "$(texec ls $DIR/machine-"$(texec cat /etc/machine-id)")" = "$DIR/machine-$(texec cat /etc/machine-id)"
|
||||
boot=$(texec cat /proc/sys/kernel/random/boot_id | tr -d -)
|
||||
assert "Boot ID" "$(texec ls $DIR/boot-"$boot")" = "$DIR/boot-$boot"
|
||||
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"
|
||||
@@ -20,7 +20,7 @@ test_teardown()
|
||||
run "cat > $CONF" <<CONF
|
||||
d $DIR
|
||||
h $DIR - - - - +i
|
||||
f $DIR/%m
|
||||
f $DIR/%!
|
||||
f $DIR/after
|
||||
CONF
|
||||
|
||||
|
||||
Reference in new issue
Block a user