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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:03 +01:00
1 parent 9ea64e5c3c
commit 9160ca3ddf
4 files changed
+33 -8

No files matched your search

+3
View File
@@ -40,6 +40,9 @@ All relevant changes are documented in this file.
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`, - tmpfiles: the `%T` and `%V` specifiers now follow `$TMPDIR`, `$TEMP`,
or `$TMP`, when set to a directory 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 - 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
@@ -33,6 +33,4 @@ Missing options: `--root`, `--replace`, and `--purge`.
in `/etc` should run before `50-y.conf` in `/usr/lib` 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 * `a` and `A` handle an ACL of at most 32 entries, a path with more is
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
by that age, the age is not read
* `mksubsys()` in `src/util.c` is unused * `mksubsys()` in `src/util.c` is unused
+17 -6
View File
@@ -596,6 +596,7 @@ struct keep {
static struct keep *keeps; static struct keep *keeps;
static int num_keeps; static int num_keeps;
static int collect; /* Pass that only gathers the above */ 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) 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++) { for (int i = 0; i < num_keeps; i++) {
struct keep *k = &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))) if (!fnmatch(k->pattern, path, FNM_PATHNAME | (k->self ? 0 : FNM_LEADING_DIR)))
return 1; return 1;
} }
@@ -1612,7 +1615,7 @@ static void tmpfiles(char *line)
if (remove_flag && strchr("DrR", type[0])) if (remove_flag && strchr("DrR", type[0]))
printf("remove %s %s\n", type, path); 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); printf("clean %s %s, older than %s\n", type, path, age);
if (create_flag && !strchr("rRxX", type[0])) if (create_flag && !strchr("rRxX", type[0]))
printf("create %s %s%s%s\n", type, path, arg && !b64 ? " " : "", arg && !b64 ? arg : ""); 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 */ /* 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); clean_now = time(NULL);
/* Only e takes a glob, and nothing is older than the epoch */ /* Only e, x, and X take a glob, and nothing is older than the epoch */
if (clean_age.sec > clean_now) 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); glob_do(path, clean_dir);
else keep_own = NULL;
} else if (type[0] == 'e') {
glob_do(path, clean_dir);
} else {
clean_dir(path); clean_dir(path);
}
} }
// file & directory creation logic // file & directory creation logic
+13
View File
@@ -102,3 +102,16 @@ CONF
run "chmod 600 $DIR/by-all/file $DIR/by-m/file; /libexec/finit/tmpfiles --clean $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-all "file "
assert_left $DIR/by-m "" 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 <<CONF
d $DIR/own - - - 1s
x $DIR/own/keep - - - 1h
x $DIR/own/lon* - - - 1s
CONF
assert_left $DIR/own "keep long "
assert_left $DIR/own/keep "file "
assert_left $DIR/own/long ""