]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/TEST-20-MAINPIDGAMES/testsuite.sh
scripts: use 4 space indentation
[thirdparty/systemd.git] / test / TEST-20-MAINPIDGAMES / testsuite.sh
CommitLineData
db256aab 1#!/bin/bash
db256aab
LP
2set -ex
3set -o pipefail
4
6c34ed51
ZJS
5systemd-analyze log-level debug
6systemd-analyze log-target console
db256aab
LP
7
8test `systemctl show -p MainPID --value testsuite.service` -eq $$
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
16systemd-run -p DynamicUser=1 --unit=sleep.service /bin/sleep infinity
17EXTERNALPID=`systemctl show -p MainPID --value sleep.service`
18
19# Update our own main PID to the external test PID, this should work
20systemd-notify MAINPID=$EXTERNALPID
21test `systemctl show -p MainPID --value testsuite.service` -eq $EXTERNALPID
22
23# Update our own main PID to the internal test PID, this should work, too
24systemd-notify MAINPID=$INTERNALPID
25test `systemctl show -p MainPID --value testsuite.service` -eq $INTERNALPID
26
27# Update it back to our own PID, this should also work
28systemd-notify MAINPID=$$
29test `systemctl show -p MainPID --value testsuite.service` -eq $$
30
31# Try to set it to PID 1, which it should ignore, because that's the manager
32systemd-notify MAINPID=1
33test `systemctl show -p MainPID --value testsuite.service` -eq $$
34
35# Try to set it to PID 0, which is invalid and should be ignored
36systemd-notify MAINPID=0
37test `systemctl show -p MainPID --value testsuite.service` -eq $$
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
43test `systemctl show -p MainPID --value testsuite.service` -eq $$
44
45# Change it again to the external PID, without priviliges this time. This should be ignored, because the PID is from outside of our cgroup and we lack privileges.
46systemd-notify --uid=1000 MAINPID=$EXTERNALPID
47test `systemctl show -p MainPID --value testsuite.service` -eq $$
48
49# Change it again to the internal PID, without priviliges this time. This should work, as the process is on our cgroup, and that's enough even if we lack privileges.
50systemd-notify --uid=1000 MAINPID=$INTERNALPID
51test `systemctl show -p MainPID --value testsuite.service` -eq $INTERNALPID
52
53# Update it back to our own PID, this should also work
54systemd-notify --uid=1000 MAINPID=$$
55test `systemctl show -p MainPID --value testsuite.service` -eq $$
56
57cat >/tmp/mainpid.sh <<EOF
58#!/bin/bash
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
76chmod +x /tmp/mainpid.sh
77
78systemd-run --unit=mainpidsh.service -p StandardOutput=tty -p StandardError=tty -p Type=forking -p RuntimeDirectory=mainpidsh -p PIDFile=/run/mainpidsh/pid /tmp/mainpid.sh
79test `systemctl show -p MainPID --value mainpidsh.service` -eq `cat /run/mainpidsh/pid`
80
81cat >/tmp/mainpid2.sh <<EOF
82#!/bin/bash
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
101chmod +x /tmp/mainpid2.sh
102
103systemd-run --unit=mainpidsh2.service -p StandardOutput=tty -p StandardError=tty -p Type=forking -p RuntimeDirectory=mainpidsh2 -p PIDFile=/run/mainpidsh2/pid /tmp/mainpid2.sh
104test `systemctl show -p MainPID --value mainpidsh2.service` -eq `cat /run/mainpidsh2/pid`
105
106cat >/dev/shm/mainpid3.sh <<EOF
107#!/bin/bash
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
127chmod 755 /dev/shm/mainpid3.sh
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
130! systemd-run --unit=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/mainpid3.sh
131
132# Test that this failed due to timeout, and not some other error
133test `systemctl show -p Result --value mainpidsh3.service` = timeout
134
6c34ed51 135systemd-analyze log-level info
db256aab
LP
136
137echo OK > /testok
138
139exit 0