tmpfiles: read all of the age formats in tmpfiles.d(5)

Only one number with one unit was read from the age of an entry.  An
age like this one was taken as two days:

    d /var/tmp 1777 root root 2d12h

What came after the first unit was not looked at.  ms and us were not
known, so 500ms gave no cleaning at all.

Read any number of values with units, us, ms, s, m or min, h, d, and
w, and refuse an age that cannot be read or that overflows.  Add the ~
prefix, which keeps what is right in the directory, and the prefix
that says which timestamps to go by, e.g. "aAmM:" for access and
modification time of files and directories.

Without that prefix the birth time of a file now counts too, as in
tmpfiles.d(5).  It is read with statx(), where the C library has it,
and left out on a file system that has none.

A prefix that names timestamps for files only, or directories only,
leaves the default set for the other.  The age is capped at what a 32
bit time_t holds, and nothing is cleaned when it reaches back past
the epoch.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:03:33 +01:00
1 parent b87d235cf7
commit 9d49eaf4c7
6 files changed
+189 -88

No files matched your search

+139 -67
View File
@@ -268,45 +268,105 @@ static void write_arg(FILE *fp, char *arg)
fputs(arg, fp);
}
/**
* Parse age string and return age in seconds.
* Returns -1 on invalid/empty age ("-" or NULL).
* Examples: "10d" = 864000, "1w" = 604800, "2h" = 7200
/* Timestamps of a file to go by when cleaning, those of a directory are shifted up */
#define AGE_ATIME 0x01
#define AGE_BTIME 0x02
#define AGE_CTIME 0x04
#define AGE_MTIME 0x08
#define AGE_DIR 4
struct age {
time_t sec;
int below; /* ~, keep what is right in the directory */
unsigned by;
};
/*
* Parse the age of an entry, e.g. "10d", "1h30m", "~2w", or "aAmM:1d".
* A number without a unit is in seconds. Returns -1 when there is no
* age, "-" or NULL, or when it cannot be read.
*/
static time_t parse_age(const char *age)
static int parse_age(const char *str, struct age *age)
{
time_t val;
char *end;
const struct { const char *unit; unsigned long long usec; } units[] = {
{ "us", 1ULL },
{ "ms", 1000ULL },
{ "s", 1000000ULL },
{ "", 1000000ULL },
{ "m", 60 * 1000000ULL },
{ "min", 60 * 1000000ULL },
{ "h", 3600 * 1000000ULL },
{ "d", 86400 * 1000000ULL },
{ "w", 7 * 86400 * 1000000ULL },
};
/* The change time of a directory is set by the cleaning itself */
const unsigned file = AGE_ATIME | AGE_BTIME | AGE_CTIME | AGE_MTIME;
const unsigned dir = (AGE_ATIME | AGE_BTIME | AGE_MTIME) << AGE_DIR;
unsigned long long usec = 0;
const char *ptr;
if (!age || !strcmp(age, "-"))
if (!str || !strcmp(str, "-"))
return -1;
errno = 0;
val = strtoul(age, &end, 10);
if (errno || end == age)
return -1;
age->by = 0;
age->below = 0;
switch (*end) {
case 'w':
val *= 7;
/* fallthrough */
case 'd':
val *= 24;
/* fallthrough */
case 'h':
val *= 60;
/* fallthrough */
case 'm':
val *= 60;
/* fallthrough */
case 's':
case '\0':
break;
default:
return -1; /* unknown suffix */
ptr = strchr(str, ':');
if (ptr) {
const char *flags = "abcmABCM";
if (ptr == str)
return -1;
for (; str < ptr; str++) {
const char *flag = strchr(flags, *str);
if (!flag)
return -1;
age->by |= 1 << (flag - flags);
}
str++;
}
return val;
/* Never none at all, that would remove everything */
if (!(age->by & file))
age->by |= file;
if (!(age->by & ~file))
age->by |= dir;
if (*str == '~') {
age->below = 1;
str++;
}
do {
unsigned long long val;
size_t i, len;
char *end;
if (!isdigit((unsigned char)*str))
return -1;
errno = 0;
val = strtoull(str, &end, 10);
if (errno)
return -1;
len = strcspn(end, "0123456789");
for (i = 0; i < NELEMS(units); i++) {
if (strlen(units[i].unit) == len && !strncmp(end, units[i].unit, len))
break;
}
if (i == NELEMS(units) || val > (ULLONG_MAX - usec) / units[i].usec)
return -1;
usec += val * units[i].usec;
str = end + len;
} while (*str);
/* Capped, time_t may be 32 bits. Older than the epoch is never cleaned */
usec = usec / 1000000 + !!(usec % 1000000);
age->sec = usec > INT32_MAX ? INT32_MAX : usec;
return 0;
}
/**
@@ -494,35 +554,52 @@ static int kept(const char *path)
}
/* Globals for do_clean() callback - nftw doesn't support user data */
static time_t clean_age;
static struct age clean_age;
static time_t clean_now;
/* The file system may not have a birth time, then it is not looked at */
static time_t btime(const char *path)
{
#ifdef HAVE_STATX
struct statx stx;
if (statx(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, STATX_BTIME, &stx) || !(stx.stx_mask & STATX_BTIME))
return 0;
return stx.stx_btime.tv_sec;
#else
(void)path;
return 0;
#endif
}
static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftw)
{
int is_dir;
unsigned by;
/* Skip the root directory itself */
if (ftw->level == 0)
return 0;
is_dir = (tflag == FTW_D || tflag == FTW_DP);
if (clean_age.below && ftw->level == 1)
return 0;
/*
* Conservative cleanup matching systemd-tmpfiles behavior:
* - 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_age) {
if (clean_now - sb->st_mtime < clean_age)
return 0; /* mtime is too new, keep it */
/* Kept as long as one of the chosen timestamps is newer than the age */
by = clean_age.by;
if (tflag == FTW_D || tflag == FTW_DP || tflag == FTW_DNR)
by >>= AGE_DIR;
if (clean_age.sec) {
time_t then = clean_now - clean_age.sec;
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 ((by & AGE_MTIME) && sb->st_mtime > then)
return 0;
if ((by & AGE_ATIME) && sb->st_atime > then)
return 0;
if ((by & AGE_CTIME) && sb->st_ctime > then)
return 0;
if ((by & AGE_BTIME) && btime(fpath) > then)
return 0;
}
if (kept(fpath))
@@ -534,6 +611,14 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
return 0;
}
static int clean_dir(const char *path)
{
if (fisdir(path))
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
return 0;
}
/*
* POSIX ACLs for the a and A entries. Set straight
* through the system.posix_acl_* extended attributes, in the kernel's
@@ -1113,25 +1198,12 @@ static void tmpfiles(char *line)
}
/* age-based cleanup logic */
if (clean_flag) {
time_t max_age = parse_age(age);
if (clean_flag && strchr("dDe", type[0]) && !parse_age(age, &clean_age)) {
clean_now = time(NULL);
/* Only process if age is specified */
if (max_age >= 0) {
switch (type[0]) {
case 'd':
case 'D':
case 'e':
if (!fisdir(path))
break;
clean_age = max_age;
clean_now = time(NULL);
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
break;
default:
break;
}
}
/* Nothing is older than the epoch */
if (clean_age.sec <= clean_now)
clean_dir(path);
}
// file & directory creation logic