From 9160ca3ddfd96941313a071a81fb565b9648f558 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 10:14:44 +0100 Subject: [PATCH] tmpfiles: clean what an x entry keeps by the age of the entry An x or X entry can have an age of its own in tmpfiles.d(5). What it keeps from the cleaning of the directory above is then cleaned by that age instead: d /tmp 1777 root root 10d x /tmp/build - - - 30d tmpfiles does not read the age, so /tmp/build is never cleaned. Clean the paths of such an entry by its age, with the entry itself left out of what is kept. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 +++ doc/tmpfiles-todo.md | 2 -- src/tmpfiles.c | 23 +++++++++++++++++------ test/tmpfiles-clean.sh | 13 +++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index f103ba6c..0d4fa799 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -40,6 +40,9 @@ All relevant changes are documented in this file. 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: an `x` or `X` entry with an age now has what it keeps cleaned + by that age, as in `x /tmp/build 30d` below a `/tmp` cleaned after ten + days. Before the age was not read - 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 e39f7543..6c5dbc76 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -33,6 +33,4 @@ Missing options: `--root`, `--replace`, and `--purge`. in `/etc` should run before `50-y.conf` in `/usr/lib` * `a` and `A` handle an ACL of at most 32 entries, a path with more is 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 * `mksubsys()` in `src/util.c` is unused diff --git a/src/tmpfiles.c b/src/tmpfiles.c index a7c135b6..bf87a3f5 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -596,6 +596,7 @@ struct keep { static struct keep *keeps; static int num_keeps; static int collect; /* Pass that only gathers the above */ +static const char *keep_own; /* Entry that cleans what it keeps, by its own age */ static void keep_add(const char *pattern, int self) { @@ -623,6 +624,8 @@ static int kept(const char *path) for (int i = 0; i < num_keeps; i++) { struct keep *k = &keeps[i]; + if (keep_own && !strcmp(k->pattern, keep_own)) + continue; if (!fnmatch(k->pattern, path, FNM_PATHNAME | (k->self ? 0 : FNM_LEADING_DIR))) return 1; } @@ -1612,7 +1615,7 @@ static void tmpfiles(char *line) 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)) + if (clean_flag && strchr("CdDexX", 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 && !b64 ? " " : "", arg && !b64 ? arg : ""); @@ -1670,16 +1673,24 @@ static void tmpfiles(char *line) } /* age-based cleanup logic */ - if (clean_flag && strchr("CdDe", type[0]) && !parse_age(age, &clean_age)) { + if (clean_flag && strchr("CdDexX", type[0]) && !parse_age(age, &clean_age)) { clean_now = time(NULL); - /* Only e takes a glob, and nothing is older than the epoch */ - if (clean_age.sec > clean_now) + /* Only e, x, and X take a glob, and nothing is older than the epoch */ + if (clean_age.sec > clean_now) { ; - else if (type[0] == 'e') + } else if (type[0] == 'x' || type[0] == 'X') { + /* As keep_add() has it */ + for (size_t len = strlen(path); len > 1 && path[len - 1] == '/'; len--) + path[len - 1] = 0; + keep_own = path; glob_do(path, clean_dir); - else + keep_own = NULL; + } else if (type[0] == 'e') { + glob_do(path, clean_dir); + } else { clean_dir(path); + } } // file & directory creation logic diff --git a/test/tmpfiles-clean.sh b/test/tmpfiles-clean.sh index e56ac1be..87afaa43 100755 --- a/test/tmpfiles-clean.sh +++ b/test/tmpfiles-clean.sh @@ -102,3 +102,16 @@ CONF run "chmod 600 $DIR/by-all/file $DIR/by-m/file; /libexec/finit/tmpfiles --clean $CONF" assert_left $DIR/by-all "file " assert_left $DIR/by-m "" + +sep +say 'An x entry with an age of its own has what it keeps cleaned by that age' +run "mkdir -p $DIR/own/keep $DIR/own/long; touch $DIR/own/file $DIR/own/keep/file $DIR/own/long/file" +sleep 2 +clean <