summaryrefslogtreecommitdiffstats
path: root/src/internal/testpty
diff options
context:
space:
mode:
Diffstat (limited to 'src/internal/testpty')
-rw-r--r--src/internal/testpty/pty.go38
-rw-r--r--src/internal/testpty/pty_cgo.go34
-rw-r--r--src/internal/testpty/pty_darwin.go32
-rw-r--r--src/internal/testpty/pty_none.go13
4 files changed, 117 insertions, 0 deletions
diff --git a/src/internal/testpty/pty.go b/src/internal/testpty/pty.go
new file mode 100644
index 0000000..f0b2a33
--- /dev/null
+++ b/src/internal/testpty/pty.go
@@ -0,0 +1,38 @@
+// 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 is a simple pseudo-terminal package for Unix systems,
+// implemented by calling C functions via cgo.
+package testpty
+
+import (
+ "errors"
+ "fmt"
+ "os"
+)
+
+type PtyError struct {
+ FuncName string
+ ErrorString string
+ Errno error
+}
+
+func ptyError(name string, err error) *PtyError {
+ return &PtyError{name, err.Error(), err}
+}
+
+func (e *PtyError) Error() string {
+ return fmt.Sprintf("%s: %s", e.FuncName, e.ErrorString)
+}
+
+func (e *PtyError) Unwrap() error { return e.Errno }
+
+var ErrNotSupported = errors.New("testpty.Open not implemented on this platform")
+
+// Open returns a control pty and the name of the linked process tty.
+//
+// If Open is not implemented on this platform, it returns ErrNotSupported.
+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 0000000..442fbcf
--- /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 m < 0 {
+ return nil, "", ptyError("posix_openpt", err)
+ }
+ if res, err := C.grantpt(m); res < 0 {
+ C.close(m)
+ return nil, "", ptyError("grantpt", err)
+ }
+ if res, err := C.unlockpt(m); res < 0 {
+ 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 0000000..f29517c
--- /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
+}
diff --git a/src/internal/testpty/pty_none.go b/src/internal/testpty/pty_none.go
new file mode 100644
index 0000000..4f9e2b7
--- /dev/null
+++ b/src/internal/testpty/pty_none.go
@@ -0,0 +1,13 @@
+// Copyright 2022 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)) && !darwin
+
+package testpty
+
+import "os"
+
+func open() (pty *os.File, processTTY string, err error) {
+ return nil, "", ErrNotSupported
+}