process

package
v2.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 7 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 process provides Linux child-subreaper controls and wait-any reaping for a dedicated worker supervisor or PID namespace init.

Use os/exec for process startup, os/signal for signal handling, and pidfd for stable identity and signaling of an identified process. Assign one reaper for the lifetime of each child. Wait and TryWait can reap any eligible child of the calling process, including a direct child started through os/exec. Complete any Cmd.Wait calls for startup children before using wait-any and keep other managed commands in a separate process.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableChildSubreaper

func DisableChildSubreaper() error

DisableChildSubreaper clears the calling process's child-subreaper flag. Previously adopted children stay with the process. PID namespace init still adopts orphans in its namespace. Repeated calls leave the flag disabled.

func EnableChildSubreaper

func EnableChildSubreaper() error

EnableChildSubreaper makes the calling process eligible to adopt orphaned descendants in its PID namespace. The kernel chooses the nearest living ancestor marked as a subreaper, falling back to namespace init.

The flag is shared by all threads, survives exec, and starts clear in newly created child processes. Repeated calls leave it enabled.

func IsChildSubreaper

func IsChildSubreaper() (bool, error)

IsChildSubreaper reports whether the calling process has its child-subreaper flag set. A PID namespace's PID 1 adopts orphans even when this returns false.

Types

type Exit

type Exit struct {
	// PID is the child's numeric ID in the caller's PID namespace. It can be
	// reused as soon as the child is reaped.
	PID int
	// Code is the normal exit code (0 through 255), or -1 for signal termination.
	Code int
	// Signal is the terminating signal, or zero for normal exit.
	Signal syscall.Signal
	// CoreDump reports Linux's CLD_DUMPED status. A core file may be unavailable.
	CoreDump bool
}

Exit is the termination status of one reaped child.

func TryWait

func TryWait() (Exit, bool, error)

TryWait reaps one eligible exited child without blocking. If eligible children exist but none has exited, it returns a zero Exit, false, and nil. If no eligible children remain, it returns a zero Exit, false, and ECHILD. The boolean is true only when a child was reaped. Wait's reaping ownership and error rules also apply here.

func Wait

func Wait() (Exit, error)

Wait blocks until an eligible child exits and reaps that child. Both normal exit and signal termination return a status and nil error. If there are no eligible children, it returns ECHILD. Errors return a zero Exit. Syscall errors preserve their errno, including EINTR when Linux reports interruption.

Wait can occupy an OS thread while blocked. The caller owns reaping for all eligible children. Do not race Wait with Cmd.Wait, os.Process.Wait, or pidfd.Process.Wait. Keep ptrace supervision in a separate process. Linux can consume a ptrace stop during wait-any. A nonterminal status returns an error matching errors.ErrUnsupported. Children with nonstandard clone exit signals are outside this contract.

Example
package main

import (
	"fmt"
	"os"

	"github.com/alexfalkowski/go-linux/v2/process"
)

func main() { //nolint:testableexamples // Executed in a dedicated subprocess by TestWaitExample.
	// Run this in a dedicated supervisor that owns wait status for its children.
	if err := process.EnableChildSubreaper(); err != nil {
		panic(err)
	}

	child, err := os.StartProcess("/bin/sh", []string{"sh", "-c", "exit 23"}, &os.ProcAttr{})
	if err != nil {
		panic(err)
	}
	defer func() { _ = child.Release() }()

	exit, err := process.Wait()
	if err != nil {
		panic(err)
	}

	fmt.Println(exit.Code) // 23
}

Jump to

Keyboard shortcuts

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