#!/usr/bin/perl -w

$MM_DIR     = '/usr/local/mailman';  # The Mailman dir
$LOCK_DIR   = "$MM_DIR/locks";       # The Mailman lock dir

$live_count = 0;
$dead_count = 0;


opendir(LOCKS, $LOCK_DIR) || die("Couldn't open lock dir '$LOCK_DIR': $!");

## Loop over the list of lock files ##
while ( defined($lock = readdir(LOCKS)) ) {
	chomp($lock);
		
	## if the filename ends in a PID ##
	if ($lock =~ m/(\d+)$/) {
		$pid = $1;
		
		## `kill -0 pid` will return the number of matching processes ##
		## and 0 if none match.  So boolean true if the process       ##
		## exists and false if not.  It won't actually *kill*         ##
		## anything.  It's just and are-you-alive ping.               ##

		if ( kill(0, $pid) ) {
			$live_count++;
		}
		else {
			$dead_count++;
			unlink("$LOCK_DIR/$lock") || die("Could not delete dead lock file '$LOCK_DIR/$lock': $!");
		}
	}
}

print("Removed $dead_count dead lock(s).\n");
print("Left $live_count live lock(s).\n");
