mirror of
https://github.com/golang/go.git
synced 2025-05-08 00:53:07 +00:00
For #69284 Fixes #69402 Change-Id: I6350209302778ba5e44fa03d0b9e680d2b4ec192 Reviewed-on: https://go-review.googlesource.com/c/go/+/611495 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: roger peppe <rogpeppe@gmail.com> Reviewed-by: Tim King <taking@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> (cherry picked from commit 8926ca9c5ec3ea0b51e413e87f737aeb1422ea48) Reviewed-on: https://go-review.googlesource.com/c/go/+/613616 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
// 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.
|
|
|
|
package os_test
|
|
|
|
import (
|
|
"errors"
|
|
"internal/syscall/unix"
|
|
"internal/testenv"
|
|
"os"
|
|
"os/exec"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestFindProcessViaPidfd(t *testing.T) {
|
|
testenv.MustHaveGoBuild(t)
|
|
t.Parallel()
|
|
|
|
if err := os.CheckPidfdOnce(); err != nil {
|
|
// Non-pidfd code paths tested in exec_unix_test.go.
|
|
t.Skipf("skipping: pidfd not available: %v", err)
|
|
}
|
|
|
|
p, err := os.StartProcess(testenv.GoToolPath(t), []string{"go"}, &os.ProcAttr{})
|
|
if err != nil {
|
|
t.Fatalf("starting test process: %v", err)
|
|
}
|
|
p.Wait()
|
|
|
|
// Use pid of a non-existing process.
|
|
proc, err := os.FindProcess(p.Pid)
|
|
// FindProcess should never return errors on Unix.
|
|
if err != nil {
|
|
t.Fatalf("FindProcess: got error %v, want <nil>", err)
|
|
}
|
|
// FindProcess should never return nil Process.
|
|
if proc == nil {
|
|
t.Fatal("FindProcess: got nil, want non-nil")
|
|
}
|
|
if proc.Status() != os.StatusDone {
|
|
t.Fatalf("got process status: %v, want %d", proc.Status(), os.StatusDone)
|
|
}
|
|
|
|
// Check that all Process' public methods work as expected with
|
|
// "done" Process.
|
|
if err := proc.Kill(); err != os.ErrProcessDone {
|
|
t.Errorf("Kill: got %v, want %v", err, os.ErrProcessDone)
|
|
}
|
|
if err := proc.Signal(os.Kill); err != os.ErrProcessDone {
|
|
t.Errorf("Signal: got %v, want %v", err, os.ErrProcessDone)
|
|
}
|
|
if _, err := proc.Wait(); !errors.Is(err, syscall.ECHILD) {
|
|
t.Errorf("Wait: got %v, want %v", err, os.ErrProcessDone)
|
|
}
|
|
// Release never returns errors on Unix.
|
|
if err := proc.Release(); err != nil {
|
|
t.Fatalf("Release: got %v, want <nil>", err)
|
|
}
|
|
}
|
|
|
|
func TestStartProcessWithPidfd(t *testing.T) {
|
|
testenv.MustHaveGoBuild(t)
|
|
t.Parallel()
|
|
|
|
if err := os.CheckPidfdOnce(); err != nil {
|
|
// Non-pidfd code paths tested in exec_unix_test.go.
|
|
t.Skipf("skipping: pidfd not available: %v", err)
|
|
}
|
|
|
|
var pidfd int
|
|
p, err := os.StartProcess(testenv.GoToolPath(t), []string{"go"}, &os.ProcAttr{
|
|
Sys: &syscall.SysProcAttr{
|
|
PidFD: &pidfd,
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("starting test process: %v", err)
|
|
}
|
|
defer syscall.Close(pidfd)
|
|
|
|
if _, err := p.Wait(); err != nil {
|
|
t.Fatalf("Wait: got %v, want <nil>", err)
|
|
}
|
|
|
|
// Check the pidfd is still valid
|
|
err = unix.PidFDSendSignal(uintptr(pidfd), syscall.Signal(0))
|
|
if !errors.Is(err, syscall.ESRCH) {
|
|
t.Errorf("SendSignal: got %v, want %v", err, syscall.ESRCH)
|
|
}
|
|
}
|
|
|
|
// Issue #69284
|
|
func TestPidfdLeak(t *testing.T) {
|
|
testenv.MustHaveExec(t)
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Find the next 10 descriptors.
|
|
// We need to get more than one descriptor in practice;
|
|
// the pidfd winds up not being the next descriptor.
|
|
const count = 10
|
|
want := make([]int, count)
|
|
for i := range count {
|
|
var err error
|
|
want[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Close the descriptors.
|
|
for _, d := range want {
|
|
syscall.Close(d)
|
|
}
|
|
|
|
// Start a process 10 times.
|
|
for range 10 {
|
|
// For testing purposes this has to be an absolute path.
|
|
// Otherwise we will fail finding the executable
|
|
// and won't start a process at all.
|
|
cmd := exec.Command("/noSuchExecutable")
|
|
cmd.Run()
|
|
}
|
|
|
|
// Open the next 10 descriptors again.
|
|
got := make([]int, count)
|
|
for i := range count {
|
|
var err error
|
|
got[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Close the descriptors
|
|
for _, d := range got {
|
|
syscall.Close(d)
|
|
}
|
|
|
|
t.Logf("got %v", got)
|
|
t.Logf("want %v", want)
|
|
|
|
// Allow some slack for runtime epoll descriptors and the like.
|
|
if got[count-1] > want[count-1]+5 {
|
|
t.Errorf("got descriptor %d, want %d", got[count-1], want[count-1])
|
|
}
|
|
}
|