From 9195d145d1679beeea54e14af2c4d29042663c08 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 20 Nov 2026 16:07:14 +0100 Subject: [PATCH] tmpfiles: remove before creating when asked to do both With both --create and --remove each entry was removed and then created before the next one was read. An r entry took what an entry above it had just created: f /tmp/file - - - - new r /tmp/file tmpfiles --create --remove t.conf This left no /tmp/file. The same goes for --clean. Run all the files twice in that case, first to remove and clean, then to create. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 ++ src/tmpfiles.c | 114 +++++++++++++++++++++++------------------ test/Makefile.am | 2 + test/tmpfiles-order.sh | 27 ++++++++++ 4 files changed, 95 insertions(+), 51 deletions(-) create mode 100755 test/tmpfiles-order.sh diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index d70ad773..6384ba4c 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -249,6 +249,9 @@ All relevant changes are documented in this file. - Fix the argument of a tmpfiles entry written in aligned columns, the space after the age field ended up in the argument, e.g. in the target of an `L` link. A lone `-` is now read as no argument +- Fix `tmpfiles --create --remove`, each entry was removed and created in + turn, so an `r` entry removed what an entry above it had just created. + All of the removing is now done first - Fix a lost reload for a service paused mid-reload by a condition in flux. A second reload arriving meanwhile re-parsed the unchanged .conf and cleared the pending mark, so the service resumed without ever being diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 5efdf7f1..ab2508b4 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -1073,6 +1073,62 @@ static void process_file(const char *fn) fclose(fp); } +static void process(int num, char *file[]) +{ + /* If config files specified on command line, process only those */ + if (num) { + for (int i = 0; i < num; i++) + process_file(file[i]); + return; + } + + /* + * No config files specified, process standard tmpfiles.d directories. + * + * Only the three last tmpfiles.d/ directories are defined in + * tmpfiles.d(5) as system search paths. Finit adds two more + * before that to have Finit specific ones sorted first, and + * a configure prefix specific one after that for user needs. + */ + + /* in priority order */ + char *dir[] = { + FINIT_TMPFILES "/*.conf", + TMPFILES_PATH_ "/*.conf", + "/usr/lib/tmpfiles.d/*.conf", + "/run/tmpfiles.d/*.conf", + "/etc/tmpfiles.d/*.conf", /* local admin overrides */ + }; + int flags = GLOB_NOESCAPE; + glob_t gl; + size_t i; + + for (i = 0; i < NELEMS(dir); i++) { + glob(dir[i], flags, NULL, &gl); + flags |= GLOB_APPEND; + } + + for (i = 0; i < gl.gl_pathc; i++) { + char *fn = gl.gl_pathv[i]; + size_t j; + + /* check for overrides */ + for (j = i + 1; j < gl.gl_pathc; j++) { + if (strcmp(basenm(fn), basenm(gl.gl_pathv[j]))) + continue; + fn = NULL; + break; + } + + if (!fn) + continue; /* skip, override exists */ + + process_file(fn); + } + + globfree(&gl); +} + static int usage(int rc) { fprintf(stderr, @@ -1155,58 +1211,14 @@ int main(int argc, char *argv[]) return 1; } - /* If config files specified on command line, process only those */ - if (optind < argc) { - for (int i = optind; i < argc; i++) - process_file(argv[i]); - return 0; + /* Remove and clean first, or an r entry takes what an f entry above it made */ + if (create_flag && (clean_flag || remove_flag)) { + create_flag = 0; + process(argc - optind, &argv[optind]); + create_flag = 1; + clean_flag = remove_flag = 0; } - - /* - * No config files specified, process standard tmpfiles.d directories. - * - * Only the three last tmpfiles.d/ directories are defined in - * tmpfiles.d(5) as system search paths. Finit adds two more - * before that to have Finit specific ones sorted first, and - * a configure prefix specific one after that for user needs. - */ - - /* in priority order */ - char *dir[] = { - FINIT_TMPFILES "/*.conf", - TMPFILES_PATH_ "/*.conf", - "/usr/lib/tmpfiles.d/*.conf", - "/run/tmpfiles.d/*.conf", - "/etc/tmpfiles.d/*.conf", /* local admin overrides */ - }; - int flags = GLOB_NOESCAPE; - glob_t gl; - size_t i; - - for (i = 0; i < NELEMS(dir); i++) { - glob(dir[i], flags, NULL, &gl); - flags |= GLOB_APPEND; - } - - for (i = 0; i < gl.gl_pathc; i++) { - char *fn = gl.gl_pathv[i]; - size_t j; - - /* check for overrides */ - for (j = i + 1; j < gl.gl_pathc; j++) { - if (strcmp(basenm(fn), basenm(gl.gl_pathv[j]))) - continue; - fn = NULL; - break; - } - - if (!fn) - continue; /* skip, override exists */ - - process_file(fn); - } - - globfree(&gl); + process(argc - optind, &argv[optind]); return 0; } diff --git a/test/Makefile.am b/test/Makefile.am index 22b34ad3..1ef2a00a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -84,6 +84,7 @@ EXTRA_DIST += testserv.sh EXTRA_DIST += tmpfiles-acl.sh EXTRA_DIST += tmpfiles-arg.sh EXTRA_DIST += tmpfiles-perm.sh +EXTRA_DIST += tmpfiles-order.sh EXTRA_DIST += unexpected-restart.sh EXTRA_DIST += dbus-auth.sh EXTRA_DIST += dbus-authz.sh @@ -164,6 +165,7 @@ TESTS += testserv.sh TESTS += tmpfiles-acl.sh TESTS += tmpfiles-arg.sh TESTS += tmpfiles-perm.sh +TESTS += tmpfiles-order.sh endif TESTS += unexpected-restart.sh if DBUS diff --git a/test/tmpfiles-order.sh b/test/tmpfiles-order.sh new file mode 100755 index 00000000..b1155616 --- /dev/null +++ b/test/tmpfiles-order.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# Verify that tmpfiles removes before it creates when asked to do both +set -eu + +TEST_DIR=$(dirname "$0") + +DIR=/tmp/order +CONF=/tmp/order.conf + +test_teardown() +{ + say "Running test teardown." + run "rm -rf $DIR $CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "cat > $CONF" < $DIR/file" +run "/libexec/finit/tmpfiles --create --remove $CONF" + +assert "The old file is removed, a new one created" "$(texec cat $DIR/file)" = "new"