zapretgui/packaging/linux/appimage/AppRun
loop-uh 9edcd342a1
All checks were successful
Zapret source guards / validate (push) Successful in 9s
Windows-сборка / Dev Dev 21.1.7.304 опубликован
Linux: the helper service works under systemd, OpenRC and sysvinit (Devuan), chosen by the machine itself
initsys.sh finds the init system (running systemd, then OpenRC, then sysvinit, else none) and installs/starts/stops/removes the service
for it: the unit, an openrc-run script or an LSB script. install.sh, uninstall.sh and the .deb scripts use it; the .deb no longer
depends on systemd; the AppImage asks for the helper's socket instead of systemctl; hints in the program name the command of the
init system found. The OpenRC and the sysv scripts were run for real (Debian 13 chroot with OpenRC, start-stop-daemon).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 09:04:01 -05:00

208 lines
11 KiB
Shell
Executable file

#!/bin/sh
# Start of the AppImage. The window needs the system part (the root helper, the service that owns the firewall table,
# the group) of the SAME version. The helper reads the program files from /opt/zapretgui, and root cannot read an
# AppImage's own mount (it belongs to the user), so on first start (or after an update) the files are copied to a
# temporary folder and install.sh puts them into /opt/zapretgui. After that the installed program runs, like the .deb.
PREFIX="${ZAPRETGUI_PREFIX:-/opt/zapretgui}" # a test points it at a scratch folder
HERE="$(dirname "$(readlink -f "$0")")"
ROOT="$HERE/zapretgui-root"
export PYTHONUTF8=1
# The compiled program ignores PYTHONUTF8; its file-name and text encoding follow the C library's character type.
export LC_CTYPE=C.UTF-8
bundled="$(cat "$ROOT/VERSION" 2>/dev/null)"
installed="$(cat "$PREFIX/VERSION" 2>/dev/null)"
# A double-clicked AppImage has no terminal and a lean desktop may show none of our dialogs, so everything this script does
# is also written to a file the person can open and send: ~/.cache/zapretgui/appimage-start.log (the last runs only).
LOGDIR="${XDG_CACHE_HOME:-$HOME/.cache}/zapretgui"
LOG="$LOGDIR/appimage-start.log"
mkdir -p "$LOGDIR" 2>/dev/null
if [ -f "$LOG" ] && [ "$(wc -c < "$LOG" 2>/dev/null || echo 0)" -gt 200000 ]; then
tail -n 300 "$LOG" > "$LOG.tmp" 2>/dev/null && mv -f "$LOG.tmp" "$LOG" 2>/dev/null
fi
log() { printf '%s %s\n' "$(date '+%F %T' 2>/dev/null)" "$*" >> "$LOG" 2>/dev/null; return 0; }
# the same for the output of other programs (the installer, the window): every line of $1 goes into the log
log_file() { [ -s "$1" ] && sed 's/^/ | /' "$1" >> "$LOG" 2>/dev/null; return 0; }
have() { command -v "$1" >/dev/null 2>&1 && echo "$1" ; return 0; }
log "=== AppRun start: bundled=$bundled installed=${installed:-none} user=$(id -un 2>/dev/null) uid=$(id -u 2>/dev/null) args=[$*]"
log "desktop: DISPLAY=${DISPLAY:-} WAYLAND_DISPLAY=${WAYLAND_DISPLAY:-} session=${XDG_SESSION_TYPE:-} desktop=${XDG_CURRENT_DESKTOP:-} tty_stdin=$([ -t 0 ] && echo yes || echo no)"
log "tools found: $(for t in pkexec sudo zenity kdialog notify-send xmessage x-terminal-emulator gnome-terminal konsole xfce4-terminal xterm sg apt-get systemctl rc-service update-rc.d start-stop-daemon nft; do have "$t"; done | tr '\n' ' ')"
[ -r "$ROOT/packaging/linux/initsys.sh" ] && log "init system: $(. "$ROOT/packaging/linux/initsys.sh"; zg_init_kind)"
log "system: $(uname -sr 2>/dev/null) glibc=$(getconf GNU_LIBC_VERSION 2>/dev/null) os=$(. /etc/os-release 2>/dev/null && echo "$PRETTY_NAME")"
say() {
echo "zapretgui: $*" >&2
log "message to the person: $*"
if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then
command -v zenity >/dev/null && zenity --info --no-wrap --title=Zapret --text="$*" 2>/dev/null && return 0
command -v kdialog >/dev/null && kdialog --msgbox "$*" 2>/dev/null && return 0
command -v notify-send >/dev/null && notify-send Zapret "$*" 2>/dev/null && return 0
# a desktop without any of the above (a bare window manager) still has xmessage or a terminal: a double-clicked
# AppImage must never fail silently
command -v xmessage >/dev/null && xmessage -center "$*" 2>/dev/null && return 0
command -v x-terminal-emulator >/dev/null && x-terminal-emulator -e sh -c 'echo "$1"; echo; printf "Enter to close: "; read answer' sh "$*" 2>/dev/null
fi
return 0
}
# The program is built on Debian 13 and needs its C library. On an older system (Debian 12 has 2.36) the first start would
# install the system part and only then fail with "GLIBC_2.38 not found": say it at once instead. The number is the libc6
# line of deb/control.in (a test keeps them equal).
MIN_GLIBC=2.38
have_glibc="$(getconf GNU_LIBC_VERSION 2>/dev/null | cut -d' ' -f2)"
if [ -n "$have_glibc" ] && [ "$(printf '%s\n%s\n' "$MIN_GLIBC" "$have_glibc" | sort -V | head -n 1)" != "$MIN_GLIBC" ]; then
say "This Zapret build needs a newer system: the C library (glibc) here is $have_glibc, the program needs $MIN_GLIBC or newer (Debian 13, Ubuntu 24.04 or newer). Nothing was installed."
exit 1
fi
# The helper is "running" when its socket is there: the same answer under systemd, OpenRC, sysvinit and no init at all.
HELPER_SOCKET="${ZAPRETGUI_SOCKET:-/run/zapretgui/helper.sock}"
system_part_ready() {
if [ "$bundled" != "$installed" ]; then log "system part: installed version '${installed:-none}' is not the bundled '$bundled'"; return 1; fi
if [ ! -x "$PREFIX/bin/zapretgui" ]; then log "system part: $PREFIX/bin/zapretgui is missing"; return 1; fi
if ! getent group zapretgui >/dev/null; then log "system part: the group zapretgui does not exist"; return 1; fi
if [ ! -S "$HELPER_SOCKET" ]; then log "system part: the helper is not running (no socket $HELPER_SOCKET)"; return 1; fi
log "system part: ready"
}
# Runs install.sh through sudo in a terminal window, for a desktop where the graphical password dialog (pkexec) is missing
# or has no agent: a double-clicked AppImage has no terminal, so sudo alone cannot ask for the password. The terminal
# programs differ in how they take a command and in whether they wait, so the result comes back through a marker file.
sudo_in_terminal() {
marker="$1/.install-status"
rm -f "$marker"
script='sudo "$1" --user "$2"; code=$?; echo "$code" > "$3"; echo
if [ "$code" = 0 ]; then echo "Zapret is installed. You can close this window."; else echo "The installation failed (see above)."; fi
printf "Enter to close: "; read answer'
for term in x-terminal-emulator gnome-terminal konsole xfce4-terminal mate-terminal lxterminal xterm; do
command -v "$term" >/dev/null 2>&1 || continue
case "$term" in
gnome-terminal) "$term" -- sh -c "$script" sh "$1/install.sh" "$(id -un)" "$marker" >/dev/null 2>&1 & ;;
xfce4-terminal|mate-terminal) "$term" -x sh -c "$script" sh "$1/install.sh" "$(id -un)" "$marker" >/dev/null 2>&1 & ;;
*) "$term" -e sh -c "$script" sh "$1/install.sh" "$(id -un)" "$marker" >/dev/null 2>&1 & ;;
esac
pid=$!
# a terminal that cannot start exits with an error at once: try the next one instead of waiting for the password
tries=0
while kill -0 "$pid" 2>/dev/null && [ "$tries" -lt 10 ]; do sleep 0.2; tries=$((tries + 1)); done
if ! kill -0 "$pid" 2>/dev/null && ! wait "$pid" 2>/dev/null; then
[ -s "$marker" ] || continue
fi
waited=0
while [ ! -s "$marker" ] && [ "$waited" -lt 900 ]; do sleep 1; waited=$((waited + 1)); done
[ -s "$marker" ] && return "$(cat "$marker")"
return 1
done
return 1
}
install_system_part() {
tmp="$(mktemp -d /var/tmp/zapretgui-install.XXXXXX)" || return 1
status=1
if cp -a "$ROOT"/. "$tmp"/; then
# a build made from a git export may have lost the executable bit (Dev 296 did: sudo said "command not found")
chmod 0755 "$tmp/install.sh" "$tmp/uninstall.sh" 2>/dev/null
gui=
[ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ] && gui=1
if [ "$(id -u)" = 0 ]; then
"$tmp/install.sh"
status=$?
else
status=127
if [ -n "$gui" ] && command -v pkexec >/dev/null; then
log "running the installer through pkexec"
out="$tmp/pkexec.out"
pkexec "$tmp/install.sh" --user "$(id -un)" > "$out" 2>&1
status=$?
log "pkexec finished with code $status"
log_file "$out"
cat "$out" >&2
else
log "pkexec is not used (gui=${gui:-no}, pkexec=$(have pkexec))"
fi
# pkexec missing (Debian 13 ships it as its own package) or without a password agent exits 127, a dismissed dialog
# 126 (the person said no: stop). With a terminal around, ask there through sudo; without a desktop sudo is plain.
if [ "$status" = 127 ]; then
if [ -t 0 ] && command -v sudo >/dev/null; then
log "running the installer through sudo in this terminal"
sudo "$tmp/install.sh" --user "$(id -un)"
status=$?
log "sudo finished with code $status"
elif [ -n "$gui" ] && command -v sudo >/dev/null; then
log "running the installer through sudo in a new terminal window"
sudo_in_terminal "$tmp"
status=$?
log "sudo in a terminal window finished with code $status"
else
log "no way to ask for the password (no terminal, no sudo)"
fi
fi
fi
fi
log "install_system_part result: $status; the installer's own log is /var/log/zapretgui-install.log (last lines follow)"
tail -n 15 /var/log/zapretgui-install.log 2>/dev/null | sed 's/^/ | /' >> "$LOG" 2>/dev/null
rm -rf "$tmp" # not a trap: the script ends with exec, which would skip it
return $status
}
# Starts the window and, when it dies within a few seconds, says why: a double-clicked AppImage has no terminal, so a
# missing window-system library ("could not load the Qt platform plugin xcb") would otherwise be "nothing happens".
start_window() {
log="$(mktemp /var/tmp/zapretgui-start.XXXXXX)" || exec "$@"
log "starting the window: $*"
"$@" 2>"$log" &
pid=$!
waited=0
while kill -0 "$pid" 2>/dev/null && [ "$waited" -lt 8 ]; do sleep 1; waited=$((waited + 1)); done
if kill -0 "$pid" 2>/dev/null; then
log "the window is still running after $waited s: good"
rm -f "$log" # still running: the program keeps writing into the removed file, nothing is lost that matters
exit 0
fi
wait "$pid"
code=$?
log "the window exited after $waited s with code $code; its error output follows"
log_file "$log"
if [ "$code" != 0 ]; then
lacking="$(for f in "$PREFIX/_internal/zapretgui" $(find "$PREFIX/_internal" -name libqxcb.so 2>/dev/null); do ldd "$f" 2>/dev/null | grep 'not found'; done | sort -u | tr -s '\t ' ' ' | head -8)"
nodisplay=
[ -z "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ] && nodisplay=1
log "missing libraries: ${lacking:-none found}; graphical session: ${nodisplay:+none}"
say "Zapret did not start (exit code $code).
$(tail -n 8 "$log" 2>/dev/null)${nodisplay:+
There is no graphical session in this terminal (DISPLAY is empty: a text console or ssh?). Start Zapret from the desktop: the application menu or a double click in the file manager.}${lacking:+
Libraries that are missing on this system:
$lacking}
The details are in $LOG"
fi
rm -f "$log"
exit "$code"
}
if [ "$1" = "--install-system-part" ] || ! system_part_ready; then
say "First start: Zapret installs its system part (a small root service and the firewall table). It asks for your password once."
if ! install_system_part; then
reason="$(tail -n 8 /var/log/zapretgui-install.log 2>/dev/null)"
say "The system part was not installed.${reason:+
$reason
}
The details are in $LOG
Run it in a terminal to see everything: sudo $(readlink -f "$0") --install-system-part"
exit 1
fi
[ "$1" = "--install-system-part" ] && exit 0
# The person was just added to the group zapretgui, but this session does not have it until the next login. Without
# arguments the window can start at once inside the new group (sg); the helper only talks to members of the group.
if ! id -nG | tr ' ' '\n' | grep -qx zapretgui && getent group zapretgui | grep -qw "$(id -un)"; then
if [ "$#" -eq 0 ] && command -v sg >/dev/null; then
start_window sg zapretgui -c "exec $PREFIX/bin/zapretgui"
fi
say "You were added to the group zapretgui. Log out and in once, then start Zapret again."
fi
fi
start_window "$PREFIX/bin/zapretgui" "$@"