Berkeley CSUA MOTD:Entry 27680
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/04/07 [General] UID:1000 Activity:popular
4/7     

2003/3/13 [Computer/SW/Unix] UID:27680 Activity:moderate
3/12    What is the best method for securely updating the contents of
        file (unix/c)?
        What I've got right now is open() then mkstemp() and copy the
        bits from the old fd to the fd from mkstemp() and finally call
        rename(). I've specified O_RDONLY|O_EXLOCK, which should prevent
        two instances of the process from colliding, but I'm concerned
        that the call to rename() may not be "safe". TIA.
        \_ what is your definition of "secure"?  What's wrong with more
           obvious methods?
           \_ race conditions.
        \_ rename is atomic. what you describe should work fine. --aaron