Current File : /home/inlingua/public_html/decay_sym/root/var/softaculous/apps/exim/popb4smtp-clean
#!/usr/bin/perl -w
#
# $Id: popb4smtp-clean,v 1.5 2002/11/17 14:41:56 sheldonh Exp $
#
use strict;
use Unix::Syslog qw(:macros :subs);

$SIG{INT} = sub {
	syslog(LOG_CRIT, "%s", "exiting on SIGINT\n");
	exit 1;
};
$SIG{TERM} = sub {
	syslog(LOG_CRIT, "%s", "exiting on SIGTERM\n");
	exit 1;
};

my $DSEARCHDIR = '/var/webuzo-data/popb4smtp';
my $PROGNAME = 'popb4smtp-clean';
my $PIDFILE = '/var/run/popb4smtp-clean.pid';
my $KEEPSECS = 60 * 60 * 2;
my $FLUSHFREQ = 60 * 5;

sub xdie(@);
sub xwarn(@);

openlog($PROGNAME, LOG_PERROR | LOG_PID, LOG_MAIL);
open(PID, "> $PIDFILE") or
    xdie "$PIDFILE: $!\n";
print PID "$$\n";
close(PID);

syslog(LOG_CRIT, "%s", "pruning tree: $DSEARCHDIR\n");

while (1) {
	my ($now, $stat_cnt);

	$now = $stat_cnt = 0;
	opendir(DIR, $DSEARCHDIR) or
	    xwarn "$DSEARCHDIR: $!\n";
	while (my $dir = readdir(DIR)) {
		next if $dir =~ /^\.{1,2}$/;

		opendir(SUBDIR, "$DSEARCHDIR/$dir") or
		    xwarn "$DSEARCHDIR/$dir: $!\n";
		while (my $fn = readdir(SUBDIR)) {
			my ($mtime, $now);

			next if $fn =~ /^\.{1,2}$/;

			$stat_cnt++;
			if (not $mtime = (stat("$DSEARCHDIR/$dir/$fn"))[9]) {
				xwarn "$DSEARCHDIR/$dir/$fn: $!\n";
				next;
			}
			if (not $now or $stat_cnt > 250) {
				# We only call time() once every few stat()
				# calls to ensure that we don't abuse the
				# gettimeofday() system call.  We can't just
				# call it once per session, though, in case
				# a session takes a long time.
				#
				$now = time();
				$stat_cnt = 0;
			}
			if ($now - $mtime > $KEEPSECS and
			    not unlink("$DSEARCHDIR/$dir/$fn")) {
				xwarn "$DSEARCHDIR/$dir/$fn: $!\n";
			}
		}
		closedir(SUBDIR);
	}
	closedir(DIR);

	sleep($FLUSHFREQ);
}


sub xdie(@)
{
	my (@msg) = @_;
	my ($msgstr, $rv);

	$rv = $! or 1;
	$msgstr = join(' ', @msg);
	chomp($msgstr);
	syslog(LOG_CRIT, "%s", $msgstr);
	exit $rv;
}


sub xwarn(@)
{
	my (@msg) = @_;
	my ($msgstr);

	$msgstr = join(' ', @msg);
	chomp($msgstr);
	syslog(LOG_CRIT, "%s", $msgstr);
}