shell

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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) BytesOut

func (j *Job) BytesOut() int

func (*Job) Duration

func (j *Job) Duration() time.Duration

func (*Job) ExitCode

func (j *Job) ExitCode() int

func (*Job) SetTermination

func (j *Job) SetTermination(r string)

SetTermination records the reason a command ended (e.g. "canceled", "killed", "timeout"). First write wins to avoid watchdog/manual races.

func (*Job) Snapshot

func (j *Job) Snapshot() ([]byte, uint64)

Snapshot returns a copy of the accumulated output and the current version.

func (*Job) Termination

func (j *Job) Termination() string

Termination returns the last-set termination reason, or "completed" if none was set before finish().

func (*Job) Truncated

func (j *Job) Truncated() bool

Truncated reports whether the output cap was hit for this job.

func (*Job) Write

func (j *Job) Write(p []byte) (truncated bool)

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

func NewManager(idleReap time.Duration, maxOutputBytes int, log *zap.Logger) *Manager

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) CloseAll

func (m *Manager) CloseAll()

func (*Manager) Get

func (m *Manager) Get(userID int64, opts SpawnOpts) (*Shell, error)

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) Reset

func (m *Manager) Reset(userID int64)

func (*Manager) SetShellGoneHook

func (m *Manager) SetShellGoneHook(hook func(int64))

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

func (m *Manager) StartReaper(ctx context.Context)

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) BashPID

func (s *Shell) BashPID() int

func (*Shell) Close

func (s *Shell) Close()

Close kills bash and releases the pty + control pipe.

func (*Shell) Current

func (s *Shell) Current() *Job

func (*Shell) DeathCh

func (s *Shell) DeathCh() <-chan struct{}

func (*Shell) IsDead

func (s *Shell) IsDead() bool

func (*Shell) LastActivity

func (s *Shell) LastActivity() time.Time

func (*Shell) Run

func (s *Shell) Run(cmd string) (*Job, error)

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.

func (*Shell) SigInt

func (s *Shell) SigInt() error

SigInt writes Ctrl+C to the pty; tty line discipline forwards SIGINT to the foreground process group.

func (*Shell) SigKill

func (s *Shell) SigKill() error

SigKill sends SIGKILL to the foreground process group of the pty. Refuses to signal bash itself.

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL