1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
#!/usr/bin/perl
#
# Check to see if modems on the terminal server are still answering the phone
#
# This script should be run using a setgid uucp wrapper which then
# invokes this script in order to gain permissions to do UUCP-style
# modem locking.
#
# Jim Trocki
#
# $Id: dialin.monitor,v 1.1.1.1 2004/06/09 05:18:05 trockij Exp $
#
use Getopt::Std;
use Expect;
use POSIX;
use English;
use Sys::Syslog;
sub hangup_ath;
sub hangup_dtr;
sub lock_modem;
sub unlock_modem;
sub attempt_dialin;
Sys::Syslog::setlogsock ('unix');
openlog ("dialin.monitor", "cons,pid", "daemon");
getopts ('dn:D:l:r:t:', \%opt);
die "not running as GID uucp, are you using the setgid wrapper?\n"
unless ($opt{"d"} || $EGID == getgrnam ("uucp"));
$NUMBER = $opt{"n"} || die "specify dial number with -n\n";
$DIAL_TIMEOUT = $opt{"t"} || 60;
$DEVICE = $opt{"D"} || "/dev/modem";
$LOCKDIR = $opt{"l"} || "/var/lock";
$RETRIES = $opt{"r"} || 2;
$SIG{"TERM"} = \&unlock_modem_and_die;
$SIG{"INT"} = \&unlock_modem_and_die;
my $no = 0;
my $yes = 0;
my @detail = ();
for (my $retr = 0; $retr < $RETRIES; $retr++) {
my $r = attempt_dialin;
if ($r eq "OK") {
$yes++;
last;
}
$no++;
push @detail, $r;
sleep 5;
}
exit 0 if ($yes);
print "failure for $NUMBER after $RETRIES retries\n", join ("\n", @detail), "\n";
exit 1;
#
# make an attempt to dial in
# returns "OK" upon success, and an error string upon failure
#
sub attempt_dialin {
my $got_lock = 0;
for (my $tries=0; $tries < 6; $tries++) {
my $r = lock_modem;
if ($r eq "ok") {
$got_lock = 1;
last;
}
if ($opt{"d"}) { print STDERR "problem getting lock: $!\n" }
sleep 10;
}
if ($opt{"d"}) { print STDERR "got_lock=$got_lock\n" }
if (!$got_lock) {
return "could not get lock";
}
if ((my $r = hangup_dtr) != 1) {
unlock_modem;
return "could not drop DTR: $r";
}
if (!open (MODEM, "+<$DEVICE")) {
return "could not open $DEVICE: $!";
}
select (MODEM); $| = 1; select (STDOUT);
my $s = Expect->exp_init (\*MODEM);
if (!$s) {
unlock_modem;
return "could not init expect";
}
$s->exp_stty ("9600", "cs8", "-clocal", "crtscts", "-parenb", "-cstopb");
#
# Reset modem
#
my $err = "";
for (my $n =0; $n < 2; $n++) {
if ($opt{"d"}) { print STDERR "resetting modem\n" }
print $s "ATZ\r";
($matched_pattern_position, $error, $successfully_matching_string,
$before_match,$after_match) = $s->expect (5, "OK");
if ($error eq "1:TIMEOUT") {
$err = "timeout waiting for OK prompt";
next;
} elsif (!defined $matched_pattern_position) {
$err = "no pattern matched waiting for OK prompt";
next;
}
$err = "";
$s->clear_accum;
last;
}
if ($err ne "") {
unlock_modem;
return $err;
}
#
# dial phone number and wait for connect
#
if ($opt{"d"}) { print STDERR "dialing and waiting for carrier\n" }
print $s "ATDT$NUMBER\r";
($matched_pattern_position, $error, $successfully_matching_string,
$before_match,$after_match) =
$s->expect ($DIAL_TIMEOUT, "CONNECT", "BUSY", "NO CARRIER", "NO DIALTONE");
if ($error eq "1:TIMEOUT") {
unlock_modem;
return "timeout waiting for connection [$before_match] [$after_match]";
} elsif (!defined $matched_pattern_position) {
unlock_modem;
return "no pattern matched waiting for connection";
} elsif ($matched_pattern_position != 1) {
unlock_modem;
return "no connection: $successfully_matching_string";
}
$s->clear_accum;
#
# once connected, wait for login prompt
#
if ($opt{"d"}) { print STDERR "got connect, waiting for login prompt\n" }
($matched_pattern_position, $error, $successfully_matching_string,
$before_match,$after_match) =
$s->expect (30, "login:");
if ($error eq "1:TIMEOUT") {
close (MODEM);
hangup_dtr;
unlock_modem;
return "connection but timeout on prompt";
} elsif (!defined $matched_pattern_position) {
close (MODEM);
hangup_dtr;
unlock_modem;
return "no login prompt";
}
$s->clear_accum;
#
# got login prompt, hang up phone
#
if ($opt{"d"}) { print STDERR "all OK, hanging up\n" }
close (MODEM);
if (($r = hangup_dtr) != 1) {
unlock_modem;
return "could not toggle DTR: $r";
}
unlock_modem;
return "OK";
}
#
# this quite possibly only works on Linux, or at least that's
# where I stole the TIOCM_DTR and TIOCMBIC values from
#
sub hangup_dtr {
if ($opt{"d"}) { print STDERR "\tdropping DTR\n" }
open (M, "+<$DEVICE") || return "could not open device: $!";
my $TIOCM_DTR = pack ("i", 0x02);
my $TIOCMBIC = 0x5417;
if (!ioctl (M, $TIOCMBIC, $TIOCM_DTR)) {
return "could not do ioctl: $!";
}
sleep 5;
close (M);
return 1;
}
sub hangup_ath {
my $i, $ok;
my $matched_pattern_position, $error, $successfully_matching_string,
$before_match, $after_match;
if ($opt{"d"}) { print STDERR "\thanging up via ATH\n" }
$ok = 0;
PLUS_LOOP:
for ($i=0; $i<3; $i++) {
print $s "+++";
($matched_pattern_position, $error, $successfully_matching_string,
$before_match, $after_match) =
$s->expect (4, "OK");
if (defined $matched_pattern_position) {
$ok = 1;
last PLUS_LOOP;
}
}
return undef unless ($ok);
print $s "ATH\r";
($matched_pattern_position, $error, $successfully_matching_string,
$before_match, $after_match) =
$s->expect (5, "OK");
return 1 if ($matched_pattern_position == 1);
return undef;
}
sub lock_modem {
my $dev, $pid;
my $lockfile, $tmpfile;
if ($opt{"d"}) { print STDERR "\tlocking modem\n" }
($dev) = $DEVICE =~ /\/([^\/]*)$/;
return "device unparseable" unless ($dev ne "");
$lockfile = "$LOCKDIR/LCK..$dev";
$tmpfile = "$LOCKDIR/TMPLCK..$$";
open (O, ">$tmpfile") || return "could not open tmp lock: $!";
print O "$$\n";
close (O);
if (!link ($tmpfile, $lockfile)) {
if ($! == EEXIST) {
if (!open (I, $lockfile)) {
unlink ($tmpfile);
return "could not open existing lock file: $!";
}
$pid = <I>;
close (I);
my ($npid) = $pid =~ /(\d+)/;
my $v = kill 0, $npid;
if ($v == 0 && $! != ESRCH) {
unlink ($tmpfile);
return "lock already exists";
}
if ($opt{"d"}) { print STDERR "lock from dead process exists\n" }
if (!unlink ($lockfile)) {
unlink ($tmpfile);
if ($opt{"d"}) { print STDERR "lock from dead process exists, could not unlink stale lock\n" }
return "could not unlink stale lock file: $!";
}
if (!link ($tmpfile, $lockfile)) {
unlink ($tmpfile);
if ($opt{"d"}) { print STDERR "lock from dead process exists, could create lock\n" }
return "could not create lock file: $!";
}
} else {
return "cannot create lock file: $!";
}
}
unlink ($tmpfile) || return "could not unlink temp lock file: $!";
if ($opt{"d"}) { print STDERR "lock is OK\n" }
return "ok";
# create a temp file in /var/lock and put PID in it
# make a hard link to it, check return value
# if errno = EEXIST then
# open and read existing lock file
# check if the proc exists via kill -0
# if it does exist, unlink the tmpfile and return failure
# else unlink lockfile
# link tmpfile to lockfile
# unlink tmpfile
# return OK
#
}
sub unlock_modem {
my $dev;
if ($opt{"d"}) { print STDERR "\tunlocking modem\n" }
($dev) = $DEVICE =~ /\/([^\/]*)$/;
return "device unparseable" unless ($dev ne "");
unlink ("$LOCKDIR/LCK..$dev") || return "could not unlink lock file: $!";
return 1;
}
sub unlock_modem_and_die {
unlock_modem;
die;
}
|