tmpfiles: add the = modifier, remove a path of another kind

When there is a file where an entry wants a directory, or the other
way around, the entry fails or leaves the path as it is.  In
tmpfiles.d(5) an = after the type has the path removed first when it
is of another kind than the entry makes:

    d= /var/lib/foo 0755 foo foo

The same goes for the directories above the path, anything else in
place of one is removed.  tmpfiles ignores the =.

Remove what does not match before the b, c, d, D, f, F, L, and p
entries, the ones that make a path of one kind.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:12 +01:00
1 parent 37cbc12946
commit f2c0888375
5 files changed
+125 -4

No files matched your search

+3
View File
@@ -45,6 +45,9 @@ All relevant changes are documented in this file.
days. Before the age was not read
- tmpfiles: add the `C+` entry, which also copies to a directory that has
something in it. What is missing is added, nothing is overwritten
- tmpfiles: add the `=` modifier, as in `d=`, to first remove what is in
place of the path when it is of another kind than the entry makes, also
what is in place of a directory above it
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
it. `--prefix` and `--exclude-prefix` match whole path components,
`--exclude-prefix=/dev` no longer skips `/devices`
-4
View File
@@ -12,10 +12,6 @@ files or common packages already rely on, last what few will miss.
--------------------------------
* `z` and `Z` only set the SELinux label when `restorecon` is installed
* Modifier `=` (remove what is of another type). `-` (do not fail) has
nothing to do, tmpfiles never exits with an error for an entry, and
an entry with `^` (credential as argument) is skipped, Finit has no
credentials
2. Command line
+57
View File
@@ -1582,6 +1582,44 @@ static char *expand(const char *str, char *buf, size_t len)
return buf;
}
/*
* For the = modifier: remove @path when it is of another kind than
* @fmt, and what is in place of a directory above it. A link to a
* directory is fine there, when it is root's.
*/
static void replace_kind(const char *path, mode_t fmt)
{
char *copy = strdupa(path);
struct stat st;
/* Nothing is removed by a path that may lead somewhere else */
if (path[0] != '/' || strstr(path, "/./") || strstr(path, "/../"))
return;
for (size_t len = strlen(copy); len > 1 && copy[len - 1] == '/'; len--)
copy[len - 1] = 0;
if (!copy[1] || fnmatch("*/.", copy, 0) == 0 || fnmatch("*/..", copy, 0) == 0)
return;
for (char *ptr = strchr(&copy[1], '/'); ptr; ptr = strchr(&ptr[1], '/')) {
*ptr = 0;
if (!lstat(copy, &st) && !S_ISDIR(st.st_mode)) {
if (!fisdir(copy))
erase(copy);
else if (st.st_uid != 0)
return; /* Link to a directory, and not root's */
}
*ptr = '/';
}
if (lstat(copy, &st) || (st.st_mode & S_IFMT) == fmt)
return;
if (S_ISDIR(st.st_mode))
rmrf(copy);
else
erase(copy);
}
/*
* Next field of @line, NULL when there are no more, a quote is left
* open, or the field is empty or has a NUL. Quotes and backslash
@@ -1667,6 +1705,7 @@ static void tmpfiles(char *line)
int strc, rc = 0;
struct perm perm = { 0 };
int plus = 0, boot = 0, b64 = 0;
mode_t kind = 0;
ssize_t arglen = -1;
FILE *fp = NULL;
char buf[1024], pathbuf[PATH_MAX], argbuf[1024];
@@ -1683,6 +1722,8 @@ static void tmpfiles(char *line)
boot = 1;
else if (*mod == '~')
b64 = 1;
else if (*mod == '=')
kind = 1;
else if (*mod == '?' && type[0] == 'L')
type[0] = 'l'; /* What Finit has had as l */
else if (*mod == '^')
@@ -1797,6 +1838,22 @@ static void tmpfiles(char *line)
return;
}
if (kind && create_flag) {
switch (type[0]) {
case 'b': kind = S_IFBLK; break;
case 'c': kind = S_IFCHR; break;
case 'd':
case 'D': kind = S_IFDIR; break;
case 'f':
case 'F': kind = S_IFREG; break;
case 'L': kind = S_IFLNK; break;
case 'p': kind = S_IFIFO; break;
default: kind = 0; break;
}
if (kind)
replace_kind(path, kind);
}
strc = stat(path, &st);
// file and directory removal logic
+2
View File
@@ -88,6 +88,7 @@ EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += tmpfiles-boot.sh
EXTRA_DIST += tmpfiles-clean.sh
EXTRA_DIST += tmpfiles-copy.sh
EXTRA_DIST += tmpfiles-match.sh
EXTRA_DIST += tmpfiles-order.sh
EXTRA_DIST += tmpfiles-specifier.sh
EXTRA_DIST += tmpfiles-unsupported.sh
@@ -175,6 +176,7 @@ TESTS += tmpfiles-perm.sh
TESTS += tmpfiles-boot.sh
TESTS += tmpfiles-clean.sh
TESTS += tmpfiles-copy.sh
TESTS += tmpfiles-match.sh
TESTS += tmpfiles-order.sh
TESTS += tmpfiles-specifier.sh
TESTS += tmpfiles-unsupported.sh
+63
View File
@@ -0,0 +1,63 @@
#!/bin/sh
# Verify the = modifier of tmpfiles, what is of another kind than the
# entry makes is removed first
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/match
CONF=/tmp/match.conf
kind()
{
texec stat -c %F "$1"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
setup()
{
run "rm -rf $DIR; mkdir -p $DIR/dir/sub $DIR/same; mkfifo $DIR/fifo $DIR/parent; echo old > $DIR/file; echo old > $DIR/keep; echo old > $DIR/link; echo same > $DIR/same/file"
}
conf()
{
run "cat > $CONF" <<CONF
d$1 $DIR/fifo
f$1 $DIR/dir - - - - new
d$1 $DIR/file
f$1 $DIR/parent/file - - - - new
p$1 $DIR/keep
L$1 $DIR/link - - - - /dev/null
d$1 $DIR/same
f$1 $DIR/same/file
CONF
run "/libexec/finit/tmpfiles --create $CONF"
}
setup
conf ""
say 'Without = a path of another kind is left alone'
assert "Pipe for d" "$(kind $DIR/fifo)" = "fifo"
assert "Directory for f" "$(kind $DIR/dir)" = "directory"
assert "File for d" "$(kind $DIR/file)" = "regular file"
assert "Pipe as parent" "$(kind $DIR/parent)" = "fifo"
setup
conf "="
sep
say 'With = it is removed, and made anew'
assert "Pipe for d" "$(kind $DIR/fifo)" = "directory"
assert "Directory for f" "$(texec cat $DIR/dir)" = "new"
assert "File for d" "$(kind $DIR/file)" = "directory"
assert "Pipe as parent" "$(texec cat $DIR/parent/file)" = "new"
assert "File for p" "$(kind $DIR/keep)" = "fifo"
assert "File for L" "$(texec readlink $DIR/link)" = "/dev/null"
assert "Same kind is left alone" "$(texec cat $DIR/same/file)" = "same"