Documentation
¶
Overview ¶
Package ptyhost is Lectern's own terminal keeper: one small process per user per machine that owns pseudo-terminals, so agents and shells outlive the Lectern server, its restarts and its upgrades, with nothing but the lectern binary installed (docs/ptyhost.md).
The host keeps each session's screen in a terminal emulator, so a client can read it back (capture), and any number of clients can attach to the live stream. Clients reach it over a Unix socket (an AF_UNIX socket on Windows too) in a directory only the user can enter.
Index ¶
- Constants
- Variables
- func AttachTerminal(cl *Client, name string, in, out *os.File) error
- func Command(args []string) int
- func HostCommand(args []string, build string) int
- func IsNotRunning(err error) bool
- func Serve(opts Options) error
- func SocketPath() (string, error)
- type CLI
- type Client
- type Host
- type Info
- type MissingError
- type Options
- type Probe
- type Process
- type Request
- type Response
- type Session
- type Stream
Constants ¶
const ( Protocol = 1 MinProtocol = 1 )
Protocol is the version this build speaks; MinProtocol the oldest it still accepts. New operations are additive and announced in the handshake, so a newer client can keep using an older host for everything that host knows.
const DefaultHistory = 5000
DefaultHistory is how many lines of scrollback a session keeps unless LECTERN_PTY_HISTORY says otherwise.
const PollEnd = "ADK-POLL-END-v2"
PollEnd ends a batched poll or probe; it is the framing of internal/sessions/backend, and a test there keeps the two equal.
const SocketEnv = "LECTERN_PTYHOST_SOCKET"
SocketEnv overrides where the host listens (tests, several instances).
const TestGuardEnv = "LECTERN_PTYHOST_TEST_GUARD"
TestGuardEnv marks a process started by a test, directly or through any number of children. Under it the default socket is refused: a test names its own (SocketEnv), so it never starts, reaches or leaves behind a host on the user's real socket — where a running Lectern server would count its sessions as its own. The test runners set it; every Go test binary sets it for itself below, so a `go test` run by hand is guarded too.
Variables ¶
var ErrDefaultSocketInTest = errors.New("refusing the default PTY host socket in a test: set " + SocketEnv + " to a private path")
ErrDefaultSocketInTest is SocketPath's answer under TestGuardEnv when no socket was named.
var ErrRunning = errors.New("a PTY host is already running for this socket")
ErrRunning means another host already owns the socket.
Functions ¶
func AttachTerminal ¶
AttachTerminal shows a session in this process's terminal until the session ends, the host goes away, or the user detaches.
func Command ¶
Command is `lectern pty …`, the tmux-language client of this user's PTY host (docs/md).
func HostCommand ¶
HostCommand is `lectern ptyhost serve|status|stop`; build labels the host.
func IsNotRunning ¶
IsNotRunning reports whether err means no host is listening.
func Serve ¶
Serve runs a host until it is idle or told to stop. It returns ErrRunning without touching the socket when another host holds the lock.
func SocketPath ¶
SocketPath is where this user's PTY host listens on this machine.
The directory is private to the user: the socket is a raw input channel into every session, with the user's own permissions behind it.
Types ¶
type CLI ¶
type CLI struct {
Socket string
// Bin is the lectern binary that starts a host when a session is made.
Bin string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// Attach runs an interactive attachment; the command layer supplies it
// because it needs the real terminal.
Attach func(c *Client, name string) error
}
CLI is `lectern pty …`: the subset of tmux's command language Lectern's session code uses, answered by the PTY host, with tmux's output and error texts (docs/ptyhost.md §3).
type Client ¶
type Client struct {
Hello hello
// contains filtered or unexported fields
}
Client is one connection to a host.
func Ensure ¶
Ensure connects to the host on socket, starting one from bin when none is running. Only a caller that is about to create a session should start one; everything else treats "not running" as "no sessions".
func (*Client) Attach ¶
Attach turns the connection into a stream of the named session. The size, when given, becomes the session's size.
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host owns the sessions of one user on one machine.
type Info ¶
type Info struct {
Name string `json:"name"`
ID int `json:"id"`
Created int64 `json:"created"`
Activity int64 `json:"activity"`
PID int `json:"pid"`
TTY string `json:"tty,omitempty"`
Cwd string `json:"cwd,omitempty"`
Current string `json:"current,omitempty"` // the foreground command
StartDir string `json:"start_dir,omitempty"`
Cols int `json:"cols"`
Rows int `json:"rows"`
Clients int `json:"clients"`
Options map[string]string `json:"options,omitempty"`
Env []string `json:"env,omitempty"`
}
Info describes one session.
type MissingError ¶
type MissingError struct{ Name string }
MissingError is tmux's "can't find session".
func (*MissingError) Error ¶
func (e *MissingError) Error() string
type Options ¶
type Options struct {
Socket string
// History is the scrollback per session; 0 means DefaultHistory.
History int
// Idle is how long a host with no sessions and no clients waits before
// exiting; 0 means ten seconds.
Idle time.Duration
Build string
Log *slog.Logger
}
Options configure a host.
type Probe ¶
type Probe struct {
Name string `json:"name"`
Found bool `json:"found"`
RootArgs string `json:"root_args,omitempty"`
Current string `json:"current,omitempty"`
TTYArgs []string `json:"tty_args,omitempty"`
}
Probe is what the agent-exit probe needs about one session: the arguments of its root process and its foreground command, and the arguments of every process on its terminal.
type Process ¶
type Process struct {
// contains filtered or unexported fields
}
Process is one program on a pseudo-terminal of its own, with no screen model and no host: what the web terminal runs for an attachment that is not a PTY-host session (a `tmux attach`, an ssh to another machine).
func StartProcess ¶
StartProcess starts argv on a new pseudo-terminal, in this process's environment with TERM set for an xterm-compatible client.
func (*Process) Close ¶
Close hangs the terminal up, as a closed terminal window does, and kills whatever is left of the program a moment later.
func (*Process) Done ¶
func (p *Process) Done() <-chan struct{}
Done closes when the program has exited.
type Request ¶
type Request struct {
Op string `json:"op"`
Name string `json:"name,omitempty"`
// new: Argv runs as it is; Shell, when Argv is empty, is one command
// line for the host's shell.
Argv []string `json:"argv,omitempty"`
Shell string `json:"shell,omitempty"`
Dir string `json:"dir,omitempty"`
Env []string `json:"env,omitempty"` // the program's whole environment
SessionEnv []string `json:"session_env,omitempty"` // -e entries, read back by show-environment
Cols int `json:"cols,omitempty"`
Rows int `json:"rows,omitempty"`
// write: Data is typed as it is; with Paste it is bracketed when the
// program asked for bracketed paste, as tmux's paste-buffer -p does.
Data []byte `json:"data,omitempty"`
Paste bool `json:"paste,omitempty"`
// capture
Lines int `json:"lines,omitempty"`
// set-option
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
OnlyIfUnset bool `json:"only_if_unset,omitempty"`
// info, list: Detail also asks for the foreground process and the
// working directory, which can cost a process-table read per session.
Detail bool `json:"detail,omitempty"`
// probe
Names []string `json:"names,omitempty"`
// shutdown
Force bool `json:"force,omitempty"`
}
Request is one operation. Unused fields are omitted.
type Response ¶
type Response struct {
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
Missing bool `json:"missing,omitempty"` // no such session
Info *Info `json:"info,omitempty"`
Sessions []Info `json:"sessions,omitempty"`
Text string `json:"text,omitempty"`
Set bool `json:"set,omitempty"`
Probes []Probe `json:"probes,omitempty"`
}
Response answers a Request.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one program on one pseudo-terminal.
type Stream ¶
type Stream struct {
// Exited is set once the host said the session ended.
Exited bool
// contains filtered or unexported fields
}
Stream is an attached session.
func (*Stream) Read ¶
Read returns terminal output. It returns io.EOF when the session ends or the host goes away.