Merge pull request #8361 from jandelgado/add_udptunnel_package
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # written by Dirk Brenken (dev@brenken.org)
4
5 # This is free software, licensed under the GNU General Public License v3.
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8
9 # set initial defaults
10 #
11 LC_ALL=C
12 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
13 trm_ver="1.4.5"
14 trm_sysver="unknown"
15 trm_enabled=0
16 trm_debug=0
17 trm_iface="trm_wwan"
18 trm_captive=1
19 trm_proactive=1
20 trm_netcheck=0
21 trm_captiveurl="http://captive.apple.com"
22 trm_minquality=35
23 trm_maxretry=3
24 trm_maxwait=30
25 trm_timeout=60
26 trm_listexpiry=0
27 trm_radio=""
28 trm_connection=""
29 trm_rtfile="/tmp/trm_runtime.json"
30 trm_fetch="$(command -v uclient-fetch)"
31 trm_iwinfo="$(command -v iwinfo)"
32 trm_wpa="$(command -v wpa_supplicant)"
33 trm_action="${1:-"start"}"
34 trm_pidfile="/var/run/travelmate.pid"
35
36 # trim leading and trailing whitespace characters
37 #
38 f_trim()
39 {
40 local IFS trim="${1}"
41
42 trim="${trim#"${trim%%[![:space:]]*}"}"
43 trim="${trim%"${trim##*[![:space:]]}"}"
44 printf '%s' "${trim}"
45 }
46
47 # load travelmate environment
48 #
49 f_envload()
50 {
51 local IFS sys_call sys_desc sys_model
52
53 # (re-)initialize global list variables
54 #
55 unset trm_devlist trm_stalist trm_radiolist trm_active_sta
56
57 # get system information
58 #
59 sys_call="$(ubus -S call system board 2>/dev/null)"
60 if [ -n "${sys_call}" ]
61 then
62 sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
63 sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
64 trm_sysver="${sys_model}, ${sys_desc}"
65 fi
66
67 # get eap capabilities and rebind protection setting
68 #
69 trm_eap="$("${trm_wpa}" -veap >/dev/null 2>&1; printf "%u" ${?})"
70 trm_rebind="$(uci_get dhcp "@dnsmasq[0]" rebind_protection)"
71
72 # load config and check 'enabled' option
73 #
74 config_cb()
75 {
76 local name="${1}" type="${2}"
77 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
78 then
79 option_cb()
80 {
81 local option="${1}" value="${2}"
82 eval "${option}=\"${value}\""
83 }
84 else
85 option_cb()
86 {
87 return 0
88 }
89 fi
90 }
91 config_load travelmate
92
93 if [ ${trm_enabled} -ne 1 ]
94 then
95 f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
96 exit 0
97 fi
98
99 # validate input ranges
100 #
101 if [ ${trm_minquality} -lt 20 ] || [ ${trm_minquality} -gt 80 ]
102 then
103 trm_minquality=35
104 fi
105 if [ ${trm_listexpiry} -lt 0 ] || [ ${trm_listexpiry} -gt 300 ]
106 then
107 trm_listexpiry=0
108 fi
109 if [ ${trm_maxretry} -lt 1 ] || [ ${trm_maxretry} -gt 10 ]
110 then
111 trm_maxretry=5
112 fi
113 if [ ${trm_maxwait} -lt 20 ] || [ ${trm_maxwait} -gt 40 ] || [ ${trm_maxwait} -ge ${trm_timeout} ]
114 then
115 trm_maxwait=30
116 fi
117 if [ ${trm_timeout} -lt 30 ] || [ ${trm_timeout} -gt 300 ] || [ ${trm_timeout} -le ${trm_maxwait} ]
118 then
119 trm_timeout=60
120 fi
121
122 # load json runtime file
123 #
124 json_load_file "${trm_rtfile}" >/dev/null 2>&1
125 json_select data >/dev/null 2>&1
126 if [ ${?} -ne 0 ]
127 then
128 > "${trm_rtfile}"
129 json_init
130 json_add_object "data"
131 fi
132 }
133
134 # gather radio information & bring down all STA interfaces
135 #
136 f_prep()
137 {
138 local IFS config="${1}" proactive="${2}"
139 local mode="$(uci_get wireless "${config}" mode)"
140 local network="$(uci_get wireless "${config}" network)"
141 local radio="$(uci_get wireless "${config}" device)"
142 local disabled="$(uci_get wireless "${config}" disabled)"
143 local eaptype="$(uci_get wireless "${config}" eap_type)"
144
145 if [ -n "${config}" ] && [ -n "${radio}" ] && [ -n "${mode}" ] && [ -n "${network}" ]
146 then
147 if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
148 then
149 trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
150 elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
151 then
152 trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
153 awk '{while(match(tolower($0),/radio[0-9]/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
154 fi
155 if [ "${mode}" = "sta" ] && [ "${network}" = "${trm_iface}" ]
156 then
157 if ([ -z "${disabled}" ] || [ "${disabled}" = "0" ]) && ([ ${proactive} -eq 0 ] || [ "${trm_ifstatus}" != "true" ])
158 then
159 uci_set wireless "${config}" disabled 1
160 elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ -z "${trm_active_sta}" ] && [ ${proactive} -eq 1 ]
161 then
162 trm_active_sta="${config}"
163 fi
164 if [ -z "${eaptype}" ] || ([ -n "${eaptype}" ] && [ ${trm_eap:-1} -eq 0 ])
165 then
166 trm_stalist="$(f_trim "${trm_stalist} ${config}-${radio}")"
167 fi
168 fi
169 fi
170 f_log "debug" "f_prep ::: config: ${config}, mode: ${mode}, network: ${network}, radio: ${radio}, trm_radio: ${trm_radio:-"-"}, trm_active_sta: ${trm_active_sta:-"-"}, proactive: ${proactive}, trm_eap: ${trm_eap:-"-"}, trm_rebind: ${trm_rebind:-"-"}, disabled: ${disabled}"
171 }
172
173 # check interface status
174 #
175 f_check()
176 {
177 local IFS ifname radio dev_status last_status config sta_essid sta_bssid result cp_domain wait mode="${1}" status="${2:-"false"}"
178
179 if [ "${mode}" != "initial" ] && [ "${status}" = "false" ]
180 then
181 ubus call network reload
182 wait=$(( ${trm_maxwait} / 6 ))
183 sleep ${wait}
184 fi
185
186 wait=1
187 while [ ${wait} -le ${trm_maxwait} ]
188 do
189 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
190 if [ -n "${dev_status}" ]
191 then
192 if [ "${mode}" = "dev" ]
193 then
194 if [ "${trm_ifstatus}" != "${status}" ]
195 then
196 trm_ifstatus="${status}"
197 f_jsnup
198 fi
199 for radio in ${trm_radiolist}
200 do
201 result="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e "@.${radio}.up")"
202 if [ "${result}" = "true" ] && [ -z "$(printf "%s" "${trm_devlist}" | grep -Fo "${radio}")" ]
203 then
204 trm_devlist="$(f_trim "${trm_devlist} ${radio}")"
205 fi
206 done
207 if [ "${trm_devlist}" = "${trm_radiolist}" ] || [ ${wait} -eq ${trm_maxwait} ]
208 then
209 ifname="${trm_devlist}"
210 break
211 else
212 unset trm_devlist
213 fi
214 elif [ "${mode}" = "rev" ]
215 then
216 break
217 else
218 ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
219 if [ -n "${ifname}" ]
220 then
221 trm_ifquality="$(${trm_iwinfo} ${ifname} info 2>/dev/null | awk -F "[\/| ]" '/Link Quality:/{printf "%i\n", (100 / $NF * $(NF-1)) }')"
222 if [ ${trm_captive} -eq 1 ]
223 then
224 result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev/null 2>&1 | \
225 awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
226 fi
227 if [ ${trm_ifquality} -ge ${trm_minquality} ] && ([ ${trm_captive} -eq 0 ] || [ ${trm_netcheck} -eq 0 ] || [ "${result%/*}" != "net nok" ])
228 then
229 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
230 if [ "${trm_ifstatus}" = "true" ]
231 then
232 if [ ${trm_captive} -eq 1 ]
233 then
234 cp_domain="$(printf "%s" "${result}" | awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
235 if [ -n "${cp_domain}" ] && [ ${trm_rebind:-0} -eq 1 ] && [ -x "/etc/init.d/dnsmasq" ]
236 then
237 while [ -n "${cp_domain}" ] && [ -z "$(uci_get dhcp "@dnsmasq[0]" rebind_domain | grep -Fo "${cp_domain}")" ]
238 do
239 uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
240 uci_commit dhcp
241 /etc/init.d/dnsmasq reload
242 f_log "info" "captive portal domain '${cp_domain}' added to rebind whitelist"
243 result="$(${trm_fetch} --timeout=$(( ${trm_maxwait} / 3 )) "${trm_captiveurl}" -O /dev/null 2>&1 | \
244 awk '/^Failed to redirect|^Redirected/{printf "%s" "net cp \047"$NF"\047";exit}/^Download completed/{printf "%s" "net ok";exit}/^Failed|^Connection error/{printf "%s" "net nok";exit}')"
245 cp_domain="$(printf "%s" "${result}" | awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
246 done
247 fi
248 fi
249 trm_connection="${result}/${trm_ifquality}"
250 f_jsnup
251 break
252 fi
253 else
254 if [ -n "${trm_connection}" ]
255 then
256 sta_essid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.ssid')"
257 sta_bssid="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].*.bssid')"
258 if [ ${trm_ifquality} -lt ${trm_minquality} ]
259 then
260 f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' is out of range (${trm_ifquality}/${trm_minquality})"
261 elif [ ${trm_captive} -eq 1 ] && [ ${trm_netcheck} -eq 1 ] && [ "${result%/*}" = "net nok" ]
262 then
263 f_log "info" "uplink '${sta_essid:-"-"}/${sta_bssid:-"-"}' has no internet (${result})"
264 fi
265 unset trm_connection
266 trm_ifstatus="${status}"
267 f_jsnup
268 break
269 fi
270 fi
271 else
272 if [ -n "${trm_connection}" ]
273 then
274 unset trm_connection
275 trm_ifstatus="${status}"
276 f_jsnup
277 break
278 fi
279 fi
280 fi
281 fi
282 wait=$(( wait + 1 ))
283 sleep 1
284 done
285 f_log "debug" "f_check::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, quality: ${trm_ifquality}, result: ${result:-"-"}, connection: ${trm_connection:-"-"}, wait: ${wait}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
286 }
287
288 # update runtime information
289 #
290 f_jsnup()
291 {
292 local IFS config d1 d2 d3 last_date last_station sta_iface sta_radio sta_essid sta_bssid last_status dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
293
294 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
295 if [ -n "${dev_status}" ]
296 then
297 config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
298 if [ -n "${config}" ]
299 then
300 sta_iface="$(uci_get wireless "${config}" network)"
301 sta_radio="$(uci_get wireless "${config}" device)"
302 sta_essid="$(uci_get wireless "${config}" ssid)"
303 sta_bssid="$(uci_get wireless "${config}" bssid)"
304 fi
305 fi
306
307 json_get_var last_date "last_rundate"
308 json_get_var last_station "station_id"
309 if [ "${status}" = "true" ]
310 then
311 status="connected (${trm_connection:-"-"})"
312 json_get_var last_status "travelmate_status"
313 if [ "${last_status}" = "running / not connected" ] || [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]
314 then
315 last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
316 fi
317 else
318 unset trm_connection
319 status="running / not connected"
320 fi
321 if [ -z "${last_date}" ]
322 then
323 last_date="$(/bin/date "+%Y.%m.%d-%H:%M:%S")"
324 fi
325
326 json_get_var faulty_list "faulty_stations"
327 if [ -n "${faulty_list}" ] && [ ${trm_listexpiry} -gt 0 ]
328 then
329 d1="$(/bin/date -d "${last_date}" "+%s")"
330 d2="$(/bin/date "+%s")"
331 d3=$(( (d2 - d1) / 60 ))
332 if [ ${d3} -ge ${trm_listexpiry} ]
333 then
334 faulty_list=""
335 fi
336 fi
337
338 if [ -n "${faulty_station}" ]
339 then
340 if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
341 then
342 faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
343 fi
344 fi
345 json_add_string "travelmate_status" "${status}"
346 json_add_string "travelmate_version" "${trm_ver}"
347 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
348 json_add_string "station_interface" "${sta_iface:-"-"}"
349 json_add_string "faulty_stations" "${faulty_list}"
350 json_add_string "last_rundate" "${last_date}"
351 json_add_string "system" "${trm_sysver}"
352 json_dump > "${trm_rtfile}"
353 f_log "debug" "f_jsnup::: config: ${config:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, faulty_list: ${faulty_list:-"-"}, list_expiry: ${trm_listexpiry}"
354 }
355
356 # write to syslog
357 #
358 f_log()
359 {
360 local IFS class="${1}" log_msg="${2}"
361
362 if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
363 then
364 logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
365 if [ "${class}" = "err" ]
366 then
367 trm_ifstatus="error"
368 f_jsnup
369 logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
370 exit 1
371 fi
372 fi
373 }
374
375 # main function for connection handling
376 #
377 f_main()
378 {
379 local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
380 local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
381
382 f_check "initial"
383 f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
384 if [ "${trm_ifstatus}" != "true" ] || [ ${trm_proactive} -eq 1 ]
385 then
386 config_load wireless
387 config_foreach f_prep wifi-iface ${trm_proactive}
388 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ ${trm_proactive} -eq 1 ]
389 then
390 json_get_var station_id "station_id"
391 active_radio="${station_id%%/*}"
392 active_essid="${station_id%/*}"
393 active_essid="${active_essid#*/}"
394 active_bssid="${station_id##*/}"
395 f_check "dev" "true"
396 f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
397 else
398 uci_commit wireless
399 f_check "dev"
400 fi
401 json_get_var faulty_list "faulty_stations"
402 f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
403 # radio loop
404 #
405 for dev in ${trm_devlist}
406 do
407 if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\-${dev}")" ]
408 then
409 f_log "debug" "f_main ::: no station on '${dev}' - continue"
410 continue
411 fi
412 # station loop
413 #
414 for sta in ${trm_stalist}
415 do
416 config="${sta%%-*}"
417 sta_radio="${sta##*-}"
418 sta_essid="$(uci_get wireless "${config}" ssid)"
419 sta_bssid="$(uci_get wireless "${config}" bssid)"
420 sta_iface="$(uci_get wireless "${config}" network)"
421 json_get_var faulty_list "faulty_stations"
422 if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
423 then
424 f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
425 continue
426 fi
427 if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
428 then
429 f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
430 break 2
431 fi
432 f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
433 if [ -z "${scan_list}" ]
434 then
435 scan_list="$(f_trim "$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
436 awk 'BEGIN{FS="[/ ]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};gsub(/,/,".",var2)}/Quality:/{printf "%i,%s,%s\n",(100/$NF*$(NF-1)),var1,var2}' | \
437 sort -rn | awk 'BEGIN{ORS=","}{print $0}' | awk '{print substr($0,1,4096)}')")"
438 f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
439 if [ -z "${scan_list}" ]
440 then
441 f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
442 continue 2
443 fi
444 fi
445 # scan loop
446 #
447 IFS=","
448 for scan in ${scan_list}
449 do
450 if [ -z "${scan_quality}" ]
451 then
452 scan_quality="${scan}"
453 elif [ -z "${scan_bssid}" ]
454 then
455 scan_bssid="${scan}"
456 elif [ -z "${scan_essid}" ]
457 then
458 scan_essid="${scan}"
459 fi
460 if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
461 then
462 if [ ${scan_quality} -ge ${trm_minquality} ]
463 then
464 if (([ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
465 ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
466 then
467 f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}"
468 if [ "${dev}" = "${active_radio}" ]
469 then
470 unset trm_connection active_radio active_essid active_bssid
471 uci_set wireless "${trm_active_sta}" disabled 1
472 uci_commit wireless
473 fi
474 # retry loop
475 #
476 cnt=1
477 while [ ${cnt} -le ${trm_maxretry} ]
478 do
479 uci_set wireless "${config}" disabled 0
480 f_check "sta"
481 if [ "${trm_ifstatus}" = "true" ]
482 then
483 unset IFS scan_list
484 uci_commit wireless
485 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
486 return 0
487 else
488 uci -q revert wireless
489 f_check "rev"
490 if [ ${cnt} -eq ${trm_maxretry} ]
491 then
492 faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
493 f_jsnup "${faulty_station}"
494 f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
495 break 2
496 else
497 f_jsnup
498 f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
499 fi
500 fi
501 cnt=$(( cnt + 1 ))
502 sleep $(( ${trm_maxwait} / 6 ))
503 done
504 else
505 unset scan_quality scan_bssid scan_essid
506 continue
507 fi
508 else
509 unset scan_quality scan_bssid scan_essid
510 continue
511 fi
512 fi
513 done
514 unset IFS scan_quality scan_bssid scan_essid
515 done
516 unset scan_list
517 done
518 fi
519 }
520
521 # source required system libraries
522 #
523 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
524 then
525 . "/lib/functions.sh"
526 . "/usr/share/libubox/jshn.sh"
527 else
528 f_log "err" "system libraries not found"
529 fi
530
531 # control travelmate actions
532 #
533 f_envload
534 while true
535 do
536 if [ -z "${trm_action}" ]
537 then
538 rc=0
539 while true
540 do
541 if [ ${rc} -eq 0 ]
542 then
543 f_check "initial"
544 fi
545 sleep ${trm_timeout} 0
546 rc=${?}
547 if [ ${rc} -ne 0 ]
548 then
549 f_check "initial"
550 fi
551 if [ ${rc} -eq 0 ] || ([ ${rc} -ne 0 ] && [ "${trm_ifstatus}" = "false" ])
552 then
553 break
554 fi
555 done
556 elif [ "${trm_action}" = "stop" ]
557 then
558 > "${trm_rtfile}"
559 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
560 exit 0
561 else
562 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
563 unset trm_action
564 fi
565 json_cleanup
566 f_envload
567 f_main
568 done