|
|
|
|
|
|
| Author |
Message |
GarfGarf *nix forums beginner
Joined: 16 Nov 2005
Posts: 4
|
Posted: Thu Jul 20, 2006 8:54 am Post subject:
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 ! |
|
| Back to top |
|
 |
anno4000@radom.zrz.tu-ber *nix forums beginner
Joined: 05 Jul 2006
Posts: 40
|
Posted: Thu Jul 20, 2006 9:03 am Post subject:
Re: Unable to lock a file to stop sendmail writing to it
|
|
|
GarfGarf <garfield_99999@yahoo.com> wrote in comp.lang.perl.misc:
| Quote: | 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.
| Quote: | 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 |
|
| Back to top |
|
 |
Gunnar Hjalmarsson *nix forums Guru
Joined: 26 Feb 2005
Posts: 852
|
Posted: Thu Jul 20, 2006 9:11 am Post subject:
Re: Unable to lock a file to stop sendmail writing to it
|
|
|
GarfGarf wrote:
| Quote: | 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.
| Quote: | 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.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl |
|
| Back to top |
|
 |
GarfGarf *nix forums beginner
Joined: 16 Nov 2005
Posts: 4
|
Posted: Thu Jul 20, 2006 9:16 am Post subject:
Re: Unable to lock a file to stop sendmail writing to it
|
|
|
Gunnar Hjalmarsson wrote:
| Quote: | GarfGarf wrote:
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.
|
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? |
|
| Back to top |
|
 |
Ben Morrow *nix forums Guru Wannabe
Joined: 24 Apr 2006
Posts: 193
|
Posted: Thu Jul 20, 2006 9:35 am Post subject:
Re: Unable to lock a file to stop sendmail writing to it
|
|
|
Quoth "GarfGarf" <garfield_99999@yahoo.com>:
| Quote: |
Gunnar Hjalmarsson wrote:
GarfGarf wrote:
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.
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
--
The cosmos, at best, is like a rubbish heap scattered at random.
Heraclitus
benmorrow@tiscali.co.uk |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Nov 20, 2008 11:58 pm | All times are GMT
|
|
Free Advertising | Loans | Mortgage | Xbox Mod Chip | Free Ringtones
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|