childproc

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package childproc builds and manages a single child process with portable stdio, signal, credential, and extra-file-descriptor plumbing. It receives exec specs from cmd/mproxy-agent and feeds the agent's mux loop with process handles, pipes, exit codes, and signal delivery.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseChildSide

func CloseChildSide(cmd *exec.Cmd)

CloseChildSide releases the child-side ends of the std pipes and extra files attached to cmd. Call this once Start has succeeded so the child becomes the only holder of its end of each pipe.

func ExitCode

func ExitCode(state *os.ProcessState) int

ExitCode extracts the exit code from a finished process state. Returns 128+signal when the process was killed by a signal.

func SignalGroup

func SignalGroup(cmd *exec.Cmd, sig int) error

SignalGroup sends sig (a unix signal number) to the child's process group. Falls back to signaling the lead process if the pgid lookup fails, e.g. because the child has already exited.

Types

type Pipes

type Pipes struct {
	Stdin  *os.File            // parent writes → child stdin
	Stdout *os.File            // parent reads ← child stdout
	Stderr *os.File            // parent reads ← child stderr
	Extra  map[uint32]*os.File // parent end of each extra-fd channel
}

Pipes holds the parent-side ends of the standard pipes and any extra-fd channels connected to the child process. The child-side ends live on the *exec.Cmd until Start, after which CloseChildSide releases them in the parent.

func Build

func Build(spec Spec) (*exec.Cmd, *Pipes, error)

Build creates an *exec.Cmd from spec with:

  • A fresh process group (Setpgid) so signals can be delivered to the whole tree.
  • Optional credential drop based on $SUDO_UID/$SUDO_GID.
  • Pipes for stdin/stdout/stderr.
  • Unix socketpairs for each requested extra fd.

The returned Pipes holds the parent-side ends. The caller must Close them when done.

func (*Pipes) Close

func (p *Pipes) Close()

Close releases every file descriptor in p. Safe to call multiple times.

type Spec

type Spec struct {
	Path     string
	Argv     []string
	Env      []string
	Cwd      string
	ExtraFDs []uint32

	// DropSudoCredentials, when true, instructs Build to launch the
	// child with reduced privileges if the current process is running
	// with elevated ones:
	//   - On unix, switch the child's uid/gid to the values in
	//     $SUDO_UID / $SUDO_GID if those variables are set.
	//   - On Windows, if the current process token is elevated (UAC
	//     "split token" admin), launch the child with the linked
	//     filtered (standard-user) token. If the current token is not
	//     elevated, this is a no-op.
	DropSudoCredentials bool
}

Spec describes the command to launch. It is intentionally decoupled from any wire protocol so this package can be reused.

Jump to

Keyboard shortcuts

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