fix: guard AppImage against duplicate launches and rotate router log
- Add an exclusive instance lock and detect an already-running Firefox on the profile, so a second launch exits with a notification instead of clobbering the first - Write the instance PID to instance.lock and record app_run_pid in active_ports.json - Write active_ports.json atomically and only remove it on cleanup if this instance wrote it - Rotate router.log to router.log.1 once it reaches MOON_I2P_LOG_MAX_BYTES (default 5 MiB) and mark each start in the log - Close the lock fd in the router and Firefox children so they do not hold the lock
This commit is contained in:
1 parent
d95d4962f4
commit
9d3df6e100
1 file changed
+95
-8
@@ -187,8 +187,78 @@ APPDIR="$(dirname "$(readlink -f "$0")")"
|
|||||||
DATA_DIR="${MOON_I2P_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/moon-i2p-browser}"
|
DATA_DIR="${MOON_I2P_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/moon-i2p-browser}"
|
||||||
PROFILE_DIR="${MOON_I2P_PROFILE_DIR:-$DATA_DIR/profile}"
|
PROFILE_DIR="${MOON_I2P_PROFILE_DIR:-$DATA_DIR/profile}"
|
||||||
DOWNLOAD_DIR="${MOON_I2P_DOWNLOAD_DIR:-$HOME/Downloads/moon-i2p}"
|
DOWNLOAD_DIR="${MOON_I2P_DOWNLOAD_DIR:-$HOME/Downloads/moon-i2p}"
|
||||||
|
LOG_FILE="$DATA_DIR/router.log"
|
||||||
|
LOCK_FILE="$DATA_DIR/instance.lock"
|
||||||
|
|
||||||
mkdir -p "$DATA_DIR" "$PROFILE_DIR/extensions" "$DOWNLOAD_DIR"
|
mkdir -p "$DATA_DIR"
|
||||||
|
|
||||||
|
PYTHON_BIN="$(command -v python3 || true)"
|
||||||
|
|
||||||
|
abort_already_running() {
|
||||||
|
local detail="$1"
|
||||||
|
local msg="Moon I2P Browser is already running ($detail). Close the existing window before relaunching."
|
||||||
|
echo "$msg" >&2
|
||||||
|
if [[ "${MOON_I2P_NO_NOTIFY:-0}" != "1" ]] && [[ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]] && command -v notify-send >/dev/null 2>&1; then
|
||||||
|
notify-send -a "Moon I2P Browser" \
|
||||||
|
"Moon I2P Browser is already running" \
|
||||||
|
"Close the existing Moon I2P Browser window before relaunching ($detail)." \
|
||||||
|
>/dev/null 2>&1 || true
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Guard 1: Exclusive AppRun instance lock (append mode so held lockfiles are never truncated).
|
||||||
|
exec 9>>"$LOCK_FILE"
|
||||||
|
if command -v flock >/dev/null 2>&1; then
|
||||||
|
if ! flock -n 9; then
|
||||||
|
EXISTING_PID="$(tr -dc '0-9' < "$LOCK_FILE" 2>/dev/null || true)"
|
||||||
|
if [[ -n "$EXISTING_PID" ]]; then
|
||||||
|
abort_already_running "PID $EXISTING_PID"
|
||||||
|
fi
|
||||||
|
abort_already_running "instance lock held"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Guard 2: Detect an already-running Firefox on this profile (e.g. legacy AppImage without instance.lock).
|
||||||
|
if [[ -n "$PYTHON_BIN" && -f "$PROFILE_DIR/.parentlock" ]]; then
|
||||||
|
EXISTING_FF_PID="$("$PYTHON_BIN" -c '
|
||||||
|
import fcntl, os, struct, sys
|
||||||
|
p = sys.argv[1]
|
||||||
|
try:
|
||||||
|
fd = os.open(p, os.O_RDONLY)
|
||||||
|
except OSError:
|
||||||
|
sys.exit(0)
|
||||||
|
try:
|
||||||
|
fl = struct.pack("hhqqi4x", fcntl.F_WRLCK, os.SEEK_SET, 0, 0, 0)
|
||||||
|
res = fcntl.fcntl(fd, fcntl.F_GETLK, fl)
|
||||||
|
l_type, _, _, _, l_pid = struct.unpack("hhqqi4x", res)
|
||||||
|
if l_type != fcntl.F_UNLCK and l_pid > 0:
|
||||||
|
print(l_pid)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
os.close(fd)
|
||||||
|
' "$PROFILE_DIR/.parentlock" 2>/dev/null || true)"
|
||||||
|
if [[ -n "$EXISTING_FF_PID" ]]; then
|
||||||
|
abort_already_running "Firefox PID $EXISTING_FF_PID"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -L "$PROFILE_DIR/lock" ]]; then
|
||||||
|
LOCK_TARGET="$(readlink "$PROFILE_DIR/lock" 2>/dev/null || true)"
|
||||||
|
if [[ "$LOCK_TARGET" == *":+"* ]]; then
|
||||||
|
LOCK_PID="${LOCK_TARGET##*:+}"
|
||||||
|
if [[ "$LOCK_PID" =~ ^[0-9]+$ ]] && kill -0 "$LOCK_PID" 2>/dev/null; then
|
||||||
|
if ! LOCK_CMDLINE="$(tr '\0' ' ' < "/proc/$LOCK_PID/cmdline" 2>/dev/null)" || printf '%s' "$LOCK_CMDLINE" | grep -qi "firefox"; then
|
||||||
|
abort_already_running "Firefox PID $LOCK_PID"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf '%s\n' "$$" > "$LOCK_FILE"
|
||||||
|
|
||||||
|
mkdir -p "$PROFILE_DIR/extensions" "$DOWNLOAD_DIR"
|
||||||
|
|
||||||
# Ensure the bundled extension XPI is installed in the browser profile.
|
# Ensure the bundled extension XPI is installed in the browser profile.
|
||||||
cp -f "$APPDIR/usr/share/moon-i2p/moon-i2p-torrent@local.xpi" \
|
cp -f "$APPDIR/usr/share/moon-i2p/moon-i2p-torrent@local.xpi" \
|
||||||
@@ -215,7 +285,6 @@ EOF
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
PYTHON_BIN="$(command -v python3 || true)"
|
|
||||||
if [[ -n "$PYTHON_BIN" ]]; then
|
if [[ -n "$PYTHON_BIN" ]]; then
|
||||||
read -r PROXY_PORT API_PORT SAM_PORT < <("$PYTHON_BIN" -c '
|
read -r PROXY_PORT API_PORT SAM_PORT < <("$PYTHON_BIN" -c '
|
||||||
import socket
|
import socket
|
||||||
@@ -273,10 +342,13 @@ EOF
|
|||||||
ROUTER_BIN="$APPDIR/usr/bin/moon-i2p-router"
|
ROUTER_BIN="$APPDIR/usr/bin/moon-i2p-router"
|
||||||
ROUTER_PID=""
|
ROUTER_PID=""
|
||||||
FF_PID=""
|
FF_PID=""
|
||||||
|
ACTIVE_PORTS_WRITTEN=0
|
||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
local rc=$?
|
local rc=$?
|
||||||
rm -f "$DATA_DIR/active_ports.json"
|
if [[ "$ACTIVE_PORTS_WRITTEN" == "1" ]]; then
|
||||||
|
rm -f "$DATA_DIR/active_ports.json"
|
||||||
|
fi
|
||||||
if [[ -n "$FF_PID" ]] && kill -0 "$FF_PID" 2>/dev/null; then
|
if [[ -n "$FF_PID" ]] && kill -0 "$FF_PID" 2>/dev/null; then
|
||||||
kill -TERM "$FF_PID" 2>/dev/null || true
|
kill -TERM "$FF_PID" 2>/dev/null || true
|
||||||
wait "$FF_PID" 2>/dev/null || true
|
wait "$FF_PID" 2>/dev/null || true
|
||||||
@@ -296,6 +368,18 @@ cleanup() {
|
|||||||
}
|
}
|
||||||
trap cleanup EXIT INT TERM
|
trap cleanup EXIT INT TERM
|
||||||
|
|
||||||
|
LOG_MAX_BYTES="${MOON_I2P_LOG_MAX_BYTES:-5242880}"
|
||||||
|
[[ "$LOG_MAX_BYTES" =~ ^[0-9]+$ ]] || LOG_MAX_BYTES=5242880
|
||||||
|
if [[ -f "$LOG_FILE" ]]; then
|
||||||
|
LOG_SIZE="$(stat -c%s "$LOG_FILE" 2>/dev/null || wc -c < "$LOG_FILE")"
|
||||||
|
LOG_SIZE="${LOG_SIZE//[[:space:]]/}"
|
||||||
|
if [[ "$LOG_SIZE" =~ ^[0-9]+$ ]] && (( LOG_SIZE >= LOG_MAX_BYTES )); then
|
||||||
|
mv -f "$LOG_FILE" "$LOG_FILE.1"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
printf '\n=== Moon I2P Browser start %s appdir=%s proxy=%s api=%s sam=%s ===\n' \
|
||||||
|
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$APPDIR" "$PROXY_PORT" "$API_PORT" "$SAM_PORT" >> "$LOG_FILE"
|
||||||
|
|
||||||
"$ROUTER_BIN" \
|
"$ROUTER_BIN" \
|
||||||
--public \
|
--public \
|
||||||
--debug \
|
--debug \
|
||||||
@@ -303,12 +387,15 @@ trap cleanup EXIT INT TERM
|
|||||||
--http-proxy "127.0.0.1:$PROXY_PORT" \
|
--http-proxy "127.0.0.1:$PROXY_PORT" \
|
||||||
--torrent-api "127.0.0.1:$API_PORT" \
|
--torrent-api "127.0.0.1:$API_PORT" \
|
||||||
--download-dir "$DOWNLOAD_DIR" \
|
--download-dir "$DOWNLOAD_DIR" \
|
||||||
>> "$DATA_DIR/router.log" 2>&1 &
|
9>&- >> "$LOG_FILE" 2>&1 &
|
||||||
ROUTER_PID=$!
|
ROUTER_PID=$!
|
||||||
|
|
||||||
cat > "$DATA_DIR/active_ports.json" <<EOF
|
ACTIVE_PORTS_TMP="$DATA_DIR/active_ports.json.tmp.$$"
|
||||||
{"proxy_port": $PROXY_PORT, "api_port": $API_PORT, "sam_port": $SAM_PORT, "router_pid": $ROUTER_PID}
|
cat > "$ACTIVE_PORTS_TMP" <<EOF
|
||||||
|
{"proxy_port": $PROXY_PORT, "api_port": $API_PORT, "sam_port": $SAM_PORT, "router_pid": $ROUTER_PID, "app_run_pid": $$}
|
||||||
EOF
|
EOF
|
||||||
|
mv -f "$ACTIVE_PORTS_TMP" "$DATA_DIR/active_ports.json"
|
||||||
|
ACTIVE_PORTS_WRITTEN=1
|
||||||
|
|
||||||
if [[ -n "$PYTHON_BIN" ]]; then
|
if [[ -n "$PYTHON_BIN" ]]; then
|
||||||
"$PYTHON_BIN" -c '
|
"$PYTHON_BIN" -c '
|
||||||
@@ -328,7 +415,7 @@ fi
|
|||||||
|
|
||||||
export MOON_I2P_APPIMAGE=1
|
export MOON_I2P_APPIMAGE=1
|
||||||
export MOON_I2P_DATA_DIR="$DATA_DIR"
|
export MOON_I2P_DATA_DIR="$DATA_DIR"
|
||||||
export MOON_I2P_LOG_FILE="$DATA_DIR/router.log"
|
export MOON_I2P_LOG_FILE="$LOG_FILE"
|
||||||
export MOON_I2P_ROUTER_PID="$ROUTER_PID"
|
export MOON_I2P_ROUTER_PID="$ROUTER_PID"
|
||||||
export MOON_I2P_ROUTER_BIN="$ROUTER_BIN"
|
export MOON_I2P_ROUTER_BIN="$ROUTER_BIN"
|
||||||
export MOON_I2P_PROXY_HOST=127.0.0.1
|
export MOON_I2P_PROXY_HOST=127.0.0.1
|
||||||
@@ -342,7 +429,7 @@ export MOON_I2P_DOWNLOAD_DIR="$DOWNLOAD_DIR"
|
|||||||
--no-remote \
|
--no-remote \
|
||||||
--profile "$PROFILE_DIR" \
|
--profile "$PROFILE_DIR" \
|
||||||
--class "MoonI2PBrowser" \
|
--class "MoonI2PBrowser" \
|
||||||
"$@" &
|
"$@" 9>&- &
|
||||||
FF_PID=$!
|
FF_PID=$!
|
||||||
wait "$FF_PID"
|
wait "$FF_PID"
|
||||||
'''
|
'''
|
||||||
|
|||||||
Reference in new issue
Block a user