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