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

+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