aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2024-02-23 13:08:46 -0500
committerGopher Robot <gobot@golang.org>2024-02-23 18:31:19 +0000
commite58486e5991c7dc3ac65ef12421cb33b91dd3d9e (patch)
treee5c27d3b01daec21b4bcdeaa54c37c23340f3f5e /src/os
parentc4e4afc90eb6fd31710edb062bacfae0643d170f (diff)
downloadgo-e58486e5991c7dc3ac65ef12421cb33b91dd3d9e.tar.gz
go-e58486e5991c7dc3ac65ef12421cb33b91dd3d9e.zip
Revert "os: make use of pidfd on linux"
This reverts CL 528438. Reason for revert: Implicated in "bad FD" test failures. Full extent of issue still unclear. For #62654. Fixes #65857. Change-Id: I066e38040544c506917e90255bd0e330964a0276 Reviewed-on: https://go-review.googlesource.com/c/go/+/566477 Auto-Submit: Michael Pratt <mpratt@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/os')
-rw-r--r--src/os/exec_posix.go10
-rw-r--r--src/os/exec_unix.go39
-rw-r--r--src/os/export_linux_test.go1
-rw-r--r--src/os/pidfd_linux.go145
-rw-r--r--src/os/pidfd_linux_test.go17
-rw-r--r--src/os/pidfd_other.go27
6 files changed, 13 insertions, 226 deletions
diff --git a/src/os/exec_posix.go b/src/os/exec_posix.go
index 944d936c11..4f9ea08cde 100644
--- a/src/os/exec_posix.go
+++ b/src/os/exec_posix.go
@@ -13,10 +13,6 @@ import (
"syscall"
)
-// unsetHandle is a value for Process.handle used when the handle is not set.
-// Same as syscall.InvalidHandle for Windows.
-const unsetHandle = ^uintptr(0)
-
// The only signal values guaranteed to be present in the os package on all
// systems are os.Interrupt (send the process an interrupt) and os.Kill (force
// the process to exit). On Windows, sending os.Interrupt to a process with
@@ -42,7 +38,7 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
sysattr := &syscall.ProcAttr{
Dir: attr.Dir,
Env: attr.Env,
- Sys: ensurePidfd(attr.Sys),
+ Sys: attr.Sys,
}
if sysattr.Env == nil {
sysattr.Env, err = execenv.Default(sysattr.Sys)
@@ -64,10 +60,6 @@ func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err e
return nil, &PathError{Op: "fork/exec", Path: name, Err: e}
}
- if runtime.GOOS == "linux" {
- h = getPidfd(sysattr.Sys)
- }
-
return newProcess(pid, h), nil
}
diff --git a/src/os/exec_unix.go b/src/os/exec_unix.go
index 2c66a8be20..36b320df18 100644
--- a/src/os/exec_unix.go
+++ b/src/os/exec_unix.go
@@ -17,16 +17,6 @@ func (p *Process) wait() (ps *ProcessState, err error) {
if p.Pid == -1 {
return nil, syscall.EINVAL
}
- // Use pidfd if possible; fallback on ENOSYS or EPERM (the latter can be
- // returned if syscall is prohibited by seccomp or a similar mechanism).
- //
- // When pidfd is used, there is no wait/kill race (described in CL 23967)
- // because PID recycle issue doesn't exist (IOW, pidfd, unlike PID, is
- // guaranteed to refer to one particular process). Thus, there is no
- // need for the workaround (blockUntilWaitable + sigMu) below.
- if ps, e := p.pidfdWait(); e != syscall.ENOSYS && e != syscall.EPERM {
- return ps, NewSyscallError("waitid", e)
- }
// If we can block until Wait4 will succeed immediately, do so.
ready, err := p.blockUntilWaitable()
@@ -74,31 +64,26 @@ func (p *Process) signal(sig Signal) error {
if p.Pid == 0 {
return errors.New("os: process not initialized")
}
- s, ok := sig.(syscall.Signal)
- if !ok {
- return errors.New("os: unsupported signal type")
- }
- // Use pidfd if possible; fallback on ENOSYS.
- if err := p.pidfdSendSignal(s); err != syscall.ENOSYS {
- return err
- }
p.sigMu.RLock()
defer p.sigMu.RUnlock()
if p.done() {
return ErrProcessDone
}
- return convertESRCH(syscall.Kill(p.Pid, s))
-}
-
-func convertESRCH(err error) error {
- if err == syscall.ESRCH {
- return ErrProcessDone
+ s, ok := sig.(syscall.Signal)
+ if !ok {
+ return errors.New("os: unsupported signal type")
}
- return err
+ if e := syscall.Kill(p.Pid, s); e != nil {
+ if e == syscall.ESRCH {
+ return ErrProcessDone
+ }
+ return e
+ }
+ return nil
}
func (p *Process) release() error {
- p.pidfdRelease()
+ // NOOP for unix.
p.Pid = -1
// no need for a finalizer anymore
runtime.SetFinalizer(p, nil)
@@ -107,7 +92,7 @@ func (p *Process) release() error {
func findProcess(pid int) (p *Process, err error) {
// NOOP for unix.
- return newProcess(pid, unsetHandle), nil
+ return newProcess(pid, 0), nil
}
func (p *ProcessState) userTime() time.Duration {
diff --git a/src/os/export_linux_test.go b/src/os/export_linux_test.go
index 839242f986..942b48a17d 100644
--- a/src/os/export_linux_test.go
+++ b/src/os/export_linux_test.go
@@ -9,5 +9,4 @@ var (
PollSpliceFile = &pollSplice
PollSendFile = &pollSendFile
GetPollFDAndNetwork = getPollFDAndNetwork
- CheckPidfdOnce = checkPidfdOnce
)
diff --git a/src/os/pidfd_linux.go b/src/os/pidfd_linux.go
deleted file mode 100644
index d6e1d53eee..0000000000
--- a/src/os/pidfd_linux.go
+++ /dev/null
@@ -1,145 +0,0 @@
-// Copyright 2023 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-//go:build linux
-
-// Support for pidfd was added during the course of a few Linux releases:
-// v5.1: pidfd_send_signal syscall;
-// v5.2: CLONE_PIDFD flag for clone syscall;
-// v5.3: pidfd_open syscall, clone3 syscall;
-// v5.4: P_PIDFD idtype support for waitid syscall;
-// v5.6: pidfd_getfd syscall.
-
-package os
-
-import (
- "internal/syscall/unix"
- "sync"
- "syscall"
- "unsafe"
-)
-
-func ensurePidfd(sysAttr *syscall.SysProcAttr) *syscall.SysProcAttr {
- if !pidfdWorks() {
- return sysAttr
- }
-
- var pidfd int
-
- if sysAttr == nil {
- return &syscall.SysProcAttr{
- PidFD: &pidfd,
- }
- }
- if sysAttr.PidFD == nil {
- newSys := *sysAttr // copy
- newSys.PidFD = &pidfd
- return &newSys
- }
-
- return sysAttr
-}
-
-func getPidfd(sysAttr *syscall.SysProcAttr) uintptr {
- if !pidfdWorks() {
- return unsetHandle
- }
-
- return uintptr(*sysAttr.PidFD)
-}
-
-func (p *Process) pidfdRelease() {
- // Release pidfd unconditionally.
- handle := p.handle.Swap(unsetHandle)
- if handle != unsetHandle {
- syscall.Close(int(handle))
- }
-}
-
-// _P_PIDFD is used as idtype argument to waitid syscall.
-const _P_PIDFD = 3
-
-func (p *Process) pidfdWait() (*ProcessState, error) {
- handle := p.handle.Load()
- if handle == unsetHandle || !pidfdWorks() {
- return nil, syscall.ENOSYS
- }
- var (
- info unix.SiginfoChild
- rusage syscall.Rusage
- e syscall.Errno
- )
- for {
- _, _, e = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, handle, uintptr(unsafe.Pointer(&info)), syscall.WEXITED, uintptr(unsafe.Pointer(&rusage)), 0)
- if e != syscall.EINTR {
- break
- }
- }
- if e != 0 {
- if e == syscall.EINVAL {
- // This is either invalid option value (which should not happen
- // as we only use WEXITED), or missing P_PIDFD support (Linux
- // kernel < 5.4), meaning pidfd support is not implemented.
- e = syscall.ENOSYS
- }
- return nil, e
- }
- p.setDone()
- defer p.pidfdRelease()
- return &ProcessState{
- pid: int(info.Pid),
- status: info.WaitStatus(),
- rusage: &rusage,
- }, nil
-}
-
-func (p *Process) pidfdSendSignal(s syscall.Signal) error {
- handle := p.handle.Load()
- if handle == unsetHandle || !pidfdWorks() {
- return syscall.ENOSYS
- }
- return convertESRCH(unix.PidFDSendSignal(handle, s))
-}
-
-func pidfdWorks() bool {
- return checkPidfdOnce() == nil
-}
-
-var checkPidfdOnce = sync.OnceValue(checkPidfd)
-
-// checkPidfd checks whether all required pidfd-related syscalls work.
-// This consists of pidfd_open and pidfd_send_signal syscalls, and waitid
-// syscall with idtype of P_PIDFD.
-//
-// Reasons for non-working pidfd syscalls include an older kernel and an
-// execution environment in which the above system calls are restricted by
-// seccomp or a similar technology.
-func checkPidfd() error {
- // Get a pidfd of the current process (opening of "/proc/self" won't
- // work for waitid).
- fd, err := unix.PidFDOpen(syscall.Getpid(), 0)
- if err != nil {
- return NewSyscallError("pidfd_open", err)
- }
- defer syscall.Close(int(fd))
-
- // Check waitid(P_PIDFD) works.
- for {
- _, _, err = syscall.Syscall6(syscall.SYS_WAITID, _P_PIDFD, fd, 0, syscall.WEXITED, 0, 0)
- if err != syscall.EINTR {
- break
- }
- }
- // Expect ECHILD from waitid since we're not our own parent.
- if err != syscall.ECHILD {
- return NewSyscallError("pidfd_wait", err)
- }
-
- // Check pidfd_send_signal works (should be able to send 0 to itself).
- if err := unix.PidFDSendSignal(fd, 0); err != nil {
- return NewSyscallError("pidfd_send_signal", err)
- }
-
- return nil
-}
diff --git a/src/os/pidfd_linux_test.go b/src/os/pidfd_linux_test.go
deleted file mode 100644
index f185c3de66..0000000000
--- a/src/os/pidfd_linux_test.go
+++ /dev/null
@@ -1,17 +0,0 @@
-package os_test
-
-import (
- "os"
- "testing"
-)
-
-func TestCheckPidfd(t *testing.T) {
- if err := os.CheckPidfdOnce(); err != nil {
- t.Log("checkPidfd:", err)
- } else {
- t.Log("pidfd syscalls work")
- }
- // TODO: make some reasonable assumptions that pidfd must or must not
- // work in the current test environment (for example, it must work for
- // kernel >= 5.4), and fail if pidfdWorks is not as expected.
-}
diff --git a/src/os/pidfd_other.go b/src/os/pidfd_other.go
deleted file mode 100644
index 1918acbec5..0000000000
--- a/src/os/pidfd_other.go
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright 2023 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-//go:build (unix && !linux) || (js && wasm) || wasip1 || windows
-
-package os
-
-import "syscall"
-
-func ensurePidfd(sysAttr *syscall.SysProcAttr) *syscall.SysProcAttr {
- return sysAttr
-}
-
-func getPidfd(_ *syscall.SysProcAttr) uintptr {
- return unsetHandle
-}
-
-func (p *Process) pidfdRelease() {}
-
-func (_ *Process) pidfdWait() (*ProcessState, error) {
- return nil, syscall.ENOSYS
-}
-
-func (_ *Process) pidfdSendSignal(_ syscall.Signal) error {
- return syscall.ENOSYS
-}