sub get_mail { unless (-s $mailspool) { # Nothing in the mailbox? Don't bother then $debug and print LOG "No mail in mailspool\n"; return; } open(MAILBOX, "<$mailspool") or &loganddie("$mailspool exists but I can't open it : $!"); flock(MAILBOX, LOCK_EX|LOCK_NB) or do {$failed_locks++; return 0}; # Not getting the lock is not all that fatal - we can try later # If we get here, then we can finally do something with the mailspool. # First though, we have to open the file we're moving it to. opendir (PROCESSDIR, $processdir) or &loganddie("Someone broke $processdir : $!"); my @allfiles = grep !/^\.\.?\z/, readdir PROCESSDIR; closedir PROCESSDIR; my $output_file = &POSIX::tmpnam(); $output_file =~ s/\/tmp\/(.*)/$1/; # remove /tmp/ from string open(PENDING, ">>$processdir/$output_file") or &loganddie("Can't write to $processdir/$output_file : $!"); $debug and print "opened file\n"; flock(PENDING, LOCK_EX|LOCK_NB) or return 0; $debug and print "got lock\n"; seek(PENDING,0,2) or return 0; # in case some other process appended $debug and print "did seek\n"; print PENDING while (); $debug and print "read mailbox\n"; close PENDING or &loganddie("man there is some crazy sh|t going on\n\tcouldn't close $processdir/$output_file : $!"); $debug and print "closed pending\n"; #### OK, somehow we now have to truncate the file without unlinking it (don't have write on /var/spool/mail) or releasing the lock (what if more mail comes in between us releasing the lock and truncating the file?) close MAILBOX; $debug and print "closed MAILBOX\n"; return; }