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
}