Update 0.14.1 release
[feed/routing.git] / oonf-init-scripts / files / oonf_init.sh
1 #!/bin/sh
2
3 . /usr/share/libubox/jshn.sh
4
5 oonf_log()
6 {
7 logger -s -t ${DAEMON} -p daemon.info "${1}"
8 }
9
10 oonf_get_layer3_device()
11 {
12 local interface="${1}" # e.g. 'mywifi'
13 local status dev proto
14 local query="{ \"interface\" : \"${interface}\" }"
15
16 status="$( ubus -S call network.interface status "${query}" )" && {
17 json_load "${status}"
18 json_get_var 'dev' l3_device
19 json_get_var 'proto' proto
20 case "${proto}" in
21 pppoe)
22 # TODO: otherwise it segfaults
23 oonf_log "refusing to add '$interface', because of proto '${proto}'"
24 ;;
25 *)
26 echo "${dev}" # e.g. 'wlan0-1'
27 ;;
28 esac
29 }
30 }
31
32 oonf_add_devices_to_configuration()
33 {
34 local i=0
35 local device_name= section= interface= single_interface=
36
37 # make a copy of configuration and
38 # add a 'name' (physical name) for all
39 # 'interface-names' (e.g. mywifi)
40 #
41 # olsrd2.@interface[2]=interface
42 # olsrd2.@interface[2].ifname='wan lan wlanadhoc wlanadhocRADIO1'
43
44 # /var is in ramdisc/tmpfs
45 uci export ${DAEMON} >"/var/run/${DAEMON}_dev"
46
47 while section="$( uci -q -c /etc/config get "${DAEMON}.@[${i}]" )"; do {
48 echo "section: ${section}"
49
50 interface="$( uci -q -c /etc/config get "${DAEMON}.@[${i}].ifname" )" || {
51 i=$(( i + 1 ))
52 continue
53 }
54
55 case "$( uci -q get "${DAEMON}.@[${i}].ignore" )" in
56 1|on|true|enabled|yes)
57 oonf_log "removing/ignore section '$section'"
58 uci -q -c /var/run delete "${DAEMON}_dev.@[${j}]"
59 i=$(( i + 1 ))
60
61 continue
62 ;;
63 esac
64
65 for single_interface in ${interface}; do {
66 device_name="$( oonf_get_layer3_device "${single_interface}" )"
67
68 echo "Interface: ${single_interface} = ${device_name}"
69
70 if [ ! -z "${device_name}" ]
71 then
72 # add option 'name' for 'ifname' (e.g. 'mywifi')
73 uci -q -c /var/run add_list "${DAEMON}_dev.@[${i}].name=${device_name}"
74 fi
75 } done
76 i=$(( $i + 1 ))
77 } done
78
79 uci -q -c /var/run commit "${DAEMON}_dev"
80
81 oonf_log "wrote '/var/run/${DAEMON}_dev'"
82 }
83
84 oonf_reread_config()
85 {
86 local pid
87 local pidfile="/var/run/${DAEMON}.pid"
88
89 if [ -e "${pidfile}" ]; then
90 read pid <"${pidfile}"
91 elif pidfile="$( uci -q get "${DAEMON}.@global[0].pidfile" )"; then
92 read pid <"${pidfile}"
93 fi
94
95 # if empty, ask kernel
96 pid="${pid:-$( pidof ${DAEMON} )}"
97
98 [ -n "${pid}" ] && kill -SIGHUP ${pid}
99 }
100
101 start()
102 {
103 oonf_add_devices_to_configuration
104
105 # produce coredumps
106 ulimit -c unlimited
107
108 service_start /usr/sbin/${DAEMON} --set global.fork=true --load uci:///var/run/${DAEMON}_dev
109 }
110
111 stop()
112 {
113 service_stop /usr/sbin/${DAEMON}
114 }
115
116 reload()
117 {
118 oonf_add_devices_to_configuration
119 oonf_reread_config
120 }