niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Unable to lock a file to stop sendmail writing to it
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Ben Morrow
*nix forums Guru Wannabe


Joined: 24 Apr 2006
Posts: 193

PostPosted: Thu Jul 20, 2006 9:35 am    Post subject: Re: Unable to lock a file to stop sendmail writing to it Reply with quote

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
GarfGarf
*nix forums beginner


Joined: 16 Nov 2005
Posts: 4

PostPosted: Thu Jul 20, 2006 9:16 am    Post subject: Re: Unable to lock a file to stop sendmail writing to it Reply with quote

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
Gunnar Hjalmarsson
*nix forums Guru


Joined: 26 Feb 2005
Posts: 852

PostPosted: Thu Jul 20, 2006 9:11 am    Post subject: Re: Unable to lock a file to stop sendmail writing to it Reply with quote

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
anno4000@radom.zrz.tu-ber
*nix forums beginner


Joined: 05 Jul 2006
Posts: 40

PostPosted: Thu Jul 20, 2006 9:03 am    Post subject: Re: Unable to lock a file to stop sendmail writing to it Reply with quote

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
GarfGarf
*nix forums beginner


Joined: 16 Nov 2005
Posts: 4

PostPosted: Thu Jul 20, 2006 8:54 am    Post subject: Unable to lock a file to stop sendmail writing to it Reply with 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;

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
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 3:42 am | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Unable to print PDF only. subhankar RedHat 0 Mon May 05, 2008 9:08 am
No new posts unable to pop bahadirtonguc Postfix 0 Fri Apr 04, 2008 6:32 pm
No new posts Unable to detect HDD infoshirish hardware 0 Wed Jun 06, 2007 11:12 am
No new posts Unable to detect HDD infoshirish hardware 0 Wed Jun 06, 2007 11:11 am
No new posts Unable to detect HDD infoshirish Distributions 0 Wed Jun 06, 2007 11:09 am

Bad Credit Mortgages | Mortgage Calculator | Mortgages | Loans | Loans
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
[ Time: 0.1730s ][ Queries: 20 (0.0846s) ][ GZIP on - Debug on ]