safeexec

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package safeexec provides filtered-environment subprocess execution with inactivity-based timeouts. It is the single place where os/exec is called from extensions — all shell helpers route through here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterEnv

func FilterEnv(environ []string) []string

FilterEnv returns a copy of environ containing only safe variables. If environ is nil, os.Environ() is used.

func Run

func Run(ctx context.Context, dir string, inactivity time.Duration, name string, args ...string) (string, error)

Run is a convenience wrapper: executes name+args in dir, captures stdout, returns stderr on error. Optionally resets an inactivity watchdog on each output chunk.

func SafeCmd

func SafeCmd(ctx context.Context, dir string, inactivity time.Duration, name string, args ...string) (cmd *exec.Cmd, stop func())

SafeCmd wraps exec.Cmd with a filtered environment and optional inactivity timeout. The caller is responsible for cmd.Start() / cmd.Wait().

Usage:

cmd, stop := safeexec.SafeCmd(ctx, dir, inactivity, "sh", "-c", command)
defer stop()
// wire cmd.Stdout / cmd.Stderr as needed
err := cmd.Run()

Types

type InactivityTimer

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

InactivityTimer cancels a context after the given duration of no Write calls. Useful for killing subprocesses that produce no output and appear stalled.

func NewInactivityTimer

func NewInactivityTimer(cancel context.CancelFunc, dur time.Duration) *InactivityTimer

NewInactivityTimer creates a timer that fires after dur of inactivity. The returned cancel func should be deferred by the caller.

func (*InactivityTimer) Stop

func (t *InactivityTimer) Stop()

Stop cancels the inactivity timer without firing it.

func (*InactivityTimer) Write

func (t *InactivityTimer) Write(p []byte) (int, error)

Write resets the inactivity countdown. Implements io.Writer for easy wiring to cmd.Stdout / cmd.Stderr.

Jump to

Keyboard shortcuts

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