b2926505d32ad3e9e47c07e3d310f1529c9632fb
[feed/routing.git] / alfred / files / alfred.init
1 #!/bin/sh /etc/rc.common
2
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 START=99
9 USE_PROCD=1
10 alfred_args=""
11 vis_args=""
12 facters_dir="/etc/alfred"
13 enable=0
14 vis_enable=0
15
16 wait_for_dir() {
17 local ifce="$1" dir="$2"
18
19 if ! [ -d "$dir" ] ; then
20 timeout=30
21 echo "waiting $timeout secs for $ifce interface..."
22 for i in $(seq $timeout); do
23 sleep 1
24 [ -d "$dir" ] && break
25 if [ $i = $timeout ] ; then
26 echo "$ifce not detected, alfred not starting."
27 exit 1
28 fi
29 done
30 fi
31 }
32
33 wait_for_ll_address() {
34 local iface="$1"
35 local timeout=30
36
37 echo "waiting $timeout secs for $iface address..."
38 for i in $(seq $timeout); do
39 # We look for
40 # - the link-local address (starts with fe80)
41 # - without tentative flag (bit 0x40 in the flags field; the first char of the fifth field is evaluated)
42 # - on interface $iface
43 if awk '
44 BEGIN { RET=1 }
45 $1 ~ /^fe80/ && $5 ~ /^[012389ab]/ && $6 == "'"$iface"'" { RET=0 }
46 END { exit RET }
47 ' /proc/net/if_inet6; then
48 return
49 fi
50 sleep 1
51 done
52
53 echo "$iface address not detected, alfred not starting."
54 exit 1
55 }
56
57 alfred_start() {
58 local args=""
59 local section="$1"
60 local disabled interface mode
61
62 # check if section is disabled
63 config_get_bool disabled "$section" disabled 0
64 [ $disabled = 0 ] || return 1
65
66 args=""
67
68 config_get interface "$section" interface
69 append args "-i $interface"
70
71 config_get mode "$section" mode
72 [ "$mode" = "master" ] && append args "-m"
73
74 config_get batmanif "$section" batmanif
75 append args "-b $batmanif"
76
77 if [ "$batmanif" != "none" ]; then
78 wait_for_dir "$batmanif" "/sys/class/net/$batmanif/mesh"
79 fi
80
81 wait_for_ll_address "$interface"
82
83 append alfred_args "$args"
84 enable=1
85
86 config_get_bool start_vis "$section" start_vis 0
87 if [ "$start_vis" = 1 ] && [ -x /usr/sbin/batadv-vis ]; then
88 vis_enable=1
89 append vis_args "-i $batmanif -s"
90 fi
91
92 config_get_bool run_facters "$section" run_facters 0
93
94 return 0
95 }
96
97 start_service() {
98 config_load "alfred"
99 config_foreach alfred_start alfred
100
101 [ "$enable" = "0" ] && { exit 0 }
102
103 procd_open_instance "alfred"
104 procd_set_param command /usr/sbin/alfred
105 procd_append_param command ${alfred_args}
106 procd_close_instance
107
108 [ "$vis_enable" = "1" ] && {
109 procd_open_instance "batadv-vis"
110 procd_set_param command /usr/sbin/batadv-vis
111 procd_append_param command ${vis_args}
112 procd_close_instance
113 }
114
115 [ "$run_facters" = "1" ] && {
116 ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done )
117 if ! ( grep -q "for file in $facters_dir/\* ; do " /etc/crontabs/root 2>/dev/null ) ; then
118 echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x \$file ] && \$file ; done )" >> /etc/crontabs/root
119 /etc/init.d/cron enable
120 /etc/init.d/cron restart
121 fi
122 }
123 }
124
125 service_triggers() {
126 procd_add_reload_trigger "alfred"
127 }
128
129 stop_service() {
130 [ -e /etc/crontabs/root ] && {
131 sed "\|for file in $facters_dir/\* ; do |d" -i /etc/crontabs/root
132 /etc/init.d/cron restart
133 }
134 }