Skip to content

Commit

Permalink
Fix ptsname() for big-endian architectures (again)
Browse files Browse the repository at this point in the history
This reverts commit f1b333f (Use Ioctl{SetPointerInt,GetInt} from
golang.org/x/sys/unix on linux), changing the code to what it was after
commit dbd69c5. This fixes a blocker bug on s390.

The original description from commit dbd69c5 follows:

    On big-endian architectures unix.IoctlGetInt() leads to a wrong result
    because a 32 bit value is stored into a 64 bit buffer. When dereferencing
    the result is left shifted by 32. Without this patch ptsname() returns
    a wrong path from the second pty onwards.
    To protect syscalls against re-arranging by the GC the conversion from
    unsafe.Pointer to uintptr must occur in the Syscall expression itself.
    See the documentation of the unsafe package for details.

Cc: Peter Morjan <[email protected]>
Cc: Tobias Klauser <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Apr 6, 2021
1 parent 68336a7 commit a261251
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package console
import (
"fmt"
"os"
"unsafe"

"golang.org/x/sys/unix"
)
Expand All @@ -31,13 +32,17 @@ const (
// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
// unlockpt should be called before opening the slave side of a pty.
func unlockpt(f *os.File) error {
return unix.IoctlSetPointerInt(int(f.Fd()), unix.TIOCSPTLCK, 0)
var u int32
if _, _, err := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))); err != 0 {
return err
}
return nil
}

// ptsname retrieves the name of the first available pts for the given master.
func ptsname(f *os.File) (string, error) {
u, err := unix.IoctlGetInt(int(f.Fd()), unix.TIOCGPTN)
if err != nil {
var u uint32
if _, _, err := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.TIOCGPTN, uintptr(unsafe.Pointer(&u))); err != 0 {
return "", err
}
return fmt.Sprintf("/dev/pts/%d", u), nil
Expand Down

0 comments on commit a261251

Please sign in to comment.