[pacman-dev] Improving locking - Was: [PATCH] Use file locks (flock) to stop multiple pacman instances from running concurrently. This improves the current model by automatically removing locks held by programs has failed to remove the lock, for example at a crash, forced kill or power outage.
Jan Alexander Steffens
jan.steffens at gmail.com
Mon Aug 19 06:13:01 EDT 2013
On Mon, Aug 19, 2013 at 11:38 AM, Jan Alexander Steffens
<jan.steffens at gmail.com> wrote:
> How about this?
> * Open lock file with O_RDWR | O_APPEND | O_CREAT and read.
> -> If file is empty, we can run.
> -> If file contains a non-existing PID and the current hostname on its
> last line, we can run.
> * If we can run, write new line containing current PID and hostname.
> * Rewind file and read again. Verify content is as expected.
Correction: Close and reopen instead of rewinding, to catch the lock
file being removed right after the open().
> * Do database operation.
> * Remove lock file.
Addendum: The file is only removed iff the lock was taken successfully.
> I'm not sure how well it works over a network. Maybe one or more
> fsyncs can be introduced to ensure the appending and reading is
> synchronized with the server?
More information about the pacman-dev