github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/signals/signal.go (about)

     1  // Copyright (C) 2020, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package signals
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  )
    10  
    11  var onlyOneSignalHandler = make(chan struct{})
    12  
    13  // SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
    14  // which is closed on one of these signals. If a second signal is caught, the program
    15  // is terminated with exit code 1.
    16  func SetupSignalHandler() (stopCh <-chan struct{}) {
    17  	close(onlyOneSignalHandler) // panics when called twice
    18  
    19  	stop := make(chan struct{})
    20  	c := make(chan os.Signal, 2)
    21  	signal.Notify(c, shutdownSignals...)
    22  	go func() {
    23  		<-c
    24  		close(stop)
    25  		<-c
    26  		os.Exit(1) // second signal. Exit directly.
    27  	}()
    28  
    29  	return stop
    30  }