#!/usr/bin/perl

#
# eTrust updater. Original code by Julian Field. Timeout code by
# Alessandro Bianchi. Timeout code is not perfect but should be okay.
#

use Sys::Syslog;

$PackageDir = shift || "/opt/eTrustAntivirus";

$LockFile = "/tmp/eTrustBusy.lock";

$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

eval { Sys::Syslog::setlogsock('unix'); }; # This may fail!
Sys::Syslog::openlog("eTrust-autoupdate", 'pid, nowait', 'mail');

unless (chdir $PackageDir . '/ino/config') {
  Sys::Syslog::syslog('err', "eTrust installation dir \"$PackageDir\" does not exist!");
  Sys::Syslog::closelog();
  exit 1;
}

$ENV{'CAIGLBL0000'} = $PackageDir;
$ENV{'LD_LIBRARY_PATH'} = "$PackageDir/ino/config:$PackageDir/ino/lib";

$DA0time = (stat('virsig.da0'))[9]; # mtime

if (-x 'InoDist') {
  # Timeout prevention
  $SIG{ALRM} = sub { die "timeout"};

  &LockAV();
  eval {
    alarm 300;
    $Command = './InoDist -cfg InoDist.ini';
    system($Command)>>8;
    &UnlockAV();
    alarm 0;
  };

  if ($@) {
    if ($@ =~ /timeout/) {
      # We timed out!
      &UnlockAV();
      alarm 0;
    }
  } else {
    alarm 0;
    if ($DA0time != (stat('virsig.da0'))[9]) {
      Sys::Syslog::syslog('info', "eTrust updated");
    } else {
      Sys::Syslog::syslog('info', "eTrust did not need updating");
    }
  }
} else {
  Sys::Syslog::syslog('err', "eTrust updater $PackageDir/ino/config/InoDist cannot be run");
}

Sys::Syslog::closelog();
exit 0;

sub LockAV {
	open(LOCK, ">$LockFile") or return;
	flock(LOCK, $LOCK_EX);
	print LOCK "Locked for updating eTrust definitions by $$\n";
}

sub UnlockAV {
	print LOCK "Unlocked after updating eTrust definitions by $$\n";
	unlink $LockFile;
	flock(LOCK, $LOCK_UN);
	close LOCK;
}



syntax highlighted by Code2HTML, v. 0.9.1