From efe9d8dafd6a8bc5845eae3b4dbc172ad68aa187 Mon Sep 17 00:00:00 2001 From: Niko Tyni Date: Tue, 18 Mar 2008 06:46:15 +0000 Subject: revert the locking change in r951 for portability reasons --- CHANGES | 3 --- lib/Smokeping/Master.pm | 32 +++++++++++++------------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/CHANGES b/CHANGES index a552f48..acd0f9b 100644 --- a/CHANGES +++ b/CHANGES @@ -6,9 +6,6 @@ * store the slave updates in 'dyndir', defaulting to 'datadir' --niko -* change the slave update locking code so that reading works without - write access to the temporary storage file --niko - * allow SSH probe to config rsa1 key -- Walery Kokarev * make zooming work properly even for graphs generated with the diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm index 81ce01f..6fbdcbf 100644 --- a/lib/Smokeping/Master.pm +++ b/lib/Smokeping/Master.pm @@ -1,7 +1,7 @@ # -*- perl -*- package Smokeping::Master; use Data::Dumper; -use Storable qw(nstore_fd dclone fd_retrieve); +use Storable qw(nstore dclone retrieve); use strict; use warnings; use Fcntl qw(:flock); @@ -115,16 +115,13 @@ sub save_updates { warn "Skipping update for ${name}.${slave}.slave_cache since ". "you seem to try todo some directory magic here. Don't!"; } - else { + elsif ( open (my $lock, '>>' , "$file.lock") ) { for (my $i = 3; $i > 0; $i--){ - my $fh; - if ( open ($fh, '+>>' , $file) and flock($fh, LOCK_EX) ){ + if ( flock($lock, LOCK_EX) ){ my $existing = []; - next if ! -e $file; # the reader unlinked it from under us - seek $fh, 0, 0; - if ( -s _ ){ - my $in = eval { fd_retrieve $fh }; + if ( -r $file ){ + my $in = eval { retrieve $file }; if ($@) { #error warn "Loading $file: $@"; } else { @@ -132,17 +129,16 @@ sub save_updates { }; }; push @{$existing}, [ $slave, $time, $updatestring ]; - nstore_fd($existing, $fh); - flock($fh, LOCK_UN); - close $fh; + nstore($existing, $file); last; } else { warn "Could not lock $file ($!). Trying again for $i rounds.\n"; sleep rand(2); } - warn "Could not update $file, giving up for now."; - close $fh; } + close $lock; + } else { + warn "Could not update $file: $!"; } } }; @@ -165,12 +161,10 @@ sub get_slaveupdates { my $dir = slavedatadir($cfg); $file =~ s/^\Q$datadir\E/$dir/; - my $fh; - if ( open ($fh, '<', $file) ) { - if ( flock $fh, LOCK_SH ){ - eval { $data = fd_retrieve $fh }; + if ( -r $file and open (my $lock, '>>', "$file.lock") ) { + if ( flock $lock, LOCK_EX ){ + eval { $data = retrieve $file }; unlink $file; - flock $fh, LOCK_UN; if ($@) { #error warn "Loading $file: $@"; return $empty; @@ -178,7 +172,7 @@ sub get_slaveupdates { } else { warn "Could not lock $file. Will skip and try again in the next round. No harm done!\n"; } - close $fh; + close $lock; return $data; } return $empty; -- cgit v1.2.3-24-g4f1b