tmpfiles: let %T and %V follow $TMPDIR

The %T and %V specifiers are always /tmp and /var/tmp.  In systemd
both are the first of $TMPDIR, $TEMP, and $TMP that is set to an
absolute path of a directory, and only otherwise /tmp and /var/tmp.

Do the same.  At boot none of them are set, so nothing changes there.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:03 +01:00
1 parent c349d564d4
commit 9ea64e5c3c
4 files changed
+29 -4

No files matched your search

+2
View File
@@ -38,6 +38,8 @@ All relevant changes are documented in this file.
each below a line with its name each below a line with its name
- tmpfiles: add `--graceful`, for scripts that give it. It changes - tmpfiles: add `--graceful`, for scripts that give it. It changes
nothing, an unknown user or group is never an error in Finit nothing, an unknown user or group is never an error in Finit
- tmpfiles: the `%T` and `%V` specifiers now follow `$TMPDIR`, `$TEMP`,
or `$TMP`, when set to a directory
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below - tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
it. `--prefix` and `--exclude-prefix` match whole path components, it. `--prefix` and `--exclude-prefix` match whole path components,
`--exclude-prefix=/dev` no longer skips `/devices` `--exclude-prefix=/dev` no longer skips `/devices`
-2
View File
@@ -35,6 +35,4 @@ Missing options: `--root`, `--replace`, and `--purge`.
left as it is, with a warning left as it is, with a warning
* An `x` or `X` entry with an age should have what it names cleaned * An `x` or `X` entry with an age should have what it names cleaned
by that age, the age is not read by that age, the age is not read
* The `%T` and `%V` specifiers are always `/tmp` and `/var/tmp`, they
do not look at `$TMPDIR`
* `mksubsys()` in `src/util.c` is unused * `mksubsys()` in `src/util.c` is unused
+17 -2
View File
@@ -1297,6 +1297,21 @@ static const char *specifier_arch(const char *machine)
return NULL; return NULL;
} }
/* As in systemd, the first of these that names a directory */
static const char *specifier_tmp(const char *fallback)
{
static const char *env[] = { "TMPDIR", "TEMP", "TMP" };
for (size_t i = 0; i < NELEMS(env); i++) {
const char *dir = getenv(env[i]);
if (dir && dir[0] == '/' && fisdir(dir))
return dir;
}
return fallback;
}
/* /*
* The % specifiers of tmpfiles.d(5). tmpfiles runs as root, so the * The % specifiers of tmpfiles.d(5). tmpfiles runs as root, so the
* user ones are fixed. Returns NULL for one that is unknown or has * user ones are fixed. Returns NULL for one that is unknown or has
@@ -1315,10 +1330,10 @@ static const char *specifier(char c, char *buf, size_t len)
case 'L': return "/var/log"; case 'L': return "/var/log";
case 'S': return "/var/lib"; case 'S': return "/var/lib";
case 't': return "/run"; case 't': return "/run";
case 'T': return "/tmp"; case 'T': return specifier_tmp("/tmp");
case 'u': return "root"; case 'u': return "root";
case 'U': return "0"; case 'U': return "0";
case 'V': return "/var/tmp"; case 'V': return specifier_tmp("/var/tmp");
case 'b': case 'b':
specifier_file("/proc/sys/kernel/random/boot_id", "-", buf, len); specifier_file("/proc/sys/kernel/random/boot_id", "-", buf, len);
break; break;
+10
View File
@@ -50,3 +50,13 @@ assert "Skipped: a lone % and an unknown specifier" "$(texec sh -c "ls $DIR | gr
assert "OS ID and version" "$(texec ls $DIR/os-finit-test-1.2)" = "$DIR/os-finit-test-1.2" 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 "Architecture" "$(texec sh -c "ls $DIR | grep -c '^arch-.'")" = "1"
assert "Empty when not in os-release" "$(texec ls $DIR/variant-)" = "$DIR/variant-" assert "Empty when not in os-release" "$(texec ls $DIR/variant-)" = "$DIR/variant-"
sep
say "%T and %V follow \$TMPDIR when it is a directory"
run "cat > $CONF" <<CONF
f $DIR/tmpdir - - - - %T %V
CONF
run "TMPDIR=$DIR /libexec/finit/tmpfiles --create $CONF"
assert "From the environment" "$(texec cat $DIR/tmpdir)" = "$DIR $DIR"
run "rm $DIR/tmpdir; TMPDIR=/no/such/dir /libexec/finit/tmpfiles --create $CONF"
assert "Not a directory" "$(texec cat $DIR/tmpdir)" = "/tmp /var/tmp"