Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

23.0 KB
Raw Blame
//go:build linux

// Copyright 2026 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Init mode (OSEP-0018): execd is the sandbox init — it reaps children
// through a single reaper, forwards application signals to the entrypoint,
// and owns the container lifecycle (exit code propagated to the runtime).
//
// The reaper is the only wait4-family caller, so execd never calls
// os/exec.Cmd.Wait; managedProcess reproduces the pipe teardown Cmd.Wait
// would perform. The reaper registry lock spans child start and
// registration, closing the start/register race structurally; unowned
// children are reparented orphans, reaped and logged.

package runtime

import (
	"errors"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"sync"
	"syscall"
	"time"
	"unsafe"

	"github.com/alibaba/opensandbox/internal/safego"
	"golang.org/x/sys/unix"

	"github.com/alibaba/opensandbox/execd/pkg/log"
)

var (
	initShutdownGrace    = 10 * time.Second
	initReaper           *reaper
	reaperSweepInterval  = 200 * time.Millisecond
	initForwardedSignals = []os.Signal{
		syscall.SIGTERM,
		syscall.SIGHUP,
		syscall.SIGUSR1,
		syscall.SIGUSR2,
		syscall.SIGWINCH,
	}
)

// siginfoWait mirrors the kernel siginfo_t fields waitid fills. The vendored
// x/sys Siginfo only exposes the first three fields, and the union member
// offsets are arch-specific, so this struct is the 64-bit Linux layout
// (verified on amd64 and arm64): the union is 8-byte aligned, so si_pid sits
// at offset 16, si_uid at 20, si_status at 24. waitid's si_status is the raw
// exit code or signal number, not the wait4 status encoding.
type siginfoWait struct {
	signo  int32
	errno  int32
	code   int32
	_      int32
	pid    int32
	uid    uint32
	status int32
	_      [104]byte
}

// waitStatus converts waitid's si_status/si_code into the syscall.WaitStatus
// encoding the rest of the code base understands. si_code values are the
// stable UAPI CLD_* constants (linux/siginfo.h).
func (i *siginfoWait) waitStatus() syscall.WaitStatus {
	switch i.code {
	case 1: // CLD_EXITED
		return syscall.WaitStatus(uint32(i.status&0xff) << 8)
	case 2, 3: // CLD_KILLED, CLD_DUMPED
		return syscall.WaitStatus(uint32(i.status & 0x7f))
	default:
		return 0
	}
}

func waitidObserve(info *siginfoWait) error {
	_, _, errno := unix.Syscall6(unix.SYS_WAITID,
		uintptr(unix.P_ALL), 0,
		uintptr(unsafe.Pointer(info)),
		uintptr(unix.WEXITED|unix.WNOHANG|unix.WNOWAIT),
		0, 0)
	if errno == 0 {
		return nil
	}
	if errno == unix.EINTR {
		return unix.EINTR
	}
	return errno
}

func waitidConsume(pid int) (syscall.WaitStatus, error) {
	var info siginfoWait
	_, _, errno := unix.Syscall6(unix.SYS_WAITID,
		uintptr(unix.P_PID), uintptr(pid),
		uintptr(unsafe.Pointer(&info)),
		uintptr(unix.WEXITED|unix.WNOHANG),
		0, 0)
	if errno == 0 {
		return info.waitStatus(), nil
	}
	if errno == unix.EINTR {
		return 0, unix.EINTR
	}
	return 0, errno
}

// reaper is the single wait4-family caller while init mode is active.
type reaper struct {
	mu       sync.Mutex
	owned    map[int]*managedProcess
	sigchld  chan os.Signal
	quit     chan struct{}
	quitOnce sync.Once //nolint:unused // test-only lifecycle; see stop
	done     chan struct{}
}

func newReaper() *reaper {
	return &reaper{
		owned: map[int]*managedProcess{},
		quit:  make(chan struct{}),
		done:  make(chan struct{}),
	}
}

// start registers the SIGCHLD notification synchronously so no child can
// exit before the handler exists (a lost SIGCHLD would strand its status).
func (r *reaper) start() {
	r.sigchld = make(chan os.Signal, 1)
	signal.Notify(r.sigchld, syscall.SIGCHLD)
}

// stop terminates the reaper and waits until its signal subscription is
// removed.
//
//nolint:unused // test-only lifecycle; execd runs one reaper for its lifetime
func (r *reaper) stop() {
	r.quitOnce.Do(func() { close(r.quit) })
	<-r.done
}

func (r *reaper) run() {
	defer func() {
		signal.Stop(r.sigchld)
		close(r.done)
	}()
	// The ticker is a backstop: SIGCHLD may be coalesced or (in edge cases)
	// lost, so a periodic drain keeps the process table bounded regardless.
	sweep := time.NewTicker(reaperSweepInterval)
	defer sweep.Stop()
	for {
		select {
		case <-r.quit:
			return
		case <-r.sigchld:
			r.drain()
		case <-sweep.C:
			r.drain()
		}
	}
}

func (r *reaper) drain() {
	r.mu.Lock()
	defer r.mu.Unlock()
	for {
		var info siginfoWait
		if err := waitidObserve(&info); err != nil {
			if errors.Is(err, unix.EINTR) {
				continue
			}
			if !errors.Is(err, unix.ECHILD) {
				log.Warn("init: reaper observe: %v", err)
			}
			return
		}
		if info.pid == 0 {
			return
		}
		pid := int(info.pid)
		if mp := r.owned[pid]; mp != nil {
			// The pre-reap barrier runs between the WNOWAIT observe and the
			// consuming wait, while the kernel still reserves the PID/PGID
			// (isolated sessions rely on this to avoid signalling a recycled
			// process group).
			if mp.preReap != nil {
				mp.preReap()
			}
			ws, err := waitidConsume(pid)
			if err != nil {
				if errors.Is(err, unix.EINTR) {
					continue
				}
				log.Error("init: reaper consume pid %d: %v", pid, err)
				return
			}
			// Drop the child from the registry once reaped: stale entries
			// would grow without bound and shutdown could signal a recycled
			// process group.
			delete(r.owned, pid)
			mp.deliver(ws)
			continue
		}
		// Unknown child: reparented orphan. Reap it so the process table
		// stays bounded; its status is not delivered to anyone.
		ws, err := waitidConsume(pid)
		if err != nil {
			if errors.Is(err, unix.EINTR) {
				continue
			}
			log.Error("init: reaper consume orphan pid %d: %v", pid, err)
			return
		}
		log.Info("init: reaped orphan pid=%d status=%s", pid, ws)
	}
}

// managedProcess wraps an exec.Cmd whose status is delivered by the reaper.
// In non-init mode it falls back to plain Cmd.Start/Cmd.Wait, so callers
// share one launch path regardless of mode.
type managedProcess struct {
	cmd         *exec.Cmd
	stateMu     sync.Mutex
	exited      bool
	preReap     func()
	noHardening bool
	stripEnv    []string // nil = default blacklist; explicit list overrides
	done        chan struct{}
	once        sync.Once
	ws          syscall.WaitStatus
	exitErr     error
}

func newManagedProcess(cmd *exec.Cmd) *managedProcess {
	return &managedProcess{cmd: cmd, done: make(chan struct{})}
}

func (mp *managedProcess) pid() int {
	return mp.cmd.Process.Pid
}

func (mp *managedProcess) deliver(ws syscall.WaitStatus) {
	mp.once.Do(func() {
		mp.ws = ws
		mp.exitErr = exitStatusError(ws)
		close(mp.done)
	})
}

func (mp *managedProcess) Wait() error {
	if initReaper == nil {
		return waitCommandWithExitBarrier(mp.cmd, func(_ error) {
			// Success marks exit before reap. A failed barrier cannot prove
			// ownership, so also disable signaling rather than risk PID reuse.
			mp.stateMu.Lock()
			mp.exited = true
			mp.stateMu.Unlock()
		})
	}
	<-mp.done
	return mp.exitErr
}

func (mp *managedProcess) Cancel(cancel func()) {
	if initReaper == nil {
		mp.stateMu.Lock()
		defer mp.stateMu.Unlock()
		if !mp.exited {
			cancel()
		}
		return
	}

	// Keep the reaper lock across signal delivery so the PID/PGID cannot be
	// recycled. cancel must only deliver a signal and must not block.
	initReaper.mu.Lock()
	defer initReaper.mu.Unlock()
	if initReaper.owned[mp.pid()] == mp {
		cancel()
	}
}

// ExitCode returns the process exit code, or -1 if it has not exited (or was
// killed by a signal), matching os.ProcessState.ExitCode semantics.
func (mp *managedProcess) ExitCode() int {
	if initReaper == nil {
		if mp.cmd.ProcessState == nil {
			return -1
		}
		return mp.cmd.ProcessState.ExitCode()
	}
	select {
	case <-mp.done:
		return mp.ws.ExitStatus()
	default:
		return -1
	}
}

func (mp *managedProcess) exitStatus() syscall.WaitStatus {
	return mp.ws
}

type launchOption func(*managedProcess)

func withPreReap(fn func()) launchOption {
	return func(mp *managedProcess) {
		mp.preReap = fn
	}
}

// withoutHardening exempts a launch from the hardening floor. Used for the
// bwrap process of isolated sessions, whose workload is already reduced
// inside the namespace and whose own syscalls (unshare) the floor would deny.
func withoutHardening() launchOption {
	return func(mp *managedProcess) {
		mp.noHardening = true
	}
}

// bootstrapEnv overrides the env strip for the user entrypoint: its scripts
// may need JUPYTER_TOKEN/EXECD_ENVS to configure themselves (e.g. the
// code-interpreter entrypoint), but credentials and lifecycle transport must
// never reach the long-lived entrypoint (its Jupyter kernels are user code).
func bootstrapEnv() launchOption {
	return func(mp *managedProcess) {
		mp.stripEnv = bootstrapStripEnv
	}
}

// bootstrapStripEnv names stripped from the entrypoint environment before
// launch (cmd.Env) and again at execve time (hardening launcher policy).
var bootstrapStripEnv = []string{
	"EXECD_ACCESS_TOKEN",
	"OPENSANDBOX_LIFECYCLE",
	"EXECD_LIFECYCLE_CONFIG",
	"EXECD_RUNTIME_INIT",
}

// launchManagedWith starts the command and registers it with the reaper.
// startFn is called under the reaper lock so the child cannot be observed
// (and misclassified as an orphan) before registration. When the hardening
// floor is active, cmd is first rewritten to exec through the launcher.
func launchManagedWith(cmd *exec.Cmd, startFn func() error, opts ...launchOption) (*managedProcess, error) {
	mp := newManagedProcess(cmd)
	for _, o := range opts {
		o(mp)
	}
	policyFile, err := hardenCmd(cmd, mp.noHardening, mp.stripEnv)
	if err != nil {
		return nil, err
	}
	if policyFile != nil {
		defer policyFile.Close()
	}
	if initReaper == nil {
		if err := startFn(); err != nil {
			return nil, err
		}
		return mp, nil
	}
	initReaper.mu.Lock()
	defer initReaper.mu.Unlock()
	if err := startFn(); err != nil {
		return nil, err
	}
	initReaper.owned[cmd.Process.Pid] = mp
	return mp, nil
}

func launchManaged(cmd *exec.Cmd, opts ...launchOption) (*managedProcess, error) {
	return launchManagedWith(cmd, cmd.Start, opts...)
}

// waitManagedWithBarrier mirrors waitCommandWithExitBarrier: in init mode the
// pre-reap barrier was registered at launch and runs inside the reaper, so
// this just waits; otherwise the original WNOWAIT barrier path applies.
func waitManagedWithBarrier(mp *managedProcess, mark func(error)) error {
	if initReaper == nil {
		return waitCommandWithExitBarrier(mp.cmd, mark)
	}
	return mp.Wait()
}

// processExitError is the error returned by managedProcess.Wait in init mode
// when the child did not exit cleanly. It mirrors exec.ExitError's contract
// without needing a constructed os.ProcessState.
type processExitError struct {
	code int
	msg  string
}

func (e *processExitError) Error() string {
	return e.msg
}

func (e *processExitError) ExitCode() int {
	return e.code
}

func exitStatusError(ws syscall.WaitStatus) error {
	if ws.Exited() {
		if code := ws.ExitStatus(); code == 0 {
			return nil
		} else {
			return &processExitError{code: code, msg: fmt.Sprintf("exit status %d", code)}
		}
	}
	return &processExitError{code: -1, msg: fmt.Sprintf("signal: %v", ws.Signal())}
}

// PrepareInitMode activates the init/reaper duties and registers signal
// handling before any managed child starts. The returned function launches
// (or replaces) the user entrypoint; the legacy startup path calls it once
// and POST /internal/init calls it again when a RuntimeBinding reassigns the sandbox.
func PrepareInitMode() func([]string) error {
	if err := unix.Prctl(unix.PR_SET_DUMPABLE, 0, 0, 0, 0); err != nil {
		log.Warn("init: PR_SET_DUMPABLE(0) failed: %v", err)
	}
	if os.Getpid() != 1 {
		// Pool path (or a misconfigured background launch): execd is not the
		// kernel init, so orphaned descendants reparent to it only if it is a
		// subreaper. The kernel signal shield is lost in this mode.
		if err := unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); err != nil {
			log.Warn("init: PR_SET_CHILD_SUBREAPER failed: %v", err)
		}
	}
	initReaper = newReaper()
	initReaper.start()
	safego.Go(initReaper.run)
	log.Info("init: execd is the sandbox init (pid=%d mode=%s)", os.Getpid(), initModeName())

	// Register the application-signal subscription before the entrypoint
	// starts: an early SIGTERM must reach the forwarding loop instead of
	// hitting the runtime default handler.
	sigCh := make(chan os.Signal, 8)
	signal.Notify(sigCh, initForwardedSignals...)
	safego.Go(func() { forwardInitSignals(sigCh) })

	return LaunchUserEntrypoint
}

// entrypointSupervisor tracks the current user entrypoint generation so
// application signals are forwarded to the active process and runtime init
// can retire and replace it without tearing down the container.
type entrypointSupervisor struct {
	mu         sync.Mutex
	current    *managedProcess
	retired    map[*managedProcess]struct{}
	restarting bool
	disabled   bool // no entrypoint will ever be supervised (no user command)
	started    chan struct{}
}

var entrypoints = &entrypointSupervisor{
	retired: map[*managedProcess]struct{}{},
	started: make(chan struct{}, 1),
}

func (s *entrypointSupervisor) currentEntry() *managedProcess {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.current
}

func (s *entrypointSupervisor) isRestarting() bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.restarting
}

func (s *entrypointSupervisor) isDisabled() bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.disabled
}

func (s *entrypointSupervisor) markNoEntrypoint() {
	s.mu.Lock()
	s.disabled = true
	s.restarting = false
	s.mu.Unlock()
	s.notify()
}

func (s *entrypointSupervisor) setEntry(mp *managedProcess) {
	s.mu.Lock()
	s.current = mp
	s.restarting = false
	s.mu.Unlock()
	s.notify()
}

func (s *entrypointSupervisor) notify() {
	select {
	case s.started <- struct{}{}:
	default:
	}
}

func (s *entrypointSupervisor) isRetired(mp *managedProcess) bool {
	s.mu.Lock()
	defer s.mu.Unlock()
	_, wasRetired := s.retired[mp]
	return wasRetired
}

// signalEntryGroup delivers sig to the entrypoint process group while the
// reaper lock guarantees the PID/PGID is still owned (a recycled group can
// never be signalled).
func signalEntryGroup(mp *managedProcess, sig syscall.Signal) error {
	if initReaper == nil {
		return nil
	}
	initReaper.mu.Lock()
	defer initReaper.mu.Unlock()
	if initReaper.owned[mp.pid()] != mp {
		return nil
	}
	return killGroup(mp.pid(), sig)
}

// retireCurrent stops the active entrypoint (SIGTERM → grace → SIGKILL),
// marks it retired so its exit does not terminate the container, and waits
// for the reaper to collect it.
func (s *entrypointSupervisor) retireCurrent() {
	s.mu.Lock()
	entry := s.current
	s.current = nil
	s.restarting = true
	if entry != nil {
		s.retired[entry] = struct{}{}
	}
	s.mu.Unlock()
	if entry == nil {
		return
	}
	log.Info("init: stopping previous entrypoint pid=%d for runtime init", entry.pid())
	if err := signalEntryGroup(entry, syscall.SIGTERM); err != nil && !errors.Is(err, syscall.ESRCH) {
		log.Warn("init: SIGTERM previous entrypoint group: %v", err)
	}
	deadline := time.After(initShutdownGrace)
	select {
	case <-entry.done:
	case <-deadline:
		if err := signalEntryGroup(entry, syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
			log.Warn("init: SIGKILL previous entrypoint group: %v", err)
		}
		select {
		case <-entry.done:
		case <-time.After(5 * time.Second):
		}
	}
}

// LaunchUserEntrypoint starts (or replaces) the supervised user entrypoint.
func LaunchUserEntrypoint(args []string) error {
	if len(args) == 0 {
		log.Warn("init: --init set but no user command provided; no entrypoint to supervise")
		entrypoints.markNoEntrypoint()
		return nil
	}
	entrypoints.retireCurrent()
	entry, err := launchEntrypoint(args)
	if err != nil {
		return err
	}
	entrypoints.setEntry(entry)
	safego.Go(func() { waitEntrypointExit(entry) })
	return nil
}

// EntrypointRunning reports whether a supervised entrypoint process is
// currently active.
func EntrypointRunning() bool {
	return entrypoints.currentEntry() != nil
}

// RetireEntrypoint stops and retires the supervised entrypoint (runtime
// init with entrypointPolicy=restart). Retirement must be visible before
// the process exits: waitEntrypointExit treats a non-retired exit as the
// container exiting. No-op when no entrypoint is running.
func RetireEntrypoint() {
	entrypoints.retireCurrent()
}

// StopUserProcesses terminates every reaper-tracked child, including the
// entrypoint. With keepEntrypoint the supervised entrypoint is spared
// (entrypointPolicy=keep adopts the running process). POST /internal/init
// uses this to stop pre-init workloads before applying the binding; outside
// init mode it is a no-op (sessions tear down through Controller.Reset).
func StopUserProcesses(keepEntrypoint bool) {
	if initReaper == nil {
		return
	}
	if keepEntrypoint {
		stopChildrenExcept(entrypoints.currentEntry())
		return
	}
	stopChildrenExcept(nil)
}

func forwardInitSignals(sigCh chan os.Signal) {
	termPending := false
	for {
		if entrypoints.isDisabled() {
			signal.Stop(sigCh)
			return
		}
		entry := entrypoints.currentEntry()
		if entry != nil && termPending {
			terminateInit(entry)
			return
		}
		select {
		case sig := <-sigCh:
			s, ok := sig.(syscall.Signal)
			if !ok {
				continue
			}
			if s == syscall.SIGTERM {
				if entry == nil && !entrypoints.isRestarting() {
					// No entrypoint has ever been launched (runtime-init
					// gating before POST /internal/init): the container is being
					// stopped before any workload exists.
					log.Info("init: received SIGTERM before any entrypoint; stopping children and exiting")
					stopChildrenExcept(nil)
					os.Exit(128 + int(syscall.SIGTERM))
				}
				termPending = true
				continue
			}
			if entry != nil {
				log.Info("init: forwarding %v to workload", s)
				if err := signalEntryGroup(entry, s); err != nil {
					log.Warn("init: forward %v to entrypoint group: %v", s, err)
				}
			}
			// With no entrypoint yet, non-TERM signals stay queued in sigCh
			// and are forwarded once the entrypoint starts.
		case <-entrypoints.started:
		}
	}
}

func launchEntrypoint(args []string) (*managedProcess, error) {
	cmd := exec.Command(args[0], args[1:]...)
	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	// Resolve through the shared user-env layering so the entrypoint sees the
	// /init RuntimeBinding envs; transport/credential names are stripped both
	// here and again by the launcher at execve time (bootstrapEnv).
	cmd.Env = mergeEnvs(filterEnvNames(os.Environ(), bootstrapStripEnv), UserEnvOverlay())
	mp, err := launchManaged(cmd, bootstrapEnv())
	if err != nil {
		return nil, fmt.Errorf("start user entrypoint %q: %w", args[0], err)
	}
	log.Info("init: user entrypoint started pid=%d argv=%v", mp.pid(), args)
	return mp, nil
}

// waitEntrypointExit owns the container lifecycle: when the active
// entrypoint exits, the other children are stopped gracefully and execd
// exits with the entrypoint's status so Docker/kubelet observe it. A retired
// entrypoint (replaced by runtime init) must not tear the container down.
func waitEntrypointExit(entry *managedProcess) {
	entryErr := entry.Wait()
	if entrypoints.isRetired(entry) {
		log.Info("init: previous entrypoint exited after runtime init: code=%d err=%v", initExitCode(entry), entryErr)
		return
	}
	code := initExitCode(entry)
	log.Info("init: user entrypoint exited: code=%d err=%v", code, entryErr)
	stopChildrenExcept(entry)
	log.Info("init: exiting with entrypoint status %d", code)
	os.Exit(code)
}

// initExitCode converts a delivered status into the container exit code,
// following the shell convention of 128+signal for signalled processes.
func initExitCode(mp *managedProcess) int {
	ws := mp.exitStatus()
	if ws.Signaled() {
		return 128 + int(ws.Signal())
	}
	if ws.Exited() {
		return ws.ExitStatus()
	}
	return 1
}

// terminateInit performs the SIGTERM shutdown: forward TERM to the entrypoint
// tree, stop the other children, then exit once the entrypoint is reaped
// (SIGKILL after a bounded grace).
func terminateInit(entry *managedProcess) {
	if err := killGroup(entry.pid(), syscall.SIGTERM); err != nil && !errors.Is(err, syscall.ESRCH) {
		log.Warn("init: SIGTERM entrypoint group: %v", err)
	}
	stopChildrenExcept(entry)
	deadline := time.After(initShutdownGrace)
	select {
	case <-entry.done:
	case <-deadline:
		if err := killGroup(entry.pid(), syscall.SIGKILL); err != nil && !errors.Is(err, syscall.ESRCH) {
			log.Warn("init: SIGKILL entrypoint group: %v", err)
		}
		select {
		case <-entry.done:
		case <-time.After(5 * time.Second):
		}
	}
	log.Info("init: exiting after SIGTERM shutdown")
	os.Exit(initExitCode(entry))
}

// stopChildrenExcept signals every other tracked child group with SIGTERM,
// waits up to the shutdown grace (total budget across all children), then
// SIGKILLs the survivors. Reaping is done by the reaper; the kernel reaps
// anything left when execd exits.
func stopChildrenExcept(keep *managedProcess) {
	// Signal while holding the reaper lock: the pid stays verified against
	// the owned map, so the reaper cannot release the PID/PGID between the
	// check and the kill (no recycled process group can be signalled).
	others := initReaper.signalOthers(keep, syscall.SIGTERM)
	if len(others) == 0 {
		return
	}
	deadline := time.Now().Add(initShutdownGrace)
	for _, mp := range others {
		select {
		case <-mp.done:
		case <-time.After(time.Until(deadline)):
		}
	}
	initReaper.signalOthers(keep, syscall.SIGKILL)
}

// signalOthers delivers sig to every still-tracked child group except keep,
// while holding the reaper lock. It returns the targets that were signalled.
func (r *reaper) signalOthers(keep *managedProcess, sig syscall.Signal) []*managedProcess {
	r.mu.Lock()
	defer r.mu.Unlock()
	var others []*managedProcess
	for pid, mp := range r.owned {
		if mp == keep {
			continue
		}
		others = append(others, mp)
		if err := killGroup(pid, sig); err != nil && !errors.Is(err, syscall.ESRCH) {
			log.Warn("init: %v child group %d: %v", sig, pid, err)
		}
	}
	return others
}

// killGroup sends sig to the child's process group; all managed children are
// launched with Setpgid, so the group id equals the child pid.
func killGroup(pid int, sig syscall.Signal) error {
	return syscall.Kill(-pid, sig)
}

func initModeName() string {
	if os.Getpid() == 1 {
		return "pid1"
	}
	return "subreaper"
}

// InitModeReport reports the init mode actually in effect for the
// capabilities endpoint.
func InitModeReport() (mode string, signalShield bool) {
	if initReaper == nil {
		return "none", false
	}
	if os.Getpid() == 1 {
		return "pid1", true
	}
	return "subreaper", false
}

// initModeActive reports whether the init-mode signal/runtime ownership is in
// effect (execd started with --init). Shared launch paths consult it to avoid
// competing with forwardInitSignals.
func initModeActive() bool {
	return initReaper != nil
}