diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 13dfc517..db02880d 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -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` diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 34adf624..f72fc773 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -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 diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 1564688e..eec16b35 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -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(©[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 diff --git a/test/Makefile.am b/test/Makefile.am index e31c308c..5a660d16 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -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 diff --git a/test/tmpfiles-match.sh b/test/tmpfiles-match.sh new file mode 100755 index 00000000..856a7383 --- /dev/null +++ b/test/tmpfiles-match.sh @@ -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" <