From 334d8e453b5d45ede5265debc51b4775195d9ce9 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 10 Nov 2022 16:02:48 -0500 Subject: internal/testpty: move from os/signal/internal/pty We're going to use this for another test, so make it more accessible. Preparation for #37486. Change-Id: If194cc4244c4b9e1b1f253759b813555b39ad67e Reviewed-on: https://go-review.googlesource.com/c/go/+/449502 Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills Run-TryBot: Austin Clements TryBot-Result: Gopher Robot --- src/internal/testpty/pty.go | 36 ++++++++++++++++++++++++++++++++++++ src/internal/testpty/pty_cgo.go | 34 ++++++++++++++++++++++++++++++++++ src/internal/testpty/pty_darwin.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/internal/testpty/pty.go create mode 100644 src/internal/testpty/pty_cgo.go create mode 100644 src/internal/testpty/pty_darwin.go (limited to 'src/internal') diff --git a/src/internal/testpty/pty.go b/src/internal/testpty/pty.go new file mode 100644 index 0000000000..88a47cf85f --- /dev/null +++ b/src/internal/testpty/pty.go @@ -0,0 +1,36 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build ((aix || dragonfly || freebsd || (linux && !android) || netbsd || openbsd) && cgo) || darwin + +// Package testpty is a simple pseudo-terminal package for Unix systems, +// implemented by calling C functions via cgo. +package testpty + +import ( + "fmt" + "os" + "syscall" +) + +type PtyError struct { + FuncName string + ErrorString string + Errno syscall.Errno +} + +func ptyError(name string, err error) *PtyError { + return &PtyError{name, err.Error(), err.(syscall.Errno)} +} + +func (e *PtyError) Error() string { + return fmt.Sprintf("%s: %s", e.FuncName, e.ErrorString) +} + +func (e *PtyError) Unwrap() error { return e.Errno } + +// Open returns a control pty and the name of the linked process tty. +func Open() (pty *os.File, processTTY string, err error) { + return open() +} diff --git a/src/internal/testpty/pty_cgo.go b/src/internal/testpty/pty_cgo.go new file mode 100644 index 0000000000..1db6a925af --- /dev/null +++ b/src/internal/testpty/pty_cgo.go @@ -0,0 +1,34 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build cgo && (aix || dragonfly || freebsd || (linux && !android) || netbsd || openbsd) + +package testpty + +/* +#define _XOPEN_SOURCE 600 +#include +#include +#include +*/ +import "C" + +import "os" + +func open() (pty *os.File, processTTY string, err error) { + m, err := C.posix_openpt(C.O_RDWR) + if err != nil { + return nil, "", ptyError("posix_openpt", err) + } + if _, err := C.grantpt(m); err != nil { + C.close(m) + return nil, "", ptyError("grantpt", err) + } + if _, err := C.unlockpt(m); err != nil { + C.close(m) + return nil, "", ptyError("unlockpt", err) + } + processTTY = C.GoString(C.ptsname(m)) + return os.NewFile(uintptr(m), "pty"), processTTY, nil +} diff --git a/src/internal/testpty/pty_darwin.go b/src/internal/testpty/pty_darwin.go new file mode 100644 index 0000000000..f29517c0e2 --- /dev/null +++ b/src/internal/testpty/pty_darwin.go @@ -0,0 +1,32 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package testpty + +import ( + "internal/syscall/unix" + "os" + "syscall" +) + +func open() (pty *os.File, processTTY string, err error) { + m, err := unix.PosixOpenpt(syscall.O_RDWR) + if err != nil { + return nil, "", ptyError("posix_openpt", err) + } + if err := unix.Grantpt(m); err != nil { + syscall.Close(m) + return nil, "", ptyError("grantpt", err) + } + if err := unix.Unlockpt(m); err != nil { + syscall.Close(m) + return nil, "", ptyError("unlockpt", err) + } + processTTY, err = unix.Ptsname(m) + if err != nil { + syscall.Close(m) + return nil, "", ptyError("ptsname", err) + } + return os.NewFile(uintptr(m), "pty"), processTTY, nil +} -- cgit v1.3