generated from ztimson/template
Update fallback-ap.sh
All checks were successful
Code review / review (pull_request) Successful in 35s
All checks were successful
Code review / review (pull_request) Successful in 35s
This commit is contained in:
@@ -14,14 +14,7 @@ set -e
|
|||||||
# and stays as plain constants.
|
# and stays as plain constants.
|
||||||
|
|
||||||
CONFIG_FILE="/etc/fallback-ap.conf"
|
CONFIG_FILE="/etc/fallback-ap.conf"
|
||||||
|
if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE"; fi
|
||||||
# Load persisted settings from a previous install, if any. These are stored
|
|
||||||
# under SAVED_* names (not FALLBACK_AP_*) specifically so that sourcing this
|
|
||||||
# file can never clobber an explicit environment-variable override below.
|
|
||||||
if [ -f "$CONFIG_FILE" ]; then
|
|
||||||
# shellcheck disable=SC1090
|
|
||||||
source "$CONFIG_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
WLAN0_IFACE="${FALLBACK_AP_IFACE:-${SAVED_IFACE:-wlan0}}"
|
WLAN0_IFACE="${FALLBACK_AP_IFACE:-${SAVED_IFACE:-wlan0}}"
|
||||||
|
|
||||||
@@ -52,10 +45,10 @@ RECOVERY_WINDOW=20
|
|||||||
STATE_SETTLE_DELAY=1
|
STATE_SETTLE_DELAY=1
|
||||||
|
|
||||||
CONNECTION_NAME="Fallback AP"
|
CONNECTION_NAME="Fallback AP"
|
||||||
SERVICE_NAME="pi-fallback-ap"
|
SERVICE_NAME="fallback-ap"
|
||||||
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
|
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
|
||||||
|
|
||||||
STATE_DIR="/var/lib/pi-fallback-ap"
|
STATE_DIR="/var/lib/fallback-ap"
|
||||||
LAST_WIFI_FILE="$STATE_DIR/last-wifi"
|
LAST_WIFI_FILE="$STATE_DIR/last-wifi"
|
||||||
LOCK_FILE="$STATE_DIR/lock"
|
LOCK_FILE="$STATE_DIR/lock"
|
||||||
STATE_FILE="$STATE_DIR/state"
|
STATE_FILE="$STATE_DIR/state"
|
||||||
@@ -214,16 +207,6 @@ stop_ap() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# ==========================================================================
|
|
||||||
# One tick of the control loop. Called once per second, always while
|
|
||||||
# holding the wlan0 lock, so everything here runs to completion before
|
|
||||||
# the next tick starts - there is no interleaving to race against.
|
|
||||||
#
|
|
||||||
# `last_attempt` is intentionally not declared `local` here: it lives in
|
|
||||||
# run()'s scope and this function updates it directly (bash resolves it
|
|
||||||
# via the caller's scope as long as nothing else shadows it).
|
|
||||||
# ==========================================================================
|
|
||||||
tick() {
|
tick() {
|
||||||
local now="$1"
|
local now="$1"
|
||||||
|
|
||||||
@@ -252,8 +235,6 @@ tick() {
|
|||||||
clients="$(get_client_count)"
|
clients="$(get_client_count)"
|
||||||
|
|
||||||
if [ "$clients" -gt 0 ]; then
|
if [ "$clients" -gt 0 ]; then
|
||||||
# Someone is using the AP right now. Leave it alone and push the
|
|
||||||
# next recovery attempt back so we don't kick them off.
|
|
||||||
last_attempt="$now"
|
last_attempt="$now"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -264,9 +245,6 @@ tick() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Stop the AP, give NetworkManager a window to autoconnect to any known
|
|
||||||
# network it can see, and bring the AP back if that doesn't happen.
|
|
||||||
attempt_recovery() {
|
attempt_recovery() {
|
||||||
log "No AP clients, checking for known Wi-Fi (AP down for up to ${RECOVERY_WINDOW}s)"
|
log "No AP clients, checking for known Wi-Fi (AP down for up to ${RECOVERY_WINDOW}s)"
|
||||||
|
|
||||||
@@ -303,7 +281,6 @@ attempt_recovery() {
|
|||||||
|
|
||||||
save_config() {
|
save_config() {
|
||||||
mkdir -p "$(dirname "$CONFIG_FILE")"
|
mkdir -p "$(dirname "$CONFIG_FILE")"
|
||||||
|
|
||||||
cat > "$CONFIG_FILE" <<EOF
|
cat > "$CONFIG_FILE" <<EOF
|
||||||
SAVED_IFACE="$WLAN0_IFACE"
|
SAVED_IFACE="$WLAN0_IFACE"
|
||||||
SAVED_SSID="$AP_NAME"
|
SAVED_SSID="$AP_NAME"
|
||||||
@@ -313,8 +290,6 @@ SAVED_CIDR="$FALLBACK_AP_CIDR"
|
|||||||
SAVED_DHCP_START_OCTET="$FALLBACK_AP_DHCP_START_OCTET"
|
SAVED_DHCP_START_OCTET="$FALLBACK_AP_DHCP_START_OCTET"
|
||||||
SAVED_DHCP_END_OCTET="$FALLBACK_AP_DHCP_END_OCTET"
|
SAVED_DHCP_END_OCTET="$FALLBACK_AP_DHCP_END_OCTET"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# Contains the AP passphrase - keep it out of reach of other users.
|
|
||||||
chmod 600 "$CONFIG_FILE"
|
chmod 600 "$CONFIG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,13 +514,6 @@ run() {
|
|||||||
|
|
||||||
log "Fallback AP service started"
|
log "Fallback AP service started"
|
||||||
log "Script: $SCRIPT_PATH"
|
log "Script: $SCRIPT_PATH"
|
||||||
|
|
||||||
# Open the lock fd once for the life of the process. Using `flock`
|
|
||||||
# directly against this fd (instead of wrapping each tick in its own
|
|
||||||
# `( ... ) 9>"$LOCK_FILE"` subshell) matters: any variable a tick
|
|
||||||
# updates - like last_attempt below - has to survive into the next
|
|
||||||
# loop iteration, and a subshell would silently throw those updates
|
|
||||||
# away when it exits.
|
|
||||||
exec 9>"$LOCK_FILE"
|
exec 9>"$LOCK_FILE"
|
||||||
|
|
||||||
flock 9
|
flock 9
|
||||||
|
|||||||
Reference in New Issue
Block a user