180 lines
3.9 KiB
Bash
Executable File
180 lines
3.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# vim: set ts=2 sw=2 et:
|
|
# /etc/bewan/lib/base
|
|
|
|
[ ${BASE_LIB_LOADED:-0} = 1 ] && return
|
|
|
|
BASE_LIB_LOADED=1
|
|
|
|
# Used to push ARG in local functions
|
|
PUSH_ARG='[ "${TOP_ARG:-}" = "" ] && TOP_ARG=$ARG; local top_arg=$ARG; local ARG=$top_arg'
|
|
|
|
base_log() {
|
|
local debug=`tty`
|
|
local level=${2:-debug}
|
|
[ ! -c "$debug" ] && debug='/dev/console'
|
|
if [ "x$1" = 'x-' ]; then
|
|
cat > ${debug}
|
|
else
|
|
echo $1 > ${debug}
|
|
fi
|
|
local pid=`pidof syslogd`
|
|
if [ "$pid" != '' ]; then
|
|
if [ "x$1" = 'x-' ]; then
|
|
cat | /usr/bin/logger -t `basename $0`[$$] -p daemon.$level
|
|
else
|
|
echo $1 | /usr/bin/logger -t `basename $0`[$$] -p daemon.$level
|
|
fi
|
|
fi
|
|
}
|
|
|
|
#This function is created for modem3g script, the dongle device is not corrupted with console.
|
|
base_log_noconsole() {
|
|
local level=${2:-debug}
|
|
local pid=`pidof syslogd`
|
|
if [ "$pid" != '' ]; then
|
|
if [ "x$1" = 'x-' ]; then
|
|
cat | /usr/bin/logger -t `basename $0`[$$] -p daemon.$level
|
|
else
|
|
echo $1 | /usr/bin/logger -t `basename $0`[$$] -p daemon.$level
|
|
fi
|
|
fi
|
|
}
|
|
|
|
base_call_initd() {
|
|
local srv;
|
|
for srv in $1; do
|
|
if [ -f $INITD/$srv ]; then
|
|
. $INITD/$srv
|
|
fi
|
|
done
|
|
}
|
|
|
|
base_call_scripts() {
|
|
local srv;
|
|
for srv in $1; do
|
|
if [ -f $SCRIPTD/$srv ]; then
|
|
. $SCRIPTD/$srv
|
|
fi
|
|
done
|
|
}
|
|
|
|
base_load_param() {
|
|
if which configd >/dev/null; then
|
|
# Export all configuration parameters as environment variables
|
|
if [ ${PARAM_LOADED:-0} = 0 ]; then
|
|
PARAM_LOADED=1
|
|
#base_log "Exporting configuration parameters" debug
|
|
eval $(echo export | cli -q)
|
|
fi
|
|
else
|
|
base_enter_critical 'config.lck'
|
|
. /var/bewan/router.conf
|
|
base_exit_critical 'config.lck'
|
|
fi
|
|
}
|
|
|
|
# Modify ARG according to the state of the service
|
|
# enable means that the service is activated in the configuration
|
|
# active means that the service is currently running
|
|
base_check_arg() {
|
|
local enable="$1"
|
|
local active="$2"
|
|
|
|
if [ "$ARG" = restart ] && ! $enable; then
|
|
ARG=stop
|
|
fi
|
|
if [ "$active" != '' ]; then
|
|
if [ "$ARG" = restart ] && ! $active; then
|
|
ARG=start
|
|
fi
|
|
if [ "$ARG" = start ] && $active; then
|
|
ARG=nothing
|
|
fi
|
|
if [ "$ARG" = stop ] && ! $active; then
|
|
ARG=nothing
|
|
fi
|
|
fi
|
|
if [ "$ARG" = start ] && ! $enable; then
|
|
ARG=nothing
|
|
fi
|
|
}
|
|
|
|
base_kill_daemon() {
|
|
local name=$1 pids="$2"
|
|
base_enter_critical 'kills.lock'
|
|
if [ "$pids" != '' ]; then
|
|
echo `cat $KILLS 2>/dev/null` $pids >$KILLS
|
|
fi
|
|
base_exit_critical 'kills.lock'
|
|
rm -f $ITABD/$name
|
|
}
|
|
|
|
base_kill9_daemon() {
|
|
local name=$1 pids="$2"
|
|
base_enter_critical 'kills.lock'
|
|
if [ "$pids" != '' ]; then
|
|
echo `cat $KILLS9 2>/dev/null` $pids >$KILLS9
|
|
fi
|
|
base_exit_critical 'kills.lock'
|
|
rm -f $ITABD/$name
|
|
}
|
|
|
|
base_get_password() {
|
|
local cipher="${1:-}"
|
|
if [ -f "/bin/aes_decrypt" ]; then
|
|
aes_decrypt "$cipher"
|
|
else
|
|
echo "$cipher"
|
|
fi
|
|
}
|
|
|
|
base_add_daemon() {
|
|
local name=$1 cmd="$2" tty="${3:-/dev/null}"
|
|
echo "$tty::respawn:$cmd" >>$ITABD/$name
|
|
}
|
|
|
|
base_exist_daemon() {
|
|
local name=$1
|
|
test -f $ITABD/$name
|
|
return $?
|
|
}
|
|
|
|
base_enter_critical() {
|
|
local file=${1:-'bad.lock'}
|
|
local wait=${2:-'wait'}
|
|
local duration=${3:-}
|
|
tnset /var/bewan/lock/$file $wait $duration
|
|
local retcode=$?
|
|
if [ $file = 'onchange' ]; then
|
|
# We always take the mutex wan-up-down in parallel with the onchange mutex
|
|
base_enter_critical 'wan-up-down'
|
|
fi
|
|
return $retcode
|
|
}
|
|
|
|
base_exit_critical() {
|
|
local file=${1:-'bad.lock'}
|
|
if [ $file = 'onchange' ]; then
|
|
# We always release the mutex wan-up-down before the onchange mutex
|
|
base_exit_critical 'wan-up-down'
|
|
fi
|
|
rm -f /var/bewan/lock/$file
|
|
}
|
|
|
|
base_mtd_config() {
|
|
cat /proc/mtd | grep mtd2 | grep Config >/dev/null
|
|
return $?
|
|
}
|
|
|
|
base_trigger_softdog() {
|
|
# Yes this command does not exist.
|
|
# Yes we want to trigger a softdog.
|
|
/bin/trigger_softdog
|
|
}
|
|
|
|
base_reboot_on_exit() {
|
|
base_log "Unexpected exit broke system, triggering softdog ..." debug
|
|
base_trigger_softdog
|
|
}
|