Unable to lock a file to stop sendmail writing to it

G

GarfGarf

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 !
 
A

anno4000

GarfGarf said:
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;

The O_EXCL flag to sysopen() doesn't do what you seem to think it does.
This is explicitly pointed out in perldoc -f sysopen. Read it.
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.

Quite so. File locking is advisory, not mandatory. Read perldoc -f
flock.

Anno
 
G

Gunnar Hjalmarsson

GarfGarf said:
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.

I think your problem is merely a misconception with respect to the
meaning of flock(). As stated in

perldoc -f flock

the nature of an flock() lock is advisory.
sendmail writes into the file while i'm writing and that
causes all sorts of problems.

That's probably because sendmail doesn't use flock on the mailboxes.
 
G

GarfGarf

Gunnar said:
I think your problem is merely a misconception with respect to the
meaning of flock(). As stated in

perldoc -f flock

the nature of an flock() lock is advisory.


That's probably because sendmail doesn't use flock on the mailboxes.

Ok, thanks for the replies.
It seems flock is not the way to go..

So, how can I lock the file to stop sendmail spatting it?
 
B

Ben Morrow

Quoth "GarfGarf said:
Ok, thanks for the replies.
It seems flock is not the way to go..

So, how can I lock the file to stop sendmail spatting it?

You need to find out how sendmail *does* lock the files, then do the
same. It might use flock locking, fcntl locking or create a lockfile.
Then you do the same.

FWIW, if you use maildir mail folders you don't need locking...

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top