2 # SPDX-License-Identifier: LGPL-2.1-or-later
6 systemd-analyze log-level debug
8 test "$(systemctl show -P MainPID testsuite-20.service)" -eq $$
10 # Start a test process inside of our own cgroup
15 # Start a test process outside of our own cgroup
16 systemd-run
-p DynamicUser
=1 --unit=test-sleep.service
/bin
/sleep infinity
17 EXTERNALPID
="$(systemctl show -P MainPID test-sleep.service)"
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"
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"
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 $$
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 $$
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 $$
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 $$
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 $$
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"
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 $$
57 cat >/tmp
/test-mainpid.sh
<<EOF
63 # Create a number of children, and make one the main one
74 echo \$MAINPID >/run/mainpidsh/pid
76 chmod +x
/tmp
/test-mainpid.sh
78 systemd-run
--unit=test-mainpidsh.service
-p StandardOutput
=tty
-p StandardError
=tty
-p Type
=forking
-p RuntimeDirectory
=mainpidsh
-p PIDFile
=/run
/mainpidsh
/pid
/tmp
/test-mainpid.sh
79 test "$(systemctl show -P MainPID test-mainpidsh.service)" -eq "$(cat /run/mainpidsh/pid)"
81 cat >/tmp
/test-mainpid2.sh
<<EOF
87 # Create a number of children, and make one the main one
98 echo \$MAINPID >/run/mainpidsh2/pid
99 chown 1001:1001 /run/mainpidsh2/pid
101 chmod +x
/tmp
/test-mainpid2.sh
103 systemd-run
--unit=test-mainpidsh2.service
-p StandardOutput
=tty
-p StandardError
=tty
-p Type
=forking
-p RuntimeDirectory
=mainpidsh2
-p PIDFile
=/run
/mainpidsh
2/pid
/tmp
/test-mainpid2.sh
104 test "$(systemctl show -P MainPID test-mainpidsh2.service)" -eq "$(cat /run/mainpidsh2/pid)"
106 cat >/dev
/shm
/test-mainpid3.sh
<<EOF
121 # Let's try to play games, and link up a privileged PID file
122 ln -s ../mainpidsh/pid /run/mainpidsh3/pid
124 # Quick assertion that the link isn't dead
125 test -f /run/mainpidsh3/pid
127 chmod 755 /dev
/shm
/test-mainpid3.sh
129 # This has to fail, as we shouldn't accept the dangerous PID file, and then
130 # inotify-wait on it to be corrected which we never do.
131 systemd-run
--unit=test-mainpidsh3.service \
132 -p StandardOutput
=tty \
133 -p StandardError
=tty \
135 -p RuntimeDirectory
=mainpidsh3 \
136 -p PIDFile
=/run
/mainpidsh
3/pid \
138 -p TimeoutStartSec
=2s \
139 /dev
/shm
/test-mainpid3.sh \
140 && { echo 'unexpected success'; exit 1; }
142 # Test that this failed due to timeout, and not some other error
143 test "$(systemctl show -P Result test-mainpidsh3.service)" = timeout
145 # Test that scope units work
146 systemd-run
--scope --unit test-true.scope
/bin
/true
147 test "$(systemctl show -P Result test-true.scope)" = success
149 # Test that user scope units work as well
154 XDG_RUNTIME_DIR
=/run
/user
/"$(id -u "$userid")" setpriv
--reuid="$userid" --init-groups "$@"
157 systemctl start user@
4711.service
158 runas testuser systemd-run
--scope --user --unit test-true.scope
/bin
/true
159 test "$(systemctl show -P Result test-true.scope)" = success
161 systemd-analyze log-level info