From 3e9ed03149c521817f309d82321b0d64d4be7264 Mon Sep 17 00:00:00 2001 From: Jan Schaumann Date: Thu, 7 Dec 2017 16:46:07 -0500 Subject: [PATCH] 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. --- cmd/common.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmd/common.go b/cmd/common.go index 8f86fba..8237cb1 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -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)