diff --git a/agent/openwrt/rmm-agent.init b/agent/openwrt/rmm-agent.init index efc923c..41872c1 100644 --- a/agent/openwrt/rmm-agent.init +++ b/agent/openwrt/rmm-agent.init @@ -11,10 +11,14 @@ start_service() { procd_open_instance procd_set_param command /bin/sh "$PROG" procd_set_param env CONFIG_FILE="$CONFIG_FILE" + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_set_param term_timeout 15 procd_set_param respawn 3600 5 5 procd_close_instance } stop_service() { service_stop "$PROG" + rm -rf /tmp/rmm-agent.lock } diff --git a/agent/openwrt/rmm-agent.sh b/agent/openwrt/rmm-agent.sh index 9c74924..02f8c5f 100644 --- a/agent/openwrt/rmm-agent.sh +++ b/agent/openwrt/rmm-agent.sh @@ -23,15 +23,36 @@ log() { printf '%s %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$*" >&2 } +cleanup() { + if [ -n "${SLEEP_PID:-}" ]; then + kill "$SLEEP_PID" 2>/dev/null || true + fi + rm -rf "$LOCK_FILE" +} + +stop_agent() { + cleanup + exit 0 +} + acquire_lock() { if mkdir "$LOCK_FILE" 2>/dev/null; then - trap 'rm -rf "$LOCK_FILE"' EXIT INT TERM + trap cleanup EXIT + trap stop_agent INT TERM return 0 fi log "another rmm-agent instance is running" return 1 } +interruptible_sleep() { + SLEEP_PID="" + sleep "$1" & + SLEEP_PID="$!" + wait "$SLEEP_PID" 2>/dev/null || true + SLEEP_PID="" +} + json_escape() { printf '%s' "$1" | awk ' BEGIN { ORS = "" } @@ -954,7 +975,7 @@ main() { backoff=$((backoff * 2)) [ "$backoff" -le 300 ] || backoff=300 fi - sleep "$backoff" + interruptible_sleep "$backoff" done } diff --git a/agent/package/rmm-agent/files/etc/init.d/rmm-agent b/agent/package/rmm-agent/files/etc/init.d/rmm-agent index efc923c..41872c1 100644 --- a/agent/package/rmm-agent/files/etc/init.d/rmm-agent +++ b/agent/package/rmm-agent/files/etc/init.d/rmm-agent @@ -11,10 +11,14 @@ start_service() { procd_open_instance procd_set_param command /bin/sh "$PROG" procd_set_param env CONFIG_FILE="$CONFIG_FILE" + procd_set_param stdout 1 + procd_set_param stderr 1 + procd_set_param term_timeout 15 procd_set_param respawn 3600 5 5 procd_close_instance } stop_service() { service_stop "$PROG" + rm -rf /tmp/rmm-agent.lock } diff --git a/agent/package/rmm-agent/files/usr/bin/rmm-agent b/agent/package/rmm-agent/files/usr/bin/rmm-agent index ca45e8e..7a6273a 100644 --- a/agent/package/rmm-agent/files/usr/bin/rmm-agent +++ b/agent/package/rmm-agent/files/usr/bin/rmm-agent @@ -12,6 +12,7 @@ SPOOL_DIR="${SPOOL_DIR:-/tmp/rmm-agent-results}" BACKUP_DIR="${BACKUP_DIR:-/tmp/rmm-agent-backups}" CHECK_TARGETS="${CHECK_TARGETS:-1.1.1.1 8.8.8.8}" TUNNEL_IDENTITY_FILE="${TUNNEL_IDENTITY_FILE:-/etc/rmm-agent/tunnel_key}" +TUNNEL_STATE_DIR="${TUNNEL_STATE_DIR:-/tmp/rmm-agent-tunnels}" if [ -f "$CONFIG_FILE" ]; then # shellcheck disable=SC1090 @@ -22,15 +23,36 @@ log() { printf '%s %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$*" >&2 } +cleanup() { + if [ -n "${SLEEP_PID:-}" ]; then + kill "$SLEEP_PID" 2>/dev/null || true + fi + rm -rf "$LOCK_FILE" +} + +stop_agent() { + cleanup + exit 0 +} + acquire_lock() { if mkdir "$LOCK_FILE" 2>/dev/null; then - trap 'rm -rf "$LOCK_FILE"' EXIT INT TERM + trap cleanup EXIT + trap stop_agent INT TERM return 0 fi log "another rmm-agent instance is running" return 1 } +interruptible_sleep() { + SLEEP_PID="" + sleep "$1" & + SLEEP_PID="$!" + wait "$SLEEP_PID" 2>/dev/null || true + SLEEP_PID="" +} + json_escape() { printf '%s' "$1" | awk ' BEGIN { ORS = "" } @@ -879,7 +901,7 @@ main() { backoff=$((backoff * 2)) [ "$backoff" -le 300 ] || backoff=300 fi - sleep "$backoff" + interruptible_sleep "$backoff" done }