github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/signal.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"github.com/mitchellh/packer/packer/plugin"
     6  	"log"
     7  	"os"
     8  	"os/signal"
     9  )
    10  
    11  // Prepares the signal handlers so that we handle interrupts properly.
    12  // The signal handler exists in a goroutine.
    13  func setupSignalHandlers(env packer.Environment) {
    14  	ch := make(chan os.Signal, 1)
    15  	signal.Notify(ch, os.Interrupt)
    16  
    17  	go func() {
    18  		// First interrupt. We mostly ignore this because it allows the
    19  		// plugins time to cleanup.
    20  		<-ch
    21  		log.Println("First interrupt. Ignoring to allow plugins to clean up.")
    22  
    23  		// Second interrupt. Go down hard.
    24  		<-ch
    25  		log.Println("Second interrupt. Exiting now.")
    26  
    27  		env.Ui().Error("Interrupt signal received twice. Forcefully exiting now.")
    28  
    29  		// Force kill all the plugins
    30  		plugin.CleanupClients()
    31  		os.Exit(1)
    32  	}()
    33  }