2022-02-17 16:44:49 +00:00
|
|
|
//go: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"
|
|
|
|
|
2022-02-19 04:06:56 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-02-17 16:44:49 +00:00
|
|
|
)
|
|
|
|
|
2022-09-12 16:27:51 +00:00
|
|
|
func startPty(cmd *exec.Cmd, opt ...StartOption) (retPTY *otherPty, proc Process, err error) {
|
|
|
|
var opts startOptions
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
opty, err := newPty(opts.ptyOpts...)
|
2022-02-17 16:44:49 +00:00
|
|
|
if err != nil {
|
2022-09-12 16:27:51 +00:00
|
|
|
return nil, nil, xerrors.Errorf("newPty failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
origEnv := cmd.Env
|
|
|
|
if opty.opts.sshReq != nil {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("SSH_TTY=%s", opty.Name()))
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|
2023-01-06 07:52:19 +00:00
|
|
|
if opty.opts.setGPGTTY {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("GPG_TTY=%s", opty.Name()))
|
|
|
|
}
|
2022-08-23 18:19:57 +00:00
|
|
|
|
2022-02-17 16:44:49 +00:00
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setsid: true,
|
|
|
|
Setctty: true,
|
|
|
|
}
|
2022-09-12 16:27:51 +00:00
|
|
|
cmd.Stdout = opty.tty
|
|
|
|
cmd.Stderr = opty.tty
|
|
|
|
cmd.Stdin = opty.tty
|
2022-02-17 16:44:49 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2022-09-12 16:27:51 +00:00
|
|
|
_ = opty.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.
|
2022-09-12 16:27:51 +00:00
|
|
|
cmd.Env = origEnv
|
|
|
|
return startPty(cmd, opt...)
|
2022-03-22 22:09:04 +00:00
|
|
|
}
|
2022-02-19 05:13:32 +00:00
|
|
|
return nil, nil, xerrors.Errorf("start: %w", err)
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|
2023-04-24 10:53:57 +00:00
|
|
|
// Now that we've started the command, and passed the TTY to it, close our
|
|
|
|
// file so that the other process has the only open file to the TTY. Once
|
|
|
|
// the process closes the TTY (usually on exit), there will be no open
|
|
|
|
// references and the OS kernel returns an error when trying to read or
|
|
|
|
// write to our PTY end. Without this, reading from the process output
|
|
|
|
// will block until we close our TTY.
|
|
|
|
if err := opty.tty.Close(); err != nil {
|
|
|
|
_ = cmd.Process.Kill()
|
|
|
|
return nil, nil, xerrors.Errorf("close tty: %w", err)
|
|
|
|
}
|
|
|
|
opty.tty = nil // remove so we don't attempt to close it again.
|
2022-07-27 19:23:28 +00:00
|
|
|
oProcess := &otherProcess{
|
2022-09-12 16:27:51 +00:00
|
|
|
pty: opty.pty,
|
2022-07-27 19:23:28 +00:00
|
|
|
cmd: cmd,
|
|
|
|
cmdDone: make(chan any),
|
|
|
|
}
|
|
|
|
go oProcess.waitInternal()
|
2022-09-12 16:27:51 +00:00
|
|
|
return opty, oProcess, nil
|
2022-02-17 16:44:49 +00:00
|
|
|
}
|