2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
7 systemd-analyze set-log-level debug
8 systemd-analyze set-log-target console
10 test `systemctl show -p MainPID --value testsuite.service` -eq $$
12 # Start a test process inside of our own cgroup
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`
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
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
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 $$
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 $$
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 $$
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 $$
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 $$
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
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 $$
59 cat >/tmp
/mainpid.sh
<<EOF
65 # Create a number of children, and make one the main one
76 echo \$MAINPID > /run/mainpidsh/pid
78 chmod +x
/tmp
/mainpid.sh
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`
83 cat >/tmp
/mainpid2.sh
<<EOF
89 # Create a number of children, and make one the main one
100 echo \$MAINPID > /run/mainpidsh2/pid
101 chown 1001:1001 /run/mainpidsh2/pid
103 chmod +x
/tmp
/mainpid2.sh
105 systemd-run
--unit=mainpidsh2.service
-p StandardOutput
=tty
-p StandardError
=tty
-p Type
=forking
-p RuntimeDirectory
=mainpidsh2
-p PIDFile
=/run
/mainpidsh
2/pid
/tmp
/mainpid2.sh
106 test `systemctl show -p MainPID --value mainpidsh2.service` -eq `cat /run/mainpidsh2/pid`
108 cat >/dev
/shm
/mainpid3.sh
<<EOF
123 # Let's try to play games, and link up a privileged PID file
124 ln -s ../mainpidsh/pid /run/mainpidsh3/pid
126 # Quick assertion that the link isn't dead
127 test -f /run/mainpidsh3/pid
129 chmod 755 /dev
/shm
/mainpid3.sh
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
/mainpidsh
3/pid
-p DynamicUser
=1 -p TimeoutStartSec
=2s
/dev
/shm
/mainpid3.sh
134 # Test that this failed due to timeout, and not some other error
135 test `systemctl show -p Result --value mainpidsh3.service` = timeout
137 systemd-analyze set-log-level info