diff options
author | Guilhem Moulin <guilhem@fripost.org> | 2016-03-12 23:35:59 +0100 |
---|---|---|
committer | Guilhem Moulin <guilhem@fripost.org> | 2016-03-12 23:38:23 +0100 |
commit | 83529ed336f5de59d043ae76be36b2b5dfba68a3 (patch) | |
tree | 26c31223fc2493655acd841337f5f6257d82bab8 | |
parent | 24524d011751d065367743342702b8794a6de205 (diff) |
interimap: use SQLite's own locking mechanism to lock down the database.
(instead of rolling our own)
-rwxr-xr-x | interimap | 28 |
1 files changed, 4 insertions, 24 deletions
@@ -77,7 +77,7 @@ my $CONF = read_config( delete $CONFIG{config} // $NAME , 'list-select-opts' => qr/\A([\x21\x23\x24\x26\x27\x2B-\x5B\x5E-\x7A\x7C-\x7E]+)\z/ , 'ignore-mailbox' => qr/\A([\x01-\x09\x0B\x0C\x0E-\x7F]+)\z/ ); -my ($DBFILE, $LOCKFILE, $LOGGER_FD); +my ($DBFILE, $LOGGER_FD); { $DBFILE = $CONF->{_}->{database} if defined $CONF->{_}; @@ -95,8 +95,6 @@ my ($DBFILE, $LOCKFILE, $LOGGER_FD); } } - $LOCKFILE = $DBFILE =~ s/([^\/]+)\z/.$1.lck/r; - if (defined $CONF->{_} and defined $CONF->{_}->{logfile}) { require 'POSIX.pm'; require 'Time/HiRes.pm'; @@ -117,7 +115,6 @@ my ($IMAP, $lIMAP, $rIMAP); sub cleanup() { undef $_ foreach grep defined, ($IMAP, $lIMAP, $rIMAP); logger(undef, "Cleaning up...") if $CONFIG{debug}; - unlink $LOCKFILE if defined $LOCKFILE and -f $LOCKFILE; close $LOGGER_FD if defined $LOGGER_FD; $DBH->disconnect() if defined $DBH; } @@ -126,33 +123,16 @@ $SIG{TERM} = sub { cleanup(); exit 0; }; ############################################################################# -# Lock the database -{ - if (-f $LOCKFILE) { - open my $lock, '<', $LOCKFILE or die "Can't open $LOCKFILE: $!\n"; - my $pid = <$lock>; - close $lock; - chomp $pid; - my $msg = "LOCKFILE '$LOCKFILE' exists."; - undef $LOCKFILE; # don't delete the lockfile - $msg .= " (Is PID $pid running?)" if defined $pid and $pid =~ /^[0-9]+$/; - die $msg, "\n"; - } - - open my $lock, '>', $LOCKFILE or die "Can't open $LOCKFILE: $!\n"; - print $lock $$, "\n"; - close $lock; -} - - -############################################################################# # Open the database and create tables $DBH = DBI::->connect("dbi:SQLite:dbname=$DBFILE", undef, undef, { AutoCommit => 0, RaiseError => 1, sqlite_see_if_its_a_number => 1, # see if the bind values are numbers or not + sqlite_use_immediate_transaction => 1, }); +$DBH->sqlite_busy_timeout(250); +$DBH->do('PRAGMA locking_mode = EXCLUSIVE'); $DBH->do('PRAGMA foreign_keys = ON'); |