Debian Codegraph @

debian/extra/autopppd

raw
1 #!/usr/bin/perl -w
2
3 # From: Brian May <[email protected]>
4 # To: Debian Bug Tracking System <[email protected]>
5 # Subject: ppp: Increase redial time with each failed PPP connection attempt
6 # Date: Fri, 04 Oct 2002 10:02:50 +1000
7 # Message-Id: <[email protected]>
8 #
9 # Package: ppp
10 # Version: 2.4.1.uus-4
11 # Severity: normal
12 # Tags: patch
13 #
14 # Suggested script to include. It means that each time to
15 # connect is made longer, so that you don't get a huge bill if
16 # for some reason the modem connects but a PPP connection cannot
17 # be established:
18 #
19 # This program depends on libproc-daemon-perl and libproc-waitstat-perl .
20
21 use Proc::Daemon;
22 use strict;
23 use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock
24 use Proc::WaitStat qw(:DEFAULT waitstat);
25
26
27 Proc::Daemon::Init;
28
29 my $pidfile = '/var/run/autopppd.pid';
30 open(PIDFILE, $pidfile);
31 print(PIDFILE "$$\n");
32 close(PIDFILE);
33
34 sub toseconds($) {
35 my ($hours,$minutes,$seconds) = split(/:/,shift);
36 return ($hours*60+$minutes)*60+$seconds;
37 }
38
39 sub dseconds($) {
40 my ($total) = @_;
41
42 my $seconds = $total % 60; $total = ($total - $seconds)/60;
43 my $minutes = $total % 60; $total = ($total - $minutes)/60;
44 my $hours = $total % 24; $total = ($total - $hours)/24;
45 my $days = $total;
46 if ($days > 0) {
47 return(sprintf("%d-%02d:%02d:%02d",$days,$hours,$minutes,$seconds));
48 } else {
49 return(sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds));
50 }
51 }
52
53 my $program="autopppd";
54
55 setlogsock('unix');
56 openlog($program, 'cons,pid', 'daemon');
57
58 my $pppd_start_time;
59 my $pppd_end_time;
60 my $pppd_run_time;
61 my $pppd_fail;
62 my $delay=0;
63 my $idelay=0;
64
65 my @delays = (
66 toseconds("00:01:00"), # 1 minute
67 toseconds("00:07:00"), # 8 minutes
68 toseconds("00:07:00"), # 15 minutes
69 toseconds("00:15:00"), # 30 minutes
70 toseconds("00:30:00"), # 1 hour
71 toseconds("01:00:00"), # 2 hours
72 toseconds("01:00:00"), # 3 hours
73 toseconds("03:00:00"), # 6 hours
74 toseconds("06:00:00"), # 12 hours
75 toseconds("12:00:00"), # 24 hours
76 toseconds("24:00:00") # 48 hours
77 );
78
79 # action == 0 => immediate retry (!FIXME! needs to have some delay)
80 # action == 1 => delayed retry
81 # action == 2 => abort
82
83 my $code = {
84 0 => { message=>"pppd detached", action=> 2 },
85 1 => { message=>"fatal error", action=> 2 },
86 2 => { message=>"options error", action=> 2 },
87 3 => { message=>"not setuid-root error", action=> 2 },
88 4 => { message=>"no kernel support for PPP", action=> 2 },
89 5 => { message=>"SIGINT or SIGTERM or SIGHUP", action=> 1 },
90 6 => { message=>"Serial port locked", action=> 1 }, # should be 0
91 7 => { message=>"Serial port open error", action=> 1 },
92 8 => { message=>"Connect failed", action=> 1 },
93 9 => { message=>"Could not execute pty command", action=> 1 },
94 10 => { message=>"PPP negotiation failed", action=> 1 },
95 11 => { message=>"Peer failed to authenticate", action=> 1 },
96 12 => { message=>"Link was idle", action=> 1 },
97 13 => { message=>"Time limit exceeded", action=> 1 },
98 14 => { message=>"call back not implemented", action=> 2 },
99 15 => { message=>"peer not responding", action=> 1 },
100 16 => { message=>"modem hang up", action=> 1 },
101 17 => { message=>"Serial loopback detected", action=> 1 },
102 18 => { message=>"Init script failed", action=> 1 },
103 19 => { message=>"We failed to authenticate", action=> 1 },
104 };
105
106 while (1)
107 {
108 $pppd_start_time=time;
109 syslog('info', 'restarting pppd');
110
111 # logging sometimes stopped working after ppp was running for
112 # some time. lets see if closing and reopening the log file helps...
113 closelog();
114
115 # run ppp
116 my $rc=system("pppd","-detach",@ARGV);
117
118 # reopon log file
119 openlog($program, 'cons,pid', 'daemon');
120
121 # calculate run time
122 $pppd_end_time=time;
123 $pppd_run_time=$pppd_end_time-$pppd_start_time;
124
125 my $pppd_code = ($? >> 8);
126 my $pppd_signal = $? & 127;
127 my $pppd_coredump = $? & 128;
128
129 $pppd_fail = 1;
130 if ($pppd_signal != 0) {
131 if ($pppd_coredump)
132 { syslog('err',"pppd died with signal $pppd_signal, coredump"); }
133 else
134 { syslog('err',"pppd died with signal $pppd_signal"); }
135 }
136 elsif ($pppd_coredump) {
137 syslog('err',"pppd died with coredump");
138 }
139 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 0) {
140 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), immediate retry");
141 $pppd_fail = 0;
142 }
143 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 1) {
144 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), delayed retry");
145 $pppd_fail = 1;
146 }
147 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
148 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), aborting");
149 exit(255);
150 unlink $pidfile;
151 }
152 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
153 syslog('err', "pppd returned: unknown error ($pppd_code), delayed retry");
154 $pppd_fail = 1;
155 }
156 # if it hasn't ran for at least an hour, then somthing went wrong
157 elsif ($pppd_run_time < toseconds("01:00:00")) {
158 syslog('err',"pppd session didn't last 1 hour, delayed retry");
159 $pppd_fail = 1;
160 }
161 else { $pppd_fail = 0; }
162
163 # if not failed, then reset delay.
164 if (!$pppd_fail) { $idelay = 0; }
165
166 # get next delay.
167 $delay = $delays[$idelay];
168
169 # log statistics.
170 syslog('info',"rc=".waitstat($rc)." runtime=".dseconds($pppd_run_time)." delay[$idelay]=".dseconds($delay)."");
171
172 # delay for desired time.
173 sleep($delay);
174
175 # increment delay for next time.
176 if (defined($delays[$idelay+1])) { $idelay++; }
177 }
178
179 closelog();
180 unlink $pidfile;
181