#!/usr/contrib/bin/perl

#
# inetcis.cleanup
#
# SSR Version 2.0 - 08Mar96 (bchatfield) Added xpad restarting code
# SSR Version 1.0 - unknown (bchatfield) Original version
#

require "getopts.pl";
require "syslog.pl";

&Getopts ('ak');

if ($opt_a) {
    $auto = 1;
}

if ($opt_k) {
    $no_kill = 1;
}

#
# Initialize $running_xpads associative array.
#
foreach $x (split(//,"pqrstuvw")) {
    foreach $y (split(//,"0123456789abcdef")) {
	$running_xpads{"$x$y"} = "no";
#	print "$x$y:" . $running_xpads{"$x$y"} . "\n";
    }
}

$process_name = "telnetd (inetcis.telnetd)";
$found_some = 0;

###############################################################################
#                            Begin Main Loop                                  #
###############################################################################

open (PS, "ps -axo pid,start,time,command|");
$heading = <PS>;
while ($line = <PS>) {
    chop ($line);
    ($pid, $start, $time, $minutes, $seconds, $hundredths, $command) =
	($line =~ /(\d+)\s+([a-zA-Z0-9:]+)\s+((\d+):(\d+)\.(\d+))\s+(.*$)/);

    if ($command eq $process_name) {
	if ($start =~ /[A-Za-z]{3,3}\d\d[AP]M|\d{1,2}[A-Za-z]{3,3}\d\d/) {
	    if ($minutes == 0 && $seconds == 0) {
                $found_some = 1;
                $line = $pid . " " . $start . " " . $time . " " . $command;
		if ($no_kill) {
                    print $line . "\n";
                } elsif ($auto) {
		    print "killing process $line\n";
		    kill ('TERM', $pid) || print "$!\n";
		} else {
		    $response_is_good = 1;
		    do {
			print "Kill process \"$line\" (y n a q) ? ";
			$response = <STDIN>;
			chop $response;
			if ($response eq "y") {
			    print "killing process $pid\n";
			    kill ('TERM', $pid) || print "$!\n";
			} elsif ($response eq "n") {
			    print "skipping process $pid\n";
			} elsif ($response eq "a") {
			    $auto = 1;
			    print "killing process $pid\n";
			    kill ('TERM', $pid) || print "$!\n";
			} elsif ($response eq "q") {
			    die "quitting\n"; # 
			} else {
			    $response_is_good = 0;
			}
		    } until ($response_is_good);
		}
	    }
	}
    }

    #
    # Record running xpads.
    #
    if (($path, $xpad_word, $ppad_word, $identifier)
	= ($command =~ /^(.*)(xpad).*(ppad)(\w\w)/)) {
#	print "$path $xpad_word $ppad_word $identifier\n";
	if ($path eq "/usr/local/adm/daemon/" || $path eq "./"
	    || $path eq "") {
	    $running_xpads{$identifier} = "yes";
	}
    }
}

###############################################################################
#                              End Main Loop                                  #
###############################################################################

if (! $found_some) {
    print "There are no $process_name processes to clean up.\n";
}

&openlog('inetcis.cleanup','ndelay','user');

#
# Restart xpads that are not running.  This should probably prompt for
# conformation before restarting xpads.  It could restart the ones that aren't
# running automatically with the -a switch.  This is how killing
# processes works in the above loop.
#
foreach $x (split(//,"pqrstuvw")) {
    foreach $y (split(//,"0123456789abcdef")) {
	$ppad_file = "/dev/ppad" . $x . $y;
	if ($running_xpads{"$x$y"} eq "no") {
	    if (-c $ppad_file) {
	        system "/usr/local/adm/daemon/xpad", "-x", "-p", "$ppad_file";
		# Check to see if this actually worked.  Print error if not.
		if ($? == 0) {
                    # Restarting the xpad worked.
		    print "Restarted xpad for $ppad_file.\n";
		    &syslog('notice',"Restarted xpad for $ppad_file");
		} else {
		    # Restarting the xpad didn't work.
		    print "Restart of xpad for $ppad_file failed.\n";
		    &syslog('notice',"Restart of xpad for $ppad_file failed");
		}
	    } else {
		print "$ppad_file does not exist.\n";
		&syslog('notice',"$ppad_file does not exist");
	    }
	}
    }
}

&closelog();

#
# Check, store, and report core files.
#
