Debian Codegraph @

debian/tests/logind

raw
1 #!/bin/sh
2 set -e
3
4 test_started() {
5 # ensure the *old* logind from before the upgrade isn't running
6 echo " * try-restarting systemd-logind"
7 systemctl try-restart systemd-logind
8
9 echo " * daemon is started"
10 # should start at boot, not with D-BUS activation
11 LOGINDPID=$(pidof systemd-logind)
12
13 # loginctl should succeed
14 echo " * loginctl succeeds"
15 LOGINCTL_OUT=`loginctl`
16 }
17
18 test_properties() {
19 # Default KillUserProcesses should be off for debian/ubuntu builds
20 r=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses)
21 [ "$r" = "b false" ]
22 }
23
24 # args: <timeout>
25 wait_suspend() {
26 timeout=$1
27 while [ $timeout -gt 0 ] && [ ! -e /run/suspend.flag ]; do
28 sleep 1
29 timeout=$((timeout - 1))
30 [ $(($timeout % 5)) -ne 0 ] || echo " waiting for suspend, ${timeout}s remaining..."
31 done
32 if [ ! -e /run/suspend.flag ]; then
33 echo "closing lid did not cause suspend" >&2
34 exit 1
35 fi
36 rm /run/suspend.flag
37 echo " * closing lid caused suspend"
38 }
39
40 test_suspend_on_lid() {
41 if systemd-detect-virt --quiet --container; then
42 echo " * Skipping suspend test in container"
43 return
44 fi
45 if ! grep -s -q mem /sys/power/state; then
46 echo " * suspend not supported on this testbed, skipping"
47 return
48 fi
49
50 # cleanup handler
51 trap 'rm -f /run/udev/rules.d/70-logindtest-*.rules; udevadm control --reload;
52 kill $KILL_PID;
53 rm /run/systemd/system/systemd-suspend.service.d/override.conf;
54 if [ -d /sys/module/scsi_debug ]; then rmmod scsi_debug 2>/dev/null || (sleep 2; rmmod scsi_debug ) || true; fi' \
55 EXIT INT QUIT TERM PIPE
56
57 # watch what's going on
58 journalctl -f -u systemd-logind.service -u systemd-suspend.service &
59 KILL_PID="$KILL_PID $!"
60
61 # create fake suspend
62 mkdir -p /run/systemd/system/systemd-suspend.service.d
63 cat >/run/systemd/system/systemd-suspend.service.d/override.conf <<EOF
64 [Service]
65 ExecStart=
66 ExecStart=touch /run/suspend.flag
67 EOF
68 sync
69 systemctl daemon-reload
70
71 # create fake lid switch
72 mkdir -p /run/udev/rules.d
73 echo 'SUBSYSTEM=="input", KERNEL=="event*", ATTRS{name}=="Fake Lid Switch", TAG+="power-switch"' \
74 > /run/udev/rules.d/70-logindtest-lid.rules
75 sync
76 udevadm control --reload
77 evemu-device $(dirname $0)/lidswitch.evemu &
78 KILL_PID="$KILL_PID $!"
79 while [ -z "$O" ]; do
80 sleep 0.1
81 O=$(grep -l '^Fake Lid Switch' /sys/class/input/*/device/name)
82 done
83 O=${O%/device/name}
84 LID_DEV=/dev/${O#/sys/class/}
85 udevadm info --wait-for-initialization=10s $LID_DEV
86 udevadm settle
87
88 # close lid
89 evemu-event $LID_DEV --sync --type 5 --code 0 --value 1
90 # need to wait for 30s suspend inhibition after boot
91 wait_suspend 31
92 # open lid again
93 evemu-event $LID_DEV --sync --type 5 --code 0 --value 0
94
95 echo " * waiting for 30s inhibition time between suspends"
96 sleep 30
97
98 # now closing lid should cause instant suspend
99 evemu-event $LID_DEV --sync --type 5 --code 0 --value 1
100 wait_suspend 2
101 evemu-event $LID_DEV --sync --type 5 --code 0 --value 0
102
103 P=$(pidof systemd-logind)
104 [ "$P" = "$LOGINDPID" ] || { echo "logind crashed" >&2; exit 1; }
105 }
106
107 test_shutdown() {
108 echo " * scheduled shutdown with wall message"
109 shutdown 2>&1
110 sleep 5
111 shutdown -c || true
112 # logind should still be running
113 P=$(pidof systemd-logind)
114 [ "$P" = "$LOGINDPID" ] || { echo "logind crashed" >&2; exit 1; }
115
116 echo " * scheduled shutdown without wall message"
117 shutdown --no-wall 2>&1
118 sleep 5
119 shutdown -c --no-wall || true
120 P=$(pidof systemd-logind)
121 [ "$P" = "$LOGINDPID" ] || { echo "logind crashed" >&2; exit 1; }
122 }
123
124 test_in_logind_session() {
125 echo " * XDG_SESSION_ID=$XDG_SESSION_ID"
126 # cgroup v1: "1:name=systemd:/user.slice/..."; unified hierarchy: "0::/user.slice"
127 if grep -E '(name=systemd|^0:):.*session.*scope' /proc/self/cgroup; then
128 echo " * process is in session cgroup"
129 else
130 echo "FAIL: process is not in session cgroup"
131 echo "/proc/self/cgroup:"
132 cat /proc/self/cgroup
133 loginctl
134 loginctl show-session "$XDG_SESSION_ID"
135 exit 1
136 fi
137 }
138
139 test_acl() {
140 # ACL tests
141 if ! echo "$LOGINCTL_OUT" | grep -q "seat0"; then
142 echo " * Skipping ACL tests, as there is no seat"
143 return
144 fi
145 if systemd-detect-virt --quiet --container; then
146 echo " * Skipping ACL tests in container"
147 return
148 fi
149
150 # determine user
151 USER=`echo "$OUT" | grep seat0 | awk '{print $3}'`
152 echo "seat user: $USER"
153
154 # scsi_debug should not be loaded yet
155 ! test -d /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block
156
157 # we use scsi_debug to create new devices which we can put ACLs on
158 # tell udev about the tagging, so that logind can pick it up
159 cat <<EOF > /run/udev/rules.d/70-logindtest-scsi_debug-user.rules
160 SUBSYSTEM=="block", ATTRS{model}=="scsi_debug*", TAG+="uaccess"
161 EOF
162 sync
163 udevadm control --reload
164
165 echo " * coldplug: logind started with existing device"
166 killall systemd-logind
167 modprobe scsi_debug
168 while ! dev=/dev/`ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block 2>/dev/null`; do sleep 0.1; done
169 test -b $dev
170 echo "got block device $dev"
171 udevadm settle
172 # trigger logind
173 loginctl > /dev/null
174 sleep 1
175 if getfacl -p $dev | grep -q "user:$USER:rw-"; then
176 echo "$dev has ACL for user $USER"
177 else
178 echo "$dev has no ACL for user $USER:" >&2
179 getfacl -p $dev >&2
180 exit 1
181 fi
182
183 rmmod scsi_debug
184
185 echo " * hotplug: new device appears while logind is running"
186 modprobe scsi_debug
187 while ! dev=/dev/`ls /sys/bus/pseudo/drivers/scsi_debug/adapter*/host*/target*/*:*/block`; do sleep 0.1; done
188 test -b $dev
189 echo "got block device $dev"
190 udevadm settle
191 sleep 1
192 if getfacl -p $dev | grep -q "user:$USER:rw-"; then
193 echo "$dev has ACL for user $USER"
194 else
195 echo "$dev has no ACL for user $USER:" >&2
196 getfacl -p $dev >&2
197 exit 1
198 fi
199 }
200
201 #
202 # main
203 #
204
205 # Make sure we are running with the logind package under test instead of the one
206 # from the base distro
207 if [ -z "$AUTOPKGTEST_REBOOT_MARK" ]; then
208 /tmp/autopkgtest-reboot 1
209 fi
210
211 test_started
212 test_properties
213 test_in_logind_session
214 test_suspend_on_lid
215 test_shutdown
216 test_acl