aboutsummaryrefslogtreecommitdiffstats
path: root/imapsync
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2015-07-24 16:55:21 +0200
committerGuilhem Moulin <guilhem@fripost.org>2015-07-24 17:35:16 +0200
commitb2beb8ab9149ea4a4977209160ab8a9f15f05ab7 (patch)
tree8ff94d71902b137e8b461477212d9c45deddc49e /imapsync
parenta8445f6da256a09bac321a0bd2c23e3b8424caf4 (diff)
Exit gracefully when catching a SIGHUP.
Diffstat (limited to 'imapsync')
-rwxr-xr-ximapsync19
1 files changed, 11 insertions, 8 deletions
diff --git a/imapsync b/imapsync
index f80106d..ea6c4e8 100755
--- a/imapsync
+++ b/imapsync
@@ -69,17 +69,16 @@ my ($DBFILE, $LOCKFILE);
$LOCKFILE = $DBFILE =~ s/([^\/]+)\z/.$1.lck/r;
}
-my ($DBH, $IMAP);
-
+my $DBH;
# Clean after us
-sub clean() {
- print STDERR "Cleaning...\n" if $CONFIG{debug};
+sub cleanup() {
+ print STDERR "Cleaning up...\n" if $CONFIG{debug};
unlink $LOCKFILE if defined $LOCKFILE and -f $LOCKFILE;
- undef $_ foreach grep defined, map {$IMAP->{$_}->{client}} keys %$IMAP;
$DBH->disconnect() if defined $DBH;
}
-$SIG{$_} = sub { clean(); die "$!\n"; } foreach qw/INT TERM/;
+$SIG{$_} = sub { cleanup(); print STDERR "$!\n"; exit 1; } foreach qw/INT TERM/;
+$SIG{$_} = sub { cleanup(); print STDERR "$!\n"; exit 0; } foreach qw/HUP/;
#############################################################################
@@ -176,6 +175,7 @@ sub msg($@) {
#############################################################################
# Connect to the local and remote IMAP servers
+my $IMAP;
foreach my $name (qw/local remote/) {
my %config = %{$CONF->{$name}};
$config{$_} = $CONFIG{$_} foreach keys %CONFIG;
@@ -934,8 +934,11 @@ while(1) {
}
}
# clean state!
- exit 0 if $CONFIG{oneshot} or $CONFIG{check};
+ if ($CONFIG{oneshot} or $CONFIG{check}) {
+ cleanup();
+ exit 0;
+ }
wait_notifications(900);
}
-END { clean (); }
+END { cleanup(); }