lastlog_daemon.in 2.94 KB
Newer Older
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
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
# Generate lastlog info from syslog data on users node.
#
#	usage: lastlog_daemon [-d]
#
# TODO: Use "logger" instead of writing a log file.
#
sub usage()
{
    print STDOUT "Usage: lastlog_daemon [-d]\n" .
	"Use the -d option to prevent daemonization\n";
    exit(-1);
}
my  $optlist = "d";

# Configure variables
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";

# Testbed Support library
use lib "@prefix@/lib";
use libdb;
use libtestbed;
28 29
use POSIX; # for sigprocmask and friends
use Fcntl; # file constants for pidfile
30 31 32

my $genlastlog  = "$TB/sbin/genlastlog";
my $logfile     = "$TB/log/genlastlog";
33
my $debug       = 0;
34

35
$| = 1; # Turn off line buffering on output
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

# Untaint the path
$ENV{'PATH'} = "/bin:/usr/bin:";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 0) {
    usage();
}
if (defined($options{"d"})) {
    $debug = $options{"d"};
}

# Only root and TB Admins.
if ($UID && !TBAdmin($UID)) {
    die("*** $0:\n".
	"    You must be root or a TB administrator\n");
}
59 60

# Go to background.
61 62 63 64 65 66
if (! $debug) {
    if (TBBackGround($logfile)) {
	exit(0);
    }
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
my $str=$TB;
$str =~ s/\/usr\/testbed//g;
$str =~ s/\//\./g;
my $pidfile = ($TB eq "/usr/testbed" ? 
	       "/var/run/lastlog_daemon.pid" :
	       "/var/run/lastlog_daemon$str.pid" );
sysopen(PIDFILE, $pidfile, O_WRONLY | O_EXCL | O_CREAT) || 
  die ("Couldn't create '$pidfile': $? $!\n");
print PIDFILE "$$";
close PIDFILE;

$SIG{HUP} = \&restart;

$SIG{USR1} = \&cleanup;
$SIG{USR2} = \&cleanup;
$SIG{INT}  = \&cleanup;
$SIG{QUIT} = \&cleanup;
$SIG{ABRT} = \&cleanup;
$SIG{TERM} = \&cleanup;
$SIG{KILL} = \&cleanup;

print "lastlog_daemon starting, pid $$\n";

90 91
# And just loop.
while (1) {
92
    print "Running at " . `date`;
93 94
    
    if (system("$genlastlog")) {
95
	print "Exited with status $? at " . `date`;
96

97
	SENDMAIL($TBOPS, "Genlastlog Failed!",
98
		 "Failed with exit value $?\n".
99 100
		 "Please look at the syslog entries for genlastlog!");
    }
101
    sleep(60 * 5);
102 103 104 105
}

exit(0);

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
sub restart() {
  my $prog = "";
  # If we're started from an abosolute path, use that.
  if ($0 =~ /^(\/.*)$/) { $prog = $1; }
  else { $prog = "$TB/sbin/lastlog_daemon"; }
  print "SIGHUP received, restarting from '$prog'\n";
  unlink $pidfile;
  if (!defined(sigprocmask(SIG_UNBLOCK, POSIX::SigSet->new(SIGHUP)))) {
    $msg = "lastlog_daemon couldn't restart\n".
	     "sigprocmask: sig unblock failed! $?, $!\n";
    SENDMAIL($TBOPS,"lastlog_daemon couldn't restart",$msg);
    die("$msg\n");
  }
  exec("$prog") or 
    do {
      my $msg = "Couldn't restart lastlog_daemon! cmd='$prog'\n".
	"Error: ($?) $!\n";
      SENDMAIL($TBOPS, "lastlog_daemon couldn't restart",$msg);
      die($msg);
    };
}

sub cleanup() {
  print "Signal received, exiting.\n";
  unlink $pidfile;
  exit(0);
}