lastlog_daemon.in 3.82 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# Copyright (c) 2002-2006 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
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
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;
50 51
use POSIX; # for sigprocmask and friends
use Fcntl; # file constants for pidfile
52 53 54

my $genlastlog  = "$TB/sbin/genlastlog";
my $logfile     = "$TB/log/genlastlog";
55
my $debug       = 0;
56

57
$| = 1; # Turn off line buffering on output
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

# 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");
}
81 82

# Go to background.
83 84 85 86 87 88
if (! $debug) {
    if (TBBackGround($logfile)) {
	exit(0);
    }
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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";

112 113
# And just loop.
while (1) {
114
    print "Running at " . `date`;
115 116
    
    if (system("$genlastlog")) {
117 118 119 120
	# Save status across the date call in print statement.
	my $exitval = $?;
	
	print "Exited with status $exitval at " . `date`;
121

122
	SENDMAIL($TBOPS, "Genlastlog Failed!",
123
		 "Failed with exit value $exitval\n".
124 125
		 "Please look at the syslog entries for genlastlog!");
    }
126
    sleep(60 * 5);
127 128 129 130
}

exit(0);

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
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);
}