]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-20.sh
test: use show -P in places
[thirdparty/systemd.git] / test / units / testsuite-20.sh
1 #!/usr/bin/env bash
2 set -ex
3 set -o pipefail
4
5 systemd-analyze log-level debug
6 systemd-analyze log-target console
7
8 test `systemctl show -P MainPID testsuite-20.service` -eq $$
9
10 # Start a test process inside of our own cgroup
11 sleep infinity &
12 INTERNALPID=$!
13 disown
14
15 # Start a test process outside of our own cgroup
16 systemd-run -p DynamicUser=1 --unit=test20-sleep.service /bin/sleep infinity
17 EXTERNALPID=`systemctl show -P MainPID test20-sleep.service`
18
19 # Update our own main PID to the external test PID, this should work
20 systemd-notify MAINPID=$EXTERNALPID
21 test `systemctl show -P MainPID testsuite-20.service` -eq $EXTERNALPID
22
23 # Update our own main PID to the internal test PID, this should work, too
24 systemd-notify MAINPID=$INTERNALPID
25 test `systemctl show -P MainPID testsuite-20.service` -eq $INTERNALPID
26
27 # Update it back to our own PID, this should also work
28 systemd-notify MAINPID=$$
29 test `systemctl show -P MainPID testsuite-20.service` -eq $$
30
31 # Try to set it to PID 1, which it should ignore, because that's the manager
32 systemd-notify MAINPID=1
33 test `systemctl show -P MainPID testsuite-20.service` -eq $$
34
35 # Try to set it to PID 0, which is invalid and should be ignored
36 systemd-notify MAINPID=0
37 test `systemctl show -P MainPID testsuite-20.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)
42 systemd-notify MAINPID=1073741824
43 test `systemctl show -P MainPID testsuite-20.service` -eq $$
44
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.
46 systemd-notify --uid=1000 MAINPID=$EXTERNALPID
47 test `systemctl show -P MainPID testsuite-20.service` -eq $$
48
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.
50 systemd-notify --uid=1000 MAINPID=$INTERNALPID
51 test `systemctl show -P MainPID testsuite-20.service` -eq $INTERNALPID
52
53 # Update it back to our own PID, this should also work
54 systemd-notify --uid=1000 MAINPID=$$
55 test `systemctl show -P MainPID testsuite-20.service` -eq $$
56
57 cat >/tmp/test20-mainpid.sh <<EOF
58 #!/usr/bin/env bash
59
60 set -eux
61 set -o pipefail
62
63 # Create a number of children, and make one the main one
64 sleep infinity &
65 disown
66
67 sleep infinity &
68 MAINPID=\$!
69 disown
70
71 sleep infinity &
72 disown
73
74 echo \$MAINPID > /run/mainpidsh/pid
75 EOF
76 chmod +x /tmp/test20-mainpid.sh
77
78 systemd-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
79 test `systemctl show -P MainPID test20-mainpidsh.service` -eq `cat /run/mainpidsh/pid`
80
81 cat >/tmp/test20-mainpid2.sh <<EOF
82 #!/usr/bin/env bash
83
84 set -eux
85 set -o pipefail
86
87 # Create a number of children, and make one the main one
88 sleep infinity &
89 disown
90
91 sleep infinity &
92 MAINPID=\$!
93 disown
94
95 sleep infinity &
96 disown
97
98 echo \$MAINPID > /run/mainpidsh2/pid
99 chown 1001:1001 /run/mainpidsh2/pid
100 EOF
101 chmod +x /tmp/test20-mainpid2.sh
102
103 systemd-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
104 test `systemctl show -P MainPID test20-mainpidsh2.service` -eq `cat /run/mainpidsh2/pid`
105
106 cat >/dev/shm/test20-mainpid3.sh <<EOF
107 #!/usr/bin/env bash
108
109 set -eux
110 set -o pipefail
111
112 sleep infinity &
113 disown
114
115 sleep infinity &
116 disown
117
118 sleep infinity &
119 disown
120
121 # Let's try to play games, and link up a privileged PID file
122 ln -s ../mainpidsh/pid /run/mainpidsh3/pid
123
124 # Quick assertion that the link isn't dead
125 test -f /run/mainpidsh3/pid
126 EOF
127 chmod 755 /dev/shm/test20-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=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
131
132 # Test that this failed due to timeout, and not some other error
133 test `systemctl show -P Result test20-mainpidsh3.service` = timeout
134
135 systemd-analyze log-level info
136
137 echo OK > /testok
138
139 exit 0