Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Update config file parameters

by afoken (Chancellor)
on Jan 07, 2016 at 21:38 UTC ( [id://1152253]=note: print w/replies, xml ) Need Help??


in reply to Re: Update config file parameters
in thread Update config file parameters

You can do inplace editing of files:

But that introduces a race condition. The problem is documented in perlrun:

-i[extension]
specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements.

The race: Instance 1 is started, reads the configuration file, then starts rewriting by renaming the configuration file and creating a new, still empty (or partly written) configuration file. Instance 2 is started, and reads an empty (or partly written) configuration file.

How to get rid of that problem:

  • Use a (separate) lock file to synchronise access to the configuration file. Only the instance that can successfully lock the lock file may read or write the configuration file.
  • Use an atomic operation to change the configuration file. The only way I know is to rewrite the configuration file to a new, temporary file, and to rename that file to the original configuration file name.

(Note that safely creating temporary files is another problem: mkstemp(3) and tmpfile(3) should be safe. mktemp(3) and tmpnam(3) aren't. File::Temp has safe and unsafe functions.)

Alexander

(And yes, the problem of "disappearing configuration data" has bitten me once, and it was quite hard to find.)

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1152253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-19 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found