From ddb4f18fa94c358279376a209bf9b5aa6a611e4b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 20 Nov 2026 18:56:50 +0100 Subject: [PATCH] tmpfiles: add --dry-run, to show what would be run There was no way to see what the tmpfiles.d files on a system amount to, short of running them: tmpfiles --dry-run --create --remove --boot With -n or --dry-run each entry that would be run is printed, with what would be done, remove, clean, or create, in the order it would be. Nothing is changed. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 2 ++ doc/plugins.md | 5 +++++ doc/tmpfiles-todo.md | 9 +++++---- src/tmpfiles.c | 25 ++++++++++++++++++++++++- test/tmpfiles-order.sh | 11 +++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 6132c5fe..885262b8 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -22,6 +22,8 @@ All relevant changes are documented in this file. `%w`, `%W`, `%B`, `%M`, and `%A`. 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: add `-n`, `--dry-run`, to show the entries that would be run, + in the order they would be, without doing anything - tmpfiles: add the `t` and `T` entries, to set extended attributes on a path, `T` also on everything below it - tmpfiles: add the `h` and `H` entries, to set file attributes as with diff --git a/doc/plugins.md b/doc/plugins.md index bd3e12b4..083f8766 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -31,6 +31,11 @@ For your convenience a set of *optional* plugins are available: files distributed with Finit. It is read first but can be overridden by any of the standard tmpfiles.d directories, e.g. `/etc/tmpfiles.d`. + To see what the files on a system amount to, without anything being + done, add `--dry-run`: + + /libexec/finit/tmpfiles --dry-run --create --remove --boot + The age of an entry, e.g. `10d` below, is used only by `tmpfiles --clean`, which Finit never runs on its own: diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 8d8fab3a..4e733b0b 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -13,16 +13,17 @@ files or common packages already rely on, last what few will miss. * `C+`, which is read as `C` * `z` and `Z` only set the SELinux label when `restorecon` is installed -* Modifiers `-` (do not fail), `=` (remove what is of another type), - `~` (base64 argument), and `^` (credential as argument) +* Modifiers `=` (remove what is of another type), `~` (base64 + argument), and `^` (credential as argument). `-` (do not fail) has + nothing to do, tmpfiles never exits with an error for an entry * Quoted paths, a path with a space in it cannot be written 2. Command line --------------- -Missing options: `--root`, `--replace`, `--dry-run`, `--purge`, -`--graceful`, and `--cat-config`. +Missing options: `--root`, `--replace`, `--purge`, `--graceful`, and +`--cat-config`. 3. Smaller things diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 9b098153..e228dba1 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -61,6 +61,7 @@ int create_flag = 0; int clean_flag = 0; int remove_flag = 0; int boot_flag = 0; +int dry_run = 0; /* Set for the first of two passes, the second one does the complaining */ static int quiet; @@ -1469,6 +1470,22 @@ static void tmpfiles(char *line) } } + if (dry_run) { + if (!strchr("aAbcCdDefFhHlLprRtTwxXzZ", type[0])) { + if (!quiet) + warnx("Unsupported tmpfiles command '%s', skipping %s", type, path); + return; + } + + if (remove_flag && strchr("DrR", type[0])) + printf("remove %s %s\n", type, path); + if (clean_flag && strchr("CdDe", type[0]) && !parse_age(age, &clean_age)) + printf("clean %s %s, older than %s\n", type, path, age); + if (create_flag && !strchr("rRxX", type[0])) + printf("create %s %s%s%s\n", type, path, arg ? " " : "", arg ?: ""); + return; + } + strc = stat(path, &st); // file and directory removal logic @@ -1878,6 +1895,7 @@ static int usage(int rc) " -C, --clean Clean files and directories based on age\n" " -c, --create Create files and directories\n" " -d, --debug Show developer debug messages\n" + " -n, --dry-run Only show the entries that would be run\n" " -E Exclude /dev, /proc, /run, and /sys\n" " --exclude-prefix=PFX Ignore rules for paths starting with PFX\n" " --prefix=PFX Only apply rules for paths starting with PFX\n" @@ -1904,6 +1922,7 @@ int main(int argc, char *argv[]) { "clean", 0, NULL, 'C' }, { "create", 0, NULL, 'c' }, { "debug", 0, NULL, 'd' }, + { "dry-run", 0, NULL, 'n' }, { "exclude-prefix", 1, NULL, OPT_EXCLUDE_PREFIX }, { "prefix", 1, NULL, OPT_PREFIX }, { "remove", 0, NULL, 'r' }, @@ -1913,7 +1932,7 @@ int main(int argc, char *argv[]) int c; - while ((c = getopt_long(argc, argv, "CcdrEh?", long_options, NULL)) != EOF) { + while ((c = getopt_long(argc, argv, "CcdnrEh?", long_options, NULL)) != EOF) { switch(c) { case 'C': clean_flag = 1; @@ -1927,6 +1946,10 @@ int main(int argc, char *argv[]) debug = 1; break; + case 'n': + dry_run = 1; + break; + case 'E': add_exclude_prefix("/dev"); add_exclude_prefix("/proc"); diff --git a/test/tmpfiles-order.sh b/test/tmpfiles-order.sh index b1155616..4615bf27 100755 --- a/test/tmpfiles-order.sh +++ b/test/tmpfiles-order.sh @@ -25,3 +25,14 @@ run "mkdir $DIR; echo old > $DIR/file" run "/libexec/finit/tmpfiles --create --remove $CONF" assert "The old file is removed, a new one created" "$(texec cat $DIR/file)" = "new" + +sep +say 'With --dry-run it only tells what it would do, in that order' +run "cat > $CONF" <