Documentation
¶
Index ¶
- Variables
- type Job
- type Manager
- type Shell
- func (s *Shell) BashPID() int
- func (s *Shell) Close()
- func (s *Shell) Current() *Job
- func (s *Shell) DeathCh() <-chan struct{}
- func (s *Shell) IsDead() bool
- func (s *Shell) LastActivity() time.Time
- func (s *Shell) Run(cmd string) (*Job, error)
- func (s *Shell) SigInt() error
- func (s *Shell) SigKill() error
- type SpawnOpts
Constants ¶
This section is empty.
Variables ¶
var ErrBusy = errors.New("shell busy")
Functions ¶
This section is empty.
Types ¶
type Job ¶
type Job struct {
Cmd string
Started time.Time
Done chan int
// MaxBytes caps the output buffer. On overflow, Write stops
// appending and flips `truncated`. A zero value means unlimited.
MaxBytes int
// contains filtered or unexported fields
}
Job tracks one running command and accumulates its output until the shell prints its next prompt (the sentinel).
func (*Job) SetTermination ¶
SetTermination records the reason a command ended (e.g. "canceled", "killed", "timeout"). First write wins to avoid watchdog/manual races.
func (*Job) Termination ¶
Termination returns the last-set termination reason, or "completed" if none was set before finish().
func (*Job) Write ¶
Write appends p to the job's buffer, respecting MaxBytes. If writing all of p would exceed the cap, any prefix that still fits is written, the truncated flag is set, and the remainder is dropped. Returns whether the buffer was (or just became) truncated — callers use this to trigger an async SIGKILL on the foreground process.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
func NewManager ¶
NewManager constructs a shell manager.
- idleReap: shells idle longer than this are killed on the next sweep.
- maxOutputBytes: per-command output cap in bytes. 0 = unlimited.
- log: zap logger; nil = nop.
func (*Manager) Get ¶
Get returns (or lazily spawns) the shell for userID. When a new shell is spawned, `opts` determines its credentials / cwd / env. Once a shell exists for this user, opts is ignored for subsequent Get calls (the existing shell is returned). Use Reset() to drop a shell so the next Get respawns with fresh opts.
func (*Manager) SetShellGoneHook ¶
SetShellGoneHook registers a callback fired with userID every time a shell is torn down by Reset or the idle reaper. Safe to call at most once during startup. The hook runs outside the manager's internal lock; it must not call back into Manager.
func (*Manager) StartReaper ¶
type Shell ¶
type Shell struct {
UserID int64
// contains filtered or unexported fields
}
Shell wraps a bash process in a pty. Command-boundary signalling (ready + per-command exit code) travels on a dedicated control pipe inherited by bash as fd 3 — not embedded in pty stdout. Nothing the user's command prints to stdout is parsed by the bot; command output and control are fully separated channels.
func (*Shell) LastActivity ¶
func (*Shell) Run ¶
Run writes cmd to the pty. Returns a Job whose Done fires with the exit code when bash's PROMPT_COMMAND emits `done:<exit>` on the control pipe (fd 100) — i.e. as bash returns to its next prompt after finishing the dispatched command.
type SpawnOpts ¶
type SpawnOpts struct {
// Creds, when non-nil, runs bash as a different Unix user/group.
// Caller is responsible for ensuring Dir/HOME is accessible under
// those credentials.
Creds *syscall.Credential
// Dir sets bash's working directory.
Dir string
// Env adds (or overrides) entries to bash's environment beyond the
// shellboto-essential set (PS1=, PS2=, etc).
Env []string
}
SpawnOpts overrides the defaults for spawning a shell. A zero-value SpawnOpts reproduces the pre-existing behavior (current process's credentials, default cwd, default env).