procmail
[Top] [All Lists]

Re: Procmail/Perl and locking

2004-07-14 06:42:56
On Wed, Jul 14, 2004 at 01:46:22PM +0200, Sven Schmitt wrote:
Now I thought about doing something like this:

use Fcntl qw(:DEFAULT :flock);
open(MBOX, ">>/path/to/mbox") or die $!;
flock(MBOX, LOCK_EX) || die "flock failed on mbox $!";
print MBOX $emails;
close(MBOX) or warn "close failed for mbox $!";

Afaik my Procmail uses dotlocking (Locking strategies: dotlocking,
lockf). Now I wonder if the above Perl code will be safe, or if there
still is the chance that I run into Procmail/Perl file access problems.

The above code is unsafe, as there is a delay between open and flock.
Say another process is writing to the file when you do your 'open';
by the time your program obtains its exclusive lock, the EOF has moved
beyond your program's file handle. Any writes by your program at this
point will not occur at the end of the file but somewhere before the
end (clobbering data).

You should at least put in a seek after the flock and before the print:

    seek MBOX, 0, 2;

This will make sure that your handle is really at the end where it
should be after the flock.

For better safety, use Paul's approach of writing via procmail
directly; you can have it bypass or use any procmailrc files you wish
by specifying them on the command-line (as per the procmail manpage).
This way, you're sure to get locking compatible with procmail and
don't need the flock or seek.

    open MBOX, "|procmail (args)" or die $!;

Scott
-- 
Scott Wiersdorf
scott(_at_)perlcode(_dot_)org

____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail

<Prev in Thread] Current Thread [Next in Thread>