Unable to lock a file to stop sendmail writing to it



Hi all,

I am trying to write a script that reads a mailbox that sendmail
updates, but I want to be able to lock the file, read the file, empty
the file and unlock the file again.
The problem I have is that locking doesn't seem to work. As I can't
lock the file, sendmail writes into the file while i'm writing and that
causes all sorts of problems.

Here are some things I tried:-

sysopen(FH,"/tmp/file",O_EXCL | O_RDWR) || die "Error:$!";
print "File opened\n";
sleep 20;
close FH;

or


use Fcntl ':flock'; # import LOCK_* constants
open(FILE,"</tmp/file") || die $!;
flock(FILE,LOCK_EX);
sleep 20;
close FILE;


each one of those should lock the file, and hold the lock for 20
seconds.
However during that 20 seconds I can still run date >> /tmp/file
and it gets written to.

This happens both on solaris and linux.

Help !

.



Relevant Pages

  • [UNIX] File Locking Local Denial of Service (Sendmails Impact)
    ... Any application which uses either flockor fcntlstyle locking or ... Since this attack requires a user to use their own account to lock a file, ... process holds an exclusive lock on a file, no other process can obtain an ... File locking is used throughout Sendmail for a variety of files including ...
    (Securiteam)
  • Fwd: File Locking Local Denial of Service; Impact on sendmail
    ... File Locking Local Denial of Service; Impact on sendmail ... Since this attack requires a user to use their own account to lock ... fcntl() locks require the file ...
    (FreeBSD-Security)
  • Re: Unable to lock a file to stop sendmail writing to it
    ... meaning of flock(). ... the nature of an flock() lock is advisory. ... That's probably because sendmail doesn't use flock on the mailboxes. ... It might use flock locking, fcntl locking or create a lockfile. ...
    (comp.lang.perl.misc)
  • Re: Unable to lock a file to stop sendmail writing to it
    ... I am trying to write a script that reads a mailbox that sendmail ... updates, but I want to be able to lock the file, read the file, empty ... sleep 20; ... This is explicitly pointed out in perldoc -f sysopen. ...
    (comp.lang.perl.misc)
  • Re: Thunderbird really doesnt grok maildir?
    ... >wants to lock the mailbox when using the client on it precisely so that ... sendmail does not touch the mailbox. ...
    (comp.os.linux.misc)