iopsys-feed/netmode/files/etc/init.d/netmode

214 lines
4.9 KiB
Bash
Executable file

#!/bin/sh /etc/rc.common
START=11
USE_PROCD=1
. /lib/functions.sh
MODEDIR="/etc/netmodes"
SUPP_MODES_SEC=""
_log() {
logger -s -p user.info -t "netmode" "$*"
}
libnetmode_exec() {
when="$1"
# Execute /lib/netmode scripts
if [ -d /lib/netmode/$when ]; then
_log "Executing /lib/netmode/$when scripts"
for script in $(ls /lib/netmode/$when/); do
sh /lib/netmode/$when/$script
done
fi
}
_get_modes_sec_name() {
local sec mode name
sec="${1}"
mode="${2}"
config_get name ${1} name ""
if [ "${name}" = "${mode}" ]; then
SUPP_MODES_SEC="${sec}"
break;
fi
}
_set_env_args() {
local sec name value required dm_parent
sec="${1}"
config_get_bool required ${sec} required "0"
config_get name ${sec} name ""
config_get value ${sec} value ""
config_get dm_parent ${sec} dm_parent ""
if [ -z "${dm_parent}" ]; then
continue;
fi
if [ "${dm_parent}" != "${SUPP_MODES_SEC}" ]; then
continue;
fi
if [ "${required}" -eq "1" ]; then
if [ -z "${name}" -o -z "${value}" ]; then
_log "Can't apply mode, name[${name}] or value[${value}] is missing"
exit 0
fi
fi
if [ -n "${name}" -a -n "${value}" ]; then
export NETMODE_${name}="${value}"
fi
}
configure_env_vars() {
local mode
mode="${1}"
if [ -z "${mode}" ]; then
_log "mode info missing"
exit 0
fi
SUPP_MODES_SEC=""
config_load "netmode"
config_foreach _get_modes_sec_name supported_modes "${mode}"
if [ -z "${SUPP_MODES_SEC}" ]; then
_log "mode ${mode} not found in uci"
exit 0
fi
config_foreach _set_env_args supported_args
}
cleanup_env_vars() {
for e in $(env); do
if echo ${e} |grep -q "^NETMODE_"; then
unset ${e}
fi
done
}
start_service() {
if [ ! -f /etc/config/netmode ]; then
_log "/etc/config/netmode not found, returning"
return
fi
config_load netmode
config_get_bool enabled global enabled '0'
if [ "$enabled" -eq 0 ]; then
_log "netmode service disabled, returning"
return
fi
[ -d $MODEDIR ] || mkdir -p $MODEDIR
# Get the desired netmode from config
config_get mode global mode ""
# Check if netmode is set as boot environment parameter
if [ -z "$mode" ]; then
_log "mode not set in UCI, checking bootenv"
mode="$(fw_printenv -n netmode 2>/dev/null)"
fi
# Return if mode is not set
if [ -z "$mode" ]; then
_log "mode still empty, returning"
return
fi
# Build configuration signature from UCI values directly
# This ensures parameter changes trigger reconfiguration
SUPP_MODES_SEC=""
config_foreach _get_modes_sec_name supported_modes "${mode}"
local config_signature="${mode}"
if [ -n "${SUPP_MODES_SEC}" ]; then
# Get all arguments for this mode, sorted by section name for consistency
for arg_sec in $(uci show netmode | grep "=supported_args" | cut -d'.' -f2 | cut -d'=' -f1 | sort); do
local dm_parent=$(uci -q get "netmode.${arg_sec}.dm_parent")
if [ "${dm_parent}" = "${SUPP_MODES_SEC}" ]; then
local arg_name=$(uci -q get "netmode.${arg_sec}.name")
local arg_value=$(uci -q get "netmode.${arg_sec}.value")
if [ -n "${arg_name}" -a -n "${arg_value}" ]; then
config_signature="${config_signature}:${arg_name}=${arg_value}"
fi
fi
done
else
_log "Could not find supported mode section"
fi
# Get the last saved configuration signature
local last_config_signature="$(cat $MODEDIR/.last_mode 2>/dev/null)"
# Return if configuration hasn't changed
if [ "${config_signature}" = "${last_config_signature}" ]; then
_log "Not reconfiguring, configuration unchanged"
return
fi
_log "Configuration changed, applying [${mode}] mode"
if [ -n "${last_config_signature}" ]; then
_log "Previous config: ${last_config_signature}"
fi
_log "Current config: ${config_signature}"
# Configure env variables (needed by mode scripts)
configure_env_vars ${mode}
# Execute netmode generic pre-mode-switch scripts
libnetmode_exec "pre"
# Copy netmode UCI config files
if [ -d $MODEDIR/$mode/uci ]; then
_log "Copying $MODEDIR/$mode/uci/* to /etc/config/"
cp $MODEDIR/$mode/uci/* /etc/config/ 2>/dev/null
fi
# Execute netmode generic scripts
libnetmode_exec
# Execute mode specific scripts
local script_exit_code=0
if [ -d $MODEDIR/$mode/scripts ]; then
for script in $(ls $MODEDIR/$mode/scripts/); do
_log "Executing [${mode}], script [${script}]"
sh $MODEDIR/$mode/scripts/$script
script_exit_code=$?
if [ $script_exit_code -ne 0 ]; then
_log "ERROR: Mode script [${script}] failed with exit code ${script_exit_code}"
break
fi
done
fi
# Only save configuration if scripts succeeded
if [ $script_exit_code -eq 0 ]; then
# Save configuration signature as last mode
echo "$config_signature" > $MODEDIR/.last_mode
_log "Switching to Mode [${mode}] done, configuration saved"
# Execute netmode generic post-mode-switch scripts
libnetmode_exec "post"
else
_log "ERROR: Mode switch to [${mode}] FAILED - configuration NOT saved"
_log "The system will retry on next netmode restart"
fi
cleanup_env_vars "${mode}"
}
service_triggers()
{
procd_add_reload_trigger netmode
}