OONF Init script fix for the case that /var/etc does not exist
[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 mkdir -p /var/etc
46 uci export ${DAEMON} >"/var/etc/${DAEMON}_dev"
47
48 while section="$( uci -q -c /etc/config get "${DAEMON}.@[${i}]" )"; do {
49 echo "section: ${section}"
50
51 interface="$( uci -q -c /etc/config get "${DAEMON}.@[${i}].ifname" )" || {
52 i=$(( i + 1 ))
53 continue
54 }
55
56 case "$( uci -q get "${DAEMON}.@[${i}].ignore" )" in
57 1|on|true|enabled|yes)
58 oonf_log "removing/ignore section '$section'"
59 uci -q -c /var/etc delete "${DAEMON}_dev.@[${j}]"
60 i=$(( i + 1 ))
61
62 continue
63 ;;
64 esac
65
66 for single_interface in ${interface}; do {
67 device_name="$( oonf_get_layer3_device "${single_interface}" )"
68
69 echo "Interface: ${single_interface} = ${device_name}"
70
71 if [ ! -z "${device_name}" ]
72 then
73 # add option 'name' for 'ifname' (e.g. 'mywifi')
74 uci -q -c /var/etc add_list "${DAEMON}_dev.@[${i}].name=${device_name}"
75 fi
76 } done
77 i=$(( $i + 1 ))
78 } done
79
80 uci -q -c /var/etc commit "${DAEMON}_dev"
81
82 oonf_log "wrote '/var/etc/${DAEMON}_dev'"
83 }
84
85 oonf_reread_config()
86 {
87 local pid
88 local pidfile="/var/run/${DAEMON}.pid"
89
90 if [ -e "${pidfile}" ]; then
91 read pid <"${pidfile}"
92 elif pidfile="$( uci -q get "${DAEMON}.@global[0].pidfile" )"; then
93 read pid <"${pidfile}"
94 fi
95
96 # if empty, ask kernel
97 pid="${pid:-$( pidof ${DAEMON} )}"
98
99 [ -n "${pid}" ] && kill -SIGHUP ${pid}
100 }
101
102 start()
103 {
104 oonf_add_devices_to_configuration
105
106 # produce coredumps
107 ulimit -c unlimited
108
109 service_start /usr/sbin/${DAEMON} --set global.fork=true --load uci:///var/etc/${DAEMON}_dev
110 }
111
112 stop()
113 {
114 service_stop /usr/sbin/${DAEMON}
115 }
116
117 reload()
118 {
119 oonf_add_devices_to_configuration
120 oonf_reread_config
121 }