develooper Front page | perl.perl5.porters | Postings from March 2001

flock() on dup'ed filehandle is not enforced

Thread Next
From:
Mark-Jason Dominus
Date:
March 28, 2001 00:31
Subject:
flock() on dup'ed filehandle is not enforced
Message ID:
20010328083301.22822.qmail@plover.com

        use Fcntl ':flock';
        open F,">>lock" or die $!;
        flock F,LOCK_EX|LOCK_NB or die "lock fail\n";
        open STDOUT,">>&F" or die $!;
        print sleep 1 for 1..10;
        print "\n";

If you run two instances of this program simultaneously, the first
instance gets the lock.   The second instance should die with "lock
fail".  

Perl 5.6 on linux 2.2.16 behaves correctly, but Perl 5.005_03 on
Solaris does not.  On Solaris, neither process dies.  I have attached
Perl -V output for the perl/solaris combination that fails.

John Lin reports that 5.6 also fails on his solaris and HPUX systems.
You can contact him for more details.  (John brought this to my
attention; I did not find it myself.)  

I wrote a corresponding C program to confirm that the fault was in
Perl and not in the OS:

        
        #include <fcntl.h>
        #include <sys/file.h>
        #include <stdio.h>
        
        void die(char *s) {
          printf("%s\n", s);
          exit(1);
        }
        
        int main(void)
        {
          int fd = open("/tmp/lockfile", O_WRONLY|O_CREAT|O_TRUNC,0666);
        
          if (fd < 0) die("open failed");
          if (flock(fd, LOCK_EX|LOCK_NB) < 0) die("lock failed");
          close(1);
          dup(fd);
          fprintf(stderr, "locked.\n");
          sleep(10);
          return 0;
        }
        
        
Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=solaris, osvers=2.5.1, archname=sun4-solaris
    uname='sunos saul.cis.upenn.edu 5.5.1 generic_103640-19 sun4u sparc sunw,ultra-enterprise '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='gcc', optimize='-O', gccversion=2.7.2.1
    cppflags='-I/pkg/include -I/usr/local/include'
    ccflags ='-I/pkg/include -I/usr/local/include'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags ='-L/pkg/lib -L/usr/ucblib -R/pkg/lib:/usr/ucblib:/usr/lib -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib /pkg/lib /usr/ucblib
    libs=-lsocket -lnsl -lgdbm -ldbm -ldl -lm -lc -lcrypt
    libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-fPIC', lddlflags='-G -L/pkg/lib -L/usr/ucblib -L/usr/local/lib'


Characteristics of this binary (from libperl): 
  Built under solaris
  Compiled at Apr  5 1999 12:14:30
  %ENV:
    PERL5LIB="/home/m/mjd/lib/perl5:/home/m/mjd/lib/perl5/site_perl"
  @INC:
    /home/m/mjd/lib/perl5
    /home/m/mjd/lib/perl5/site_perl
    /pkg/p/perl5.00503/lib/sun4-solaris
    /pkg/p/perl5.00503/lib
    /pkg/p/perl5.00503/lib/site_perl/sun4-solaris
    /pkg/p/perl5.00503/lib/site_perl
    .


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About