in reply to open FH, "|foo" under modperl2

You've got a nasty little race condition in your log handling. Someone can squeeze in and modify the log file between the time you've opened the file for append and the time you've thrown a lock on the file. The simple cure for this is to toss in a seek $fh, 0, 2; after the call to flock().

Replies are listed 'Best First'.
Re: Re: open FH, "|foo" under modperl2
by bart (Canon) on Feb 06, 2004 at 23:21 UTC
    Someone can squeeze in and modify the log file between the time you've opened the file for append and the time you've thrown a lock on the file. The simple cure for this is to toss in a seek $fh, 0, 2; after the call to flock().
    I thought "open for append" implied that the system takes care of this, all by itself?

    The next test seems to confirm that (Redhat Linux):

    #!/usr/local/bin/perl -lw my $file = "test.txt"; unlink $file; if(fork) { open STDOUT, ">>$file" or die "Can't write to file: $!"; $| = 1; print "first"; sleep 3; print "third"; } else { open STDOUT, ">>$file" or die "Can't write to file: $!"; $| = 1; sleep 1; print "second"; }
    Result in the file:
    first
    second
    third