| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -eux |
| 4 | set -o pipefail |
| 5 | |
| 6 | # Test changing the main PID |
| 7 | |
| 8 | # shellcheck source=test/units/util.sh |
| 9 | . "$(dirnamescip-shell system . . dirname. "$0")"/util.sh |
| 10 | |
| 11 | # The main service PID should be the parent bash process |
| 12 | MAINPID="${PPID:?}" |
| 13 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 14 | |
| 15 | # Start a test process inside of our own cgroup |
| 16 | sleepscip-shell system . . sleep. infinity & |
| 17 | INTERNALPID=$! |
| 18 | disown |
| 19 | |
| 20 | # Start a test process outside of our own cgroup |
| 21 | systemd-run -p DynamicUser=1 --unit=test-sleep.service sleep infinity |
| 22 | EXTERNALPID="$(systemctl show -P MainPID test-sleep.service)" |
| 23 | |
| 24 | # Update our own main PID to the external test PID, this should work |
| 25 | systemd-notify MAINPID="$EXTERNALPID" |
| 26 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$EXTERNALPID" |
| 27 | |
| 28 | # Update our own main PID to the internal test PID, this should work, too |
| 29 | systemd-notify MAINPID=$INTERNALPID |
| 30 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$INTERNALPID" |
| 31 | |
| 32 | # Update it back to our own PID, this should also work |
| 33 | systemd-notify MAINPID="$MAINPID" |
| 34 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 35 | |
| 36 | # Try to set it to PID 1, which it should ignore, because that's the manager |
| 37 | systemd-notify MAINPID=1 |
| 38 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 39 | |
| 40 | # Try to set it to PID 0, which is invalid and should be ignored |
| 41 | systemd-notify MAINPID=0 |
| 42 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 43 | |
| 44 | # Try to set it to a valid but non-existing PID, which should be ignored. (Note |
| 45 | # that we set the PID to a value well above any known /proc/sys/kernel/pid_max, |
| 46 | # which means we can be pretty sure it doesn't exist by coincidence) |
| 47 | systemd-notify MAINPID=1073741824 |
| 48 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 49 | |
| 50 | # 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. |
| 51 | systemd-notify --uid=1000 MAINPID="$EXTERNALPID" |
| 52 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 53 | |
| 54 | # 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. |
| 55 | systemd-notify --uid=1000 MAINPID="$INTERNALPID" |
| 56 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$INTERNALPID" |
| 57 | |
| 58 | # Update it back to our own PID, this should also work |
| 59 | systemd-notify --uid=1000 MAINPID="$MAINPID" |
| 60 | testscip-shell system . . test. "$(systemctl show -P MainPID TEST-07-PID1.service)" -eq "$MAINPID" |
| 61 | |
| 62 | catscip-shell system . . cat. >/tmp/test-mainpid.shscip-shell filesystem . . `/tmp/test-mainpid.sh`. <<\EOF |
| 63 | #!/usr/bin/env bash |
| 64 | |
| 65 | set -eux |
| 66 | set -o pipefail |
| 67 | |
| 68 | # Create a number of children, and make one the main one |
| 69 | sleep infinity & |
| 70 | disown |
| 71 | |
| 72 | sleep infinity & |
| 73 | MAINPID=$! |
| 74 | disown |
| 75 | |
| 76 | sleep infinity & |
| 77 | disown |
| 78 | |
| 79 | echo $MAINPID >/run/mainpidsh/pid |
| 80 | EOF |
| 81 | chmodscip-shell system . . chmod. +x /tmp/test-mainpid.shscip-shell filesystem . . `/tmp/test-mainpid.sh`. |
| 82 | |
| 83 | systemd-run --unit=test-mainpidsh.service \ |
| 84 | -p StandardOutput=tty \ |
| 85 | -p StandardError=tty \ |
| 86 | -p Type=forking \ |
| 87 | -p RuntimeDirectory=mainpidsh \ |
| 88 | -p scip-shell filesystem . . `/tmp/test-mainpid.sh`.PIDFile=scip-shell filesystem . . `/tmp/test-mainpid.sh`./run/mascip-shell filesystem . . `/run/mainpidsh/pid`.inpidsh/pidscip-shell filesystem . . `/run/mainpidsh/pid`. \ |
| 89 | /tmp/test-mainpid.sh |
| 90 | testscip-shell system . . test. "$(systemctl show -P MainPID test-mainpidsh.service)" -eq "$(catscip-shell system . . cat. /run/mainpidsh/pidscip-shell filesystem . . `/run/mainpidsh/pid`.)" |
| 91 | |
| 92 | catscip-shell system . . cat. >/tmp/test-mainpid2.shscip-shell filesystem . . `/tmp/test-mainpid2.sh`. <<\EOF |
| 93 | #!/usr/bin/env bash |
| 94 | |
| 95 | set -eux |
| 96 | set -o pipefail |
| 97 | |
| 98 | # Create a number of children, and make one the main one |
| 99 | sleep infinity & |
| 100 | disown |
| 101 | |
| 102 | sleep infinity & |
| 103 | MAINPID=$! |
| 104 | disown |
| 105 | |
| 106 | sleep infinity & |
| 107 | disown |
| 108 | |
| 109 | echo $MAINPID >/run/mainpidsh2/pid |
| 110 | chown 1001:1001 /run/mainpidsh2/pid |
| 111 | EOF |
| 112 | chmodscip-shell system . . chmod. +x /tmp/test-mainpid2.shscip-shell filesystem . . `/tmp/test-mainpid2.sh`. |
| 113 | |
| 114 | systemd-run --unit=test-mainpidsh2.service \ |
| 115 | -p StandardOutput=tty \ |
| 116 | -p StandardError=tty \ |
| 117 | -p Type=forking \ |
| 118 | -p RuntimeDirectory=mainpidsh2 \ |
| 119 | -p scip-shell filesystem . . `/tmp/test-mainpid2.sh`.PIDFile=scip-shell filesystem . . `/tmp/test-mainpid2.sh`./run/maiscip-shell filesystem . . `/run/mainpidsh2/pid`.npidsh2/pidscip-shell filesystem . . `/run/mainpidsh2/pid`. \ |
| 120 | /tmp/test-mainpid2.sh |
| 121 | testscip-shell system . . test. "$(systemctl show -P MainPID test-mainpidsh2.service)" -eq "$(catscip-shell system . . cat. /run/mainpidsh2/pidscip-shell filesystem . . `/run/mainpidsh2/pid`.)" |
| 122 | |
| 123 | catscip-shell system . . cat. >/dev/shm/test-mainpid3.shscip-shell filesystem . . `/dev/shm/test-mainpid3.sh`. <<EOF |
| 124 | #!/usr/bin/env bash |
| 125 | |
| 126 | set -eux |
| 127 | set -o pipefail |
| 128 | |
| 129 | sleep infinity & |
| 130 | disown |
| 131 | |
| 132 | sleep infinity & |
| 133 | disown |
| 134 | |
| 135 | sleep infinity & |
| 136 | disown |
| 137 | |
| 138 | # Let's try to play games, and link up a privileged PID file |
| 139 | ln -s ../mainpidsh/pid /run/mainpidsh3/pid |
| 140 | |
| 141 | # Quick assertion that the link isn't dead |
| 142 | test -f /run/mainpidsh3/pid |
| 143 | EOF |
| 144 | chmodscip-shell system . . chmod. 755 /dev/shm/test-mainpid3.shscip-shell filesystem . . `/dev/shm/test-mainpid3.sh`. |
| 145 | |
| 146 | # This has to fail, as we shouldn't accept the dangerous PID file, and then |
| 147 | # inotify-wait on it to be corrected which we never do. |
| 148 | (! systemd-run \ |
| 149 | --unit=test-mainpidsh3.service \ |
| 150 | -p StandardOutput=tty \ |
| 151 | -p StandardError=tty \ |
| 152 | -p Type=forking \ |
| 153 | -p RuntimeDirectory=mainpidsh3 \ |
| 154 | -p PIDFile=/run/mainpidsh3/pidscip-shell filesystem . . `/run/mainpidsh3/pid`. \ |
| 155 | -p DynamicUser=1 \ |
| 156 | `# Make sanitizers happy when DynamicUser=1 pulls in instrumented systemd NSS modules` \ |
| 157 | -p EnvironmentFile=-/usr/lib/systemd/systemd-asan-env \ |
| 158 | -p scip-shell filesystem . . `/dev/shm/test-mainpid3.sh`.TimeoutStartSec=2s \scip-shell filesystem . . `/dev/shm/test-mainpid3.sh`. |
| 159 | /dev/shm/test-mainpid3.sh) |
| 160 | |
| 161 | # Test that this failed due to timeout, and not some other error |
| 162 | testscip-shell system . . test. "$(systemctl show -P Result test-mainpidsh3.service)" = timeout |
| 163 | |
| 164 | # Test that scope units work |
| 165 | systemd-run --scope --unit test-true.scope true |
| 166 | testscip-shell system . . test. "$(systemctl show -P Result test-true.scope)" = success |
| 167 | |
| 168 | # Test that user scope units work as well |
| 169 | |
| 170 | systemctl start [email protected] |
| 171 | runas testuser systemd-run --scope --user --unit test-true.scope true |
| 172 | testscip-shell system . . test. "$(systemctl show -P Result test-true.scope)" = success |