]>
Commit | Line | Data |
---|---|---|
f2d9af81 MT |
1 | #!/bin/bash |
2 | # | |
3 | ||
4 | eval $(/usr/local/bin/readhash /var/ipfire/ppp/settings) | |
5 | ||
6 | # MAXRETRIES is now a count before red clear and if reconnection fail, eventually switching to a backup profil | |
7 | MAXCONFIGURE=160 | |
8 | if [ ! $HOLDOFF ]; then | |
9 | HOLDOFF=30 | |
10 | fi | |
11 | ||
12 | # Debugging. Comment it out to stop logging | |
13 | DEBUG="yes" | |
14 | msg() { | |
15 | if [ "z$DEBUG" != "z" ] ; then | |
16 | /usr/bin/logger -t red "Connectioncheck: $*" | |
17 | fi | |
18 | /bin/echo "$*" | |
19 | } | |
20 | ||
21 | if [ -s "/var/ipfire/red/keepconnected" ]; then | |
22 | ATTEMPTS=$(/bin/cat /var/ipfire/red/keepconnected) | |
23 | else | |
24 | echo "0" > /var/ipfire/red/keepconnected | |
25 | fi | |
26 | ||
27 | case "$1" in | |
28 | start) | |
29 | # waiting pppd start or sync timout set at 90 s when available in driver | |
30 | # So just wait 5 s after 'red start' end where pppd should have been started | |
31 | TIMETOSTART=0 | |
32 | while ( ! /bin/ps ax | /bin/grep -q [p]ppd ); do | |
33 | if [ ! -e "/var/ipfire/red/keepconnected" ]; then | |
34 | # user pressed disconnect in gui | |
35 | exit 0 | |
36 | fi | |
37 | RCREDSTART=`/bin/ps ax | /bin/grep 'red start'` | |
38 | if [ "$TIMETOSTART" -eq 1 ]; then | |
39 | msg "fail before pppd start : no sync, trying again" | |
40 | /etc/rc.d/init.d/red stop | |
41 | /etc/rc.d/init.d/red clear | |
42 | /etc/rc.d/init.d/red start | |
43 | exit | |
44 | fi | |
45 | # give 5 s more at pppd to be visible with ps ax after 'rc.red start' end | |
46 | if [ "$RCREDSTART" = '' ]; then | |
47 | TIMETOSTART=1 | |
48 | fi | |
49 | /bin/sleep 5 | |
50 | done | |
51 | ||
52 | #watching if an IP is received in $MAXCONFIGURE time to avoid some pppoa waiting forever even after LCP timeout | |
53 | COUNT=0 | |
54 | while [ "$COUNT" -le "$MAXCONFIGURE" ]; do | |
55 | if [ ! -e "/var/ipfire/red/keepconnected" ]; then | |
56 | # user pressed disconnect in gui | |
57 | exit 0 | |
58 | fi | |
59 | if [ -e "/var/ipfire/red/active" ]; then | |
60 | # connected in time so exit | |
61 | echo "0" > /var/ipfire/red/keepconnected | |
62 | exit 0 | |
63 | fi | |
64 | if [ -e "/var/ipfire/red/dial-on-demand" ]; then | |
65 | # Don't count time when dial-on-demand is not really connected | |
66 | if ( ! /sbin/ifconfig | /bin/grep -q addr:10.64.64.64 ); then | |
67 | (( COUNT += 5 )) | |
68 | fi | |
69 | else | |
70 | (( COUNT += 5 )) | |
71 | fi | |
72 | if ( ! /bin/ps ax | /bin/grep -q [p]ppd ); then | |
73 | #pppd exit, so don't need to wait MAXCONFIGURE | |
74 | break | |
75 | fi | |
76 | /bin/sleep 5 | |
77 | done | |
78 | msg "RED fail to connect" | |
79 | RCREDSTART=`/bin/ps ax | /bin/grep 'red start' | /usr/bin/cut -f1 -d ' '` | |
80 | if [ "$RCREDSTART" != '' ]; then | |
81 | /bin/kill "$RCREDSTART" | |
82 | fi | |
83 | # this attempt to connect fail, so retry | |
84 | ||
85 | #Don't erase keepconnected because it is used to watch user actions | |
86 | /bin/touch /var/ipfire/red/redial | |
87 | /etc/rc.d/init.d/red stop | |
88 | while ( /bin/ps ax | /bin/grep -q [p]ppd ); do | |
89 | msg "waiting pppd exit" | |
90 | /bin/sleep 2 | |
91 | done | |
92 | /bin/sleep $HOLDOFF | |
93 | if [ ! -e "/var/ipfire/red/keepconnected" ]; then | |
94 | # user pressed disconnect in gui | |
95 | exit 0 | |
96 | fi | |
97 | (( ATTEMPTS += 1 )) | |
98 | msg "Restarting $ATTEMPTS/$MAXRETRIES" | |
99 | if [ "$ATTEMPTS" -ge "$MAXRETRIES" ]; then | |
100 | echo "0" > /var/ipfire/red/keepconnected | |
101 | msg "Clearing RED interface" | |
102 | /etc/rc.d/init.d/red clear | |
103 | if [ "$BACKUPPROFILE" != '' ]; then | |
104 | /bin/rm -f /var/ipfire/ppp/settings | |
105 | /bin/cp "/var/ipfire/ppp/settings-$BACKUPPROFILE" /var/ipfire/ppp/settings | |
106 | fi | |
107 | else | |
108 | echo $ATTEMPTS > /var/ipfire/red/keepconnected | |
109 | fi | |
110 | /etc/rc.d/init.d/red start | |
111 | ;; | |
112 | reconnect) | |
113 | while ( /bin/ps ax | /bin/grep -q [p]ppd ); do | |
114 | /bin/sleep 2 | |
115 | done | |
116 | msg "will connect again" | |
117 | /etc/rc.d/init.d/red start | |
118 | ;; | |
119 | *) | |
120 | /bin/echo "Usage: $0 {start|reconnect}" | |
121 | exit 1 | |
122 | ;; | |
123 | esac |