diff --git a/configure.ac b/configure.ac index 1a575dc2..a3fb52b8 100644 --- a/configure.ac +++ b/configure.ac @@ -40,7 +40,7 @@ AC_PROG_MKDIR_P # Configuration. AC_CHECK_HEADERS([termios.h sys/ioctl.h mntent.h sys/sysmacros.h]) -AC_CHECK_FUNCS([strstr getopt getmntent getmntent_r close_range]) +AC_CHECK_FUNCS([strstr getopt getmntent getmntent_r close_range statx]) # Check for uint[8,16,32]_t AC_TYPE_UINT8_T diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 3703ccb5..cafbacc0 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -21,6 +21,11 @@ All relevant changes are documented in this file. and the user ones `%u`, `%U`, `%g`, `%G`. 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: the age of an entry can now have several units, as in + `1d12h`, also `ms`, `us`, and `min`. New are the `~` prefix, to keep + what is right in the directory, and a choice of the timestamps to go + by, as in `aAmM:10d`. The birth time of a file now counts too, like + its other timestamps, when the file system has one - tmpfiles: the `v`, `q`, and `Q` entries now make a plain directory, like `d`. Finit has no support for btrfs subvolumes, and before the entries were skipped as unsupported diff --git a/doc/plugins.md b/doc/plugins.md index a59044c3..fe2e1a22 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -54,8 +54,9 @@ For your convenience a set of *optional* plugins are available: Cleaning stays on the file system of the directory, and it leaves what `x` and `X` entries name. A file is kept as long as one of its - access, change, and modification times is newer than the age. With - the age `0` everything in the directory is removed. + access, birth, change, and modification times is newer than the age, + or those the age names, as in `aAmM:10d` for access and modification + time only. With the age `0` everything in the directory is removed. > [!NOTE] > On an embedded system both `/var` and `/run` can be `tmpfs` RAM diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 4b2676cb..e3bf0619 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -8,20 +8,7 @@ files or common packages already rely on, last what few will miss. [tmpfiles.d(5)]: https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html -1. Ages -------- - -Finit never runs `tmpfiles --clean` itself, `doc/plugins.md` tells the -user to call it from cron. - -* Only one number with `s`, `m`, `h`, `d`, or `w` is read. Missing - are `1d12h`, `ms`, `us`, the `~` prefix, and the `a`, `b`, `c`, `m` - selectors for which timestamps to compare -* The age is only used for `d`, `D`, and `e`, and an `e` entry with a - glob in the path is not cleaned - - -2. Specifiers +1. Specifiers ------------- Missing are the ones for the OS release and the architecture: `%a`, @@ -30,7 +17,7 @@ skipped. `%T` and `%V` are always `/tmp` and `/var/tmp`, they do not look at `$TMPDIR`. -3. Remaining types and modifiers +2. Remaining types and modifiers -------------------------------- * `L?`, and `C+`, which is read as `C` @@ -41,7 +28,7 @@ look at `$TMPDIR`. * Quoted paths, a path with a space in it cannot be written -4. Command line +3. Command line --------------- Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`, @@ -50,7 +37,7 @@ is the useful one, for packages that run `tmpfiles --create` on only their own paths. -5. Smaller things +4. Smaller things ------------------ * Files are processed one directory at a time. tmpfiles.d(5) sorts @@ -62,7 +49,8 @@ their own paths. it should be 0644 * A `d` or `e` entry for a path that is a link to a directory warns that mode and owner could not be set -* An age so large that it overflows is not caught +* An `x` or `X` entry with an age should have what it names cleaned + by that age, the age is not read * A `w` entry with a glob that matches several paths writes its argument only up to the first backslash for all but the first, `write_arg()` cuts the argument up as it goes diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 5ddaaf7e..70fc2ebe 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -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 diff --git a/test/tmpfiles-clean.sh b/test/tmpfiles-clean.sh index f5505f53..e15f0f96 100755 --- a/test/tmpfiles-clean.sh +++ b/test/tmpfiles-clean.sh @@ -64,3 +64,38 @@ d $DIR/none CONF assert_left $DIR/zero "" assert_left $DIR/none "file " + +sep +say 'Ages with several units, and ms' +run "mkdir -p $DIR/units $DIR/msec $DIR/long $DIR/level/sub $DIR/by-all $DIR/by-m" +run "touch $DIR/units/file $DIR/msec/file $DIR/long/file" +run "touch $DIR/level/file $DIR/level/sub/file $DIR/by-all/file $DIR/by-m/file" +sleep 2 +clean < $CONF" <