mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
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:
6 files changed
+189
-88
No files matched your search
+1
-1
@@ -40,7 +40,7 @@ AC_PROG_MKDIR_P
|
|||||||
|
|
||||||
# Configuration.
|
# Configuration.
|
||||||
AC_CHECK_HEADERS([termios.h sys/ioctl.h mntent.h sys/sysmacros.h])
|
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
|
# Check for uint[8,16,32]_t
|
||||||
AC_TYPE_UINT8_T
|
AC_TYPE_UINT8_T
|
||||||
|
|||||||
@@ -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 `%`
|
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,
|
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 `%%`
|
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
|
- 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
|
`d`. Finit has no support for btrfs subvolumes, and before the entries
|
||||||
were skipped as unsupported
|
were skipped as unsupported
|
||||||
|
|||||||
+3
-2
@@ -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
|
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
|
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
|
access, birth, change, and modification times is newer than the age,
|
||||||
the age `0` everything in the directory is removed.
|
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]
|
> [!NOTE]
|
||||||
> On an embedded system both `/var` and `/run` can be `tmpfs` RAM
|
> On an embedded system both `/var` and `/run` can be `tmpfs` RAM
|
||||||
|
|||||||
+6
-18
@@ -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
|
[tmpfiles.d(5)]: https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html
|
||||||
|
|
||||||
|
|
||||||
1. Ages
|
1. Specifiers
|
||||||
-------
|
|
||||||
|
|
||||||
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
|
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
Missing are the ones for the OS release and the architecture: `%a`,
|
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`.
|
look at `$TMPDIR`.
|
||||||
|
|
||||||
|
|
||||||
3. Remaining types and modifiers
|
2. Remaining types and modifiers
|
||||||
--------------------------------
|
--------------------------------
|
||||||
|
|
||||||
* `L?`, and `C+`, which is read as `C`
|
* `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
|
* 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`,
|
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.
|
their own paths.
|
||||||
|
|
||||||
|
|
||||||
5. Smaller things
|
4. Smaller things
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
* Files are processed one directory at a time. tmpfiles.d(5) sorts
|
* Files are processed one directory at a time. tmpfiles.d(5) sorts
|
||||||
@@ -62,7 +49,8 @@ their own paths.
|
|||||||
it should be 0644
|
it should be 0644
|
||||||
* A `d` or `e` entry for a path that is a link to a directory warns
|
* A `d` or `e` entry for a path that is a link to a directory warns
|
||||||
that mode and owner could not be set
|
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
|
* A `w` entry with a glob that matches several paths writes its
|
||||||
argument only up to the first backslash for all but the first,
|
argument only up to the first backslash for all but the first,
|
||||||
`write_arg()` cuts the argument up as it goes
|
`write_arg()` cuts the argument up as it goes
|
||||||
|
|||||||
+139
-67
@@ -268,45 +268,105 @@ static void write_arg(FILE *fp, char *arg)
|
|||||||
fputs(arg, fp);
|
fputs(arg, fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/* Timestamps of a file to go by when cleaning, those of a directory are shifted up */
|
||||||
* Parse age string and return age in seconds.
|
#define AGE_ATIME 0x01
|
||||||
* Returns -1 on invalid/empty age ("-" or NULL).
|
#define AGE_BTIME 0x02
|
||||||
* Examples: "10d" = 864000, "1w" = 604800, "2h" = 7200
|
#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;
|
const struct { const char *unit; unsigned long long usec; } units[] = {
|
||||||
char *end;
|
{ "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;
|
return -1;
|
||||||
|
|
||||||
errno = 0;
|
age->by = 0;
|
||||||
val = strtoul(age, &end, 10);
|
age->below = 0;
|
||||||
if (errno || end == age)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
switch (*end) {
|
ptr = strchr(str, ':');
|
||||||
case 'w':
|
if (ptr) {
|
||||||
val *= 7;
|
const char *flags = "abcmABCM";
|
||||||
/* fallthrough */
|
|
||||||
case 'd':
|
if (ptr == str)
|
||||||
val *= 24;
|
return -1;
|
||||||
/* fallthrough */
|
for (; str < ptr; str++) {
|
||||||
case 'h':
|
const char *flag = strchr(flags, *str);
|
||||||
val *= 60;
|
|
||||||
/* fallthrough */
|
if (!flag)
|
||||||
case 'm':
|
return -1;
|
||||||
val *= 60;
|
age->by |= 1 << (flag - flags);
|
||||||
/* fallthrough */
|
}
|
||||||
case 's':
|
str++;
|
||||||
case '\0':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return -1; /* unknown suffix */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 */
|
/* 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;
|
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)
|
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 */
|
/* Skip the root directory itself */
|
||||||
if (ftw->level == 0)
|
if (ftw->level == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
is_dir = (tflag == FTW_D || tflag == FTW_DP);
|
if (clean_age.below && ftw->level == 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
/*
|
/* Kept as long as one of the chosen timestamps is newer than the age */
|
||||||
* Conservative cleanup matching systemd-tmpfiles behavior:
|
by = clean_age.by;
|
||||||
* - Files: keep if ANY of atime, ctime, mtime is recent
|
if (tflag == FTW_D || tflag == FTW_DP || tflag == FTW_DNR)
|
||||||
* - Directories: keep if ANY of atime, mtime is recent (ctime
|
by >>= AGE_DIR;
|
||||||
* excluded because cleanup itself updates directory ctime)
|
if (clean_age.sec) {
|
||||||
* - Age 0 removes everything, whatever the timestamps
|
time_t then = clean_now - clean_age.sec;
|
||||||
*/
|
|
||||||
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)
|
if ((by & AGE_MTIME) && sb->st_mtime > then)
|
||||||
return 0; /* atime is too new, keep it */
|
return 0;
|
||||||
|
if ((by & AGE_ATIME) && sb->st_atime > then)
|
||||||
if (!is_dir && clean_now - sb->st_ctime < clean_age)
|
return 0;
|
||||||
return 0; /* ctime is too new, keep it (files only) */
|
if ((by & AGE_CTIME) && sb->st_ctime > then)
|
||||||
|
return 0;
|
||||||
|
if ((by & AGE_BTIME) && btime(fpath) > then)
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kept(fpath))
|
if (kept(fpath))
|
||||||
@@ -534,6 +611,14 @@ static int do_clean(const char *fpath, const struct stat *sb, int tflag, struct
|
|||||||
return 0;
|
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
|
* POSIX ACLs for the a and A entries. Set straight
|
||||||
* through the system.posix_acl_* extended attributes, in the kernel's
|
* through the system.posix_acl_* extended attributes, in the kernel's
|
||||||
@@ -1113,25 +1198,12 @@ static void tmpfiles(char *line)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* age-based cleanup logic */
|
/* age-based cleanup logic */
|
||||||
if (clean_flag) {
|
if (clean_flag && strchr("dDe", type[0]) && !parse_age(age, &clean_age)) {
|
||||||
time_t max_age = parse_age(age);
|
|
||||||
|
|
||||||
/* 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);
|
clean_now = time(NULL);
|
||||||
nftw(path, do_clean, 20, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
|
|
||||||
break;
|
/* Nothing is older than the epoch */
|
||||||
default:
|
if (clean_age.sec <= clean_now)
|
||||||
break;
|
clean_dir(path);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// file & directory creation logic
|
// file & directory creation logic
|
||||||
|
|||||||
@@ -64,3 +64,38 @@ d $DIR/none
|
|||||||
CONF
|
CONF
|
||||||
assert_left $DIR/zero ""
|
assert_left $DIR/zero ""
|
||||||
assert_left $DIR/none "file "
|
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
|
||||||
|
d $DIR/units - - - 0m1s
|
||||||
|
d $DIR/msec - - - 1500ms
|
||||||
|
d $DIR/long - - - 1h1s
|
||||||
|
CONF
|
||||||
|
assert_left $DIR/units ""
|
||||||
|
assert_left $DIR/msec ""
|
||||||
|
assert_left $DIR/long "file "
|
||||||
|
|
||||||
|
sep
|
||||||
|
say 'With ~ what is right in the directory is kept'
|
||||||
|
clean <<CONF
|
||||||
|
d $DIR/level - - - ~1s
|
||||||
|
CONF
|
||||||
|
assert_left $DIR/level "file sub "
|
||||||
|
assert_left $DIR/level/sub ""
|
||||||
|
|
||||||
|
sep
|
||||||
|
say 'The timestamps to go by can be chosen, here not the change time'
|
||||||
|
run "cat > $CONF" <<CONF
|
||||||
|
d $DIR/by-all - - - 1s
|
||||||
|
d $DIR/by-m - - - am:1s
|
||||||
|
d $DIR/by-all - - - AM:1s
|
||||||
|
d $DIR/by-all - - - :1s
|
||||||
|
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 ""
|
||||||
Reference in new issue
Block a user