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.

528 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.2"
  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 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} -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. if [ -n "${result}" ] && ([ -z "${trm_connection}" ] || [ "${result}" != "${trm_connection%/*}" ])
  230. then
  231. cp_domain="$(printf "%s" "${result}" | awk -F "['| ]" '/^net cp/{printf "%s" $4}')"
  232. if [ -x "/etc/init.d/dnsmasq" ] && [ -n "${cp_domain}" ]
  233. then
  234. if [ -z "$(uci_get dhcp "@dnsmasq[0]" rebind_domain | grep -Fo "${cp_domain}")" ]
  235. then
  236. uci -q add_list dhcp.@dnsmasq[0].rebind_domain="${cp_domain}"
  237. uci_commit dhcp
  238. /etc/init.d/dnsmasq reload
  239. fi
  240. fi
  241. trm_connection="${result}/${trm_ifquality}"
  242. f_jsnup
  243. fi
  244. fi
  245. break
  246. fi
  247. fi
  248. wait=$(( wait + 1 ))
  249. sleep 1
  250. done
  251. 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}"
  252. }
  253. # update runtime information
  254. #
  255. f_jsnup()
  256. {
  257. local IFS config sta_iface sta_radio sta_essid sta_bssid dev_status status="${trm_ifstatus}" faulty_list faulty_station="${1}"
  258. if [ "${status}" = "true" ]
  259. then
  260. status="connected (${trm_connection:-"-"})"
  261. else
  262. unset trm_connection
  263. status="running / not connected"
  264. fi
  265. dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
  266. if [ -n "${dev_status}" ]
  267. then
  268. config="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
  269. if [ -n "${config}" ]
  270. then
  271. sta_iface="$(uci_get wireless "${config}" network)"
  272. sta_radio="$(uci_get wireless "${config}" device)"
  273. sta_essid="$(uci_get wireless "${config}" ssid)"
  274. sta_bssid="$(uci_get wireless "${config}" bssid)"
  275. fi
  276. fi
  277. json_get_var faulty_list "faulty_stations"
  278. if [ -n "${faulty_list}" ] || [ -n "${faulty_station}" ]
  279. then
  280. if [ -z "$(printf "%s" "${faulty_list}" | grep -Fo "${faulty_station}")" ]
  281. then
  282. faulty_list="$(f_trim "${faulty_list} ${faulty_station}")"
  283. fi
  284. fi
  285. json_add_string "travelmate_status" "${status}"
  286. json_add_string "travelmate_version" "${trm_ver}"
  287. json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
  288. json_add_string "station_interface" "${sta_iface:-"-"}"
  289. json_add_string "faulty_stations" "${faulty_list}"
  290. json_add_string "last_rundate" "$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  291. json_add_string "system" "${trm_sysver}"
  292. json_dump > "${trm_rtfile}"
  293. 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:-"-"}"
  294. }
  295. # write to syslog
  296. #
  297. f_log()
  298. {
  299. local IFS class="${1}" log_msg="${2}"
  300. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${trm_debug} -eq 1 ])
  301. then
  302. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "${log_msg}"
  303. if [ "${class}" = "err" ]
  304. then
  305. trm_ifstatus="error"
  306. f_jsnup
  307. logger -p "${class}" -t "travelmate-${trm_ver}[${$}]" "Please check 'https://github.com/openwrt/packages/blob/master/net/travelmate/files/README.md' (${trm_sysver})"
  308. exit 1
  309. fi
  310. fi
  311. }
  312. # main function for connection handling
  313. #
  314. f_main()
  315. {
  316. local IFS cnt dev config scan scan_list scan_essid scan_bssid scan_quality faulty_list
  317. local station_id sta sta_essid sta_bssid sta_radio sta_iface active_essid active_bssid active_radio
  318. f_check "initial"
  319. f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
  320. if [ "${trm_ifstatus}" != "true" ] || [ ${trm_proactive} -eq 1 ]
  321. then
  322. config_load wireless
  323. config_foreach f_prep wifi-iface ${trm_proactive}
  324. if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_active_sta}" ] && [ ${trm_proactive} -eq 1 ]
  325. then
  326. json_get_var station_id "station_id"
  327. active_radio="${station_id%%/*}"
  328. active_essid="${station_id%/*}"
  329. active_essid="${active_essid#*/}"
  330. active_bssid="${station_id##*/}"
  331. f_check "dev" "true"
  332. f_log "debug" "f_main ::: active_radio: ${active_radio}, active_essid: \"${active_essid}\", active_bssid: ${active_bssid:-"-"}"
  333. else
  334. uci_commit wireless
  335. f_check "dev"
  336. fi
  337. json_get_var faulty_list "faulty_stations"
  338. f_log "debug" "f_main ::: iwinfo: ${trm_iwinfo:-"-"}, dev_list: ${trm_devlist:-"-"}, sta_list: ${trm_stalist:0:800}, faulty_list: ${faulty_list:-"-"}"
  339. # radio loop
  340. #
  341. for dev in ${trm_devlist}
  342. do
  343. if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\-${dev}")" ]
  344. then
  345. f_log "debug" "f_main ::: no station on '${dev}' - continue"
  346. continue
  347. fi
  348. # station loop
  349. #
  350. for sta in ${trm_stalist}
  351. do
  352. config="${sta%%-*}"
  353. sta_radio="${sta##*-}"
  354. sta_essid="$(uci_get wireless "${config}" ssid)"
  355. sta_bssid="$(uci_get wireless "${config}" bssid)"
  356. sta_iface="$(uci_get wireless "${config}" network)"
  357. json_get_var faulty_list "faulty_stations"
  358. if [ -n "$(printf "%s" "${faulty_list}" | grep -Fo "${sta_radio}/${sta_essid}/${sta_bssid}")" ]
  359. then
  360. f_log "debug" "f_main ::: faulty station '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' - continue"
  361. continue
  362. fi
  363. if [ "${dev}" = "${active_radio}" ] && [ "${sta_essid}" = "${active_essid}" ] && [ "${sta_bssid:-"-"}" = "${active_bssid}" ]
  364. then
  365. f_log "debug" "f_main ::: active station prioritized '${active_radio}/${active_essid}/${active_bssid:-"-"}' - break"
  366. break 2
  367. fi
  368. f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
  369. if [ -z "${scan_list}" ]
  370. then
  371. scan_list="$(f_trim "$("${trm_iwinfo}" "${dev}" scan 2>/dev/null | \
  372. 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}' | \
  373. sort -rn | awk 'BEGIN{ORS=","}{print $0}' | awk '{print substr($0,1,4096)}')")"
  374. f_log "debug" "f_main ::: scan_list: ${scan_list:0:800}"
  375. if [ -z "${scan_list}" ]
  376. then
  377. f_log "debug" "f_main ::: no scan results on '${dev}' - continue"
  378. continue 2
  379. fi
  380. fi
  381. # scan loop
  382. #
  383. IFS=","
  384. for scan in ${scan_list}
  385. do
  386. if [ -z "${scan_quality}" ]
  387. then
  388. scan_quality="${scan}"
  389. elif [ -z "${scan_bssid}" ]
  390. then
  391. scan_bssid="${scan}"
  392. elif [ -z "${scan_essid}" ]
  393. then
  394. scan_essid="${scan}"
  395. fi
  396. if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ]
  397. then
  398. if [ ${scan_quality} -ge ${trm_minquality} ]
  399. then
  400. if (([ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && ([ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ])) || \
  401. ([ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ])) && [ "${dev}" = "${sta_radio}" ]
  402. then
  403. f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}"
  404. if [ "${dev}" = "${active_radio}" ]
  405. then
  406. unset trm_connection active_radio active_essid active_bssid
  407. uci_set wireless "${trm_active_sta}" disabled 1
  408. uci_commit wireless
  409. fi
  410. # retry loop
  411. #
  412. cnt=1
  413. while [ ${cnt} -le ${trm_maxretry} ]
  414. do
  415. uci_set wireless "${config}" disabled 0
  416. f_check "sta"
  417. if [ "${trm_ifstatus}" = "true" ]
  418. then
  419. unset IFS scan_list
  420. uci_commit wireless
  421. f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"} (${cnt}/${trm_maxretry}, ${trm_sysver})"
  422. return 0
  423. else
  424. uci -q revert wireless
  425. f_check "rev"
  426. if [ ${cnt} -eq ${trm_maxretry} ]
  427. then
  428. faulty_station="${sta_radio}/${sta_essid}/${sta_bssid:-"-"}"
  429. f_jsnup "${faulty_station}"
  430. f_log "info" "uplink disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  431. break 2
  432. else
  433. f_jsnup
  434. f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${cnt}/${trm_maxretry}, ${trm_sysver})"
  435. fi
  436. fi
  437. cnt=$(( cnt + 1 ))
  438. sleep $(( ${trm_maxwait} / 6 ))
  439. done
  440. else
  441. unset scan_quality scan_bssid scan_essid
  442. continue
  443. fi
  444. else
  445. unset scan_quality scan_bssid scan_essid
  446. continue
  447. fi
  448. fi
  449. done
  450. unset IFS scan_quality scan_bssid scan_essid
  451. done
  452. unset scan_list
  453. done
  454. fi
  455. }
  456. # source required system libraries
  457. #
  458. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  459. then
  460. . "/lib/functions.sh"
  461. . "/usr/share/libubox/jshn.sh"
  462. else
  463. f_log "err" "system libraries not found"
  464. fi
  465. # control travelmate actions
  466. #
  467. f_envload
  468. while true
  469. do
  470. if [ -z "${trm_action}" ]
  471. then
  472. rc=0
  473. while true
  474. do
  475. if [ ${rc} -eq 0 ]
  476. then
  477. f_check "initial"
  478. fi
  479. sleep ${trm_timeout} 0
  480. rc=${?}
  481. if [ ${rc} -ne 0 ]
  482. then
  483. f_check "initial"
  484. fi
  485. if [ ${rc} -eq 0 ] || ([ ${rc} -ne 0 ] && [ "${trm_ifstatus}" = "false" ])
  486. then
  487. break
  488. fi
  489. done
  490. elif [ "${trm_action}" = "stop" ]
  491. then
  492. > "${trm_rtfile}"
  493. f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
  494. exit 0
  495. else
  496. f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
  497. unset trm_action
  498. fi
  499. json_cleanup
  500. f_envload
  501. f_main
  502. done