tmpfiles: run boot-only entries only at boot

A ! after the type marks an entry that is only safe to run at boot.
Finit's own files have them, like this one in x11.conf:

    D! /tmp/.X11-unix 1777 root root

tmpfiles ignored the ! and ran these entries on every call, so this
emptied the directory under a running X server:

    tmpfiles --remove

Only the character right after the type was looked at, and only for
a +.

Read all the modifiers after the type, in any order, and skip an
entry with ! unless the new --boot option is given.  Finit gives it
at boot, together with --remove.  Before this the r, R, and D entries
were never run at boot, so "r! /forcefsck" in legacy.conf left the
file in place.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:02:35 +01:00
1 parent 4a11294f98
commit 3c9500fc08
7 files changed
+108 -34

No files matched your search

+4
View File
@@ -11,6 +11,10 @@ All relevant changes are documented in this file.
- `shutdown` now takes a time, `now`, `+MIN`, or `HH:MM`, and a message. - `shutdown` now takes a time, `now`, `+MIN`, or `HH:MM`, and a message.
Finit warns logged-in users as the time approaches, refuses new logins Finit warns logged-in users as the time approaches, refuses new logins
from five minutes before, and `shutdown -c` cancels. Issue #149 from five minutes before, and `shutdown -c` cancels. Issue #149
- tmpfiles: entries marked boot-only, with a `!` after the type, are now
skipped unless the new `--boot` option is given. Finit gives it at
boot, and now also `--remove`, so `r`, `R`, and `D` entries take effect
at boot and no longer need a separate call
- tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used - tmpfiles: add the `a`, `a+`, `A`, and `A+` entries for POSIX ACLs, used
by for example the tpm2-tss package. Finit sets them itself, no need by for example the tpm2-tss package. Finit sets them itself, no need
for libacl or `setfacl` for libacl or `setfacl`
+2
View File
@@ -24,6 +24,8 @@ For your convenience a set of *optional* plugins are available:
This plugin is a wrapper for the [tmpfiles.d(5)][] implementation that This plugin is a wrapper for the [tmpfiles.d(5)][] implementation that
Finit has. Capable but limited: no aging, attributes, or subvolumes. Finit has. Capable but limited: no aging, attributes, or subvolumes.
It runs `tmpfiles --create --remove --boot`, so entries marked
boot-only with a `!` are run here and at no other time.
By default, `/lib/finit/tmpfiles.d` carries all the default .conf By default, `/lib/finit/tmpfiles.d` carries all the default .conf
files distributed with Finit. It is read first but can be overridden files distributed with Finit. It is read first but can be overridden
+13 -26
View File
@@ -8,25 +8,12 @@ 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. Boot-only entries 1. Nothing runs the clean pass
--------------------
The `!` modifier marks an entry that is only safe at boot. Finit's own
files use it, `D!` and `r!` in `x11.conf`, `r!` in `legacy.conf`, and
`C!` in `etc.conf`, but the modifier is ignored and there is no `--boot`
option. The entries run on every call, so `tmpfiles --remove` on a
running system empties `/tmp/.X11-unix` and removes the X lock files.
* Parse the modifiers after the type letter, in any order
* Add `--boot`, skip `!` entries without it
* Call `tmpfiles --create --remove --boot` from `plugins/bootmisc.c`
2. Nothing runs the clean pass
------------------------------ ------------------------------
Finit calls `tmpfiles --create` at boot and nothing else. The age Finit calls `tmpfiles --create --remove --boot` at boot and nothing
field has no effect until the user adds a task for `tmpfiles --clean`. else. The age field has no effect until the user adds a task for
`tmpfiles --clean`.
* Ship a task for it, systemd runs its timer 15 min after boot and * Ship a task for it, systemd runs its timer 15 min after boot and
then daily then daily
@@ -39,7 +26,7 @@ field has no effect until the user adds a task for `tmpfiles --clean`.
* The age is only used for `d`, `D`, and `e` * The age is only used for `d`, `D`, and `e`
3. Prefixes that are silently dropped 2. Prefixes that are silently dropped
------------------------------------- -------------------------------------
* Mode `~0755`, mask with the mode the path has, and `:0755`, only * Mode `~0755`, mask with the mode the path has, and `:0755`, only
@@ -52,7 +39,7 @@ field has no effect until the user adds a task for `tmpfiles --clean`.
all of them all of them
4. Mode and owner not applied 3. Mode and owner not applied
----------------------------- -----------------------------
* `f` on a file that exists: the entry is skipped, tmpfiles.d(5) * `f` on a file that exists: the entry is skipped, tmpfiles.d(5)
@@ -62,7 +49,7 @@ field has no effect until the user adds a task for `tmpfiles --clean`.
* `z`, `Z`: mode and owner, and they do nothing without `restorecon` * `z`, `Z`: mode and owner, and they do nothing without `restorecon`
5. Device nodes with major or minor 0 4. Device nodes with major or minor 0
------------------------------------- -------------------------------------
`parse_mm()` rejects a 0 in either number, so an entry like this one `parse_mm()` rejects a 0 in either number, so an entry like this one
@@ -71,7 +58,7 @@ is skipped:
c /dev/tty 0666 root tty - 5:0 c /dev/tty 0666 root tty - 5:0
6. Subvolumes 5. Subvolumes
------------- -------------
`v`, `q`, and `Q` are skipped as unsupported. systemd makes a plain `v`, `q`, and `Q` are skipped as unsupported. systemd makes a plain
@@ -79,7 +66,7 @@ directory when the file system is not btrfs, doing the same gets
distribution files with these entries to work. distribution files with these entries to work.
7. Specifiers 6. Specifiers
------------- -------------
A path with `%` in it is skipped, also the escaped `%%`. The ones that A path with `%` in it is skipped, also the escaped `%%`. The ones that
@@ -89,7 +76,7 @@ and `%V`. This is the largest gap for using such files unchanged, and
the most work. the most work.
8. Remaining types and modifiers 7. Remaining types and modifiers
-------------------------------- --------------------------------
* `L?`, and `C+`, which is read as `C` * `L?`, and `C+`, which is read as `C`
@@ -99,7 +86,7 @@ the most work.
* Quoted paths, a path with a space in it cannot be written * Quoted paths, a path with a space in it cannot be written
9. Command line 8. Command line
--------------- ---------------
Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`, Missing options: `--prefix`, `--root`, `--replace`, `--dry-run`,
@@ -108,14 +95,14 @@ is the useful one, for packages that run `tmpfiles --create` on only
their own paths. their own paths.
10. Smaller things 9. 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
all of them by name, whatever directory they are in, so `00-x.conf` all of them by name, whatever directory they are in, so `00-x.conf`
in `/etc` should run before `50-y.conf` in `/usr/lib` in `/etc` should run before `50-y.conf` in `/usr/lib`
* `doc/README.md`, `doc/index.md`, and `doc/plugins.md` say there is * `doc/README.md`, `doc/index.md`, and `doc/plugins.md` say there is
no aging. There is, see 2 above no aging. There is, see 1 above
* `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
* `mksubsys()` in `src/util.c` is unused * `mksubsys()` in `src/util.c` is unused
+1 -1
View File
@@ -163,7 +163,7 @@ static void setup(void *arg)
kernel_links(); kernel_links();
/* Create all system tmpfiles.d(5) */ /* Create all system tmpfiles.d(5) */
run_interactive(_PATH_TMPFILES " --create", "Creating required directories and files"); run_interactive(_PATH_TMPFILES " --create --remove --boot", "Creating required directories and files");
/* Set BOOT_TIME UTMP entry */ /* Set BOOT_TIME UTMP entry */
utmp_set_boot(); utmp_set_boot();
+25 -7
View File
@@ -55,6 +55,7 @@ int debug;
int create_flag = 0; int create_flag = 0;
int clean_flag = 0; int clean_flag = 0;
int remove_flag = 0; int remove_flag = 0;
int boot_flag = 0;
/* Set for the first of two passes, the second one does the complaining */ /* Set for the first of two passes, the second one does the complaining */
static int quiet; static int quiet;
@@ -717,6 +718,7 @@ static void tmpfiles(char *line)
struct stat st, ast; struct stat st, ast;
int major, minor; int major, minor;
int strc, rc = 0; int strc, rc = 0;
int plus = 0, boot = 0;
mode_t mode = 0; mode_t mode = 0;
FILE *fp = NULL; FILE *fp = NULL;
char buf[1024]; char buf[1024];
@@ -726,6 +728,15 @@ static void tmpfiles(char *line)
if (!type) if (!type)
return; return;
for (char *mod = &type[1]; *mod; mod++) {
if (*mod == '+')
plus = 1;
else if (*mod == '!')
boot = 1;
}
if (boot && !boot_flag)
return;
path = strtok(NULL, "\t "); path = strtok(NULL, "\t ");
if (!path) if (!path)
return; return;
@@ -837,14 +848,14 @@ static void tmpfiles(char *line)
switch (type[0]) { switch (type[0]) {
case 'a': case 'a':
case 'A': case 'A':
rc = acl_entry(path, arg, type[0] == 'A', type[1] == '+'); rc = acl_entry(path, arg, type[0] == 'A', plus);
break; break;
case 'b': case 'b':
rc = parse_mm(arg, &major, &minor); rc = parse_mm(arg, &major, &minor);
if (rc) if (rc)
break; break;
if (!strc) { if (!strc) {
if (type[1] != '+') if (!plus)
break; break;
erase(path); erase(path);
} }
@@ -856,7 +867,7 @@ static void tmpfiles(char *line)
if (rc) if (rc)
break; break;
if (!strc) { if (!strc) {
if (type[1] != '+') if (!plus)
break; break;
erase(path); erase(path);
} }
@@ -926,7 +937,7 @@ static void tmpfiles(char *line)
int flags = O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC; int flags = O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC;
int fd, uid, gid; int fd, uid, gid;
if (type[1] == '+' || type[0] == 'F') { if (plus || type[0] == 'F') {
flags |= O_TRUNC; /* create or truncate */ flags |= O_TRUNC; /* create or truncate */
} else { } else {
if (!strc) if (!strc)
@@ -980,7 +991,7 @@ static void tmpfiles(char *line)
/* fallthrough */ /* fallthrough */
case 'L': case 'L':
if (!strc) { if (!strc) {
if (type[1] != '+') if (!plus)
break; break;
if (fisdir(path)) if (fisdir(path))
rmrf(path); rmrf(path);
@@ -998,7 +1009,7 @@ static void tmpfiles(char *line)
break; break;
case 'p': case 'p':
if (!strc) { if (!strc) {
if (type[1] != '+') if (!plus)
break; break;
erase(path); erase(path);
} }
@@ -1016,7 +1027,7 @@ static void tmpfiles(char *line)
break; break;
for (size_t i = 0; i < gl.gl_pathc; i++) { for (size_t i = 0; i < gl.gl_pathc; i++) {
fp = fopen(gl.gl_pathv[i], type[1] == '+' ? "a" : "w"); fp = fopen(gl.gl_pathv[i], plus ? "a" : "w");
if (fp) { if (fp) {
write_arg(fp, arg); write_arg(fp, arg);
rc = fclose(fp); rc = fclose(fp);
@@ -1141,6 +1152,7 @@ static int usage(int rc)
"Usage: tmpfiles [OPTIONS] [CONFIGFILE...]\n" "Usage: tmpfiles [OPTIONS] [CONFIGFILE...]\n"
"\n" "\n"
"Options:\n" "Options:\n"
" --boot Also run boot-only entries, marked with !\n"
" -C, --clean Clean files and directories based on age\n" " -C, --clean Clean files and directories based on age\n"
" -c, --create Create files and directories\n" " -c, --create Create files and directories\n"
" -d, --debug Show developer debug messages\n" " -d, --debug Show developer debug messages\n"
@@ -1158,11 +1170,13 @@ static int usage(int rc)
enum { enum {
OPT_EXCLUDE_PREFIX = 256, OPT_EXCLUDE_PREFIX = 256,
OPT_BOOT,
}; };
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct option long_options[] = { struct option long_options[] = {
{ "boot", 0, NULL, OPT_BOOT },
{ "clean", 0, NULL, 'C' }, { "clean", 0, NULL, 'C' },
{ "create", 0, NULL, 'c' }, { "create", 0, NULL, 'c' },
{ "debug", 0, NULL, 'd' }, { "debug", 0, NULL, 'd' },
@@ -1195,6 +1209,10 @@ int main(int argc, char *argv[])
add_exclude_prefix("/sys"); add_exclude_prefix("/sys");
break; break;
case OPT_BOOT:
boot_flag = 1;
break;
case OPT_EXCLUDE_PREFIX: case OPT_EXCLUDE_PREFIX:
add_exclude_prefix(optarg); add_exclude_prefix(optarg);
break; break;
+2
View File
@@ -84,6 +84,7 @@ EXTRA_DIST += testserv.sh
EXTRA_DIST += tmpfiles-acl.sh EXTRA_DIST += tmpfiles-acl.sh
EXTRA_DIST += tmpfiles-arg.sh EXTRA_DIST += tmpfiles-arg.sh
EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += tmpfiles-boot.sh
EXTRA_DIST += tmpfiles-order.sh EXTRA_DIST += tmpfiles-order.sh
EXTRA_DIST += tmpfiles-unsupported.sh EXTRA_DIST += tmpfiles-unsupported.sh
EXTRA_DIST += unexpected-restart.sh EXTRA_DIST += unexpected-restart.sh
@@ -166,6 +167,7 @@ TESTS += testserv.sh
TESTS += tmpfiles-acl.sh TESTS += tmpfiles-acl.sh
TESTS += tmpfiles-arg.sh TESTS += tmpfiles-arg.sh
TESTS += tmpfiles-perm.sh TESTS += tmpfiles-perm.sh
TESTS += tmpfiles-boot.sh
TESTS += tmpfiles-order.sh TESTS += tmpfiles-order.sh
TESTS += tmpfiles-unsupported.sh TESTS += tmpfiles-unsupported.sh
endif endif
+61
View File
@@ -0,0 +1,61 @@
#!/bin/sh
# Verify that tmpfiles only runs the boot-only entries, the ones with
# a ! after the type, at boot or when called with --boot
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/boot
CONF=/tmp/boot.conf
tmpfiles()
{
run "/libexec/finit/tmpfiles $* $CONF"
}
assert_exists()
{
assert "$1 exists" "$(texec sh -c "test -e $1 && echo yes || echo no")" = "$2"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF /fastboot"
}
# Removed at boot by an r! entry in Finit's own legacy.conf
touch "${SYSROOT:-$(pwd)/$TEST_DIR/sysroot}/fastboot"
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
sep
say 'Finit runs the boot-only entries, and removes, at boot'
assert_exists /fastboot no
run "cat > $CONF" <<CONF
d $DIR
f $DIR/always
f! $DIR/boot
f+! $DIR/plus-boot - - - - text
f!+ $DIR/boot-plus - - - - text
r! $DIR/always
CONF
sep
say 'Without --boot they are skipped'
tmpfiles --create --remove
assert_exists $DIR/always yes
assert_exists $DIR/boot no
assert_exists $DIR/plus-boot no
assert_exists $DIR/boot-plus no
sep
say 'With --boot they run, remove before create'
tmpfiles --create --remove --boot
assert_exists $DIR/boot yes
assert "Modifiers in any order" "$(texec cat $DIR/plus-boot $DIR/boot-plus)" = "texttext"
assert_exists $DIR/always yes
tmpfiles --remove --boot
assert_exists $DIR/always no