From a9bfb9ad8409fa4c80853d1881d12362271a19aa Mon Sep 17 00:00:00 2001 From: Marat Radchenko Date: Sat, 22 Jul 2023 20:03:20 +0300 Subject: [PATCH] Fix ptsname macOS implementation See #79 Signed-off-by: Marat Radchenko --- console_test.go | 4 ++-- tc_darwin.go | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/console_test.go b/console_test.go index 74695ab..8a69f9b 100644 --- a/console_test.go +++ b/console_test.go @@ -1,5 +1,5 @@ -//go:build linux || zos || freebsd -// +build linux zos freebsd +//go:build linux || zos || freebsd || darwin +// +build linux zos freebsd darwin /* Copyright The containerd Authors. diff --git a/tc_darwin.go b/tc_darwin.go index 7871545..9514671 100644 --- a/tc_darwin.go +++ b/tc_darwin.go @@ -17,8 +17,11 @@ package console import ( - "fmt" + "bytes" + "errors" "os" + "syscall" + "unsafe" "golang.org/x/sys/unix" ) @@ -36,9 +39,15 @@ func unlockpt(f *os.File) error { // ptsname retrieves the name of the first available pts for the given master. func ptsname(f *os.File) (string, error) { - n, err := unix.IoctlGetInt(int(f.Fd()), unix.TIOCPTYGNAME) - if err != nil { - return "", err + n := make([]byte, 128) + if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0]))); errno != 0 { + return "", errno } - return fmt.Sprintf("/dev/pts/%d", n), nil + + end := bytes.IndexByte(n, 0) + if end < 0 { + return "", errors.New("TIOCPTYGNAME string not NUL-terminated") + } + + return string(n[:end]), nil }