Documentation
¶
Overview ¶
Package execsession keeps a long exec running on a Unix server after the connection drops: a session folder on the server, its ID, the record on this computer, and the shell text that starts, reads and stops it (vault note 2026-11-24-komando-exec-session-and-winrm-design). The shell text is komando's own; the only value from outside that goes into it is an ID that passed CheckID.
Index ¶
- Constants
- Variables
- func BeginScript(s Start, recs []*store.ExecSession) (string, error)
- func CheckID(id string) error
- func CheckSignal(sig string) error
- func Feed(pw bool) string
- func FolderScript(id string) (string, error)
- func ForStop(rt *store.Runtime, host, id string) (*store.ExecSession, error)
- func NewID() (string, error)
- func ReadScript(id string, from int64) (string, error)
- func RemoveExit(host, id string, exit int) error
- func RemoveScript(id string, rec *store.ExecSession) (string, error)
- func Signalled(stdout string) string
- func StartScript(s Start) (string, error)
- func StopCommand(rec *store.ExecSession, sig string, password bool) (string, error)
- func StopExit(host, id string, exit int, stderr string) error
- func StopScript(rec *store.ExecSession, sig string) (string, error)
- func SudoGuard(inner string, timeout time.Duration) string
- func Sweep(recs []*store.ExecSession) string
- func Swept(stdout, id string) []string
- type Facts
- type Follow
- type Followed
- type Lines
- type Reader
- type Start
- type Status
Constants ¶
const ( ExitUnsafe = 90 ExitNoFolder = 91 )
Exit codes of the folder prelude, apart from any a command gives: ExitUnsafe refuses the start (a link, a file, a folder not the login user's, or a session folder that already exists); ExitNoFolder means no folder can be made there (a read-only or full home), and exec runs the command without a session.
const ( ReadMax = 1 << 20 LineMax = 64 << 10 )
ReadMax is the most of out.log one read takes; LineMax is the longest line an agent is shown.
const ExitGone = 95
ExitGone is the read script's exit when the session folder or its log is not there.
const ExitNoGroup = 94
ExitNoGroup is the start script's exit when the run would not lead its own process group (setsid gave none, or the root process never came up): nothing of the command ran, and the folder is gone.
const ExitNoSpace = 93
ExitNoSpace is the start script's exit when the home file system has less free space than the log cap plus 5% of the disk.
const ExitNotRunning = 92
ExitNotRunning is the stop script's exit when the stored process is not the live one: gone, or its PID reused by another process.
const ExitStillRunning = 96
ExitStillRunning is the stop script's exit when the run's root is still alive after the wait: the signal was sent, and the run goes on.
const FactsScript = `ks_start() { if [ -r "/proc/$1/stat" ]; then ` +
`t=$(sed 's/.*) //' "/proc/$1/stat" 2>/dev/null | cut -d' ' -f20); [ -z "$t" ] || echo "proc $t"; ` +
`else t=$(LC_ALL=C ps -o lstart= -p "$1" 2>/dev/null | sed 's/^ *//;s/ *$//'); [ -z "$t" ] || echo "ps $t"; fi; }; ` +
`ks_pgid() { if [ -r "/proc/$1/stat" ]; then sed 's/.*) //' "/proc/$1/stat" 2>/dev/null | cut -d' ' -f3; ` +
`else ps -o pgid= -p "$1" 2>/dev/null | tr -d ' '; fi; }; ` +
`ks_facts() { echo "facts-begin $2"; echo "pid $1"; echo "pgid $(ks_pgid "$1")"; echo "start $(ks_start "$1")"; echo "facts-end $2"; }`
FactsScript defines two shell functions:
ks_start PID prints the process's start, or nothing when it is gone
ks_pgid PID prints the process's group
ks_facts PID ID prints the lines ParseFacts reads, between markers
that carry the session ID
ks_start reads field 22 of /proc/PID/stat and ks_pgid field 5, counted after the last ")" because the command name may hold spaces; without /proc they ask ps (in the C locale for the start). busybox ps has no -p, but busybox systems have /proc.
const Retention = 7 * 24 * 60 * 60
Retention is how long a finished session stays when nobody removes it.
const Root = ".komando/sessions"
Root is the folder that holds the sessions, below the login user's home.
const TreeScript = `ks_tree() { { ps -A -o pid= -o ppid= 2>/dev/null || ps -o pid,ppid 2>/dev/null; } | ` +
`awk -v r="$1" '$1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ { c[$2] = c[$2] " " $1 } ` +
`END { s = r; while (s != "") { n = split(s, a, " "); s = ""; for (i = 1; i <= n; i++) if (a[i] in c) { s = s c[a[i]]; o = o c[a[i]]; delete c[a[i]] } } print o }'; }`
TreeScript defines ks_tree PID, which prints the PIDs of every process below PID, from one ps listing. busybox ps has no -A and lists every process without it. A caller never asks for its own ancestors' tree: ps and awk would be in it, and their PIDs free again by the signal.
Variables ¶
var ErrNoFolder = errors.New("no session folder can be made in the login user's home")
ErrNoFolder: no session folder can be made on the server (a read-only or full home). The start ran nothing of the command, but it read the input, so a run without a session sends the input again in a new call. Only this exit falls back: every other refusal stands.
var Signals = []string{"TERM", "KILL"}
Signals a stop may send. A run in a session runs in the background with no job control, so it ignores INT and QUIT, and nohup makes it ignore HUP.
Functions ¶
func BeginScript ¶
func BeginScript(s Start, recs []*store.ExecSession) (string, error)
BeginScript is a starting exec's one call: the sweep, between markers that carry the new session's ID, then StartScript.
func CheckID ¶
CheckID refuses anything but an ID NewID could make, before it goes into a path or shell text: an ID an agent passes in is outside input.
func CheckSignal ¶
CheckSignal refuses a signal a stop does not send.
func Feed ¶
Feed sends a run's env and input to its command, in that order, deleting each file once it is sent; a sudo password goes first (pw). The files are in $d.
func FolderScript ¶
FolderScript is the shell text that makes the session folder and sets $d to it: umask 077; ~/.komando and its sessions folder must be folders of the login user, not links. One that others can write is refused (someone may have put anything there); one others can only read is made private, with a "warning" line, since earlier logs may have been read. The session folder is made new (mkdir without -p). Never /tmp.
func ForStop ¶
ForStop finds the record a stop works from. The record on this computer is the only source: the session folder on the server belongs to the running command, which could rewrite the pid and start written there.
func NewID ¶
NewID makes a session ID: 256 random bits as 52 lowercase base32 letters. A failed random read is an error, never a weaker ID.
func ReadScript ¶
ReadScript is the shell text that reads a session from byte from on: it checks on every read that ~/.komando, sessions, the session folder and its files are the login user's and not links (exit ExitUnsafe; a missing one exits ExitGone), prints the status between markers that carry the ID and then at most ReadMax bytes of out.log. The exit file is read before the size: the wrapper writes it only after the log is complete. It deletes nothing.
func RemoveExit ¶
RemoveExit is the error for a remove's exit code: nil for 0. ExitNotRunning is SESSION_BUSY (stop it first); a folder that is not there or not safe is the same error as for a read.
func RemoveScript ¶
func RemoveScript(id string, rec *store.ExecSession) (string, error)
RemoveScript is the shell text that deletes one finished session: the folder and the two above it must be the login user's and not links (ExitUnsafe), and the run must have ended (ExitNotRunning). With rec, the record on this computer, the run has ended only when its stored process is gone, whatever files the run wrote; without it, the files decide.
func Signalled ¶
Signalled reads which a stop signalled: "group" or "tree", or "" when it printed neither.
func StartScript ¶
StartScript is the one shell call that starts a run in a session: it checks free space, makes the folder (FolderScript), saves the input in private files, starts the wrapper under setsid nohup (plain nohup where there is no setsid) and prints the run's facts (FactsScript). Its exit is ExitNoSpace, ExitUnsafe or ExitNoFolder when it starts nothing.
func StopCommand ¶
StopCommand is StopScript run as the run ran: a run under sudo is stopped through sudo as the same user, and the start check runs inside that same sudo call. With a password (the host's sudo reference, sent as the command's input) sudo is first asked whether it needs one. Around it, as the login user: the killed mark before (markStopped), the wait for the run's ending after (awaitEnding).
func StopExit ¶
StopExit is the error for a stop's exit code: nil for 0. ExitNotRunning is SESSION_PROCESS_MISSING: the stored process ended, or its PID belongs to another process now. ExitStillRunning is SESSION_STILL_BUSY: the signal was sent and the run goes on. Any other is SESSION_STOP_FAILED with the server's words.
func StopScript ¶
func StopScript(rec *store.ExecSession, sig string) (string, error)
StopScript is the shell text that sends sig to a run, built from its record alone: it checks that the live process started when the record says (exit ExitNotRunning when not), then signals the whole group when the run leads its own group (PGID equal to PID), and otherwise the PID and every process below it (ks_tree, read before the first signal; never the group the run shares), and prints which ("signalled group" or "signalled tree").
func SudoGuard ¶
SudoGuard is the command that runs inner under sudo (Start.SudoGuard): a shell of sudo's user around the run, which the login user reaches only through sudo's relay. It passes TERM on to the run. With a timeout it owns the timer: sudo before 1.9.13 drops a signal sent from its own process group, where the watcher is. A second after the watcher wrote its mark it TERMs the root process's whole group as that user when the root leads one (setsid), else the run and every process below it. "$$" is the root process's PID.
func Sweep ¶
func Sweep(recs []*store.ExecSession) string
Sweep is the shell text that deletes every session folder whose run ended more than Retention ago by the server's own clock, and prints "swept ID" for each. It takes only folders right below ~/.komando/sessions whose name is a session ID, that are the login user's and not links; a running session is never swept. A folder with a record in recs (this host's records on this computer) is decided by its process alone: a live one is skipped whatever files the run wrote, and a gone one is marked lost when it wrote no exit and no KILL stop mark (a TERM mark is no end: Status.Ended), so counts as ended. The files alone decide only for folders without a record. A sessions folder that is missing, a link or not the login user's: nothing is swept.
func Swept ¶
Swept reads the IDs the sweep of session id's start printed. Only the lines between its markers count, which carry the new session's ID (a login banner or a profile script may print first and cannot know it); only the first such block, and only when it ends. An ID twice in it: nothing is read.
Types ¶
type Facts ¶
type Facts struct {
PID int
PGID int
Start string // "proc TICKS" (Linux, /proc/PID/stat field 22) or "ps LSTART"
}
Facts are what the server says about a run's process when it starts: its PID, its process group, and when it started. They go to the record on this computer, and a stop checks the start again before it signals, so a PID the system gave to another process is never signalled.
func ParseFacts ¶
ParseFacts reads ks_facts's lines for session id. Only the lines between its markers count (a login banner or a profile script may print first), each fact once. PID and group 0 and 1 are refused: a signal to either would reach far more than one run.
type Follow ¶
type Follow struct {
Host, ID string
From int64
Wait time.Duration // 0: one read
Every time.Duration // the pause after a read that found nothing new
Lines *Lines
Show func(string)
}
Follow reads a session's log from From on, through Lines, until the run ends or Wait passes. It only reads: it deletes nothing on the server.
type Followed ¶
type Followed struct {
Status Status
NextFrom int64 // the offset a later read starts from: always a line start
}
Followed is where a follow stopped.
type Lines ¶
type Lines struct {
Redact func(string) string
Agent bool
// contains filtered or unexported fields
}
Lines passes a session log to the masker by whole lines, the same way exec's stream does (cli lineBuffer), but without keeping the text: each read starts at the offset the last one used, which is always the start of a line, so a read with --offset N masks the same as the stream. An open private key is held back until its END line; a key that outgrows a read, a line that fills a whole read, and for an agent a line over LineMax, are hidden whole.
type Start ¶
type Start struct {
ID string
Shown string // the command as shown: the cmd file holds it
Run string // the shell text that starts the run in the background and sets w to its PID (Feed piped into it)
Env int // bytes of env exports at the head of the input
Password int // bytes of the sudo password after them
Input bool // the rest of the input is the command's own
SudoGuard bool // Run starts the command under sudo inside SudoGuard
Timeout time.Duration // the run is stopped after it; 0: no limit
LogMax int64 // out.log's cap in bytes
}
Start is what one run in a session needs.
type Status ¶
type Status struct {
Size int64 // bytes in out.log when it was read
Exit int // the run's exit code, once it ended on its own
Killed string // what stopped it: "TERM stop", "KILL stop", "TERM timeout"
Truncated int64 // bytes dropped past the log cap
Lost bool // the run's process died without writing exit or a KILL stop mark (a reboot, an OOM kill)
// contains filtered or unexported fields
}
Status is what a read says about the run.
func ParseRead ¶
ParseRead reads ReadScript's output for session id: the status lines between its markers, each once, and the log bytes after the end marker.
func (Status) Ended ¶
Ended says that the run is over: its exit code is written, or it was stopped with KILL, or found lost. Size is then final.
func (Status) TruncatedNote ¶
TruncatedNote says how much output past the log cap was dropped. The count is a lower bound: head may read past the cap into its buffer before the rest is counted.