nodogsplash2: update to version 2.1.1 (#376)
[feed/routing.git] / bird-openwrt / bird4-openwrt / src / init.d / bird4
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="bird4"
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 -n "" > ${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 Bird4 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 # PID & ERROR contents are read from their files to avoid an issue
114 # where if [ -s ${SERVICE_PID_FILE} ] and if [ -s ${BIRD_ERR} ]
115 # fails unless a previous command reads its contents making its
116 # behaviour unreliable.
117 SVC_PID="$(cat ${SERVICE_PID_FILE})"
118 BRDERR_TXT="$(cat ${BIRD_ERR})"
119 if [ -n "${SVC_PID}" ]; then
120 if [ -n "${BRDERR_TXT}" ]; then
121 if [ "${LUCI}" == "false" ]; then
122 echo -e "${BIRD} Daemon already started. Status \033[0;32m[ RUNNING ]\e[m"
123 else
124 echo "${BIRD} already started"
125 fi
126 else
127 if [ "${LUCI}" == "false" ]; then
128 echo -e "${BIRD} Daemon Start Status: \033[0;32m[ STARTED ]\e[m"
129 else
130 echo "${BIRD} - Started"
131 fi
132 fi
133 # PID File found (service started correctly)
134 return 0
135 fi
136
137 # PID File not found (error while starting service)
138 return 1
139 }
140
141 stop() {
142 if [ -s ${SERVICE_PID_FILE} ]; then
143 config_load ${BIRD}
144 local log_file
145 get log_file 'global'
146 BIRD_LOG="${log_file:-$BIRD_LOG}"
147 start-stop-daemon -p ${SERVICE_PID_FILE} -K 2>&1 >> ${BIRD_LOG}
148 if [ $? -eq 0 ]; then
149 if [ "${LUCI}" == "false" ]; then
150 echo -e "${BIRD} Daemon Stop Status: \033[0;32m[ OK ]\e[m"
151 else
152 echo "${BIRD} - Stopped"
153 fi
154 echo -n "" > ${BIRD_ERR}
155 else
156 if [ "${LUCI}" == "false" ]; then
157 echo -e "${BIRD} Daemon Stop Status: \033[0;31m[ FAILED ]\e[m"
158 echo "Check ${BIRD_LOG} file for more information."
159 else
160 echo "${BIRD} Failed to Stop. See Log file: ${BIRD_LOG}"
161 fi
162 fi
163 else
164 if [ "${LUCI}" == "false" ]; then
165 echo -e "${BIRD} Daemon Service already stopped. \033[0;31m[ FAILED ]\e[m"
166 else
167 echo "${BIRD} already stopped"
168 fi
169 fi
170 return 0
171 }
172
173 restart() {
174 stop
175 sleep 1
176 if [ "${LUCI}" == "true" ]; then
177 echo " ... "
178 fi
179 start
180 }
181
182 reload() {
183 service_reload ${BIRD_BIN}
184 }
185
186 status() {
187 if [ -s ${SERVICE_PID_FILE} ]; then
188 if [ "${LUCI}" == "false" ]; then
189 echo -e "${BIRD} start status: \033[0;32m[ RUNNING ]\e[m"
190 else
191 echo "${BIRD}: Running"
192 fi
193 return 0
194 else
195 if [ -s ${BIRD_ERR} ]; then
196 if [ "${LUCI}" == "false" ]; then
197 echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
198 cat ${BIRD_ERR}
199 else
200 echo "${BIRD}: Failed - $(cat ${BIRD_ERR})"
201 fi
202 return 2
203 else
204 if [ "${LUCI}" == "false" ]; then
205 echo -e "${BIRD} service status: \033[0;31m[ STOPPED ]\e[m"
206 else
207 echo "${BIRD}: Stopped"
208 fi
209 return 1
210 fi
211 fi
212 }
213
214
215 # Luci-specific calls (stripped output).
216 # The following scripts are not meant to be ran using Ash Terminal
217 # Used in: LUCI/model/cbi/bird4/status.lua
218 start_quiet() {
219 LUCI="true"
220 start
221 }
222 stop_quiet() {
223 LUCI="true"
224 stop
225 }
226 restart_quiet() {
227 LUCI="true"
228 restart
229 }
230 status_quiet() {
231 LUCI="true"
232 status
233 }