1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merged Ctrl-C interception by @creack

This commit is contained in:
Solomon Hykes 2013-03-29 13:12:04 -07:00
commit c937e237ad
2 changed files with 16 additions and 6 deletions

View file

@ -8,6 +8,7 @@ import (
"io"
"log"
"os"
"os/signal"
)
func main() {
@ -47,12 +48,21 @@ func daemon() error {
func runCommand(args []string) error {
var oldState *term.State
var err error
if term.IsTerminal(0) && os.Getenv("NORAW") == "" {
oldState, err = term.MakeRaw(0)
if term.IsTerminal(int(os.Stdin.Fd())) && os.Getenv("NORAW") == "" {
oldState, err = term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return err
}
defer term.Restore(0, oldState)
defer term.Restore(int(os.Stdin.Fd()), oldState)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
term.Restore(int(os.Stdin.Fd()), oldState)
log.Printf("\nSIGINT received\n")
os.Exit(0)
}
}()
}
// FIXME: we want to use unix sockets here, but net.UnixConn doesn't expose
// CloseWrite(), which we need to cleanly signal that stdin is closed without
@ -73,7 +83,7 @@ func runCommand(args []string) error {
if err := <-receiveStdout; err != nil {
return err
}
if !term.IsTerminal(0) {
if !term.IsTerminal(int(os.Stdin.Fd())) {
if err := <-sendStdin; err != nil {
return err
}
@ -88,7 +98,7 @@ func runCommand(args []string) error {
}
}
if oldState != nil {
term.Restore(0, oldState)
term.Restore(int(os.Stdin.Fd()), oldState)
}
return nil
}

View file

@ -15,7 +15,7 @@ void MakeRaw(int fd) {
ioctl(fd, TCGETS, &t);
t.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
t.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
t.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
t.c_cflag &= ~(CSIZE | PARENB);
t.c_cflag |= CS8;