Commit 23450837 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

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

parent 35ee6313
......@@ -46,6 +46,9 @@ func main() {
packer.Version, packer.VersionPrerelease, packer.GitCommit)
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()
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err)
......
......@@ -18,7 +18,7 @@ func setupSignalHandlers(env packer.Environment) {
// First interrupt. We mostly ignore this because it allows the
// plugins time to cleanup.
<-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.
<-ch
......
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.")
}()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment