Skip to content

Commit

Permalink
Merge pull request opencontainers#1146 from cyphar/io-set-termios-onlcr
Browse files Browse the repository at this point in the history
libcontainer: io: stop screwing with \n in console output
  • Loading branch information
crosbymichael authored Nov 3, 2016
2 parents d7481c1 + fd7ab60 commit 5f24c9a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
26 changes: 26 additions & 0 deletions libcontainer/console_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func NewConsole(uid, gid int) (Console, 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
Expand Down Expand Up @@ -143,3 +146,26 @@ func ptsname(f *os.File) (string, error) {
}
return fmt.Sprintf("/dev/pts/%d", n), 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
}
8 changes: 7 additions & 1 deletion libcontainer/integration/execin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func TestExecIn(t *testing.T) {
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
t.Fatalf("unexpected running process, output %q", out)
}
if strings.Contains(out, "\r") {
t.Fatalf("unexpected carriage-return in output")
}
}

func TestExecInUsernsRlimit(t *testing.T) {
Expand Down Expand Up @@ -296,9 +299,12 @@ func TestExecInTTY(t *testing.T) {
waitProcess(process, t)

out := stdout.String()
if !strings.Contains(out, "cat") || !strings.Contains(string(out), "ps") {
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
t.Fatalf("unexpected running process, output %q", out)
}
if strings.Contains(out, "\r") {
t.Fatalf("unexpected carriage-return in output")
}
}

func TestExecInEnvironment(t *testing.T) {
Expand Down

0 comments on commit 5f24c9a

Please sign in to comment.