2022-02-17 16:44:49 +00:00
|
|
|
//go:build !windows
|
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package pty
|
|
|
|
|
|
|
|
import (
|
2022-08-23 18:19:57 +00:00
|
|
|
"fmt"
|
2022-02-17 16:44:49 +00:00
|
|
|
"os/exec"
|
2022-03-08 17:48:58 +00:00
|
|
|
"runtime"
|
2022-03-22 22:09:04 +00:00
|
|
|
"strings"
|
2022-02-17 16:44:49 +00:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/creack/pty"
|
2022-02-19 04:06:56 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-02-17 16:44:49 +00:00
|
|
|
)
|
|
|
|
|
2022-07-27 19:23:28 +00:00
|
|
|
func startPty(cmd *exec.Cmd) (PTY, Process, error) {
|
2022-02-17 16:44:49 +00:00
|
|
|
ptty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2022-02-19 05:13:32 +00:00
|
|
|
return nil, nil, xerrors.Errorf("open: %w", err)
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|
2022-08-23 18:19:57 +00:00
|
|
|
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_PTY=%s", tty.Name()))
|
2022-02-17 16:44:49 +00:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setsid: true,
|
|
|
|
Setctty: true,
|
|
|
|
}
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
cmd.Stdin = tty
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
_ = ptty.Close()
|
2022-08-05 18:31:54 +00:00
|
|
|
_ = tty.Close()
|
2022-03-22 22:09:04 +00:00
|
|
|
if runtime.GOOS == "darwin" && strings.Contains(err.Error(), "bad file descriptor") {
|
2022-08-12 01:22:06 +00:00
|
|
|
// macOS has an obscure issue where the PTY occasionally closes
|
2022-03-22 22:09:04 +00:00
|
|
|
// before it's used. It's unknown why this is, but creating a new
|
|
|
|
// TTY resolves it.
|
|
|
|
return startPty(cmd)
|
|
|
|
}
|
2022-02-19 05:13:32 +00:00
|
|
|
return nil, nil, xerrors.Errorf("start: %w", err)
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|
2022-02-19 05:13:32 +00:00
|
|
|
oPty := &otherPty{
|
2022-02-17 16:44:49 +00:00
|
|
|
pty: ptty,
|
|
|
|
tty: tty,
|
2022-02-19 05:13:32 +00:00
|
|
|
}
|
2022-07-27 19:23:28 +00:00
|
|
|
oProcess := &otherProcess{
|
|
|
|
pty: ptty,
|
|
|
|
cmd: cmd,
|
|
|
|
cmdDone: make(chan any),
|
|
|
|
}
|
|
|
|
go oProcess.waitInternal()
|
|
|
|
return oPty, oProcess, nil
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|