summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-04-16 01:36:51 +0900
committerGitHub <noreply@github.com>2022-04-16 01:36:51 +0900
commit7be87278a36710e634096277752c6c8f3ddf96a0 (patch)
tree4ec0eeed21a5944476007ca4584b5bc49bee384a
parent6e961aeb262521742a4cd92e4620de193f159f7c (diff)
parenta6aafd6ab6211a4142ebd72bd8c42ddbe2841eaa (diff)
downloadsystemd-7be87278a36710e634096277752c6c8f3ddf96a0.tar.gz
Merge pull request #23021 from fbuihuu/tmpfiles-fix-precedence-with-plus-sign
Tmpfiles fix precedence with plus sign
-rw-r--r--src/tmpfiles/tmpfiles.c33
-rw-r--r--test/test-functions2
-rwxr-xr-xtest/units/testsuite-22.03.sh12
-rwxr-xr-xtest/units/testsuite-22.13.sh75
4 files changed, 112 insertions, 10 deletions
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 0842d67d85..94973c2aa5 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -2657,7 +2657,7 @@ static int item_compare(const Item *a, const Item *b) {
return CMP(a->type, b->type);
}
-static bool item_compatible(Item *a, Item *b) {
+static bool item_compatible(const Item *a, const Item *b) {
assert(a);
assert(b);
assert(streq(a->path, b->path));
@@ -2896,6 +2896,26 @@ static int parse_age_by_from_arg(const char *age_by_str, Item *item) {
return 0;
}
+static bool is_duplicated_item(ItemArray *existing, const Item *i) {
+
+ assert(existing);
+ assert(i);
+
+ for (size_t n = 0; n < existing->n_items; n++) {
+ const Item *e = existing->items + n;
+
+ if (item_compatible(e, i))
+ continue;
+
+ /* Only multiple 'w+' lines for the same path are allowed. */
+ if (e->type != WRITE_FILE || !e->append_or_force ||
+ i->type != WRITE_FILE || !i->append_or_force)
+ return true;
+ }
+
+ return false;
+}
+
static int parse_line(
const char *fname,
unsigned line,
@@ -3247,13 +3267,10 @@ static int parse_line(
existing = ordered_hashmap_get(h, i.path);
if (existing) {
- size_t n;
-
- for (n = 0; n < existing->n_items; n++) {
- if (!item_compatible(existing->items + n, &i) && !i.append_or_force) {
- log_syntax(NULL, LOG_NOTICE, fname, line, 0, "Duplicate line for path \"%s\", ignoring.", i.path);
- return 0;
- }
+ if (is_duplicated_item(existing, &i)) {
+ log_syntax(NULL, LOG_NOTICE, fname, line, 0,
+ "Duplicate line for path \"%s\", ignoring.", i.path);
+ return 0;
}
} else {
existing = new0(ItemArray, 1);
diff --git a/test/test-functions b/test/test-functions
index b5a618c741..7155e99c0e 100644
--- a/test/test-functions
+++ b/test/test-functions
@@ -1869,7 +1869,7 @@ install_pam() {
paths+=(/lib*/security)
fi
- for d in /etc/pam.d /etc/security /usr/{etc,lib}/pam.d; do
+ for d in /etc/pam.d /{usr/,}etc/security /usr/{etc,lib}/pam.d; do
[ -d "$d" ] && paths+=("$d")
done
diff --git a/test/units/testsuite-22.03.sh b/test/units/testsuite-22.03.sh
index 404e33e2fa..a9df3d6bdd 100755
--- a/test/units/testsuite-22.03.sh
+++ b/test/units/testsuite-22.03.sh
@@ -186,6 +186,7 @@ test ! -e /tmp/F/daemon/unsafe-symlink/exploit
# 'w'
#
touch /tmp/w/overwritten
+touch /tmp/w/appended
### nop if the target does not exist.
systemd-tmpfiles --create - <<EOF
@@ -205,13 +206,22 @@ EOF
test -f /tmp/w/overwritten
test "$(< /tmp/w/overwritten)" = "old content"
-### new content is overwritten
+### old content is overwritten
systemd-tmpfiles --create - <<EOF
w /tmp/w/overwritten 0644 - - - new content
EOF
test -f /tmp/w/overwritten
test "$(< /tmp/w/overwritten)" = "new content"
+### append lines
+systemd-tmpfiles --create - <<EOF
+w+ /tmp/w/appended 0644 - - - 1
+w+ /tmp/w/appended 0644 - - - 2\n
+w+ /tmp/w/appended 0644 - - - 3
+EOF
+test -f /tmp/w/appended
+test "$(< /tmp/w/appended)" = "$(echo -ne '12\n3')"
+
### writing into an 'exotic' file should be allowed.
systemd-tmpfiles --create - <<EOF
w /dev/null - - - - new content
diff --git a/test/units/testsuite-22.13.sh b/test/units/testsuite-22.13.sh
new file mode 100755
index 0000000000..33ef451f2a
--- /dev/null
+++ b/test/units/testsuite-22.13.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+#
+# Tests for configuration directory and file precedences
+#
+set -eux
+
+rm -f /{usr/lib,etc}/tmpfiles.d/{L,w}-*.conf
+rm -fr /tmp/precedence/{L,w}
+
+mkdir -p /{usr/lib,etc}/tmpfiles.d
+mkdir -p /tmp/precedence/{L,w}
+
+#
+# 'L'
+#
+ln -s /dev/null /tmp/precedence/L
+
+# Overwrite the existing symlink
+cat >/usr/lib/tmpfiles.d/L-z.conf<<EOF
+L+ /tmp/precedence/L - - - - /usr/lib/tmpfiles.d/L-z.conf
+EOF
+
+systemd-tmpfiles --create
+test "$(readlink /tmp/precedence/L)" = "/usr/lib/tmpfiles.d/L-z.conf"
+
+# Files in /etc should override those in /usr
+cat >/etc/tmpfiles.d/L-z.conf<<EOF
+L+ /tmp/precedence/L - - - - /etc/tmpfiles.d/L-z.conf
+EOF
+
+systemd-tmpfiles --create
+test "$(readlink /tmp/precedence/L)" = "/etc/tmpfiles.d/L-z.conf"
+
+# /usr/…/L-a.conf has higher prio than /etc/…/L-z.conf
+cat >/usr/lib/tmpfiles.d/L-a.conf<<EOF
+L+ /tmp/precedence/L - - - - /usr/lib/tmpfiles.d/L-a.conf
+EOF
+
+systemd-tmpfiles --create
+test "$(readlink /tmp/precedence/L)" = "/usr/lib/tmpfiles.d/L-a.conf"
+
+# Files in /etc should override those in /usr
+cat >/etc/tmpfiles.d/L-a.conf<<EOF
+L+ /tmp/precedence/L - - - - /etc/tmpfiles.d/L-a.conf
+EOF
+
+systemd-tmpfiles --create
+test "$(readlink /tmp/precedence/L)" = "/etc/tmpfiles.d/L-a.conf"
+
+#
+# 'w'
+#
+touch /tmp/precedence/w/f
+
+# Multiple configuration files specifying 'w+' for the same path is allowed.
+for i in a c; do
+ cat >/usr/lib/tmpfiles.d/w-$i.conf<<EOF
+w+ /tmp/precedence/w/f - - - - /usr/lib/tmpfiles.d/w-$i.conf\n
+EOF
+ cat >/etc/tmpfiles.d/w-$i.conf<<EOF
+w+ /tmp/precedence/w/f - - - - /etc/tmpfiles.d/w-$i.conf\n
+EOF
+done
+
+cat >/usr/lib/tmpfiles.d/w-b.conf<<EOF
+w+ /tmp/precedence/w/f - - - - /usr/lib/tmpfiles.d/w-b.conf\n
+EOF
+
+systemd-tmpfiles --create
+cmp /tmp/precedence/w/f <<EOF
+/etc/tmpfiles.d/w-a.conf
+/usr/lib/tmpfiles.d/w-b.conf
+/etc/tmpfiles.d/w-c.conf
+EOF