aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-19 16:43:23 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-06-19 16:43:23 +0200
commitb6e969be7a69a638a73618f45ae7a5f40f1157ac (patch)
tree414f782bc889f536a79d4576385487f27c3c82f3 /pkg/osutil
parenta89409fa44d9743dd6044515273f913e9733b72a (diff)
pkg/osutil: add helper function for SIGINT handling
Diffstat (limited to 'pkg/osutil')
-rw-r--r--pkg/osutil/osutil.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go
index a9c64ad1c..cbd287d4d 100644
--- a/pkg/osutil/osutil.go
+++ b/pkg/osutil/osutil.go
@@ -9,6 +9,7 @@ import (
"io"
"os"
"os/exec"
+ "os/signal"
"path/filepath"
"syscall"
"time"
@@ -75,3 +76,21 @@ func IsExist(name string) bool {
_, err := os.Stat(name)
return err == nil || !os.IsNotExist(err)
}
+
+// HandleInterrupts closes shutdown chan on first SIGINT
+// (expecting that the program will gracefully shutdown and exit)
+// and terminates the process on third SIGINT.
+func HandleInterrupts(shutdown chan struct{}) {
+ go func() {
+ c := make(chan os.Signal, 3)
+ signal.Notify(c, syscall.SIGINT)
+ <-c
+ close(shutdown)
+ fmt.Fprint(os.Stderr, "SIGINT: shutting down...\n")
+ <-c
+ fmt.Fprint(os.Stderr, "SIGINT: shutting down harder...\n")
+ <-c
+ fmt.Fprint(os.Stderr, "SIGINT: terminating\n")
+ os.Exit(int(syscall.SIGINT))
+ }()
+}