1#!/usr/bin/perl -w 2 3# Auto dial script by Brian May <bam@snoopy.apana.org.au> 4 5use Proc::Daemon; 6use strict; 7use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock 8use Proc::WaitStat qw(:DEFAULT waitstat); 9 10 11Proc::Daemon::Init; 12open(PIDFILE,">/var/run/autopppd.pid"); 13print(PIDFILE "$$"); 14close(PIDFILE); 15 16sub toseconds($) { 17 my ($hours,$minutes,$seconds) = split(/:/,shift); 18 return ($hours*60+$minutes)*60+$seconds; 19} 20 21sub dseconds($) { 22 my ($total) = @_; 23 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; 27 my $days = $total; 28 if ($days > 0) { 29 return(sprintf("%d-%02d:%02d:%02d",$days,$hours,$minutes,$seconds)); 30 } else { 31 return(sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds)); 32 } 33} 34 35my $program="autopppd"; 36 37setlogsock('unix'); 38openlog($program, 'cons,pid', 'daemon'); 39 40my $pppd_start_time; 41my $pppd_end_time; 42my $pppd_run_time; 43my $pppd_fail; 44my $delay=0; 45my $idelay=0; 46 47my @delays = ( 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 59 ); 60 61# action == 0 => immediate retry (!FIXME! needs to have some delay) 62# action == 1 => delayed retry 63# action == 2 => abort 64 65my $code = { 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 }, 86}; 87 88while (1) 89{ 90 $pppd_start_time=time; 91 syslog('info', 'restarting pppd'); 92 93 # logging sometimes stopped working after ppp was running for 94 # some time. lets see if closing and reopening the log file helps... 95 closelog(); 96 97 # run ppp 98 my $rc=system("pppd","-detach",@ARGV); 99 100 # reopon log file 101 openlog($program, 'cons,pid', 'daemon'); 102 103 # calculate run time 104 $pppd_end_time=time; 105 $pppd_run_time=$pppd_end_time-$pppd_start_time; 106 107 my $pppd_code = ($? >> 8); 108 my $pppd_signal = $? & 127; 109 my $pppd_coredump = $? & 128; 110 111 $pppd_fail = 1; 112 if ($pppd_signal != 0) { 113 if ($pppd_coredump) 114 { syslog('err',"pppd died with signal $pppd_signal, coredump"); } 115 else 116 { syslog('err',"pppd died with signal $pppd_signal"); } 117 } 118 elsif ($pppd_coredump) { 119 syslog('err',"pppd died with coredump"); 120 } 121 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 0) { 122 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), immediate retry"); 123 $pppd_fail = 0; 124 } 125 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 1) { 126 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), delayed retry"); 127 $pppd_fail = 1; 128 } 129 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) { 130 syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), aborting"); 131 exit(255); 132 } 133 elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) { 134 syslog('err', "pppd returned: unknown error ($pppd_code), delayed retry"); 135 $pppd_fail = 1; 136 } 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"); 140 $pppd_fail = 1; 141 } 142 else { $pppd_fail = 0; } 143 144 # if not failed, then reset delay. 145 if (!$pppd_fail) { $idelay = 0; } 146 147 # get next delay. 148 $delay = $delays[$idelay]; 149 150 # log statistics. 151 syslog('info',"rc=".waitstat($rc)." runtime=".dseconds($pppd_run_time)." delay[$idelay]=".dseconds($delay).""); 152 153 # delay for desired time. 154 sleep($delay); 155 156 # increment delay for next time. 157 if (defined($delays[$idelay+1])) { $idelay++; } 158} 159 160closelog(); 161