From bc2f2ba4bb19d423dedd40bf9c7a4bef43b172f5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 20 Nov 2026 16:43:52 +0100 Subject: [PATCH] tmpfiles: age 0 cleans everything An entry with the age 0 was read as having no age, so nothing in its directory was ever cleaned: d /var/spool/work - - - 0 tmpfiles --clean t.conf In tmpfiles.d(5) age 0 means everything in the directory is removed on each clean. parse_age() returned 0 for both. It now returns -1 for no age and for one it cannot read, and with age 0 the timestamps are not looked at. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 3 +++ src/tmpfiles.c | 27 +++++++++++++++------------ test/tmpfiles-clean.sh | 11 +++++++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index af206187..8b8f40c5 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -263,6 +263,9 @@ All relevant changes are documented in this file. file or in the files after it. Such an entry is now skipped with a warning - Fix `tmpfiles --clean` cleaning other file systems mounted below the directory of an entry, it now stays on the file system of the directory +- Fix age `0` in a tmpfiles entry, it was read as no age. As in + tmpfiles.d(5), `tmpfiles --clean` now empties the directory of such an + entry - 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 71cc7d3d..0dda8818 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -270,7 +270,7 @@ static void write_arg(FILE *fp, char *arg) /** * Parse age string and return age in seconds. - * Returns 0 on invalid/empty age ("-" or NULL). + * Returns -1 on invalid/empty age ("-" or NULL). * Examples: "10d" = 864000, "1w" = 604800, "2h" = 7200 */ static time_t parse_age(const char *age) @@ -278,13 +278,13 @@ static time_t parse_age(const char *age) time_t val; char *end; - if (!age || !strcmp(age, "-") || !strcmp(age, "0")) - return 0; + if (!age || !strcmp(age, "-")) + return -1; errno = 0; val = strtoul(age, &end, 10); if (errno || end == age) - return 0; + return -1; switch (*end) { case 'w': @@ -303,7 +303,7 @@ static time_t parse_age(const char *age) case '\0': break; default: - return 0; /* unknown suffix */ + return -1; /* unknown suffix */ } return val; @@ -414,15 +414,18 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct * - Files: keep if ANY of atime, ctime, mtime is recent * - Directories: keep if ANY of atime, mtime is recent (ctime * excluded because cleanup itself updates directory ctime) + * - Age 0 removes everything, whatever the timestamps */ - if (clean_now - sb->st_mtime < clean_age) - return 0; /* mtime is too new, keep it */ + if (clean_age) { + if (clean_now - sb->st_mtime < clean_age) + return 0; /* mtime is too new, keep it */ - if (clean_now - sb->st_atime < clean_age) - return 0; /* atime is too new, keep it */ + if (clean_now - sb->st_atime < clean_age) + return 0; /* atime is too new, keep it */ - if (!is_dir && clean_now - sb->st_ctime < clean_age) - return 0; /* ctime is too new, keep it (files only) */ + if (!is_dir && clean_now - sb->st_ctime < clean_age) + return 0; /* ctime is too new, keep it (files only) */ + } if (kept(fpath)) return 0; @@ -874,7 +877,7 @@ static void tmpfiles(char *line) time_t max_age = parse_age(age); /* Only process if age is specified */ - if (max_age > 0) { + if (max_age >= 0) { switch (type[0]) { case 'd': case 'D': diff --git a/test/tmpfiles-clean.sh b/test/tmpfiles-clean.sh index 4b4692e4..f5505f53 100755 --- a/test/tmpfiles-clean.sh +++ b/test/tmpfiles-clean.sh @@ -53,3 +53,14 @@ CONF assert_left $DIR/keep "all dir " assert_left $DIR/keep/all "file " assert_left $DIR/keep/dir "" + +sep +say 'Age 0 removes everything, no age nothing' +run "mkdir -p $DIR/zero $DIR/none; touch $DIR/zero/file $DIR/none/file" +clean <