diff --git a/root/etc/s6-overlay/s6-rc.d/svc-de/down b/root/etc/s6-overlay/s6-rc.d/svc-de/down new file mode 100755 index 00000000..44370a7d --- /dev/null +++ b/root/etc/s6-overlay/s6-rc.d/svc-de/down @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/svc-de/finish diff --git a/root/etc/s6-overlay/s6-rc.d/svc-de/finish b/root/etc/s6-overlay/s6-rc.d/svc-de/finish new file mode 100755 index 00000000..acf5f066 --- /dev/null +++ b/root/etc/s6-overlay/s6-rc.d/svc-de/finish @@ -0,0 +1,46 @@ +#!/usr/bin/with-contenv bash + +# exit early if no pid file +PID_FILE="/de-pid" +WAIT_SECONDS=5 +if [ ! -r "$PID_FILE" ]; then + exit 0 +fi +PARENT_PID=$(cat "$PID_FILE") +if [ -z "$PARENT_PID" ]; then + rm -f "$PID_FILE" + exit 0 +fi + +# get all descendant pids of de +PIDS_TO_KILL=$(pstree -p "$PARENT_PID" | grep -o '([0-9]\+)' | grep -o '[0-9]\+' | grep -v "^${PARENT_PID}$") + +# kill all descendant pids +if [ -z "$PIDS_TO_KILL" ]; then + echo "No desktop processes found to terminate." +else + echo "$PIDS_TO_KILL" | xargs --no-run-if-empty kill -TERM -- 2>/dev/null + echo "Waiting up to ${WAIT_SECONDS} seconds for desktop processes to terminate..." + for ((i=0; i < WAIT_SECONDS * 4; i++)); do + all_gone=1 + for pid in $PIDS_TO_KILL; do + if kill -0 "$pid" 2>/dev/null; then + all_gone=0 + break + fi + done + if [ "$all_gone" -eq 1 ]; then + echo "All desktop processes terminated cleanly." + break + fi + sleep 0.25 + done + if [ "$all_gone" -eq 0 ]; then + echo "Timeout reached. Handing off to s6 for final termination." + fi +fi + +# clean up the PID file. +rm -f "$PID_FILE" + +exit 0 diff --git a/root/etc/s6-overlay/s6-rc.d/svc-de/run b/root/etc/s6-overlay/s6-rc.d/svc-de/run index 24f89869..dbce780e 100755 --- a/root/etc/s6-overlay/s6-rc.d/svc-de/run +++ b/root/etc/s6-overlay/s6-rc.d/svc-de/run @@ -26,4 +26,7 @@ chmod 777 /tmp/selkies* # run cd $HOME exec s6-setuidgid abc \ - /bin/bash /defaults/startwm.sh + /bin/bash /defaults/startwm.sh & +PID=$! +echo "$PID" > /de-pid +wait "$PID"