3 # Auto dial script by Brian May <bam@snoopy.apana.org.au>
7 use Sys
::Syslog
qw(:DEFAULT setlogsock); # default set, plus setlogsock
8 use Proc
::WaitStat
qw(:DEFAULT waitstat);
12 open(PIDFILE
,">/var/run/autopppd.pid");
17 my ($hours,$minutes,$seconds) = split(/:/,shift);
18 return ($hours*60+$minutes)*60+$seconds;
24 my $seconds = $total % 60; $total = ($total - $seconds)/60;
25 my $minutes = $total % 60; $total = ($total - $minutes)/60;
26 my $hours = $total % 24; $total = ($total - $hours)/24;
29 return(sprintf("%d-%02d:%02d:%02d",$days,$hours,$minutes,$seconds));
31 return(sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds));
35 my $program="autopppd";
38 openlog
($program, 'cons,pid', 'daemon');
48 toseconds
("00:01:00"), # 1 minute
49 toseconds
("00:07:00"), # 8 minutes
50 toseconds
("00:07:00"), # 15 minutes
51 toseconds
("00:15:00"), # 30 minutes
52 toseconds
("00:30:00"), # 1 hour
53 toseconds
("01:00:00"), # 2 hours
54 toseconds
("01:00:00"), # 3 hours
55 toseconds
("03:00:00"), # 6 hours
56 toseconds
("06:00:00"), # 12 hours
57 toseconds
("12:00:00"), # 24 hours
58 toseconds
("24:00:00") # 48 hours
61 # action == 0 => immediate retry (!FIXME! needs to have some delay)
62 # action == 1 => delayed retry
63 # action == 2 => abort
66 0 => { message
=>"pppd detached", action
=> 2 },
67 1 => { message
=>"fatal error", action
=> 2 },
68 2 => { message
=>"options error", action
=> 2 },
69 3 => { message
=>"not setuid-root error", action
=> 2 },
70 4 => { message
=>"no kernel support for PPP", action
=> 2 },
71 5 => { message
=>"SIGINT or SIGTERM or SIGHUP", action
=> 1 },
72 6 => { message
=>"Serial port locked", action
=> 1 }, # should be 0
73 7 => { message
=>"Serial port open error", action
=> 1 },
74 8 => { message
=>"Connect failed", action
=> 1 },
75 9 => { message
=>"Could not execute pty command", action
=> 1 },
76 10 => { message
=>"PPP negotiation failed", action
=> 1 },
77 11 => { message
=>"Peer failed to authenticate", action
=> 1 },
78 12 => { message
=>"Link was idle", action
=> 1 },
79 13 => { message
=>"Time limit exceeded", action
=> 1 },
80 14 => { message
=>"call back not implemented", action
=> 2 },
81 15 => { message
=>"peer not responding", action
=> 1 },
82 16 => { message
=>"modem hang up", action
=> 1 },
83 17 => { message
=>"Serial loopback detected", action
=> 1 },
84 18 => { message
=>"Init script failed", action
=> 1 },
85 19 => { message
=>"We failed to authenticate", action
=> 1 },
90 $pppd_start_time=time;
91 syslog
('info', 'restarting pppd');
93 # logging sometimes stopped working after ppp was running for
94 # some time. lets see if closing and reopening the log file helps...
98 my $rc=system("pppd","-detach",@ARGV);
101 openlog
($program, 'cons,pid', 'daemon');
105 $pppd_run_time=$pppd_end_time-$pppd_start_time;
107 my $pppd_code = ($?
>> 8);
108 my $pppd_signal = $?
& 127;
109 my $pppd_coredump = $?
& 128;
112 if ($pppd_signal != 0) {
114 { syslog
('err',"pppd died with signal $pppd_signal, coredump"); }
116 { syslog
('err',"pppd died with signal $pppd_signal"); }
118 elsif ($pppd_coredump) {
119 syslog
('err',"pppd died with coredump");
121 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 0) {
122 syslog
('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), immediate retry");
125 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 1) {
126 syslog
('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), delayed retry");
129 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
130 syslog
('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), aborting");
133 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
134 syslog
('err', "pppd returned: unknown error ($pppd_code), delayed retry");
137 # if it hasn't ran for at least an hour, then somthing went wrong
138 elsif ($pppd_run_time < toseconds
("01:00:00")) {
139 syslog
('err',"pppd session didn't last 1 hour, delayed retry");
142 else { $pppd_fail = 0; }
144 # if not failed, then reset delay.
145 if (!$pppd_fail) { $idelay = 0; }
148 $delay = $delays[$idelay];
151 syslog
('info',"rc=".waitstat
($rc)." runtime=".dseconds
($pppd_run_time)." delay[$idelay]=".dseconds
($delay)."");
153 # delay for desired time.
156 # increment delay for next time.
157 if (defined($delays[$idelay+1])) { $idelay++; }