autopppd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/perl -w
  2. # Auto dial script by Brian May <bam@snoopy.apana.org.au>
  3. use Proc::Daemon;
  4. use strict;
  5. use Sys::Syslog qw(:DEFAULT setlogsock); # default set, plus setlogsock
  6. use Proc::WaitStat qw(:DEFAULT waitstat);
  7. Proc::Daemon::Init;
  8. open(PIDFILE,">/var/run/autopppd.pid");
  9. print(PIDFILE "$$");
  10. close(PIDFILE);
  11. sub toseconds($) {
  12. my ($hours,$minutes,$seconds) = split(/:/,shift);
  13. return ($hours*60+$minutes)*60+$seconds;
  14. }
  15. sub dseconds($) {
  16. my ($total) = @_;
  17. my $seconds = $total % 60; $total = ($total - $seconds)/60;
  18. my $minutes = $total % 60; $total = ($total - $minutes)/60;
  19. my $hours = $total % 24; $total = ($total - $hours)/24;
  20. my $days = $total;
  21. if ($days > 0) {
  22. return(sprintf("%d-%02d:%02d:%02d",$days,$hours,$minutes,$seconds));
  23. } else {
  24. return(sprintf("%02d:%02d:%02d",$hours,$minutes,$seconds));
  25. }
  26. }
  27. my $program="autopppd";
  28. setlogsock('unix');
  29. openlog($program, 'cons,pid', 'daemon');
  30. my $pppd_start_time;
  31. my $pppd_end_time;
  32. my $pppd_run_time;
  33. my $pppd_fail;
  34. my $delay=0;
  35. my $idelay=0;
  36. my @delays = (
  37. toseconds("00:01:00"), # 1 minute
  38. toseconds("00:07:00"), # 8 minutes
  39. toseconds("00:07:00"), # 15 minutes
  40. toseconds("00:15:00"), # 30 minutes
  41. toseconds("00:30:00"), # 1 hour
  42. toseconds("01:00:00"), # 2 hours
  43. toseconds("01:00:00"), # 3 hours
  44. toseconds("03:00:00"), # 6 hours
  45. toseconds("06:00:00"), # 12 hours
  46. toseconds("12:00:00"), # 24 hours
  47. toseconds("24:00:00") # 48 hours
  48. );
  49. # action == 0 => immediate retry (!FIXME! needs to have some delay)
  50. # action == 1 => delayed retry
  51. # action == 2 => abort
  52. my $code = {
  53. 0 => { message=>"pppd detached", action=> 2 },
  54. 1 => { message=>"fatal error", action=> 2 },
  55. 2 => { message=>"options error", action=> 2 },
  56. 3 => { message=>"not setuid-root error", action=> 2 },
  57. 4 => { message=>"no kernel support for PPP", action=> 2 },
  58. 5 => { message=>"SIGINT or SIGTERM or SIGHUP", action=> 1 },
  59. 6 => { message=>"Serial port locked", action=> 1 }, # should be 0
  60. 7 => { message=>"Serial port open error", action=> 1 },
  61. 8 => { message=>"Connect failed", action=> 1 },
  62. 9 => { message=>"Could not execute pty command", action=> 1 },
  63. 10 => { message=>"PPP negotiation failed", action=> 1 },
  64. 11 => { message=>"Peer failed to authenticate", action=> 1 },
  65. 12 => { message=>"Link was idle", action=> 1 },
  66. 13 => { message=>"Time limit exceeded", action=> 1 },
  67. 14 => { message=>"call back not implemented", action=> 2 },
  68. 15 => { message=>"peer not responding", action=> 1 },
  69. 16 => { message=>"modem hang up", action=> 1 },
  70. 17 => { message=>"Serial loopback detected", action=> 1 },
  71. 18 => { message=>"Init script failed", action=> 1 },
  72. 19 => { message=>"We failed to authenticate", action=> 1 },
  73. };
  74. while (1)
  75. {
  76. $pppd_start_time=time;
  77. syslog('info', 'restarting pppd');
  78. # logging sometimes stopped working after ppp was running for
  79. # some time. lets see if closing and reopening the log file helps...
  80. closelog();
  81. # run ppp
  82. my $rc=system("pppd","-detach",@ARGV);
  83. # reopon log file
  84. openlog($program, 'cons,pid', 'daemon');
  85. # calculate run time
  86. $pppd_end_time=time;
  87. $pppd_run_time=$pppd_end_time-$pppd_start_time;
  88. my $pppd_code = ($? >> 8);
  89. my $pppd_signal = $? & 127;
  90. my $pppd_coredump = $? & 128;
  91. $pppd_fail = 1;
  92. if ($pppd_signal != 0) {
  93. if ($pppd_coredump)
  94. { syslog('err',"pppd died with signal $pppd_signal, coredump"); }
  95. else
  96. { syslog('err',"pppd died with signal $pppd_signal"); }
  97. }
  98. elsif ($pppd_coredump) {
  99. syslog('err',"pppd died with coredump");
  100. }
  101. elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 0) {
  102. syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), immediate retry");
  103. $pppd_fail = 0;
  104. }
  105. elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} == 1) {
  106. syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), delayed retry");
  107. $pppd_fail = 1;
  108. }
  109. elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
  110. syslog('err', "pppd returned: ".$code->{$pppd_code}{"message"}." ($pppd_code), aborting");
  111. exit(255);
  112. }
  113. elsif (defined($code->{$pppd_code}) && $code->{$pppd_code}{"action"} >= 2) {
  114. syslog('err', "pppd returned: unknown error ($pppd_code), delayed retry");
  115. $pppd_fail = 1;
  116. }
  117. # if it hasn't ran for at least an hour, then somthing went wrong
  118. elsif ($pppd_run_time < toseconds("01:00:00")) {
  119. syslog('err',"pppd session didn't last 1 hour, delayed retry");
  120. $pppd_fail = 1;
  121. }
  122. else { $pppd_fail = 0; }
  123. # if not failed, then reset delay.
  124. if (!$pppd_fail) { $idelay = 0; }
  125. # get next delay.
  126. $delay = $delays[$idelay];
  127. # log statistics.
  128. syslog('info',"rc=".waitstat($rc)." runtime=".dseconds($pppd_run_time)." delay[$idelay]=".dseconds($delay)."");
  129. # delay for desired time.
  130. sleep($delay);
  131. # increment delay for next time.
  132. if (defined($delays[$idelay+1])) { $idelay++; }
  133. }
  134. closelog();