icwmp/init/cwmpd.init
2015-02-05 16:17:02 +01:00

228 lines
5.5 KiB
Bash

#!/bin/sh /etc/rc.common
# Copyright (C) 2013 Inteno Broadband Technology AB
# Author Ahmed Zribi <ahmed.zribi@pivasoftware.com>
# Author Mohamed Kallel <mohamed.kallel@pivasoftware.com>
include /lib/network
. /usr/share/libubox/jshn.sh
START=10
STOP=40
EXTRA_HELP=" start [GetRPCMethods] Start cwmpd service and send GetRPCMethods"
check_url_format() {
# SCHEMA_LIST: contain list of possible schemas that could be present in the acs url
# Example: SCHEMA_LIST="http https"
SCHEMA_LIST="http"
for schema in $SCHEMA_LIST; do
dest=`echo $1 | sed 's/$schema:\/\///g' | cut -f1 -d \/ | cut -f1 -d:`
if [ "_$dest" != "_" ]; then
return 0
fi
done
return 1
}
check_acs_url() {
default_acs="http://10.10.1.6:8000/openacs/acs"
acs_dhcp_discovery=`uci -q get cwmp.acs.dhcp_discovery`
url=`uci -q get cwmp.acs.url`
dhcp_url_path=`uci -q get cwmp.acs.dhcp_url_path`
if [ \( "_$acs_dhcp_discovery" = "_enable" \) -o \( "_$url" = "_" \) ];then
url=`uci -P /var/state -q get $dhcp_url_path`
echo "ACS URL from dhcp: $url"
elif [ "_$url" != "_" ];then
url=`uci -q get cwmp.acs.url`
echo "ACS URL from configuration: $url"
else
url=$default_acs
echo "Using default ACS URL: $url"
fi
check_url_format $url
if [ "$?" != "0" ];then
echo "Invalid ACS URL: $url"
exit 1
fi
}
check_dhcp() {
i=0
dhcp_discovery=`uci -q get cwmp.acs.dhcp_discovery`
url=`uci -q get cwmp.acs.url`
dhcp_url_path=`uci -q get cwmp.acs.dhcp_url_path`
if [ \( "_$acs_dhcp_discovery" = "_enable" \) -o \( "_$url" = "_" \) ]
then
while [ $i -le 10 ]
do
acs_url=`uci -P /var/state -q get $dhcp_url_path`
if [ "$acs_url" != "" ]
then
break
else
echo "Waiting for discovery of acs url from dhcp server ..."
sleep 10
fi
i=`expr $i + 1`
done
fi
}
set_wan_interface() {
local l3_device=""
local default_wan_interface=""
config_load cwmp
config_get default_wan_interface cpe default_wan_interface
json_load "$(ifstatus $default_wan_interface)"
json_get_var l3_device l3_device
if [ "$l3_device" != "" ];then
uci_set cwmp cpe interface "$l3_device"
uci_commit
fi
}
start_msg="Start cwmpd ..."
stop_msg="Stop cwmpd ..."
run() {
echo $start_msg;printf "\033[A" ;sleep 1
set_wan_interface
check_dhcp
check_acs_url
if [ "_$1" = "_boot" ];then
opt=$opt"-b "
fi
if [ "_$1" = "_GetRPCMethods" ];then
opt=$opt"-g "
fi
[ -f /etc/config/cwmp ] && /usr/sbin/cwmpd $opt &
while [ "`ps | grep /usr/sbin/cwmpd | grep -v grep | grep -v rc.common`" = "" ]; do
sleep 1
done
}
boot() {
touch /etc/cwmpd/.cwmpd_boot
/bin/sh /usr/sbin/freecwmp clean cache
local scron=`crontab -l | grep "/usr/sbin/watchcwmpd"`
if [ "$scron" != "" ]; then
crontab -l | sed "/\/usr\/sbin\/watchcwmpd/d" | crontab -
fi
}
start_cwmpd() {
run=$(ps | grep /usr/sbin/cwmpd | grep -v grep | grep -v rc.common)
if [ "$run" = "" ];then
stop_freecwmp_cache
echo "Check if the whole cache is built. Wait if the whole cache is not built yet..."
/bin/sh /usr/sbin/freecwmp wait cache
printf "\033[A"
if [ "$1" = "GetRPCMethods" ];then
run "GetRPCMethods"
else
if [ ! -f /etc/cwmpd/.cwmpd_boot ]; then
run
else
run "boot"
rm -f /etc/cwmpd/.cwmpd_boot 2>/dev/null
fi
fi
else
echo "cwmpd is currently running ..."
fi
}
kill_process_childs() {
local pid_list="$1"
local proc="" line="" pt="" ppid="" pid="" pid_childs="" pid_grep=""
[ "$pid_list" = "" ] && return
pid_grep=${pid_list// /\\|}
proc=`cat /proc/[0-9]*/stat | grep "$pid_grep"`
proc=${proc// /;;;;;}
for line in $proc; do
pid="${line%%;;;;;*}"
ppid="${line#*;;;;;*;;;;;*;;;;;}"; ppid="${ppid%%;;;;;*}"
pt=`echo "$pid" | grep "$pid_grep"`
if [ "_$pt" != "_" -a "$pt" = "$pid" ]; then
continue
fi
pt=`echo "$ppid" | grep "$pid_grep"`
if [ "$pt" != "$ppid" -o "_$pid" = "_" ]; then
continue
fi
pid_childs="$pid_childs $pid"
done
pid_childs=${pid_childs% }
pid_childs=${pid_childs# }
kill_process_childs "$pid_childs"
[ "$pid_childs" != "" ] && kill -9 $pid_childs
}
pidof_cwmp() {
local pids=""
pids="`ps | grep /usr/sbin/cwmpd | sed 's/^ \+//g' | sed 's/ \+/:/g' | grep -v \" Z \" | grep -v grep | cut -f1 -d: | tr '\n' ' '`"
pids=${pids% }
pids=${pids# }
echo "$pids"
}
pidof_freecwmp() {
local pids=""
pids="`ps | grep /usr/sbin/freecwmp | sed 's/^ \+//g' | sed 's/ \+/:/g' | grep -v grep | cut -f1 -d: | tr '\n' ' '`"
pids=${pids% }
pids=${pids# }
echo "$pids"
}
stop_freecwmp_cache() {
local pid_list="" pid="" pids="" p name state ppid rest cmd
pid_list="`pidof_freecwmp`"
for pid in $pid_list; do
read p name state ppid rest < /proc/$pid/stat
cmd=`cat /proc/$ppid/cmdline | grep /etc/init.d/cwmpd`
if [ "_$cmd" = "_" ]; then
continue
fi
kill_process_childs "$ppid"
[ "$ppid" != "" ] && kill -9 "$ppid"
done
}
stop_cwmpd() {
echo $stop_msg;printf "\033[A"
local pid_list="" pids="" str=""
pid_list=`pidof_cwmp`
if [ "_$pid_list" != "_" ]; then
kill_process_childs "$pid_list"
ubus call tr069 command '{"command": "exit"}' -t 3 >/dev/null
local ret=$?
if [ "$ret" != "0" ]; then
kill -9 $pid_list
fi
fi
stop_freecwmp_cache
}
start() {
start_cwmpd "$1" 2> /dev/null
local scron=`crontab -l | grep "/usr/sbin/watchcwmpd"`
if [ "$scron" = "" ]; then
{ crontab -l; echo "*/5 * * * * /usr/sbin/watchcwmpd"; } | crontab -
fi
}
stop() {
stop_cwmpd 2> /dev/null
local scron=`crontab -l | grep "/usr/sbin/watchcwmpd"`
if [ "$scron" != "" ]; then
crontab -l | sed "/\/usr\/sbin\/watchcwmpd/d" | crontab -
fi
}
restart() {
stop_cwmpd 2> /dev/null
start
}