Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found