]> Cypherpunks repositories - gostls13.git/commit
os: make use of pidfd on linux
authorKir Kolyshkin <kolyshkin@gmail.com>
Wed, 13 Sep 2023 08:07:10 +0000 (01:07 -0700)
committerMichael Pratt <mpratt@google.com>
Wed, 21 Feb 2024 21:26:00 +0000 (21:26 +0000)
commit750738b5d197b8b70403c7941fbe0dd20e00ebc9
treed9201d09700e3ff6cba4918c61461418308ea8db
parent4cd743e27eef7495a71cee08a9e8ca971ffcc0a5
os: make use of pidfd on linux

Use Process.handle field to store pidfd, and make use of it. Only use
pidfd functionality if all the needed syscalls are available.

1. StartProcess: obtain the pidfd from the kernel, if available,
   using the functionality added by CL 520266. Note we could not modify
   syscall.StartProcess to return pidfd directly because it is a public
   API and its callers do not expect it, so we have to use ensurePidfd
   and getPidfd.

2. (*Process).Kill: use pidfdSendSignal, if the syscall is available
   and pidfd is known. This is slightly more complicated than it should
   be, since the syscall can be blocked by e.g. seccomp security policy,
   therefore the need for a function to check if it's actually working,
   and a soft fallback to kill. Perhaps this precaution is not really
   needed.

3. (*Process).Wait: use pidfdWait, if available, otherwise fall back to
   using waitid/wait4. This is also more complicated than expected due
   to struct siginfo_t idiosyncrasy.

NOTE pidfdSendSignal and pidfdWait are used without a race workaround
(blockUntilWaitable and sigMu, added by CL 23967) because with pidfd,
PID recycle issue doesn't exist (IOW, pidfd, unlike PID, is guaranteed
to refer to one particular process) and thus the race doesn't exist
either.

For #62654.
Updates #13987.

Change-Id: I22ebcc7142b16a3a94c422d2f32504d1a80e8a8f
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/528438
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
19 files changed:
src/internal/syscall/unix/pidfd_linux.go
src/internal/syscall/unix/siginfo_linux.go [new file with mode: 0644]
src/internal/syscall/unix/siginfo_linux_mipsx.go [new file with mode: 0644]
src/internal/syscall/unix/siginfo_linux_other.go [new file with mode: 0644]
src/internal/syscall/unix/siginfo_linux_test.go [new file with mode: 0644]
src/internal/syscall/unix/sysnum_linux_386.go
src/internal/syscall/unix/sysnum_linux_amd64.go
src/internal/syscall/unix/sysnum_linux_arm.go
src/internal/syscall/unix/sysnum_linux_generic.go
src/internal/syscall/unix/sysnum_linux_mips64x.go
src/internal/syscall/unix/sysnum_linux_mipsx.go
src/internal/syscall/unix/sysnum_linux_ppc64x.go
src/internal/syscall/unix/sysnum_linux_s390x.go
src/os/exec_posix.go
src/os/exec_unix.go
src/os/export_linux_test.go
src/os/pidfd_linux.go [new file with mode: 0644]
src/os/pidfd_linux_test.go [new file with mode: 0644]
src/os/pidfd_other.go [new file with mode: 0644]