mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-28 08:20:04 +08:00
feat(linux): preserve Service state across upgrades and surface failures
Reinstall an existing Service while keeping its enabled, runtime-masked and active state. Detect, from the journal, a running Service removed by a previous deb prerm and reinstall it with its recorded group. Package script output never reaches the app, so leave a /run marker that makes the app offer a reinstall once when the Service is not ready. Hold the core lifecycle until the package manager exits, even after the startup install times out, so no core start overlaps the Service reinstall; restart the app once a late install succeeds. Refs #8515 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -5,42 +5,63 @@ chmod +x /usr/bin/clash-verge-service-install
|
||||
chmod +x /usr/bin/clash-verge-service-uninstall
|
||||
chmod +x /usr/bin/clash-verge-service
|
||||
|
||||
# Failures only warn: dpkg and rpm cannot restore the previous Service from a failed postinst,
|
||||
# and a failing script would leave the upgraded package unconfigured or report the update as failed.
|
||||
reinstall_existing_service() {
|
||||
local unit="/etc/systemd/system/clash-verge-service.service"
|
||||
# Only a unit kept by an upgrade-aware removal script shows the Service existed before this upgrade.
|
||||
if [ ! -f "$unit" ] || [ -L "$unit" ]; then
|
||||
return 0
|
||||
fi
|
||||
if [ ! -d /run/systemd/system ]; then
|
||||
echo "Warning: systemd is not running, so Clash Verge Service was not reinstalled; reinstall it from Clash Verge." >&2
|
||||
return 0
|
||||
fi
|
||||
if ! systemctl is-enabled --quiet clash-verge-service.service; then
|
||||
echo "Clash Verge Service is disabled; leaving it unchanged." >&2
|
||||
return 0
|
||||
fi
|
||||
# Package managers run without the invoking user's identity, so keep the group that may use the Service.
|
||||
local group gid
|
||||
group="$(sed -n 's/^Group=//p' "$unit" | head -n 1)"
|
||||
gid=""
|
||||
if [ -n "$group" ]; then
|
||||
gid="$(getent group "$group" | cut -d: -f3)"
|
||||
fi
|
||||
if [ -z "$gid" ]; then
|
||||
echo "Warning: the Clash Verge Service group '$group' could not be resolved, so the Service was not reinstalled; reinstall it from Clash Verge." >&2
|
||||
return 0
|
||||
fi
|
||||
if ! CLASH_VERGE_SERVICE_GID="$gid" /usr/bin/clash-verge-service-install --install-service; then
|
||||
echo "Warning: reinstalling Clash Verge Service failed; reinstall it from Clash Verge." >&2
|
||||
fi
|
||||
SERVICE_UNIT="clash-verge-service.service"
|
||||
SERVICE_UNIT_FILE="/etc/systemd/system/$SERVICE_UNIT"
|
||||
# Read by the app, which offers a reinstall when the package could not keep the Service.
|
||||
UPGRADE_MARKER="/run/clash-verge-service-upgrade"
|
||||
|
||||
write_upgrade_marker() {
|
||||
(umask 022 && printf 'gid=%s\nid=%s\n' "$1" "$(date +%s%N)" >"$UPGRADE_MARKER")
|
||||
}
|
||||
|
||||
# Upgrades keep an existing Service current; first installs never add one. No success is reported:
|
||||
# a previous package whose %preun uninstalls unconditionally still removes the Service afterwards.
|
||||
if [ "$1" -ge 2 ]; then
|
||||
reinstall_existing_service
|
||||
existing_service_gid() {
|
||||
local group
|
||||
group="$(sed -n 's/^Group=//p' "$SERVICE_UNIT_FILE" | head -n 1)"
|
||||
if [ -n "$group" ]; then
|
||||
getent group "$group" | cut -d: -f3
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Failures only warn: dpkg and rpm cannot restore the previous Service from a failed postinst,
|
||||
# and a failing script would leave the package unconfigured or make the app report a failed update.
|
||||
# The installer always enables and starts the unit, so the previous enablement and activity are restored.
|
||||
reinstall_existing_service() {
|
||||
local gid="$1" enabled active=no
|
||||
enabled="$(systemctl is-enabled "$SERVICE_UNIT" 2>/dev/null)"
|
||||
if systemctl is-active --quiet "$SERVICE_UNIT"; then
|
||||
active=yes
|
||||
fi
|
||||
if [ "$enabled" = "masked-runtime" ]; then
|
||||
systemctl unmask --runtime --quiet "$SERVICE_UNIT"
|
||||
fi
|
||||
local status=0
|
||||
if ! CLASH_VERGE_SERVICE_GID="$gid" /usr/bin/clash-verge-service-install --install-service; then
|
||||
echo "Warning: reinstalling Clash Verge Service failed; Clash Verge will offer to reinstall it." >&2
|
||||
status=1
|
||||
fi
|
||||
case "$enabled" in
|
||||
disabled) systemctl disable --quiet "$SERVICE_UNIT" ;;
|
||||
masked-runtime) systemctl mask --runtime --quiet "$SERVICE_UNIT" ;;
|
||||
esac
|
||||
if [ "$active" = "no" ]; then
|
||||
systemctl stop "$SERVICE_UNIT"
|
||||
fi
|
||||
return "$status"
|
||||
}
|
||||
|
||||
# Upgrades keep an existing Service current; first installs and explicit removals never add one.
|
||||
# A symlinked unit is masked or managed by the administrator and is left alone.
|
||||
# The marker is written first: a previous package whose %preun uninstalls unconditionally
|
||||
# still removes the Service after this script, and the app only acts on it if the Service is not ready.
|
||||
if [ "$1" -ge 2 ] && [ -d /run/systemd/system ] && [ -f "$SERVICE_UNIT_FILE" ] && [ ! -L "$SERVICE_UNIT_FILE" ]; then
|
||||
gid="$(existing_service_gid)"
|
||||
write_upgrade_marker "$gid"
|
||||
if [ -z "$gid" ]; then
|
||||
echo "Warning: the Clash Verge Service group could not be resolved; Clash Verge will offer to reinstall the Service." >&2
|
||||
else
|
||||
reinstall_existing_service "$gid" || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# The enforcement file exists in both Enforcing and Permissive mode.
|
||||
|
||||
@@ -3,41 +3,75 @@ chmod +x /usr/bin/clash-verge-service-install
|
||||
chmod +x /usr/bin/clash-verge-service-uninstall
|
||||
chmod +x /usr/bin/clash-verge-service
|
||||
|
||||
# Failures only warn: dpkg and rpm cannot restore the previous Service from a failed postinst,
|
||||
# and a failing script would leave the upgraded package unconfigured or report the update as failed.
|
||||
reinstall_existing_service() {
|
||||
local unit="/etc/systemd/system/clash-verge-service.service"
|
||||
# Only a unit kept by an upgrade-aware removal script shows the Service existed before this upgrade.
|
||||
if [ ! -f "$unit" ] || [ -L "$unit" ]; then
|
||||
return 0
|
||||
fi
|
||||
if [ ! -d /run/systemd/system ]; then
|
||||
echo "Warning: systemd is not running, so Clash Verge Service was not reinstalled; reinstall it from Clash Verge." >&2
|
||||
return 0
|
||||
fi
|
||||
if ! systemctl is-enabled --quiet clash-verge-service.service; then
|
||||
echo "Clash Verge Service is disabled; leaving it unchanged." >&2
|
||||
return 0
|
||||
fi
|
||||
# Package managers run without the invoking user's identity, so keep the group that may use the Service.
|
||||
local group gid
|
||||
group="$(sed -n 's/^Group=//p' "$unit" | head -n 1)"
|
||||
gid=""
|
||||
if [ -n "$group" ]; then
|
||||
gid="$(getent group "$group" | cut -d: -f3)"
|
||||
fi
|
||||
if [ -z "$gid" ]; then
|
||||
echo "Warning: the Clash Verge Service group '$group' could not be resolved, so the Service was not reinstalled; reinstall it from Clash Verge." >&2
|
||||
return 0
|
||||
fi
|
||||
if ! CLASH_VERGE_SERVICE_GID="$gid" /usr/bin/clash-verge-service-install --install-service; then
|
||||
echo "Warning: reinstalling Clash Verge Service failed; reinstall it from Clash Verge." >&2
|
||||
fi
|
||||
SERVICE_UNIT="clash-verge-service.service"
|
||||
SERVICE_UNIT_FILE="/etc/systemd/system/$SERVICE_UNIT"
|
||||
# Read by the app, which offers a reinstall when the package could not keep the Service.
|
||||
UPGRADE_MARKER="/run/clash-verge-service-upgrade"
|
||||
|
||||
write_upgrade_marker() {
|
||||
(umask 022 && printf 'gid=%s\nid=%s\n' "$1" "$(date +%s%N)" >"$UPGRADE_MARKER")
|
||||
}
|
||||
|
||||
# Upgrades keep an existing Service current; first installs never add one.
|
||||
if [ "$1" = "configure" ] && [ -n "$2" ]; then
|
||||
reinstall_existing_service
|
||||
existing_service_gid() {
|
||||
local group
|
||||
group="$(sed -n 's/^Group=//p' "$SERVICE_UNIT_FILE" | head -n 1)"
|
||||
if [ -n "$group" ]; then
|
||||
getent group "$group" | cut -d: -f3
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Failures only warn: dpkg and rpm cannot restore the previous Service from a failed postinst,
|
||||
# and a failing script would leave the package unconfigured or make the app report a failed update.
|
||||
# The installer always enables and starts the unit, so the previous enablement and activity are restored.
|
||||
reinstall_existing_service() {
|
||||
local gid="$1" enabled active=no
|
||||
enabled="$(systemctl is-enabled "$SERVICE_UNIT" 2>/dev/null)"
|
||||
if systemctl is-active --quiet "$SERVICE_UNIT"; then
|
||||
active=yes
|
||||
fi
|
||||
if [ "$enabled" = "masked-runtime" ]; then
|
||||
systemctl unmask --runtime --quiet "$SERVICE_UNIT"
|
||||
fi
|
||||
local status=0
|
||||
if ! CLASH_VERGE_SERVICE_GID="$gid" /usr/bin/clash-verge-service-install --install-service; then
|
||||
echo "Warning: reinstalling Clash Verge Service failed; Clash Verge will offer to reinstall it." >&2
|
||||
status=1
|
||||
fi
|
||||
case "$enabled" in
|
||||
disabled) systemctl disable --quiet "$SERVICE_UNIT" ;;
|
||||
masked-runtime) systemctl mask --runtime --quiet "$SERVICE_UNIT" ;;
|
||||
esac
|
||||
if [ "$active" = "no" ]; then
|
||||
systemctl stop "$SERVICE_UNIT"
|
||||
fi
|
||||
return "$status"
|
||||
}
|
||||
|
||||
# Upgrades keep an existing Service current; first installs and explicit removals never add one.
|
||||
# A symlinked unit is masked or managed by the administrator and is left alone.
|
||||
if [ "$1" = "configure" ] && [ -n "$2" ] && [ -d /run/systemd/system ]; then
|
||||
if [ -f "$SERVICE_UNIT_FILE" ] && [ ! -L "$SERVICE_UNIT_FILE" ]; then
|
||||
gid="$(existing_service_gid)"
|
||||
if [ -z "$gid" ]; then
|
||||
echo "Warning: the Clash Verge Service group could not be resolved; Clash Verge will offer to reinstall the Service." >&2
|
||||
write_upgrade_marker ""
|
||||
elif reinstall_existing_service "$gid"; then
|
||||
rm -f "$UPGRADE_MARKER"
|
||||
else
|
||||
write_upgrade_marker "$gid"
|
||||
fi
|
||||
elif [ -f "$UPGRADE_MARKER" ]; then
|
||||
# The preinst found that the previous package's prerm removed a running Service.
|
||||
gid="$(sed -n 's/^gid=//p' "$UPGRADE_MARKER")"
|
||||
if [ -z "$gid" ]; then
|
||||
echo "Warning: the previous package removed Clash Verge Service and its group is unknown; Clash Verge will offer to reinstall it." >&2
|
||||
elif CLASH_VERGE_SERVICE_GID="$gid" /usr/bin/clash-verge-service-install --install-service; then
|
||||
rm -f "$UPGRADE_MARKER"
|
||||
else
|
||||
echo "Warning: reinstalling Clash Verge Service failed; Clash Verge will offer to reinstall it." >&2
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
. /etc/os-release
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
# Previous packages' prerm uninstalls the Service before this script runs, deleting the unit that names
|
||||
# its group. The journal still shows whether this dpkg run stopped a running Service and which group it ran with;
|
||||
# the postinst reinstalls from that evidence, and without it nothing is installed.
|
||||
SERVICE_UNIT="clash-verge-service.service"
|
||||
UPGRADE_MARKER="/run/clash-verge-service-upgrade"
|
||||
# systemd's "unit stopped" catalog entry.
|
||||
UNIT_STOPPED_MESSAGE_ID="9d1aaa27d60140bd96365438aad20286"
|
||||
|
||||
dpkg_start_epoch() {
|
||||
local stat start btime ticks
|
||||
read -r stat <"/proc/$PPID/stat" || return 1
|
||||
start="$(printf '%s\n' "${stat##*) }" | cut -d' ' -f20)"
|
||||
btime="$(sed -n 's/^btime //p' /proc/stat)"
|
||||
ticks="$(getconf CLK_TCK)"
|
||||
[ -n "$start" ] && [ -n "$btime" ] && [ -n "$ticks" ] || return 1
|
||||
echo $((btime + start / ticks))
|
||||
}
|
||||
|
||||
if [ "$1" = "upgrade" ] && [ ! -e "/etc/systemd/system/$SERVICE_UNIT" ] && [ -d /run/systemd/system ] &&
|
||||
[ "$(cat "/proc/$PPID/comm" 2>/dev/null)" = "dpkg" ] && command -v journalctl >/dev/null; then
|
||||
since="$(dpkg_start_epoch)"
|
||||
if [ -n "$since" ] &&
|
||||
[ -n "$(journalctl -q --no-pager --since "@$since" _PID=1 UNIT="$SERVICE_UNIT" MESSAGE_ID="$UNIT_STOPPED_MESSAGE_ID" -o cat 2>/dev/null)" ]; then
|
||||
# The truncated process name excludes cores the Service spawned in its cgroup.
|
||||
gid="$(journalctl -q --no-pager -b _SYSTEMD_UNIT="$SERVICE_UNIT" _COMM=clash-verge-ser -n 1 -o export 2>/dev/null |
|
||||
sed -n 's/^_GID=//p')"
|
||||
if [ -z "$gid" ] || [ "$gid" = "0" ] || ! getent group "$gid" >/dev/null; then
|
||||
gid=""
|
||||
fi
|
||||
(umask 022 && printf 'gid=%s\nid=%s\n' "$gid" "$(date +%s%N)" >"$UPGRADE_MARKER")
|
||||
fi
|
||||
fi
|
||||
exit 0
|
||||
Reference in New Issue
Block a user