You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

532 lines
17 KiB

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