Dear Monks,

I fear this may well be a FAQ or the combination of a couple of them. Forgive me if this is the case.

All I want is to lock a file. Truthfully. Its got so bad that this is the limit of my ambition. In the main I'm not a perl numpty, but this is driving my looney.

Running on an FC4 box I have the following code to reset (i.e. remove) an entry from the current user's ssh known_hosts file. I cannot get the lock to work. I have commented some of the artifacts of what I've already tried. Please guide me to enlightenment.

#!/usr/bin/perl my $host = $ARGV[0]; my $known_hosts = './kh.tt'; ##USE '>>' OTHERWISE WE WILL TRUNCATE AT open() - BEFORE WE HAVE THE L +OCK!! #do{ # print 'Error opening ssh known hosts file : ', $!, "\n"; # exit; #}unless( open KNOWN_HOSTS, ">> $known_hosts" ); open LOCK, "> $known_hosts.sem" or die "Can't open semaphore: $!\n"; ##lock the file my $lock_tries = 3; my $got_lock = 0; GETLOCK: while( $lock_tries ) { print "TRYING FOR LOCK ($lock_tries)\n"; #if( flock(KNOWN_HOSTS, LOCK_EX|LOCK_NB) ) my $ret = flock(LOCK, LOCK_EX|LOCK_NB); print "Flock ret : $ret\n"; if( $ret ) { print "GOT LOCK\n"; $got_lock = 1; last GETLOCK; } sleep(1); $lock_tries --; } do{ print 'Unable to lock known hosts file for updates', "\n"; close LOCK; exit; } unless($got_lock == 1); #open KNOWN_HOSTS_RD, "< $known_hosts"; open KNOWN_HOSTS, "+< $known_hosts" or die "Can't open data file: $!\n +"; #my @host_data = <KNOWN_HOSTS_RD>; my @host_data = <KNOWN_HOSTS>; #close KNOWN_HOSTS_RD; my $host_pattern = $host; $host_pattern =~ s/\./\\\./; my @new_data = grep(!/^$host_pattern/,@host_data); truncate KNOWN_HOSTS, 0; print KNOWN_HOSTS join("\n", @new_data); ##This releases the lock. close(KNOWN_HOSTS); close LOCK;
Many thanks for any help.

In reply to flock(), dodgy return? by danstuken

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.