blob: e0e87a16ab22103ab150a568a3066643b3fd6237
1 | #!/bin/sh |
2 | |
3 | # How often to test, seconds |
4 | ping_time=67 |
5 | # "One ping, must have reply in 1 sec" |
6 | ping_opts="-c1 -W1 -w1" |
7 | # If ping failed, how soon to retry |
8 | retry_time=5 |
9 | # Reinit after this many consecutive ping error |
10 | max_fail=5 |
11 | # Interface whose DHCP data to use |
12 | if=${PWD##*/dhcp_} |
13 | if=${if%%_pinger} |
14 | |
15 | msg() { |
16 | echo "`date '+%Y-%m-%d %H:%M:%S'` $*" >>"$0.log" |
17 | } |
18 | |
19 | if test -f "$0.log"; then |
20 | tail -999 "$0.log" >"$0.log.new" |
21 | mv "$0.log.new" "$0.log" |
22 | fi |
23 | |
24 | test -f "/var/service/dhcp_$if/env.out" || exec env - sleep "$ping_time" |
25 | |
26 | . "/var/service/dhcp_$if/env.out" |
27 | test x"$router" != x"" || exec env - sleep "$ping_time" |
28 | |
29 | #msg "Pinging $router" |
30 | failcnt=0 |
31 | while true; do |
32 | ping $ping_opts "$router" && exec env - sleep "$ping_time" |
33 | : $((failcnt++)) |
34 | msg "Failed to ping $router, fail count:$failcnt" |
35 | test $failcnt -ge $max_fail && break |
36 | env - sleep "$retry_time" |
37 | done |
38 | |
39 | test -d "/var/service/dhcp_$if" && { |
40 | msg "Restarting /var/service/dhcp_$if" |
41 | sv t "/var/service/dhcp_$if" |
42 | } |
43 | test -d "/var/service/supplicant_$if" && { |
44 | msg "Restarting /var/service/supplicant_$if" |
45 | sv t "/var/service/supplicant_$if" |
46 | } |
47 | exec env - sleep "$ping_time" |
48 |