From 9ea64e5c3c8ba977ffe1110119f35d8371f94d07 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 10:14:19 +0100 Subject: [PATCH] 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 --- doc/ChangeLog.md | 2 ++ doc/tmpfiles-todo.md | 2 -- src/tmpfiles.c | 19 +++++++++++++++++-- test/tmpfiles-specifier.sh | 10 ++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index ea3ffc21..f103ba6c 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -38,6 +38,8 @@ All relevant changes are documented in this file. each below a line with its name - tmpfiles: add `--graceful`, for scripts that give it. It changes 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 it. `--prefix` and `--exclude-prefix` match whole path components, `--exclude-prefix=/dev` no longer skips `/devices` diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 03a3fd80..e39f7543 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -35,6 +35,4 @@ Missing options: `--root`, `--replace`, and `--purge`. left as it is, with a warning * An `x` or `X` entry with an age should have what it names cleaned 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 diff --git a/src/tmpfiles.c b/src/tmpfiles.c index d0ebbf79..a7c135b6 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -1297,6 +1297,21 @@ static const char *specifier_arch(const char *machine) 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 * 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 'S': return "/var/lib"; case 't': return "/run"; - case 'T': return "/tmp"; + case 'T': return specifier_tmp("/tmp"); case 'u': return "root"; case 'U': return "0"; - case 'V': return "/var/tmp"; + case 'V': return specifier_tmp("/var/tmp"); case 'b': specifier_file("/proc/sys/kernel/random/boot_id", "-", buf, len); break; diff --git a/test/tmpfiles-specifier.sh b/test/tmpfiles-specifier.sh index e4715a2c..eefcebe5 100755 --- a/test/tmpfiles-specifier.sh +++ b/test/tmpfiles-specifier.sh @@ -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 "Architecture" "$(texec sh -c "ls $DIR | grep -c '^arch-.'")" = "1" 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" <