]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/units/testsuite-20.sh
test: use show -P in places
[thirdparty/systemd.git] / test / units / testsuite-20.sh
CommitLineData
ff12a795 1#!/usr/bin/env bash
db256aab
LP
2set -ex
3set -o pipefail
4
6c34ed51
ZJS
5systemd-analyze log-level debug
6systemd-analyze log-target console
db256aab 7
5522e628 8test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab
LP
9
10# Start a test process inside of our own cgroup
11sleep infinity &
12INTERNALPID=$!
13disown
14
15# Start a test process outside of our own cgroup
c1d6c135 16systemd-run -p DynamicUser=1 --unit=test20-sleep.service /bin/sleep infinity
5522e628 17EXTERNALPID=`systemctl show -P MainPID test20-sleep.service`
db256aab
LP
18
19# Update our own main PID to the external test PID, this should work
20systemd-notify MAINPID=$EXTERNALPID
5522e628 21test `systemctl show -P MainPID testsuite-20.service` -eq $EXTERNALPID
db256aab
LP
22
23# Update our own main PID to the internal test PID, this should work, too
24systemd-notify MAINPID=$INTERNALPID
5522e628 25test `systemctl show -P MainPID testsuite-20.service` -eq $INTERNALPID
db256aab
LP
26
27# Update it back to our own PID, this should also work
28systemd-notify MAINPID=$$
5522e628 29test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab
LP
30
31# Try to set it to PID 1, which it should ignore, because that's the manager
32systemd-notify MAINPID=1
5522e628 33test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab
LP
34
35# Try to set it to PID 0, which is invalid and should be ignored
36systemd-notify MAINPID=0
5522e628 37test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab
LP
38
39# Try to set it to a valid but non-existing PID, which should be ignored. (Note
40# that we set the PID to a value well above any known /proc/sys/kernel/pid_max,
41# which means we can be pretty sure it doesn't exist by coincidence)
42systemd-notify MAINPID=1073741824
5522e628 43test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab 44
5238e957 45# Change it again to the external PID, without privileges this time. This should be ignored, because the PID is from outside of our cgroup and we lack privileges.
db256aab 46systemd-notify --uid=1000 MAINPID=$EXTERNALPID
5522e628 47test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab 48
5238e957 49# Change it again to the internal PID, without privileges this time. This should work, as the process is on our cgroup, and that's enough even if we lack privileges.
db256aab 50systemd-notify --uid=1000 MAINPID=$INTERNALPID
5522e628 51test `systemctl show -P MainPID testsuite-20.service` -eq $INTERNALPID
db256aab
LP
52
53# Update it back to our own PID, this should also work
54systemd-notify --uid=1000 MAINPID=$$
5522e628 55test `systemctl show -P MainPID testsuite-20.service` -eq $$
db256aab 56
c1d6c135 57cat >/tmp/test20-mainpid.sh <<EOF
ff12a795 58#!/usr/bin/env bash
db256aab
LP
59
60set -eux
61set -o pipefail
62
63# Create a number of children, and make one the main one
64sleep infinity &
65disown
66
67sleep infinity &
68MAINPID=\$!
69disown
70
71sleep infinity &
72disown
73
74echo \$MAINPID > /run/mainpidsh/pid
75EOF
c1d6c135 76chmod +x /tmp/test20-mainpid.sh
db256aab 77
c1d6c135 78systemd-run --unit=test20-mainpidsh.service -p StandardOutput=tty -p StandardError=tty -p Type=forking -p RuntimeDirectory=mainpidsh -p PIDFile=/run/mainpidsh/pid /tmp/test20-mainpid.sh
5522e628 79test `systemctl show -P MainPID test20-mainpidsh.service` -eq `cat /run/mainpidsh/pid`
db256aab 80
c1d6c135 81cat >/tmp/test20-mainpid2.sh <<EOF
ff12a795 82#!/usr/bin/env bash
db256aab
LP
83
84set -eux
85set -o pipefail
86
87# Create a number of children, and make one the main one
88sleep infinity &
89disown
90
91sleep infinity &
92MAINPID=\$!
93disown
94
95sleep infinity &
96disown
97
98echo \$MAINPID > /run/mainpidsh2/pid
99chown 1001:1001 /run/mainpidsh2/pid
100EOF
c1d6c135 101chmod +x /tmp/test20-mainpid2.sh
db256aab 102
c1d6c135 103systemd-run --unit=test20-mainpidsh2.service -p StandardOutput=tty -p StandardError=tty -p Type=forking -p RuntimeDirectory=mainpidsh2 -p PIDFile=/run/mainpidsh2/pid /tmp/test20-mainpid2.sh
5522e628 104test `systemctl show -P MainPID test20-mainpidsh2.service` -eq `cat /run/mainpidsh2/pid`
db256aab 105
c1d6c135 106cat >/dev/shm/test20-mainpid3.sh <<EOF
ff12a795 107#!/usr/bin/env bash
db256aab
LP
108
109set -eux
110set -o pipefail
111
112sleep infinity &
113disown
114
115sleep infinity &
116disown
117
118sleep infinity &
119disown
120
121# Let's try to play games, and link up a privileged PID file
122ln -s ../mainpidsh/pid /run/mainpidsh3/pid
123
124# Quick assertion that the link isn't dead
125test -f /run/mainpidsh3/pid
126EOF
c1d6c135 127chmod 755 /dev/shm/test20-mainpid3.sh
db256aab
LP
128
129# This has to fail, as we shouldn't accept the dangerous PID file, and then inotify-wait on it to be corrected which we never do
c1d6c135 130! systemd-run --unit=test20-mainpidsh3.service -p StandardOutput=tty -p StandardError=tty -p Type=forking -p RuntimeDirectory=mainpidsh3 -p PIDFile=/run/mainpidsh3/pid -p DynamicUser=1 -p TimeoutStartSec=2s /dev/shm/test20-mainpid3.sh
db256aab
LP
131
132# Test that this failed due to timeout, and not some other error
5522e628 133test `systemctl show -P Result test20-mainpidsh3.service` = timeout
db256aab 134
6c34ed51 135systemd-analyze log-level info
db256aab
LP
136
137echo OK > /testok
138
139exit 0