From 7c473041a0044d7be460902d8c4598f559cb403e Mon Sep 17 00:00:00 2001 From: Wang Long Date: Sat, 14 Jan 2017 06:43:57 +0000 Subject: [PATCH] io: stop screwing with \n in console output The default terminal setting for a new pty on Linux (unix98) has +ONLCR, resulting in '\n' writes by a container process to be converted to '\r\n' reads by the managing process. This is quite unexpected, To fix it, make the terminal sane after opening it by setting -ONLCR. this patch fix method comes from: https://github.com/opencontainers/runc/commit/eea28f480db435dbef4a275de9776b9934818b8c thanks @cyphar Aleksa Sarai Signed-off-by: Wang Long --- cmd/containerd-shim/console.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cmd/containerd-shim/console.go b/cmd/containerd-shim/console.go index 1d3262d..96d9ebc 100644 --- a/cmd/containerd-shim/console.go +++ b/cmd/containerd-shim/console.go @@ -16,6 +16,9 @@ func newConsole(uid, gid int) (*os.File, string, error) { if err != nil { return nil, "", err } + if err = saneTerminal(master); err != nil { + return nil, "", err + } console, err := ptsname(master) if err != nil { return nil, "", err @@ -32,6 +35,29 @@ func newConsole(uid, gid int) (*os.File, string, error) { return master, console, nil } +// saneTerminal sets the necessary tty_ioctl(4)s to ensure that a pty pair +// created by us acts normally. In particular, a not-very-well-known default of +// Linux unix98 ptys is that they have +onlcr by default. While this isn't a +// problem for terminal emulators, because we relay data from the terminal we +// also relay that funky line discipline. +func saneTerminal(terminal *os.File) error { + // Go doesn't have a wrapper for any of the termios ioctls. + var termios syscall.Termios + + if err := ioctl(terminal.Fd(), syscall.TCGETS, uintptr(unsafe.Pointer(&termios))); err != nil { + return fmt.Errorf("ioctl(tty, tcgets): %s", err.Error()) + } + + // Set -onlcr so we don't have to deal with \r. + termios.Oflag &^= syscall.ONLCR + + if err := ioctl(terminal.Fd(), syscall.TCSETS, uintptr(unsafe.Pointer(&termios))); err != nil { + return fmt.Errorf("ioctl(tty, tcsets): %s", err.Error()) + } + + return nil +} + func ioctl(fd uintptr, flag, data uintptr) error { if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, flag, data); err != 0 { return err