exec

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 17 Imported by: ? 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

Overview

Package exec is the only place Vedette starts external processes (SYS-1, core PRE-4/POST-4). A Runner holds an allowlist of binaries; each call names one, passes an argv array that the binary's validator accepted, and must carry a deadline. There is no shell anywhere.

OS assumption: Unix (process groups). Other platforms get ErrUnsupportedOS from Run.

Index

Constants

View Source
const DefaultMaxOutput int64 = 4 << 20

DefaultMaxOutput is the per-stream capture limit when Binary.MaxOutput is 0.

Variables

View Source
var (
	// ErrNotAllowed means the binary name is not on the allowlist.
	ErrNotAllowed = errors.New("exec: binary not allowed")
	// ErrNoDeadline means the context has no deadline; every run must be bounded.
	ErrNoDeadline = errors.New("exec: context has no deadline")
	// ErrBadArgs means the argv was rejected (NUL byte or the binary's validator).
	ErrBadArgs = errors.New("exec: arguments rejected")
	// ErrUnsupportedOS is returned on platforms without process groups.
	ErrUnsupportedOS = errors.New("exec: unsupported operating system")
)

Functions

This section is empty.

Types

type Binary

type Binary struct {
	Name      string                    // key used by callers
	Path      string                    // absolute path; PATH is never searched
	Validate  func(args []string) error // must accept the whole argv before the process starts
	MaxOutput int64                     // per-stream capture limit in bytes; 0 = DefaultMaxOutput
	// MaxStderr, when > 0, is a capture limit for stderr alone, and stderr then never sets Result.Truncated (Truncated means
	// stdout). 0 keeps the one MaxOutput limit for both streams, and either of them sets Truncated.
	MaxStderr int64
	// Env adds KEY=value entries after the fixed PATH and LANG. Only keys on the allowlist
	// (DEBIAN_FRONTEND, HOME with an absolute path, and NEEDRESTART_MODE=l and NEEDRESTART_SUSPEND=1 with exactly
	// those values) are accepted; the parent environment is never inherited.
	Env []string
	// TTY gives the program a terminal of its own (#830): a new pseudo-terminal is its stdin and its controlling terminal, in a new
	// session it leads, so a program that stays quiet without one talks. nmap 7.95 prints its --stats-every progress only when
	// /dev/tty opens and its process group is the terminal's foreground one (nmap_tty.cc tty_init and keyWasPressed; nmap changed
	// that only in 2026-06, "Allow --stats-every and --noninteractive together", nmap#3175). stdout and stderr stay the capped
	// pipes; what the program writes to the terminal is read and dropped, so it never blocks on it. It is a property of the
	// allowlist entry, never an argument, and a run of a TTY binary takes no stdin. Linux only; elsewhere, or when no terminal can
	// be opened, the program runs as without TTY (a warning, once per Runner).
	TTY bool
}

Binary describes one allowlisted program.

type ExitError

type ExitError struct {
	Name     string
	ExitCode int
}

ExitError is returned (together with the Result) when the process exits non-zero. It carries no output: stderr may hold secrets, read it from Result.

func (*ExitError) Error

func (e *ExitError) Error() string

type Result

type Result struct {
	Stdout, Stderr []byte
	ExitCode       int
	Truncated      bool // stdout or stderr hit MaxOutput; the rest was discarded
	Duration       time.Duration
}

Result is what a finished process produced.

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner runs allowlisted binaries.

func NewRunner

func NewRunner(log *slog.Logger, bins ...Binary) (*Runner, error)

NewRunner validates the allowlist. Names must be unique, paths absolute, Validate non-nil.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, name string, args []string, stdin io.Reader) (Result, error)

Run starts the allowlisted binary name with args. ctx must have a deadline; when it expires the whole process group is killed. A non-zero exit returns the Result and an *ExitError.

func (*Runner) RunWatch

func (r *Runner) RunWatch(ctx context.Context, name string, args []string, stdin io.Reader, w Watch) (Result, error)

RunWatch is Run with a Watch on the output.

type Watch

type Watch struct {
	Stdout, Stderr io.Writer
}

Watch lets a caller read a process's output while it runs (#830: the progress lines nmap prints). Each writer gets a copy of the bytes Run keeps (never more than the capture limit), in order, from the goroutine that copies the pipe: it must be quick and must not block, or the child blocks on a full pipe. A nil writer sees nothing. The output is the child's: untrusted.

Directories

Path Synopsis
Package exectest helps tests that run fake programs (a fake nmap, apt, systemctl…) through core/exec.
Package exectest helps tests that run fake programs (a fake nmap, apt, systemctl…) through core/exec.

Jump to

Keyboard shortcuts

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