alfred: Fix procd process handling for disable state
[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 return 1
28 fi
29 done
30 fi
31
32 return 0
33 }
34
35 wait_for_ll_address() {
36 local iface="$1"
37 local timeout=30
38
39 echo "waiting $timeout secs for $iface address..."
40 for i in $(seq $timeout); do
41 # We look for
42 # - the link-local address (starts with fe80)
43 # - without tentative flag (bit 0x40 in the flags field; the first char of the fifth field is evaluated)
44 # - on interface $iface
45 if awk '
46 BEGIN { RET=1 }
47 $1 ~ /^fe80/ && $5 ~ /^[012389ab]/ && $6 == "'"$iface"'" { RET=0 }
48 END { exit RET }
49 ' /proc/net/if_inet6; then
50 return 0
51 fi
52 sleep 1
53 done
54
55 echo "$iface address not detected, alfred not starting."
56 return 1
57 }
58
59 append_interface()
60 {
61 append "interfaces" "$1" ","
62 wait_for_ll_address "$1"
63 }
64
65 alfred_start() {
66 local args=""
67 local section="$1"
68 local disabled interface mode
69 local interfaces
70
71 # check if section is disabled
72 config_get_bool disabled "$section" disabled 0
73 [ $disabled = 0 ] || return 1
74
75 args=""
76
77 config_list_foreach "$section" "interface" append_interface
78 if [ -z "$interfaces" ]; then
79 config_get interface "$section" interface
80 append_interface "$interface"
81 fi
82 append args "-i $interfaces"
83
84 config_get mode "$section" mode
85 [ "$mode" = "master" ] && append args "-m"
86
87 config_get batmanif "$section" batmanif
88 append args "-b $batmanif"
89
90 if [ "$batmanif" != "none" ]; then
91 wait_for_dir "$batmanif" "/sys/devices/virtual/net/$batmanif" || return 1
92 fi
93
94 append alfred_args "$args"
95 enable=1
96
97 config_get_bool start_vis "$section" start_vis 0
98 if [ "$start_vis" = 1 ] && [ -x /usr/sbin/batadv-vis ]; then
99 vis_enable=1
100 append vis_args "-i $batmanif -s"
101 fi
102
103 config_get_bool run_facters "$section" run_facters 0
104
105 return 0
106 }
107
108 start_service() {
109 config_load "alfred"
110 config_foreach alfred_start alfred
111
112 [ "$enable" = "0" ] && return 0
113
114 procd_open_instance "alfred"
115 procd_set_param command /usr/sbin/alfred
116 procd_append_param command ${alfred_args}
117 procd_close_instance
118
119 [ "$vis_enable" = "1" ] && {
120 procd_open_instance "batadv-vis"
121 procd_set_param command /usr/sbin/batadv-vis
122 procd_append_param command ${vis_args}
123 procd_close_instance
124 }
125
126 [ "$run_facters" = "1" ] && {
127 ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done )
128 if ! ( grep -q "for file in $facters_dir/\* ; do " /etc/crontabs/root 2>/dev/null ) ; then
129 echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x \$file ] && \$file ; done )" >> /etc/crontabs/root
130 /etc/init.d/cron enable
131 /etc/init.d/cron restart
132 fi
133 }
134 }
135
136 service_triggers() {
137 procd_add_reload_trigger "alfred"
138 }
139
140 stop_service() {
141 [ -e /etc/crontabs/root ] && {
142 sed "\|for file in $facters_dir/\* ; do |d" -i /etc/crontabs/root
143 /etc/init.d/cron restart
144 }
145 }