I am writing a small program (below) to enable me to replace the /etc/sudoers file (via an RPM), while respecting visudo and its file lock. (First thing, please note that the visudo man page, at least for my distro is wrong - there is no lock on /etc/sudoers.tmp AFAICT). I am doing this on x86 FC4 (so that you may grok the pack()). Basically, this program works - when I do a visudo after acquiring the lock through this program, it behaves just like 2 visudos were contending. That is, I get a "visudo: sudoers file busy, try again later." So all is swell, except I am picky and ...

I look at the output of "lsof -p" for visudo and compare the same output for the perl program.

When visudo is running, I get...

visudo 13070 root 3uW REG 3,3 640 12835419 /etc/sudoers

When the perl program is running (having gotten lock and is looping), I get...

fcntl_loc 13091 root 3w REG 3,3 640 12835419 /etc/sudoers

See, the 3uW looks good for visudo, but where's the "W" to indicate write lock for the perl? Do I have a file lock, or is visudo failing to acquire the lock on /etc/sudoers for some other reason while this perl code is running?

So, I wonder if perhaps my pack is wrong and I am stomping on some memory. Or else something else is going on? Its too late on Friday for me to see what it is - maybe one of you monks can? (Pls don't kill me for the treatment of $rc - this code is just hacked together, thx).

Here's the program.

#!/usr/bin/perl use Fcntl qw(:DEFAULT :flock); use Errno qw(EAGAIN); use strict; use warnings; @ARGV == 1 or die "usage: $0 <filename>\n"; print "opening $ARGV[0]...\n"; open my $fh, ">>$ARGV[0]" or die "open:$!\n"; my $results = pack( "sslll", &F_WRLCK, 0, 0, 0, 0); print "locking $ARGV[0]...\n"; my $rc; while (1) { $rc = fcntl($fh, F_SETLK, $results); last if ($! != EAGAIN || (defined $rc && $rc == 0)); print "sleeping...\n"; sleep 5; print "trying again...\n"; } if ((!defined $rc) || $rc != 0) { die "fcntl:$!\n" }; print "looping forever...\n"; sleep 64*64;

In reply to With a fcntl lock, why this lsof output? by welchavw

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.