aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2022-11-10 16:02:48 -0500
committerAustin Clements <austin@google.com>2022-11-16 19:00:19 +0000
commit334d8e453b5d45ede5265debc51b4775195d9ce9 (patch)
tree5618db8d56d563e9c52f62b28ece838684116fcb /src/internal
parent80d8c77a43f2445a78bd5b5c3b113c95e49766a9 (diff)
downloadgo-334d8e453b5d45ede5265debc51b4775195d9ce9.tar.xz
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 <iant@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Austin Clements <austin@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/testpty/pty.go36
-rw-r--r--src/internal/testpty/pty_cgo.go34
-rw-r--r--src/internal/testpty/pty_darwin.go32
3 files changed, 102 insertions, 0 deletions
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 <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+*/
+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
+}