go-linux

pidfd

github.com/alexfalkowski/go-linux/v2/pidfd provides owned process descriptors, signals, exit observation, eligible child reaping, and file duplication on Linux 7.0+. Use os.Process and os/exec for ordinary subprocess management. This package adds independent handle ownership and waits interrupted by closing the handle.

Good for

Good for supervisors that need a stable process handle for exit observation and signaling. systemd monitors children through pidfds, keeping process identity separate from a reusable numeric PID.

Observe a command

Start an exec.Cmd, call pidfd.OpenProcess(command.Process), and defer closing the returned *pidfd.Process. process.WaitReady() waits for the last thread to exit without consuming child status. Call command.Wait() to reap the child and complete os/exec pipe cleanup. Closing a pidfd neither kills nor reaps the process.

OpenProcess borrows command.Process only during acquisition and owns a close-on-exec duplicate of its stable native handle. It does not consume, release, signal, or reap the original process. Either owner can close independently. Unavailable native handles return os.ErrNoHandle; errors after Wait or Release are preserved. There is no PID fallback. Failed acquisition returns nil and closes all acquired descriptors.

Use pidfd.Open(pid) when only a numeric PID is available. Numeric lookup can race with exit, reaping, and PID reuse; keep the intended process alive or unreaped during acquisition. Once acquired, a handle retains its process identity.

See the executable observation example for a bounded command lifecycle and the package documentation for the package boundary.

Adopt an existing handle

pidfd.Adopt(file) consumes exclusive ownership immediately, whether it succeeds or fails. It closes the input on any validation or construction failure; callers do not need a separate error-path file.Close(). After calling Adopt, stop using the original file. On success, close the returned process when done. Adoption accepts blocking and nonblocking process pidfds with close-on-exec set. It rejects other file types, thread pidfds, O_PATH descriptors, and inheritable descriptors with os.ErrInvalid. It preserves shared file status flags and any existing file deadlines.

For an os.Process, prefer OpenProcess; no callback or descriptor duplication is needed at the call site. Adopt remains available for an independently owned standard-library file supplied by a low-level integration, including startup descriptors. Never adopt a borrowed handle.

Adoption validates pidfs identity and descriptor flags without sending a signal. It can adopt handles whose targets have already been reaped, or whose signal permissions differ from the caller’s. It does not add signaling rights or turn a non-child into a waitable child.

Signal and reap

process.Signal(syscall.SIGTERM) delivers a process-directed signal. Signal zero checks existence and permission without delivery. Actual Linux errors such as EPERM, EINVAL, and ESRCH remain available through errors.Is and errors.As; a failure is not converted into a generic unsupported result.

Use process.Wait() only when this package is the chosen owner of reaping, such as for a child started with os.StartProcess. Wait returns an Exit with the normal code, or code -1 plus the terminating signal. CoreDump reports Linux’s core-dump status, without guaranteeing an accessible core file. Both normal and signal termination return nil error. After pidfd reaping, release the separate os.Process with Release. Keep reaping with Cmd.Wait for an exec.Cmd child.

Multiple WaitReady observers can all succeed, including after reaping. Multiple Wait callers compete: exactly one can consume child status; others receive ECHILD. A live non-child also returns ECHILD immediately from Wait, although its exit can be observed with WaitReady. Concurrent external reapers follow the same rule. Status is not cached and Wait is not an idempotent query.

Lifetime and waits

Methods are safe concurrently, but do not copy an initialized process. The zero value returns os.ErrInvalid. Repeated close, signaling, duplication, and waits after close match os.ErrClosed. Nil receivers are unsupported. Close interrupts pending waits. A concurrent signal, duplication, or reap may still succeed.

Each process owns a pidfd and a private eventfd used to wake every pending poll wait on close. Each blocked waiter may occupy an OS thread. There is no context, deadline, or wait-many API, and preexisting deadlines on adopted files do not affect waits. This avoids changing flags shared with another descriptor owner.

The pidfd and wake descriptor remain private. Use Signal, DuplicateFile, WaitReady, and Wait directly.

Thread pidfds, process-group signaling, process creation, ptrace waits, stop/continue notifications, and resource-usage accounting are omitted.

For orphan adoption and wait-any reaping, use a dedicated supervisor with the process package. Use the pidfd Wait when the reaper already knows which eligible child to collect. The application owns signal forwarding, and Cmd.Wait remains responsible for command pipe cleanup.

The Linux 7.0 pidfd UAPI also offers process-information and namespace-acquisition ioctls; these are not exposed. Numeric process/descriptor observations in procfs do not provide those stable-handle operations.

Duplicate a worker file

Call process.DuplicateFile(reportFileNumber) to recover an open file when a supervised worker supplies or agrees on the file number. The returned close-on-exec *os.File belongs to the supervisor. Close it when done. It remains usable after the process handle closes or the worker exits. Its offset and file status flags are shared with the worker, so ordinary reads can advance the worker’s offset. Use ReadAt for regular-file reads that must preserve it. See the executable worker example.

Keep the worker’s file number stable until the call completes. The pidfd binds the process identity, while Linux resolves the file number against that process’s current file table. The kernel applies ptrace attach permissions, security modules, and syscall filtering to duplication. errors.Is exposes errors such as EPERM for denied access, EBADF for an unopened file number, and ESRCH after the target drops its file table. Negative numbers and numbers above math.MaxInt32 return os.ErrInvalid. Every failure returns a nil file. Linux pidfd_getfd.

Verification and prerequisites

Run package=pidfd make specs through the repository harness. Ordinary operations require permitted pidfd_open, pidfd_send_signal, pidfs/fcntl inspection, eventfd, poll, and waitid calls. Tests use procfs, /bin/sh, /bin/cat, and bounded test-owned children. Core-dump status and lowered descriptor limits run in subprocesses without changing host policy. A sibling-process fixture verifies non-child observation while its parent retains reaping.

The rootless permission-denial scenario uses signal zero against a different-UID PID 1. On root or same-UID runners, optional runs skip this scenario and required=true fails its prerequisite. Select the live file duplication cases with make specs package=pidfd profile=namespaces required=true in a runner that permits pidfd_getfd. The namespace launcher provides the CI environment. Verification limits records the positive core-dump evidence gap. See the design note for the design and primary sources.