Skip to content

Commit eacba1b

Browse files
committed
sigterm all children of the DE service on container shutdown
1 parent 5120377 commit eacba1b

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/etc/s6-overlay/s6-rc.d/svc-de/finish
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/with-contenv bash
2+
3+
# exit early if no pid file
4+
PID_FILE="/de-pid"
5+
WAIT_SECONDS=5
6+
if [ ! -r "$PID_FILE" ]; then
7+
exit 0
8+
fi
9+
PARENT_PID=$(cat "$PID_FILE")
10+
if [ -z "$PARENT_PID" ]; then
11+
rm -f "$PID_FILE"
12+
exit 0
13+
fi
14+
15+
# get all descendant pids of de
16+
PIDS_TO_KILL=$(pstree -p "$PARENT_PID" | grep -o '([0-9]\+)' | grep -o '[0-9]\+' | grep -v "^${PARENT_PID}$")
17+
18+
# kill all descendant pids
19+
if [ -z "$PIDS_TO_KILL" ]; then
20+
echo "No desktop processes found to terminate."
21+
else
22+
echo "$PIDS_TO_KILL" | xargs --no-run-if-empty kill -TERM -- 2>/dev/null
23+
echo "Waiting up to ${WAIT_SECONDS} seconds for desktop processes to terminate..."
24+
for ((i=0; i < WAIT_SECONDS * 4; i++)); do
25+
all_gone=1
26+
for pid in $PIDS_TO_KILL; do
27+
if kill -0 "$pid" 2>/dev/null; then
28+
all_gone=0
29+
break
30+
fi
31+
done
32+
if [ "$all_gone" -eq 1 ]; then
33+
echo "All desktop processes terminated cleanly."
34+
break
35+
fi
36+
sleep 0.25
37+
done
38+
if [ "$all_gone" -eq 0 ]; then
39+
echo "Timeout reached. Handing off to s6 for final termination."
40+
fi
41+
fi
42+
43+
# clean up the PID file.
44+
rm -f "$PID_FILE"
45+
46+
exit 0

root/etc/s6-overlay/s6-rc.d/svc-de/run

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ chmod 777 /tmp/selkies*
2626
# run
2727
cd $HOME
2828
exec s6-setuidgid abc \
29-
/bin/bash /defaults/startwm.sh
29+
/bin/bash /defaults/startwm.sh &
30+
PID=$!
31+
echo "$PID" > /de-pid
32+
wait "$PID"

0 commit comments

Comments
 (0)