ctrl-c closes stdin for plugins so that they are unblocked

This commit is contained in:
Mitchell Hashimoto 2013-07-25 23:27:13 -07:00
parent 35ee6313e4
commit 234508376a
3 changed files with 41 additions and 1 deletions

View File

@ -46,6 +46,9 @@ func main() {
packer.Version, packer.VersionPrerelease, packer.GitCommit) packer.Version, packer.VersionPrerelease, packer.GitCommit)
log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH) log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH)
// Prepare stdin for plugin usage by switching it to a pipe
setupStdin()
config, err := loadConfig() config, err := loadConfig()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err) fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err)

View File

@ -18,7 +18,7 @@ func setupSignalHandlers(env packer.Environment) {
// First interrupt. We mostly ignore this because it allows the // First interrupt. We mostly ignore this because it allows the
// plugins time to cleanup. // plugins time to cleanup.
<-ch <-ch
log.Println("First interrupt. Ignoring, but closing stdin...") log.Println("First interrupt. Ignoring to allow plugins to clean up.")
// Second interrupt. Go down hard. // Second interrupt. Go down hard.
<-ch <-ch

37
stdin.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"io"
"log"
"os"
"os/signal"
)
// setupStdin switches out stdin for a pipe. We do this so that we can
// close the writer end of the pipe when we receive an interrupt so plugins
// blocked on reading from stdin are unblocked.
func setupStdin() {
// Create the pipe and swap stdin for the reader end
r, w, _ := os.Pipe()
originalStdin := os.Stdin
os.Stdin = r
// Create a goroutine that copies data from the original stdin
// into the writer end of the pipe forever.
go func() {
defer w.Close()
io.Copy(w, originalStdin)
}()
// Register a signal handler for interrupt in order to close the
// writer end of our pipe so that readers get EOF downstream.
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
defer signal.Stop(ch)
defer w.Close()
<-ch
log.Println("Closing stdin because interrupt received.")
}()
}