Description
Based on a discussion in #51246 (in particular, #51246 (comment)), this is a proposal to implement pidfd support for process methods in the os package -- StartProcess, FindProcess, Kill, Wait.
-
Reuse the
handle
field ofstruct Process
to keep pidfd. Since0
is a valid value, use^uintptr(0)
as a default value (if pidfd is not known). -
Instrument
syscall.StartProcess
os.StartProcess
to getpidfd
from the kerneland return it as a. The initial idea was to modifyhandle
syscall.StartProcess
as it returns ahandle
(which we're reusing for pidfd) but that would be backward-incompatible change and can result in e.g. leaking pidfd as callers are not expecting it. -
Use pidfdSendSignal from
(*process).Signal
, if handle is known and the syscall is available; fallback to old code usingsyscall.Kill
if syscall is not available (e.g. disabled by seccomp). -
Use
waitid(P_PIDFD, ...)
in Wait, if pidfd is set; fall back to old code if P_PIDFD is not supported.
- Amend FindProcess to use pidfd_open(2), optionally returning
ErrProcessGone
, and modify the documentation accordingly (adding that it can returnErrProcessGone
on Linux).
Attention doc team: items 1-5 above ^^^ are fully implemented as of Go 1.23rc1, and need to be part of go 1.23 release notes.
- (Optional) Add
(*Process).Handle() uintptr
method to return process handle on Windows and pidfd on Linux. This might be useful for low-level operations that require handle/pidfd (e.g. pidfd_getfd on Linux), or to ensure that pidfd (rather than pid) is being used for kill/wait.