Author: jim
Date: Fri May 23 13:14:10 2014
New Revision: 1597087
URL: http://svn.apache.org/r1597087
Log:
Provide locking
Modified:
steve/trunk/cmdline/steve.pm
steve/trunk/cmdline/vote.pl
Modified: steve/trunk/cmdline/steve.pm
URL:
http://svn.apache.org/viewvc/steve/trunk/cmdline/steve.pm?rev=1597087&r1=1597086&r2=1597087&view=diff
==============================================================================
--- steve/trunk/cmdline/steve.pm (original)
+++ steve/trunk/cmdline/steve.pm Fri May 23 13:14:10 2014
@@ -21,6 +21,8 @@
##use strict;
+use Fcntl qw(:flock SEEK_END);
+
$ECHO = '/bin/echo';
$CAT = '/bin/cat';
$MD5 = '/sbin/md5';
@@ -244,4 +246,19 @@ sub ballots {
return @ballots;
}
+# ==========================================================================
+
+sub lock {
+ my ($f) = @_;
+ flock($f, LOCK_EX) or die "Cannot lock file: $!\n";
+ seek($f, 0, SEEK_END) or die "Cannot seek: $!\n";
+}
+
+# ==========================================================================
+
+sub unlock {
+ my ($f) = @_;
+ flock($f, LOCK_UN) or die "Cannot unlock file: $!\n";
+}
+
1;
Modified: steve/trunk/cmdline/vote.pl
URL:
http://svn.apache.org/viewvc/steve/trunk/cmdline/vote.pl?rev=1597087&r1=1597086&r2=1597087&view=diff
==============================================================================
--- steve/trunk/cmdline/vote.pl (original)
+++ steve/trunk/cmdline/vote.pl Fri May 23 13:14:10 2014
@@ -193,12 +193,12 @@ $date = &get_date;
$entry = "[$date] $vhash2 $vote\n";
# ==========================================================================
-# write vote entry to tally file -- this should be atomic
+# write vote entry to tally file -- this should be atomic (ensure w/ flock)
if (-e $closerfile) { die "$pname: $issuename already closed voting\n"; }
open(TALLY, ">>$tallyfile") || die "$pname: cannot open tally: $!\n";
-
+lock(TALLY);
$len = length($entry);
$off = 0;
do {
@@ -207,11 +207,9 @@ do {
$len -= $written;
$off -= $written;
} while ($len > 0);
+unlock(TALLY);
close(TALLY);
-# Disregard below "^####" comments for now...
-#### Close TALLY later, to "keep the books balanced" (well, noisily unbalanced)
-##### in case someone ^C's us just before sendmail is run.
print "Your vote has been accepted on issue $issuename\n";
# ==========================================================================
@@ -256,10 +254,6 @@ foreach $vf (@vfiles) {
}
close(MAIL);
-# Finish log entry
-####syswrite(TALLY, "\n") or die "$pname: cannot finalize tally: $!\n";
-####close(TALLY);
-
# ==========================================================================
# Send mail to voter telling them that someone voted using their hash-ID