2021-04-09 20:39:41 +03:00
#!/bin/bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2018-04-13 16:32:25 +03:00
#
# Basic tests for types creating directories
2021-04-09 20:39:41 +03:00
set -eux
set -o pipefail
2018-04-13 16:32:25 +03:00
2019-01-07 03:13:13 +03:00
rm -fr /tmp/{ C,d,D,e}
mkdir /tmp/{ C,d,D,e}
2018-04-13 16:32:25 +03:00
#
# 'd'
#
mkdir /tmp/d/2
chmod 777 /tmp/d/2
2023-12-08 12:17:04 +03:00
systemd-tmpfiles --dry-run --create - <<EOF
d /tmp/d/1 0755 daemon daemon - -
d /tmp/d/2 0755 daemon daemon - -
EOF
test ! -d /tmp/d/1
test -d /tmp/d/2
2018-04-13 16:32:25 +03:00
systemd-tmpfiles --create - <<EOF
d /tmp/d/1 0755 daemon daemon - -
d /tmp/d/2 0755 daemon daemon - -
EOF
test -d /tmp/d/1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/d/1) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
test -d /tmp/d/2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/d/2) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
#
# 'D'
#
mkdir /tmp/D/2
chmod 777 /tmp/D/2
touch /tmp/D/2/foo
systemd-tmpfiles --create - <<EOF
D /tmp/D/1 0755 daemon daemon - -
D /tmp/D/2 0755 daemon daemon - -
EOF
test -d /tmp/D/1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/D/1) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
test -d /tmp/D/2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/D/2) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
systemd-tmpfiles --remove - <<EOF
D /tmp/D/2 0755 daemon daemon - -
EOF
# the content of '2' should be removed
test " $( echo /tmp/D/2/*) " = "/tmp/D/2/*"
#
# 'e'
#
mkdir -p /tmp/e/2/{ d1,d2}
chmod 777 /tmp/e/2
chmod 777 /tmp/e/2/d*
systemd-tmpfiles --create - <<EOF
e /tmp/e/1 0755 daemon daemon - -
e /tmp/e/2/* 0755 daemon daemon - -
EOF
2021-04-08 00:24:25 +03:00
test ! -d /tmp/e/1
2018-04-13 16:32:25 +03:00
test -d /tmp/e/2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/2) " = "root:root:777"
2018-04-13 16:32:25 +03:00
test -d /tmp/e/2/d1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/2/d1) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
test -d /tmp/e/2/d2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/2/d2) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
# 'e' operates on directories only
mkdir -p /tmp/e/3/{ d1,d2}
chmod 777 /tmp/e/3
chmod 777 /tmp/e/3/d*
touch /tmp/e/3/f1
chmod 644 /tmp/e/3/f1
tmpfiles: make handling of existing-but-different targets more consistent
create_fifo() was added in a2fc2f8dd30c17ad1e23a31fc6ff2aeba4c6fa27, and
would always ignore failure. The test was trying to fail in this case, but
we actually don't fail, which seems to be correct. We didn't notice before
because the test was ineffective.
To make things consistent, generally log at warning level, but don't propagate
the error. For symlinks, log at debug level, as before.
For 'e', failure is not propagated now. The test is adjusted to match.
I think warning is appropriate in most cases: we do not expect a device node to
be replaced by a different device node or even a non-device file. This would
most likely be an error somewhere. An exception is made for symlinks, which are
mismatched on purpose, for example /etc/resolv.conf. With this patch, we don't
get any warnings with the any of the 74 tmpfiles.d files, which suggests that
increasing the warning levels will not cause too many unexpected warnings. If
it turns out that there are valid cases where people have expected mismatches
for non-symlink types, we can always decrease the log levels again.
2021-04-08 01:48:35 +03:00
systemd-tmpfiles --create - <<EOF
2018-04-13 16:32:25 +03:00
e /tmp/e/3/* 0755 daemon daemon - -
EOF
# the directories should have been processed although systemd-tmpfiles failed
# previously due to the presence of a file.
test -d /tmp/e/3/d1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/3/d1) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
test -d /tmp/e/3/d2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/3/d2) " = "daemon:daemon:755"
2018-04-13 16:32:25 +03:00
test -f /tmp/e/3/f1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/e/3/f1) " = "root:root:644"
2019-01-07 03:13:13 +03:00
#
# 'C'
#
tmpfiles: fix specifier expansion in arguments of C lines
Make sure the argument of "C" type undergoes specifier expansion
before it's checked for validity. In particular, starting with
commit ce610af143b2, the check for path existence used to fail
in case of presence of any specifier in the argument.
Also, starting with commit 2f3b873a4973, when the path contains
a specifier and the argument is omitted, tmpfiles used to perform
specifier expansions twice: first specifier expansion was applied
to the path itself, and afterwards the result of the first expansion
was used in the constructed argument and expanded once again.
Finally, starting with commit 849958d1ba35, when the argument begins
with %h specifier, tmpfiles used to complain that the source path
is not absolute.
Resolves: #25381
Fixes: ce610af143b2 ('tmpfiles: in C lines, make missing source graceful error')
Fixes: 2f3b873a4973 ('tmpfiles: copy/link /usr/share/factory/ files when the source argument is omitted')
Fixes: 849958d1ba35 ('tmpfiles: add new "C" line for copying files or directories')
2022-01-27 23:00:00 +03:00
mkdir /tmp/C/{ 0,1,2,3} -origin
2019-01-07 03:13:13 +03:00
touch /tmp/C/{ 1,2,3} -origin/f1
chmod 755 /tmp/C/{ 1,2,3} -origin/f1
mkdir /tmp/C/{ 2,3}
touch /tmp/C/3/f1
2023-12-08 12:17:04 +03:00
systemd-tmpfiles --dry-run --create - <<EOF
C /tmp/C/1 0755 daemon daemon - /tmp/C/1-origin
C /tmp/C/2 0755 daemon daemon - /tmp/C/2-origin
EOF
test ! -d /tmp/C/1
test -d /tmp/C/2
2019-01-07 03:13:13 +03:00
systemd-tmpfiles --create - <<EOF
C /tmp/C/1 0755 daemon daemon - /tmp/C/1-origin
C /tmp/C/2 0755 daemon daemon - /tmp/C/2-origin
EOF
test -d /tmp/C/1
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/C/1/f1) " = "daemon:daemon:755"
2019-01-07 03:13:13 +03:00
test -d /tmp/C/2
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/C/2/f1) " = "daemon:daemon:755"
2019-01-07 03:13:13 +03:00
tmpfiles: make handling of existing-but-different targets more consistent
create_fifo() was added in a2fc2f8dd30c17ad1e23a31fc6ff2aeba4c6fa27, and
would always ignore failure. The test was trying to fail in this case, but
we actually don't fail, which seems to be correct. We didn't notice before
because the test was ineffective.
To make things consistent, generally log at warning level, but don't propagate
the error. For symlinks, log at debug level, as before.
For 'e', failure is not propagated now. The test is adjusted to match.
I think warning is appropriate in most cases: we do not expect a device node to
be replaced by a different device node or even a non-device file. This would
most likely be an error somewhere. An exception is made for symlinks, which are
mismatched on purpose, for example /etc/resolv.conf. With this patch, we don't
get any warnings with the any of the 74 tmpfiles.d files, which suggests that
increasing the warning levels will not cause too many unexpected warnings. If
it turns out that there are valid cases where people have expected mismatches
for non-symlink types, we can always decrease the log levels again.
2021-04-08 01:48:35 +03:00
systemd-tmpfiles --create - <<EOF
2019-01-07 03:13:13 +03:00
C /tmp/C/3 0755 daemon daemon - /tmp/C/3-origin
2022-08-25 18:16:02 +03:00
C /tmp/C/4 0755 daemon daemon - /tmp/C/definitely-missing
2019-01-07 03:13:13 +03:00
EOF
2021-04-09 20:49:32 +03:00
test " $( stat -c %U:%G:%a /tmp/C/3/f1) " = "root:root:644"
2022-08-25 18:16:02 +03:00
test ! -e /tmp/C/4
tmpfiles: fix specifier expansion in arguments of C lines
Make sure the argument of "C" type undergoes specifier expansion
before it's checked for validity. In particular, starting with
commit ce610af143b2, the check for path existence used to fail
in case of presence of any specifier in the argument.
Also, starting with commit 2f3b873a4973, when the path contains
a specifier and the argument is omitted, tmpfiles used to perform
specifier expansions twice: first specifier expansion was applied
to the path itself, and afterwards the result of the first expansion
was used in the constructed argument and expanded once again.
Finally, starting with commit 849958d1ba35, when the argument begins
with %h specifier, tmpfiles used to complain that the source path
is not absolute.
Resolves: #25381
Fixes: ce610af143b2 ('tmpfiles: in C lines, make missing source graceful error')
Fixes: 2f3b873a4973 ('tmpfiles: copy/link /usr/share/factory/ files when the source argument is omitted')
Fixes: 849958d1ba35 ('tmpfiles: add new "C" line for copying files or directories')
2022-01-27 23:00:00 +03:00
2023-05-09 14:45:16 +03:00
touch /tmp/C/3-origin/f{ 2,3,4}
echo -n ABC > /tmp/C/3/f1
systemd-tmpfiles --create - <<EOF
C+ /tmp/C/3 0755 daemon daemon - /tmp/C/3-origin
EOF
# Test that the trees got merged, even though /tmp/C/3 already exists.
test -e /tmp/C/3/f1
test -e /tmp/C/3/f2
test -e /tmp/C/3/f3
test -e /tmp/C/3/f4
# Test that /tmp/C/3/f1 did not get overwritten.
test " $( cat /tmp/C/3/f1) " = "ABC"
tmpfiles: fix specifier expansion in arguments of C lines
Make sure the argument of "C" type undergoes specifier expansion
before it's checked for validity. In particular, starting with
commit ce610af143b2, the check for path existence used to fail
in case of presence of any specifier in the argument.
Also, starting with commit 2f3b873a4973, when the path contains
a specifier and the argument is omitted, tmpfiles used to perform
specifier expansions twice: first specifier expansion was applied
to the path itself, and afterwards the result of the first expansion
was used in the constructed argument and expanded once again.
Finally, starting with commit 849958d1ba35, when the argument begins
with %h specifier, tmpfiles used to complain that the source path
is not absolute.
Resolves: #25381
Fixes: ce610af143b2 ('tmpfiles: in C lines, make missing source graceful error')
Fixes: 2f3b873a4973 ('tmpfiles: copy/link /usr/share/factory/ files when the source argument is omitted')
Fixes: 849958d1ba35 ('tmpfiles: add new "C" line for copying files or directories')
2022-01-27 23:00:00 +03:00
# Check that %U expands to 0, both in the path and in the argument.
home = '/tmp/C'
systemd-tmpfiles --create - <<EOF
C $home /%U - - - - $home /%U-origin
EOF
test -d " $home /0 "
# Check that %h expands to $home, both in the path and in the argument.
HOME = " $home " \
systemd-tmpfiles --create - <<EOF
C %h/5 - - - - %h/3-origin
EOF
test -f " $home /5/f1 "
# Check that %h in the path is expanded, but
# the result of this expansion is not expanded once again.
root = '/tmp/C/6'
home = '/%U'
mkdir -p " $root /usr/share/factory $home "
HOME = " $home " \
systemd-tmpfiles --create --root= " $root " - <<EOF
C %h - - - -
EOF
test -d " $root $home "