32e16a911297a78a64b314eddafd32c954d92805
[feed/routing.git] / bird-openwrt / bird6-openwrt / src / init.d / bird6
1 #!/bin/sh /etc/rc.common
2
3 # Copyright (C) 2014-2017 - Eloi Carbo
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # Extra Service Function to get the Status of the Service
20 # This complements /etc/rc.common functions
21 # Commands ending with *_quiet are meant to be ran in Luci. These
22 # scripts' return minimal output.
23 EXTRA_COMMANDS="status start_quiet stop_quiet restart_quiet status_quiet"
24 EXTRA_HELP=" status Returns service status"
25
26 BIRD="bird6"
27 BIRD_CONFIG="/etc/${BIRD}.conf"
28 BIRD_LOG="/var/log/${BIRD}.log"
29 BIRD_ERR="/tmp/${BIRD}.err"
30
31 START=99
32 STOP=10
33
34 SERVICE_DAEMONIZE=1
35 SERVICE_USE_PID=1
36 SERVICE_PID_FILE="/var/run/${BIRD}.pid"
37
38 BIRD_BIN="/usr/sbin/${BIRD}"
39 # Special non-terminal-rich output for Luci calls
40 LUCI="false"
41
42 . /etc/${BIRD}/init.d/${BIRD}-lib.sh
43
44 start() {
45 config_load ${BIRD}
46 local use_UCI_config
47 get use_UCI_config 'bird'
48
49 #Start the service
50 if [ "${LUCI}" == "false" ]; then
51 echo "Starting ${BIRD} Service [ ... ]"
52 fi
53 if [ -f ${BIRD_ERR} ]; then
54 echo "" > ${BIRD_ERR}
55 else
56 touch ${BIRD_ERR}
57 fi
58
59 if [ -z "${use_UCI_config}" -o "${use_UCI_config}" = "0" ]; then
60 # Disable Custom bird-openwrt settings.
61 # Use default behaviour and files
62 ${BIRD_BIN} -d -c ${BIRD_CONFIG} -P ${SERVICE_PID_FILE} -D ${BIRD_LOG} &> ${BIRD_ERR} &
63 else
64 #Set Bird6 configuration location:
65 local UCI_config_file
66 local log_file
67 get UCI_config_file 'bird'
68 get log_file 'global'
69 BIRD_CONFIG="${UCI_config_file:-$BIRD_CONFIG}"
70 BIRD_LOG="${log_file:-$BIRD_LOG}"
71 #Backup previous configuration
72 [ -f ${BIRD_CONFIG} ] && cp ${BIRD_CONFIG} ${BIRD_CONFIG}.bak
73 #Setup the basic configuration
74 prepare_global 'global'
75
76 # Gather and set all Functions
77 gather_functions
78 # Gather and set all Filters
79 gather_filters
80
81 # Setup Main Protocols
82 config_foreach prepare_kernel 'kernel'
83 config_foreach prepare_static 'static'
84 config_foreach prepare_device 'device'
85 config_foreach prepare_direct 'direct'
86 config_foreach prepare_pipe 'pipe'
87
88 #Setup protocol's configuration: BGP
89 config_foreach prepare_bgp_template 'bgp_template'
90 config_foreach prepare_bgp 'bgp'
91
92 #Setup protocol's configuration: OSPF
93 #config_foreach prepare_ospf_instance 'ospf'
94
95 #Start the service
96 ${BIRD_BIN} -d -c ${BIRD_CONFIG} -P ${SERVICE_PID_FILE} -D ${BIRD_LOG} &>${BIRD_ERR} &
97 fi
98 while [ ! -s ${SERVICE_PID_FILE} ]; do
99 sleep 1
100 if [ -s ${BIRD_ERR} ]; then
101 if [ "${LUCI}" == "false" ]; then
102 echo -e "${BIRD} Daemon Start Status: \033[0;31m[ FAILED ]\e[m"
103 cat ${BIRD_ERR}
104 cat ${BIRD_ERR} >> ${BIRD_LOG}
105 else
106 echo "${BIRD} - Failed: $(cat ${BIRD_ERR})"
107 cat ${BIRD_ERR} >> ${BIRD_LOG}
108 fi
109 break
110 fi
111 done
112
113 if [ -s ${SERVICE_PID_FILE} ]; then
114 if [ -s ${BIRD_ERR} ]; then
115 if [ "${LUCI}" == "false" ]; then
116 echo -e "${BIRD} Daemon already started. Status \033[0;32m[ RUNNING ]\e[m"
117 else
118 echo "${BIRD} already started"
119 fi
120 else
121 if [ "${LUCI}" == "false" ]; then
122 echo -e "${BIRD} Daemon Start Status: \033[0;32m[ STARTED ]\e[m"
123 else
124 echo "${BIRD} - Started"
125 fi
126 fi
127 # PID File found (service started correctly)
128 return 0
129 fi
130
131 # PID File not found (error while starting service)
132 return 1
133 }
134
135 stop() {
136 if [ -s ${SERVICE_PID_FILE} ]; then
137 config_load ${BIRD}
138 local log_file
139 get log_file 'global'
140 BIRD_LOG="${log_file:-$BIRD_LOG}"
141 start-stop-daemon -p ${SERVICE_PID_FILE} -K 2>&1 >> ${BIRD_LOG}
142 if [ $? -eq 0 ]; then
143 if [ "${LUCI}" == "false" ]; then
144 echo -e "${BIRD} Daemon Stop Status: \033[0;32m[ OK ]\e[m"
145 else
146 echo "${BIRD} - Stopped"
147 fi
148 else
149 if [ "${LUCI}" == "false" ]; then
150 echo -e "${BIRD} Daemon Stop Status: \033[0;31m[ FAILED ]\e[m"
151 echo "Check ${BIRD_LOG} file for more information."
152 else
153 echo "${BIRD} Failed to Stop. See Log file: ${BIRD_LOG}"
154 fi
155 fi
156 else
157 if [ "${LUCI}" == "false" ]; then
158 echo -e "${BIRD} Daemon Service already stopped. \033[0;31m[ FAILED ]\e[m"
159 else
160 echo "${BIRD} already stopped"
161 fi
162 fi
163 return 0
164 }
165
166 restart() {
167 stop
168 sleep 1
169 if [ "${LUCI}" == "true" ]; then
170 echo " ... "
171 fi
172 start
173 }
174
175 reload() {
176 service_reload ${BIRD_BIN}
177 }
178
179 status() {
180 if [ -s ${SERVICE_PID_FILE} ]; then
181 if [ "${LUCI}" == "false" ]; then
182 echo -e "${BIRD} start status: \033[0;32m[ RUNNING ]\e[m"
183 else
184 echo "${BIRD}: Running"
185 fi
186 return 0
187 else
188 if [ -s ${BIRD_ERR} ]; then
189 if [ "${LUCI}" == "false" ]; then
190 echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
191 cat ${BIRD_ERR}
192 else
193 echo "${BIRD}: Failed - $(cat ${BIRD_ERR})"
194 fi
195 return 2
196 else
197 if [ "${LUCI}" == "false" ]; then
198 echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
199 else
200 echo "${BIRD}: Stopped"
201 fi
202 return 1
203 fi
204 fi
205 }
206
207 # Luci-specific calls (stripped output).
208 # The following scripts are not meant to be ran using Ash Terminal
209 # Used in: LUCI/model/cbi/bird6/status.lua
210 start_quiet() {
211 LUCI="true"
212 start
213 }
214 stop_quiet() {
215 LUCI="true"
216 stop
217 }
218 restart_quiet() {
219 LUCI="true"
220 restart
221 }
222 status_quiet() {
223 LUCI="true"
224 status
225 }