2023-03-21 22:40:52 +03:00
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# shellcheck disable=SC2016
set -eux
set -o pipefail
2023-05-16 20:09:13 +03:00
# shellcheck source=test/units/util.sh
. " $( dirname " $0 " ) " /util.sh
2023-03-21 22:40:52 +03:00
2023-03-28 17:35:35 +03:00
mkfifo /tmp/syncfifo1 /tmp/syncfifo2
sync_in( ) {
read -r x < /tmp/syncfifo1
test " $x " = " $1 "
}
sync_out( ) {
echo " $1 " > /tmp/syncfifo2
}
export SYSTEMD_LOG_LEVEL = debug
2023-03-21 22:40:52 +03:00
systemctl --no-block start notify.service
2023-03-28 17:35:35 +03:00
sync_in a
2023-03-21 22:40:52 +03:00
assert_eq " $( systemctl show notify.service -p NotifyAccess --value) " "all"
2023-03-28 17:35:35 +03:00
assert_eq " $( systemctl show notify.service -p StatusText --value) " "Test starts"
sync_out b
sync_in c
2023-03-21 22:40:52 +03:00
assert_eq " $( systemctl show notify.service -p NotifyAccess --value) " "main"
2023-03-28 17:35:35 +03:00
assert_eq " $( systemctl show notify.service -p StatusText --value) " "Sending READY=1 in an unprivileged process"
2023-03-21 22:40:52 +03:00
assert_rc 3 systemctl --quiet is-active notify.service
2023-03-28 17:35:35 +03:00
sync_out d
sync_in e
2023-03-21 22:40:52 +03:00
systemctl --quiet is-active notify.service
assert_eq " $( systemctl show notify.service -p StatusText --value) " "OK"
assert_eq " $( systemctl show notify.service -p NotifyAccess --value) " "none"
systemctl stop notify.service
assert_eq " $( systemctl show notify.service -p NotifyAccess --value) " "all"
2023-03-28 17:35:35 +03:00
rm /tmp/syncfifo1 /tmp/syncfifo2
2023-04-04 12:41:55 +03:00
# Now test basic fdstore behaviour
2023-03-28 19:28:15 +03:00
MYSCRIPT = " /tmp/myscript $RANDOM .sh "
cat >> " $MYSCRIPT " <<'EOF'
#!/usr/bin/env bash
set -eux
set -o pipefail
2023-04-05 16:48:31 +03:00
test " $FDSTORE " -eq 7
2023-03-28 19:28:15 +03:00
N = " /tmp/ $RANDOM "
echo $RANDOM > " $N "
systemd-notify --fd= 4 --fdname= quux --pid= parent 4< " $N "
rm " $N "
systemd-notify --ready
exec sleep infinity
EOF
chmod +x " $MYSCRIPT "
MYUNIT = " myunit $RANDOM .service "
systemd-run -u " $MYUNIT " -p Type = notify -p StandardOutput = journal+console -p StandardError = journal+console -p FileDescriptorStoreMax = 7 " $MYSCRIPT "
test " $( systemd-analyze fdstore " $MYUNIT " | wc -l) " -eq 2
systemd-analyze fdstore " $MYUNIT " --json= short
systemd-analyze fdstore " $MYUNIT " --json= short | grep -P -q '\[{"fdname":"quux","type":.*,"devno":\[.*\],"inode":.*,"rdevno":null,"path":"/tmp/.*","flags":"ro"}\]'
systemctl stop " $MYUNIT "
rm " $MYSCRIPT "
2023-04-04 12:41:55 +03:00
systemd-analyze log-level debug
# Test fdstore pinning (this will pull in fdstore-pin.service fdstore-nopin.service)
systemctl start fdstore-pin.target
assert_eq " $( systemctl show fdstore-pin.service -P FileDescriptorStorePreserve) " yes
assert_eq " $( systemctl show fdstore-nopin.service -P FileDescriptorStorePreserve) " restart
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " running
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " running
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 1
# The file descriptor store should survive service restarts
systemctl restart fdstore-pin.service fdstore-nopin.service
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " running
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " running
# It should not survive the service stop plus a later start (unless pinned)
systemctl stop fdstore-pin.service fdstore-nopin.service
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 0
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " dead-resources-pinned
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " dead
systemctl start fdstore-pin.service fdstore-nopin.service
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 0
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " running
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " running
systemctl stop fdstore-pin.service fdstore-nopin.service
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 1
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 0
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " dead-resources-pinned
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " dead
systemctl clean fdstore-pin.service --what= fdstore
assert_eq " $( systemctl show fdstore-pin.service -P NFileDescriptorStore) " 0
assert_eq " $( systemctl show fdstore-nopin.service -P NFileDescriptorStore) " 0
assert_eq " $( systemctl show fdstore-pin.service -P SubState) " dead
assert_eq " $( systemctl show fdstore-nopin.service -P SubState) " dead
2023-03-21 22:40:52 +03:00
touch /testok