Types
type Attachment struct {
// Has unexported fields.
}
Attachment is one caller's view of the shell.
func (attachment *Attachment) ClearOutput() error
ClearOutput forgets the output kept to replay, so what has been cleared from
the screen does not come back with the next Attach. What already waits for
this reader still arrives.
func (attachment *Attachment) Detach()
Detach ends the attachment and leaves the shell running; the idle limit
starts if this was the last attachment to any shell. It does nothing to an
attachment that has already ended.
func (attachment *Attachment) Next() ([]byte, bool)
Next returns the output so far, waiting until there is some. It returns
false once the attachment has ended and its last output has been taken;
Reason and Result are valid after that.
func (attachment *Attachment) Reason() DetachReason
Reason says why the attachment ended; it is valid once Next has returned
false.
func (attachment *Attachment) Replayed() int
Replayed is how many bytes at the start of what Next first returns an
earlier attachment took. A terminal that shows them again must not answer
the questions in them again: the program that asked has its answer, and may
be gone.
func (attachment *Attachment) Resize(rows, cols uint16) error
Resize changes the terminal's size.
func (attachment *Attachment) Result() Result
Result is how the shell ended, when Reason is ShellExited.
func (attachment *Attachment) Write(input []byte) (int, error)
Write sends input to the shell. A tab that lost the shell cannot type into
it.
type CommandEntry struct {
Command string
// Exit is its exit status, or nil when the shell gave none.
Exit *int
StartedAt, EndedAt time.Time
}
CommandEntry is a command that a shell of a group ran and that ended.
type DetachReason int
DetachReason says why an Attachment ended.
const (
// DetachedByCaller means Detach was called.
DetachedByCaller DetachReason = iota
// TakenOver means a newer Attach replaced this one.
TakenOver
// ShellExited means the shell ended; Result says how.
ShellExited
// TooSlow means the reader took nothing for stallLimit. Attaching again
// replays recent output.
TooSlow
// KeeperClosed means the Keeper was closed.
KeeperClosed
)
type DirEntry struct {
Dir string
// EnteredAt is when one of them last went there.
EnteredAt time.Time
}
DirEntry is a directory that shells of a group have been in.
type Keeper struct {
// Has unexported fields.
}
Keeper holds shells, which outlive the attachments that show them: callers
come and go, and each new one sees recent output. A shell with nothing
attached sends its output only to its ring, so a program never waits.
An attached reader that falls behind makes the program wait, so it misses
nothing, unless it stalls.
A person watches one shell at a time, so a shell with nothing attached says
nothing about whether anyone is there. The idle limit therefore applies
to them together: it runs only while nothing is attached to any shell,
restarts on KeepAlive, and then ends them all.
func NewKeeper(options KeeperOptions) (*Keeper, error)
NewKeeper returns a Keeper with no shells yet.
func (keeper *Keeper) Attach(id string, rows, cols uint16) (*Attachment, error)
Attach shows the shell at the given size, and ends any earlier Attachment to
it with TakenOver. Next begins with the replay of recent output. A program
already running is made to redraw itself.
func (keeper *Keeper) AttachIfFree(id string, rows, cols uint16) (*Attachment, error)
AttachIfFree is Attach that leaves another attachment alone and returns
ErrAttachedElsewhere instead. It is for reconnecting by itself, which must
not take the shell from whoever is using it.
func (keeper *Keeper) Close()
Close ends the shells and refuses further ones.
func (keeper *Keeper) Commands(group string) []CommandEntry
Commands is the commands a group's shells ran and that ended, newest first,
up to 200, as the shells' marks said them; a shell that does not mark its
commands adds none. EndAll forgets every group's. Nothing of it is kept
outside memory.
func (keeper *Keeper) DefaultDir() string
DefaultDir is where a shell starts unless told otherwise.
func (keeper *Keeper) Dir(id string) (string, error)
Dir is a shell's working directory now. It attaches nothing, so it neither
takes the shell from an attachment nor counts as showing its output.
It returns ErrNoShell for a shell that is not running, and another error
when the directory cannot be found.
func (keeper *Keeper) End(id string) error
End ends a shell and returns once it has.
func (keeper *Keeper) EndAll()
EndAll ends every shell and returns once they have, and forgets every
group's history. Each may take a moment to hang up, so they end together
rather than in turn.
func (keeper *Keeper) Foreground(id string) (string, error)
Foreground is the command line of the program a shell runs in the
foreground, or "" while the shell is at its prompt. As Dir, it attaches
nothing, and returns ErrNoShell for a shell that is not running.
func (keeper *Keeper) History(group string) []DirEntry
History is the directories a group's shells have been in, newest first,
each once, up to 20. A shell's directory counts when it is back at its
prompt after Enter, and where it started counts too; one changed by a
program in front of the shell, such as a nested shell or ssh, does not.
A shell ending leaves the history, and EndAll forgets every group's. Nothing
of it is kept outside memory.
func (keeper *Keeper) KeepAlive()
KeepAlive says someone is still there, attached or not, and restarts the
idle limit.
func (keeper *Keeper) Limits() (shells, inGroup int)
Limits are how many shells may run at once, and how many of one group.
func (keeper *Keeper) Move(id, group string, set, unset []string) (ShellInfo, error)
Move puts a running shell in another group, after that group's shells,
with the lowest number free there and its name kept, and gives the shell the
variables set (NAME=VALUE) and unset (NAME) of its new place, all ROTARI_
variables, which rotari's prompt hook takes at the shell's next prompt; what
is already running keeps its own. Moving to the shell's own group changes
nothing. Directories the shell goes to from then on are its new group's;
those it went to stay its old group's. It returns ErrNoShell, ErrCannotMove
for a shell that does not follow moves, ErrTooManyInGroup at the new group's
limit, and an error for a variable that is not a ROTARI_ one or a value
holding a line break or NUL.
func (keeper *Keeper) Processes(id string) ([]Process, error)
Processes are the processes below a shell, as Session.Descendants lists
them. As Dir, it attaches nothing, and returns ErrNoShell for a shell that
is not running.
func (keeper *Keeper) Rename(id, name string) (ShellInfo, error)
Rename sets a shell's name, which means nothing to the Keeper.
func (keeper *Keeper) Reorder(ids []string)
Reorder arranges the group of the first running shell it is given in the
order given. Shells of that group it does not name follow, as they were,
and it ignores shells that have ended or are of another group, since a
page's list can be a moment behind.
func (keeper *Keeper) Shells() []ShellInfo
Shells lists the running shells, by group, and within one in the order
arranged.
func (keeper *Keeper) Start(start StartOptions) (ShellInfo, error)
Start starts a shell at the given size. A shell nobody attaches to falls
under the idle limit, which starting one restarts.
type KeeperOptions struct {
// Shell starts each shell; its Rows and Cols are ignored, since Start gives
// the size.
Shell Options
// IdleLimit is how long shells run with nothing attached to any of them; 0 means
// they run until ended, however long nothing is attached.
IdleLimit time.Duration
// RingSize is how many bytes of recent output, per shell, a new Attach replays.
RingSize int
// ShellLimit is how many shells may run at once.
ShellLimit int
// GroupLimit is how many shells of one group may run at once.
GroupLimit int
// Log, if set, is told when a shell starts and ends, and never what it shows.
Log func(message string)
// ShellIntegration starts bash and zsh so that they mark their prompts and commands
// (see WriteIntegration), from files the Keeper writes now: in the user's cache, kept,
// or else in a temporary directory, removed on Close.
ShellIntegration bool
}
KeeperOptions says how a Keeper runs its shells.
type LastCommand struct {
// Command is its command line, as the shell said it in C, or "" when it said none.
Command string
// Exit is its exit status, or nil when the shell gave none (no D, or one without an
// integer).
Exit *int
// StartedAt is when its C passed, and EndedAt when its D, or the next prompt, did.
StartedAt, EndedAt time.Time
// Sequence counts the shell's ended commands, this one included, so that a reader can
// tell a new one.
Sequence int
}
LastCommand is how a shell's last marked command ended.
type Options struct {
// Shell is the program to run, as a login shell.
Shell string
// Integration, if set, is a directory WriteIntegration filled: bash and zsh then
// start with its files, which read the user's and mark prompts and commands.
Integration string
// Dir is the working directory.
Dir string
// Env is the shell's whole environment; nothing is inherited, so a caller
// cannot pass on a secret by leaving it out. TERM is always set.
Env []string
// Rows and Cols are the terminal's first size, both above zero.
Rows, Cols uint16
}
Options says how to start a shell.
type Process struct {
PID, PPID int
// Depth is how far below the shell it is: 1 for the shell's own children.
Depth int
Command string
// Has unexported fields.
}
Process is a process a shell started, directly or through others.
type Result struct {
// ExitCode is the shell's exit status, or -1 when a signal ended it.
ExitCode int
// Signal is the signal that ended the shell, or 0.
Signal syscall.Signal
}
Result says how a shell ended.
func (result Result) String() string
String describes how the shell ended.
type Session struct {
// Has unexported fields.
}
Session is a started shell and its terminal. Read and Write may run at the
same time as each other and as Resize and Close.
func Start(options Options) (*Session, error)
Start starts a shell in a session of its own, on a new terminal.
func (session *Session) Close()
Close closes the terminal, which sends the shell and its foreground program
SIGHUP, and returns once the shell has exited. A shell still running after
closeGrace is killed with its process group. What has left the shell's
session or ignores SIGHUP, such as a nohup command or a rotari supervisor,
keeps running, as after closing any terminal.
func (session *Session) Descendants() ([]Process, error)
Descendants are the processes below the shell, its children first, each
followed by its own. A process that left the shell's tree, as a daemon does,
is not among them.
func (session *Session) Dir() (string, error)
Dir is the current shell's working directory now, which programs it runs do
not change: the foreground group's leader if that is an interactive shell,
else the nearest one above it, or else the session's shell.
func (session *Session) Done() <-chan struct{}
Done is closed when the shell has exited.
func (session *Session) Foreground() (string, error)
Foreground is the command line of the program the current shell runs in
the foreground, or "" while it is at its prompt. A pipeline gives its first
program.
func (session *Session) Read(buffer []byte) (int, error)
Read reads what the shell and its programs wrote. It returns io.EOF once the
terminal has closed or nothing still has it open; Linux reports the latter
as EIO, which is not an error here.
func (session *Session) Resize(rows, cols uint16) error
Resize changes the terminal's size, which tells the running program.
func (session *Session) Result() Result
Result is how the shell ended; it is valid once Done is closed.
func (session *Session) Write(input []byte) (int, error)
Write sends input to the shell, as typing would.
type ShellActivity struct {
PID int
Foreground string
Background []Process
}
ShellActivity is what runs in one of a server's shells, seen from outside
the server: Foreground is the command holding the shell's terminal,
empty while the shell is at its prompt, and Background the shell's other
processes.
func ServerShells(serverPID int) ([]ShellActivity, error)
ServerShells describes the shells of the server with the given PID from the
process table, for a process that cannot ask the server, such as rotari web
--shutdown. A shell is a child of the server that leads its own session on
a terminal, as a Keeper starts it; other children, such as launches, are not
shells. A failure to read the process table is an error, never "no shells".
func (activity ShellActivity) Busy() bool
Busy says something runs in the shell.
type ShellInfo struct {
// ID names this shell for as long as the Keeper lasts, and is never reused, so
// that a stale reference cannot reach a later shell.
ID string
// Number is what a person calls it within its group: the lowest not in use when
// it started.
Number int
// Name is what someone has called it, if anyone has.
Name string
Group string
// Dir is where it started.
Dir string
// UnseenOutput says it has printed something that no attachment has shown since one
// last did. A shell nothing has ever shown does not count: it is only starting.
UnseenOutput bool
// CommandFinished says a command the shell ran ended, back at its prompt, while no
// attachment showed it, and none has since.
CommandFinished bool
// Running says a command holds the shell's foreground as Shells looks, rather than
// its prompt; false when that cannot be told.
Running bool
// Program is the name of the program running then, without its arguments, or "" when
// none is or it is not known.
Program string
// StartedAt is when the shell started, and ActiveAt when it last printed something or
// was typed into, or started; the time since ActiveAt is how long it has been idle.
StartedAt, ActiveAt time.Time
// LastCommand is how its last command ended, read from the marks it prints, or nil
// before one has.
LastCommand *LastCommand
// FollowsMoves says Move can move it: it runs rotari's prompt hook, which takes the
// new place's variables.
FollowsMoves bool
}
ShellInfo describes a running shell.
type StartOptions struct {
Rows, Cols uint16
// Group is what the shell belongs to, which means nothing to the Keeper beyond
// its numbers and limit.
Group string
// Dir is where it starts, instead of the Keeper's Shell.Dir.
Dir string
// Banner is shown before the shell's own output, and replayed with it.
Banner string
// Env is added to the Keeper's Shell.Env, overriding what is there.
Env []string
// Unset names variables of the Keeper's Shell.Env that this shell does not get.
Unset []string
}
StartOptions say how to start one shell.