fix(replace,wm,autostart): preserve workspaces across slow/3rd-party replace and guard polkit agent startup

This commit is contained in:
Luxferre committed 2026-11-27 09:06:02 +02:00
1 parent 4ec3f59e02
commit c5ce065c33
6 files changed
+759 -53

No files matched your search

+164 -8
View File
@@ -120,6 +120,143 @@ if [ -n "$PROFILE" ]; then
fi
fi
MY_UID="$(id -u)"
SELF_PID="$$"
LAUNCHED_BASES=":"
norm_display() {
case "${1:-}" in
*.0) printf '%s\n' "${1%.0}" ;;
*) printf '%s\n' "${1:-}" ;;
esac
}
is_polkit_agent_base() {
case "${1:-}" in
lxpolkit|polkit-gnome-authentication-agent-1|polkit-mate-authentication-agent-1|polkit-kde-authentication-agent-1|xfce-polkit|lxqt-policykit-agent|mate-polkit)
return 0
;;
esac
return 1
}
is_generic_wrapper_base() {
case "${1:-}" in
sh|bash|dash|ksh|zsh|ash|env|nohup|setsid|st|xterm|urxvt|alacritty|kitty|python|python3|perl|ruby|true|false|echo|printf)
return 0
;;
esac
return 1
}
polkit_agent_running_on_display() {
_cands="$( (ps -eo uid=,pid=,comm= 2>/dev/null || ps -ax -o uid=,pid=,comm= 2>/dev/null || true) | awk -v myuid="$MY_UID" -v self="$SELF_PID" '
$1 == myuid && $2 > 1 && $2 != self {
c = $3
sub(/^.*\//, "", c)
if (c == "lxpolkit" ||
c == "polkit-gnome-authentication-agent-1" ||
c == "polkit-mate-authentication-agent-1" ||
c == "polkit-kde-authentication-agent-1" ||
c == "xfce-polkit" ||
c == "lxqt-policykit-agent" ||
c == "mate-polkit") {
print $2
}
}
' )"
[ -n "${_cands:-}" ] || return 1
if [ -n "${DISPLAY:-}" ] && [ -d "/proc/$SELF_PID" ]; then
_cur_disp="$(norm_display "$DISPLAY")"
for _cpid in $_cands; do
[ -r "/proc/$_cpid/environ" ] || continue
_pdisp="$(tr '\0' '\n' < "/proc/$_cpid/environ" 2>/dev/null | awk 'substr($0, 1, 8) == "DISPLAY=" { print substr($0, 9); exit }' || true)"
[ -n "${_pdisp:-}" ] || continue
if [ "$(norm_display "$_pdisp")" = "$_cur_disp" ]; then
return 0
fi
done
return 1
fi
return 0
}
polkit_session_is_invalid() {
[ "${ANODE_FORCE_POLKIT_AGENT:-0}" = "1" ] && return 1
command -v loginctl >/dev/null 2>&1 || return 1
_has_sess="$(loginctl list-sessions 2>/dev/null | awk '
NF >= 2 && $1 != "SESSION" && $1 !~ /listed\.$/ && $2 ~ /^[0-9]+$/ { print $1; exit }
' || true)"
[ -n "${_has_sess:-}" ] || return 1
_sid=""
if [ -r "/proc/$SELF_PID/cgroup" ]; then
_sid="$(awk -F/ '
{
s = $NF
if (s ~ /^session-.*\.scope$/) {
sub(/^session-/, "", s)
sub(/\.scope$/, "", s)
if (s != "") { print s; exit }
} else if ($1 ~ /^0:/ && s != "" && s !~ /\.(slice|service|scope)$/) {
print s
exit
}
}
' "/proc/$SELF_PID/cgroup" 2>/dev/null || true)"
fi
if [ -z "${_sid:-}" ]; then
_sid="${XDG_SESSION_ID:-}"
fi
[ -n "${_sid:-}" ] || return 1
if ! loginctl show-session "$_sid" >/dev/null 2>&1; then
return 0
fi
return 1
}
should_skip_launch() {
_kind="$1"
_base="$2"
[ -n "${_base:-}" ] || return 1
if is_polkit_agent_base "$_base"; then
case "$LAUNCHED_BASES" in
*":$_base:"*) return 0 ;;
esac
if polkit_agent_running_on_display; then
return 0
fi
if polkit_session_is_invalid; then
return 0
fi
return 1
fi
case "$_kind" in
exec_once)
case "$LAUNCHED_BASES" in
*":$_base:"*) return 0 ;;
esac
if is_proc_running "$_base"; then
return 0
fi
;;
desktop)
if ! is_generic_wrapper_base "$_base"; then
case "$LAUNCHED_BASES" in
*":$_base:"*) return 0 ;;
esac
if is_proc_running "$_base"; then
return 0
fi
fi
;;
esac
return 1
}
# Extract executable basename from a shell command line for exec_once tracking
cmd_exec_base() {
awk -v raw="$1" 'BEGIN {
@@ -300,16 +437,17 @@ run_declarative_files() {
fi
_tab="$(printf '\t')"
printf '%s\n' "$_parsed" | while IFS="$_tab" read -r _kind _cmd; do
while IFS="$_tab" read -r _kind _cmd; do
[ -n "${_kind:-}" ] || continue
case "$MODE:$_kind" in
reload:exec|reload:exec_once)
continue
;;
esac
if [ "$_kind" = "exec_once" ] && [ "$DRY_RUN" != "1" ]; then
_base=""
if [ "$DRY_RUN" != "1" ]; then
_base="$(cmd_exec_base "$_cmd")"
if is_proc_running "$_base"; then
if should_skip_launch "$_kind" "$_base"; then
continue
fi
fi
@@ -317,8 +455,13 @@ run_declarative_files() {
printf '[%s] %s\n' "$_kind" "$_cmd"
else
launch_cmd "$_cmd"
if [ -n "$_base" ] && ! is_generic_wrapper_base "$_base"; then
LAUNCHED_BASES="${LAUNCHED_BASES}${_base}:"
fi
fi
done
done <<EOF
$_parsed
EOF
}
# Parse a .desktop autostart file and print:
@@ -438,7 +581,13 @@ run_dir_dropins_and_xdg() {
if [ "$DRY_RUN" = "1" ]; then
printf '[desktop] %s\n' "$_ecmd"
else
launch_cmd "$_ecmd"
_base="$(cmd_exec_base "$_ecmd")"
if ! should_skip_launch desktop "$_base"; then
launch_cmd "$_ecmd"
if [ -n "$_base" ]; then
LAUNCHED_BASES="${LAUNCHED_BASES}${_base}:"
fi
fi
fi
fi
;;
@@ -461,7 +610,9 @@ run_dir_dropins_and_xdg() {
_seen_xdg=":"
_old_ifs="$IFS"
IFS=:
for _xbase in "$XDG_CONFIG_HOME" $XDG_CONFIG_DIRS; do
set -- "$XDG_CONFIG_HOME" $XDG_CONFIG_DIRS
IFS="$_old_ifs"
for _xbase in "$@"; do
[ -n "$_xbase" ] || continue
_xdir="$_xbase/autostart"
[ -d "$_xdir" ] || continue
@@ -482,14 +633,19 @@ run_dir_dropins_and_xdg() {
if [ "$DRY_RUN" = "1" ]; then
printf '[desktop] %s\n' "$_ecmd"
else
launch_cmd "$_ecmd"
_base="$(cmd_exec_base "$_ecmd")"
if ! should_skip_launch desktop "$_base"; then
launch_cmd "$_ecmd"
if [ -n "$_base" ]; then
LAUNCHED_BASES="${LAUNCHED_BASES}${_base}:"
fi
fi
fi
fi
;;
esac
done
done
IFS="$_old_ifs"
fi
}
+40 -7
View File
@@ -11,6 +11,7 @@ PROFILE="${ANODE_SESSION_PROFILE:-}"
KEEP_PANELS=0
FOREGROUND=0
NO_AUTOSTART=0
FORCE_AUTOSTART=0
FORCE=0
SWITCH_SESSION=0
DRY_RUN=0
@@ -26,7 +27,8 @@ Options:
--profile NAME Pass session autostart profile NAME to anode-session
--keep-panels Do not stop existing panel, notification, or xsettings daemons
--foreground Exec anode-session in the foreground instead of detaching
--no-autostart Skip anode-autostart start during replacement
--no-autostart Skip anode-autostart completely during replacement
--autostart Run full anode-autostart start even during replacement
--force Proceed with in-place replacement even if WM is DM session leader
--switch-session Switch to an ANODE LightDM session cleanly (schedules one-shot ~/.xprofile handoff when a user session is already active)
--dry-run Print detected WM and planned actions without replacing
@@ -55,6 +57,12 @@ while [ $# -gt 0 ]; do
;;
--no-autostart)
NO_AUTOSTART=1
FORCE_AUTOSTART=0
shift
;;
--autostart)
FORCE_AUTOSTART=1
NO_AUTOSTART=0
shift
;;
--force)
@@ -474,6 +482,8 @@ if [ -n "$PROFILE" ]; then
fi
if [ "$NO_AUTOSTART" = "1" ]; then
set -- "$@" --no-autostart
elif [ "$FORCE_AUTOSTART" = "1" ]; then
set -- "$@" --autostart
fi
if [ "$DRY_RUN" = "1" ]; then
@@ -525,19 +535,35 @@ if [ "$KEEP_PANELS" != "1" ]; then
dunst xfce4-notifyd xsettingsd xfsettingsd
fi
_snap_dir="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}"
[ -d "$_snap_dir" ] && [ -w "$_snap_dir" ] || _snap_dir="/tmp"
SNAP_READY_FILE="$_snap_dir/anode-replace-snap.$$"
rm -f "$SNAP_READY_FILE"
ANODE_REPLACE_SNAP_FILE="$SNAP_READY_FILE"
ANODE_REPLACE_PREV_WM_WIN="${PREV_WM_WIN:-}"
export ANODE_REPLACE_SNAP_FILE ANODE_REPLACE_PREV_WM_WIN
if [ "$FOREGROUND" = "1" ]; then
if [ -n "${PREV_WM_PID:-}" ] && is_my_pid "$PREV_WM_PID"; then
(
_w=0
while [ "$_w" -lt 40 ] && [ ! -e "$SNAP_READY_FILE" ]; do
_w=$(( _w + 1 ))
sleep 0.1
done
sleep 1.5
_cur_win="$(get_root_wm_win || true)"
if [ -n "${PREV_WM_WIN:-}" ] && [ "$_cur_win" = "$PREV_WM_WIN" ] && kill -0 "$PREV_WM_PID" 2>/dev/null; then
kill -TERM "$PREV_WM_PID" 2>/dev/null || true
fi
rm -f "$SNAP_READY_FILE"
) </dev/null >/dev/null 2>&1 &
fi
exec "$ANODE_SESSION_BIN" "$@"
fi
trap 'rm -f "$SNAP_READY_FILE"' EXIT INT TERM
(
trap '' HUP
exec "$ANODE_SESSION_BIN" "$@"
@@ -545,19 +571,26 @@ fi
SESSION_PID=$!
_tick=0
_snap_tick=""
_fallback_sent=0
_replaced=0
while [ "$_tick" -lt 50 ]; do
while [ "$_tick" -lt 80 ]; do
if ! kill -0 "$SESSION_PID" 2>/dev/null; then
printf 'anode-replace: error: anode-session exited prematurely\n' >&2
exit 1
fi
if [ "$_tick" -ge 15 ] && [ "$_fallback_sent" = "0" ] && [ -n "${PREV_WM_PID:-}" ]; then
_cur_win="$(get_root_wm_win || true)"
if [ -n "${PREV_WM_WIN:-}" ] && [ "$_cur_win" = "$PREV_WM_WIN" ] && is_my_pid "$PREV_WM_PID"; then
kill -TERM "$PREV_WM_PID" 2>/dev/null || true
_fallback_sent=1
if [ -z "$_snap_tick" ] && [ -e "$SNAP_READY_FILE" ]; then
_snap_tick="$_tick"
fi
if [ "$_fallback_sent" = "0" ] && [ -n "${PREV_WM_PID:-}" ]; then
if { [ -n "$_snap_tick" ] && [ $(( _tick - _snap_tick )) -ge 15 ]; } || [ "$_tick" -ge 45 ]; then
_cur_win="$(get_root_wm_win || true)"
if [ -n "${PREV_WM_WIN:-}" ] && [ "$_cur_win" = "$PREV_WM_WIN" ] && is_my_pid "$PREV_WM_PID"; then
kill -TERM "$PREV_WM_PID" 2>/dev/null || true
_fallback_sent=1
fi
fi
fi
+61 -14
View File
@@ -7,6 +7,7 @@ set -u
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
REPLACE_WM=0
NO_AUTOSTART=0
FORCE_AUTOSTART=0
while [ $# -gt 0 ]; do
case "$1" in
@@ -28,6 +29,12 @@ while [ $# -gt 0 ]; do
;;
--no-autostart)
NO_AUTOSTART=1
FORCE_AUTOSTART=0
shift
;;
--autostart)
FORCE_AUTOSTART=1
NO_AUTOSTART=0
shift
;;
*)
@@ -94,7 +101,7 @@ for _envfile in "$ANODE_PREFIX/share/anode/env.sh" "$SCRIPT_DIR/../data/env.sh";
done
unset _envfile
if [ -n "${DISPLAY:-}" ] && command -v anode-apply-display >/dev/null 2>&1; then
if [ "$REPLACE_WM" != "1" ] && [ -n "${DISPLAY:-}" ] && command -v anode-apply-display >/dev/null 2>&1; then
anode-apply-display >/dev/null 2>&1 || true
fi
if [ ! -f "$XDG_CONFIG_HOME/anode/scale.conf" ] && command -v anode-apply-scale >/dev/null 2>&1; then
@@ -116,17 +123,22 @@ if [ "${ANODE_NO_DBUS:-0}" != "1" ]; then
fi
fi
DEFER_THEME_SYNC=0
if [ ! -f "$HOME/.Xresources" ] && command -v anode-apply-theme >/dev/null 2>&1; then
anode-apply-theme Anodized >/dev/null 2>&1 || true
elif [ -f "$HOME/.Xresources" ]; then
if command -v xrdb >/dev/null 2>&1; then
xrdb -merge "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
if command -v anode-sync-gtk-theme >/dev/null 2>&1; then
ANODE_NO_SIGNAL=1 anode-sync-gtk-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
if command -v anode-sync-dunst-theme >/dev/null 2>&1; then
ANODE_NO_SIGNAL=1 anode-sync-dunst-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
if [ "$REPLACE_WM" = "1" ]; then
DEFER_THEME_SYNC=1
else
if command -v anode-sync-gtk-theme >/dev/null 2>&1; then
ANODE_NO_SIGNAL=1 anode-sync-gtk-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
if command -v anode-sync-dunst-theme >/dev/null 2>&1; then
ANODE_NO_SIGNAL=1 anode-sync-dunst-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
fi
fi
@@ -137,7 +149,10 @@ fi
BG_PIDS=""
spawn_bg() {
if command -v "$1" >/dev/null 2>&1; then
"$@" >/dev/null 2>&1 &
(
unset ANODE_REPLACE_SNAP_FILE ANODE_REPLACE_PREV_WM_WIN || true
exec "$@"
) </dev/null >/dev/null 2>&1 &
BG_PIDS="$BG_PIDS $!"
fi
}
@@ -161,6 +176,19 @@ if [ "${ANODE_TOUCH:-0}" = "1" ]; then
spawn_bg anode-vkbd
fi
if [ "$DEFER_THEME_SYNC" = "1" ]; then
(
unset ANODE_REPLACE_SNAP_FILE ANODE_REPLACE_PREV_WM_WIN || true
if command -v anode-sync-gtk-theme >/dev/null 2>&1; then
anode-sync-gtk-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
if command -v anode-sync-dunst-theme >/dev/null 2>&1; then
anode-sync-dunst-theme "$HOME/.Xresources" >/dev/null 2>&1 || true
fi
) </dev/null >/dev/null 2>&1 &
BG_PIDS="$BG_PIDS $!"
fi
# Seed default user autostart files on first session launch
mkdir -p "$XDG_CONFIG_HOME/anode/autostart.d"
if [ ! -e "$XDG_CONFIG_HOME/anode/autostart" ]; then
@@ -173,12 +201,24 @@ if [ ! -e "$XDG_CONFIG_HOME/anode/autostart" ]; then
unset _tpl
fi
_prev_wm_id="${ANODE_REPLACE_PREV_WM_WIN:-}"
if [ "$REPLACE_WM" = "1" ] && [ -z "$_prev_wm_id" ] && [ -n "${DISPLAY:-}" ] && command -v xprop >/dev/null 2>&1; then
_prev_wm_id="$(xprop -root _NET_SUPPORTING_WM_CHECK 2>/dev/null | awk '
/window id/ {
for (i = 1; i <= NF; i++) {
if ($i ~ /^0x[0-9a-fA-F]+/) { sub(/,$/, "", $i); print $i; exit }
}
}
')"
fi
# Launch i3-style and XDG session autostart entries once ANODE DWM is ready
if [ "$NO_AUTOSTART" != "1" ] && command -v anode-autostart >/dev/null 2>&1; then
(
unset ANODE_REPLACE_SNAP_FILE ANODE_REPLACE_PREV_WM_WIN || true
_i=0
if command -v xprop >/dev/null 2>&1; then
while [ "$_i" -lt 50 ]; do
while [ "$_i" -lt 80 ]; do
_wm_id="$(xprop -root _NET_SUPPORTING_WM_CHECK 2>/dev/null | awk '
/window id/ {
for (i = 1; i <= NF; i++) {
@@ -187,10 +227,12 @@ if [ "$NO_AUTOSTART" != "1" ] && command -v anode-autostart >/dev/null 2>&1; the
}
')"
if [ -n "${_wm_id:-}" ]; then
_wm_name="$(xprop -id "$_wm_id" _NET_WM_NAME 2>/dev/null || true)"
case "$_wm_name" in
*ANODE*) break ;;
esac
if [ "$REPLACE_WM" != "1" ] || [ -z "${_prev_wm_id:-}" ] || [ "$_wm_id" != "$_prev_wm_id" ]; then
_wm_name="$(xprop -id "$_wm_id" _NET_WM_NAME 2>/dev/null || true)"
case "$_wm_name" in
*ANODE*) break ;;
esac
fi
fi
_i=$(( _i + 1 ))
sleep 0.1
@@ -198,8 +240,12 @@ if [ "$NO_AUTOSTART" != "1" ] && command -v anode-autostart >/dev/null 2>&1; the
else
sleep 1
fi
anode-autostart start
) >/dev/null 2>&1 &
if [ "$REPLACE_WM" = "1" ] && [ "$FORCE_AUTOSTART" != "1" ]; then
anode-autostart reload
else
anode-autostart start
fi
) </dev/null >/dev/null 2>&1 &
BG_PIDS="$BG_PIDS $!"
fi
@@ -210,6 +256,7 @@ if command -v dwm >/dev/null 2>&1; then
else
dwm || _dwm_rc=$?
fi
unset ANODE_REPLACE_SNAP_FILE ANODE_REPLACE_PREV_WM_WIN || true
if [ "$_dwm_rc" = "75" ]; then
# Replaced by another ICCCM WM: stop old background daemons and hold the
# session open until the replacement WM exits so display managers stay up.