Documentation
¶
Overview ¶
Package pty starts a command in a pseudo-terminal the engine owns and ends the command's process group. The command leads its own session with the terminal as its controlling terminal, so an agent's program in it behaves as it would in a person's terminal; the engine reads what it writes and types into it. It works on Linux and macOS; elsewhere Start refuses.
It parses nothing it reads: what a terminal's output means is another package's concern.
Index ¶
Constants ¶
const ( DefaultCols = 120 DefaultRows = 40 )
The terminal's size when Options leaves it unset.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
// Command is the command line, run as /bin/sh -c Command.
Command string
// Dir is the directory the command starts in; empty means the engine's.
Dir string
// Env is the command's whole environment, as KEY=value lines. Nothing
// is taken from the engine's own: nil means an empty environment.
Env []string
// Cols and Rows are the terminal's size; 0 means DefaultCols and
// DefaultRows.
Cols, Rows uint16
}
Options says what to start and in what terminal.
type Process ¶
type Process struct {
// contains filtered or unexported fields
}
Process is a command running in a terminal the engine owns.
Who waits: exactly one caller calls Wait, which reaps the process and then closes exited. End signals the process group and watches exited, so End is meant to be called while another goroutine is blocked in Wait (or before Wait is called); it never reaps the process itself.
func Start ¶
Start starts o.Command in a new terminal and returns once the process is running. The process leads its own session and process group, with the terminal as its standard input, output and error and as its controlling terminal. The caller must call Wait once and Close once.
func (*Process) Close ¶
Close closes the engine's side of the terminal. A Read of Output that is blocked returns, and the processes still holding the terminal see it hang up. Close does not end or wait for the process.
func (*Process) End ¶
End ends the process group: it sends SIGHUP and SIGTERM to the group, then SIGKILL to the group if the process has not exited when grace has passed. It returns as soon as Wait has seen the process exit, or right after the SIGKILL; it does not reap the process, which is Wait's job. If no goroutine is in Wait, End cannot see the exit and sends SIGKILL after grace.
The group's id is the process's pid. Once the process has been reaped and every member of its group has exited, that id may be reused; End called then may signal a stranger's group, so callers end a process before, or while, they wait for it.
func (*Process) Output ¶
Output reads what the process writes to its terminal. It returns io.EOF once the process side of the terminal is closed: the command and every process that inherited the terminal have exited or closed it. After Close it returns an error.
func (*Process) Pid ¶
Pid is the command's process id. The command leads its own session and process group, so it is also the group's id.
func (*Process) Wait ¶
Wait waits for the process to exit and returns its exit code: the code it exited with, or 128 plus the signal's number when a signal ended it, as a shell reports it. The error is for a wait that failed, not for a non-zero code. Wait may be called once; a second call returns an error at once.