catch ^C and clean up lock file

Go doesn't have atexit(3), and the only signals guaranteed to
be available on all platforms are SIGINT and SIGKILL, so we only
catch SIGINT here.  This allows us to at least have the user
not have to manually clean up the state file after ^C'ing
certspotter.
This commit is contained in:
Jan Schaumann 2017-12-07 16:46:07 -05:00
parent e96ccbab62
commit 3e9ed03149
1 changed files with 11 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import (
"log"
"os"
"os/user"
"os/signal"
"path/filepath"
"sync"
@ -330,6 +331,16 @@ func Main(statePath string, processCallback certspotter.ProcessCallback) int {
return 1
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
if err := state.Unlock(); err != nil {
fmt.Fprintf(os.Stderr, "%s: Error unlocking state directory: %s\n", os.Args[0], err)
}
os.Exit(1)
}()
exitCode := 0
for i := range logs {
exitCode |= processLog(&logs[i], processCallback)