head 1.2; access; symbols; locks; strict; comment @# @; 1.2 date 2001.05.09.11.08.52; author ahuxley; state Exp; branches; next 1.1; 1.1 date 2001.02.04.15.23.41; author alexis; state Exp; branches; next ; desc @SCRIPT TO SEND SIGHUP TO XRINGD WITH PPP EXITS @ 1.2 log @converted to 'active' format added usage function added comment blocks various other fixes - undocumented @ text @#!MARKER_PERL_CMD #shpp include ../bldcfg/paths.shpp require 5; use strict; ($main::progname = $0) =~ s/^.*\/([^\/]+)/$1/; $main::rcs_id = '$Header: /diskb/home/alexis/dev/active/small/bin/RCS/reset-xringd-after-ppp.shpp,v 1.1 2001/02/04 15:23:41 alexis Exp alexis $'; $main::version_scheme = 'rcs'; ############################################################################## # # RESET XRINGD WHEN PPPD EXITS # # According to the xringd(8) manual page: # # pppd (and probably some other programs) like to hold a tty # in exclusive mode. Make sure you start xringd before such # programs, otherwise it won't be allowed to open the modem # device. Also, when such a program closes it may leave the # line hung up. You need to restart (kill -HUP) xringd in # such a case. # # This script does exactly that. There are two ways pppd can exit, one # of them involving no calls to the ip-up and ip-down scripts. Since there # is the possibility that the xringd needs resetting without the connection # ever having come up or down, in the worst possible case we need to # constantly monitor pppd to see if it exits, and if it does reset xringd. # This isn't terribly efficient but the time between pppd starting and # it exiting in such a case is probably less that 60 seconds, so we'll # just endure this inefficiency. For the case where the connection does # come up, then this program can stop its regular health checks on pppd and # simply await the connection coming down in a more efficient way. So ... # # This program needs to be called three times: # # It needs to be started as a server from the connection chat scripts # where it will await pppd exiting or pppd calling ip-up. In the # former case it resets xringd and exits. In the latter it goes to # sleep awaiting a signal from ip-down. When that signal comes (which it # is certain to do) it resets xringd and exits. # # It needs to be called from ip-up.d/ in order to tell the # server process that it can stop its regular checks and go to sleep # pending a signal from ip-down. # # It needs to be called from ip-down.d/ in order to tell # the server process to reset xringd and exit. (Actually, this call # could have take the responsiblity to reset xringd, in which case # the server could exit as soon as ip-up got called.) # ############################################################################## ############################################################################## # # CONFIGURABLE STUFF STARTS HERE # ############################################################################## # How often should this program (when run in server mode) check to see if # pppd is 'up or has exited failing to connect'? $main::pppd_exit_with_error_checking_period = 5; ############################################################################## # # CONFIGURABLE STUFF ENDS HERE # ############################################################################## sub main { my(@@ARGV) = @@_; $main::mode = "client" ;; $main::i_am_a_daemon = 1; while (defined($ARGV[0]) && $ARGV[0] =~ /^-/) { $_ = shift @@ARGV; if (/^-d(.*)/) { $main::verboselevel = ($1 ? $1 : shift @@ARGV); } elsif (/^-v$/) { $main::verboselevel = 3; } elsif (/^-s$/) { $main::mode = "server"; } elsif (/^-V$/) { print "$main::progname version ", $main::version, "\n"; exit 0; } else { &usage; } } if ($main::mode eq "client") { &client_main; } elsif (fork == 0) { &server_main; } } sub usage { print STDERR "Usage: $main::progname [ -v ] [ -s ]\n"; print STDERR " $main::progname -V\n"; exit 1; } sub lockfile_name_generator { return "/tmp/$main::progname.lock"; } sub client_main { my($locking_pid); $SIG{'INT'} = \&genericsighandler; $SIG{'TERM'} = \&genericsighandler; if (($locking_pid=&lock("", \&lockfile_name_generator)) == 0) { error("$main::mode: server does not appear to be running"); } &debug(5, "client_main: waking up server (pid=$locking_pid) ..."); kill 15, $locking_pid; } sub server_main { # We use lock files, so ensure their cleanup $SIG{'INT'} = \&genericsighandler; # Since this script forks and its parent exits, we must ensure survival # when all the process group get sent SIGHUP. $SIG{'HUP'} = 'IGNORE'; &lock("", \&lockfile_name_generator) && &error("lock failed"); # Now await ip-up sending us a signal, or pppd exiting. &mysleep($main::pppd_exit_with_error_checking_period, \&ppp_running); # If ip-up ran and send us a sig, then pppd will still be running, and # in that case it is certain ip-down will run when the connection comes # down, so we can go to total sleep until ip-down sends us a sig. if (&ppp_running) { &debug(5, "server_main: pppd still running, assuming connection up"); # Note the anonymous subroutine which, every 3600, will tell mysleep # to do another sleep (i.e. no real check to run). &mysleep(3600, sub { return(1) }); } # Thus we would only get to here when ip-down sends us a signal, which # will break it out of the above mysleep(). And if ip-down is running # then it's time to reset xringd. &debug(5, "server_main: before reset_xringd()"); &reset_xringd; &unlock("", \&lockfile_name_generator); } sub ppp_running { return(`ps ax | grep pppd | grep -v grep`); } sub reset_xringd { my($rc); # give ppp a chance to exit sleep 3; $rc = system("killall -HUP xringd") >> 8; &debug(5, "reset_xringd: killall returned $rc"); } ############################################################################## # # Function: mysleep() # # Description: this function calls a checking function at a specified # interval until (i) the checking function returns zero, or # (ii) it recieves a SIGTERM. # # Parameters: period between checks # checking function # # Returns: not specified (it is up to the called to call the checking # function again to determine what happened). # ############################################################################## sub mysleep { my($period, $callbackfnc) = @@_; $main::keep_sleeping = 1; $SIG{TERM} = sub { $main::keep_sleeping = 0 }; while (&$callbackfnc && $main::keep_sleeping) { sleep $period; } $SIG{TERM} = 'DEFAULT'; } #shpp include ../lib/utils.pl.shpp #shpp include ../lib/gep.pl.shpp @ 1.1 log @Initial revision @ text @d2 1 a4 4 $main::rcs_id = '$Header: /home/ahuxley/dev/supported/fad/bin/RCS/mkfad.shpp,v 1.38 2000/04/05 10:56:31 ahuxley Exp $'; $main::version_scheme = 'release'; # Important bits d6 2 d9 42 a50 6 sub usage { print STDERR "Usage: $main::progname [ -v ] [ -s ]\n"; print STDERR " $main::progname -V\n"; exit 1; } d52 16 d98 7 d127 1 d129 3 d136 6 a141 1 &mysleep(2, \&ppp_running); d144 2 d148 3 d160 1 a160 3 my($ppp_is_running); $ppp_is_running = `ps ax | grep pppd | grep -v grep`; return($ppp_is_running); d174 16 d202 2 a203 2 #shpp include utils.pl.shpp #shpp include gep.pl.shpp @